]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-56-account-apply.c
Security-57336.1.9.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-56-account-apply.c
1 /*
2 * Copyright (c) 2013-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
26
27 #include <Security/SecBase.h>
28 #include <Security/SecItem.h>
29
30 #include <CoreFoundation/CFDictionary.h>
31
32 #include <Security/SecureObjectSync/SOSAccount.h>
33 #include <Security/SecureObjectSync/SOSCloudCircle.h>
34 #include <Security/SecureObjectSync/SOSInternal.h>
35 #include <Security/SecureObjectSync/SOSUserKeygen.h>
36 #include <Security/SecureObjectSync/SOSTransport.h>
37
38 #include <stdlib.h>
39 #include <unistd.h>
40
41 #include "secd_regressions.h"
42 #include "SOSTestDataSource.h"
43
44 #include "SOSRegressionUtilities.h"
45 #include <utilities/SecCFWrappers.h>
46 #include <Security/SecKeyPriv.h>
47
48 #include <securityd/SOSCloudCircleServer.h>
49
50 #include "SOSAccountTesting.h"
51
52 #include "SecdTestKeychainUtilities.h"
53
54 static int kTestTestCount = 118;
55
56 #if 0
57 static int countPeers(SOSAccountRef account, bool active) {
58 CFErrorRef error = NULL;
59 CFArrayRef peers;
60
61 if(active) peers = SOSAccountCopyActivePeers(account, &error);
62 else peers = SOSAccountCopyPeers(account, &error);
63 int retval = (int) CFArrayGetCount(peers);
64 CFReleaseNull(error);
65 CFReleaseNull(peers);
66 return retval;
67 }
68 #endif
69
70 static void tests(void)
71 {
72 CFErrorRef error = NULL;
73 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
74 CFStringRef cfaccount = CFSTR("test@test.org");
75
76 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
77
78 SOSAccountRef alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
79 SOSAccountRef bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
80 SOSAccountRef carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
81 SOSAccountRef david_account = CreateAccountForLocalChanges(CFSTR("David"), CFSTR("TestSource"));
82
83 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
84
85 // Bob wins writing at this point, feed the changes back to alice.
86 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
87
88 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
89 CFReleaseNull(error);
90
91 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
92 CFReleaseNull(error);
93
94 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
95 CFReleaseNull(cfpassword);
96 CFReleaseNull(error);
97
98 ok(SOSAccountResetToOffering(alice_account, &error), "Reset to offering (%@)", error);
99 CFReleaseNull(error);
100
101 // Lost Application Scenario
102 is(ProcessChangesOnce(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
103
104 ok(SOSAccountJoinCircles(bob_account, &error), "Bob Applies (%@)", error);
105 CFReleaseNull(error);
106
107 ok(SOSAccountJoinCircles(carole_account, &error), "Carole Applies too (%@)", error);
108 CFReleaseNull(error);
109
110 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
111
112 accounts_agree("alice and carole agree", alice_account, carole_account);
113 accounts_agree("alice and bob agree", alice_account, bob_account);
114
115 {
116 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
117
118 ok(applicants && CFArrayGetCount(applicants) == 2, "See two applicants %@ (%@)", applicants, error);
119 CFReleaseNull(error);
120 CFReleaseSafe(applicants);
121 }
122
123 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
124
125 accounts_agree("alice and carole agree", alice_account, carole_account);
126
127 CFReleaseNull(error);
128
129 {
130 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
131 ok(applicants && CFArrayGetCount(applicants) == 2, "See two applicants %@ (%@)", applicants, error);
132 ok(SOSAccountRejectApplicants(alice_account, applicants, &error), "Everyone out the pool");
133 CFReleaseNull(error);
134 CFReleaseSafe(applicants);
135 }
136
137 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
138
139 accounts_agree("alice and carole agree", alice_account, carole_account);
140
141 {
142 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
143 ok(applicants && CFArrayGetCount(applicants) == 0, "See no applicants %@ (%@)", applicants, error);
144 CFReleaseNull(error);
145 CFReleaseSafe(applicants);
146 }
147
148 ok(SOSAccountJoinCircles(bob_account, &error), "Bob asks again");
149 CFReleaseNull(error);
150
151 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
152
153 {
154 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
155 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicants %@ (%@)", applicants, error);
156 CFReleaseNull(error);
157 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Accept bob into the fold");
158 CFReleaseNull(error);
159 CFReleaseSafe(applicants);
160 }
161
162 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
163
164 #if 0
165
166 {
167 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
168
169 ok(applicants && CFArrayGetCount(applicants) == 1, "Bob automatically re-applied %@ (%@)", applicants, error);
170 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
171 CFReleaseNull(error);
172 CFReleaseNull(applicants);
173 }
174
175 is(countPeers(alice_account, 0), 3, "Bob is accepted after auto-reapply");
176
177 FillAllChanges(changes);
178 FeedChangesToMulti(AliceChanges, bob_account, carole_account, david_account, NULL);
179 FeedChangesToMulti(BobChanges, alice_account, carole_account, david_account, NULL);
180 FeedChangesToMulti(CarolChanges, bob_account, alice_account, david_account, NULL);
181 FeedChangesToMulti(DavidChanges, bob_account, alice_account, carole_account, NULL);
182
183 FillAllChanges(changes);
184 FeedChangesToMulti(AliceChanges, bob_account, carole_account, david_account, NULL);
185 FeedChangesToMulti(BobChanges, alice_account, carole_account, david_account, NULL);
186 FeedChangesToMulti(CarolChanges, bob_account, alice_account, david_account, NULL);
187 FeedChangesToMulti(DavidChanges, bob_account, alice_account, carole_account, NULL);
188
189 FillAllChanges(changes);
190 FeedChangesToMulti(AliceChanges, bob_account, carole_account, david_account, NULL);
191 FeedChangesToMulti(BobChanges, alice_account, carole_account, david_account, NULL);
192 FeedChangesToMulti(CarolChanges, bob_account, alice_account, david_account, NULL);
193 FeedChangesToMulti(DavidChanges, bob_account, alice_account, carole_account, NULL);
194 accounts_agree("alice and carole agree after bob gets in", alice_account, carole_account);
195
196 // Rejected Application Scenario
197 ok(SOSAccountJoinCircles(david_account, &error), "Dave Applies (%@)", error);
198 CFReleaseNull(error);
199
200 FillAllChanges(changes);
201 FeedChangesToMulti(AliceChanges, bob_account, carole_account, david_account, NULL);
202 FeedChangesToMulti(BobChanges, alice_account, carole_account, david_account, NULL);
203 FeedChangesToMulti(CarolChanges, bob_account, alice_account, david_account, NULL);
204 FeedChangesToMulti(DavidChanges, bob_account, alice_account, carole_account, NULL);
205
206 SOSAccountPurgePrivateCredential(alice_account);
207
208 {
209 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
210
211 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
212 ok(SOSAccountRejectApplicants(alice_account, applicants, &error), "Alice rejects (%@)", error);
213 CFReleaseNull(error);
214 CFReleaseNull(applicants);
215 }
216
217 FillAllChanges(changes);
218 FeedChangesToMulti(AliceChanges, bob_account, carole_account, david_account, NULL);
219 FeedChangesToMulti(BobChanges, alice_account, carole_account, david_account, NULL);
220 FeedChangesToMulti(CarolChanges, bob_account, alice_account, david_account, NULL);
221 FeedChangesToMulti(DavidChanges, bob_account, alice_account, carole_account, NULL);
222
223 FillAllChanges(changes);
224 FeedChangesToMulti(AliceChanges, bob_account, carole_account, david_account, NULL);
225 FeedChangesToMulti(BobChanges, alice_account, carole_account, david_account, NULL);
226 FeedChangesToMulti(CarolChanges, bob_account, alice_account, david_account, NULL);
227 FeedChangesToMulti(DavidChanges, bob_account, alice_account, carole_account, NULL);
228
229 FillAllChanges(changes);
230 FeedChangesToMulti(AliceChanges, bob_account, carole_account, david_account, NULL);
231 FeedChangesToMulti(BobChanges, alice_account, carole_account, david_account, NULL);
232 FeedChangesToMulti(CarolChanges, bob_account, alice_account, david_account, NULL);
233 FeedChangesToMulti(DavidChanges, bob_account, alice_account, carole_account, NULL);
234
235 accounts_agree("alice and carole still agree after david is rejected", alice_account, carole_account);
236 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
237 CFReleaseNull(error);
238
239 FillAllChanges(changes);
240 FeedChangesToMulti(AliceChanges, bob_account, carole_account, david_account, NULL);
241 FeedChangesToMulti(BobChanges, alice_account, carole_account, david_account, NULL);
242 FeedChangesToMulti(CarolChanges, bob_account, alice_account, david_account, NULL);
243 FeedChangesToMulti(DavidChanges, bob_account, alice_account, carole_account, NULL);
244
245 FillAllChanges(changes);
246
247 ok(CFDictionaryGetCount(CarolChanges) == 0, "We converged. (%@)", CarolChanges);
248 ok(CFDictionaryGetCount(BobChanges) == 0, "We converged. (%@)", BobChanges);
249 ok(CFDictionaryGetCount(AliceChanges) == 0, "We converged. (%@)", AliceChanges);
250 ok(CFDictionaryGetCount(DavidChanges) == 0, "We converged. (%@)", DavidChanges);
251
252 accounts_agree("bob&alice pair", bob_account, alice_account);
253
254 ok(SOSAccountJoinCirclesAfterRestore(carole_account, &error), "Carole cloud identiy joins (%@)", error);
255 CFReleaseNull(error);
256
257 is(countPeers(carole_account, false), 3, "Carole sees 3 valid peers after sliding in");
258
259 FillAllChanges(changes);
260 FeedChangesToMulti(AliceChanges, bob_account, carole_account, david_account, NULL);
261 FeedChangesToMulti(BobChanges, alice_account, carole_account, david_account, NULL);
262 FeedChangesToMulti(CarolChanges, bob_account, alice_account, david_account, NULL);
263 FeedChangesToMulti(DavidChanges, bob_account, alice_account, carole_account, NULL);
264
265 FillAllChanges(changes);
266 FeedChangesToMulti(AliceChanges, bob_account, carole_account, david_account, NULL);
267 FeedChangesToMulti(BobChanges, alice_account, carole_account, david_account, NULL);
268 FeedChangesToMulti(CarolChanges, bob_account, alice_account, david_account, NULL);
269 FeedChangesToMulti(DavidChanges, bob_account, alice_account, carole_account, NULL);
270
271 FillAllChanges(changes);
272 FeedChangesToMulti(AliceChanges, bob_account, carole_account, david_account, NULL);
273 FeedChangesToMulti(BobChanges, alice_account, carole_account, david_account, NULL);
274 FeedChangesToMulti(CarolChanges, bob_account, alice_account, david_account, NULL);
275 FeedChangesToMulti(DavidChanges, bob_account, alice_account, carole_account, NULL); // Bob and carole see the final result.
276
277 accounts_agree_internal("Carole's in", bob_account, alice_account, false);
278 accounts_agree_internal("Carole's in - 2", bob_account, carole_account, false);
279 #endif
280 CFReleaseNull(bob_account);
281 CFReleaseNull(alice_account);
282 CFReleaseNull(carole_account);
283 CFReleaseNull(david_account);
284
285 SOSUnregisterAllTransportMessages();
286 SOSUnregisterAllTransportCircles();
287 SOSUnregisterAllTransportKeyParameters();
288 CFArrayRemoveAllValues(key_transports);
289 CFArrayRemoveAllValues(circle_transports);
290 CFArrayRemoveAllValues(message_transports);
291
292 }
293
294 int secd_56_account_apply(int argc, char *const *argv)
295 {
296 plan_tests(kTestTestCount);
297
298 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
299
300 tests();
301
302 return 0;
303 }