]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd_77_ids_messaging.m
Security-58286.200.222.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd_77_ids_messaging.m
1 //
2 // secd_77_ids_messaging.c
3 // sec
4 //
5
6 /*
7 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
8 *
9 * @APPLE_LICENSE_HEADER_START@
10 *
11 * This file contains Original Code and/or Modifications of Original Code
12 * as defined in and that are subject to the Apple Public Source License
13 * Version 2.0 (the 'License'). You may not use this file except in
14 * compliance with the License. Please obtain a copy of the License at
15 * http://www.opensource.apple.com/apsl/ and read it before using this
16 * file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_LICENSE_HEADER_END@
27 */
28
29
30 #include <stdio.h>
31 #include <Security/SecBase.h>
32 #include <Security/SecItem.h>
33
34 #include <Security/SecureObjectSync/SOSAccount.h>
35 #include <Security/SecureObjectSync/SOSCloudCircle.h>
36 #include <Security/SecureObjectSync/SOSInternal.h>
37 #include <Security/SecureObjectSync/SOSFullPeerInfo.h>
38 #include <Security/SecureObjectSync/SOSUserKeygen.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 #include "secd_regressions.h"
43 #include "SOSTestDataSource.h"
44
45 #include "SOSRegressionUtilities.h"
46 #include <utilities/SecCFWrappers.h>
47
48 #include <securityd/SOSCloudCircleServer.h>
49 #include "SecdTestKeychainUtilities.h"
50 #include "SOSAccountTesting.h"
51 #import "SOSTransportTestTransports.h"
52 #include "SOSTestDevice.h"
53 #include "SOSTestDataSource.h"
54 #include <Security/SecureObjectSync/SOSTransportMessageIDS.h>
55 #include <Security/SecureObjectSync/SOSAccountTrustClassic+Circle.h>
56
57 static bool SOSAccountIsThisPeerIDMe(SOSAccount* account, CFStringRef peerID) {
58 SOSAccountTrustClassic* trust = account.trust;
59 SOSPeerInfoRef mypi = trust.peerInfo;
60 CFStringRef myPeerID = SOSPeerInfoGetPeerID(mypi);
61
62 return myPeerID && CFEqualSafe(myPeerID, peerID);
63 }
64
65 __unused static void ids_test_sync(SOSAccount* alice_account, SOSAccount* bob_account){
66
67 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
68 __block bool SyncingCompletedOverIDS = false;
69 __block CFErrorRef localError = NULL;
70 __block bool done = false;
71 SOSAccountTrustClassic* aliceTrust = alice_account.trust;
72 SOSAccountTrustClassic* bobTrust = bob_account.trust;
73
74 do{
75 SOSCircleForEachValidPeer(aliceTrust.trustedCircle, alice_account.accountKey, ^(SOSPeerInfoRef peer) {
76 if (!SOSAccountIsThisPeerIDMe(alice_account, SOSPeerInfoGetPeerID(peer))) {
77 if(SOSPeerInfoShouldUseIDSTransport(aliceTrust.peerInfo, peer) &&
78 SOSPeerInfoShouldUseIDSMessageFragmentation(aliceTrust.peerInfo, peer)){
79 secnotice("IDS Transport","Syncing with IDS capable peers using IDS!");
80
81 CFMutableSetRef ids = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
82 CFSetAddValue(ids, SOSPeerInfoGetPeerID(peer));
83
84 SyncingCompletedOverIDS = [alice_account.ids_message_transport SOSTransportMessageSyncWithPeers:alice_account.ids_message_transport p:ids err:&localError];
85 CFReleaseNull(ids);
86 }
87 }
88 });
89
90 ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL);
91
92 SOSCircleForEachValidPeer(bobTrust.trustedCircle, bob_account.accountKey, ^(SOSPeerInfoRef peer) {
93 if (!SOSAccountIsThisPeerIDMe(bob_account, SOSPeerInfoGetPeerID(peer))) {
94 if(SOSPeerInfoShouldUseIDSTransport(bobTrust.peerInfo, peer) &&
95 SOSPeerInfoShouldUseIDSMessageFragmentation(bobTrust.peerInfo, peer)){
96 secnotice("IDS Transport","Syncing with IDS capable peers using IDS!");
97
98 CFMutableSetRef ids = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
99 CFSetAddValue(ids, SOSPeerInfoGetPeerID(peer));
100
101 SyncingCompletedOverIDS = [(SOSMessageIDSTest*)bob_account.ids_message_transport SOSTransportMessageSyncWithPeers:(SOSMessageIDSTest*)bob_account.ids_message_transport p:ids err:&localError];
102 CFReleaseNull(ids);
103 }
104 }
105 });
106
107 ok(SyncingCompletedOverIDS, "synced items over IDS");
108 if(CFDictionaryGetCount(SOSTransportMessageIDSTestGetChanges((SOSMessageIDSTest*)alice_account.ids_message_transport)) == 0 && CFDictionaryGetCount(SOSTransportMessageIDSTestGetChanges((SOSMessageIDSTest*)bob_account.ids_message_transport)) == 0){
109 done = true;
110 break;
111 }
112
113 ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL);
114
115 }while(done == false);
116 CFReleaseNull(changes);
117 }
118
119 static void tests()
120 {
121 CFErrorRef error = NULL;
122
123 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
124 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
125 CFStringRef cfaccount = CFSTR("test@test.org");
126 CFStringRef dsName = CFSTR("Test");
127
128 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), dsName);
129 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), dsName);
130
131 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
132
133 // Bob wins writing at this point, feed the changes back to alice.
134 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
135
136 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
137 CFReleaseNull(cfpassword);
138 CFReleaseNull(error);
139
140 ok(NULL != alice_account, "Alice Created");
141 ok(NULL != bob_account, "Bob Created");
142
143 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
144 CFReleaseNull(error);
145
146 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
147
148 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
149 CFReleaseNull(error);
150
151 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
152
153 {
154 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
155
156 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
157 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
158 CFReleaseNull(error);
159 CFReleaseNull(applicants);
160 }
161
162 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
163
164 accounts_agree("bob&alice pair", bob_account, alice_account);
165
166 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
167 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
168 CFReleaseNull(peers);
169
170 //creating test devices
171 CFIndex version = 0;
172
173 // Optionally prefix each peer with name to make them more unique.
174 CFArrayRef deviceIDs = CFArrayCreateForCFTypes(kCFAllocatorDefault,alice_account.peerID, bob_account.peerID, NULL);
175 CFSetRef views = SOSViewsCopyTestV2Default();
176 CFMutableArrayRef peerMetas = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
177 CFStringRef deviceID;
178 CFArrayForEachC(deviceIDs, deviceID) {
179 SOSPeerMetaRef peerMeta = SOSPeerMetaCreateWithComponents(deviceID, views, NULL);
180 CFArrayAppendValue(peerMetas, peerMeta);
181 CFReleaseNull(peerMeta);
182 }
183
184 CFReleaseNull(views);
185 CFArrayForEachC(deviceIDs, deviceID) {
186 SOSTestDeviceRef device = SOSTestDeviceCreateWithDbNamed(kCFAllocatorDefault, deviceID, deviceID);
187 SOSTestDeviceSetPeerIDs(device, peerMetas, version, NULL);
188
189 if([alice_account.peerID isEqual: (__bridge id)(deviceID)]){
190 alice_account.factory = device->dsf;
191 SOSTestDeviceAddGenericItem(device, CFSTR("Alice"), CFSTR("Alice-add"));
192 }
193 else{
194 bob_account.factory = device->dsf;
195 SOSTestDeviceAddGenericItem(device, CFSTR("Bob"), CFSTR("Bob-add"));
196 }
197
198 CFReleaseNull(device);
199 }
200 CFReleaseNull(deviceIDs);
201 CFReleaseNull(peerMetas);
202
203 SOSUnregisterAllTransportMessages();
204 CFArrayRemoveAllValues(message_transports);
205
206 SOSAccountTrustClassic* aliceTrust = alice_account.trust;
207 SOSAccountTrustClassic* bobTrust = bob_account.trust;
208
209 alice_account.ids_message_transport = (SOSMessageIDS*)[[SOSMessageIDSTest alloc] initWithAccount:alice_account andAccountName:CFSTR("Alice") andCircleName:SOSCircleGetName(aliceTrust.trustedCircle) err:&error ];
210
211 bob_account.ids_message_transport = (SOSMessageIDS*)[[SOSMessageIDSTest alloc] initWithAccount:bob_account andAccountName:CFSTR("Bob") andCircleName:SOSCircleGetName(bobTrust.trustedCircle) err:&error];
212
213 ok(alice_account.ids_message_transport != NULL, "Alice Account, Created IDS Test Transport");
214 ok(bob_account.ids_message_transport != NULL, "Bob Account, Created IDS Test Transport");
215
216 bool result = [alice_account.trust modifyCircle:alice_account.circle_transport err:&error action:^bool(SOSCircleRef circle) {
217 CFErrorRef localError = NULL;
218
219 SOSFullPeerInfoUpdateTransportType(aliceTrust.fullPeerInfo, SOSTransportMessageTypeIDSV2, &localError);
220 SOSFullPeerInfoUpdateTransportPreference(aliceTrust.fullPeerInfo, kCFBooleanFalse, &localError);
221 SOSFullPeerInfoUpdateTransportFragmentationPreference(aliceTrust.fullPeerInfo, kCFBooleanTrue, &localError);
222 SOSFullPeerInfoUpdateTransportAckModelPreference(aliceTrust.fullPeerInfo, kCFBooleanTrue, &localError);
223
224 return SOSCircleHasPeer(circle, aliceTrust.peerInfo, NULL);
225 }];
226
227 ok(result, "Alice account update circle with transport type");
228
229 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
230
231 result = [bob_account.trust modifyCircle:bob_account.circle_transport err:&error action:^bool(SOSCircleRef circle) {
232 CFErrorRef localError = NULL;
233
234 SOSFullPeerInfoUpdateTransportType(bobTrust.fullPeerInfo, SOSTransportMessageTypeIDSV2, &localError);
235 SOSFullPeerInfoUpdateTransportPreference(bobTrust.fullPeerInfo, kCFBooleanFalse, &localError);
236 SOSFullPeerInfoUpdateTransportFragmentationPreference(bobTrust.fullPeerInfo, kCFBooleanTrue, &localError);
237 SOSFullPeerInfoUpdateTransportAckModelPreference(bobTrust.fullPeerInfo, kCFBooleanTrue, &localError);
238
239 return SOSCircleHasPeer(circle, bobTrust.peerInfo, NULL);
240 }];
241
242 ok(result, "Bob account update circle with transport type");
243 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
244
245 CFStringRef alice_transportType =SOSPeerInfoCopyTransportType(alice_account.peerInfo);
246 CFStringRef bob_accountTransportType = SOSPeerInfoCopyTransportType(bob_account.peerInfo);
247 ok(CFEqualSafe(alice_transportType, CFSTR("IDS2.0")), "Alice transport type not IDS");
248 ok(CFEqualSafe(bob_accountTransportType, CFSTR("IDS2.0")), "Bob transport type not IDS");
249
250 CFReleaseNull(alice_transportType);
251 CFReleaseNull(bob_accountTransportType);
252
253 SOSTransportMessageIDSTestSetName((SOSMessageIDSTest*)alice_account.ids_message_transport, CFSTR("Alice Account"));
254 ok(SOSTransportMessageIDSTestGetName((SOSMessageIDSTest*)alice_account.ids_message_transport) != NULL, "retrieved getting account name");
255 ok(SOSAccountRetrieveDeviceIDFromKeychainSyncingOverIDSProxy(alice_account, &error) != false, "device ID from KeychainSyncingOverIDSProxy");
256
257 SOSTransportMessageIDSTestSetName((SOSMessageIDSTest*)bob_account.ids_message_transport, CFSTR("Bob Account"));
258 ok(SOSTransportMessageIDSTestGetName((SOSMessageIDSTest*)bob_account.ids_message_transport) != NULL, "retrieved getting account name");
259 ok(SOSAccountRetrieveDeviceIDFromKeychainSyncingOverIDSProxy(bob_account, &error) != false, "device ID from KeychainSyncingOverIDSProxy");
260
261
262 ok(SOSAccountSetMyDSID_wTxn(alice_account, CFSTR("Alice"),&error), "Setting IDS device ID");
263 CFStringRef alice_dsid = SOSAccountCopyDeviceID(alice_account, &error);
264 ok(CFEqualSafe(alice_dsid, CFSTR("Alice")), "Getting IDS device ID");
265
266 ok(SOSAccountSetMyDSID_wTxn(bob_account, CFSTR("Bob"),&error), "Setting IDS device ID");
267 CFStringRef bob_dsid = SOSAccountCopyDeviceID(bob_account, &error);
268 ok(CFEqualSafe(bob_dsid, CFSTR("Bob")), "Getting IDS device ID");
269
270 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
271
272
273 ok(SOSAccountEnsurePeerRegistration(alice_account, NULL), "ensure peer registration - alice");
274
275 ok(SOSAccountEnsurePeerRegistration(bob_account, NULL), "ensure peer registration - bob");
276
277
278 //ids_test_sync(alice_account, bob_account);
279
280 CFReleaseNull(bob_dsid);
281 CFReleaseNull(alice_dsid);
282 CFReleaseNull(changes);
283
284 SOSTestCleanup();
285 }
286
287 int secd_77_ids_messaging(int argc, char *const *argv)
288 {
289 plan_tests(100);
290
291 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
292
293 tests();
294
295 return 0;
296 }