]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-100-initialsync.m
Security-59306.120.7.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
55 static void tests(void)
56 {
57
58 CFErrorRef error = NULL;
59 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
60 CFStringRef cfaccount = CFSTR("test@test.org");
61 CFSetRef initialSyncViews = SOSViewCopyViewSet(kViewSetInitial);
62 CFMutableSetRef alwaysOnViews = SOSViewCopyViewSet(kViewSetAlwaysOn);
63 CFSetRef defaultViews = SOSViewCopyViewSet(kViewSetDefault);
64 int initialSyncViewCount = (int) CFSetGetCount(initialSyncViews);
65 CFReleaseNull(initialSyncViews);
66 CFSetRef backupSyncViews = SOSViewCopyViewSet(kViewSetRequiredForBackup);
67 int backupSyncViewCount = (int) CFSetGetCount(backupSyncViews);
68 CFReleaseNull(backupSyncViews);
69 int expectedStartupViewCount;
70
71 if(initialSyncViewCount == 0) {
72 CFSetUnion(alwaysOnViews, defaultViews);
73 expectedStartupViewCount = (int) CFSetGetCount(alwaysOnViews);
74 } else {
75 CFMutableSetRef isViews = CFSetCreateMutableCopy(kCFAllocatorDefault, 0, initialSyncViews);
76 CFSetUnion(isViews, backupSyncViews);
77 expectedStartupViewCount = (int) CFSetGetCount(isViews);
78 CFReleaseNull(isViews);
79 }
80 CFReleaseNull(alwaysOnViews);
81 CFReleaseNull(defaultViews);
82
83
84
85 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
86
87 SOSDataSourceFactoryRef test_factory = SOSTestDataSourceFactoryCreate();
88 SOSDataSourceRef test_source = SOSTestDataSourceCreate();
89 SOSTestDataSourceFactorySetDataSource(test_factory, CFSTR("TestType"), test_source);
90
91 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("AliceAccount"),CFSTR("TestType") );
92 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("BobAccount"),CFSTR("TestType") );
93
94 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
95
96 ok(SOSAccountJoinCircles_wTxn(alice_account, &error), "Join circle: %@", error);
97 ok(SOSAccountCheckHasBeenInSync_wTxn(alice_account), "Alice account initial sync done");
98
99 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
100
101 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
102 CFReleaseNull(error);
103 CFReleaseNull(cfpassword);
104
105 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
106
107 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
108 CFReleaseNull(error);
109
110 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
111
112 {
113 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
114
115 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
116 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
117 CFReleaseNull(error);
118 CFReleaseNull(applicants);
119 }
120
121
122 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
123
124 accounts_agree("bob&alice pair", bob_account, alice_account);
125
126 if(initialSyncViewCount > 0) {
127 ok(!SOSAccountCheckHasBeenInSync_wTxn(bob_account), "Bob should not be initially synced");
128 }
129 CFSetRef bob_viewSet = SOSPeerInfoCopyEnabledViews(bob_account.peerInfo);
130 is(CFSetGetCount(bob_viewSet), expectedStartupViewCount, "bob's initial view set should be just the initial sync and backup views");
131 CFReleaseNull(bob_viewSet);
132
133 if(initialSyncViewCount > 0) {
134 ok(!SOSAccountCheckHasBeenInSync_wTxn(bob_account), "Bob should not be initially synced");
135 }
136
137 SOSAccountPeerGotInSync_wTxn(bob_account, alice_account.peerInfo);
138
139 if(initialSyncViewCount > 0) {
140 bob_viewSet = SOSPeerInfoCopyEnabledViews(bob_account.peerInfo);
141 is(CFSetGetCount(bob_viewSet), backupSyncViewCount, "bob's initial view set should be just the back up");
142 CFReleaseNull(bob_viewSet);
143 } else {
144 ok(true, "don't mess with the total test count");
145 }
146 bob_account = nil;
147 alice_account = nil;
148
149 SOSDataSourceFactoryRelease(test_factory);
150
151 SOSTestCleanup();
152 }
153
154 int secd_100_initialsync(int argc, char *const *argv)
155 {
156 plan_tests(33);
157
158 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
159
160 tests();
161
162 return 0;
163 }