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"
37 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
42 #include "secd_regressions.h"
43 #include "SOSTestDataSource.h"
45 #include "SOSRegressionUtilities.h"
46 #include <utilities/SecCFWrappers.h>
47 #include <Security/SecKeyPriv.h>
49 #include <securityd/SOSCloudCircleServer.h>
51 #include "SOSAccountTesting.h"
53 #include "SecdTestKeychainUtilities.h"
56 typedef void (^stir_block)(int expected_iterations);
57 typedef int (^execute_block)(void);
59 static void stirBetween(stir_block stir, ...) {
63 execute_block execute = NULL;
65 while ((execute = va_arg(va, execute_block)) != NULL)
69 static void VerifyCountAndAcceptAllApplicants(SOSAccount* account, int expected)
71 CFErrorRef error = NULL;
72 CFArrayRef applicants = SOSAccountCopyApplicants(account, &error);
75 skip("Empty applicant array", 2, applicants);
77 is(CFArrayGetCount(applicants), expected, "Applicants: %@ (%@)", applicants, error);
80 ok(SOSAccountAcceptApplicants(account , applicants, &error), "Accepting all (%@)", error);
84 CFReleaseNull(applicants);
88 static void tests(void)
90 __block CFErrorRef error = NULL;
91 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
92 CFStringRef cfaccount = CFSTR("test@test.org");
94 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
96 const CFStringRef data_source_name = CFSTR("TestSource");
98 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), data_source_name);
99 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), data_source_name);
100 SOSAccount* carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), data_source_name);
102 SOSAccount* alice_resurrected = NULL;
104 __block CFDataRef frozen_alice = NULL;
107 stirBetween(^(int expected){
108 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), expected, "stirring");
110 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account , cfaccount, cfpassword, &error), "bob credential setting (%@)", error);
114 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account , cfaccount, cfpassword, &error), "alice credential setting (%@)", error);
115 CFReleaseNull(error);
117 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "carole credential setting (%@)", error);
118 CFReleaseNull(error);
120 ok(SOSAccountResetToOffering_wTxn(alice_account , &error), "Reset to offering (%@)", error);
121 CFReleaseNull(error);
125 ok(SOSAccountJoinCircles_wTxn(bob_account , &error), "Bob Applies (%@)", error);
126 CFReleaseNull(error);
130 VerifyCountAndAcceptAllApplicants(alice_account, 1);
134 accounts_agree("bob&alice pair", bob_account, alice_account);
135 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSNeverLeftCircle, "Bob affirms he hasn't left.");
137 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
138 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
139 CFReleaseNull(peers);
144 NSError *ns_error = nil;
145 frozen_alice = (CFDataRef) CFBridgingRetain([alice_account encodedData:&ns_error]);
146 ok(frozen_alice, "Copy encoded %@", ns_error);
149 SOSAccountPurgePrivateCredential(alice_account);
151 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
152 CFReleaseNull(error);
157 accounts_agree("Alice bails", bob_account, alice_account);
160 CFArrayRef concurring = SOSAccountCopyConcurringPeers(alice_account, &error);
162 ok(concurring && CFArrayGetCount(concurring) == 2, "See two concurring %@ (%@)", concurring, error);
163 CFReleaseNull(error);
164 CFReleaseNull(concurring);
171 alice_resurrected = CreateAccountForLocalChangesFromData(frozen_alice, CFSTR("Alice risen"), data_source_name);
172 // This is necessary from the change that makes accounts not inflate if the private key was lost - alice_resurected now
173 // Starts as a brand new account, so this whole series of tests needs to amount to "is this brand new"?
174 // The trigger is alice leaving the circle - that kills the deviceKey.
175 ProcessChangesUntilNoChange(changes, alice_resurrected, bob_account, carole_account, NULL);
177 stirBetween(^(int expected){
178 is(ProcessChangesUntilNoChange(changes, alice_resurrected, bob_account, carole_account, NULL), expected, "stirring");
180 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_resurrected, cfaccount, cfpassword, &error), "alice_resurrected credential setting (%@)", error);
181 CFReleaseNull(error);
184 ok(![alice_resurrected isInCircle:&error], "Ressurrected not in circle: %@", error);
185 CFReleaseNull(error);
187 ok(SOSAccountJoinCircles_wTxn(alice_resurrected, &error), "Risen-alice Applies (%@)", error);
188 CFReleaseNull(error);
191 VerifyCountAndAcceptAllApplicants(bob_account, 1);
196 CFReleaseNull(frozen_alice);
202 int secd_63_account_resurrection(int argc, char *const *argv)
206 secd_test_setup_temp_keychain(__FUNCTION__, NULL);