]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-71-engine-save.c
Security-57740.31.2.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-71-engine-save.c
1 /*
2 * Copyright (c) 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 // Test save and restore of SOSEngine states
26
27 #include <SOSCircle/Regressions/SOSTestDevice.h>
28 #include <SOSCircle/Regressions/SOSTestDataSource.h>
29 #include "secd_regressions.h"
30 #include "SecdTestKeychainUtilities.h"
31
32 #include <Security/SecureObjectSync/SOSEnginePriv.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>
43
44 #include <AssertMacros.h>
45 #include <stdint.h>
46
47 static int kTestTestCount = 28 + 1; // +1 for secd_test_setup_temp_keychain
48
49 #include "secd-71-engine-save-sample1.h"
50
51 static bool addEngineStateWithData(CFDataRef engineStateData) {
52 /*
53 MANGO-iPhone:~ mobile$ security item class=genp,acct=engine-state
54 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
58 musr : //
59 pdmn : dk
60 svce : SOSDataSource-ak
61 sync : 0
62 tomb : 0
63 */
64
65 CFMutableDictionaryRef item = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
66
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);
74
75 CFErrorRef localError = NULL;
76 OSStatus status = noErr;
77 is_status(status = SecItemAdd(item, (CFTypeRef *)&localError), errSecSuccess, "add v0 engine-state");
78 CFReleaseSafe(item);
79 CFReleaseSafe(localError);
80 return status == noErr;
81 }
82
83 #if 0
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);
89 }
90 return false;
91 }, ^bool(SOSTestDeviceRef source, SOSTestDeviceRef dest, SOSMessageRef message) {
92 iteration++;
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);
98 return true;
99 }
100 return false;
101 }, CFSTR("alice"), CFSTR("bob"), CFSTR("claire"), CFSTR("dave"),CFSTR("edward"), CFSTR("frank"), CFSTR("gary"), NULL);
102 }
103 #endif
104
105 static void testsync2p(void) {
106 __block int iteration = 0;
107 SOSTestDeviceListTestSync("testsync2p", test_directive, test_reason, 0, false, ^bool(SOSTestDeviceRef source, SOSTestDeviceRef dest) {
108 iteration++;
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);
114 return true;
115 }
116 return false;
117 }, ^bool(SOSTestDeviceRef source, SOSTestDeviceRef dest, SOSMessageRef message) {
118 return false;
119 }, CFSTR("Atestsync2p"), CFSTR("Btestsync2p"), NULL);
120 }
121
122 static void savetests(void) {
123 ok(true,"message");
124 // SOSEngineSave(SOSEngineRef engine, SOSTransactionRef txn, CFErrorRef *error)
125 testsync2p();
126 }
127
128 int secd_71_engine_save(int argc, char *const *argv)
129 {
130 plan_tests(kTestTestCount);
131
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();
136
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);
141 });
142
143 // TODO: use call that prepopulates keychain (block for above)
144 ok(sizeof(es_mango_bin)== es_mango_bin_len,"bad mango");
145 savetests();
146
147 return 0;
148 }