2 // sc-60-account-cloud-identity.c
5 // Created by Mitch Adler on 6/25/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
= 97;
39 static int countPeers(SOSAccountRef account
, bool active
) {
40 CFErrorRef error
= NULL
;
43 if(active
) peers
= SOSAccountCopyActivePeers(account
, &error
);
44 else peers
= SOSAccountCopyPeers(account
, &error
);
45 int retval
= (int) CFArrayGetCount(peers
);
52 static void tests(void)
54 CFErrorRef error
= NULL
;
55 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
56 CFStringRef cfaccount
= CFSTR("test@test.org");
58 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
60 SOSAccountRef alice_account
= CreateAccountForLocalChanges(changes
, CFSTR("Alice"), CFSTR("TestSource"));
61 SOSAccountRef bob_account
= CreateAccountForLocalChanges(changes
, CFSTR("Bob"), CFSTR("TestSource"));
62 SOSAccountRef carole_account
= CreateAccountForLocalChanges(changes
, CFSTR("Carole"), CFSTR("TestSource"));
63 SOSAccountRef david_account
= CreateAccountForLocalChanges(changes
, CFSTR("David"), CFSTR("TestSource"));
65 ok(SOSAccountAssertUserCredentials(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
67 // Bob wins writing at this point, feed the changes back to alice.
69 FeedChangesToMulti(changes
, alice_account
, carole_account
, david_account
, NULL
);
71 ok(SOSAccountAssertUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
74 ok(SOSAccountAssertUserCredentials(carole_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
77 ok(SOSAccountAssertUserCredentials(david_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
78 CFReleaseNull(cfpassword
);
81 ok(SOSAccountResetToOffering(alice_account
, &error
), "Reset to offering (%@)", error
);
84 // Lost Application Scenario
85 FeedChangesToMulti(changes
, bob_account
, carole_account
, NULL
);
87 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
90 ok(SOSAccountJoinCircles(carole_account
, &error
), "Carole Applies too (%@)", error
);
93 FeedChangesToMulti(changes
, alice_account
, bob_account
, NULL
);
94 FeedChangesToMulti(changes
, alice_account
, bob_account
, carole_account
, NULL
);
96 accounts_agree("alice and carole agree", alice_account
, carole_account
);
97 accounts_agree("alice and bob agree", alice_account
, bob_account
);
100 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
102 ok(applicants
&& CFArrayGetCount(applicants
) == 2, "See two applicants %@ (%@)", applicants
, error
);
103 CFReleaseNull(error
);
104 CFReleaseSafe(applicants
);
107 FeedChangesToMulti(changes
, bob_account
, carole_account
, NULL
);
108 FeedChangesToMulti(changes
, bob_account
, alice_account
, carole_account
, NULL
);
109 accounts_agree("alice and carole agree", alice_account
, carole_account
);
110 ok(CFDictionaryGetCount(changes
) == 0, "Nothing left to deal with (%@)", changes
);
111 CFReleaseNull(error
);
114 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
115 ok(applicants
&& CFArrayGetCount(applicants
) == 2, "See two applicants %@ (%@)", applicants
, error
);
116 ok(SOSAccountRejectApplicants(alice_account
, applicants
, &error
), "Everyone out the pool");
117 CFReleaseNull(error
);
118 CFReleaseSafe(applicants
);
121 FeedChangesToMulti(changes
, bob_account
, carole_account
, NULL
);
122 FeedChangesToMulti(changes
, bob_account
, alice_account
, carole_account
, NULL
);
123 accounts_agree("alice and carole agree", alice_account
, carole_account
);
124 ok(CFDictionaryGetCount(changes
) == 0, "Nothing left to deal with (%@)", changes
);
127 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
128 ok(applicants
&& CFArrayGetCount(applicants
) == 0, "See no applicants %@ (%@)", applicants
, error
);
129 CFReleaseNull(error
);
130 CFReleaseSafe(applicants
);
133 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob asks again");
134 CFReleaseNull(error
);
135 FeedChangesToMulti(changes
, bob_account
, alice_account
, carole_account
, NULL
);
136 ok(CFDictionaryGetCount(changes
) == 0, "Nothing left to deal with (%@)", changes
);
139 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
140 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicants %@ (%@)", applicants
, error
);
141 CFReleaseNull(error
);
142 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Accept bob into the fold");
143 CFReleaseNull(error
);
144 CFReleaseSafe(applicants
);
147 FeedChangesTo(changes
, bob_account
); // Countersign
148 FeedChangesToMulti(changes
, alice_account
, bob_account
, carole_account
, NULL
); // Everyone sees the fallout.
149 ok(CFDictionaryGetCount(changes
) == 0, "Nothing left to deal with (%@)", changes
);
154 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
156 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "Bob automatically re-applied %@ (%@)", applicants
, error
);
157 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
158 CFReleaseNull(error
);
159 CFReleaseNull(applicants
);
162 is(countPeers(alice_account
, 0), 3, "Bob is accepted after auto-reapply");
163 FeedChangesToMulti(changes
, bob_account
, carole_account
, david_account
, NULL
);
164 FeedChangesToMulti(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
);
165 FeedChangesToMulti(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
);
166 accounts_agree("alice and carole agree after bob gets in", alice_account
, carole_account
);
168 // Rejected Application Scenario
169 ok(SOSAccountJoinCircles(david_account
, &error
), "Dave Applies (%@)", error
);
170 CFReleaseNull(error
);
172 FeedChangesTo(changes
, alice_account
);
173 SOSAccountPurgePrivateCredential(alice_account
);
176 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
178 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
179 ok(SOSAccountRejectApplicants(alice_account
, applicants
, &error
), "Alice rejects (%@)", error
);
180 CFReleaseNull(error
);
181 CFReleaseNull(applicants
);
183 FeedChangesToMulti(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
);
184 FeedChangesToMulti(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
);
185 FeedChangesToMulti(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
);
186 accounts_agree("alice and carole still agree after david is rejected", alice_account
, carole_account
);
187 ok(SOSAccountTryUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
188 CFReleaseNull(error
);
193 FeedChangesToMulti(changes
, alice_account
, carole_account
, NULL
); // Everyone sees conurring circle
195 ok(CFDictionaryGetCount(changes
) == 0, "We converged. (%@)", changes
);
197 accounts_agree("bob&alice pair", bob_account
, alice_account
);
199 ok(SOSAccountJoinCirclesAfterRestore(carole_account
, &error
), "Carole cloud identiy joins (%@)", error
);
200 CFReleaseNull(error
);
202 is(countPeers(carole_account
, false), 3, "Carole sees 3 valid peers after sliding in");
204 FeedChangesTo(changes
, bob_account
);
205 FeedChangesTo(changes
, alice_account
);
206 FeedChangesToMulti(changes
, bob_account
, carole_account
, NULL
); // Bob and carole see the final result.
208 accounts_agree_internal("Carole's in", bob_account
, alice_account
, false);
209 accounts_agree_internal("Carole's in - 2", bob_account
, carole_account
, false);
211 CFReleaseNull(bob_account
);
212 CFReleaseNull(alice_account
);
213 CFReleaseNull(carole_account
);
216 int secd_56_account_apply(int argc
, char *const *argv
)
218 plan_tests(kTestTestCount
);