Add new test cases. Correct old tests with history.
[platform/core/test/security-tests.git] / tests / security-server-tests / security_server_clean_env.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  */
4 /*
5  * @file    security_server_tests_clean_env.cpp
6  * @author  Zbigniew Jasinski (z.jasinski@samsung.com)
7  * @version 1.0
8  * @brief   Functions to prepare clean env for tests.
9  *
10  */
11
12 #include <stdio.h>
13 #include <unistd.h>
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>
19
20 // DBus definitions
21 #define DBUS_SYSTEMD_NAME       "org.freedesktop.systemd1"
22 /*
23  * DBUS_SYSTEMD_OBJECT
24  *
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.
30  */
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"
34
35 int restart_security_server()
36 {
37     int ret = -1;
38     const char *dbus_client_name = "tests.dbus.client";
39
40     DBusMessage* msg = NULL;
41     DBusMessageIter args, iter;
42     DBusConnection* conn = NULL;
43     DBusError err;
44     DBusPendingCall *pending = NULL;
45
46     // initialize the errors
47     dbus_error_init(&err);
48
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);
52     if (0 != ret) {
53         dbus_error_free(&err);
54         dbus_connection_unref(conn);
55         RUNNER_ASSERT_MSG(0 == ret, "dbus_bus_get() failed, ret: " << ret);
56     }
57
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);
65     }
66
67     // crate a new method call for checking SMACK context from DBus interface
68     msg = dbus_message_new_method_call(DBUS_SYSTEMD_NAME,
69                                        DBUS_SYSTEMD_OBJECT,
70                                        DBUS_SYSTEMD_INTERFACE,
71                                        DBUS_SYSTEMD_METHOD);
72
73     if (NULL == msg) {
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);
78     }
79
80     /*
81      * mode = fail
82      * the call will start the unit and its dependencies,
83      * but will fail if this would change an already queued job.
84      */
85     const char *dbus_systemd_srv_unit_mode = "fail";
86
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);
90     if (1 != ret) {
91         dbus_message_unref(msg);
92         dbus_connection_unref(conn);
93         RUNNER_ASSERT_MSG(1 == ret, "Out of memory");
94     }
95
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);
99     if (ret != 1) {
100         dbus_message_unref(msg);
101         dbus_connection_unref(conn);
102         RUNNER_ASSERT_MSG(1 == ret, "Out of memory");
103     }
104     if (NULL == pending) {
105         dbus_message_unref(msg);
106         dbus_connection_unref(conn);
107         RUNNER_ASSERT_MSG(NULL != pending, "Pending call null");
108     }
109
110     dbus_connection_flush(conn);
111
112     // free message
113     dbus_message_unref(msg);
114
115     // block until reply
116     dbus_pending_call_block(pending);
117
118     // get the reply
119     msg = dbus_pending_call_steal_reply(pending);
120     RUNNER_ASSERT_MSG(NULL != msg, "Reply null");
121
122     // free message handle
123     dbus_pending_call_unref(pending);
124
125     ret = dbus_message_iter_init(msg, &iter);
126     if (0 == ret) {
127         dbus_message_unref(msg);
128         dbus_connection_unref(conn);
129         RUNNER_ASSERT_MSG(0 != ret, "Message has no arguments");
130     }
131
132     // free reply and close connection
133     dbus_message_unref(msg);
134     dbus_connection_unref(conn);
135
136     return 0;
137 }