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
= 324;
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(alice_account
, &error
), "Reset to offering (%@)", error
);
85 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
87 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
90 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
93 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
95 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
96 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
98 CFReleaseNull(applicants
);
101 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 3, "updates");
103 accounts_agree("bob&alice pair", bob_account
, alice_account
);
105 CFArrayRef peers
= SOSAccountCopyPeers(alice_account
, &error
);
106 ok(peers
&& CFArrayGetCount(peers
) == 2, "See two peers %@ (%@)", peers
, error
);
107 CFReleaseNull(peers
);
109 CFDictionaryRef alice_new_gestalt
= SOSCreatePeerGestaltFromName(CFSTR("Alice, but different"));
111 ok(SOSAccountUpdateGestalt(alice_account
, alice_new_gestalt
), "Update gestalt %@ (%@)", alice_account
, error
);
112 SOSAccountUpdateTestTransports(alice_account
, alice_new_gestalt
);
113 CFReleaseNull(alice_new_gestalt
);
115 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
117 accounts_agree("Alice's name changed", bob_account
, alice_account
);
119 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice Leaves (%@)", error
);
120 CFReleaseNull(error
);
122 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
124 accounts_agree("Alice bails", bob_account
, alice_account
);
126 peers
= SOSAccountCopyPeers(alice_account
, &error
);
127 ok(peers
&& CFArrayGetCount(peers
) == 1, "See one peer %@ (%@)", peers
, error
);
128 CFReleaseNull(peers
);
130 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
131 CFReleaseNull(error
);
133 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
137 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
139 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
140 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
141 CFReleaseNull(error
);
142 CFReleaseNull(applicants
);
145 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 3, "updates");
147 accounts_agree("Alice accepts' Bob", bob_account
, alice_account
);
149 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice Leaves (%@)", error
);
150 CFReleaseNull(error
);
151 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
152 CFReleaseNull(error
);
154 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
157 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
159 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
160 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
161 CFReleaseNull(error
);
162 CFReleaseNull(applicants
);
165 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 3, "updates");
167 accounts_agree("Bob accepts Alice", bob_account
, alice_account
);
169 // As of PR-13917727/PR-13906870 this no longer works (by "design"), in favor of making another more common
170 // failure (apply/OSX-psudo-reject/re-apply) work. Write races might be "fixed better" with affirmitave rejection.
174 // Write race emulation.
178 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice Leaves (%@)", error
);
179 CFReleaseNull(error
);
180 FeedChangesTo(changes
, bob_account
); // Bob sees Alice leaving and rejoining
181 FeedChangesTo(changes
, alice_account
); // Alice sees bob concurring
183 accounts_agree("Alice leaves & returns", bob_account
, alice_account
);
185 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
186 CFReleaseNull(error
);
188 FeedChangesTo(changes
, bob_account
); // Bob sees Alice Applying
191 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
193 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
194 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
195 CFReleaseNull(error
);
196 CFReleaseNull(applicants
);
199 CFMutableDictionaryRef bobAcceptanceChanges
= ExtractPendingChanges(changes
);
201 // Alice re-applies without seeing that she was accepted.
202 ok(SOSAccountLeaveCircles(alice_account
, &error
), "Alice Leaves again (%@)", error
);
203 CFReleaseNull(error
);
204 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
205 CFReleaseNull(error
);
207 CFReleaseSafe(ExtractPendingChanges(changes
)); // Alice loses the race to write her changes - bob never sees em.
209 FeedChangesTo(&bobAcceptanceChanges
, alice_account
); // Alice sees bob inviting her in the circle.
211 FeedChangesTo(changes
, bob_account
); // Bob sees Alice Concurring
213 // As of PR-13917727/PR-13906870
214 accounts_agree("Alice leave, applies back, loses a race and eventually gets in", bob_account
, alice_account
);
219 // Emulation of <rdar://problem/13919554> Innsbruck11A368 +Roots: Device A was removed when Device B joined.
221 // 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.
223 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice leaves once more (%@)", error
);
224 CFReleaseNull(error
);
226 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
227 accounts_agree("Alice and Bob see Alice out of circle", bob_account
, alice_account
);
229 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
230 CFReleaseNull(error
);
232 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice leaves while applying (%@)", error
);
233 FeedChangesTo(changes
, bob_account
); // Bob sees Alice become an Alum.
235 CFReleaseNull(error
);
237 is(SOSAccountGetCircleStatus(alice_account
, &error
), kSOSCCNotInCircle
, "Alice isn't applying any more");
238 accounts_agree("Alice leaves & some fancy concordance stuff happens", bob_account
, alice_account
);
240 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
241 CFReleaseNull(error
);
243 FeedChangesTo(changes
, bob_account
); // Bob sees Alice reapply.
246 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
248 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
249 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
250 CFReleaseNull(error
);
251 CFReleaseNull(applicants
);
253 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
255 accounts_agree("Alice comes back", bob_account
, alice_account
);
257 // Emulation of <rdar://problem/13889901>
258 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 1, "updates");
260 ok(SOSAccountAssertUserCredentialsAndUpdate(carol_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
261 CFReleaseNull(cfpassword
);
262 ok(SOSAccountJoinCircles(carol_account
, &error
), "Carol Applies (%@)", error
);
263 CFReleaseNull(error
);
265 CFMutableDictionaryRef dropped_changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
266 FillChanges(dropped_changes
, carol_account
);
267 CFReleaseNull(dropped_changes
);
269 ok(SOSAccountResetToOffering(carol_account
, &error
), "Reset to offering (%@)", error
);
270 CFReleaseNull(error
);
272 is(ProcessChangesUntilNoChange(changes
, bob_account
, carol_account
, NULL
), 2, "updates");
273 accounts_agree("13889901", carol_account
, bob_account
);
274 is(SOSAccountGetLastDepartureReason(bob_account
, &error
), kSOSMembershipRevoked
, "Bob affirms he hasn't left.");
276 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob ReApplies (%@)", error
);
277 is(ProcessChangesUntilNoChange(changes
, bob_account
, carol_account
, NULL
), 2, "updates");
279 CFArrayRef applicants
= SOSAccountCopyApplicants(carol_account
, &error
);
281 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
282 ok(SOSAccountAcceptApplicants(carol_account
, applicants
, &error
), "Carol accepts (%@)", error
);
283 CFReleaseNull(error
);
284 CFReleaseNull(applicants
);
286 is(ProcessChangesUntilNoChange(changes
, bob_account
, carol_account
, NULL
), 3, "updates");
287 accounts_agree("rdar://problem/13889901-II", bob_account
, carol_account
);
289 // Alice has been out of the loop, bring her back up to speed before changing things (to avoid gen count race)
290 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 1, "Reset propogation");
292 // Test multiple removal, including our own departure via that API
293 ok(SOSAccountResetToOffering(alice_account
, NULL
), "Reset to offering");
295 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "Reset propogation");
297 ok(SOSAccountJoinCircles(bob_account
, NULL
), "bob joins again");
299 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "Bob request");
302 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
304 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
305 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
306 CFReleaseNull(error
);
307 CFReleaseNull(applicants
);
310 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 3, "carol request");
312 ok(SOSAccountJoinCircles(carol_account
, NULL
), "carol joins again");
314 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "carol request");
316 CFArrayRef peers_to_remove_array
= CFArrayCreateForCFTypes(kCFAllocatorDefault
,
317 SOSAccountGetMyPeerInfo(bob_account
),
318 SOSAccountGetMyPeerInfo(carol_account
),
321 ok(SOSAccountRemovePeersFromCircle(bob_account
, peers_to_remove_array
, NULL
));
323 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 4, "Remove peers");
325 ok(SOSAccountIsInCircle(alice_account
, NULL
), "Alice still here");
326 ok(!SOSAccountIsInCircle(bob_account
, NULL
), "Bob not in circle");
327 // Carol's not in circle, but reapplied, as she's persistent until positive rejection.
328 ok(!SOSAccountIsInCircle(carol_account
, NULL
), "carol not in circle");
330 CFReleaseNull(peers_to_remove_array
);
332 CFReleaseNull(alice_new_gestalt
);
334 CFReleaseNull(bob_account
);
335 CFReleaseNull(alice_account
);
336 CFReleaseNull(carol_account
);
338 SOSUnregisterAllTransportMessages();
339 SOSUnregisterAllTransportCircles();
340 SOSUnregisterAllTransportKeyParameters();
341 CFArrayRemoveAllValues(key_transports
);
342 CFArrayRemoveAllValues(circle_transports
);
343 CFArrayRemoveAllValues(message_transports
);
347 int secd_55_account_circle(int argc
, char *const *argv
)
349 plan_tests(kTestTestCount
);
351 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);