@@ -45,6 +45,8 @@ - (NSString *)bundleIdentifierForKeychainAccount;
45
45
static NSString *const kScope = @" test-scope" ;
46
46
static NSString *const kSecret = @" test-secret" ;
47
47
static NSString *const kToken = @" test-token" ;
48
+ static NSString *const kFakeErrorDomain = @" fakeDomain" ;
49
+ static const NSUInteger kFakeErrorCode = -1 ;
48
50
49
51
static int64_t const kLastCheckinTimestamp = 123456 ;
50
52
@@ -111,7 +113,6 @@ - (void)testCheckinSaveFailsOnKeychainWriteFailure {
111
113
FIRInstanceIDBackupExcludedPlist *checkinPlist =
112
114
[[FIRInstanceIDBackupExcludedPlist alloc ] initWithFileName: kFakeCheckinPlistName
113
115
subDirectory: kSubDirectoryName ];
114
-
115
116
FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc ] init ];
116
117
fakeKeychain.cannotWriteToKeychain = YES ;
117
118
@@ -134,6 +135,65 @@ - (void)testCheckinSaveFailsOnKeychainWriteFailure {
134
135
[self waitForExpectationsWithTimeout: kExpectationTimeout handler: nil ];
135
136
}
136
137
138
+ - (void )testCheckinSaveFailsOnPlistWriteFailure {
139
+ XCTestExpectation *checkinSaveFailsExpectation =
140
+ [self expectationWithDescription: @" Checkin save should fail after plist write failure" ];
141
+ FIRInstanceIDBackupExcludedPlist *checkinPlist =
142
+ [[FIRInstanceIDBackupExcludedPlist alloc ] initWithFileName: kFakeCheckinPlistName
143
+ subDirectory: kSubDirectoryName ];
144
+ id plistMock = OCMPartialMock (checkinPlist);
145
+ NSError *error = [NSError errorWithDomain: kFakeErrorDomain code: kFakeErrorCode userInfo: nil ];
146
+ OCMStub ([plistMock writeDictionary: [OCMArg any ] error: [OCMArg setTo: error]]).andReturn (NO );
147
+
148
+ FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc ] init ];
149
+
150
+ FIRInstanceIDCheckinStore *checkinStore =
151
+ [[FIRInstanceIDCheckinStore alloc ] initWithCheckinPlist: plistMock keychain: fakeKeychain];
152
+
153
+ __block FIRInstanceIDCheckinPreferences *preferences =
154
+ [[FIRInstanceIDCheckinPreferences alloc ] initWithDeviceID: kAuthID secretToken: kSecret ];
155
+ [preferences updateWithCheckinPlistContents: [[self class ] newCheckinPlistPreferences ]];
156
+ [checkinStore saveCheckinPreferences: preferences
157
+ handler: ^(NSError *error) {
158
+ XCTAssertNotNil (error);
159
+ XCTAssertEqual (error.code , kFakeErrorCode );
160
+
161
+ preferences = [checkinStore cachedCheckinPreferences ];
162
+ XCTAssertNil (preferences.deviceID );
163
+ XCTAssertNil (preferences.secretToken );
164
+ XCTAssertFalse ([preferences hasValidCheckinInfo ]);
165
+ [checkinSaveFailsExpectation fulfill ];
166
+ }];
167
+ [self waitForExpectationsWithTimeout: kExpectationTimeout handler: nil ];
168
+ }
169
+
170
+ - (void )testCheckinSaveSuccess {
171
+ XCTestExpectation *checkinSaveSuccessExpectation =
172
+ [self expectationWithDescription: @" Checkin save should succeed" ];
173
+ FIRInstanceIDBackupExcludedPlist *checkinPlist =
174
+ [[FIRInstanceIDBackupExcludedPlist alloc ] initWithFileName: kFakeCheckinPlistName
175
+ subDirectory: kSubDirectoryName ];
176
+ id plistMock = OCMPartialMock (checkinPlist);
177
+
178
+ FIRInstanceIDFakeKeychain *fakeKeychain = [[FIRInstanceIDFakeKeychain alloc ] init ];
179
+ FIRInstanceIDCheckinStore *checkinStore =
180
+ [[FIRInstanceIDCheckinStore alloc ] initWithCheckinPlist: plistMock keychain: fakeKeychain];
181
+
182
+ __block FIRInstanceIDCheckinPreferences *preferences =
183
+ [[FIRInstanceIDCheckinPreferences alloc ] initWithDeviceID: kAuthID secretToken: kSecret ];
184
+ [preferences updateWithCheckinPlistContents: [[self class ] newCheckinPlistPreferences ]];
185
+ [checkinStore saveCheckinPreferences: preferences
186
+ handler: ^(NSError *error) {
187
+ XCTAssertNil (error);
188
+
189
+ preferences = [checkinStore cachedCheckinPreferences ];
190
+ XCTAssertEqualObjects (preferences.deviceID , kAuthID );
191
+ XCTAssertEqualObjects (preferences.secretToken , kSecret );
192
+ [checkinSaveSuccessExpectation fulfill ];
193
+ }];
194
+ [self waitForExpectationsWithTimeout: kExpectationTimeout handler: nil ];
195
+ }
196
+
137
197
// Write fake checkin data to legacy location, then test if migration worked.
138
198
- (void )testCheckinMigrationMovesToNewLocationInKeychain {
139
199
XCTestExpectation *checkinMigrationExpectation =
0 commit comments