1 /* Unit tests for systemd activation.
3 * Copyright © 2010-2011 Nokia Corporation
4 * Copyright © 2015 Collabora Ltd.
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation files
8 * (the "Software"), to deal in the Software without restriction,
9 * including without limitation the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #include "test-utils-glib.h"
41 DBusConnection *caller;
42 const char *caller_name;
43 DBusConnection *systemd;
44 const char *systemd_name;
45 DBusMessage *systemd_message;
46 DBusConnection *activated;
47 const char *activated_name;
48 DBusMessage *activated_message;
51 /* this is a macro so it gets the right line number */
52 #define assert_signal(m, \
53 sender, path, iface, member, signature, \
56 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
57 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_SIGNAL)); \
58 g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
59 g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
60 g_assert_cmpstr (dbus_message_get_path (m), ==, path); \
61 g_assert_cmpstr (dbus_message_get_interface (m), ==, iface); \
62 g_assert_cmpstr (dbus_message_get_member (m), ==, member); \
63 g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
64 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
65 g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
68 static DBusHandlerResult
69 systemd_filter (DBusConnection *connection,
73 Fixture *f = user_data;
75 if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
77 dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
80 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
83 g_assert (f->systemd_message == NULL);
84 f->systemd_message = dbus_message_ref (message);
86 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
89 static DBusHandlerResult
90 activated_filter (DBusConnection *connection,
94 Fixture *f = user_data;
96 if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
98 dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
101 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
104 g_assert (f->activated_message == NULL);
105 f->activated_message = dbus_message_ref (message);
107 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
112 gconstpointer context G_GNUC_UNUSED)
114 f->ctx = test_main_context_get ();
117 dbus_error_init (&f->e);
119 f->address = test_get_dbus_daemon (
120 "valid-config-files/systemd-activation.conf",
121 TEST_USER_ME, &f->daemon_pid);
123 if (f->address == NULL)
126 f->caller = test_connect_to_bus (f->ctx, f->address);
127 f->caller_name = dbus_bus_get_unique_name (f->caller);
131 take_well_known_name (Fixture *f,
132 DBusConnection *connection,
137 ret = dbus_bus_request_name (connection, name,
138 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
139 test_assert_no_error (&f->e);
140 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
144 test_activation (Fixture *f,
145 gconstpointer context)
149 if (f->address == NULL)
152 /* The sender sends a message to an activatable service. */
153 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
154 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable1"))
156 dbus_connection_send (f->caller, m, NULL);
157 dbus_message_unref (m);
159 /* The fake systemd connects to the bus. */
160 f->systemd = test_connect_to_bus (f->ctx, f->address);
161 if (!dbus_connection_add_filter (f->systemd, systemd_filter, f, NULL))
163 f->systemd_name = dbus_bus_get_unique_name (f->systemd);
164 take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
166 /* It gets its activation request. */
167 while (f->systemd_message == NULL)
168 test_main_context_iterate (f->ctx, TRUE);
170 m = f->systemd_message;
171 f->systemd_message = NULL;
172 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
173 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
174 "org.freedesktop.systemd1");
175 dbus_message_unref (m);
177 /* systemd starts the activatable service. */
178 f->activated = test_connect_to_bus (f->ctx, f->address);
179 if (!dbus_connection_add_filter (f->activated, activated_filter,
182 f->activated_name = dbus_bus_get_unique_name (f->activated);
183 take_well_known_name (f, f->activated, "com.example.SystemdActivatable1");
185 /* The message is delivered to the activatable service. */
186 while (f->activated_message == NULL)
187 test_main_context_iterate (f->ctx, TRUE);
189 m = f->activated_message;
190 f->activated_message = NULL;
191 assert_signal (m, f->caller_name, "/foo",
192 "com.example.bar", "UnicastSignal1", "",
193 "com.example.SystemdActivatable1");
194 dbus_message_unref (m);
196 /* The sender sends a message to a different activatable service. */
197 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
198 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable2"))
200 dbus_connection_send (f->caller, m, NULL);
201 dbus_message_unref (m);
203 /* This time systemd is already ready for it. */
204 while (f->systemd_message == NULL)
205 test_main_context_iterate (f->ctx, TRUE);
207 m = f->systemd_message;
208 f->systemd_message = NULL;
209 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
210 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
211 "org.freedesktop.systemd1");
212 dbus_message_unref (m);
214 /* A malicious process tries to disrupt the activation.
215 * In a more realistic scenario this would be another parallel
217 m = dbus_message_new_signal ("/org/freedesktop/systemd1",
218 "org.freedesktop.systemd1.Activator", "ActivationFailure");
219 if (!dbus_message_set_destination (m, "org.freedesktop.DBus"))
224 const char *unit = "dbus-com.example.SystemdActivatable2.service";
225 const char *error_name = "com.example.Malice";
226 const char *error_message = "I'm on yr bus, making yr activations fail";
228 if (!dbus_message_append_args (m,
229 DBUS_TYPE_STRING, &unit,
230 DBUS_TYPE_STRING, &error_name,
231 DBUS_TYPE_STRING, &error_message,
237 dbus_connection_send (f->caller, m, NULL);
238 dbus_message_unref (m);
240 /* This is just to make sure that the malicious message has arrived and
241 * been processed by the dbus-daemon, i.e. @caller won the race
242 * with @activated. */
243 take_well_known_name (f, f->caller, "com.example.Sync");
245 /* The activatable service takes its name. Here I'm faking it by using
246 * an existing connection; in real life it would be yet another
248 take_well_known_name (f, f->activated, "com.example.SystemdActivatable2");
250 /* The message is delivered to the activatable service. */
251 while (f->activated_message == NULL)
252 test_main_context_iterate (f->ctx, TRUE);
254 m = f->activated_message;
255 f->activated_message = NULL;
256 assert_signal (m, f->caller_name, "/foo",
257 "com.example.bar", "UnicastSignal2", "",
258 "com.example.SystemdActivatable2");
259 dbus_message_unref (m);
261 /* A third activation. */
262 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
263 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable3"))
265 dbus_connection_send (f->caller, m, NULL);
266 dbus_message_unref (m);
268 while (f->systemd_message == NULL)
269 test_main_context_iterate (f->ctx, TRUE);
271 m = f->systemd_message;
272 f->systemd_message = NULL;
273 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
274 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
275 "org.freedesktop.systemd1");
276 dbus_message_unref (m);
278 /* This time activation fails */
279 m = dbus_message_new_signal ("/org/freedesktop/systemd1",
280 "org.freedesktop.systemd1.Activator", "ActivationFailure");
284 const char *unit = "dbus-com.example.SystemdActivatable3.service";
285 const char *error_name = "com.example.Nope";
286 const char *error_message = "Computer says no";
288 if (!dbus_message_append_args (m,
289 DBUS_TYPE_STRING, &unit,
290 DBUS_TYPE_STRING, &error_name,
291 DBUS_TYPE_STRING, &error_message,
297 if (!dbus_message_set_destination (m, "org.freedesktop.DBus"))
299 dbus_connection_send (f->systemd, m, NULL);
300 dbus_message_unref (m);
304 teardown (Fixture *f,
305 gconstpointer context G_GNUC_UNUSED)
307 dbus_error_free (&f->e);
308 g_clear_error (&f->ge);
310 if (f->caller != NULL)
312 dbus_connection_close (f->caller);
313 dbus_connection_unref (f->caller);
317 if (f->systemd != NULL)
319 dbus_connection_remove_filter (f->systemd, systemd_filter, f);
320 dbus_connection_close (f->systemd);
321 dbus_connection_unref (f->systemd);
325 if (f->activated != NULL)
327 dbus_connection_remove_filter (f->activated, activated_filter, f);
328 dbus_connection_close (f->activated);
329 dbus_connection_unref (f->activated);
333 test_kill_pid (f->daemon_pid);
334 g_spawn_close_pid (f->daemon_pid);
335 test_main_context_unref (f->ctx);
343 test_init (&argc, &argv);
345 g_test_add ("/sd-activation", Fixture, NULL,
346 setup, test_activation, teardown);
348 return g_test_run ();