]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-61-account-leave-not-in-kansas-anymore.c
Security-57337.20.44.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-61-account-leave-not-in-kansas-anymore.c
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 <Security/SecureObjectSync/SOSAccount.h>
32 #include <Security/SecureObjectSync/SOSCloudCircle.h>
33 #include <Security/SecureObjectSync/SOSInternal.h>
34 #include <Security/SecureObjectSync/SOSUserKeygen.h>
35 #include <Security/SecureObjectSync/SOSTransport.h>
36
37 #include <stdlib.h>
38 #include <unistd.h>
39
40 #include "secd_regressions.h"
41 #include "SOSTestDataSource.h"
42
43 #include "SOSRegressionUtilities.h"
44 #include <utilities/SecCFWrappers.h>
45 #include <Security/SecKeyPriv.h>
46
47 #include <securityd/SOSCloudCircleServer.h>
48
49 #include "SOSAccountTesting.h"
50
51 #include "SecdTestKeychainUtilities.h"
52
53 static int kTestTestCount = 118;
54
55 /*
56 static void trim_retirements_from_circle(SOSAccountRef account) {
57 SOSAccountForEachCircle(account, ^(SOSCircleRef circle) {
58 SOSCircleRemoveRetired(circle, NULL);
59 });
60 }
61 */
62 static bool accept_applicants(SOSAccountRef account, int count) {
63 CFErrorRef error = NULL;
64 CFArrayRef applicants = SOSAccountCopyApplicants(account, &error);
65 bool retval = false;
66 ok(applicants, "Have Applicants");
67 if(!applicants) goto errout;
68 is(CFArrayGetCount(applicants), count, "See applicants %@ (%@)", applicants, error);
69 if(CFArrayGetCount(applicants) != count) goto errout;
70 ok(retval = SOSAccountAcceptApplicants(account, applicants, &error), "Account accepts (%@)", error);
71 errout:
72 CFReleaseNull(error);
73 CFReleaseNull(applicants);
74 return retval;
75 }
76
77
78 static void tests(void)
79 {
80 CFErrorRef error = NULL;
81 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
82 CFStringRef cfaccount = CFSTR("test@test.org");
83
84 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
85
86 SOSAccountRef alice_account = CreateAccountForLocalChanges ( CFSTR("Alice"), CFSTR("TestSource"));
87 SOSAccountRef bob_account = CreateAccountForLocalChanges ( CFSTR("Bob"), CFSTR("TestSource"));
88 SOSAccountRef carole_account = CreateAccountForLocalChanges ( CFSTR("Carole"), CFSTR("TestSource"));
89 SOSAccountRef david_account = CreateAccountForLocalChanges ( CFSTR("David"), CFSTR("TestSource"));
90
91 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
92
93 // Bob wins writing at this point, feed the changes back to alice.
94 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
95
96 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
97 CFReleaseNull(error);
98
99 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
100 CFReleaseNull(error);
101
102 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
103 CFReleaseNull(error);
104
105 ok(SOSAccountResetToOffering(alice_account, &error), "Reset to offering (%@)", error);
106 CFReleaseNull(error);
107
108 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
109
110
111 ok(SOSAccountJoinCircles(bob_account, &error), "Bob Applies (%@)", error);
112 CFReleaseNull(error);
113
114 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
115
116
117 ok(accept_applicants(alice_account, 1), "Alice Accepts Application");
118
119 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
120
121 accounts_agree("bob&alice pair", bob_account, alice_account);
122 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSNeverLeftCircle, "Bob affirms he hasn't left.");
123
124 // ============================== Alice and Bob are in the Account. ============================================
125
126
127 ok(SOSAccountJoinCircles(carole_account, &error), "Carole Applies (%@)", error);
128 CFReleaseNull(error);
129 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 2, "updates");
130
131 ok(accept_applicants(alice_account, 1), "Alice Accepts Application");
132
133 // Let everyone concur.
134 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 3, "updates");
135
136 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
137
138 ok(peers && CFArrayGetCount(peers) == 3, "See three peers %@ (%@)", peers, error);
139 CFReleaseNull(peers);
140
141 // SOSAccountPurgePrivateCredential(alice_account);
142
143 ok(SOSAccountLeaveCircle(alice_account, &error), "Alice Leaves (%@)", error);
144 CFReleaseNull(error);
145
146 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 2, "updates");
147
148
149 ok(SOSAccountJoinCircles(david_account, &error), "David Applies (%@)", error);
150 CFReleaseNull(error);
151
152 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 2, "updates");
153
154 ok(accept_applicants(carole_account, 1), "Carole Accepts Application");
155
156 // ============================== We added Carole and David while Bob was in a drawer. Alice has left ============================================
157
158 // ============================== Bob comes out of the drawer seeing alice left and doesn't recognize the remainder. ============================================
159
160 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
161
162 CFReleaseNull(error);
163 is(SOSAccountGetCircleStatus(carole_account, &error), kSOSCCInCircle, "Carole still in Circle (%@)", error);
164 CFReleaseNull(error);
165 is(SOSAccountGetCircleStatus(david_account, &error), kSOSCCInCircle, "David still in Circle (%@)", error);
166 CFReleaseNull(error);
167 is(SOSAccountGetCircleStatus(bob_account, &error), kSOSCCNotInCircle, "Bob is not in Circle (%@)", error);
168 CFReleaseNull(error);
169 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSLeftUntrustedCircle, "Bob affirms he left because he doesn't know anyone.");
170 CFReleaseNull(error);
171 is(SOSAccountGetCircleStatus(alice_account, &error), kSOSCCNotInCircle, "Alice is not in Circle (%@)", error);
172 CFReleaseNull(error);
173 is(SOSAccountGetLastDepartureReason(alice_account, &error), kSOSWithdrewMembership, "Alice affirms she left by request.");
174 CFReleaseNull(error);
175
176
177 CFReleaseNull(carole_account);
178 CFReleaseNull(david_account);
179 CFReleaseNull(bob_account);
180 CFReleaseNull(alice_account);
181 CFReleaseNull(cfpassword);
182
183 SOSUnregisterAllTransportMessages();
184 SOSUnregisterAllTransportCircles();
185 SOSUnregisterAllTransportKeyParameters();
186 CFArrayRemoveAllValues(key_transports);
187 CFArrayRemoveAllValues(circle_transports);
188 CFArrayRemoveAllValues(message_transports);
189
190 }
191
192 int secd_61_account_leave_not_in_kansas_anymore(int argc, char *const *argv)
193 {
194 plan_tests(kTestTestCount);
195
196 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
197
198 tests();
199
200 return 0;
201 }