]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-100-initialsync.m
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-100-initialsync.m
1 //
2 // secd-100-initialsync.c
3 // sec
4 //
5
6
7 /*
8 * Copyright (c) 2014 Apple Inc. All Rights Reserved.
9 *
10 * @APPLE_LICENSE_HEADER_START@
11 *
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
17 * file.
18 *
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.
26 *
27 * @APPLE_LICENSE_HEADER_END@
28 */
29
30
31 #include <Security/SecBase.h>
32 #include <Security/SecItem.h>
33
34 #include <CoreFoundation/CFDictionary.h>
35
36 #include "keychain/SecureObjectSync/SOSAccount.h"
37 #include <Security/SecureObjectSync/SOSCloudCircle.h>
38 #include "keychain/SecureObjectSync/SOSInternal.h"
39 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
40 #include "keychain/SecureObjectSync/SOSTransport.h"
41
42 #include <stdlib.h>
43 #include <unistd.h>
44
45 #include "secd_regressions.h"
46 #include <utilities/SecCFWrappers.h>
47 #include <Security/SecKeyPriv.h>
48
49 #include "keychain/securityd/SOSCloudCircleServer.h"
50
51 #include "SOSAccountTesting.h"
52
53 #include "SecdTestKeychainUtilities.h"
54 #if SOS_ENABLED
55
56 static void tests(void)
57 {
58
59 CFErrorRef error = NULL;
60 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
61 CFStringRef cfaccount = CFSTR("test@test.org");
62 CFSetRef initialSyncViews = SOSViewCopyViewSet(kViewSetInitial);
63 CFMutableSetRef alwaysOnViews = SOSViewCopyViewSet(kViewSetAlwaysOn);
64 CFSetRef defaultViews = SOSViewCopyViewSet(kViewSetDefault);
65 int initialSyncViewCount = (int) CFSetGetCount(initialSyncViews);
66 CFReleaseNull(initialSyncViews);
67 CFSetRef backupSyncViews = SOSViewCopyViewSet(kViewSetRequiredForBackup);
68 int backupSyncViewCount = (int) CFSetGetCount(backupSyncViews);
69 CFReleaseNull(backupSyncViews);
70 int expectedStartupViewCount;
71
72 if(initialSyncViewCount == 0) {
73 CFSetUnion(alwaysOnViews, defaultViews);
74 expectedStartupViewCount = (int) CFSetGetCount(alwaysOnViews);
75 } else {
76 CFMutableSetRef isViews = CFSetCreateMutableCopy(kCFAllocatorDefault, 0, initialSyncViews);
77 CFSetUnion(isViews, backupSyncViews);
78 expectedStartupViewCount = (int) CFSetGetCount(isViews);
79 CFReleaseNull(isViews);
80 }
81 CFReleaseNull(alwaysOnViews);
82 CFReleaseNull(defaultViews);
83
84
85
86 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
87
88 SOSDataSourceFactoryRef test_factory = SOSTestDataSourceFactoryCreate();
89 SOSDataSourceRef test_source = SOSTestDataSourceCreate();
90 SOSTestDataSourceFactorySetDataSource(test_factory, CFSTR("TestType"), test_source);
91
92 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("AliceAccount"),CFSTR("TestType") );
93 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("BobAccount"),CFSTR("TestType") );
94
95 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
96
97 ok(SOSAccountJoinCircles_wTxn(alice_account, &error), "Join circle: %@", error);
98 ok(SOSAccountCheckHasBeenInSync_wTxn(alice_account), "Alice account initial sync done");
99
100 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
101
102 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
103 CFReleaseNull(error);
104 CFReleaseNull(cfpassword);
105
106 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
107
108 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
109 CFReleaseNull(error);
110
111 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
112
113 {
114 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
115
116 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
117 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
118 CFReleaseNull(error);
119 CFReleaseNull(applicants);
120 }
121
122
123 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
124
125 accounts_agree("bob&alice pair", bob_account, alice_account);
126
127 if(initialSyncViewCount > 0) {
128 ok(!SOSAccountCheckHasBeenInSync_wTxn(bob_account), "Bob should not be initially synced");
129 }
130 CFSetRef bob_viewSet = SOSPeerInfoCopyEnabledViews(bob_account.peerInfo);
131 is(CFSetGetCount(bob_viewSet), expectedStartupViewCount, "bob's initial view set should be just the initial sync and backup views");
132 CFReleaseNull(bob_viewSet);
133
134 if(initialSyncViewCount > 0) {
135 ok(!SOSAccountCheckHasBeenInSync_wTxn(bob_account), "Bob should not be initially synced");
136 }
137
138 SOSAccountPeerGotInSync_wTxn(bob_account, alice_account.peerInfo);
139
140 if(initialSyncViewCount > 0) {
141 bob_viewSet = SOSPeerInfoCopyEnabledViews(bob_account.peerInfo);
142 is(CFSetGetCount(bob_viewSet), backupSyncViewCount, "bob's initial view set should be just the back up");
143 CFReleaseNull(bob_viewSet);
144 } else {
145 ok(true, "don't mess with the total test count");
146 }
147 bob_account = nil;
148 alice_account = nil;
149
150 SOSDataSourceFactoryRelease(test_factory);
151
152 SOSTestCleanup();
153 }
154 #endif
155
156 int secd_100_initialsync(int argc, char *const *argv)
157 {
158 #if SOS_ENABLED
159 plan_tests(33);
160 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
161 tests();
162 #else
163 plan_tests(0);
164 #endif
165 return 0;
166 }