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"
54 static int kTestTestCount
= 118;
57 static int countPeers(SOSAccountRef account
, bool active
) {
58 CFErrorRef error
= NULL
;
61 if(active
) peers
= SOSAccountCopyActivePeers(account
, &error
);
62 else peers
= SOSAccountCopyPeers(account
, &error
);
63 int retval
= (int) CFArrayGetCount(peers
);
70 static void tests(void)
72 CFErrorRef error
= NULL
;
73 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
74 CFStringRef cfaccount
= CFSTR("test@test.org");
76 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
78 SOSAccountRef alice_account
= CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
79 SOSAccountRef bob_account
= CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
80 SOSAccountRef carole_account
= CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
81 SOSAccountRef david_account
= CreateAccountForLocalChanges(CFSTR("David"), CFSTR("TestSource"));
83 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
85 // Bob wins writing at this point, feed the changes back to alice.
86 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 1, "updates");
88 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
91 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
94 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
95 CFReleaseNull(cfpassword
);
98 ok(SOSAccountResetToOffering(alice_account
, &error
), "Reset to offering (%@)", error
);
101 // Lost Application Scenario
102 is(ProcessChangesOnce(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 1, "updates");
104 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
105 CFReleaseNull(error
);
107 ok(SOSAccountJoinCircles(carole_account
, &error
), "Carole Applies too (%@)", error
);
108 CFReleaseNull(error
);
110 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 3, "updates");
112 accounts_agree("alice and carole agree", alice_account
, carole_account
);
113 accounts_agree("alice and bob agree", alice_account
, bob_account
);
116 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
118 ok(applicants
&& CFArrayGetCount(applicants
) == 2, "See two applicants %@ (%@)", applicants
, error
);
119 CFReleaseNull(error
);
120 CFReleaseSafe(applicants
);
123 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 1, "updates");
125 accounts_agree("alice and carole agree", alice_account
, carole_account
);
127 CFReleaseNull(error
);
130 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
131 ok(applicants
&& CFArrayGetCount(applicants
) == 2, "See two applicants %@ (%@)", applicants
, error
);
132 ok(SOSAccountRejectApplicants(alice_account
, applicants
, &error
), "Everyone out the pool");
133 CFReleaseNull(error
);
134 CFReleaseSafe(applicants
);
137 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
139 accounts_agree("alice and carole agree", alice_account
, carole_account
);
142 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
143 ok(applicants
&& CFArrayGetCount(applicants
) == 0, "See no applicants %@ (%@)", applicants
, error
);
144 CFReleaseNull(error
);
145 CFReleaseSafe(applicants
);
148 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob asks again");
149 CFReleaseNull(error
);
151 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
154 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
155 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicants %@ (%@)", applicants
, error
);
156 CFReleaseNull(error
);
157 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Accept bob into the fold");
158 CFReleaseNull(error
);
159 CFReleaseSafe(applicants
);
162 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 3, "updates");
167 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
169 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "Bob automatically re-applied %@ (%@)", applicants
, error
);
170 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
171 CFReleaseNull(error
);
172 CFReleaseNull(applicants
);
175 is(countPeers(alice_account
, 0), 3, "Bob is accepted after auto-reapply");
177 FillAllChanges(changes
);
178 FeedChangesToMulti(AliceChanges
, bob_account
, carole_account
, david_account
, NULL
);
179 FeedChangesToMulti(BobChanges
, alice_account
, carole_account
, david_account
, NULL
);
180 FeedChangesToMulti(CarolChanges
, bob_account
, alice_account
, david_account
, NULL
);
181 FeedChangesToMulti(DavidChanges
, bob_account
, alice_account
, carole_account
, NULL
);
183 FillAllChanges(changes
);
184 FeedChangesToMulti(AliceChanges
, bob_account
, carole_account
, david_account
, NULL
);
185 FeedChangesToMulti(BobChanges
, alice_account
, carole_account
, david_account
, NULL
);
186 FeedChangesToMulti(CarolChanges
, bob_account
, alice_account
, david_account
, NULL
);
187 FeedChangesToMulti(DavidChanges
, bob_account
, alice_account
, carole_account
, NULL
);
189 FillAllChanges(changes
);
190 FeedChangesToMulti(AliceChanges
, bob_account
, carole_account
, david_account
, NULL
);
191 FeedChangesToMulti(BobChanges
, alice_account
, carole_account
, david_account
, NULL
);
192 FeedChangesToMulti(CarolChanges
, bob_account
, alice_account
, david_account
, NULL
);
193 FeedChangesToMulti(DavidChanges
, bob_account
, alice_account
, carole_account
, NULL
);
194 accounts_agree("alice and carole agree after bob gets in", alice_account
, carole_account
);
196 // Rejected Application Scenario
197 ok(SOSAccountJoinCircles(david_account
, &error
), "Dave Applies (%@)", error
);
198 CFReleaseNull(error
);
200 FillAllChanges(changes
);
201 FeedChangesToMulti(AliceChanges
, bob_account
, carole_account
, david_account
, NULL
);
202 FeedChangesToMulti(BobChanges
, alice_account
, carole_account
, david_account
, NULL
);
203 FeedChangesToMulti(CarolChanges
, bob_account
, alice_account
, david_account
, NULL
);
204 FeedChangesToMulti(DavidChanges
, bob_account
, alice_account
, carole_account
, NULL
);
206 SOSAccountPurgePrivateCredential(alice_account
);
209 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
211 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
212 ok(SOSAccountRejectApplicants(alice_account
, applicants
, &error
), "Alice rejects (%@)", error
);
213 CFReleaseNull(error
);
214 CFReleaseNull(applicants
);
217 FillAllChanges(changes
);
218 FeedChangesToMulti(AliceChanges
, bob_account
, carole_account
, david_account
, NULL
);
219 FeedChangesToMulti(BobChanges
, alice_account
, carole_account
, david_account
, NULL
);
220 FeedChangesToMulti(CarolChanges
, bob_account
, alice_account
, david_account
, NULL
);
221 FeedChangesToMulti(DavidChanges
, bob_account
, alice_account
, carole_account
, NULL
);
223 FillAllChanges(changes
);
224 FeedChangesToMulti(AliceChanges
, bob_account
, carole_account
, david_account
, NULL
);
225 FeedChangesToMulti(BobChanges
, alice_account
, carole_account
, david_account
, NULL
);
226 FeedChangesToMulti(CarolChanges
, bob_account
, alice_account
, david_account
, NULL
);
227 FeedChangesToMulti(DavidChanges
, bob_account
, alice_account
, carole_account
, NULL
);
229 FillAllChanges(changes
);
230 FeedChangesToMulti(AliceChanges
, bob_account
, carole_account
, david_account
, NULL
);
231 FeedChangesToMulti(BobChanges
, alice_account
, carole_account
, david_account
, NULL
);
232 FeedChangesToMulti(CarolChanges
, bob_account
, alice_account
, david_account
, NULL
);
233 FeedChangesToMulti(DavidChanges
, bob_account
, alice_account
, carole_account
, NULL
);
235 accounts_agree("alice and carole still agree after david is rejected", alice_account
, carole_account
);
236 ok(SOSAccountTryUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
237 CFReleaseNull(error
);
239 FillAllChanges(changes
);
240 FeedChangesToMulti(AliceChanges
, bob_account
, carole_account
, david_account
, NULL
);
241 FeedChangesToMulti(BobChanges
, alice_account
, carole_account
, david_account
, NULL
);
242 FeedChangesToMulti(CarolChanges
, bob_account
, alice_account
, david_account
, NULL
);
243 FeedChangesToMulti(DavidChanges
, bob_account
, alice_account
, carole_account
, NULL
);
245 FillAllChanges(changes
);
247 ok(CFDictionaryGetCount(CarolChanges
) == 0, "We converged. (%@)", CarolChanges
);
248 ok(CFDictionaryGetCount(BobChanges
) == 0, "We converged. (%@)", BobChanges
);
249 ok(CFDictionaryGetCount(AliceChanges
) == 0, "We converged. (%@)", AliceChanges
);
250 ok(CFDictionaryGetCount(DavidChanges
) == 0, "We converged. (%@)", DavidChanges
);
252 accounts_agree("bob&alice pair", bob_account
, alice_account
);
254 ok(SOSAccountJoinCirclesAfterRestore(carole_account
, &error
), "Carole cloud identiy joins (%@)", error
);
255 CFReleaseNull(error
);
257 is(countPeers(carole_account
, false), 3, "Carole sees 3 valid peers after sliding in");
259 FillAllChanges(changes
);
260 FeedChangesToMulti(AliceChanges
, bob_account
, carole_account
, david_account
, NULL
);
261 FeedChangesToMulti(BobChanges
, alice_account
, carole_account
, david_account
, NULL
);
262 FeedChangesToMulti(CarolChanges
, bob_account
, alice_account
, david_account
, NULL
);
263 FeedChangesToMulti(DavidChanges
, bob_account
, alice_account
, carole_account
, NULL
);
265 FillAllChanges(changes
);
266 FeedChangesToMulti(AliceChanges
, bob_account
, carole_account
, david_account
, NULL
);
267 FeedChangesToMulti(BobChanges
, alice_account
, carole_account
, david_account
, NULL
);
268 FeedChangesToMulti(CarolChanges
, bob_account
, alice_account
, david_account
, NULL
);
269 FeedChangesToMulti(DavidChanges
, bob_account
, alice_account
, carole_account
, NULL
);
271 FillAllChanges(changes
);
272 FeedChangesToMulti(AliceChanges
, bob_account
, carole_account
, david_account
, NULL
);
273 FeedChangesToMulti(BobChanges
, alice_account
, carole_account
, david_account
, NULL
);
274 FeedChangesToMulti(CarolChanges
, bob_account
, alice_account
, david_account
, NULL
);
275 FeedChangesToMulti(DavidChanges
, bob_account
, alice_account
, carole_account
, NULL
); // Bob and carole see the final result.
277 accounts_agree_internal("Carole's in", bob_account
, alice_account
, false);
278 accounts_agree_internal("Carole's in - 2", bob_account
, carole_account
, false);
280 CFReleaseNull(bob_account
);
281 CFReleaseNull(alice_account
);
282 CFReleaseNull(carole_account
);
283 CFReleaseNull(david_account
);
285 SOSUnregisterAllTransportMessages();
286 SOSUnregisterAllTransportCircles();
287 SOSUnregisterAllTransportKeyParameters();
288 CFArrayRemoveAllValues(key_transports
);
289 CFArrayRemoveAllValues(circle_transports
);
290 CFArrayRemoveAllValues(message_transports
);
294 int secd_56_account_apply(int argc
, char *const *argv
)
296 plan_tests(kTestTestCount
);
298 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);