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"
18 #include "SOSCloudKeychainConstants.h"
19 #include "SOSAccountTesting.h"
24 @interface CKDSimulatedLockMonitor : NSObject<CKDLockMonitor>
26 @property (readwrite) BOOL unlockedSinceBoot;
27 @property (readwrite) BOOL locked;
29 @property (weak) NSObject<CKDLockListener>* listener;
31 + (instancetype) monitor;
33 - (instancetype) init;
37 - (void) notifyListener;
38 - (void) connectTo: (NSObject<CKDLockListener>*) listener;
46 @implementation CKDSimulatedLockMonitor
48 + (instancetype) monitor {
49 return [[CKDSimulatedLockMonitor alloc] init];
52 - (instancetype) init {
53 if ((self = [super init])) {
55 _unlockedSinceBoot = false;
58 [self notifyListener];
66 - (void) notifyListener {
67 // Take a strong reference:
68 __strong __typeof(self.listener) listener = self.listener;
79 - (void) connectTo: (NSObject<CKDLockListener>*) listener {
80 self.listener = listener;
81 [self notifyListener];
86 [self notifyListener];
90 self.unlockedSinceBoot = true;
91 [self notifyListener];
97 @interface UbiqitousKVSProxy (Testing)
101 @implementation UbiqitousKVSProxy (Testing)
103 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
105 [self doAfterFlush:^{
106 dispatch_semaphore_signal(sema);
109 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
113 static void tests(void) {
114 CKDSimulatedStore* store = [CKDSimulatedStore simulatedInterface];
115 CKDSimulatedAccount* account = [[CKDSimulatedAccount alloc] init];
116 CKDSimulatedLockMonitor* monitor = [CKDSimulatedLockMonitor monitor];
118 NSString * testKey = @"TestKey";
120 UbiqitousKVSProxy * proxy = [UbiqitousKVSProxy withAccount:account
123 persistence:[NSURL fileURLWithPath:@"/tmp/kvsPersistenceTestFile"]];
125 NSDictionary* interests = @{ [NSString stringWithUTF8String:kMessageKeyParameter]:@{ @"UnlockedKeys":@[ testKey ] } };
126 NSString* accountID = @"Account1";
128 dispatch_sync([proxy ckdkvsproxy_queue], ^{
129 [proxy registerKeys:interests forAccount:accountID];
132 is([[account extractKeyChanges] count], (NSUInteger)0, "No changes yet");
134 [store remoteSetObject:@1 forKey:testKey];
137 is([[account extractKeyChanges] count], (NSUInteger)0, "Still none while locked");
142 is([[account extractKeyChanges] count], (NSUInteger)1, "Notified after unlock");
148 is([[account extractKeyChanges] count], (NSUInteger)0, "lock unlock and nothing changes");
150 [store remoteSetObject:@2 forKey:testKey];
154 NSDictionary<NSString*, NSObject*> *changes = [account extractKeyChanges];
155 is([changes count], (NSUInteger)1, "lock, nothing changes");
156 is(changes[testKey], @2, "Sent second value");
160 [store remoteSetObject:@3 forKey:testKey];
163 is([[account extractKeyChanges] count], (NSUInteger)0, "Changes to Unlocked not when locked");
169 NSDictionary<NSString*, NSObject*> *changes = [account extractKeyChanges];
170 is([changes count], (NSUInteger)1, "Change defered to after unlock");
171 is(changes[testKey], @3, "Correct value");
174 dispatch_sync([proxy ckdkvsproxy_queue], ^{
175 [proxy registerKeys:interests forAccount:accountID];
179 is([[account extractKeyChanges] count], (NSUInteger)0, "Same interests, no new data");
181 dispatch_sync([proxy ckdkvsproxy_queue], ^{
182 [proxy registerKeys:interests forAccount:@"different"];
187 NSDictionary<NSString*, NSObject*> *changes = [account extractKeyChanges];
188 is([changes count], (NSUInteger)1, "New account, same interests, new data");
189 is(changes[testKey], @3, "Latest value for new data");
196 int secd_210_keyinterest(int argc, char *const *argv)