]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-90-hsa2.c
Security-57740.51.3.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-90-hsa2.c
1 /*
2 * Copyright (c) 2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #include <Security/SecBase.h>
26 #include <Security/SecItem.h>
27
28 #include <CoreFoundation/CFDictionary.h>
29
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>
35
36 #include <stdlib.h>
37 #include <unistd.h>
38
39 #include "secd_regressions.h"
40 #include "SOSTestDataSource.h"
41
42 #include "SOSRegressionUtilities.h"
43 #include <utilities/SecCFWrappers.h>
44 #include <Security/SecKeyPriv.h>
45
46 #include <securityd/SOSCloudCircleServer.h>
47
48 #include "SOSAccountTesting.h"
49
50 #include "SecdTestKeychainUtilities.h"
51
52 static int kTestTestCount = 75;
53
54
55 static bool SOSTestAcceptApplicant(SOSAccountRef acceptor, CFIndex appsExpected, CFErrorRef *error) {
56 bool retval = true;
57 CFArrayRef applicants = SOSAccountCopyApplicants(acceptor, error);
58 CFStringRef acceptorName = SOSAccountCopyName(acceptor);
59
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);
66
67 return retval;
68 }
69
70 static void tests(void)
71 {
72 CFErrorRef error = NULL;
73 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
74 CFStringRef cfaccount = CFSTR("test@test.org");
75
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"));
80
81 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
82
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");
85
86 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
87 CFReleaseNull(error);
88
89 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
90 CFReleaseNull(error);
91
92 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
93
94 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
95 CFReleaseNull(error);
96
97 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
98
99 ok(SOSTestAcceptApplicant(alice_account, 1, &error), "Alice Accepts");
100
101 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 3, "updates");
102
103 accounts_agree("bob&alice pair", bob_account, alice_account);
104
105 ok(SOSAccountLeaveCircle(alice_account, &error));
106 CFReleaseNull(error);
107
108 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
109
110 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
111 CFReleaseNull(cfpassword);
112
113 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
114
115 SOSAccountTransactionRef txn = SOSAccountTransactionCreate(carole_account);
116
117 SOSPeerInfoRef carole_pi = SOSAccountCopyApplication(carole_account, &error);
118 ok(carole_pi != NULL, "Got Carole's Application PeerInfo");
119 CFReleaseNull(error);
120
121 SOSAccountTransactionFinish(txn);
122 CFReleaseNull(txn);
123
124 SOSAccountPurgePrivateCredential(bob_account);
125
126 CFDataRef bobBlob = SOSAccountCopyCircleJoiningBlob(bob_account, carole_pi, &error);
127 ok(bobBlob != NULL, "Got Piggy Back Blob from Bob");
128 CFReleaseNull(error);
129
130 ok(SOSAccountJoinWithCircleJoiningBlob(carole_account, bobBlob, &error), "Carole joins circle through piggy backing: %@", error);
131 CFReleaseNull(error);
132 CFReleaseNull(bobBlob);
133
134 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "Settle Circle");
135
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");
139
140 ok(!SOSAccountCheckHasBeenInSync_wTxn(carole_account), "Carol not in sync");
141
142 CFReleaseNull(bob_account);
143 CFReleaseNull(alice_account);
144 CFReleaseNull(carole_account);
145
146 SOSTestCleanup();
147 }
148
149
150 int secd_90_hsa2(int argc, char *const *argv)
151 {
152 plan_tests(kTestTestCount);
153
154 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
155
156 tests();
157
158 return 0;
159 }