2012-04-18 23:21:13 +02:00
|
|
|
|
/* GIO testing utilities
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2008-2010 Red Hat, Inc.
|
|
|
|
|
* Copyright (C) 2012 Collabora Ltd. <http://www.collabora.co.uk/>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-05-27 18:21:30 +02:00
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2012-04-18 23:21:13 +02:00
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
2014-01-23 12:58:29 +01:00
|
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2012-04-18 23:21:13 +02:00
|
|
|
|
*
|
|
|
|
|
* Authors: David Zeuthen <davidz@redhat.com>
|
|
|
|
|
* Xavier Claessens <xavier.claessens@collabora.co.uk>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
2015-10-16 18:46:26 +02:00
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
2012-06-26 02:46:28 +02:00
|
|
|
|
#include <gstdio.h>
|
Replace #ifdef HAVE_UNISTD_H checks with #ifdef G_OS_UNIX
In Windows development environments that have it, <unistd.h> is mostly
just a wrapper around several other native headers (in particular,
<io.h>, which contains read(), close(), etc, and <process.h>, which
contains getpid()). But given that some Windows dev environments don't
have <unistd.h>, everything that uses those functions on Windows
already needed to include the correct Windows header as well, and so
there is never any point to including <unistd.h> on Windows.
Also, remove some <unistd.h> includes (and a few others) that were
unnecessary even on unix.
https://bugzilla.gnome.org/show_bug.cgi?id=710519
2013-10-19 19:04:00 +02:00
|
|
|
|
#ifdef G_OS_UNIX
|
2012-04-18 23:21:13 +02:00
|
|
|
|
#include <unistd.h>
|
2012-06-19 06:04:28 +02:00
|
|
|
|
#endif
|
2012-08-17 13:22:43 +02:00
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
|
#include <io.h>
|
|
|
|
|
#endif
|
2012-04-18 23:21:13 +02:00
|
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
|
|
#include "gdbusconnection.h"
|
|
|
|
|
#include "gdbusprivate.h"
|
|
|
|
|
#include "gfile.h"
|
|
|
|
|
#include "gioenumtypes.h"
|
|
|
|
|
#include "gtestdbus.h"
|
|
|
|
|
|
|
|
|
|
#include "glibintl.h"
|
|
|
|
|
|
2012-04-19 13:49:52 +02:00
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-04-18 23:21:13 +02:00
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
/* Utility: Wait until object has a single ref */
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
GMainLoop *loop;
|
|
|
|
|
gboolean timed_out;
|
|
|
|
|
} WeakNotifyData;
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
on_weak_notify_timeout (gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
WeakNotifyData *data = user_data;
|
|
|
|
|
data->timed_out = TRUE;
|
|
|
|
|
g_main_loop_quit (data->loop);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2013-11-09 20:18:34 +01:00
|
|
|
|
dispose_on_idle (gpointer object)
|
2012-04-18 23:21:13 +02:00
|
|
|
|
{
|
2013-11-09 20:18:34 +01:00
|
|
|
|
g_object_run_dispose (object);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
g_object_unref (object);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 00:35:38 +01:00
|
|
|
|
static gboolean
|
2013-11-09 20:18:34 +01:00
|
|
|
|
_g_object_dispose_and_wait_weak_notify (gpointer object)
|
2012-04-18 23:21:13 +02:00
|
|
|
|
{
|
|
|
|
|
WeakNotifyData data;
|
|
|
|
|
guint timeout_id;
|
|
|
|
|
|
|
|
|
|
data.loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
|
data.timed_out = FALSE;
|
|
|
|
|
|
|
|
|
|
g_object_weak_ref (object, (GWeakNotify) g_main_loop_quit, data.loop);
|
|
|
|
|
|
|
|
|
|
/* Drop the ref in an idle callback, this is to make sure the mainloop
|
|
|
|
|
* is already running when weak notify happens */
|
2013-11-09 20:18:34 +01:00
|
|
|
|
g_idle_add (dispose_on_idle, object);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
|
|
|
|
|
/* Make sure we don't block forever */
|
|
|
|
|
timeout_id = g_timeout_add (30 * 1000, on_weak_notify_timeout, &data);
|
|
|
|
|
|
|
|
|
|
g_main_loop_run (data.loop);
|
|
|
|
|
|
|
|
|
|
if (data.timed_out)
|
|
|
|
|
{
|
2018-04-27 17:45:36 +02:00
|
|
|
|
g_warning ("Weak notify timeout, object ref_count=%d",
|
2012-04-18 23:21:13 +02:00
|
|
|
|
G_OBJECT (object)->ref_count);
|
|
|
|
|
}
|
2013-11-09 20:17:49 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_source_remove (timeout_id);
|
|
|
|
|
}
|
2012-04-18 23:21:13 +02:00
|
|
|
|
|
2013-11-09 20:19:44 +01:00
|
|
|
|
g_main_loop_unref (data.loop);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
return data.timed_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
/* Utilities to cleanup the mess in the case unit test process crash */
|
|
|
|
|
|
2012-04-19 13:49:52 +02:00
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
|
|
|
|
|
|
/* This could be interesting to expose in public API */
|
|
|
|
|
static void
|
|
|
|
|
_g_test_watcher_add_pid (GPid pid)
|
|
|
|
|
{
|
|
|
|
|
static gsize started = 0;
|
|
|
|
|
HANDLE job;
|
|
|
|
|
|
|
|
|
|
if (g_once_init_enter (&started))
|
|
|
|
|
{
|
|
|
|
|
JOBOBJECT_EXTENDED_LIMIT_INFORMATION info;
|
|
|
|
|
|
|
|
|
|
job = CreateJobObjectW (NULL, NULL);
|
|
|
|
|
memset (&info, 0, sizeof (info));
|
|
|
|
|
info.BasicLimitInformation.LimitFlags = 0x2000 /* JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE */;
|
|
|
|
|
|
|
|
|
|
if (!SetInformationJobObject(job, JobObjectExtendedLimitInformation, &info, sizeof (info)))
|
|
|
|
|
g_warning ("Can't enable JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE: %s", g_win32_error_message (GetLastError()));
|
|
|
|
|
|
|
|
|
|
g_once_init_leave (&started,(gsize)job);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
job = (HANDLE)started;
|
|
|
|
|
|
|
|
|
|
if (!AssignProcessToJobObject(job, pid))
|
|
|
|
|
g_warning ("Can't assign process to job: %s", g_win32_error_message (GetLastError()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_g_test_watcher_remove_pid (GPid pid)
|
|
|
|
|
{
|
|
|
|
|
/* No need to unassign the process from the job object as the process
|
|
|
|
|
will be killed anyway */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
2012-04-18 23:21:13 +02:00
|
|
|
|
#define ADD_PID_FORMAT "add pid %d\n"
|
|
|
|
|
#define REMOVE_PID_FORMAT "remove pid %d\n"
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
watch_parent (gint fd)
|
|
|
|
|
{
|
|
|
|
|
GIOChannel *channel;
|
|
|
|
|
GPollFD fds[1];
|
|
|
|
|
GArray *pids_to_kill;
|
|
|
|
|
|
|
|
|
|
channel = g_io_channel_unix_new (fd);
|
|
|
|
|
|
|
|
|
|
fds[0].fd = fd;
|
|
|
|
|
fds[0].events = G_IO_HUP | G_IO_IN;
|
|
|
|
|
fds[0].revents = 0;
|
|
|
|
|
|
|
|
|
|
pids_to_kill = g_array_new (FALSE, FALSE, sizeof (guint));
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
gint num_events;
|
|
|
|
|
gchar *command = NULL;
|
|
|
|
|
guint pid;
|
|
|
|
|
guint n;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
|
|
num_events = g_poll (fds, 1, -1);
|
|
|
|
|
if (num_events == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
gtestdbus: Fix watcher crash on FreeBSD
In file gio/gtestdbus.c, function watch_parent, there is a loop which
waits for commands sent from the parent process and kills all processes
recorded in 'pids_to_kill' array on parent process exit. The detection
of parent process exit is done by calling g_poll and checking whether
the returned event is G_IO_HUP. However, 'revents' is a bit mask, and
we should use a bitwise-AND check instead of the equality check here.
It seems to work fine on Linux, but it fails on FreeBSD because the
g_poll returns both G_IO_IN and G_IO_HUP on pipe close. This means the
watcher process continues waiting for commands after the parent process
exit, and g_io_channel_read_line returns G_IO_STATUS_EOF with 'command'
set to NULL. Then the watcher process crashes with segfault when calling
sscanf because 'command' is NULL. Since the test result is already
reported by the parent process as 'OK', this kind of crash is likely to
be unnoticed unless someone checks dmesg messages after the test:
pid 57611 (defaultvalue), uid 1001: exited on signal 11
pid 57935 (actions), uid 1001: exited on signal 11
pid 57945 (gdbus-bz627724), uid 1001: exited on signal 11
pid 57952 (gdbus-connection), uid 1001: exited on signal 11
pid 57970 (gdbus-connection-lo), uid 1001: exited on signal 11
pid 57976 (gdbus-connection-sl), uid 1001: exited on signal 11
pid 58039 (gdbus-exit-on-close), uid 1001: exited on signal 11
pid 58043 (gdbus-exit-on-close), uid 1001: exited on signal 11
pid 58047 (gdbus-exit-on-close), uid 1001: exited on signal 11
pid 58051 (gdbus-exit-on-close), uid 1001: exited on signal 11
pid 58055 (gdbus-export), uid 1001: exited on signal 11
pid 58059 (gdbus-introspection), uid 1001: exited on signal 11
pid 58065 (gdbus-names), uid 1001: exited on signal 11
pid 58071 (gdbus-proxy), uid 1001: exited on signal 11
pid 58079 (gdbus-proxy-threads), uid 1001: exited on signal 11
pid 58083 (gdbus-proxy-well-kn), uid 1001: exited on signal 11
pid 58091 (gdbus-test-codegen), uid 1001: exited on signal 11
pid 58095 (gdbus-threading), uid 1001: exited on signal 11
pid 58104 (gmenumodel), uid 1001: exited on signal 11
pid 58108 (gnotification), uid 1001: exited on signal 11
pid 58112 (gdbus-test-codegen-), uid 1001: exited on signal 11
pid 58116 (gapplication), uid 1001: exited on signal 11
pid 58132 (dbus-appinfo), uid 1001: exited on signal 11
If the watcher process crashes before killing the dbus-daemon process
spawned by the parent process, the dbus-daemon process will keep running
after all tests complete. Due to the implementation of 'communicate'
function in Python subprocess, it causes meson to crash. 'communicate'
assumes the stdout and stderr pipes are closed when the child process
exits, but it is not true if processes forked by the child process
doesn't exit. It causes Python subprocess 'communicate' function to
block on the call to poll until the timeout expires even if the test
finishes in a few seconds. Meson assumes the timeout exception always
means the test is still running. It calls 'communicate' again and
crashes because pipes no longer exist.
https://gitlab.gnome.org/Infrastructure/GitLab/issues/286
https://github.com/mesonbuild/meson/issues/3967
https://bugs.python.org/issue30154
2018-08-19 05:30:44 +02:00
|
|
|
|
if (fds[0].revents & G_IO_HUP)
|
2012-04-18 23:21:13 +02:00
|
|
|
|
{
|
|
|
|
|
/* Parent quit, cleanup the mess and exit */
|
|
|
|
|
for (n = 0; n < pids_to_kill->len; n++)
|
|
|
|
|
{
|
|
|
|
|
pid = g_array_index (pids_to_kill, guint, n);
|
2015-05-11 17:34:35 +02:00
|
|
|
|
g_printerr ("cleaning up pid %d\n", pid);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
kill (pid, SIGTERM);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_array_unref (pids_to_kill);
|
|
|
|
|
g_io_channel_shutdown (channel, FALSE, &error);
|
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
g_io_channel_unref (channel);
|
|
|
|
|
|
|
|
|
|
exit (0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Read the command from the input */
|
|
|
|
|
g_io_channel_read_line (channel, &command, NULL, NULL, &error);
|
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
|
|
/* Check for known commands */
|
|
|
|
|
if (sscanf (command, ADD_PID_FORMAT, &pid) == 1)
|
|
|
|
|
{
|
|
|
|
|
g_array_append_val (pids_to_kill, pid);
|
|
|
|
|
}
|
|
|
|
|
else if (sscanf (command, REMOVE_PID_FORMAT, &pid) == 1)
|
|
|
|
|
{
|
|
|
|
|
for (n = 0; n < pids_to_kill->len; n++)
|
|
|
|
|
{
|
|
|
|
|
if (g_array_index (pids_to_kill, guint, n) == pid)
|
|
|
|
|
{
|
|
|
|
|
g_array_remove_index (pids_to_kill, n);
|
|
|
|
|
pid = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (pid != 0)
|
|
|
|
|
{
|
|
|
|
|
g_warning ("unknown pid %d to remove", pid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_warning ("unknown command from parent '%s'", command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_free (command);
|
|
|
|
|
}
|
|
|
|
|
while (TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static GIOChannel *
|
|
|
|
|
watcher_init (void)
|
|
|
|
|
{
|
|
|
|
|
static gsize started = 0;
|
|
|
|
|
static GIOChannel *channel = NULL;
|
2017-07-31 12:30:55 +02:00
|
|
|
|
int errsv;
|
2012-04-18 23:21:13 +02:00
|
|
|
|
|
|
|
|
|
if (g_once_init_enter (&started))
|
|
|
|
|
{
|
|
|
|
|
gint pipe_fds[2];
|
|
|
|
|
|
|
|
|
|
/* fork a child to clean up when we are killed */
|
|
|
|
|
if (pipe (pipe_fds) != 0)
|
|
|
|
|
{
|
2017-07-31 12:30:55 +02:00
|
|
|
|
errsv = errno;
|
|
|
|
|
g_warning ("pipe() failed: %s", g_strerror (errsv));
|
2012-04-18 23:21:13 +02:00
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (fork ())
|
|
|
|
|
{
|
|
|
|
|
case -1:
|
2017-07-31 12:30:55 +02:00
|
|
|
|
errsv = errno;
|
|
|
|
|
g_warning ("fork() failed: %s", g_strerror (errsv));
|
2012-04-18 23:21:13 +02:00
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
|
/* child */
|
|
|
|
|
close (pipe_fds[1]);
|
|
|
|
|
watch_parent (pipe_fds[0]);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
/* parent */
|
|
|
|
|
close (pipe_fds[0]);
|
|
|
|
|
channel = g_io_channel_unix_new (pipe_fds[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_once_init_leave (&started, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
watcher_send_command (const gchar *command)
|
|
|
|
|
{
|
|
|
|
|
GIOChannel *channel;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
|
|
channel = watcher_init ();
|
|
|
|
|
|
|
|
|
|
g_io_channel_write_chars (channel, command, -1, NULL, &error);
|
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
|
|
g_io_channel_flush (channel, &error);
|
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This could be interesting to expose in public API */
|
|
|
|
|
static void
|
|
|
|
|
_g_test_watcher_add_pid (GPid pid)
|
|
|
|
|
{
|
|
|
|
|
gchar *command;
|
|
|
|
|
|
|
|
|
|
command = g_strdup_printf (ADD_PID_FORMAT, (guint) pid);
|
|
|
|
|
watcher_send_command (command);
|
|
|
|
|
g_free (command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_g_test_watcher_remove_pid (GPid pid)
|
|
|
|
|
{
|
|
|
|
|
gchar *command;
|
|
|
|
|
|
|
|
|
|
command = g_strdup_printf (REMOVE_PID_FORMAT, (guint) pid);
|
|
|
|
|
watcher_send_command (command);
|
|
|
|
|
g_free (command);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-19 13:49:52 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
2012-04-18 23:21:13 +02:00
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
/* GTestDBus object implementation */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SECTION:gtestdbus
|
|
|
|
|
* @short_description: D-Bus testing helper
|
|
|
|
|
* @include: gio/gio.h
|
|
|
|
|
*
|
2012-10-09 10:30:06 +02:00
|
|
|
|
* A helper class for testing code which uses D-Bus without touching the user's
|
|
|
|
|
* session bus.
|
2013-01-16 08:26:08 +01:00
|
|
|
|
*
|
2014-02-01 16:48:36 +01:00
|
|
|
|
* Note that #GTestDBus modifies the user’s environment, calling setenv().
|
|
|
|
|
* This is not thread-safe, so all #GTestDBus calls should be completed before
|
2013-12-03 09:27:05 +01:00
|
|
|
|
* threads are spawned, or should have appropriate locking to ensure no access
|
|
|
|
|
* conflicts to environment variables shared between #GTestDBus and other
|
|
|
|
|
* threads.
|
|
|
|
|
*
|
2014-02-01 16:48:36 +01:00
|
|
|
|
* ## Creating unit tests using GTestDBus
|
|
|
|
|
*
|
|
|
|
|
* Testing of D-Bus services can be tricky because normally we only ever run
|
|
|
|
|
* D-Bus services over an existing instance of the D-Bus daemon thus we
|
|
|
|
|
* usually don't activate D-Bus services that are not yet installed into the
|
|
|
|
|
* target system. The #GTestDBus object makes this easier for us by taking care
|
|
|
|
|
* of the lower level tasks such as running a private D-Bus daemon and looking
|
|
|
|
|
* up uninstalled services in customizable locations, typically in your source
|
|
|
|
|
* code tree.
|
|
|
|
|
*
|
|
|
|
|
* The first thing you will need is a separate service description file for the
|
2014-02-06 02:17:46 +01:00
|
|
|
|
* D-Bus daemon. Typically a `services` subdirectory of your `tests` directory
|
|
|
|
|
* is a good place to put this file.
|
2014-02-01 16:48:36 +01:00
|
|
|
|
*
|
|
|
|
|
* The service file should list your service along with an absolute path to the
|
|
|
|
|
* uninstalled service executable in your source tree. Using autotools we would
|
2014-02-06 02:17:46 +01:00
|
|
|
|
* achieve this by adding a file such as `my-server.service.in` in the services
|
|
|
|
|
* directory and have it processed by configure.
|
2014-02-01 16:48:36 +01:00
|
|
|
|
* |[
|
2013-01-16 08:26:08 +01:00
|
|
|
|
* [D-BUS Service]
|
|
|
|
|
* Name=org.gtk.GDBus.Examples.ObjectManager
|
|
|
|
|
* Exec=@abs_top_builddir@/gio/tests/gdbus-example-objectmanager-server
|
2014-02-01 16:48:36 +01:00
|
|
|
|
* ]|
|
|
|
|
|
* You will also need to indicate this service directory in your test
|
|
|
|
|
* fixtures, so you will need to pass the path while compiling your
|
|
|
|
|
* test cases. Typically this is done with autotools with an added
|
|
|
|
|
* preprocessor flag specified to compile your tests such as:
|
|
|
|
|
* |[
|
2013-01-16 08:26:08 +01:00
|
|
|
|
* -DTEST_SERVICES=\""$(abs_top_builddir)/tests/services"\"
|
2014-02-01 16:48:36 +01:00
|
|
|
|
* ]|
|
2013-01-16 08:26:08 +01:00
|
|
|
|
* Once you have a service definition file which is local to your source tree,
|
2014-02-01 16:48:36 +01:00
|
|
|
|
* you can proceed to set up a GTest fixture using the #GTestDBus scaffolding.
|
|
|
|
|
*
|
2014-02-02 02:41:47 +01:00
|
|
|
|
* An example of a test fixture for D-Bus services can be found
|
2014-02-06 03:23:28 +01:00
|
|
|
|
* here:
|
|
|
|
|
* [gdbus-test-fixture.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-test-fixture.c)
|
2014-02-02 02:41:47 +01:00
|
|
|
|
*
|
2014-02-01 16:48:36 +01:00
|
|
|
|
* Note that these examples only deal with isolating the D-Bus aspect of your
|
|
|
|
|
* service. To successfully run isolated unit tests on your service you may need
|
|
|
|
|
* some additional modifications to your test case fixture. For example; if your
|
|
|
|
|
* service uses GSettings and installs a schema then it is important that your test service
|
|
|
|
|
* not load the schema in the ordinary installed location (chances are that your service
|
|
|
|
|
* and schema files are not yet installed, or worse; there is an older version of the
|
|
|
|
|
* schema file sitting in the install location).
|
|
|
|
|
*
|
2014-02-06 01:32:41 +01:00
|
|
|
|
* Most of the time we can work around these obstacles using the
|
|
|
|
|
* environment. Since the environment is inherited by the D-Bus daemon
|
|
|
|
|
* created by #GTestDBus and then in turn inherited by any services the
|
|
|
|
|
* D-Bus daemon activates, using the setup routine for your fixture is
|
|
|
|
|
* a practical place to help sandbox your runtime environment. For the
|
|
|
|
|
* rather typical GSettings case we can work around this by setting
|
|
|
|
|
* `GSETTINGS_SCHEMA_DIR` to the in tree directory holding your schemas
|
|
|
|
|
* in the above fixture_setup() routine.
|
2014-02-01 16:48:36 +01:00
|
|
|
|
*
|
|
|
|
|
* The GSettings schemas need to be locally pre-compiled for this to work. This can be achieved
|
|
|
|
|
* by compiling the schemas locally as a step before running test cases, an autotools setup might
|
|
|
|
|
* do the following in the directory holding schemas:
|
|
|
|
|
* |[
|
2013-01-16 08:26:08 +01:00
|
|
|
|
* all-am:
|
|
|
|
|
* $(GLIB_COMPILE_SCHEMAS) .
|
|
|
|
|
*
|
|
|
|
|
* CLEANFILES += gschemas.compiled
|
2014-02-01 16:48:36 +01:00
|
|
|
|
* ]|
|
2012-04-18 23:21:13 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
typedef struct _GTestDBusClass GTestDBusClass;
|
|
|
|
|
typedef struct _GTestDBusPrivate GTestDBusPrivate;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GTestDBus:
|
|
|
|
|
*
|
|
|
|
|
* The #GTestDBus structure contains only private data and
|
|
|
|
|
* should only be accessed using the provided API.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.34
|
|
|
|
|
*/
|
|
|
|
|
struct _GTestDBus {
|
|
|
|
|
GObject parent;
|
|
|
|
|
|
|
|
|
|
GTestDBusPrivate *priv;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct _GTestDBusClass {
|
|
|
|
|
GObjectClass parent_class;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct _GTestDBusPrivate
|
|
|
|
|
{
|
|
|
|
|
GTestDBusFlags flags;
|
|
|
|
|
GPtrArray *service_dirs;
|
|
|
|
|
GPid bus_pid;
|
2014-06-22 00:01:08 +02:00
|
|
|
|
gint bus_stdout_fd;
|
2012-04-18 23:21:13 +02:00
|
|
|
|
gchar *bus_address;
|
|
|
|
|
gboolean up;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
PROP_0,
|
|
|
|
|
PROP_FLAGS,
|
|
|
|
|
};
|
|
|
|
|
|
2013-06-11 01:29:58 +02:00
|
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GTestDBus, g_test_dbus, G_TYPE_OBJECT)
|
2012-04-18 23:21:13 +02:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_test_dbus_init (GTestDBus *self)
|
|
|
|
|
{
|
2013-06-24 16:43:04 +02:00
|
|
|
|
self->priv = g_test_dbus_get_instance_private (self);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
self->priv->service_dirs = g_ptr_array_new_with_free_func (g_free);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_test_dbus_dispose (GObject *object)
|
|
|
|
|
{
|
|
|
|
|
GTestDBus *self = (GTestDBus *) object;
|
|
|
|
|
|
|
|
|
|
if (self->priv->up)
|
|
|
|
|
g_test_dbus_down (self);
|
|
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (g_test_dbus_parent_class)->dispose (object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_test_dbus_finalize (GObject *object)
|
|
|
|
|
{
|
|
|
|
|
GTestDBus *self = (GTestDBus *) object;
|
|
|
|
|
|
|
|
|
|
g_ptr_array_unref (self->priv->service_dirs);
|
|
|
|
|
g_free (self->priv->bus_address);
|
|
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (g_test_dbus_parent_class)->finalize (object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_test_dbus_get_property (GObject *object,
|
|
|
|
|
guint property_id,
|
|
|
|
|
GValue *value,
|
|
|
|
|
GParamSpec *pspec)
|
|
|
|
|
{
|
|
|
|
|
GTestDBus *self = (GTestDBus *) object;
|
|
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
|
{
|
|
|
|
|
case PROP_FLAGS:
|
|
|
|
|
g_value_set_flags (value, g_test_dbus_get_flags (self));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_test_dbus_set_property (GObject *object,
|
|
|
|
|
guint property_id,
|
|
|
|
|
const GValue *value,
|
|
|
|
|
GParamSpec *pspec)
|
|
|
|
|
{
|
|
|
|
|
GTestDBus *self = (GTestDBus *) object;
|
|
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
|
{
|
|
|
|
|
case PROP_FLAGS:
|
|
|
|
|
self->priv->flags = g_value_get_flags (value);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_test_dbus_class_init (GTestDBusClass *klass)
|
|
|
|
|
{
|
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
|
|
object_class->dispose = g_test_dbus_dispose;
|
|
|
|
|
object_class->finalize = g_test_dbus_finalize;
|
|
|
|
|
object_class->get_property = g_test_dbus_get_property;
|
|
|
|
|
object_class->set_property = g_test_dbus_set_property;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GTestDBus:flags:
|
|
|
|
|
*
|
2013-11-12 14:14:19 +01:00
|
|
|
|
* #GTestDBusFlags specifying the behaviour of the D-Bus session.
|
2012-04-18 23:21:13 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.34
|
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (object_class, PROP_FLAGS,
|
|
|
|
|
g_param_spec_flags ("flags",
|
2013-11-12 14:14:19 +01:00
|
|
|
|
P_("D-Bus session flags"),
|
|
|
|
|
P_("Flags specifying the behaviour of the D-Bus session"),
|
2012-04-18 23:21:13 +02:00
|
|
|
|
G_TYPE_TEST_DBUS_FLAGS, G_TEST_DBUS_NONE,
|
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-06 18:08:21 +02:00
|
|
|
|
static gchar *
|
2012-04-18 23:21:13 +02:00
|
|
|
|
write_config_file (GTestDBus *self)
|
|
|
|
|
{
|
|
|
|
|
GString *contents;
|
2012-08-06 18:08:21 +02:00
|
|
|
|
gint fd;
|
2012-04-18 23:21:13 +02:00
|
|
|
|
guint i;
|
|
|
|
|
GError *error = NULL;
|
2012-08-06 18:08:21 +02:00
|
|
|
|
gchar *path = NULL;
|
2012-04-18 23:21:13 +02:00
|
|
|
|
|
2012-08-06 18:08:21 +02:00
|
|
|
|
fd = g_file_open_tmp ("g-test-dbus-XXXXXX", &path, &error);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
|
|
contents = g_string_new (NULL);
|
|
|
|
|
g_string_append (contents,
|
|
|
|
|
"<busconfig>\n"
|
|
|
|
|
" <type>session</type>\n"
|
2012-04-19 13:49:52 +02:00
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
|
" <listen>nonce-tcp:</listen>\n"
|
|
|
|
|
#else
|
|
|
|
|
" <listen>unix:tmpdir=/tmp</listen>\n"
|
|
|
|
|
#endif
|
|
|
|
|
);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < self->priv->service_dirs->len; i++)
|
|
|
|
|
{
|
2013-11-12 14:09:56 +01:00
|
|
|
|
const gchar *dir_path = g_ptr_array_index (self->priv->service_dirs, i);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
|
|
|
|
|
g_string_append_printf (contents,
|
2013-11-12 14:09:56 +01:00
|
|
|
|
" <servicedir>%s</servicedir>\n", dir_path);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_string_append (contents,
|
|
|
|
|
" <policy context=\"default\">\n"
|
|
|
|
|
" <!-- Allow everything to be sent -->\n"
|
|
|
|
|
" <allow send_destination=\"*\" eavesdrop=\"true\"/>\n"
|
|
|
|
|
" <!-- Allow everything to be received -->\n"
|
|
|
|
|
" <allow eavesdrop=\"true\"/>\n"
|
|
|
|
|
" <!-- Allow anyone to own anything -->\n"
|
|
|
|
|
" <allow own=\"*\"/>\n"
|
|
|
|
|
" </policy>\n"
|
|
|
|
|
"</busconfig>\n");
|
|
|
|
|
|
2014-03-18 05:54:10 +01:00
|
|
|
|
close (fd);
|
2012-08-06 18:08:21 +02:00
|
|
|
|
g_file_set_contents (path, contents->str, contents->len, &error);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
|
|
g_string_free (contents, TRUE);
|
|
|
|
|
|
2012-08-06 18:08:21 +02:00
|
|
|
|
return path;
|
2012-04-18 23:21:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
start_daemon (GTestDBus *self)
|
|
|
|
|
{
|
2013-11-12 14:10:33 +01:00
|
|
|
|
const gchar *argv[] = {"dbus-daemon", "--print-address", "--config-file=foo", NULL};
|
2012-04-18 23:21:13 +02:00
|
|
|
|
gchar *config_path;
|
|
|
|
|
gchar *config_arg;
|
|
|
|
|
GIOChannel *channel;
|
2014-06-22 00:01:08 +02:00
|
|
|
|
gint stdout_fd2;
|
2012-04-18 23:21:13 +02:00
|
|
|
|
gsize termpos;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
2012-04-19 11:01:24 +02:00
|
|
|
|
if (g_getenv ("G_TEST_DBUS_DAEMON") != NULL)
|
2012-04-30 12:31:20 +02:00
|
|
|
|
argv[0] = (gchar *)g_getenv ("G_TEST_DBUS_DAEMON");
|
2012-04-19 11:01:24 +02:00
|
|
|
|
|
2012-04-18 23:21:13 +02:00
|
|
|
|
/* Write config file and set its path in argv */
|
2012-08-06 18:08:21 +02:00
|
|
|
|
config_path = write_config_file (self);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
config_arg = g_strdup_printf ("--config-file=%s", config_path);
|
|
|
|
|
argv[2] = config_arg;
|
|
|
|
|
|
|
|
|
|
/* Spawn dbus-daemon */
|
|
|
|
|
g_spawn_async_with_pipes (NULL,
|
2013-11-12 14:10:33 +01:00
|
|
|
|
(gchar **) argv,
|
2012-04-18 23:21:13 +02:00
|
|
|
|
NULL,
|
2012-04-19 13:49:52 +02:00
|
|
|
|
#ifdef G_OS_WIN32
|
2012-04-19 23:13:52 +02:00
|
|
|
|
/* We Need this to get the pid returned on win32 */
|
|
|
|
|
G_SPAWN_DO_NOT_REAP_CHILD |
|
2012-04-19 13:49:52 +02:00
|
|
|
|
#endif
|
2012-04-19 23:13:52 +02:00
|
|
|
|
G_SPAWN_SEARCH_PATH,
|
2012-04-18 23:21:13 +02:00
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
&self->priv->bus_pid,
|
|
|
|
|
NULL,
|
2014-06-22 00:01:08 +02:00
|
|
|
|
&self->priv->bus_stdout_fd,
|
2012-04-18 23:21:13 +02:00
|
|
|
|
NULL,
|
|
|
|
|
&error);
|
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
|
|
_g_test_watcher_add_pid (self->priv->bus_pid);
|
|
|
|
|
|
2014-06-22 00:01:08 +02:00
|
|
|
|
/* Read bus address from daemon' stdout. We have to be careful to avoid
|
|
|
|
|
* closing the FD, as it is passed to any D-Bus service activated processes,
|
|
|
|
|
* and if we close it, they will get a SIGPIPE and die when they try to write
|
|
|
|
|
* to their stdout. */
|
|
|
|
|
stdout_fd2 = dup (self->priv->bus_stdout_fd);
|
|
|
|
|
g_assert_cmpint (stdout_fd2, >=, 0);
|
|
|
|
|
channel = g_io_channel_unix_new (stdout_fd2);
|
|
|
|
|
|
2012-04-18 23:21:13 +02:00
|
|
|
|
g_io_channel_read_line (channel, &self->priv->bus_address, NULL,
|
|
|
|
|
&termpos, &error);
|
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
self->priv->bus_address[termpos] = '\0';
|
|
|
|
|
|
|
|
|
|
/* start dbus-monitor */
|
|
|
|
|
if (g_getenv ("G_DBUS_MONITOR") != NULL)
|
|
|
|
|
{
|
|
|
|
|
gchar *command;
|
|
|
|
|
|
|
|
|
|
command = g_strdup_printf ("dbus-monitor --address %s",
|
|
|
|
|
self->priv->bus_address);
|
|
|
|
|
g_spawn_command_line_async (command, NULL);
|
|
|
|
|
g_free (command);
|
|
|
|
|
|
2012-06-19 06:04:28 +02:00
|
|
|
|
g_usleep (500 * 1000);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Cleanup */
|
|
|
|
|
g_io_channel_shutdown (channel, FALSE, &error);
|
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
g_io_channel_unref (channel);
|
|
|
|
|
|
2012-06-26 02:46:28 +02:00
|
|
|
|
/* Don't use g_file_delete since it calls into gvfs */
|
|
|
|
|
if (g_unlink (config_path) != 0)
|
|
|
|
|
g_assert_not_reached ();
|
2012-04-18 23:21:13 +02:00
|
|
|
|
|
|
|
|
|
g_free (config_path);
|
|
|
|
|
g_free (config_arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
stop_daemon (GTestDBus *self)
|
|
|
|
|
{
|
2012-04-19 13:49:52 +02:00
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
|
if (!TerminateProcess (self->priv->bus_pid, 0))
|
|
|
|
|
g_warning ("Can't terminate process: %s", g_win32_error_message (GetLastError()));
|
|
|
|
|
#else
|
2012-04-18 23:21:13 +02:00
|
|
|
|
kill (self->priv->bus_pid, SIGTERM);
|
2012-04-19 13:49:52 +02:00
|
|
|
|
#endif
|
2012-04-18 23:21:13 +02:00
|
|
|
|
_g_test_watcher_remove_pid (self->priv->bus_pid);
|
2012-04-19 13:49:52 +02:00
|
|
|
|
g_spawn_close_pid (self->priv->bus_pid);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
self->priv->bus_pid = 0;
|
2014-06-22 00:01:08 +02:00
|
|
|
|
close (self->priv->bus_stdout_fd);
|
|
|
|
|
self->priv->bus_stdout_fd = -1;
|
2012-04-18 23:21:13 +02:00
|
|
|
|
|
|
|
|
|
g_free (self->priv->bus_address);
|
|
|
|
|
self->priv->bus_address = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_test_dbus_new:
|
|
|
|
|
* @flags: a #GTestDBusFlags
|
|
|
|
|
*
|
|
|
|
|
* Create a new #GTestDBus object.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full): a new #GTestDBus.
|
|
|
|
|
*/
|
|
|
|
|
GTestDBus *
|
|
|
|
|
g_test_dbus_new (GTestDBusFlags flags)
|
|
|
|
|
{
|
|
|
|
|
return g_object_new (G_TYPE_TEST_DBUS,
|
|
|
|
|
"flags", flags,
|
|
|
|
|
NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_test_dbus_get_flags:
|
|
|
|
|
* @self: a #GTestDBus
|
|
|
|
|
*
|
2013-11-12 14:14:19 +01:00
|
|
|
|
* Get the flags of the #GTestDBus object.
|
2012-09-24 02:14:27 +02:00
|
|
|
|
*
|
2012-04-18 23:21:13 +02:00
|
|
|
|
* Returns: the value of #GTestDBus:flags property
|
|
|
|
|
*/
|
|
|
|
|
GTestDBusFlags
|
|
|
|
|
g_test_dbus_get_flags (GTestDBus *self)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_TEST_DBUS (self), G_TEST_DBUS_NONE);
|
|
|
|
|
|
|
|
|
|
return self->priv->flags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_test_dbus_get_bus_address:
|
|
|
|
|
* @self: a #GTestDBus
|
|
|
|
|
*
|
2013-11-12 14:14:19 +01:00
|
|
|
|
* Get the address on which dbus-daemon is running. If g_test_dbus_up() has not
|
2012-04-18 23:21:13 +02:00
|
|
|
|
* been called yet, %NULL is returned. This can be used with
|
2013-11-12 14:14:19 +01:00
|
|
|
|
* g_dbus_connection_new_for_address().
|
2012-04-18 23:21:13 +02:00
|
|
|
|
*
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* Returns: (nullable): the address of the bus, or %NULL.
|
2012-04-18 23:21:13 +02:00
|
|
|
|
*/
|
|
|
|
|
const gchar *
|
|
|
|
|
g_test_dbus_get_bus_address (GTestDBus *self)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_TEST_DBUS (self), NULL);
|
|
|
|
|
|
|
|
|
|
return self->priv->bus_address;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_test_dbus_add_service_dir:
|
|
|
|
|
* @self: a #GTestDBus
|
|
|
|
|
* @path: path to a directory containing .service files
|
|
|
|
|
*
|
2013-11-12 14:14:19 +01:00
|
|
|
|
* Add a path where dbus-daemon will look up .service files. This can't be
|
2012-04-18 23:21:13 +02:00
|
|
|
|
* called after g_test_dbus_up().
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_test_dbus_add_service_dir (GTestDBus *self,
|
|
|
|
|
const gchar *path)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (G_IS_TEST_DBUS (self));
|
|
|
|
|
g_return_if_fail (self->priv->bus_address == NULL);
|
|
|
|
|
|
|
|
|
|
g_ptr_array_add (self->priv->service_dirs, g_strdup (path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_test_dbus_up:
|
|
|
|
|
* @self: a #GTestDBus
|
|
|
|
|
*
|
|
|
|
|
* Start a dbus-daemon instance and set DBUS_SESSION_BUS_ADDRESS. After this
|
2012-10-09 10:30:06 +02:00
|
|
|
|
* call, it is safe for unit tests to start sending messages on the session bus.
|
2012-04-18 23:21:13 +02:00
|
|
|
|
*
|
|
|
|
|
* If this function is called from setup callback of g_test_add(),
|
|
|
|
|
* g_test_dbus_down() must be called in its teardown callback.
|
|
|
|
|
*
|
|
|
|
|
* If this function is called from unit test's main(), then g_test_dbus_down()
|
|
|
|
|
* must be called after g_test_run().
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_test_dbus_up (GTestDBus *self)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (G_IS_TEST_DBUS (self));
|
|
|
|
|
g_return_if_fail (self->priv->bus_address == NULL);
|
|
|
|
|
g_return_if_fail (!self->priv->up);
|
|
|
|
|
|
|
|
|
|
start_daemon (self);
|
|
|
|
|
|
2013-10-28 19:54:22 +01:00
|
|
|
|
g_test_dbus_unset ();
|
2012-04-18 23:21:13 +02:00
|
|
|
|
g_setenv ("DBUS_SESSION_BUS_ADDRESS", self->priv->bus_address, TRUE);
|
|
|
|
|
self->priv->up = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_test_dbus_stop:
|
|
|
|
|
* @self: a #GTestDBus
|
|
|
|
|
*
|
|
|
|
|
* Stop the session bus started by g_test_dbus_up().
|
|
|
|
|
*
|
|
|
|
|
* Unlike g_test_dbus_down(), this won't verify the #GDBusConnection
|
|
|
|
|
* singleton returned by g_bus_get() or g_bus_get_sync() is destroyed. Unit
|
|
|
|
|
* tests wanting to verify behaviour after the session bus has been stopped
|
|
|
|
|
* can use this function but should still call g_test_dbus_down() when done.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_test_dbus_stop (GTestDBus *self)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (G_IS_TEST_DBUS (self));
|
|
|
|
|
g_return_if_fail (self->priv->bus_address != NULL);
|
|
|
|
|
|
|
|
|
|
stop_daemon (self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_test_dbus_down:
|
|
|
|
|
* @self: a #GTestDBus
|
|
|
|
|
*
|
|
|
|
|
* Stop the session bus started by g_test_dbus_up().
|
|
|
|
|
*
|
|
|
|
|
* This will wait for the singleton returned by g_bus_get() or g_bus_get_sync()
|
|
|
|
|
* is destroyed. This is done to ensure that the next unit test won't get a
|
|
|
|
|
* leaked singleton from this test.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_test_dbus_down (GTestDBus *self)
|
|
|
|
|
{
|
|
|
|
|
GDBusConnection *connection;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_TEST_DBUS (self));
|
|
|
|
|
g_return_if_fail (self->priv->up);
|
|
|
|
|
|
|
|
|
|
connection = _g_bus_get_singleton_if_exists (G_BUS_TYPE_SESSION);
|
|
|
|
|
if (connection != NULL)
|
|
|
|
|
g_dbus_connection_set_exit_on_close (connection, FALSE);
|
|
|
|
|
|
|
|
|
|
if (self->priv->bus_address != NULL)
|
|
|
|
|
stop_daemon (self);
|
|
|
|
|
|
|
|
|
|
if (connection != NULL)
|
2013-11-09 20:18:34 +01:00
|
|
|
|
_g_object_dispose_and_wait_weak_notify (connection);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
|
2013-10-28 19:54:22 +01:00
|
|
|
|
g_test_dbus_unset ();
|
2018-04-04 09:35:39 +02:00
|
|
|
|
_g_bus_forget_singleton (G_BUS_TYPE_SESSION);
|
2012-04-18 23:21:13 +02:00
|
|
|
|
self->priv->up = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_test_dbus_unset:
|
|
|
|
|
*
|
|
|
|
|
* Unset DISPLAY and DBUS_SESSION_BUS_ADDRESS env variables to ensure the test
|
|
|
|
|
* won't use user's session bus.
|
|
|
|
|
*
|
|
|
|
|
* This is useful for unit tests that want to verify behaviour when no session
|
|
|
|
|
* bus is running. It is not necessary to call this if unit test already calls
|
|
|
|
|
* g_test_dbus_up() before acquiring the session bus.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_test_dbus_unset (void)
|
|
|
|
|
{
|
|
|
|
|
g_unsetenv ("DISPLAY");
|
|
|
|
|
g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
|
2013-10-28 19:54:22 +01:00
|
|
|
|
g_unsetenv ("DBUS_STARTER_ADDRESS");
|
|
|
|
|
g_unsetenv ("DBUS_STARTER_BUS_TYPE");
|
2015-04-15 18:57:29 +02:00
|
|
|
|
/* avoid using XDG_RUNTIME_DIR/bus */
|
|
|
|
|
g_unsetenv ("XDG_RUNTIME_DIR");
|
2012-04-18 23:21:13 +02:00
|
|
|
|
}
|