2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <Security/SecBase.h>
28 #include <Security/SecItem.h>
30 #include <CoreFoundation/CFDictionary.h>
32 #include <SecureObjectSync/SOSAccount.h>
33 #include <SecureObjectSync/SOSCloudCircle.h>
34 #include <SecureObjectSync/SOSInternal.h>
35 #include <SecureObjectSync/SOSUserKeygen.h>
36 #include <SecureObjectSync/SOSKVSKeys.h>
37 #include <SecureObjectSync/SOSTransport.h>
42 #include "secd_regressions.h"
43 #include "SOSTestDataSource.h"
45 #include "SOSRegressionUtilities.h"
46 #include <utilities/SecCFWrappers.h>
47 #include <Security/SecKeyPriv.h>
49 #include <securityd/SOSCloudCircleServer.h>
51 #include "SOSAccountTesting.h"
54 static int kTestTestCount
= 10;
56 static void tests(void)
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 CFStringRef data_name
= CFSTR("TestSource");
63 CFStringRef circle_key_name
= SOSCircleKeyCreateWithName(data_name
, NULL
);
65 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
67 SOSAccountRef alice_account
= CreateAccountForLocalChanges(CFSTR("Alice"), data_name
);
68 SOSAccountRef bob_account
= CreateAccountForLocalChanges(CFSTR("Bob"), data_name
);
69 SOSAccountRef carol_account
= CreateAccountForLocalChanges(CFSTR("Carol"), data_name
);
71 ok(SOSAccountAssertUserCredentials(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
73 // Bob wins writing at this point, feed the changes back to alice.
74 FillAllChanges(changes
);
75 FeedChangesToMulti(changes
, alice_account
, carol_account
, NULL
);
77 ok(SOSAccountAssertUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
79 ok(SOSAccountTryUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential trying (%@)", error
);
81 CFReleaseNull(cfpassword
);
83 ok(!SOSAccountTryUserCredentials(alice_account
, cfaccount
, cfwrong_password
, &error
), "Credential failing (%@)", error
);
84 CFReleaseNull(cfwrong_password
);
85 is(error
? CFErrorGetCode(error
) : 0, kSOSErrorWrongPassword
, "Expected SOSErrorWrongPassword");
88 CFDataRef incompatibleDER
= SOSCircleCreateIncompatibleCircleDER(&error
);
90 InjectChangeToMulti(changes
, circle_key_name
, incompatibleDER
, alice_account
, NULL
);
91 CFReleaseNull(circle_key_name
);
92 CFReleaseNull(incompatibleDER
);
94 is(ProcessChangesUntilNoChange(changes
, alice_account
, NULL
), 1, "updates");
97 is(SOSAccountIsInCircles(alice_account
, &error
), kSOSCCError
, "Is in circle");
101 ok(SOSAccountResetToOffering(alice_account
, &error
), "Reset to offering (%@)", error
);
102 CFReleaseNull(error
);
103 FillChanges(changes
, CFSTR("Alice"));
104 FeedChangesTo(changes
, bob_account
, transport_bob
);
106 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
107 CFReleaseNull(error
);
109 FeedChangesTo(changes
, alice_account
, transport_alice
);
112 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
114 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
115 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
116 CFReleaseNull(error
);
117 CFReleaseNull(applicants
);
120 FillChanges(changes
, CFSTR("Alice"));
121 FeedChangesTo(changes
, bob_account
, transport_bob
); // Bob sees he's accepted
123 FillChanges(changes
, CFSTR("Bob"));
124 FeedChangesTo(changes
, alice_account
, transport_alice
); // Alice sees bob-concurring
126 ok(CFDictionaryGetCount(changes
) == 0, "We converged. (%@)", changes
);
128 FillChanges(changes
, CFSTR("Alice"));
129 accounts_agree("bob&alice pair", bob_account
, alice_account
);
131 CFArrayRef peers
= SOSAccountCopyPeers(alice_account
, &error
);
132 ok(peers
&& CFArrayGetCount(peers
) == 2, "See two peers %@ (%@)", peers
, error
);
133 CFReleaseNull(peers
);
135 CFDictionaryRef alice_new_gestalt
= SOSCreatePeerGestaltFromName(CFSTR("Alice, but different"));
137 ok(SOSAccountUpdateGestalt(alice_account
, alice_new_gestalt
), "Update gestalt %@ (%@)", alice_account
, error
);
138 CFReleaseNull(alice_new_gestalt
);
140 FillChanges(changes
, CFSTR("Alice"));
141 FeedChangesTo(changes
, bob_account
, transport_bob
); // Bob sees alice change her name.
143 FillChanges(changes
, CFSTR("Bob"));
144 FeedChangesTo(changes
, alice_account
, transport_alice
); // Alice sees the fallout.
146 accounts_agree("Alice's name changed", bob_account
, alice_account
);
148 ok(SOSAccountLeaveCircles(alice_account
, &error
), "Alice Leaves (%@)", error
);
149 CFReleaseNull(error
);
151 FillChanges(changes
, CFSTR("Alice"));
152 FeedChangesTo(changes
, bob_account
, transport_bob
); // Bob sees alice bail.
154 FillChanges(changes
, CFSTR("Bob"));
155 FeedChangesTo(changes
, alice_account
), transport_alice
; // Alice sees the fallout.
157 FillChanges(changes
, CFSTR("Alice"));
158 FillChanges(changes
, CFSTR("Bob"));
159 accounts_agree("Alice bails", bob_account
, alice_account
);
161 peers
= SOSAccountCopyPeers(alice_account
, &error
);
162 ok(peers
&& CFArrayGetCount(peers
) == 1, "See one peer %@ (%@)", peers
, error
);
163 CFReleaseNull(peers
);
165 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
166 CFReleaseNull(error
);
168 FillChanges(changes
, CFSTR("Alice"));
169 FeedChangesTo(changes
, bob_account
, transport_bob
);
173 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
175 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
176 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
177 CFReleaseNull(error
);
178 CFReleaseNull(applicants
);
181 FillChanges(changes
, CFSTR("Bob"));
182 FeedChangesTo(changes
, alice_account
, transport_alice
); // Alice sees bob accepting
184 FillChanges(changes
, CFSTR("Alice"));
185 FeedChangesTo(changes
, bob_account
, transport_bob
); // Bob sees Alice concurring
187 accounts_agree("Alice accepts' Bob", bob_account
, alice_account
);
189 ok(SOSAccountLeaveCircles(alice_account
, &error
), "Alice Leaves (%@)", error
);
190 CFReleaseNull(error
);
191 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
192 CFReleaseNull(error
);
194 FillChanges(changes
, CFSTR("Alice"));
195 FeedChangesTo(changes
, bob_account
, transport_bob
); // Bob sees Alice leaving and rejoining
196 FillChanges(changes
, CFSTR("Bob"));
197 FeedChangesTo(changes
, alice_account
, transport_alice
); // Alice sees bob concurring
200 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
202 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
203 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
204 CFReleaseNull(error
);
205 CFReleaseNull(applicants
);
207 FillChanges(changes
, CFSTR("Bob"));
208 FeedChangesTo(changes
, alice_account
, transport_alice
); // Alice sees bob accepting
209 FillChanges(changes
, CFSTR("Alice"));
210 FeedChangesTo(changes
, bob_account
, transport_bob
); // Bob sees Alice concurring
212 accounts_agree("Bob accepts Alice", bob_account
, alice_account
);
215 CFReleaseNull(alice_new_gestalt
);
218 CFReleaseNull(bob_account
);
219 CFReleaseNull(alice_account
);
220 CFReleaseNull(carol_account
);
222 SOSUnregisterAllTransportMessages();
223 SOSUnregisterAllTransportCircles();
224 SOSUnregisterAllTransportKeyParameters();
225 CFArrayRemoveAllValues(key_transports
);
226 CFArrayRemoveAllValues(circle_transports
);
227 CFArrayRemoveAllValues(message_transports
);
232 int secd_55_account_incompatibility(int argc
, char *const *argv
)
234 plan_tests(kTestTestCount
);