FIRInstanceIDTokenInfoTest.m 8.7 KB

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