2 * Copyright (c) 2013-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@
26 #include <Security/SecBase.h>
27 #include <Security/SecItem.h>
29 #include <SecureObjectSync/SOSAccount.h>
30 #include <SecureObjectSync/SOSCloudCircle.h>
31 #include <SecureObjectSync/SOSInternal.h>
32 #include <SecureObjectSync/SOSUserKeygen.h>
37 #include "secd_regressions.h"
38 #include "SOSTestDataSource.h"
40 #include "SOSRegressionUtilities.h"
41 #include <utilities/SecCFWrappers.h>
43 #include <securityd/SOSCloudCircleServer.h>
45 #include "SecdTestKeychainUtilities.h"
46 #include "SOSAccountTesting.h"
48 static int kTestTestCount
= 156;
50 static void tests(void)
52 CFErrorRef error
= NULL
;
53 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
54 CFStringRef cfaccount
= CFSTR("test@test.org");
56 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
58 SOSAccountRef alice_account
= CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
59 SOSAccountRef bob_account
= CreateAccountForLocalChanges( CFSTR("Bob"), CFSTR("TestSource"));
60 SOSAccountRef carol_account
= CreateAccountForLocalChanges(CFSTR("Carol"), CFSTR("TestSource"));
62 ok(SOSAccountAssertUserCredentials(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
64 // Bob wins writing at this point, feed the changes back to alice.
65 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 1, "update");
68 CFDictionaryRef new_gestalt
= SOSCreatePeerGestaltFromName(CFSTR("New Device"));
69 ok (SOSAccountUpdateGestalt(bob_account
, new_gestalt
), "did we send a null circle?");
70 CFReleaseNull(new_gestalt
);
72 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 1, "nothing published");
74 ok(SOSAccountAssertUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
76 ok(SOSAccountAssertUserCredentials(carol_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
78 CFReleaseNull(cfpassword
);
80 /* ==================== Three Accounts setup =============================================*/
82 ok(SOSAccountResetToOffering(alice_account
, &error
), "Reset to offering (%@)", error
);
85 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "update");
87 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
90 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "update");
93 ok(SOSAccountJoinCircles(carol_account
, &error
), "Carol Applies (%@)", error
);
96 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "update");
100 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
102 ok(applicants
&& CFArrayGetCount(applicants
) == 2, "See two applicants %@ (%@)", applicants
, error
);
103 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
104 CFReleaseNull(error
);
105 CFReleaseNull(applicants
);
108 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 4, "update");
110 accounts_agree_internal("bob&alice pair", bob_account
, alice_account
, false);
111 accounts_agree_internal("bob&carol pair", bob_account
, carol_account
, false);
113 /* ==================== Three Accounts in circle =============================================*/
114 InjectChangeToMulti(changes
, CFSTR("^AccountChanged"), CFSTR("none"), alice_account
, bob_account
, carol_account
, NULL
);
116 SOSAccountEnsureFactoryCirclesTest(alice_account
, CFSTR("Alice"));
117 SOSAccountEnsureFactoryCirclesTest(bob_account
, CFSTR("Bob"));
118 SOSAccountEnsureFactoryCirclesTest(carol_account
, CFSTR("Carol"));
120 is(SOSAccountIsInCircles(alice_account
, &error
), kSOSCCError
, "Account reset - no user keys - error");
121 is(SOSAccountIsInCircles(bob_account
, &error
), kSOSCCError
, "Account reset - no user keys - error");
122 is(SOSAccountIsInCircles(carol_account
, &error
), kSOSCCError
, "Account reset - no user keys - error");
124 CFDataRef cfpassword2
= CFDataCreate(NULL
, (uint8_t *) "ooFooFooF", 10);
125 CFStringRef cfaccount2
= CFSTR("test2@test.org");
127 ok(SOSAccountAssertUserCredentials(alice_account
, cfaccount2
, cfpassword2
, &error
), "Credential setting (%@)", error
);
128 CFReleaseNull(error
);
130 is(SOSAccountIsInCircles(alice_account
, &error
), kSOSCCCircleAbsent
, "Account reset - circle is absent");
131 is(SOSAccountIsInCircles(bob_account
, &error
), kSOSCCError
, "Account reset - no user keys - error");
133 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
135 ok(SOSAccountAssertUserCredentials(bob_account
, cfaccount2
, cfpassword2
, &error
), "Credential setting (%@)", error
);
136 CFReleaseNull(error
);
138 ok(SOSAccountAssertUserCredentials(carol_account
, cfaccount2
, cfpassword2
, &error
), "Credential setting (%@)", error
);
139 CFReleaseNull(error
);
141 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 1, "updates");
143 is(SOSAccountIsInCircles(bob_account
, &error
), kSOSCCCircleAbsent
, "Account reset - circle is absent");
144 is(SOSAccountIsInCircles(carol_account
, &error
), kSOSCCCircleAbsent
, "Account reset - circle is absent");
145 // Now everyone is playing the same account.
147 /* ==================== Three Accounts setup =============================================*/
149 ok(SOSAccountResetToOffering(alice_account
, &error
), "Reset to offering (%@)", error
);
150 CFReleaseNull(error
);
151 is(countActivePeers(alice_account
), 2, "2 peers - alice and icloud");
153 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
155 is(SOSAccountIsInCircles(alice_account
, &error
), kSOSCCInCircle
, "Alice is in circle");
156 is(SOSAccountIsInCircles(bob_account
, &error
), kSOSCCNotInCircle
, "Bob is not in circle");
157 is(SOSAccountIsInCircles(carol_account
, &error
), kSOSCCNotInCircle
, "Carol is not in circle");
159 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
160 CFReleaseNull(error
);
162 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
164 ok(SOSAccountJoinCircles(carol_account
, &error
), "Carol Applies (%@)", error
);
165 CFReleaseNull(error
);
167 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 2, "updates");
169 is(SOSAccountIsInCircles(carol_account
, &error
), kSOSCCRequestPending
, "Carol has a pending request");
172 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
174 ok(applicants
&& CFArrayGetCount(applicants
) == 2, "See two applicants %@ (%@)", applicants
, error
);
175 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
176 CFReleaseNull(error
);
177 CFReleaseNull(applicants
);
178 is(countActivePeers(alice_account
), 4, "4 peers - alice, bob, carol, and icloud");
181 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 4, "updates");
183 accounts_agree_internal("bob&alice pair", bob_account
, alice_account
, false);
184 accounts_agree_internal("bob&carol pair", bob_account
, carol_account
, false);
186 CFReleaseSafe(cfpassword2
);
188 SOSUnregisterAllTransportMessages();
189 SOSUnregisterAllTransportCircles();
190 SOSUnregisterAllTransportKeyParameters();
191 CFArrayRemoveAllValues(key_transports
);
192 CFArrayRemoveAllValues(circle_transports
);
193 CFArrayRemoveAllValues(message_transports
);
198 int secd_52_account_changed(int argc
, char *const *argv
)
200 plan_tests(kTestTestCount
);