]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-59-account-cleanup.m
Security-59306.11.20.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-59-account-cleanup.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 "keychain/SecureObjectSync/SOSAccount.h"
33 #include <Security/SecureObjectSync/SOSCloudCircle.h>
34 #include "keychain/SecureObjectSync/SOSInternal.h"
35 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
36 #include "keychain/SecureObjectSync/SOSKVSKeys.h"
37 #include "keychain/SecureObjectSync/SOSTransport.h"
38 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Retirement.h"
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 void tests(void)
57 {
58 CFErrorRef error = NULL;
59 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
60 CFStringRef cfaccount = CFSTR("test@test.org");
61 CFStringRef circle_name = CFSTR("TestSource");
62
63 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
64
65 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), circle_name);
66 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), circle_name);
67 SOSAccount* carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), circle_name);
68
69 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
70
71 // Bob wins writing at this point, feed the changes back to alice.
72 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
73
74 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
75 CFReleaseNull(error);
76
77 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
78 CFReleaseNull(error);
79 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
80 CFReleaseNull(error);
81
82 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
83
84 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
85 CFReleaseNull(error);
86
87 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
88
89 {
90 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
91
92 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
93 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
94 CFReleaseNull(error);
95 CFReleaseNull(applicants);
96 }
97
98 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 3, "updates");
99
100 accounts_agree("bob&alice pair", bob_account, alice_account);
101
102 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
103 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
104 CFReleaseNull(peers);
105
106 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
107 CFReleaseNull(error);
108 CFReleaseNull(cfpassword);
109
110 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
111
112 accounts_agree("Alice bails", bob_account, alice_account);
113 accounts_agree("Alice bails", bob_account, carole_account);
114
115 [bob_account.trust cleanupRetirementTickets:bob_account circle:bob_account.trust.trustedCircle time:0 err:&error];
116
117 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
118
119 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole Applies (%@)", error);
120 CFReleaseNull(error);
121
122 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
123
124
125 {
126 CFArrayRef applicants = SOSAccountCopyApplicants(bob_account, &error);
127
128 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
129 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts Carole (%@)", error);
130 CFReleaseNull(error);
131 CFReleaseNull(applicants);
132 }
133
134 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 3, "updates");
135
136 accounts_agree("Carole joins", bob_account, carole_account);
137
138 [bob_account.trust cleanupRetirementTickets:bob_account circle:bob_account.trust.trustedCircle time:0 err:&error];
139
140 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
141
142 is(countPeers(bob_account), 2, "Active peers after forced cleanup");
143 is(countActivePeers(bob_account), 3, "Inactive peers after forced cleanup");
144
145 alice_account = nil;
146 bob_account = nil;
147 SOSTestCleanup();
148 }
149
150 int secd_59_account_cleanup(int argc, char *const *argv)
151 {
152 plan_tests(91);
153
154 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
155
156 tests();
157
158 return 0;
159 }