Skip to content

Commit 813bd98

Browse files
committed
Make test fixture methods async
1 parent 073d7df commit 813bd98

File tree

3 files changed

+273
-331
lines changed

3 files changed

+273
-331
lines changed

Tests/PackageCollectionsSigningTests/CertificatePolicyTests.swift

Lines changed: 84 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import XCTest
1919

2020
class CertificatePolicyTests: XCTestCase {
2121
func test_RSA_validate_happyCase() async throws {
22-
let certChain = try temp_await { callback in self.readTestRSACertChain(callback: callback) }
22+
let certChain = try await self.readTestRSACertChain()
2323
let policy = TestCertificatePolicy(trustedRoots: certChain.suffix(1))
2424

2525
try await policy.validate(
@@ -29,7 +29,7 @@ class CertificatePolicyTests: XCTestCase {
2929
}
3030

3131
func test_EC_validate_happyCase() async throws {
32-
let certChain = try temp_await { callback in self.readTestECCertChain(callback: callback) }
32+
let certChain = try await self.readTestECCertChain()
3333
let policy = TestCertificatePolicy(trustedRoots: certChain.suffix(1))
3434

3535
try await policy.validate(
@@ -39,7 +39,7 @@ class CertificatePolicyTests: XCTestCase {
3939
}
4040

4141
func test_validate_untrustedRoot() async throws {
42-
let certChain = try temp_await { callback in self.readTestRSACertChain(callback: callback) }
42+
let certChain = try await self.readTestRSACertChain()
4343
// Test root is not trusted
4444
let policy = TestCertificatePolicy(trustedRoots: nil)
4545

@@ -57,7 +57,7 @@ class CertificatePolicyTests: XCTestCase {
5757
}
5858

5959
func test_validate_expiredCert() async throws {
60-
let certChain = try temp_await { callback in self.readTestRSACertChain(callback: callback) }
60+
let certChain = try await self.readTestRSACertChain()
6161
let policy = TestCertificatePolicy(trustedRoots: certChain.suffix(1))
6262

6363
// Use verify date outside of cert's validity period
@@ -80,18 +80,15 @@ class CertificatePolicyTests: XCTestCase {
8080
try XCTSkipIf(true)
8181
#endif
8282

83-
let certChain = try temp_await { callback in
84-
self.readTestCertChain(
85-
paths: { fixturePath in
86-
[
87-
fixturePath.appending(components: "Certificates", "development-revoked.cer"),
88-
fixturePath.appending(components: "Certificates", "AppleWWDRCAG3.cer"),
89-
fixturePath.appending(components: "Certificates", "AppleIncRoot.cer"),
90-
]
91-
},
92-
callback: callback
93-
)
94-
}
83+
let certChain = try await self.readTestCertChain(
84+
paths: { fixturePath in
85+
[
86+
fixturePath.appending(components: "Certificates", "development-revoked.cer"),
87+
fixturePath.appending(components: "Certificates", "AppleWWDRCAG3.cer"),
88+
fixturePath.appending(components: "Certificates", "AppleIncRoot.cer"),
89+
]
90+
}
91+
)
9592

9693
// Apple root certs are in SwiftPM's default trust store
9794
let policy = DefaultCertificatePolicy(
@@ -116,18 +113,15 @@ class CertificatePolicyTests: XCTestCase {
116113
try XCTSkipIf(true)
117114
#endif
118115

119-
let certChain = try temp_await { callback in
120-
self.readTestCertChain(
121-
paths: { fixturePath in
122-
[
123-
fixturePath.appending(components: "Certificates", "development.cer"),
124-
fixturePath.appending(components: "Certificates", "AppleWWDRCAG3.cer"),
125-
fixturePath.appending(components: "Certificates", "AppleIncRoot.cer"),
126-
]
127-
},
128-
callback: callback
129-
)
130-
}
116+
let certChain = try await self.readTestCertChain(
117+
paths: { fixturePath in
118+
[
119+
fixturePath.appending(components: "Certificates", "development.cer"),
120+
fixturePath.appending(components: "Certificates", "AppleWWDRCAG3.cer"),
121+
fixturePath.appending(components: "Certificates", "AppleIncRoot.cer"),
122+
]
123+
}
124+
)
131125

132126
do {
133127
// Apple root certs are in SwiftPM's default trust store
@@ -179,18 +173,15 @@ class CertificatePolicyTests: XCTestCase {
179173
try XCTSkipIf(true)
180174
#endif
181175

182-
let certChain = try temp_await { callback in
183-
self.readTestCertChain(
184-
paths: { fixturePath in
185-
[
186-
fixturePath.appending(components: "Certificates", "swift_package_collection.cer"),
187-
fixturePath.appending(components: "Certificates", "AppleWWDRCAG3.cer"),
188-
fixturePath.appending(components: "Certificates", "AppleIncRoot.cer"),
189-
]
190-
},
191-
callback: callback
192-
)
193-
}
176+
let certChain = try await self.readTestCertChain(
177+
paths: { fixturePath in
178+
[
179+
fixturePath.appending(components: "Certificates", "swift_package_collection.cer"),
180+
fixturePath.appending(components: "Certificates", "AppleWWDRCAG3.cer"),
181+
fixturePath.appending(components: "Certificates", "AppleIncRoot.cer"),
182+
]
183+
}
184+
)
194185

195186
do {
196187
// Apple root certs are in SwiftPM's default trust store
@@ -242,18 +233,15 @@ class CertificatePolicyTests: XCTestCase {
242233
try XCTSkipIf(true)
243234
#endif
244235

245-
let certChain = try temp_await { callback in
246-
self.readTestCertChain(
247-
paths: { fixturePath in
248-
[
249-
fixturePath.appending(components: "Certificates", "swift_package.cer"),
250-
fixturePath.appending(components: "Certificates", "AppleWWDRCAG6.cer"),
251-
fixturePath.appending(components: "Certificates", "AppleRootCAG3.cer"),
252-
]
253-
},
254-
callback: callback
255-
)
256-
}
236+
let certChain = try await self.readTestCertChain(
237+
paths: { fixturePath in
238+
[
239+
fixturePath.appending(components: "Certificates", "swift_package.cer"),
240+
fixturePath.appending(components: "Certificates", "AppleWWDRCAG6.cer"),
241+
fixturePath.appending(components: "Certificates", "AppleRootCAG3.cer"),
242+
]
243+
}
244+
)
257245

258246
do {
259247
// Apple root certs are in SwiftPM's default trust store
@@ -305,18 +293,15 @@ class CertificatePolicyTests: XCTestCase {
305293
try XCTSkipIf(true)
306294
#endif
307295

308-
let certChain = try temp_await { callback in
309-
self.readTestCertChain(
310-
paths: { fixturePath in
311-
[
312-
fixturePath.appending(components: "Certificates", "development.cer"),
313-
fixturePath.appending(components: "Certificates", "AppleWWDRCAG3.cer"),
314-
fixturePath.appending(components: "Certificates", "AppleIncRoot.cer"),
315-
]
316-
},
317-
callback: callback
318-
)
319-
}
296+
let certChain = try await self.readTestCertChain(
297+
paths: { fixturePath in
298+
[
299+
fixturePath.appending(components: "Certificates", "development.cer"),
300+
fixturePath.appending(components: "Certificates", "AppleWWDRCAG3.cer"),
301+
fixturePath.appending(components: "Certificates", "AppleIncRoot.cer"),
302+
]
303+
}
304+
)
320305

321306
do {
322307
// Apple root certs are in SwiftPM's default trust store
@@ -393,18 +378,15 @@ class CertificatePolicyTests: XCTestCase {
393378
try XCTSkipIf(true)
394379
#endif
395380

396-
let certChain = try temp_await { callback in
397-
self.readTestCertChain(
398-
paths: { fixturePath in
399-
[
400-
fixturePath.appending(components: "Certificates", "swift_package_collection.cer"),
401-
fixturePath.appending(components: "Certificates", "AppleWWDRCAG3.cer"),
402-
fixturePath.appending(components: "Certificates", "AppleIncRoot.cer"),
403-
]
404-
},
405-
callback: callback
406-
)
407-
}
381+
let certChain = try await self.readTestCertChain(
382+
paths: { fixturePath in
383+
[
384+
fixturePath.appending(components: "Certificates", "swift_package_collection.cer"),
385+
fixturePath.appending(components: "Certificates", "AppleWWDRCAG3.cer"),
386+
fixturePath.appending(components: "Certificates", "AppleIncRoot.cer"),
387+
]
388+
}
389+
)
408390

409391
do {
410392
// Apple root certs are in SwiftPM's default trust store
@@ -481,18 +463,15 @@ class CertificatePolicyTests: XCTestCase {
481463
try XCTSkipIf(true)
482464
#endif
483465

484-
let certChain = try temp_await { callback in
485-
self.readTestCertChain(
486-
paths: { fixturePath in
487-
[
488-
fixturePath.appending(components: "Certificates", "swift_package.cer"),
489-
fixturePath.appending(components: "Certificates", "AppleWWDRCAG6.cer"),
490-
fixturePath.appending(components: "Certificates", "AppleRootCAG3.cer"),
491-
]
492-
},
493-
callback: callback
494-
)
495-
}
466+
let certChain = try await self.readTestCertChain(
467+
paths: { fixturePath in
468+
[
469+
fixturePath.appending(components: "Certificates", "swift_package.cer"),
470+
fixturePath.appending(components: "Certificates", "AppleWWDRCAG6.cer"),
471+
fixturePath.appending(components: "Certificates", "AppleRootCAG3.cer"),
472+
]
473+
}
474+
)
496475

497476
do {
498477
// Apple root certs are in SwiftPM's default trust store
@@ -563,46 +542,43 @@ class CertificatePolicyTests: XCTestCase {
563542
}
564543
}
565544

566-
private func readTestRSACertChain(callback: (Result<[Certificate], Error>) -> Void) {
567-
self.readTestCertChain(
545+
private func readTestRSACertChain() async throws -> [Certificate] {
546+
try await self.readTestCertChain(
568547
paths: { fixturePath in
569548
[
570549
fixturePath.appending(components: "Certificates", "Test_rsa.cer"),
571550
fixturePath.appending(components: "Certificates", "TestIntermediateCA.cer"),
572551
fixturePath.appending(components: "Certificates", "TestRootCA.cer"),
573552
]
574-
},
575-
callback: callback
553+
}
576554
)
577555
}
578556

579-
private func readTestECCertChain(callback: (Result<[Certificate], Error>) -> Void) {
580-
self.readTestCertChain(
557+
private func readTestECCertChain() async throws -> [Certificate] {
558+
try await self.readTestCertChain(
581559
paths: { fixturePath in
582560
[
583561
fixturePath.appending(components: "Certificates", "Test_ec.cer"),
584562
fixturePath.appending(components: "Certificates", "TestIntermediateCA.cer"),
585563
fixturePath.appending(components: "Certificates", "TestRootCA.cer"),
586564
]
587-
},
588-
callback: callback
565+
}
589566
)
590567
}
591568

592-
private func readTestCertChain(
593-
paths: (AbsolutePath) -> [AbsolutePath],
594-
callback: (Result<[Certificate], Error>) -> Void
595-
) {
596-
do {
597-
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
598-
let certPaths = paths(fixturePath)
599-
let certificates = try certPaths.map { certPath in
600-
try Certificate(derEncoded: try localFileSystem.readFileContents(certPath).contents)
569+
private func readTestCertChain(paths: (AbsolutePath) -> [AbsolutePath]) async throws -> [Certificate] {
570+
try await withCheckedThrowingContinuation { continuation in
571+
do {
572+
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
573+
let certPaths = paths(fixturePath)
574+
let certificates = try certPaths.map { certPath in
575+
try Certificate(derEncoded: try localFileSystem.readFileContents(certPath).contents)
576+
}
577+
continuation.resume(returning: certificates)
601578
}
602-
callback(.success(certificates))
579+
} catch {
580+
continuation.resume(throwing: error)
603581
}
604-
} catch {
605-
callback(.failure(error))
606582
}
607583
}
608584
}

0 commit comments

Comments
 (0)