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