2 // secd-76-idstransport.c
8 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
10 * @APPLE_LICENSE_HEADER_START@
12 * This file contains Original Code and/or Modifications of Original Code
13 * as defined in and that are subject to the Apple Public Source License
14 * Version 2.0 (the 'License'). You may not use this file except in
15 * compliance with the License. Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this
19 * The Original Code and all software distributed under the License are
20 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
21 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
22 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
24 * Please see the License for the specific language governing rights and
25 * limitations under the License.
27 * @APPLE_LICENSE_HEADER_END@
32 #include <Security/SecBase.h>
33 #include <Security/SecItem.h>
35 #include <Security/SecureObjectSync/SOSAccount.h>
36 #include <Security/SecureObjectSync/SOSCloudCircle.h>
37 #include <Security/SecureObjectSync/SOSInternal.h>
38 #include <Security/SecureObjectSync/SOSFullPeerInfo.h>
39 #include <Security/SecureObjectSync/SOSUserKeygen.h>
40 #include <Security/SecureObjectSync/SOSAccountTrustClassic+Circle.h>
44 #include "secd_regressions.h"
45 #include "SOSTestDataSource.h"
47 #include "SOSRegressionUtilities.h"
48 #include <utilities/SecCFWrappers.h>
50 #include <securityd/SOSCloudCircleServer.h>
51 #include "SecdTestKeychainUtilities.h"
52 #import "SOSAccountTesting.h"
53 #import "SOSTransportTestTransports.h"
54 #include <Security/SecureObjectSync/SOSTransportMessageIDS.h>
55 #include <SOSCircle/CKBridge/SOSCloudKeychainConstants.h>
56 #include "SOSTestDevice.h"
60 static int kTestTestCount = 73;
64 CFErrorRef error = NULL;
66 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
67 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
68 CFStringRef cfaccount = CFSTR("test@test.org");
70 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("ak"));
71 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("ak"));
72 SOSAccountTrustClassic *aliceTrust = alice_account.trust;
73 SOSAccountTrustClassic *bobTrust = bob_account.trust;
75 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
77 // Bob wins writing at this point, feed the changes back to alice.
78 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
80 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
81 CFReleaseNull(cfpassword);
84 ok(NULL != alice_account, "Alice Created");
85 ok(NULL != bob_account, "Bob Created");
87 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
90 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
92 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
95 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
98 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
100 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
101 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
102 CFReleaseNull(error);
103 CFReleaseNull(applicants);
106 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
108 accounts_agree("bob&alice pair", bob_account, alice_account);
110 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
111 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
112 CFReleaseNull(peers);
114 //creating test devices
117 // Optionally prefix each peer with name to make them more unique.
118 CFArrayRef deviceIDs = CFArrayCreateForCFTypes(kCFAllocatorDefault,alice_account.peerID, bob_account.peerID, NULL);
119 CFSetRef views = SOSViewsCopyTestV2Default();
120 CFMutableArrayRef peerMetas = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
121 CFStringRef deviceID;
122 CFArrayForEachC(deviceIDs, deviceID) {
123 SOSPeerMetaRef peerMeta = SOSPeerMetaCreateWithComponents(deviceID, views, NULL);
124 CFArrayAppendValue(peerMetas, peerMeta);
125 CFReleaseNull(peerMeta);
128 CFReleaseNull(views);
129 CFArrayForEachC(deviceIDs, deviceID) {
130 SOSTestDeviceRef device = SOSTestDeviceCreateWithDbNamed(kCFAllocatorDefault, deviceID, deviceID);
131 SOSTestDeviceSetPeerIDs(device, peerMetas, version, NULL);
133 if(CFEqualSafe(deviceID, (__bridge CFTypeRef)(alice_account.peerID))){
134 alice_account.factory = device->dsf;
135 SOSTestDeviceAddGenericItem(device, CFSTR("Alice"), CFSTR("Alice-add"));
138 bob_account.factory = device->dsf;
139 SOSTestDeviceAddGenericItem(device, CFSTR("Bob"), CFSTR("Bob-add"));
141 CFReleaseNull(device);
143 CFReleaseNull(deviceIDs);
144 CFReleaseNull(peerMetas);
146 SOSUnregisterAllTransportMessages();
147 CFArrayRemoveAllValues(message_transports);
149 alice_account.ids_message_transport = (SOSMessageIDS*)[[SOSMessageIDSTest alloc] initWithAccount:alice_account andAccountName:CFSTR("Alice") andCircleName:SOSCircleGetName(aliceTrust.trustedCircle) err:&error];
152 bob_account.ids_message_transport = (SOSMessageIDS*)[[SOSMessageIDSTest alloc] initWithAccount:bob_account andAccountName:CFSTR("Bob") andCircleName:SOSCircleGetName(bobTrust.trustedCircle) err:&error];
153 ok(alice_account.ids_message_transport != NULL, "Alice Account, Created IDS Test Transport");
154 ok(bob_account.ids_message_transport != NULL, "Bob Account, Created IDS Test Transport");
156 bool result = [alice_account.trust modifyCircle:alice_account.circle_transport err:&error action:^(SOSCircleRef circle) {
157 CFErrorRef localError = NULL;
159 SOSFullPeerInfoUpdateTransportType(aliceTrust.fullPeerInfo, SOSTransportMessageTypeIDSV2, &localError);
160 SOSFullPeerInfoUpdateTransportPreference(aliceTrust.fullPeerInfo, kCFBooleanFalse, &localError);
161 SOSFullPeerInfoUpdateTransportFragmentationPreference(aliceTrust.fullPeerInfo, kCFBooleanTrue, &localError);
162 SOSFullPeerInfoUpdateTransportAckModelPreference(aliceTrust.fullPeerInfo, kCFBooleanTrue, &localError);
164 return SOSCircleHasPeer(circle, aliceTrust.peerInfo, NULL);
167 ok(result, "Alice account update circle with transport type");
169 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
171 result &= [bob_account.trust modifyCircle:bob_account.circle_transport err:&error action:^(SOSCircleRef circle) {
172 CFErrorRef localError = NULL;
174 SOSFullPeerInfoUpdateTransportType(bobTrust.fullPeerInfo, SOSTransportMessageTypeIDSV2, &localError);
175 SOSFullPeerInfoUpdateTransportPreference(bobTrust.fullPeerInfo, kCFBooleanFalse, &localError);
176 SOSFullPeerInfoUpdateTransportFragmentationPreference(bobTrust.fullPeerInfo, kCFBooleanTrue, &localError);
177 SOSFullPeerInfoUpdateTransportAckModelPreference(bobTrust.fullPeerInfo, kCFBooleanTrue, &localError);
179 return SOSCircleHasPeer(circle, bobTrust.peerInfo, NULL);
182 ok(result, "Bob account update circle with transport type");
183 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
185 CFStringRef alice_transportType =SOSPeerInfoCopyTransportType(alice_account.peerInfo);
186 CFStringRef bob_accountTransportType = SOSPeerInfoCopyTransportType(bob_account.peerInfo);
187 ok(CFEqualSafe(alice_transportType, CFSTR("IDS2.0")), "Alice transport type not IDS");
188 ok(CFEqualSafe(bob_accountTransportType, CFSTR("IDS2.0")), "Bob transport type not IDS");
190 CFReleaseNull(alice_transportType);
191 CFReleaseNull(bob_accountTransportType);
193 SOSTransportMessageIDSTestSetName((SOSMessageIDSTest*)alice_account.ids_message_transport, CFSTR("Alice Account"));
194 ok(SOSTransportMessageIDSTestGetName((SOSMessageIDSTest*)alice_account.ids_message_transport) != NULL, "retrieved getting account name");
195 ok(SOSAccountRetrieveDeviceIDFromKeychainSyncingOverIDSProxy(alice_account, &error) != false, "device ID from KeychainSyncingOverIDSProxy");
197 SOSTransportMessageIDSTestSetName((SOSMessageIDSTest*)bob_account.ids_message_transport, CFSTR("Bob Account"));
198 ok(SOSTransportMessageIDSTestGetName((SOSMessageIDSTest*)bob_account.ids_message_transport) != NULL, "retrieved getting account name");
199 ok(SOSAccountRetrieveDeviceIDFromKeychainSyncingOverIDSProxy(bob_account, &error) != false, "device ID from KeychainSyncingOverIDSProxy");
201 ok(SOSAccountSetMyDSID_wTxn(alice_account, CFSTR("Alice"),&error), "Setting IDS device ID");
202 CFStringRef alice_dsid = SOSAccountCopyDeviceID(alice_account, &error);
203 ok(CFEqualSafe(alice_dsid, CFSTR("Alice")), "Getting IDS device ID");
205 ok(SOSAccountSetMyDSID_wTxn(bob_account, CFSTR("Bob"),&error), "Setting IDS device ID");
206 CFStringRef bob_dsid = SOSAccountCopyDeviceID(bob_account, &error);
207 ok(CFEqualSafe(bob_dsid, CFSTR("Bob")), "Getting IDS device ID");
209 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
211 SOSTransportMessageIDSTestSetName((SOSMessageIDSTest*)alice_account.ids_message_transport, CFSTR("Alice Account"));
212 ok(SOSTransportMessageIDSTestGetName((SOSMessageIDSTest*)alice_account.ids_message_transport) != NULL, "retrieved getting account name");
213 ok(SOSAccountRetrieveDeviceIDFromKeychainSyncingOverIDSProxy(alice_account, &error) != false, "device ID from KeychainSyncingOverIDSProxy");
215 ok(SOSAccountSetMyDSID_wTxn(alice_account, CFSTR("DSID"),&error), "Setting IDS device ID");
216 CFStringRef dsid = SOSAccountCopyDeviceID(alice_account, &error);
217 ok(CFEqualSafe(dsid, CFSTR("DSID")), "Getting IDS device ID");
220 ok(SOSAccountStartPingTest(alice_account, CFSTR("hai there!"), &error), "Ping test");
221 ok(CFDictionaryGetCount(SOSTransportMessageIDSTestGetChanges((SOSMessageIDSTest*)alice_account.ids_message_transport)) != 0, "ping message made it to transport");
222 SOSTransportMessageIDSTestClearChanges((SOSMessageIDSTest*)alice_account.ids_message_transport);
224 ok(SOSAccountSendIDSTestMessage(alice_account, CFSTR("hai again!"), &error), "Send Test Message");
225 ok(CFDictionaryGetCount(SOSTransportMessageIDSTestGetChanges((SOSMessageIDSTest*)alice_account.ids_message_transport)) != 0, "ping message made it to transport");
227 CFStringRef dataKey = CFStringCreateWithCString(kCFAllocatorDefault, kMessageKeyIDSDataMessage, kCFStringEncodingASCII);
228 CFStringRef deviceIDKey = CFStringCreateWithCString(kCFAllocatorDefault, kMessageKeyDeviceID, kCFStringEncodingASCII);
229 CFStringRef sendersPeerIDKey = CFStringCreateWithCString(kCFAllocatorDefault, kMessageKeySendersPeerID, kCFStringEncodingASCII);
231 //test IDS message handling
232 CFMutableDictionaryRef messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
234 ok([alice_account.ids_message_transport SOSTransportMessageIDSHandleMessage:alice_account m:messageDict err:&error]
235 == kHandleIDSMessageDontHandle, "sending empty message dictionary");
237 CFDictionaryAddValue(messageDict, deviceIDKey, CFSTR("Alice Account"));
238 ok([alice_account.ids_message_transport SOSTransportMessageIDSHandleMessage:alice_account m:messageDict err:&error] == kHandleIDSMessageDontHandle, "sending device ID only");
240 CFReleaseNull(messageDict);
241 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
242 CFDictionaryAddValue(messageDict, sendersPeerIDKey, CFSTR("Alice Account"));
243 ok([alice_account.ids_message_transport SOSTransportMessageIDSHandleMessage:alice_account m:messageDict err:&error] == kHandleIDSMessageDontHandle, "sending peer ID only");
245 CFReleaseNull(messageDict);
246 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
247 CFDataRef data = CFDataCreate(kCFAllocatorDefault, 0, 0);
248 CFDictionaryAddValue(messageDict, dataKey, data);
249 ok( [alice_account.ids_message_transport SOSTransportMessageIDSHandleMessage:alice_account m:messageDict err:&error] == kHandleIDSMessageDontHandle, "sending data only");
251 CFReleaseNull(messageDict);
253 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
254 data = CFDataCreate(kCFAllocatorDefault, 0, 0);
255 CFDictionaryAddValue(messageDict, dataKey, data);
256 CFDictionaryAddValue(messageDict, sendersPeerIDKey, CFSTR("Alice Account"));
257 ok([(SOSMessageIDS*)alice_account.ids_message_transport SOSTransportMessageIDSHandleMessage:alice_account m:messageDict err:&error]== kHandleIDSMessageDontHandle, "sending data and peerid only");
259 CFReleaseNull(messageDict);
261 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
262 data = CFDataCreate(kCFAllocatorDefault, 0, 0);
263 CFDictionaryAddValue(messageDict, dataKey, data);
264 CFDictionaryAddValue(messageDict, deviceIDKey, CFSTR("Alice Account"));
265 ok([(SOSMessageIDS*)alice_account.ids_message_transport SOSTransportMessageIDSHandleMessage:alice_account m:messageDict err:&error] == kHandleIDSMessageDontHandle, "sending data and deviceid only");
267 CFReleaseNull(messageDict);
269 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
270 CFDictionaryAddValue(messageDict, deviceIDKey, CFSTR("Alice Account"));
271 CFDictionaryAddValue(messageDict, sendersPeerIDKey, CFSTR("Alice Account"));
272 ok([(SOSMessageIDS*)alice_account.ids_message_transport SOSTransportMessageIDSHandleMessage:alice_account m:messageDict err:&error] == kHandleIDSMessageDontHandle, "sending peerid and deviceid only");
274 CFReleaseNull(messageDict);
276 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
277 data = CFDataCreate(kCFAllocatorDefault, 0, 0);
278 CFDictionaryAddValue(messageDict, dataKey, data);
279 CFDictionaryAddValue(messageDict, deviceIDKey, CFSTR("Alice Account"));
280 CFDictionaryAddValue(messageDict, sendersPeerIDKey, SOSPeerInfoGetPeerID(bob_account.peerInfo));
281 ok([(SOSMessageIDS*)alice_account.ids_message_transport SOSTransportMessageIDSHandleMessage:alice_account m:messageDict err:&error]== kHandleIDSMessageDontHandle, "sending peerid and deviceid and data");
283 CFReleaseNull(messageDict);
286 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
287 data = CFDataCreate(kCFAllocatorDefault, 0, 0);
288 CFDictionaryAddValue(messageDict, dataKey, data);
289 CFStringRef BobDeviceID = SOSPeerInfoCopyDeviceID(bob_account.peerInfo);
290 CFDictionaryAddValue(messageDict, deviceIDKey, BobDeviceID);
291 CFReleaseNull(BobDeviceID);
292 CFDictionaryAddValue(messageDict, sendersPeerIDKey, CFSTR("Alice Account"));
293 ok([(SOSMessageIDS*)alice_account.ids_message_transport SOSTransportMessageIDSHandleMessage:alice_account m:messageDict err:&error]== kHandleIDSMessageDontHandle, "sending peerid and deviceid and data");
296 CFReleaseNull(dataKey);
297 CFReleaseNull(deviceIDKey);
298 CFReleaseNull(sendersPeerIDKey);
300 CFReleaseNull(alice_dsid);
301 CFReleaseNull(bob_dsid);
302 CFReleaseNull(changes);
306 int secd_76_idstransport(int argc, char *const *argv)
308 plan_tests(kTestTestCount);
310 secd_test_setup_temp_keychain(__FUNCTION__, NULL);