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>
41 #include "secd_regressions.h"
42 #include "SOSTestDataSource.h"
44 #include "SOSRegressionUtilities.h"
45 #include <utilities/SecCFWrappers.h>
46 #include <Security/SecKeyPriv.h>
48 #include <securityd/SOSCloudCircleServer.h>
50 #include "SOSAccountTesting.h"
52 #include "SecdTestKeychainUtilities.h"
54 static int kTestTestCount
= 326;
56 static void tests(void)
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");
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"));
68 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
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");
73 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
75 ok(SOSAccountTryUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential trying (%@)", 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");
82 ok(SOSAccountResetToOffering_wTxn(alice_account
, &error
), "Reset to offering (%@)", error
);
85 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
87 ok(SOSAccountHasCompletedInitialSync(alice_account
), "Alice thinks she's completed initial sync");
89 ok(SOSAccountJoinCircles_wTxn(bob_account
, &error
), "Bob Applies (%@)", error
);
92 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
95 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
97 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
98 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
100 CFReleaseNull(applicants
);
103 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 3, "updates");
105 accounts_agree("bob&alice pair", bob_account
, alice_account
);
107 CFArrayRef peers
= SOSAccountCopyPeers(alice_account
, &error
);
108 ok(peers
&& CFArrayGetCount(peers
) == 2, "See two peers %@ (%@)", peers
, error
);
109 CFReleaseNull(peers
);
111 ok(!SOSAccountHasCompletedInitialSync(bob_account
), "Bob thinks he hasn't completed initial sync");
113 CFDictionaryRef alice_new_gestalt
= SOSCreatePeerGestaltFromName(CFSTR("Alice, but different"));
115 ok(SOSAccountUpdateGestalt(alice_account
, alice_new_gestalt
), "Update gestalt %@ (%@)", alice_account
, error
);
116 SOSAccountUpdateTestTransports(alice_account
, alice_new_gestalt
);
117 CFReleaseNull(alice_new_gestalt
);
119 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
121 accounts_agree("Alice's name changed", bob_account
, alice_account
);
123 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice Leaves (%@)", error
);
124 CFReleaseNull(error
);
126 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
128 accounts_agree("Alice bails", bob_account
, alice_account
);
130 peers
= SOSAccountCopyPeers(alice_account
, &error
);
131 ok(peers
&& CFArrayGetCount(peers
) == 1, "See one peer %@ (%@)", peers
, error
);
132 CFReleaseNull(peers
);
134 ok(SOSAccountJoinCircles_wTxn(alice_account
, &error
), "Alice re-applies (%@)", error
);
135 CFReleaseNull(error
);
137 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
141 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
143 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
144 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
145 CFReleaseNull(error
);
146 CFReleaseNull(applicants
);
149 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 3, "updates");
151 accounts_agree("Alice accepts' Bob", bob_account
, alice_account
);
153 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice Leaves (%@)", error
);
154 CFReleaseNull(error
);
155 ok(SOSAccountJoinCircles_wTxn(alice_account
, &error
), "Alice re-applies (%@)", error
);
156 CFReleaseNull(error
);
158 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
161 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
163 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
164 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
165 CFReleaseNull(error
);
166 CFReleaseNull(applicants
);
169 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 3, "updates");
171 accounts_agree("Bob accepts Alice", bob_account
, alice_account
);
173 // As of PR-13917727/PR-13906870 this no longer works (by "design"), in favor of making another more common
174 // failure (apply/OSX-psudo-reject/re-apply) work. Write races might be "fixed better" with affirmitave rejection.
178 // Write race emulation.
182 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice Leaves (%@)", error
);
183 CFReleaseNull(error
);
184 FeedChangesTo(changes
, bob_account
); // Bob sees Alice leaving and rejoining
185 FeedChangesTo(changes
, alice_account
); // Alice sees bob concurring
187 accounts_agree("Alice leaves & returns", bob_account
, alice_account
);
189 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
190 CFReleaseNull(error
);
192 FeedChangesTo(changes
, bob_account
); // Bob sees Alice Applying
195 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
197 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
198 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
199 CFReleaseNull(error
);
200 CFReleaseNull(applicants
);
203 CFMutableDictionaryRef bobAcceptanceChanges
= ExtractPendingChanges(changes
);
205 // Alice re-applies without seeing that she was accepted.
206 ok(SOSAccountLeaveCircles(alice_account
, &error
), "Alice Leaves again (%@)", error
);
207 CFReleaseNull(error
);
208 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
209 CFReleaseNull(error
);
211 CFReleaseSafe(ExtractPendingChanges(changes
)); // Alice loses the race to write her changes - bob never sees em.
213 FeedChangesTo(&bobAcceptanceChanges
, alice_account
); // Alice sees bob inviting her in the circle.
215 FeedChangesTo(changes
, bob_account
); // Bob sees Alice Concurring
217 // As of PR-13917727/PR-13906870
218 accounts_agree("Alice leave, applies back, loses a race and eventually gets in", bob_account
, alice_account
);
223 // Emulation of <rdar://problem/13919554> Innsbruck11A368 +Roots: Device A was removed when Device B joined.
225 // 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.
227 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice leaves once more (%@)", error
);
228 CFReleaseNull(error
);
230 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
231 accounts_agree("Alice and Bob see Alice out of circle", bob_account
, alice_account
);
233 ok(SOSAccountJoinCircles_wTxn(alice_account
, &error
), "Alice re-applies (%@)", error
);
234 CFReleaseNull(error
);
236 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice leaves while applying (%@)", error
);
237 FeedChangesTo(changes
, bob_account
); // Bob sees Alice become an Alum.
239 CFReleaseNull(error
);
241 is(SOSAccountGetCircleStatus(alice_account
, &error
), kSOSCCNotInCircle
, "Alice isn't applying any more");
242 accounts_agree("Alice leaves & some fancy concordance stuff happens", bob_account
, alice_account
);
244 ok(SOSAccountJoinCircles_wTxn(alice_account
, &error
), "Alice re-applies (%@)", error
);
245 CFReleaseNull(error
);
247 FeedChangesTo(changes
, bob_account
); // Bob sees Alice reapply.
250 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
252 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
253 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
254 CFReleaseNull(error
);
255 CFReleaseNull(applicants
);
257 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
259 accounts_agree("Alice comes back", bob_account
, alice_account
);
261 // Emulation of <rdar://problem/13889901>
262 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 1, "updates");
264 ok(SOSAccountAssertUserCredentialsAndUpdate(carol_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
265 CFReleaseNull(cfpassword
);
266 ok(SOSAccountJoinCircles_wTxn(carol_account
, &error
), "Carol Applies (%@)", error
);
267 CFReleaseNull(error
);
269 CFMutableDictionaryRef dropped_changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
270 FillChanges(dropped_changes
, carol_account
);
271 CFReleaseNull(dropped_changes
);
273 ok(SOSAccountResetToOffering_wTxn(carol_account
, &error
), "Reset to offering (%@)", error
);
274 CFReleaseNull(error
);
276 is(ProcessChangesUntilNoChange(changes
, bob_account
, carol_account
, NULL
), 2, "updates");
277 accounts_agree("13889901", carol_account
, bob_account
);
278 is(SOSAccountGetLastDepartureReason(bob_account
, &error
), kSOSMembershipRevoked
, "Bob affirms he hasn't left.");
280 ok(SOSAccountJoinCircles_wTxn(bob_account
, &error
), "Bob ReApplies (%@)", error
);
281 is(ProcessChangesUntilNoChange(changes
, bob_account
, carol_account
, NULL
), 2, "updates");
283 CFArrayRef applicants
= SOSAccountCopyApplicants(carol_account
, &error
);
285 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
286 ok(SOSAccountAcceptApplicants(carol_account
, applicants
, &error
), "Carol accepts (%@)", error
);
287 CFReleaseNull(error
);
288 CFReleaseNull(applicants
);
290 is(ProcessChangesUntilNoChange(changes
, bob_account
, carol_account
, NULL
), 3, "updates");
291 accounts_agree("rdar://problem/13889901-II", bob_account
, carol_account
);
293 // Alice has been out of the loop, bring her back up to speed before changing things (to avoid gen count race)
294 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 1, "Reset propogation");
296 // Test multiple removal, including our own departure via that API
297 ok(SOSAccountResetToOffering_wTxn(alice_account
, NULL
), "Reset to offering");
299 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "Reset propogation");
301 ok(SOSAccountJoinCircles_wTxn(bob_account
, NULL
), "bob joins again");
303 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "Bob request");
306 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
308 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
309 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
310 CFReleaseNull(error
);
311 CFReleaseNull(applicants
);
314 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 3, "carol request");
316 ok(SOSAccountJoinCircles_wTxn(carol_account
, NULL
), "carol joins again");
318 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "carol request");
320 CFArrayRef peers_to_remove_array
= CFArrayCreateForCFTypes(kCFAllocatorDefault
,
321 SOSAccountGetMyPeerInfo(bob_account
),
322 SOSAccountGetMyPeerInfo(carol_account
),
325 ok(SOSAccountRemovePeersFromCircle(bob_account
, peers_to_remove_array
, NULL
));
327 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 4, "Remove peers");
329 ok(SOSAccountIsInCircle(alice_account
, NULL
), "Alice still here");
330 ok(!SOSAccountIsInCircle(bob_account
, NULL
), "Bob not in circle");
331 // Carol's not in circle, but reapplied, as she's persistent until positive rejection.
332 ok(!SOSAccountIsInCircle(carol_account
, NULL
), "carol not in circle");
334 CFReleaseNull(peers_to_remove_array
);
336 CFReleaseNull(alice_new_gestalt
);
338 CFReleaseNull(bob_account
);
339 CFReleaseNull(alice_account
);
340 CFReleaseNull(carol_account
);
342 SOSUnregisterAllTransportMessages();
343 SOSUnregisterAllTransportCircles();
344 SOSUnregisterAllTransportKeyParameters();
345 CFArrayRemoveAllValues(key_transports
);
346 CFArrayRemoveAllValues(circle_transports
);
347 CFArrayRemoveAllValues(message_transports
);
351 int secd_55_account_circle(int argc
, char *const *argv
)
353 plan_tests(kTestTestCount
);
355 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);