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 save and restore of SOSEngine states
27 #include <SOSCircle/Regressions/SOSTestDevice.h>
28 #include <SOSCircle/Regressions/SOSTestDataSource.h>
29 #include "secd_regressions.h"
30 #include "SecdTestKeychainUtilities.h"
32 #include <Security/SecureObjectSync/SOSEngine.h>
33 #include <Security/SecureObjectSync/SOSPeer.h>
34 #include <Security/SecBase64.h>
35 #include <Security/SecItem.h>
36 #include <Security/SecItemPriv.h>
37 #include <corecrypto/ccsha2.h>
38 #include <securityd/SecItemServer.h>
39 #include <securityd/SecItemDataSource.h>
40 #include <utilities/SecCFWrappers.h>
41 #include <utilities/SecIOFormat.h>
42 #include <utilities/SecFileLocations.h>
44 #include <AssertMacros.h>
47 static int kTestTestCount
= 28 + 1; // +1 for secd_test_setup_temp_keychain
49 #include "secd-71-engine-save-sample1.h"
51 static bool addEngineStateWithData(CFDataRef engineStateData
) {
53 MANGO-iPhone:~ mobile$ security item class=genp,acct=engine-state
55 agrp : com.apple.security.sos
56 cdat : 2016-04-18 20:40:33 +0000
57 mdat : 2016-04-18 20:40:33 +0000
60 svce : SOSDataSource-ak
65 CFMutableDictionaryRef item
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
67 CFDictionarySetValue(item
, kSecClass
, kSecClassGenericPassword
);
68 CFDictionarySetValue(item
, kSecAttrAccount
, CFSTR("engine-state"));
69 CFDictionarySetValue(item
, kSecAttrAccessGroup
, CFSTR("com.apple.security.sos"));
70 CFDictionarySetValue(item
, kSecAttrAccessible
, kSecAttrAccessibleAlwaysPrivate
);
71 CFDictionarySetValue(item
, kSecAttrService
, CFSTR("SOSDataSource-ak"));
72 CFDictionarySetValue(item
, kSecAttrSynchronizable
, kCFBooleanFalse
);
73 CFDictionarySetValue(item
, kSecValueData
, engineStateData
);
75 CFErrorRef localError
= NULL
;
76 OSStatus status
= noErr
;
77 is_status(status
= SecItemAdd(item
, (CFTypeRef
*)&localError
), errSecSuccess
, "add v0 engine-state");
79 CFReleaseSafe(localError
);
80 return status
== noErr
;
84 static void testsync2(const char *name
, const char *test_directive
, const char *test_reason
, void (^aliceInit
)(SOSDataSourceRef ds
), void (^bobInit
)(SOSDataSourceRef ds
), CFStringRef msg
, ...) {
85 __block
int iteration
=0;
86 SOSTestDeviceListTestSync(name
, test_directive
, test_reason
, kSOSPeerVersion
, false, ^bool(SOSTestDeviceRef source
, SOSTestDeviceRef dest
) {
87 if (iteration
== 96) {
88 pass("%@ before message", source
);
91 }, ^bool(SOSTestDeviceRef source
, SOSTestDeviceRef dest
, SOSMessageRef message
) {
93 if (iteration
== 60) {
94 pass("%@ before addition", source
);
95 //SOSTestDeviceAddGenericItem(source, CFSTR("test_account"), CFSTR("test service"));
96 SOSTestDeviceAddRemoteGenericItem(source
, CFSTR("test_account"), CFSTR("test service"));
97 pass("%@ after addition", source
);
101 }, CFSTR("alice"), CFSTR("bob"), CFSTR("claire"), CFSTR("dave"),CFSTR("edward"), CFSTR("frank"), CFSTR("gary"), NULL
);
105 static void testsync2p(void) {
106 __block
int iteration
= 0;
107 SOSTestDeviceListTestSync("testsync2p", test_directive
, test_reason
, 0, false, ^bool(SOSTestDeviceRef source
, SOSTestDeviceRef dest
) {
109 // Add 10 items in first 10 sync messages
110 if (iteration
<= 10) {
111 CFStringRef account
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("item%d"), iteration
);
112 SOSTestDeviceAddGenericItem(source
, account
, CFSTR("testsync2p"));
113 CFReleaseSafe(account
);
117 }, ^bool(SOSTestDeviceRef source
, SOSTestDeviceRef dest
, SOSMessageRef message
) {
119 }, CFSTR("Atestsync2p"), CFSTR("Btestsync2p"), NULL
);
122 static void savetests(void) {
124 // SOSEngineSave(SOSEngineRef engine, SOSTransactionRef txn, CFErrorRef *error)
128 int secd_71_engine_save(int argc
, char *const *argv
)
130 plan_tests(kTestTestCount
);
132 /* custom keychain dir */
133 // secd_test_setup_temp_keychain(__FUNCTION__, NULL);
134 secd_test_setup_temp_keychain(__FUNCTION__
, ^{
135 CFStringRef keychain_path_cf
= __SecKeychainCopyPath();
137 CFDataRef engineStateData
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, es_mango_bin
, es_mango_bin_len
, kCFAllocatorNull
);
138 ok(addEngineStateWithData(engineStateData
),"failed to add v0 engine state");
139 CFReleaseSafe(engineStateData
);
140 CFReleaseSafe(keychain_path_cf
);
143 // TODO: use call that prepopulates keychain (block for above)
144 ok(sizeof(es_mango_bin
)== es_mango_bin_len
,"bad mango");