]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-59-account-cleanup.c
Security-57337.50.23.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-59-account-cleanup.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/SOSKVSKeys.h>
37 #include <Security/SecureObjectSync/SOSTransport.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 static int kTestTestCount = 123;
56
57 static void tests(void)
58 {
59 CFErrorRef error = NULL;
60 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
61 CFStringRef cfaccount = CFSTR("test@test.org");
62 CFStringRef circle_name = CFSTR("TestSource");
63
64 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
65
66 SOSAccountRef alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), circle_name);
67 SOSAccountRef bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), circle_name);
68 SOSAccountRef carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), circle_name);
69
70 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
71
72 // Bob wins writing at this point, feed the changes back to alice.
73 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
74
75 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
76 CFReleaseNull(error);
77
78 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
79 CFReleaseNull(error);
80 ok(SOSAccountResetToOffering(alice_account, &error), "Reset to offering (%@)", error);
81 CFReleaseNull(error);
82
83 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
84
85 ok(SOSAccountJoinCircles(bob_account, &error), "Bob Applies (%@)", error);
86 CFReleaseNull(error);
87
88 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
89
90 {
91 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
92
93 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
94 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
95 CFReleaseNull(error);
96 CFReleaseNull(applicants);
97 }
98
99 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 3, "updates");
100
101 accounts_agree("bob&alice pair", bob_account, alice_account);
102
103 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
104 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
105 CFReleaseNull(peers);
106
107 SOSFullPeerInfoRef fpiAlice = SOSAccountGetMyFullPeerInfo(alice_account);
108 CFStringRef alice_id = CFStringCreateCopy(NULL, SOSPeerInfoGetPeerID(SOSFullPeerInfoGetPeerInfo(fpiAlice)));
109
110 ok(SOSAccountLeaveCircle(alice_account, &error), "Alice Leaves (%@)", error);
111 CFReleaseNull(error);
112 CFReleaseNull(cfpassword);
113
114 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
115
116 accounts_agree("Alice bails", bob_account, alice_account);
117 accounts_agree("Alice bails", bob_account, carole_account);
118
119 SOSAccountCleanupRetirementTickets(bob_account, 0, &error);
120
121 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
122
123 //is(CFDictionaryGetCountOfValue(BobChanges, kCFNull),0, "0 Keys Nulled Out");
124
125 ok(SOSAccountJoinCircles(carole_account, &error), "Carole Applies (%@)", error);
126 CFReleaseNull(error);
127
128 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
129
130
131 {
132 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
133
134 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
135 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts Carole (%@)", error);
136 CFReleaseNull(error);
137 CFReleaseNull(applicants);
138 }
139
140 // Bob should not yet cleanup Alice's retirment here on his own since it hasn't been long enough
141 // by default.
142 //is(CFDictionaryGetCountOfValue(BobChanges, kCFNull),0, "0 Keys Nulled Out");
143
144 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 3, "updates");
145
146 accounts_agree("Carole joins", bob_account, carole_account);
147
148 SOSAccountCleanupRetirementTickets(bob_account, 0, &error);
149
150 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
151
152 is(countPeers(bob_account), 2, "Active peers after forced cleanup");
153 is(countActivePeers(bob_account), 3, "Inactive peers after forced cleanup");
154
155 //is(CFDictionaryGetCountOfValue(BobChanges, kCFNull), 1, "1 Keys Nulled Out");
156
157 // CFDictionaryForEach(BobChanges, ^(const void *key, const void *value) {
158 // if(isNull(value)) {
159 // CFStringRef circle_name = NULL, retiree = NULL;
160 // SOSKVSKeyType keytype = SOSKVSKeyGetKeyTypeAndParse(key, &circle_name, &retiree, NULL);
161 // is(keytype, kRetirementKey, "Expect only a retirement key");
162 // ok(CFEqualSafe(alice_id, retiree), "Alice (%@) is retiree (%@)", alice_id, retiree);
163 // CFReleaseNull(circle_name);
164 // CFReleaseNull(retiree);
165 // }
166 // });
167
168 CFReleaseNull(alice_id);
169 CFReleaseNull(carole_account);
170 CFReleaseNull(bob_account);
171 CFReleaseNull(alice_account);
172
173 SOSUnregisterAllTransportMessages();
174 SOSUnregisterAllTransportCircles();
175 SOSUnregisterAllTransportKeyParameters();
176 CFArrayRemoveAllValues(key_transports);
177 CFArrayRemoveAllValues(circle_transports);
178 CFArrayRemoveAllValues(message_transports);
179 }
180
181 int secd_59_account_cleanup(int argc, char *const *argv)
182 {
183 plan_tests(kTestTestCount);
184
185 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
186
187 tests();
188
189 return 0;
190 }