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