9
9
10
10
class TestURLCredentialStorage : XCTestCase {
11
11
12
- static var allTests : [ ( String , ( TestURLCredentialStorage ) -> ( ) throws -> Void ) ] {
13
- return [
14
- ( " test_storageStartsEmpty " , test_storageStartsEmpty) ,
15
- ( " test_sessionCredentialGetsReturnedForTheRightProtectionSpace " , test_sessionCredentialGetsReturnedForTheRightProtectionSpace) ,
16
- ( " test_sessionCredentialDoesNotGetReturnedForTheWrongProtectionSpace " , test_sessionCredentialDoesNotGetReturnedForTheWrongProtectionSpace) ,
17
- ( " test_sessionCredentialBecomesDefaultForProtectionSpace " , test_sessionCredentialBecomesDefaultForProtectionSpace) ,
18
- ( " test_sessionCredentialGetsReturnedAsDefaultIfSetAsDefaultForSpace " , test_sessionCredentialGetsReturnedAsDefaultIfSetAsDefaultForSpace) ,
19
- ( " test_sessionCredentialGetsReturnedIfSetAsDefaultForSpace " , test_sessionCredentialGetsReturnedIfSetAsDefaultForSpace) ,
20
- ( " test_sessionCredentialDoesNotGetReturnedIfSetAsDefaultForOtherSpace " , test_sessionCredentialDoesNotGetReturnedIfSetAsDefaultForOtherSpace) ,
21
- ( " test_sessionCredentialDoesNotGetReturnedWhenNotAddedAsDefault " , test_sessionCredentialDoesNotGetReturnedWhenNotAddedAsDefault) ,
22
- ( " test_sessionCredentialCanBeRemovedFromSpace " , test_sessionCredentialCanBeRemovedFromSpace) ,
23
- ( " test_sessionDefaultCredentialCanBeRemovedFromSpace " , test_sessionDefaultCredentialCanBeRemovedFromSpace) ,
24
- ( " test_synchronizableCredentialCanBeRemovedWithRightOptions " , test_synchronizableCredentialCanBeRemovedWithRightOptions) ,
25
- ( " test_synchronizableCredentialWillNotBeRemovedWithoutRightOptions " , test_synchronizableCredentialWillNotBeRemovedWithoutRightOptions) ,
26
- ( " test_storageCanRemoveArbitraryCredentialWithoutFailing " , test_storageCanRemoveArbitraryCredentialWithoutFailing) ,
27
- ( " test_storageWillNotSaveCredentialsWithoutPersistence " , test_storageWillNotSaveCredentialsWithoutPersistence) ,
28
- ( " test_storageWillSendNotificationWhenAddingNewCredential " , test_storageWillSendNotificationWhenAddingNewCredential) ,
29
- ( " test_storageWillSendNotificationWhenAddingExistingCredentialToDifferentSpace " , test_storageWillSendNotificationWhenAddingExistingCredentialToDifferentSpace) ,
30
- ( " test_storageWillNotSendNotificationWhenAddingExistingCredential " , test_storageWillNotSendNotificationWhenAddingExistingCredential) ,
31
- ( " test_storageWillSendNotificationWhenAddingNewDefaultCredential " , test_storageWillSendNotificationWhenAddingNewDefaultCredential) ,
32
- ( " test_storageWillNotSendNotificationWhenAddingExistingDefaultCredential " , test_storageWillNotSendNotificationWhenAddingExistingDefaultCredential) ,
33
- ( " test_storageWillSendNotificationWhenAddingDifferentDefaultCredential " , test_storageWillSendNotificationWhenAddingDifferentDefaultCredential) ,
34
- ( " test_storageWillSendNotificationWhenRemovingExistingCredential " , test_storageWillSendNotificationWhenRemovingExistingCredential) ,
35
- ( " test_storageWillNotSendNotificationWhenRemovingExistingCredentialInOtherSpace " , test_storageWillNotSendNotificationWhenRemovingExistingCredentialInOtherSpace) ,
36
- ( " test_storageWillSendNotificationWhenRemovingDefaultNotification " , test_storageWillSendNotificationWhenRemovingDefaultNotification) ,
37
- ( " test_taskBasedGetCredentialsReturnsCredentialsForSpace " , test_taskBasedGetCredentialsReturnsCredentialsForSpace) ,
38
- ( " test_taskBasedSetCredentialStoresGivenCredentials " , test_taskBasedSetCredentialStoresGivenCredentials) ,
39
- ( " test_taskBasedRemoveCredentialDeletesCredentialsFromSpace " , test_taskBasedRemoveCredentialDeletesCredentialsFromSpace) ,
40
- ( " test_taskBasedGetDefaultCredentialReturnsTheDefaultCredential " , test_taskBasedGetDefaultCredentialReturnsTheDefaultCredential) ,
41
- ( " test_taskBasedSetDefaultCredentialStoresTheDefaultCredential " , test_taskBasedSetDefaultCredentialStoresTheDefaultCredential) ,
42
- ]
43
- }
44
-
45
12
func test_storageStartsEmpty( ) {
46
13
let storage = URLCredentialStorage . shared
47
14
XCTAssertEqual ( storage. allCredentials. count, 0 )
@@ -185,6 +152,12 @@ class TestURLCredentialStorage : XCTestCase {
185
152
storage. remove ( credential2, for: space)
186
153
}
187
154
155
+ #if NS_FOUNDATION_NETWORKING_URLCREDENTIALSTORAGE_SYNCHRONIZABLE_ALLOWED
156
+ /*
157
+ swift-corelibs-foundation does not support synchronizable credentials, refusing to save them much like Darwin when logged out of iCloud.
158
+ Thus, these tests cannot succeed — there is never a credential to remove.
159
+ If we ever implement synchronizable credentials, uncomment this.
160
+ */
188
161
func test_synchronizableCredentialCanBeRemovedWithRightOptions( ) {
189
162
let storage = URLCredentialStorage . shared
190
163
@@ -196,7 +169,7 @@ class TestURLCredentialStorage : XCTestCase {
196
169
storage. remove ( credential, for: space, options: [ NSURLCredentialStorageRemoveSynchronizableCredentials: NSNumber ( value: true ) ] )
197
170
}
198
171
}
199
-
172
+
200
173
func test_synchronizableCredentialWillNotBeRemovedWithoutRightOptions( ) {
201
174
let storage = URLCredentialStorage . shared
202
175
@@ -223,7 +196,8 @@ class TestURLCredentialStorage : XCTestCase {
223
196
224
197
storage. remove ( credential, for: space, options: [ NSURLCredentialStorageRemoveSynchronizableCredentials: NSNumber ( value: true ) ] )
225
198
}
226
-
199
+ #endif
200
+
227
201
func test_storageCanRemoveArbitraryCredentialWithoutFailing( ) {
228
202
let storage = URLCredentialStorage . shared
229
203
@@ -481,13 +455,20 @@ class TestURLCredentialStorage : XCTestCase {
481
455
482
456
storage. set ( credential, for: space, task: task)
483
457
484
- guard let credentials = storage. credentials ( for: space) ,
485
- let recovered = credentials [ try credential. user. unwrapped ( ) ] else {
486
- XCTFail ( " Credential not found in storage " )
487
- return
458
+ let expectation = self . expectation ( description: " Done " )
459
+
460
+ storage. getCredentials ( for: space, task: task) { credentials in
461
+ guard let credentials = credentials,
462
+ let user = credential. user,
463
+ let recovered = credentials [ user] else {
464
+ XCTFail ( " Credential not found in storage " )
465
+ return
466
+ }
467
+ XCTAssertEqual ( recovered, credential)
468
+ expectation. fulfill ( )
488
469
}
489
- XCTAssertEqual ( recovered, credential)
490
470
471
+ waitForExpectations ( timeout: 10 )
491
472
storage. remove ( credential, for: space)
492
473
}
493
474
@@ -546,4 +527,46 @@ class TestURLCredentialStorage : XCTestCase {
546
527
547
528
storage. remove ( credential, for: space)
548
529
}
530
+
531
+
532
+ static var allTests : [ ( String , ( TestURLCredentialStorage ) -> ( ) throws -> Void ) ] {
533
+ var tests : [ ( String , ( TestURLCredentialStorage ) -> ( ) throws -> Void ) ] = [
534
+ ( " test_storageStartsEmpty " , test_storageStartsEmpty) ,
535
+ ( " test_sessionCredentialGetsReturnedForTheRightProtectionSpace " , test_sessionCredentialGetsReturnedForTheRightProtectionSpace) ,
536
+ ( " test_sessionCredentialDoesNotGetReturnedForTheWrongProtectionSpace " , test_sessionCredentialDoesNotGetReturnedForTheWrongProtectionSpace) ,
537
+ ( " test_sessionCredentialBecomesDefaultForProtectionSpace " , test_sessionCredentialBecomesDefaultForProtectionSpace) ,
538
+ ( " test_sessionCredentialGetsReturnedAsDefaultIfSetAsDefaultForSpace " , test_sessionCredentialGetsReturnedAsDefaultIfSetAsDefaultForSpace) ,
539
+ ( " test_sessionCredentialGetsReturnedIfSetAsDefaultForSpace " , test_sessionCredentialGetsReturnedIfSetAsDefaultForSpace) ,
540
+ ( " test_sessionCredentialDoesNotGetReturnedIfSetAsDefaultForOtherSpace " , test_sessionCredentialDoesNotGetReturnedIfSetAsDefaultForOtherSpace) ,
541
+ ( " test_sessionCredentialDoesNotGetReturnedWhenNotAddedAsDefault " , test_sessionCredentialDoesNotGetReturnedWhenNotAddedAsDefault) ,
542
+ ( " test_sessionCredentialCanBeRemovedFromSpace " , test_sessionCredentialCanBeRemovedFromSpace) ,
543
+ ( " test_sessionDefaultCredentialCanBeRemovedFromSpace " , test_sessionDefaultCredentialCanBeRemovedFromSpace) ,
544
+ ( " test_storageCanRemoveArbitraryCredentialWithoutFailing " , test_storageCanRemoveArbitraryCredentialWithoutFailing) ,
545
+ ( " test_storageWillNotSaveCredentialsWithoutPersistence " , test_storageWillNotSaveCredentialsWithoutPersistence) ,
546
+ ( " test_storageWillSendNotificationWhenAddingNewCredential " , test_storageWillSendNotificationWhenAddingNewCredential) ,
547
+ ( " test_storageWillSendNotificationWhenAddingExistingCredentialToDifferentSpace " , test_storageWillSendNotificationWhenAddingExistingCredentialToDifferentSpace) ,
548
+ ( " test_storageWillNotSendNotificationWhenAddingExistingCredential " , test_storageWillNotSendNotificationWhenAddingExistingCredential) ,
549
+ ( " test_storageWillSendNotificationWhenAddingNewDefaultCredential " , test_storageWillSendNotificationWhenAddingNewDefaultCredential) ,
550
+ ( " test_storageWillNotSendNotificationWhenAddingExistingDefaultCredential " , test_storageWillNotSendNotificationWhenAddingExistingDefaultCredential) ,
551
+ ( " test_storageWillSendNotificationWhenAddingDifferentDefaultCredential " , test_storageWillSendNotificationWhenAddingDifferentDefaultCredential) ,
552
+ ( " test_storageWillSendNotificationWhenRemovingExistingCredential " , test_storageWillSendNotificationWhenRemovingExistingCredential) ,
553
+ ( " test_storageWillNotSendNotificationWhenRemovingExistingCredentialInOtherSpace " , test_storageWillNotSendNotificationWhenRemovingExistingCredentialInOtherSpace) ,
554
+ ( " test_storageWillSendNotificationWhenRemovingDefaultNotification " , test_storageWillSendNotificationWhenRemovingDefaultNotification) ,
555
+ ( " test_taskBasedGetCredentialsReturnsCredentialsForSpace " , test_taskBasedGetCredentialsReturnsCredentialsForSpace) ,
556
+ ( " test_taskBasedSetCredentialStoresGivenCredentials " , test_taskBasedSetCredentialStoresGivenCredentials) ,
557
+ ( " test_taskBasedRemoveCredentialDeletesCredentialsFromSpace " , test_taskBasedRemoveCredentialDeletesCredentialsFromSpace) ,
558
+ ( " test_taskBasedGetDefaultCredentialReturnsTheDefaultCredential " , test_taskBasedGetDefaultCredentialReturnsTheDefaultCredential) ,
559
+ ( " test_taskBasedSetDefaultCredentialStoresTheDefaultCredential " , test_taskBasedSetDefaultCredentialStoresTheDefaultCredential) ,
560
+ ]
561
+
562
+ #if NS_FOUNDATION_NETWORKING_URLCREDENTIALSTORAGE_SYNCHRONIZABLE_ALLOWED
563
+ // See these test methods for why they aren't added by default.
564
+ tests. append ( contentsOf: [
565
+ ( " test_synchronizableCredentialCanBeRemovedWithRightOptions " , test_synchronizableCredentialCanBeRemovedWithRightOptions) ,
566
+ ( " test_synchronizableCredentialWillNotBeRemovedWithoutRightOptions " , test_synchronizableCredentialWillNotBeRemovedWithoutRightOptions) ,
567
+ ] )
568
+ #endif
569
+
570
+ return tests
571
+ }
549
572
}
0 commit comments