]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/tests/CloudKitMockXCTest.h
Security-59306.11.20.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
98 @property bool silentFetchesAllowed;
99 @property bool silentZoneDeletesAllowed;
100
101 @property CKKSMockSOSPresentAdapter* mockSOSAdapter;
102 @property (nullable) CKKSMockOctagonAdapter *mockOctagonAdapter;
103
104 -(NSSet*)managedViewList;
105 @property (nullable) id mockCKKSViewManager;
106 @property (nullable) CKKSViewManager* injectedManager;
107
108 // Fill this in to fail the next modifyzones operation
109 @property (nullable) NSError* nextModifyRecordZonesError;
110
111 - (CKKSKey*)fakeTLK:(CKRecordZoneID*)zoneID;
112
113 - (void)expectCKModifyItemRecords:(NSUInteger)expectedNumberOfRecords
114 currentKeyPointerRecords:(NSUInteger)expectedCurrentKeyRecords
115 zoneID:(CKRecordZoneID*)zoneID;
116 - (void)expectCKModifyItemRecords:(NSUInteger)expectedNumberOfRecords
117 currentKeyPointerRecords:(NSUInteger)expectedCurrentKeyRecords
118 zoneID:(CKRecordZoneID*)zoneID
119 checkItem:(BOOL (^_Nullable)(CKRecord*))checkItem;
120
121 - (void)expectCKModifyItemRecords:(NSUInteger)expectedNumberOfModifiedRecords
122 deletedRecords:(NSUInteger)expectedNumberOfDeletedRecords
123 currentKeyPointerRecords:(NSUInteger)expectedCurrentKeyRecords
124 zoneID:(CKRecordZoneID*)zoneID
125 checkItem:(BOOL (^_Nullable)(CKRecord*))checkItem;
126
127 - (void)expectCKDeleteItemRecords:(NSUInteger)expectedNumberOfRecords zoneID:(CKRecordZoneID*)zoneID;
128
129 - (void)expectCKModifyKeyRecords:(NSUInteger)expectedNumberOfRecords
130 currentKeyPointerRecords:(NSUInteger)expectedCurrentKeyRecords
131 tlkShareRecords:(NSUInteger)expectedTLKShareRecords
132 zoneID:(CKRecordZoneID*)zoneID;
133 - (void)expectCKModifyKeyRecords:(NSUInteger)expectedNumberOfRecords
134 currentKeyPointerRecords:(NSUInteger)expectedCurrentKeyRecords
135 tlkShareRecords:(NSUInteger)expectedTLKShareRecords
136 zoneID:(CKRecordZoneID*)zoneID
137 checkModifiedRecord:(BOOL (^_Nullable)(CKRecord*))checkModifiedRecord;
138
139 - (void)expectCKModifyRecords:(NSDictionary<NSString*, NSNumber*>*)expectedRecordTypeCounts
140 deletedRecordTypeCounts:(NSDictionary<NSString*, NSNumber*>* _Nullable)expectedDeletedRecordTypeCounts
141 zoneID:(CKRecordZoneID*)zoneID
142 checkModifiedRecord:(BOOL (^_Nullable)(CKRecord*))checkRecord
143 runAfterModification:(void (^_Nullable)(void))afterModification;
144
145 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure:(CKRecordZoneID*)zoneID;
146 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure:(CKRecordZoneID*)zoneID
147 blockAfterReject:(void (^_Nullable)(void))blockAfterReject;
148 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure:(CKRecordZoneID*)zoneID
149 blockAfterReject:(void (^_Nullable)(void))blockAfterReject
150 withError:(NSError* _Nullable)error;
151 - (void)expectCKAtomicModifyItemRecordsUpdateFailure:(CKRecordZoneID*)zoneID;
152
153 - (void)failNextZoneCreation:(CKRecordZoneID*)zoneID;
154 - (void)failNextZoneCreationSilently:(CKRecordZoneID*)zoneID;
155 - (void)failNextZoneSubscription:(CKRecordZoneID*)zoneID;
156 - (void)failNextZoneSubscription:(CKRecordZoneID*)zoneID withError:(NSError*)error;
157
158 - (NSError* _Nullable)shouldFailModifyRecordZonesOperation;
159 - (void)ensureZoneDeletionAllowed:(FakeCKZone*)zone;
160
161 // Use this to assert that a fetch occurs (especially if silentFetchesAllowed = false)
162 - (void)expectCKFetch;
163
164 // 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'.
165 // This way, you can modify the CK zone to cause later collisions.
166 - (void)expectCKFetchAndRunBeforeFinished:(void (^_Nullable)(void))blockAfterFetch;
167
168 // Introspect the fetch object before allowing it to proceed
169 - (void)expectCKFetchWithFilter:(BOOL (^)(FakeCKFetchRecordZoneChangesOperation*))operationMatch
170 runBeforeFinished:(void (^)(void))blockAfterFetch;
171
172 // Use this to assert that a FakeCKFetchRecordsOperation occurs.
173 - (void)expectCKFetchByRecordID;
174
175 // Use this to assert that a FakeCKQueryOperation occurs.
176 - (void)expectCKFetchByQuery;
177
178 // Wait until all scheduled cloudkit operations are reflected in the currentDatabase
179 - (void)waitForCKModifications;
180
181 // Unblocks the CKAccount mock subsystem. Until this is called, the tests believe cloudd hasn't returned any account status yet.
182 - (void)startCKAccountStatusMock;
183
184 // Starts everything.
185 - (void)startCKKSSubsystem;
186
187 // Blocks the completion (partial or full) of CloudKit modifications
188 - (void)holdCloudKitModifications;
189
190 // Unblocks the hold you've added with holdCloudKitModifications; CloudKit modifications will finish
191 - (void)releaseCloudKitModificationHold;
192
193 // Blocks the CloudKit fetches from beginning (similar to network latency)
194 - (void)holdCloudKitFetches;
195 // Unblocks the hold you've added with holdCloudKitFetches; CloudKit fetches will finish
196 - (void)releaseCloudKitFetchHold;
197
198 // Make a CK internal server extension error with a given code and description.
199 - (NSError*)ckInternalServerExtensionError:(NSInteger)code description:(NSString*)desc;
200
201 // Schedule an operation for execution (and failure), with some existing record errors.
202 // Other records in the operation but not in failedRecords will have CKErrorBatchRequestFailed errors created.
203 - (void)rejectWrite:(CKModifyRecordsOperation*)op failedRecords:(NSMutableDictionary<CKRecordID*, NSError*>*)failedRecords;
204
205 @end
206
207 NS_ASSUME_NONNULL_END
208
209 #endif /* OCTAGON */