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