Skip to content

Commit 8a6c1b6

Browse files
maksymmalyhinpaulb777
authored andcommitted
IID tests improvements (#3067)
* Use bridged __weak reference instead CFRetain/CFRelease * Use SecKeyRef instead of FIRInstanceIDKeyPair for the memory management test * Use SecKeyGeneratePair instead of SecKeyCreateRandomKey available since iOS 10 only * FIRInstanceIDKeyPairStoreTest wait until all keychain operations finished before finishing a test * FIRInstanceIDTokenManagerTest: cleanup in tearDown; fix expectation to produce an error nor a crash. * Use properties instead of ivars FIRInstanceIDTokenManagerTest * FIRInstanceIDTokenManager fix OCMPartialMock() usage.
1 parent 56c3614 commit 8a6c1b6

File tree

5 files changed

+146
-82
lines changed

5 files changed

+146
-82
lines changed

Example/InstanceID/Tests/FIRInstanceIDKeyPairMigrationTest.m

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ - (void)tearDown {
7070
[super tearDown];
7171
NSError *error = nil;
7272
[self.keyPairStore removeKeyPairCreationTimePlistWithError:&error];
73+
74+
// TODO: Real FIRInstanceIDKeychain should not be used for the tests, it should be mocked instead.
75+
// Drain Keychain private queue before exiting.
76+
[[FIRInstanceIDKeychain sharedInstance] itemWithQuery:@{}];
7377
}
7478

7579
- (void)testMigrationDataIfLegacyKeyPairsNotExist {
@@ -139,33 +143,49 @@ - (void)testMigrationIfLegacyKeyPairsExist {
139143
[self waitForExpectationsWithTimeout:1 handler:nil];
140144
}
141145

142-
// Disabling test for now. We need to find a flake free way to insure the publicKeyRef is retained.
143-
#ifdef DISABLED
144146
- (void)testUpdateKeyRefWithTagRetainsAndReleasesKeyRef {
145-
SecKeyRef publicKeyRef;
147+
__weak id weakKeyRef;
146148

149+
// Use a local autorelease pool to make sure any autorelease objects allocated will be released.
147150
@autoreleasepool {
148-
NSString *legacyPublicKeyTag =
149-
FIRInstanceIDLegacyPublicTagWithSubtype(kFIRInstanceIDKeyPairSubType);
150-
NSString *legacyPrivateKeyTag =
151-
FIRInstanceIDLegacyPrivateTagWithSubtype(kFIRInstanceIDKeyPairSubType);
152-
FIRInstanceIDKeyPair *keyPair =
153-
[[FIRInstanceIDKeychain sharedInstance] generateKeyPairWithPrivateTag:legacyPrivateKeyTag
154-
publicTag:legacyPublicKeyTag];
155-
XCTAssertTrue([keyPair isValid]);
156-
157-
publicKeyRef = keyPair.publicKey;
158-
151+
SecKeyRef keyRef = [self generateKeyRef];
152+
weakKeyRef = (__bridge id)(keyRef);
159153
XCTestExpectation *completionExpectation =
160154
[self expectationWithDescription:@"completionExpectation"];
161-
[self.keyPairStore updateKeyRef:keyPair.publicKey
155+
[self.keyPairStore updateKeyRef:keyRef
162156
withTag:@"test"
163157
handler:^(NSError *error) {
164158
[completionExpectation fulfill];
165159
}];
160+
161+
// Release locally allocated CoreFoundation object.
162+
CFRelease(keyRef);
166163
}
164+
165+
// Should be still alive until execution finished
166+
XCTAssertNotNil(weakKeyRef);
167167
[self waitForExpectationsWithTimeout:0.5 handler:NULL];
168+
169+
// Should be released once finished
170+
// The check below is flaky for build under DEBUG (petentially due to ARC specifics).
171+
// Comment it so far as not-so-important one.
172+
// XCTAssertNil(weakKeyRef);
173+
}
174+
175+
- (SecKeyRef)generateKeyRef {
176+
NSDictionary *keyAttributes = @{(__bridge id)kSecAttrIsPermanent : @YES};
177+
NSDictionary *keyPairAttributes = @{
178+
(__bridge id)kSecAttrKeyType : (__bridge id)kSecAttrKeyTypeRSA,
179+
(__bridge id)kSecAttrLabel : @"[FIRInstanceIDKeyPairMigrationTest generateKeyRef]",
180+
(__bridge id)kSecAttrKeySizeInBits : @(2048),
181+
(__bridge id)kSecPrivateKeyAttrs : keyAttributes,
182+
(__bridge id)kSecPublicKeyAttrs : keyAttributes,
183+
};
184+
185+
SecKeyRef publicKey = NULL;
186+
SecKeyGeneratePair((__bridge CFDictionaryRef)keyPairAttributes, &publicKey, NULL);
187+
188+
return publicKey;
168189
}
169-
#endif
170190

171191
@end

Example/InstanceID/Tests/FIRInstanceIDKeyPairStoreTest.m

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,17 @@ - (void)setUp {
6262
}
6363

6464
- (void)tearDown {
65-
[super tearDown];
6665
NSError *error = nil;
6766
[self.keyPairStore removeKeyPairCreationTimePlistWithError:&error];
68-
[self.keyPairStore deleteSavedKeyPairWithSubtype:kFIRInstanceIDKeyPairSubType handler:nil];
67+
68+
XCTestExpectation *queueDrained = [self expectationWithDescription:@"drainKeychainQueue"];
69+
[self.keyPairStore deleteSavedKeyPairWithSubtype:kFIRInstanceIDKeyPairSubType
70+
handler:^(NSError *error) {
71+
[queueDrained fulfill];
72+
}];
73+
[self waitForExpectations:@[ queueDrained ] timeout:10];
74+
75+
[super tearDown];
6976
}
7077

7178
/**
@@ -125,23 +132,23 @@ - (void)testResetIdentity {
125132
XCTAssertNil(error);
126133
NSString *iid1 = FIRInstanceIDAppIdentity(keyPair);
127134

128-
[self.keyPairStore
129-
deleteSavedKeyPairWithSubtype:kFIRInstanceIDKeyPairSubType
130-
handler:^(NSError *error) {
131-
XCTAssertNil(error);
132-
[self.keyPairStore removeKeyPairCreationTimePlistWithError:&error];
133-
XCTAssertNil(error);
134-
135-
// regenerate instance-id
136-
FIRInstanceIDKeyPair *keyPair =
137-
[self.keyPairStore loadKeyPairWithError:&error];
138-
XCTAssertNil(error);
139-
NSString *iid2 = FIRInstanceIDAppIdentity(keyPair);
135+
[self.keyPairStore deleteSavedKeyPairWithSubtype:kFIRInstanceIDKeyPairSubType
136+
handler:^(NSError *error) {
137+
XCTAssertNil(error);
138+
[identityResetExpectation fulfill];
139+
}];
140140

141-
XCTAssertNotEqualObjects(iid1, iid2);
142-
[identityResetExpectation fulfill];
143-
}];
144141
[self waitForExpectationsWithTimeout:5 handler:nil];
142+
143+
[self.keyPairStore removeKeyPairCreationTimePlistWithError:&error];
144+
XCTAssertNil(error);
145+
146+
// regenerate instance-id
147+
FIRInstanceIDKeyPair *keyPair2 = [self.keyPairStore loadKeyPairWithError:&error];
148+
XCTAssertNil(error);
149+
NSString *iid2 = FIRInstanceIDAppIdentity(keyPair2);
150+
151+
XCTAssertNotEqualObjects(iid1, iid2);
145152
}
146153

147154
/**
@@ -236,4 +243,5 @@ - (void)testDeleteKeyPair {
236243
}];
237244
[self waitForExpectationsWithTimeout:1 handler:nil];
238245
}
246+
239247
@end

Example/InstanceID/Tests/FIRInstanceIDKeyPairTest.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,13 @@ - (void)testValidKeychain {
6565
XCTAssertNotNil(keypair.privateKeyData);
6666
XCTAssertNotNil(FIRInstanceIDAppIdentity(keypair));
6767

68+
XCTestExpectation *keyPairDeleted = [self expectationWithDescription:@"keyPairDeleted"];
6869
[FIRInstanceIDKeyPairStore deleteKeyPairWithPrivateTag:kKeyPairPrivateTag
6970
publicTag:kKeyPairPublicTag
70-
handler:nil];
71+
handler:^(NSError *error) {
72+
[keyPairDeleted fulfill];
73+
}];
74+
75+
[self waitForExpectations:@[ keyPairDeleted ] timeout:1.0];
7176
}
7277
@end

Example/InstanceID/Tests/FIRInstanceIDResultTest.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ @implementation FIRInstanceIDResultTest
4545

4646
- (void)setUp {
4747
[super setUp];
48-
_instanceID = [[FIRInstanceID alloc] initPrivately];
49-
[_instanceID start];
50-
_mockInstanceID = OCMPartialMock(_instanceID);
48+
_mockInstanceID = OCMClassMock([FIRInstanceID class]);
5149
}
5250

5351
- (void)tearDown {
5452
[_mockInstanceID stopMocking];
53+
_mockInstanceID = nil;
5554
[super tearDown];
5655
}
5756

0 commit comments

Comments
 (0)