FIRInstanceIDTokenInfoTest.m 7.2 KB

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