FIRMessagingTokenInfoTest.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "FirebaseMessaging/Sources/Token/FIRMessagingTokenInfo.h"
  17. #import <XCTest/XCTest.h>
  18. #import <OCMock/OCMock.h>
  19. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  20. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  21. #import "FirebaseMessaging/Sources/Token/FIRMessagingAPNSInfo.h"
  22. static NSString *const kAuthorizedEntity = @"authorizedEntity";
  23. static NSString *const kScope = @"scope";
  24. static NSString *const kToken = @"eMP633ZkDYA:APA91bGfnlnbinRVE7nUwJSr_k6cuSTKectOlt66dKv1r_-"
  25. @"9Qvhy9XljAI62QPw307rgA0MaFHPnrU5sFxGZvsncRnkfuciwTUeyRpPNDZMFhNXt"
  26. @"7h1BKq9Wb2A0LAANpQefrPHVUp4p";
  27. static NSString *const kFirebaseAppID = @"firebaseAppID";
  28. static NSString *const kIID = @"eMP633ZkDYA";
  29. static BOOL const kAPNSSandbox = NO;
  30. @interface FIRMessagingTokenInfoTest : XCTestCase
  31. @property(nonatomic, strong) NSData *APNSDeviceToken;
  32. @property(nonatomic, strong) FIRMessagingTokenInfo *validTokenInfo;
  33. @property(nonatomic, strong) id mockOptions;
  34. @end
  35. @implementation FIRMessagingTokenInfoTest
  36. - (void)setUp {
  37. [super setUp];
  38. self.APNSDeviceToken = [@"validDeviceToken" dataUsingEncoding:NSUTF8StringEncoding];
  39. self.mockOptions = OCMClassMock([FIROptions class]);
  40. OCMStub([self.mockOptions defaultOptionsDictionary]).andReturn(@{
  41. kFIRGoogleAppID : kFirebaseAppID
  42. });
  43. self.validTokenInfo =
  44. [[FIRMessagingTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
  45. scope:kScope
  46. token:kToken
  47. appVersion:FIRMessagingCurrentAppVersion()
  48. firebaseAppID:FIRMessagingFirebaseAppID()];
  49. self.validTokenInfo.APNSInfo =
  50. [[FIRMessagingAPNSInfo alloc] initWithDeviceToken:self.APNSDeviceToken
  51. isSandbox:kAPNSSandbox];
  52. self.validTokenInfo.cacheTime = [NSDate date];
  53. [[NSUserDefaults standardUserDefaults] setObject:FIRMessagingCurrentLocale()
  54. forKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
  55. }
  56. - (void)tearDown {
  57. [self.mockOptions stopMocking];
  58. [super tearDown];
  59. }
  60. // Test that archiving a FIRMessagingTokenInfo object and restoring it from the archive
  61. // yields the same values for all the fields.
  62. - (void)testTokenInfoEncodingAndDecoding {
  63. FIRMessagingTokenInfo *info = self.validTokenInfo;
  64. NSError *error;
  65. NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:info
  66. requiringSecureCoding:YES
  67. error:&error];
  68. XCTAssertNil(error);
  69. NSSet *classes = [[NSSet alloc] initWithArray:@[ FIRMessagingTokenInfo.class, NSDate.class ]];
  70. FIRMessagingTokenInfo *restoredInfo = [NSKeyedUnarchiver unarchivedObjectOfClasses:classes
  71. fromData:archive
  72. error:&error];
  73. XCTAssertNil(error);
  74. XCTAssertEqualObjects(restoredInfo.authorizedEntity, info.authorizedEntity);
  75. XCTAssertEqualObjects(restoredInfo.scope, info.scope);
  76. XCTAssertEqualObjects(restoredInfo.token, info.token);
  77. XCTAssertEqualObjects(restoredInfo.appVersion, info.appVersion);
  78. XCTAssertEqualObjects(restoredInfo.firebaseAppID, info.firebaseAppID);
  79. XCTAssertEqualObjects(restoredInfo.cacheTime, info.cacheTime);
  80. XCTAssertEqualObjects(restoredInfo.APNSInfo.deviceToken, info.APNSInfo.deviceToken);
  81. XCTAssertEqual(restoredInfo.APNSInfo.sandbox, info.APNSInfo.sandbox);
  82. }
  83. // Test that archiving a FIRMessagingTokenInfo object with missing fields and restoring it
  84. // from the archive yields the same values for all the fields.
  85. - (void)testTokenInfoEncodingAndDecodingWithMissingFields {
  86. // Don't include appVersion, firebaseAppID, APNSInfo and cacheTime
  87. FIRMessagingTokenInfo *sparseInfo =
  88. [[FIRMessagingTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
  89. scope:kScope
  90. token:kToken
  91. appVersion:nil
  92. firebaseAppID:nil];
  93. NSError *error;
  94. NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:sparseInfo
  95. requiringSecureCoding:YES
  96. error:&error];
  97. XCTAssertNil(error);
  98. NSSet *classes = [[NSSet alloc] initWithArray:@[ FIRMessagingTokenInfo.class, NSDate.class ]];
  99. FIRMessagingTokenInfo *restoredInfo = [NSKeyedUnarchiver unarchivedObjectOfClasses:classes
  100. fromData:archive
  101. error:&error];
  102. XCTAssertNil(error);
  103. XCTAssertEqualObjects(restoredInfo.authorizedEntity, sparseInfo.authorizedEntity);
  104. XCTAssertEqualObjects(restoredInfo.scope, sparseInfo.scope);
  105. XCTAssertEqualObjects(restoredInfo.token, sparseInfo.token);
  106. XCTAssertNil(restoredInfo.appVersion);
  107. XCTAssertNil(restoredInfo.firebaseAppID);
  108. XCTAssertNil(restoredInfo.cacheTime);
  109. XCTAssertNil(restoredInfo.APNSInfo);
  110. }
  111. - (void)testTokenFreshnessWithLocaleChange {
  112. // Default should be fresh because we mock last fetch token time just now.
  113. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  114. // Locale change should affect token refreshness.
  115. // Set to a different locale than the current locale.
  116. [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant"
  117. forKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
  118. [[NSUserDefaults standardUserDefaults] synchronize];
  119. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  120. // Reset locale
  121. [[NSUserDefaults standardUserDefaults] setObject:FIRMessagingCurrentLocale()
  122. forKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
  123. [[NSUserDefaults standardUserDefaults] synchronize];
  124. }
  125. - (void)testTokenFreshnessWithTokenTimestampChange {
  126. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  127. // Set last fetch token time 7 days ago.
  128. NSTimeInterval lastFetchTokenTimestamp =
  129. FIRMessagingCurrentTimestampInSeconds() - 7 * 24 * 60 * 60;
  130. self.validTokenInfo.cacheTime = [NSDate dateWithTimeIntervalSince1970:lastFetchTokenTimestamp];
  131. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  132. // Set last fetch token time more than 7 days ago.
  133. lastFetchTokenTimestamp = FIRMessagingCurrentTimestampInSeconds() - 8 * 24 * 60 * 60;
  134. self.validTokenInfo.cacheTime = [NSDate dateWithTimeIntervalSince1970:lastFetchTokenTimestamp];
  135. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  136. // Set last fetch token time nil to mock legacy storage format. Token should be considered not
  137. // fresh.
  138. self.validTokenInfo.cacheTime = nil;
  139. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  140. }
  141. - (void)testTokenFreshnessWithFirebaseAppIDChange {
  142. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  143. // Change Firebase App ID.
  144. [FIROptions defaultOptions].googleAppID = @"newFirebaseAppID:ios:abcdefg";
  145. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  146. }
  147. - (void)testTokenFreshnessWithAppVersionChange {
  148. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  149. // Change app version.
  150. self.validTokenInfo =
  151. [[FIRMessagingTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
  152. scope:kScope
  153. token:kToken
  154. appVersion:@"1.1"
  155. firebaseAppID:FIRMessagingFirebaseAppID()];
  156. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  157. }
  158. - (void)testTokenInconsistentWithIID {
  159. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  160. // Change token.
  161. self.validTokenInfo = [[FIRMessagingTokenInfo alloc]
  162. initWithAuthorizedEntity:kAuthorizedEntity
  163. scope:kScope
  164. token:@"cxhhwVY27AE:APA91bGfnlnbinRVE7nUwJSr_k6cuSTKectOlt66dKv1r_-"
  165. @"9Qvhy9XljAI62QPw307rgA0MaFHPnrU5sFxGZvsncRnkfuciwTUeyRpPNDZMFhNXt7"
  166. @"h1BKq9Wb2A0LAANpQefrPHVUp4p"
  167. appVersion:@"1.1"
  168. firebaseAppID:FIRMessagingFirebaseAppID()];
  169. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  170. }
  171. @end