Add service class in dbus tests 37/32937/21
authorMarcin Niesluchowski <m.niesluchow@samsung.com>
Mon, 12 Jan 2015 15:31:30 +0000 (16:31 +0100)
committerZbigniew Jasinski <z.jasinski@samsung.com>
Wed, 22 Apr 2015 08:26:38 +0000 (01:26 -0700)
Service class is handling dbus connection from service site

Change-Id: I246a65d09fafe9b09773e6bc718f0355ee5d88bc

tests/dbus-tests/CMakeLists.txt
tests/dbus-tests/common/dbus_test_commons.cpp [new file with mode: 0644]
tests/dbus-tests/common/dbus_test_commons.h [new file with mode: 0644]
tests/dbus-tests/common/dbus_test_service.cpp [new file with mode: 0644]
tests/dbus-tests/common/dbus_test_service.h [new file with mode: 0644]
tests/dbus-tests/cynara_dbus_tests.cpp

index 9dda30f8e081f0ac07c0cb5696eb5854da7e7ca3..a263055e1b3572f870fbb2bd8065724dfe139c51 100644 (file)
@@ -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 (file)
index 0000000..548d015
--- /dev/null
@@ -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 <m.niesluchow@samsung.com>
+ * @version     1.0
+ * @brief       commons for dbus test files
+ */
+
+#include <dbus_test_commons.h>
+
+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 (file)
index 0000000..2409063
--- /dev/null
@@ -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 <m.niesluchow@samsung.com>
+ * @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 <string>
+
+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 (file)
index 0000000..81da52b
--- /dev/null
@@ -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 <m.niesluchow@samsung.com>
+ * @version     1.0
+ * @brief       dbus test service definition
+ */
+
+#include <dbus_test_service.h>
+#include <dbus_test_commons.h>
+
+#include <chrono>
+
+#include <dpl/test/test_runner.h>
+
+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<Service*>(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 (file)
index 0000000..bf986df
--- /dev/null
@@ -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 <m.niesluchow@samsung.com>
+ * @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 <dbus_test_service_object.h>
+
+#include <dbus_connection.h>
+
+#include <string>
+
+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
index 7d7a4fd20195c5bc0b64cd7a3407798185babce1..09148f2d6bd0945bb27a763d6584d191ae248abe 100644 (file)
@@ -27,6 +27,7 @@
 #include <cynara_test_admin.h>
 #include <dbus_connection.h>
 #include <dbus_test_busconfig_writer.h>
+#include <dbus_test_commons.h>
 #include <smack_access.h>
 #include <tests_common.h>
 
@@ -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";