FIRInstanceIDCheckinStoreTest.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 kSubDirectoryName = @"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 createSubDirectory:kSubDirectoryName];
  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 removeSubDirectory:kSubDirectoryName error:nil];
  56. [super tearDown];
  57. }
  58. /**
  59. * Keychain read failure should lead to checkin preferences with invalid credentials.
  60. */
  61. - (void)testInvalidCheckinPreferencesOnKeychainFail {
  62. XCTestExpectation *checkinInvalidExpectation = [self
  63. expectationWithDescription:@"Checkin preference should be invalid after keychain failure"];
  64. FIRInstanceIDBackupExcludedPlist *checkinPlist =
  65. [[FIRInstanceIDBackupExcludedPlist alloc] initWithFileName:kFakeCheckinPlistName
  66. subDirectory:kSubDirectoryName];
  67. FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc] init];
  68. FIRInstanceIDCheckinStore *checkinStore =
  69. [[FIRInstanceIDCheckinStore alloc] initWithCheckinPlist:checkinPlist keychain:fakeKeychain];
  70. __block FIRInstanceIDCheckinPreferences *preferences =
  71. [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:kAuthID secretToken:kSecret];
  72. [preferences updateWithCheckinPlistContents:[[self class] newCheckinPlistPreferences]];
  73. [checkinStore saveCheckinPreferences:preferences
  74. handler:^(NSError *error) {
  75. XCTAssertNil(error);
  76. fakeKeychain.cannotReadFromKeychain = YES;
  77. preferences = [checkinStore cachedCheckinPreferences];
  78. XCTAssertNil(preferences.deviceID);
  79. XCTAssertNil(preferences.secretToken);
  80. XCTAssertFalse([preferences hasValidCheckinInfo]);
  81. [checkinInvalidExpectation fulfill];
  82. }];
  83. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  84. }
  85. /**
  86. * CheckinStore should not be able to save the checkin preferences if the write to the
  87. * Keychain fails.
  88. */
  89. - (void)testCheckinSaveFailsOnKeychainWriteFailure {
  90. XCTestExpectation *checkinSaveFailsExpectation =
  91. [self expectationWithDescription:@"Checkin save should fail after keychain write failure"];
  92. FIRInstanceIDBackupExcludedPlist *checkinPlist =
  93. [[FIRInstanceIDBackupExcludedPlist alloc] initWithFileName:kFakeCheckinPlistName
  94. subDirectory:kSubDirectoryName];
  95. FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc] init];
  96. fakeKeychain.cannotWriteToKeychain = YES;
  97. FIRInstanceIDCheckinStore *checkinStore =
  98. [[FIRInstanceIDCheckinStore alloc] initWithCheckinPlist:checkinPlist keychain:fakeKeychain];
  99. __block FIRInstanceIDCheckinPreferences *preferences =
  100. [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:kAuthID secretToken:kSecret];
  101. [preferences updateWithCheckinPlistContents:[[self class] newCheckinPlistPreferences]];
  102. [checkinStore saveCheckinPreferences:preferences
  103. handler:^(NSError *error) {
  104. XCTAssertNotNil(error);
  105. preferences = [checkinStore cachedCheckinPreferences];
  106. XCTAssertNil(preferences.deviceID);
  107. XCTAssertNil(preferences.secretToken);
  108. XCTAssertFalse([preferences hasValidCheckinInfo]);
  109. [checkinSaveFailsExpectation fulfill];
  110. }];
  111. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  112. }
  113. // Write fake checkin data to legacy location, then test if migration worked.
  114. - (void)testCheckinMigrationMovesToNewLocationInKeychain {
  115. XCTestExpectation *checkinMigrationExpectation =
  116. [self expectationWithDescription:@"checkin migration should move to the new location"];
  117. // Create checkin store class.
  118. FIRInstanceIDBackupExcludedPlist *checkinPlist =
  119. [[FIRInstanceIDBackupExcludedPlist alloc] initWithFileName:kFakeCheckinPlistName
  120. subDirectory:kSubDirectoryName];
  121. FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc] init];
  122. FIRInstanceIDFakeKeychain *weakKeychain = fakeKeychain;
  123. // Create fake checkin preferences object.
  124. FIRInstanceIDCheckinPreferences *preferences =
  125. [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:kAuthID secretToken:kSecret];
  126. [preferences updateWithCheckinPlistContents:[[self class] newCheckinPlistPreferences]];
  127. // Write checkin into legacy location in Fake keychain.
  128. NSString *checkinKeychainContent = [preferences checkinKeychainContent];
  129. NSData *data = [checkinKeychainContent dataUsingEncoding:NSUTF8StringEncoding];
  130. [fakeKeychain setData:data
  131. forService:kFIRInstanceIDLegacyCheckinKeychainService
  132. accessibility:nil
  133. account:kFIRInstanceIDLegacyCheckinKeychainAccount
  134. handler:^(NSError *error) {
  135. XCTAssertNil(error);
  136. // Check that we saved it correctly to the legacy location.
  137. NSData *dataInLegacyLocation =
  138. [weakKeychain dataForService:kFIRInstanceIDLegacyCheckinKeychainService
  139. account:kFIRInstanceIDLegacyCheckinKeychainAccount];
  140. XCTAssertNotNil(dataInLegacyLocation);
  141. FIRInstanceIDCheckinStore *checkinStore =
  142. [[FIRInstanceIDCheckinStore alloc] initWithCheckinPlist:checkinPlist
  143. keychain:weakKeychain];
  144. // Perform migration.
  145. [checkinStore migrateCheckinItemIfNeeded];
  146. // Ensure the item is no longer in the old location.
  147. dataInLegacyLocation =
  148. [weakKeychain dataForService:kFIRInstanceIDLegacyCheckinKeychainService
  149. account:kFIRInstanceIDLegacyCheckinKeychainAccount];
  150. XCTAssertNil(dataInLegacyLocation);
  151. // Check that it exists in the new location.
  152. NSData *dataInMigratedLocation =
  153. [weakKeychain dataForService:kFIRInstanceIDCheckinKeychainService
  154. account:checkinStore.bundleIdentifierForKeychainAccount];
  155. XCTAssertNotNil(dataInMigratedLocation);
  156. // Ensure that the data is the same as what we originally saved.
  157. XCTAssertEqualObjects(dataInMigratedLocation, data);
  158. [checkinMigrationExpectation fulfill];
  159. }];
  160. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  161. }
  162. #pragma mark - Private Helpers
  163. - (BOOL)savePreferencesToPlist:(NSDictionary *)preferences {
  164. NSString *path = [self pathForCheckinPlist];
  165. return [preferences writeToFile:path atomically:YES];
  166. }
  167. - (NSString *)pathForCheckinPlist {
  168. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  169. NSString *plistNameWithExtension = [NSString stringWithFormat:@"%@.plist", kFakeCheckinPlistName];
  170. return [paths[0] stringByAppendingPathComponent:plistNameWithExtension];
  171. }
  172. + (NSDictionary *)checkinPreferences {
  173. return @{
  174. kFIRInstanceIDDeviceAuthIdKey : kAuthID,
  175. kFIRInstanceIDSecretTokenKey : kSecret,
  176. kFIRInstanceIDDigestStringKey : kDigest,
  177. kFIRInstanceIDGServicesDictionaryKey : @{},
  178. kFIRInstanceIDLastCheckinTimeKey : @(kLastCheckinTimestamp),
  179. };
  180. }
  181. + (NSDictionary *)newCheckinPlistPreferences {
  182. NSMutableDictionary *oldPreferences = [[self checkinPreferences] mutableCopy];
  183. [oldPreferences removeObjectForKey:kFIRInstanceIDDeviceAuthIdKey];
  184. [oldPreferences removeObjectForKey:kFIRInstanceIDSecretTokenKey];
  185. oldPreferences[kFIRInstanceIDLastCheckinTimeKey] =
  186. @(FIRInstanceIDCurrentTimestampInMilliseconds() - 1000);
  187. return [oldPreferences copy];
  188. }
  189. @end