@@ -319,16 +319,46 @@ class CertificatePolicyTests: XCTestCase {
319
319
320
320
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
321
321
// The Apple root certs come preinstalled on Apple platforms and they are automatically trusted
322
- let policy = DefaultCertificatePolicy ( trustedRootCertsDir: nil , additionalTrustedRootCerts: nil , expectedSubjectUserID: expectedSubjectUserID,
323
- callbackQueue: DispatchQueue . global ( ) , diagnosticsEngine: DiagnosticsEngine ( ) )
324
- XCTAssertNoThrow ( try tsc_await { callback in policy. validate ( certChain: certChain, callback: callback) } )
322
+
323
+ // Subject user ID matches
324
+ do {
325
+ let policy = DefaultCertificatePolicy ( trustedRootCertsDir: nil , additionalTrustedRootCerts: nil , expectedSubjectUserID: expectedSubjectUserID,
326
+ callbackQueue: DispatchQueue . global ( ) , diagnosticsEngine: DiagnosticsEngine ( ) )
327
+ XCTAssertNoThrow ( try tsc_await { callback in policy. validate ( certChain: certChain, callback: callback) } )
328
+ }
329
+ // Subject user ID does not match
330
+ do {
331
+ let mismatchSubjectUserID = " \( expectedSubjectUserID) -2 "
332
+ let policy = DefaultCertificatePolicy ( trustedRootCertsDir: nil , additionalTrustedRootCerts: nil , expectedSubjectUserID: mismatchSubjectUserID,
333
+ callbackQueue: DispatchQueue . global ( ) , diagnosticsEngine: DiagnosticsEngine ( ) )
334
+ XCTAssertThrowsError ( try tsc_await { callback in policy. validate ( certChain: certChain, callback: callback) } ) { error in
335
+ guard CertificatePolicyError . subjectUserIDMismatch == error as? CertificatePolicyError else {
336
+ return XCTFail ( " Expected CertificatePolicyError.subjectUserIDMismatch " )
337
+ }
338
+ }
339
+ }
325
340
#else
326
341
// On other platforms we have to specify `trustedRootCertsDir` so the Apple root cert is trusted
327
342
try withTemporaryDirectory { tmp in
328
343
try localFileSystem. copy ( from: rootCAPath, to: tmp. appending ( components: " AppleIncRoot.cer " ) )
329
- let policy = DefaultCertificatePolicy ( trustedRootCertsDir: tmp. asURL, additionalTrustedRootCerts: nil , expectedSubjectUserID: expectedSubjectUserID,
330
- callbackQueue: DispatchQueue . global ( ) , diagnosticsEngine: DiagnosticsEngine ( ) )
331
- XCTAssertNoThrow ( try tsc_await { callback in policy. validate ( certChain: certChain, callback: callback) } )
344
+
345
+ // Subject user ID matches
346
+ do {
347
+ let policy = DefaultCertificatePolicy ( trustedRootCertsDir: tmp. asURL, additionalTrustedRootCerts: nil , expectedSubjectUserID: expectedSubjectUserID,
348
+ callbackQueue: DispatchQueue . global ( ) , diagnosticsEngine: DiagnosticsEngine ( ) )
349
+ XCTAssertNoThrow ( try tsc_await { callback in policy. validate ( certChain: certChain, callback: callback) } )
350
+ }
351
+ // Subject user ID does not match
352
+ do {
353
+ let mismatchSubjectUserID = " \( expectedSubjectUserID) -2 "
354
+ let policy = DefaultCertificatePolicy ( trustedRootCertsDir: tmp. asURL, additionalTrustedRootCerts: nil , expectedSubjectUserID: mismatchSubjectUserID,
355
+ callbackQueue: DispatchQueue . global ( ) , diagnosticsEngine: DiagnosticsEngine ( ) )
356
+ XCTAssertThrowsError ( try tsc_await { callback in policy. validate ( certChain: certChain, callback: callback) } ) { error in
357
+ guard CertificatePolicyError . subjectUserIDMismatch == error as? CertificatePolicyError else {
358
+ return XCTFail ( " Expected CertificatePolicyError.subjectUserIDMismatch " )
359
+ }
360
+ }
361
+ }
332
362
}
333
363
#endif
334
364
}
@@ -359,16 +389,46 @@ class CertificatePolicyTests: XCTestCase {
359
389
360
390
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
361
391
// The Apple root certs come preinstalled on Apple platforms and they are automatically trusted
362
- let policy = AppleDeveloperCertificatePolicy ( trustedRootCertsDir: nil , additionalTrustedRootCerts: nil , expectedSubjectUserID: expectedSubjectUserID,
363
- callbackQueue: DispatchQueue . global ( ) , diagnosticsEngine: DiagnosticsEngine ( ) )
364
- XCTAssertNoThrow ( try tsc_await { callback in policy. validate ( certChain: certChain, callback: callback) } )
392
+
393
+ // Subject user ID matches
394
+ do {
395
+ let policy = AppleDeveloperCertificatePolicy ( trustedRootCertsDir: nil , additionalTrustedRootCerts: nil , expectedSubjectUserID: expectedSubjectUserID,
396
+ callbackQueue: DispatchQueue . global ( ) , diagnosticsEngine: DiagnosticsEngine ( ) )
397
+ XCTAssertNoThrow ( try tsc_await { callback in policy. validate ( certChain: certChain, callback: callback) } )
398
+ }
399
+ // Subject user ID does not match
400
+ do {
401
+ let mismatchSubjectUserID = " \( expectedSubjectUserID) -2 "
402
+ let policy = AppleDeveloperCertificatePolicy ( trustedRootCertsDir: nil , additionalTrustedRootCerts: nil , expectedSubjectUserID: mismatchSubjectUserID,
403
+ callbackQueue: DispatchQueue . global ( ) , diagnosticsEngine: DiagnosticsEngine ( ) )
404
+ XCTAssertThrowsError ( try tsc_await { callback in policy. validate ( certChain: certChain, callback: callback) } ) { error in
405
+ guard CertificatePolicyError . subjectUserIDMismatch == error as? CertificatePolicyError else {
406
+ return XCTFail ( " Expected CertificatePolicyError.subjectUserIDMismatch " )
407
+ }
408
+ }
409
+ }
365
410
#else
366
411
// On other platforms we have to specify `trustedRootCertsDir` so the Apple root cert is trusted
367
412
try withTemporaryDirectory { tmp in
368
413
try localFileSystem. copy ( from: rootCAPath, to: tmp. appending ( components: " AppleIncRoot.cer " ) )
369
- let policy = AppleDeveloperCertificatePolicy ( trustedRootCertsDir: tmp. asURL, additionalTrustedRootCerts: nil , expectedSubjectUserID: expectedSubjectUserID,
370
- callbackQueue: DispatchQueue . global ( ) , diagnosticsEngine: DiagnosticsEngine ( ) )
371
- XCTAssertNoThrow ( try tsc_await { callback in policy. validate ( certChain: certChain, callback: callback) } )
414
+
415
+ // Subject user ID matches
416
+ do {
417
+ let policy = AppleDeveloperCertificatePolicy ( trustedRootCertsDir: tmp. asURL, additionalTrustedRootCerts: nil , expectedSubjectUserID: expectedSubjectUserID,
418
+ callbackQueue: DispatchQueue . global ( ) , diagnosticsEngine: DiagnosticsEngine ( ) )
419
+ XCTAssertNoThrow ( try tsc_await { callback in policy. validate ( certChain: certChain, callback: callback) } )
420
+ }
421
+ // Subject user ID does not match
422
+ do {
423
+ let mismatchSubjectUserID = " \( expectedSubjectUserID) -2 "
424
+ let policy = AppleDeveloperCertificatePolicy ( trustedRootCertsDir: tmp. asURL, additionalTrustedRootCerts: nil , expectedSubjectUserID: mismatchSubjectUserID,
425
+ callbackQueue: DispatchQueue . global ( ) , diagnosticsEngine: DiagnosticsEngine ( ) )
426
+ XCTAssertThrowsError ( try tsc_await { callback in policy. validate ( certChain: certChain, callback: callback) } ) { error in
427
+ guard CertificatePolicyError . subjectUserIDMismatch == error as? CertificatePolicyError else {
428
+ return XCTFail ( " Expected CertificatePolicyError.subjectUserIDMismatch " )
429
+ }
430
+ }
431
+ }
372
432
}
373
433
#endif
374
434
}
0 commit comments