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 <Security/SecureObjectSync/SOSAccount.h>
33 #include <Security/SecureObjectSync/SOSCloudCircle.h>
34 #include <Security/SecureObjectSync/SOSInternal.h>
35 #include <Security/SecureObjectSync/SOSUserKeygen.h>
36 #include <Security/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"
55 static int kTestTestCount
= 47;
57 typedef void (^stir_block
)(int expected_iterations
);
58 typedef int (^execute_block
)();
60 static void stirBetween(stir_block stir
, ...) {
64 execute_block execute
= NULL
;
66 while ((execute
= va_arg(va
, execute_block
)) != NULL
)
70 __unused
static void VerifyCountAndAcceptAllApplicants(SOSAccountRef account
, int expected
)
72 CFErrorRef error
= NULL
;
73 CFArrayRef applicants
= SOSAccountCopyApplicants(account
, &error
);
76 skip("Empty applicant array", 2, applicants
);
78 is(CFArrayGetCount(applicants
), expected
, "Applicants: %@ (%@)", applicants
, error
);
81 ok(SOSAccountAcceptApplicants(account
, applicants
, &error
), "Accepting all (%@)", error
);
85 CFReleaseNull(applicants
);
89 static void tests(void)
91 __block CFErrorRef error
= NULL
;
92 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
93 CFStringRef cfaccount
= CFSTR("test@test.org");
95 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
97 const CFStringRef data_source_name
= CFSTR("TestSource");
99 SOSAccountRef alice_account
= CreateAccountForLocalChanges(CFSTR("Alice"), data_source_name
);
100 SOSAccountRef bob_account
= CreateAccountForLocalChanges(CFSTR("Bob"), data_source_name
);
101 SOSAccountRef carole_account
= CreateAccountForLocalChanges(CFSTR("Carole"), data_source_name
);
103 SOSAccountRef alice_resurrected
= NULL
;
105 __block CFDataRef frozen_alice
= NULL
;
108 stirBetween(^(int expected
){
109 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, NULL
), expected
, "stirring");
111 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account
, cfaccount
, cfpassword
, &error
), "bob credential setting (%@)", error
);
115 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account
, cfaccount
, cfpassword
, &error
), "alice credential setting (%@)", error
);
116 CFReleaseNull(error
);
118 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account
, cfaccount
, cfpassword
, &error
), "carole credential setting (%@)", error
);
119 CFReleaseNull(error
);
121 ok(SOSAccountResetToOffering_wTxn(alice_account
, &error
), "Reset to offering (%@)", error
);
122 CFReleaseNull(error
);
127 frozen_alice
= SOSAccountCopyEncodedData(alice_account
, kCFAllocatorNull
, &error
);
128 ok(frozen_alice
, "Copy encoded %@", error
);
129 CFReleaseNull(error
);
131 SOSAccountPurgePrivateCredential(alice_account
);
133 ok(SOSAccountLeaveCircle(alice_account
, &error
), "Alice Leaves (%@)", error
);
134 CFReleaseNull(error
);
138 ok(SOSAccountResetToOffering_wTxn(bob_account
, &error
), "Reset to offering (%@)", error
);
139 CFReleaseNull(error
);
145 alice_resurrected
= CreateAccountForLocalChangesFromData(frozen_alice
, CFSTR("Alice risen"), data_source_name
);
146 // This is necessary from the change that makes accounts not inflate if the private key was lost - alice_resurected now
147 // Starts as a brand new account, so this whole series of tests needs to amount to "is this brand new"?
148 // The trigger is alice leaving the circle - that kills the deviceKey.
149 ProcessChangesUntilNoChange(changes
, alice_resurrected
, bob_account
, carole_account
, NULL
);
151 stirBetween(^(int expected
){
152 is(ProcessChangesUntilNoChange(changes
, alice_resurrected
, bob_account
, carole_account
, NULL
), expected
, "stirring");
154 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_resurrected
, cfaccount
, cfpassword
, &error
), "alice_resurrected credential setting (%@)", error
);
155 CFReleaseNull(error
);
158 ok(!SOSAccountIsInCircle(alice_resurrected
, &error
), "Ressurrected not in circle: %@", error
);
159 CFReleaseNull(error
);
161 ok(SOSAccountIsInCircle(bob_account
, &error
), "Should be in circle: %@", error
);
162 CFReleaseNull(error
);
168 CFReleaseNull(bob_account
);
169 CFReleaseNull(alice_account
);
170 CFReleaseNull(carole_account
);
171 CFReleaseNull(alice_resurrected
);
172 CFReleaseNull(frozen_alice
);
177 int secd_65_account_retirement_reset(int argc
, char *const *argv
)
179 plan_tests(kTestTestCount
);
181 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);