]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-61-account-leave-not-in-kansas-anymore.m
Security-59306.101.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 /*
55 static void trim_retirements_from_circle(SOSAccount* account) {
56 SOSAccountForEachCircle(account, ^(SOSCircleRef circle) {
57 SOSCircleRemoveRetired(circle, NULL);
58 });
59 }
60 */
61 static bool accept_applicants(SOSAccount* account, int count) {
62 CFErrorRef error = NULL;
63 CFArrayRef applicants = SOSAccountCopyApplicants(account, &error);
64 bool retval = false;
65 ok(applicants, "Have Applicants");
66 if(!applicants) goto errout;
67 is(CFArrayGetCount(applicants), count, "See applicants %@ (%@)", applicants, error);
68 if(CFArrayGetCount(applicants) != count) goto errout;
69 ok(retval = SOSAccountAcceptApplicants(account, applicants, &error), "Account accepts (%@)", error);
70 errout:
71 CFReleaseNull(error);
72 CFReleaseNull(applicants);
73 return retval;
74 }
75
76
77 static void tests(void)
78 {
79 CFErrorRef error = NULL;
80 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
81 CFStringRef cfaccount = CFSTR("test@test.org");
82
83 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
84
85 SOSAccount* alice_account = CreateAccountForLocalChanges ( CFSTR("Alice"), CFSTR("TestSource"));
86 SOSAccount* bob_account = CreateAccountForLocalChanges ( CFSTR("Bob"), CFSTR("TestSource"));
87 SOSAccount* carole_account = CreateAccountForLocalChanges ( CFSTR("Carole"), CFSTR("TestSource"));
88 SOSAccount* david_account = CreateAccountForLocalChanges ( CFSTR("David"), CFSTR("TestSource"));
89
90 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
91
92 // Bob wins writing at this point, feed the changes back to alice.
93 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
94
95 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
96 CFReleaseNull(error);
97
98 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
99 CFReleaseNull(error);
100
101 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
102 CFReleaseNull(error);
103
104 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
105 CFReleaseNull(error);
106
107 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
108
109
110 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
111 CFReleaseNull(error);
112
113 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
114
115
116 ok(accept_applicants(alice_account, 1), "Alice Accepts Application");
117
118 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
119
120 accounts_agree("bob&alice pair", bob_account, alice_account);
121 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSNeverLeftCircle, "Bob affirms he hasn't left.");
122
123 // ============================== Alice and Bob are in the Account. ============================================
124
125
126 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole Applies (%@)", error);
127 CFReleaseNull(error);
128 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 2, "updates");
129
130 ok(accept_applicants(alice_account, 1), "Alice Accepts Application");
131
132 // Let everyone concur.
133 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 3, "updates");
134
135 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
136
137 ok(peers && CFArrayGetCount(peers) == 3, "See three peers %@ (%@)", peers, error);
138 CFReleaseNull(peers);
139
140 // SOSAccountPurgePrivateCredential(alice_account);
141
142 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
143 CFReleaseNull(error);
144
145 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 2, "updates");
146
147
148 ok(SOSAccountJoinCircles_wTxn(david_account, &error), "David Applies (%@)", error);
149 CFReleaseNull(error);
150
151 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 2, "updates");
152
153 ok(accept_applicants(carole_account, 1), "Carole Accepts Application");
154
155 // ============================== We added Carole and David while Bob was in a drawer. Alice has left ============================================
156
157 // ============================== Bob comes out of the drawer seeing alice left and doesn't recognize the remainder. ============================================
158
159 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
160
161 CFReleaseNull(error);
162 is([carole_account getCircleStatus:&error], kSOSCCInCircle, "Carole still in Circle (%@)", error);
163 CFReleaseNull(error);
164 is([david_account getCircleStatus:&error], kSOSCCInCircle, "David still in Circle (%@)", error);
165 CFReleaseNull(error);
166 is([bob_account getCircleStatus:&error], kSOSCCNotInCircle, "Bob is not in Circle (%@)", error);
167 CFReleaseNull(error);
168 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSLeftUntrustedCircle, "Bob affirms he left because he doesn't know anyone.");
169 CFReleaseNull(error);
170 is([alice_account getCircleStatus:&error], kSOSCCNotInCircle, "Alice is not in Circle (%@)", error);
171 CFReleaseNull(error);
172 is(SOSAccountGetLastDepartureReason(alice_account, &error), kSOSWithdrewMembership, "Alice affirms she left by request.");
173 CFReleaseNull(error);
174
175 CFReleaseNull(cfpassword);
176 alice_account = nil;
177 bob_account = nil;
178 SOSTestCleanup();
179 }
180
181 int secd_61_account_leave_not_in_kansas_anymore(int argc, char *const *argv)
182 {
183 plan_tests(82);
184
185 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
186
187 tests();
188
189 return 0;
190 }