2 // sc-160-keyinterest.m
5 // Created by Mitch Adler on 10/31/16.
9 #import <Foundation/Foundation.h>
11 #include "secd_regressions.h"
14 #import "CKDKVSProxy.h"
15 #import "CKDSimulatedStore.h"
16 #import "CKDSimulatedAccount.h"
17 #import "CKDAKSLockMonitor.h"
19 #include "SOSCloudKeychainConstants.h"
21 @interface CKDSimulatedLockMonitor : NSObject<CKDLockMonitor>
23 @property (readwrite) BOOL unlockedSinceBoot;
24 @property (readwrite) BOOL locked;
26 @property (weak) NSObject<CKDLockListener>* listener;
28 + (instancetype) monitor;
30 - (instancetype) init;
34 - (void) notifyListener;
35 - (void) connectTo: (NSObject<CKDLockListener>*) listener;
43 @implementation CKDSimulatedLockMonitor
45 + (instancetype) monitor {
46 return [[CKDSimulatedLockMonitor alloc] init];
49 - (instancetype) init {
53 _unlockedSinceBoot = false;
56 [self notifyListener];
64 - (void) notifyListener {
67 [self.listener locked];
69 [self.listener unlocked];
74 - (void) connectTo: (NSObject<CKDLockListener>*) listener {
75 self.listener = listener;
76 [self notifyListener];
81 [self notifyListener];
85 self.unlockedSinceBoot = true;
86 [self notifyListener];
92 @interface UbiqitousKVSProxy (Testing)
96 @implementation UbiqitousKVSProxy (Testing)
98 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
100 [self doAfterFlush:^{
101 dispatch_semaphore_signal(sema);
104 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
108 static void tests(void) {
109 CKDSimulatedStore* store = [CKDSimulatedStore simulatedInterface];
110 CKDSimulatedAccount* account = [[CKDSimulatedAccount alloc] init];
111 CKDSimulatedLockMonitor* monitor = [CKDSimulatedLockMonitor monitor];
113 NSString * testKey = @"TestKey";
115 UbiqitousKVSProxy * proxy = [UbiqitousKVSProxy withAccount:account
118 persistence:[NSURL fileURLWithPath:@"/tmp/kvsPersistenceTestFile"]];
120 NSDictionary* interests = @{ [NSString stringWithUTF8String:kMessageKeyParameter]:@{ @"UnlockedKeys":@[ testKey ] } };
121 NSString* accountID = @"Account1";
123 dispatch_sync([proxy ckdkvsproxy_queue], ^{
124 [proxy registerKeys:interests forAccount:accountID];
127 is([[account extractKeyChanges] count], (NSUInteger)0, "No changes yet");
129 [store remoteSetObject:@1 forKey:testKey];
132 is([[account extractKeyChanges] count], (NSUInteger)0, "Still none while locked");
137 is([[account extractKeyChanges] count], (NSUInteger)1, "Notified after unlock");
143 is([[account extractKeyChanges] count], (NSUInteger)0, "lock unlock and nothing changes");
145 [store remoteSetObject:@2 forKey:testKey];
149 NSDictionary<NSString*, NSObject*> *changes = [account extractKeyChanges];
150 is([changes count], (NSUInteger)1, "lock, nothing changes");
151 is(changes[testKey], @2, "Sent second value");
155 [store remoteSetObject:@3 forKey:testKey];
158 is([[account extractKeyChanges] count], (NSUInteger)0, "Changes to Unlocked not when locked");
164 NSDictionary<NSString*, NSObject*> *changes = [account extractKeyChanges];
165 is([changes count], (NSUInteger)1, "Change defered to after unlock");
166 is(changes[testKey], @3, "Correct value");
169 dispatch_sync([proxy ckdkvsproxy_queue], ^{
170 [proxy registerKeys:interests forAccount:accountID];
174 is([[account extractKeyChanges] count], (NSUInteger)0, "Same interests, no new data");
176 dispatch_sync([proxy ckdkvsproxy_queue], ^{
177 [proxy registerKeys:interests forAccount:@"different"];
182 NSDictionary<NSString*, NSObject*> *changes = [account extractKeyChanges];
183 is([changes count], (NSUInteger)1, "New account, same interests, new data");
184 is(changes[testKey], @3, "Latest value for new data");
190 int secd_210_keyinterest(int argc, char *const *argv)