]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-57-1-account-last-standing.m
Security-58286.1.32.tar.gz
[apple/security.git] / OSX / sec / 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 <Security/SecureObjectSync/SOSAccount.h>
37 #include <Security/SecureObjectSync/SOSCloudCircle.h>
38 #include <Security/SecureObjectSync/SOSInternal.h>
39 #include <Security/SecureObjectSync/SOSUserKeygen.h>
40 #include <Security/SecureObjectSync/SOSTransport.h>
41 #include <Security/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 <securityd/SOSCloudCircleServer.h>
54
55 #include "SOSAccountTesting.h"
56
57 #include "SecdTestKeychainUtilities.h"
58
59
60 static int kTestTestCount = 61;
61
62
63 static bool acceptApplicants(SOSAccount* account, CFIndex count) {
64 bool retval = false;
65 CFErrorRef error = NULL;
66 CFArrayRef applicants = SOSAccountCopyApplicants(account, &error);
67 ok(applicants && CFArrayGetCount(applicants) == count, "See %ld applicants %@ (%@)", count, applicants, error);
68 CFReleaseNull(error);
69 require_quiet(CFArrayGetCount(applicants) == count, xit);
70 ok((retval=SOSAccountAcceptApplicants(account, applicants, &error)), "Accept applicants into the fold");
71 CFReleaseNull(error);
72 xit:
73 CFReleaseNull(applicants);
74 return retval;
75 }
76
77
78 static void tests(void)
79 {
80 CFErrorRef error = NULL;
81 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
82 CFStringRef cfaccount = CFSTR("test@test.org");
83
84 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
85
86 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
87
88 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account , cfaccount, cfpassword, &error), "Credential setting (%@)", error);
89
90 is(ProcessChangesUntilNoChange(changes, alice_account, NULL), 1, "updates");
91
92 ok(SOSAccountResetToOffering_wTxn(alice_account , &error), "Reset to offering (%@)", error);
93 CFReleaseNull(error);
94
95 is(ProcessChangesUntilNoChange(changes, alice_account, NULL), 1, "updates");
96
97 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
98 CFReleaseNull(error);
99
100 is(ProcessChangesUntilNoChange(changes, alice_account, NULL), 1, "updates");
101
102 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
103 CFReleaseNull(error);
104
105 ok(SOSAccountJoinCircles_wTxn(alice_account , &error), "Alice re-applies (%@)", error);
106 CFReleaseNull(error);
107
108 is(ProcessChangesUntilNoChange(changes, alice_account, NULL), 1, "updates");
109
110 ok([alice_account.trust isInCircle:&error], "Alice is back in the circle (%@)", error);
111 CFReleaseNull(error);
112
113 is(countActivePeers(alice_account), 2, "Alice sees 2 active peers");
114 is(countPeers(alice_account), 1, "Alice sees 1 valid peer");
115
116 // Have Alice leave the circle just as Bob tries to join.
117 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
118 SOSAccount* carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
119
120 is(ProcessChangesUntilNoChange(changes, bob_account, alice_account, carole_account, NULL), 1, "updates");
121 ok(SOSAccountTryUserCredentials(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
122 CFReleaseNull(error);
123 ok(SOSAccountTryUserCredentials(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
124 CFReleaseNull(error);
125 is(ProcessChangesUntilNoChange(changes, bob_account, alice_account, carole_account, NULL), 1, "updates");
126
127 ok(SOSAccountJoinCircles_wTxn(carole_account , &error), "Carole applies (%@)", error);
128 CFReleaseNull(error);
129 is(ProcessChangesUntilNoChange(changes, bob_account, alice_account, carole_account, NULL), 2, "updates");
130 ok(acceptApplicants(alice_account, 1), "Alice accepts Carole");
131 is(ProcessChangesUntilNoChange(changes, bob_account, alice_account, carole_account, NULL), 3, "updates");
132
133 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
134 CFReleaseNull(error);
135 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, NULL), 2, "updates");
136
137 ok([carole_account.trust leaveCircle:carole_account err:&error], "Carole Leaves (%@)", error);
138 CFReleaseNull(error);
139
140 is(ProcessChangesUntilNoChange(changes, alice_account, carole_account, NULL), 2, "updates");
141 ok(SOSAccountJoinCircles_wTxn(bob_account , &error), "Bob applies (%@)", error);
142 is(ProcessChangesUntilNoChange(changes, bob_account, NULL), 2, "updates");
143 CFReleaseNull(error);
144
145 is(countActivePeers(bob_account), 2, "Bob sees 2 active peers");
146 is(countPeers(bob_account), 1, "Bob sees 1 valid peer");
147
148
149 CFReleaseNull(cfpassword);
150
151 alice_account = nil;
152 SOSTestCleanup();
153 }
154
155 int secd_57_1_account_last_standing(int argc, char *const *argv)
156 {
157 plan_tests(kTestTestCount);
158
159 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
160
161 tests();
162
163 return 0;
164 }