]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/tests/MockCloudKit.h
Security-59306.101.1.tar.gz
[apple/security.git] / keychain / ckks / tests / MockCloudKit.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 <Foundation/Foundation.h>
29
30 #import "keychain/ckks/CKKSNotifier.h"
31 #import "keychain/ckks/CloudKitDependencies.h"
32
33 NS_ASSUME_NONNULL_BEGIN
34
35 @class CKKSCKRecordHolder;
36 @class FakeCKZone;
37
38 typedef NSMutableDictionary<CKRecordZoneID*, FakeCKZone*> FakeCKDatabase;
39
40 @interface FakeCKOperation : NSBlockOperation
41 @property (nonatomic, assign, readonly) BOOL isFinishingOnCallbackQueue;
42 @end
43
44
45 @interface FakeCKModifyRecordZonesOperation : FakeCKOperation <CKKSModifyRecordZonesOperation> {
46 CKOperationConfiguration* _configuration;
47 }
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;
52
53 + (FakeCKDatabase*)ckdb;
54
55 + (NSError* _Nullable)shouldFailModifyRecordZonesOperation;
56
57 + (void)ensureZoneDeletionAllowed:(FakeCKZone*)zone;
58 @end
59
60 @interface FakeCKModifySubscriptionsOperation : FakeCKOperation <CKKSModifySubscriptionsOperation> {
61 CKOperationConfiguration* _configuration;
62 }
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;
68 @end
69
70 @interface FakeCKFetchRecordZoneChangesOperation : FakeCKOperation <CKKSFetchRecordZoneChangesOperation> {
71 CKOperationConfiguration* _configuration;
72 }
73
74 + (FakeCKDatabase*)ckdb;
75 @property (nonatomic, copy) NSString *operationID;
76 @property (nonatomic, readonly, strong, nullable) CKOperationConfiguration *resolvedConfiguration;
77 @property (nullable) void (^blockAfterFetch)(void);
78 @end
79
80 @interface FakeCKFetchRecordsOperation : FakeCKOperation <CKKSFetchRecordsOperation>
81 + (FakeCKDatabase*)ckdb;
82 @end
83
84 @interface FakeCKQueryOperation : FakeCKOperation <CKKSQueryOperation>
85 + (FakeCKDatabase*)ckdb;
86 @end
87
88 @interface FakeAPSConnection : NSObject <OctagonAPSConnection>
89 @end
90
91 @interface FakeNSNotificationCenter : NSObject <CKKSNSNotificationCenter>
92 + (instancetype)defaultCenter;
93 - (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject;
94 @end
95
96 @interface FakeNSDistributedNotificationCenter : NSObject <CKKSNSDistributedNotificationCenter>
97 @end
98
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;
110
111 // Usually nil. If set, trying to 'create' this zone should fail.
112 @property (nullable) NSError* creationError;
113
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;
116
117 // Usually nil. If set, trying to subscribe to this zone should fail.
118 @property (nullable) NSError* subscriptionError;
119
120 // Serial queue. Use this for transactionality.
121 @property dispatch_queue_t queue;
122
123 - (instancetype)initZone:(CKRecordZoneID*)zoneID;
124
125 // Always Succeed
126 - (void)addToZone:(CKKSCKRecordHolder*)item zoneID:(CKRecordZoneID*)zoneID;
127 - (void)addToZone:(CKRecord*)record;
128
129 // If you want a transaction of adding, use these
130 - (CKRecord*)_onqueueAddToZone:(CKKSCKRecordHolder*)item zoneID:(CKRecordZoneID*)zoneID;
131 - (CKRecord*)_onqueueAddToZone:(CKRecord*)record;
132
133 // Removes this record from all versions of the CK database, without changing the change tag
134 - (void)deleteFromHistory:(CKRecordID*)recordID;
135
136 - (void)addCKRecordToZone:(CKRecord*)record;
137 - (NSError* _Nullable)deleteCKRecordIDFromZone:(CKRecordID*)recordID;
138
139 // Sets up the next fetchChanges to fail with this error
140 - (void)failNextFetchWith:(NSError*)fetchChangesError;
141
142 // Get the next fetchChanges error. Returns NULL if the fetchChanges should succeed.
143 - (NSError* _Nullable)popFetchChangesError;
144
145 // Checks if this record add/modification should fail
146 - (NSError* _Nullable)errorFromSavingRecord:(CKRecord*)record;
147
148 // Helpfully creates an internal plugin error that CK would return
149 + (NSError*)internalPluginError:(NSString*)serverDomain code:(NSInteger)code description:(NSString*)desc;
150 @end
151
152 @interface FakeCKKSNotifier : NSObject <CKKSNotifier>
153 @end
154
155 NS_ASSUME_NONNULL_END
156
157 #endif /* OCTAGON */