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"
57 static void trim_retirements_from_circle(SOSAccount* account) {
58 SOSAccountForEachCircle(account, ^(SOSCircleRef circle) {
59 SOSCircleRemoveRetired(circle, NULL);
65 static void tests(void)
67 CFErrorRef error = NULL;
68 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
69 CFStringRef cfaccount = CFSTR("test@test.org");
71 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
73 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
74 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
75 SOSAccount* carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
76 SOSAccount* david_account = CreateAccountForLocalChanges(CFSTR("David"), CFSTR("TestSource"));
78 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account , cfaccount, cfpassword, &error), "Credential setting (%@)", error);
80 // Bob wins writing at this point, feed the changes back to alice.
82 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
84 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account , cfaccount, cfpassword, &error), "Credential setting (%@)", error);
87 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
90 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
93 ok(SOSAccountResetToOffering_wTxn(alice_account , &error), "Reset to offering (%@)", error);
96 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
98 ok(SOSAccountJoinCircles_wTxn(bob_account , &error), "Bob Applies (%@)", error);
101 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
104 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
106 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
107 ok(SOSAccountAcceptApplicants(alice_account , applicants, &error), "Alice accepts (%@)", error);
108 CFReleaseNull(error);
109 CFReleaseNull(applicants);
113 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
115 accounts_agree("bob&alice pair", bob_account, alice_account);
116 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSNeverLeftCircle, "Bob affirms he hasn't left.");
118 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
119 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
120 CFReleaseNull(peers);
122 SOSAccountPurgePrivateCredential(alice_account);
124 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
125 CFReleaseNull(error);
127 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
129 accounts_agree("Alice bails", bob_account, alice_account);
132 CFArrayRef concurring = SOSAccountCopyConcurringPeers(alice_account, &error);
134 ok(concurring && CFArrayGetCount(concurring) == 2, "See two concurring %@ (%@)", concurring, error);
135 CFReleaseNull(error);
136 CFReleaseNull(concurring);
139 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
140 CFReleaseNull(error);
142 ok(SOSAccountJoinCircles_wTxn(alice_account , &error), "Alice re-applies (%@)", error);
143 CFReleaseNull(error);
145 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
147 is(countActivePeers(bob_account), 3, "Bob sees 2 active peers");
148 is(countPeers(bob_account), 1, "Bob sees 1 valid peer");
151 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
153 ok(applicants && CFArrayGetCount(applicants) == 1, "See alice's reapp. %@ (%@)", applicants, error);
154 ok(SOSAccountAcceptApplicants(bob_account , applicants, &error), "Bob accepts (%@)", error);
155 CFReleaseNull(error);
156 CFReleaseNull(applicants);
160 is(countActivePeers(bob_account), 3, "Bob sees 3 active peers");
161 is(countPeers(bob_account), 2, "Bob sees 2 valid peers");
163 is(countActivePeers(alice_account), 3, "Alice sees 2 active peers");
164 is(countPeers(alice_account), 1, "Alice sees 1 valid peers");
166 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
168 accounts_agree("Alice rejoined", bob_account, alice_account);
169 accounts_agree_internal("Alice rejoined, carole noticed", bob_account, carole_account, false);
171 ok(SOSAccountJoinCircles_wTxn(carole_account , &error), "Carole applies (%@)", error);
172 CFReleaseNull(error);
174 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
176 accounts_agree_internal("Carole applied", bob_account, alice_account, false);
177 accounts_agree_internal("Carole applied - 2", bob_account, carole_account, false);
180 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
182 ok(applicants && CFArrayGetCount(applicants) == 1, "See Carole's eapp. %@ (%@)", applicants, error);
183 ok(SOSAccountAcceptApplicants(bob_account , applicants, &error), "Bob accepts carole (%@)", error);
184 CFReleaseNull(error);
185 CFReleaseNull(applicants);
188 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 4, "updates");
189 // david ends up with a 3 peerinfo member circle.
191 accounts_agree_internal("Carole joined", bob_account, alice_account, false);
192 accounts_agree_internal("Carole joined - 2", bob_account, carole_account, false);
194 // Now test lost circle change when two leave simultaneously, needing us to see the retirement tickets
196 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
197 CFReleaseNull(error);
199 ok([carole_account.trust leaveCircle:carole_account err:&error], "carole Leaves (%@)", error);
200 CFReleaseNull(error);
202 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
204 is(countPeers(bob_account), 1, "Bob sees 1 valid peer");
205 is(countActivePeers(bob_account), 4, "Bob sees 4 active peers");
207 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
209 is(SOSAccountGetLastDepartureReason(carole_account, &error), kSOSWithdrewMembership, "Carole affirms she left on her own.");
211 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account , cfaccount, cfpassword, &error), "Credential setting (%@)", error);
212 CFReleaseNull(error);
213 CFReleaseNull(cfpassword);
214 is(countPeers(david_account), 1, "david sees 1 peers");
215 is(countActivePeers(david_account), 4, "david sees 4 active peers");
217 ok(SOSAccountJoinCircles_wTxn(david_account , &error), "David applies (%@)", error);
218 CFReleaseNull(error);
219 is(countPeers(david_account), 1, "david sees 1 peers");
220 is(countActivePeers(david_account), 4, "david sees 4 active peers");
222 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
225 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
227 ok(applicants && CFArrayGetCount(applicants) == 1, "See David's app. %@ (%@)", applicants, error);
228 ok(SOSAccountAcceptApplicants(bob_account , applicants, &error), "Bob accepts carole (%@)", error);
229 CFReleaseNull(error);
230 CFReleaseNull(applicants);
233 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
235 is(countPeers(bob_account), 2, "Bob sees 2 valid peer");
236 is(countActivePeers(bob_account), 3, "Bob sees 3 active peers");
239 // Now see that bob can call leave without his private key
240 SOSAccountPurgeIdentity(bob_account); // Hopefully this actually purges, no errors to process
242 ok([bob_account.trust leaveCircle:bob_account err:&error], "bob Leaves w/o credentials (%@)", error);
243 CFReleaseNull(error);
245 ok(![bob_account isInCircle:&error], "bob knows he's out (%@)", error);
246 CFReleaseNull(error);
253 int secd_57_account_leave(int argc, char *const *argv)
257 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
258 secd_test_clear_testviews();