Add licence and copyright header to cynara test files
[platform/core/test/security-tests.git] / tests / common / dbus_access.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Bumjin Im <bj.im@samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License
17  */
18 /*
19  * @file        dbus_access.cpp
20  * @author      Zbigniew Jasinski (z.jasinski@samsung.com)
21  * @version     1.0
22  * @brief       Functios used in security-tests package for restarting Security Server through
23  *              SystemD DBuS interface.
24  */
25
26 #include <dpl/log/log.h>
27 #include <tests_common.h>
28
29 #include <cstring>
30 #include <unistd.h>
31
32 #include "dbus_access.h"
33
34 DBusAccess::DBusAccess(const char *service_name)
35   : m_conn(nullptr)
36   , m_msg(nullptr)
37   , m_pending(nullptr)
38   , m_handled(FALSE)
39 {
40     dbus_error_init(&m_err);
41
42     m_signal_type         = "signal";
43     m_signal_interface    = "org.freedesktop.systemd1.Manager";
44     m_signal_member       = "JobRemoved";
45     m_signal_path         = "/org/freedesktop/systemd1";
46
47     m_signal_match        = "type='" + m_signal_type + "',interface='" + m_signal_interface +
48                             "',member='" + m_signal_member + "',path='" + m_signal_path + "'";
49
50     m_dbus_client_name    = "tests.dbus.client";
51
52     m_service_name        = service_name;
53
54     connectToDBus();
55 }
56
57 void DBusAccess::connect() {
58     m_conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &m_err);
59     RUNNER_ASSERT_MSG(dbus_error_is_set(&m_err) != 1,
60         "Error in dbus_bus_get: " << m_err.message);
61     dbus_connection_set_exit_on_disconnect(m_conn, FALSE);
62 }
63
64 void DBusAccess::requestName() {
65     dbus_bus_request_name(m_conn, m_dbus_client_name.c_str(),
66         DBUS_NAME_FLAG_REPLACE_EXISTING , &m_err);
67     RUNNER_ASSERT_MSG(dbus_error_is_set(&m_err) != 1,
68         "Error in dbus_bus_request_name: " << m_err.message);
69 }
70
71 void DBusAccess::newMethodCall(const char *method) {
72     const std::string dbus_systemd_name = "org.freedesktop.systemd1";
73     const std::string dbus_systemd_object = "/org/freedesktop/systemd1";
74     const std::string dbus_systemd_interface = "org.freedesktop.systemd1.Manager";
75     m_msg = dbus_message_new_method_call(dbus_systemd_name.c_str(),
76                                          dbus_systemd_object.c_str(),
77                                          dbus_systemd_interface.c_str(),
78                                          method);
79     RUNNER_ASSERT_MSG(nullptr != m_msg,
80         "Error in dbus_message_new_method_call");
81 }
82
83 void DBusAccess::appendToMsg(const char *argument) {
84     DBusMessageIter iter;
85
86     dbus_message_iter_init_append(m_msg, &iter);
87     int ret = dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING,
88         &argument);
89     RUNNER_ASSERT_MSG(ret != 0,
90         "Error in dbus_message_iter_append_basic");
91 }
92
93 void DBusAccess::sendMsgWithReply() {
94     int ret = dbus_connection_send_with_reply(m_conn, m_msg, &m_pending, -1);
95     RUNNER_ASSERT_MSG(ret == 1,
96         "Error in dbus_connection_send_with_reply");
97
98     RUNNER_ASSERT_MSG(nullptr != m_pending, "Pending call null");
99
100     dbus_connection_flush(m_conn);
101     dbus_pending_call_block(m_pending);
102
103     dbus_message_unref(m_msg);
104     m_msg = nullptr;
105 }
106
107 void DBusAccess::getMsgReply() {
108     m_msg = dbus_pending_call_steal_reply(m_pending);
109     RUNNER_ASSERT_MSG(nullptr != m_msg,
110         "Error in dbus_pending_call_steal_reply");
111 }
112
113 void DBusAccess::handleMsgReply() {
114     DBusMessageIter iter;
115
116     RUNNER_ASSERT_MSG(dbus_message_iter_init(m_msg, &iter) != 0,
117         "Message has no arguments");
118     if (DBUS_TYPE_OBJECT_PATH != dbus_message_iter_get_arg_type(&iter)) {
119         RUNNER_FAIL_MSG("No job path in msg");
120     }
121     dbus_message_unref(m_msg);
122     dbus_pending_call_unref(m_pending);
123     m_msg = nullptr;
124     m_pending = nullptr;
125 }
126
127 void DBusAccess::connectToDBus() {
128     connect();
129     requestName();
130 }
131
132 void DBusAccess::sendToService(const char *method) {
133     newMethodCall(method);
134     appendToMsg(m_service_name.c_str());
135     appendToMsg("fail");
136     sendMsgWithReply();
137     getMsgReply();
138     handleMsgReply();
139     usleep(50000);
140 }
141
142 void DBusAccess::sendResetFailedToService() {
143     newMethodCall("ResetFailedUnit");
144     appendToMsg(m_service_name.c_str());
145     sendMsgWithReply();
146     getMsgReply();
147 }
148
149 void DBusAccess::startService() {
150     sendToService("StartUnit");
151     sendResetFailedToService();
152 }
153
154 void DBusAccess::stopService() {
155     sendToService("StopUnit");
156     sendResetFailedToService();
157 }
158
159 void DBusAccess::restartService() {
160     sendToService("RestartUnit");
161     sendResetFailedToService();
162 }
163
164 DBusAccess::~DBusAccess() {
165     dbus_connection_close(m_conn);
166     if (m_conn)
167         dbus_connection_unref(m_conn);
168     dbus_error_free(&m_err);
169     if (m_msg)
170         dbus_message_unref(m_msg);
171     if (m_pending)
172         dbus_pending_call_unref(m_pending);
173 }