FIRInstanceIDCheckinStoreTest.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 <XCTest/XCTest.h>
  17. #import <OCMock/OCMock.h>
  18. #import "FIRInstanceIDFakeKeychain.h"
  19. #import "Firebase/InstanceID/FIRInstanceIDAuthKeyChain.h"
  20. #import "Firebase/InstanceID/FIRInstanceIDBackupExcludedPlist.h"
  21. #import "Firebase/InstanceID/FIRInstanceIDCheckinPreferences+Internal.h"
  22. #import "Firebase/InstanceID/FIRInstanceIDCheckinPreferences.h"
  23. #import "Firebase/InstanceID/FIRInstanceIDCheckinService.h"
  24. #import "Firebase/InstanceID/FIRInstanceIDCheckinStore.h"
  25. #import "Firebase/InstanceID/FIRInstanceIDStore.h"
  26. #import "Firebase/InstanceID/FIRInstanceIDUtilities.h"
  27. #import "Firebase/InstanceID/FIRInstanceIDVersionUtilities.h"
  28. static const NSTimeInterval kExpectationTimeout = 12;
  29. @interface FIRInstanceIDCheckinStore ()
  30. - (NSString *)bundleIdentifierForKeychainAccount;
  31. @end
  32. // Testing constants
  33. static NSString *const kFakeCheckinPlistName = @"com.google.test.IIDStoreTestCheckin";
  34. static NSString *const kApplicationSupportSubDirectoryName = @"FirebaseInstanceIDCheckinTest";
  35. static NSString *const kAuthorizedEntity = @"test-audience";
  36. static NSString *const kAuthID = @"test-auth-id";
  37. static NSString *const kDigest = @"test-digest";
  38. static NSString *const kScope = @"test-scope";
  39. static NSString *const kSecret = @"test-secret";
  40. static NSString *const kToken = @"test-token";
  41. static int64_t const kLastCheckinTimestamp = 123456;
  42. @interface FIRInstanceIDCheckinStoreTest : XCTestCase
  43. @end
  44. @implementation FIRInstanceIDCheckinStoreTest
  45. - (void)setUp {
  46. [super setUp];
  47. [FIRInstanceIDStore createApplicationSupportSubDirectory:kApplicationSupportSubDirectoryName];
  48. }
  49. - (void)tearDown {
  50. NSString *path = [self pathForCheckinPlist];
  51. if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
  52. NSError *error;
  53. [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
  54. }
  55. [FIRInstanceIDStore removeApplicationSupportSubDirectory:kApplicationSupportSubDirectoryName
  56. error:nil];
  57. [super tearDown];
  58. }
  59. /**
  60. * Keychain read failure should lead to checkin preferences with invalid credentials.
  61. */
  62. - (void)testInvalidCheckinPreferencesOnKeychainFail {
  63. XCTestExpectation *checkinInvalidExpectation = [self
  64. expectationWithDescription:@"Checkin preference should be invalid after keychain failure"];
  65. FIRInstanceIDBackupExcludedPlist *checkinPlist = [[FIRInstanceIDBackupExcludedPlist alloc]
  66. initWithFileName:kFakeCheckinPlistName
  67. applicationSupportSubDirectory:kApplicationSupportSubDirectoryName];
  68. FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc] init];
  69. FIRInstanceIDCheckinStore *checkinStore =
  70. [[FIRInstanceIDCheckinStore alloc] initWithCheckinPlist:checkinPlist keychain:fakeKeychain];
  71. __block FIRInstanceIDCheckinPreferences *preferences =
  72. [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:kAuthID secretToken:kSecret];
  73. [preferences updateWithCheckinPlistContents:[[self class] newCheckinPlistPreferences]];
  74. [checkinStore saveCheckinPreferences:preferences
  75. handler:^(NSError *error) {
  76. XCTAssertNil(error);
  77. fakeKeychain.cannotReadFromKeychain = YES;
  78. preferences = [checkinStore cachedCheckinPreferences];
  79. XCTAssertNil(preferences.deviceID);
  80. XCTAssertNil(preferences.secretToken);
  81. XCTAssertFalse([preferences hasValidCheckinInfo]);
  82. [checkinInvalidExpectation fulfill];
  83. }];
  84. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  85. }
  86. /**
  87. * CheckinStore should not be able to save the checkin preferences if the write to the
  88. * Keychain fails.
  89. */
  90. - (void)testCheckinSaveFailsOnKeychainWriteFailure {
  91. XCTestExpectation *checkinSaveFailsExpectation =
  92. [self expectationWithDescription:@"Checkin save should fail after keychain write failure"];
  93. FIRInstanceIDBackupExcludedPlist *checkinPlist = [[FIRInstanceIDBackupExcludedPlist alloc]
  94. initWithFileName:kFakeCheckinPlistName
  95. applicationSupportSubDirectory:kApplicationSupportSubDirectoryName];
  96. FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc] init];
  97. fakeKeychain.cannotWriteToKeychain = YES;
  98. FIRInstanceIDCheckinStore *checkinStore =
  99. [[FIRInstanceIDCheckinStore alloc] initWithCheckinPlist:checkinPlist keychain:fakeKeychain];
  100. __block FIRInstanceIDCheckinPreferences *preferences =
  101. [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:kAuthID secretToken:kSecret];
  102. [preferences updateWithCheckinPlistContents:[[self class] newCheckinPlistPreferences]];
  103. [checkinStore saveCheckinPreferences:preferences
  104. handler:^(NSError *error) {
  105. XCTAssertNotNil(error);
  106. preferences = [checkinStore cachedCheckinPreferences];
  107. XCTAssertNil(preferences.deviceID);
  108. XCTAssertNil(preferences.secretToken);
  109. XCTAssertFalse([preferences hasValidCheckinInfo]);
  110. [checkinSaveFailsExpectation fulfill];
  111. }];
  112. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  113. }
  114. // Write fake checkin data to legacy location, then test if migration worked.
  115. - (void)testCheckinMigrationMovesToNewLocationInKeychain {
  116. XCTestExpectation *checkinMigrationExpectation =
  117. [self expectationWithDescription:@"checkin migration should move to the new location"];
  118. // Create checkin store class.
  119. FIRInstanceIDBackupExcludedPlist *checkinPlist = [[FIRInstanceIDBackupExcludedPlist alloc]
  120. initWithFileName:kFakeCheckinPlistName
  121. applicationSupportSubDirectory:kApplicationSupportSubDirectoryName];
  122. FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc] init];
  123. FIRInstanceIDFakeKeychain *weakKeychain = fakeKeychain;
  124. // Create fake checkin preferences object.
  125. FIRInstanceIDCheckinPreferences *preferences =
  126. [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:kAuthID secretToken:kSecret];
  127. [preferences updateWithCheckinPlistContents:[[self class] newCheckinPlistPreferences]];
  128. // Write checkin into legacy location in Fake keychain.
  129. NSString *checkinKeychainContent = [preferences checkinKeychainContent];
  130. NSData *data = [checkinKeychainContent dataUsingEncoding:NSUTF8StringEncoding];
  131. [fakeKeychain setData:data
  132. forService:kFIRInstanceIDLegacyCheckinKeychainService
  133. accessibility:nil
  134. account:kFIRInstanceIDLegacyCheckinKeychainAccount
  135. handler:^(NSError *error) {
  136. XCTAssertNil(error);
  137. // Check that we saved it correctly to the legacy location.
  138. NSData *dataInLegacyLocation =
  139. [weakKeychain dataForService:kFIRInstanceIDLegacyCheckinKeychainService
  140. account:kFIRInstanceIDLegacyCheckinKeychainAccount];
  141. XCTAssertNotNil(dataInLegacyLocation);
  142. FIRInstanceIDCheckinStore *checkinStore =
  143. [[FIRInstanceIDCheckinStore alloc] initWithCheckinPlist:checkinPlist
  144. keychain:weakKeychain];
  145. // Perform migration.
  146. [checkinStore migrateCheckinItemIfNeeded];
  147. // Ensure the item is no longer in the old location.
  148. dataInLegacyLocation =
  149. [weakKeychain dataForService:kFIRInstanceIDLegacyCheckinKeychainService
  150. account:kFIRInstanceIDLegacyCheckinKeychainAccount];
  151. XCTAssertNil(dataInLegacyLocation);
  152. // Check that it exists in the new location.
  153. NSData *dataInMigratedLocation =
  154. [weakKeychain dataForService:kFIRInstanceIDCheckinKeychainService
  155. account:checkinStore.bundleIdentifierForKeychainAccount];
  156. XCTAssertNotNil(dataInMigratedLocation);
  157. // Ensure that the data is the same as what we originally saved.
  158. XCTAssertEqualObjects(dataInMigratedLocation, data);
  159. [checkinMigrationExpectation fulfill];
  160. }];
  161. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  162. }
  163. #pragma mark - Private Helpers
  164. - (BOOL)savePreferencesToPlist:(NSDictionary *)preferences {
  165. NSString *path = [self pathForCheckinPlist];
  166. return [preferences writeToFile:path atomically:YES];
  167. }
  168. - (NSString *)pathForCheckinPlist {
  169. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  170. NSString *plistNameWithExtension = [NSString stringWithFormat:@"%@.plist", kFakeCheckinPlistName];
  171. return [paths[0] stringByAppendingPathComponent:plistNameWithExtension];
  172. }
  173. + (NSDictionary *)checkinPreferences {
  174. return @{
  175. kFIRInstanceIDDeviceAuthIdKey : kAuthID,
  176. kFIRInstanceIDSecretTokenKey : kSecret,
  177. kFIRInstanceIDDigestStringKey : kDigest,
  178. kFIRInstanceIDGServicesDictionaryKey : @{},
  179. kFIRInstanceIDLastCheckinTimeKey : @(kLastCheckinTimestamp),
  180. };
  181. }
  182. + (NSDictionary *)newCheckinPlistPreferences {
  183. NSMutableDictionary *oldPreferences = [[self checkinPreferences] mutableCopy];
  184. [oldPreferences removeObjectForKey:kFIRInstanceIDDeviceAuthIdKey];
  185. [oldPreferences removeObjectForKey:kFIRInstanceIDSecretTokenKey];
  186. oldPreferences[kFIRInstanceIDLastCheckinTimeKey] =
  187. @(FIRInstanceIDCurrentTimestampInMilliseconds() - 1000);
  188. return [oldPreferences copy];
  189. }
  190. @end