2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
18 #include <dpl/test/test_runner.h>
19 #include <dpl/test/test_runner_child.h>
21 #include <tests_common.h>
22 #include <test-certs.h>
23 #include <access_provider2.h>
24 #include <ckm-common.h>
25 #include <ckm-privileged-common.h>
27 #include <ckm/ckm-manager.h>
28 #include <ckm/ckm-control.h>
29 #include <ckm/ckm-password.h>
30 #include <ckm/ckm-type.h>
31 #include <ckm/ckm-pkcs12.h>
35 #include <openssl/x509.h>
36 #include <openssl/x509v3.h>
39 const int USER_APP = 5000;
40 const int GROUP_APP = 5000;
42 const int USER_APP_2 = 5020;
43 const int USER_APP_3 = 5030;
45 const char * const APP_PASS = "user-pass";
46 const int USER_TEST = 5001;
48 const CKM::CertificateShPtrVector EMPTY_CERT_VECTOR;
49 const CKM::AliasVector EMPTY_ALIAS_VECTOR;
50 } // namespace anonymous
53 * How to numerate tests:
55 * T - test case (always T)
56 * AB - number of test group (always two digits)
57 * C - test number in group (all tests with same TABC must be run in the same time).
61 RUNNER_TEST_GROUP_INIT(T151_CKM_STORAGE_PERNAMENT_TESTS);
63 RUNNER_TEST(T1510_init_unlock_key)
65 reset_user_data(USER_TEST, APP_PASS);
68 RUNNER_TEST(T1511_insert_data)
70 auto certee = TestData::getTestCertificate(TestData::OCSP_AVAILABLE_LEAF);
71 auto certim = TestData::getTestCertificate(TestData::OCSP_AVAILABLE_IM);
72 CKM::Alias certeeAlias("CertEE");
73 CKM::Alias certimAlias("CertIM");
75 ScopedDBUnlock unlock(USER_TEST, APP_PASS);
76 ScopedAccessProvider ap(TEST_LABEL);
77 ap.allowAPI("key-manager::api-storage", "rw");
78 ap.applyAndSwithToUser(USER_TEST, GROUP_APP);
80 auto manager = CKM::Manager::create();
81 RUNNER_ASSERT(CKM_API_SUCCESS == manager->saveCertificate(certeeAlias, certee, CKM::Policy()));
82 RUNNER_ASSERT(CKM_API_SUCCESS == manager->saveCertificate(certimAlias, certim, CKM::Policy()));
86 stop_service(MANAGER);
87 start_service(MANAGER);
91 ScopedDBUnlock unlock(USER_TEST, APP_PASS);
92 ScopedAccessProvider ap(TEST_LABEL);
93 ap.allowAPI("key-manager::api-storage", "rw");
94 ap.applyAndSwithToUser(USER_TEST, GROUP_APP);
96 auto manager = CKM::Manager::create();
97 int status1 = manager->saveCertificate(certeeAlias, certee, CKM::Policy());
98 int status2 = manager->saveCertificate(certimAlias, certim, CKM::Policy());
100 CKM_API_ERROR_DB_ALIAS_EXISTS == status1,
101 "Certificate should be in database already. Error=" << CKMErrorToString(status1));
103 CKM_API_ERROR_DB_ALIAS_EXISTS == status2,
104 "Certificate should be in database already. Error=" << CKMErrorToString(status2));
108 RUNNER_TEST(T1519_deinit)
110 remove_user_data(USER_TEST);
113 RUNNER_TEST_GROUP_INIT(T170_CKM_STORAGE_PERNAMENT_TESTS);
115 RUNNER_TEST(T1701_init_unlock_key)
117 unlock_user_data(USER_TEST+1, "t170-special-password");
119 ScopedAccessProvider ap(TEST_LABEL);
120 ap.applyAndSwithToUser(USER_TEST+1, GROUP_APP);
123 RUNNER_CHILD_TEST(T1702_insert_data)
126 ScopedAccessProvider ap(TEST_LABEL);
127 ap.applyAndSwithToUser(USER_TEST+1, GROUP_APP);
129 auto certee = TestData::getTestCertificate(TestData::THIRD_PARTY_LEAF);
131 auto manager = CKM::Manager::create();
132 size_t current_aliases_num = count_aliases(ALIAS_CERT);
133 int status1 = manager->saveCertificate(CKM::Alias("CertEEE"), certee, CKM::Policy());
136 CKM_API_SUCCESS == status1,
137 "Could not put certificate in datbase. Error=" << CKMErrorToString(status1));
141 CKM_API_SUCCESS == (temp = manager->getCertificateAliasVector(av)),
142 "Error=" << CKMErrorToString(temp));
144 (current_aliases_num+1) == static_cast<size_t>(temp = av.size()),
145 "Vector size: " << temp << ". Expected: " << (current_aliases_num+1));
148 RUNNER_TEST(T1703_removeApplicationData)
151 auto control = CKM::Control::create();
153 CKM_API_SUCCESS == (tmp = control->removeApplicationData(TEST_LABEL)),
154 "Error=" << CKMErrorToString(tmp));
157 RUNNER_CHILD_TEST(T1704_data_test)
160 ScopedAccessProvider ap(TEST_LABEL);
161 ap.applyAndSwithToUser(USER_TEST+1, GROUP_APP);
164 auto manager = CKM::Manager::create();
167 CKM_API_SUCCESS == (temp = manager->getCertificateAliasVector(av)),
168 "Error=" << CKMErrorToString(temp));
170 0 == (temp = av.size()),
171 "Vector size: " << temp << ". Expected: 0");
174 RUNNER_TEST(T1705_deinit)
176 remove_user_data(USER_TEST+1);
179 RUNNER_TEST(T17101_init)
183 auto control = CKM::Control::create();
185 CKM_API_SUCCESS == (tmp = control->lockUserKey(USER_TEST+2)),
186 "Error=" << CKMErrorToString(tmp));
188 CKM_API_SUCCESS == (tmp = control->removeUserData(USER_TEST+2)),
189 "Error=" << CKMErrorToString(tmp));
191 CKM_API_SUCCESS == (tmp = control->unlockUserKey(USER_TEST+2, "t1706-special-password")),
192 "Error=" << CKMErrorToString(tmp));
195 CKM_API_SUCCESS == (tmp = control->lockUserKey(USER_TEST+3)),
196 "Error=" << CKMErrorToString(tmp));
198 CKM_API_SUCCESS == (tmp = control->removeUserData(USER_TEST+3)),
199 "Error=" << CKMErrorToString(tmp));
201 CKM_API_SUCCESS == (tmp = control->unlockUserKey(USER_TEST+3, "t1706-special-password")),
202 "Error=" << CKMErrorToString(tmp));
205 RUNNER_CHILD_TEST(T17102_prep_data_01)
208 ScopedAccessProvider ap(TEST_LABEL);
209 ap.applyAndSwithToUser(USER_TEST+2, GROUP_APP);
212 auto manager = CKM::Manager::create();
214 std::string data = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4";
216 CKM::RawBuffer buffer(data.begin(), data.end());
217 CKM::Policy exportable(CKM::Password(), true);
220 CKM_API_SUCCESS == (temp = manager->saveData("data1", buffer, exportable)),
221 "Error=" << CKMErrorToString(temp));
224 RUNNER_CHILD_TEST(T17103_prep_data_02)
227 ScopedAccessProvider ap(TEST_LABEL_2);
228 ap.applyAndSwithToUser(USER_TEST+2, GROUP_APP);
231 auto manager = CKM::Manager::create();
233 std::string data = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4";
235 CKM::RawBuffer buffer(data.begin(), data.end());
236 CKM::Policy exportable(CKM::Password(), true);
239 CKM_API_SUCCESS == (temp = manager->saveData("data2", buffer, exportable)),
240 "Error=" << CKMErrorToString(temp));
243 RUNNER_CHILD_TEST(T17104_prep_data_03)
246 ScopedAccessProvider ap(TEST_LABEL);
247 ap.applyAndSwithToUser(USER_TEST+3, GROUP_APP);
250 auto manager = CKM::Manager::create();
252 std::string data = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4";
254 CKM::RawBuffer buffer(data.begin(), data.end());
255 CKM::Policy exportable(CKM::Password(), true);
258 CKM_API_SUCCESS == (temp = manager->saveData("data3", buffer, exportable)),
259 "Error=" << CKMErrorToString(temp));
262 RUNNER_CHILD_TEST(T17105_prep_data_04)
265 ScopedAccessProvider ap(TEST_LABEL_2);
266 ap.applyAndSwithToUser(USER_TEST+3, GROUP_APP);
269 auto manager = CKM::Manager::create();
271 std::string data = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4";
273 CKM::RawBuffer buffer(data.begin(), data.end());
274 CKM::Policy exportable(CKM::Password(), true);
277 CKM_API_SUCCESS == (temp = manager->saveData("data4", buffer, exportable)),
278 "Error=" << CKMErrorToString(temp));
281 RUNNER_TEST(T17106_remove_application)
285 auto control = CKM::Control::create();
287 CKM_API_SUCCESS == (tmp = control->lockUserKey(USER_TEST+3)),
288 "Error=" << CKMErrorToString(tmp));
290 CKM_API_SUCCESS == (tmp = control->removeApplicationData(TEST_LABEL)),
291 "Error=" << CKMErrorToString(tmp));
294 RUNNER_CHILD_TEST(T17107_check_data_01)
297 ScopedAccessProvider ap(TEST_LABEL);
298 ap.applyAndSwithToUser(USER_TEST+2, GROUP_APP);
301 auto manager = CKM::Manager::create();
304 CKM_API_SUCCESS == (temp = manager->getDataAliasVector(av)),
305 "Error=" << CKMErrorToString(temp));
307 0 == (temp = av.size()),
308 "Vector size: " << temp << ". Expected: 0");
311 RUNNER_CHILD_TEST(T17108_check_data_02)
314 ScopedAccessProvider ap(TEST_LABEL_2);
315 ap.applyAndSwithToUser(USER_TEST+2, GROUP_APP);
318 auto manager = CKM::Manager::create();
321 CKM_API_SUCCESS == (temp = manager->getDataAliasVector(av)),
322 "Error=" << CKMErrorToString(temp));
324 1 == (temp = av.size()),
325 "Vector size: " << temp << ". Expected: 1");
328 RUNNER_TEST(T17109_unlock_user2)
332 auto control = CKM::Control::create();
334 CKM_API_SUCCESS == (tmp = control->unlockUserKey(USER_TEST+3, "t1706-special-password")),
335 "Error=" << CKMErrorToString(tmp));
338 RUNNER_CHILD_TEST(T17110_check_data_03)
341 ScopedAccessProvider ap(TEST_LABEL);
342 ap.allowAPI("key-manager::api-storage", "rw");
343 ap.applyAndSwithToUser(USER_TEST+3, GROUP_APP);
346 auto manager = CKM::Manager::create();
349 CKM_API_SUCCESS == (temp = manager->getDataAliasVector(av)),
350 "Error=" << CKMErrorToString(temp));
352 0 == (temp = av.size()),
353 "Vector size: " << temp << ". Expected: 0");
356 RUNNER_CHILD_TEST(T17111_check_data_04)
359 ScopedAccessProvider ap(TEST_LABEL_2);
360 ap.allowAPI("key-manager::api-storage", "rw");
361 ap.applyAndSwithToUser(USER_TEST+3, GROUP_APP);
364 auto manager = CKM::Manager::create();
367 CKM_API_SUCCESS == (temp = manager->getDataAliasVector(av)),
368 "Error=" << CKMErrorToString(temp));
370 1 == (temp = av.size()),
371 "Vector size: " << temp << ". Expected: 1");
374 RUNNER_TEST(T17112_deinit)
376 remove_user_data(USER_TEST+2);
377 remove_user_data(USER_TEST+3);
380 int main(int argc, char *argv[])
382 return DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);