]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-62-account-backup.c
Security-57337.20.44.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 is(SOSAccountUpdateView(alice_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
133 CFReleaseNull(error);
134
135 is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
136 CFReleaseNull(error);
137
138 ok(SOSAccountSetBackupPublicKey(alice_account, alice_backup_key, &error), "Set backup public key, alice (%@)", error);
139 CFReleaseNull(error);
140
141 ok(SOSAccountSetBackupPublicKey(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
142 CFReleaseNull(error);
143
144 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice is in backup before sync?");
145
146 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Is bob in the backup after sync? - 1");
147
148 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 4, "updates");
149
150 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice is in backup after sync?");
151
152 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "IS bob in the backup after sync");
153
154 //
155 //Bob leaves the circle
156 //
157 ok(SOSAccountLeaveCircle(bob_account, &error), "Bob Leaves (%@)", error);
158 CFReleaseNull(error);
159
160 //Alice should kick Bob out of the backup!
161 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
162
163 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Bob left the circle, Alice is not in the backup");
164
165 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), kTestView1), "Bob is still in the backup!");
166
167 //Bob gets back into the circle
168 ok(SOSAccountJoinCircles(bob_account, &error));
169 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
170 {
171 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
172
173 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
174 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
175 CFReleaseNull(error);
176 CFReleaseNull(applicants);
177 }
178
179 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
180
181
182 //enables view
183 is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
184 CFReleaseNull(error);
185
186 ok(SOSAccountSetBackupPublicKey(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
187 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
188
189
190 //
191 //removing backup key for bob account
192 //
193
194
195 ok(SOSAccountRemoveBackupPublickey(bob_account, &error), "Removing Bob's backup key (%@)", error);
196 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
197
198 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is in the backup - should not be so!");
199 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), kTestView1), "Bob is up to date in the backup!");
200
201 //
202 // Setting new backup public key for Bob
203 //
204
205 ok(SOSAccountSetBackupPublicKey(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
206 CFReleaseNull(error);
207
208 is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
209 ok(SOSAccountStartNewBackup(bob_account, kTestView1, &error), "Setting new backup public key for bob account failed: (%@)", error);
210
211 //bob is in his own backup
212 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is not in the backup");
213 //alice does not have bob in her backup
214 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), kTestView1), "Bob is up to date in the backup - should not be so!");
215
216 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
217
218 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key should be in the backup");
219 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Alice is in the backup");
220
221 ok(SOSAccountResetToEmpty(alice_account, &error), "Reset circle to empty");
222 CFReleaseNull(error);
223 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
224 ok(SOSAccountIsBackupRingEmpty(bob_account, kTestView1), "Bob should not be in the backup");
225 ok(SOSAccountIsBackupRingEmpty(alice_account, kTestView1), "Alice should not be in the backup");
226
227 CFReleaseNull(bob_account);
228 CFReleaseNull(alice_account);
229 CFReleaseNull(cfpassword);
230
231 SOSViewsSetTestViewsSet(NULL);
232
233 SOSUnregisterAllTransportMessages();
234 SOSUnregisterAllTransportCircles();
235 SOSUnregisterAllTransportKeyParameters();
236 CFArrayRemoveAllValues(key_transports);
237 CFArrayRemoveAllValues(circle_transports);
238 CFArrayRemoveAllValues(message_transports);
239
240 CFReleaseNull(testViews);
241 CFReleaseNull(kTestView1);
242 CFReleaseNull(kTestView2);
243 #endif
244
245 }
246
247 int secd_62_account_backup(int argc, char *const *argv)
248 {
249 plan_tests(kTestTestCount);
250
251 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
252
253 tests();
254
255 return 0;
256 }