]> git.saurik.com Git - apple/security.git/blob - sec/securityd/Regressions/secd-57-account-leave.c
Security-55471.14.8.tar.gz
[apple/security.git] / sec / securityd / Regressions / secd-57-account-leave.c
1 //
2 // sc-57-account-leave.c
3 // sec
4 //
5 // Created by Mitch Adler on 6/18/13.
6 //
7 //
8
9
10
11 #include <Security/SecBase.h>
12 #include <Security/SecItem.h>
13
14 #include <CoreFoundation/CFDictionary.h>
15
16 #include <SecureObjectSync/SOSAccount.h>
17 #include <SecureObjectSync/SOSCloudCircle.h>
18 #include <SecureObjectSync/SOSInternal.h>
19 #include <SecureObjectSync/SOSUserKeygen.h>
20
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 #include "secd_regressions.h"
25 #include "SOSTestDataSource.h"
26
27 #include "SOSRegressionUtilities.h"
28 #include <utilities/SecCFWrappers.h>
29 #include <Security/SecKeyPriv.h>
30
31 #include <securityd/SOSCloudCircleServer.h>
32
33 #include "SOSAccountTesting.h"
34
35
36 static int kTestTestCount = 158;
37
38 /*
39 static void trim_retirements_from_circle(SOSAccountRef account) {
40 SOSAccountForEachCircle(account, ^(SOSCircleRef circle) {
41 SOSCircleRemoveRetired(circle, NULL);
42 });
43 }
44 */
45
46
47 static void tests(void)
48 {
49 CFErrorRef error = NULL;
50 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
51 CFStringRef cfaccount = CFSTR("test@test.org");
52
53 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
54
55 SOSAccountRef alice_account = CreateAccountForLocalChanges(changes, CFSTR("Alice"), CFSTR("TestSource"));
56 SOSAccountRef bob_account = CreateAccountForLocalChanges(changes, CFSTR("Bob"), CFSTR("TestSource"));
57 SOSAccountRef carole_account = CreateAccountForLocalChanges(changes, CFSTR("Carole"), CFSTR("TestSource"));
58 SOSAccountRef david_account = CreateAccountForLocalChanges(changes, CFSTR("David"), CFSTR("TestSource"));
59
60 ok(SOSAccountAssertUserCredentials(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
61
62 // Bob wins writing at this point, feed the changes back to alice.
63
64 FeedChangesToMulti(changes, alice_account, carole_account, david_account, NULL);
65
66 ok(SOSAccountAssertUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
67 CFReleaseNull(error);
68
69 ok(SOSAccountAssertUserCredentials(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
70 CFReleaseNull(error);
71
72 ok(SOSAccountResetToOffering(alice_account, &error), "Reset to offering (%@)", error);
73 CFReleaseNull(error);
74
75 FeedChangesTo(changes, bob_account);
76
77 ok(SOSAccountJoinCircles(bob_account, &error), "Bob Applies (%@)", error);
78 CFReleaseNull(error);
79
80 FeedChangesTo(changes, alice_account);
81
82 {
83 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
84
85 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
86 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
87 CFReleaseNull(error);
88 CFReleaseNull(applicants);
89 }
90
91
92 FeedChangesTo(changes, bob_account); // Bob sees he's accepted
93
94 FeedChangesTo(changes, alice_account); // Alice sees bob-concurring
95
96 ok(CFDictionaryGetCount(changes) == 0, "We converged. (%@)", changes);
97
98 accounts_agree("bob&alice pair", bob_account, alice_account);
99 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSNeverLeftCircle, "Bob affirms he hasn't left.");
100
101 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
102 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
103 CFReleaseNull(peers);
104
105 SOSAccountPurgePrivateCredential(alice_account);
106
107 ok(SOSAccountLeaveCircles(alice_account, &error), "Alice Leaves (%@)", error);
108 CFReleaseNull(error);
109
110 FeedChangesTo(changes, bob_account); // Bob sees alice bail.
111
112 FeedChangesTo(changes, alice_account); // Alice sees the fallout.
113
114 accounts_agree("Alice bails", bob_account, alice_account);
115
116 {
117 CFArrayRef concurring = SOSAccountCopyConcurringPeers(alice_account, &error);
118
119 ok(concurring && CFArrayGetCount(concurring) == 2, "See two concurring %@ (%@)", concurring, error);
120 CFReleaseNull(error);
121 CFReleaseNull(concurring);
122 }
123
124 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
125 CFReleaseNull(error);
126
127 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
128 CFReleaseNull(error);
129
130 FeedChangesTo(changes, bob_account); // Bob sees alice request.
131
132 is(countActivePeers(bob_account), 3, "Bob sees 2 active peers");
133 is(countPeers(bob_account), 1, "Bob sees 1 valid peer");
134
135 {
136 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
137
138 ok(applicants && CFArrayGetCount(applicants) == 1, "See alice's reapp. %@ (%@)", applicants, error);
139 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
140 CFReleaseNull(error);
141 CFReleaseNull(applicants);
142 }
143
144
145 is(countActivePeers(bob_account), 3, "Bob sees 3 active peers");
146 is(countPeers(bob_account), 2, "Bob sees 2 valid peers");
147
148 is(countActivePeers(alice_account), 3, "Alice sees 2 active peers");
149 is(countPeers(alice_account), 1, "Alice sees 1 valid peers");
150
151 FeedChangesToMulti(changes, alice_account, carole_account, NULL); // Alice sees bob accepts.
152
153 FeedChangesToMulti(changes, bob_account, carole_account, NULL); // Bob sees Alice concurr.
154
155 accounts_agree("Alice rejoined", bob_account, alice_account);
156 accounts_agree_internal("Alice rejoined, carole noticed", bob_account, carole_account, false);
157
158 ok(SOSAccountJoinCircles(carole_account, &error), "Carole applies (%@)", error);
159 CFReleaseNull(error);
160
161 FeedChangesToMulti(changes, bob_account, alice_account, NULL); // Bob and carole see the final result.
162
163 accounts_agree_internal("Carole applied", bob_account, alice_account, false);
164 accounts_agree_internal("Carole applied - 2", bob_account, carole_account, false);
165
166 {
167 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
168
169 ok(applicants && CFArrayGetCount(applicants) == 1, "See Carole's eapp. %@ (%@)", applicants, error);
170 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts carole (%@)", error);
171 CFReleaseNull(error);
172 CFReleaseNull(applicants);
173 }
174
175 FeedChangesToMulti(changes, alice_account, NULL); // Alice sees the change and countersigns
176 FeedChangesToMulti(changes, carole_account, NULL); // Carole countersigns her acceptance.
177 FeedChangesToMulti(changes, bob_account, alice_account, david_account, NULL); // Bob and Alice see carole's counter signature.
178 // david ends up with a 3 peerinfo member circle.
179
180 accounts_agree_internal("Carole joined", bob_account, alice_account, false);
181 accounts_agree_internal("Carole joined - 2", bob_account, carole_account, false);
182
183 // Now test lost circle change when two leave simultaneously, needing us to see the retirement tickets
184
185 ok(SOSAccountLeaveCircles(alice_account, &error), "Alice Leaves (%@)", error);
186 CFReleaseNull(error);
187
188 ok(SOSAccountLeaveCircles(carole_account, &error), "carole Leaves (%@)", error);
189 CFReleaseNull(error);
190
191 FeedChangesToMulti(changes, bob_account, NULL); // Bob sees both retirements and a circle missing one
192
193 is(countPeers(bob_account), 1, "Bob sees 1 valid peer");
194 is(countActivePeers(bob_account), 4, "Bob sees 4 active peers");
195
196 FeedChangesToMulti(changes, carole_account, alice_account, david_account, NULL);
197
198 is(SOSAccountGetLastDepartureReason(carole_account, &error), kSOSWithdrewMembership, "Carole affirms she left on her own.");
199
200 ok(SOSAccountAssertUserCredentials(david_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
201 CFReleaseNull(error);
202 CFReleaseNull(cfpassword);
203 is(countPeers(david_account), 1, "david sees 1 peers");
204 is(countActivePeers(david_account), 4, "david sees 4 active peers");
205
206 ok(SOSAccountJoinCircles(david_account, &error), "David applies (%@)", error);
207 CFReleaseNull(error);
208 is(countPeers(david_account), 1, "david sees 1 peers");
209 is(countActivePeers(david_account), 4, "david sees 4 active peers");
210
211 FeedChangesToMulti(changes, carole_account, alice_account, bob_account, NULL);
212
213 {
214 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
215
216 ok(applicants && CFArrayGetCount(applicants) == 1, "See David's app. %@ (%@)", applicants, error);
217 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts carole (%@)", error);
218 CFReleaseNull(error);
219 CFReleaseNull(applicants);
220 }
221
222 FeedChangesToMulti(changes, carole_account, alice_account, david_account, NULL);
223
224 is(countPeers(bob_account), 2, "Bob sees 2 valid peer");
225 is(countActivePeers(bob_account), 3, "Bob sees 3 active peers");
226
227 CFReleaseNull(bob_account);
228 CFReleaseNull(alice_account);
229 }
230
231 int secd_57_account_leave(int argc, char *const *argv)
232 {
233 plan_tests(kTestTestCount);
234
235 tests();
236
237 return 0;
238 }