]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-200-logstate.m
Security-58286.240.4.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-200-logstate.m
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
44 #include <stdlib.h>
45 #include <unistd.h>
46
47 #include "secd_regressions.h"
48 #include "SOSTestDataSource.h"
49
50 #include "SOSRegressionUtilities.h"
51 #include <utilities/SecCFWrappers.h>
52 #include <Security/SecKeyPriv.h>
53
54 #include <securityd/SOSCloudCircleServer.h>
55
56 #include "SOSAccountTesting.h"
57
58 #include "SecdTestKeychainUtilities.h"
59
60 #define HOW_MANY_MINIONS 4
61
62 static int kTestTestCount = ((HOW_MANY_MINIONS+1)*20);
63
64
65 static bool SOSArrayForEachAccount(CFArrayRef accounts, bool (^operation)(SOSAccount* account)) {
66 __block bool retval = true;
67 CFArrayForEach(accounts, ^(const void *value) {
68 SOSAccount* account = (__bridge SOSAccount*) value;
69 retval &= operation(account);
70 });
71 return retval;
72 }
73
74
75 static inline void FeedChangesToMasterMinions(CFMutableDictionaryRef changes, SOSAccount* master_account, CFArrayRef minion_accounts) {
76 FeedChangesTo(changes, master_account);
77 SOSArrayForEachAccount(minion_accounts, ^bool(SOSAccount* account) {
78 FeedChangesTo(changes, account);
79 return true;
80 });
81 FeedChangesTo(changes, master_account);
82
83 }
84
85
86 static inline bool ProcessChangesOnceMasterMinions(CFMutableDictionaryRef changes, SOSAccount* master_account, CFArrayRef minion_accounts) {
87 bool result = FillAllChanges(changes);
88 FeedChangesToMasterMinions(changes, master_account, minion_accounts);
89 return result;
90 }
91
92 static inline int ProcessChangesForMasterAndMinions(CFMutableDictionaryRef changes, SOSAccount* master_account, CFArrayRef minion_accounts) {
93 int result = 0;
94 bool new_data = false;
95 do {
96 new_data = ProcessChangesOnceMasterMinions(changes, master_account, minion_accounts);
97 ++result;
98 } while (new_data);
99 return result;
100 }
101
102 static bool MakeTheBigCircle(CFMutableDictionaryRef changes, SOSAccount* master_account, CFArrayRef minion_accounts, CFErrorRef *error) {
103 bool retval = SOSAccountResetToOffering_wTxn(master_account, error);
104 if(!retval)
105 return retval;
106 ProcessChangesForMasterAndMinions(changes, master_account, minion_accounts);
107 retval = SOSArrayForEachAccount(minion_accounts, ^bool(SOSAccount* 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 CFReleaseNull(applicants);
116 ProcessChangesForMasterAndMinions(changes, master_account, minion_accounts);
117 errOut:
118 return retval;
119 }
120
121
122 static CFArrayRef CreateManyAccountsForLocalChanges(CFStringRef namefmt, CFStringRef data_source_name, size_t howmany)
123 CF_FORMAT_FUNCTION(1, 0);
124
125 static CFArrayRef CreateManyAccountsForLocalChanges(CFStringRef name, CFStringRef data_source_name, size_t howmany) {
126 CFMutableArrayRef accounts = CFArrayCreateMutable(kCFAllocatorDefault, howmany, &kCFTypeArrayCallBacks);
127
128 for(size_t i = 0; i < howmany; i++) {
129 CFStringRef tmpname = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@%ld"), name, (long)i);
130 SOSAccount* tmp = CreateAccountForLocalChanges(tmpname, CFSTR("TestSource"));
131 CFArraySetValueAtIndex(accounts, i, (__bridge const void *)(tmp));
132 CFReleaseNull(tmpname);
133 }
134 return accounts;
135 }
136
137 static bool AssertAllCredentialsAndUpdate(CFMutableDictionaryRef changes, SOSAccount* master_account, CFArrayRef minion_accounts, CFStringRef user_account, CFDataRef user_password, CFErrorRef *error) {
138 __block bool retval = SOSAccountAssertUserCredentialsAndUpdate(master_account, user_account, user_password, error);
139 ProcessChangesForMasterAndMinions(changes, master_account, minion_accounts);
140 retval &= SOSArrayForEachAccount(minion_accounts, ^bool(SOSAccount* account) {
141 CFReleaseNull(*error);
142 return SOSAccountAssertUserCredentialsAndUpdate(account, user_account, user_password, error);
143 });
144 CFReleaseNull(*error);
145
146 return retval;
147 }
148
149 static void timeval_delta(struct timeval *delta, struct timeval *start, struct timeval *end) {
150 if(end->tv_usec >= start->tv_usec) {
151 delta->tv_usec = end->tv_usec - start->tv_usec;
152 } else {
153 end->tv_sec--;
154 end->tv_usec += 1000000;
155 delta->tv_usec = end->tv_usec - start->tv_usec;
156 }
157 delta->tv_sec = end->tv_sec - start->tv_sec;
158 }
159
160 static void reportTime(int peers, void(^action)(void)) {
161 struct rusage start_rusage;
162 struct rusage end_rusage;
163 struct timeval delta_utime;
164 struct timeval delta_stime;
165
166 getrusage(RUSAGE_SELF, &start_rusage);
167 action();
168 getrusage(RUSAGE_SELF, &end_rusage);
169 timeval_delta(&delta_utime, &start_rusage.ru_utime, &end_rusage.ru_utime);
170 timeval_delta(&delta_stime, &start_rusage.ru_stime, &end_rusage.ru_stime);
171
172 diag("AccountLogState for %d peers: %ld.%06d user %ld.%06d system", peers,
173 delta_utime.tv_sec, delta_utime.tv_usec,
174 delta_stime.tv_sec, delta_stime.tv_usec);
175 }
176
177 static void tests(void)
178 {
179 NSError* ns_error = nil;
180 CFErrorRef error = NULL;
181 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
182 CFStringRef cfaccount = CFSTR("test@test.org");
183
184 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
185
186 SOSAccount* master_account = CreateAccountForLocalChanges(CFSTR("master"), CFSTR("TestSource"));
187 CFArrayRef minion_accounts = CreateManyAccountsForLocalChanges(CFSTR("minion"), CFSTR("TestSource"), HOW_MANY_MINIONS);
188
189 ok(AssertAllCredentialsAndUpdate(changes, master_account, minion_accounts, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
190 CFReleaseNull(cfpassword);
191
192 secLogEnable();
193 reportTime(1, ^{
194 SOSAccountLogState(master_account);
195 });
196 secLogDisable();
197
198 ok(MakeTheBigCircle(changes, master_account, minion_accounts, &error), "Get Everyone into the circle %@", error);
199
200 diag("WHAT?");
201 secLogEnable();
202 reportTime(HOW_MANY_MINIONS+1, ^{
203 SOSAccountLogState(master_account);
204 });
205 SOSAccountLogViewState(master_account);
206 secLogDisable();
207
208 NSData* acctData = [master_account encodedData:&ns_error];
209 diag("Account DER Size is %lu for %d peers", (unsigned long)[acctData length], HOW_MANY_MINIONS+1);
210 ns_error = nil;
211
212 SOSAccountTrustClassic* trust = master_account.trust;
213 CFDataRef circleData = SOSCircleCopyEncodedData(trust.trustedCircle, kCFAllocatorDefault, &error);
214 diag("Circle DER Size is %ld for %d peers", CFDataGetLength(circleData), HOW_MANY_MINIONS+1);
215 CFReleaseNull(circleData);
216 CFReleaseNull(error);
217
218 CFDataRef peerData = SOSPeerInfoCopyEncodedData(master_account.peerInfo, kCFAllocatorDefault, &error);
219 diag("Peer DER Size is %ld", CFDataGetLength(peerData));
220 CFReleaseNull(peerData);
221 CFReleaseNull(error);
222
223 CFReleaseNull(error);
224 CFReleaseNull(minion_accounts);
225
226 SOSTestCleanup();
227
228 }
229
230 int secd_200_logstate(int argc, char *const *argv)
231 {
232 plan_tests(kTestTestCount);
233
234 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
235
236 tests();
237
238 return 0;
239 }