FIRMessagingTokenInfoTest.m 9.2 KB

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