FIRMessagingTokenStoreTest.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright 2021 Google LLC
  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 <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  19. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  20. #import "FirebaseMessaging/Sources/Token/FIRMessagingBackupExcludedPlist.h"
  21. #import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinPreferences.h"
  22. #import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinService.h"
  23. #import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinStore.h"
  24. #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenInfo.h"
  25. #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenStore.h"
  26. #import "FirebaseMessaging/Tests/UnitTests/FIRMessagingFakeKeychain.h"
  27. static NSString *const kSubDirectoryName = @"FirebaseMessagingStoreTest";
  28. static NSString *const kAuthorizedEntity = @"test-audience";
  29. static NSString *const kScope = @"test-scope";
  30. static NSString *const kToken = @"test-token";
  31. static NSString *const kAuthID = @"test-auth-id";
  32. static NSString *const kSecret = @"test-secret";
  33. static NSString *const kFakeCheckinPlistName = @"com.google.test.TestTokenStore";
  34. @interface FIRMessaging (ExposedForTest)
  35. + (BOOL)createSubDirectory:(NSString *)subDirectoryName;
  36. @end
  37. @interface FIRMessagingCheckinStore ()
  38. @property(nonatomic, readwrite, strong) FIRMessagingAuthKeychain *keychain;
  39. @end
  40. @interface FIRMessagingTokenStore ()
  41. @property(nonatomic, readwrite, strong) FIRMessagingAuthKeychain *keychain;
  42. @end
  43. @interface FIRMessagingBackupExcludedPlist (ExposedForTest)
  44. - (BOOL)deleteFile:(NSError **)error;
  45. @end
  46. @interface FIRMessagingTokenStoreTest : XCTestCase
  47. @property(strong, nonatomic) FIRMessagingBackupExcludedPlist *checkinPlist;
  48. @property(strong, nonatomic) FIRMessagingCheckinStore *checkinStore;
  49. @property(strong, nonatomic) FIRMessagingTokenStore *tokenStore;
  50. @property(strong, nonatomic) id mockCheckinStore;
  51. @property(strong, nonatomic) id mockTokenStore;
  52. @property(strong, nonatomic) id mockMessagingStore;
  53. @end
  54. @implementation FIRMessagingTokenStoreTest
  55. - (void)setUp {
  56. [super setUp];
  57. [FIRMessaging createSubDirectory:kSubDirectoryName];
  58. self.checkinPlist =
  59. [[FIRMessagingBackupExcludedPlist alloc] initWithPlistFile:kFakeCheckinPlistName
  60. subDirectory:kSubDirectoryName];
  61. // checkin store
  62. FIRMessagingFakeKeychain *fakeKeychain = [[FIRMessagingFakeKeychain alloc] init];
  63. _checkinStore = [[FIRMessagingCheckinStore alloc] init];
  64. _checkinStore.keychain = fakeKeychain;
  65. _mockCheckinStore = OCMPartialMock(_checkinStore);
  66. // token store
  67. _tokenStore = [[FIRMessagingTokenStore alloc] init];
  68. _tokenStore.keychain = fakeKeychain;
  69. _mockTokenStore = OCMPartialMock(_tokenStore);
  70. }
  71. - (void)tearDown {
  72. [self.checkinPlist deleteFile:nil];
  73. [_tokenStore removeAllTokensWithHandler:nil];
  74. [_mockCheckinStore stopMocking];
  75. [_mockTokenStore stopMocking];
  76. [_mockMessagingStore stopMocking];
  77. [super tearDown];
  78. }
  79. /**
  80. * Tests that an Messaging token can be stored in the FIRMessagingStore for
  81. * an authorizedEntity and scope.
  82. */
  83. - (void)testSaveToken {
  84. XCTestExpectation *tokenExpectation = [self expectationWithDescription:@"token is saved"];
  85. FIRMessagingTokenInfo *tokenInfo =
  86. [[FIRMessagingTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
  87. scope:kScope
  88. token:kToken
  89. appVersion:@"1.0"
  90. firebaseAppID:@"firebaseAppID"];
  91. [self.tokenStore saveTokenInfo:tokenInfo
  92. handler:^(NSError *error) {
  93. XCTAssertNil(error);
  94. FIRMessagingTokenInfo *retrievedTokenInfo =
  95. [self.tokenStore tokenInfoWithAuthorizedEntity:kAuthorizedEntity
  96. scope:kScope];
  97. XCTAssertEqualObjects(retrievedTokenInfo.token, kToken);
  98. [tokenExpectation fulfill];
  99. }];
  100. [self waitForExpectationsWithTimeout:1 handler:nil];
  101. }
  102. /**
  103. * Tests that a token can be removed from from FIRMessagingStore's cache when specifying
  104. * its authorizedEntity and scope.
  105. */
  106. - (void)testRemoveCachedToken {
  107. XCTestExpectation *tokenExpectation = [self expectationWithDescription:@"token is removed"];
  108. FIRMessagingTokenInfo *tokenInfo =
  109. [[FIRMessagingTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
  110. scope:kScope
  111. token:kToken
  112. appVersion:@"1.0"
  113. firebaseAppID:@"firebaseAppID"];
  114. [self.tokenStore
  115. saveTokenInfo:tokenInfo
  116. handler:^(NSError *error) {
  117. XCTAssertNotNil([self.tokenStore tokenInfoWithAuthorizedEntity:kAuthorizedEntity
  118. scope:kScope]);
  119. [self.tokenStore removeTokenWithAuthorizedEntity:kAuthorizedEntity scope:kScope];
  120. XCTAssertNil([self.tokenStore tokenInfoWithAuthorizedEntity:kAuthorizedEntity
  121. scope:kScope]);
  122. [tokenExpectation fulfill];
  123. }];
  124. [self waitForExpectationsWithTimeout:1 handler:nil];
  125. }
  126. /**
  127. * Tests that a checkin authentication ID can be stored in the FIRMessagingStore.
  128. */
  129. - (void)testSaveCheckinAuthID {
  130. XCTestExpectation *checkinExpectation = [self expectationWithDescription:@"checkin is saved"];
  131. NSDictionary *plistContent = @{
  132. kFIRMessagingDigestStringKey : @"digest-xyz",
  133. kFIRMessagingLastCheckinTimeKey : @(FIRMessagingCurrentTimestampInMilliseconds())
  134. };
  135. FIRMessagingCheckinPreferences *preferences =
  136. [[FIRMessagingCheckinPreferences alloc] initWithDeviceID:kAuthID secretToken:kSecret];
  137. [preferences updateWithCheckinPlistContents:plistContent];
  138. [self.checkinStore saveCheckinPreferences:preferences
  139. handler:^(NSError *_Nonnull error) {
  140. XCTAssertNil(error);
  141. FIRMessagingCheckinPreferences *cachedPreferences =
  142. [self.checkinStore cachedCheckinPreferences];
  143. XCTAssertEqualObjects(cachedPreferences.deviceID, kAuthID);
  144. XCTAssertEqualObjects(cachedPreferences.secretToken, kSecret);
  145. [checkinExpectation fulfill];
  146. }];
  147. [self waitForExpectationsWithTimeout:1 handler:nil];
  148. }
  149. /**
  150. * Tests that a checkin authentication ID can be removed from FIRMessagingStore's cache.
  151. */
  152. - (void)testRemoveCheckinPreferences {
  153. XCTestExpectation *checkinExpectation = [self expectationWithDescription:@"checkin is removed"];
  154. NSDictionary *plistContent = @{
  155. kFIRMessagingDigestStringKey : @"digest-xyz",
  156. kFIRMessagingLastCheckinTimeKey : @(FIRMessagingCurrentTimestampInMilliseconds())
  157. };
  158. FIRMessagingCheckinPreferences *preferences =
  159. [[FIRMessagingCheckinPreferences alloc] initWithDeviceID:kAuthID secretToken:kSecret];
  160. [preferences updateWithCheckinPlistContents:plistContent];
  161. [self.checkinStore saveCheckinPreferences:preferences
  162. handler:^(NSError *error) {
  163. XCTAssertNil(error);
  164. [self.checkinStore removeCheckinPreferencesWithHandler:^(
  165. NSError *_Nullable error) {
  166. XCTAssertNil(error);
  167. FIRMessagingCheckinPreferences *cachedPreferences =
  168. [self.checkinStore cachedCheckinPreferences];
  169. XCTAssertNil(cachedPreferences.deviceID);
  170. XCTAssertNil(cachedPreferences.secretToken);
  171. [checkinExpectation fulfill];
  172. }];
  173. }];
  174. [self waitForExpectationsWithTimeout:1 handler:nil];
  175. }
  176. #pragma mark - Private Helpers
  177. - (NSString *)pathForCheckinPlist {
  178. NSArray *paths =
  179. NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
  180. NSString *plistNameWithExtension = [NSString stringWithFormat:@"%@.plist", kFakeCheckinPlistName];
  181. return [paths[0] stringByAppendingPathComponent:plistNameWithExtension];
  182. }
  183. @end