2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <Security/SecBase.h>
28 #include <Security/SecItem.h>
30 #include <CoreFoundation/CFDictionary.h>
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"
43 #include "secd_regressions.h"
44 #include "SOSTestDataSource.h"
46 #include "SOSRegressionUtilities.h"
47 #include <utilities/SecCFWrappers.h>
48 #include <Security/SecKeyPriv.h>
50 #include <securityd/SOSCloudCircleServer.h>
52 #include "SOSAccountTesting.h"
54 #include "SecdTestKeychainUtilities.h"
56 static int kTestTestCount = 324;
58 static void tests(void)
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");
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"));
70 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
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");
75 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
77 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential trying (%@)", 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");
84 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
87 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
89 ok(SOSAccountHasCompletedInitialSync(alice_account), "Alice thinks she's completed initial sync");
91 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
94 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
97 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
105 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
107 accounts_agree("bob&alice pair", bob_account, alice_account);
109 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
110 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
111 CFReleaseNull(peers);
113 ok(!SOSAccountHasCompletedInitialSync(bob_account), "Bob thinks he hasn't completed initial sync");
115 CFDictionaryRef alice_new_gestalt = SOSCreatePeerGestaltFromName(CFSTR("Alice, but different"));
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);
121 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
123 accounts_agree("Alice's name changed", bob_account, alice_account);
125 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
126 CFReleaseNull(error);
128 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
130 accounts_agree("Alice bails", bob_account, alice_account);
132 peers = SOSAccountCopyPeers(alice_account, &error);
133 ok(peers && CFArrayGetCount(peers) == 1, "See one peer %@ (%@)", peers, error);
134 CFReleaseNull(peers);
136 ok(SOSAccountJoinCircles_wTxn(alice_account, &error), "Alice re-applies (%@)", error);
137 CFReleaseNull(error);
139 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
143 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
151 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
153 accounts_agree("Alice accepts' Bob", bob_account, alice_account);
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);
160 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
163 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
171 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
173 accounts_agree("Bob accepts Alice", bob_account, alice_account);
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.
180 // Write race emulation.
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
189 accounts_agree("Alice leaves & returns", bob_account, alice_account);
191 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
192 CFReleaseNull(error);
194 FeedChangesTo(changes, bob_account); // Bob sees Alice Applying
197 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
205 CFMutableDictionaryRef bobAcceptanceChanges = ExtractPendingChanges(changes);
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);
213 CFReleaseSafe(ExtractPendingChanges(changes)); // Alice loses the race to write her changes - bob never sees em.
215 FeedChangesTo(&bobAcceptanceChanges, alice_account); // Alice sees bob inviting her in the circle.
217 FeedChangesTo(changes, bob_account); // Bob sees Alice Concurring
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);
225 // Emulation of <rdar://problem/13919554> Innsbruck11A368 +Roots: Device A was removed when Device B joined.
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.
229 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice leaves once more (%@)", error);
230 CFReleaseNull(error);
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);
235 ok(SOSAccountJoinCircles_wTxn(alice_account, &error), "Alice re-applies (%@)", error);
236 CFReleaseNull(error);
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.
241 CFReleaseNull(error);
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);
246 ok(SOSAccountJoinCircles_wTxn(alice_account, &error), "Alice re-applies (%@)", error);
247 CFReleaseNull(error);
249 FeedChangesTo(changes, bob_account); // Bob sees Alice reapply.
252 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
259 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
261 accounts_agree("Alice comes back", bob_account, alice_account);
263 // Emulation of <rdar://problem/13889901>
264 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
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);
271 CFMutableDictionaryRef dropped_changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
272 FillChanges(dropped_changes, carol_account);
273 CFReleaseNull(dropped_changes);
275 ok(SOSAccountResetToOffering_wTxn(carol_account, &error), "Reset to offering (%@)", error);
276 CFReleaseNull(error);
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.");
282 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob ReApplies (%@)", error);
283 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 2, "updates");
285 CFArrayRef applicants = SOSAccountCopyApplicants(carol_account, &error);
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);
292 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 3, "updates");
293 accounts_agree("rdar://problem/13889901-II", bob_account, carol_account);
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");
298 // Test multiple removal, including our own departure via that API
299 ok(SOSAccountResetToOffering_wTxn(alice_account, NULL), "Reset to offering");
301 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "Reset propogation");
303 ok(SOSAccountJoinCircles_wTxn(bob_account, NULL), "bob joins again");
305 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "Bob request");
308 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
316 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "carol request");
318 ok(SOSAccountJoinCircles_wTxn(carol_account, NULL), "carol joins again");
320 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "carol request");
322 CFArrayRef peers_to_remove_array = CFArrayCreateForCFTypes(kCFAllocatorDefault,
323 bob_account.peerInfo,
324 carol_account.peerInfo,
327 ok(SOSAccountRemovePeersFromCircle(bob_account, peers_to_remove_array, NULL));
329 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "Remove peers");
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");
336 CFReleaseNull(peers_to_remove_array);
338 CFReleaseNull(alice_new_gestalt);
346 int secd_55_account_circle(int argc, char *const *argv)
348 plan_tests(kTestTestCount);
350 secd_test_setup_temp_keychain(__FUNCTION__, NULL);