Merging tizen into ckm. Stage 1.
[platform/core/test/security-tests.git] / tests / common / dbus_message_out.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        dbus_message_out.cpp
18  * @author      Marcin Niesluchowski (m.niesluchow@samsung.com)
19  * @version     1.0
20  * @brief       DBus outgoing message wrapper class source file
21  */
22
23 #include <dbus_message_out.h>
24
25 #include <dpl/test/test_runner.h>
26
27 namespace DBus
28 {
29
30 MessageOut::MessageOut(const std::string &destination,
31                        const std::string &path,
32                        const std::string &interface,
33                        const std::string &method)
34 {
35     m_message = dbus_message_new_method_call(destination.c_str(),
36                                              path.c_str(),
37                                              interface.c_str(),
38                                              method.c_str());
39     RUNNER_ASSERT_MSG(nullptr != m_message,
40                       "Failed to create new method call. Not enough memory");
41 }
42
43 MessageOut::MessageOut(MessageOut &&other)
44     : m_message(other.m_message)
45 {
46     other.m_message = nullptr;
47 }
48
49 MessageOut::~MessageOut()
50 {
51     if (m_message != nullptr)
52         dbus_message_unref(m_message);
53 }
54
55 DBusMessage* MessageOut::getMessage() const
56 {
57     return m_message;
58 }
59
60 void MessageOut::append(bool b)
61 {
62     DBusMessageIter iter;
63     dbus_message_iter_init_append(m_message, &iter);
64
65     dbus_bool_t bArg = b ? TRUE : FALSE;
66     dbus_bool_t ret = dbus_message_iter_append_basic(&iter,
67                                                      DBUS_TYPE_BOOLEAN,
68                                                      &bArg);
69     RUNNER_ASSERT_MSG(ret != FALSE, "Failed to append basic boolean. Not enough memory");
70 }
71
72 void MessageOut::append(const char *cstr)
73 {
74     DBusMessageIter iter;
75     dbus_message_iter_init_append(m_message, &iter);
76
77     dbus_bool_t ret = dbus_message_iter_append_basic(&iter,
78                                                      DBUS_TYPE_STRING,
79                                                      &cstr);
80     RUNNER_ASSERT_MSG(ret != FALSE, "Failed to append basic string. Not enough memory");
81 }
82
83 void MessageOut::append(const std::string &str)
84 {
85     append(str.c_str());
86 }
87
88 void MessageOut::append(const std::vector<std::string> &strs)
89 {
90     DBusMessageIter iter;
91     dbus_message_iter_init_append(m_message, &iter);
92
93     DBusMessageIter subIter;
94     dbus_bool_t ret = dbus_message_iter_open_container(&iter,
95                                                        DBUS_TYPE_ARRAY,
96                                                        DBUS_TYPE_STRING_AS_STRING,
97                                                        &subIter);
98     RUNNER_ASSERT_MSG(ret != FALSE, "Failed to open container. Not enough memory");
99     for (const auto &str : strs) {
100         const char *cstr = str.c_str();
101         ret = dbus_message_iter_append_basic(&subIter, DBUS_TYPE_STRING, &cstr);
102         if (ret == FALSE) {
103             dbus_message_iter_abandon_container(&iter, &subIter);
104             RUNNER_FAIL_MSG("Failed to append basic string. Not enough memory");
105         }
106     }
107     ret = dbus_message_iter_close_container(&iter, &subIter);
108     RUNNER_ASSERT_MSG(ret != FALSE, "Failed to close container. Not enough memory");
109 }
110
111 } // namespace DBus