]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-57-account-leave.m
Security-59754.80.3.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-57-account-leave.m
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 "keychain/SecureObjectSync/SOSAccount.h"
33 #include <Security/SecureObjectSync/SOSCloudCircle.h>
34 #include "keychain/SecureObjectSync/SOSInternal.h"
35 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
36 #include "keychain/SecureObjectSync/SOSTransport.h"
37 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
38
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 #include "secd_regressions.h"
43 #include "SOSTestDataSource.h"
44
45 #include "SOSRegressionUtilities.h"
46 #include <utilities/SecCFWrappers.h>
47 #include <Security/SecKeyPriv.h>
48
49 #include "keychain/securityd/SOSCloudCircleServer.h"
50
51 #include "SOSAccountTesting.h"
52
53 #include "SecdTestKeychainUtilities.h"
54
55 #if SOS_ENABLED
56
57
58 /*
59 static void trim_retirements_from_circle(SOSAccount* account) {
60 SOSAccountForEachCircle(account, ^(SOSCircleRef circle) {
61 SOSCircleRemoveRetired(circle, NULL);
62 });
63 }
64 */
65
66
67 static void tests(void)
68 {
69 CFErrorRef error = NULL;
70 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
71 CFStringRef cfaccount = CFSTR("test@test.org");
72
73 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
74
75 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
76 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
77 SOSAccount* carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
78 SOSAccount* david_account = CreateAccountForLocalChanges(CFSTR("David"), CFSTR("TestSource"));
79
80 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account , cfaccount, cfpassword, &error), "Credential setting (%@)", error);
81
82 // Bob wins writing at this point, feed the changes back to alice.
83
84 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
85
86 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account , cfaccount, cfpassword, &error), "Credential setting (%@)", error);
87 CFReleaseNull(error);
88
89 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
90 CFReleaseNull(error);
91
92 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
93 CFReleaseNull(error);
94
95 ok(SOSAccountResetToOffering_wTxn(alice_account , &error), "Reset to offering (%@)", error);
96 CFReleaseNull(error);
97
98 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
99
100 ok(SOSAccountJoinCircles_wTxn(bob_account , &error), "Bob Applies (%@)", error);
101 CFReleaseNull(error);
102
103 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
104
105 {
106 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
107
108 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
109 ok(SOSAccountAcceptApplicants(alice_account , applicants, &error), "Alice accepts (%@)", error);
110 CFReleaseNull(error);
111 CFReleaseNull(applicants);
112 }
113
114
115 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
116
117 accounts_agree("bob&alice pair", bob_account, alice_account);
118 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSNeverLeftCircle, "Bob affirms he hasn't left.");
119
120 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
121 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
122 CFReleaseNull(peers);
123
124 SOSAccountPurgePrivateCredential(alice_account);
125
126 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
127 CFReleaseNull(error);
128
129 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
130
131 accounts_agree("Alice bails", bob_account, alice_account);
132
133 {
134 CFArrayRef concurring = SOSAccountCopyConcurringPeers(alice_account, &error);
135
136 ok(concurring && CFArrayGetCount(concurring) == 2, "See two concurring %@ (%@)", concurring, error);
137 CFReleaseNull(error);
138 CFReleaseNull(concurring);
139 }
140
141 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
142 CFReleaseNull(error);
143
144 ok(SOSAccountJoinCircles_wTxn(alice_account , &error), "Alice re-applies (%@)", error);
145 CFReleaseNull(error);
146
147 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
148
149 is(countActivePeers(bob_account), 3, "Bob sees 2 active peers");
150 is(countPeers(bob_account), 1, "Bob sees 1 valid peer");
151
152 {
153 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
154
155 ok(applicants && CFArrayGetCount(applicants) == 1, "See alice's reapp. %@ (%@)", applicants, error);
156 ok(SOSAccountAcceptApplicants(bob_account , applicants, &error), "Bob accepts (%@)", error);
157 CFReleaseNull(error);
158 CFReleaseNull(applicants);
159 }
160
161
162 is(countActivePeers(bob_account), 3, "Bob sees 3 active peers");
163 is(countPeers(bob_account), 2, "Bob sees 2 valid peers");
164
165 is(countActivePeers(alice_account), 3, "Alice sees 2 active peers");
166 is(countPeers(alice_account), 1, "Alice sees 1 valid peers");
167
168 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
169
170 accounts_agree("Alice rejoined", bob_account, alice_account);
171 accounts_agree_internal("Alice rejoined, carole noticed", bob_account, carole_account, false);
172
173 ok(SOSAccountJoinCircles_wTxn(carole_account , &error), "Carole applies (%@)", error);
174 CFReleaseNull(error);
175
176 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
177
178 accounts_agree_internal("Carole applied", bob_account, alice_account, false);
179 accounts_agree_internal("Carole applied - 2", bob_account, carole_account, false);
180
181 {
182 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
183
184 ok(applicants && CFArrayGetCount(applicants) == 1, "See Carole's eapp. %@ (%@)", applicants, error);
185 ok(SOSAccountAcceptApplicants(bob_account , applicants, &error), "Bob accepts carole (%@)", error);
186 CFReleaseNull(error);
187 CFReleaseNull(applicants);
188 }
189
190 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 4, "updates");
191 // david ends up with a 3 peerinfo member circle.
192
193 accounts_agree_internal("Carole joined", bob_account, alice_account, false);
194 accounts_agree_internal("Carole joined - 2", bob_account, carole_account, false);
195
196 // Now test lost circle change when two leave simultaneously, needing us to see the retirement tickets
197
198 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
199 CFReleaseNull(error);
200
201 ok([carole_account.trust leaveCircle:carole_account err:&error], "carole Leaves (%@)", error);
202 CFReleaseNull(error);
203
204 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
205
206 is(countPeers(bob_account), 1, "Bob sees 1 valid peer");
207 is(countActivePeers(bob_account), 4, "Bob sees 4 active peers");
208
209 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
210
211 is(SOSAccountGetLastDepartureReason(carole_account, &error), kSOSWithdrewMembership, "Carole affirms she left on her own.");
212
213 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account , cfaccount, cfpassword, &error), "Credential setting (%@)", error);
214 CFReleaseNull(error);
215 CFReleaseNull(cfpassword);
216 is(countPeers(david_account), 1, "david sees 1 peers");
217 is(countActivePeers(david_account), 4, "david sees 4 active peers");
218
219 ok(SOSAccountJoinCircles_wTxn(david_account , &error), "David applies (%@)", error);
220 CFReleaseNull(error);
221 is(countPeers(david_account), 1, "david sees 1 peers");
222 is(countActivePeers(david_account), 4, "david sees 4 active peers");
223
224 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
225
226 {
227 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
228
229 ok(applicants && CFArrayGetCount(applicants) == 1, "See David's app. %@ (%@)", applicants, error);
230 ok(SOSAccountAcceptApplicants(bob_account , applicants, &error), "Bob accepts carole (%@)", error);
231 CFReleaseNull(error);
232 CFReleaseNull(applicants);
233 }
234
235 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
236
237 is(countPeers(bob_account), 2, "Bob sees 2 valid peer");
238 is(countActivePeers(bob_account), 3, "Bob sees 3 active peers");
239
240
241 // Now see that bob can call leave without his private key
242 SOSAccountPurgeIdentity(bob_account); // Hopefully this actually purges, no errors to process
243
244 ok([bob_account.trust leaveCircle:bob_account err:&error], "bob Leaves w/o credentials (%@)", error);
245 CFReleaseNull(error);
246
247 ok(![bob_account isInCircle:&error], "bob knows he's out (%@)", error);
248 CFReleaseNull(error);
249 alice_account = nil;
250 bob_account = nil;
251
252 SOSTestCleanup();
253 }
254
255 #endif
256
257 int secd_57_account_leave(int argc, char *const *argv)
258 {
259 #if SOS_ENABLED
260 plan_tests(191);
261 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
262 secd_test_clear_testviews();
263 tests();
264 #else
265 plan_tests(0);
266 #endif
267 return 0;
268 }