]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-64-circlereset.m
Security-58286.240.4.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-64-circlereset.m
1 //
2 // secd-64-circlereset.c
3 // sec
4 //
5 // Created by Richard Murphy on 7/22/15.
6 //
7 //
8
9
10
11 #include <Security/SecBase.h>
12 #include <Security/SecItem.h>
13
14 #include <CoreFoundation/CFDictionary.h>
15
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>
22
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #include "secd_regressions.h"
27 #include "SOSTestDataSource.h"
28
29 #include "SOSRegressionUtilities.h"
30 #include <utilities/SecCFWrappers.h>
31 #include <Security/SecKeyPriv.h>
32
33 #include <securityd/SOSCloudCircleServer.h>
34
35 #include "SOSAccountTesting.h"
36
37 #include "SecdTestKeychainUtilities.h"
38
39 static int64_t getCurrentGenCount(SOSAccount* account) {
40 SOSAccountTrustClassic* trust = account.trust;
41 return SOSCircleGetGenerationSint(trust.trustedCircle);
42 }
43
44 static bool SOSAccountResetWithGenCountValue(SOSAccount* account, int64_t gcount, CFErrorRef* error) {
45 if (!SOSAccountHasPublicKey(account, error))
46 return false;
47 __block bool result = true;
48 SOSAccountTrustClassic* trust = account.trust;
49
50 result &= [account.trust resetAllRings:account err:error];
51
52 trust.fullPeerInfo = nil;
53 [trust setDepartureCode:kSOSWithdrewMembership];
54
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);
60 return result;
61 }];
62
63 if (!result) {
64 secerror("error: %@", error ? *error : NULL);
65 }
66
67 return result;
68 }
69
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);
75 return c;
76 }
77
78 static int kTestTestCount = 45;
79
80 static void tests(void)
81 {
82 CFErrorRef error = NULL;
83 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
84 CFStringRef cfaccount = CFSTR("test@test.org");
85
86 SOSCircleRef c1 = SOSCircleCreateWithGenCount(1);
87 SOSCircleRef c99 = SOSCircleCreateWithGenCount(99);
88 ok(SOSCircleIsOlderGeneration(c1, c99), "Is Comparison working correctly?", NULL);
89 CFReleaseNull(c1);
90 CFReleaseNull(c99);
91
92
93 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
94 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
95 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
96
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");
108 {
109 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
110
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);
115 }
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);
121
122 uint64_t cnt = getCurrentGenCount(alice_account);
123
124 ok(SOSAccountResetWithGenCountValue(alice_account, cnt-1, &error), "Alice resets the circle to empty with old value");
125 CFReleaseNull(error);
126
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");
130
131 CFReleaseNull(cfpassword);
132 alice_account = nil;
133 bob_account = nil;
134 SOSTestCleanup();
135 }
136
137 int secd_64_circlereset(int argc, char *const *argv)
138 {
139 plan_tests(kTestTestCount);
140
141 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
142
143 tests();
144
145 return 0;
146 }