2 // secd-64-circlereset.c
5 // Created by Richard Murphy on 7/22/15.
11 #include <Security/SecBase.h>
12 #include <Security/SecItem.h>
14 #include <CoreFoundation/CFDictionary.h>
16 #include <Security/SecureObjectSync/SOSAccount.h>
17 #include <Security/SecureObjectSync/SOSCloudCircle.h>
18 #include <Security/SecureObjectSync/SOSInternal.h>
19 #include <Security/SecureObjectSync/SOSUserKeygen.h>
20 #include <Security/SecureObjectSync/SOSTransport.h>
21 #include <Security/SecureObjectSync/SOSAccountTrustClassic+Circle.h>
26 #include "secd_regressions.h"
27 #include "SOSTestDataSource.h"
29 #include "SOSRegressionUtilities.h"
30 #include <utilities/SecCFWrappers.h>
31 #include <Security/SecKeyPriv.h>
33 #include <securityd/SOSCloudCircleServer.h>
35 #include "SOSAccountTesting.h"
37 #include "SecdTestKeychainUtilities.h"
39 static int64_t getCurrentGenCount(SOSAccount* account) {
40 SOSAccountTrustClassic* trust = account.trust;
41 return SOSCircleGetGenerationSint(trust.trustedCircle);
44 static bool SOSAccountResetWithGenCountValue(SOSAccount* account, int64_t gcount, CFErrorRef* error) {
45 if (!SOSAccountHasPublicKey(account, error))
47 __block bool result = true;
48 SOSAccountTrustClassic* trust = account.trust;
50 result &= [account.trust resetAllRings:account err:error];
52 trust.fullPeerInfo = nil;
53 [trust setDepartureCode:kSOSWithdrewMembership];
55 result &= [account.trust modifyCircle:account.circle_transport err:error action:^(SOSCircleRef circle) {
56 SOSGenCountRef gencount = SOSGenerationCreateWithValue(gcount);
57 result = SOSCircleResetToEmpty(circle, error);
58 SOSCircleSetGeneration(circle, gencount);
59 CFReleaseNull(gencount);
64 secerror("error: %@", error ? *error : NULL);
70 static SOSCircleRef SOSCircleCreateWithGenCount(int64_t gcount) {
71 SOSCircleRef c = SOSCircleCreate(kCFAllocatorDefault, CFSTR("a"), NULL);
72 SOSGenCountRef gencount = SOSGenerationCreateWithValue(gcount);
73 SOSCircleSetGeneration(c, gencount);
74 CFReleaseNull(gencount);
78 static int kTestTestCount = 45;
80 static void tests(void)
82 CFErrorRef error = NULL;
83 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
84 CFStringRef cfaccount = CFSTR("test@test.org");
86 SOSCircleRef c1 = SOSCircleCreateWithGenCount(1);
87 SOSCircleRef c99 = SOSCircleCreateWithGenCount(99);
88 ok(SOSCircleIsOlderGeneration(c1, c99), "Is Comparison working correctly?", NULL);
93 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
94 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
95 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
97 // Setup Circle with Bob and Alice in it
98 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
99 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
100 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
101 CFReleaseNull(error);
102 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
103 CFReleaseNull(error);
104 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
105 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
106 CFReleaseNull(error);
107 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
109 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
111 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
112 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
113 CFReleaseNull(error);
114 CFReleaseNull(applicants);
116 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
117 accounts_agree("bob&alice pair", bob_account, alice_account);
118 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
119 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
120 CFReleaseNull(peers);
122 uint64_t cnt = getCurrentGenCount(alice_account);
124 ok(SOSAccountResetWithGenCountValue(alice_account, cnt-1, &error), "Alice resets the circle to empty with old value");
125 CFReleaseNull(error);
127 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
128 is([bob_account getCircleStatus:&error], 0, "Bob Survives bad circle post");
129 is([alice_account getCircleStatus:&error], 1, "Alice does not survive bad circle post");
131 CFReleaseNull(cfpassword);
137 int secd_64_circlereset(int argc, char *const *argv)
139 plan_tests(kTestTestCount);
141 secd_test_setup_temp_keychain(__FUNCTION__, NULL);