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 <CloudKit/CloudKit.h>
27 #import <CloudKit/CloudKit_Private.h>
28 #import <XCTest/XCTest.h>
30 #import <Foundation/Foundation.h>
31 #import <SystemConfiguration/SystemConfiguration.h>
33 #import "keychain/ckks/tests/CKKSMockSOSPresentAdapter.h"
34 #import "keychain/ckks/tests/CKKSMockOctagonAdapter.h"
35 #import "keychain/ckks/CKKSAccountStateTracker.h"
36 #import "keychain/ckks/tests/MockCloudKit.h"
38 #import "keychain/ot/OTManager.h"
40 NS_ASSUME_NONNULL_BEGIN
43 @
class CKKSCKRecordHolder
;
44 @
class CKKSKeychainView
;
45 @
class CKKSViewManager
;
47 @
class CKKSLockStateTracker
;
48 @
class CKKSReachabilityTracker
;
49 @
class SOSCKKSPeerAdapter
;
51 @interface CloudKitMockXCTest
: XCTestCase
53 @property CKRecordZoneID
* testZoneID
;
55 @
property (nullable
) id mockDatabase
;
56 @
property (nullable
) id mockDatabaseExceptionCatcher
;
57 @
property (nullable
) id mockContainer
;
58 @
property (nullable
) id mockContainerExpectations
;
59 @
property (nullable
) id mockFakeCKModifyRecordZonesOperation
;
60 @
property (nullable
) id mockFakeCKModifySubscriptionsOperation
;
61 @
property (nullable
) id mockFakeCKFetchRecordZoneChangesOperation
;
62 @
property (nullable
) id mockFakeCKFetchRecordsOperation
;
63 @
property (nullable
) id mockFakeCKQueryOperation
;
65 @
property (nullable
) id mockAccountStateTracker
;
67 @
property (nullable
) id mockTTR
;
68 @property BOOL isTTRRatelimited
;
69 @
property (nullable
) XCTestExpectation
*ttrExpectation
;
71 // The CloudKit account status
72 @property CKAccountStatus accountStatus
;
74 // The current HSA2-ness of the world
75 // Set to 'unknown' to not inject any answer into
76 @property CKKSAccountStatus fakeHSA2AccountStatus
;
78 @property BOOL iCloudHasValidCredentials
;
80 @
property (readonly
) NSString
* ckDeviceID
;
81 @
property (readonly
) CKKSAccountStateTracker
* accountStateTracker
;
84 @property NSString
* apsEnvironment
;
86 @property
bool aksLockState
; // The current 'AKS lock state'
87 @
property (readonly
) CKKSLockStateTracker
* lockStateTracker
;
88 @
property (nullable
) id mockLockStateTracker
;
90 @
property (readonly
) CKKSReachabilityTracker
*reachabilityTracker
;
92 @
property (nullable
) NSMutableDictionary
<CKRecordZoneID
*, FakeCKZone
*>* zones
;
94 @property NSOperationQueue
* operationQueue
;
95 @
property (nullable
) NSBlockOperation
* ckaccountHoldOperation
;
97 @
property (nullable
) NSBlockOperation
* ckModifyHoldOperation
;
98 @
property (nullable
) NSBlockOperation
* ckFetchHoldOperation
;
99 @
property (nullable
) NSBlockOperation
* ckModifyRecordZonesHoldOperation
;
100 @
property (nullable
) NSBlockOperation
* ckModifySubscriptionsHoldOperation
;
102 @property
bool silentFetchesAllowed
;
103 @property
bool silentZoneDeletesAllowed
;
105 @property CKKSMockSOSPresentAdapter
* mockSOSAdapter
;
106 @
property (nullable
) CKKSMockOctagonAdapter
*mockOctagonAdapter
;
108 - (NSSet
<NSString
*>*)managedViewList
;
109 - (TPPolicy
*)viewSortingPolicyForManagedViewList
;
111 @
property (nullable
) id mockCKKSViewManager
;
112 @
property (nullable
) CKKSViewManager
* injectedManager
;
114 // Injected into CKKSViewManager using OCMock
115 @property BOOL overrideUseCKKSViewsFromPolicy
;
117 // Set this to true before calling -setup if you're going to configure the ckks view manager yourself
118 // !disable format used because this will be initialized to false, and so will happen unless subclasses take positive action
119 @property BOOL disableConfigureCKKSViewManagerWithViews
;
121 // Set this to true to override CKKSViewsFromPolicy to NO instead of the default YES
122 // !disable format used because this will be initialized to false, and so will happen unless subclasses take positive action
123 @property BOOL setCKKSViewsFromPolicyToNo
;
125 @
property (nullable
) OTManager
* injectedOTManager
;
127 // Fill this in to fail the next modifyzones operation
128 @
property (nullable
) NSError
* nextModifyRecordZonesError
;
130 - (CKKSKey
*)fakeTLK
:(CKRecordZoneID
*)zoneID
;
132 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfRecords
133 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
134 zoneID
:(CKRecordZoneID
*)zoneID
;
135 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfRecords
136 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
137 zoneID
:(CKRecordZoneID
*)zoneID
138 checkItem
:(BOOL (^_Nullable
)(CKRecord
*))checkItem
;
140 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfModifiedRecords
141 deletedRecords
:(NSUInteger
)expectedNumberOfDeletedRecords
142 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
143 zoneID
:(CKRecordZoneID
*)zoneID
144 checkItem
:(BOOL (^_Nullable
)(CKRecord
*))checkItem
;
146 - (void)expectCKDeleteItemRecords
:(NSUInteger
)expectedNumberOfRecords zoneID
:(CKRecordZoneID
*)zoneID
;
148 - (void)expectCKModifyKeyRecords
:(NSUInteger
)expectedNumberOfRecords
149 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
150 tlkShareRecords
:(NSUInteger
)expectedTLKShareRecords
151 zoneID
:(CKRecordZoneID
*)zoneID
;
152 - (void)expectCKModifyKeyRecords
:(NSUInteger
)expectedNumberOfRecords
153 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
154 tlkShareRecords
:(NSUInteger
)expectedTLKShareRecords
155 zoneID
:(CKRecordZoneID
*)zoneID
156 checkModifiedRecord
:(BOOL (^_Nullable
)(CKRecord
*))checkModifiedRecord
;
158 - (void)expectCKModifyRecords
:(NSDictionary
<NSString
*, NSNumber
*>*)expectedRecordTypeCounts
159 deletedRecordTypeCounts
:(NSDictionary
<NSString
*, NSNumber
*>* _Nullable
)expectedDeletedRecordTypeCounts
160 zoneID
:(CKRecordZoneID
*)zoneID
161 checkModifiedRecord
:(BOOL (^_Nullable
)(CKRecord
*))checkRecord
162 runAfterModification
:(void (^_Nullable
)(void))afterModification
;
164 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
;
165 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
166 blockAfterReject
:(void (^_Nullable
)(void))blockAfterReject
;
167 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
168 blockAfterReject
:(void (^_Nullable
)(void))blockAfterReject
169 withError
:(NSError
* _Nullable
)error
;
170 - (void)expectCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
;
172 - (void)failNextZoneCreation
:(CKRecordZoneID
*)zoneID
;
173 - (void)failNextZoneCreationSilently
:(CKRecordZoneID
*)zoneID
;
174 - (void)failNextZoneSubscription
:(CKRecordZoneID
*)zoneID
;
175 - (void)failNextZoneSubscription
:(CKRecordZoneID
*)zoneID withError
:(NSError
*)error
;
177 - (NSError
* _Nullable
)shouldFailModifyRecordZonesOperation
;
178 - (void)ensureZoneDeletionAllowed
:(FakeCKZone
*)zone
;
180 // Override this to interpose on how an OTManager is created
181 // This will be called during setUp, after the CloudKit mocks are in-place
182 // This needs to fill in the injectedOTManager variable on this class
183 - (OTManager
*)setUpOTManager
:(CKKSCloudKitClassDependencies
*)cloudKitClassDependencies
;
185 // Use this to assert that a fetch occurs (especially if silentFetchesAllowed = false)
186 - (void)expectCKFetch
;
188 // 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'.
189 // This way, you can modify the CK zone to cause later collisions.
190 - (void)expectCKFetchAndRunBeforeFinished
:(void (^_Nullable
)(void))blockAfterFetch
;
192 // Introspect the fetch object before allowing it to proceed
193 - (void)expectCKFetchWithFilter
:(BOOL (^)(FakeCKFetchRecordZoneChangesOperation
*))operationMatch
194 runBeforeFinished
:(void (^)(void))blockAfterFetch
;
196 // Use this to assert that a FakeCKFetchRecordsOperation occurs.
197 - (void)expectCKFetchByRecordID
;
199 // Use this to assert that a FakeCKQueryOperation occurs.
200 - (void)expectCKFetchByQuery
;
202 // Wait until all scheduled cloudkit operations are reflected in the currentDatabase
203 - (void)waitForCKModifications
;
205 // Unblocks the CKAccount mock subsystem. Until this is called, the tests believe cloudd hasn't returned any account status yet.
206 - (void)startCKAccountStatusMock
;
208 // Starts everything.
209 - (void)startCKKSSubsystem
;
211 // Blocks the completion (partial or full) of CloudKit modifications
212 - (void)holdCloudKitModifications
;
214 // Unblocks the hold you've added with holdCloudKitModifications; CloudKit modifications will finish
215 - (void)releaseCloudKitModificationHold
;
217 // Blocks the CloudKit fetches from beginning (similar to network latency)
218 - (void)holdCloudKitFetches
;
219 // Unblocks the hold you've added with holdCloudKitFetches; CloudKit fetches will finish
220 - (void)releaseCloudKitFetchHold
;
222 - (void)holdCloudKitModifyRecordZones
;
223 - (void)releaseCloudKitModifyRecordZonesHold
;
225 - (void)holdCloudKitModifySubscription
;
226 - (void)releaseCloudKitModifySubscriptionHold
;
229 // Make a CK internal server extension error with a given code and description.
230 - (NSError
*)ckInternalServerExtensionError
:(NSInteger
)code description
:(NSString
*)desc
;
232 // Schedule an operation for execution (and failure), with some existing record errors.
233 // Other records in the operation but not in failedRecords will have CKErrorBatchRequestFailed errors created.
234 - (void)rejectWrite
:(CKModifyRecordsOperation
*)op failedRecords
:(NSMutableDictionary
<CKRecordID
*, NSError
*>*)failedRecords
;
238 NS_ASSUME_NONNULL_END