2 * Copyright (c) 2017 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 "CloudKitMockXCTest.h"
27 #import "keychain/ckks/CKKS.h"
28 #import "keychain/ckks/CKKSControl.h"
29 #import "keychain/ckks/CKKSCurrentKeyPointer.h"
30 #import "keychain/ckks/CKKSItem.h"
32 NS_ASSUME_NONNULL_BEGIN
35 @
class CKKSCurrentKeyPointer
;
37 @interface ZoneKeys
: CKKSCurrentKeySet
38 @property CKKSKey
* rolledTLK
;
40 - (instancetype
)initLoadingRecordsFromZone
:(FakeCKZone
*)zone
;
44 * Builds on the CloudKit mock infrastructure and adds keychain helper methods.
47 @interface CloudKitKeychainSyncingMockXCTest
: CloudKitMockXCTest
49 @property CKKSControl
* ckksControl
;
51 @
property (nullable
) id mockCKKSKey
;
53 @
property (nullable
) CKKSSOSSelfPeer
* currentSelfPeer
;
54 @
property (nullable
) NSError
* currentSelfPeerError
;
55 @
property (nullable
) NSMutableSet
<id
<CKKSPeer
>>* currentPeers
;
56 @
property (nullable
) NSError
* currentPeersError
;
58 @
property (nullable
) NSError
* keychainFetchError
;
60 // A single trusted SOSPeer, but without any CKKS keys
61 @property CKKSSOSPeer
* remoteSOSOnlyPeer
;
63 @property NSMutableSet
<CKKSKeychainView
*>* ckksViews
;
64 @property NSMutableSet
<CKRecordZoneID
*>* ckksZones
;
65 @
property (nullable
) NSMutableDictionary
<CKRecordZoneID
*, ZoneKeys
*>* keys
;
67 // Pass in an oldTLK to wrap it to the new TLK; otherwise, pass nil
68 - (ZoneKeys
*)createFakeKeyHierarchy
:(CKRecordZoneID
*)zoneID oldTLK
:(CKKSKey
* _Nullable
)oldTLK
;
69 - (void)saveFakeKeyHierarchyToLocalDatabase
:(CKRecordZoneID
*)zoneID
;
70 - (void)putFakeKeyHierarchyInCloudKit
:(CKRecordZoneID
*)zoneID
;
71 - (void)saveTLKMaterialToKeychain
:(CKRecordZoneID
*)zoneID
;
72 - (void)deleteTLKMaterialFromKeychain
:(CKRecordZoneID
*)zoneID
;
73 - (void)saveTLKMaterialToKeychainSimulatingSOS
:(CKRecordZoneID
*)zoneID
;
74 - (void)putFakeDeviceStatusInCloudKit
:(CKRecordZoneID
*)zoneID
;
75 - (void)putFakeDeviceStatusInCloudKit
:(CKRecordZoneID
*)zoneID
76 zonekeys
:(ZoneKeys
*)zonekeys
;
78 - (void)putFakeOctagonOnlyDeviceStatusInCloudKit
:(CKRecordZoneID
*)zoneID zonekeys
:(ZoneKeys
*)zonekeys
;
79 - (void)putFakeOctagonOnlyDeviceStatusInCloudKit
:(CKRecordZoneID
*)zoneID
;
81 - (void)SOSPiggyBackAddToKeychain
:(NSDictionary
*)piggydata
;
82 - (NSMutableDictionary
*)SOSPiggyBackCopyFromKeychain
;
83 - (NSMutableArray
<NSData
*>*)SOSPiggyICloudIdentities
;
85 - (void)putTLKShareInCloudKit
:(CKKSKey
*)key
86 from
:(CKKSSOSSelfPeer
*)sharingPeer
87 to
:(id
<CKKSPeer
>)receivingPeer
88 zoneID
:(CKRecordZoneID
*)zoneID
;
89 - (void)putTLKSharesInCloudKit
:(CKKSKey
*)key from
:(CKKSSOSSelfPeer
*)sharingPeer zoneID
:(CKRecordZoneID
*)zoneID
;
90 - (void)putSelfTLKSharesInCloudKit
:(CKRecordZoneID
*)zoneID
;
91 - (void)saveTLKSharesInLocalDatabase
:(CKRecordZoneID
*)zoneID
;
93 - (void)saveClassKeyMaterialToKeychain
:(CKRecordZoneID
*)zoneID
;
95 // Call this to fake out your test: all keys are created, saved in cloudkit, and saved locally (as if the key state machine had processed them)
96 - (void)createAndSaveFakeKeyHierarchy
:(CKRecordZoneID
*)zoneID
;
98 - (void)rollFakeKeyHierarchyInCloudKit
:(CKRecordZoneID
*)zoneID
;
100 - (NSDictionary
*)fakeRecordDictionary
:(NSString
* _Nullable
)account zoneID
:(CKRecordZoneID
*)zoneID
;
101 - (CKRecord
*)createFakeRecord
:(CKRecordZoneID
*)zoneID recordName
:(NSString
*)recordName
;
102 - (CKRecord
*)createFakeRecord
:(CKRecordZoneID
*)zoneID recordName
:(NSString
*)recordName withAccount
:(NSString
* _Nullable
)account
;
103 - (CKRecord
*)createFakeRecord
:(CKRecordZoneID
*)zoneID
104 recordName
:(NSString
*)recordName
105 withAccount
:(NSString
* _Nullable
)account
106 key
:(CKKSKey
* _Nullable
)key
;
108 - (CKKSItem
*)newItem
:(CKRecordID
*)recordID withNewItemData
:(NSDictionary
*) dictionary key
:(CKKSKey
*)key
;
109 - (CKRecord
*)newRecord
:(CKRecordID
*)recordID withNewItemData
:(NSDictionary
*)dictionary
;
110 - (CKRecord
*)newRecord
:(CKRecordID
*)recordID withNewItemData
:(NSDictionary
*)dictionary key
:(CKKSKey
*)key
;
111 - (NSDictionary
*)decryptRecord
:(CKRecord
*)record
;
113 // Do keychain things:
114 - (void)addGenericPassword
:(NSString
*)password account
:(NSString
*)account
;
115 - (void)addGenericPassword
:(NSString
*)password account
:(NSString
*)account viewHint
:(NSString
* _Nullable
)viewHint
;
116 - (void)addGenericPassword
:(NSString
*)password
117 account
:(NSString
*)account
118 viewHint
:(NSString
* _Nullable
)viewHint
119 access
:(NSString
*)access
120 expecting
:(OSStatus
)status
121 message
:(NSString
*)message
;
122 - (void)addGenericPassword
:(NSString
*)password account
:(NSString
*)account expecting
:(OSStatus
)status message
:(NSString
*)message
;
124 - (void)updateGenericPassword
:(NSString
*)newPassword account
:(NSString
*)account
;
125 - (void)updateAccountOfGenericPassword
:(NSString
*)newAccount account
:(NSString
*)account
;
127 - (void)checkNoCKKSData
:(CKKSKeychainView
*)view
;
129 - (void)deleteGenericPassword
:(NSString
*)account
;
131 - (void)findGenericPassword
:(NSString
*)account expecting
:(OSStatus
)status
;
132 - (void)checkGenericPassword
:(NSString
*)password account
:(NSString
*)account
;
134 - (void)createClassCItemAndWaitForUpload
:(CKRecordZoneID
*)zoneID account
:(NSString
*)account
;
135 - (void)createClassAItemAndWaitForUpload
:(CKRecordZoneID
*)zoneID account
:(NSString
*)account
;
137 // Pass the blocks created with these to expectCKModifyItemRecords to check if all items were encrypted with a particular class key
138 - (BOOL (^)(CKRecord
*))checkClassABlock
:(CKRecordZoneID
*)zoneID message
:(NSString
*)message
;
139 - (BOOL (^)(CKRecord
*))checkClassCBlock
:(CKRecordZoneID
*)zoneID message
:(NSString
*)message
;
141 - (BOOL (^)(CKRecord
*))checkPasswordBlock
:(CKRecordZoneID
*)zoneID account
:(NSString
*)account password
:(NSString
*)password
;
143 - (void)checkNSyncableTLKsInKeychain
:(size_t)n
;
145 // Returns an expectation that someone will send an NSNotification that this view changed
146 - (XCTestExpectation
*)expectChangeForView
:(NSString
*)view
;
148 // Establish an assertion that CKKS will cause a server extension error soon.
149 - (void)expectCKReceiveSyncKeyHierarchyError
:(CKRecordZoneID
*)zoneID
;
151 // Add expectations that CKKS will upload a single TLK share
152 - (void)expectCKKSTLKSelfShareUpload
:(CKRecordZoneID
*)zoneID
;
155 NS_ASSUME_NONNULL_END