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"
58 static int kTestTestCount = 257;
60 static void tests(void)
62 CFErrorRef error = NULL;
63 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
64 CFDataRef cfwrong_password = CFDataCreate(NULL, (uint8_t *) "NotFooFooFoo", 10);
65 CFStringRef cfaccount = CFSTR("test@test.org");
67 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
68 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
69 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
70 SOSAccount* carol_account = CreateAccountForLocalChanges(CFSTR("Carol"), CFSTR("TestSource"));
72 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
74 // Bob wins writing at this point, feed the changes back to alice.
75 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
77 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
79 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential trying (%@)", error);
81 ok(!SOSAccountTryUserCredentials(alice_account, cfaccount, cfwrong_password, &error), "Credential failing (%@)", error);
82 CFReleaseNull(cfwrong_password);
83 is(error ? CFErrorGetCode(error) : 0, kSOSErrorWrongPassword, "Expected SOSErrorWrongPassword");
86 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
89 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
91 ok(SOSAccountHasCompletedInitialSync(alice_account), "Alice thinks she's completed initial sync");
93 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
96 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
99 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
101 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
102 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
103 CFReleaseNull(error);
104 CFReleaseNull(applicants);
107 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
109 accounts_agree("bob&alice pair", bob_account, alice_account);
111 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
112 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
113 CFReleaseNull(peers);
115 ok(!SOSAccountHasCompletedInitialSync(bob_account), "Bob thinks he hasn't completed initial sync");
117 CFDictionaryRef alice_new_gestalt = SOSCreatePeerGestaltFromName(CFSTR("Alice, but different"));
119 ok([alice_account.trust updateGestalt:alice_account newGestalt:alice_new_gestalt], "Update gestalt %@ (%@)", alice_account, error);
120 SOSAccountUpdateTestTransports(alice_account, alice_new_gestalt);
121 CFReleaseNull(alice_new_gestalt);
123 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
125 accounts_agree("Alice's name changed", bob_account, alice_account);
127 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
128 CFReleaseNull(error);
130 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
132 accounts_agree("Alice bails", bob_account, alice_account);
134 peers = SOSAccountCopyPeers(alice_account, &error);
135 ok(peers && CFArrayGetCount(peers) == 1, "See one peer %@ (%@)", peers, error);
136 CFReleaseNull(peers);
138 ok(SOSAccountJoinCircles_wTxn(alice_account, &error), "Alice re-applies (%@)", error);
139 CFReleaseNull(error);
141 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
145 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
147 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
148 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
149 CFReleaseNull(error);
150 CFReleaseNull(applicants);
153 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
155 accounts_agree("Alice accepts' Bob", bob_account, alice_account);
157 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
158 CFReleaseNull(error);
159 ok(SOSAccountJoinCircles_wTxn(alice_account, &error), "Alice re-applies (%@)", error);
160 CFReleaseNull(error);
162 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
165 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
167 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
168 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
169 CFReleaseNull(error);
170 CFReleaseNull(applicants);
173 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
175 accounts_agree("Bob accepts Alice", bob_account, alice_account);
177 // As of PR-13917727/PR-13906870 this no longer works (by "design"), in favor of making another more common
178 // failure (apply/OSX-psudo-reject/re-apply) work. Write races might be "fixed better" with affirmitave rejection.
182 // Write race emulation.
186 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
187 CFReleaseNull(error);
188 FeedChangesTo(changes, bob_account); // Bob sees Alice leaving and rejoining
189 FeedChangesTo(changes, alice_account); // Alice sees bob concurring
191 accounts_agree("Alice leaves & returns", bob_account, alice_account);
193 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
194 CFReleaseNull(error);
196 FeedChangesTo(changes, bob_account); // Bob sees Alice Applying
199 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
201 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
202 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
203 CFReleaseNull(error);
204 CFReleaseNull(applicants);
207 CFMutableDictionaryRef bobAcceptanceChanges = ExtractPendingChanges(changes);
209 // Alice re-applies without seeing that she was accepted.
210 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves again (%@)", error);
211 CFReleaseNull(error);
212 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
213 CFReleaseNull(error);
215 CFReleaseSafe(ExtractPendingChanges(changes)); // Alice loses the race to write her changes - bob never sees em.
217 FeedChangesTo(&bobAcceptanceChanges, alice_account); // Alice sees bob inviting her in the circle.
219 FeedChangesTo(changes, bob_account); // Bob sees Alice Concurring
221 // As of PR-13917727/PR-13906870
222 accounts_agree("Alice leave, applies back, loses a race and eventually gets in", bob_account, alice_account);
227 // Emulation of <rdar://problem/13919554> Innsbruck11A368 +Roots: Device A was removed when Device B joined.
229 // 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.
231 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice leaves once more (%@)", error);
232 CFReleaseNull(error);
234 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
235 accounts_agree("Alice and Bob see Alice out of circle", bob_account, alice_account);
237 ok(SOSAccountJoinCircles_wTxn(alice_account, &error), "Alice re-applies (%@)", error);
238 CFReleaseNull(error);
240 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice leaves while applying (%@)", error);
241 FeedChangesTo(changes, bob_account); // Bob sees Alice become an Alum.
243 CFReleaseNull(error);
245 is([alice_account getCircleStatus:&error], kSOSCCNotInCircle, "Alice isn't applying any more");
246 accounts_agree("Alice leaves & some fancy concordance stuff happens", bob_account, alice_account);
248 ok(SOSAccountJoinCircles_wTxn(alice_account, &error), "Alice re-applies (%@)", error);
249 CFReleaseNull(error);
251 //FeedChangesTo(changes, bob_account); // Bob sees Alice reapply.
252 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
255 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
257 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
258 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
259 CFReleaseNull(error);
260 CFReleaseNull(applicants);
262 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
264 accounts_agree("Alice comes back", bob_account, alice_account);
266 // Emulation of <rdar://problem/13889901>
267 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
269 ok(SOSAccountAssertUserCredentialsAndUpdate(carol_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
270 CFReleaseNull(cfpassword);
271 ok(SOSAccountJoinCircles_wTxn(carol_account, &error), "Carol Applies (%@)", error);
272 CFReleaseNull(error);
274 CFMutableDictionaryRef dropped_changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
275 FillChanges(dropped_changes, carol_account);
276 CFReleaseNull(dropped_changes);
278 ok(SOSAccountResetToOffering_wTxn(carol_account, &error), "Reset to offering (%@)", error);
279 CFReleaseNull(error);
281 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 2, "updates");
282 accounts_agree("13889901", carol_account, bob_account);
283 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSMembershipRevoked, "Bob affirms he hasn't left.");
285 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob ReApplies (%@)", error);
286 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 2, "updates");
288 CFArrayRef applicants = SOSAccountCopyApplicants(carol_account, &error);
290 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
291 ok(SOSAccountAcceptApplicants(carol_account, applicants, &error), "Carol accepts (%@)", error);
292 CFReleaseNull(error);
293 CFReleaseNull(applicants);
295 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 3, "updates");
296 accounts_agree("rdar://problem/13889901-II", bob_account, carol_account);
298 // Alice has been out of the loop, bring her back up to speed before changing things (to avoid gen count race)
299 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "Reset propogation");
301 // Test multiple removal, including our own departure via that API
302 ok(SOSAccountResetToOffering_wTxn(alice_account, NULL), "Reset to offering");
304 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "Reset propogation");
306 ok(SOSAccountJoinCircles_wTxn(bob_account, NULL), "bob joins again");
308 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "Bob request");
311 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
313 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
314 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
315 CFReleaseNull(error);
316 CFReleaseNull(applicants);
319 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "carol request");
321 ok(SOSAccountJoinCircles_wTxn(carol_account, NULL), "carol joins again");
323 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "carol request");
325 CFArrayRef peers_to_remove_array = CFArrayCreateForCFTypes(kCFAllocatorDefault,
326 bob_account.peerInfo,
327 carol_account.peerInfo,
330 ok(SOSAccountRemovePeersFromCircle(bob_account, peers_to_remove_array, NULL));
332 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "Remove peers");
334 ok([alice_account isInCircle:&error], "Alice still here");
335 ok(![bob_account isInCircle:&error], "Bob not in circle");
336 // Carol's not in circle, but reapplied, as she's persistent until positive rejection.
337 ok(![carol_account isInCircle:NULL], "carol not in circle");
339 CFReleaseNull(peers_to_remove_array);
341 CFReleaseNull(alice_new_gestalt);
350 int secd_55_account_circle(int argc, char *const *argv)
353 plan_tests(kTestTestCount);
354 secd_test_setup_temp_keychain(__FUNCTION__, NULL);