]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-200-logstate.c
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-200-logstate.c
1 /*
2 * Copyright (c) 2013-2016 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 // secd-200-logstate.c
25 // sec
26 //
27
28 #include <stdio.h>
29
30
31
32
33 #include <Security/SecBase.h>
34 #include <Security/SecItem.h>
35
36 #include <CoreFoundation/CFDictionary.h>
37
38 #include <Security/SecureObjectSync/SOSAccount.h>
39 #include <Security/SecureObjectSync/SOSCloudCircle.h>
40 #include <Security/SecureObjectSync/SOSInternal.h>
41 #include <Security/SecureObjectSync/SOSUserKeygen.h>
42 #include <Security/SecureObjectSync/SOSTransport.h>
43 #include "SOSCloudKeychainLogging.h"
44
45 #include <stdlib.h>
46 #include <unistd.h>
47
48 #include "secd_regressions.h"
49 #include "SOSTestDataSource.h"
50
51 #include "SOSRegressionUtilities.h"
52 #include <utilities/SecCFWrappers.h>
53 #include <Security/SecKeyPriv.h>
54
55 #include <securityd/SOSCloudCircleServer.h>
56
57 #include "SOSAccountTesting.h"
58
59 #include "SecdTestKeychainUtilities.h"
60
61 #define HOW_MANY_MINIONS 4
62
63 static int kTestTestCount = (5+(HOW_MANY_MINIONS+1)*20);
64
65
66 static bool SOSArrayForEachAccount(CFArrayRef accounts, bool (^operation)(SOSAccountRef account)) {
67 __block bool retval = true;
68 CFArrayForEach(accounts, ^(const void *value) {
69 SOSAccountRef account = (SOSAccountRef) value;
70 retval &= operation(account);
71 });
72 return retval;
73 }
74
75
76 static inline void FeedChangesToMasterMinions(CFMutableDictionaryRef changes, SOSAccountRef master_account, CFArrayRef minion_accounts) {
77 FeedChangesTo(changes, master_account);
78 SOSArrayForEachAccount(minion_accounts, ^bool(SOSAccountRef account) {
79 FeedChangesTo(changes, account);
80 return true;
81 });
82 FeedChangesTo(changes, master_account);
83
84 }
85
86
87 static inline bool ProcessChangesOnceMasterMinions(CFMutableDictionaryRef changes, SOSAccountRef master_account, CFArrayRef minion_accounts) {
88 bool result = FillAllChanges(changes);
89 FeedChangesToMasterMinions(changes, master_account, minion_accounts);
90 return result;
91 }
92
93 static inline int ProcessChangesForMasterAndMinions(CFMutableDictionaryRef changes, SOSAccountRef master_account, CFArrayRef minion_accounts) {
94 int result = 0;
95 bool new_data = false;
96 do {
97 new_data = ProcessChangesOnceMasterMinions(changes, master_account, minion_accounts);
98 ++result;
99 } while (new_data);
100 return result;
101 }
102
103 static bool MakeTheBigCircle(CFMutableDictionaryRef changes, SOSAccountRef master_account, CFArrayRef minion_accounts, CFErrorRef *error) {
104 bool retval = SOSAccountResetToOffering_wTxn(master_account, error);
105 require_quiet(retval, errOut);
106 ProcessChangesForMasterAndMinions(changes, master_account, minion_accounts);
107 retval = SOSArrayForEachAccount(minion_accounts, ^bool(SOSAccountRef account) {
108 bool localret = SOSAccountJoinCircles_wTxn(account, error);
109 ProcessChangesForMasterAndMinions(changes, master_account, minion_accounts);
110 return localret;
111 });
112 require_quiet(retval, errOut);
113 CFArrayRef applicants = SOSAccountCopyApplicants(master_account, error);
114 retval = SOSAccountAcceptApplicants(master_account , applicants, error);
115 ProcessChangesForMasterAndMinions(changes, master_account, minion_accounts);
116 errOut:
117 return retval;
118 }
119
120
121 static CFArrayRef CreateManyAccountsForLocalChanges(CFStringRef namefmt, CFStringRef data_source_name, size_t howmany) {
122 CFMutableArrayRef accounts = CFArrayCreateMutable(kCFAllocatorDefault, howmany, &kCFTypeArrayCallBacks);
123
124 for(size_t i = 0; i < howmany; i++) {
125 CFStringRef tmpname = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, namefmt, i);
126 SOSAccountRef tmp = CreateAccountForLocalChanges(tmpname, CFSTR("TestSource"));
127 CFArraySetValueAtIndex(accounts, i, tmp);
128 CFReleaseNull(tmpname);
129 CFReleaseNull(tmp);
130 }
131 return accounts;
132 }
133
134 static bool AssertAllCredentialsAndUpdate(CFMutableDictionaryRef changes, SOSAccountRef master_account, CFArrayRef minion_accounts, CFStringRef user_account, CFDataRef user_password, CFErrorRef *error) {
135 __block bool retval = SOSAccountAssertUserCredentialsAndUpdate(master_account, user_account, user_password, error);
136 ProcessChangesForMasterAndMinions(changes, master_account, minion_accounts);
137 retval &= SOSArrayForEachAccount(minion_accounts, ^bool(SOSAccountRef account) {
138 CFReleaseNull(*error);
139 return SOSAccountAssertUserCredentialsAndUpdate(account, user_account, user_password, error);
140 });
141 CFReleaseNull(*error);
142
143 return retval;
144 }
145
146 static void tests(void)
147 {
148 CFErrorRef error = NULL;
149 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
150 CFStringRef cfaccount = CFSTR("test@test.org");
151
152 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
153
154 SOSAccountRef master_account = CreateAccountForLocalChanges(CFSTR("master"), CFSTR("TestSource"));
155 CFArrayRef minion_accounts = CreateManyAccountsForLocalChanges(CFSTR("minion%d"), CFSTR("TestSource"), HOW_MANY_MINIONS);
156
157 ok(AssertAllCredentialsAndUpdate(changes, master_account, minion_accounts, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
158
159 secLogEnable();
160 SOSAccountLogState(master_account);
161 secLogDisable();
162
163 ok(MakeTheBigCircle(changes, master_account, minion_accounts, &error), "Get Everyone into the circle %@", error);
164
165 diag("WHAT?");
166 secLogEnable();
167 SOSAccountLogState(master_account);
168 SOSAccountLogViewState(master_account);
169 SOSCloudKVSLogState();
170 secLogDisable();
171
172 CFDataRef acctData = SOSAccountCopyEncodedData(master_account, kCFAllocatorDefault, &error);
173 diag("Account DER Size is %d for %d peers", CFDataGetLength(acctData), HOW_MANY_MINIONS+1);
174 CFReleaseNull(acctData);
175 CFReleaseNull(error);
176
177 CFDataRef circleData = SOSCircleCopyEncodedData(master_account->trusted_circle, kCFAllocatorDefault, &error);
178 diag("Circle DER Size is %d for %d peers", CFDataGetLength(circleData), HOW_MANY_MINIONS+1);
179 CFReleaseNull(circleData);
180 CFReleaseNull(error);
181
182 CFDataRef peerData = SOSPeerInfoCopyEncodedData(SOSAccountGetMyPeerInfo(master_account), kCFAllocatorDefault, &error);
183 diag("Peer DER Size is %d", CFDataGetLength(peerData));
184 CFReleaseNull(peerData);
185 CFReleaseNull(error);
186
187 CFReleaseNull(error);
188 CFReleaseNull(master_account);
189 CFReleaseNull(minion_accounts);
190
191 SOSUnregisterAllTransportMessages();
192 SOSUnregisterAllTransportCircles();
193 SOSUnregisterAllTransportKeyParameters();
194 CFArrayRemoveAllValues(key_transports);
195 CFArrayRemoveAllValues(circle_transports);
196 CFArrayRemoveAllValues(message_transports);
197
198 }
199
200 int secd_200_logstate(int argc, char *const *argv)
201 {
202 plan_tests(kTestTestCount);
203
204 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
205
206 tests();
207
208 return 0;
209 }