]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-52-offering-gencount-reset.c
Security-57337.40.85.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-52-offering-gencount-reset.c
1 /*
2 * Copyright (c) 2012-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
38 #include <stdlib.h>
39 #include <unistd.h>
40
41 #include "secd_regressions.h"
42 #include "SOSTestDataSource.h"
43
44 #include "SOSRegressionUtilities.h"
45 #include <utilities/SecCFWrappers.h>
46 #include <Security/SecKeyPriv.h>
47
48 #include <securityd/SOSCloudCircleServer.h>
49
50 #include "SOSAccountTesting.h"
51
52 #include "SecdTestKeychainUtilities.h"
53
54 static int kTestTestCount = 81;
55
56 static void tests(void)
57 {
58 CFErrorRef error = NULL;
59 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
60 CFDataRef cfwrong_password = CFDataCreate(NULL, (uint8_t *) "NotFooFooFoo", 10);
61 CFStringRef cfaccount = CFSTR("test@test.org");
62
63 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
64 SOSAccountRef alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
65 SOSAccountRef bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
66 SOSAccountRef carol_account = CreateAccountForLocalChanges(CFSTR("Carol"), CFSTR("TestSource"));
67
68 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
69
70 // Bob wins writing at this point, feed the changes back to alice.
71 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
72
73 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
74 CFReleaseNull(error);
75 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential trying (%@)", error);
76 CFReleaseNull(error);
77 ok(!SOSAccountTryUserCredentials(alice_account, cfaccount, cfwrong_password, &error), "Credential failing (%@)", error);
78 CFReleaseNull(cfwrong_password);
79 is(error ? CFErrorGetCode(error) : 0, kSOSErrorWrongPassword, "Expected SOSErrorWrongPassword");
80 CFReleaseNull(error);
81
82 ok(SOSAccountResetToOffering(alice_account, &error), "Reset to offering (%@)", error);
83 CFReleaseNull(error);
84
85 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
86
87 ok(SOSAccountJoinCircles(bob_account, &error), "Bob Applies (%@)", error);
88 CFReleaseNull(error);
89
90 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
91
92 {
93 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
94
95 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
96 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
97 CFReleaseNull(error);
98 CFReleaseNull(applicants);
99 }
100
101 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
102
103 accounts_agree("bob&alice pair", bob_account, alice_account);
104
105 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
106 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
107 CFReleaseNull(peers);
108
109 //bob now goes def while Alice does some stuff.
110
111 ok(SOSAccountLeaveCircle(alice_account, &error), "ALICE LEAVES THE CIRCLE (%@)", error);
112 ok(SOSAccountResetToOffering(alice_account, &error), "Alice resets to offering again (%@)", error);
113
114 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
115
116 accounts_agree("bob&alice pair", bob_account, alice_account);
117
118 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
119
120
121 ok(SOSAccountAssertUserCredentialsAndUpdate(carol_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
122 SOSAccountSetUserPublicTrustedForTesting(carol_account);
123 ok(SOSAccountResetToOffering(carol_account, &error), "Carol is going to push a reset to offering (%@)", error);
124
125 int64_t valuePtr = 0;
126 CFNumberRef gencount = CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &valuePtr);
127 SOSCircleSetGeneration(carol_account->trusted_circle, gencount);
128
129 SecKeyRef user_privkey = SOSUserKeygen(cfpassword, carol_account->user_key_parameters, &error);
130 CFNumberRef genCountTest = SOSCircleGetGeneration(carol_account->trusted_circle);
131 int testPtr;
132 CFNumberGetValue(genCountTest, kCFNumberCFIndexType, &testPtr);
133 ok(testPtr== 0);
134
135 SOSCircleSignOldStyleResetToOfferingCircle(carol_account->trusted_circle, carol_account->my_identity, user_privkey, &error);
136 SOSTransportCircleTestRemovePendingChange(carol_account->circle_transport, SOSCircleGetName(carol_account->trusted_circle), NULL);
137 CFDataRef circle_data = SOSCircleCopyEncodedData(carol_account->trusted_circle, kCFAllocatorDefault, &error);
138 if (circle_data) {
139 SOSTransportCirclePostCircle(carol_account->circle_transport, SOSCircleGetName(carol_account->trusted_circle), circle_data, &error);
140 }
141
142 genCountTest = SOSCircleGetGeneration(carol_account->trusted_circle);
143 CFNumberGetValue(genCountTest, kCFNumberCFIndexType, &testPtr);
144 ok(testPtr== 0);
145
146 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
147 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
148
149 SOSAccountSetUserPublicTrustedForTesting(alice_account);
150 SOSAccountSetUserPublicTrustedForTesting(bob_account);
151
152 is(ProcessChangesUntilNoChange(changes, carol_account, alice_account, bob_account, NULL), 2, "updates");
153
154 ok(kSOSCCNotInCircle == SOSAccountGetCircleStatus(alice_account, &error), "alice is not in the account (%@)", error);
155 ok(kSOSCCNotInCircle == SOSAccountGetCircleStatus(bob_account, &error), "bob is not in the account (%@)", error);
156 ok(kSOSCCInCircle == SOSAccountGetCircleStatus(carol_account, &error), "carol is in the account (%@)", error);
157
158 CFReleaseNull(gencount);
159 CFReleaseNull(bob_account);
160 CFReleaseNull(alice_account);
161 CFReleaseNull(cfpassword);
162
163 SOSUnregisterAllTransportMessages();
164 SOSUnregisterAllTransportCircles();
165 SOSUnregisterAllTransportKeyParameters();
166 CFArrayRemoveAllValues(key_transports);
167 CFArrayRemoveAllValues(circle_transports);
168 CFArrayRemoveAllValues(message_transports);
169
170 }
171
172 int secd_52_offering_gencount_reset(int argc, char *const *argv)
173 {
174 plan_tests(kTestTestCount);
175
176 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
177
178 tests();
179
180 return 0;
181 }