]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/tests/CloudKitMockXCTest.h
Security-59754.80.3.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 #import "keychain/trust/TrustedPeers/TPSyncingPolicy.h"
40
41 NS_ASSUME_NONNULL_BEGIN
42
43 @class CKKSKey;
44 @class CKKSCKRecordHolder;
45 @class CKKSKeychainView;
46 @class CKKSViewManager;
47 @class FakeCKZone;
48 @class CKKSLockStateTracker;
49 @class CKKSReachabilityTracker;
50 @class SOSCKKSPeerAdapter;
51
52 @interface CKKSTestFailureLogger : NSObject <XCTestObservation>
53 @end
54
55 @interface CloudKitMockXCTest : XCTestCase
56
57 @property CKRecordZoneID* testZoneID;
58
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;
68
69 @property (nullable) id mockAccountStateTracker;
70
71 @property (nullable) id mockTTR;
72 @property BOOL isTTRRatelimited;
73 @property (nullable) XCTestExpectation *ttrExpectation;
74
75 // The CloudKit account status
76 @property CKAccountStatus accountStatus;
77
78 // The current HSA2-ness of the world
79 // Set to 'unknown' to not inject any answer into
80 @property CKKSAccountStatus fakeHSA2AccountStatus;
81
82 @property BOOL iCloudHasValidCredentials;
83
84 @property (readonly) NSString* ckDeviceID;
85 @property (readonly) CKKSAccountStateTracker* accountStateTracker;
86
87
88 @property NSString* apsEnvironment;
89
90 @property bool aksLockState; // The current 'AKS lock state'
91 @property (readonly) CKKSLockStateTracker* lockStateTracker;
92 @property (nullable) id mockLockStateTracker;
93
94 @property (readonly) CKKSReachabilityTracker *reachabilityTracker;
95
96 @property (nullable) NSMutableDictionary<CKRecordZoneID*, FakeCKZone*>* zones;
97
98 @property NSOperationQueue* operationQueue;
99 @property (nullable) NSBlockOperation* ckaccountHoldOperation;
100
101 @property (nullable) NSBlockOperation* ckModifyHoldOperation;
102 @property (nullable) NSBlockOperation* ckFetchHoldOperation;
103 @property (nullable) NSBlockOperation* ckModifyRecordZonesHoldOperation;
104 @property (nullable) NSBlockOperation* ckModifySubscriptionsHoldOperation;
105
106 @property bool silentFetchesAllowed;
107 @property bool silentZoneDeletesAllowed;
108
109 @property CKKSMockSOSPresentAdapter* mockSOSAdapter;
110 @property (nullable) CKKSMockOctagonAdapter *mockOctagonAdapter;
111
112 - (NSSet<NSString*>*)managedViewList;
113 - (TPSyncingPolicy*)viewSortingPolicyForManagedViewList;
114 - (TPSyncingPolicy*)viewSortingPolicyForManagedViewListWithUserControllableViews:(NSSet<NSString*>*)ucv
115 syncUserControllableViews:(TPPBPeerStableInfo_UserControllableViewStatus)syncUserControllableViews;
116
117 @property (nullable) id mockCKKSViewManager;
118 @property (nullable) CKKSViewManager* injectedManager;
119
120 // Injected into CKKSViewManager using OCMock
121 @property BOOL overrideUseCKKSViewsFromPolicy;
122
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;
126
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;
130
131 @property (nullable) OTManager* injectedOTManager;
132
133 // Fill this in to fail the next modifyzones operation
134 @property (nullable) NSError* nextModifyRecordZonesError;
135
136 // Used to track the test failure logger (for test teardown purposes)
137 @property (class) CKKSTestFailureLogger* testFailureLogger;
138
139 - (CKKSKey*)fakeTLK:(CKRecordZoneID*)zoneID;
140
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;
148
149 - (void)expectCKModifyItemRecords:(NSUInteger)expectedNumberOfModifiedRecords
150 deletedRecords:(NSUInteger)expectedNumberOfDeletedRecords
151 currentKeyPointerRecords:(NSUInteger)expectedCurrentKeyRecords
152 zoneID:(CKRecordZoneID*)zoneID
153 checkItem:(BOOL (^_Nullable)(CKRecord*))checkItem;
154
155 - (void)expectCKDeleteItemRecords:(NSUInteger)expectedNumberOfRecords zoneID:(CKRecordZoneID*)zoneID;
156
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;
166
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;
172
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;
180
181 - (void)failNextZoneCreation:(CKRecordZoneID*)zoneID;
182 - (void)failNextZoneCreationSilently:(CKRecordZoneID*)zoneID;
183 - (void)failNextZoneSubscription:(CKRecordZoneID*)zoneID;
184 - (void)failNextZoneSubscription:(CKRecordZoneID*)zoneID withError:(NSError*)error;
185
186 - (NSError* _Nullable)shouldFailModifyRecordZonesOperation;
187 - (void)ensureZoneDeletionAllowed:(FakeCKZone*)zone;
188
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;
193
194 // Use this to assert that a fetch occurs (especially if silentFetchesAllowed = false)
195 - (void)expectCKFetch;
196
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;
200
201 // Introspect the fetch object before allowing it to proceed
202 - (void)expectCKFetchWithFilter:(BOOL (^)(FakeCKFetchRecordZoneChangesOperation*))operationMatch
203 runBeforeFinished:(void (^)(void))blockAfterFetch;
204
205 // Use this to assert that a FakeCKFetchRecordsOperation occurs.
206 - (void)expectCKFetchByRecordID;
207
208 // Use this to assert that a FakeCKQueryOperation occurs.
209 - (void)expectCKFetchByQuery;
210
211 // Wait until all scheduled cloudkit operations are reflected in the currentDatabase
212 - (void)waitForCKModifications;
213
214 // Unblocks the CKAccount mock subsystem. Until this is called, the tests believe cloudd hasn't returned any account status yet.
215 - (void)startCKAccountStatusMock;
216
217 // Starts everything.
218 - (void)startCKKSSubsystem;
219
220 // Blocks the completion (partial or full) of CloudKit modifications
221 - (void)holdCloudKitModifications;
222
223 // Unblocks the hold you've added with holdCloudKitModifications; CloudKit modifications will finish
224 - (void)releaseCloudKitModificationHold;
225
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;
230
231 - (void)holdCloudKitModifyRecordZones;
232 - (void)releaseCloudKitModifyRecordZonesHold;
233
234 - (void)holdCloudKitModifySubscription;
235 - (void)releaseCloudKitModifySubscriptionHold;
236
237
238 // Make a CK internal server extension error with a given code and description.
239 - (NSError*)ckInternalServerExtensionError:(NSInteger)code description:(NSString*)desc;
240
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;
244
245 @end
246
247 NS_ASSUME_NONNULL_END
248
249 #endif /* OCTAGON */