2 // sc-57-account-leave.c
5 // Created by Mitch Adler on 6/18/13.
11 #include <Security/SecBase.h>
12 #include <Security/SecItem.h>
14 #include <CoreFoundation/CFDictionary.h>
16 #include <SecureObjectSync/SOSAccount.h>
17 #include <SecureObjectSync/SOSCloudCircle.h>
18 #include <SecureObjectSync/SOSInternal.h>
19 #include <SecureObjectSync/SOSUserKeygen.h>
24 #include "secd_regressions.h"
25 #include "SOSTestDataSource.h"
27 #include "SOSRegressionUtilities.h"
28 #include <utilities/SecCFWrappers.h>
29 #include <Security/SecKeyPriv.h>
31 #include <securityd/SOSCloudCircleServer.h>
33 #include "SOSAccountTesting.h"
36 static int kTestTestCount
= 158;
39 static void trim_retirements_from_circle(SOSAccountRef account) {
40 SOSAccountForEachCircle(account, ^(SOSCircleRef circle) {
41 SOSCircleRemoveRetired(circle, NULL);
47 static void tests(void)
49 CFErrorRef error
= NULL
;
50 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
51 CFStringRef cfaccount
= CFSTR("test@test.org");
53 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
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"));
60 ok(SOSAccountAssertUserCredentials(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
62 // Bob wins writing at this point, feed the changes back to alice.
64 FeedChangesToMulti(changes
, alice_account
, carole_account
, david_account
, NULL
);
66 ok(SOSAccountAssertUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
69 ok(SOSAccountAssertUserCredentials(carole_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
72 ok(SOSAccountResetToOffering(alice_account
, &error
), "Reset to offering (%@)", error
);
75 FeedChangesTo(changes
, bob_account
);
77 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
80 FeedChangesTo(changes
, alice_account
);
83 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
85 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
86 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
88 CFReleaseNull(applicants
);
92 FeedChangesTo(changes
, bob_account
); // Bob sees he's accepted
94 FeedChangesTo(changes
, alice_account
); // Alice sees bob-concurring
96 ok(CFDictionaryGetCount(changes
) == 0, "We converged. (%@)", changes
);
98 accounts_agree("bob&alice pair", bob_account
, alice_account
);
99 is(SOSAccountGetLastDepartureReason(bob_account
, &error
), kSOSNeverLeftCircle
, "Bob affirms he hasn't left.");
101 CFArrayRef peers
= SOSAccountCopyPeers(alice_account
, &error
);
102 ok(peers
&& CFArrayGetCount(peers
) == 2, "See two peers %@ (%@)", peers
, error
);
103 CFReleaseNull(peers
);
105 SOSAccountPurgePrivateCredential(alice_account
);
107 ok(SOSAccountLeaveCircles(alice_account
, &error
), "Alice Leaves (%@)", error
);
108 CFReleaseNull(error
);
110 FeedChangesTo(changes
, bob_account
); // Bob sees alice bail.
112 FeedChangesTo(changes
, alice_account
); // Alice sees the fallout.
114 accounts_agree("Alice bails", bob_account
, alice_account
);
117 CFArrayRef concurring
= SOSAccountCopyConcurringPeers(alice_account
, &error
);
119 ok(concurring
&& CFArrayGetCount(concurring
) == 2, "See two concurring %@ (%@)", concurring
, error
);
120 CFReleaseNull(error
);
121 CFReleaseNull(concurring
);
124 ok(SOSAccountTryUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
125 CFReleaseNull(error
);
127 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
128 CFReleaseNull(error
);
130 FeedChangesTo(changes
, bob_account
); // Bob sees alice request.
132 is(countActivePeers(bob_account
), 3, "Bob sees 2 active peers");
133 is(countPeers(bob_account
), 1, "Bob sees 1 valid peer");
136 CFArrayRef applicants
= SOSAccountCopyApplicants(bob_account
, &error
);
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
);
145 is(countActivePeers(bob_account
), 3, "Bob sees 3 active peers");
146 is(countPeers(bob_account
), 2, "Bob sees 2 valid peers");
148 is(countActivePeers(alice_account
), 3, "Alice sees 2 active peers");
149 is(countPeers(alice_account
), 1, "Alice sees 1 valid peers");
151 FeedChangesToMulti(changes
, alice_account
, carole_account
, NULL
); // Alice sees bob accepts.
153 FeedChangesToMulti(changes
, bob_account
, carole_account
, NULL
); // Bob sees Alice concurr.
155 accounts_agree("Alice rejoined", bob_account
, alice_account
);
156 accounts_agree_internal("Alice rejoined, carole noticed", bob_account
, carole_account
, false);
158 ok(SOSAccountJoinCircles(carole_account
, &error
), "Carole applies (%@)", error
);
159 CFReleaseNull(error
);
161 FeedChangesToMulti(changes
, bob_account
, alice_account
, NULL
); // Bob and carole see the final result.
163 accounts_agree_internal("Carole applied", bob_account
, alice_account
, false);
164 accounts_agree_internal("Carole applied - 2", bob_account
, carole_account
, false);
167 CFArrayRef applicants
= SOSAccountCopyApplicants(bob_account
, &error
);
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
);
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.
180 accounts_agree_internal("Carole joined", bob_account
, alice_account
, false);
181 accounts_agree_internal("Carole joined - 2", bob_account
, carole_account
, false);
183 // Now test lost circle change when two leave simultaneously, needing us to see the retirement tickets
185 ok(SOSAccountLeaveCircles(alice_account
, &error
), "Alice Leaves (%@)", error
);
186 CFReleaseNull(error
);
188 ok(SOSAccountLeaveCircles(carole_account
, &error
), "carole Leaves (%@)", error
);
189 CFReleaseNull(error
);
191 FeedChangesToMulti(changes
, bob_account
, NULL
); // Bob sees both retirements and a circle missing one
193 is(countPeers(bob_account
), 1, "Bob sees 1 valid peer");
194 is(countActivePeers(bob_account
), 4, "Bob sees 4 active peers");
196 FeedChangesToMulti(changes
, carole_account
, alice_account
, david_account
, NULL
);
198 is(SOSAccountGetLastDepartureReason(carole_account
, &error
), kSOSWithdrewMembership
, "Carole affirms she left on her own.");
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");
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");
211 FeedChangesToMulti(changes
, carole_account
, alice_account
, bob_account
, NULL
);
214 CFArrayRef applicants
= SOSAccountCopyApplicants(bob_account
, &error
);
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
);
222 FeedChangesToMulti(changes
, carole_account
, alice_account
, david_account
, NULL
);
224 is(countPeers(bob_account
), 2, "Bob sees 2 valid peer");
225 is(countActivePeers(bob_account
), 3, "Bob sees 3 active peers");
227 CFReleaseNull(bob_account
);
228 CFReleaseNull(alice_account
);
231 int secd_57_account_leave(int argc
, char *const *argv
)
233 plan_tests(kTestTestCount
);