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 "keychain/SecureObjectSync/SOSAccount.h"
33 #include <Security/SecureObjectSync/SOSCloudCircle.h>
34 #include "keychain/SecureObjectSync/SOSInternal.h"
35 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
36 #include "keychain/SecureObjectSync/SOSTransport.h"
37 #import "keychain/SecureObjectSync/SOSAccountTrustClassic+Expansion.h"
38 #import "keychain/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 "keychain/securityd/SOSCloudCircleServer.h"
52 #include "SOSAccountTesting.h"
54 #include "SecdTestKeychainUtilities.h"
56 static int kTestTestCount = 257;
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.
250 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
253 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
255 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
256 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
257 CFReleaseNull(error);
258 CFReleaseNull(applicants);
260 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
262 accounts_agree("Alice comes back", bob_account, alice_account);
264 // Emulation of <rdar://problem/13889901>
265 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
267 ok(SOSAccountAssertUserCredentialsAndUpdate(carol_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
268 CFReleaseNull(cfpassword);
269 ok(SOSAccountJoinCircles_wTxn(carol_account, &error), "Carol Applies (%@)", error);
270 CFReleaseNull(error);
272 CFMutableDictionaryRef dropped_changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
273 FillChanges(dropped_changes, carol_account);
274 CFReleaseNull(dropped_changes);
276 ok(SOSAccountResetToOffering_wTxn(carol_account, &error), "Reset to offering (%@)", error);
277 CFReleaseNull(error);
279 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 2, "updates");
280 accounts_agree("13889901", carol_account, bob_account);
281 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSMembershipRevoked, "Bob affirms he hasn't left.");
283 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob ReApplies (%@)", error);
284 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 2, "updates");
286 CFArrayRef applicants = SOSAccountCopyApplicants(carol_account, &error);
288 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
289 ok(SOSAccountAcceptApplicants(carol_account, applicants, &error), "Carol accepts (%@)", error);
290 CFReleaseNull(error);
291 CFReleaseNull(applicants);
293 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 3, "updates");
294 accounts_agree("rdar://problem/13889901-II", bob_account, carol_account);
296 // Alice has been out of the loop, bring her back up to speed before changing things (to avoid gen count race)
297 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "Reset propogation");
299 // Test multiple removal, including our own departure via that API
300 ok(SOSAccountResetToOffering_wTxn(alice_account, NULL), "Reset to offering");
302 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "Reset propogation");
304 ok(SOSAccountJoinCircles_wTxn(bob_account, NULL), "bob joins again");
306 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "Bob request");
309 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
311 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
312 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
313 CFReleaseNull(error);
314 CFReleaseNull(applicants);
317 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "carol request");
319 ok(SOSAccountJoinCircles_wTxn(carol_account, NULL), "carol joins again");
321 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "carol request");
323 CFArrayRef peers_to_remove_array = CFArrayCreateForCFTypes(kCFAllocatorDefault,
324 bob_account.peerInfo,
325 carol_account.peerInfo,
328 ok(SOSAccountRemovePeersFromCircle(bob_account, peers_to_remove_array, nil, NULL));
330 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "Remove peers");
332 ok([alice_account isInCircle:&error], "Alice still here");
333 ok(![bob_account isInCircle:&error], "Bob not in circle");
334 // Carol's not in circle, but reapplied, as she's persistent until positive rejection.
335 ok(![carol_account isInCircle:NULL], "carol not in circle");
337 CFReleaseNull(peers_to_remove_array);
339 CFReleaseNull(alice_new_gestalt);
347 int secd_55_account_circle(int argc, char *const *argv)
349 plan_tests(kTestTestCount);
351 secd_test_setup_temp_keychain(__FUNCTION__, NULL);