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 <Foundation/Foundation.h>
30 #import "keychain/ckks/CKKSNotifier.h"
31 #import "keychain/ckks/CloudKitDependencies.h"
33 NS_ASSUME_NONNULL_BEGIN
35 @
class CKKSCKRecordHolder
;
38 typedef NSMutableDictionary
<CKRecordZoneID
*, FakeCKZone
*> FakeCKDatabase
;
40 @interface FakeCKOperation
: NSBlockOperation
41 @
property (nonatomic
, assign
, readonly
) BOOL isFinishingOnCallbackQueue
;
45 @interface FakeCKModifyRecordZonesOperation
: FakeCKOperation
<CKKSModifyRecordZonesOperation
> {
46 CKOperationConfiguration
* _configuration
;
48 @
property (nullable
) NSError
* creationError
;
49 @
property (nonatomic
, nullable
) NSMutableArray
<CKRecordZone
*>* recordZonesSaved
;
50 @
property (nonatomic
, nullable
) NSMutableArray
<CKRecordZoneID
*>* recordZoneIDsDeleted
;
51 @
property (nonatomic
, copy
, null_resettable
) CKOperationConfiguration
*configuration
;
53 + (FakeCKDatabase
*)ckdb
;
55 + (NSError
* _Nullable
)shouldFailModifyRecordZonesOperation
;
57 + (void)ensureZoneDeletionAllowed
:(FakeCKZone
*)zone
;
60 @interface FakeCKModifySubscriptionsOperation
: FakeCKOperation
<CKKSModifySubscriptionsOperation
> {
61 CKOperationConfiguration
* _configuration
;
63 @
property (nullable
) NSError
* subscriptionError
;
64 @
property (nonatomic
, nullable
) NSMutableArray
<CKSubscription
*>* subscriptionsSaved
;
65 @
property (nonatomic
, nullable
) NSMutableArray
<NSString
*>* subscriptionIDsDeleted
;
66 @
property (nonatomic
, copy
, null_resettable
) CKOperationConfiguration
*configuration
;
67 + (FakeCKDatabase
*)ckdb
;
70 @interface FakeCKFetchRecordZoneChangesOperation
: FakeCKOperation
<CKKSFetchRecordZoneChangesOperation
> {
71 CKOperationConfiguration
* _configuration
;
74 + (FakeCKDatabase
*)ckdb
;
75 @
property (nonatomic
, copy
) NSString
*operationID
;
76 @
property (nonatomic
, readonly
, strong
, nullable
) CKOperationConfiguration
*resolvedConfiguration
;
77 @
property (nullable
) void (^blockAfterFetch
)(void);
80 @interface FakeCKFetchRecordsOperation
: FakeCKOperation
<CKKSFetchRecordsOperation
>
81 + (FakeCKDatabase
*)ckdb
;
84 @interface FakeCKQueryOperation
: FakeCKOperation
<CKKSQueryOperation
>
85 + (FakeCKDatabase
*)ckdb
;
88 @interface FakeAPSConnection
: NSObject
<OctagonAPSConnection
>
91 @interface FakeNSNotificationCenter
: NSObject
<CKKSNSNotificationCenter
>
92 + (instancetype
)defaultCenter
;
93 - (void)addObserver
:(id
)observer selector
:(SEL
)aSelector name
:(nullable NSNotificationName
)aName object
:(nullable id
)anObject
;
96 @interface FakeNSDistributedNotificationCenter
: NSObject
<CKKSNSDistributedNotificationCenter
>
99 @interface FakeCKZone
: NSObject
100 // Used while mocking: database is the contents of the current current CloudKit database, pastDatabase is the state of the world in the past at different change tokens
101 @property CKRecordZoneID
* zoneID
;
102 @property CKServerChangeToken
* currentChangeToken
;
103 @property NSMutableDictionary
<CKRecordID
*, CKRecord
*>* currentDatabase
;
104 @property NSMutableDictionary
<CKServerChangeToken
*, NSMutableDictionary
<CKRecordID
*, CKRecord
*>*>* pastDatabases
;
105 @property
bool flag
; // used however you'd like in a test
106 @
property (nullable
) CKServerChangeToken
* limitFetchTo
; // Only return partial results up until here (if asking for current change token)
107 @
property (nullable
) NSError
* limitFetchError
; // If limitFetchTo fires, finish the operation with this error (likely a network timeout)
108 @property
int fetchRecordZoneChangesOperationCount
;
109 @property NSMutableArray
<NSDate
*>* fetchRecordZoneChangesTimestamps
;
111 // Usually nil. If set, trying to 'create' this zone should fail.
112 @
property (nullable
) NSError
* creationError
;
114 // Usually false. If set, trying to 'create' this should should 1) pretend to succeed but 2) delete this zone from existence
115 @property
bool failCreationSilently
;
117 // Usually nil. If set, trying to subscribe to this zone should fail.
118 @
property (nullable
) NSError
* subscriptionError
;
120 // Serial queue. Use this for transactionality.
121 @property dispatch_queue_t queue
;
123 // Set this to run some code after a write operation has started, but before any results are delivered
124 @
property (nullable
) void (^blockBeforeWriteOperation
)(void);
126 - (instancetype
)initZone
:(CKRecordZoneID
*)zoneID
;
129 - (void)addToZone
:(CKKSCKRecordHolder
*)item zoneID
:(CKRecordZoneID
*)zoneID
;
130 - (void)addToZone
:(CKRecord
*)record
;
132 // If you want a transaction of adding, use these
133 - (CKRecord
*)_onqueueAddToZone
:(CKKSCKRecordHolder
*)item zoneID
:(CKRecordZoneID
*)zoneID
;
134 - (CKRecord
*)_onqueueAddToZone
:(CKRecord
*)record
;
136 // Removes this record from all versions of the CK database, without changing the change tag
137 - (void)deleteFromHistory
:(CKRecordID
*)recordID
;
139 - (void)addCKRecordToZone
:(CKRecord
*)record
;
140 - (NSError
* _Nullable
)deleteCKRecordIDFromZone
:(CKRecordID
*)recordID
;
142 // Sets up the next fetchChanges to fail with this error
143 - (void)failNextFetchWith
:(NSError
*)fetchChangesError
;
145 // Get the next fetchChanges error. Returns NULL if the fetchChanges should succeed.
146 - (NSError
* _Nullable
)popFetchChangesError
;
148 // Checks if this record add/modification should fail
149 - (NSError
* _Nullable
)errorFromSavingRecord
:(CKRecord
*)record
;
151 // Helpfully creates an internal plugin error that CK would return
152 + (NSError
*)internalPluginError
:(NSString
*)serverDomain code
:(NSInteger
)code description
:(NSString
*)desc
;
155 @interface FakeCKKSNotifier
: NSObject
<CKKSNotifier
>
158 NS_ASSUME_NONNULL_END