Merging tizen into ckm. Stage 1.
[platform/core/test/security-tests.git] / src / common / dbus_connection.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_connection.cpp
18  * @author      Marcin Niesluchowski (m.niesluchow@samsung.com)
19  * @version     1.0
20  * @brief       DBus connection wrapper class source file
21  */
22
23 #include <dbus_connection.h>
24
25 #include <dpl/test/test_runner.h>
26
27 namespace DBus
28 {
29
30 Connection::Connection(DBusBusType busType, bool busPrivate)
31     : m_busPrivate(busPrivate)
32 {
33     DBusError error;
34     dbus_error_init(&error);
35     ErrorPtr errorPtr(&error);
36
37     if (busPrivate)
38         m_connection = dbus_bus_get_private(busType, &error);
39     else
40         m_connection = dbus_bus_get(busType, &error);
41     RUNNER_ASSERT_MSG(m_connection != nullptr,
42                       "Failed to open connection on "
43                           << (busPrivate ? "private" : "public") << " bus."
44                           << " Error: " << error.message);
45     dbus_connection_set_exit_on_disconnect(m_connection, FALSE);
46 }
47
48 Connection::~Connection()
49 {
50     if (m_busPrivate)
51         dbus_connection_close(m_connection);
52     dbus_connection_unref(m_connection);
53 }
54
55 void Connection::addMatch(const std::string &rule)
56 {
57     DBusError error;
58     dbus_error_init(&error);
59     ErrorPtr errorPtr(&error);
60
61     dbus_bus_add_match(m_connection, rule.c_str(), &error);
62     RUNNER_ASSERT_MSG(dbus_error_is_set(&error) != TRUE, "Failed to add match."
63                                                              << " Rule: " << rule << ";"
64                                                              << " Error: " << error.message);
65 }
66
67 void Connection::addFilter(DBusHandleMessageFunction handleMessageFunction,
68                            void *userData,
69                            DBusFreeFunction freeDataFunction)
70 {
71     if (freeDataFunction == nullptr)
72         freeDataFunction = [](void*)->void {};
73
74     dbus_bool_t ret = dbus_connection_add_filter(m_connection,
75                                                  handleMessageFunction,
76                                                  userData,
77                                                  freeDataFunction);
78     RUNNER_ASSERT_MSG(ret == TRUE, "Failed to add filter. Not enough memory");
79 }
80
81 void Connection::readWriteDispatch()
82 {
83     dbus_bool_t ret = dbus_connection_read_write_dispatch(m_connection, -1);
84     RUNNER_ASSERT_MSG(ret == TRUE, "Failed to read write dispatch. Disconnect message has been processed");
85 }
86
87 void Connection::flush()
88 {
89     dbus_connection_flush(m_connection);
90 }
91
92 void Connection::requestName(const std::string &name)
93 {
94     DBusError error;
95     dbus_error_init(&error);
96     ErrorPtr errorPtr(&error);
97
98     int ret = dbus_bus_request_name(m_connection,
99                                     name.c_str(),
100                                     DBUS_NAME_FLAG_REPLACE_EXISTING | DBUS_NAME_FLAG_DO_NOT_QUEUE,
101                                     &error);
102     switch (ret)
103     {
104         case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
105         case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
106             return;
107         case DBUS_REQUEST_NAME_REPLY_EXISTS:
108             RUNNER_FAIL_MSG("Failed to request name."
109                                 << " Name: " << name << ";"
110                                 << " Owner did not specified DBUS_NAME_FLAG_ALLOW_REPLACEMENT flag");
111         case -1:
112             RUNNER_FAIL_MSG("Failed to request name."
113                                 << " Name: " << name << ";"
114                                 << " Error: " << error.message);
115         default: // DBUS_REQUEST_NAME_REPLY_IN_QUEUE
116             RUNNER_FAIL_MSG("Should not happen");
117     }
118 }
119
120 MessageIn Connection::sendWithReplyAndBlock(const MessageOut &messageOut)
121 {
122     DBusError error;
123     dbus_error_init(&error);
124     ErrorPtr errorPtr(&error);
125
126     DBusMessage *messageRecv = dbus_connection_send_with_reply_and_block(m_connection,
127                                                                          messageOut.getMessage(),
128                                                                          -1,
129                                                                          &error);
130     RUNNER_ASSERT_MSG(messageRecv != nullptr, "Failed to send with reply and block. "
131                                                   << "Error: " << error.message);
132     return MessageIn(messageRecv);
133 }
134
135 } // namespace DBus