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