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;
22 _excludeSelfPeerFromTrustSet = false;
24 _peerChangeListeners = [[CKKSListenerCollection alloc] initWithName:@"ckks-mock-sos"];
27 _trustedPeers = [trustedPeers mutableCopy];
32 - (NSString*)providerID
34 return [NSString stringWithFormat:@"[CKKSMockSOSPresentAdapter: %@]", self.selfPeer.peerID];
37 - (SOSCCStatus)circleStatus:(NSError * _Nullable __autoreleasing * _Nullable)error
39 if(!self.sosEnabled || self.circleStatus == kSOSCCError) {
40 if(error && self.circleStatus == kSOSCCError) {
41 // I'm not at all sure that the second error here actually is any error in particular
42 *error = self.circleStatusError ?: [NSError errorWithDomain:(__bridge NSString*)kSOSErrorDomain code:self.circleStatus userInfo:nil];
47 return self.circleStatus;
50 // I currently don't understand when SOS returns a self or not. I've seen it return a self while not in kSOSCCInCircle,
51 // which seems wrong. So, always return a self, unless we're in an obvious error state.
52 - (id<CKKSSelfPeer> _Nullable)currentSOSSelf:(NSError * _Nullable __autoreleasing * _Nullable)error
54 if(self.selfPeerError) {
56 *error = self.selfPeerError;
61 if(self.sosEnabled && self.circleStatus == kSOSCCInCircle) {
65 *error = [NSError errorWithDomain:(__bridge NSString*)kSOSErrorDomain code:self.circleStatus userInfo:nil];
71 - (CKKSSelves * _Nullable)fetchSelfPeers:(NSError *__autoreleasing _Nullable * _Nullable)error
73 id<CKKSSelfPeer> peer = [self currentSOSSelf:error];
78 return [[CKKSSelves alloc] initWithCurrent:peer allSelves:nil];
81 - (NSSet<id<CKKSRemotePeerProtocol>> * _Nullable)fetchTrustedPeers:(NSError * _Nullable __autoreleasing * _Nullable)error
83 if(self.trustedPeersError) {
85 *error = self.trustedPeersError;
90 // TODO: I'm actually not entirely sure what SOS does if it's not in circle?
91 if(self.sosEnabled && self.circleStatus == kSOSCCInCircle) {
92 if(self.excludeSelfPeerFromTrustSet) {
93 return self.trustedPeers;
95 return [self allPeers];
99 *error = [NSError errorWithDomain:(__bridge NSString*)kSOSErrorDomain code:kSOSCCNotInCircle userInfo:nil];
105 - (void)updateOctagonKeySetWithAccount:(nonnull id<CKKSSelfPeer>)currentSelfPeer error:(NSError *__autoreleasing _Nullable * _Nullable)error {
106 if(self.updateOctagonKeySetListener) {
107 self.updateOctagonKeySetListener(currentSelfPeer);
112 - (void)registerForPeerChangeUpdates:(nonnull id<CKKSPeerUpdateListener>)listener {
113 [self.peerChangeListeners registerListener:listener];
116 - (void)sendSelfPeerChangedUpdate {
117 [self.peerChangeListeners iterateListeners: ^(id<CKKSPeerUpdateListener> listener) {
118 [listener selfPeerChanged:self];
122 - (void)sendTrustedPeerSetChangedUpdate {
123 [self.peerChangeListeners iterateListeners: ^(id<CKKSPeerUpdateListener> listener) {
124 [listener trustedPeerSetChanged:self];
128 - (nonnull CKKSPeerProviderState *)currentState {
129 return [CKKSPeerProviderState createFromProvider:self];
132 - (NSSet<id<CKKSRemotePeerProtocol>>*)allPeers
134 // include the self peer, but as a CKKSSOSPeer object instead of a self peer
135 CKKSSOSPeer* s = [[CKKSSOSPeer alloc] initWithSOSPeerID:self.selfPeer.peerID
136 encryptionPublicKey:self.selfPeer.publicEncryptionKey
137 signingPublicKey:self.selfPeer.publicSigningKey
138 viewList:self.selfPeer.viewList];
140 return [self.trustedPeers setByAddingObject: s];