5 // Created by John Hurley on 9/6/12.
9 #include <AssertMacros.h>
16 #include <Security/SecBase.h>
17 #include <Security/SecItem.h>
19 #include <SecureObjectSync/SOSAccount.h>
20 #include <CKBridge/SOSCloudKeychainClient.h>
22 #include <utilities/SecCFWrappers.h>
23 #include <utilities/debugging.h>
25 #include "SOSCircle_regressions.h"
26 #include "SOSRegressionUtilities.h"
29 static bool verboseCKDClientDebugging
= false;
31 static CFStringRef kTestKeyIDTimestamp
= CFSTR("IDTimestamp");
32 static dispatch_group_t sDispatchGroup
= NULL
;
33 static dispatch_queue_t xpc_queue
= NULL
;
35 // MARK: ----- Test Data -----
37 static CFStringRef kTestKeyString
= CFSTR("teststring");
38 static CFStringRef kTestKeyData
= CFSTR("testdata");
39 static const UInt8 tdata
[] = {0x01, 0x02, 0x03, 0x04, 'a', 'b', 'c'};
40 static CFStringRef kTestKeyArray
= CFSTR("testarray");
41 static const CFStringRef adata
[] = {CFSTR("A"), CFSTR("b"), CFSTR("C"), CFSTR("D")};
42 static const CFStringRef circleKeyStrings
[] = {CFSTR("circleA"), CFSTR("circleB"), CFSTR("circleC"), CFSTR("circleD")};
43 static const CFStringRef keysWhenUnlockedKeyStrings
[] = {CFSTR("foo"), CFSTR("bar"), CFSTR("baz")};
45 static CFDataRef testData
= NULL
;
46 static CFArrayRef testArray
= NULL
;
47 static CFArrayRef circleKeys
= NULL
;
48 static CFArrayRef keysWhenUnlocked
= NULL
;
50 static void initializeTestData(void)
52 testData
= CFDataCreate(kCFAllocatorDefault
, tdata
, sizeof(tdata
)/sizeof(UInt8
));
53 testArray
= CFArrayCreate(kCFAllocatorDefault
, (const void **)&adata
, sizeof(adata
)/sizeof(CFStringRef
), &kCFTypeArrayCallBacks
);
55 circleKeys
= CFArrayCreate(kCFAllocatorDefault
, (const void **)&circleKeyStrings
, sizeof(circleKeyStrings
)/sizeof(CFStringRef
), &kCFTypeArrayCallBacks
);
56 keysWhenUnlocked
= CFArrayCreate(kCFAllocatorDefault
, (const void **)&keysWhenUnlockedKeyStrings
, sizeof(keysWhenUnlockedKeyStrings
)/sizeof(CFStringRef
),
57 &kCFTypeArrayCallBacks
);
60 // MARK: ----- utilities -----
62 static void printTimeNow(const char *msg
)
64 if (verboseCKDClientDebugging
)
66 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
67 const char *nowstr
= cfabsoluteTimeToString(now
);
70 printf("%s %s\n", nowstr
, msg
);
76 // MARK: ----- basicKVSTests -----
78 static bool testPostGet(CFStringRef key
, CFTypeRef cfobj
, dispatch_queue_t processQueue
, dispatch_group_t dgroup
)
80 CFErrorRef error
= NULL
;
84 testPutObjectInCloud(key
, cfobj
, &error
, dgroup
, processQueue
);
85 CFTypeRef cfvalue
= testGetObjectFromCloud(key
, processQueue
, dgroup
);
86 printTimeNow("finished getObjectFromCloud");
89 if (CFGetTypeID(cfvalue
)==CFDictionaryGetTypeID())
90 cfv
= CFDictionaryGetValue(cfvalue
, key
);
93 result
= CFEqual(cfobj
, cfv
);
97 static bool postIDTimestamp(dispatch_queue_t theq
, dispatch_group_t dgroup
)
100 CFStringRef macaddr
= myMacAddress();
101 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
102 const char *nowstr
= cfabsoluteTimeToStringLocal(now
);
103 CFStringRef cfidstr
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("%@: %@ %s"), kTestKeyIDTimestamp
, macaddr
,nowstr
);
104 secerror("Setting %@ key: %@", kTestKeyIDTimestamp
, cfidstr
);
105 result
= testPostGet(kTestKeyIDTimestamp
, cfidstr
, theq
, dgroup
);
108 free((void *)nowstr
);
109 CFReleaseSafe(cfidstr
);
114 static const int kbasicKVSTestsCount
= 10;
115 static void basicKVSTests(dispatch_group_t dgroup
)
117 dispatch_queue_t generalq
= dispatch_queue_create("general", DISPATCH_QUEUE_SERIAL
);
119 printTimeNow("Start tests [basicKVSTests]");
120 // dispatch_group_enter(dgroup);
122 // synchronize first to make sure we see cloud values
123 ok(testSynchronize(generalq
, dgroup
), "test synchronize");
125 // Next, get the TimeNow value, since this is the only one that differs from test to test (i.e. sc-90-ckdclient)
126 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
127 const char *nowstr
= cfabsoluteTimeToStringLocal(now
);
128 CFStringRef cfstrtime
= CFStringCreateWithCString(kCFAllocatorDefault
, nowstr
, kCFStringEncodingUTF8
);
129 ok(testGetObjectFromCloud(kTestKeyIDTimestamp
, generalq
, dgroup
) != nil
, "testGet for %@", kTestKeyIDTimestamp
);
131 ok(postIDTimestamp(generalq
, dgroup
), "testPostGet for %@", kTestKeyIDTimestamp
);
133 ok(testPostGet(kTestKeyString
, CFSTR("test string"), generalq
, dgroup
), "testPostGet for CFStringRef");
135 // Now the fixed values
136 ok(testPostGet(kTestKeyString
, CFSTR("test string"), generalq
, dgroup
), "testPostGet for CFStringRef");
137 ok(testPostGet(kTestKeyData
, testData
, generalq
, dgroup
), "testPostGet for CFDataRef");
138 ok(testPostGet(kTestKeyArray
, testArray
, generalq
, dgroup
), "testPostGet for CFDataRef");
139 ok(testRegisterKeys(circleKeys
, generalq
, dgroup
), "test register keys");
141 ok(postIDTimestamp(generalq
, dgroup
), "testPostGet for %@", kTestKeyIDTimestamp
);
143 // Synchronize one more time before exit
144 ok(testSynchronize(generalq
, dgroup
), "test synchronize");
146 printTimeNow("End tests [basicKVSTests]");
148 // dispatch_group_leave(dgroup);
152 CFRelease(testArray
);
153 CFRelease(circleKeys
);
154 CFRelease(keysWhenUnlocked
);
155 CFRelease(cfstrtime
);
157 free((void *)nowstr
);
160 // MARK: ----- start of all tests -----
162 static int kTestTestCount
= kbasicKVSTestsCount
;
163 static void tests(void)
166 skip("Skipping ckdclient tests because CloudKeychainProxy.xpc is not installed", kTestTestCount
, XPCServiceInstalled());
167 // dispatch_queue_t dqueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
168 xpc_queue
= dispatch_queue_create("sc_90_ckdclient", DISPATCH_QUEUE_SERIAL
);
170 sDispatchGroup
= dispatch_group_create();
172 initializeTestData();
173 basicKVSTests(sDispatchGroup
);
177 int sc_90_ckdclient(int argc
, char *const *argv
)
179 plan_tests(kTestTestCount
);