2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #import "keychain/ckks/tests/MockCloudKit.h"
27 #import "keychain/ckks/CKKS.h"
28 #import "keychain/ckks/CKKSRecordHolder.h"
29 #import "keychain/ckks/CKKSReachabilityTracker.h"
31 #import <CloudKit/CloudKit.h>
32 #import <CloudKit/CloudKit_Private.h>
33 #include <security_utilities/debugging.h>
34 #import <Foundation/Foundation.h>
37 @implementation FakeCKModifyRecordZonesOperation
38 @synthesize database = _database;
39 @synthesize recordZonesToSave = _recordZonesToSave;
40 @synthesize recordZoneIDsToDelete = _recordZoneIDsToDelete;
41 @synthesize modifyRecordZonesCompletionBlock = _modifyRecordZonesCompletionBlock;
42 @synthesize group = _group;
44 - (CKOperationConfiguration*)configuration {
45 return _configuration;
48 - (void)setConfiguration:(CKOperationConfiguration*)configuration {
50 _configuration = configuration;
52 _configuration = [[CKOperationConfiguration alloc] init];
56 - (instancetype)initWithRecordZonesToSave:(nullable NSArray<CKRecordZone *> *)recordZonesToSave recordZoneIDsToDelete:(nullable NSArray<CKRecordZoneID *> *)recordZoneIDsToDelete {
57 if(self = [super init]) {
58 _recordZonesToSave = recordZonesToSave;
59 _recordZoneIDsToDelete = recordZoneIDsToDelete;
60 _modifyRecordZonesCompletionBlock = nil;
62 _recordZonesSaved = nil;
63 _recordZoneIDsDeleted = nil;
66 __weak __typeof(self) weakSelf = self;
67 self.completionBlock = ^{
68 __strong __typeof(weakSelf) strongSelf = weakSelf;
70 secerror("ckks: received callback for released object");
74 strongSelf.modifyRecordZonesCompletionBlock(strongSelf.recordZonesSaved, strongSelf.recordZoneIDsDeleted, strongSelf.creationError);
81 // Create the zones we want; delete the ones we don't
82 // No error handling whatsoever
83 FakeCKDatabase* ckdb = [FakeCKModifyRecordZonesOperation ckdb];
85 for(CKRecordZone* zone in self.recordZonesToSave) {
86 bool skipCreation = false;
87 FakeCKZone* fakezone = ckdb[zone.zoneID];
88 if(fakezone.failCreationSilently) {
89 // Don't report an error, but do delete the zone
90 ckdb[zone.zoneID] = nil;
93 } else if(fakezone.creationError) {
95 // Not the best way to do this, but it's an error
96 // Needs fixing if you want to support multiple zone failures
97 self.creationError = fakezone.creationError;
100 ckdb[zone.zoneID] = nil;
103 } else if(fakezone) {
109 secnotice("ckks", "Creating zone %@", zone);
110 ckdb[zone.zoneID] = [[FakeCKZone alloc] initZone: zone.zoneID];
113 if(!self.recordZonesSaved) {
114 self.recordZonesSaved = [[NSMutableArray alloc] init];
116 [self.recordZonesSaved addObject:zone];
119 for(CKRecordZoneID* zoneID in self.recordZoneIDsToDelete) {
120 FakeCKZone* zone = ckdb[zoneID];
123 // The zone exists. Its deletion will succeed.
124 [FakeCKModifyRecordZonesOperation ensureZoneDeletionAllowed:zone];
127 if(!self.recordZoneIDsDeleted) {
128 self.recordZoneIDsDeleted = [[NSMutableArray alloc] init];
130 [self.recordZoneIDsDeleted addObject:zoneID];
132 // The zone does not exist! CloudKit will tell us that the deletion failed.
133 if(!self.creationError) {
134 self.creationError = [[CKPrettyError alloc] initWithDomain:CKErrorDomain code:CKErrorPartialFailure userInfo:nil];
137 // There really should be a better way to do this...
138 NSMutableDictionary* newDictionary = [self.creationError.userInfo mutableCopy] ?: [NSMutableDictionary dictionary];
139 NSMutableDictionary* newPartials = newDictionary[CKPartialErrorsByItemIDKey] ?: [NSMutableDictionary dictionary];
140 newPartials[zoneID] = [[CKPrettyError alloc] initWithDomain:CKErrorDomain code:CKErrorZoneNotFound
141 userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Mock CloudKit: zone '%@' not found", zoneID.zoneName]}];
142 newDictionary[CKPartialErrorsByItemIDKey] = newPartials;
144 self.creationError = [[CKPrettyError alloc] initWithDomain:self.creationError.domain code:self.creationError.code userInfo:newDictionary];
149 +(FakeCKDatabase*) ckdb {
150 // Shouldn't ever be called: must be mocked out.
151 @throw [NSException exceptionWithName:NSInternalInconsistencyException
152 reason:[NSString stringWithFormat:@"+ckdb[] must be mocked out for use"]
156 +(void)ensureZoneDeletionAllowed:(FakeCKZone*)zone {
157 // Shouldn't ever be called; will be mocked out
162 @implementation FakeCKModifySubscriptionsOperation
163 @synthesize database = _database;
164 @synthesize group = _group;
165 @synthesize subscriptionsToSave = _subscriptionsToSave;
166 @synthesize subscriptionIDsToDelete = _subscriptionIDsToDelete;
167 @synthesize modifySubscriptionsCompletionBlock = _modifySubscriptionsCompletionBlock;
169 - (CKOperationConfiguration*)configuration {
170 return _configuration;
173 - (void)setConfiguration:(CKOperationConfiguration*)configuration {
175 _configuration = configuration;
177 _configuration = [[CKOperationConfiguration alloc] init];
181 - (instancetype)initWithSubscriptionsToSave:(nullable NSArray<CKSubscription *> *)subscriptionsToSave subscriptionIDsToDelete:(nullable NSArray<NSString *> *)subscriptionIDsToDelete {
182 if(self = [super init]) {
183 _subscriptionsToSave = subscriptionsToSave;
184 _subscriptionIDsToDelete = subscriptionIDsToDelete;
185 _modifySubscriptionsCompletionBlock = nil;
187 __weak __typeof(self) weakSelf = self;
188 self.completionBlock = ^{
189 __strong __typeof(weakSelf) strongSelf = weakSelf;
191 secerror("ckks: received callback for released object");
195 strongSelf.modifySubscriptionsCompletionBlock(strongSelf.subscriptionsSaved, strongSelf.subscriptionIDsDeleted, strongSelf.subscriptionError);
202 FakeCKDatabase* ckdb = [FakeCKModifySubscriptionsOperation ckdb];
204 // Are these CKRecordZoneSubscription? Who knows!
205 for(CKRecordZoneSubscription* subscription in self.subscriptionsToSave) {
206 FakeCKZone* fakezone = ckdb[subscription.zoneID];
209 // This is an error: the zone doesn't exist
210 self.subscriptionError = [[CKPrettyError alloc] initWithDomain:CKErrorDomain
211 code:CKErrorPartialFailure
212 userInfo:@{CKPartialErrorsByItemIDKey:
213 @{subscription.zoneID:[[CKPrettyError alloc] initWithDomain:CKErrorDomain
214 code:CKErrorZoneNotFound
218 } else if(fakezone.subscriptionError) {
219 // Not the best way to do this, but it's an error
220 // Needs fixing if you want to support multiple zone failures
221 self.subscriptionError = fakezone.subscriptionError;
224 fakezone.subscriptionError = nil;
226 if(!self.subscriptionsSaved) {
227 self.subscriptionsSaved = [[NSMutableArray alloc] init];
229 [self.subscriptionsSaved addObject:subscription];
233 for(NSString* subscriptionID in self.subscriptionIDsToDelete) {
234 if(!self.subscriptionIDsDeleted) {
235 self.subscriptionIDsDeleted = [[NSMutableArray alloc] init];
238 [self.subscriptionIDsDeleted addObject:subscriptionID];
242 +(FakeCKDatabase*) ckdb {
243 // Shouldn't ever be called: must be mocked out.
244 @throw [NSException exceptionWithName:NSInternalInconsistencyException
245 reason:[NSString stringWithFormat:@"+ckdb[] must be mocked out for use"]
250 @implementation FakeCKFetchRecordZoneChangesOperation
251 @synthesize database = _database;
252 @synthesize recordZoneIDs = _recordZoneIDs;
253 @synthesize configurationsByRecordZoneID = _configurationsByRecordZoneID;
255 @synthesize fetchAllChanges = _fetchAllChanges;
256 @synthesize recordChangedBlock = _recordChangedBlock;
258 @synthesize recordWithIDWasDeletedBlock = _recordWithIDWasDeletedBlock;
259 @synthesize recordZoneChangeTokensUpdatedBlock = _recordZoneChangeTokensUpdatedBlock;
260 @synthesize recordZoneFetchCompletionBlock = _recordZoneFetchCompletionBlock;
261 @synthesize fetchRecordZoneChangesCompletionBlock = _fetchRecordZoneChangesCompletionBlock;
263 @synthesize operationID = _operationID;
264 @synthesize resolvedConfiguration = _resolvedConfiguration;
265 @synthesize group = _group;
267 - (CKOperationConfiguration*)configuration {
268 return _configuration;
271 - (void)setConfiguration:(CKOperationConfiguration*)configuration {
273 _configuration = configuration;
275 _configuration = [[CKOperationConfiguration alloc] init];
279 - (instancetype)initWithRecordZoneIDs:(NSArray<CKRecordZoneID *> *)recordZoneIDs configurationsByRecordZoneID:(nullable NSDictionary<CKRecordZoneID *, CKFetchRecordZoneChangesConfiguration *> *)configurationsByRecordZoneID {
280 if(self = [super init]) {
281 _recordZoneIDs = recordZoneIDs;
282 _configurationsByRecordZoneID = configurationsByRecordZoneID;
284 _operationID = @"fake-operation-ID";
290 // iterate through database, and return items that aren't in lastDatabase
291 FakeCKDatabase* ckdb = [FakeCKFetchRecordZoneChangesOperation ckdb];
292 if(self.recordZoneIDs.count == 0) {
293 secerror("fakeck: No zones to fetch. Likely a bug?");
296 for(CKRecordZoneID* zoneID in self.recordZoneIDs) {
297 FakeCKZone* zone = ckdb[zoneID];
299 // Only really supports a single zone failure
300 ckksnotice("fakeck", zoneID, "Fetched for a missing zone %@", zoneID);
301 NSError* zoneNotFoundError = [[CKPrettyError alloc] initWithDomain:CKErrorDomain
302 code:CKErrorZoneNotFound
304 NSError* error = [[CKPrettyError alloc] initWithDomain:CKErrorDomain
305 code:CKErrorPartialFailure
306 userInfo:@{CKPartialErrorsByItemIDKey: @{zoneID:zoneNotFoundError}}];
308 self.fetchRecordZoneChangesCompletionBlock(error);
312 SCNetworkReachabilityFlags reachabilityFlags = [CKKSReachabilityTracker getReachabilityFlags:NULL];
313 if ((reachabilityFlags & kSCNetworkReachabilityFlagsReachable) == 0) {
314 NSError *networkError = [NSError errorWithDomain:CKErrorDomain code:CKErrorNetworkFailure userInfo:NULL];
315 self.fetchRecordZoneChangesCompletionBlock(networkError);
319 // Not precisely correct in the case of multiple zone fetches. However, we don't currently do that, so it'll work for now.
320 NSError* mockError = [zone popFetchChangesError];
322 self.fetchRecordZoneChangesCompletionBlock(mockError);
326 // Extract the database at the last time they asked
327 CKServerChangeToken* fetchToken = self.configurationsByRecordZoneID[zoneID].previousServerChangeToken;
328 __block NSMutableDictionary<CKRecordID*, CKRecord*>* lastDatabase = nil;
329 __block NSDictionary<CKRecordID*, CKRecord*>* currentDatabase = nil;
330 __block CKServerChangeToken* currentChangeToken = nil;
332 dispatch_sync(zone.queue, ^{
333 lastDatabase = fetchToken ? zone.pastDatabases[fetchToken] : nil;
335 // You can fetch with the current change token; that's fine
336 if([fetchToken isEqual:zone.currentChangeToken]) {
337 lastDatabase = zone.currentDatabase;
340 currentDatabase = zone.currentDatabase;
341 currentChangeToken = zone.currentChangeToken;
344 ckksnotice("fakeck", zone.zoneID, "FakeCKFetchRecordZoneChangesOperation(%@): database is currently %@ change token %@ database then: %@", zone.zoneID, currentDatabase, fetchToken, lastDatabase);
346 if(!lastDatabase && fetchToken) {
347 ckksnotice("fakeck", zone.zoneID, "no database for this change token: failing fetch with 'CKErrorChangeTokenExpired'");
348 self.fetchRecordZoneChangesCompletionBlock([[CKPrettyError alloc]
349 initWithDomain:CKErrorDomain
350 code:CKErrorPartialFailure userInfo:@{CKPartialErrorsByItemIDKey:
351 @{zoneID:[[CKPrettyError alloc] initWithDomain:CKErrorDomain code:CKErrorChangeTokenExpired userInfo:@{}]}
356 [currentDatabase enumerateKeysAndObjectsUsingBlock:^(CKRecordID * _Nonnull recordID, CKRecord * _Nonnull record, BOOL * _Nonnull stop) {
357 id last = [lastDatabase objectForKey: recordID];
358 if(!last || ![record isEqual:last]) {
359 self.recordChangedBlock(record);
363 // iterate through lastDatabase, and delete items that aren't in database
364 [lastDatabase enumerateKeysAndObjectsUsingBlock:^(CKRecordID * _Nonnull recordID, CKRecord * _Nonnull record, BOOL * _Nonnull stop) {
366 id current = [currentDatabase objectForKey: recordID];
368 self.recordWithIDWasDeletedBlock(recordID, [record recordType]);
372 self.recordZoneChangeTokensUpdatedBlock(zoneID, currentChangeToken, nil);
373 self.recordZoneFetchCompletionBlock(zoneID, currentChangeToken, nil, NO, nil);
375 if(self.blockAfterFetch) {
376 self.blockAfterFetch();
381 self.fetchRecordZoneChangesCompletionBlock(nil);
384 +(FakeCKDatabase*) ckdb {
385 // Shouldn't ever be called: must be mocked out.
386 @throw [NSException exceptionWithName:NSInternalInconsistencyException
387 reason:[NSString stringWithFormat:@"+ckdb[] must be mocked out for use"]
392 @implementation FakeCKFetchRecordsOperation
393 @synthesize database = _database;
394 @synthesize recordIDs = _recordIDs;
395 @synthesize desiredKeys = _desiredKeys;
396 @synthesize configuration = _configuration;
398 @synthesize perRecordProgressBlock = _perRecordProgressBlock;
399 @synthesize perRecordCompletionBlock = _perRecordCompletionBlock;
401 @synthesize fetchRecordsCompletionBlock = _fetchRecordsCompletionBlock;
403 - (instancetype)init {
404 if((self = [super init])) {
409 - (instancetype)initWithRecordIDs:(NSArray<CKRecordID *> *)recordIDs {
410 if((self = [super init])) {
411 _recordIDs = recordIDs;
417 FakeCKDatabase* ckdb = [FakeCKFetchRecordsOperation ckdb];
419 // Doesn't call the per-record progress block
420 NSMutableDictionary<CKRecordID*, CKRecord*>* records = [NSMutableDictionary dictionary];
421 NSError* operror = nil;
423 for(CKRecordID* recordID in self.recordIDs) {
424 CKRecordZoneID* zoneID = recordID.zoneID;
425 FakeCKZone* zone = ckdb[zoneID];
428 ckksnotice("fakeck", zoneID, "Fetched for a missing zone %@", zoneID);
429 NSError* zoneNotFoundError = [[CKPrettyError alloc] initWithDomain:CKErrorDomain
430 code:CKErrorZoneNotFound
432 NSError* error = [[CKPrettyError alloc] initWithDomain:CKErrorDomain
433 code:CKErrorPartialFailure
434 userInfo:@{CKPartialErrorsByItemIDKey: @{zoneID:zoneNotFoundError}}];
436 // Not strictly right, but good enough for now
437 self.fetchRecordsCompletionBlock(nil, error);
441 CKRecord* record = zone.currentDatabase[recordID];
443 if(self.perRecordCompletionBlock) {
444 self.perRecordCompletionBlock(record, recordID, nil);
446 records[recordID] = record;
448 secerror("fakeck: Should be an error fetching %@", recordID);
451 operror = [[CKPrettyError alloc] initWithDomain:CKErrorDomain code:CKErrorPartialFailure userInfo:nil];
454 // There really should be a better way to do this...
455 NSMutableDictionary* newDictionary = [operror.userInfo mutableCopy] ?: [NSMutableDictionary dictionary];
456 NSMutableDictionary* newPartials = newDictionary[CKPartialErrorsByItemIDKey] ?: [NSMutableDictionary dictionary];
457 newPartials[recordID] = [[CKPrettyError alloc] initWithDomain:operror.domain code:CKErrorUnknownItem
458 userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Mock CloudKit: no record of %@", recordID]}];
459 newDictionary[CKPartialErrorsByItemIDKey] = newPartials;
461 operror = [[CKPrettyError alloc] initWithDomain:operror.domain code:operror.code userInfo:newDictionary];
463 /// TODO: do this better
464 if(self.perRecordCompletionBlock) {
465 self.perRecordCompletionBlock(nil, recordID, newPartials[zoneID]);
470 if(self.fetchRecordsCompletionBlock) {
471 self.fetchRecordsCompletionBlock(records, operror);
475 +(FakeCKDatabase*) ckdb {
476 // Shouldn't ever be called: must be mocked out.
477 @throw [NSException exceptionWithName:NSInternalInconsistencyException
478 reason:[NSString stringWithFormat:@"+ckdb[] must be mocked out for use"]
484 @implementation FakeCKQueryOperation
485 @synthesize query = _query;
486 @synthesize cursor = _cursor;
487 @synthesize zoneID = _zoneID;
488 @synthesize resultsLimit = _resultsLimit;
489 @synthesize desiredKeys = _desiredKeys;
490 @synthesize recordFetchedBlock = _recordFetchedBlock;
491 @synthesize queryCompletionBlock = _queryCompletionBlock;
493 - (instancetype)initWithQuery:(CKQuery *)query {
494 if((self = [super init])) {
501 FakeCKDatabase* ckdb = [FakeCKFetchRecordsOperation ckdb];
503 FakeCKZone* zone = ckdb[self.zoneID];
505 ckksnotice("fakeck", self.zoneID, "Queried a missing zone %@", self.zoneID);
507 // I'm really not sure if this is right, but...
508 NSError* zoneNotFoundError = [[CKPrettyError alloc] initWithDomain:CKErrorDomain
509 code:CKErrorZoneNotFound
511 self.queryCompletionBlock(nil, zoneNotFoundError);
515 NSMutableArray<CKRecord*>* matches = [NSMutableArray array];
516 for(CKRecordID* recordID in zone.currentDatabase.keyEnumerator) {
517 CKRecord* record = zone.currentDatabase[recordID];
519 if([self.query.recordType isEqualToString: record.recordType] &&
520 [self.query.predicate evaluateWithObject:record]) {
522 [matches addObject:record];
523 self.recordFetchedBlock(record);
527 if(self.queryCompletionBlock) {
528 // The query cursor will be non-null if there are more than self.resultsLimit classes. Don't implement this.
529 self.queryCompletionBlock(nil, nil);
534 +(FakeCKDatabase*) ckdb {
535 // Shouldn't ever be called: must be mocked out.
536 @throw [NSException exceptionWithName:NSInternalInconsistencyException
537 reason:[NSString stringWithFormat:@"+ckdb[] must be mocked out for use"]
544 // Do literally nothing
545 @implementation FakeAPSConnection
546 @synthesize delegate;
548 - (id)initWithEnvironmentName:(NSString *)environmentName namedDelegatePort:(NSString*)namedDelegatePort queue:(dispatch_queue_t)queue {
549 if(self = [super init]) {
554 - (void)setEnabledTopics:(NSArray *)enabledTopics {
559 // Do literally nothing
560 @implementation FakeNSNotificationCenter
561 + (instancetype)defaultCenter {
562 return [[FakeNSNotificationCenter alloc] init];
564 - (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject {
566 - (void)removeObserver:(id)observer {
570 @interface FakeCKZone ()
571 @property NSMutableArray<NSError*>* fetchErrors;
574 @implementation FakeCKZone
575 - (instancetype)initZone: (CKRecordZoneID*) zoneID {
576 if(self = [super init]) {
579 _currentDatabase = [[NSMutableDictionary alloc] init];
580 _pastDatabases = [[NSMutableDictionary alloc] init];
582 _fetchErrors = [[NSMutableArray alloc] init];
584 _queue = dispatch_queue_create("fake-ckzone", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
586 dispatch_sync(_queue, ^{
587 [self _onqueueRollChangeToken];
593 - (void)_onqueueRollChangeToken {
594 dispatch_assert_queue(self.queue);
596 NSData* changeToken = [[[NSUUID UUID] UUIDString] dataUsingEncoding:NSUTF8StringEncoding];
597 self.currentChangeToken = [[CKServerChangeToken alloc] initWithData: changeToken];
600 - (void)addToZone: (CKKSCKRecordHolder*) item zoneID: (CKRecordZoneID*) zoneID {
601 dispatch_sync(self.queue, ^{
602 [self _onqueueAddToZone:item zoneID:zoneID];
606 - (void)_onqueueAddToZone:(CKKSCKRecordHolder*)item zoneID:(CKRecordZoneID*)zoneID {
607 dispatch_assert_queue(self.queue);
609 CKRecord* record = [item CKRecordWithZoneID: zoneID];
610 [self _onqueueAddToZone: record];
613 item.storedCKRecord = record;
616 - (void)addToZone: (CKRecord*) record {
617 dispatch_sync(self.queue, ^{
618 [self _onqueueAddToZone:record];
622 - (void)_onqueueAddToZone:(CKRecord*)record {
623 dispatch_assert_queue(self.queue);
625 // Save off this current databse
626 self.pastDatabases[self.currentChangeToken] = [self.currentDatabase mutableCopy];
628 [self _onqueueRollChangeToken];
630 record.etag = [self.currentChangeToken description];
631 ckksnotice("fakeck", self.zoneID, "change tag: %@ %@", record.recordChangeTag, record.recordID);
632 record.modificationDate = [NSDate date];
633 self.currentDatabase[record.recordID] = record;
636 - (NSError * _Nullable)errorFromSavingRecord:(CKRecord*) record {
637 CKRecord* existingRecord = self.currentDatabase[record.recordID];
638 if(existingRecord && ![existingRecord.recordChangeTag isEqualToString: record.recordChangeTag]) {
639 ckksnotice("fakeck", self.zoneID, "change tag mismatch! Fail the write: %@ %@", record, existingRecord);
641 // TODO: doesn't yet support CKRecordChangedErrorAncestorRecordKey, since I don't understand it
642 return [[CKPrettyError alloc] initWithDomain:CKErrorDomain code:CKErrorServerRecordChanged
643 userInfo:@{CKRecordChangedErrorClientRecordKey:record,
644 CKRecordChangedErrorServerRecordKey:existingRecord}];
647 if(!existingRecord && record.etag != nil) {
648 ckksnotice("fakeck", self.zoneID, "update to a record that doesn't exist! Fail the write: %@", record);
649 return [[CKPrettyError alloc] initWithDomain:CKErrorDomain code:CKErrorUnknownItem
655 - (void)addCKRecordToZone:(CKRecord*) record {
656 if([self errorFromSavingRecord: record]) {
657 ckksnotice("fakeck", self.zoneID, "change tag mismatch! Fail the write!");
660 [self addToZone: record];
663 - (void)deleteFromHistory:(CKRecordID*)recordID {
664 for(NSMutableDictionary* pastDatabase in self.pastDatabases.objectEnumerator) {
665 [pastDatabase removeObjectForKey:recordID];
667 [self.currentDatabase removeObjectForKey:recordID];
671 - (NSError*)deleteCKRecordIDFromZone:(CKRecordID*) recordID {
672 // todo: fail somehow
673 dispatch_sync(self.queue, ^{
674 self.pastDatabases[self.currentChangeToken] = [self.currentDatabase mutableCopy];
675 [self _onqueueRollChangeToken];
677 [self.currentDatabase removeObjectForKey: recordID];
682 - (void)failNextFetchWith: (NSError*) fetchChangesError {
683 @synchronized(self.fetchErrors) {
684 [self.fetchErrors addObject: fetchChangesError];
688 - (NSError * _Nullable)popFetchChangesError {
689 NSError* error = nil;
690 @synchronized(self.fetchErrors) {
691 if(self.fetchErrors.count > 0) {
692 error = self.fetchErrors[0];
693 [self.fetchErrors removeObjectAtIndex:0];
700 @implementation FakeCKKSNotifier
701 +(void)post:(NSString*)notification {
703 // This isn't actually fake, but XCTest likes NSNotificationCenter a whole lot.
704 // These notifications shouldn't escape this process, so it's perfect.
705 secnotice("ckks", "sending fake NSNotification %@", notification);
706 [[NSNotificationCenter defaultCenter] postNotificationName:notification object:nil];