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 <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"
55 static int kTestTestCount
= 253;
58 static void trim_retirements_from_circle(SOSAccountRef account) {
59 SOSAccountForEachCircle(account, ^(SOSCircleRef circle) {
60 SOSCircleRemoveRetired(circle, NULL);
66 static void tests(void)
68 CFErrorRef error
= NULL
;
69 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
70 CFStringRef cfaccount
= CFSTR("test@test.org");
72 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
74 SOSAccountRef alice_account
= CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
75 SOSAccountRef bob_account
= CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
76 SOSAccountRef carole_account
= CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
77 SOSAccountRef david_account
= CreateAccountForLocalChanges(CFSTR("David"), CFSTR("TestSource"));
79 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
81 // Bob wins writing at this point, feed the changes back to alice.
83 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 1, "updates");
85 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
88 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
91 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
94 ok(SOSAccountResetToOffering_wTxn(alice_account
, &error
), "Reset to offering (%@)", error
);
97 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
99 ok(SOSAccountJoinCircles_wTxn(bob_account
, &error
), "Bob Applies (%@)", error
);
100 CFReleaseNull(error
);
102 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
105 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
107 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
108 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
109 CFReleaseNull(error
);
110 CFReleaseNull(applicants
);
114 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 3, "updates");
116 accounts_agree("bob&alice pair", bob_account
, alice_account
);
117 is(SOSAccountGetLastDepartureReason(bob_account
, &error
), kSOSNeverLeftCircle
, "Bob affirms he hasn't left.");
119 CFArrayRef peers
= SOSAccountCopyPeers(alice_account
, &error
);
120 ok(peers
&& CFArrayGetCount(peers
) == 2, "See two peers %@ (%@)", peers
, error
);
121 CFReleaseNull(peers
);
123 SOSAccountPurgePrivateCredential(alice_account
);
125 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice Leaves (%@)", error
);
126 CFReleaseNull(error
);
128 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
130 accounts_agree("Alice bails", bob_account
, alice_account
);
133 CFArrayRef concurring
= SOSAccountCopyConcurringPeers(alice_account
, &error
);
135 ok(concurring
&& CFArrayGetCount(concurring
) == 2, "See two concurring %@ (%@)", concurring
, error
);
136 CFReleaseNull(error
);
137 CFReleaseNull(concurring
);
140 ok(SOSAccountTryUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
141 CFReleaseNull(error
);
143 ok(SOSAccountJoinCircles_wTxn(alice_account
, &error
), "Alice re-applies (%@)", error
);
144 CFReleaseNull(error
);
146 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
148 is(countActivePeers(bob_account
), 3, "Bob sees 2 active peers");
149 is(countPeers(bob_account
), 1, "Bob sees 1 valid peer");
152 CFArrayRef applicants
= SOSAccountCopyApplicants(bob_account
, &error
);
154 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See alice's reapp. %@ (%@)", applicants
, error
);
155 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
156 CFReleaseNull(error
);
157 CFReleaseNull(applicants
);
161 is(countActivePeers(bob_account
), 3, "Bob sees 3 active peers");
162 is(countPeers(bob_account
), 2, "Bob sees 2 valid peers");
164 is(countActivePeers(alice_account
), 3, "Alice sees 2 active peers");
165 is(countPeers(alice_account
), 1, "Alice sees 1 valid peers");
167 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 3, "updates");
169 accounts_agree("Alice rejoined", bob_account
, alice_account
);
170 accounts_agree_internal("Alice rejoined, carole noticed", bob_account
, carole_account
, false);
172 ok(SOSAccountJoinCircles_wTxn(carole_account
, &error
), "Carole applies (%@)", error
);
173 CFReleaseNull(error
);
175 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
177 accounts_agree_internal("Carole applied", bob_account
, alice_account
, false);
178 accounts_agree_internal("Carole applied - 2", bob_account
, carole_account
, false);
181 CFArrayRef applicants
= SOSAccountCopyApplicants(bob_account
, &error
);
183 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See Carole's eapp. %@ (%@)", applicants
, error
);
184 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts carole (%@)", error
);
185 CFReleaseNull(error
);
186 CFReleaseNull(applicants
);
189 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 4, "updates");
190 // david ends up with a 3 peerinfo member circle.
192 accounts_agree_internal("Carole joined", bob_account
, alice_account
, false);
193 accounts_agree_internal("Carole joined - 2", bob_account
, carole_account
, false);
195 // Now test lost circle change when two leave simultaneously, needing us to see the retirement tickets
197 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice Leaves (%@)", error
);
198 CFReleaseNull(error
);
200 ok(SOSAccountLeaveCircle(carole_account
, &error
), "carole Leaves (%@)", error
);
201 CFReleaseNull(error
);
203 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
205 is(countPeers(bob_account
), 1, "Bob sees 1 valid peer");
206 is(countActivePeers(bob_account
), 4, "Bob sees 4 active peers");
208 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 1, "updates");
210 is(SOSAccountGetLastDepartureReason(carole_account
, &error
), kSOSWithdrewMembership
, "Carole affirms she left on her own.");
212 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
213 CFReleaseNull(error
);
214 CFReleaseNull(cfpassword
);
215 is(countPeers(david_account
), 1, "david sees 1 peers");
216 is(countActivePeers(david_account
), 4, "david sees 4 active peers");
218 ok(SOSAccountJoinCircles_wTxn(david_account
, &error
), "David applies (%@)", error
);
219 CFReleaseNull(error
);
220 is(countPeers(david_account
), 1, "david sees 1 peers");
221 is(countActivePeers(david_account
), 4, "david sees 4 active peers");
223 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
226 CFArrayRef applicants
= SOSAccountCopyApplicants(bob_account
, &error
);
228 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See David's app. %@ (%@)", applicants
, error
);
229 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts carole (%@)", error
);
230 CFReleaseNull(error
);
231 CFReleaseNull(applicants
);
234 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 3, "updates");
236 is(countPeers(bob_account
), 2, "Bob sees 2 valid peer");
237 is(countActivePeers(bob_account
), 3, "Bob sees 3 active peers");
240 // Now see that bob can call leave without his private key
241 SOSAccountPurgeIdentity(bob_account
); // Hopefully this actually purges, no errors to process
243 ok(SOSAccountLeaveCircle(bob_account
, &error
), "bob Leaves w/o credentials (%@)", error
);
244 CFReleaseNull(error
);
246 ok(!SOSAccountIsInCircle(bob_account
, &error
), "bob knows he's out (%@)", error
);
247 CFReleaseNull(error
);
249 CFReleaseNull(bob_account
);
250 CFReleaseNull(alice_account
);
251 CFReleaseNull(carole_account
);
252 CFReleaseNull(david_account
);
254 SOSUnregisterAllTransportMessages();
255 SOSUnregisterAllTransportCircles();
256 SOSUnregisterAllTransportKeyParameters();
257 CFArrayRemoveAllValues(key_transports
);
258 CFArrayRemoveAllValues(circle_transports
);
259 CFArrayRemoveAllValues(message_transports
);
262 int secd_57_account_leave(int argc
, char *const *argv
)
264 plan_tests(kTestTestCount
);
266 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);