2 // secd-59-account-cleanup.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
= 93;
38 static void tests(void)
40 CFErrorRef error
= NULL
;
41 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
42 CFStringRef cfaccount
= CFSTR("test@test.org");
43 CFStringRef circle_name
= CFSTR("TestSource");
45 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
47 SOSAccountRef alice_account
= CreateAccountForLocalChanges(changes
, CFSTR("Alice"), circle_name
);
48 SOSAccountRef bob_account
= CreateAccountForLocalChanges(changes
, CFSTR("Bob"), circle_name
);
49 SOSAccountRef carole_account
= CreateAccountForLocalChanges(changes
, CFSTR("Carole"), circle_name
);
51 ok(SOSAccountAssertUserCredentials(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
53 // Bob wins writing at this point, feed the changes back to alice.
55 FeedChangesToMulti(changes
, alice_account
, carole_account
, NULL
);
57 ok(SOSAccountAssertUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
60 ok(SOSAccountAssertUserCredentials(carole_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
62 ok(SOSAccountResetToOffering(alice_account
, &error
), "Reset to offering (%@)", error
);
65 FeedChangesTo(changes
, bob_account
);
67 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
70 FeedChangesTo(changes
, alice_account
);
73 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
75 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
76 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
78 CFReleaseNull(applicants
);
82 FeedChangesTo(changes
, bob_account
); // Bob sees he's accepted
84 FeedChangesTo(changes
, alice_account
); // Alice sees bob-concurring
86 ok(CFDictionaryGetCount(changes
) == 0, "We converged. (%@)", changes
);
88 accounts_agree("bob&alice pair", bob_account
, alice_account
);
90 CFArrayRef peers
= SOSAccountCopyPeers(alice_account
, &error
);
91 ok(peers
&& CFArrayGetCount(peers
) == 2, "See two peers %@ (%@)", peers
, error
);
94 SOSFullPeerInfoRef fpiAlice
= SOSAccountGetMyFullPeerInCircleNamed(alice_account
, circle_name
, NULL
);
95 CFStringRef alice_id
= CFStringCreateCopy(NULL
, SOSPeerInfoGetPeerID(SOSFullPeerInfoGetPeerInfo(fpiAlice
)));
97 ok(SOSAccountLeaveCircles(alice_account
, &error
), "Alice Leaves (%@)", error
);
99 CFReleaseNull(cfpassword
);
101 FeedChangesTo(changes
, bob_account
); // Bob sees alice bail.
103 is(CFDictionaryGetCountOfValue(changes
, kCFNull
),2, "2 Keys Nulled Out");
105 CFDictionaryForEach(changes
, ^(const void *key
, const void *value
) {
107 CFStringRef circle_name
= NULL
, from_name
= NULL
, to_name
= NULL
;
108 SOSKVSKeyType keytype
= SOSKVSKeyGetKeyTypeAndParse(key
, &circle_name
, &from_name
, &to_name
);
109 is(keytype
, kMessageKey
, "Expect only a message key");
110 bool testcmp
= CFEqualSafe(alice_id
, from_name
) || CFEqualSafe(alice_id
, to_name
);
111 ok(testcmp
, "Alice is from_name(%@) or to_name(%@)", from_name
, to_name
);
112 CFReleaseNull(circle_name
);
113 CFReleaseNull(from_name
);
114 CFReleaseNull(to_name
);
118 FeedChangesToMulti(changes
, alice_account
, carole_account
, NULL
);
120 accounts_agree("Alice bails", bob_account
, alice_account
);
121 accounts_agree("Alice bails", bob_account
, carole_account
);
123 SOSAccountCleanupRetirementTickets(bob_account
, 0, &error
);
124 is(CFDictionaryGetCountOfValue(changes
, kCFNull
),0, "0 Keys Nulled Out");
126 ok(SOSAccountJoinCircles(carole_account
, &error
), "Carole Applies (%@)", error
);
127 CFReleaseNull(error
);
129 FeedChangesTo(changes
, bob_account
);
132 CFArrayRef applicants
= SOSAccountCopyApplicants(bob_account
, &error
);
134 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
135 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts Carole (%@)", error
);
136 CFReleaseNull(error
);
137 CFReleaseNull(applicants
);
140 // Bob should not yet cleanup Alice's retirment here on his own since it hasn't been long enough
142 is(CFDictionaryGetCountOfValue(changes
, kCFNull
),0, "0 Keys Nulled Out");
144 FeedChangesTo(changes
, carole_account
); // Carole sees he's accepted
145 FeedChangesTo(changes
, bob_account
); // Bob sees she's all happy.
147 accounts_agree("Carole joins", bob_account
, carole_account
);
149 SOSAccountCleanupRetirementTickets(bob_account
, 0, &error
);
151 is(countPeers(bob_account
), 2, "Active peers after forced cleanup");
152 is(countActivePeers(bob_account
), 3, "Inactive peers after forced cleanup");
154 is(CFDictionaryGetCountOfValue(changes
, kCFNull
), 1, "1 Keys Nulled Out");
156 CFDictionaryForEach(changes
, ^(const void *key
, const void *value
) {
158 CFStringRef circle_name
= NULL
, retiree
= NULL
;
159 SOSKVSKeyType keytype
= SOSKVSKeyGetKeyTypeAndParse(key
, &circle_name
, &retiree
, NULL
);
160 is(keytype
, kRetirementKey
, "Expect only a retirement key");
161 ok(CFEqualSafe(alice_id
, retiree
), "Alice (%@) is retiree (%@)", alice_id
, retiree
);
162 CFReleaseNull(circle_name
);
163 CFReleaseNull(retiree
);
167 CFReleaseNull(alice_id
);
168 CFReleaseNull(carole_account
);
169 CFReleaseNull(bob_account
);
170 CFReleaseNull(alice_account
);
173 int secd_59_account_cleanup(int argc
, char *const *argv
)
175 plan_tests(kTestTestCount
);