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 "keychain/SecureObjectSync/SOSAccount.h"
17 #include <Security/SecureObjectSync/SOSCloudCircle.h>
18 #include "keychain/SecureObjectSync/SOSInternal.h"
19 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
20 #include "keychain/SecureObjectSync/SOSTransport.h"
21 #include "keychain/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 void tests(void)
80 CFErrorRef error = NULL;
81 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
82 CFStringRef cfaccount = CFSTR("test@test.org");
84 SOSCircleRef c1 = SOSCircleCreateWithGenCount(1);
85 SOSCircleRef c99 = SOSCircleCreateWithGenCount(99);
86 ok(SOSCircleIsOlderGeneration(c1, c99), "Is Comparison working correctly?", NULL);
91 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
92 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
93 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
95 // Setup Circle with Bob and Alice in it
96 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
97 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
98 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
100 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
101 CFReleaseNull(error);
102 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
103 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
104 CFReleaseNull(error);
105 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
107 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
109 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
110 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
111 CFReleaseNull(error);
112 CFReleaseNull(applicants);
114 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
115 accounts_agree("bob&alice pair", bob_account, alice_account);
116 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
117 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
118 CFReleaseNull(peers);
120 uint64_t cnt = getCurrentGenCount(alice_account);
122 ok(SOSAccountResetWithGenCountValue(alice_account, cnt-1, &error), "Alice resets the circle to empty with old value");
123 CFReleaseNull(error);
125 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
126 is([bob_account getCircleStatus:&error], 0, "Bob Survives bad circle post");
127 is([alice_account getCircleStatus:&error], 1, "Alice does not survive bad circle post");
129 CFReleaseNull(cfpassword);
135 int secd_64_circlereset(int argc, char *const *argv)
139 secd_test_setup_temp_keychain(__FUNCTION__, NULL);