@@ -19,7 +19,7 @@ import XCTest
19
19
20
20
class CertificatePolicyTests : XCTestCase {
21
21
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 ( )
23
23
let policy = TestCertificatePolicy ( trustedRoots: certChain. suffix ( 1 ) )
24
24
25
25
try await policy. validate (
@@ -29,7 +29,7 @@ class CertificatePolicyTests: XCTestCase {
29
29
}
30
30
31
31
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 ( )
33
33
let policy = TestCertificatePolicy ( trustedRoots: certChain. suffix ( 1 ) )
34
34
35
35
try await policy. validate (
@@ -39,7 +39,7 @@ class CertificatePolicyTests: XCTestCase {
39
39
}
40
40
41
41
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 ( )
43
43
// Test root is not trusted
44
44
let policy = TestCertificatePolicy ( trustedRoots: nil )
45
45
@@ -57,7 +57,7 @@ class CertificatePolicyTests: XCTestCase {
57
57
}
58
58
59
59
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 ( )
61
61
let policy = TestCertificatePolicy ( trustedRoots: certChain. suffix ( 1 ) )
62
62
63
63
// Use verify date outside of cert's validity period
@@ -80,18 +80,15 @@ class CertificatePolicyTests: XCTestCase {
80
80
try XCTSkipIf ( true )
81
81
#endif
82
82
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
+ )
95
92
96
93
// Apple root certs are in SwiftPM's default trust store
97
94
let policy = DefaultCertificatePolicy (
@@ -116,18 +113,15 @@ class CertificatePolicyTests: XCTestCase {
116
113
try XCTSkipIf ( true )
117
114
#endif
118
115
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
+ )
131
125
132
126
do {
133
127
// Apple root certs are in SwiftPM's default trust store
@@ -179,18 +173,15 @@ class CertificatePolicyTests: XCTestCase {
179
173
try XCTSkipIf ( true )
180
174
#endif
181
175
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
+ )
194
185
195
186
do {
196
187
// Apple root certs are in SwiftPM's default trust store
@@ -242,18 +233,15 @@ class CertificatePolicyTests: XCTestCase {
242
233
try XCTSkipIf ( true )
243
234
#endif
244
235
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
+ )
257
245
258
246
do {
259
247
// Apple root certs are in SwiftPM's default trust store
@@ -305,18 +293,15 @@ class CertificatePolicyTests: XCTestCase {
305
293
try XCTSkipIf ( true )
306
294
#endif
307
295
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
+ )
320
305
321
306
do {
322
307
// Apple root certs are in SwiftPM's default trust store
@@ -393,18 +378,15 @@ class CertificatePolicyTests: XCTestCase {
393
378
try XCTSkipIf ( true )
394
379
#endif
395
380
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
+ )
408
390
409
391
do {
410
392
// Apple root certs are in SwiftPM's default trust store
@@ -481,18 +463,15 @@ class CertificatePolicyTests: XCTestCase {
481
463
try XCTSkipIf ( true )
482
464
#endif
483
465
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
+ )
496
475
497
476
do {
498
477
// Apple root certs are in SwiftPM's default trust store
@@ -563,46 +542,43 @@ class CertificatePolicyTests: XCTestCase {
563
542
}
564
543
}
565
544
566
- private func readTestRSACertChain( callback : ( Result < [ Certificate ] , Error > ) -> Void ) {
567
- self . readTestCertChain (
545
+ private func readTestRSACertChain( ) async throws -> [ Certificate ] {
546
+ try await self . readTestCertChain (
568
547
paths: { fixturePath in
569
548
[
570
549
fixturePath. appending ( components: " Certificates " , " Test_rsa.cer " ) ,
571
550
fixturePath. appending ( components: " Certificates " , " TestIntermediateCA.cer " ) ,
572
551
fixturePath. appending ( components: " Certificates " , " TestRootCA.cer " ) ,
573
552
]
574
- } ,
575
- callback: callback
553
+ }
576
554
)
577
555
}
578
556
579
- private func readTestECCertChain( callback : ( Result < [ Certificate ] , Error > ) -> Void ) {
580
- self . readTestCertChain (
557
+ private func readTestECCertChain( ) async throws -> [ Certificate ] {
558
+ try await self . readTestCertChain (
581
559
paths: { fixturePath in
582
560
[
583
561
fixturePath. appending ( components: " Certificates " , " Test_ec.cer " ) ,
584
562
fixturePath. appending ( components: " Certificates " , " TestIntermediateCA.cer " ) ,
585
563
fixturePath. appending ( components: " Certificates " , " TestRootCA.cer " ) ,
586
564
]
587
- } ,
588
- callback: callback
565
+ }
589
566
)
590
567
}
591
568
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 )
601
578
}
602
- callback ( . success( certificates) )
579
+ } catch {
580
+ continuation. resume ( throwing: error)
603
581
}
604
- } catch {
605
- callback ( . failure( error) )
606
582
}
607
583
}
608
584
}
0 commit comments