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@
24 #import <XCTest/XCTest.h>
25 #import <CloudKit/CloudKit.h>
26 #import <CloudKit/CloudKit_Private.h>
28 #import <Foundation/Foundation.h>
30 #import "keychain/ckks/tests/MockCloudKit.h"
31 #import "keychain/ckks/CKKSCKAccountStateTracker.h"
34 @
class CKKSCKRecordHolder
;
35 @
class CKKSKeychainView
;
36 @
class CKKSViewManager
;
38 @
class CKKSLockStateTracker
;
40 @interface CloudKitMockXCTest
: XCTestCase
44 @property CKRecordZoneID
* testZoneID
;
46 @property id mockDatabase
;
47 @property id mockContainer
;
48 @property id mockFakeCKModifyRecordZonesOperation
;
49 @property id mockFakeCKModifySubscriptionsOperation
;
50 @property id mockFakeCKFetchRecordZoneChangesOperation
;
51 @property id mockFakeCKFetchRecordsOperation
;
52 @property id mockFakeCKQueryOperation
;
54 @property id mockAccountStateTracker
;
56 @property CKAccountStatus accountStatus
;
57 @property BOOL supportsDeviceToDeviceEncryption
;
58 @property SOSCCStatus circleStatus
;
59 @
property (readonly
) NSString
* ckDeviceID
;
60 @
property (readonly
) CKKSCKAccountStateTracker
* accountStateTracker
;
62 @property NSString
* circlePeerID
;
64 @property
bool aksLockState
; // The current 'AKS lock state'
65 @
property (readonly
) CKKSLockStateTracker
* lockStateTracker
;
66 @property id mockLockStateTracker
;
68 @property NSMutableDictionary
<CKRecordZoneID
*, FakeCKZone
*>* zones
;
70 @property NSOperationQueue
* operationQueue
;
71 @property NSBlockOperation
* ckksHoldOperation
;
72 @property NSBlockOperation
* ckaccountHoldOperation
;
74 @property NSBlockOperation
* ckModifyHoldOperation
;
75 @property NSBlockOperation
* ckFetchHoldOperation
;
77 @property
bool silentFetchesAllowed
;
79 @property id mockCKKSViewManager
;
80 @property CKKSViewManager
* injectedManager
;
82 - (CKKSKey
*) fakeTLK
: (CKRecordZoneID
*)zoneID
;
84 - (void)expectCKModifyItemRecords
: (NSUInteger
) expectedNumberOfRecords
85 currentKeyPointerRecords
: (NSUInteger
) expectedCurrentKeyRecords
86 zoneID
: (CKRecordZoneID
*) zoneID
;
87 - (void)expectCKModifyItemRecords
: (NSUInteger
) expectedNumberOfRecords
88 currentKeyPointerRecords
: (NSUInteger
) expectedCurrentKeyRecords
89 zoneID
: (CKRecordZoneID
*) zoneID
90 checkItem
: (BOOL (^)(CKRecord
*)) checkItem
;
92 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfModifiedRecords
93 deletedRecords
:(NSUInteger
)expectedNumberOfDeletedRecords
94 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
95 zoneID
:(CKRecordZoneID
*)zoneID
96 checkItem
:(BOOL (^)(CKRecord
*))checkItem
;
98 - (void)expectCKDeleteItemRecords
: (NSUInteger
) expectedNumberOfRecords zoneID
: (CKRecordZoneID
*) zoneID
;
100 - (void)expectCKModifyKeyRecords
:(NSUInteger
)expectedNumberOfRecords
101 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
102 tlkShareRecords
:(NSUInteger
)expectedTLKShareRecords
103 zoneID
:(CKRecordZoneID
*)zoneID
;
105 - (void)expectCKModifyRecords
:(NSDictionary
<NSString
*, NSNumber
*>*) expectedRecordTypeCounts
106 deletedRecordTypeCounts
:(NSDictionary
<NSString
*, NSNumber
*>*) expectedDeletedRecordTypeCounts
107 zoneID
:(CKRecordZoneID
*) zoneID
108 checkModifiedRecord
:(BOOL (^)(CKRecord
*)) checkRecord
109 runAfterModification
:(void (^) ())afterModification
;
111 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
;
112 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID blockAfterReject
: (void (^)())blockAfterReject
;
113 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID blockAfterReject
: (void (^)())blockAfterReject withError
:(NSError
*)error
;
114 - (void)expectCKAtomicModifyItemRecordsUpdateFailure
: (CKRecordZoneID
*) zoneID
;
116 - (void)failNextZoneCreation
:(CKRecordZoneID
*)zoneID
;
117 - (void)failNextZoneCreationSilently
:(CKRecordZoneID
*)zoneID
;
118 - (void)failNextZoneSubscription
:(CKRecordZoneID
*)zoneID
;
119 - (void)failNextZoneSubscription
:(CKRecordZoneID
*)zoneID withError
:(NSError
*)error
;
121 // Use this to assert that a fetch occurs (especially if silentFetchesAllowed = false)
122 - (void)expectCKFetch
;
124 // Use this to 1) assert that a fetch occurs and 2) cause a block to run _after_ all changes have been delivered but _before_ the fetch 'completes'.
125 // This way, you can modify the CK zone to cause later collisions.
126 - (void)expectCKFetchAndRunBeforeFinished
: (void (^)())blockAfterFetch
;
128 // Use this to assert that a FakeCKFetchRecordsOperation occurs.
129 -(void)expectCKFetchByRecordID
;
131 // Use this to assert that a FakeCKQueryOperation occurs.
132 - (void)expectCKFetchByQuery
;
134 // Wait until all scheduled cloudkit operations are reflected in the currentDatabase
135 - (void)waitForCKModifications
;
137 // Unblocks the CKKS subsystem only.
138 - (void)startCKKSSubsystemOnly
;
140 // Unblocks the CKAccount mock subsystem. Until this is called, the tests believe cloudd hasn't returned any account status yet.
141 - (void)startCKAccountStatusMock
;
143 // Starts everything.
144 - (void)startCKKSSubsystem
;
146 // Blocks the completion (partial or full) of CloudKit modifications
147 -(void)holdCloudKitModifications
;
149 // Unblocks the hold you've added with holdCloudKitModifications; CloudKit modifications will finish
150 -(void)releaseCloudKitModificationHold
;
152 // Blocks the CloudKit fetches from beginning (similar to network latency)
153 -(void)holdCloudKitFetches
;
154 // Unblocks the hold you've added with holdCloudKitFetches; CloudKit fetches will finish
155 -(void)releaseCloudKitFetchHold
;
157 // Make a CK internal server extension error with a given code and description.
158 - (NSError
*)ckInternalServerExtensionError
:(NSInteger
)code description
:(NSString
*)desc
;
160 // Schedule an operation for execution (and failure), with some existing record errors.
161 // Other records in the operation but not in failedRecords will have CKErrorBatchRequestFailed errors created.
162 -(void)rejectWrite
:(CKModifyRecordsOperation
*)op failedRecords
:(NSMutableDictionary
<CKRecordID
*, NSError
*>*)failedRecords
;