Skip to content

Commit e53657e

Browse files
committed
修改測試卡住的問題
1 parent c14e273 commit e53657e

4 files changed

Lines changed: 44 additions & 37 deletions

File tree

.github/workflows/main.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ jobs:
1616
run: sudo xcode-select -switch /Applications/Xcode_16.4.app && /usr/bin/xcodebuild -version
1717

1818
- name: Build for iOS
19-
uses: sersoft-gmbh/xcodebuild-action@v1
19+
uses: mxcl/xcodebuild@v3
2020
with:
21-
project: JsonDecodeProtection.xcodeproj
21+
platform: iOS
22+
xcode: ^16.4
2223
scheme: JsonDecodeProtection
23-
destination: platform=iOS Simulator,name=iPhone 16,OS=18.6
24-
action: build
24+
action: build
2525

2626
- name: Runing tests for iOS
27-
uses: sersoft-gmbh/xcodebuild-action@v1
27+
uses: mxcl/xcodebuild@v3
2828
with:
29-
project: JsonDecodeProtection.xcodeproj
29+
platform: iOS
30+
xcode: ^16.4
3031
scheme: JsonDecodeProtection
31-
destination: platform=iOS Simulator,name=iPhone 16,OS=18.6
3232
action: test
3333

JsonDecodeProtectionTests/AESDecoderTest.swift

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ struct AESObject: Decodable
1414
@AESDecoder(adopter: AESAdopting.self)
1515
private(set)
1616
var url: String?
17-
}
18-
19-
struct AESObject2: Decodable
20-
{
17+
2118
@AESDecoder(adopter: AESAdopting.self)
2219
private(set)
2320
var urls: Array<String>?
@@ -44,59 +41,69 @@ struct AESAdopting: AESAdopter
4441
final
4542
class AESDecoderTest: XCTestCase
4643
{
47-
override func setUpWithError() throws
44+
var object: AESObject?
45+
46+
override
47+
func setUpWithError() throws
4848
{
4949
// Put setup code here. This method is called before the invocation of each test method in the class.
5050

5151
// Arrange
52+
let jsonString: String = """
53+
{
54+
"url": "0NhMzVQIsjShyNnck3huFVjVCcku2a+iAQVfY3CDrUw=",
55+
"urls": [
56+
"0NhMzVQIsjShyNnck3huFVjVCcku2a+iAQVfY3CDrUw=",
57+
"0NhMzVQIsjShyNnck3huFVjVCcku2a+iAQVfY3CDrUw="
58+
]
59+
}
60+
"""
61+
let jsonData: Data = jsonString.data(using: .utf8)!
62+
let jsonDecoder = JSONDecoder()
5263

5364
// Act
65+
let object = try jsonDecoder.decode(AESObject.self, from: jsonData)
66+
self.object = object
5467

5568
// Assert
5669
}
5770

71+
override func tearDown() {
72+
super.tearDown()
73+
// 確保每個測試案例結束後清理狀態
74+
}
75+
5876
func testAESDecoderSuccess() throws
5977
{
6078
// Arrange
61-
let jsonString: String = """
62-
{
63-
"url": "0NhMzVQIsjShyNnck3huFVjVCcku2a+iAQVfY3CDrUw="
79+
guard let object: AESObject = self.object else {
80+
81+
XCTFail("Object should not be nil")
82+
return
6483
}
65-
"""
66-
let jsonData: Data = jsonString.data(using: .utf8)!
67-
let jsonDecoder = JSONDecoder()
6884

6985
// Act
70-
let object = try jsonDecoder.decode(AESObject.self, from: jsonData)
71-
72-
// Assert
7386
let url: String? = object.url
7487
let expect: String = "https://www.apple.com"
7588

89+
// Assert
7690
XCTAssertEqual(url, expect)
7791
}
7892

7993
func testDecoderStringArraySuccess() throws
8094
{
8195
// Arrange
82-
let jsonString: String = """
83-
{
84-
"urls": [
85-
"0NhMzVQIsjShyNnck3huFVjVCcku2a+iAQVfY3CDrUw=",
86-
"0NhMzVQIsjShyNnck3huFVjVCcku2a+iAQVfY3CDrUw="
87-
]
96+
guard let object: AESObject = self.object else {
97+
98+
XCTFail("Object should not be nil")
99+
return
88100
}
89-
"""
90-
let jsonData: Data = jsonString.data(using: .utf8)!
91-
let jsonDecoder = JSONDecoder()
92101

93102
// Act
94-
let object = try jsonDecoder.decode(AESObject2.self, from: jsonData)
95-
96-
// Assert
97103
let urls: Array<String>? = object.urls
98104
let expect: Array<String> = ["https://www.apple.com", "https://www.apple.com"]
99105

106+
// Assert
100107
XCTAssertEqual(urls, expect)
101108
}
102109
}

SourceCode/JsonProtection/AESDecoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ extension AESDecoder
8181

8282
// MARK: - Conform Protocols -
8383

84-
extension AESDecoder: Decodable
84+
extension AESDecoder: Decodable
8585
{
8686
public
8787
init(from decoder: Decoder) throws
@@ -99,7 +99,7 @@ extension AESDecoder: Decodable
9999

100100
if let encryptedArray = encryptedValue as? Array<String> {
101101

102-
let decodeValue: Array<String> = try self.decodeArray(encryptedArray)
102+
let decodeValue: Array<String>? = try? self.decodeArray(encryptedArray)
103103
self.wrappedValue = decodeValue as? Value
104104

105105
return

SourceCode/JsonProtection/DTAES.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ extension DTAES
181181
}
182182

183183
let bufferSize: Int = contentData.count + kCCBlockSizeAES128
184-
var buffer: Array<UInt8> = []
184+
var buffer: Array<UInt8> = Array(repeating: 0, count: bufferSize)
185185

186186
var numberBytesCrypto: Int = 0
187187

188188
let cryptStatus: CCCryptorStatus = CCCrypt(self.ccOperation,
189189
algorithm,
190-
CCOptions(currentOptions.rawValue), // 使用局部變數
190+
CCOptions(currentOptions.rawValue),
191191
key,
192192
key.count,
193193
iv,

0 commit comments

Comments
 (0)