]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/tests/CloudKitMockXCTest.h
Security-59306.101.1.tar.gz
[apple/security.git] / keychain / ckks / tests / CloudKitMockXCTest.h
1 /*
2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #if OCTAGON
25
26 #import <CloudKit/CloudKit.h>
27 #import <CloudKit/CloudKit_Private.h>
28 #import <XCTest/XCTest.h>
29
30 #import <Foundation/Foundation.h>
31 #import <SystemConfiguration/SystemConfiguration.h>
32
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"
37
38 #import "keychain/ot/OTManager.h"
39
40 NS_ASSUME_NONNULL_BEGIN
41
42 @class CKKSKey;
43 @class CKKSCKRecordHolder;
44 @class CKKSKeychainView;
45 @class CKKSViewManager;
46 @class FakeCKZone;
47 @class CKKSLockStateTracker;
48 @class CKKSReachabilityTracker;
49 @class SOSCKKSPeerAdapter;
50
51 @interface CloudKitMockXCTest : XCTestCase
52
53 @property CKRecordZoneID* testZoneID;
54
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;
64
65 @property (nullable) id mockAccountStateTracker;
66
67 @property (nullable) id mockTTR;
68 @property BOOL isTTRRatelimited;
69 @property (nullable) XCTestExpectation *ttrExpectation;
70
71 // The CloudKit account status
72 @property CKAccountStatus accountStatus;
73
74 // The current HSA2-ness of the world
75 // Set to 'unknown' to not inject any answer into
76 @property CKKSAccountStatus fakeHSA2AccountStatus;
77
78 @property BOOL iCloudHasValidCredentials;
79
80 @property (readonly) NSString* ckDeviceID;
81 @property (readonly) CKKSAccountStateTracker* accountStateTracker;
82
83
84 @property NSString* apsEnvironment;
85
86 @property bool aksLockState; // The current 'AKS lock state'
87 @property (readonly) CKKSLockStateTracker* lockStateTracker;
88 @property (nullable) id mockLockStateTracker;
89
90 @property (readonly) CKKSReachabilityTracker *reachabilityTracker;
91
92 @property (nullable) NSMutableDictionary<CKRecordZoneID*, FakeCKZone*>* zones;
93
94 @property NSOperationQueue* operationQueue;
95 @property (nullable) NSBlockOperation* ckaccountHoldOperation;
96
97 @property (nullable) NSBlockOperation* ckModifyHoldOperation;
98 @property (nullable) NSBlockOperation* ckFetchHoldOperation;
99 @property (nullable) NSBlockOperation* ckModifyRecordZonesHoldOperation;
100 @property (nullable) NSBlockOperation* ckModifySubscriptionsHoldOperation;
101
102 @property bool silentFetchesAllowed;
103 @property bool silentZoneDeletesAllowed;
104
105 @property CKKSMockSOSPresentAdapter* mockSOSAdapter;
106 @property (nullable) CKKSMockOctagonAdapter *mockOctagonAdapter;
107
108 - (NSSet<NSString*>*)managedViewList;
109 - (TPPolicy*)viewSortingPolicyForManagedViewList;
110
111 @property (nullable) id mockCKKSViewManager;
112 @property (nullable) CKKSViewManager* injectedManager;
113
114 // Injected into CKKSViewManager using OCMock
115 @property BOOL overrideUseCKKSViewsFromPolicy;
116
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;
120
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;
124
125 @property (nullable) OTManager* injectedOTManager;
126
127 // Fill this in to fail the next modifyzones operation
128 @property (nullable) NSError* nextModifyRecordZonesError;
129
130 - (CKKSKey*)fakeTLK:(CKRecordZoneID*)zoneID;
131
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;
139
140 - (void)expectCKModifyItemRecords:(NSUInteger)expectedNumberOfModifiedRecords
141 deletedRecords:(NSUInteger)expectedNumberOfDeletedRecords
142 currentKeyPointerRecords:(NSUInteger)expectedCurrentKeyRecords
143 zoneID:(CKRecordZoneID*)zoneID
144 checkItem:(BOOL (^_Nullable)(CKRecord*))checkItem;
145
146 - (void)expectCKDeleteItemRecords:(NSUInteger)expectedNumberOfRecords zoneID:(CKRecordZoneID*)zoneID;
147
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;
157
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;
163
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;
171
172 - (void)failNextZoneCreation:(CKRecordZoneID*)zoneID;
173 - (void)failNextZoneCreationSilently:(CKRecordZoneID*)zoneID;
174 - (void)failNextZoneSubscription:(CKRecordZoneID*)zoneID;
175 - (void)failNextZoneSubscription:(CKRecordZoneID*)zoneID withError:(NSError*)error;
176
177 - (NSError* _Nullable)shouldFailModifyRecordZonesOperation;
178 - (void)ensureZoneDeletionAllowed:(FakeCKZone*)zone;
179
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;
184
185 // Use this to assert that a fetch occurs (especially if silentFetchesAllowed = false)
186 - (void)expectCKFetch;
187
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;
191
192 // Introspect the fetch object before allowing it to proceed
193 - (void)expectCKFetchWithFilter:(BOOL (^)(FakeCKFetchRecordZoneChangesOperation*))operationMatch
194 runBeforeFinished:(void (^)(void))blockAfterFetch;
195
196 // Use this to assert that a FakeCKFetchRecordsOperation occurs.
197 - (void)expectCKFetchByRecordID;
198
199 // Use this to assert that a FakeCKQueryOperation occurs.
200 - (void)expectCKFetchByQuery;
201
202 // Wait until all scheduled cloudkit operations are reflected in the currentDatabase
203 - (void)waitForCKModifications;
204
205 // Unblocks the CKAccount mock subsystem. Until this is called, the tests believe cloudd hasn't returned any account status yet.
206 - (void)startCKAccountStatusMock;
207
208 // Starts everything.
209 - (void)startCKKSSubsystem;
210
211 // Blocks the completion (partial or full) of CloudKit modifications
212 - (void)holdCloudKitModifications;
213
214 // Unblocks the hold you've added with holdCloudKitModifications; CloudKit modifications will finish
215 - (void)releaseCloudKitModificationHold;
216
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;
221
222 - (void)holdCloudKitModifyRecordZones;
223 - (void)releaseCloudKitModifyRecordZonesHold;
224
225 - (void)holdCloudKitModifySubscription;
226 - (void)releaseCloudKitModifySubscriptionHold;
227
228
229 // Make a CK internal server extension error with a given code and description.
230 - (NSError*)ckInternalServerExtensionError:(NSInteger)code description:(NSString*)desc;
231
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;
235
236 @end
237
238 NS_ASSUME_NONNULL_END
239
240 #endif /* OCTAGON */