]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-65-account-retirement-reset.m
Security-58286.200.222.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-65-account-retirement-reset.m
1 /*
2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25
26
27 #include <Security/SecBase.h>
28 #include <Security/SecItem.h>
29
30 #include <CoreFoundation/CFDictionary.h>
31
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>
37 #include <Security/SecureObjectSync/SOSAccountTrustClassic+Circle.h>
38
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 #include "secd_regressions.h"
43 #include "SOSTestDataSource.h"
44
45 #include "SOSRegressionUtilities.h"
46 #include <utilities/SecCFWrappers.h>
47 #include <Security/SecKeyPriv.h>
48
49 #include <securityd/SOSCloudCircleServer.h>
50
51 #include "SOSAccountTesting.h"
52
53 #include "SecdTestKeychainUtilities.h"
54
55
56 static int kTestTestCount = 43;
57
58 typedef void (^stir_block)(int expected_iterations);
59 typedef int (^execute_block)(void);
60
61 static void stirBetween(stir_block stir, ...) {
62 va_list va;
63 va_start(va, stir);
64
65 execute_block execute = NULL;
66
67 while ((execute = va_arg(va, execute_block)) != NULL)
68 stir(execute());
69 }
70
71 __unused static void VerifyCountAndAcceptAllApplicants(SOSAccount* account, int expected)
72 {
73 CFErrorRef error = NULL;
74 CFArrayRef applicants = SOSAccountCopyApplicants(account, &error);
75
76 SKIP: {
77 skip("Empty applicant array", 2, applicants);
78
79 is(CFArrayGetCount(applicants), expected, "Applicants: %@ (%@)", applicants, error);
80 CFReleaseNull(error);
81
82 ok(SOSAccountAcceptApplicants(account , applicants, &error), "Accepting all (%@)", error);
83 CFReleaseNull(error);
84 }
85
86 CFReleaseNull(applicants);
87 }
88
89
90 static void tests(void)
91 {
92 __block CFErrorRef error = NULL;
93 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
94 CFStringRef cfaccount = CFSTR("test@test.org");
95
96 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
97
98 const CFStringRef data_source_name = CFSTR("TestSource");
99
100 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), data_source_name);
101 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), data_source_name);
102 SOSAccount* carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), data_source_name);
103
104 SOSAccount* alice_resurrected = NULL;
105
106 __block CFDataRef frozen_alice = NULL;
107
108
109 stirBetween(^(int expected){
110 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), expected, "stirring");
111 }, ^{
112 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account , cfaccount, cfpassword, &error), "bob credential setting (%@)", error);
113
114 return 1;
115 }, ^{
116 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account , cfaccount, cfpassword, &error), "alice credential setting (%@)", error);
117 CFReleaseNull(error);
118
119 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "carole credential setting (%@)", error);
120 CFReleaseNull(error);
121
122 ok(SOSAccountResetToOffering_wTxn(alice_account , &error), "Reset to offering (%@)", error);
123 CFReleaseNull(error);
124
125 return 2;
126 }, ^{
127 NSError *ns_error = nil;
128 frozen_alice = (CFDataRef) CFBridgingRetain([alice_account encodedData:&ns_error]);
129 ok(frozen_alice, "Copy encoded %@", ns_error);
130 ns_error = nil;
131
132 SOSAccountPurgePrivateCredential(alice_account);
133
134 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
135 CFReleaseNull(error);
136
137 return 2;
138 }, ^{
139 ok(SOSAccountResetToOffering_wTxn(bob_account , &error), "Reset to offering (%@)", error);
140 CFReleaseNull(error);
141
142 return 2;
143 },
144 NULL);
145
146 alice_resurrected = CreateAccountForLocalChangesFromData(frozen_alice, CFSTR("Alice risen"), data_source_name);
147 // This is necessary from the change that makes accounts not inflate if the private key was lost - alice_resurected now
148 // Starts as a brand new account, so this whole series of tests needs to amount to "is this brand new"?
149 // The trigger is alice leaving the circle - that kills the deviceKey.
150 ProcessChangesUntilNoChange(changes, alice_resurrected, bob_account, carole_account, NULL);
151
152 stirBetween(^(int expected){
153 is(ProcessChangesUntilNoChange(changes, alice_resurrected, bob_account, carole_account, NULL), expected, "stirring");
154 }, ^{
155 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_resurrected, cfaccount, cfpassword, &error), "alice_resurrected credential setting (%@)", error);
156 CFReleaseNull(error);
157 return 1;
158 }, ^{
159 ok(![alice_resurrected isInCircle:&error], "Ressurrected not in circle: %@", error);
160 CFReleaseNull(error);
161
162 ok([bob_account isInCircle:&error], "Should be in circle: %@", error);
163 CFReleaseNull(error);
164
165 return 1;
166 },
167 NULL);
168
169 CFReleaseNull(frozen_alice);
170 alice_account = nil;
171 bob_account = nil;
172 SOSTestCleanup();
173 }
174
175 int secd_65_account_retirement_reset(int argc, char *const *argv)
176 {
177 plan_tests(kTestTestCount);
178
179 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
180
181 tests();
182
183 return 0;
184 }