2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <Security/SecBase.h>
28 #include <Security/SecItem.h>
30 #include <CoreFoundation/CFDictionary.h>
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>
42 #include "secd_regressions.h"
43 #include "SOSTestDataSource.h"
45 #include "SOSRegressionUtilities.h"
46 #include <utilities/SecCFWrappers.h>
47 #include <Security/SecKeyPriv.h>
49 #include <securityd/SOSCloudCircleServer.h>
51 #if !TARGET_IPHONE_SIMULATOR
52 #include "SOSAccountTesting.h"
54 #include "SecdTestKeychainUtilities.h"
56 #if !TARGET_IPHONE_SIMULATOR
58 static CFDataRef
CopyBackupKeyForString(CFStringRef string
, CFErrorRef
*error
)
60 __block CFDataRef result
= NULL
;
61 CFStringPerformWithUTF8CFData(string
, ^(CFDataRef stringAsData
) {
62 result
= SOSCopyDeviceBackupPublicKey(stringAsData
, error
);
67 static int kTestTestCount
= 133;
69 static int kTestTestCount
= 1;
72 static void tests(void)
74 #if !TARGET_IPHONE_SIMULATOR
76 __block CFErrorRef error
= NULL
;
77 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
78 CFStringRef cfaccount
= CFSTR("test@test.org");
80 secd_test_setup_testviews(); // for running this test solo
82 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
83 SOSAccountRef alice_account
= CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
84 SOSAccountRef bob_account
= CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
86 CFDataRef alice_backup_key
= CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error
);
87 CFDataRef bob_backup_key
= CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error
);
89 // Bob wins writing at this point, feed the changes back to alice.
90 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 1, "updates");
92 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
95 ok(SOSAccountResetToOffering_wTxn(alice_account
, &error
), "Reset to offering (%@)", error
);
98 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
100 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
101 CFReleaseNull(error
);
103 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 1, "updates");
105 ok(SOSAccountJoinCircles_wTxn(bob_account
, &error
), "Bob Applies (%@)", error
);
106 CFReleaseNull(error
);
108 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
111 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
113 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
114 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
115 CFReleaseNull(error
);
116 CFReleaseNull(applicants
);
119 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 3, "updates");
121 CFArrayRef peers
= SOSAccountCopyPeers(alice_account
, &error
);
122 ok(peers
&& CFArrayGetCount(peers
) == 2, "See two peers %@ (%@)", peers
, error
);
123 CFReleaseNull(peers
);
125 is(SOSAccountUpdateView(alice_account
, kTestView1
, kSOSCCViewEnable
, &error
), kSOSCCViewMember
, "Enable view (%@)", error
);
126 CFReleaseNull(error
);
128 ok(SOSAccountCheckForRings(alice_account
, &error
), "Alice_account is good");
129 CFReleaseNull(error
);
131 is(SOSAccountUpdateView(bob_account
, kTestView1
, kSOSCCViewEnable
, &error
), kSOSCCViewMember
, "Enable view (%@)", error
);
132 CFReleaseNull(error
);
134 ok(SOSAccountCheckForRings(bob_account
, &error
), "Alice_account is good");
135 CFReleaseNull(error
);
137 ok(SOSAccountSetBackupPublicKey_wTxn(alice_account
, alice_backup_key
, &error
), "Set backup public key, alice (%@)", error
);
138 CFReleaseNull(error
);
140 ok(SOSAccountCheckForRings(alice_account
, &error
), "Alice_account is good");
141 CFReleaseNull(error
);
143 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account
, bob_backup_key
, &error
), "Set backup public key, alice (%@)", error
);
144 CFReleaseNull(error
);
146 ok(SOSAccountCheckForRings(bob_account
, &error
), "Alice_account is good");
147 CFReleaseNull(error
);
149 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account
, kTestView1
), "Is alice is in backup before sync?");
151 ok(SOSAccountCheckForRings(alice_account
, &error
), "Alice_account is good");
152 CFReleaseNull(error
);
154 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Is bob in the backup after sync? - 1");
156 ok(SOSAccountCheckForRings(bob_account
, &error
), "Alice_account is good");
157 CFReleaseNull(error
);
159 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 4, "updates");
162 ok(SOSAccountCheckForRings(alice_account
, &error
), "Alice_account is good");
163 CFReleaseNull(error
);
165 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account
, kTestView1
), "Is alice is in backup after sync?");
167 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "IS bob in the backup after sync");
169 ok(!SOSAccountIsLastBackupPeer(alice_account
, &error
), "Alice is not last backup peer");
170 CFReleaseNull(error
);
173 //Bob leaves the circle
175 ok(SOSAccountLeaveCircle(bob_account
, &error
), "Bob Leaves (%@)", error
);
176 CFReleaseNull(error
);
178 //Alice should kick Bob out of the backup!
179 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
181 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account
, kTestView1
), "Bob left the circle, Alice is not in the backup");
183 ok(SOSAccountIsLastBackupPeer(alice_account
, &error
), "Alice is last backup peer");
184 CFReleaseNull(error
);
185 ok(!SOSAccountIsLastBackupPeer(bob_account
, &error
), "Bob is not last backup peer");
186 CFReleaseNull(error
);
188 ok(testAccountPersistence(alice_account
), "Test Account->DER->Account Equivalence");
190 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account
, SOSFullPeerInfoGetPeerInfo(bob_account
->my_identity
), kTestView1
), "Bob is still in the backup!");
192 //Bob gets back into the circle
193 ok(SOSAccountJoinCircles_wTxn(bob_account
, &error
));
194 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
196 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
198 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
199 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
200 CFReleaseNull(error
);
201 CFReleaseNull(applicants
);
204 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 3, "updates");
208 is(SOSAccountUpdateView(bob_account
, kTestView1
, kSOSCCViewEnable
, &error
), kSOSCCViewMember
, "Enable view (%@)", error
);
209 CFReleaseNull(error
);
211 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Bob isn't in the backup yet");
213 ok(!SOSAccountIsLastBackupPeer(alice_account
, &error
), "Alice is not last backup peer - Bob still registers as one");
214 CFReleaseNull(error
);
216 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account
, bob_backup_key
, &error
), "Set backup public key, alice (%@)", error
);
218 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 1, "updates");
220 ok(!SOSAccountIsLastBackupPeer(alice_account
, &error
), "Alice is not last backup peer");
221 CFReleaseNull(error
);
224 //removing backup key for bob account
227 ok(SOSAccountRemoveBackupPublickey_wTxn(bob_account
, &error
), "Removing Bob's backup key (%@)", error
);
228 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 3, "updates");
230 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Bob's backup key is in the backup - should not be so!");
231 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account
, SOSFullPeerInfoGetPeerInfo(bob_account
->my_identity
), kTestView1
), "Bob is up to date in the backup!");
234 // Setting new backup public key for Bob
237 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account
, bob_backup_key
, &error
), "Set backup public key, alice (%@)", error
);
238 CFReleaseNull(error
);
240 is(SOSAccountUpdateView(bob_account
, kTestView1
, kSOSCCViewEnable
, &error
), kSOSCCViewMember
, "Enable view (%@)", error
);
241 ok(SOSAccountNewBKSBForView(bob_account
, kTestView1
, &error
), "Setting new backup public key for bob account failed: (%@)", error
);
243 //bob is in his own backup
244 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Bob's backup key is not in the backup");
245 //alice does not have bob in her backup
246 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account
, SOSFullPeerInfoGetPeerInfo(bob_account
->my_identity
), kTestView1
), "Bob is up to date in the backup - should not be so!");
248 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 5, "updates");
250 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Bob's backup key should be in the backup");
251 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account
, kTestView1
), "Alice is in the backup");
253 ok(SOSAccountResetToEmpty(alice_account
, &error
), "Reset circle to empty");
254 CFReleaseNull(error
);
255 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
256 ok(SOSAccountIsBackupRingEmpty(bob_account
, kTestView1
), "Bob should not be in the backup");
257 ok(SOSAccountIsBackupRingEmpty(alice_account
, kTestView1
), "Alice should not be in the backup");
259 CFReleaseNull(bob_account
);
260 CFReleaseNull(alice_account
);
261 CFReleaseNull(cfpassword
);
268 int secd_62_account_backup(int argc
, char *const *argv
)
270 plan_tests(kTestTestCount
);
272 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);