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 <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/SOSKVSKeys.h>
37 #include <Security/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"
53 #include "SecdTestKeychainUtilities.h"
55 static int kTestTestCount
= 11;
57 static void tests(void)
59 CFErrorRef error
= NULL
;
60 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
61 CFDataRef cfwrong_password
= CFDataCreate(NULL
, (uint8_t *) "NotFooFooFoo", 10);
62 CFStringRef cfaccount
= CFSTR("test@test.org");
63 CFStringRef data_name
= CFSTR("TestSource");
64 CFStringRef circle_key_name
= SOSCircleKeyCreateWithName(data_name
, NULL
);
66 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
68 SOSAccountRef alice_account
= CreateAccountForLocalChanges(CFSTR("Alice"), data_name
);
69 SOSAccountRef bob_account
= CreateAccountForLocalChanges(CFSTR("Bob"), data_name
);
70 SOSAccountRef carol_account
= CreateAccountForLocalChanges(CFSTR("Carol"), data_name
);
72 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
74 // Bob wins writing at this point, feed the changes back to alice.
75 FillAllChanges(changes
);
76 FeedChangesToMulti(changes
, alice_account
, carol_account
, NULL
);
78 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
80 ok(SOSAccountTryUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential trying (%@)", error
);
82 CFReleaseNull(cfpassword
);
84 ok(!SOSAccountTryUserCredentials(alice_account
, cfaccount
, cfwrong_password
, &error
), "Credential failing (%@)", error
);
85 CFReleaseNull(cfwrong_password
);
86 is(error
? CFErrorGetCode(error
) : 0, kSOSErrorWrongPassword
, "Expected SOSErrorWrongPassword");
89 CFDataRef incompatibleDER
= SOSCircleCreateIncompatibleCircleDER(&error
);
91 InjectChangeToMulti(changes
, circle_key_name
, incompatibleDER
, alice_account
, NULL
);
92 CFReleaseNull(circle_key_name
);
93 CFReleaseNull(incompatibleDER
);
95 is(ProcessChangesUntilNoChange(changes
, alice_account
, NULL
), 1, "updates");
98 is(SOSAccountGetCircleStatus(alice_account
, &error
), kSOSCCError
, "Is in circle");
102 ok(SOSAccountResetToOffering(alice_account
, &error
), "Reset to offering (%@)", error
);
103 CFReleaseNull(error
);
104 FillChanges(changes
, CFSTR("Alice"));
105 FeedChangesTo(changes
, bob_account
, transport_bob
);
107 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
108 CFReleaseNull(error
);
110 FeedChangesTo(changes
, alice_account
, transport_alice
);
113 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
115 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
116 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
117 CFReleaseNull(error
);
118 CFReleaseNull(applicants
);
121 FillChanges(changes
, CFSTR("Alice"));
122 FeedChangesTo(changes
, bob_account
, transport_bob
); // Bob sees he's accepted
124 FillChanges(changes
, CFSTR("Bob"));
125 FeedChangesTo(changes
, alice_account
, transport_alice
); // Alice sees bob-concurring
127 ok(CFDictionaryGetCount(changes
) == 0, "We converged. (%@)", changes
);
129 FillChanges(changes
, CFSTR("Alice"));
130 accounts_agree("bob&alice pair", bob_account
, alice_account
);
132 CFArrayRef peers
= SOSAccountCopyPeers(alice_account
, &error
);
133 ok(peers
&& CFArrayGetCount(peers
) == 2, "See two peers %@ (%@)", peers
, error
);
134 CFReleaseNull(peers
);
136 CFDictionaryRef alice_new_gestalt
= SOSCreatePeerGestaltFromName(CFSTR("Alice, but different"));
138 ok(SOSAccountUpdateGestalt(alice_account
, alice_new_gestalt
), "Update gestalt %@ (%@)", alice_account
, error
);
139 CFReleaseNull(alice_new_gestalt
);
141 FillChanges(changes
, CFSTR("Alice"));
142 FeedChangesTo(changes
, bob_account
, transport_bob
); // Bob sees alice change her name.
144 FillChanges(changes
, CFSTR("Bob"));
145 FeedChangesTo(changes
, alice_account
, transport_alice
); // Alice sees the fallout.
147 accounts_agree("Alice's name changed", bob_account
, alice_account
);
149 ok(SOSAccountLeaveCircles(alice_account
, &error
), "Alice Leaves (%@)", error
);
150 CFReleaseNull(error
);
152 FillChanges(changes
, CFSTR("Alice"));
153 FeedChangesTo(changes
, bob_account
, transport_bob
); // Bob sees alice bail.
155 FillChanges(changes
, CFSTR("Bob"));
156 FeedChangesTo(changes
, alice_account
), transport_alice
; // Alice sees the fallout.
158 FillChanges(changes
, CFSTR("Alice"));
159 FillChanges(changes
, CFSTR("Bob"));
160 accounts_agree("Alice bails", bob_account
, alice_account
);
162 peers
= SOSAccountCopyPeers(alice_account
, &error
);
163 ok(peers
&& CFArrayGetCount(peers
) == 1, "See one peer %@ (%@)", peers
, error
);
164 CFReleaseNull(peers
);
166 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
167 CFReleaseNull(error
);
169 FillChanges(changes
, CFSTR("Alice"));
170 FeedChangesTo(changes
, bob_account
, transport_bob
);
174 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
176 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
177 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
178 CFReleaseNull(error
);
179 CFReleaseNull(applicants
);
182 FillChanges(changes
, CFSTR("Bob"));
183 FeedChangesTo(changes
, alice_account
, transport_alice
); // Alice sees bob accepting
185 FillChanges(changes
, CFSTR("Alice"));
186 FeedChangesTo(changes
, bob_account
, transport_bob
); // Bob sees Alice concurring
188 accounts_agree("Alice accepts' Bob", bob_account
, alice_account
);
190 ok(SOSAccountLeaveCircles(alice_account
, &error
), "Alice Leaves (%@)", error
);
191 CFReleaseNull(error
);
192 ok(SOSAccountJoinCircles(alice_account
, &error
), "Alice re-applies (%@)", error
);
193 CFReleaseNull(error
);
195 FillChanges(changes
, CFSTR("Alice"));
196 FeedChangesTo(changes
, bob_account
, transport_bob
); // Bob sees Alice leaving and rejoining
197 FillChanges(changes
, CFSTR("Bob"));
198 FeedChangesTo(changes
, alice_account
, transport_alice
); // Alice sees bob concurring
201 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
203 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
204 ok(SOSAccountAcceptApplicants(bob_account
, applicants
, &error
), "Bob accepts (%@)", error
);
205 CFReleaseNull(error
);
206 CFReleaseNull(applicants
);
208 FillChanges(changes
, CFSTR("Bob"));
209 FeedChangesTo(changes
, alice_account
, transport_alice
); // Alice sees bob accepting
210 FillChanges(changes
, CFSTR("Alice"));
211 FeedChangesTo(changes
, bob_account
, transport_bob
); // Bob sees Alice concurring
213 accounts_agree("Bob accepts Alice", bob_account
, alice_account
);
216 CFReleaseNull(alice_new_gestalt
);
219 CFReleaseNull(bob_account
);
220 CFReleaseNull(alice_account
);
221 CFReleaseNull(carol_account
);
226 int secd_55_account_incompatibility(int argc
, char *const *argv
)
228 plan_tests(kTestTestCount
);
230 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);