2 // secd-210-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 {
65 // Take a strong reference:
66 __strong __typeof(self.listener) listener = self.listener;
77 - (void) connectTo: (NSObject<CKDLockListener>*) listener {
78 self.listener = listener;
79 [self notifyListener];
84 [self notifyListener];
88 self.unlockedSinceBoot = true;
89 [self notifyListener];
95 @interface UbiqitousKVSProxy (Testing)
99 @implementation UbiqitousKVSProxy (Testing)
101 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
103 [self doAfterFlush:^{
104 dispatch_semaphore_signal(sema);
107 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
111 static void tests(void) {
112 CKDSimulatedStore* store = [CKDSimulatedStore simulatedInterface];
113 CKDSimulatedAccount* account = [[CKDSimulatedAccount alloc] init];
114 CKDSimulatedLockMonitor* monitor = [CKDSimulatedLockMonitor monitor];
116 NSString * testKey = @"TestKey";
118 UbiqitousKVSProxy * proxy = [UbiqitousKVSProxy withAccount:account
121 persistence:[NSURL fileURLWithPath:@"/tmp/kvsPersistenceTestFile"]];
123 NSDictionary* interests = @{ [NSString stringWithUTF8String:kMessageKeyParameter]:@{ @"UnlockedKeys":@[ testKey ] } };
124 NSString* accountID = @"Account1";
126 dispatch_sync([proxy ckdkvsproxy_queue], ^{
127 [proxy registerKeys:interests forAccount:accountID];
130 is([[account extractKeyChanges] count], (NSUInteger)0, "No changes yet");
132 [store remoteSetObject:@1 forKey:testKey];
135 is([[account extractKeyChanges] count], (NSUInteger)0, "Still none while locked");
140 is([[account extractKeyChanges] count], (NSUInteger)1, "Notified after unlock");
146 is([[account extractKeyChanges] count], (NSUInteger)0, "lock unlock and nothing changes");
148 [store remoteSetObject:@2 forKey:testKey];
152 NSDictionary<NSString*, NSObject*> *changes = [account extractKeyChanges];
153 is([changes count], (NSUInteger)1, "lock, nothing changes");
154 is(changes[testKey], @2, "Sent second value");
158 [store remoteSetObject:@3 forKey:testKey];
161 is([[account extractKeyChanges] count], (NSUInteger)0, "Changes to Unlocked not when locked");
167 NSDictionary<NSString*, NSObject*> *changes = [account extractKeyChanges];
168 is([changes count], (NSUInteger)1, "Change defered to after unlock");
169 is(changes[testKey], @3, "Correct value");
172 dispatch_sync([proxy ckdkvsproxy_queue], ^{
173 [proxy registerKeys:interests forAccount:accountID];
177 is([[account extractKeyChanges] count], (NSUInteger)0, "Same interests, no new data");
179 dispatch_sync([proxy ckdkvsproxy_queue], ^{
180 [proxy registerKeys:interests forAccount:@"different"];
185 NSDictionary<NSString*, NSObject*> *changes = [account extractKeyChanges];
186 is([changes count], (NSUInteger)1, "New account, same interests, new data");
187 is(changes[testKey], @3, "Latest value for new data");
193 int secd_210_keyinterest(int argc, char *const *argv)