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 NS_ASSUME_NONNULL_BEGIN
41 @
class CKKSCKRecordHolder
;
42 @
class CKKSKeychainView
;
43 @
class CKKSViewManager
;
45 @
class CKKSLockStateTracker
;
46 @
class CKKSReachabilityTracker
;
47 @
class SOSCKKSPeerAdapter
;
49 @interface CloudKitMockXCTest
: XCTestCase
51 @property CKRecordZoneID
* testZoneID
;
53 @
property (nullable
) id mockDatabase
;
54 @
property (nullable
) id mockDatabaseExceptionCatcher
;
55 @
property (nullable
) id mockContainer
;
56 @
property (nullable
) id mockContainerExpectations
;
57 @
property (nullable
) id mockFakeCKModifyRecordZonesOperation
;
58 @
property (nullable
) id mockFakeCKModifySubscriptionsOperation
;
59 @
property (nullable
) id mockFakeCKFetchRecordZoneChangesOperation
;
60 @
property (nullable
) id mockFakeCKFetchRecordsOperation
;
61 @
property (nullable
) id mockFakeCKQueryOperation
;
63 @
property (nullable
) id mockAccountStateTracker
;
65 @
property (nullable
) id mockTTR
;
66 @property BOOL isTTRRatelimited
;
67 @
property (nullable
) XCTestExpectation
*ttrExpectation
;
69 // The CloudKit account status
70 @property CKAccountStatus accountStatus
;
72 // The current HSA2-ness of the world
73 // Set to 'unknown' to not inject any answer into
74 @property CKKSAccountStatus fakeHSA2AccountStatus
;
76 @property BOOL iCloudHasValidCredentials
;
78 @
property (readonly
) NSString
* ckDeviceID
;
79 @
property (readonly
) CKKSAccountStateTracker
* accountStateTracker
;
82 @property NSString
* apsEnvironment
;
84 @property
bool aksLockState
; // The current 'AKS lock state'
85 @
property (readonly
) CKKSLockStateTracker
* lockStateTracker
;
86 @
property (nullable
) id mockLockStateTracker
;
88 @
property (readonly
) CKKSReachabilityTracker
*reachabilityTracker
;
90 @
property (nullable
) NSMutableDictionary
<CKRecordZoneID
*, FakeCKZone
*>* zones
;
92 @property NSOperationQueue
* operationQueue
;
93 @
property (nullable
) NSBlockOperation
* ckaccountHoldOperation
;
95 @
property (nullable
) NSBlockOperation
* ckModifyHoldOperation
;
96 @
property (nullable
) NSBlockOperation
* ckFetchHoldOperation
;
97 @
property (nullable
) NSBlockOperation
* ckModifyRecordZonesHoldOperation
;
98 @
property (nullable
) NSBlockOperation
* ckModifySubscriptionsHoldOperation
;
100 @property
bool silentFetchesAllowed
;
101 @property
bool silentZoneDeletesAllowed
;
103 @property CKKSMockSOSPresentAdapter
* mockSOSAdapter
;
104 @
property (nullable
) CKKSMockOctagonAdapter
*mockOctagonAdapter
;
106 -(NSSet
*)managedViewList
;
107 @
property (nullable
) id mockCKKSViewManager
;
108 @
property (nullable
) CKKSViewManager
* injectedManager
;
110 // Fill this in to fail the next modifyzones operation
111 @
property (nullable
) NSError
* nextModifyRecordZonesError
;
113 - (CKKSKey
*)fakeTLK
:(CKRecordZoneID
*)zoneID
;
115 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfRecords
116 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
117 zoneID
:(CKRecordZoneID
*)zoneID
;
118 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfRecords
119 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
120 zoneID
:(CKRecordZoneID
*)zoneID
121 checkItem
:(BOOL (^_Nullable
)(CKRecord
*))checkItem
;
123 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfModifiedRecords
124 deletedRecords
:(NSUInteger
)expectedNumberOfDeletedRecords
125 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
126 zoneID
:(CKRecordZoneID
*)zoneID
127 checkItem
:(BOOL (^_Nullable
)(CKRecord
*))checkItem
;
129 - (void)expectCKDeleteItemRecords
:(NSUInteger
)expectedNumberOfRecords zoneID
:(CKRecordZoneID
*)zoneID
;
131 - (void)expectCKModifyKeyRecords
:(NSUInteger
)expectedNumberOfRecords
132 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
133 tlkShareRecords
:(NSUInteger
)expectedTLKShareRecords
134 zoneID
:(CKRecordZoneID
*)zoneID
;
135 - (void)expectCKModifyKeyRecords
:(NSUInteger
)expectedNumberOfRecords
136 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
137 tlkShareRecords
:(NSUInteger
)expectedTLKShareRecords
138 zoneID
:(CKRecordZoneID
*)zoneID
139 checkModifiedRecord
:(BOOL (^_Nullable
)(CKRecord
*))checkModifiedRecord
;
141 - (void)expectCKModifyRecords
:(NSDictionary
<NSString
*, NSNumber
*>*)expectedRecordTypeCounts
142 deletedRecordTypeCounts
:(NSDictionary
<NSString
*, NSNumber
*>* _Nullable
)expectedDeletedRecordTypeCounts
143 zoneID
:(CKRecordZoneID
*)zoneID
144 checkModifiedRecord
:(BOOL (^_Nullable
)(CKRecord
*))checkRecord
145 runAfterModification
:(void (^_Nullable
)(void))afterModification
;
147 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
;
148 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
149 blockAfterReject
:(void (^_Nullable
)(void))blockAfterReject
;
150 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
151 blockAfterReject
:(void (^_Nullable
)(void))blockAfterReject
152 withError
:(NSError
* _Nullable
)error
;
153 - (void)expectCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
;
155 - (void)failNextZoneCreation
:(CKRecordZoneID
*)zoneID
;
156 - (void)failNextZoneCreationSilently
:(CKRecordZoneID
*)zoneID
;
157 - (void)failNextZoneSubscription
:(CKRecordZoneID
*)zoneID
;
158 - (void)failNextZoneSubscription
:(CKRecordZoneID
*)zoneID withError
:(NSError
*)error
;
160 - (NSError
* _Nullable
)shouldFailModifyRecordZonesOperation
;
161 - (void)ensureZoneDeletionAllowed
:(FakeCKZone
*)zone
;
163 // Use this to assert that a fetch occurs (especially if silentFetchesAllowed = false)
164 - (void)expectCKFetch
;
166 // 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'.
167 // This way, you can modify the CK zone to cause later collisions.
168 - (void)expectCKFetchAndRunBeforeFinished
:(void (^_Nullable
)(void))blockAfterFetch
;
170 // Introspect the fetch object before allowing it to proceed
171 - (void)expectCKFetchWithFilter
:(BOOL (^)(FakeCKFetchRecordZoneChangesOperation
*))operationMatch
172 runBeforeFinished
:(void (^)(void))blockAfterFetch
;
174 // Use this to assert that a FakeCKFetchRecordsOperation occurs.
175 - (void)expectCKFetchByRecordID
;
177 // Use this to assert that a FakeCKQueryOperation occurs.
178 - (void)expectCKFetchByQuery
;
180 // Wait until all scheduled cloudkit operations are reflected in the currentDatabase
181 - (void)waitForCKModifications
;
183 // Unblocks the CKAccount mock subsystem. Until this is called, the tests believe cloudd hasn't returned any account status yet.
184 - (void)startCKAccountStatusMock
;
186 // Starts everything.
187 - (void)startCKKSSubsystem
;
189 // Blocks the completion (partial or full) of CloudKit modifications
190 - (void)holdCloudKitModifications
;
192 // Unblocks the hold you've added with holdCloudKitModifications; CloudKit modifications will finish
193 - (void)releaseCloudKitModificationHold
;
195 // Blocks the CloudKit fetches from beginning (similar to network latency)
196 - (void)holdCloudKitFetches
;
197 // Unblocks the hold you've added with holdCloudKitFetches; CloudKit fetches will finish
198 - (void)releaseCloudKitFetchHold
;
200 - (void)holdCloudKitModifyRecordZones
;
201 - (void)releaseCloudKitModifyRecordZonesHold
;
203 - (void)holdCloudKitModifySubscription
;
204 - (void)releaseCloudKitModifySubscriptionHold
;
207 // Make a CK internal server extension error with a given code and description.
208 - (NSError
*)ckInternalServerExtensionError
:(NSInteger
)code description
:(NSString
*)desc
;
210 // Schedule an operation for execution (and failure), with some existing record errors.
211 // Other records in the operation but not in failedRecords will have CKErrorBatchRequestFailed errors created.
212 - (void)rejectWrite
:(CKModifyRecordsOperation
*)op failedRecords
:(NSMutableDictionary
<CKRecordID
*, NSError
*>*)failedRecords
;
216 NS_ASSUME_NONNULL_END