]> git.saurik.com Git - apple/security.git/blob - Security/sec/SOSCircle/Regressions/sc-95-ckd2client.c
Security-57031.1.35.tar.gz
[apple/security.git] / Security / sec / SOSCircle / Regressions / sc-95-ckd2client.c
1 /*
2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #include <AssertMacros.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <xpc/xpc.h>
31
32 #include <Security/SecBase.h>
33 #include <Security/SecItem.h>
34
35 #include <SecureObjectSync/SOSAccount.h>
36 #include <CKBridge/SOSCloudKeychainClient.h>
37 #include <utilities/SecCFWrappers.h>
38 #include <utilities/debugging.h>
39
40 #include "SOSCircle_regressions.h"
41 #include "SOSRegressionUtilities.h"
42
43 static bool verboseCKDClientDebugging = false;
44
45 static CFStringRef kTestKeyIDTimestamp = CFSTR("IDTimestamp");
46
47 static void printTimeNow(const char *msg)
48 {
49 if (verboseCKDClientDebugging)
50 {
51 CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
52 const char *nowstr = cfabsoluteTimeToString(now);
53 if (nowstr)
54 {
55 printf("%s %s\n", nowstr, msg);
56 free((void *)nowstr);
57 }
58 }
59 }
60
61 // MARK: ----- basicKVSTests -----
62
63 static bool postIDTimestamp(dispatch_queue_t theq)
64 {
65 __block bool result = false;
66 CFStringRef macaddr = myMacAddress();
67 CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
68 const char *nowstr = cfabsoluteTimeToStringLocal(now);
69 CFStringRef cfidstr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@: %@ %s"), kTestKeyIDTimestamp, macaddr ,nowstr);
70 secerror("Setting %@ key: %@", kTestKeyIDTimestamp, cfidstr);
71
72 // create a dictionary with one key/value pair
73 CFDictionaryRef objects = CFDictionaryCreateForCFTypes(kCFAllocatorDefault, kTestKeyIDTimestamp, cfidstr, NULL);
74 SOSCloudKeychainPutObjectsInCloud(objects, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
75 ^(CFDictionaryRef returnedValues, CFErrorRef error) {
76 if (error) {
77 fail("Error putting: %@", error);
78 CFReleaseSafe(error);
79 }
80 });
81
82 CFMutableArrayRef keysToGet = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
83 CFArrayAppendValue(keysToGet, kTestKeyIDTimestamp);
84 SOSCloudKeychainGetObjectsFromCloud(keysToGet, theq, ^ (CFDictionaryRef returnedValues, CFErrorRef error)
85 {
86 CFStringRef returnedTimestamp = (CFStringRef)CFDictionaryGetValue(returnedValues, kTestKeyIDTimestamp);
87 if (returnedTimestamp)
88 result = CFEqual(returnedTimestamp, cfidstr);
89 });
90
91 if (nowstr)
92 free((void *)nowstr);
93 return result;
94 }
95
96 static const int kbasicKVSTestsCount = 1;
97 static void basicKVSTests2()
98 {
99 dispatch_queue_t generalq = dispatch_queue_create("general", DISPATCH_QUEUE_SERIAL);
100
101 const UInt8 tdata[] = {0x01, 0x02, 0x03, 0x04, 'a', 'b', 'c'};
102 CFDataRef testData = CFDataCreate(kCFAllocatorDefault, tdata, sizeof(tdata)/sizeof(UInt8));
103
104 const CFStringRef adata[] = {CFSTR("A"), CFSTR("b"), CFSTR("C"), CFSTR("D")};
105 CFArrayRef testArray = CFArrayCreate(kCFAllocatorDefault, (const void **)&adata, sizeof(adata)/sizeof(CFStringRef), &kCFTypeArrayCallBacks);
106
107 // Register keys
108 const CFStringRef circleKeyStrings[] = {CFSTR("circleA"), CFSTR("circleB"), CFSTR("circleC"), CFSTR("circleD")};
109 CFArrayRef circleKeys = CFArrayCreate(kCFAllocatorDefault, (const void **)&circleKeyStrings, sizeof(circleKeyStrings)/sizeof(CFStringRef), &kCFTypeArrayCallBacks);
110
111 const CFStringRef keysWhenUnlockedKeyStrings[] = {CFSTR("foo"), CFSTR("bar"), CFSTR("baz")};
112 CFArrayRef keysWhenUnlocked = CFArrayCreate(kCFAllocatorDefault, (const void **)&keysWhenUnlockedKeyStrings, sizeof(keysWhenUnlockedKeyStrings)/sizeof(CFStringRef), &kCFTypeArrayCallBacks);
113
114 printTimeNow("Start tests [basicKVSTests]");
115
116 ok(postIDTimestamp(generalq), "testPostGet for %@", kTestKeyIDTimestamp);
117
118 printTimeNow("Start tests [basicKVSTests2]");
119
120 // Release test data
121 CFRelease(testData);
122 CFRelease(testArray);
123 CFRelease(circleKeys);
124 CFRelease(keysWhenUnlocked);
125 }
126
127 // MARK: ----- start of all tests -----
128
129 static const int kCloudTransportTestsCount = 0;
130 static int kTestTestCount = kbasicKVSTestsCount + kCloudTransportTestsCount;
131 static void tests(void)
132 {
133 SKIP: {
134 skip("Skipping ckdclient tests because CloudKeychainProxy.xpc is not installed", kTestTestCount, XPCServiceInstalled());
135 basicKVSTests2();
136 // cloudTransportTests();
137 sleep(15); // on iOS, we need to hang around long enough for syncdefaultsd to see us
138 }
139 }
140
141 int sc_95_ckd2client(int argc, char *const *argv)
142 {
143 plan_tests(kTestTestCount);
144
145 tests();
146
147 return 0;
148 }
149
150