2 * Copyright (c) 2013-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 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
42 #include "secd_regressions.h"
43 #include "SOSTestDataSource.h"
45 #include "SOSRegressionUtilities.h"
46 #include <utilities/SecCFWrappers.h>
47 #include <Security/SecKeyPriv.h>
49 #include "keychain/securityd/SOSCloudCircleServer.h"
51 #include "SOSAccountTesting.h"
53 #include "SecdTestKeychainUtilities.h"
59 static void trim_retirements_from_circle(SOSAccount* account) {
60 SOSAccountForEachCircle(account, ^(SOSCircleRef circle) {
61 SOSCircleRemoveRetired(circle, NULL);
67 static void tests(void)
69 CFErrorRef error = NULL;
70 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
71 CFStringRef cfaccount = CFSTR("test@test.org");
73 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
75 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
76 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
77 SOSAccount* carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
78 SOSAccount* david_account = CreateAccountForLocalChanges(CFSTR("David"), CFSTR("TestSource"));
80 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account , cfaccount, cfpassword, &error), "Credential setting (%@)", error);
82 // Bob wins writing at this point, feed the changes back to alice.
84 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
86 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account , cfaccount, cfpassword, &error), "Credential setting (%@)", error);
89 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
92 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
95 ok(SOSAccountResetToOffering_wTxn(alice_account , &error), "Reset to offering (%@)", error);
98 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
100 ok(SOSAccountJoinCircles_wTxn(bob_account , &error), "Bob Applies (%@)", error);
101 CFReleaseNull(error);
103 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
106 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
108 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
109 ok(SOSAccountAcceptApplicants(alice_account , applicants, &error), "Alice accepts (%@)", error);
110 CFReleaseNull(error);
111 CFReleaseNull(applicants);
115 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
117 accounts_agree("bob&alice pair", bob_account, alice_account);
118 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSNeverLeftCircle, "Bob affirms he hasn't left.");
120 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
121 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
122 CFReleaseNull(peers);
124 SOSAccountPurgePrivateCredential(alice_account);
126 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
127 CFReleaseNull(error);
129 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
131 accounts_agree("Alice bails", bob_account, alice_account);
134 CFArrayRef concurring = SOSAccountCopyConcurringPeers(alice_account, &error);
136 ok(concurring && CFArrayGetCount(concurring) == 2, "See two concurring %@ (%@)", concurring, error);
137 CFReleaseNull(error);
138 CFReleaseNull(concurring);
141 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
142 CFReleaseNull(error);
144 ok(SOSAccountJoinCircles_wTxn(alice_account , &error), "Alice re-applies (%@)", error);
145 CFReleaseNull(error);
147 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
149 is(countActivePeers(bob_account), 3, "Bob sees 2 active peers");
150 is(countPeers(bob_account), 1, "Bob sees 1 valid peer");
153 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
155 ok(applicants && CFArrayGetCount(applicants) == 1, "See alice's reapp. %@ (%@)", applicants, error);
156 ok(SOSAccountAcceptApplicants(bob_account , applicants, &error), "Bob accepts (%@)", error);
157 CFReleaseNull(error);
158 CFReleaseNull(applicants);
162 is(countActivePeers(bob_account), 3, "Bob sees 3 active peers");
163 is(countPeers(bob_account), 2, "Bob sees 2 valid peers");
165 is(countActivePeers(alice_account), 3, "Alice sees 2 active peers");
166 is(countPeers(alice_account), 1, "Alice sees 1 valid peers");
168 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
170 accounts_agree("Alice rejoined", bob_account, alice_account);
171 accounts_agree_internal("Alice rejoined, carole noticed", bob_account, carole_account, false);
173 ok(SOSAccountJoinCircles_wTxn(carole_account , &error), "Carole applies (%@)", error);
174 CFReleaseNull(error);
176 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
178 accounts_agree_internal("Carole applied", bob_account, alice_account, false);
179 accounts_agree_internal("Carole applied - 2", bob_account, carole_account, false);
182 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
184 ok(applicants && CFArrayGetCount(applicants) == 1, "See Carole's eapp. %@ (%@)", applicants, error);
185 ok(SOSAccountAcceptApplicants(bob_account , applicants, &error), "Bob accepts carole (%@)", error);
186 CFReleaseNull(error);
187 CFReleaseNull(applicants);
190 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 4, "updates");
191 // david ends up with a 3 peerinfo member circle.
193 accounts_agree_internal("Carole joined", bob_account, alice_account, false);
194 accounts_agree_internal("Carole joined - 2", bob_account, carole_account, false);
196 // Now test lost circle change when two leave simultaneously, needing us to see the retirement tickets
198 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
199 CFReleaseNull(error);
201 ok([carole_account.trust leaveCircle:carole_account err:&error], "carole Leaves (%@)", error);
202 CFReleaseNull(error);
204 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
206 is(countPeers(bob_account), 1, "Bob sees 1 valid peer");
207 is(countActivePeers(bob_account), 4, "Bob sees 4 active peers");
209 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
211 is(SOSAccountGetLastDepartureReason(carole_account, &error), kSOSWithdrewMembership, "Carole affirms she left on her own.");
213 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account , cfaccount, cfpassword, &error), "Credential setting (%@)", error);
214 CFReleaseNull(error);
215 CFReleaseNull(cfpassword);
216 is(countPeers(david_account), 1, "david sees 1 peers");
217 is(countActivePeers(david_account), 4, "david sees 4 active peers");
219 ok(SOSAccountJoinCircles_wTxn(david_account , &error), "David applies (%@)", error);
220 CFReleaseNull(error);
221 is(countPeers(david_account), 1, "david sees 1 peers");
222 is(countActivePeers(david_account), 4, "david sees 4 active peers");
224 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
227 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
229 ok(applicants && CFArrayGetCount(applicants) == 1, "See David's app. %@ (%@)", applicants, error);
230 ok(SOSAccountAcceptApplicants(bob_account , applicants, &error), "Bob accepts carole (%@)", error);
231 CFReleaseNull(error);
232 CFReleaseNull(applicants);
235 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
237 is(countPeers(bob_account), 2, "Bob sees 2 valid peer");
238 is(countActivePeers(bob_account), 3, "Bob sees 3 active peers");
241 // Now see that bob can call leave without his private key
242 SOSAccountPurgeIdentity(bob_account); // Hopefully this actually purges, no errors to process
244 ok([bob_account.trust leaveCircle:bob_account err:&error], "bob Leaves w/o credentials (%@)", error);
245 CFReleaseNull(error);
247 ok(![bob_account isInCircle:&error], "bob knows he's out (%@)", error);
248 CFReleaseNull(error);
257 int secd_57_account_leave(int argc, char *const *argv)
261 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
262 secd_test_clear_testviews();