Add dbus outgoing message wrapper class 06/32406/4
authorMarcin Niesluchowski <m.niesluchow@samsung.com>
Wed, 17 Dec 2014 16:31:47 +0000 (17:31 +0100)
committerMarcin Niesluchowski <m.niesluchow@samsung.com>
Tue, 23 Dec 2014 16:34:49 +0000 (17:34 +0100)
Change-Id: I44199708452dd5c95b4e777f51890066199b7b1c

tests/common/CMakeLists.txt
tests/common/dbus_message_out.cpp [new file with mode: 0644]
tests/common/dbus_message_out.h [new file with mode: 0644]

index 35d987d6e472bf626c1b5fd9a3ece1c15bf26850..aaa78112a2f3b744268853665ead299f46758d35 100644 (file)
@@ -15,6 +15,7 @@ SET(COMMON_TARGET_TEST_SOURCES
     ${PROJECT_SOURCE_DIR}/tests/common/access_provider.cpp
     ${PROJECT_SOURCE_DIR}/tests/common/smack_access.cpp
     ${PROJECT_SOURCE_DIR}/tests/common/dbus_access.cpp
+    ${PROJECT_SOURCE_DIR}/tests/common/dbus_message_out.cpp
     ${PROJECT_SOURCE_DIR}/tests/common/memory.cpp
     ${PROJECT_SOURCE_DIR}/tests/common/db_sqlite.cpp
     ${PROJECT_SOURCE_DIR}/tests/common/fs_label_manager.cpp
diff --git a/tests/common/dbus_message_out.cpp b/tests/common/dbus_message_out.cpp
new file mode 100644 (file)
index 0000000..2de0659
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2014 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_message_out.cpp
+ * @author      Marcin Niesluchowski (m.niesluchow@samsung.com)
+ * @version     1.0
+ * @brief       DBus outgoing message wrapper class source file
+ */
+
+#include <dbus_message_out.h>
+
+#include <dpl/test/test_runner.h>
+
+namespace DBus
+{
+
+MessageOut::MessageOut(const std::string &destination,
+                       const std::string &path,
+                       const std::string &interface,
+                       const std::string &method)
+{
+    m_message = dbus_message_new_method_call(destination.c_str(),
+                                             path.c_str(),
+                                             interface.c_str(),
+                                             method.c_str());
+    RUNNER_ASSERT_MSG(nullptr != m_message,
+                      "Failed to create new method call. Not enough memory");
+}
+
+MessageOut::MessageOut(MessageOut &&other)
+    : m_message(other.m_message)
+{
+    other.m_message = nullptr;
+}
+
+MessageOut::~MessageOut()
+{
+    if (m_message != nullptr)
+        dbus_message_unref(m_message);
+}
+
+DBusMessage* MessageOut::getMessage() const
+{
+    return m_message;
+}
+
+void MessageOut::append(bool b)
+{
+    DBusMessageIter iter;
+    dbus_message_iter_init_append(m_message, &iter);
+
+    dbus_bool_t bArg = b ? TRUE : FALSE;
+    dbus_bool_t ret = dbus_message_iter_append_basic(&iter,
+                                                     DBUS_TYPE_BOOLEAN,
+                                                     &bArg);
+    RUNNER_ASSERT_MSG(ret != FALSE, "Failed to append basic boolean. Not enough memory");
+}
+
+void MessageOut::append(const char *cstr)
+{
+    DBusMessageIter iter;
+    dbus_message_iter_init_append(m_message, &iter);
+
+    dbus_bool_t ret = dbus_message_iter_append_basic(&iter,
+                                                     DBUS_TYPE_STRING,
+                                                     &cstr);
+    RUNNER_ASSERT_MSG(ret != FALSE, "Failed to append basic string. Not enough memory");
+}
+
+void MessageOut::append(const std::string &str)
+{
+    append(str.c_str());
+}
+
+void MessageOut::append(const std::vector<std::string> &strs)
+{
+    DBusMessageIter iter;
+    dbus_message_iter_init_append(m_message, &iter);
+
+    DBusMessageIter subIter;
+    dbus_bool_t ret = dbus_message_iter_open_container(&iter,
+                                                       DBUS_TYPE_ARRAY,
+                                                       DBUS_TYPE_STRING_AS_STRING,
+                                                       &subIter);
+    RUNNER_ASSERT_MSG(ret != FALSE, "Failed to open container. Not enough memory");
+    for (const auto &str : strs) {
+        const char *cstr = str.c_str();
+        ret = dbus_message_iter_append_basic(&subIter, DBUS_TYPE_STRING, &cstr);
+        if (ret == FALSE) {
+            dbus_message_iter_abandon_container(&iter, &subIter);
+            RUNNER_FAIL_MSG("Failed to append basic string. Not enough memory");
+        }
+    }
+    ret = dbus_message_iter_close_container(&iter, &subIter);
+    RUNNER_ASSERT_MSG(ret != FALSE, "Failed to close container. Not enough memory");
+}
+
+} // namespace DBus
diff --git a/tests/common/dbus_message_out.h b/tests/common/dbus_message_out.h
new file mode 100644 (file)
index 0000000..8b34d06
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2014 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_message_out.h
+ * @author      Marcin Niesluchowski (m.niesluchow@samsung.com)
+ * @version     1.0
+ * @brief       DBus outgoing message wrapper class header
+ */
+
+#ifndef COMMON_DBUS_MESSAGE_OUT_H
+#define COMMON_DBUS_MESSAGE_OUT_H
+
+#include <dbus/dbus.h>
+
+#include <string>
+#include <vector>
+
+namespace DBus
+{
+
+class MessageOut
+{
+public:
+    MessageOut(const std::string &destination,
+               const std::string &path,
+               const std::string &interface,
+               const std::string &method);
+    MessageOut(const MessageOut &other) = delete;
+    MessageOut(MessageOut &&other);
+    ~MessageOut();
+
+    MessageOut& operator=(const MessageOut &other) = delete;
+
+    DBusMessage* getMessage() const;
+
+    void append(bool b);
+    void append(const char *cstr);
+    void append(const std::string &str);
+    void append(const std::vector<std::string> &strs);
+
+private:
+    DBusMessage *m_message;
+};
+
+} // namespace DBus
+
+#endif // COMMON_DBUS_MESSAGE_OUT_H