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@
26 #include <Security/SecBase.h>
27 #include <Security/SecItem.h>
29 #include <CoreFoundation/CFDictionary.h>
31 #include "keychain/SecureObjectSync/SOSAccount.h"
32 #include <Security/SecureObjectSync/SOSCloudCircle.h>
33 #include "keychain/SecureObjectSync/SOSInternal.h"
34 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
35 #include "keychain/SecureObjectSync/SOSTransport.h"
36 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.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"
57 static void trim_retirements_from_circle(SOSAccount* account) {
58 SOSAccountForEachCircle(account, ^(SOSCircleRef circle) {
59 SOSCircleRemoveRetired(circle, NULL);
63 static bool accept_applicants(SOSAccount* account, int count) {
64 CFErrorRef error = NULL;
65 CFArrayRef applicants = SOSAccountCopyApplicants(account, &error);
67 ok(applicants, "Have Applicants");
68 if(!applicants) goto errout;
69 is(CFArrayGetCount(applicants), count, "See applicants %@ (%@)", applicants, error);
70 if(CFArrayGetCount(applicants) != count) goto errout;
71 ok(retval = SOSAccountAcceptApplicants(account, applicants, &error), "Account accepts (%@)", error);
74 CFReleaseNull(applicants);
79 static void tests(void)
81 CFErrorRef error = NULL;
82 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
83 CFStringRef cfaccount = CFSTR("test@test.org");
85 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
87 SOSAccount* alice_account = CreateAccountForLocalChanges ( CFSTR("Alice"), CFSTR("TestSource"));
88 SOSAccount* bob_account = CreateAccountForLocalChanges ( CFSTR("Bob"), CFSTR("TestSource"));
89 SOSAccount* carole_account = CreateAccountForLocalChanges ( CFSTR("Carole"), CFSTR("TestSource"));
90 SOSAccount* david_account = CreateAccountForLocalChanges ( CFSTR("David"), CFSTR("TestSource"));
92 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
94 // Bob wins writing at this point, feed the changes back to alice.
95 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 1, "updates");
97 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
100 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
101 CFReleaseNull(error);
103 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
104 CFReleaseNull(error);
106 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
107 CFReleaseNull(error);
109 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 2, "updates");
112 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
113 CFReleaseNull(error);
115 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
118 ok(accept_applicants(alice_account, 1), "Alice Accepts Application");
120 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
122 accounts_agree("bob&alice pair", bob_account, alice_account);
123 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSNeverLeftCircle, "Bob affirms he hasn't left.");
125 // ============================== Alice and Bob are in the Account. ============================================
128 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole Applies (%@)", error);
129 CFReleaseNull(error);
130 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 2, "updates");
132 ok(accept_applicants(alice_account, 1), "Alice Accepts Application");
134 // Let everyone concur.
135 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 3, "updates");
137 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
139 ok(peers && CFArrayGetCount(peers) == 3, "See three peers %@ (%@)", peers, error);
140 CFReleaseNull(peers);
142 // SOSAccountPurgePrivateCredential(alice_account);
144 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
145 CFReleaseNull(error);
147 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 2, "updates");
150 ok(SOSAccountJoinCircles_wTxn(david_account, &error), "David Applies (%@)", error);
151 CFReleaseNull(error);
153 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, david_account, NULL), 2, "updates");
155 ok(accept_applicants(carole_account, 1), "Carole Accepts Application");
157 // ============================== We added Carole and David while Bob was in a drawer. Alice has left ============================================
159 // ============================== Bob comes out of the drawer seeing alice left and doesn't recognize the remainder. ============================================
161 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, david_account, NULL), 3, "updates");
163 CFReleaseNull(error);
164 is([carole_account getCircleStatus:&error], kSOSCCInCircle, "Carole still in Circle (%@)", error);
165 CFReleaseNull(error);
166 is([david_account getCircleStatus:&error], kSOSCCInCircle, "David still in Circle (%@)", error);
167 CFReleaseNull(error);
168 is([bob_account getCircleStatus:&error], kSOSCCNotInCircle, "Bob is not in Circle (%@)", error);
169 CFReleaseNull(error);
170 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSLeftUntrustedCircle, "Bob affirms he left because he doesn't know anyone.");
171 CFReleaseNull(error);
172 is([alice_account getCircleStatus:&error], kSOSCCNotInCircle, "Alice is not in Circle (%@)", error);
173 CFReleaseNull(error);
174 is(SOSAccountGetLastDepartureReason(alice_account, &error), kSOSWithdrewMembership, "Alice affirms she left by request.");
175 CFReleaseNull(error);
177 CFReleaseNull(cfpassword);
185 int secd_61_account_leave_not_in_kansas_anymore(int argc, char *const *argv)
189 secd_test_setup_temp_keychain(__FUNCTION__, NULL);