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