]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-200-logstate.c
Security-57740.60.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
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 = (5+(HOW_MANY_MINIONS+1)*20);
63
64
65 static bool SOSArrayForEachAccount(CFArrayRef accounts, bool (^operation)(SOSAccountRef account)) {
66 __block bool retval = true;
67 CFArrayForEach(accounts, ^(const void *value) {
68 SOSAccountRef account = (SOSAccountRef) value;
69 retval &= operation(account);
70 });
71 return retval;
72 }
73
74
75 static inline void FeedChangesToMasterMinions(CFMutableDictionaryRef changes, SOSAccountRef master_account, CFArrayRef minion_accounts) {
76 FeedChangesTo(changes, master_account);
77 SOSArrayForEachAccount(minion_accounts, ^bool(SOSAccountRef 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, SOSAccountRef 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, SOSAccountRef 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, SOSAccountRef master_account, CFArrayRef minion_accounts, CFErrorRef *error) {
103 bool retval = SOSAccountResetToOffering_wTxn(master_account, error);
104 require_quiet(retval, errOut);
105 ProcessChangesForMasterAndMinions(changes, master_account, minion_accounts);
106 retval = SOSArrayForEachAccount(minion_accounts, ^bool(SOSAccountRef account) {
107 bool localret = SOSAccountJoinCircles_wTxn(account, error);
108 ProcessChangesForMasterAndMinions(changes, master_account, minion_accounts);
109 return localret;
110 });
111 require_quiet(retval, errOut);
112 CFArrayRef applicants = SOSAccountCopyApplicants(master_account, error);
113 retval = SOSAccountAcceptApplicants(master_account , applicants, error);
114 ProcessChangesForMasterAndMinions(changes, master_account, minion_accounts);
115 errOut:
116 return retval;
117 }
118
119
120 static CFArrayRef CreateManyAccountsForLocalChanges(CFStringRef namefmt, CFStringRef data_source_name, size_t howmany) {
121 CFMutableArrayRef accounts = CFArrayCreateMutable(kCFAllocatorDefault, howmany, &kCFTypeArrayCallBacks);
122
123 for(size_t i = 0; i < howmany; i++) {
124 CFStringRef tmpname = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, namefmt, i);
125 SOSAccountRef tmp = CreateAccountForLocalChanges(tmpname, CFSTR("TestSource"));
126 CFArraySetValueAtIndex(accounts, i, tmp);
127 CFReleaseNull(tmpname);
128 CFReleaseNull(tmp);
129 }
130 return accounts;
131 }
132
133 static bool AssertAllCredentialsAndUpdate(CFMutableDictionaryRef changes, SOSAccountRef master_account, CFArrayRef minion_accounts, CFStringRef user_account, CFDataRef user_password, CFErrorRef *error) {
134 __block bool retval = SOSAccountAssertUserCredentialsAndUpdate(master_account, user_account, user_password, error);
135 ProcessChangesForMasterAndMinions(changes, master_account, minion_accounts);
136 retval &= SOSArrayForEachAccount(minion_accounts, ^bool(SOSAccountRef account) {
137 CFReleaseNull(*error);
138 return SOSAccountAssertUserCredentialsAndUpdate(account, user_account, user_password, error);
139 });
140 CFReleaseNull(*error);
141
142 return retval;
143 }
144
145 static void tests(void)
146 {
147 CFErrorRef error = NULL;
148 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
149 CFStringRef cfaccount = CFSTR("test@test.org");
150
151 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
152
153 SOSAccountRef master_account = CreateAccountForLocalChanges(CFSTR("master"), CFSTR("TestSource"));
154 CFArrayRef minion_accounts = CreateManyAccountsForLocalChanges(CFSTR("minion%d"), CFSTR("TestSource"), HOW_MANY_MINIONS);
155
156 ok(AssertAllCredentialsAndUpdate(changes, master_account, minion_accounts, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
157
158 secLogEnable();
159 SOSAccountLogState(master_account);
160 secLogDisable();
161
162 ok(MakeTheBigCircle(changes, master_account, minion_accounts, &error), "Get Everyone into the circle %@", error);
163
164 diag("WHAT?");
165 secLogEnable();
166 SOSAccountLogState(master_account);
167 SOSAccountLogViewState(master_account);
168 secLogDisable();
169
170 CFDataRef acctData = SOSAccountCopyEncodedData(master_account, kCFAllocatorDefault, &error);
171 diag("Account DER Size is %d for %d peers", CFDataGetLength(acctData), HOW_MANY_MINIONS+1);
172 CFReleaseNull(acctData);
173 CFReleaseNull(error);
174
175 CFDataRef circleData = SOSCircleCopyEncodedData(master_account->trusted_circle, kCFAllocatorDefault, &error);
176 diag("Circle DER Size is %d for %d peers", CFDataGetLength(circleData), HOW_MANY_MINIONS+1);
177 CFReleaseNull(circleData);
178 CFReleaseNull(error);
179
180 CFDataRef peerData = SOSPeerInfoCopyEncodedData(SOSAccountGetMyPeerInfo(master_account), kCFAllocatorDefault, &error);
181 diag("Peer DER Size is %d", CFDataGetLength(peerData));
182 CFReleaseNull(peerData);
183 CFReleaseNull(error);
184
185 CFReleaseNull(error);
186 CFReleaseNull(master_account);
187 CFReleaseNull(minion_accounts);
188
189 SOSTestCleanup();
190
191 }
192
193 int secd_200_logstate(int argc, char *const *argv)
194 {
195 plan_tests(kTestTestCount);
196
197 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
198
199 tests();
200
201 return 0;
202 }