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 <securityd/SOSCloudCircleServer.h>
50 #include "SOSAccountTesting.h"
52 #include "SecdTestKeychainUtilities.h"
54 #define kAccountPasswordString ((uint8_t*) "FooFooFoo")
55 #define kAccountPasswordStringLen 10
57 static void tests(void)
59 CFErrorRef error = NULL;
60 CFDataRef cfpassword = CFDataCreate(NULL, kAccountPasswordString, kAccountPasswordStringLen);
61 CFStringRef cfaccount = CFSTR("test@test.org");
63 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
65 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
66 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
67 SOSAccount* carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
68 SOSAccount* david_account = CreateAccountForLocalChanges(CFSTR("David"), CFSTR("TestSource"));
70 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
72 // Bob wins writing at this point, feed the changes back to alice.
73 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
75 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
78 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
81 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
82 CFReleaseNull(cfpassword);
85 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
88 // Lost Application Scenario
89 is(ProcessChangesOnce(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
91 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
94 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole Applies too (%@)", error);
97 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
99 accounts_agree("alice and carole agree", alice_account, carole_account);
100 accounts_agree("alice and bob agree", alice_account, bob_account);
103 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
105 ok(applicants && CFArrayGetCount(applicants) == 2, "See two applicants %@ (%@)", applicants, error);
106 CFReleaseNull(error);
107 CFReleaseSafe(applicants);
110 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
112 accounts_agree("alice and carole agree", alice_account, carole_account);
114 CFReleaseNull(error);
117 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
118 ok(applicants && CFArrayGetCount(applicants) == 2, "See two applicants %@ (%@)", applicants, error);
119 ok(SOSAccountRejectApplicants(alice_account, applicants, &error), "Everyone out the pool");
120 CFReleaseNull(error);
121 CFReleaseSafe(applicants);
124 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
126 accounts_agree("alice and carole agree", alice_account, carole_account);
129 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
130 ok(applicants && CFArrayGetCount(applicants) == 0, "See no applicants %@ (%@)", applicants, error);
131 CFReleaseNull(error);
132 CFReleaseSafe(applicants);
135 ok([carole_account.trust leaveCircle:carole_account err:&error], "Carole bails (%@)", error);
136 CFReleaseNull(error);
138 // Everyone but bob sees that carole bails.
139 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 1, "updates");
142 // Bob reapplies, but it's to an old circle.
143 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob asks again");
144 CFReleaseNull(error);
146 // Bob returns and we mix our split worlds up.
147 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
150 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
151 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicants %@ (%@)", applicants, error);
152 CFReleaseNull(error);
153 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Accept bob into the fold");
154 CFReleaseNull(error);
155 CFReleaseSafe(applicants);
158 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
160 is(countPeers(bob_account), 2, "Bob sees 2 valid peers after admission from re-apply");
162 accounts_agree("alice and bob agree", alice_account, bob_account);
163 accounts_agree_internal("alice and carole agree", alice_account, carole_account, false);
166 // Rejected Application Scenario
167 ok(SOSAccountJoinCircles_wTxn(david_account, &error), "Dave Applies (%@)", error);
168 CFReleaseNull(error);
170 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
172 accounts_agree_internal("alice and david agree", alice_account, david_account, false);
174 SOSAccountPurgePrivateCredential(alice_account);
177 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
179 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
180 ok(SOSAccountRejectApplicants(alice_account, applicants, &error), "Alice rejects (%@)", error);
181 CFReleaseNull(error);
182 CFReleaseNull(applicants);
185 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
187 accounts_agree_internal("alice and carole still agree after david is rejected", alice_account, carole_account, false);
189 cfpassword = CFDataCreate(NULL, kAccountPasswordString, kAccountPasswordStringLen);
191 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
192 CFReleaseNull(error);
193 CFReleaseNull(cfpassword);
195 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
197 accounts_agree("bob&alice pair", bob_account, alice_account);
199 ok(SOSAccountJoinCirclesAfterRestore_wTxn(carole_account, &error), "Carole cloud identiy joins (%@)", error);
200 CFReleaseNull(error);
202 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 4, "updates");
204 accounts_agree_internal("carole&alice pair", carole_account, alice_account, false);
206 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
213 int secd_56_account_apply(int argc, char *const *argv)
217 secd_test_setup_temp_keychain(__FUNCTION__, NULL);