FIRInstanceIDTokenInfoTest.m 7.7 KB

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