]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Regressions/CKDSimulatedAccount.m
Security-58286.270.3.0.1.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / Regressions / CKDSimulatedAccount.m
1 //
2 // CKDSimulatedAccount+CKDSimulatedAccount.m
3 // Security
4 //
5
6 #import "CKDSimulatedAccount.h"
7
8 #import <Foundation/Foundation.h>
9
10 @interface CKDSimulatedAccount ()
11 @property (readwrite) NSMutableDictionary<NSString*, NSObject*>* keyChanges;
12 @property (readwrite) NSMutableSet<NSString*>* peerChanges;
13 @property (readwrite) NSMutableSet<NSString*>* backupPeerChanges;
14 @property (readwrite) BOOL peerRegistrationEnsured;
15 @end
16
17 @implementation CKDSimulatedAccount
18
19 + (instancetype) account {
20 return [[CKDSimulatedAccount alloc] init];
21 }
22 - (instancetype) init {
23 self = [super init];
24 if (self) {
25 self.keysToNotHandle = [NSMutableSet<NSString*> set];
26 self.keyChanges = [NSMutableDictionary<NSString*, NSObject*> dictionary];
27
28 self.peerChanges = [NSMutableSet<NSString*> set];
29 self.peersToNotSyncWith = [NSMutableSet<NSString*> set];
30
31 self.backupPeerChanges = [NSMutableSet<NSString*> set];
32 self.backupPeersToNotSyncWith = [NSMutableSet<NSString*> set];
33
34 self.peerRegistrationEnsured = NO;
35 }
36 return self;
37 }
38
39 - (NSSet*) keysChanged: (NSDictionary<NSString*, NSObject*>*) keyValues
40 error: (NSError**) error {
41
42 NSMutableSet<NSString*>* result = [NSMutableSet<NSString*> set];
43
44 [keyValues enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSObject * _Nonnull obj, BOOL * _Nonnull stop) {
45 if (![self.keysToNotHandle containsObject:key]) {
46 [self.keyChanges setObject:obj forKey:key];
47 [result addObject:key];
48 }
49 }];
50
51 return result;
52 }
53
54 - (bool) ensurePeerRegistration: (NSError**) error {
55 if (self.peerRegistrationFailureReason == nil) {
56 self.peerRegistrationEnsured = YES;
57 return true;
58 } else {
59 if (error) {
60 *error = self.peerRegistrationFailureReason;
61 }
62 return false;
63 }
64 }
65
66 - (NSSet<NSString*>*) syncWithPeers: (NSSet<NSString*>*) peerIDs
67 backups: (NSSet<NSString*>*) backupPeerIDs
68 error: (NSError**) error {
69 NSMutableSet<NSString*>* peerIDsToTake = [peerIDs mutableCopy];
70 [peerIDsToTake minusSet:self.peersToNotSyncWith];
71 [self.peerChanges unionSet: peerIDsToTake];
72
73 NSMutableSet<NSString*>* backupPeerIDsToTake = [NSMutableSet<NSString*> setWithSet: backupPeerIDs];
74 [backupPeerIDsToTake minusSet:self.backupPeersToNotSyncWith];
75 [self.backupPeerChanges unionSet: backupPeerIDsToTake];
76
77 // Calculate what we took.
78 [peerIDsToTake unionSet:backupPeerIDsToTake];
79 return peerIDsToTake;
80 }
81
82 - (bool) syncWithAllPeers: (NSError**) error {
83 return true;
84 }
85
86 - (NSDictionary<NSString*, NSObject*>*) extractKeyChanges {
87 NSDictionary<NSString*, NSObject*>* result = self.keyChanges;
88 self.keyChanges = [NSMutableDictionary<NSString*, NSObject*> dictionary];
89 return result;
90 }
91
92 - (NSSet<NSString*>*) extractPeerChanges {
93 NSSet<NSString*>* result = self.peerChanges;
94 self.peerChanges = [NSMutableSet<NSString*> set];
95 return result;
96 }
97 - (NSSet<NSString*>*) extractBackupPeerChanges {
98 NSSet<NSString*>* result = self.backupPeerChanges;
99 self.backupPeerChanges = [NSMutableSet<NSString*> set];
100 return result;
101 }
102
103 - (BOOL) extractRegistrationEnsured {
104 BOOL result = self.peerRegistrationEnsured;
105 self.peerRegistrationEnsured = NO;
106 return result;
107 }
108
109 @end