]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-62-account-backup.c
Security-57337.50.23.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-62-account-backup.c
1 /*
2 * Copyright (c) 2012-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 #include <Security/SecureObjectSync/SOSViews.h>
38
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 #include "secd_regressions.h"
43 #include "SOSTestDataSource.h"
44
45 #include "SOSRegressionUtilities.h"
46 #include <utilities/SecCFWrappers.h>
47 #include <Security/SecKeyPriv.h>
48
49 #include <securityd/SOSCloudCircleServer.h>
50
51 #if !TARGET_IPHONE_SIMULATOR
52 #include "SOSAccountTesting.h"
53 #endif
54 #include "SecdTestKeychainUtilities.h"
55
56 #if !TARGET_IPHONE_SIMULATOR
57
58 static CFDataRef CopyBackupKeyForString(CFStringRef string, CFErrorRef *error)
59 {
60 __block CFDataRef result = NULL;
61 CFStringPerformWithUTF8CFData(string, ^(CFDataRef stringAsData) {
62 result = SOSCopyDeviceBackupPublicKey(stringAsData, error);
63 });
64 return result;
65 }
66
67 static int kTestTestCount = 112;
68 #else
69 static int kTestTestCount = 1;
70 #endif
71
72 static void tests(void)
73 {
74 #if !TARGET_IPHONE_SIMULATOR
75
76 __block CFErrorRef error = NULL;
77 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
78 CFStringRef cfaccount = CFSTR("test@test.org");
79
80 CFStringRef kTestView1 = CFSTR("TestView1");
81 CFStringRef kTestView2 = CFSTR("TestView2");
82
83 CFMutableSetRef testViews = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
84 CFSetAddValue(testViews, kTestView1);
85 //CFSetAddValue(testViews, kTestView2);
86
87 SOSViewsSetTestViewsSet(testViews);
88
89 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
90 SOSAccountRef alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
91 SOSAccountRef bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
92
93 CFDataRef alice_backup_key = CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error);
94 CFDataRef bob_backup_key = CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error);
95
96 // Bob wins writing at this point, feed the changes back to alice.
97 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
98
99 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
100 CFReleaseNull(error);
101
102 ok(SOSAccountResetToOffering(alice_account, &error), "Reset to offering (%@)", error);
103 CFReleaseNull(error);
104
105 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
106
107 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
108 CFReleaseNull(error);
109
110 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
111
112 ok(SOSAccountJoinCircles(bob_account, &error), "Bob Applies (%@)", error);
113 CFReleaseNull(error);
114
115 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
116
117 {
118 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
119
120 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
121 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
122 CFReleaseNull(error);
123 CFReleaseNull(applicants);
124 }
125
126 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
127
128 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
129 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
130 CFReleaseNull(peers);
131
132
133
134 is(SOSAccountUpdateView(alice_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
135 CFReleaseNull(error);
136
137 is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
138 CFReleaseNull(error);
139
140 ok(SOSAccountSetBackupPublicKey(alice_account, alice_backup_key, &error), "Set backup public key, alice (%@)", error);
141 CFReleaseNull(error);
142
143 ok(SOSAccountSetBackupPublicKey(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
144 CFReleaseNull(error);
145
146 SOSAccountEnsureBackupStarts(alice_account);
147 SOSAccountEnsureBackupStarts(bob_account);
148
149 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice is in backup before sync?");
150
151 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Is bob in the backup after sync? - 1");
152
153 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 4, "updates");
154
155 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice is in backup after sync?");
156
157 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "IS bob in the backup after sync");
158
159 //
160 //Bob leaves the circle
161 //
162 ok(SOSAccountLeaveCircle(bob_account, &error), "Bob Leaves (%@)", error);
163 CFReleaseNull(error);
164
165 //Alice should kick Bob out of the backup!
166 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
167
168 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Bob left the circle, Alice is not in the backup");
169
170 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), kTestView1), "Bob is still in the backup!");
171
172 //Bob gets back into the circle
173 ok(SOSAccountJoinCircles(bob_account, &error));
174 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
175 {
176 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
177
178 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
179 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
180 CFReleaseNull(error);
181 CFReleaseNull(applicants);
182 }
183
184 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
185
186
187 //enables view
188 is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
189 CFReleaseNull(error);
190
191 ok(SOSAccountSetBackupPublicKey(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
192 SOSAccountEnsureBackupStarts(bob_account);
193
194 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
195
196
197 //
198 //removing backup key for bob account
199 //
200
201
202 ok(SOSAccountRemoveBackupPublickey(bob_account, &error), "Removing Bob's backup key (%@)", error);
203 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
204
205 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is in the backup - should not be so!");
206 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), kTestView1), "Bob is up to date in the backup!");
207
208 //
209 // Setting new backup public key for Bob
210 //
211
212 ok(SOSAccountSetBackupPublicKey(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
213 CFReleaseNull(error);
214 SOSAccountEnsureBackupStarts(bob_account);
215
216 is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
217 ok(SOSAccountStartNewBackup(bob_account, kTestView1, &error), "Setting new backup public key for bob account failed: (%@)", error);
218
219 //bob is in his own backup
220 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is not in the backup");
221 //alice does not have bob in her backup
222 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), kTestView1), "Bob is up to date in the backup - should not be so!");
223
224 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
225
226 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key should be in the backup");
227 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Alice is in the backup");
228
229 ok(SOSAccountResetToEmpty(alice_account, &error), "Reset circle to empty");
230 CFReleaseNull(error);
231 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
232 ok(SOSAccountIsBackupRingEmpty(bob_account, kTestView1), "Bob should not be in the backup");
233 ok(SOSAccountIsBackupRingEmpty(alice_account, kTestView1), "Alice should not be in the backup");
234
235 CFReleaseNull(bob_account);
236 CFReleaseNull(alice_account);
237 CFReleaseNull(cfpassword);
238
239 SOSViewsSetTestViewsSet(NULL);
240
241 SOSUnregisterAllTransportMessages();
242 SOSUnregisterAllTransportCircles();
243 SOSUnregisterAllTransportKeyParameters();
244 CFArrayRemoveAllValues(key_transports);
245 CFArrayRemoveAllValues(circle_transports);
246 CFArrayRemoveAllValues(message_transports);
247
248 CFReleaseNull(testViews);
249 CFReleaseNull(kTestView1);
250 CFReleaseNull(kTestView2);
251 #endif
252
253 }
254
255 int secd_62_account_backup(int argc, char *const *argv)
256 {
257 plan_tests(kTestTestCount);
258
259 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
260
261 tests();
262
263 return 0;
264 }