]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/tests/CKKSMockOctagonAdapter.m
Security-59306.61.1.tar.gz
[apple/security.git] / keychain / ckks / tests / CKKSMockOctagonAdapter.m
1 /*
2 * Copyright (c) 2017 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 <SecurityFoundation/SFKey.h>
28 #import <SecurityFoundation/SFKey_Private.h>
29 #import <SecurityFoundation/SFDigestOperation.h>
30
31 #import "keychain/ckks/CKKS.h"
32 #import "keychain/ckks/CKKSKey.h"
33 #import "keychain/ckks/CKKSViewManager.h"
34 #import "keychain/ckks/CloudKitCategories.h"
35 #import "keychain/categories/NSError+UsefulConstructors.h"
36
37 #import "keychain/ot/OctagonCKKSPeerAdapter.h"
38
39 #import "keychain/ckks/CKKSListenerCollection.h"
40 #import "keychain/ckks/tests/CKKSMockOctagonAdapter.h"
41
42 @implementation CKKSMockOctagonPeer
43
44 @synthesize publicSigningKey = _publicSigningKey;
45 @synthesize publicEncryptionKey = _publicEncryptionKey;
46
47
48 - (instancetype)initWithOctagonPeerID:(NSString*)syncingPeerID
49 publicEncryptionKey:(SFECPublicKey* _Nullable)publicEncryptionKey
50 publicSigningKey:(SFECPublicKey* _Nullable)publicSigningKey
51 viewList:(NSSet<NSString*>* _Nullable)viewList
52 {
53 if((self = [super init])) {
54 _peerID = syncingPeerID;
55 _publicEncryptionKey = publicEncryptionKey;
56 _publicSigningKey = publicSigningKey;
57 _viewList = viewList;
58 }
59 return self;
60 }
61
62 - (bool)matchesPeer:(nonnull id<CKKSPeer>)peer {
63 NSString* otherPeerID = peer.peerID;
64
65 if(self.peerID == nil && otherPeerID == nil) {
66 return true;
67 }
68
69 return [self.peerID isEqualToString:otherPeerID];
70 }
71
72 - (BOOL)shouldHaveView:(nonnull NSString *)viewName {
73 return [self.viewList containsObject: viewName];
74 }
75
76 @end
77
78
79 @implementation CKKSMockOctagonAdapter
80 @synthesize essential = _essential;
81
82 - (instancetype)initWithSelfPeer:(OctagonSelfPeer*)selfPeer
83 trustedPeers:(NSSet<id<CKKSRemotePeerProtocol>>*)trustedPeers
84 essential:(BOOL)essential
85 {
86 if((self = [super init])) {
87 _essential = essential;
88
89 _peerChangeListeners = [[CKKSListenerCollection alloc] initWithName:@"ckks-mock-sos"];
90
91 _selfOTPeer = selfPeer;
92 _trustedPeers = [trustedPeers mutableCopy];
93 }
94 return self;
95 }
96
97 - (NSString*)providerID
98 {
99 return [NSString stringWithFormat:@"[CKKSMockOctagonAdapter: %@]", self.selfOTPeer.peerID];
100 }
101
102 - (CKKSSelves * _Nullable)fetchSelfPeers:(NSError * _Nullable __autoreleasing * _Nullable)error {
103 return [[CKKSSelves alloc] initWithCurrent:self.selfOTPeer allSelves:nil];
104 }
105
106 - (NSSet<id<CKKSRemotePeerProtocol>> * _Nullable)fetchTrustedPeers:(NSError * _Nullable __autoreleasing * _Nullable)error {
107 // include the self peer
108 CKKSMockOctagonPeer *s = [[CKKSMockOctagonPeer alloc] initWithOctagonPeerID:self.selfOTPeer.peerID
109 publicEncryptionKey:self.selfOTPeer.publicEncryptionKey
110 publicSigningKey:self.selfOTPeer.publicSigningKey
111 viewList:self.selfViewList];
112 return [self.trustedPeers setByAddingObject: s];
113 }
114
115 - (void)registerForPeerChangeUpdates:(nonnull id<CKKSPeerUpdateListener>)listener {
116 [self.peerChangeListeners registerListener:listener];
117 }
118
119 - (void)sendSelfPeerChangedUpdate {
120 [self.peerChangeListeners iterateListeners: ^(id<CKKSPeerUpdateListener> listener) {
121 [listener selfPeerChanged:self];
122 }];
123 }
124
125 - (void)sendTrustedPeerSetChangedUpdate {
126 [self.peerChangeListeners iterateListeners: ^(id<CKKSPeerUpdateListener> listener) {
127 [listener trustedPeerSetChanged:self];
128 }];
129 }
130
131 - (nonnull CKKSPeerProviderState *)currentState {
132 return [CKKSPeerProviderState createFromProvider:self];
133 }
134
135
136 @end
137
138 #endif