2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <AssertMacros.h>
32 #include <Security/SecBase.h>
33 #include <Security/SecItem.h>
35 #include <SecureObjectSync/SOSAccount.h>
36 #include <CKBridge/SOSCloudKeychainClient.h>
38 #include <utilities/SecCFWrappers.h>
39 #include <utilities/debugging.h>
41 #include "SOSCircle_regressions.h"
42 #include "SOSRegressionUtilities.h"
45 static bool verboseCKDClientDebugging
= false;
47 static CFStringRef kTestKeyIDTimestamp
= CFSTR("IDTimestamp");
48 static dispatch_group_t sDispatchGroup
= NULL
;
49 static dispatch_queue_t xpc_queue
= NULL
;
51 // MARK: ----- Test Data -----
53 static CFStringRef kTestKeyString
= CFSTR("teststring");
54 static CFStringRef kTestKeyData
= CFSTR("testdata");
55 static const UInt8 tdata
[] = {0x01, 0x02, 0x03, 0x04, 'a', 'b', 'c'};
56 static CFStringRef kTestKeyArray
= CFSTR("testarray");
57 static const CFStringRef adata
[] = {CFSTR("A"), CFSTR("b"), CFSTR("C"), CFSTR("D")};
58 static const CFStringRef circleKeyStrings
[] = {CFSTR("circleA"), CFSTR("circleB"), CFSTR("circleC"), CFSTR("circleD")};
59 static const CFStringRef keysWhenUnlockedKeyStrings
[] = {CFSTR("foo"), CFSTR("bar"), CFSTR("baz")};
61 static CFDataRef testData
= NULL
;
62 static CFArrayRef testArray
= NULL
;
63 static CFArrayRef circleKeys
= NULL
;
64 static CFArrayRef keysWhenUnlocked
= NULL
;
66 static void initializeTestData(void)
68 testData
= CFDataCreate(kCFAllocatorDefault
, tdata
, sizeof(tdata
)/sizeof(UInt8
));
69 testArray
= CFArrayCreate(kCFAllocatorDefault
, (const void **)&adata
, sizeof(adata
)/sizeof(CFStringRef
), &kCFTypeArrayCallBacks
);
71 circleKeys
= CFArrayCreate(kCFAllocatorDefault
, (const void **)&circleKeyStrings
, sizeof(circleKeyStrings
)/sizeof(CFStringRef
), &kCFTypeArrayCallBacks
);
72 keysWhenUnlocked
= CFArrayCreate(kCFAllocatorDefault
, (const void **)&keysWhenUnlockedKeyStrings
, sizeof(keysWhenUnlockedKeyStrings
)/sizeof(CFStringRef
),
73 &kCFTypeArrayCallBacks
);
76 // MARK: ----- utilities -----
78 static void printTimeNow(const char *msg
)
80 if (verboseCKDClientDebugging
)
82 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
83 const char *nowstr
= cfabsoluteTimeToString(now
);
86 printf("%s %s\n", nowstr
, msg
);
92 // MARK: ----- basicKVSTests -----
94 static bool testPostGet(CFStringRef key
, CFTypeRef cfobj
, dispatch_queue_t processQueue
, dispatch_group_t dgroup
)
96 CFErrorRef error
= NULL
;
100 testPutObjectInCloud(key
, cfobj
, &error
, dgroup
, processQueue
);
101 CFTypeRef cfvalue
= testGetObjectFromCloud(key
, processQueue
, dgroup
);
102 printTimeNow("finished getObjectFromCloud");
105 if (CFGetTypeID(cfvalue
)==CFDictionaryGetTypeID())
106 cfv
= CFDictionaryGetValue(cfvalue
, key
);
109 result
= CFEqual(cfobj
, cfv
);
113 static bool postIDTimestamp(dispatch_queue_t theq
, dispatch_group_t dgroup
)
116 CFStringRef macaddr
= myMacAddress();
117 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
118 const char *nowstr
= cfabsoluteTimeToStringLocal(now
);
119 CFStringRef cfidstr
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("%@: %@ %s"), kTestKeyIDTimestamp
, macaddr
,nowstr
);
120 secerror("Setting %@ key: %@", kTestKeyIDTimestamp
, cfidstr
);
121 result
= testPostGet(kTestKeyIDTimestamp
, cfidstr
, theq
, dgroup
);
124 free((void *)nowstr
);
125 CFReleaseSafe(cfidstr
);
130 static const int kbasicKVSTestsCount
= 9;
131 static void basicKVSTests(dispatch_group_t dgroup
)
133 dispatch_queue_t generalq
= dispatch_queue_create("general", DISPATCH_QUEUE_SERIAL
);
135 printTimeNow("Start tests [basicKVSTests]");
136 // dispatch_group_enter(dgroup);
138 // synchronize first to make sure we see cloud values
139 ok(testSynchronize(generalq
, dgroup
), "test synchronize");
141 // Next, get the TimeNow value, since this is the only one that differs from test to test (i.e. sc-90-ckdclient)
142 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
143 const char *nowstr
= cfabsoluteTimeToStringLocal(now
);
144 CFStringRef cfstrtime
= CFStringCreateWithCString(kCFAllocatorDefault
, nowstr
, kCFStringEncodingUTF8
);
145 ok(testGetObjectFromCloud(kTestKeyIDTimestamp
, generalq
, dgroup
) != nil
, "testGet for %@", kTestKeyIDTimestamp
);
147 ok(postIDTimestamp(generalq
, dgroup
), "testPostGet for %@", kTestKeyIDTimestamp
);
149 ok(testPostGet(kTestKeyString
, CFSTR("test string"), generalq
, dgroup
), "testPostGet for CFStringRef");
151 // Now the fixed values
152 ok(testPostGet(kTestKeyString
, CFSTR("test string"), generalq
, dgroup
), "testPostGet for CFStringRef");
153 ok(testPostGet(kTestKeyData
, testData
, generalq
, dgroup
), "testPostGet for CFDataRef");
154 ok(testPostGet(kTestKeyArray
, testArray
, generalq
, dgroup
), "testPostGet for CFDataRef");
156 ok(postIDTimestamp(generalq
, dgroup
), "testPostGet for %@", kTestKeyIDTimestamp
);
158 // Synchronize one more time before exit
159 ok(testSynchronize(generalq
, dgroup
), "test synchronize");
161 printTimeNow("End tests [basicKVSTests]");
163 // dispatch_group_leave(dgroup);
167 CFRelease(testArray
);
168 CFRelease(circleKeys
);
169 CFRelease(keysWhenUnlocked
);
170 CFRelease(cfstrtime
);
172 free((void *)nowstr
);
175 // MARK: ----- start of all tests -----
177 static int kTestTestCount
= kbasicKVSTestsCount
;
178 static void tests(void)
181 skip("Skipping ckdclient tests because CloudKeychainProxy.xpc is not installed", kTestTestCount
, XPCServiceInstalled());
182 // dispatch_queue_t dqueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
183 xpc_queue
= dispatch_queue_create("sc_90_ckdclient", DISPATCH_QUEUE_SERIAL
);
185 sDispatchGroup
= dispatch_group_create();
187 initializeTestData();
188 basicKVSTests(sDispatchGroup
);
192 int sc_90_ckdclient(int argc
, char *const *argv
)
194 plan_tests(kTestTestCount
);