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"
39 #import "keychain/trust/TrustedPeers/TPSyncingPolicy.h"
41 NS_ASSUME_NONNULL_BEGIN
44 @
class CKKSCKRecordHolder
;
45 @
class CKKSKeychainView
;
46 @
class CKKSViewManager
;
48 @
class CKKSLockStateTracker
;
49 @
class CKKSReachabilityTracker
;
50 @
class SOSCKKSPeerAdapter
;
52 @interface CKKSTestFailureLogger
: NSObject
<XCTestObservation
>
55 @interface CloudKitMockXCTest
: XCTestCase
57 @property CKRecordZoneID
* testZoneID
;
59 @
property (nullable
) id mockDatabase
;
60 @
property (nullable
) id mockDatabaseExceptionCatcher
;
61 @
property (nullable
) id mockContainer
;
62 @
property (nullable
) id mockContainerExpectations
;
63 @
property (nullable
) id mockFakeCKModifyRecordZonesOperation
;
64 @
property (nullable
) id mockFakeCKModifySubscriptionsOperation
;
65 @
property (nullable
) id mockFakeCKFetchRecordZoneChangesOperation
;
66 @
property (nullable
) id mockFakeCKFetchRecordsOperation
;
67 @
property (nullable
) id mockFakeCKQueryOperation
;
69 @
property (nullable
) id mockAccountStateTracker
;
71 @
property (nullable
) id mockTTR
;
72 @property BOOL isTTRRatelimited
;
73 @
property (nullable
) XCTestExpectation
*ttrExpectation
;
75 // The CloudKit account status
76 @property CKAccountStatus accountStatus
;
78 // The current HSA2-ness of the world
79 // Set to 'unknown' to not inject any answer into
80 @property CKKSAccountStatus fakeHSA2AccountStatus
;
82 @property BOOL iCloudHasValidCredentials
;
84 @
property (readonly
) NSString
* ckDeviceID
;
85 @
property (readonly
) CKKSAccountStateTracker
* accountStateTracker
;
88 @property NSString
* apsEnvironment
;
90 @property
bool aksLockState
; // The current 'AKS lock state'
91 @
property (readonly
) CKKSLockStateTracker
* lockStateTracker
;
92 @
property (nullable
) id mockLockStateTracker
;
94 @
property (readonly
) CKKSReachabilityTracker
*reachabilityTracker
;
96 @
property (nullable
) NSMutableDictionary
<CKRecordZoneID
*, FakeCKZone
*>* zones
;
98 @property NSOperationQueue
* operationQueue
;
99 @
property (nullable
) NSBlockOperation
* ckaccountHoldOperation
;
101 @
property (nullable
) NSBlockOperation
* ckModifyHoldOperation
;
102 @
property (nullable
) NSBlockOperation
* ckFetchHoldOperation
;
103 @
property (nullable
) NSBlockOperation
* ckModifyRecordZonesHoldOperation
;
104 @
property (nullable
) NSBlockOperation
* ckModifySubscriptionsHoldOperation
;
106 @property
bool silentFetchesAllowed
;
107 @property
bool silentZoneDeletesAllowed
;
109 @property CKKSMockSOSPresentAdapter
* mockSOSAdapter
;
110 @
property (nullable
) CKKSMockOctagonAdapter
*mockOctagonAdapter
;
112 - (NSSet
<NSString
*>*)managedViewList
;
113 - (TPSyncingPolicy
*)viewSortingPolicyForManagedViewList
;
114 - (TPSyncingPolicy
*)viewSortingPolicyForManagedViewListWithUserControllableViews
:(NSSet
<NSString
*>*)ucv
115 syncUserControllableViews
:(TPPBPeerStableInfo_UserControllableViewStatus
)syncUserControllableViews
;
117 @
property (nullable
) id mockCKKSViewManager
;
118 @
property (nullable
) CKKSViewManager
* injectedManager
;
120 // Injected into CKKSViewManager using OCMock
121 @property BOOL overrideUseCKKSViewsFromPolicy
;
123 // Set this to true before calling -setup if you're going to configure the ckks view manager yourself
124 // !disable format used because this will be initialized to false, and so will happen unless subclasses take positive action
125 @property BOOL disableConfigureCKKSViewManagerWithViews
;
127 // Set this to true to override CKKSViewsFromPolicy to NO instead of the default YES
128 // !disable format used because this will be initialized to false, and so will happen unless subclasses take positive action
129 @property BOOL setCKKSViewsFromPolicyToNo
;
131 @
property (nullable
) OTManager
* injectedOTManager
;
133 // Fill this in to fail the next modifyzones operation
134 @
property (nullable
) NSError
* nextModifyRecordZonesError
;
136 // Used to track the test failure logger (for test teardown purposes)
137 @
property (class) CKKSTestFailureLogger
* testFailureLogger
;
139 - (CKKSKey
*)fakeTLK
:(CKRecordZoneID
*)zoneID
;
141 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfRecords
142 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
143 zoneID
:(CKRecordZoneID
*)zoneID
;
144 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfRecords
145 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
146 zoneID
:(CKRecordZoneID
*)zoneID
147 checkItem
:(BOOL (^_Nullable
)(CKRecord
*))checkItem
;
149 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfModifiedRecords
150 deletedRecords
:(NSUInteger
)expectedNumberOfDeletedRecords
151 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
152 zoneID
:(CKRecordZoneID
*)zoneID
153 checkItem
:(BOOL (^_Nullable
)(CKRecord
*))checkItem
;
155 - (void)expectCKDeleteItemRecords
:(NSUInteger
)expectedNumberOfRecords zoneID
:(CKRecordZoneID
*)zoneID
;
157 - (void)expectCKModifyKeyRecords
:(NSUInteger
)expectedNumberOfRecords
158 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
159 tlkShareRecords
:(NSUInteger
)expectedTLKShareRecords
160 zoneID
:(CKRecordZoneID
*)zoneID
;
161 - (void)expectCKModifyKeyRecords
:(NSUInteger
)expectedNumberOfRecords
162 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
163 tlkShareRecords
:(NSUInteger
)expectedTLKShareRecords
164 zoneID
:(CKRecordZoneID
*)zoneID
165 checkModifiedRecord
:(BOOL (^_Nullable
)(CKRecord
*))checkModifiedRecord
;
167 - (void)expectCKModifyRecords
:(NSDictionary
<NSString
*, NSNumber
*>*)expectedRecordTypeCounts
168 deletedRecordTypeCounts
:(NSDictionary
<NSString
*, NSNumber
*>* _Nullable
)expectedDeletedRecordTypeCounts
169 zoneID
:(CKRecordZoneID
*)zoneID
170 checkModifiedRecord
:(BOOL (^_Nullable
)(CKRecord
*))checkRecord
171 runAfterModification
:(void (^_Nullable
)(void))afterModification
;
173 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
;
174 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
175 blockAfterReject
:(void (^_Nullable
)(void))blockAfterReject
;
176 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
177 blockAfterReject
:(void (^_Nullable
)(void))blockAfterReject
178 withError
:(NSError
* _Nullable
)error
;
179 - (void)expectCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
;
181 - (void)failNextZoneCreation
:(CKRecordZoneID
*)zoneID
;
182 - (void)failNextZoneCreationSilently
:(CKRecordZoneID
*)zoneID
;
183 - (void)failNextZoneSubscription
:(CKRecordZoneID
*)zoneID
;
184 - (void)failNextZoneSubscription
:(CKRecordZoneID
*)zoneID withError
:(NSError
*)error
;
186 - (NSError
* _Nullable
)shouldFailModifyRecordZonesOperation
;
187 - (void)ensureZoneDeletionAllowed
:(FakeCKZone
*)zone
;
189 // Override this to interpose on how an OTManager is created
190 // This will be called during setUp, after the CloudKit mocks are in-place
191 // This needs to fill in the injectedOTManager variable on this class
192 - (OTManager
*)setUpOTManager
:(CKKSCloudKitClassDependencies
*)cloudKitClassDependencies
;
194 // Use this to assert that a fetch occurs (especially if silentFetchesAllowed = false)
195 - (void)expectCKFetch
;
197 // 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'.
198 // This way, you can modify the CK zone to cause later collisions.
199 - (void)expectCKFetchAndRunBeforeFinished
:(void (^_Nullable
)(void))blockAfterFetch
;
201 // Introspect the fetch object before allowing it to proceed
202 - (void)expectCKFetchWithFilter
:(BOOL (^)(FakeCKFetchRecordZoneChangesOperation
*))operationMatch
203 runBeforeFinished
:(void (^)(void))blockAfterFetch
;
205 // Use this to assert that a FakeCKFetchRecordsOperation occurs.
206 - (void)expectCKFetchByRecordID
;
208 // Use this to assert that a FakeCKQueryOperation occurs.
209 - (void)expectCKFetchByQuery
;
211 // Wait until all scheduled cloudkit operations are reflected in the currentDatabase
212 - (void)waitForCKModifications
;
214 // Unblocks the CKAccount mock subsystem. Until this is called, the tests believe cloudd hasn't returned any account status yet.
215 - (void)startCKAccountStatusMock
;
217 // Starts everything.
218 - (void)startCKKSSubsystem
;
220 // Blocks the completion (partial or full) of CloudKit modifications
221 - (void)holdCloudKitModifications
;
223 // Unblocks the hold you've added with holdCloudKitModifications; CloudKit modifications will finish
224 - (void)releaseCloudKitModificationHold
;
226 // Blocks the CloudKit fetches from beginning (similar to network latency)
227 - (void)holdCloudKitFetches
;
228 // Unblocks the hold you've added with holdCloudKitFetches; CloudKit fetches will finish
229 - (void)releaseCloudKitFetchHold
;
231 - (void)holdCloudKitModifyRecordZones
;
232 - (void)releaseCloudKitModifyRecordZonesHold
;
234 - (void)holdCloudKitModifySubscription
;
235 - (void)releaseCloudKitModifySubscriptionHold
;
238 // Make a CK internal server extension error with a given code and description.
239 - (NSError
*)ckInternalServerExtensionError
:(NSInteger
)code description
:(NSString
*)desc
;
241 // Schedule an operation for execution (and failure), with some existing record errors.
242 // Other records in the operation but not in failedRecords will have CKErrorBatchRequestFailed errors created.
243 - (void)rejectWrite
:(CKModifyRecordsOperation
*)op failedRecords
:(NSMutableDictionary
<CKRecordID
*, NSError
*>*)failedRecords
;
247 NS_ASSUME_NONNULL_END