FIRMessagingTokenInfoTest.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/Sources/Private/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. #pragma clang diagnostic push
  65. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  66. NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:info];
  67. FIRMessagingTokenInfo *restoredInfo = [NSKeyedUnarchiver unarchiveObjectWithData:archive];
  68. #pragma clang diagnostic pop
  69. XCTAssertEqualObjects(restoredInfo.authorizedEntity, info.authorizedEntity);
  70. XCTAssertEqualObjects(restoredInfo.scope, info.scope);
  71. XCTAssertEqualObjects(restoredInfo.token, info.token);
  72. XCTAssertEqualObjects(restoredInfo.appVersion, info.appVersion);
  73. XCTAssertEqualObjects(restoredInfo.firebaseAppID, info.firebaseAppID);
  74. XCTAssertEqualObjects(restoredInfo.cacheTime, info.cacheTime);
  75. XCTAssertEqualObjects(restoredInfo.APNSInfo.deviceToken, info.APNSInfo.deviceToken);
  76. XCTAssertEqual(restoredInfo.APNSInfo.sandbox, info.APNSInfo.sandbox);
  77. }
  78. // Test that archiving a FIRMessagingTokenInfo object with missing fields and restoring it
  79. // from the archive yields the same values for all the fields.
  80. - (void)testTokenInfoEncodingAndDecodingWithMissingFields {
  81. // Don't include appVersion, firebaseAppID, APNSInfo and cacheTime
  82. FIRMessagingTokenInfo *sparseInfo =
  83. [[FIRMessagingTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
  84. scope:kScope
  85. token:kToken
  86. appVersion:nil
  87. firebaseAppID:nil];
  88. #pragma clang diagnostic push
  89. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  90. NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:sparseInfo];
  91. FIRMessagingTokenInfo *restoredInfo = [NSKeyedUnarchiver unarchiveObjectWithData:archive];
  92. #pragma clang diagnostic pop
  93. XCTAssertEqualObjects(restoredInfo.authorizedEntity, sparseInfo.authorizedEntity);
  94. XCTAssertEqualObjects(restoredInfo.scope, sparseInfo.scope);
  95. XCTAssertEqualObjects(restoredInfo.token, sparseInfo.token);
  96. XCTAssertNil(restoredInfo.appVersion);
  97. XCTAssertNil(restoredInfo.firebaseAppID);
  98. XCTAssertNil(restoredInfo.cacheTime);
  99. XCTAssertNil(restoredInfo.APNSInfo);
  100. }
  101. - (void)testTokenFreshnessWithLocaleChange {
  102. // Default should be fresh because we mock last fetch token time just now.
  103. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  104. // Locale change should affect token refreshness.
  105. // Set to a different locale than the current locale.
  106. [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant"
  107. forKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
  108. [[NSUserDefaults standardUserDefaults] synchronize];
  109. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  110. // Reset locale
  111. [[NSUserDefaults standardUserDefaults] setObject:FIRMessagingCurrentLocale()
  112. forKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
  113. [[NSUserDefaults standardUserDefaults] synchronize];
  114. }
  115. - (void)testTokenFreshnessWithTokenTimestampChange {
  116. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  117. // Set last fetch token time 7 days ago.
  118. NSTimeInterval lastFetchTokenTimestamp =
  119. FIRMessagingCurrentTimestampInSeconds() - 7 * 24 * 60 * 60;
  120. self.validTokenInfo.cacheTime = [NSDate dateWithTimeIntervalSince1970:lastFetchTokenTimestamp];
  121. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  122. // Set last fetch token time more than 7 days ago.
  123. lastFetchTokenTimestamp = FIRMessagingCurrentTimestampInSeconds() - 8 * 24 * 60 * 60;
  124. self.validTokenInfo.cacheTime = [NSDate dateWithTimeIntervalSince1970:lastFetchTokenTimestamp];
  125. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  126. // Set last fetch token time nil to mock legacy storage format. Token should be considered not
  127. // fresh.
  128. self.validTokenInfo.cacheTime = nil;
  129. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  130. }
  131. - (void)testTokenFreshnessWithFirebaseAppIDChange {
  132. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  133. // Change Firebase App ID.
  134. [FIROptions defaultOptions].googleAppID = @"newFirebaseAppID:ios:abcdefg";
  135. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  136. }
  137. - (void)testTokenFreshnessWithAppVersionChange {
  138. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  139. // Change app version.
  140. self.validTokenInfo =
  141. [[FIRMessagingTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
  142. scope:kScope
  143. token:kToken
  144. appVersion:@"1.1"
  145. firebaseAppID:FIRMessagingFirebaseAppID()];
  146. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  147. }
  148. - (void)testTokenInconsistentWithIID {
  149. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  150. // Change token.
  151. self.validTokenInfo = [[FIRMessagingTokenInfo alloc]
  152. initWithAuthorizedEntity:kAuthorizedEntity
  153. scope:kScope
  154. token:@"cxhhwVY27AE:APA91bGfnlnbinRVE7nUwJSr_k6cuSTKectOlt66dKv1r_-"
  155. @"9Qvhy9XljAI62QPw307rgA0MaFHPnrU5sFxGZvsncRnkfuciwTUeyRpPNDZMFhNXt7"
  156. @"h1BKq9Wb2A0LAANpQefrPHVUp4p"
  157. appVersion:@"1.1"
  158. firebaseAppID:FIRMessagingFirebaseAppID()];
  159. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  160. }
  161. @end