]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-57-account-leave.c
Security-57740.31.2.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-57-account-leave.c
1 /*
2 * Copyright (c) 2013-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 <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>
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 #include "SecdTestKeychainUtilities.h"
53
54
55 static int kTestTestCount = 253;
56
57 /*
58 static void trim_retirements_from_circle(SOSAccountRef account) {
59 SOSAccountForEachCircle(account, ^(SOSCircleRef circle) {
60 SOSCircleRemoveRetired(circle, NULL);
61 });
62 }
63 */
64
65
66 static void tests(void)
67 {
68 CFErrorRef error = NULL;
69 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
70 CFStringRef cfaccount = CFSTR("test@test.org");
71
72 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
73
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"));
78
79 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account , cfaccount, cfpassword, &error), "Credential setting (%@)", error);
80
81 // Bob wins writing at this point, feed the changes back to alice.
82
83 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
84
85 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account , cfaccount, cfpassword, &error), "Credential setting (%@)", error);
86 CFReleaseNull(error);
87
88 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
89 CFReleaseNull(error);
90
91 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
92 CFReleaseNull(error);
93
94 ok(SOSAccountResetToOffering_wTxn(alice_account , &error), "Reset to offering (%@)", error);
95 CFReleaseNull(error);
96
97 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
98
99 ok(SOSAccountJoinCircles_wTxn(bob_account , &error), "Bob Applies (%@)", error);
100 CFReleaseNull(error);
101
102 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
103
104 {
105 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
106
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);
111 }
112
113
114 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
115
116 accounts_agree("bob&alice pair", bob_account, alice_account);
117 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSNeverLeftCircle, "Bob affirms he hasn't left.");
118
119 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
120 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
121 CFReleaseNull(peers);
122
123 SOSAccountPurgePrivateCredential(alice_account);
124
125 ok(SOSAccountLeaveCircle(alice_account , &error), "Alice Leaves (%@)", error);
126 CFReleaseNull(error);
127
128 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
129
130 accounts_agree("Alice bails", bob_account, alice_account);
131
132 {
133 CFArrayRef concurring = SOSAccountCopyConcurringPeers(alice_account, &error);
134
135 ok(concurring && CFArrayGetCount(concurring) == 2, "See two concurring %@ (%@)", concurring, error);
136 CFReleaseNull(error);
137 CFReleaseNull(concurring);
138 }
139
140 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
141 CFReleaseNull(error);
142
143 ok(SOSAccountJoinCircles_wTxn(alice_account , &error), "Alice re-applies (%@)", error);
144 CFReleaseNull(error);
145
146 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
147
148 is(countActivePeers(bob_account), 3, "Bob sees 2 active peers");
149 is(countPeers(bob_account), 1, "Bob sees 1 valid peer");
150
151 {
152 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
153
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);
158 }
159
160
161 is(countActivePeers(bob_account), 3, "Bob sees 3 active peers");
162 is(countPeers(bob_account), 2, "Bob sees 2 valid peers");
163
164 is(countActivePeers(alice_account), 3, "Alice sees 2 active peers");
165 is(countPeers(alice_account), 1, "Alice sees 1 valid peers");
166
167 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
168
169 accounts_agree("Alice rejoined", bob_account, alice_account);
170 accounts_agree_internal("Alice rejoined, carole noticed", bob_account, carole_account, false);
171
172 ok(SOSAccountJoinCircles_wTxn(carole_account , &error), "Carole applies (%@)", error);
173 CFReleaseNull(error);
174
175 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
176
177 accounts_agree_internal("Carole applied", bob_account, alice_account, false);
178 accounts_agree_internal("Carole applied - 2", bob_account, carole_account, false);
179
180 {
181 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
182
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);
187 }
188
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.
191
192 accounts_agree_internal("Carole joined", bob_account, alice_account, false);
193 accounts_agree_internal("Carole joined - 2", bob_account, carole_account, false);
194
195 // Now test lost circle change when two leave simultaneously, needing us to see the retirement tickets
196
197 ok(SOSAccountLeaveCircle(alice_account , &error), "Alice Leaves (%@)", error);
198 CFReleaseNull(error);
199
200 ok(SOSAccountLeaveCircle(carole_account , &error), "carole Leaves (%@)", error);
201 CFReleaseNull(error);
202
203 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
204
205 is(countPeers(bob_account), 1, "Bob sees 1 valid peer");
206 is(countActivePeers(bob_account), 4, "Bob sees 4 active peers");
207
208 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
209
210 is(SOSAccountGetLastDepartureReason(carole_account, &error), kSOSWithdrewMembership, "Carole affirms she left on her own.");
211
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");
217
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");
222
223 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
224
225 {
226 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
227
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);
232 }
233
234 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
235
236 is(countPeers(bob_account), 2, "Bob sees 2 valid peer");
237 is(countActivePeers(bob_account), 3, "Bob sees 3 active peers");
238
239
240 // Now see that bob can call leave without his private key
241 SOSAccountPurgeIdentity(bob_account); // Hopefully this actually purges, no errors to process
242
243 ok(SOSAccountLeaveCircle(bob_account, &error), "bob Leaves w/o credentials (%@)", error);
244 CFReleaseNull(error);
245
246 ok(!SOSAccountIsInCircle(bob_account, &error), "bob knows he's out (%@)", error);
247 CFReleaseNull(error);
248
249 CFReleaseNull(bob_account);
250 CFReleaseNull(alice_account);
251 CFReleaseNull(carole_account);
252 CFReleaseNull(david_account);
253
254 SOSUnregisterAllTransportMessages();
255 SOSUnregisterAllTransportCircles();
256 SOSUnregisterAllTransportKeyParameters();
257 CFArrayRemoveAllValues(key_transports);
258 CFArrayRemoveAllValues(circle_transports);
259 CFArrayRemoveAllValues(message_transports);
260 }
261
262 int secd_57_account_leave(int argc, char *const *argv)
263 {
264 plan_tests(kTestTestCount);
265
266 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
267
268 tests();
269
270 return 0;
271 }