1 /* GIO testing utilities
3 * Copyright (C) 2008-2010 Red Hat, Inc.
4 * Copyright (C) 2012 Collabora Ltd. <http://www.collabora.co.uk/>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: David Zeuthen <davidz@redhat.com>
22 * Xavier Claessens <xavier.claessens@collabora.co.uk>
33 #include "gdbus-tests.h"
34 #include "gdbusconnection.h"
35 #include "gdbusprivate.h"
37 #include "gioenumtypes.h"
38 #include "gtestdbus.h"
42 /* -------------------------------------------------------------------------- */
43 /* Utilities to cleanup the mess in the case unit test process crash */
45 #define ADD_PID_FORMAT "add pid %d\n"
46 #define REMOVE_PID_FORMAT "remove pid %d\n"
49 watch_parent (gint fd)
55 channel = g_io_channel_unix_new (fd);
58 fds[0].events = G_IO_HUP | G_IO_IN;
61 pids_to_kill = g_array_new (FALSE, FALSE, sizeof (guint));
66 gchar *command = NULL;
71 num_events = g_poll (fds, 1, -1);
75 if (fds[0].revents == G_IO_HUP)
77 /* Parent quit, cleanup the mess and exit */
78 for (n = 0; n < pids_to_kill->len; n++)
80 pid = g_array_index (pids_to_kill, guint, n);
81 g_print ("cleaning up pid %d\n", pid);
85 g_array_unref (pids_to_kill);
86 g_io_channel_shutdown (channel, FALSE, &error);
87 g_assert_no_error (error);
88 g_io_channel_unref (channel);
93 /* Read the command from the input */
94 g_io_channel_read_line (channel, &command, NULL, NULL, &error);
95 g_assert_no_error (error);
97 /* Check for known commands */
98 if (sscanf (command, ADD_PID_FORMAT, &pid) == 1)
100 g_array_append_val (pids_to_kill, pid);
102 else if (sscanf (command, REMOVE_PID_FORMAT, &pid) == 1)
104 for (n = 0; n < pids_to_kill->len; n++)
106 if (g_array_index (pids_to_kill, guint, n) == pid)
108 g_array_remove_index (pids_to_kill, n);
115 g_warning ("unknown pid %d to remove", pid);
120 g_warning ("unknown command from parent '%s'", command);
131 static gsize started = 0;
132 static GIOChannel *channel = NULL;
134 if (g_once_init_enter (&started))
138 /* fork a child to clean up when we are killed */
139 if (pipe (pipe_fds) != 0)
141 g_warning ("pipe() failed: %m");
142 g_assert_not_reached ();
148 g_warning ("fork() failed: %m");
149 g_assert_not_reached ();
155 watch_parent (pipe_fds[0]);
161 channel = g_io_channel_unix_new (pipe_fds[1]);
164 g_once_init_leave (&started, 1);
171 watcher_send_command (const gchar *command)
174 GError *error = NULL;
176 channel = watcher_init ();
178 g_io_channel_write_chars (channel, command, -1, NULL, &error);
179 g_assert_no_error (error);
181 g_io_channel_flush (channel, &error);
182 g_assert_no_error (error);
185 /* This could be interesting to expose in public API */
187 _g_test_watcher_add_pid (GPid pid)
191 command = g_strdup_printf (ADD_PID_FORMAT, (guint) pid);
192 watcher_send_command (command);
197 _g_test_watcher_remove_pid (GPid pid)
201 command = g_strdup_printf (REMOVE_PID_FORMAT, (guint) pid);
202 watcher_send_command (command);
206 /* -------------------------------------------------------------------------- */
207 /* GTestDBus object implementation */
211 * @short_description: D-Bus testing helper
212 * @include: gio/gio.h
214 * Helper to test D-Bus code wihtout messing up with user' session bus.
219 typedef struct _GTestDBusClass GTestDBusClass;
220 typedef struct _GTestDBusPrivate GTestDBusPrivate;
225 * The #GTestDBus structure contains only private data and
226 * should only be accessed using the provided API.
233 GTestDBusPrivate *priv;
239 * Class structure for #GTestDBus.
243 struct _GTestDBusClass {
244 GObjectClass parent_class;
247 struct _GTestDBusPrivate
249 GTestDBusFlags flags;
250 GPtrArray *service_dirs;
262 G_DEFINE_TYPE (GTestDBus, g_test_dbus, G_TYPE_OBJECT)
265 g_test_dbus_init (GTestDBus *self)
267 self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self), G_TYPE_TEST_DBUS,
270 self->priv->service_dirs = g_ptr_array_new_with_free_func (g_free);
274 g_test_dbus_dispose (GObject *object)
276 GTestDBus *self = (GTestDBus *) object;
279 g_test_dbus_down (self);
281 G_OBJECT_CLASS (g_test_dbus_parent_class)->dispose (object);
285 g_test_dbus_finalize (GObject *object)
287 GTestDBus *self = (GTestDBus *) object;
289 g_ptr_array_unref (self->priv->service_dirs);
290 g_free (self->priv->bus_address);
292 G_OBJECT_CLASS (g_test_dbus_parent_class)->finalize (object);
296 g_test_dbus_get_property (GObject *object,
301 GTestDBus *self = (GTestDBus *) object;
306 g_value_set_flags (value, g_test_dbus_get_flags (self));
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
315 g_test_dbus_set_property (GObject *object,
320 GTestDBus *self = (GTestDBus *) object;
325 self->priv->flags = g_value_get_flags (value);
328 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
334 g_test_dbus_class_init (GTestDBusClass *klass)
336 GObjectClass *object_class = G_OBJECT_CLASS (klass);
338 object_class->dispose = g_test_dbus_dispose;
339 object_class->finalize = g_test_dbus_finalize;
340 object_class->get_property = g_test_dbus_get_property;
341 object_class->set_property = g_test_dbus_set_property;
343 g_type_class_add_private (object_class, sizeof (GTestDBusPrivate));
348 * #GTestDBusFlags specifying the behaviour of the dbus session
352 g_object_class_install_property (object_class, PROP_FLAGS,
353 g_param_spec_flags ("flags",
354 P_("dbus session flags"),
355 P_("Flags specifying the behaviour of the dbus session"),
356 G_TYPE_TEST_DBUS_FLAGS, G_TEST_DBUS_NONE,
357 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
358 G_PARAM_STATIC_STRINGS));
363 write_config_file (GTestDBus *self)
367 GFileIOStream *iostream;
369 GError *error = NULL;
371 file = g_file_new_tmp ("g-test-dbus-XXXXXX", &iostream, &error);
372 g_assert_no_error (error);
373 g_object_unref (iostream);
375 contents = g_string_new (NULL);
376 g_string_append (contents,
378 " <type>session</type>\n"
379 " <listen>unix:tmpdir=/tmp</listen>\n");
381 for (i = 0; i < self->priv->service_dirs->len; i++)
383 const gchar *path = g_ptr_array_index (self->priv->service_dirs, i);
385 g_string_append_printf (contents,
386 " <servicedir>%s</servicedir>\n", path);
389 g_string_append (contents,
390 " <policy context=\"default\">\n"
391 " <!-- Allow everything to be sent -->\n"
392 " <allow send_destination=\"*\" eavesdrop=\"true\"/>\n"
393 " <!-- Allow everything to be received -->\n"
394 " <allow eavesdrop=\"true\"/>\n"
395 " <!-- Allow anyone to own anything -->\n"
396 " <allow own=\"*\"/>\n"
400 g_file_replace_contents (file, contents->str, contents->len,
401 NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, &error);
402 g_assert_no_error (error);
404 g_string_free (contents, TRUE);
410 start_daemon (GTestDBus *self)
412 gchar *argv[] = {"dbus-daemon", "--print-address", "--config-file=foo", NULL};
419 GError *error = NULL;
421 /* Write config file and set its path in argv */
422 file = write_config_file (self);
423 config_path = g_file_get_path (file);
424 config_arg = g_strdup_printf ("--config-file=%s", config_path);
425 argv[2] = config_arg;
427 /* Spawn dbus-daemon */
428 g_spawn_async_with_pipes (NULL,
434 &self->priv->bus_pid,
439 g_assert_no_error (error);
441 _g_test_watcher_add_pid (self->priv->bus_pid);
443 /* Read bus address from daemon' stdout */
444 channel = g_io_channel_unix_new (stdout_fd);
445 g_io_channel_read_line (channel, &self->priv->bus_address, NULL,
447 g_assert_no_error (error);
448 self->priv->bus_address[termpos] = '\0';
450 /* start dbus-monitor */
451 if (g_getenv ("G_DBUS_MONITOR") != NULL)
455 command = g_strdup_printf ("dbus-monitor --address %s",
456 self->priv->bus_address);
457 g_spawn_command_line_async (command, NULL);
464 g_io_channel_shutdown (channel, FALSE, &error);
465 g_assert_no_error (error);
466 g_io_channel_unref (channel);
468 g_file_delete (file, NULL, &error);
469 g_assert_no_error (error);
470 g_object_unref (file);
472 g_free (config_path);
477 stop_daemon (GTestDBus *self)
479 kill (self->priv->bus_pid, SIGTERM);
480 _g_test_watcher_remove_pid (self->priv->bus_pid);
481 self->priv->bus_pid = 0;
483 g_free (self->priv->bus_address);
484 self->priv->bus_address = NULL;
489 * @flags: a #GTestDBusFlags
491 * Create a new #GTestDBus object.
493 * Returns: (transfer full): a new #GTestDBus.
496 g_test_dbus_new (GTestDBusFlags flags)
498 return g_object_new (G_TYPE_TEST_DBUS,
504 * g_test_dbus_get_flags:
505 * @self: a #GTestDBus
507 * Returns: the value of #GTestDBus:flags property
510 g_test_dbus_get_flags (GTestDBus *self)
512 g_return_val_if_fail (G_IS_TEST_DBUS (self), G_TEST_DBUS_NONE);
514 return self->priv->flags;
518 * g_test_dbus_get_bus_address:
519 * @self: a #GTestDBus
521 * Get the address on which dbus-daemon is running. if g_test_dbus_up() has not
522 * been called yet, %NULL is returned. This can be used with
523 * g_dbus_connection_new_for_address()
525 * Returns: the address of the bus, or %NULL.
528 g_test_dbus_get_bus_address (GTestDBus *self)
530 g_return_val_if_fail (G_IS_TEST_DBUS (self), NULL);
532 return self->priv->bus_address;
536 * g_test_dbus_add_service_dir:
537 * @self: a #GTestDBus
538 * @path: path to a directory containing .service files
540 * Add a path where dbus-daemon will lookup for .services files. This can't be
541 * called after g_test_dbus_up().
544 g_test_dbus_add_service_dir (GTestDBus *self,
547 g_return_if_fail (G_IS_TEST_DBUS (self));
548 g_return_if_fail (self->priv->bus_address == NULL);
550 g_ptr_array_add (self->priv->service_dirs, g_strdup (path));
555 * @self: a #GTestDBus
557 * Start a dbus-daemon instance and set DBUS_SESSION_BUS_ADDRESS. After this
558 * call, it is safe for unit tests to start sending messages on the session bug.
560 * If this function is called from setup callback of g_test_add(),
561 * g_test_dbus_down() must be called in its teardown callback.
563 * If this function is called from unit test's main(), then g_test_dbus_down()
564 * must be called after g_test_run().
567 g_test_dbus_up (GTestDBus *self)
569 g_return_if_fail (G_IS_TEST_DBUS (self));
570 g_return_if_fail (self->priv->bus_address == NULL);
571 g_return_if_fail (!self->priv->up);
575 g_setenv ("DBUS_SESSION_BUS_ADDRESS", self->priv->bus_address, TRUE);
576 self->priv->up = TRUE;
582 * @self: a #GTestDBus
584 * Stop the session bus started by g_test_dbus_up().
586 * Unlike g_test_dbus_down(), this won't verify the #GDBusConnection
587 * singleton returned by g_bus_get() or g_bus_get_sync() is destroyed. Unit
588 * tests wanting to verify behaviour after the session bus has been stopped
589 * can use this function but should still call g_test_dbus_down() when done.
592 g_test_dbus_stop (GTestDBus *self)
594 g_return_if_fail (G_IS_TEST_DBUS (self));
595 g_return_if_fail (self->priv->bus_address != NULL);
602 * @self: a #GTestDBus
604 * Stop the session bus started by g_test_dbus_up().
606 * This will wait for the singleton returned by g_bus_get() or g_bus_get_sync()
607 * is destroyed. This is done to ensure that the next unit test won't get a
608 * leaked singleton from this test.
611 g_test_dbus_down (GTestDBus *self)
613 GDBusConnection *connection;
615 g_return_if_fail (G_IS_TEST_DBUS (self));
616 g_return_if_fail (self->priv->up);
618 connection = _g_bus_get_singleton_if_exists (G_BUS_TYPE_SESSION);
619 if (connection != NULL)
620 g_dbus_connection_set_exit_on_close (connection, FALSE);
622 if (self->priv->bus_address != NULL)
625 if (connection != NULL)
627 _g_object_wait_for_single_ref (connection);
628 g_object_unref (connection);
631 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
632 self->priv->up = FALSE;
638 * Unset DISPLAY and DBUS_SESSION_BUS_ADDRESS env variables to ensure the test
639 * won't use user's session bus.
641 * This is useful for unit tests that want to verify behaviour when no session
642 * bus is running. It is not necessary to call this if unit test already calls
643 * g_test_dbus_up() before acquiring the session bus.
646 g_test_dbus_unset (void)
648 g_unsetenv ("DISPLAY");
649 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");