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