]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-63-account-resurrection.c
Security-57740.31.2.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-63-account-resurrection.c
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
38 #include <stdlib.h>
39 #include <unistd.h>
40
41 #include "secd_regressions.h"
42 #include "SOSTestDataSource.h"
43
44 #include "SOSRegressionUtilities.h"
45 #include <utilities/SecCFWrappers.h>
46 #include <Security/SecKeyPriv.h>
47
48 #include <securityd/SOSCloudCircleServer.h>
49
50 #include "SOSAccountTesting.h"
51
52 #include "SecdTestKeychainUtilities.h"
53
54
55 static int kTestTestCount = 109;
56
57 typedef void (^stir_block)(int expected_iterations);
58 typedef int (^execute_block)();
59
60 static void stirBetween(stir_block stir, ...) {
61 va_list va;
62 va_start(va, stir);
63
64 execute_block execute = NULL;
65
66 while ((execute = va_arg(va, execute_block)) != NULL)
67 stir(execute());
68 }
69
70 static void VerifyCountAndAcceptAllApplicants(SOSAccountRef account, int expected)
71 {
72 CFErrorRef error = NULL;
73 CFArrayRef applicants = SOSAccountCopyApplicants(account, &error);
74
75 SKIP: {
76 skip("Empty applicant array", 2, applicants);
77
78 is(CFArrayGetCount(applicants), expected, "Applicants: %@ (%@)", applicants, error);
79 CFReleaseNull(error);
80
81 ok(SOSAccountAcceptApplicants(account , applicants, &error), "Accepting all (%@)", error);
82 CFReleaseNull(error);
83 }
84
85 CFReleaseNull(applicants);
86 }
87
88
89 static void tests(void)
90 {
91 __block CFErrorRef error = NULL;
92 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
93 CFStringRef cfaccount = CFSTR("test@test.org");
94
95 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
96
97 const CFStringRef data_source_name = CFSTR("TestSource");
98
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);
102
103 SOSAccountRef alice_resurrected = NULL;
104
105 __block CFDataRef frozen_alice = NULL;
106
107
108 stirBetween(^(int expected){
109 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), expected, "stirring");
110 }, ^{
111 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account , cfaccount, cfpassword, &error), "bob credential setting (%@)", error);
112
113 return 1;
114 }, ^{
115 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account , cfaccount, cfpassword, &error), "alice credential setting (%@)", error);
116 CFReleaseNull(error);
117
118 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "carole credential setting (%@)", error);
119 CFReleaseNull(error);
120
121 ok(SOSAccountResetToOffering_wTxn(alice_account , &error), "Reset to offering (%@)", error);
122 CFReleaseNull(error);
123
124 return 2;
125 }, ^{
126 ok(SOSAccountJoinCircles_wTxn(bob_account , &error), "Bob Applies (%@)", error);
127 CFReleaseNull(error);
128
129 return 2;
130 }, ^{
131 VerifyCountAndAcceptAllApplicants(alice_account, 1);
132
133 return 3;
134 }, ^{
135 accounts_agree("bob&alice pair", bob_account, alice_account);
136 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSNeverLeftCircle, "Bob affirms he hasn't left.");
137
138 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
139 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
140 CFReleaseNull(peers);
141
142 return 1;
143 }, ^{
144
145 frozen_alice = SOSAccountCopyEncodedData(alice_account, kCFAllocatorNull, &error);
146 ok(frozen_alice, "Copy encoded %@", error);
147 CFReleaseNull(error);
148
149 SOSAccountPurgePrivateCredential(alice_account);
150
151 ok(SOSAccountLeaveCircle(alice_account , &error), "Alice Leaves (%@)", error);
152 CFReleaseNull(error);
153
154 return 2;
155 }, ^{
156
157 accounts_agree("Alice bails", bob_account, alice_account);
158
159 {
160 CFArrayRef concurring = SOSAccountCopyConcurringPeers(alice_account, &error);
161
162 ok(concurring && CFArrayGetCount(concurring) == 2, "See two concurring %@ (%@)", concurring, error);
163 CFReleaseNull(error);
164 CFReleaseNull(concurring);
165 }
166
167 return 1;
168 },
169 NULL);
170
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);
176
177 stirBetween(^(int expected){
178 is(ProcessChangesUntilNoChange(changes, alice_resurrected, bob_account, carole_account, NULL), expected, "stirring");
179 }, ^{
180 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_resurrected, cfaccount, cfpassword, &error), "alice_resurrected credential setting (%@)", error);
181 CFReleaseNull(error);
182 return 1;
183 }, ^{
184 ok(!SOSAccountIsInCircle(alice_resurrected, &error), "Ressurrected not in circle: %@", error);
185 CFReleaseNull(error);
186
187 ok(SOSAccountJoinCircles_wTxn(alice_resurrected, &error), "Risen-alice Applies (%@)", error);
188 CFReleaseNull(error);
189 return 2;
190 }, ^{
191 VerifyCountAndAcceptAllApplicants(bob_account, 1);
192 return 3;
193 },
194 NULL);
195
196 CFReleaseNull(bob_account);
197 CFReleaseNull(alice_account);
198 CFReleaseNull(carole_account);
199 CFReleaseNull(alice_resurrected);
200 CFReleaseNull(frozen_alice);
201
202 SOSUnregisterAllTransportMessages();
203 SOSUnregisterAllTransportCircles();
204 SOSUnregisterAllTransportKeyParameters();
205 CFArrayRemoveAllValues(key_transports);
206 CFArrayRemoveAllValues(circle_transports);
207 CFArrayRemoveAllValues(message_transports);
208 }
209
210 int secd_63_account_resurrection(int argc, char *const *argv)
211 {
212 plan_tests(kTestTestCount);
213
214 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
215
216 tests();
217
218 return 0;
219 }