FIRInstallationsIIDTokenStoreTests.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import "FBLPromise+Testing.h"
  18. #import "FIRInstanceIDAuthKeychain.h"
  19. #import "FIRInstanceIDBackupExcludedPlist.h"
  20. #import "FIRInstanceIDCheckinPreferences+Internal.h"
  21. #import "FIRInstanceIDStore.h"
  22. #import "FIRInstanceIDTokenInfo.h"
  23. #import "FIRInstanceIDTokenStore.h"
  24. #import "FirebaseInstallations/Source/Library/IIDMigration/FIRInstallationsIIDTokenStore.h"
  25. static NSString *const kFakeCheckinPlistName = @"com.google.test.IIDStoreTestCheckin";
  26. static NSString *const kSubDirectoryName = @"FIRInstallationsIIDCheckinStoreTests";
  27. static NSString *const kIDTokenKeychainId = @"com.google.iid-tokens";
  28. @interface FIRInstallationsIIDTokenStoreTests : XCTestCase
  29. @property(nonatomic) FIRInstallationsIIDTokenStore *installationsIIDCheckinStore;
  30. @property(nonatomic) FIRInstanceIDTokenStore *IIDTokenStore;
  31. @property(nonatomic) FIRInstanceIDAuthKeychain *IIDKeychain;
  32. @property(nonatomic) NSString *GCMSenderID;
  33. @end
  34. @implementation FIRInstallationsIIDTokenStoreTests
  35. - (void)setUp {
  36. self.GCMSenderID = @"GCMSenderID";
  37. self.installationsIIDCheckinStore =
  38. [[FIRInstallationsIIDTokenStore alloc] initWithGCMSenderID:self.GCMSenderID];
  39. self.IIDKeychain = [[FIRInstanceIDAuthKeychain alloc] initWithIdentifier:kIDTokenKeychainId];
  40. self.IIDTokenStore = [[FIRInstanceIDTokenStore alloc] initWithKeychain:self.IIDKeychain];
  41. }
  42. - (void)tearDown {
  43. self.IIDKeychain = nil;
  44. self.IIDTokenStore = nil;
  45. self.installationsIIDCheckinStore = nil;
  46. }
  47. - (void)testExistingAuthToken_WhenNoToken_ThenFails {
  48. [self removeIIDTokenWithScope:@"*"];
  49. __auto_type checkinPromise = [self.installationsIIDCheckinStore existingIIDDefaultToken];
  50. XCTAssert(FBLWaitForPromisesWithTimeout(1));
  51. XCTAssertTrue(checkinPromise.isRejected);
  52. XCTAssertNil(checkinPromise.value);
  53. XCTAssertNotNil(checkinPromise.error);
  54. }
  55. - (void)testExistingAuthToken_WhenThereAreTokensButNoDefaultToken_ThenFails {
  56. [self removeIIDTokenWithScope:@"*"];
  57. [self saveIIDDefaultTokenForScope:@"FIAM" token:@"iid-auth-token"];
  58. __auto_type checkinPromise = [self.installationsIIDCheckinStore existingIIDDefaultToken];
  59. XCTAssert(FBLWaitForPromisesWithTimeout(1));
  60. XCTAssertTrue(checkinPromise.isRejected);
  61. XCTAssertNil(checkinPromise.value);
  62. XCTAssertNotNil(checkinPromise.error);
  63. }
  64. - (void)testExistingAuthToken_WhenDataCorrupted_ThenFails {
  65. [self removeIIDTokenWithScope:@"*"];
  66. [self saveIIDDefaultTokenForScope:@"FIAM" token:@""];
  67. __auto_type checkinPromise = [self.installationsIIDCheckinStore existingIIDDefaultToken];
  68. XCTAssert(FBLWaitForPromisesWithTimeout(1));
  69. XCTAssertTrue(checkinPromise.isRejected);
  70. XCTAssertNil(checkinPromise.value);
  71. XCTAssertNotNil(checkinPromise.error);
  72. }
  73. - (void)testExistingAuthTokenSuccess {
  74. NSString *savedToken = [self saveIIDDefaultTokenForScope:@"*" token:@"iid-auth-token"];
  75. __auto_type checkinPromise = [self.installationsIIDCheckinStore existingIIDDefaultToken];
  76. XCTAssert(FBLWaitForPromisesWithTimeout(1));
  77. XCTAssertTrue(checkinPromise.isFulfilled);
  78. XCTAssertNil(checkinPromise.error);
  79. XCTAssertNotNil(checkinPromise.value);
  80. XCTAssert([checkinPromise.value isKindOfClass:[NSString class]]);
  81. XCTAssertEqualObjects(checkinPromise.value, savedToken);
  82. }
  83. #pragma mark - Helpers
  84. - (NSString *)saveIIDDefaultTokenForScope:(NSString *)scope token:(NSString *)token {
  85. FIRInstanceIDTokenInfo *tokenInfoToSave =
  86. [[FIRInstanceIDTokenInfo alloc] initWithAuthorizedEntity:self.GCMSenderID
  87. scope:scope
  88. token:token
  89. appVersion:nil
  90. firebaseAppID:nil];
  91. XCTestExpectation *saveExpectation =
  92. [self expectationWithDescription:@"saveIIDCheckingPreferences"];
  93. [self.IIDTokenStore saveTokenInfo:tokenInfoToSave
  94. handler:^(NSError *error) {
  95. XCTAssertNil(error);
  96. [saveExpectation fulfill];
  97. }];
  98. [self waitForExpectations:@[ saveExpectation ] timeout:1];
  99. FIRInstanceIDTokenInfo *savedTokenInfo =
  100. [self.IIDTokenStore tokenInfoWithAuthorizedEntity:self.GCMSenderID scope:scope];
  101. XCTAssertEqualObjects(tokenInfoToSave.token, savedTokenInfo.token);
  102. return savedTokenInfo.token;
  103. }
  104. - (void)removeIIDTokenWithScope:(NSString *)scope {
  105. XCTestExpectation *expectation = [self expectationWithDescription:@"removeIIDTokens"];
  106. [self.IIDTokenStore removeTokenWithAuthorizedEntity:self.GCMSenderID scope:scope];
  107. [self.IIDTokenStore removeAllTokensWithHandler:^(NSError *_Nonnull error) {
  108. XCTAssertNil(error);
  109. [expectation fulfill];
  110. }];
  111. [self waitForExpectations:@[ expectation ] timeout:1];
  112. }
  113. @end