2 * Copyright (c) 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@
25 #include <Security/SecBase.h>
26 #include <Security/SecItem.h>
28 #include <CoreFoundation/CFDictionary.h>
30 #include <Security/SecureObjectSync/SOSAccount.h>
31 #include <Security/SecureObjectSync/SOSCloudCircle.h>
32 #include <Security/SecureObjectSync/SOSInternal.h>
33 #include <Security/SecureObjectSync/SOSUserKeygen.h>
34 #include <Security/SecureObjectSync/SOSTransport.h>
39 #include "secd_regressions.h"
40 #include "SOSTestDataSource.h"
42 #include "SOSRegressionUtilities.h"
43 #include <utilities/SecCFWrappers.h>
44 #include <Security/SecKeyPriv.h>
46 #include <securityd/SOSCloudCircleServer.h>
48 #include "SOSAccountTesting.h"
50 #include "SecdTestKeychainUtilities.h"
52 static int kTestTestCount
= 75;
55 static bool SOSTestAcceptApplicant(SOSAccountRef acceptor
, CFIndex appsExpected
, CFErrorRef
*error
) {
57 CFArrayRef applicants
= SOSAccountCopyApplicants(acceptor
, error
);
58 CFStringRef acceptorName
= SOSAccountCopyName(acceptor
);
60 ok(applicants
&& CFArrayGetCount(applicants
) == appsExpected
, "See %ld applicant %@ (%@)", appsExpected
, applicants
, *error
);
61 if(CFArrayGetCount(applicants
) != appsExpected
) retval
= false;
62 ok(retval
&& (retval
= SOSAccountAcceptApplicants(acceptor
, applicants
, error
)), "%@ accepts (%@)", acceptorName
, *error
);
63 CFReleaseNull(*error
);
64 CFReleaseNull(applicants
);
65 CFReleaseNull(acceptorName
);
70 static void tests(void)
72 CFErrorRef error
= NULL
;
73 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
74 CFStringRef cfaccount
= CFSTR("test@test.org");
76 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
77 SOSAccountRef alice_account
= CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
78 SOSAccountRef bob_account
= CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
79 SOSAccountRef carole_account
= CreateAccountForLocalChanges(CFSTR("Carol"), CFSTR("TestSource"));
81 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
83 // Bob wins writing at this point, feed the changes back to alice.
84 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, NULL
), 1, "updates");
86 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
89 ok(SOSAccountResetToOffering_wTxn(alice_account
, &error
), "Reset to offering (%@)", error
);
92 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, NULL
), 2, "updates");
94 ok(SOSAccountJoinCircles_wTxn(bob_account
, &error
), "Bob Applies (%@)", error
);
97 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, NULL
), 2, "updates");
99 ok(SOSTestAcceptApplicant(alice_account
, 1, &error
), "Alice Accepts");
101 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, NULL
), 3, "updates");
103 accounts_agree("bob&alice pair", bob_account
, alice_account
);
105 ok(SOSAccountLeaveCircle(alice_account
, &error
));
106 CFReleaseNull(error
);
108 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, NULL
), 2, "updates");
110 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account
, cfaccount
, cfpassword
, &error
), "Credential setting (%@)", error
);
111 CFReleaseNull(cfpassword
);
113 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, NULL
), 1, "updates");
115 SOSAccountTransactionRef txn
= SOSAccountTransactionCreate(carole_account
);
117 SOSPeerInfoRef carole_pi
= SOSAccountCopyApplication(carole_account
, &error
);
118 ok(carole_pi
!= NULL
, "Got Carole's Application PeerInfo");
119 CFReleaseNull(error
);
121 SOSAccountTransactionFinish(txn
);
124 SOSAccountPurgePrivateCredential(bob_account
);
126 CFDataRef bobBlob
= SOSAccountCopyCircleJoiningBlob(bob_account
, carole_pi
, &error
);
127 ok(bobBlob
!= NULL
, "Got Piggy Back Blob from Bob");
128 CFReleaseNull(error
);
130 ok(SOSAccountJoinWithCircleJoiningBlob(carole_account
, bobBlob
, &error
), "Carole joins circle through piggy backing: %@", error
);
131 CFReleaseNull(error
);
132 CFReleaseNull(bobBlob
);
134 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, carole_account
, NULL
), 2, "Settle Circle");
136 ok(!SOSAccountIsInCircle(alice_account
, NULL
), "Alice still retired");
137 ok(SOSAccountIsInCircle(bob_account
, NULL
), "Bob still here");
138 ok(SOSAccountIsInCircle(carole_account
, NULL
), "carol still here");
140 ok(!SOSAccountCheckHasBeenInSync_wTxn(carole_account
), "Carol not in sync");
142 CFReleaseNull(bob_account
);
143 CFReleaseNull(alice_account
);
144 CFReleaseNull(carole_account
);
146 SOSUnregisterAllTransportMessages();
147 SOSUnregisterAllTransportCircles();
148 SOSUnregisterAllTransportKeyParameters();
149 CFArrayRemoveAllValues(key_transports
);
150 CFArrayRemoveAllValues(circle_transports
);
151 CFArrayRemoveAllValues(message_transports
);
156 int secd_90_hsa2(int argc
, char *const *argv
)
158 plan_tests(kTestTestCount
);
160 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);