FIRInstanceIDTokenInfoTest.m 7.6 KB

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