]> git.saurik.com Git - apple/security.git/blob - Security/sec/securityd/Regressions/secd-55-account-circle.c
39b31e84891dcc5fd78b3d1de13d40be23f8340d
[apple/security.git] / Security / sec / securityd / Regressions / secd-55-account-circle.c
1 /*
2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25
26
27 #include <Security/SecBase.h>
28 #include <Security/SecItem.h>
29
30 #include <CoreFoundation/CFDictionary.h>
31
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>
37
38 #include <stdlib.h>
39 #include <unistd.h>
40
41 #include "secd_regressions.h"
42 #include "SOSTestDataSource.h"
43
44 #include "SOSRegressionUtilities.h"
45 #include <utilities/SecCFWrappers.h>
46 #include <Security/SecKeyPriv.h>
47
48 #include <securityd/SOSCloudCircleServer.h>
49
50 #include "SOSAccountTesting.h"
51
52 static int kTestTestCount = 304;
53
54 static void tests(void)
55 {
56 CFErrorRef error = NULL;
57 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
58 CFDataRef cfwrong_password = CFDataCreate(NULL, (uint8_t *) "NotFooFooFoo", 10);
59 CFStringRef cfaccount = CFSTR("test@test.org");
60
61 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
62 SOSAccountRef alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
63 SOSAccountRef bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
64 SOSAccountRef carol_account = CreateAccountForLocalChanges(CFSTR("Carol"), CFSTR("TestSource"));
65
66 ok(SOSAccountAssertUserCredentials(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
67
68 // Bob wins writing at this point, feed the changes back to alice.
69 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
70
71 ok(SOSAccountAssertUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
72 CFReleaseNull(error);
73 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential trying (%@)", error);
74 CFReleaseNull(error);
75 ok(!SOSAccountTryUserCredentials(alice_account, cfaccount, cfwrong_password, &error), "Credential failing (%@)", error);
76 CFReleaseNull(cfwrong_password);
77 is(error ? CFErrorGetCode(error) : 0, kSOSErrorWrongPassword, "Expected SOSErrorWrongPassword");
78 CFReleaseNull(error);
79
80 ok(SOSAccountResetToOffering(alice_account, &error), "Reset to offering (%@)", error);
81 CFReleaseNull(error);
82
83 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
84
85 ok(SOSAccountJoinCircles(bob_account, &error), "Bob Applies (%@)", error);
86 CFReleaseNull(error);
87
88 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
89
90 {
91 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
92
93 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
94 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
95 CFReleaseNull(error);
96 CFReleaseNull(applicants);
97 }
98
99 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
100
101 accounts_agree("bob&alice pair", bob_account, alice_account);
102
103 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
104 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
105 CFReleaseNull(peers);
106
107 CFDictionaryRef alice_new_gestalt = SOSCreatePeerGestaltFromName(CFSTR("Alice, but different"));
108
109 ok(SOSAccountUpdateGestalt(alice_account, alice_new_gestalt), "Update gestalt %@ (%@)", alice_account, error);
110 SOSAccountUpdateTestTransports(alice_account, alice_new_gestalt);
111 CFReleaseNull(alice_new_gestalt);
112
113 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
114
115 accounts_agree("Alice's name changed", bob_account, alice_account);
116
117 ok(SOSAccountLeaveCircles(alice_account, &error), "Alice Leaves (%@)", error);
118 CFReleaseNull(error);
119
120 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
121
122 accounts_agree("Alice bails", bob_account, alice_account);
123
124 peers = SOSAccountCopyPeers(alice_account, &error);
125 ok(peers && CFArrayGetCount(peers) == 1, "See one peer %@ (%@)", peers, error);
126 CFReleaseNull(peers);
127
128 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
129 CFReleaseNull(error);
130
131 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
132
133
134 {
135 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
136
137 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
138 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
139 CFReleaseNull(error);
140 CFReleaseNull(applicants);
141 }
142
143 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
144
145 accounts_agree("Alice accepts' Bob", bob_account, alice_account);
146
147 ok(SOSAccountLeaveCircles(alice_account, &error), "Alice Leaves (%@)", error);
148 CFReleaseNull(error);
149 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
150 CFReleaseNull(error);
151
152 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "updates");
153
154 {
155 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
156
157 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
158 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
159 CFReleaseNull(error);
160 CFReleaseNull(applicants);
161 }
162
163 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
164
165 accounts_agree("Bob accepts Alice", bob_account, alice_account);
166
167 // As of PR-13917727/PR-13906870 this no longer works (by "design"), in favor of making another more common
168 // failure (apply/OSX-psudo-reject/re-apply) work. Write races might be "fixed better" with affirmitave rejection.
169 #if 0
170
171 //
172 // Write race emulation.
173 //
174 //
175
176 ok(SOSAccountLeaveCircles(alice_account, &error), "Alice Leaves (%@)", error);
177 CFReleaseNull(error);
178 FeedChangesTo(changes, bob_account); // Bob sees Alice leaving and rejoining
179 FeedChangesTo(changes, alice_account); // Alice sees bob concurring
180
181 accounts_agree("Alice leaves & returns", bob_account, alice_account);
182
183 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
184 CFReleaseNull(error);
185
186 FeedChangesTo(changes, bob_account); // Bob sees Alice Applying
187
188 {
189 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
190
191 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
192 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
193 CFReleaseNull(error);
194 CFReleaseNull(applicants);
195 }
196
197 CFMutableDictionaryRef bobAcceptanceChanges = ExtractPendingChanges(changes);
198
199 // Alice re-applies without seeing that she was accepted.
200 ok(SOSAccountLeaveCircles(alice_account, &error), "Alice Leaves again (%@)", error);
201 CFReleaseNull(error);
202 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
203 CFReleaseNull(error);
204
205 CFReleaseSafe(ExtractPendingChanges(changes)); // Alice loses the race to write her changes - bob never sees em.
206
207 FeedChangesTo(&bobAcceptanceChanges, alice_account); // Alice sees bob inviting her in the circle.
208
209 FeedChangesTo(changes, bob_account); // Bob sees Alice Concurring
210
211 // As of PR-13917727/PR-13906870
212 accounts_agree("Alice leave, applies back, loses a race and eventually gets in", bob_account, alice_account);
213 #endif
214
215 // Both in circle.
216
217 // Emulation of <rdar://problem/13919554> Innsbruck11A368 +Roots: Device A was removed when Device B joined.
218
219 // We want Alice to leave circle while an Applicant on a full concordance signed circle with old-Alice as an Alum and Bob a peer.
220 // ZZZ
221 ok(SOSAccountLeaveCircles(alice_account, &error), "Alice leaves once more (%@)", error);
222 CFReleaseNull(error);
223
224 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
225 accounts_agree("Alice and Bob see Alice out of circle", bob_account, alice_account);
226
227 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
228 CFReleaseNull(error);
229
230 ok(SOSAccountLeaveCircles(alice_account, &error), "Alice leaves while applying (%@)", error);
231 FeedChangesTo(changes, bob_account); // Bob sees Alice become an Alum.
232
233 CFReleaseNull(error);
234
235 is(SOSAccountIsInCircles(alice_account, &error), kSOSCCNotInCircle, "Alice isn't applying any more");
236 accounts_agree("Alice leaves & some fancy concordance stuff happens", bob_account, alice_account);
237
238 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
239 CFReleaseNull(error);
240
241 FeedChangesTo(changes, bob_account); // Bob sees Alice reapply.
242
243 {
244 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
245
246 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
247 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
248 CFReleaseNull(error);
249 CFReleaseNull(applicants);
250 }
251 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
252
253 accounts_agree("Alice comes back", bob_account, alice_account);
254
255 // Emulation of <rdar://problem/13889901>
256 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
257
258 ok(SOSAccountAssertUserCredentials(carol_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
259 CFReleaseNull(cfpassword);
260 ok(SOSAccountJoinCircles(carol_account, &error), "Carol Applies (%@)", error);
261 CFReleaseNull(error);
262
263 CFMutableDictionaryRef dropped_changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
264 FillChanges(dropped_changes, carol_account);
265 CFReleaseNull(dropped_changes);
266
267 ok(SOSAccountResetToOffering(carol_account, &error), "Reset to offering (%@)", error);
268 CFReleaseNull(error);
269
270 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 2, "updates");
271 accounts_agree("13889901", carol_account, bob_account);
272 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSMembershipRevoked, "Bob affirms he hasn't left.");
273
274 ok(SOSAccountJoinCircles(bob_account, &error), "Bob ReApplies (%@)", error);
275 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 2, "updates");
276 {
277 CFArrayRef applicants = SOSAccountCopyApplicants(carol_account, &error);
278
279 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
280 ok(SOSAccountAcceptApplicants(carol_account, applicants, &error), "Carol accepts (%@)", error);
281 CFReleaseNull(error);
282 CFReleaseNull(applicants);
283 }
284 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 3, "updates");
285 accounts_agree("rdar://problem/13889901-II", bob_account, carol_account);
286
287 CFReleaseNull(alice_new_gestalt);
288
289 CFReleaseNull(bob_account);
290 CFReleaseNull(alice_account);
291 CFReleaseNull(carol_account);
292
293 SOSUnregisterAllTransportMessages();
294 SOSUnregisterAllTransportCircles();
295 SOSUnregisterAllTransportKeyParameters();
296 CFArrayRemoveAllValues(key_transports);
297 CFArrayRemoveAllValues(circle_transports);
298 CFArrayRemoveAllValues(message_transports);
299
300 }
301
302 int secd_55_account_circle(int argc, char *const *argv)
303 {
304 plan_tests(kTestTestCount);
305
306 tests();
307
308 return 0;
309 }