2 * Copyright (c) 2013-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 "keychain/SecureObjectSync/SOSAccount.h"
33 #include <Security/SecureObjectSync/SOSCloudCircle.h>
34 #include "keychain/SecureObjectSync/SOSInternal.h"
35 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
36 #include "keychain/SecureObjectSync/SOSTransport.h"
41 #include "secd_regressions.h"
42 #include "SOSTestDataSource.h"
44 #include "SOSRegressionUtilities.h"
45 #include <utilities/SecCFWrappers.h>
46 #include <Security/SecKeyPriv.h>
48 #include "keychain/securityd/SOSCloudCircleServer.h"
50 #include "SOSAccountTesting.h"
52 #include "SecdTestKeychainUtilities.h"
56 #define kAccountPasswordString ((uint8_t*) "FooFooFoo")
57 #define kAccountPasswordStringLen 10
59 static void tests(void)
61 CFErrorRef error = NULL;
62 CFDataRef cfpassword = CFDataCreate(NULL, kAccountPasswordString, kAccountPasswordStringLen);
63 CFStringRef cfaccount = CFSTR("test@test.org");
65 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
67 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
68 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
69 SOSAccount* carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
70 SOSAccount* david_account = CreateAccountForLocalChanges(CFSTR("David"), CFSTR("TestSource"));
72 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
74 // Bob wins writing at this point, feed the changes back to alice.
75 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
77 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
80 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
83 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
84 CFReleaseNull(cfpassword);
87 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
90 // Lost Application Scenario
91 is(ProcessChangesOnce(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
93 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
96 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole Applies too (%@)", error);
99 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
101 accounts_agree("alice and carole agree", alice_account, carole_account);
102 accounts_agree("alice and bob agree", alice_account, bob_account);
105 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
107 ok(applicants && CFArrayGetCount(applicants) == 2, "See two applicants %@ (%@)", applicants, error);
108 CFReleaseNull(error);
109 CFReleaseSafe(applicants);
112 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
114 accounts_agree("alice and carole agree", alice_account, carole_account);
116 CFReleaseNull(error);
119 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
120 ok(applicants && CFArrayGetCount(applicants) == 2, "See two applicants %@ (%@)", applicants, error);
121 ok(SOSAccountRejectApplicants(alice_account, applicants, &error), "Everyone out the pool");
122 CFReleaseNull(error);
123 CFReleaseSafe(applicants);
126 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
128 accounts_agree("alice and carole agree", alice_account, carole_account);
131 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
132 ok(applicants && CFArrayGetCount(applicants) == 0, "See no applicants %@ (%@)", applicants, error);
133 CFReleaseNull(error);
134 CFReleaseSafe(applicants);
137 ok([carole_account.trust leaveCircle:carole_account err:&error], "Carole bails (%@)", error);
138 CFReleaseNull(error);
140 // Everyone but bob sees that carole bails.
141 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 1, "updates");
144 // Bob reapplies, but it's to an old circle.
145 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob asks again");
146 CFReleaseNull(error);
148 // Bob returns and we mix our split worlds up.
149 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
152 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
153 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicants %@ (%@)", applicants, error);
154 CFReleaseNull(error);
155 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Accept bob into the fold");
156 CFReleaseNull(error);
157 CFReleaseSafe(applicants);
160 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
162 is(countPeers(bob_account), 2, "Bob sees 2 valid peers after admission from re-apply");
164 accounts_agree("alice and bob agree", alice_account, bob_account);
165 accounts_agree_internal("alice and carole agree", alice_account, carole_account, false);
168 // Rejected Application Scenario
169 ok(SOSAccountJoinCircles_wTxn(david_account, &error), "Dave Applies (%@)", error);
170 CFReleaseNull(error);
172 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
174 accounts_agree_internal("alice and david agree", alice_account, david_account, false);
176 SOSAccountPurgePrivateCredential(alice_account);
179 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
181 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
182 ok(SOSAccountRejectApplicants(alice_account, applicants, &error), "Alice rejects (%@)", error);
183 CFReleaseNull(error);
184 CFReleaseNull(applicants);
187 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
189 accounts_agree_internal("alice and carole still agree after david is rejected", alice_account, carole_account, false);
191 cfpassword = CFDataCreate(NULL, kAccountPasswordString, kAccountPasswordStringLen);
193 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
194 CFReleaseNull(error);
195 CFReleaseNull(cfpassword);
197 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
199 accounts_agree("bob&alice pair", bob_account, alice_account);
201 ok(SOSAccountJoinCirclesAfterRestore_wTxn(carole_account, &error), "Carole cloud identiy joins (%@)", error);
202 CFReleaseNull(error);
204 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 4, "updates");
206 accounts_agree_internal("carole&alice pair", carole_account, alice_account, false);
208 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
216 int secd_56_account_apply(int argc, char *const *argv)
220 secd_test_setup_temp_keychain(__FUNCTION__, NULL);