1 #import "keychain/ckks/CKKSListenerCollection.h"
2 #import "keychain/ckks/tests/CKKSMockSOSPresentAdapter.h"
4 @interface CKKSMockSOSPresentAdapter()
5 @property CKKSListenerCollection* peerChangeListeners;
8 @implementation CKKSMockSOSPresentAdapter
9 @synthesize essential = _essential;
10 @synthesize sosEnabled = _sosEnabled;
12 - (instancetype)initWithSelfPeer:(CKKSSOSSelfPeer*)selfPeer
13 trustedPeers:(NSSet<CKKSSOSPeer*>*)trustedPeers
14 essential:(BOOL)essential
16 if((self = [super init])) {
18 _essential = essential;
20 _circleStatus = kSOSCCInCircle;
21 _safariViewEnabled = YES;
23 _excludeSelfPeerFromTrustSet = false;
25 _peerChangeListeners = [[CKKSListenerCollection alloc] initWithName:@"ckks-mock-sos"];
28 _ckks4AllStatusIsSet = NO;
31 _trustedPeers = [trustedPeers mutableCopy];
36 - (NSString*)providerID
38 return [NSString stringWithFormat:@"[CKKSMockSOSPresentAdapter: %@]", self.selfPeer.peerID];
41 - (SOSCCStatus)circleStatus:(NSError * _Nullable __autoreleasing * _Nullable)error
43 if(!self.sosEnabled || self.circleStatus == kSOSCCError) {
44 if(error && self.circleStatus == kSOSCCError) {
45 // I'm not at all sure that the second error here actually is any error in particular
46 *error = self.circleStatusError ?: [NSError errorWithDomain:(__bridge NSString*)kSOSErrorDomain code:self.circleStatus userInfo:nil];
51 return self.circleStatus;
54 // I currently don't understand when SOS returns a self or not. I've seen it return a self while not in kSOSCCInCircle,
55 // which seems wrong. So, always return a self, unless we're in an obvious error state.
56 - (id<CKKSSelfPeer> _Nullable)currentSOSSelf:(NSError * _Nullable __autoreleasing * _Nullable)error
58 if(self.selfPeerError) {
60 *error = self.selfPeerError;
67 *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecInteractionNotAllowed userInfo:nil];
72 if(self.sosEnabled && self.circleStatus == kSOSCCInCircle) {
76 *error = [NSError errorWithDomain:(__bridge NSString*)kSOSErrorDomain code:self.circleStatus userInfo:nil];
82 - (CKKSSelves * _Nullable)fetchSelfPeers:(NSError *__autoreleasing _Nullable * _Nullable)error
84 id<CKKSSelfPeer> peer = [self currentSOSSelf:error];
89 return [[CKKSSelves alloc] initWithCurrent:peer allSelves:nil];
92 - (NSSet<id<CKKSRemotePeerProtocol>> * _Nullable)fetchTrustedPeers:(NSError * _Nullable __autoreleasing * _Nullable)error
94 if(self.trustedPeersError) {
96 *error = self.trustedPeersError;
101 // TODO: I'm actually not entirely sure what SOS does if it's not in circle?
102 if(self.sosEnabled && self.circleStatus == kSOSCCInCircle) {
103 if(self.excludeSelfPeerFromTrustSet) {
104 return self.trustedPeers;
106 return [self allPeers];
110 *error = [NSError errorWithDomain:(__bridge NSString*)kSOSErrorDomain code:kSOSCCNotInCircle userInfo:nil];
116 - (BOOL)updateOctagonKeySetWithAccount:(nonnull id<CKKSSelfPeer>)currentSelfPeer error:(NSError *__autoreleasing _Nullable * _Nullable)error {
117 if(self.updateOctagonKeySetListener) {
118 self.updateOctagonKeySetListener(currentSelfPeer);
123 - (BOOL)updateCKKS4AllStatus:(BOOL)status error:(NSError**)error
125 self.ckks4AllStatus = status;
126 self.ckks4AllStatusIsSet = YES;
130 - (void)registerForPeerChangeUpdates:(nonnull id<CKKSPeerUpdateListener>)listener {
131 [self.peerChangeListeners registerListener:listener];
134 - (void)sendSelfPeerChangedUpdate {
135 [self.peerChangeListeners iterateListeners: ^(id<CKKSPeerUpdateListener> listener) {
136 [listener selfPeerChanged:self];
140 - (void)sendTrustedPeerSetChangedUpdate {
141 [self.peerChangeListeners iterateListeners: ^(id<CKKSPeerUpdateListener> listener) {
142 [listener trustedPeerSetChanged:self];
146 - (nonnull CKKSPeerProviderState *)currentState {
147 return [CKKSPeerProviderState createFromProvider:self];
150 - (NSSet<id<CKKSRemotePeerProtocol>>*)allPeers
152 // include the self peer, but as a CKKSSOSPeer object instead of a self peer
153 CKKSSOSPeer* s = [[CKKSSOSPeer alloc] initWithSOSPeerID:self.selfPeer.peerID
154 encryptionPublicKey:self.selfPeer.publicEncryptionKey
155 signingPublicKey:self.selfPeer.publicSigningKey
156 viewList:self.selfPeer.viewList];
158 return [self.trustedPeers setByAddingObject: s];
161 - (BOOL)safariViewSyncingEnabled:(NSError**)error
163 // TODO: what happens if you call this when not in circle?
164 return self.safariViewEnabled;
167 - (BOOL)preloadOctagonKeySetOnAccount:(nonnull id<CKKSSelfPeer>)currentSelfPeer error:(NSError *__autoreleasing _Nullable * _Nullable)error {