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 "CloudKitMockXCTest.h"
28 #import <ApplePushService/ApplePushService.h>
29 #import <Foundation/Foundation.h>
30 #import <CloudKit/CloudKit.h>
31 #import <CloudKit/CloudKit_Private.h>
32 #import <CloudKit/CKContainer_Private.h>
33 #import <OCMock/OCMock.h>
35 #include "keychain/securityd/Regressions/SecdTestKeychainUtilities.h"
36 #include <utilities/SecFileLocations.h>
37 #include "keychain/securityd/SecItemServer.h"
40 #include "keychain/securityd/spi.h"
43 #include <Security/SecureObjectSync/SOSViews.h>
45 #include <utilities/SecDb.h>
46 #include "keychain/securityd/SecItemServer.h"
47 #include <keychain/ckks/CKKS.h>
48 #include <keychain/ckks/CKKSViewManager.h>
49 #include <keychain/ckks/CKKSKeychainView.h>
50 #include <keychain/ckks/CKKSItem.h>
51 #include <keychain/ckks/CKKSOutgoingQueueEntry.h>
52 #include <keychain/ckks/CKKSKey.h>
53 #include "keychain/ckks/CKKSGroupOperation.h"
54 #include "keychain/ckks/CKKSLockStateTracker.h"
55 #include "keychain/ckks/CKKSReachabilityTracker.h"
57 #import "tests/secdmockaks/mockaks.h"
58 #import "utilities/SecTapToRadar.h"
60 #import "MockCloudKit.h"
62 @interface BoolHolder : NSObject
66 @implementation BoolHolder
69 // Inform OCMock about the internals of CKContainer
70 @interface CKContainer ()
71 - (void)_checkSelfCloudServicesEntitlement;
75 @implementation CloudKitMockXCTest
76 @synthesize aksLockState = _aksLockState;
82 SecCKKSSetReduceRateLimiting(true);
86 securityd_init_local_spi();
90 - (BOOL)isRateLimited:(SecTapToRadar *)ttrRequest
92 return self.isTTRRatelimited;
95 - (BOOL)askUserIfTTR:(SecTapToRadar *)ttrRequest
100 - (void)triggerTapToRadar:(SecTapToRadar *)ttrRequest
102 [self.ttrExpectation fulfill];
108 NSString* testName = [self.name componentsSeparatedByString:@" "][1];
109 testName = [testName stringByReplacingOccurrencesOfString:@"]" withString:@""];
110 secnotice("ckkstest", "Beginning test %@", testName);
112 // All tests start with the same flag set.
113 SecCKKSTestResetFlags();
114 SecCKKSTestSetDisableSOS(true);
116 self.silentFetchesAllowed = true;
117 self.silentZoneDeletesAllowed = false; // Set to true if you want to do any deletes
119 __weak __typeof(self) weakSelf = self;
120 self.operationQueue = [[NSOperationQueue alloc] init];
121 self.operationQueue.maxConcurrentOperationCount = 1;
123 self.zones = [[NSMutableDictionary alloc] init];
125 self.apsEnvironment = @"fake APS push string";
127 // Static variables are a scourge. Let's reset this one...
128 [OctagonAPSReceiver resetGlobalEnviornmentMap];
130 self.mockDatabaseExceptionCatcher = OCMStrictClassMock([CKDatabase class]);
131 self.mockDatabase = OCMStrictClassMock([CKDatabase class]);
132 self.mockContainerExpectations = OCMStrictClassMock([CKContainer class]);
133 self.mockContainer = OCMClassMock([CKContainer class]);
134 OCMStub([self.mockContainer containerWithIdentifier:[OCMArg isKindOfClass:[NSString class]]]).andReturn(self.mockContainer);
135 OCMStub([self.mockContainer defaultContainer]).andReturn(self.mockContainer);
136 OCMStub([self.mockContainer alloc]).andReturn(self.mockContainer);
137 OCMStub([self.mockContainer containerIdentifier]).andReturn(SecCKKSContainerName);
138 OCMStub([self.mockContainer initWithContainerID: [OCMArg any] options: [OCMArg any]]).andReturn(self.mockContainer);
139 OCMStub([self.mockContainer privateCloudDatabase]).andReturn(self.mockDatabaseExceptionCatcher);
140 OCMStub([self.mockContainer serverPreferredPushEnvironmentWithCompletionHandler: ([OCMArg invokeBlockWithArgs:self.apsEnvironment, [NSNull null], nil])]);
141 OCMStub([self.mockContainer submitEventMetric:[OCMArg any]]).andCall(self, @selector(ckcontainerSubmitEventMetric:));
143 // Use two layers of mockDatabase here, so we can both add Expectations and catch the exception (instead of crash) when one fails.
144 OCMStub([self.mockDatabaseExceptionCatcher addOperation:[OCMArg any]]).andCall(self, @selector(ckdatabaseAddOperation:));
146 // If you want to change this, you'll need to update the mock
147 _ckDeviceID = [NSString stringWithFormat:@"fake-cloudkit-device-id-%@", testName];
148 OCMStub([self.mockContainer fetchCurrentDeviceIDWithCompletionHandler: ([OCMArg invokeBlockWithArgs:self.ckDeviceID, [NSNull null], nil])]);
150 self.accountStatus = CKAccountStatusAvailable;
151 self.iCloudHasValidCredentials = YES;
153 self.fakeHSA2AccountStatus = CKKSAccountStatusAvailable;
155 // Inject a fake operation dependency so we won't respond with the CloudKit account status immediately
156 // The CKKSAccountStateTracker won't send any login/logout calls without that information, so this blocks all CKKS setup
157 self.ckaccountHoldOperation = [NSBlockOperation named:@"ckaccount-hold" withBlock:^{
158 secnotice("ckks", "CKKS CK account status test hold released");
161 OCMStub([self.mockContainer accountStatusWithCompletionHandler:
162 [OCMArg checkWithBlock:^BOOL(void (^passedBlock) (CKAccountStatus accountStatus,
163 NSError * _Nullable error)) {
166 __strong __typeof(self) strongSelf = weakSelf;
167 NSBlockOperation* fulfillBlock = [NSBlockOperation named:@"account-status-completion" withBlock: ^{
168 passedBlock(weakSelf.accountStatus, nil);
170 [fulfillBlock addDependency: strongSelf.ckaccountHoldOperation];
171 [strongSelf.operationQueue addOperation: fulfillBlock];
178 OCMStub([self.mockContainer accountInfoWithCompletionHandler:
179 [OCMArg checkWithBlock:^BOOL(void (^passedBlock) (CKAccountInfo* accountInfo,
181 __strong __typeof(self) strongSelf = weakSelf;
182 if(passedBlock && strongSelf) {
183 NSBlockOperation* fulfillBlock = [NSBlockOperation named:@"account-info-completion" withBlock: ^{
184 __strong __typeof(self) blockStrongSelf = weakSelf;
185 CKAccountInfo* account = [[CKAccountInfo alloc] init];
186 account.accountStatus = blockStrongSelf.accountStatus;
187 account.hasValidCredentials = blockStrongSelf.iCloudHasValidCredentials;
188 account.accountPartition = CKAccountPartitionTypeProduction;
189 passedBlock((CKAccountInfo*)account, nil);
191 [fulfillBlock addDependency: strongSelf.ckaccountHoldOperation];
192 [strongSelf.operationQueue addOperation: fulfillBlock];
199 self.mockAccountStateTracker = OCMClassMock([CKKSAccountStateTracker class]);
200 OCMStub([self.mockAccountStateTracker getCircleStatus]).andCall(self, @selector(circleStatus));
202 // Fake out SOS peers
203 // One trusted non-self peer, but it doesn't have any Octagon keys. Your test can change this if it wants.
204 // However, note that [self putFakeDeviceStatusInCloudKit:] will likely not do what you want after you change this
205 CKKSSOSSelfPeer* currentSelfPeer = [[CKKSSOSSelfPeer alloc] initWithSOSPeerID:@"local-peer"
206 encryptionKey:[[SFECKeyPair alloc] initRandomKeyPairWithSpecifier:[[SFECKeySpecifier alloc] initWithCurve:SFEllipticCurveNistp384]]
207 signingKey:[[SFECKeyPair alloc] initRandomKeyPairWithSpecifier:[[SFECKeySpecifier alloc] initWithCurve:SFEllipticCurveNistp384]]
208 viewList:self.managedViewList];
210 self.mockSOSAdapter = [[CKKSMockSOSPresentAdapter alloc] initWithSelfPeer:currentSelfPeer
211 trustedPeers:[NSSet set]
214 // If we're in circle, come up with a fake circle id. Otherwise, return an error.
215 OCMStub([self.mockAccountStateTracker fetchCirclePeerID:
216 [OCMArg checkWithBlock:^BOOL(void (^passedBlock) (NSString* peerID,
218 __strong __typeof(self) strongSelf = weakSelf;
219 if(passedBlock && strongSelf) {
220 if(strongSelf.mockSOSAdapter.circleStatus == kSOSCCInCircle) {
221 passedBlock(strongSelf.mockSOSAdapter.selfPeer.peerID, nil);
223 passedBlock(nil, [NSError errorWithDomain:@"securityd" code:errSecInternalError userInfo:@{NSLocalizedDescriptionKey:@"no account, no circle id"}]);
231 self.aksLockState = false; // Lie and say AKS is always unlocked
232 self.mockLockStateTracker = OCMClassMock([CKKSLockStateTracker class]);
233 OCMStub([self.mockLockStateTracker queryAKSLocked]).andCall(self, @selector(aksLockState));
235 self.mockTTR = OCMClassMock([SecTapToRadar class]);
236 OCMStub([self.mockTTR isRateLimited:[OCMArg any]]).andCall(self, @selector(isRateLimited:));
237 OCMStub([self.mockTTR askUserIfTTR:[OCMArg any]]).andCall(self, @selector(askUserIfTTR:));
238 OCMStub([self.mockTTR triggerTapToRadar:[OCMArg any]]).andCall(self, @selector(triggerTapToRadar:));
239 self.isTTRRatelimited = true;
241 self.mockFakeCKModifyRecordZonesOperation = OCMClassMock([FakeCKModifyRecordZonesOperation class]);
242 OCMStub([self.mockFakeCKModifyRecordZonesOperation ckdb]).andReturn(self.zones);
243 OCMStub([self.mockFakeCKModifyRecordZonesOperation shouldFailModifyRecordZonesOperation]).andCall(self, @selector(shouldFailModifyRecordZonesOperation));
245 OCMStub([self.mockFakeCKModifyRecordZonesOperation ensureZoneDeletionAllowed:[OCMArg any]]).andCall(self, @selector(ensureZoneDeletionAllowed:));
247 self.mockFakeCKModifySubscriptionsOperation = OCMClassMock([FakeCKModifySubscriptionsOperation class]);
248 OCMStub([self.mockFakeCKModifySubscriptionsOperation ckdb]).andReturn(self.zones);
250 self.mockFakeCKFetchRecordZoneChangesOperation = OCMClassMock([FakeCKFetchRecordZoneChangesOperation class]);
251 OCMStub([self.mockFakeCKFetchRecordZoneChangesOperation ckdb]).andReturn(self.zones);
252 OCMStub([self.mockFakeCKFetchRecordZoneChangesOperation isNetworkReachable]).andCall(self, @selector(isNetworkReachable));
254 self.mockFakeCKFetchRecordsOperation = OCMClassMock([FakeCKFetchRecordsOperation class]);
255 OCMStub([self.mockFakeCKFetchRecordsOperation ckdb]).andReturn(self.zones);
257 self.mockFakeCKQueryOperation = OCMClassMock([FakeCKQueryOperation class]);
258 OCMStub([self.mockFakeCKQueryOperation ckdb]).andReturn(self.zones);
261 OCMStub([self.mockDatabase addOperation: [OCMArg checkWithBlock:^BOOL(id obj) {
262 __strong __typeof(self) strongSelf = weakSelf;
264 if ([obj isKindOfClass: [FakeCKFetchRecordZoneChangesOperation class]]) {
265 if(strongSelf.silentFetchesAllowed) {
268 FakeCKFetchRecordZoneChangesOperation *frzco = (FakeCKFetchRecordZoneChangesOperation *)obj;
269 [frzco addNullableDependency:strongSelf.ckFetchHoldOperation];
270 [strongSelf.operationQueue addOperation: frzco];
276 OCMStub([self.mockDatabase addOperation: [OCMArg checkWithBlock:^BOOL(id obj) {
277 __strong __typeof(self) strongSelf = weakSelf;
279 if ([obj isKindOfClass: [FakeCKFetchRecordsOperation class]]) {
280 if(strongSelf.silentFetchesAllowed) {
283 FakeCKFetchRecordsOperation *ffro = (FakeCKFetchRecordsOperation *)obj;
284 [ffro addNullableDependency:strongSelf.ckFetchHoldOperation];
285 [strongSelf.operationQueue addOperation: ffro];
291 OCMStub([self.mockDatabase addOperation: [OCMArg checkWithBlock:^BOOL(id obj) {
292 __strong __typeof(self) strongSelf = weakSelf;
294 if ([obj isKindOfClass: [FakeCKQueryOperation class]]) {
295 if(strongSelf.silentFetchesAllowed) {
298 FakeCKQueryOperation *fqo = (FakeCKQueryOperation *)obj;
299 [fqo addNullableDependency:strongSelf.ckFetchHoldOperation];
300 [strongSelf.operationQueue addOperation: fqo];
306 OCMStub([self.mockDatabase addOperation: [OCMArg checkWithBlock:^BOOL(id obj) {
307 __strong __typeof(self) strongSelf = weakSelf;
309 if ([obj isKindOfClass: [FakeCKModifyRecordZonesOperation class]]) {
310 FakeCKModifyRecordZonesOperation *frzco = (FakeCKModifyRecordZonesOperation *)obj;
311 [frzco addNullableDependency:strongSelf.ckModifyRecordZonesHoldOperation];
312 [strongSelf.operationQueue addOperation: frzco];
318 OCMStub([self.mockDatabase addOperation: [OCMArg checkWithBlock:^BOOL(id obj) {
319 __strong __typeof(self) strongSelf = weakSelf;
321 if ([obj isKindOfClass: [FakeCKModifySubscriptionsOperation class]]) {
322 FakeCKModifySubscriptionsOperation *frzco = (FakeCKModifySubscriptionsOperation *)obj;
323 [frzco addNullableDependency:strongSelf.ckModifySubscriptionsHoldOperation];
324 [strongSelf.operationQueue addOperation: frzco];
330 self.testZoneID = [[CKRecordZoneID alloc] initWithZoneName:@"testzone" ownerName:CKCurrentUserDefaultName];
332 // We don't want to use class mocks here, because they don't play well with partial mocks
333 CKKSCloudKitClassDependencies* cloudKitClassDependencies = [[CKKSCloudKitClassDependencies alloc] initWithFetchRecordZoneChangesOperationClass:[FakeCKFetchRecordZoneChangesOperation class]
334 fetchRecordsOperationClass:[FakeCKFetchRecordsOperation class]
335 queryOperationClass:[FakeCKQueryOperation class]
336 modifySubscriptionsOperationClass:[FakeCKModifySubscriptionsOperation class]
337 modifyRecordZonesOperationClass:[FakeCKModifyRecordZonesOperation class]
338 apsConnectionClass:[FakeAPSConnection class]
339 nsnotificationCenterClass:[FakeNSNotificationCenter class]
340 nsdistributednotificationCenterClass:[FakeNSDistributedNotificationCenter class]
341 notifierClass:[FakeCKKSNotifier class]];
343 self.mockCKKSViewManager = OCMPartialMock(
344 [[CKKSViewManager alloc] initWithContainerName:SecCKKSContainerName
345 usePCS:SecCKKSContainerUsePCS
346 sosAdapter:self.mockSOSAdapter
347 cloudKitClassDependencies:cloudKitClassDependencies]);
349 OCMStub([self.mockCKKSViewManager defaultViewList]).andCall(self, @selector(managedViewList));
350 OCMStub([self.mockCKKSViewManager syncBackupAndNotifyAboutSync]);
351 OCMStub([self.mockCKKSViewManager waitForTrustReady]).andReturn(YES);
353 self.injectedManager = self.mockCKKSViewManager;
355 [CKKSViewManager resetManager:false setTo:self.injectedManager];
357 // Lie and say network is available
358 [self.reachabilityTracker setNetworkReachability:true];
360 // Make a new fake keychain
361 NSString* tmp_dir = [NSString stringWithFormat: @"/tmp/%@.%X", testName, arc4random()];
362 [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat: @"%@/Library/Keychains", tmp_dir] withIntermediateDirectories:YES attributes:nil error:NULL];
364 SetCustomHomeURLString((__bridge CFStringRef) tmp_dir);
365 SecKeychainDbReset(NULL);
367 // Actually load the database.
368 kc_with_dbt(true, NULL, ^bool (SecDbConnectionRef dbt) { return false; });
371 - (SOSAccountStatus*)circleStatus {
372 NSError* error = nil;
373 SOSCCStatus status = [self.mockSOSAdapter circleStatus:&error];
374 return [[SOSAccountStatus alloc] init:status error:error];
379 return _aksLockState;
382 - (void)setAksLockState:(bool)aksLockState
386 [SecMockAKS lockClassA];
388 [SecMockAKS unlockAllClasses];
390 _aksLockState = aksLockState;
393 - (bool)isNetworkReachable {
394 return self.reachabilityTracker.currentReachability;
397 - (void)ckcontainerSubmitEventMetric:(CKEventMetric*)metric {
399 [self.mockContainerExpectations submitEventMetric:metric];
400 } @catch (NSException *exception) {
401 XCTFail("Received an container exception when trying to add a metric: %@", exception);
405 - (void)ckdatabaseAddOperation:(NSOperation*)op {
407 [self.mockDatabase addOperation:op];
408 } @catch (NSException *exception) {
409 XCTFail("Received an database exception: %@", exception);
413 - (NSError* _Nullable)shouldFailModifyRecordZonesOperation {
414 NSError* error = self.nextModifyRecordZonesError;
416 self.nextModifyRecordZonesError = nil;
422 - (void)ensureZoneDeletionAllowed:(FakeCKZone*)zone {
423 XCTAssertTrue(self.silentZoneDeletesAllowed, "Should be allowing zone deletes");
426 -(CKKSAccountStateTracker*)accountStateTracker {
427 return self.injectedManager.accountTracker;
430 -(CKKSLockStateTracker*)lockStateTracker {
431 return self.injectedManager.lockStateTracker;
434 -(CKKSReachabilityTracker*)reachabilityTracker {
435 return self.injectedManager.reachabilityTracker;
438 -(NSSet*)managedViewList {
439 return (NSSet*) CFBridgingRelease(SOSViewCopyViewSet(kViewSetCKKS));
442 -(void)expectCKFetch {
443 [self expectCKFetchAndRunBeforeFinished: nil];
446 -(void)expectCKFetchAndRunBeforeFinished: (void (^)(void))blockAfterFetch {
447 [self expectCKFetchWithFilter:^BOOL(FakeCKFetchRecordZoneChangesOperation * op) {
450 runBeforeFinished:blockAfterFetch];
453 - (void)expectCKFetchWithFilter:(BOOL (^)(FakeCKFetchRecordZoneChangesOperation*))operationMatch
454 runBeforeFinished:(void (^)(void))blockAfterFetch
456 // Create an object for the block to retain and modify
457 BoolHolder* runAlready = [[BoolHolder alloc] init];
459 __weak __typeof(self) weakSelf = self;
460 [[self.mockDatabase expect] addOperation: [OCMArg checkWithBlock:^BOOL(id obj) {
461 __strong __typeof(self) strongSelf = weakSelf;
462 if(runAlready.state) {
466 secnotice("fakecloudkit", "Received an operation (%@), checking if it's a fetch changes", obj);
468 if ([obj isKindOfClass: [FakeCKFetchRecordZoneChangesOperation class]]) {
469 FakeCKFetchRecordZoneChangesOperation *frzco = (FakeCKFetchRecordZoneChangesOperation *)obj;
470 matches = operationMatch(frzco);
471 runAlready.state = true;
473 secnotice("fakecloudkit", "Running fetch changes: %@", obj);
474 frzco.blockAfterFetch = blockAfterFetch;
475 [frzco addNullableDependency: strongSelf.ckFetchHoldOperation];
476 [strongSelf.operationQueue addOperation: frzco];
482 -(void)expectCKFetchByRecordID {
483 // Create an object for the block to retain and modify
484 BoolHolder* runAlready = [[BoolHolder alloc] init];
486 __weak __typeof(self) weakSelf = self;
487 [[self.mockDatabase expect] addOperation: [OCMArg checkWithBlock:^BOOL(id obj) {
488 __strong __typeof(self) strongSelf = weakSelf;
489 if(runAlready.state) {
493 if ([obj isKindOfClass: [FakeCKFetchRecordsOperation class]]) {
495 runAlready.state = true;
497 FakeCKFetchRecordsOperation *ffro = (FakeCKFetchRecordsOperation *)obj;
498 [ffro addNullableDependency: strongSelf.ckFetchHoldOperation];
499 [strongSelf.operationQueue addOperation: ffro];
506 -(void)expectCKFetchByQuery {
507 // Create an object for the block to retain and modify
508 BoolHolder* runAlready = [[BoolHolder alloc] init];
510 __weak __typeof(self) weakSelf = self;
511 [[self.mockDatabase expect] addOperation: [OCMArg checkWithBlock:^BOOL(id obj) {
512 __strong __typeof(self) strongSelf = weakSelf;
513 if(runAlready.state) {
517 if ([obj isKindOfClass: [FakeCKQueryOperation class]]) {
519 runAlready.state = true;
521 FakeCKQueryOperation *fqo = (FakeCKQueryOperation *)obj;
522 [fqo addNullableDependency: strongSelf.ckFetchHoldOperation];
523 [strongSelf.operationQueue addOperation: fqo];
529 - (void)startCKKSSubsystem {
530 if(self.fakeHSA2AccountStatus != CKKSAccountStatusUnknown) {
531 [self.accountStateTracker setHSA2iCloudAccountStatus:self.fakeHSA2AccountStatus];
533 [self startCKAccountStatusMock];
536 - (void)startCKAccountStatusMock {
537 // Note: currently, based on how we're mocking up the zone creation and zone subscription operation,
538 // they will 'fire' before this method is called. It's harmless, since the mocks immediately succeed
539 // and return; it's just a tad confusing.
540 if([self.ckaccountHoldOperation isPending]) {
541 [self.operationQueue addOperation: self.ckaccountHoldOperation];
544 [self.accountStateTracker performInitialDispatches];
547 -(void)holdCloudKitModifications {
548 XCTAssertFalse([self.ckModifyHoldOperation isPending], "Shouldn't already be a pending cloudkit modify hold operation");
549 self.ckModifyHoldOperation = [NSBlockOperation blockOperationWithBlock:^{
550 secnotice("ckks", "Released CloudKit modification hold.");
553 -(void)releaseCloudKitModificationHold {
554 if([self.ckModifyHoldOperation isPending]) {
555 [self.operationQueue addOperation: self.ckModifyHoldOperation];
559 -(void)holdCloudKitFetches {
560 XCTAssertFalse([self.ckFetchHoldOperation isPending], "Shouldn't already be a pending cloudkit fetch hold operation");
561 self.ckFetchHoldOperation = [NSBlockOperation blockOperationWithBlock:^{
562 secnotice("ckks", "Released CloudKit fetch hold.");
565 -(void)releaseCloudKitFetchHold {
566 if([self.ckFetchHoldOperation isPending]) {
567 [self.operationQueue addOperation: self.ckFetchHoldOperation];
571 -(void)holdCloudKitModifyRecordZones {
572 XCTAssertFalse([self.ckModifyRecordZonesHoldOperation isPending], "Shouldn't already be a pending cloudkit zone create hold operation");
573 self.ckModifyRecordZonesHoldOperation = [NSBlockOperation blockOperationWithBlock:^{
574 secnotice("ckks", "Released CloudKit zone create hold.");
577 -(void)releaseCloudKitModifyRecordZonesHold {
578 if([self.ckModifyRecordZonesHoldOperation isPending]) {
579 [self.operationQueue addOperation: self.ckModifyRecordZonesHoldOperation];
583 -(void)holdCloudKitModifySubscription {
584 XCTAssertFalse([self.ckModifySubscriptionsHoldOperation isPending], "Shouldn't already be a pending cloudkit subscription hold operation");
585 self.ckModifySubscriptionsHoldOperation = [NSBlockOperation blockOperationWithBlock:^{
586 secnotice("ckks", "Released CloudKit zone create hold.");
589 -(void)releaseCloudKitModifySubscriptionHold {
590 if([self.ckModifySubscriptionsHoldOperation isPending]) {
591 [self.operationQueue addOperation: self.ckModifySubscriptionsHoldOperation];
595 - (void)expectCKModifyItemRecords: (NSUInteger) expectedNumberOfRecords currentKeyPointerRecords: (NSUInteger) expectedCurrentKeyRecords zoneID: (CKRecordZoneID*) zoneID {
596 [self expectCKModifyItemRecords:expectedNumberOfRecords
597 currentKeyPointerRecords:expectedCurrentKeyRecords
602 - (void)expectCKModifyItemRecords: (NSUInteger) expectedNumberOfRecords currentKeyPointerRecords: (NSUInteger) expectedCurrentKeyRecords zoneID: (CKRecordZoneID*) zoneID checkItem: (BOOL (^)(CKRecord*)) checkItem {
603 [self expectCKModifyItemRecords:expectedNumberOfRecords
605 currentKeyPointerRecords:expectedCurrentKeyRecords
607 checkItem:checkItem];
610 - (void)expectCKModifyItemRecords:(NSUInteger)expectedNumberOfModifiedRecords
611 deletedRecords:(NSUInteger)expectedNumberOfDeletedRecords
612 currentKeyPointerRecords:(NSUInteger)expectedCurrentKeyRecords
613 zoneID:(CKRecordZoneID*)zoneID
614 checkItem:(BOOL (^)(CKRecord*))checkItem {
615 // We're updating the device state type on every update, so add it in here
616 NSMutableDictionary* expectedRecords = [@{SecCKRecordItemType: [NSNumber numberWithUnsignedInteger: expectedNumberOfModifiedRecords],
617 SecCKRecordCurrentKeyType: [NSNumber numberWithUnsignedInteger: expectedCurrentKeyRecords],
618 SecCKRecordDeviceStateType: [NSNumber numberWithUnsignedInt: 1],
621 if(SecCKKSSyncManifests()) {
622 expectedRecords[SecCKRecordManifestType] = [NSNumber numberWithInt: 1];
623 expectedRecords[SecCKRecordManifestLeafType] = [NSNumber numberWithInt: 72];
626 NSDictionary* deletedRecords = nil;
627 if(expectedNumberOfDeletedRecords != 0) {
628 deletedRecords = @{SecCKRecordItemType: [NSNumber numberWithUnsignedInteger: expectedNumberOfDeletedRecords]};
631 [self expectCKModifyRecords:expectedRecords
632 deletedRecordTypeCounts:deletedRecords
634 checkModifiedRecord: ^BOOL (CKRecord* record){
635 if([record.recordType isEqualToString: SecCKRecordItemType] && checkItem) {
636 return checkItem(record);
641 runAfterModification:nil];
646 - (void)expectCKModifyKeyRecords:(NSUInteger)expectedNumberOfRecords
647 currentKeyPointerRecords:(NSUInteger)expectedCurrentKeyRecords
648 tlkShareRecords:(NSUInteger)expectedTLKShareRecords
649 zoneID:(CKRecordZoneID*)zoneID
651 return [self expectCKModifyKeyRecords:expectedNumberOfRecords
652 currentKeyPointerRecords:expectedCurrentKeyRecords
653 tlkShareRecords:expectedTLKShareRecords
655 checkModifiedRecord:nil];
658 - (void)expectCKModifyKeyRecords:(NSUInteger)expectedNumberOfRecords
659 currentKeyPointerRecords:(NSUInteger)expectedCurrentKeyRecords
660 tlkShareRecords:(NSUInteger)expectedTLKShareRecords
661 zoneID:(CKRecordZoneID*)zoneID
662 checkModifiedRecord:(BOOL (^_Nullable)(CKRecord*))checkModifiedRecord
664 NSNumber* nkeys = [NSNumber numberWithUnsignedInteger: expectedNumberOfRecords];
665 NSNumber* ncurrentkeys = [NSNumber numberWithUnsignedInteger: expectedCurrentKeyRecords];
666 NSNumber* ntlkshares = [NSNumber numberWithUnsignedInteger: expectedTLKShareRecords];
668 [self expectCKModifyRecords:@{SecCKRecordIntermediateKeyType: nkeys,
669 SecCKRecordCurrentKeyType: ncurrentkeys,
670 SecCKRecordTLKShareType: ntlkshares,
672 deletedRecordTypeCounts:nil
674 checkModifiedRecord:checkModifiedRecord
675 runAfterModification:nil];
678 - (void)expectCKModifyRecords:(NSDictionary<NSString*, NSNumber*>*) expectedRecordTypeCounts
679 deletedRecordTypeCounts:(NSDictionary<NSString*, NSNumber*>*) expectedDeletedRecordTypeCounts
680 zoneID:(CKRecordZoneID*) zoneID
681 checkModifiedRecord:(BOOL (^)(CKRecord*)) checkModifiedRecord
682 runAfterModification:(void (^) (void))afterModification
684 __weak __typeof(self) weakSelf = self;
686 // Create an object for the block to retain and modify
687 BoolHolder* runAlready = [[BoolHolder alloc] init];
689 secnotice("fakecloudkit", "expecting an operation matching modifications: %@ deletions: %@",
690 expectedRecordTypeCounts, expectedDeletedRecordTypeCounts);
692 [[self.mockDatabase expect] addOperation:[OCMArg checkWithBlock:^BOOL(id obj) {
693 secnotice("fakecloudkit", "Received an operation (%@), checking if it's a modification", obj);
694 __block bool matches = false;
695 if(runAlready.state) {
696 secnotice("fakecloudkit", "Run already, skipping");
700 if ([obj isKindOfClass:[CKModifyRecordsOperation class]]) {
701 __strong __typeof(weakSelf) strongSelf = weakSelf;
702 XCTAssertNotNil(strongSelf, "self exists");
704 CKModifyRecordsOperation *op = (CKModifyRecordsOperation *)obj;
707 NSMutableDictionary<NSString*, NSNumber*>* modifiedRecordTypeCounts = [[NSMutableDictionary alloc] init];
708 NSMutableDictionary<NSString*, NSNumber*>* deletedRecordTypeCounts = [[NSMutableDictionary alloc] init];
710 // First: check if it matches. If it does, _then_ execute the operation.
711 // Supports single-zone atomic writes only
714 // We only care about atomic operations
715 secnotice("fakecloudkit", "Not an atomic operation; quitting: %@", op);
719 FakeCKZone* zone = strongSelf.zones[zoneID];
720 XCTAssertNotNil(zone, "Have a zone for these records");
722 __block BOOL result = YES;
723 dispatch_sync(zone.queue, ^{
725 for(CKRecord* record in op.recordsToSave) {
726 if(![record.recordID.zoneID isEqual: zoneID]) {
727 secnotice("fakecloudkit", "Modified record zone ID mismatch: %@ %@", zoneID, record.recordID.zoneID);
732 NSError* recordError = [zone errorFromSavingRecord: record];
734 secnotice("fakecloudkit", "Record zone rejected record write: %@ %@", recordError, record);
735 XCTFail(@"Record zone rejected record write: %@ %@", recordError, record);
740 NSNumber* currentCountNumber = modifiedRecordTypeCounts[record.recordType];
741 NSUInteger currentCount = currentCountNumber ? [currentCountNumber unsignedIntegerValue] : 0;
742 modifiedRecordTypeCounts[record.recordType] = [NSNumber numberWithUnsignedInteger: currentCount + 1];
745 for(CKRecordID* recordID in op.recordIDsToDelete) {
746 if(![recordID.zoneID isEqual: zoneID]) {
748 secnotice("fakecloudkit", "Deleted record zone ID mismatch: %@ %@", zoneID, recordID.zoneID);
751 // Find the object in CloudKit, and record its type
752 CKRecord* record = strongSelf.zones[zoneID].currentDatabase[recordID];
754 NSNumber* currentCountNumber = deletedRecordTypeCounts[record.recordType];
755 NSUInteger currentCount = currentCountNumber ? [currentCountNumber unsignedIntegerValue] : 0;
756 deletedRecordTypeCounts[record.recordType] = [NSNumber numberWithUnsignedInteger: currentCount + 1];
760 NSMutableDictionary* filteredExpectedRecordTypeCounts = [expectedRecordTypeCounts mutableCopy];
761 for(NSString* key in filteredExpectedRecordTypeCounts.allKeys) {
762 if([filteredExpectedRecordTypeCounts[key] isEqual: [NSNumber numberWithInt:0]]) {
763 filteredExpectedRecordTypeCounts[key] = nil;
766 filteredExpectedRecordTypeCounts[SecCKRecordManifestType] = modifiedRecordTypeCounts[SecCKRecordManifestType];
767 filteredExpectedRecordTypeCounts[SecCKRecordManifestLeafType] = modifiedRecordTypeCounts[SecCKRecordManifestLeafType];
769 // Inspect that we have exactly the same records as we expect
770 if(expectedRecordTypeCounts) {
771 matches &= !![modifiedRecordTypeCounts isEqual: filteredExpectedRecordTypeCounts];
773 secnotice("fakecloudkit", "Record number mismatch: %@ %@", modifiedRecordTypeCounts, filteredExpectedRecordTypeCounts);
778 matches &= op.recordsToSave.count == 0u;
780 secnotice("fakecloudkit", "Record number mismatch: %@ 0", modifiedRecordTypeCounts);
785 if(expectedDeletedRecordTypeCounts) {
786 matches &= !![deletedRecordTypeCounts isEqual: expectedDeletedRecordTypeCounts];
788 secnotice("fakecloudkit", "Deleted record number mismatch: %@ %@", deletedRecordTypeCounts, expectedDeletedRecordTypeCounts);
793 matches &= op.recordIDsToDelete.count == 0u;
795 secnotice("fakecloudkit", "Deleted record number mismatch: %@ 0", deletedRecordTypeCounts);
801 // We have the right number of things, and their etags match. Ensure that they have the right etags
802 if(matches && checkModifiedRecord) {
803 // Clearly we have the right number of things. Call checkRecord on them...
804 for(CKRecord* record in op.recordsToSave) {
805 matches &= !!(checkModifiedRecord(record));
807 secnotice("fakecloudkit", "Check record reports NO: %@ 0", record);
815 // Emulate cloudkit and schedule the operation for execution. Be sure to wait for this operation
816 // if you'd like to read the data from this write.
817 NSBlockOperation* ckop = [NSBlockOperation named:@"cloudkit-write" withBlock: ^{
818 @synchronized(zone.currentDatabase) {
819 NSMutableArray* savedRecords = [[NSMutableArray alloc] init];
820 for(CKRecord* record in op.recordsToSave) {
821 CKRecord* reflectedRecord = [record copy];
822 reflectedRecord.modificationDate = [NSDate date];
824 [zone addToZone: reflectedRecord];
826 [savedRecords addObject:reflectedRecord];
827 op.perRecordCompletionBlock(reflectedRecord, nil);
829 for(CKRecordID* recordID in op.recordIDsToDelete) {
830 // I don't believe CloudKit fails an operation if you delete a record that's not there, so:
831 [zone deleteCKRecordIDFromZone: recordID];
834 if(afterModification) {
838 op.modifyRecordsCompletionBlock(savedRecords, op.recordIDsToDelete, nil);
842 [ckop addNullableDependency:strongSelf.ckModifyHoldOperation];
843 [strongSelf.operationQueue addOperation: ckop];
851 runAlready.state = true;
853 return matches ? YES : NO;
857 - (void)failNextZoneCreation:(CKRecordZoneID*)zoneID {
858 XCTAssertNil(self.zones[zoneID], "Zone does not exist yet");
859 self.zones[zoneID] = [[FakeCKZone alloc] initZone: zoneID];
860 self.zones[zoneID].creationError = [[CKPrettyError alloc] initWithDomain:CKErrorDomain
861 code:CKErrorNetworkUnavailable
863 CKErrorRetryAfterKey: @(0.5),
867 // Report success, but don't actually create the zone.
868 // This way, you can find ZoneNotFound errors later on
869 - (void)failNextZoneCreationSilently:(CKRecordZoneID*)zoneID {
870 XCTAssertNil(self.zones[zoneID], "Zone does not exist yet");
871 self.zones[zoneID] = [[FakeCKZone alloc] initZone: zoneID];
872 self.zones[zoneID].failCreationSilently = true;
875 - (void)failNextZoneSubscription:(CKRecordZoneID*)zoneID {
876 XCTAssertNotNil(self.zones[zoneID], "Zone exists");
877 self.zones[zoneID].subscriptionError = [[CKPrettyError alloc] initWithDomain:CKErrorDomain code:CKErrorNetworkUnavailable userInfo:@{}];
880 - (void)failNextZoneSubscription:(CKRecordZoneID*)zoneID withError:(NSError*)error {
881 XCTAssertNotNil(self.zones[zoneID], "Zone exists");
882 self.zones[zoneID].subscriptionError = error;
885 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure:(CKRecordZoneID*)zoneID {
886 [self failNextCKAtomicModifyItemRecordsUpdateFailure:zoneID blockAfterReject:nil];
889 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure:(CKRecordZoneID*)zoneID blockAfterReject: (void (^)(void))blockAfterReject {
890 [self failNextCKAtomicModifyItemRecordsUpdateFailure:zoneID blockAfterReject:blockAfterReject withError:nil];
893 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure:(CKRecordZoneID*)zoneID blockAfterReject: (void (^)(void))blockAfterReject withError:(NSError*)error {
894 __weak __typeof(self) weakSelf = self;
896 [[self.mockDatabase expect] addOperation:[OCMArg checkWithBlock:^BOOL(id obj) {
897 __strong __typeof(weakSelf) strongSelf = weakSelf;
898 XCTAssertNotNil(strongSelf, "self exists");
900 __block bool rejected = false;
901 if ([obj isKindOfClass:[CKModifyRecordsOperation class]]) {
902 CKModifyRecordsOperation *op = (CKModifyRecordsOperation *)obj;
905 // We only care about atomic operations
909 // We want to only match zone updates pertaining to this zone
910 for(CKRecord* record in op.recordsToSave) {
911 if(![record.recordID.zoneID isEqual: zoneID]) {
916 FakeCKZone* zone = strongSelf.zones[zoneID];
917 XCTAssertNotNil(zone, "Have a zone for these records");
922 [strongSelf rejectWrite: op withError:error];
924 NSMutableDictionary<CKRecordID*, NSError*>* failedRecords = [[NSMutableDictionary alloc] init];
925 [strongSelf rejectWrite: op failedRecords:failedRecords];
928 if(blockAfterReject) {
932 return rejected ? YES : NO;
936 - (void)expectCKAtomicModifyItemRecordsUpdateFailure:(CKRecordZoneID*)zoneID {
937 __weak __typeof(self) weakSelf = self;
939 [[self.mockDatabase expect] addOperation:[OCMArg checkWithBlock:^BOOL(id obj) {
940 __strong __typeof(weakSelf) strongSelf = weakSelf;
941 XCTAssertNotNil(strongSelf, "self exists");
943 __block bool rejected = false;
944 if ([obj isKindOfClass:[CKModifyRecordsOperation class]]) {
945 CKModifyRecordsOperation *op = (CKModifyRecordsOperation *)obj;
947 secnotice("fakecloudkit", "checking for expectCKAtomicModifyItemRecordsUpdateFailure");
950 // We only care about atomic operations
951 secnotice("fakecloudkit", "expectCKAtomicModifyItemRecordsUpdateFailure: update not atomic");
955 // We want to only match zone updates pertaining to this zone
956 for(CKRecord* record in op.recordsToSave) {
957 if(![record.recordID.zoneID isEqual: zoneID]) {
958 secnotice("fakecloudkit", "expectCKAtomicModifyItemRecordsUpdateFailure: %@ is not %@", record.recordID.zoneID, zoneID);
963 FakeCKZone* zone = strongSelf.zones[zoneID];
964 XCTAssertNotNil(zone, "Have a zone for these records");
966 NSMutableDictionary<CKRecordID*, NSError*>* failedRecords = [[NSMutableDictionary alloc] init];
968 @synchronized(zone.currentDatabase) {
969 for(CKRecord* record in op.recordsToSave) {
970 // Check if we should allow this transaction
971 NSError* recordSaveError = [zone errorFromSavingRecord: record];
972 if(recordSaveError) {
973 failedRecords[record.recordID] = recordSaveError;
980 [strongSelf rejectWrite: op failedRecords:failedRecords];
982 secnotice("fakecloudkit", "expectCKAtomicModifyItemRecordsUpdateFailure: doesn't seem like an error to us");
985 return rejected ? YES : NO;
989 -(void)rejectWrite:(CKModifyRecordsOperation*)op withError:(NSError*)error {
990 // Emulate cloudkit and schedule the operation for execution. Be sure to wait for this operation
991 // if you'd like to read the data from this write.
992 NSBlockOperation* ckop = [NSBlockOperation named:@"cloudkit-reject-write-error" withBlock: ^{
993 op.modifyRecordsCompletionBlock(nil, nil, error);
996 [ckop addNullableDependency: self.ckModifyHoldOperation];
997 [self.operationQueue addOperation: ckop];
1000 -(void)rejectWrite:(CKModifyRecordsOperation*)op failedRecords:(NSMutableDictionary<CKRecordID*, NSError*>*)failedRecords {
1001 // Add the batch request failed errors
1002 for(CKRecord* record in op.recordsToSave) {
1003 NSError* exists = failedRecords[record.recordID];
1005 // TODO: might have important userInfo, but we're not mocking that yet
1006 failedRecords[record.recordID] = [[CKPrettyError alloc] initWithDomain: CKErrorDomain code: CKErrorBatchRequestFailed userInfo: @{}];
1010 NSError* error = [[CKPrettyError alloc] initWithDomain: CKErrorDomain code: CKErrorPartialFailure userInfo: @{CKPartialErrorsByItemIDKey: failedRecords}];
1012 // Emulate cloudkit and schedule the operation for execution. Be sure to wait for this operation
1013 // if you'd like to read the data from this write.
1014 NSBlockOperation* ckop = [NSBlockOperation named:@"cloudkit-reject-write" withBlock: ^{
1015 op.modifyRecordsCompletionBlock(nil, nil, error);
1016 op.isFinished = YES;
1018 [ckop addNullableDependency: self.ckModifyHoldOperation];
1019 [self.operationQueue addOperation: ckop];
1022 - (void)expectCKDeleteItemRecords:(NSUInteger)expectedNumberOfRecords
1023 zoneID:(CKRecordZoneID*) zoneID {
1025 // We're updating the device state type on every update, so add it in here
1026 NSMutableDictionary* expectedRecords = [@{
1027 SecCKRecordDeviceStateType: [NSNumber numberWithUnsignedInteger:expectedNumberOfRecords],
1029 if(SecCKKSSyncManifests()) {
1030 // TODO: this really shouldn't be 2.
1031 expectedRecords[SecCKRecordManifestType] = [NSNumber numberWithInt: 2];
1032 expectedRecords[SecCKRecordManifestLeafType] = [NSNumber numberWithInt: 72];
1035 [self expectCKModifyRecords:expectedRecords
1036 deletedRecordTypeCounts:@{SecCKRecordItemType: [NSNumber numberWithUnsignedInteger: expectedNumberOfRecords]}
1038 checkModifiedRecord:nil
1039 runAfterModification:nil];
1042 -(void)waitForCKModifications {
1043 // CloudKit modifications are put on the local queue.
1044 // This is heavyweight but should suffice.
1045 [self.operationQueue waitUntilAllOperationsAreFinished];
1049 NSString* testName = [self.name componentsSeparatedByString:@" "][1];
1050 testName = [testName stringByReplacingOccurrencesOfString:@"]" withString:@""];
1051 secnotice("ckkstest", "Ending test %@", testName);
1053 if(SecCKKSIsEnabled()) {
1054 self.accountStatus = CKAccountStatusCouldNotDetermine;
1056 // If the test never initialized the account state, don't call status later
1057 bool callStatus = [self.ckaccountHoldOperation isFinished];
1058 [self.ckaccountHoldOperation cancel];
1059 self.ckaccountHoldOperation = nil;
1061 // Ensure we don't have any blocking operations left
1062 [self.operationQueue cancelAllOperations];
1063 [self waitForCKModifications];
1065 XCTAssertEqual(0, [self.injectedManager.completedSecCKKSInitialize wait:20*NSEC_PER_SEC],
1066 "Timeout did not occur waiting for SecCKKSInitialize");
1068 // Ensure that we can fetch zone status for all zones
1070 XCTestExpectation *statusReturned = [self expectationWithDescription:@"status returned"];
1071 [self.injectedManager rpcStatus:nil reply:^(NSArray<NSDictionary *> *result, NSError *error) {
1072 XCTAssertNil(error, "Should be no error fetching status");
1073 [statusReturned fulfill];
1075 [self waitForExpectations: @[statusReturned] timeout:20];
1077 // Make sure this happens before teardown.
1078 XCTAssertEqual(0, [self.accountStateTracker.finishedInitialDispatches wait:20*NSEC_PER_SEC], "Account state tracker initialized itself");
1080 dispatch_group_t accountChangesDelivered = [self.accountStateTracker checkForAllDeliveries];
1081 XCTAssertEqual(0, dispatch_group_wait(accountChangesDelivered, dispatch_time(DISPATCH_TIME_NOW, 10*NSEC_PER_SEC)), "Account state tracker finished delivering everything");
1087 [self.injectedManager cancelPendingOperations];
1088 [CKKSViewManager resetManager:true setTo:nil];
1089 self.injectedManager = nil;
1090 [self.mockCKKSViewManager stopMocking];
1091 self.mockCKKSViewManager = nil;
1093 [self.mockAccountStateTracker stopMocking];
1094 self.mockAccountStateTracker = nil;
1096 [self.mockLockStateTracker stopMocking];
1097 self.mockLockStateTracker = nil;
1099 [self.mockFakeCKModifyRecordZonesOperation stopMocking];
1100 self.mockFakeCKModifyRecordZonesOperation = nil;
1102 [self.mockFakeCKModifySubscriptionsOperation stopMocking];
1103 self.mockFakeCKModifySubscriptionsOperation = nil;
1105 [self.mockFakeCKFetchRecordZoneChangesOperation stopMocking];
1106 self.mockFakeCKFetchRecordZoneChangesOperation = nil;
1108 [self.mockFakeCKFetchRecordsOperation stopMocking];
1109 self.mockFakeCKFetchRecordsOperation = nil;
1111 [self.mockFakeCKQueryOperation stopMocking];
1112 self.mockFakeCKQueryOperation = nil;
1114 [self.mockDatabase stopMocking];
1115 self.mockDatabase = nil;
1117 [self.mockDatabaseExceptionCatcher stopMocking];
1118 self.mockDatabaseExceptionCatcher = nil;
1120 [self.mockContainer stopMocking];
1121 self.mockContainer = nil;
1123 [self.mockTTR stopMocking];
1125 self.ttrExpectation = nil;
1126 self.isTTRRatelimited = true;
1130 _mockSOSAdapter = nil;
1131 _mockOctagonAdapter = nil;
1133 SecCKKSTestResetFlags();
1136 - (CKKSKey*) fakeTLK: (CKRecordZoneID*)zoneID {
1137 CKKSKey* key = [[CKKSKey alloc] initSelfWrappedWithAESKey:[[CKKSAESSIVKey alloc] initWithBase64: @"uImdbZ7Zg+6WJXScTnRBfNmoU1UiMkSYxWc+d1Vuq3IFn2RmTRkTdWTe3HmeWo1pAomqy+upK8KHg2PGiRGhqg=="]
1138 uuid:[[NSUUID UUID] UUIDString]
1139 keyclass:SecCKKSKeyClassTLK
1140 state: SecCKKSProcessedStateLocal
1142 encodedCKRecord: nil
1144 [key CKRecordWithZoneID: zoneID];
1148 - (NSError*)ckInternalServerExtensionError:(NSInteger)code description:(NSString*)desc {
1149 return [FakeCKZone internalPluginError:@"CloudkitKeychainService" code:code description:desc];