2 // secd_77_ids_messaging.c
7 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
9 * @APPLE_LICENSE_HEADER_START@
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
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.
26 * @APPLE_LICENSE_HEADER_END@
31 #include <Security/SecBase.h>
32 #include <Security/SecItem.h>
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>
42 #include "secd_regressions.h"
43 #include "SOSTestDataSource.h"
45 #include "SOSRegressionUtilities.h"
46 #include <utilities/SecCFWrappers.h>
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>
57 static bool SOSAccountIsThisPeerIDMe(SOSAccount* account, CFStringRef peerID) {
58 SOSAccountTrustClassic* trust = account.trust;
59 SOSPeerInfoRef mypi = trust.peerInfo;
60 CFStringRef myPeerID = SOSPeerInfoGetPeerID(mypi);
62 return myPeerID && CFEqualSafe(myPeerID, peerID);
65 __unused static void ids_test_sync(SOSAccount* alice_account, SOSAccount* bob_account){
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;
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!");
81 CFMutableSetRef ids = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
82 CFSetAddValue(ids, SOSPeerInfoGetPeerID(peer));
84 SyncingCompletedOverIDS = [alice_account.ids_message_transport SOSTransportMessageSyncWithPeers:alice_account.ids_message_transport p:ids err:&localError];
90 ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL);
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!");
98 CFMutableSetRef ids = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
99 CFSetAddValue(ids, SOSPeerInfoGetPeerID(peer));
101 SyncingCompletedOverIDS = [(SOSMessageIDSTest*)bob_account.ids_message_transport SOSTransportMessageSyncWithPeers:(SOSMessageIDSTest*)bob_account.ids_message_transport p:ids err:&localError];
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){
113 ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL);
115 }while(done == false);
116 CFReleaseNull(changes);
121 CFErrorRef error = NULL;
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");
128 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), dsName);
129 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), dsName);
131 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
133 // Bob wins writing at this point, feed the changes back to alice.
134 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
136 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
137 CFReleaseNull(cfpassword);
138 CFReleaseNull(error);
140 ok(NULL != alice_account, "Alice Created");
141 ok(NULL != bob_account, "Bob Created");
143 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
144 CFReleaseNull(error);
146 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
148 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
149 CFReleaseNull(error);
151 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
154 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
162 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
164 accounts_agree("bob&alice pair", bob_account, alice_account);
166 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
167 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
168 CFReleaseNull(peers);
170 //creating test devices
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);
184 CFReleaseNull(views);
185 CFArrayForEachC(deviceIDs, deviceID) {
186 SOSTestDeviceRef device = SOSTestDeviceCreateWithDbNamed(kCFAllocatorDefault, deviceID, deviceID);
187 SOSTestDeviceSetPeerIDs(device, peerMetas, version, NULL);
189 if([alice_account.peerID isEqual: (__bridge id)(deviceID)]){
190 alice_account.factory = device->dsf;
191 SOSTestDeviceAddGenericItem(device, CFSTR("Alice"), CFSTR("Alice-add"));
194 bob_account.factory = device->dsf;
195 SOSTestDeviceAddGenericItem(device, CFSTR("Bob"), CFSTR("Bob-add"));
198 CFReleaseNull(device);
200 CFReleaseNull(deviceIDs);
201 CFReleaseNull(peerMetas);
203 SOSUnregisterAllTransportMessages();
204 CFArrayRemoveAllValues(message_transports);
206 SOSAccountTrustClassic* aliceTrust = alice_account.trust;
207 SOSAccountTrustClassic* bobTrust = bob_account.trust;
209 alice_account.ids_message_transport = (SOSMessageIDS*)[[SOSMessageIDSTest alloc] initWithAccount:alice_account andAccountName:CFSTR("Alice") andCircleName:SOSCircleGetName(aliceTrust.trustedCircle) err:&error ];
211 bob_account.ids_message_transport = (SOSMessageIDS*)[[SOSMessageIDSTest alloc] initWithAccount:bob_account andAccountName:CFSTR("Bob") andCircleName:SOSCircleGetName(bobTrust.trustedCircle) err:&error];
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");
216 bool result = [alice_account.trust modifyCircle:alice_account.circle_transport err:&error action:^bool(SOSCircleRef circle) {
217 CFErrorRef localError = NULL;
219 SOSFullPeerInfoUpdateTransportType(aliceTrust.fullPeerInfo, SOSTransportMessageTypeIDSV2, &localError);
220 SOSFullPeerInfoUpdateTransportPreference(aliceTrust.fullPeerInfo, kCFBooleanFalse, &localError);
221 SOSFullPeerInfoUpdateTransportFragmentationPreference(aliceTrust.fullPeerInfo, kCFBooleanTrue, &localError);
222 SOSFullPeerInfoUpdateTransportAckModelPreference(aliceTrust.fullPeerInfo, kCFBooleanTrue, &localError);
224 return SOSCircleHasPeer(circle, aliceTrust.peerInfo, NULL);
227 ok(result, "Alice account update circle with transport type");
229 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
231 result = [bob_account.trust modifyCircle:bob_account.circle_transport err:&error action:^bool(SOSCircleRef circle) {
232 CFErrorRef localError = NULL;
234 SOSFullPeerInfoUpdateTransportType(bobTrust.fullPeerInfo, SOSTransportMessageTypeIDSV2, &localError);
235 SOSFullPeerInfoUpdateTransportPreference(bobTrust.fullPeerInfo, kCFBooleanFalse, &localError);
236 SOSFullPeerInfoUpdateTransportFragmentationPreference(bobTrust.fullPeerInfo, kCFBooleanTrue, &localError);
237 SOSFullPeerInfoUpdateTransportAckModelPreference(bobTrust.fullPeerInfo, kCFBooleanTrue, &localError);
239 return SOSCircleHasPeer(circle, bobTrust.peerInfo, NULL);
242 ok(result, "Bob account update circle with transport type");
243 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
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");
250 CFReleaseNull(alice_transportType);
251 CFReleaseNull(bob_accountTransportType);
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");
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");
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");
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");
270 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
273 ok(SOSAccountEnsurePeerRegistration(alice_account, NULL), "ensure peer registration - alice");
275 ok(SOSAccountEnsurePeerRegistration(bob_account, NULL), "ensure peer registration - bob");
278 //ids_test_sync(alice_account, bob_account);
280 CFReleaseNull(bob_dsid);
281 CFReleaseNull(alice_dsid);
282 CFReleaseNull(changes);
287 int secd_77_ids_messaging(int argc, char *const *argv)
291 secd_test_setup_temp_keychain(__FUNCTION__, NULL);