2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Bumjin Im <bj.im@samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * @file dbus_access.cpp
20 * @author Zbigniew Jasinski (z.jasinski@samsung.com)
22 * @brief Functios used in security-tests package for restarting Security Server through
23 * SystemD DBuS interface.
26 #include <dpl/log/log.h>
27 #include <tests_common.h>
32 #include "dbus_access.h"
34 DBusAccess::DBusAccess(const char *service_name)
40 dbus_error_init(&m_err);
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";
47 m_signal_match = "type='" + m_signal_type + "',interface='" + m_signal_interface +
48 "',member='" + m_signal_member + "',path='" + m_signal_path + "'";
50 m_dbus_client_name = "tests.dbus.client";
52 m_service_name = service_name;
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);
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);
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(),
79 RUNNER_ASSERT_MSG(nullptr != m_msg,
80 "Error in dbus_message_new_method_call");
83 void DBusAccess::appendToMsg(const char *argument) {
86 dbus_message_iter_init_append(m_msg, &iter);
87 int ret = dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING,
89 RUNNER_ASSERT_MSG(ret != 0,
90 "Error in dbus_message_iter_append_basic");
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");
98 RUNNER_ASSERT_MSG(nullptr != m_pending, "Pending call null");
100 dbus_connection_flush(m_conn);
101 dbus_pending_call_block(m_pending);
103 dbus_message_unref(m_msg);
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");
113 void DBusAccess::handleMsgReply() {
114 DBusMessageIter iter;
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");
121 dbus_message_unref(m_msg);
122 dbus_pending_call_unref(m_pending);
127 void DBusAccess::connectToDBus() {
132 void DBusAccess::sendToService(const char *method) {
133 newMethodCall(method);
134 appendToMsg(m_service_name.c_str());
142 void DBusAccess::sendResetFailedToService() {
143 newMethodCall("ResetFailedUnit");
144 appendToMsg(m_service_name.c_str());
149 void DBusAccess::startService() {
150 sendToService("StartUnit");
151 sendResetFailedToService();
154 void DBusAccess::stopService() {
155 sendToService("StopUnit");
156 sendResetFailedToService();
159 void DBusAccess::restartService() {
160 sendToService("RestartUnit");
161 sendResetFailedToService();
164 DBusAccess::~DBusAccess() {
165 dbus_connection_close(m_conn);
167 dbus_connection_unref(m_conn);
168 dbus_error_free(&m_err);
170 dbus_message_unref(m_msg);
172 dbus_pending_call_unref(m_pending);