fixup spk deletion test

This commit is contained in:
Michael Kirk 2018-10-01 08:04:51 -06:00
parent 06b763dfc4
commit f45908c89d

View file

@ -9,7 +9,7 @@
@interface TSPreKeyManager (Testing)
+ (void)clearSignedPreKeyRecordsWithKeyId:(NSNumber *)keyId success:(void (^_Nullable)(void))successHandler;
+ (void)clearSignedPreKeyRecordsWithKeyId:(NSNumber *)keyId;
@end
@ -41,7 +41,10 @@
int secondsAgo = (i - days) * 24 * 60 * 60;
NSAssert(secondsAgo <= 0, @"Time in past must be negative");
NSDate *generatedAt = [NSDate dateWithTimeIntervalSinceNow:secondsAgo];
SignedPreKeyRecord *record = [[SignedPreKeyRecord alloc] initWithId:i keyPair:[Curve25519 generateKeyPair] signature:nil generatedAt:generatedAt];
SignedPreKeyRecord *record = [[SignedPreKeyRecord alloc] initWithId:i
keyPair:[Curve25519 generateKeyPair]
signature:[NSData new]
generatedAt:generatedAt];
[[OWSPrimaryStorage sharedManager] storeSignedPreKey:i signedPreKeyRecord:record];
}
@ -49,21 +52,12 @@
// Sanity check
XCTAssert(signedPreKeys.count == 21);
XCTestExpectation *expection = [self expectationWithDescription:@"successfully cleared old keys"];
[TSPreKeyManager
clearSignedPreKeyRecordsWithKeyId:[NSNumber numberWithInt:lastPreKeyId]
success:^{
XCTAssert(
[[OWSPrimaryStorage sharedManager] loadSignedPrekey:lastPreKeyId] != nil);
[TSPreKeyManager clearSignedPreKeyRecordsWithKeyId:@(lastPreKeyId)];
XCTAssert([[OWSPrimaryStorage sharedManager] loadSignedPrekey:lastPreKeyId] != nil);
// We'll delete every key created 7 or more days ago.
NSArray<SignedPreKeyRecord *> *signedPreKeys =
[[OWSPrimaryStorage sharedManager] loadSignedPreKeys];
XCTAssert(signedPreKeys.count == 7);
[expection fulfill];
}];
[self waitForExpectationsWithTimeout:5.0 handler:nil];
// We'll delete every key created 7 or more days ago.
signedPreKeys = [[OWSPrimaryStorage sharedManager] loadSignedPreKeys];
XCTAssert(signedPreKeys.count == 7);
}
- (void)testSignedPreKeyDeletionKeepsSomeOldKeys
@ -80,7 +74,7 @@
NSDate *generatedAt = [NSDate dateWithTimeIntervalSinceNow:secondsAgo];
SignedPreKeyRecord *record = [[SignedPreKeyRecord alloc] initWithId:i
keyPair:[Curve25519 generateKeyPair]
signature:nil
signature:[NSData new]
generatedAt:generatedAt];
// we only retain accepted keys
[record markAsAcceptedByService];
@ -92,22 +86,14 @@
// Sanity check
XCTAssert(signedPreKeys.count == 11);
XCTestExpectation *expection = [self expectationWithDescription:@"successfully cleared old keys"];
[TSPreKeyManager
clearSignedPreKeyRecordsWithKeyId:[NSNumber numberWithInt:lastPreKeyId]
success:^{
XCTAssert(
[[OWSPrimaryStorage sharedManager] loadSignedPrekey:lastPreKeyId] != nil);
[TSPreKeyManager clearSignedPreKeyRecordsWithKeyId:@(lastPreKeyId)];
NSArray<SignedPreKeyRecord *> *signedPreKeys =
[[OWSPrimaryStorage sharedManager] loadSignedPreKeys];
XCTAssert([[OWSPrimaryStorage sharedManager] loadSignedPrekey:lastPreKeyId] != nil);
// We need to keep 3 "old" keys, plus the "current" key
XCTAssert(signedPreKeys.count == 4);
[expection fulfill];
}];
signedPreKeys = [[OWSPrimaryStorage sharedManager] loadSignedPreKeys];
[self waitForExpectationsWithTimeout:5.0 handler:nil];
// We need to keep 3 "old" keys, plus the "current" key
XCTAssert(signedPreKeys.count == 4);
}
- (void)testOlderRecordsNotDeletedIfNoReplacement {
@ -123,7 +109,10 @@
int secondsAgo = (i - days) * 24 * 60 * 60;
NSAssert(secondsAgo <= 0, @"Time in past must be negative");
NSDate *generatedAt = [NSDate dateWithTimeIntervalSinceNow:secondsAgo];
SignedPreKeyRecord *record = [[SignedPreKeyRecord alloc] initWithId:i keyPair:[Curve25519 generateKeyPair] signature:nil generatedAt:generatedAt];
SignedPreKeyRecord *record = [[SignedPreKeyRecord alloc] initWithId:i
keyPair:[Curve25519 generateKeyPair]
signature:[NSData new]
generatedAt:generatedAt];
[[OWSPrimaryStorage sharedManager] storeSignedPreKey:i signedPreKeyRecord:record];
}
@ -131,20 +120,12 @@
// Sanity check
XCTAssert(signedPreKeys.count == 4);
XCTestExpectation *expection = [self expectationWithDescription:@"successfully cleared old keys"];
[TSPreKeyManager
clearSignedPreKeyRecordsWithKeyId:[NSNumber numberWithInt:lastPreKeyId]
success:^{
XCTAssert(
[[OWSPrimaryStorage sharedManager] loadSignedPrekey:lastPreKeyId] != nil);
// All three records should still be stored.
NSArray<SignedPreKeyRecord *> *signedPreKeys =
[[OWSPrimaryStorage sharedManager] loadSignedPreKeys];
XCTAssert(signedPreKeys.count == 4);
[expection fulfill];
}];
[TSPreKeyManager clearSignedPreKeyRecordsWithKeyId:@(lastPreKeyId)];
XCTAssert([[OWSPrimaryStorage sharedManager] loadSignedPrekey:lastPreKeyId] != nil);
[self waitForExpectationsWithTimeout:5.0 handler:nil];
// All three records should still be stored.
signedPreKeys = [[OWSPrimaryStorage sharedManager] loadSignedPreKeys];
XCTAssert(signedPreKeys.count == 4);
}
#endif