]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-57-1-account-last-standing.m
Security-59306.41.2.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-57-1-account-last-standing.m
1 //
2 // secd-57-1-account-last-standing.c
3 // sec
4 //
5 /*
6 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
7 *
8 * @APPLE_LICENSE_HEADER_START@
9 *
10 * This file contains Original Code and/or Modifications of Original Code
11 * as defined in and that are subject to the Apple Public Source License
12 * Version 2.0 (the 'License'). You may not use this file except in
13 * compliance with the License. Please obtain a copy of the License at
14 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * file.
16 *
17 * The Original Code and all software distributed under the License are
18 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
19 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
20 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
22 * Please see the License for the specific language governing rights and
23 * limitations under the License.
24 *
25 * @APPLE_LICENSE_HEADER_END@
26 */
27
28
29
30
31 #include <Security/SecBase.h>
32 #include <Security/SecItem.h>
33
34 #include <CoreFoundation/CFDictionary.h>
35
36 #include "keychain/SecureObjectSync/SOSAccount.h"
37 #include <Security/SecureObjectSync/SOSCloudCircle.h>
38 #include "keychain/SecureObjectSync/SOSInternal.h"
39 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
40 #include "keychain/SecureObjectSync/SOSTransport.h"
41 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
42
43 #include <stdlib.h>
44 #include <unistd.h>
45
46 #include "secd_regressions.h"
47 #include "SOSTestDataSource.h"
48
49 #include "SOSRegressionUtilities.h"
50 #include <utilities/SecCFWrappers.h>
51 #include <Security/SecKeyPriv.h>
52
53 #include "keychain/securityd/SOSCloudCircleServer.h"
54
55 #include "SOSAccountTesting.h"
56
57 #include "SecdTestKeychainUtilities.h"
58
59
60 static bool acceptApplicants(SOSAccount* account, CFIndex count) {
61 bool retval = false;
62 CFErrorRef error = NULL;
63 CFArrayRef applicants = SOSAccountCopyApplicants(account, &error);
64 ok(applicants && CFArrayGetCount(applicants) == count, "See %ld applicants %@ (%@)", count, applicants, error);
65 CFReleaseNull(error);
66 require_quiet(CFArrayGetCount(applicants) == count, xit);
67 ok((retval=SOSAccountAcceptApplicants(account, applicants, &error)), "Accept applicants into the fold");
68 CFReleaseNull(error);
69 xit:
70 CFReleaseNull(applicants);
71 return retval;
72 }
73
74
75 static void tests(void)
76 {
77 CFErrorRef error = NULL;
78 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
79 CFStringRef cfaccount = CFSTR("test@test.org");
80
81 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
82
83 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
84
85 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account , cfaccount, cfpassword, &error), "Credential setting (%@)", error);
86
87 is(ProcessChangesUntilNoChange(changes, alice_account, NULL), 1, "updates");
88
89 ok(SOSAccountResetToOffering_wTxn(alice_account , &error), "Reset to offering (%@)", error);
90 CFReleaseNull(error);
91
92 is(ProcessChangesUntilNoChange(changes, alice_account, NULL), 1, "updates");
93
94 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
95 CFReleaseNull(error);
96
97 is(ProcessChangesUntilNoChange(changes, alice_account, NULL), 1, "updates");
98
99 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
100 CFReleaseNull(error);
101
102 ok(SOSAccountJoinCircles_wTxn(alice_account , &error), "Alice re-applies (%@)", error);
103 CFReleaseNull(error);
104
105 is(ProcessChangesUntilNoChange(changes, alice_account, NULL), 1, "updates");
106
107 ok([alice_account isInCircle:&error], "Alice is back in the circle (%@)", error);
108 CFReleaseNull(error);
109
110 is(countActivePeers(alice_account), 2, "Alice sees 2 active peers");
111 is(countPeers(alice_account), 1, "Alice sees 1 valid peer");
112
113 // Have Alice leave the circle just as Bob tries to join.
114 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
115 SOSAccount* carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
116
117 is(ProcessChangesUntilNoChange(changes, bob_account, alice_account, carole_account, NULL), 1, "updates");
118 ok(SOSAccountTryUserCredentials(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
119 CFReleaseNull(error);
120 ok(SOSAccountTryUserCredentials(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
121 CFReleaseNull(error);
122 is(ProcessChangesUntilNoChange(changes, bob_account, alice_account, carole_account, NULL), 1, "updates");
123
124 ok(SOSAccountJoinCircles_wTxn(carole_account , &error), "Carole applies (%@)", error);
125 CFReleaseNull(error);
126 is(ProcessChangesUntilNoChange(changes, bob_account, alice_account, carole_account, NULL), 2, "updates");
127 ok(acceptApplicants(alice_account, 1), "Alice accepts Carole");
128 is(ProcessChangesUntilNoChange(changes, bob_account, alice_account, carole_account, NULL), 3, "updates");
129
130 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
131 CFReleaseNull(error);
132 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, NULL), 2, "updates");
133
134 ok([carole_account.trust leaveCircle:carole_account err:&error], "Carole Leaves (%@)", error);
135 CFReleaseNull(error);
136
137 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, NULL), 2, "updates");
138 ok(SOSAccountJoinCircles_wTxn(bob_account , &error), "Bob applies (%@)", error);
139 is(ProcessChangesUntilNoChange(changes, bob_account, NULL), 2, "updates");
140 CFReleaseNull(error);
141
142 is(countActivePeers(bob_account), 2, "Bob sees 2 active peers");
143 is(countPeers(bob_account), 1, "Bob sees 1 valid peer");
144
145
146 CFReleaseNull(cfpassword);
147
148 alice_account = nil;
149 SOSTestCleanup();
150 }
151
152 int secd_57_1_account_last_standing(int argc, char *const *argv)
153 {
154 plan_tests(45);
155
156 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
157
158 tests();
159
160 return 0;
161 }