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