]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-201-coders.m
Security-59306.11.20.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-201-coders.m
1 /*
2 * Copyright (c) 2013-2016 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 // secd_201_coders
25 // sec
26 //
27
28 #include <stdio.h>
29
30
31
32
33 #include <Security/SecBase.h>
34 #include <Security/SecItem.h>
35
36 #include <CoreFoundation/CFDictionary.h>
37
38 #include "keychain/SecureObjectSync/SOSAccount.h"
39 #include <Security/SecureObjectSync/SOSCloudCircle.h>
40 #include "keychain/SecureObjectSync/SOSInternal.h"
41 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
42 #include "keychain/SecureObjectSync/SOSTransport.h"
43 #include "keychain/SecureObjectSync/SOSEngine.h"
44 #import "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
45 #include <stdlib.h>
46 #include <unistd.h>
47
48 #include "secd_regressions.h"
49 #include "SOSTestDataSource.h"
50 #include "SOSTestDevice.h"
51
52 #include "SOSRegressionUtilities.h"
53 #include <utilities/SecCFWrappers.h>
54 #include <Security/SecKeyPriv.h>
55
56 #include <securityd/SOSCloudCircleServer.h>
57
58 #include "SOSAccountTesting.h"
59
60 #include "SecdTestKeychainUtilities.h"
61
62 static void tests(void)
63 {
64
65 __block CFErrorRef error = NULL;
66 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
67 CFDataRef cfwrong_password = CFDataCreate(NULL, (uint8_t *) "NotFooFooFoo", 10);
68 CFStringRef cfaccount = CFSTR("test@test.org");
69
70 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
71 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
72 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
73
74 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
75
76 // Bob wins writing at this point, feed the changes back to alice.
77 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
78
79 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
80 CFReleaseNull(error);
81 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential trying (%@)", error);
82 CFReleaseNull(cfpassword);
83
84 CFReleaseNull(error);
85 ok(!SOSAccountTryUserCredentials(alice_account, cfaccount, cfwrong_password, &error), "Credential failing (%@)", error);
86 CFReleaseNull(cfwrong_password);
87 is(error ? CFErrorGetCode(error) : 0, kSOSErrorWrongPassword, "Expected SOSErrorWrongPassword");
88 CFReleaseNull(error);
89
90 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
91 CFReleaseNull(error);
92
93 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
94
95 ok(SOSAccountHasCompletedInitialSync(alice_account), "Alice thinks she's completed initial sync");
96
97 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
98 CFReleaseNull(error);
99
100 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
101
102 {
103 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
104
105 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
106 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
107 CFReleaseNull(error);
108 CFReleaseNull(applicants);
109 }
110
111 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
112
113 accounts_agree("bob&alice pair", bob_account, alice_account);
114
115 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
116 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
117 CFReleaseNull(peers);
118
119 //creating test devices
120 CFIndex version = 0;
121
122 // Optionally prefix each peer with name to make them more unique.
123 CFArrayRef deviceIDs = CFArrayCreateForCFTypes(kCFAllocatorDefault,alice_account.peerID, bob_account.peerID, NULL);
124 CFSetRef views = SOSViewsCopyTestV2Default();
125 CFMutableArrayRef peerMetas = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
126 CFStringRef deviceID;
127 CFArrayForEachC(deviceIDs, deviceID) {
128 SOSPeerMetaRef peerMeta = SOSPeerMetaCreateWithComponents(deviceID, views, NULL);
129 CFArrayAppendValue(peerMetas, peerMeta);
130 CFReleaseNull(peerMeta);
131 }
132
133 CFReleaseNull(views);
134 CFArrayForEachC(deviceIDs, deviceID) {
135 SOSTestDeviceRef device = SOSTestDeviceCreateWithDbNamed(kCFAllocatorDefault, deviceID, deviceID);
136 SOSTestDeviceSetPeerIDs(device, peerMetas, version, NULL);
137
138 if([alice_account.peerID isEqual: (__bridge id) deviceID]){
139 alice_account.factory = device->dsf;
140 SOSTestDeviceAddGenericItem(device, CFSTR("Alice"), CFSTR("Alice-add"));
141 }
142 else{
143 bob_account.factory = device->dsf;
144 SOSTestDeviceAddGenericItem(device, CFSTR("Bob"), CFSTR("Bob-add"));
145 }
146
147 CFReleaseNull(device);
148 }
149 CFReleaseNull(deviceIDs);
150 CFReleaseNull(peerMetas);
151
152 SOSUnregisterAllTransportMessages();
153 CFArrayRemoveAllValues(message_transports);
154
155
156 ok(SOSAccountEnsurePeerRegistration(alice_account, NULL), "ensure peer registration - alice");
157 ok(SOSAccountEnsurePeerRegistration(bob_account, NULL), "ensure peer registration - bob");
158
159 alice_account = nil;
160 bob_account = nil;
161
162 SOSTestCleanup();
163
164
165 CFReleaseNull(changes);
166 }
167
168 int secd_201_coders(int argc, char *const *argv)
169 {
170 plan_tests(38);
171
172 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
173
174 tests();
175
176 return 0;
177 }