1//
2//  sc-60-account-cloud-identity.c
3//  sec
4//
5//  Created by Mitch Adler on 6/25/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
36static int kTestTestCount = 95;
37
38
39
40static void tests(void)
41{
42    CFErrorRef error = NULL;
43    CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
44    CFStringRef cfaccount = CFSTR("test@test.org");
45
46    CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
47
48    SOSAccountRef alice_account = CreateAccountForLocalChanges(changes, CFSTR("Alice"), CFSTR("TestSource"));
49    SOSAccountRef bob_account = CreateAccountForLocalChanges(changes, CFSTR("Bob"), CFSTR("TestSource"));
50    SOSAccountRef carole_account = CreateAccountForLocalChanges(changes, CFSTR("Carole"), CFSTR("TestSource"));
51
52    ok(SOSAccountAssertUserCredentials(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
53
54    // Bob wins writing at this point, feed the changes back to alice.
55
56    FeedChangesToMulti(changes, alice_account, carole_account, NULL);
57
58    ok(SOSAccountAssertUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
59    CFReleaseNull(error);
60
61    ok(SOSAccountAssertUserCredentials(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
62    CFReleaseNull(error);
63    CFReleaseNull(cfpassword);
64    ok(SOSAccountResetToOffering(alice_account, &error), "Reset to offering (%@)", error);
65    CFReleaseNull(error);
66
67    FeedChangesToMulti(changes, bob_account, carole_account, NULL);
68
69    ok(SOSAccountJoinCircles(bob_account, &error), "Bob Applies (%@)", error);
70    CFReleaseNull(error);
71
72    FeedChangesTo(changes, alice_account);
73
74    {
75        CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
76
77        ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
78        ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
79        CFReleaseNull(error);
80        CFReleaseNull(applicants);
81    }
82
83
84    FeedChangesTo(changes, bob_account); // Bob sees he's accepted
85
86    FeedChangesToMulti(changes, alice_account, carole_account, NULL); // Everyone sees conurring circle
87
88    ok(CFDictionaryGetCount(changes) == 0, "We converged. (%@)", changes);
89
90    accounts_agree("bob&alice pair", bob_account, alice_account);
91
92    /*----- normal join after restore -----*/
93
94    ok(SOSAccountJoinCirclesAfterRestore(carole_account, &error), "Carole cloud identity joins (%@)", error);
95    CFReleaseNull(error);
96
97    FeedChangesToMulti(changes, bob_account, carole_account, NULL); // Bob and carole see the final result.
98    FeedChangesToMulti(changes, alice_account, bob_account, carole_account, NULL); // Bob and carole see the final result.
99
100    is(countApplicants(alice_account), 0, "See no applicants");
101
102    is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
103
104    FeedChangesToMulti(changes, bob_account, carole_account, NULL); // Bob and carole see the final result.
105
106    accounts_agree_internal("Carole's in", bob_account, alice_account, false);
107    accounts_agree_internal("Carole's in - 2", bob_account, carole_account, false);
108
109    ok(SOSAccountLeaveCircles(carole_account, &error), "Carol Leaves again");
110    CFReleaseNull(error);
111    FeedChangesToMulti(changes, bob_account, alice_account, NULL);
112    FeedChangesToMulti(changes, alice_account, bob_account, carole_account, NULL);
113
114    /*----- join - join after restore -----*/
115
116    ok(SOSAccountJoinCircles(carole_account, &error), "Carole normally joins (%@)", error);
117    CFReleaseNull(error);
118    FeedChangesTo(changes, alice_account);
119
120    is(countApplicants(alice_account), 1, "See one applicant");
121
122    ok(SOSAccountJoinCirclesAfterRestore(carole_account, &error), "Carole cloud identity joins (%@)", error);
123    CFReleaseNull(error);
124
125    FeedChangesToMulti(changes, bob_account, carole_account, NULL); // Bob and carole see the final result.
126    FeedChangesToMulti(changes, alice_account, bob_account, carole_account, NULL); // Bob and carole see the final result.
127
128    is(countApplicants(alice_account), 0, "See no applicants");
129
130    is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
131
132    FeedChangesToMulti(changes, bob_account, carole_account, NULL); // Bob and carole see the final result.
133
134    accounts_agree_internal("Carole's in", bob_account, alice_account, false);
135    accounts_agree_internal("Carole's in - 2", bob_account, carole_account, false);
136
137
138
139    CFReleaseNull(bob_account);
140    CFReleaseNull(alice_account);
141    CFReleaseNull(carole_account);
142}
143
144int secd_60_account_cloud_identity(int argc, char *const *argv)
145{
146    plan_tests(kTestTestCount);
147
148    tests();
149
150	return 0;
151}
152