2 * Copyright (c) 2013-2016 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
33 #include <Security/SecBase.h>
34 #include <Security/SecItem.h>
36 #include <CoreFoundation/CFDictionary.h>
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"
48 #include "secd_regressions.h"
49 #include "SOSTestDataSource.h"
50 #include "SOSTestDevice.h"
52 #include "SOSRegressionUtilities.h"
53 #include <utilities/SecCFWrappers.h>
54 #include <Security/SecKeyPriv.h>
56 #include "keychain/securityd/SOSCloudCircleServer.h"
58 #include "SOSAccountTesting.h"
60 #include "SecdTestKeychainUtilities.h"
63 static void tests(void)
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");
71 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
72 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
73 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
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);
82 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential trying (%@)", error);
83 CFReleaseNull(cfpassword);
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");
91 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
94 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
96 ok(SOSAccountHasCompletedInitialSync(alice_account), "Alice thinks she's completed initial sync");
98 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
101 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
104 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
112 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
114 accounts_agree("bob&alice pair", bob_account, alice_account);
116 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
117 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
118 CFReleaseNull(peers);
120 //creating test devices
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);
134 CFReleaseNull(views);
135 CFArrayForEachC(deviceIDs, deviceID) {
136 SOSTestDeviceRef device = SOSTestDeviceCreateWithDbNamed(kCFAllocatorDefault, deviceID, deviceID);
137 SOSTestDeviceSetPeerIDs(device, peerMetas, version, NULL);
139 if([alice_account.peerID isEqual: (__bridge id) deviceID]){
140 alice_account.factory = device->dsf;
141 SOSTestDeviceAddGenericItem(device, CFSTR("Alice"), CFSTR("Alice-add"));
144 bob_account.factory = device->dsf;
145 SOSTestDeviceAddGenericItem(device, CFSTR("Bob"), CFSTR("Bob-add"));
148 CFReleaseNull(device);
150 CFReleaseNull(deviceIDs);
151 CFReleaseNull(peerMetas);
153 SOSUnregisterAllTransportMessages();
154 CFArrayRemoveAllValues(message_transports);
157 ok(SOSAccountEnsurePeerRegistration(alice_account, NULL), "ensure peer registration - alice");
158 ok(SOSAccountEnsurePeerRegistration(bob_account, NULL), "ensure peer registration - bob");
166 CFReleaseNull(changes);
170 int secd_201_coders(int argc, char *const *argv)
174 secd_test_setup_temp_keychain(__FUNCTION__, NULL);