From 4fe480d5283fbfc935d37fb186aa8829b881dbf3 Mon Sep 17 00:00:00 2001 From: Krzysztof Jackiewicz Date: Mon, 25 Aug 2014 18:00:01 +0200 Subject: [PATCH] Add tests for async ckm API - saveKey Change-Id: I5caf324c5861a44edb613bf11e52fd02f270d27f --- tests/ckm/CMakeLists.txt | 1 + tests/ckm/async-api.cpp | 203 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+) create mode 100644 tests/ckm/async-api.cpp diff --git a/tests/ckm/CMakeLists.txt b/tests/ckm/CMakeLists.txt index ccdcb4a..853c510 100644 --- a/tests/ckm/CMakeLists.txt +++ b/tests/ckm/CMakeLists.txt @@ -34,6 +34,7 @@ SET(CKM_SOURCES ${PROJECT_SOURCE_DIR}/tests/ckm/main.cpp ${PROJECT_SOURCE_DIR}/tests/ckm/capi-testcases.cpp ${PROJECT_SOURCE_DIR}/tests/ckm/capi-access_control.cpp + ${PROJECT_SOURCE_DIR}/tests/ckm/async-api.cpp ) INCLUDE_DIRECTORIES(SYSTEM ${CKM_DEP_INCLUDE_DIRS}) diff --git a/tests/ckm/async-api.cpp b/tests/ckm/async-api.cpp new file mode 100644 index 0000000..22c0f81 --- /dev/null +++ b/tests/ckm/async-api.cpp @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Bumjin Im + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +/* + * @file async-api.cpp + * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com) + * @version 1.0 + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +RUNNER_TEST_GROUP_INIT(CKM_ASYNC_API); + +using namespace CKM; +using namespace std; + +namespace { + +class MyObserver: public ManagerAsync::Observer +{ +public: + MyObserver() : + m_finished(false), m_error(0) + { + } + + void ReceivedError(int error) + { + LogError("Received error: " << error); + m_finished = true; + m_error = error; + m_cv.notify_one(); + } + + void ReceivedSaveKey() + { + LogDebug("Key saved"); + m_finished = true; + m_cv.notify_one(); + } + + void WaitForResponse() + { + unique_lock < mutex > lock(m_mutex); + + if (!m_cv.wait_for(lock, std::chrono::seconds(5), [this] {return m_finished;})) + RUNNER_ASSERT_MSG(false, "Timeout reached!"); + } + + bool m_finished; + int m_error; + +protected: + mutex m_mutex; + condition_variable m_cv; +}; + +const uid_t USER_APP = 5000; +const uid_t USER_ROOT = 0; +const char* USER_PASS = "user-pass"; +const char* ROOT_PASS = "test-pass"; +const char* ALIAS1 = "alias1"; + +string keyPem = "-----BEGIN PUBLIC KEY-----\n" + "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n" + "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n" + "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n" + "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n" + "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n" + "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n" + "zQIDAQAB\n" + "-----END PUBLIC KEY-----"; + +KeyShPtr prepareKey() +{ + RawBuffer buffer(keyPem.begin(), keyPem.end()); + KeyShPtr k = Key::create(buffer); + RUNNER_ASSERT_MSG(k, "Key was not created!"); + return k; +} + +void test_expect_invalid_param(const Alias &alias, const KeyShPtr &key, const Policy &policy) +{ + shared_ptr obs = make_shared(); + ManagerAsync mgr; + + mgr.saveKey(static_pointer_cast < ManagerAsync::Observer > (obs), alias, key, policy); + obs->WaitForResponse(); + + RUNNER_ASSERT_MSG(obs->m_finished, "Request is not finished!"); + RUNNER_ASSERT_MSG(obs->m_error == CKM_API_ERROR_INPUT_PARAM, + "Expected " << CKM_API_ERROR_INPUT_PARAM << " error, got: " << obs->m_error); +} + +void test_save_key() { + shared_ptr obs = make_shared(); + ManagerAsync mgr; + + KeyShPtr key = prepareKey(); + + mgr.saveKey(static_pointer_cast < ManagerAsync::Observer > (obs), ALIAS1, key, Policy()); + obs->WaitForResponse(); + + RUNNER_ASSERT_MSG(obs->m_finished, "Request is not finished!"); + RUNNER_ASSERT_MSG(obs->m_error == 0, + "Request failed " << obs->m_error << ":" << CKM::ErrorToString(obs->m_error)); +} + +} // namespace anonymous + +// TODO get rid of test dependency +RUNNER_TEST(TA0000_init) +{ + int temp; + auto control = CKM::Control::create(); + RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->unlockUserKey(USER_APP, USER_PASS)), + "Error=" << CKM::ErrorToString(temp)); + RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->removeUserData(USER_APP)), + "Error=" << CKM::ErrorToString(temp)); + RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->unlockUserKey(USER_ROOT, ROOT_PASS)), + "Error=" << CKM::ErrorToString(temp)); + RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->removeUserData(USER_ROOT)), + "Error=" << CKM::ErrorToString(temp)); +} + +RUNNER_TEST(TA0010_save_key_no_observer) +{ + ManagerAsync::ObserverPtr obs; + ManagerAsync mgr; + + KeyShPtr key = prepareKey(); + + try { + mgr.saveKey(obs, ALIAS1, key, Policy()); + RUNNER_ASSERT_MSG(false, "saveKey() should have thrown an exception"); + } catch (const std::invalid_argument& e) { + RUNNER_ASSERT(true); + } catch (...) { + RUNNER_ASSERT_MSG(false, "Unexpected exception"); + } +} + +RUNNER_TEST(TA0020_save_key_no_alias) +{ + test_expect_invalid_param("", prepareKey(), Policy()); +} + +RUNNER_TEST(TA0030_save_key_no_key) +{ + KeyShPtr key; + test_expect_invalid_param(ALIAS1, key, Policy()); +} + +RUNNER_TEST(TA0090_save_key_positive) +{ + test_save_key(); +} + +RUNNER_CHILD_TEST(TA0100_user_save_key_positive) +{ + SecurityServer::AccessProvider ap("mylabel"); + ap.allowAPI("key-manager::api-storage", "rw"); + ap.applyAndSwithToUser(USER_APP, USER_APP); + + test_save_key(); +} + +RUNNER_TEST(TA9999_deinit) +{ + int temp; + auto control = CKM::Control::create(); + RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->lockUserKey(USER_APP)), + "Error=" << CKM::ErrorToString(temp)); + RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->removeUserData(USER_APP)), + "Error=" << CKM::ErrorToString(temp)); + RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->lockUserKey(USER_ROOT)), + "Error=" << CKM::ErrorToString(temp)); + RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = control->removeUserData(USER_ROOT)), + "Error=" << CKM::ErrorToString(temp)); +} -- 2.7.4