]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-52-account-changed.c
Security-57740.51.3.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-52-account-changed.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 <Security/SecureObjectSync/SOSAccount.h>
30 #include <Security/SecureObjectSync/SOSCloudCircle.h>
31 #include <Security/SecureObjectSync/SOSInternal.h>
32 #include <Security/SecureObjectSync/SOSUserKeygen.h>
33
34 #include <stdlib.h>
35 #include <unistd.h>
36
37 #include "secd_regressions.h"
38 #include "SOSTestDataSource.h"
39
40 #include "SOSRegressionUtilities.h"
41 #include <utilities/SecCFWrappers.h>
42
43 #include <securityd/SOSCloudCircleServer.h>
44
45 #include "SecdTestKeychainUtilities.h"
46 #include "SOSAccountTesting.h"
47
48 static int kTestTestCount = 157;
49
50 static void tests(void)
51 {
52 CFErrorRef error = NULL;
53 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
54 CFStringRef cfaccount = CFSTR("test@test.org");
55
56 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
57
58 SOSAccountRef alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
59 SOSAccountRef bob_account = CreateAccountForLocalChanges( CFSTR("Bob"), CFSTR("TestSource"));
60 SOSAccountRef carol_account = CreateAccountForLocalChanges(CFSTR("Carol"), CFSTR("TestSource"));
61
62 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
63
64 // Bob wins writing at this point, feed the changes back to alice.
65 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "update");
66
67
68 CFDictionaryRef new_gestalt = SOSCreatePeerGestaltFromName(CFSTR("New Device"));
69 ok (SOSAccountUpdateGestalt(bob_account, new_gestalt), "did we send a null circle?");
70 CFReleaseNull(new_gestalt);
71
72 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "nothing published");
73
74 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
75 CFReleaseNull(error);
76 ok(SOSAccountAssertUserCredentialsAndUpdate(carol_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
77 CFReleaseNull(error);
78 CFReleaseNull(cfpassword);
79
80 /* ==================== Three Accounts setup =============================================*/
81
82 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
83 CFReleaseNull(error);
84
85 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "update");
86
87 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
88 CFReleaseNull(error);
89
90 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "update");
91
92
93 ok(SOSAccountJoinCircles_wTxn(carol_account, &error), "Carol Applies (%@)", error);
94 CFReleaseNull(error);
95
96 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "update");
97
98
99 {
100 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
101
102 ok(applicants && CFArrayGetCount(applicants) == 2, "See two applicants %@ (%@)", applicants, error);
103 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
104 CFReleaseNull(error);
105 CFReleaseNull(applicants);
106 }
107
108 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "update");
109
110 accounts_agree_internal("bob&alice pair", bob_account, alice_account, false);
111 accounts_agree_internal("bob&carol pair", bob_account, carol_account, false);
112 CFDictionaryRef alice_devstate = SOSTestSaveStaticAccountState(alice_account);
113 CFDictionaryRef bob_devstate = SOSTestSaveStaticAccountState(bob_account);
114 CFDictionaryRef carol_devstate = SOSTestSaveStaticAccountState(carol_account);
115
116 /* ==================== Three Accounts in circle =============================================*/
117 InjectChangeToMulti(changes, CFSTR("^AccountChanged"), CFSTR("none"), alice_account, bob_account, carol_account, NULL);
118
119 SOSTestRestoreAccountState(alice_account, alice_devstate);
120 SOSTestRestoreAccountState(bob_account, bob_devstate);
121 SOSTestRestoreAccountState(carol_account, carol_devstate);
122
123 CFReleaseNull(alice_devstate);
124 CFReleaseNull(bob_devstate);
125 CFReleaseNull(carol_devstate);
126
127 SOSAccountEnsureFactoryCirclesTest(alice_account, CFSTR("Alice"));
128 SOSAccountEnsureFactoryCirclesTest(bob_account, CFSTR("Bob"));
129 SOSAccountEnsureFactoryCirclesTest(carol_account, CFSTR("Carol"));
130
131 is(SOSAccountGetCircleStatus(alice_account, &error), kSOSCCError, "Account reset - no user keys - error");
132 is(SOSAccountGetCircleStatus(bob_account, &error), kSOSCCError, "Account reset - no user keys - error");
133 is(SOSAccountGetCircleStatus(carol_account, &error), kSOSCCError, "Account reset - no user keys - error");
134
135 CFDataRef cfpassword2 = CFDataCreate(NULL, (uint8_t *) "ooFooFooF", 10);
136 CFStringRef cfaccount2 = CFSTR("test2@test.org");
137
138 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount2, cfpassword2, &error), "Credential setting (%@)", error);
139 CFReleaseNull(error);
140
141 is(SOSAccountGetCircleStatus(alice_account, &error), kSOSCCCircleAbsent, "Account reset - circle is absent");
142 is(SOSAccountGetCircleStatus(bob_account, &error), kSOSCCError, "Account reset - no user keys - error");
143
144 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
145
146 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount2, cfpassword2, &error), "Credential setting (%@)", error);
147 CFReleaseNull(error);
148
149 ok(SOSAccountAssertUserCredentialsAndUpdate(carol_account, cfaccount2, cfpassword2, &error), "Credential setting (%@)", error);
150 CFReleaseNull(error);
151
152 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
153
154 is(SOSAccountGetCircleStatus(bob_account, &error), kSOSCCCircleAbsent, "Account reset - circle is absent");
155 is(SOSAccountGetCircleStatus(carol_account, &error), kSOSCCCircleAbsent, "Account reset - circle is absent");
156 // Now everyone is playing the same account.
157
158 /* ==================== Three Accounts setup =============================================*/
159
160 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
161 CFReleaseNull(error);
162 is(countActivePeers(alice_account), 2, "2 peers - alice and icloud");
163
164 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
165
166 is(SOSAccountGetCircleStatus(alice_account, &error), kSOSCCInCircle, "Alice is in circle");
167 is(SOSAccountGetCircleStatus(bob_account, &error), kSOSCCNotInCircle, "Bob is not in circle");
168 is(SOSAccountGetCircleStatus(carol_account, &error), kSOSCCNotInCircle, "Carol is not in circle");
169
170 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
171 CFReleaseNull(error);
172
173 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
174
175 ok(SOSAccountJoinCircles_wTxn(carol_account, &error), "Carol Applies (%@)", error);
176 CFReleaseNull(error);
177
178 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
179
180 is(SOSAccountGetCircleStatus(carol_account, &error), kSOSCCRequestPending, "Carol has a pending request");
181
182 {
183 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
184
185 ok(applicants && CFArrayGetCount(applicants) == 2, "See two applicants %@ (%@)", applicants, error);
186 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
187 CFReleaseNull(error);
188 CFReleaseNull(applicants);
189 is(countActivePeers(alice_account), 4, "4 peers - alice, bob, carol, and icloud");
190 }
191
192 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "updates");
193
194 accounts_agree_internal("bob&alice pair", bob_account, alice_account, false);
195 accounts_agree_internal("bob&carol pair", bob_account, carol_account, false);
196
197 CFReleaseSafe(cfpassword2);
198 CFReleaseNull(changes);
199 CFReleaseNull(alice_account);
200 CFReleaseNull(bob_account);
201 CFReleaseNull(carol_account);
202 SOSTestCleanup();
203
204 }
205
206
207 int secd_52_account_changed(int argc, char *const *argv)
208 {
209 plan_tests(kTestTestCount);
210
211 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
212
213 tests();
214
215 return 0;
216 }