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/SOSTransport.h>
41 #include "secd_regressions.h"
42 #include "SOSTestDataSource.h"
44 #include "SOSRegressionUtilities.h"
45 #include <utilities/SecCFWrappers.h>
46 #include <Security/SecKeyPriv.h>
48 #include <securityd/SOSCloudCircleServer.h>
50 #include "SOSAccountTesting.h"
52 #include "SecdTestKeychainUtilities.h"
54 static int kTestTestCount
= 85;
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");
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"));
68 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
70 // Bob wins writing at this point, feed the changes back to alice.
71 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 1, "updates");
73 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
75 ok(SOSAccountTryUserCredentials(alice_account
, cfaccount
, cfpassword
, &error
), "Credential trying (%@)", 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");
82 ok(SOSAccountResetToOffering(alice_account
, &error
), "Reset to offering (%@)", error
);
85 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
87 ok(SOSAccountJoinCircles(bob_account
, &error
), "Bob Applies (%@)", error
);
90 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
93 CFArrayRef applicants
= SOSAccountCopyApplicants(alice_account
, &error
);
95 ok(applicants
&& CFArrayGetCount(applicants
) == 1, "See one applicant %@ (%@)", applicants
, error
);
96 ok(SOSAccountAcceptApplicants(alice_account
, applicants
, &error
), "Alice accepts (%@)", error
);
98 CFReleaseNull(applicants
);
101 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 3, "updates");
103 accounts_agree("bob&alice pair", bob_account
, alice_account
);
105 CFArrayRef peers
= SOSAccountCopyPeers(alice_account
, &error
);
106 ok(peers
&& CFArrayGetCount(peers
) == 2, "See two peers %@ (%@)", peers
, error
);
107 CFReleaseNull(peers
);
109 //bob now goes def while Alice does some stuff.
111 ok(SOSAccountLeaveCircle(alice_account
, &error
), "ALICE LEAVES THE CIRCLE (%@)", error
);
112 ok(SOSAccountResetToOffering(alice_account
, &error
), "Alice resets to offering again (%@)", error
);
114 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 4, "updates");
116 accounts_agree("bob&alice pair", bob_account
, alice_account
);
118 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carol_account
, NULL
), 1, "updates");
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
);
125 int64_t valuePtr
= 0;
126 CFNumberRef gencount
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberCFIndexType
, &valuePtr
);
127 SOSCircleSetGeneration(carol_account
->trusted_circle
, gencount
);
129 SecKeyRef user_privkey
= SOSUserKeygen(cfpassword
, carol_account
->user_key_parameters
, &error
);
130 CFNumberRef genCountTest
= SOSCircleGetGeneration(carol_account
->trusted_circle
);
132 CFNumberGetValue(genCountTest
, kCFNumberCFIndexType
, &testPtr
);
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
);
139 SOSTransportCirclePostCircle(carol_account
->circle_transport
, SOSCircleGetName(carol_account
->trusted_circle
), circle_data
, &error
);
142 genCountTest
= SOSCircleGetGeneration(carol_account
->trusted_circle
);
143 CFNumberGetValue(genCountTest
, kCFNumberCFIndexType
, &testPtr
);
146 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
147 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
149 SOSAccountSetUserPublicTrustedForTesting(alice_account
);
150 SOSAccountSetUserPublicTrustedForTesting(bob_account
);
152 is(ProcessChangesUntilNoChange(changes
, carol_account
, alice_account
, bob_account
, NULL
), 2, "updates");
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
);
158 CFReleaseNull(gencount
);
159 CFReleaseNull(bob_account
);
160 CFReleaseNull(alice_account
);
161 CFReleaseNull(cfpassword
);
163 SOSUnregisterAllTransportMessages();
164 SOSUnregisterAllTransportCircles();
165 SOSUnregisterAllTransportKeyParameters();
166 CFArrayRemoveAllValues(key_transports
);
167 CFArrayRemoveAllValues(circle_transports
);
168 CFArrayRemoveAllValues(message_transports
);
172 int secd_52_offering_gencount_reset(int argc
, char *const *argv
)
174 plan_tests(kTestTestCount
);
176 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);