From: Dariusz Michaluk Date: Mon, 7 May 2018 09:36:08 +0000 (+0200) Subject: Merge branch 'tizen' into cynara X-Git-Tag: security-manager_5.5_testing~7^4~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=476ebec06ecae35f131b5b22a5dd00da058fe1c9;hp=08f7aa56ee4a0b9e73f8223334659378fa2f49ac;p=platform%2Fcore%2Ftest%2Fsecurity-tests.git Merge branch 'tizen' into cynara Change-Id: I64b5d9579b92d7f09dd62979b7ed6e0f9c0d5f1f --- diff --git a/packaging/security-tests.manifest b/packaging/security-tests.manifest index a6ecbcd..ea004f1 100644 --- a/packaging/security-tests.manifest +++ b/packaging/security-tests.manifest @@ -7,6 +7,9 @@ + + + diff --git a/src/cynara-tests/CMakeLists.txt b/src/cynara-tests/CMakeLists.txt index 86c578f..811ddb0 100644 --- a/src/cynara-tests/CMakeLists.txt +++ b/src/cynara-tests/CMakeLists.txt @@ -28,6 +28,7 @@ PKG_CHECK_MODULES(CYNARA_TARGET_DEP cynara-creds-gdbus cynara-creds-self cynara-creds-socket + cynara-monitor cynara-plugin dbus-1 glib-2.0 @@ -48,6 +49,7 @@ SET(CYNARA_TARGET_TEST_SOURCES ${PROJECT_SOURCE_DIR}/src/cynara-tests/common/cynara_test_cynara_mask.cpp ${PROJECT_SOURCE_DIR}/src/cynara-tests/common/cynara_test_env.cpp ${PROJECT_SOURCE_DIR}/src/cynara-tests/common/cynara_test_file_operations.cpp + ${PROJECT_SOURCE_DIR}/src/cynara-tests/common/cynara_test_monitor.cpp ${PROJECT_SOURCE_DIR}/src/cynara-tests/plugins/plugins.cpp ${PROJECT_SOURCE_DIR}/src/cynara-tests/cynara-test.cpp ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases.cpp @@ -55,6 +57,7 @@ SET(CYNARA_TARGET_TEST_SOURCES ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases_async.cpp ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases_db.cpp ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases_helpers.cpp + ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases_monitor.cpp ) #header directories diff --git a/src/cynara-tests/common/cynara_test_agent.cpp b/src/cynara-tests/common/cynara_test_agent.cpp index e2f408d..48a386c 100644 --- a/src/cynara-tests/common/cynara_test_agent.cpp +++ b/src/cynara-tests/common/cynara_test_agent.cpp @@ -75,4 +75,11 @@ void Agent::putResponse(const AgentResponse &response, int expectedResult) << "response = " << response); } +void Agent::cancelWaiting(int expectedResult) { + int ret = cynara_agent_cancel_waiting(m_agent); + RUNNER_ASSERT_MSG(ret == expectedResult, + "cynara_agent_cancel_waiting returned wrong value: " + << ret << " != " << expectedResult << "."); +} + } // namespace CynaraTestAgent diff --git a/src/cynara-tests/common/cynara_test_agent.h b/src/cynara-tests/common/cynara_test_agent.h index f224fa1..ae667a9 100644 --- a/src/cynara-tests/common/cynara_test_agent.h +++ b/src/cynara-tests/common/cynara_test_agent.h @@ -32,6 +32,7 @@ public: void getRequest(AgentRequest &request, int expectedResult = CYNARA_API_SUCCESS); void putResponse(const AgentResponse &response, int expectedResult = CYNARA_API_SUCCESS); + void cancelWaiting(int expectedResult = CYNARA_API_SUCCESS); private: struct cynara_agent *m_agent; diff --git a/src/cynara-tests/common/cynara_test_client.cpp b/src/cynara-tests/common/cynara_test_client.cpp index 373130e..357b66a 100644 --- a/src/cynara-tests/common/cynara_test_client.cpp +++ b/src/cynara-tests/common/cynara_test_client.cpp @@ -33,19 +33,24 @@ Client::~Client() { cynara_finish(m_cynara); } +void Client::check(const char *client, const char *session, const char *user, + const char *privilege, int expectedResult) +{ + int ret = cynara_check(m_cynara, client, session, user, privilege); + RUNNER_ASSERT_MSG(ret == expectedResult, + "cynara_check returned wrong value: " + << ret << " != " << expectedResult << "." + << " client: " << formatCstr(client) << "," + << " session: " << formatCstr(session) << "," + << " user: " << formatCstr(user) << "," + << " privilege: " << formatCstr(privilege)); +} void Client::check(const std::string &client, const std::string &session, const std::string &user, const std::string &privilege, int expectedResult) { - int ret = cynara_check(m_cynara, client.c_str(), session.c_str(), user.c_str(), privilege.c_str()); - RUNNER_ASSERT_MSG(ret == expectedResult, - "cynara_check returned wrong value: " - << ret << " != " << expectedResult << "." - << " client: " << formatCstr(client.c_str()) << "," - << " session: " << formatCstr(session.c_str()) << "," - << " user: " << formatCstr(user.c_str()) << "," - << " privilege: " << formatCstr(privilege.c_str())); + check(client.c_str(), session.c_str(), user.c_str(), privilege.c_str(), expectedResult); } } //namespace CynaraTestClient diff --git a/src/cynara-tests/common/cynara_test_client.h b/src/cynara-tests/common/cynara_test_client.h index 5e055d1..53687be 100644 --- a/src/cynara-tests/common/cynara_test_client.h +++ b/src/cynara-tests/common/cynara_test_client.h @@ -29,10 +29,11 @@ public: Client(); virtual ~Client(); + void check(const char *client, const char *session, const char *user, const char *privilege, + int expectedResult = CYNARA_API_ACCESS_ALLOWED); void check(const std::string &client, const std::string &session, const std::string &user, const std::string &privilege, int expectedResult = CYNARA_API_ACCESS_ALLOWED); - private: struct cynara *m_cynara; }; diff --git a/src/cynara-tests/common/cynara_test_monitor.cpp b/src/cynara-tests/common/cynara_test_monitor.cpp new file mode 100644 index 0000000..f01df58 --- /dev/null +++ b/src/cynara-tests/common/cynara_test_monitor.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#include + +#include + +namespace CynaraTestMonitor { + +MonitorEntries::MonitorEntries(cynara_monitor_entry **entries) + : m_entries(entries) +{ +} + +MonitorEntries::~MonitorEntries() +{ + cynara_monitor_entries_free(m_entries); +} + +size_t MonitorEntries::count() +{ + size_t count = 0; + + while (m_entries[count]) { + ++count; + } + + return count; +} + +const MonitorEntries& MonitorEntries::operator=(cynara_monitor_entry **other) +{ + m_entries = other; + + return *this; +} + +const cynara_monitor_entry* MonitorEntries::operator[](size_t i) const +{ + return m_entries[i]; +} + +Monitor::Monitor(size_t entries) + : m_monitor(nullptr) +{ + int ret; + struct cynara_monitor_configuration *cfg = nullptr; + + if (entries > 0) { + ret = cynara_monitor_configuration_create(&cfg); + RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, + "cynara_monitor_configuration_create failed. " + "ret: " << ret); + RUNNER_ASSERT_MSG(cfg != nullptr, "cynara_monitor_configuration struct " + "was not initialized"); + + ret = cynara_monitor_configuration_set_buffer_size(cfg, entries); + RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, + "cynara_monitor_configuration_set_buffer_size failed. " + "ret: " << ret); + } + + ret = cynara_monitor_initialize(&m_monitor, cfg); + RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, + "cynara_monitor_initialize failed. ret: " << ret); + RUNNER_ASSERT_MSG(m_monitor != nullptr, "cynara_monitor struct was not initialized"); +} + +Monitor::~Monitor() +{ + cynara_monitor_finish(m_monitor); +} + +cynara_monitor_entry** Monitor::get() +{ + struct cynara_monitor_entry **entries; + int ret = cynara_monitor_entries_get(m_monitor, &entries); + RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS, + "cynara_monitor_entries_get failed. ret: " << ret); + RUNNER_ASSERT_MSG(entries != nullptr, "cynara_monitor_entry table was not initialized"); + + return entries; +} + +void Monitor::flush() +{ + cynara_monitor_entries_flush(m_monitor); +} + +} //namespace CynaraTestMonitor diff --git a/src/cynara-tests/common/cynara_test_monitor.h b/src/cynara-tests/common/cynara_test_monitor.h new file mode 100644 index 0000000..55cbeaf --- /dev/null +++ b/src/cynara-tests/common/cynara_test_monitor.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#ifndef CYNARA_TEST_MONITOR_H +#define CYNARA_TEST_MONITOR_H + +#include + +namespace CynaraTestMonitor { + +class MonitorEntries +{ +public: + MonitorEntries(cynara_monitor_entry **entries = NULL); + MonitorEntries(const MonitorEntries& that) = delete; + virtual ~MonitorEntries(); + + const MonitorEntries& operator=(const MonitorEntries &other) = delete; + const MonitorEntries& operator=(cynara_monitor_entry **other); + const cynara_monitor_entry* operator[](size_t i) const; + + size_t count(); + +private: + cynara_monitor_entry **m_entries; +}; + +class Monitor +{ +public: + Monitor(size_t entries = 0); + Monitor(const Monitor &that) = delete; + virtual ~Monitor(); + + const Monitor& operator=(const Monitor &other) = delete; + + cynara_monitor_entry** get(); + void flush(); + +private: + cynara_monitor *m_monitor; +}; + +} //namespace CynaraTestMonitor + +#endif // CYNARA_TEST_MONITOR_H diff --git a/src/cynara-tests/test_cases_agent.cpp b/src/cynara-tests/test_cases_agent.cpp index 6d90e67..dc3a675 100644 --- a/src/cynara-tests/test_cases_agent.cpp +++ b/src/cynara-tests/test_cases_agent.cpp @@ -460,6 +460,23 @@ void tcag15_agent_disconnects_func() client.process(CYNARA_API_SUCCESS, Client::TimeoutExpectation::EXPECT_NO_TIMEOUT, 2); } +void tcag16_agent_cancel_waiting_func() +{ + Agent agent; + AgentRequest request; + + auto testTimeLimit = std::chrono::seconds(2); + auto cancelRequest = [&agent]() { + agent.cancelWaiting(); + }; + auto hangOnGetRequest = [&agent, &request]() { + agent.getRequest(request, CYNARA_API_INTERRUPTED); + }; + + Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::TIMEOUT, + cancelRequest, hangOnGetRequest); +} + RUNNER_TEST_GROUP_INIT(cynara_agent_tests) RUN_CYNARA_TEST(tcag01_set_agent_type_policy_without_plugin) @@ -477,3 +494,4 @@ RUN_CYNARA_TEST(tcag12_create_two_requests) RUN_CYNARA_TEST(tcag13_create_many_requests) RUN_CYNARA_TEST(tcag14_client_disconnects) RUN_CYNARA_TEST(tcag15_agent_disconnects) +RUN_CYNARA_TEST(tcag16_agent_cancel_waiting) diff --git a/src/cynara-tests/test_cases_monitor.cpp b/src/cynara-tests/test_cases_monitor.cpp new file mode 100644 index 0000000..2cf2898 --- /dev/null +++ b/src/cynara-tests/test_cases_monitor.cpp @@ -0,0 +1,231 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 test_cases_monitor.cpp + * @author Lukasz Pawelczyk + * @version 1.0 + * @brief Tests for libcynara-monitor + */ + +#include +#include +#include + +#include +#include +#include +#include +#include + +using namespace CynaraTestClient; +using namespace CynaraTestMonitor; + +namespace { + +inline void assertResult(int ret, int expectedResult) +{ + RUNNER_ASSERT_MSG(ret == expectedResult,"Expected result: " << expectedResult + << ", got: " << ret); +} + +void clientChecks(size_t num, size_t start = 0) +{ + Client cynaraClient; + + for (size_t i = 0; i < num; ++i) { + std::string index = std::to_string(i + start); + std::string client = "client" + index; + std::string session = "session" + index; + std::string user = "user" + index; + std::string privilege = "privilege" + index; + + cynaraClient.check(client.c_str(), session.c_str(), user.c_str(), + privilege.c_str(), CYNARA_API_ACCESS_DENIED); + } +} + +bool validateEntries(const MonitorEntries &entries, size_t num, size_t start = 0) +{ + for (size_t i = 0; i < num; ++i) { + std::string index = std::to_string(i + start); + std::string client = "client" + index; + std::string user = "user" + index; + std::string privilege = "privilege" + index; + + if (client != std::string(cynara_monitor_entry_get_client(entries[i])) || + user != std::string(cynara_monitor_entry_get_user(entries[i])) || + privilege != std::string(cynara_monitor_entry_get_privilege(entries[i]))) { + return false; + } + } + + return true; +} + +} // namespace + +void tcm01_config_invalid_param_func() +{ + int ret = cynara_monitor_configuration_create(nullptr); + assertResult(ret, CYNARA_API_INVALID_PARAM); +} + +void tcm02_config_buffer_oversize_func() +{ + int ret; + struct cynara_monitor_configuration *cfg; + + ret = cynara_monitor_configuration_create(&cfg); + assertResult(ret, CYNARA_API_SUCCESS); + + ret = cynara_monitor_configuration_set_buffer_size(cfg, CYNARA_MAX_MONITOR_BUFFER_SIZE); + assertResult(ret, CYNARA_API_SUCCESS); + + ret = cynara_monitor_configuration_set_buffer_size(cfg, CYNARA_MAX_MONITOR_BUFFER_SIZE + 1); + assertResult(ret, CYNARA_API_INVALID_PARAM); +} + +void tcm03_init_invalid_param_func() +{ + int ret = cynara_monitor_initialize(nullptr, nullptr); + assertResult(ret, CYNARA_API_INVALID_PARAM); +} + +void tcm04_entries_get_flush_no_entries_func() +{ + auto timeLimit = std::chrono::milliseconds(10); + Monitor monitor(100); + MonitorEntries entries; + + auto getMonitorEntries = [&monitor, &entries]() { + entries = monitor.get(); + }; + + auto flush = [&monitor]() { + monitor.flush(); + }; + + Timeout::callAndWait(timeLimit, Timeout::ExpectMode::TIMEOUT, + flush, getMonitorEntries); + + // TODO: system's cynara checks might increase this number + if (entries.count() != 0) + RUNNER_IGNORED_MSG("no entries should've been returned"); +} + +void tcm05_entries_get_no_flush_func() +{ + auto timeLimit = std::chrono::milliseconds(10); + Monitor monitor(100); + MonitorEntries entries; + + auto getMonitorEntries = [&monitor, &entries]() { + entries = monitor.get(); + }; + + auto client = [&monitor]() { + clientChecks(100); + }; + + Timeout::callAndWait(timeLimit, Timeout::ExpectMode::TIMEOUT, + client, getMonitorEntries); + + RUNNER_ASSERT_MSG(entries.count() == 100, "100 entries should've been returned"); + + // TODO: system's cynara checks might cause the entries to differ + if (!validateEntries(entries, 100)) + RUNNER_IGNORED_MSG("Entries received from monitor are not as expected"); +} + +void tcm06_entries_get_flush_func() +{ + auto getLimit = std::chrono::milliseconds(10); + Monitor monitor(200); + MonitorEntries entries; + + auto getMonitorEntries = [&monitor, &entries]() { + entries = monitor.get(); + }; + + auto clientAndFlush = [&monitor]() { + clientChecks(10); + monitor.flush(); + }; + + Timeout::callAndWait(getLimit, Timeout::ExpectMode::TIMEOUT, + clientAndFlush, getMonitorEntries); + + // TODO: system's cynara checks might increase this number + if (entries.count() != 10) + RUNNER_IGNORED_MSG("10 entries should've been returned"); + + // TODO: system's cynara checks might cause the entries to differ + if (!validateEntries(entries, 10)) + RUNNER_IGNORED_MSG("Entries received from monitor are not as expected"); +} + +void tcm07_entries_get_two_func() +{ + auto get1Limit = std::chrono::milliseconds(10); + Monitor monitor1(200); + Monitor monitor2(100); + MonitorEntries entries1; + MonitorEntries entries2; + + auto getMonitor1Entries = [&monitor1, &entries1]() { + entries1 = monitor1.get(); + }; + + auto clientAndGetMonitor2Entries = [&monitor2, &entries2]() { + auto get2Limit = std::chrono::milliseconds(10); + + clientChecks(100); + + auto getMonitor2Entries = [&monitor2, &entries2]() { + entries2 = monitor2.get(); + }; + + auto client = []() { + clientChecks(100, 100); + }; + + Timeout::callAndWait(get2Limit, Timeout::ExpectMode::TIMEOUT, + client, getMonitor2Entries); + }; + + Timeout::callAndWait(get1Limit, Timeout::ExpectMode::TIMEOUT, + clientAndGetMonitor2Entries, getMonitor1Entries); + + RUNNER_ASSERT_MSG(entries1.count() == 200, "200 entries should've been returned by the first monitor"); + RUNNER_ASSERT_MSG(entries2.count() == 100, "100 entries should've been returned by the second monitor"); + + // TODO: system's cynara checks might cause the entries to differ + if (!validateEntries(entries1, 200)) + RUNNER_IGNORED_MSG("Entries received from the first monitor are not as expected"); + if (!validateEntries(entries2, 100, 100)) + RUNNER_IGNORED_MSG("Entries received from the second monitor are not as expected"); +} + +RUNNER_TEST_GROUP_INIT(cynara_monitor_tests) + +RUN_CYNARA_TEST(tcm01_config_invalid_param) +RUN_CYNARA_TEST(tcm02_config_buffer_oversize) +RUN_CYNARA_TEST(tcm03_init_invalid_param) +RUN_CYNARA_TEST(tcm04_entries_get_flush_no_entries) +RUN_CYNARA_TEST(tcm05_entries_get_no_flush) +RUN_CYNARA_TEST(tcm06_entries_get_flush) +RUN_CYNARA_TEST(tcm07_entries_get_two)