]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-55-account-circle.c
Security-57336.10.29.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-55-account-circle.c
1 /*
2 * Copyright (c) 2012-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 = 324;
55
56 static void tests(void)
57 {
58 CFErrorRef error = NULL;
59 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
60 CFDataRef cfwrong_password = CFDataCreate(NULL, (uint8_t *) "NotFooFooFoo", 10);
61 CFStringRef cfaccount = CFSTR("test@test.org");
62
63 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
64 SOSAccountRef alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
65 SOSAccountRef bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
66 SOSAccountRef carol_account = CreateAccountForLocalChanges(CFSTR("Carol"), CFSTR("TestSource"));
67
68 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
69
70 // Bob wins writing at this point, feed the changes back to alice.
71 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
72
73 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
74 CFReleaseNull(error);
75 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential trying (%@)", error);
76 CFReleaseNull(error);
77 ok(!SOSAccountTryUserCredentials(alice_account, cfaccount, cfwrong_password, &error), "Credential failing (%@)", error);
78 CFReleaseNull(cfwrong_password);
79 is(error ? CFErrorGetCode(error) : 0, kSOSErrorWrongPassword, "Expected SOSErrorWrongPassword");
80 CFReleaseNull(error);
81
82 ok(SOSAccountResetToOffering(alice_account, &error), "Reset to offering (%@)", error);
83 CFReleaseNull(error);
84
85 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
86
87 ok(SOSAccountJoinCircles(bob_account, &error), "Bob Applies (%@)", error);
88 CFReleaseNull(error);
89
90 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
91
92 {
93 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
94
95 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
96 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
97 CFReleaseNull(error);
98 CFReleaseNull(applicants);
99 }
100
101 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
102
103 accounts_agree("bob&alice pair", bob_account, alice_account);
104
105 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
106 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
107 CFReleaseNull(peers);
108
109 CFDictionaryRef alice_new_gestalt = SOSCreatePeerGestaltFromName(CFSTR("Alice, but different"));
110
111 ok(SOSAccountUpdateGestalt(alice_account, alice_new_gestalt), "Update gestalt %@ (%@)", alice_account, error);
112 SOSAccountUpdateTestTransports(alice_account, alice_new_gestalt);
113 CFReleaseNull(alice_new_gestalt);
114
115 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
116
117 accounts_agree("Alice's name changed", bob_account, alice_account);
118
119 ok(SOSAccountLeaveCircle(alice_account, &error), "Alice Leaves (%@)", error);
120 CFReleaseNull(error);
121
122 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
123
124 accounts_agree("Alice bails", bob_account, alice_account);
125
126 peers = SOSAccountCopyPeers(alice_account, &error);
127 ok(peers && CFArrayGetCount(peers) == 1, "See one peer %@ (%@)", peers, error);
128 CFReleaseNull(peers);
129
130 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
131 CFReleaseNull(error);
132
133 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
134
135
136 {
137 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
138
139 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
140 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
141 CFReleaseNull(error);
142 CFReleaseNull(applicants);
143 }
144
145 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
146
147 accounts_agree("Alice accepts' Bob", bob_account, alice_account);
148
149 ok(SOSAccountLeaveCircle(alice_account, &error), "Alice Leaves (%@)", error);
150 CFReleaseNull(error);
151 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
152 CFReleaseNull(error);
153
154 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
155
156 {
157 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
158
159 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
160 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
161 CFReleaseNull(error);
162 CFReleaseNull(applicants);
163 }
164
165 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
166
167 accounts_agree("Bob accepts Alice", bob_account, alice_account);
168
169 // As of PR-13917727/PR-13906870 this no longer works (by "design"), in favor of making another more common
170 // failure (apply/OSX-psudo-reject/re-apply) work. Write races might be "fixed better" with affirmitave rejection.
171 #if 0
172
173 //
174 // Write race emulation.
175 //
176 //
177
178 ok(SOSAccountLeaveCircle(alice_account, &error), "Alice Leaves (%@)", error);
179 CFReleaseNull(error);
180 FeedChangesTo(changes, bob_account); // Bob sees Alice leaving and rejoining
181 FeedChangesTo(changes, alice_account); // Alice sees bob concurring
182
183 accounts_agree("Alice leaves & returns", bob_account, alice_account);
184
185 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
186 CFReleaseNull(error);
187
188 FeedChangesTo(changes, bob_account); // Bob sees Alice Applying
189
190 {
191 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
192
193 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
194 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
195 CFReleaseNull(error);
196 CFReleaseNull(applicants);
197 }
198
199 CFMutableDictionaryRef bobAcceptanceChanges = ExtractPendingChanges(changes);
200
201 // Alice re-applies without seeing that she was accepted.
202 ok(SOSAccountLeaveCircles(alice_account, &error), "Alice Leaves again (%@)", error);
203 CFReleaseNull(error);
204 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
205 CFReleaseNull(error);
206
207 CFReleaseSafe(ExtractPendingChanges(changes)); // Alice loses the race to write her changes - bob never sees em.
208
209 FeedChangesTo(&bobAcceptanceChanges, alice_account); // Alice sees bob inviting her in the circle.
210
211 FeedChangesTo(changes, bob_account); // Bob sees Alice Concurring
212
213 // As of PR-13917727/PR-13906870
214 accounts_agree("Alice leave, applies back, loses a race and eventually gets in", bob_account, alice_account);
215 #endif
216
217 // Both in circle.
218
219 // Emulation of <rdar://problem/13919554> Innsbruck11A368 +Roots: Device A was removed when Device B joined.
220
221 // We want Alice to leave circle while an Applicant on a full concordance signed circle with old-Alice as an Alum and Bob a peer.
222 // ZZZ
223 ok(SOSAccountLeaveCircle(alice_account, &error), "Alice leaves once more (%@)", error);
224 CFReleaseNull(error);
225
226 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
227 accounts_agree("Alice and Bob see Alice out of circle", bob_account, alice_account);
228
229 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
230 CFReleaseNull(error);
231
232 ok(SOSAccountLeaveCircle(alice_account, &error), "Alice leaves while applying (%@)", error);
233 FeedChangesTo(changes, bob_account); // Bob sees Alice become an Alum.
234
235 CFReleaseNull(error);
236
237 is(SOSAccountGetCircleStatus(alice_account, &error), kSOSCCNotInCircle, "Alice isn't applying any more");
238 accounts_agree("Alice leaves & some fancy concordance stuff happens", bob_account, alice_account);
239
240 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
241 CFReleaseNull(error);
242
243 FeedChangesTo(changes, bob_account); // Bob sees Alice reapply.
244
245 {
246 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
247
248 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
249 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
250 CFReleaseNull(error);
251 CFReleaseNull(applicants);
252 }
253 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
254
255 accounts_agree("Alice comes back", bob_account, alice_account);
256
257 // Emulation of <rdar://problem/13889901>
258 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
259
260 ok(SOSAccountAssertUserCredentialsAndUpdate(carol_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
261 CFReleaseNull(cfpassword);
262 ok(SOSAccountJoinCircles(carol_account, &error), "Carol Applies (%@)", error);
263 CFReleaseNull(error);
264
265 CFMutableDictionaryRef dropped_changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
266 FillChanges(dropped_changes, carol_account);
267 CFReleaseNull(dropped_changes);
268
269 ok(SOSAccountResetToOffering(carol_account, &error), "Reset to offering (%@)", error);
270 CFReleaseNull(error);
271
272 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 2, "updates");
273 accounts_agree("13889901", carol_account, bob_account);
274 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSMembershipRevoked, "Bob affirms he hasn't left.");
275
276 ok(SOSAccountJoinCircles(bob_account, &error), "Bob ReApplies (%@)", error);
277 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 2, "updates");
278 {
279 CFArrayRef applicants = SOSAccountCopyApplicants(carol_account, &error);
280
281 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
282 ok(SOSAccountAcceptApplicants(carol_account, applicants, &error), "Carol accepts (%@)", error);
283 CFReleaseNull(error);
284 CFReleaseNull(applicants);
285 }
286 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 3, "updates");
287 accounts_agree("rdar://problem/13889901-II", bob_account, carol_account);
288
289 // Alice has been out of the loop, bring her back up to speed before changing things (to avoid gen count race)
290 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "Reset propogation");
291
292 // Test multiple removal, including our own departure via that API
293 ok(SOSAccountResetToOffering(alice_account, NULL), "Reset to offering");
294
295 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "Reset propogation");
296
297 ok(SOSAccountJoinCircles(bob_account, NULL), "bob joins again");
298
299 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "Bob request");
300
301 {
302 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
303
304 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
305 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
306 CFReleaseNull(error);
307 CFReleaseNull(applicants);
308 }
309
310 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "carol request");
311
312 ok(SOSAccountJoinCircles(carol_account, NULL), "carol joins again");
313
314 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "carol request");
315
316 CFArrayRef peers_to_remove_array = CFArrayCreateForCFTypes(kCFAllocatorDefault,
317 SOSAccountGetMyPeerInfo(bob_account),
318 SOSAccountGetMyPeerInfo(carol_account),
319 NULL);
320
321 ok(SOSAccountRemovePeersFromCircle(bob_account, peers_to_remove_array, NULL));
322
323 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "Remove peers");
324
325 ok(SOSAccountIsInCircle(alice_account, NULL), "Alice still here");
326 ok(!SOSAccountIsInCircle(bob_account, NULL), "Bob not in circle");
327 // Carol's not in circle, but reapplied, as she's persistent until positive rejection.
328 ok(!SOSAccountIsInCircle(carol_account, NULL), "carol not in circle");
329
330 CFReleaseNull(peers_to_remove_array);
331
332 CFReleaseNull(alice_new_gestalt);
333
334 CFReleaseNull(bob_account);
335 CFReleaseNull(alice_account);
336 CFReleaseNull(carol_account);
337
338 SOSUnregisterAllTransportMessages();
339 SOSUnregisterAllTransportCircles();
340 SOSUnregisterAllTransportKeyParameters();
341 CFArrayRemoveAllValues(key_transports);
342 CFArrayRemoveAllValues(circle_transports);
343 CFArrayRemoveAllValues(message_transports);
344
345 }
346
347 int secd_55_account_circle(int argc, char *const *argv)
348 {
349 plan_tests(kTestTestCount);
350
351 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
352
353 tests();
354
355 return 0;
356 }