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
;
98 @property
bool silentFetchesAllowed
;
99 @property
bool silentZoneDeletesAllowed
;
101 @property CKKSMockSOSPresentAdapter
* mockSOSAdapter
;
102 @
property (nullable
) CKKSMockOctagonAdapter
*mockOctagonAdapter
;
104 -(NSSet
*)managedViewList
;
105 @
property (nullable
) id mockCKKSViewManager
;
106 @
property (nullable
) CKKSViewManager
* injectedManager
;
108 // Fill this in to fail the next modifyzones operation
109 @
property (nullable
) NSError
* nextModifyRecordZonesError
;
111 - (CKKSKey
*)fakeTLK
:(CKRecordZoneID
*)zoneID
;
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
;
121 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfModifiedRecords
122 deletedRecords
:(NSUInteger
)expectedNumberOfDeletedRecords
123 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
124 zoneID
:(CKRecordZoneID
*)zoneID
125 checkItem
:(BOOL (^_Nullable
)(CKRecord
*))checkItem
;
127 - (void)expectCKDeleteItemRecords
:(NSUInteger
)expectedNumberOfRecords zoneID
:(CKRecordZoneID
*)zoneID
;
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
;
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
;
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
;
153 - (void)failNextZoneCreation
:(CKRecordZoneID
*)zoneID
;
154 - (void)failNextZoneCreationSilently
:(CKRecordZoneID
*)zoneID
;
155 - (void)failNextZoneSubscription
:(CKRecordZoneID
*)zoneID
;
156 - (void)failNextZoneSubscription
:(CKRecordZoneID
*)zoneID withError
:(NSError
*)error
;
158 - (NSError
* _Nullable
)shouldFailModifyRecordZonesOperation
;
159 - (void)ensureZoneDeletionAllowed
:(FakeCKZone
*)zone
;
161 // Use this to assert that a fetch occurs (especially if silentFetchesAllowed = false)
162 - (void)expectCKFetch
;
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
;
168 // Introspect the fetch object before allowing it to proceed
169 - (void)expectCKFetchWithFilter
:(BOOL (^)(FakeCKFetchRecordZoneChangesOperation
*))operationMatch
170 runBeforeFinished
:(void (^)(void))blockAfterFetch
;
172 // Use this to assert that a FakeCKFetchRecordsOperation occurs.
173 - (void)expectCKFetchByRecordID
;
175 // Use this to assert that a FakeCKQueryOperation occurs.
176 - (void)expectCKFetchByQuery
;
178 // Wait until all scheduled cloudkit operations are reflected in the currentDatabase
179 - (void)waitForCKModifications
;
181 // Unblocks the CKAccount mock subsystem. Until this is called, the tests believe cloudd hasn't returned any account status yet.
182 - (void)startCKAccountStatusMock
;
184 // Starts everything.
185 - (void)startCKKSSubsystem
;
187 // Blocks the completion (partial or full) of CloudKit modifications
188 - (void)holdCloudKitModifications
;
190 // Unblocks the hold you've added with holdCloudKitModifications; CloudKit modifications will finish
191 - (void)releaseCloudKitModificationHold
;
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
;
198 // Make a CK internal server extension error with a given code and description.
199 - (NSError
*)ckInternalServerExtensionError
:(NSInteger
)code description
:(NSString
*)desc
;
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
;
207 NS_ASSUME_NONNULL_END