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 <SecureObjectSync/SOSAccount.h>
33 #include <SecureObjectSync/SOSCloudCircle.h>
34 #include <SecureObjectSync/SOSInternal.h>
35 #include <SecureObjectSync/SOSUserKeygen.h>
36 #include <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"
53 static int kTestTestCount
= 264;
56 static void trim_retirements_from_circle(SOSAccountRef account) {
57 SOSAccountForEachCircle(account, ^(SOSCircleRef circle) {
58 SOSCircleRemoveRetired(circle, NULL);
64 static void tests(void)
66 CFErrorRef error
= NULL
;
67 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
68 CFStringRef cfaccount
= CFSTR("test@test.org");
70 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
72 SOSAccountRef alice_account
= CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
73 SOSAccountRef bob_account
= CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
74 SOSAccountRef carole_account
= CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
75 SOSAccountRef david_account
= CreateAccountForLocalChanges(CFSTR("David"), CFSTR("TestSource"));
77 ok(SOSAccountAssertUserCredentials(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
79 // Bob wins writing at this point, feed the changes back to alice.
81 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 1, "updates");
83 ok(SOSAccountAssertUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
86 ok(SOSAccountAssertUserCredentials(carole_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
89 ok(SOSAccountAssertUserCredentials(david_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
92 ok(SOSAccountResetToOffering(alice_account
, &error
), "Reset to offering (%@)", error
);
95 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
97 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
100 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
103 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
105 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
106 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
107 CFReleaseNull(error
);
108 CFReleaseNull(applicants
);
112 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 3, "updates");
114 accounts_agree("bob&alice pair", bob_account
, alice_account
);
115 is(SOSAccountGetLastDepartureReason(bob_account
, &error
), kSOSNeverLeftCircle
, "Bob affirms he hasn't left.");
117 CFArrayRef peers
= SOSAccountCopyPeers(alice_account
, &error
);
118 ok(peers
&& CFArrayGetCount(peers
) == 2, "See two peers %@ (%@)", peers
, error
);
119 CFReleaseNull(peers
);
121 SOSAccountPurgePrivateCredential(alice_account
);
123 ok(SOSAccountLeaveCircles(alice_account
, &error
), "Alice Leaves (%@)", error
);
124 CFReleaseNull(error
);
126 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 3, "updates");
128 accounts_agree("Alice bails", bob_account
, alice_account
);
131 CFArrayRef concurring
= SOSAccountCopyConcurringPeers(alice_account
, &error
);
133 ok(concurring
&& CFArrayGetCount(concurring
) == 2, "See two concurring %@ (%@)", concurring
, error
);
134 CFReleaseNull(error
);
135 CFReleaseNull(concurring
);
138 ok(SOSAccountTryUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
139 CFReleaseNull(error
);
141 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
142 CFReleaseNull(error
);
144 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
146 is(countActivePeers(bob_account
), 3, "Bob sees 2 active peers");
147 is(countPeers(bob_account
), 1, "Bob sees 1 valid peer");
150 CFArrayRef applicants
= SOSAccountCopyApplicants(bob_account
, &error
);
152 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See alice's reapp. %@ (%@)", applicants
, error
);
153 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
154 CFReleaseNull(error
);
155 CFReleaseNull(applicants
);
159 is(countActivePeers(bob_account
), 3, "Bob sees 3 active peers");
160 is(countPeers(bob_account
), 2, "Bob sees 2 valid peers");
162 is(countActivePeers(alice_account
), 3, "Alice sees 2 active peers");
163 is(countPeers(alice_account
), 1, "Alice sees 1 valid peers");
165 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 3, "updates");
167 accounts_agree("Alice rejoined", bob_account
, alice_account
);
168 accounts_agree_internal("Alice rejoined, carole noticed", bob_account
, carole_account
, false);
170 ok(SOSAccountJoinCircles(carole_account
, &error
), "Carole applies (%@)", error
);
171 CFReleaseNull(error
);
173 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
175 accounts_agree_internal("Carole applied", bob_account
, alice_account
, false);
176 accounts_agree_internal("Carole applied - 2", bob_account
, carole_account
, false);
179 CFArrayRef applicants
= SOSAccountCopyApplicants(bob_account
, &error
);
181 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See Carole's eapp. %@ (%@)", applicants
, error
);
182 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts carole (%@)", error
);
183 CFReleaseNull(error
);
184 CFReleaseNull(applicants
);
187 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 4, "updates");
188 // david ends up with a 3 peerinfo member circle.
190 accounts_agree_internal("Carole joined", bob_account
, alice_account
, false);
191 accounts_agree_internal("Carole joined - 2", bob_account
, carole_account
, false);
193 // Now test lost circle change when two leave simultaneously, needing us to see the retirement tickets
195 ok(SOSAccountLeaveCircles(alice_account
, &error
), "Alice Leaves (%@)", error
);
196 CFReleaseNull(error
);
198 ok(SOSAccountLeaveCircles(carole_account
, &error
), "carole Leaves (%@)", error
);
199 CFReleaseNull(error
);
201 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 3, "updates");
203 is(countPeers(bob_account
), 1, "Bob sees 1 valid peer");
204 is(countActivePeers(bob_account
), 4, "Bob sees 4 active peers");
206 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 1, "updates");
208 is(SOSAccountGetLastDepartureReason(carole_account
, &error
), kSOSWithdrewMembership
, "Carole affirms she left on her own.");
210 ok(SOSAccountAssertUserCredentials(david_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
211 CFReleaseNull(error
);
212 CFReleaseNull(cfpassword
);
213 is(countPeers(david_account
), 1, "david sees 1 peers");
214 is(countActivePeers(david_account
), 4, "david sees 4 active peers");
216 ok(SOSAccountJoinCircles(david_account
, &error
), "David applies (%@)", error
);
217 CFReleaseNull(error
);
218 is(countPeers(david_account
), 1, "david sees 1 peers");
219 is(countActivePeers(david_account
), 4, "david sees 4 active peers");
221 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
224 CFArrayRef applicants
= SOSAccountCopyApplicants(bob_account
, &error
);
226 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See David's app. %@ (%@)", applicants
, error
);
227 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts carole (%@)", error
);
228 CFReleaseNull(error
);
229 CFReleaseNull(applicants
);
232 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 3, "updates");
234 is(countPeers(bob_account
), 2, "Bob sees 2 valid peer");
235 is(countActivePeers(bob_account
), 3, "Bob sees 3 active peers");
237 CFReleaseNull(bob_account
);
238 CFReleaseNull(alice_account
);
240 SOSUnregisterAllTransportMessages();
241 SOSUnregisterAllTransportCircles();
242 SOSUnregisterAllTransportKeyParameters();
243 CFArrayRemoveAllValues(key_transports
);
244 CFArrayRemoveAllValues(circle_transports
);
245 CFArrayRemoveAllValues(message_transports
);
248 int secd_57_account_leave(int argc
, char *const *argv
)
250 plan_tests(kTestTestCount
);