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"
51 #include "SecdTestKeychainUtilities.h"
55 static bool AssertCreds(SOSAccount* account,CFStringRef acct_name, CFDataRef password) {
56 CFErrorRef error = NULL;
58 ok((retval = SOSAccountAssertUserCredentialsAndUpdate(account, acct_name, password, &error)), "Credential setting (%@)", error);
63 static bool ResetToOffering(SOSAccount* account) {
64 CFErrorRef error = NULL;
66 ok((retval = SOSAccountResetToOffering_wTxn(account, &error)), "Reset to offering (%@)", error);
71 static bool JoinCircle(SOSAccount* account) {
72 CFErrorRef error = NULL;
74 ok((retval = SOSAccountJoinCircles_wTxn(account, &error)), "Join Circle (%@)", error);
79 static bool AcceptApplicants(SOSAccount* account, CFIndex cnt) {
80 CFErrorRef error = NULL;
82 CFArrayRef applicants = SOSAccountCopyApplicants(account, &error);
84 ok((retval = (applicants && CFArrayGetCount(applicants) == cnt)), "See applicants %@ (%@)", applicants, error);
85 if(retval) ok((retval = SOSAccountAcceptApplicants(account, applicants, &error)), "Accept Applicants (%@)", error);
86 CFReleaseNull(applicants);
91 static void tests(void)
93 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
94 CFStringRef cfaccount = CFSTR("test@test.org");
96 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
97 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
98 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
99 SOSAccount* carol_account = CreateAccountForLocalChanges(CFSTR("Carol"), CFSTR("TestSource"));
101 /* Set Initial Credentials and Parameters for the Syncing Circles ---------------------------------------*/
102 ok(AssertCreds(bob_account, cfaccount, cfpassword), "Setting credentials for Bob");
103 // Bob wins writing at this point, feed the changes back to alice.
105 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
107 ok(AssertCreds(alice_account, cfaccount, cfpassword), "Setting credentials for Alice");
108 ok(AssertCreds(carol_account, cfaccount, cfpassword), "Setting credentials for Carol");
109 CFReleaseNull(cfpassword);
111 /* Make Alice First Peer -------------------------------------------------------------------------------*/
112 ok(ResetToOffering(alice_account), "Reset to offering - Alice as first peer");
114 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
116 /* Bob Joins -------------------------------------------------------------------------------------------*/
117 ok(JoinCircle(bob_account), "Bob Applies");
119 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
121 /* Alice Accepts -------------------------------------------------------------------------------------------*/
122 ok(AcceptApplicants(alice_account, 1), "Alice Accepts Bob's Application");
124 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "4 updates");
125 accounts_agree("bob&alice pair", bob_account, alice_account);
127 /* Carol Applies -------------------------------------------------------------------------------------------*/
128 ok(JoinCircle(carol_account), "Carol Applies");
130 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
131 is(countPeers(alice_account), 2, "See two peers");
134 /* Change Password ------------------------------------------------------------------------------------------*/
135 CFDataRef cfnewpassword = CFDataCreate(NULL, (uint8_t *) "ooFooFooF", 10);
137 ok(AssertCreds(bob_account , cfaccount, cfnewpassword), "Credential resetting for Bob");
138 is(countPeers(bob_account), 2, "There are two valid peers - iCloud and Bob");
139 is(countActivePeers(bob_account), 3, "There are three active peers - bob, alice, and iCloud");
140 is(countActiveValidPeers(bob_account), 2, "There is two active valid peer - Bob and iCloud");
142 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
144 ok(AssertCreds(alice_account , cfaccount, cfnewpassword), "Credential resetting for Alice");
146 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
148 is(countPeers(alice_account), 2, "There are two peers - bob and alice");
149 is(countActiveValidPeers(alice_account), 3, "There are three active valid peers - alice, bob, and icloud");
151 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
153 accounts_agree("bob&alice pair", bob_account, alice_account);
154 is(countPeers(alice_account), 2, "There are two peers - bob and alice");
155 is(countActiveValidPeers(alice_account), 3, "There are three active valid peers - alice, bob, and icloud");
157 ok(AssertCreds(carol_account , cfaccount, cfnewpassword), "Credential resetting for Carol");
159 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
160 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
162 accounts_agree("bob&alice pair", bob_account, alice_account);
163 accounts_agree_internal("bob&carol pair", bob_account, carol_account, false);
165 ok(AcceptApplicants(alice_account , 1), "Alice Accepts Carol's Application");
167 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "updates");
169 accounts_agree_internal("bob&alice pair", bob_account, alice_account, false);
170 accounts_agree_internal("bob&carol pair", bob_account, carol_account, false);
171 accounts_agree_internal("carol&alice pair", alice_account, carol_account, false);
174 /* Change Password 2 ----------------------------------------------------------------------------------------*/
175 CFReleaseNull(cfnewpassword);
176 cfnewpassword = CFDataCreate(NULL, (uint8_t *) "ffoffoffo", 10);
179 ok(AssertCreds(bob_account , cfaccount, cfnewpassword), "Credential resetting for Bob");
180 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
182 is(countPeers(bob_account), 3, "There are three peers - Alice, Carol, Bob");
183 is(countActivePeers(bob_account), 4, "There are four active peers - bob, alice, carol and iCloud");
184 is(countActiveValidPeers(bob_account), 2, "There is two active valid peer - Bob and iCloud");
188 ok(AssertCreds(alice_account , cfaccount, cfnewpassword), "Credential resetting for Alice");
189 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
191 is(countPeers(alice_account), 3, "There are three peers - Alice, Carol, Bob");
192 is(countActivePeers(alice_account), 4, "There are four active peers - bob, alice, carol and iCloud");
193 is(countActiveValidPeers(alice_account), 3, "There are three active valid peers - alice, bob, and icloud");
197 ok(AssertCreds(carol_account , cfaccount, cfnewpassword), "Credential resetting for Carol");
198 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "updates");
200 is(countPeers(carol_account), 3, "There are three peers - Alice, Carol, Bob");
201 is(countActivePeers(carol_account), 4, "There are four active peers - bob, alice, carol and iCloud");
202 is(countActiveValidPeers(carol_account), 4, "There are three active valid peers - alice, bob, carol, and icloud");
204 accounts_agree_internal("bob&alice pair", bob_account, alice_account, false);
206 /* Change Password 3 - cause a parm lost update collision ----------------------------------------------------*/
207 CFReleaseNull(cfnewpassword);
208 cfnewpassword = CFDataCreate(NULL, (uint8_t *) "cococococ", 10);
210 ok(AssertCreds(bob_account , cfaccount, cfnewpassword), "Credential resetting for Bob");
211 ok(AssertCreds(alice_account , cfaccount, cfnewpassword), "Credential resetting for Alice");
212 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "updates");
214 is(countPeers(alice_account), 3, "There are three peers - Alice, Carol, Bob");
215 is(countActivePeers(alice_account), 4, "There are four active peers - bob, alice, carol and iCloud");
216 is(countActiveValidPeers(alice_account), 3, "There are three active valid peers - alice, bob, and icloud");
218 /* Change Password 4 - new peer changes the password and joins ----------------------------------------------------*/
219 CFReleaseNull(cfnewpassword);
220 cfnewpassword = CFDataCreate(NULL, (uint8_t *) "dodododod", 10);
222 SOSAccount* david_account = CreateAccountForLocalChanges(CFSTR("David"), CFSTR("TestSource"));
223 ok(AssertCreds(david_account , cfaccount, cfnewpassword), "Credential resetting for David");
224 is(ProcessChangesUntilNoChange(changes, david_account, NULL), 2, "updates");
225 is(countPeers(david_account), 3, "Still 3 peers");
228 ok(JoinCircle(david_account), "David Applies");
229 is(ProcessChangesUntilNoChange(changes, david_account, NULL), 2, "updates");
230 is(countPeers(david_account), 1, "Only David is in circle");
233 CFReleaseNull(cfnewpassword);
242 int secd_58_password_change(int argc, char *const *argv)
246 secd_test_setup_temp_keychain(__FUNCTION__, NULL);