2 * Copyright (c) 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 // Test syncing between SecItemDataSource and SOSTestDataSource
27 #include "SOSTestDevice.h"
28 #include "SOSTestDataSource.h"
29 #include <test/testmore.h>
31 #include <SecureObjectSync/SOSEngine.h>
32 #include <SecureObjectSync/SOSPeer.h>
33 #include <Security/SecBase64.h>
34 #include <Security/SecItem.h>
35 #include <Security/SecItemPriv.h>
36 #include <corecrypto/ccsha2.h>
37 #include <securityd/SecItemServer.h>
38 #include <securityd/SecItemDataSource.h>
39 #include <utilities/SecCFWrappers.h>
40 #include <utilities/SecFileLocations.h>
41 #include <utilities/SecIOFormat.h>
44 #include <AssertMacros.h>
46 CFStringRef
SOSMessageCopyDigestHex(SOSMessageRef message
) {
47 uint8_t digest
[CCSHA1_OUTPUT_SIZE
];
48 // TODO: Pass in real sequenceNumber.
49 CFDataRef msgData
= SOSMessageCreateData(message
, 0, NULL
);
50 if (!msgData
) return NULL
;
51 ccdigest(ccsha1_di(), CFDataGetLength(msgData
), CFDataGetBytePtr(msgData
), digest
);
52 CFMutableStringRef hex
= CFStringCreateMutable(0, 2 * sizeof(digest
));
53 for (unsigned int ix
= 0; ix
< sizeof(digest
); ++ix
) {
54 CFStringAppendFormat(hex
, 0, CFSTR("%02X"), digest
[ix
]);
56 CFReleaseSafe(msgData
);
60 static void SOSTestDeviceDestroy(CFTypeRef cf
) {
61 SOSTestDeviceRef td
= (SOSTestDeviceRef
)cf
;
62 CFReleaseSafe(td
->peers
);
64 SOSDataSourceRelease(td
->ds
, NULL
);
66 td
->dsf
->release(td
->dsf
);
67 CFReleaseSafe(td
->db
);
70 CFStringRef
SOSTestDeviceGetID(SOSTestDeviceRef td
) {
71 CFStringRef engineID
= NULL
;
72 SOSEngineRef engine
= SOSDataSourceGetSharedEngine(td
->ds
, NULL
);
74 engineID
= SOSEngineGetMyID(engine
);
78 void SOSTestDeviceForEachPeerID(SOSTestDeviceRef td
, void(^peerBlock
)(CFStringRef peerID
, bool *stop
)) {
81 CFArrayForEachC(td
->peers
, peer
) {
82 peerBlock(SOSPeerGetID(peer
), &stop
);
88 static CFStringRef
SOSTestDeviceCopyDescription(CFTypeRef cf
) {
89 SOSTestDeviceRef td
= (SOSTestDeviceRef
)cf
;
90 CFMutableStringRef result
= CFStringCreateMutable(kCFAllocatorDefault
, 0);
91 CFStringAppendFormat(result
, NULL
, CFSTR("<SOSTestDevice %@"), td
->ds
->engine
);
92 SOSTestDeviceForEachPeerID(td
, ^(CFStringRef peerID
, bool *stop
) {
93 SOSPeerRef peer
= SOSPeerCreateWithEngine(td
->ds
->engine
, peerID
);
94 CFStringAppendFormat(result
, NULL
, CFSTR("\n%@"), peer
);
97 CFStringAppendFormat(result
, NULL
, CFSTR(">"));
101 CFGiblisFor(SOSTestDevice
)
103 static SOSTestDeviceRef
SOSTestDeviceCreateInternal(CFAllocatorRef allocator
, CFStringRef engineID
) {
104 SOSTestDeviceRef td
= CFTypeAllocate(SOSTestDevice
, struct __OpaqueSOSTestDevice
, allocator
);
105 td
->peers
= CFArrayCreateMutableForCFTypes(kCFAllocatorDefault
);
109 SOSTestDeviceRef
SOSTestDeviceCreateWithDb(CFAllocatorRef allocator
, CFStringRef engineID
, SecDbRef db
) {
110 setup("create device");
111 SOSTestDeviceRef td
= SOSTestDeviceCreateInternal(allocator
, engineID
);
112 CFRetainAssign(td
->db
, db
);
113 td
->dsf
= SecItemDataSourceFactoryGetShared(td
->db
);
114 CFArrayRef ds_names
= td
->dsf
->copy_names(td
->dsf
);
115 CFErrorRef error
= NULL
;
116 if (ds_names
&& CFArrayGetCount(ds_names
) > 0) {
117 CFStringRef sname
= CFArrayGetValueAtIndex(ds_names
, 0);
118 ok (td
->ds
= td
->dsf
->create_datasource(td
->dsf
, sname
, &error
), "%@ create datasource \"%@\" [error: %@]", engineID
, sname
, error
);
119 CFReleaseNull(error
);
121 CFReleaseNull(ds_names
);
122 assert(td
->ds
); // Shut up static analyzer and test generally run in debug mode anyway
124 SOSEngineCircleChanged(SOSDataSourceGetSharedEngine(td
->ds
, NULL
), engineID
, NULL
, NULL
);
128 SOSTestDeviceRef
SOSTestDeviceCreateWithDbNamed(CFAllocatorRef allocator
, CFStringRef engineID
, CFStringRef dbName
) {
129 CFURLRef url
= SecCopyURLForFileInKeychainDirectory(dbName
);
130 CFStringRef path
= CFURLCopyFileSystemPath(url
, kCFURLPOSIXPathStyle
);
131 SecDbRef db
= SecKeychainDbCreate(path
);
132 SOSTestDeviceRef td
= SOSTestDeviceCreateWithDb(allocator
, engineID
, db
);
139 SOSTestDeviceRef
SOSTestDeviceCreateWithTestDataSource(CFAllocatorRef allocator
, CFStringRef engineID
) {
140 setup("create device");
141 SOSTestDeviceRef td
= SOSTestDeviceCreateInternal(allocator
, engineID
);
143 td
->ds
= SOSTestDataSourceCreate();
144 CFErrorRef error
= NULL
;
145 ok(td
->ds
->engine
= SOSEngineCreate(td
->ds
, &error
), "create engine: %@", error
);
146 SOSEngineCircleChanged(td
->ds
->engine
, engineID
, NULL
, NULL
);
147 CFReleaseNull(error
);
151 SOSTestDeviceRef
SOSTestDeviceSetPeerIDs(SOSTestDeviceRef td
, CFArrayRef peerIDs
, CFIndex version
) {
152 setup("create device");
153 CFStringRef engineID
= SOSTestDeviceGetID(td
);
155 CFMutableArrayRef trustedPeersIDs
= CFArrayCreateMutableForCFTypes(kCFAllocatorDefault
);
156 CFErrorRef error
= NULL
;
157 CFArrayForEachC(peerIDs
, peerID
) {
158 if (isString(peerID
) && !CFEqualSafe(peerID
, engineID
)) {
160 ok(peer
= SOSPeerCreateSimple(td
->ds
->engine
, peerID
, version
, &error
), "create peer: %@", error
);
161 CFReleaseNull(error
);
162 CFArrayAppendValue(td
->peers
, peer
);
163 CFArrayAppendValue(trustedPeersIDs
, peerID
);
167 SOSEngineCircleChanged(td
->ds
->engine
, engineID
, trustedPeersIDs
, NULL
);
168 CFArrayForEachC(trustedPeersIDs
, peerID
) {
169 ok(SOSEnginePeerDidConnect(td
->ds
->engine
, peerID
, &error
), "tell %@ %@ connected: %@", engineID
, peerID
, error
);
170 CFReleaseNull(error
);
172 CFReleaseSafe(trustedPeersIDs
);
176 CFDataRef
SOSTestDeviceCreateMessage(SOSTestDeviceRef td
, CFStringRef peerID
) {
177 setup("create message");
178 CFErrorRef error
= NULL
;
179 SOSEnginePeerMessageSentBlock sent
= NULL
;
181 ok(msgData
= SOSEngineCreateMessageToSyncToPeer(td
->ds
->engine
, peerID
, &sent
, &error
),
182 "create message to %@: %@", peerID
, error
);
190 CFDictionaryRef
SOSTestDeviceCreateMessages(SOSTestDeviceRef td
) {
191 CFTypeRef peer
= NULL
;
192 CFMutableDictionaryRef messages
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
193 CFArrayForEachC(td
->peers
, peer
) {
194 CFStringRef peerID
= SOSPeerGetID((SOSPeerRef
)peer
);
195 CFDataRef msg
= SOSTestDeviceCreateMessage(td
, peerID
);
197 CFDictionaryAddValue(messages
, peerID
, msg
);
205 bool SOSTestDeviceHandleMessage(SOSTestDeviceRef td
, CFStringRef peerID
, CFDataRef msgData
) {
206 setup("handle message");
207 if (!msgData
) return false;
208 CFErrorRef error
= NULL
;
210 SOSMessageRef message
;
212 ok(message
= SOSMessageCreateWithData(kCFAllocatorDefault
, msgData
, &error
), "decode message %@: %@", msgData
, error
);
213 CFReleaseNull(error
);
214 pass("handeling %@->%@ %@", peerID
, SOSEngineGetMyID(SOSDataSourceGetSharedEngine(td
->ds
, &error
)), message
);
215 ok(handled
= SOSEngineHandleMessage(SOSDataSourceGetSharedEngine(td
->ds
, &error
), peerID
, msgData
, &error
),
216 "handled from %@ %@: %@", peerID
, message
, error
);
217 CFReleaseNull(error
);
219 CFReleaseNull(message
);
223 void SOSTestDeviceAddGenericItem(SOSTestDeviceRef td
, CFStringRef account
, CFStringRef server
) {
224 __block CFErrorRef error
= NULL
;
225 if (!SOSDataSourceWithAPI(td
->ds
, true, &error
, ^(SOSTransactionRef txn
, bool *commit
) {
226 SOSObjectRef object
= SOSDataSourceCreateGenericItem(td
->ds
, account
, server
);
227 ok(SOSDataSourceMergeObject(td
->ds
, txn
, object
, NULL
, &error
), "%@ added API object %@", SOSTestDeviceGetID(td
), error
? (CFTypeRef
)error
: (CFTypeRef
)CFSTR("ok"));
228 CFReleaseSafe(object
);
229 CFReleaseNull(error
);
231 fail("ds transaction %@", error
);
232 CFReleaseNull(error
);
235 void SOSTestDeviceAddRemoteGenericItem(SOSTestDeviceRef td
, CFStringRef account
, CFStringRef server
) {
236 __block CFErrorRef error
= NULL
;
237 if (!SOSDataSourceWithAPI(td
->ds
, false, &error
, ^(SOSTransactionRef txn
, bool *commit
) {
238 SOSObjectRef object
= SOSDataSourceCreateGenericItem(td
->ds
, account
, server
);
239 ok(SOSDataSourceMergeObject(td
->ds
, txn
, object
, NULL
, &error
), "%@ added remote object %@", SOSTestDeviceGetID(td
), error
? (CFTypeRef
)error
: (CFTypeRef
)CFSTR("ok"));
240 CFReleaseSafe(object
);
241 CFReleaseNull(error
);
243 fail("ds transaction %@", error
);
244 CFReleaseNull(error
);
247 bool SOSTestDeviceAddGenericItems(SOSTestDeviceRef td
, CFIndex count
, CFStringRef account
, CFStringRef server
) {
248 __block
bool didAdd
= false;
249 __block CFErrorRef error
= NULL
;
250 if (!SOSDataSourceWithAPI(td
->ds
, true, &error
, ^(SOSTransactionRef txn
, bool *commit
) {
253 for (; success
&& ix
< count
; ++ix
) {
254 CFStringRef accountStr
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("%@%" PRIdCFIndex
), account
, ix
);
255 SOSObjectRef object
= SOSDataSourceCreateGenericItem(td
->ds
, accountStr
, server
);
256 success
= SOSDataSourceMergeObject(td
->ds
, txn
, object
, NULL
, &error
);
257 CFReleaseSafe(object
);
259 ok(success
, "%@ added %" PRIdCFIndex
" API objects %@", SOSTestDeviceGetID(td
), ix
, error
? (CFTypeRef
)error
: (CFTypeRef
)CFSTR("ok"));
260 didAdd
= success
&& ix
== count
;
261 CFReleaseNull(error
);
263 fail("ds transaction %@", error
);
264 CFReleaseNull(error
);
268 CFMutableDictionaryRef
SOSTestDeviceListCreate(bool realDb
, CFIndex version
, CFArrayRef deviceIDs
) {
269 CFMutableDictionaryRef testDevices
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
270 CFStringRef deviceID
;
271 CFArrayForEachC(deviceIDs
, deviceID
) {
272 SOSTestDeviceRef device
;
274 device
= SOSTestDeviceCreateWithTestDataSource(kCFAllocatorDefault
, deviceID
);
276 device
= SOSTestDeviceCreateWithDbNamed(kCFAllocatorDefault
, deviceID
, deviceID
);
277 SOSTestDeviceSetPeerIDs(device
, deviceIDs
, version
);
278 CFDictionarySetValue(testDevices
, deviceID
, device
);
279 CFReleaseSafe(device
);
281 CFDictionarySetValue(testDevices
, CFSTR("@devicesIDs"), deviceIDs
);
285 void SOSTestDeviceListSync(const char *name
, const char *test_directive
, const char *test_reason
, CFMutableDictionaryRef testDevices
, bool(^pre
)(SOSTestDeviceRef source
, SOSTestDeviceRef dest
), bool(^post
)(SOSTestDeviceRef source
, SOSTestDeviceRef dest
, SOSMessageRef message
)) {
286 CFArrayRef deviceIDs
= (CFArrayRef
)CFDictionaryGetValue(testDevices
, CFSTR("@devicesIDs"));
287 const CFIndex edgeCount
= CFArrayGetCount(deviceIDs
) * (CFArrayGetCount(deviceIDs
) - 1);
288 CFIndex deviceIX
= 0;
289 __block CFIndex noMsgSentCount
= 0;
290 __block CFIndex msgSentSinceLastChangeCount
= 0;
291 __block
bool done
= false;
293 CFStringRef sourceID
= (CFStringRef
)CFArrayGetValueAtIndex(deviceIDs
, deviceIX
++);
294 if (deviceIX
>= CFArrayGetCount(deviceIDs
))
297 SOSTestDeviceRef source
= (SOSTestDeviceRef
)CFDictionaryGetValue(testDevices
, sourceID
);
298 SOSTestDeviceForEachPeerID(source
, ^(CFStringRef destID
, bool *stop
) {
299 SOSTestDeviceRef dest
= (SOSTestDeviceRef
)CFDictionaryGetValue(testDevices
, destID
);
302 if (pre(source
, dest
))
303 msgSentSinceLastChangeCount
= 0;
305 CFDataRef msg
= SOSTestDeviceCreateMessage(source
, destID
);
306 SOSMessageRef message
= NULL
;
307 bool handled
= false;
308 msgSentSinceLastChangeCount
++;
309 if (msg
&& CFDataGetLength(msg
) > 0) {
310 handled
= SOSTestDeviceHandleMessage(dest
, sourceID
, msg
);
315 CFErrorRef error
= NULL
;
316 message
= SOSMessageCreateWithData(kCFAllocatorDefault
, msg
, &error
);
317 ok(handled
, "%s %@->%@ %@", name
, sourceID
, destID
, message
);
318 CFReleaseNull(error
);
320 SOSManifestRef sourceManifest
= SOSEngineCopyManifest(SOSDataSourceGetSharedEngine(source
->ds
, NULL
), NULL
);
321 pass("%s %@->%@ done L:%@", name
, sourceID
, destID
, sourceManifest
);
322 CFReleaseSafe(sourceManifest
);
324 //msgSentSinceLastChangeCount = 0;
328 if (post(source
, dest
, message
))
329 msgSentSinceLastChangeCount
= 0;
331 CFReleaseNull(message
);
333 if (noMsgSentCount
>= edgeCount
) {
335 } else if (msgSentSinceLastChangeCount
>= 9 * edgeCount
+ 1) {
336 fail("%s %" PRIdCFIndex
" peers never stopped syncing %" PRIdCFIndex
" messages since last change", name
, CFArrayGetCount(deviceIDs
), msgSentSinceLastChangeCount
);
343 bool SOSTestDeviceListInSync(const char *name
, const char *test_directive
, const char *test_reason
, CFMutableDictionaryRef testDevices
) {
345 CFArrayRef deviceIDs
= (CFArrayRef
)CFDictionaryGetValue(testDevices
, CFSTR("@devicesIDs"));
346 CFStringRef sourceID
= NULL
;
347 SOSManifestRef sourceManifest
= NULL
;
348 CFStringRef currentID
;
349 CFArrayForEachC(deviceIDs
, currentID
) {
350 SOSTestDeviceRef source
= (SOSTestDeviceRef
)CFDictionaryGetValue(testDevices
, currentID
);
351 SOSManifestRef manifest
= SOSEngineCopyManifest(SOSDataSourceGetSharedEngine(source
->ds
, NULL
), NULL
);
352 if (!sourceManifest
) {
353 sourceManifest
= CFRetainSafe(manifest
);
354 sourceID
= currentID
;
355 } else if (!CFEqual(manifest
, sourceManifest
)) {
356 fail("%s %@ manifest %@ != %@ manifest %@", name
, currentID
, manifest
, sourceID
, sourceManifest
);
359 CFReleaseSafe(manifest
);
361 CFReleaseSafe(sourceManifest
);
363 pass("%s all peers in sync", name
);
367 void SOSTestDeviceListTestSync(const char *name
, const char *test_directive
, const char *test_reason
, CFIndex version
, bool use_db
,
368 bool(^pre
)(SOSTestDeviceRef source
, SOSTestDeviceRef dest
),
369 bool(^post
)(SOSTestDeviceRef source
, SOSTestDeviceRef dest
, SOSMessageRef message
), ...) {
371 va_start(args
, post
);
372 // Optionally prefix each peer with name to make them more unique.
373 CFArrayRef deviceIDs
= CFArrayCreateForVC(kCFAllocatorDefault
, &kCFTypeArrayCallBacks
, args
);
374 CFMutableDictionaryRef testDevices
= SOSTestDeviceListCreate(use_db
, version
, deviceIDs
);
375 CFReleaseSafe(deviceIDs
);
376 SOSTestDeviceListSync(name
, test_directive
, test_reason
, testDevices
, pre
, post
);
377 SOSTestDeviceListInSync(name
, test_directive
, test_reason
, testDevices
);
378 CFReleaseSafe(testDevices
);