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