FIRInstanceIDTokenInfoTest.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "Firebase/InstanceID/FIRInstanceIDTokenInfo.h"
  17. #import <XCTest/XCTest.h>
  18. #import <OCMock/OCMock.h>
  19. #import "Firebase/InstanceID/FIRInstanceIDAPNSInfo.h"
  20. #import "Firebase/InstanceID/FIRInstanceIDUtilities.h"
  21. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.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 FIRInstanceIDTokenInfoTest : XCTestCase
  31. @property(nonatomic, strong) NSData *APNSDeviceToken;
  32. @property(nonatomic, strong) FIRInstanceIDTokenInfo *validTokenInfo;
  33. @property(nonatomic, strong) id mockOptions;
  34. @end
  35. @implementation FIRInstanceIDTokenInfoTest
  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. [[FIRInstanceIDTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
  45. scope:kScope
  46. token:kToken
  47. appVersion:FIRInstanceIDCurrentAppVersion()
  48. firebaseAppID:FIRInstanceIDFirebaseAppID()];
  49. self.validTokenInfo.APNSInfo =
  50. [[FIRInstanceIDAPNSInfo alloc] initWithDeviceToken:self.APNSDeviceToken
  51. isSandbox:kAPNSSandbox];
  52. self.validTokenInfo.cacheTime = [NSDate date];
  53. }
  54. - (void)tearDown {
  55. [self.mockOptions stopMocking];
  56. [super tearDown];
  57. }
  58. - (void)testTokenInfoCreationWithInvalidArchive {
  59. NSData *badData = [@"badData" dataUsingEncoding:NSUTF8StringEncoding];
  60. FIRInstanceIDTokenInfo *info = nil;
  61. @try {
  62. #pragma clang diagnostic push
  63. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  64. info = [NSKeyedUnarchiver unarchiveObjectWithData:badData];
  65. #pragma clang diagnostic pop
  66. } @catch (NSException *e) {
  67. XCTAssertEqualObjects([e name], @"NSInvalidArgumentException");
  68. }
  69. XCTAssertNil(info);
  70. }
  71. // Test that archiving a FIRInstanceIDTokenInfo object and restoring it from the archive
  72. // yields the same values for all the fields.
  73. - (void)testTokenInfoEncodingAndDecoding {
  74. FIRInstanceIDTokenInfo *info = self.validTokenInfo;
  75. #pragma clang diagnostic push
  76. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  77. NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:info];
  78. FIRInstanceIDTokenInfo *restoredInfo = [NSKeyedUnarchiver unarchiveObjectWithData:archive];
  79. #pragma clang diagnostic pop
  80. XCTAssertEqualObjects(restoredInfo.authorizedEntity, info.authorizedEntity);
  81. XCTAssertEqualObjects(restoredInfo.scope, info.scope);
  82. XCTAssertEqualObjects(restoredInfo.token, info.token);
  83. XCTAssertEqualObjects(restoredInfo.appVersion, info.appVersion);
  84. XCTAssertEqualObjects(restoredInfo.firebaseAppID, info.firebaseAppID);
  85. XCTAssertEqualObjects(restoredInfo.cacheTime, info.cacheTime);
  86. XCTAssertEqualObjects(restoredInfo.APNSInfo.deviceToken, info.APNSInfo.deviceToken);
  87. XCTAssertEqual(restoredInfo.APNSInfo.sandbox, info.APNSInfo.sandbox);
  88. }
  89. // Test that archiving a FIRInstanceIDTokenInfo object with missing fields and restoring it
  90. // from the archive yields the same values for all the fields.
  91. - (void)testTokenInfoEncodingAndDecodingWithMissingFields {
  92. // Don't include appVersion, firebaseAppID, APNSInfo and cacheTime
  93. FIRInstanceIDTokenInfo *sparseInfo =
  94. [[FIRInstanceIDTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
  95. scope:kScope
  96. token:kToken
  97. appVersion:nil
  98. firebaseAppID:nil];
  99. #pragma clang diagnostic push
  100. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  101. NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:sparseInfo];
  102. FIRInstanceIDTokenInfo *restoredInfo = [NSKeyedUnarchiver unarchiveObjectWithData:archive];
  103. #pragma clang diagnostic pop
  104. XCTAssertEqualObjects(restoredInfo.authorizedEntity, sparseInfo.authorizedEntity);
  105. XCTAssertEqualObjects(restoredInfo.scope, sparseInfo.scope);
  106. XCTAssertEqualObjects(restoredInfo.token, sparseInfo.token);
  107. XCTAssertNil(restoredInfo.appVersion);
  108. XCTAssertNil(restoredInfo.firebaseAppID);
  109. XCTAssertNil(restoredInfo.cacheTime);
  110. XCTAssertNil(restoredInfo.APNSInfo);
  111. }
  112. - (void)testTokenFreshnessWithLocaleChange {
  113. // Default should be fresh because we mock last fetch token time just now.
  114. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  115. // Locale change should affect token refreshness.
  116. // Set to a different locale than the current locale.
  117. [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant"
  118. forKey:kFIRInstanceIDUserDefaultsKeyLocale];
  119. [[NSUserDefaults standardUserDefaults] synchronize];
  120. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  121. // Reset locale
  122. [[NSUserDefaults standardUserDefaults] setObject:FIRInstanceIDCurrentLocale()
  123. forKey:kFIRInstanceIDUserDefaultsKeyLocale];
  124. [[NSUserDefaults standardUserDefaults] synchronize];
  125. }
  126. - (void)testTokenFreshnessWithTokenTimestampChange {
  127. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  128. // Set last fetch token time 7 days ago.
  129. NSTimeInterval lastFetchTokenTimestamp =
  130. FIRInstanceIDCurrentTimestampInSeconds() - 7 * 24 * 60 * 60;
  131. self.validTokenInfo.cacheTime = [NSDate dateWithTimeIntervalSince1970:lastFetchTokenTimestamp];
  132. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  133. // Set last fetch token time more than 7 days ago.
  134. lastFetchTokenTimestamp = FIRInstanceIDCurrentTimestampInSeconds() - 8 * 24 * 60 * 60;
  135. self.validTokenInfo.cacheTime = [NSDate dateWithTimeIntervalSince1970:lastFetchTokenTimestamp];
  136. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  137. // Set last fetch token time nil to mock legacy storage format. Token should be considered not
  138. // fresh.
  139. self.validTokenInfo.cacheTime = nil;
  140. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  141. }
  142. - (void)testTokenFreshnessWithFirebaseAppIDChange {
  143. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  144. // Change Firebase App ID.
  145. [FIROptions defaultOptions].googleAppID = @"newFirebaseAppID:ios:abcdefg";
  146. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  147. }
  148. - (void)testTokenFreshnessWithAppVersionChange {
  149. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  150. // Change app version.
  151. self.validTokenInfo =
  152. [[FIRInstanceIDTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
  153. scope:kScope
  154. token:kToken
  155. appVersion:@"1.1"
  156. firebaseAppID:FIRInstanceIDFirebaseAppID()];
  157. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  158. }
  159. - (void)testTokenInconsistentWithIID {
  160. XCTAssertTrue([self.validTokenInfo isFreshWithIID:kIID]);
  161. // Change token.
  162. self.validTokenInfo = [[FIRInstanceIDTokenInfo alloc]
  163. initWithAuthorizedEntity:kAuthorizedEntity
  164. scope:kScope
  165. token:@"cxhhwVY27AE:APA91bGfnlnbinRVE7nUwJSr_k6cuSTKectOlt66dKv1r_-"
  166. @"9Qvhy9XljAI62QPw307rgA0MaFHPnrU5sFxGZvsncRnkfuciwTUeyRpPNDZMFhNXt7"
  167. @"h1BKq9Wb2A0LAANpQefrPHVUp4p"
  168. appVersion:@"1.1"
  169. firebaseAppID:FIRInstanceIDFirebaseAppID()];
  170. XCTAssertFalse([self.validTokenInfo isFreshWithIID:kIID]);
  171. }
  172. @end