]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/tests/CloudKitMockXCTest.h
Security-59306.61.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 NS_ASSUME_NONNULL_BEGIN
39
40 @class CKKSKey;
41 @class CKKSCKRecordHolder;
42 @class CKKSKeychainView;
43 @class CKKSViewManager;
44 @class FakeCKZone;
45 @class CKKSLockStateTracker;
46 @class CKKSReachabilityTracker;
47 @class SOSCKKSPeerAdapter;
48
49 @interface CloudKitMockXCTest : XCTestCase
50
51 @property CKRecordZoneID* testZoneID;
52
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;
62
63 @property (nullable) id mockAccountStateTracker;
64
65 @property (nullable) id mockTTR;
66 @property BOOL isTTRRatelimited;
67 @property (nullable) XCTestExpectation *ttrExpectation;
68
69 // The CloudKit account status
70 @property CKAccountStatus accountStatus;
71
72 // The current HSA2-ness of the world
73 // Set to 'unknown' to not inject any answer into
74 @property CKKSAccountStatus fakeHSA2AccountStatus;
75
76 @property BOOL iCloudHasValidCredentials;
77
78 @property (readonly) NSString* ckDeviceID;
79 @property (readonly) CKKSAccountStateTracker* accountStateTracker;
80
81
82 @property NSString* apsEnvironment;
83
84 @property bool aksLockState; // The current 'AKS lock state'
85 @property (readonly) CKKSLockStateTracker* lockStateTracker;
86 @property (nullable) id mockLockStateTracker;
87
88 @property (readonly) CKKSReachabilityTracker *reachabilityTracker;
89
90 @property (nullable) NSMutableDictionary<CKRecordZoneID*, FakeCKZone*>* zones;
91
92 @property NSOperationQueue* operationQueue;
93 @property (nullable) NSBlockOperation* ckaccountHoldOperation;
94
95 @property (nullable) NSBlockOperation* ckModifyHoldOperation;
96 @property (nullable) NSBlockOperation* ckFetchHoldOperation;
97 @property (nullable) NSBlockOperation* ckModifyRecordZonesHoldOperation;
98 @property (nullable) NSBlockOperation* ckModifySubscriptionsHoldOperation;
99
100 @property bool silentFetchesAllowed;
101 @property bool silentZoneDeletesAllowed;
102
103 @property CKKSMockSOSPresentAdapter* mockSOSAdapter;
104 @property (nullable) CKKSMockOctagonAdapter *mockOctagonAdapter;
105
106 -(NSSet*)managedViewList;
107 @property (nullable) id mockCKKSViewManager;
108 @property (nullable) CKKSViewManager* injectedManager;
109
110 // Fill this in to fail the next modifyzones operation
111 @property (nullable) NSError* nextModifyRecordZonesError;
112
113 - (CKKSKey*)fakeTLK:(CKRecordZoneID*)zoneID;
114
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;
122
123 - (void)expectCKModifyItemRecords:(NSUInteger)expectedNumberOfModifiedRecords
124 deletedRecords:(NSUInteger)expectedNumberOfDeletedRecords
125 currentKeyPointerRecords:(NSUInteger)expectedCurrentKeyRecords
126 zoneID:(CKRecordZoneID*)zoneID
127 checkItem:(BOOL (^_Nullable)(CKRecord*))checkItem;
128
129 - (void)expectCKDeleteItemRecords:(NSUInteger)expectedNumberOfRecords zoneID:(CKRecordZoneID*)zoneID;
130
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;
140
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;
146
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;
154
155 - (void)failNextZoneCreation:(CKRecordZoneID*)zoneID;
156 - (void)failNextZoneCreationSilently:(CKRecordZoneID*)zoneID;
157 - (void)failNextZoneSubscription:(CKRecordZoneID*)zoneID;
158 - (void)failNextZoneSubscription:(CKRecordZoneID*)zoneID withError:(NSError*)error;
159
160 - (NSError* _Nullable)shouldFailModifyRecordZonesOperation;
161 - (void)ensureZoneDeletionAllowed:(FakeCKZone*)zone;
162
163 // Use this to assert that a fetch occurs (especially if silentFetchesAllowed = false)
164 - (void)expectCKFetch;
165
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;
169
170 // Introspect the fetch object before allowing it to proceed
171 - (void)expectCKFetchWithFilter:(BOOL (^)(FakeCKFetchRecordZoneChangesOperation*))operationMatch
172 runBeforeFinished:(void (^)(void))blockAfterFetch;
173
174 // Use this to assert that a FakeCKFetchRecordsOperation occurs.
175 - (void)expectCKFetchByRecordID;
176
177 // Use this to assert that a FakeCKQueryOperation occurs.
178 - (void)expectCKFetchByQuery;
179
180 // Wait until all scheduled cloudkit operations are reflected in the currentDatabase
181 - (void)waitForCKModifications;
182
183 // Unblocks the CKAccount mock subsystem. Until this is called, the tests believe cloudd hasn't returned any account status yet.
184 - (void)startCKAccountStatusMock;
185
186 // Starts everything.
187 - (void)startCKKSSubsystem;
188
189 // Blocks the completion (partial or full) of CloudKit modifications
190 - (void)holdCloudKitModifications;
191
192 // Unblocks the hold you've added with holdCloudKitModifications; CloudKit modifications will finish
193 - (void)releaseCloudKitModificationHold;
194
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;
199
200 - (void)holdCloudKitModifyRecordZones;
201 - (void)releaseCloudKitModifyRecordZonesHold;
202
203 - (void)holdCloudKitModifySubscription;
204 - (void)releaseCloudKitModifySubscriptionHold;
205
206
207 // Make a CK internal server extension error with a given code and description.
208 - (NSError*)ckInternalServerExtensionError:(NSInteger)code description:(NSString*)desc;
209
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;
213
214 @end
215
216 NS_ASSUME_NONNULL_END
217
218 #endif /* OCTAGON */