]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-61-account-leave-not-in-kansas-anymore.m
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-61-account-leave-not-in-kansas-anymore.m
1 /*
2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25
26 #include <Security/SecBase.h>
27 #include <Security/SecItem.h>
28
29 #include <CoreFoundation/CFDictionary.h>
30
31 #include "keychain/SecureObjectSync/SOSAccount.h"
32 #include <Security/SecureObjectSync/SOSCloudCircle.h>
33 #include "keychain/SecureObjectSync/SOSInternal.h"
34 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
35 #include "keychain/SecureObjectSync/SOSTransport.h"
36 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
37
38 #include <stdlib.h>
39 #include <unistd.h>
40
41 #include "secd_regressions.h"
42 #include "SOSTestDataSource.h"
43
44 #include "SOSRegressionUtilities.h"
45 #include <utilities/SecCFWrappers.h>
46 #include <Security/SecKeyPriv.h>
47
48 #include "keychain/securityd/SOSCloudCircleServer.h"
49
50 #include "SOSAccountTesting.h"
51
52 #include "SecdTestKeychainUtilities.h"
53
54 #if SOS_ENABLED
55
56 /*
57 static void trim_retirements_from_circle(SOSAccount* account) {
58 SOSAccountForEachCircle(account, ^(SOSCircleRef circle) {
59 SOSCircleRemoveRetired(circle, NULL);
60 });
61 }
62 */
63 static bool accept_applicants(SOSAccount* account, int count) {
64 CFErrorRef error = NULL;
65 CFArrayRef applicants = SOSAccountCopyApplicants(account, &error);
66 bool retval = false;
67 ok(applicants, "Have Applicants");
68 if(!applicants) goto errout;
69 is(CFArrayGetCount(applicants), count, "See applicants %@ (%@)", applicants, error);
70 if(CFArrayGetCount(applicants) != count) goto errout;
71 ok(retval = SOSAccountAcceptApplicants(account, applicants, &error), "Account accepts (%@)", error);
72 errout:
73 CFReleaseNull(error);
74 CFReleaseNull(applicants);
75 return retval;
76 }
77
78
79 static void tests(void)
80 {
81 CFErrorRef error = NULL;
82 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
83 CFStringRef cfaccount = CFSTR("test@test.org");
84
85 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
86
87 SOSAccount* alice_account = CreateAccountForLocalChanges ( CFSTR("Alice"), CFSTR("TestSource"));
88 SOSAccount* bob_account = CreateAccountForLocalChanges ( CFSTR("Bob"), CFSTR("TestSource"));
89 SOSAccount* carole_account = CreateAccountForLocalChanges ( CFSTR("Carole"), CFSTR("TestSource"));
90 SOSAccount* david_account = CreateAccountForLocalChanges ( CFSTR("David"), CFSTR("TestSource"));
91
92 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
93
94 // Bob wins writing at this point, feed the changes back to alice.
95 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
96
97 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
98 CFReleaseNull(error);
99
100 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
101 CFReleaseNull(error);
102
103 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
104 CFReleaseNull(error);
105
106 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
107 CFReleaseNull(error);
108
109 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
110
111
112 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
113 CFReleaseNull(error);
114
115 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
116
117
118 ok(accept_applicants(alice_account, 1), "Alice Accepts Application");
119
120 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
121
122 accounts_agree("bob&alice pair", bob_account, alice_account);
123 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSNeverLeftCircle, "Bob affirms he hasn't left.");
124
125 // ============================== Alice and Bob are in the Account. ============================================
126
127
128 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole Applies (%@)", error);
129 CFReleaseNull(error);
130 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 2, "updates");
131
132 ok(accept_applicants(alice_account, 1), "Alice Accepts Application");
133
134 // Let everyone concur.
135 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 3, "updates");
136
137 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
138
139 ok(peers && CFArrayGetCount(peers) == 3, "See three peers %@ (%@)", peers, error);
140 CFReleaseNull(peers);
141
142 // SOSAccountPurgePrivateCredential(alice_account);
143
144 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
145 CFReleaseNull(error);
146
147 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 2, "updates");
148
149
150 ok(SOSAccountJoinCircles_wTxn(david_account, &error), "David Applies (%@)", error);
151 CFReleaseNull(error);
152
153 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 2, "updates");
154
155 ok(accept_applicants(carole_account, 1), "Carole Accepts Application");
156
157 // ============================== We added Carole and David while Bob was in a drawer. Alice has left ============================================
158
159 // ============================== Bob comes out of the drawer seeing alice left and doesn't recognize the remainder. ============================================
160
161 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
162
163 CFReleaseNull(error);
164 is([carole_account getCircleStatus:&error], kSOSCCInCircle, "Carole still in Circle (%@)", error);
165 CFReleaseNull(error);
166 is([david_account getCircleStatus:&error], kSOSCCInCircle, "David still in Circle (%@)", error);
167 CFReleaseNull(error);
168 is([bob_account getCircleStatus:&error], kSOSCCNotInCircle, "Bob is not in Circle (%@)", error);
169 CFReleaseNull(error);
170 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSLeftUntrustedCircle, "Bob affirms he left because he doesn't know anyone.");
171 CFReleaseNull(error);
172 is([alice_account getCircleStatus:&error], kSOSCCNotInCircle, "Alice is not in Circle (%@)", error);
173 CFReleaseNull(error);
174 is(SOSAccountGetLastDepartureReason(alice_account, &error), kSOSWithdrewMembership, "Alice affirms she left by request.");
175 CFReleaseNull(error);
176
177 CFReleaseNull(cfpassword);
178 alice_account = nil;
179 bob_account = nil;
180 SOSTestCleanup();
181 }
182
183 #endif
184
185 int secd_61_account_leave_not_in_kansas_anymore(int argc, char *const *argv)
186 {
187 #if SOS_ENABLED
188 plan_tests(82);
189 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
190 tests();
191 #else
192 plan_tests(0);
193 #endif
194 return 0;
195 }