FIRInstanceIDCheckinStoreTest.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 NSString *const kFakeErrorDomain = @"fakeDomain";
  42. static const NSUInteger kFakeErrorCode = -1;
  43. static int64_t const kLastCheckinTimestamp = 123456;
  44. @interface FIRInstanceIDCheckinStoreTest : XCTestCase
  45. @end
  46. @implementation FIRInstanceIDCheckinStoreTest
  47. - (void)setUp {
  48. [super setUp];
  49. [FIRInstanceIDStore createSubDirectory:kSubDirectoryName];
  50. }
  51. - (void)tearDown {
  52. NSString *path = [self pathForCheckinPlist];
  53. if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
  54. NSError *error;
  55. [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
  56. }
  57. [FIRInstanceIDStore removeSubDirectory:kSubDirectoryName error:nil];
  58. [super tearDown];
  59. }
  60. /**
  61. * Keychain read failure should lead to checkin preferences with invalid credentials.
  62. */
  63. - (void)testInvalidCheckinPreferencesOnKeychainFail {
  64. XCTestExpectation *checkinInvalidExpectation = [self
  65. expectationWithDescription:@"Checkin preference should be invalid after keychain failure"];
  66. FIRInstanceIDBackupExcludedPlist *checkinPlist =
  67. [[FIRInstanceIDBackupExcludedPlist alloc] initWithFileName:kFakeCheckinPlistName
  68. subDirectory:kSubDirectoryName];
  69. FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc] init];
  70. FIRInstanceIDCheckinStore *checkinStore =
  71. [[FIRInstanceIDCheckinStore alloc] initWithCheckinPlist:checkinPlist keychain:fakeKeychain];
  72. __block FIRInstanceIDCheckinPreferences *preferences =
  73. [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:kAuthID secretToken:kSecret];
  74. [preferences updateWithCheckinPlistContents:[[self class] newCheckinPlistPreferences]];
  75. [checkinStore saveCheckinPreferences:preferences
  76. handler:^(NSError *error) {
  77. XCTAssertNil(error);
  78. fakeKeychain.cannotReadFromKeychain = YES;
  79. preferences = [checkinStore cachedCheckinPreferences];
  80. XCTAssertNil(preferences.deviceID);
  81. XCTAssertNil(preferences.secretToken);
  82. XCTAssertFalse([preferences hasValidCheckinInfo]);
  83. [checkinInvalidExpectation fulfill];
  84. }];
  85. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  86. }
  87. /**
  88. * CheckinStore should not be able to save the checkin preferences if the write to the
  89. * Keychain fails.
  90. */
  91. - (void)testCheckinSaveFailsOnKeychainWriteFailure {
  92. XCTestExpectation *checkinSaveFailsExpectation =
  93. [self expectationWithDescription:@"Checkin save should fail after keychain write failure"];
  94. FIRInstanceIDBackupExcludedPlist *checkinPlist =
  95. [[FIRInstanceIDBackupExcludedPlist alloc] initWithFileName:kFakeCheckinPlistName
  96. subDirectory:kSubDirectoryName];
  97. FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc] init];
  98. fakeKeychain.cannotWriteToKeychain = YES;
  99. FIRInstanceIDCheckinStore *checkinStore =
  100. [[FIRInstanceIDCheckinStore alloc] initWithCheckinPlist:checkinPlist keychain:fakeKeychain];
  101. __block FIRInstanceIDCheckinPreferences *preferences =
  102. [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:kAuthID secretToken:kSecret];
  103. [preferences updateWithCheckinPlistContents:[[self class] newCheckinPlistPreferences]];
  104. [checkinStore saveCheckinPreferences:preferences
  105. handler:^(NSError *error) {
  106. XCTAssertNotNil(error);
  107. preferences = [checkinStore cachedCheckinPreferences];
  108. XCTAssertNil(preferences.deviceID);
  109. XCTAssertNil(preferences.secretToken);
  110. XCTAssertFalse([preferences hasValidCheckinInfo]);
  111. [checkinSaveFailsExpectation fulfill];
  112. }];
  113. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  114. }
  115. - (void)testCheckinSaveFailsOnPlistWriteFailure {
  116. XCTestExpectation *checkinSaveFailsExpectation =
  117. [self expectationWithDescription:@"Checkin save should fail after plist write failure"];
  118. FIRInstanceIDBackupExcludedPlist *checkinPlist =
  119. [[FIRInstanceIDBackupExcludedPlist alloc] initWithFileName:kFakeCheckinPlistName
  120. subDirectory:kSubDirectoryName];
  121. id plistMock = OCMPartialMock(checkinPlist);
  122. NSError *error = [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:nil];
  123. OCMStub([plistMock writeDictionary:[OCMArg any] error:[OCMArg setTo:error]]).andReturn(NO);
  124. FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc] init];
  125. FIRInstanceIDCheckinStore *checkinStore =
  126. [[FIRInstanceIDCheckinStore alloc] initWithCheckinPlist:plistMock keychain:fakeKeychain];
  127. __block FIRInstanceIDCheckinPreferences *preferences =
  128. [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:kAuthID secretToken:kSecret];
  129. [preferences updateWithCheckinPlistContents:[[self class] newCheckinPlistPreferences]];
  130. [checkinStore saveCheckinPreferences:preferences
  131. handler:^(NSError *error) {
  132. XCTAssertNotNil(error);
  133. XCTAssertEqual(error.code, kFakeErrorCode);
  134. preferences = [checkinStore cachedCheckinPreferences];
  135. XCTAssertNil(preferences.deviceID);
  136. XCTAssertNil(preferences.secretToken);
  137. XCTAssertFalse([preferences hasValidCheckinInfo]);
  138. [checkinSaveFailsExpectation fulfill];
  139. }];
  140. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  141. }
  142. - (void)testCheckinSaveSuccess {
  143. XCTestExpectation *checkinSaveSuccessExpectation =
  144. [self expectationWithDescription:@"Checkin save should succeed"];
  145. FIRInstanceIDBackupExcludedPlist *checkinPlist =
  146. [[FIRInstanceIDBackupExcludedPlist alloc] initWithFileName:kFakeCheckinPlistName
  147. subDirectory:kSubDirectoryName];
  148. id plistMock = OCMPartialMock(checkinPlist);
  149. FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc] init];
  150. FIRInstanceIDCheckinStore *checkinStore =
  151. [[FIRInstanceIDCheckinStore alloc] initWithCheckinPlist:plistMock keychain:fakeKeychain];
  152. __block FIRInstanceIDCheckinPreferences *preferences =
  153. [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:kAuthID secretToken:kSecret];
  154. [preferences updateWithCheckinPlistContents:[[self class] newCheckinPlistPreferences]];
  155. [checkinStore saveCheckinPreferences:preferences
  156. handler:^(NSError *error) {
  157. XCTAssertNil(error);
  158. preferences = [checkinStore cachedCheckinPreferences];
  159. XCTAssertEqualObjects(preferences.deviceID, kAuthID);
  160. XCTAssertEqualObjects(preferences.secretToken, kSecret);
  161. [checkinSaveSuccessExpectation fulfill];
  162. }];
  163. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  164. }
  165. // Write fake checkin data to legacy location, then test if migration worked.
  166. - (void)testCheckinMigrationMovesToNewLocationInKeychain {
  167. XCTestExpectation *checkinMigrationExpectation =
  168. [self expectationWithDescription:@"checkin migration should move to the new location"];
  169. // Create checkin store class.
  170. FIRInstanceIDBackupExcludedPlist *checkinPlist =
  171. [[FIRInstanceIDBackupExcludedPlist alloc] initWithFileName:kFakeCheckinPlistName
  172. subDirectory:kSubDirectoryName];
  173. FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc] init];
  174. FIRInstanceIDFakeKeychain *weakKeychain = fakeKeychain;
  175. // Create fake checkin preferences object.
  176. FIRInstanceIDCheckinPreferences *preferences =
  177. [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:kAuthID secretToken:kSecret];
  178. [preferences updateWithCheckinPlistContents:[[self class] newCheckinPlistPreferences]];
  179. // Write checkin into legacy location in Fake keychain.
  180. NSString *checkinKeychainContent = [preferences checkinKeychainContent];
  181. NSData *data = [checkinKeychainContent dataUsingEncoding:NSUTF8StringEncoding];
  182. [fakeKeychain setData:data
  183. forService:kFIRInstanceIDLegacyCheckinKeychainService
  184. accessibility:nil
  185. account:kFIRInstanceIDLegacyCheckinKeychainAccount
  186. handler:^(NSError *error) {
  187. XCTAssertNil(error);
  188. // Check that we saved it correctly to the legacy location.
  189. NSData *dataInLegacyLocation =
  190. [weakKeychain dataForService:kFIRInstanceIDLegacyCheckinKeychainService
  191. account:kFIRInstanceIDLegacyCheckinKeychainAccount];
  192. XCTAssertNotNil(dataInLegacyLocation);
  193. FIRInstanceIDCheckinStore *checkinStore =
  194. [[FIRInstanceIDCheckinStore alloc] initWithCheckinPlist:checkinPlist
  195. keychain:weakKeychain];
  196. // Perform migration.
  197. [checkinStore migrateCheckinItemIfNeeded];
  198. // Ensure the item is no longer in the old location.
  199. dataInLegacyLocation =
  200. [weakKeychain dataForService:kFIRInstanceIDLegacyCheckinKeychainService
  201. account:kFIRInstanceIDLegacyCheckinKeychainAccount];
  202. XCTAssertNil(dataInLegacyLocation);
  203. // Check that it exists in the new location.
  204. NSData *dataInMigratedLocation =
  205. [weakKeychain dataForService:kFIRInstanceIDCheckinKeychainService
  206. account:checkinStore.bundleIdentifierForKeychainAccount];
  207. XCTAssertNotNil(dataInMigratedLocation);
  208. // Ensure that the data is the same as what we originally saved.
  209. XCTAssertEqualObjects(dataInMigratedLocation, data);
  210. [checkinMigrationExpectation fulfill];
  211. }];
  212. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  213. }
  214. #pragma mark - Private Helpers
  215. - (BOOL)savePreferencesToPlist:(NSDictionary *)preferences {
  216. NSString *path = [self pathForCheckinPlist];
  217. return [preferences writeToFile:path atomically:YES];
  218. }
  219. - (NSString *)pathForCheckinPlist {
  220. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  221. NSString *plistNameWithExtension = [NSString stringWithFormat:@"%@.plist", kFakeCheckinPlistName];
  222. return [paths[0] stringByAppendingPathComponent:plistNameWithExtension];
  223. }
  224. + (NSDictionary *)checkinPreferences {
  225. return @{
  226. kFIRInstanceIDDeviceAuthIdKey : kAuthID,
  227. kFIRInstanceIDSecretTokenKey : kSecret,
  228. kFIRInstanceIDDigestStringKey : kDigest,
  229. kFIRInstanceIDGServicesDictionaryKey : @{},
  230. kFIRInstanceIDLastCheckinTimeKey : @(kLastCheckinTimestamp),
  231. };
  232. }
  233. + (NSDictionary *)newCheckinPlistPreferences {
  234. NSMutableDictionary *oldPreferences = [[self checkinPreferences] mutableCopy];
  235. [oldPreferences removeObjectForKey:kFIRInstanceIDDeviceAuthIdKey];
  236. [oldPreferences removeObjectForKey:kFIRInstanceIDSecretTokenKey];
  237. oldPreferences[kFIRInstanceIDLastCheckinTimeKey] =
  238. @(FIRInstanceIDCurrentTimestampInMilliseconds() - 1000);
  239. return [oldPreferences copy];
  240. }
  241. @end