2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Bumjin Im <bj.im@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
20 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
25 #include <condition_variable>
27 #include <dpl/test/test_runner.h>
28 #include <dpl/test/test_runner_child.h>
29 #include <dpl/log/log.h>
30 #include <ckm/ckm-manager-async.h>
31 #include <ckm/ckm-control.h>
32 #include <tests_common.h>
33 #include <access_provider.h>
35 RUNNER_TEST_GROUP_INIT(CKM_ASYNC_API);
42 class MyObserver: public ManagerAsync::Observer
46 m_finished(false), m_error(0)
50 void ReceivedError(int error)
52 LogError("Received error: " << error);
58 void ReceivedSaveKey()
60 LogDebug("Key saved");
65 void WaitForResponse()
67 unique_lock < mutex > lock(m_mutex);
69 if (!m_cv.wait_for(lock, std::chrono::seconds(5), [this] {return m_finished;}))
70 RUNNER_ASSERT_MSG(false, "Timeout reached!");
78 condition_variable m_cv;
81 const uid_t USER_APP = 5000;
82 const uid_t USER_ROOT = 0;
83 const char* USER_PASS = "user-pass";
84 const char* ROOT_PASS = "test-pass";
85 const char* ALIAS1 = "alias1";
87 string keyPem = "-----BEGIN PUBLIC KEY-----\n"
88 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
89 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
90 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
91 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
92 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
93 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
95 "-----END PUBLIC KEY-----";
99 RawBuffer buffer(keyPem.begin(), keyPem.end());
100 KeyShPtr k = Key::create(buffer);
101 RUNNER_ASSERT_MSG(k, "Key was not created!");
105 void test_expect_invalid_param(const Alias &alias, const KeyShPtr &key, const Policy &policy)
107 shared_ptr<MyObserver> obs = make_shared<MyObserver>();
110 mgr.saveKey(static_pointer_cast < ManagerAsync::Observer > (obs), alias, key, policy);
111 obs->WaitForResponse();
113 RUNNER_ASSERT_MSG(obs->m_finished, "Request is not finished!");
114 RUNNER_ASSERT_MSG(obs->m_error == CKM_API_ERROR_INPUT_PARAM,
115 "Expected " << CKM_API_ERROR_INPUT_PARAM << " error, got: " << obs->m_error);
118 void test_save_key() {
119 shared_ptr<MyObserver> obs = make_shared<MyObserver>();
122 KeyShPtr key = prepareKey();
124 mgr.saveKey(static_pointer_cast < ManagerAsync::Observer > (obs), ALIAS1, key, Policy());
125 obs->WaitForResponse();
127 RUNNER_ASSERT_MSG(obs->m_finished, "Request is not finished!");
128 RUNNER_ASSERT_MSG(obs->m_error == 0,
129 "Request failed " << obs->m_error << ":" << CKM::ErrorToString(obs->m_error));
132 } // namespace anonymous
134 // TODO get rid of test dependency
135 RUNNER_TEST(TA0000_init)
138 auto control = CKM::Control::create();
139 RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->unlockUserKey(USER_APP, USER_PASS)),
140 "Error=" << CKM::ErrorToString(temp));
141 RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->removeUserData(USER_APP)),
142 "Error=" << CKM::ErrorToString(temp));
143 RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->unlockUserKey(USER_ROOT, ROOT_PASS)),
144 "Error=" << CKM::ErrorToString(temp));
145 RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->removeUserData(USER_ROOT)),
146 "Error=" << CKM::ErrorToString(temp));
149 RUNNER_TEST(TA0010_save_key_no_observer)
151 ManagerAsync::ObserverPtr obs;
154 KeyShPtr key = prepareKey();
157 mgr.saveKey(obs, ALIAS1, key, Policy());
158 RUNNER_ASSERT_MSG(false, "saveKey() should have thrown an exception");
159 } catch (const std::invalid_argument& e) {
162 RUNNER_ASSERT_MSG(false, "Unexpected exception");
166 RUNNER_TEST(TA0020_save_key_no_alias)
168 test_expect_invalid_param("", prepareKey(), Policy());
171 RUNNER_TEST(TA0030_save_key_no_key)
174 test_expect_invalid_param(ALIAS1, key, Policy());
177 RUNNER_TEST(TA0090_save_key_positive)
182 RUNNER_CHILD_TEST(TA0100_user_save_key_positive)
184 SecurityServer::AccessProvider ap("mylabel");
185 ap.allowAPI("key-manager::api-storage", "rw");
186 ap.applyAndSwithToUser(USER_APP, USER_APP);
191 RUNNER_TEST(TA9999_deinit)
194 auto control = CKM::Control::create();
195 RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->lockUserKey(USER_APP)),
196 "Error=" << CKM::ErrorToString(temp));
197 RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->removeUserData(USER_APP)),
198 "Error=" << CKM::ErrorToString(temp));
199 RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->lockUserKey(USER_ROOT)),
200 "Error=" << CKM::ErrorToString(temp));
201 RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->removeUserData(USER_ROOT)),
202 "Error=" << CKM::ErrorToString(temp));