|
16 | 16 |
|
17 | 17 | #import <XCTest/XCTest.h>
|
18 | 18 |
|
| 19 | +#import <DeviceCheck/DeviceCheck.h> |
19 | 20 | #import <OCMock/OCMock.h>
|
20 | 21 | #import "FBLPromise+Testing.h"
|
21 | 22 |
|
@@ -602,6 +603,75 @@ - (void)testGetToken_WhenAttestationIsRejected_ThenAttestationIsResetAndRetriedO
|
602 | 603 | [self verifyAllMocks];
|
603 | 604 | }
|
604 | 605 |
|
| 606 | +- (void)testGetToken_WhenExistingKeyIsRejectedByApple_ThenAttestationIsResetAndRetriedOnce_Success { |
| 607 | + // 1. Expect FIRAppAttestService.isSupported. |
| 608 | + [OCMExpect([self.mockAppAttestService isSupported]) andReturnValue:@(YES)]; |
| 609 | + |
| 610 | + // 2. Expect storage getAppAttestKeyID. |
| 611 | + NSString *existingKeyID = @"existingKeyID"; |
| 612 | + OCMExpect([self.mockStorage getAppAttestKeyID]) |
| 613 | + .andReturn([FBLPromise resolvedWith:existingKeyID]); |
| 614 | + |
| 615 | + // 3. Expect a stored artifact to be requested. |
| 616 | + __auto_type rejectedPromise = [self rejectedPromiseWithError:[NSError errorWithDomain:self.name |
| 617 | + code:NSNotFound |
| 618 | + userInfo:nil]]; |
| 619 | + OCMExpect([self.mockArtifactStorage getArtifactForKey:existingKeyID]).andReturn(rejectedPromise); |
| 620 | + |
| 621 | + // 4. Expect random challenge to be requested. |
| 622 | + OCMExpect([self.mockAPIService getRandomChallenge]) |
| 623 | + .andReturn([FBLPromise resolvedWith:self.randomChallenge]); |
| 624 | + |
| 625 | + // 5. Expect the key to be attested with the challenge. |
| 626 | + NSError *attestationError = [NSError errorWithDomain:DCErrorDomain |
| 627 | + code:DCErrorInvalidKey |
| 628 | + userInfo:nil]; |
| 629 | + id attestCompletionArg = [OCMArg invokeBlockWithArgs:[NSNull null], attestationError, nil]; |
| 630 | + OCMExpect([self.mockAppAttestService attestKey:existingKeyID |
| 631 | + clientDataHash:self.randomChallengeHash |
| 632 | + completionHandler:attestCompletionArg]); |
| 633 | + |
| 634 | + // 6. Stored attestation to be reset. |
| 635 | + [self expectAttestationReset]; |
| 636 | + |
| 637 | + // 7. Expect the App Attest key pair to be generated and attested. |
| 638 | + NSString *newKeyID = @"newKeyID"; |
| 639 | + NSData *attestationData = [[NSUUID UUID].UUIDString dataUsingEncoding:NSUTF8StringEncoding]; |
| 640 | + [self expectAppAttestKeyGeneratedAndAttestedWithKeyID:newKeyID attestationData:attestationData]; |
| 641 | + |
| 642 | + // 8. Expect exchange request to be sent. |
| 643 | + FIRAppCheckToken *FACToken = [[FIRAppCheckToken alloc] initWithToken:@"FAC token" |
| 644 | + expirationDate:[NSDate date]]; |
| 645 | + NSData *artifactData = [@"attestation artifact" dataUsingEncoding:NSUTF8StringEncoding]; |
| 646 | + __auto_type attestKeyResponse = |
| 647 | + [[FIRAppAttestAttestationResponse alloc] initWithArtifact:artifactData token:FACToken]; |
| 648 | + OCMExpect([self.mockAPIService attestKeyWithAttestation:attestationData |
| 649 | + keyID:newKeyID |
| 650 | + challenge:self.randomChallenge]) |
| 651 | + .andReturn([FBLPromise resolvedWith:attestKeyResponse]); |
| 652 | + |
| 653 | + // 9. Expect the artifact received from Firebase backend to be saved. |
| 654 | + OCMExpect([self.mockArtifactStorage setArtifact:artifactData forKey:newKeyID]) |
| 655 | + .andReturn([FBLPromise resolvedWith:artifactData]); |
| 656 | + |
| 657 | + // 10. Call get token. |
| 658 | + XCTestExpectation *completionExpectation = |
| 659 | + [self expectationWithDescription:@"completionExpectation"]; |
| 660 | + [self.provider |
| 661 | + getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) { |
| 662 | + [completionExpectation fulfill]; |
| 663 | + |
| 664 | + XCTAssertEqualObjects(token.token, FACToken.token); |
| 665 | + XCTAssertEqualObjects(token.expirationDate, FACToken.expirationDate); |
| 666 | + XCTAssertNil(error); |
| 667 | + }]; |
| 668 | + |
| 669 | + [self waitForExpectations:@[ completionExpectation ] timeout:0.5 enforceOrder:YES]; |
| 670 | + |
| 671 | + // 11. Verify mocks. |
| 672 | + [self verifyAllMocks]; |
| 673 | +} |
| 674 | + |
605 | 675 | #pragma mark - FAC token refresh (assertion)
|
606 | 676 |
|
607 | 677 | - (void)testGetToken_WhenKeyRegistered_Success {
|
|
0 commit comments