Merge branch 'tizen' into yaca
[platform/core/test/security-tests.git] / src / ckm-integration / group02.cpp
1 /*
2  *  Copyright (c) 2015 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /*
17  * @file       group02.cpp
18  * @author     Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version    1.0
20  */
21
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <ckm-common.h>
25
26 #include <dpl/test/test_runner.h>
27 #include <dpl/test/test_runner_child.h>
28
29 #include <ckm/ckm-manager.h>
30 #include <ckm/ckm-control.h>
31 #include <ckm/ckm-password.h>
32 #include <ckm/ckm-type.h>
33
34 #include <ckm-policy.h>
35
36 typedef ProcessSettings::Executor<
37     CKMPolicy,
38     ProcessSettings::CreateUser,
39     ProcessSettings::UnlockCkm,
40     ProcessSettings::InstallApp,
41     ProcessSettings::ChangeSmack,
42     ProcessSettings::ChangeUid> PS;
43
44 typedef ProcessSettings::Executor<
45     CKMPolicy,
46     ProcessSettings::CreateUser,
47     ProcessSettings::UnlockCkm,
48     ProcessSettings::InstallApp,
49     ProcessSettings::ChangeSmack> PSNoUid;
50
51 typedef ProcessSettings::Executor<
52     CKMPolicy,
53     ProcessSettings::ChangeUid> PSUid;
54
55 RUNNER_TEST_GROUP_INIT(GROUP_02_IntegrationStorageApiWithCynara);
56
57 RUNNER_CHILD_TEST(G02T01_StorageNegative) {
58     RUNNER_IGNORED_MSG("Cynara integration with CKM Storage API was canceled.");
59     // We are ordinary user without any privileges.
60     // Cynara should deny all accesses.
61     PS ps("PkgIdG02T01", "UserG02T01", PrivNone);
62     ps.Apply();
63
64     int temp;
65     auto manager = CKM::Manager::create();
66     std::string data = "Custom data";
67     CKM::RawBuffer rawBuffer(data.begin(), data.end());
68     CKM::RawBuffer output;
69     const char *alias = "dataG02T01";
70
71     RUNNER_ASSERT_MSG(
72         CKM_API_ERROR_ACCESS_DENIED == (temp = manager->saveData(alias, rawBuffer, CKM::Policy())),
73         "Error=" << CKM::APICodeToString(temp));
74
75     RUNNER_ASSERT_MSG(
76         CKM_API_ERROR_ACCESS_DENIED == (temp = manager->getData(alias, CKM::Password(), output)),
77         "Error=" << CKM::APICodeToString(temp));
78 }
79
80 RUNNER_CHILD_TEST(G02T02_StoragePositive) {
81     RUNNER_IGNORED_MSG("Cynara integration with CKM Storage API was canceled.");
82     // We are root. We will be allowed.
83     int temp;
84     auto manager = CKM::Manager::create();
85     std::string data = "Custom data";
86     CKM::RawBuffer rawBuffer(data.begin(), data.end());
87     CKM::RawBuffer output;
88     const char *alias = "/System dataG02T02";
89
90     // This funciton may return error.
91     manager->removeAlias(alias);
92
93     RUNNER_ASSERT_MSG(
94         CKM_API_SUCCESS == (temp = manager->saveData(alias, rawBuffer, CKM::Policy())),
95         "Error=" << CKM::APICodeToString(temp));
96
97     RUNNER_ASSERT_MSG(
98         CKM_API_SUCCESS == (temp = manager->getData(alias, CKM::Password(), output)),
99         "Error=" << CKM::APICodeToString(temp));
100
101     RUNNER_ASSERT_MSG(rawBuffer == output, "Data mismatch.");
102 }
103
104 RUNNER_CHILD_TEST(G02T03_StoragePositive) {
105     RUNNER_IGNORED_MSG("Cynara integration with CKM Storage API was canceled.");
106     // We are oridinary user with proper privileges.
107     PS ps("PkgIdG02T03", "UserG02T03", PrivCKMStore);
108     ps.Apply();
109
110     int temp;
111     auto manager = CKM::Manager::create();
112     std::string data = "Custom data";
113     CKM::RawBuffer rawBuffer(data.begin(), data.end());
114     CKM::RawBuffer output;
115     const char *dataAlias = "dataG02T03";
116
117     RUNNER_ASSERT_MSG(
118         CKM_API_SUCCESS == (temp = manager->saveData(dataAlias, rawBuffer, CKM::Policy())),
119         "Error=" << CKM::APICodeToString(temp));
120
121     RUNNER_ASSERT_MSG(
122         CKM_API_SUCCESS == (temp = manager->getData(dataAlias, CKM::Password(), output)),
123         "Error=" << CKM::APICodeToString(temp));
124
125     RUNNER_ASSERT_MSG(rawBuffer == output, "Data mismatch.");
126 }
127
128 RUNNER_CHILD_TEST(G02T04_StorageNegative) {
129     RUNNER_IGNORED_MSG("Cynara integration with CKM Storage API was canceled.");
130     // There is some user with privileges but we are
131     // are ordinary user without any.
132     // Cynara should deny all accesses.
133     PSNoUid ps("PkgIdG02T04", "UserG02T04", PrivCKMBoth);
134     ps.Apply();
135
136     PSUid ps2("", "", PrivNone);
137     ps2.SetUid(ps.GetUid()+1);
138     ps2.Apply();
139
140     int temp;
141     auto manager = CKM::Manager::create();
142     std::string data = "Custom data";
143     CKM::RawBuffer rawBuffer(data.begin(), data.end());
144     CKM::RawBuffer output;
145     const char *alias = "dataG02T04";
146
147     RUNNER_ASSERT_MSG(
148         CKM_API_ERROR_ACCESS_DENIED == (temp = manager->saveData(alias, rawBuffer, CKM::Policy())),
149         "Error=" << CKM::APICodeToString(temp));
150
151     RUNNER_ASSERT_MSG(
152         CKM_API_ERROR_ACCESS_DENIED == (temp = manager->getData(alias, CKM::Password(), output)),
153         "Error=" << CKM::APICodeToString(temp));
154 }
155
156 RUNNER_CHILD_TEST(G02T05_StorageNegative) {
157     RUNNER_IGNORED_MSG("Cynara integration with CKM Storage API was canceled.");
158     // We have wrong privilege.
159     // Cynara should deny all accesses to storage.
160     PSNoUid ps("PkgIdG02T05", "UserG02T05", PrivCKMControl);
161     ps.Apply();
162
163     int temp;
164     auto manager = CKM::Manager::create();
165     std::string data = "Custom data";
166     CKM::RawBuffer rawBuffer(data.begin(), data.end());
167     CKM::RawBuffer output;
168     const char *alias = "dataG02T05";
169
170     RUNNER_ASSERT_MSG(
171         CKM_API_ERROR_ACCESS_DENIED == (temp = manager->saveData(alias, rawBuffer, CKM::Policy())),
172         "Error=" << CKM::APICodeToString(temp));
173
174     RUNNER_ASSERT_MSG(
175         CKM_API_ERROR_ACCESS_DENIED == (temp = manager->getData(alias, CKM::Password(), output)),
176         "Error=" << CKM::APICodeToString(temp));
177 }
178