Add dbus incoming message wrapper class 07/32407/4
authorMarcin Niesluchowski <m.niesluchow@samsung.com>
Wed, 17 Dec 2014 10:42:55 +0000 (11:42 +0100)
committerMarcin Niesluchowski <m.niesluchow@samsung.com>
Tue, 23 Dec 2014 16:53:36 +0000 (17:53 +0100)
Change-Id: I1f476b1c44605458e1168c60bf3c4e6b04462be5

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

index aaa7811..7db2584 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_in.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
diff --git a/tests/common/dbus_message_in.cpp b/tests/common/dbus_message_in.cpp
new file mode 100644 (file)
index 0000000..c04533b
--- /dev/null
@@ -0,0 +1,180 @@
+/*
+ * 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_in.cpp
+ * @author      Marcin Niesluchowski (m.niesluchow@samsung.com)
+ * @version     1.0
+ * @brief       DBus incoming message wrapper class source file
+ */
+
+#include <dbus_message_in.h>
+
+#include <dpl/test/test_runner.h>
+
+namespace DBus
+{
+
+MessageIn::MessageIn(DBusMessage *message, bool ref)
+    : m_message(message)
+{
+    RUNNER_ASSERT(m_message != nullptr);
+    if (ref)
+        dbus_message_ref(m_message);
+}
+
+MessageIn::MessageIn(MessageIn &&other)
+    : m_message(other.m_message)
+{
+    other.m_message = nullptr;
+}
+
+MessageIn::~MessageIn()
+{
+    if (m_message != nullptr)
+        dbus_message_unref(m_message);
+}
+
+int MessageIn::getType()
+{
+    return dbus_message_get_type(m_message);
+}
+
+bool MessageIn::isMethodCall(const std::string &interface, const std::string &method)
+{
+    dbus_bool_t ret = dbus_message_is_method_call(m_message,
+                                                  interface.c_str(),
+                                                  method.c_str());
+    return ret == TRUE;
+}
+
+bool MessageIn::isSignal(const std::string &interface, const std::string &signalName)
+{
+    dbus_bool_t ret = dbus_message_is_signal(m_message,
+                                             interface.c_str(),
+                                             signalName.c_str());
+    return ret == TRUE;
+}
+
+bool MessageIn::isError(const std::string &errorName)
+{
+    dbus_bool_t ret = dbus_message_is_error(m_message,
+                                            errorName.c_str());
+    return ret == TRUE;
+}
+
+MessageIn::Iterator MessageIn::iterInit()
+{
+    return Iterator(this->m_message);
+}
+
+MessageIn::Iterator::Iterator(DBusMessage* message)
+{
+    dbus_message_iter_init(message, &m_iterator);
+}
+
+MessageIn::Iterator::Iterator(DBusMessageIter *iteratorOver)
+{
+    dbus_message_iter_recurse(iteratorOver, &m_iterator);
+}
+
+bool MessageIn::Iterator::next()
+{
+    return dbus_message_iter_next(&m_iterator) != FALSE;
+}
+
+void MessageIn::Iterator::expectNext()
+{
+    RUNNER_ASSERT_MSG(next(), "No next argument in message");
+}
+
+int MessageIn::Iterator::getArgType()
+{
+    return dbus_message_iter_get_arg_type(&m_iterator);
+}
+
+void MessageIn::Iterator::expectArgType(int argType)
+{
+    int argTypeActual = getArgType();
+    RUNNER_ASSERT_MSG(argTypeActual == argType, "Wrong argument type in message"
+                                                    << " Actual: " << argTypeActual
+                                                    << " Expected: " << argType);
+}
+
+void MessageIn::Iterator::expectArgTypeValid()
+{
+    RUNNER_ASSERT_MSG(getArgType() != DBUS_TYPE_INVALID, "Invalid argument type in message");
+}
+
+char MessageIn::Iterator::getArgChar()
+{
+    return getArg<char>();
+}
+
+bool MessageIn::Iterator::getArgBool()
+{
+    dbus_bool_t value;
+    dbus_message_iter_get_basic(&m_iterator, &value);
+    return value != FALSE;
+}
+
+int16_t MessageIn::Iterator::getArgInt16()
+{
+    return getArg<int16_t>();
+}
+
+uint16_t MessageIn::Iterator::getArgUint16()
+{
+    return getArg<uint16_t>();
+}
+
+int32_t MessageIn::Iterator::getArgInt32()
+{
+    return getArg<int32_t>();
+}
+
+uint32_t MessageIn::Iterator::getArgUint32()
+{
+    return getArg<uint32_t>();
+}
+
+int64_t MessageIn::Iterator::getArgInt64()
+{
+    return getArg<int64_t>();
+}
+
+uint64_t MessageIn::Iterator::getArgUint64()
+{
+    return getArg<uint64_t>();
+}
+
+double MessageIn::Iterator::getArgDouble()
+{
+    return getArg<double>();
+}
+
+std::string MessageIn::Iterator::getArgString()
+{
+    char *value;
+    dbus_message_iter_get_basic(&m_iterator, &value);
+    return std::string(value);
+}
+
+MessageIn::Iterator MessageIn::Iterator::recurse()
+{
+    return Iterator(&(this->m_iterator));
+}
+
+} // namespace DBus
diff --git a/tests/common/dbus_message_in.h b/tests/common/dbus_message_in.h
new file mode 100644 (file)
index 0000000..e0a0dc7
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * 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_in.h
+ * @author      Marcin Niesluchowski (m.niesluchow@samsung.com)
+ * @version     1.0
+ * @brief       DBus incoming message wrapper class header
+ */
+
+#ifndef COMMON_DBUS_MESSAGE_IN_H
+#define COMMON_DBUS_MESSAGE_IN_H
+
+#include <dbus/dbus.h>
+
+#include <cstdint>
+#include <string>
+
+namespace DBus
+{
+
+class MessageIn
+{
+public:
+    MessageIn(DBusMessage *message, bool ref = false);
+    MessageIn(const MessageIn &other) = delete;
+    MessageIn(MessageIn &&other);
+    ~MessageIn();
+
+    MessageIn& operator=(const MessageIn &other) = delete;
+
+    int getType();
+    bool isMethodCall(const std::string &interface, const std::string &method);
+    bool isSignal(const std::string &interface, const std::string &signalName);
+    bool isError(const std::string &errorName);
+
+    class Iterator
+    {
+    public:
+        friend class MessageIn;
+
+        bool next();
+        void expectNext();
+        int getArgType();
+        void expectArgType(int argType);
+        void expectArgTypeValid();
+        char getArgChar();
+        bool getArgBool();
+        int16_t getArgInt16();
+        uint16_t getArgUint16();
+        int32_t getArgInt32();
+        uint32_t getArgUint32();
+        int64_t getArgInt64();
+        uint64_t getArgUint64();
+        double getArgDouble();
+        std::string getArgString();
+        Iterator recurse();
+
+    private:
+        template<class T>
+        T getArg() {
+            T value;
+            dbus_message_iter_get_basic(&m_iterator, &value);
+            return value;
+        }
+
+        // sub constructor
+        Iterator(DBusMessageIter *iteratorOver);
+        // message constructor
+        Iterator(DBusMessage *message);
+        DBusMessageIter m_iterator;
+    };
+
+    Iterator iterInit();
+
+private:
+    DBusMessage *m_message;
+};
+
+} // namespace DBus
+
+#endif // COMMON_DBUS_MESSAGE_IN_H