]> git.saurik.com Git - apple/security.git/blob - sec/SOSCircle/Regressions/sc-90-ckdclient.c
Security-55471.14.8.tar.gz
[apple/security.git] / sec / SOSCircle / Regressions / sc-90-ckdclient.c
1 //
2 // sc-90-ckdclient.c
3 // sec
4 //
5 // Created by John Hurley on 9/6/12.
6 //
7 //
8
9 #include <AssertMacros.h>
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <xpc/xpc.h>
15
16 #include <Security/SecBase.h>
17 #include <Security/SecItem.h>
18
19 #include <SecureObjectSync/SOSAccount.h>
20 #include <CKBridge/SOSCloudKeychainClient.h>
21
22 #include <utilities/SecCFWrappers.h>
23 #include <utilities/debugging.h>
24
25 #include "SOSCircle_regressions.h"
26 #include "SOSRegressionUtilities.h"
27
28
29 static bool verboseCKDClientDebugging = false;
30
31 static CFStringRef kTestKeyIDTimestamp = CFSTR("IDTimestamp");
32 static dispatch_group_t sDispatchGroup = NULL;
33 static dispatch_queue_t xpc_queue = NULL;
34
35 // MARK: ----- Test Data -----
36
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")};
44
45 static CFDataRef testData = NULL;
46 static CFArrayRef testArray = NULL;
47 static CFArrayRef circleKeys = NULL;
48 static CFArrayRef keysWhenUnlocked = NULL;
49
50 static void initializeTestData(void)
51 {
52 testData = CFDataCreate(kCFAllocatorDefault, tdata, sizeof(tdata)/sizeof(UInt8));
53 testArray = CFArrayCreate(kCFAllocatorDefault, (const void **)&adata, sizeof(adata)/sizeof(CFStringRef), &kCFTypeArrayCallBacks);
54 // Register keys
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);
58 }
59
60 // MARK: ----- utilities -----
61
62 static void printTimeNow(const char *msg)
63 {
64 if (verboseCKDClientDebugging)
65 {
66 CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
67 const char *nowstr = cfabsoluteTimeToString(now);
68 if (nowstr)
69 {
70 printf("%s %s\n", nowstr, msg);
71 free((void *)nowstr);
72 }
73 }
74 }
75
76 // MARK: ----- basicKVSTests -----
77
78 static bool testPostGet(CFStringRef key, CFTypeRef cfobj, dispatch_queue_t processQueue, dispatch_group_t dgroup)
79 {
80 CFErrorRef error = NULL;
81 bool result = false;
82 CFTypeRef cfv = NULL;
83
84 testPutObjectInCloud(key, cfobj, &error, dgroup, processQueue);
85 CFTypeRef cfvalue = testGetObjectFromCloud(key, processQueue, dgroup);
86 printTimeNow("finished getObjectFromCloud");
87 if (!cfvalue)
88 return false;
89 if (CFGetTypeID(cfvalue)==CFDictionaryGetTypeID())
90 cfv = CFDictionaryGetValue(cfvalue, key);
91 else
92 cfv = cfvalue;
93 result = CFEqual(cfobj, cfv);
94 return result;
95 }
96
97 static bool postIDTimestamp(dispatch_queue_t theq, dispatch_group_t dgroup)
98 {
99 bool result = false;
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);
106
107 if (nowstr)
108 free((void *)nowstr);
109 CFReleaseSafe(cfidstr);
110 return result;
111 }
112
113
114 static const int kbasicKVSTestsCount = 10;
115 static void basicKVSTests(dispatch_group_t dgroup)
116 {
117 dispatch_queue_t generalq = dispatch_queue_create("general", DISPATCH_QUEUE_SERIAL);
118
119 printTimeNow("Start tests [basicKVSTests]");
120 // dispatch_group_enter(dgroup);
121
122 // synchronize first to make sure we see cloud values
123 ok(testSynchronize(generalq, dgroup), "test synchronize");
124
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);
130
131 ok(postIDTimestamp(generalq, dgroup), "testPostGet for %@", kTestKeyIDTimestamp);
132
133 ok(testPostGet(kTestKeyString, CFSTR("test string"), generalq, dgroup), "testPostGet for CFStringRef");
134
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");
140
141 ok(postIDTimestamp(generalq, dgroup), "testPostGet for %@", kTestKeyIDTimestamp);
142
143 // Synchronize one more time before exit
144 ok(testSynchronize(generalq, dgroup), "test synchronize");
145 /**/
146 printTimeNow("End tests [basicKVSTests]");
147
148 // dispatch_group_leave(dgroup);
149
150 // Release test data
151 CFRelease(testData);
152 CFRelease(testArray);
153 CFRelease(circleKeys);
154 CFRelease(keysWhenUnlocked);
155 CFRelease(cfstrtime);
156 if (nowstr)
157 free((void *)nowstr);
158 }
159
160 // MARK: ----- start of all tests -----
161
162 static int kTestTestCount = kbasicKVSTestsCount;
163 static void tests(void)
164 {
165 SKIP: {
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);
169
170 sDispatchGroup = dispatch_group_create();
171
172 initializeTestData();
173 basicKVSTests(sDispatchGroup);
174 }
175 }
176
177 int sc_90_ckdclient(int argc, char *const *argv)
178 {
179 plan_tests(kTestTestCount);
180
181 tests();
182
183 return 0;
184 }
185
186