]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-56-account-apply.c
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-56-account-apply.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
27 #include <Security/SecBase.h>
28 #include <Security/SecItem.h>
29
30 #include <CoreFoundation/CFDictionary.h>
31
32 #include <Security/SecureObjectSync/SOSAccount.h>
33 #include <Security/SecureObjectSync/SOSCloudCircle.h>
34 #include <Security/SecureObjectSync/SOSInternal.h>
35 #include <Security/SecureObjectSync/SOSUserKeygen.h>
36 #include <Security/SecureObjectSync/SOSTransport.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 <securityd/SOSCloudCircleServer.h>
49
50 #include "SOSAccountTesting.h"
51
52 #include "SecdTestKeychainUtilities.h"
53
54 static int kTestTestCount = 230;
55
56 #define kAccountPasswordString ((uint8_t*) "FooFooFoo")
57 #define kAccountPasswordStringLen 10
58
59 static void tests(void)
60 {
61 CFErrorRef error = NULL;
62 CFDataRef cfpassword = CFDataCreate(NULL, kAccountPasswordString, kAccountPasswordStringLen);
63 CFStringRef cfaccount = CFSTR("test@test.org");
64
65 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
66
67 SOSAccountRef alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
68 SOSAccountRef bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
69 SOSAccountRef carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
70 SOSAccountRef david_account = CreateAccountForLocalChanges(CFSTR("David"), CFSTR("TestSource"));
71
72 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
73
74 // Bob wins writing at this point, feed the changes back to alice.
75 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
76
77 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
78 CFReleaseNull(error);
79
80 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
81 CFReleaseNull(error);
82
83 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
84 CFReleaseNull(cfpassword);
85 CFReleaseNull(error);
86
87 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
88 CFReleaseNull(error);
89
90 // Lost Application Scenario
91 is(ProcessChangesOnce(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
92
93 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
94 CFReleaseNull(error);
95
96 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole Applies too (%@)", error);
97 CFReleaseNull(error);
98
99 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
100
101 accounts_agree("alice and carole agree", alice_account, carole_account);
102 accounts_agree("alice and bob agree", alice_account, bob_account);
103
104 {
105 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
106
107 ok(applicants && CFArrayGetCount(applicants) == 2, "See two applicants %@ (%@)", applicants, error);
108 CFReleaseNull(error);
109 CFReleaseSafe(applicants);
110 }
111
112 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
113
114 accounts_agree("alice and carole agree", alice_account, carole_account);
115
116 CFReleaseNull(error);
117
118 {
119 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
120 ok(applicants && CFArrayGetCount(applicants) == 2, "See two applicants %@ (%@)", applicants, error);
121 ok(SOSAccountRejectApplicants(alice_account, applicants, &error), "Everyone out the pool");
122 CFReleaseNull(error);
123 CFReleaseSafe(applicants);
124 }
125
126 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
127
128 accounts_agree("alice and carole agree", alice_account, carole_account);
129
130 {
131 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
132 ok(applicants && CFArrayGetCount(applicants) == 0, "See no applicants %@ (%@)", applicants, error);
133 CFReleaseNull(error);
134 CFReleaseSafe(applicants);
135 }
136
137 ok(SOSAccountLeaveCircle(carole_account, &error), "Carole bails (%@)", error);
138 CFReleaseNull(error);
139
140 // Everyone but bob sees that carole bails.
141 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 1, "updates");
142
143
144 // Bob reapplies, but it's to an old circle.
145 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob asks again");
146 CFReleaseNull(error);
147
148 // Bob returns and we mix our split worlds up.
149 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
150
151 {
152 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
153 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicants %@ (%@)", applicants, error);
154 CFReleaseNull(error);
155 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Accept bob into the fold");
156 CFReleaseNull(error);
157 CFReleaseSafe(applicants);
158 }
159
160 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
161
162 is(countPeers(bob_account), 2, "Bob sees 2 valid peers after admission from re-apply");
163
164 accounts_agree("alice and bob agree", alice_account, bob_account);
165 accounts_agree_internal("alice and carole agree", alice_account, carole_account, false);
166
167
168 // Rejected Application Scenario
169 ok(SOSAccountJoinCircles_wTxn(david_account, &error), "Dave Applies (%@)", error);
170 CFReleaseNull(error);
171
172 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
173
174 accounts_agree_internal("alice and david agree", alice_account, david_account, false);
175
176 SOSAccountPurgePrivateCredential(alice_account);
177
178 {
179 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
180
181 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
182 ok(SOSAccountRejectApplicants(alice_account, applicants, &error), "Alice rejects (%@)", error);
183 CFReleaseNull(error);
184 CFReleaseNull(applicants);
185 }
186
187 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
188
189 accounts_agree_internal("alice and carole still agree after david is rejected", alice_account, carole_account, false);
190
191 cfpassword = CFDataCreate(NULL, kAccountPasswordString, kAccountPasswordStringLen);
192
193 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
194 CFReleaseNull(error);
195 CFReleaseNull(cfpassword);
196
197 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
198
199 accounts_agree("bob&alice pair", bob_account, alice_account);
200
201 ok(SOSAccountJoinCirclesAfterRestore_wTxn(carole_account, &error), "Carole cloud identiy joins (%@)", error);
202 CFReleaseNull(error);
203
204 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 4, "updates");
205
206 accounts_agree_internal("carole&alice pair", carole_account, alice_account, false);
207
208 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
209
210 CFReleaseNull(bob_account);
211 CFReleaseNull(alice_account);
212 CFReleaseNull(carole_account);
213 CFReleaseNull(david_account);
214
215 SOSUnregisterAllTransportMessages();
216 SOSUnregisterAllTransportCircles();
217 SOSUnregisterAllTransportKeyParameters();
218 CFArrayRemoveAllValues(key_transports);
219 CFArrayRemoveAllValues(circle_transports);
220 CFArrayRemoveAllValues(message_transports);
221
222 }
223
224 int secd_56_account_apply(int argc, char *const *argv)
225 {
226 plan_tests(kTestTestCount);
227
228 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
229
230 tests();
231
232 return 0;
233 }