]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-80-views-basic.m
Security-59754.60.13.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-80-views-basic.m
1 //
2 // secd-80-views-basic.c
3 // sec
4 //
5 // Created by Richard Murphy on 1/26/15.
6 //
7 //
8
9 #include <stdio.h>
10 /*
11 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
12 *
13 * @APPLE_LICENSE_HEADER_START@
14 *
15 * This file contains Original Code and/or Modifications of Original Code
16 * as defined in and that are subject to the Apple Public Source License
17 * Version 2.0 (the 'License'). You may not use this file except in
18 * compliance with the License. Please obtain a copy of the License at
19 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * file.
21 *
22 * The Original Code and all software distributed under the License are
23 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
24 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
25 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
27 * Please see the License for the specific language governing rights and
28 * limitations under the License.
29 *
30 * @APPLE_LICENSE_HEADER_END@
31 */
32
33
34
35 #include <Security/SecBase.h>
36 #include <Security/SecItem.h>
37
38 #include "keychain/SecureObjectSync/SOSAccount.h"
39 #include <Security/SecureObjectSync/SOSCloudCircle.h>
40 #include "keychain/SecureObjectSync/SOSInternal.h"
41 #include "keychain/SecureObjectSync/SOSFullPeerInfo.h"
42 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
43 #include <Security/SecureObjectSync/SOSViews.h>
44
45 #include <stdlib.h>
46 #include <unistd.h>
47
48 #include "secd_regressions.h"
49 #include "SOSTestDataSource.h"
50
51 #include "SOSRegressionUtilities.h"
52 #include <utilities/SecCFWrappers.h>
53
54 #include "keychain/securityd/SOSCloudCircleServer.h"
55 #include "SecdTestKeychainUtilities.h"
56 #include "SOSAccountTesting.h"
57 #if SOS_ENABLED
58
59
60 static void testView(SOSAccount* account, SOSViewResultCode expected, CFStringRef view, SOSViewActionCode action, char *label) {
61 CFErrorRef error = NULL;
62 SOSViewResultCode vcode = 9999;
63 switch(action) {
64 case kSOSCCViewQuery:
65 vcode = [account.trust viewStatus:account name:view err:&error];
66 break;
67 case kSOSCCViewEnable:
68 case kSOSCCViewDisable: // fallthrough
69 vcode = [account.trust updateView:account name:view code:action err:&error];
70 break;
71 default:
72 break;
73 }
74 is(vcode, expected, "%s (%@)", label, error);
75 CFReleaseNull(error);
76 }
77
78 static void testViewLists(void) {
79 CFSetRef allViews = SOSViewCopyViewSet(kViewSetAll);
80 CFSetRef defaultViews = SOSViewCopyViewSet(kViewSetDefault);
81 CFSetRef initialViews = SOSViewCopyViewSet(kViewSetInitial);
82 CFSetRef alwaysOnViews = SOSViewCopyViewSet(kViewSetAlwaysOn);
83 CFSetRef backupRequiredViews = SOSViewCopyViewSet(kViewSetRequiredForBackup);
84 CFSetRef V0Views = SOSViewCopyViewSet(kViewSetV0);
85
86 is(CFSetGetCount(allViews), 24, "make sure count of allViews is correct");
87 is(CFSetGetCount(defaultViews), 20, "make sure count of defaultViews is correct");
88 is(CFSetGetCount(initialViews), 0, "make sure count of initialViews is correct");
89 is(CFSetGetCount(alwaysOnViews), 20, "make sure count of alwaysOnViews is correct");
90 is(CFSetGetCount(backupRequiredViews), 3, "make sure count of backupRequiredViews is correct");
91 is(CFSetGetCount(V0Views), 6, "make sure count of V0Views is correct");
92
93 CFReleaseNull(allViews);
94 CFReleaseNull(defaultViews);
95 CFReleaseNull(initialViews);
96 CFReleaseNull(alwaysOnViews);
97 CFReleaseNull(backupRequiredViews);
98 CFReleaseNull(V0Views);
99 }
100
101 static int kTestTestCount = 38;
102 static void tests(void)
103 {
104 CFErrorRef error = NULL;
105 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
106 CFStringRef cfaccount = CFSTR("test@test.org");
107 CFSetRef nullSet = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
108
109 SOSDataSourceFactoryRef test_factory = SOSTestDataSourceFactoryCreate();
110 SOSDataSourceRef test_source = SOSTestDataSourceCreate();
111 SOSTestDataSourceFactorySetDataSource(test_factory, CFSTR("TestType"), test_source);
112
113 SOSAccount* account = CreateAccountForLocalChanges(CFSTR("Test Device"),CFSTR("TestType") );
114
115 ok(SOSAccountAssertUserCredentialsAndUpdate(account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
116 CFReleaseNull(error);
117 CFReleaseNull(cfpassword);
118
119 ok(SOSAccountJoinCircles_wTxn(account, &error), "Join circle: %@", error);
120
121 ok(NULL != account, "Created");
122
123 ok(SOSAccountCheckHasBeenInSync_wTxn(account), "In sync already");
124
125 testView(account, kSOSCCViewNotMember, kSOSViewKeychainV0, kSOSCCViewQuery, "Expected view capability for kSOSViewKeychain");
126 // Default views no longer includes kSOSViewAppleTV
127 testView(account, kSOSCCViewMember, kSOSViewAppleTV, kSOSCCViewQuery, "Expected view capability for kSOSViewAppleTV");
128 testView(account, kSOSCCViewMember, kSOSViewPCSPhotos, kSOSCCViewQuery, "Expected no view capability for kSOSViewPCSPhotos");
129 testView(account, kSOSCCViewMember, kSOSViewPCSiCloudDrive, kSOSCCViewQuery, "Expected no view capability for kSOSViewPCSiCloudDrive");
130 testView(account, kSOSCCNoSuchView, CFSTR("FOO"), kSOSCCViewQuery, "Expected no such view for FOO");
131
132 testView(account, kSOSCCViewMember, kSOSViewPCSiCloudDrive, kSOSCCViewEnable, "Expected to enable kSOSViewPCSiCloudDrive");
133 testView(account, kSOSCCViewMember, kSOSViewPCSiCloudDrive, kSOSCCViewQuery, "Expected view capability for kSOSViewPCSiCloudDrive");
134 testView(account, kSOSCCViewMember, kSOSViewPCSiCloudDrive, kSOSCCViewDisable, "Expected cannot disable kSOSViewPCSiCloudDrive");
135 testView(account, kSOSCCViewMember, kSOSViewPCSiCloudDrive, kSOSCCViewQuery, "Expected view capability for kSOSViewPCSiCloudDrive");
136
137 testView(account, kSOSCCViewMember, kSOSViewPCSiCloudDrive, kSOSCCViewEnable, "Expected to enable kSOSViewPCSiCloudDrive");
138 testView(account, kSOSCCViewMember, kSOSViewPCSiCloudDrive, kSOSCCViewQuery, "Expected view capability for kSOSViewPCSiCloudDrive");
139 testView(account, kSOSCCViewNotMember, kSOSViewKeychainV0, kSOSCCViewEnable, "Expected to enable kSOSViewKeychainV0");
140 testView(account, kSOSCCViewMember, kSOSViewPCSiCloudDrive, kSOSCCViewQuery, "Expected view capability for kSOSViewPCSiCloudDrive");
141 testView(account, kSOSCCViewMember, kSOSViewAppleTV, kSOSCCViewEnable, "Expected to enable kSOSViewAppleTV");
142
143 testView(account, kSOSCCViewMember, kSOSViewPCSiCloudDrive, kSOSCCViewQuery, "Expected view capability for kSOSViewPCSiCloudDrive");
144 testView(account, kSOSCCViewNotMember, kSOSViewKeychainV0, kSOSCCViewQuery, "Expected view capability for kSOSViewKeychainV0");
145 testView(account, kSOSCCViewMember, kSOSViewAppleTV, kSOSCCViewQuery, "Expected view capability for kSOSViewAppleTV");
146
147 ok([account.trust updateViewSets:account enabled:SOSViewsGetV0ViewSet() disabled:nullSet], "Expect not accepting kSOSKeychainV0");
148 testView(account, kSOSCCViewNotMember, kSOSViewKeychainV0, kSOSCCViewQuery, "Expected no addition of kSOSKeychainV0");
149
150 ok([account.trust updateViewSets:account enabled:SOSViewsGetV0ViewSet() disabled:nullSet], "Expect not accepting kSOSKeychainV0");
151 testView(account, kSOSCCViewNotMember, kSOSViewKeychainV0, kSOSCCViewQuery, "Expected no addition of kSOSKeychainV0");
152
153 SOSPeerInfoRef pi = account.peerInfo;
154 ok(pi, "should have the peerInfo");
155 SOSViewResultCode vr = SOSViewsEnable(pi, kSOSViewKeychainV0, NULL);
156
157 ok(vr == kSOSCCViewMember, "Set Virtual View manually");
158
159 ok(![account.trust updateViewSets:account enabled:nullSet disabled:SOSViewsGetV0ViewSet()], "Expect not removing kSOSKeychainV0");
160 testView(account, kSOSCCViewMember, kSOSViewKeychainV0, kSOSCCViewQuery, "Expected kSOSKeychainV0 is still there");
161
162 ok(![account.trust updateViewSets:account enabled:nullSet disabled:SOSViewsGetV0ViewSet()], "Expect not removing kSOSKeychainV0");
163 testView(account, kSOSCCViewMember, kSOSViewKeychainV0, kSOSCCViewQuery, "Expected kSOSKeychainV0 is still there");
164
165 SOSDataSourceRelease(test_source, NULL);
166 SOSDataSourceFactoryRelease(test_factory);
167
168 SOSTestCleanup();
169 }
170 #endif
171
172 int secd_80_views_basic(int argc, char *const *argv)
173 {
174 #if SOS_ENABLED
175 plan_tests(kTestTestCount);
176 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
177 secd_test_clear_testviews();
178 testViewLists();
179 tests();
180 #else
181 plan_tests(0);
182 #endif
183 return 0;
184 }