From: Marcin Niesluchowski Date: Mon, 12 Jan 2015 15:31:30 +0000 (+0100) Subject: Add service class in dbus tests X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bd6b705fc16adac6d475b5755844d402ac5a759f;p=platform%2Fcore%2Ftest%2Fsecurity-tests.git Add service class in dbus tests Service class is handling dbus connection from service site Change-Id: I246a65d09fafe9b09773e6bc718f0355ee5d88bc --- diff --git a/tests/dbus-tests/CMakeLists.txt b/tests/dbus-tests/CMakeLists.txt index 9dda30f8..a263055e 100644 --- a/tests/dbus-tests/CMakeLists.txt +++ b/tests/dbus-tests/CMakeLists.txt @@ -39,6 +39,8 @@ SET(TARGET_DBUS_TESTS "dbus-tests") SET(DBUS_TESTS_SOURCES ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_admin.cpp + ${PROJECT_SOURCE_DIR}/tests/dbus-tests/common/dbus_test_commons.cpp + ${PROJECT_SOURCE_DIR}/tests/dbus-tests/common/dbus_test_service.cpp ${PROJECT_SOURCE_DIR}/tests/dbus-tests/common/dbus_test_service_object.cpp ${PROJECT_SOURCE_DIR}/tests/dbus-tests/main.cpp ${PROJECT_SOURCE_DIR}/tests/dbus-tests/common/dbus_test_busconfig_writer.cpp diff --git a/tests/dbus-tests/common/dbus_test_commons.cpp b/tests/dbus-tests/common/dbus_test_commons.cpp new file mode 100644 index 00000000..548d015a --- /dev/null +++ b/tests/dbus-tests/common/dbus_test_commons.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2015 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 dbus_test_commons.cpp + * @author Marcin Niesluchowski + * @version 1.0 + * @brief commons for dbus test files + */ + +#include + +namespace DBusTest +{ + +const std::string CONNECTION_NAME_PREFIX("com.security_tests"); + +const std::string connectionNameFromStr(const std::string &str) +{ + return CONNECTION_NAME_PREFIX + "." + str; +} + +} diff --git a/tests/dbus-tests/common/dbus_test_commons.h b/tests/dbus-tests/common/dbus_test_commons.h new file mode 100644 index 00000000..24090635 --- /dev/null +++ b/tests/dbus-tests/common/dbus_test_commons.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2015 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 dbus_test_commons.h + * @author Marcin Niesluchowski + * @version 1.0 + * @brief commons for dbus test files + */ + +#ifndef DBUS_TESTS_COMMON_DBUS_TEST_COMMONS_H +#define DBUS_TESTS_COMMON_DBUS_TEST_COMMONS_H + +#include + +namespace DBusTest +{ + +extern const std::string CONNECTION_NAME_PREFIX; + +const std::string connectionNameFromStr(const std::string &str); + +} + +#endif // DBUS_TESTS_COMMON_DBUS_TEST_COMMONS_H diff --git a/tests/dbus-tests/common/dbus_test_service.cpp b/tests/dbus-tests/common/dbus_test_service.cpp new file mode 100644 index 00000000..81da52b7 --- /dev/null +++ b/tests/dbus-tests/common/dbus_test_service.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2015 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 dbus_test_service.cpp + * @author Marcin Niesluchowski + * @version 1.0 + * @brief dbus test service definition + */ + +#include +#include + +#include + +#include + +namespace { + +const std::string OBJECT_PATH_DIR("/com/security_tests/"); +const std::string INTERFACE_SUFFIX("TestInterface"); + +} + +namespace DBusTest +{ + +ServiceCreds::ServiceCreds(const std::string &name) + : m_connectionName(connectionNameFromStr(name)) + , m_objectPath(OBJECT_PATH_DIR + name) + , m_interface(m_connectionName + "." + INTERFACE_SUFFIX) +{ +} + +const std::string& ServiceCreds::connectionName() const +{ + return m_connectionName; +} + +const std::string& ServiceCreds::objectPath() const +{ + return m_objectPath; +} + +const std::string& ServiceCreds::interface() const +{ + return m_interface; +} + +Service::Service(const ServiceCreds &serviceCreds) + : m_connection(DBUS_BUS_SYSTEM, true) + , m_object(serviceCreds.interface()) + , m_messageReceived(false) + , m_exceptionThrown(false) +{ + m_connection.requestName(serviceCreds.connectionName()); + m_connection.registerObjectPath(serviceCreds.objectPath(), + handleMessage, + (void*)this); +} + +void Service::insertMethodHandler(const std::string &method, + const MessageHandler &messageHandler) +{ + m_object.insertMethodHandler(method, messageHandler); +} + +DBusHandlerResult Service::handleMessage(DBusConnection *connection, + DBusMessage *message, + void *userData) +{ + Service* service = reinterpret_cast(userData); + (void)userData; + try { + service->m_messageReceived = true; + DBus::MessageIn messageIn(message, true); + DBus::Connection conn(connection); + service->m_object.handleMessage(conn, messageIn, service->m_errorString); + } catch(...) { + service->m_exceptionThrown = true; + } + return DBUS_HANDLER_RESULT_HANDLED; +} + +void Service::run(bool &finish, bool timeout) +{ + using namespace std::chrono; + high_resolution_clock::time_point tpEnd = high_resolution_clock::now() + seconds(5); + while (!finish) { + m_connection.readWrite(1000); + + high_resolution_clock::time_point tp = high_resolution_clock::now(); + if (tp > tpEnd) { + if (timeout) + return; + RUNNER_FAIL_MSG("Timeout not expected"); + } + + while(true) { + m_errorString.clear(); + m_exceptionThrown = false; + + m_connection.dispatch(); + if (!m_messageReceived) + break; + m_messageReceived = false; + + RUNNER_ASSERT_MSG(!m_exceptionThrown, "Exception thrown while handling message"); + RUNNER_ASSERT_MSG(m_errorString.empty(), "Error while handling dispatched message. " + << m_errorString); + } + } +} + +} // namespace DBusTest diff --git a/tests/dbus-tests/common/dbus_test_service.h b/tests/dbus-tests/common/dbus_test_service.h new file mode 100644 index 00000000..bf986df5 --- /dev/null +++ b/tests/dbus-tests/common/dbus_test_service.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2015 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 dbus_test_service.h + * @author Marcin Niesluchowski + * @version 1.0 + * @brief dbus test service declaration + */ + +#ifndef DBUS_TESTS_COMMON_DBUS_TEST_SERVICE_H +#define DBUS_TESTS_COMMON_DBUS_TEST_SERVICE_H + +#include + +#include + +#include + +namespace DBusTest +{ + +class ServiceCreds +{ +public: + ServiceCreds(const std::string &name); + ~ServiceCreds() = default; + + const std::string& connectionName() const; + const std::string& objectPath() const; + const std::string& interface() const; + +private: + std::string m_connectionName; + std::string m_objectPath; + std::string m_interface; +}; + +class Service +{ +public: + Service(const ServiceCreds &serviceCreds); + Service(const Service &other) = delete; + ~Service() = default; + + Service& operator=(const Service &other) = delete; + + void insertMethodHandler(const std::string &method, + const MessageHandler &messageHandler); + void run(bool &finish, bool timeout); + +private: + static DBusHandlerResult handleMessage(DBusConnection *connection, + DBusMessage *message, + void *userData); + + DBus::Connection m_connection; + ServiceObject m_object; + bool m_messageReceived; + bool m_exceptionThrown; + std::string m_errorString; +}; + +} // namespace DBusTest + +#endif // DBUS_TESTS_COMMON_DBUS_TEST_SERVICE_H diff --git a/tests/dbus-tests/cynara_dbus_tests.cpp b/tests/dbus-tests/cynara_dbus_tests.cpp index 7d7a4fd2..09148f2d 100644 --- a/tests/dbus-tests/cynara_dbus_tests.cpp +++ b/tests/dbus-tests/cynara_dbus_tests.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -40,13 +41,6 @@ using namespace DBusTest; static const std::string ROOT_UID_STR("0"); -static const std::string CONNECTION_NAME_PREFIX("com.security_tests"); - -static const std::string connectionNameFromStr(const std::string &str) -{ - return CONNECTION_NAME_PREFIX + "." + str; -} - static const std::string privilegeFromStr(const std::string &str) { return str + "Privilege";