]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-63-account-resurrection.m
Security-58286.200.222.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-63-account-resurrection.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 = 107;
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 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 ok(SOSAccountJoinCircles_wTxn(bob_account , &error), "Bob Applies (%@)", error);
128 CFReleaseNull(error);
129
130 return 2;
131 }, ^{
132 VerifyCountAndAcceptAllApplicants(alice_account, 1);
133
134 return 3;
135 }, ^{
136 accounts_agree("bob&alice pair", bob_account, alice_account);
137 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSNeverLeftCircle, "Bob affirms he hasn't left.");
138
139 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
140 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
141 CFReleaseNull(peers);
142
143 return 1;
144 }, ^{
145
146 NSError *ns_error = nil;
147 frozen_alice = (CFDataRef) CFBridgingRetain([alice_account encodedData:&ns_error]);
148 ok(frozen_alice, "Copy encoded %@", ns_error);
149 ns_error = nil;
150
151 SOSAccountPurgePrivateCredential(alice_account);
152
153 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
154 CFReleaseNull(error);
155
156 return 2;
157 }, ^{
158
159 accounts_agree("Alice bails", bob_account, alice_account);
160
161 {
162 CFArrayRef concurring = SOSAccountCopyConcurringPeers(alice_account, &error);
163
164 ok(concurring && CFArrayGetCount(concurring) == 2, "See two concurring %@ (%@)", concurring, error);
165 CFReleaseNull(error);
166 CFReleaseNull(concurring);
167 }
168
169 return 1;
170 },
171 NULL);
172
173 alice_resurrected = CreateAccountForLocalChangesFromData(frozen_alice, CFSTR("Alice risen"), data_source_name);
174 // This is necessary from the change that makes accounts not inflate if the private key was lost - alice_resurected now
175 // Starts as a brand new account, so this whole series of tests needs to amount to "is this brand new"?
176 // The trigger is alice leaving the circle - that kills the deviceKey.
177 ProcessChangesUntilNoChange(changes, alice_resurrected, bob_account, carole_account, NULL);
178
179 stirBetween(^(int expected){
180 is(ProcessChangesUntilNoChange(changes, alice_resurrected, bob_account, carole_account, NULL), expected, "stirring");
181 }, ^{
182 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_resurrected, cfaccount, cfpassword, &error), "alice_resurrected credential setting (%@)", error);
183 CFReleaseNull(error);
184 return 1;
185 }, ^{
186 ok(![alice_resurrected isInCircle:&error], "Ressurrected not in circle: %@", error);
187 CFReleaseNull(error);
188
189 ok(SOSAccountJoinCircles_wTxn(alice_resurrected, &error), "Risen-alice Applies (%@)", error);
190 CFReleaseNull(error);
191 return 2;
192 }, ^{
193 VerifyCountAndAcceptAllApplicants(bob_account, 1);
194 return 3;
195 },
196 NULL);
197
198 CFReleaseNull(frozen_alice);
199 alice_account = nil;
200 bob_account = nil;
201 SOSTestCleanup();
202 }
203
204 int secd_63_account_resurrection(int argc, char *const *argv)
205 {
206 plan_tests(kTestTestCount);
207
208 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
209
210 tests();
211
212 return 0;
213 }