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"
62 static void tests(void)
65 __block CFErrorRef error = NULL;
66 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
67 CFDataRef cfwrong_password = CFDataCreate(NULL, (uint8_t *) "NotFooFooFoo", 10);
68 CFStringRef cfaccount = CFSTR("test@test.org");
70 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
71 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
72 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
74 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
76 // Bob wins writing at this point, feed the changes back to alice.
77 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
79 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
81 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential trying (%@)", error);
82 CFReleaseNull(cfpassword);
85 ok(!SOSAccountTryUserCredentials(alice_account, cfaccount, cfwrong_password, &error), "Credential failing (%@)", error);
86 CFReleaseNull(cfwrong_password);
87 is(error ? CFErrorGetCode(error) : 0, kSOSErrorWrongPassword, "Expected SOSErrorWrongPassword");
90 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
93 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
95 ok(SOSAccountHasCompletedInitialSync(alice_account), "Alice thinks she's completed initial sync");
97 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
100 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
103 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
105 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
106 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
107 CFReleaseNull(error);
108 CFReleaseNull(applicants);
111 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
113 accounts_agree("bob&alice pair", bob_account, alice_account);
115 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
116 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
117 CFReleaseNull(peers);
119 //creating test devices
122 // Optionally prefix each peer with name to make them more unique.
123 CFArrayRef deviceIDs = CFArrayCreateForCFTypes(kCFAllocatorDefault,alice_account.peerID, bob_account.peerID, NULL);
124 CFSetRef views = SOSViewsCopyTestV2Default();
125 CFMutableArrayRef peerMetas = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
126 CFStringRef deviceID;
127 CFArrayForEachC(deviceIDs, deviceID) {
128 SOSPeerMetaRef peerMeta = SOSPeerMetaCreateWithComponents(deviceID, views, NULL);
129 CFArrayAppendValue(peerMetas, peerMeta);
130 CFReleaseNull(peerMeta);
133 CFReleaseNull(views);
134 CFArrayForEachC(deviceIDs, deviceID) {
135 SOSTestDeviceRef device = SOSTestDeviceCreateWithDbNamed(kCFAllocatorDefault, deviceID, deviceID);
136 SOSTestDeviceSetPeerIDs(device, peerMetas, version, NULL);
138 if([alice_account.peerID isEqual: (__bridge id) deviceID]){
139 alice_account.factory = device->dsf;
140 SOSTestDeviceAddGenericItem(device, CFSTR("Alice"), CFSTR("Alice-add"));
143 bob_account.factory = device->dsf;
144 SOSTestDeviceAddGenericItem(device, CFSTR("Bob"), CFSTR("Bob-add"));
147 CFReleaseNull(device);
149 CFReleaseNull(deviceIDs);
150 CFReleaseNull(peerMetas);
152 SOSUnregisterAllTransportMessages();
153 CFArrayRemoveAllValues(message_transports);
156 ok(SOSAccountEnsurePeerRegistration(alice_account, NULL), "ensure peer registration - alice");
157 ok(SOSAccountEnsurePeerRegistration(bob_account, NULL), "ensure peer registration - bob");
165 CFReleaseNull(changes);
168 int secd_201_coders(int argc, char *const *argv)
172 secd_test_setup_temp_keychain(__FUNCTION__, NULL);