2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
5 * @file security_server_tests_clean_env.cpp
6 * @author Zbigniew Jasinski (z.jasinski@samsung.com)
8 * @brief Functions to prepare clean env for tests.
14 #include <dpl/test/test_runner.h>
15 #include <dpl/log/log.h>
16 #include <dbus/dbus.h>
17 #include <dbus-glib.h>
18 #include <glib-object.h>
21 #define DBUS_SYSTEMD_NAME "org.freedesktop.systemd1"
25 * We can check our service name running:
26 * gdbus introspect --system --dest org.freedesktop.systemd1 \
27 * --object-path /org/freedesktop/systemd1|grep "node unit"
28 * or calling method ListUnits on DBus org.freedesktop.systemd1.Manager
29 * interface to list all units.
31 #define DBUS_SYSTEMD_OBJECT "/org/freedesktop/systemd1/unit/security_2dserver_2eservice"
32 #define DBUS_SYSTEMD_INTERFACE "org.freedesktop.systemd1.Unit"
33 #define DBUS_SYSTEMD_METHOD "Restart"
35 int restart_security_server()
38 const char *dbus_client_name = "tests.dbus.client";
40 DBusMessage* msg = NULL;
41 DBusMessageIter args, iter;
42 DBusConnection* conn = NULL;
44 DBusPendingCall *pending = NULL;
46 // initialize the errors
47 dbus_error_init(&err);
49 // connect to the system bus and check for errors
50 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
51 ret = dbus_error_is_set(&err);
53 dbus_error_free(&err);
54 dbus_connection_unref(conn);
55 RUNNER_ASSERT_MSG(0 == ret, "dbus_bus_get() failed, ret: " << ret);
58 // request our name on the bus
59 ret = dbus_bus_request_name(conn, dbus_client_name, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
60 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret && DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER != ret) {
61 dbus_error_free(&err);
62 dbus_connection_unref(conn);
63 RUNNER_ASSERT_MSG(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER == ret,
64 "dbus_bus_request_name() failed, ret: " << ret);
67 // crate a new method call for checking SMACK context from DBus interface
68 msg = dbus_message_new_method_call(DBUS_SYSTEMD_NAME,
70 DBUS_SYSTEMD_INTERFACE,
74 dbus_error_free(&err);
75 dbus_connection_unref(conn);
76 RUNNER_ASSERT_MSG(NULL != msg,
77 "dbus_message_new_method_call() failed, ret: " << ret);
82 * the call will start the unit and its dependencies,
83 * but will fail if this would change an already queued job.
85 const char *dbus_systemd_srv_unit_mode = "fail";
87 // append arguments, mode = "fail" for "Restart" method
88 dbus_message_iter_init_append(msg, &args);
89 ret = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &dbus_systemd_srv_unit_mode);
91 dbus_message_unref(msg);
92 dbus_connection_unref(conn);
93 RUNNER_ASSERT_MSG(1 == ret, "Out of memory");
96 // send message and get a handle for a reply
97 // -1 is default timeout
98 ret = dbus_connection_send_with_reply (conn, msg, &pending, -1);
100 dbus_message_unref(msg);
101 dbus_connection_unref(conn);
102 RUNNER_ASSERT_MSG(1 == ret, "Out of memory");
104 if (NULL == pending) {
105 dbus_message_unref(msg);
106 dbus_connection_unref(conn);
107 RUNNER_ASSERT_MSG(NULL != pending, "Pending call null");
110 dbus_connection_flush(conn);
113 dbus_message_unref(msg);
116 dbus_pending_call_block(pending);
119 msg = dbus_pending_call_steal_reply(pending);
120 RUNNER_ASSERT_MSG(NULL != msg, "Reply null");
122 // free message handle
123 dbus_pending_call_unref(pending);
125 ret = dbus_message_iter_init(msg, &iter);
127 dbus_message_unref(msg);
128 dbus_connection_unref(conn);
129 RUNNER_ASSERT_MSG(0 != ret, "Message has no arguments");
132 // free reply and close connection
133 dbus_message_unref(msg);
134 dbus_connection_unref(conn);