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/CKKSCKAccountStateTracker.h"
34 #import "keychain/ckks/tests/MockCloudKit.h"
36 NS_ASSUME_NONNULL_BEGIN
39 @
class CKKSCKRecordHolder
;
40 @
class CKKSKeychainView
;
41 @
class CKKSViewManager
;
43 @
class CKKSLockStateTracker
;
44 @
class CKKSReachabilityTracker
;
46 @interface CloudKitMockXCTest
: XCTestCase
48 @property CKRecordZoneID
* testZoneID
;
50 @
property (nullable
) id mockDatabase
;
51 @
property (nullable
) id mockDatabaseExceptionCatcher
;
52 @
property (nullable
) id mockContainer
;
53 @
property (nullable
) id mockContainerExpectations
;
54 @
property (nullable
) id mockFakeCKModifyRecordZonesOperation
;
55 @
property (nullable
) id mockFakeCKModifySubscriptionsOperation
;
56 @
property (nullable
) id mockFakeCKFetchRecordZoneChangesOperation
;
57 @
property (nullable
) id mockFakeCKFetchRecordsOperation
;
58 @
property (nullable
) id mockFakeCKQueryOperation
;
60 @
property (nullable
) id mockAccountStateTracker
;
62 @property CKAccountStatus accountStatus
;
63 @property BOOL supportsDeviceToDeviceEncryption
;
64 @property BOOL iCloudHasValidCredentials
;
65 @property SOSAccountStatus
* circleStatus
;
66 @
property (readonly
) NSString
* ckDeviceID
;
67 @
property (readonly
) CKKSCKAccountStateTracker
* accountStateTracker
;
69 @property NSString
* apsEnvironment
;
71 @property NSString
* circlePeerID
;
73 @property
bool aksLockState
; // The current 'AKS lock state'
74 @
property (readonly
) CKKSLockStateTracker
* lockStateTracker
;
75 @
property (nullable
) id mockLockStateTracker
;
77 @property SCNetworkReachabilityFlags reachabilityFlags
; // The current 'network reachability flags'
78 @
property (readonly
) CKKSReachabilityTracker
*reachabilityTracker
;
79 @
property (nullable
) id mockReachabilityTracker
;
81 @
property (nullable
) NSMutableDictionary
<CKRecordZoneID
*, FakeCKZone
*>* zones
;
83 @
property (nullable
) NSOperationQueue
* operationQueue
;
84 @
property (nullable
) NSBlockOperation
* ckaccountHoldOperation
;
86 @
property (nullable
) NSBlockOperation
* ckModifyHoldOperation
;
87 @
property (nullable
) NSBlockOperation
* ckFetchHoldOperation
;
89 @property
bool silentFetchesAllowed
;
90 @property
bool silentZoneDeletesAllowed
;
92 @
property (nullable
) id mockCKKSViewManager
;
93 @
property (nullable
) CKKSViewManager
* injectedManager
;
95 - (CKKSKey
*)fakeTLK
:(CKRecordZoneID
*)zoneID
;
97 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfRecords
98 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
99 zoneID
:(CKRecordZoneID
*)zoneID
;
100 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfRecords
101 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
102 zoneID
:(CKRecordZoneID
*)zoneID
103 checkItem
:(BOOL (^_Nullable
)(CKRecord
*))checkItem
;
105 - (void)expectCKModifyItemRecords
:(NSUInteger
)expectedNumberOfModifiedRecords
106 deletedRecords
:(NSUInteger
)expectedNumberOfDeletedRecords
107 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
108 zoneID
:(CKRecordZoneID
*)zoneID
109 checkItem
:(BOOL (^_Nullable
)(CKRecord
*))checkItem
;
111 - (void)expectCKDeleteItemRecords
:(NSUInteger
)expectedNumberOfRecords zoneID
:(CKRecordZoneID
*)zoneID
;
113 - (void)expectCKModifyKeyRecords
:(NSUInteger
)expectedNumberOfRecords
114 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
115 tlkShareRecords
:(NSUInteger
)expectedTLKShareRecords
116 zoneID
:(CKRecordZoneID
*)zoneID
;
117 - (void)expectCKModifyKeyRecords
:(NSUInteger
)expectedNumberOfRecords
118 currentKeyPointerRecords
:(NSUInteger
)expectedCurrentKeyRecords
119 tlkShareRecords
:(NSUInteger
)expectedTLKShareRecords
120 zoneID
:(CKRecordZoneID
*)zoneID
121 checkModifiedRecord
:(BOOL (^_Nullable
)(CKRecord
*))checkModifiedRecord
;
123 - (void)expectCKModifyRecords
:(NSDictionary
<NSString
*, NSNumber
*>*)expectedRecordTypeCounts
124 deletedRecordTypeCounts
:(NSDictionary
<NSString
*, NSNumber
*>* _Nullable
)expectedDeletedRecordTypeCounts
125 zoneID
:(CKRecordZoneID
*)zoneID
126 checkModifiedRecord
:(BOOL (^_Nullable
)(CKRecord
*))checkRecord
127 runAfterModification
:(void (^_Nullable
)(void))afterModification
;
129 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
;
130 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
131 blockAfterReject
:(void (^_Nullable
)(void))blockAfterReject
;
132 - (void)failNextCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
133 blockAfterReject
:(void (^_Nullable
)(void))blockAfterReject
134 withError
:(NSError
* _Nullable
)error
;
135 - (void)expectCKAtomicModifyItemRecordsUpdateFailure
:(CKRecordZoneID
*)zoneID
;
137 - (void)failNextZoneCreation
:(CKRecordZoneID
*)zoneID
;
138 - (void)failNextZoneCreationSilently
:(CKRecordZoneID
*)zoneID
;
139 - (void)failNextZoneSubscription
:(CKRecordZoneID
*)zoneID
;
140 - (void)failNextZoneSubscription
:(CKRecordZoneID
*)zoneID withError
:(NSError
*)error
;
142 // Use this to assert that a fetch occurs (especially if silentFetchesAllowed = false)
143 - (void)expectCKFetch
;
145 // 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'.
146 // This way, you can modify the CK zone to cause later collisions.
147 - (void)expectCKFetchAndRunBeforeFinished
:(void (^_Nullable
)(void))blockAfterFetch
;
149 // Use this to assert that a FakeCKFetchRecordsOperation occurs.
150 - (void)expectCKFetchByRecordID
;
152 // Use this to assert that a FakeCKQueryOperation occurs.
153 - (void)expectCKFetchByQuery
;
155 // Wait until all scheduled cloudkit operations are reflected in the currentDatabase
156 - (void)waitForCKModifications
;
158 // Unblocks the CKAccount mock subsystem. Until this is called, the tests believe cloudd hasn't returned any account status yet.
159 - (void)startCKAccountStatusMock
;
161 // Starts everything.
162 - (void)startCKKSSubsystem
;
164 // Blocks the completion (partial or full) of CloudKit modifications
165 - (void)holdCloudKitModifications
;
167 // Unblocks the hold you've added with holdCloudKitModifications; CloudKit modifications will finish
168 - (void)releaseCloudKitModificationHold
;
170 // Blocks the CloudKit fetches from beginning (similar to network latency)
171 - (void)holdCloudKitFetches
;
172 // Unblocks the hold you've added with holdCloudKitFetches; CloudKit fetches will finish
173 - (void)releaseCloudKitFetchHold
;
175 // Make a CK internal server extension error with a given code and description.
176 - (NSError
*)ckInternalServerExtensionError
:(NSInteger
)code description
:(NSString
*)desc
;
178 // Schedule an operation for execution (and failure), with some existing record errors.
179 // Other records in the operation but not in failedRecords will have CKErrorBatchRequestFailed errors created.
180 - (void)rejectWrite
:(CKModifyRecordsOperation
*)op failedRecords
:(NSMutableDictionary
<CKRecordID
*, NSError
*>*)failedRecords
;
184 NS_ASSUME_NONNULL_END