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 <Security/SecureObjectSync/SOSAccount.h>
32 #include <Security/SecureObjectSync/SOSCloudCircle.h>
33 #include <Security/SecureObjectSync/SOSInternal.h>
34 #include <Security/SecureObjectSync/SOSUserKeygen.h>
35 #include <Security/SecureObjectSync/SOSTransport.h>
40 #include "secd_regressions.h"
41 #include "SOSTestDataSource.h"
43 #include "SOSRegressionUtilities.h"
44 #include <utilities/SecCFWrappers.h>
45 #include <Security/SecKeyPriv.h>
47 #include <securityd/SOSCloudCircleServer.h>
49 #include "SOSAccountTesting.h"
51 #include "SecdTestKeychainUtilities.h"
53 static int kTestTestCount
= 118;
56 static void trim_retirements_from_circle(SOSAccountRef account) {
57 SOSAccountForEachCircle(account, ^(SOSCircleRef circle) {
58 SOSCircleRemoveRetired(circle, NULL);
62 static bool accept_applicants(SOSAccountRef account
, int count
) {
63 CFErrorRef error
= NULL
;
64 CFArrayRef applicants
= SOSAccountCopyApplicants(account
, &error
);
66 ok(applicants
, "Have Applicants");
67 if(!applicants
) goto errout
;
68 is(CFArrayGetCount(applicants
), count
, "See applicants %@ (%@)", applicants
, error
);
69 if(CFArrayGetCount(applicants
) != count
) goto errout
;
70 ok(retval
= SOSAccountAcceptApplicants(account
, applicants
, &error
), "Account accepts (%@)", error
);
73 CFReleaseNull(applicants
);
78 static void tests(void)
80 CFErrorRef error
= NULL
;
81 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
82 CFStringRef cfaccount
= CFSTR("test@test.org");
84 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
86 SOSAccountRef alice_account
= CreateAccountForLocalChanges ( CFSTR("Alice"), CFSTR("TestSource"));
87 SOSAccountRef bob_account
= CreateAccountForLocalChanges ( CFSTR("Bob"), CFSTR("TestSource"));
88 SOSAccountRef carole_account
= CreateAccountForLocalChanges ( CFSTR("Carole"), CFSTR("TestSource"));
89 SOSAccountRef david_account
= CreateAccountForLocalChanges ( CFSTR("David"), CFSTR("TestSource"));
91 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
93 // Bob wins writing at this point, feed the changes back to alice.
94 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 1, "updates");
96 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
99 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
100 CFReleaseNull(error
);
102 ok(SOSAccountAssertUserCredentialsAndUpdate(david_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
103 CFReleaseNull(error
);
105 ok(SOSAccountResetToOffering_wTxn(alice_account
, &error
), "Reset to offering (%@)", error
);
106 CFReleaseNull(error
);
108 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 2, "updates");
111 ok(SOSAccountJoinCircles_wTxn(bob_account
, &error
), "Bob Applies (%@)", error
);
112 CFReleaseNull(error
);
114 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
117 ok(accept_applicants(alice_account
, 1), "Alice Accepts Application");
119 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 3, "updates");
121 accounts_agree("bob&alice pair", bob_account
, alice_account
);
122 is(SOSAccountGetLastDepartureReason(bob_account
, &error
), kSOSNeverLeftCircle
, "Bob affirms he hasn't left.");
124 // ============================== Alice and Bob are in the Account. ============================================
127 ok(SOSAccountJoinCircles_wTxn(carole_account
, &error
), "Carole Applies (%@)", error
);
128 CFReleaseNull(error
);
129 is(ProcessChangesUntilNoChange(changes
, alice_account
, carole_account
, david_account
, NULL
), 2, "updates");
131 ok(accept_applicants(alice_account
, 1), "Alice Accepts Application");
133 // Let everyone concur.
134 is(ProcessChangesUntilNoChange(changes
, alice_account
, carole_account
, david_account
, NULL
), 3, "updates");
136 CFArrayRef peers
= SOSAccountCopyPeers(alice_account
, &error
);
138 ok(peers
&& CFArrayGetCount(peers
) == 3, "See three peers %@ (%@)", peers
, error
);
139 CFReleaseNull(peers
);
141 // SOSAccountPurgePrivateCredential(alice_account);
143 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice Leaves (%@)", error
);
144 CFReleaseNull(error
);
146 is(ProcessChangesUntilNoChange(changes
, alice_account
, carole_account
, david_account
, NULL
), 2, "updates");
149 ok(SOSAccountJoinCircles_wTxn(david_account
, &error
), "David Applies (%@)", error
);
150 CFReleaseNull(error
);
152 is(ProcessChangesUntilNoChange(changes
, alice_account
, carole_account
, david_account
, NULL
), 2, "updates");
154 ok(accept_applicants(carole_account
, 1), "Carole Accepts Application");
156 // ============================== We added Carole and David while Bob was in a drawer. Alice has left ============================================
158 // ============================== Bob comes out of the drawer seeing alice left and doesn't recognize the remainder. ============================================
160 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, david_account
, NULL
), 3, "updates");
162 CFReleaseNull(error
);
163 is(SOSAccountGetCircleStatus(carole_account
, &error
), kSOSCCInCircle
, "Carole still in Circle (%@)", error
);
164 CFReleaseNull(error
);
165 is(SOSAccountGetCircleStatus(david_account
, &error
), kSOSCCInCircle
, "David still in Circle (%@)", error
);
166 CFReleaseNull(error
);
167 is(SOSAccountGetCircleStatus(bob_account
, &error
), kSOSCCNotInCircle
, "Bob is not in Circle (%@)", error
);
168 CFReleaseNull(error
);
169 is(SOSAccountGetLastDepartureReason(bob_account
, &error
), kSOSLeftUntrustedCircle
, "Bob affirms he left because he doesn't know anyone.");
170 CFReleaseNull(error
);
171 is(SOSAccountGetCircleStatus(alice_account
, &error
), kSOSCCNotInCircle
, "Alice is not in Circle (%@)", error
);
172 CFReleaseNull(error
);
173 is(SOSAccountGetLastDepartureReason(alice_account
, &error
), kSOSWithdrewMembership
, "Alice affirms she left by request.");
174 CFReleaseNull(error
);
177 CFReleaseNull(carole_account
);
178 CFReleaseNull(david_account
);
179 CFReleaseNull(bob_account
);
180 CFReleaseNull(alice_account
);
181 CFReleaseNull(cfpassword
);
186 int secd_61_account_leave_not_in_kansas_anymore(int argc
, char *const *argv
)
188 plan_tests(kTestTestCount
);
190 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);