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