Revert "Add GTestDBus object"

This reverts commit 1b5f70b5b0.
This commit is contained in:
David Zeuthen 2012-04-18 13:47:51 -04:00
parent 9dce93514e
commit a6f83d73e5
30 changed files with 469 additions and 838 deletions

View File

@ -230,10 +230,6 @@
<xi:include href="xml/gdbus-codegen.xml"/> <xi:include href="xml/gdbus-codegen.xml"/>
<xi:include href="gresource.xml"/> <xi:include href="gresource.xml"/>
</chapter> </chapter>
<chapter id="testing">
<title>GIO Testing</title>
<xi:include href="xml/gtestdbus.xml"/>
</chapter>
</part> </part>
<part id="migrating"> <part id="migrating">

View File

@ -3870,21 +3870,3 @@ g_resource_flags_get_type
g_resource_lookup_flags_get_type g_resource_lookup_flags_get_type
g_resource_error_quark g_resource_error_quark
</SECTION> </SECTION>
<SECTION>
<FILE>gtestdbus</FILE>
<TITLE>GTestDBus</TITLE>
GTestDBus
GTestDBusFlags
g_test_dbus_new
g_test_dbus_get_flags
g_test_dbus_get_bus_address
g_test_dbus_add_service_dir
g_test_dbus_up
g_test_dbus_stop
g_test_dbus_down
g_test_dbus_unset
<SUBSECTION Private>
g_test_dbus_get_type
g_test_dbus_flags_get_type
</SECTION>

View File

@ -133,5 +133,3 @@ g_menu_attribute_iter_get_type
g_menu_link_iter_get_type g_menu_link_iter_get_type
g_menu_get_type g_menu_get_type
g_menu_item_get_type g_menu_item_get_type
g_test_dbus_get_type
g_test_dbus_flags_get_type

View File

@ -65,7 +65,6 @@ gdbus_headers = \
gdbusobjectmanager.h \ gdbusobjectmanager.h \
gdbusobjectmanagerclient.h \ gdbusobjectmanagerclient.h \
gdbusobjectmanagerserver.h \ gdbusobjectmanagerserver.h \
gtestdbus.h \
$(NULL) $(NULL)
gdbus_sources = \ gdbus_sources = \
@ -96,7 +95,6 @@ gdbus_sources = \
gdbusobjectmanagerclient.h gdbusobjectmanagerclient.c \ gdbusobjectmanagerclient.h gdbusobjectmanagerclient.c \
gdbusobjectmanagerserver.h gdbusobjectmanagerserver.c \ gdbusobjectmanagerserver.h gdbusobjectmanagerserver.c \
gdbus-tests.h gdbus-tests.c \ gdbus-tests.h gdbus-tests.c \
gtestdbus.h gtestdbus.c \
$(NULL) $(NULL)
settings_headers = \ settings_headers = \

View File

@ -124,7 +124,6 @@
#include <gio/gsrvtarget.h> #include <gio/gsrvtarget.h>
#include <gio/gtcpconnection.h> #include <gio/gtcpconnection.h>
#include <gio/gtcpwrapperconnection.h> #include <gio/gtcpwrapperconnection.h>
#include <gio/gtestdbus.h>
#include <gio/gthemedicon.h> #include <gio/gthemedicon.h>
#include <gio/gthreadedsocketservice.h> #include <gio/gthreadedsocketservice.h>
#include <gio/gtlsbackend.h> #include <gio/gtlsbackend.h>

View File

@ -1721,13 +1721,3 @@ g_resolver_lookup_records
g_resolver_lookup_records_async g_resolver_lookup_records_async
g_resolver_lookup_records_finish g_resolver_lookup_records_finish
g_resolver_record_type_get_type g_resolver_record_type_get_type
g_test_dbus_add_service_dir
g_test_dbus_flags_get_type
g_test_dbus_get_bus_address
g_test_dbus_get_flags
g_test_dbus_get_type
g_test_dbus_new
g_test_dbus_down
g_test_dbus_stop
g_test_dbus_up
g_test_dbus_unset

View File

@ -1640,21 +1640,6 @@ typedef enum {
G_SOCKET_CLIENT_COMPLETE G_SOCKET_CLIENT_COMPLETE
} GSocketClientEvent; } GSocketClientEvent;
/**
* GTestDBusFlags:
* @G_TEST_DBUS_NONE: No flags.
*
* Flags to define future #GTestDBus behaviour.
*
* Since: 2.34
*/
typedef enum {
G_TEST_DBUS_NONE = 0,
/* FIXME: Must have a value, otherwise mkenum generate GEnum instead of GFlags */
G_TEST_DBUS_FOO = (1 << 0),
} GTestDBusFlags;
G_END_DECLS G_END_DECLS
#endif /* __GIO_ENUMS_H__ */ #endif /* __GIO_ENUMS_H__ */

View File

@ -465,8 +465,6 @@ typedef GType (*GDBusProxyTypeFunc) (GDBusObjectManagerClient *manager,
const gchar *interface_name, const gchar *interface_name,
gpointer user_data); gpointer user_data);
typedef struct _GTestDBus GTestDBus;
G_END_DECLS G_END_DECLS
#endif /* __GIO_TYPES_H__ */ #endif /* __GIO_TYPES_H__ */

View File

@ -1,650 +0,0 @@
/* 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
* version 2 of the License, or (at your option) any later version.
*
* 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
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: David Zeuthen <davidz@redhat.com>
* Xavier Claessens <xavier.claessens@collabora.co.uk>
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <glib.h>
#include "gdbus-tests.h"
#include "gdbusconnection.h"
#include "gdbusprivate.h"
#include "gfile.h"
#include "gioenumtypes.h"
#include "gtestdbus.h"
#include "glibintl.h"
/* -------------------------------------------------------------------------- */
/* Utilities to cleanup the mess in the case unit test process crash */
#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;
if (fds[0].revents == G_IO_HUP)
{
/* 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);
g_print ("cleaning up pid %d\n", pid);
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;
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)
{
g_warning ("pipe() failed: %m");
g_assert_not_reached ();
}
switch (fork ())
{
case -1:
g_warning ("fork() failed: %m");
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);
}
/* -------------------------------------------------------------------------- */
/* GTestDBus object implementation */
/**
* SECTION:gtestdbus
* @short_description: D-Bus testing helper
* @include: gio/gio.h
*
* Helper to test D-Bus code wihtout messing up with user' session bus.
*
* Since: 2.34
*/
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;
};
/**
* GTestDBusClass:
*
* Class structure for #GTestDBus.
*
* Since: 2.34
*/
struct _GTestDBusClass {
GObjectClass parent_class;
};
struct _GTestDBusPrivate
{
GTestDBusFlags flags;
GPtrArray *service_dirs;
GPid bus_pid;
gchar *bus_address;
gboolean up;
};
enum
{
PROP_0,
PROP_FLAGS,
};
G_DEFINE_TYPE (GTestDBus, g_test_dbus, G_TYPE_OBJECT)
static void
g_test_dbus_init (GTestDBus *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self), G_TYPE_TEST_DBUS,
GTestDBusPrivate);
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;
g_type_class_add_private (object_class, sizeof (GTestDBusPrivate));
/**
* GTestDBus:flags:
*
* #GTestDBusFlags specifying the behaviour of the dbus session
*
* Since: 2.34
*/
g_object_class_install_property (object_class, PROP_FLAGS,
g_param_spec_flags ("flags",
P_("dbus session flags"),
P_("Flags specifying the behaviour of the dbus session"),
G_TYPE_TEST_DBUS_FLAGS, G_TEST_DBUS_NONE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
}
static GFile *
write_config_file (GTestDBus *self)
{
GString *contents;
GFile *file;
GFileIOStream *iostream;
guint i;
GError *error = NULL;
file = g_file_new_tmp ("g-test-dbus-XXXXXX", &iostream, &error);
g_assert_no_error (error);
g_object_unref (iostream);
contents = g_string_new (NULL);
g_string_append (contents,
"<busconfig>\n"
" <type>session</type>\n"
" <listen>unix:tmpdir=/tmp</listen>\n");
for (i = 0; i < self->priv->service_dirs->len; i++)
{
const gchar *path = g_ptr_array_index (self->priv->service_dirs, i);
g_string_append_printf (contents,
" <servicedir>%s</servicedir>\n", path);
}
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");
g_file_replace_contents (file, contents->str, contents->len,
NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, &error);
g_assert_no_error (error);
g_string_free (contents, TRUE);
return file;
}
static void
start_daemon (GTestDBus *self)
{
gchar *argv[] = {"dbus-daemon", "--print-address", "--config-file=foo", NULL};
GFile *file;
gchar *config_path;
gchar *config_arg;
gint stdout_fd;
GIOChannel *channel;
gsize termpos;
GError *error = NULL;
/* Write config file and set its path in argv */
file = write_config_file (self);
config_path = g_file_get_path (file);
config_arg = g_strdup_printf ("--config-file=%s", config_path);
argv[2] = config_arg;
/* Spawn dbus-daemon */
g_spawn_async_with_pipes (NULL,
argv,
NULL,
G_SPAWN_SEARCH_PATH,
NULL,
NULL,
&self->priv->bus_pid,
NULL,
&stdout_fd,
NULL,
&error);
g_assert_no_error (error);
_g_test_watcher_add_pid (self->priv->bus_pid);
/* Read bus address from daemon' stdout */
channel = g_io_channel_unix_new (stdout_fd);
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);
usleep (500 * 1000);
}
/* Cleanup */
g_io_channel_shutdown (channel, FALSE, &error);
g_assert_no_error (error);
g_io_channel_unref (channel);
g_file_delete (file, NULL, &error);
g_assert_no_error (error);
g_object_unref (file);
g_free (config_path);
g_free (config_arg);
}
static void
stop_daemon (GTestDBus *self)
{
kill (self->priv->bus_pid, SIGTERM);
_g_test_watcher_remove_pid (self->priv->bus_pid);
self->priv->bus_pid = 0;
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
*
* 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
*
* Get the address on which dbus-daemon is running. if g_test_dbus_up() has not
* been called yet, %NULL is returned. This can be used with
* g_dbus_connection_new_for_address()
*
* Returns: the address of the bus, or %NULL.
*/
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
*
* Add a path where dbus-daemon will lookup for .services files. This can't be
* 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
* call, it is safe for unit tests to start sending messages on the session bug.
*
* 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);
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)
{
_g_object_wait_for_single_ref (connection);
g_object_unref (connection);
}
g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
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");
}

View File

@ -1,82 +0,0 @@
/* 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
* version 2 of the License, or (at your option) any later version.
*
* 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
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: David Zeuthen <davidz@redhat.com>
* Xavier Claessens <xavier.claessens@collabora.co.uk>
*/
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
#error "Only <gio/gio.h> can be included directly."
#endif
#ifndef __G_TEST_DBUS_H__
#define __G_TEST_DBUS_H__
#include <gio/giotypes.h>
G_BEGIN_DECLS
#define G_TYPE_TEST_DBUS \
(g_test_dbus_get_type ())
#define G_TEST_DBUS(obj) \
(G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_TEST_DBUS, \
GTestDbus))
#define G_TEST_DBUS_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST ((klass), G_TYPE_TEST_DBUS, \
GTestDBusClass))
#define G_IS_TEST_DBUS(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), G_TYPE_TEST_DBUS))
#define G_IS_TEST_DBUS_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE ((klass), G_TYPE_TEST_DBUS))
#define G_TEST_DBUS_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS ((obj), G_TYPE_TEST_DBUS, \
GTestDBusClass))
GLIB_AVAILABLE_IN_2_34
GType g_test_dbus_get_type (void) G_GNUC_CONST;
GLIB_AVAILABLE_IN_2_34
GTestDBus * g_test_dbus_new (GTestDBusFlags flags);
GLIB_AVAILABLE_IN_2_34
GTestDBusFlags g_test_dbus_get_flags (GTestDBus *self);
GLIB_AVAILABLE_IN_2_34
const gchar * g_test_dbus_get_bus_address (GTestDBus *self);
GLIB_AVAILABLE_IN_2_34
void g_test_dbus_add_service_dir (GTestDBus *self,
const gchar *path);
GLIB_AVAILABLE_IN_2_34
void g_test_dbus_up (GTestDBus *self);
GLIB_AVAILABLE_IN_2_34
void g_test_dbus_stop (GTestDBus *self);
GLIB_AVAILABLE_IN_2_34
void g_test_dbus_down (GTestDBus *self);
GLIB_AVAILABLE_IN_2_34
void g_test_dbus_unset (void);
G_END_DECLS
#endif /* __G_TEST_DBUS_H__ */

View File

@ -712,6 +712,8 @@ main (int argc, char **argv)
g_type_init (); g_type_init ();
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
g_test_add_func ("/actions/basic", test_basic); g_test_add_func ("/actions/basic", test_basic);
g_test_add_func ("/actions/simplegroup", test_simple_group); g_test_add_func ("/actions/simplegroup", test_simple_group);
g_test_add_func ("/actions/stateful", test_stateful); g_test_add_func ("/actions/stateful", test_stateful);

View File

@ -116,8 +116,11 @@ basic (void)
g_main_loop_run (main_loop); g_main_loop_run (main_loop);
g_object_unref (c);
session_bus_down (); session_bus_down ();
_g_object_wait_for_single_ref_do (c);
g_object_unref (c);
g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL);
} }
@ -249,11 +252,14 @@ properties (void)
g_application_quit (G_APPLICATION (app)); g_application_quit (G_APPLICATION (app));
g_object_unref (c);
g_object_unref (app); g_object_unref (app);
g_free (id); g_free (id);
session_bus_down (); session_bus_down ();
_g_object_wait_for_single_ref (c);
g_object_unref (c);
g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL);
} }
static void static void
@ -351,11 +357,15 @@ test_quit (void)
g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL); g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
g_application_run (app, 1, argv); g_application_run (app, 1, argv);
g_object_unref (app); g_object_unref (app);
g_object_unref (c);
g_assert (quit_activated); g_assert (quit_activated);
session_bus_down (); session_bus_down ();
_g_object_wait_for_single_ref (c);
g_object_unref (c);
g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL);
} }
static void static void
@ -413,7 +423,12 @@ main (int argc, char **argv)
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_test_dbus_unset (); /* all the tests use a session bus with a well-known address
* that we can bring up and down using session_bus_up() and
* session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
g_test_add_func ("/gapplication/no-dbus", test_nodbus); g_test_add_func ("/gapplication/no-dbus", test_nodbus);
g_test_add_func ("/gapplication/basic", basic); g_test_add_func ("/gapplication/basic", basic);

View File

@ -269,6 +269,11 @@ main (int argc,
g_type_init (); g_type_init ();
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
/* all the tests use a session bus with a well-known address that we can bring up and down
* using session_bus_up() and session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
session_bus_up (); session_bus_up ();
g_test_add_func ("/gdbus/auth/client/EXTERNAL", auth_client_external); g_test_add_func ("/gdbus/auth/client/EXTERNAL", auth_client_external);

View File

@ -83,7 +83,11 @@ main (int argc,
g_type_init (); g_type_init ();
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_test_dbus_unset (); /* all the tests use a session bus with a well-known address that we can bring up and down
* using session_bus_up() and session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
g_test_add_func ("/gdbus/bz627724", test_bz627724); g_test_add_func ("/gdbus/bz627724", test_bz627724);
return g_test_run(); return g_test_run();

View File

@ -54,7 +54,7 @@ static gboolean
on_timeout (gpointer user_data) on_timeout (gpointer user_data)
{ {
/* tear down bus */ /* tear down bus */
session_bus_stop (); session_bus_down ();
return FALSE; /* remove source */ return FALSE; /* remove source */
} }
@ -119,8 +119,19 @@ main (int argc,
/* all the tests rely on a shared main loop */ /* all the tests rely on a shared main loop */
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
/* all the tests use a session bus with a well-known address that we can bring up and down
* using session_bus_up() and session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
session_bus_up (); session_bus_up ();
/* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
* until one can connect to the bus but that's not how things work right now
*/
usleep (500 * 1000);
/* this is safe; testserver will exit once the bus goes away */ /* this is safe; testserver will exit once the bus goes away */
g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL)); g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));

View File

@ -105,6 +105,7 @@ test_connection_flush (void)
} }
g_dbus_connection_signal_unsubscribe (connection, signal_handler_id); g_dbus_connection_signal_unsubscribe (connection, signal_handler_id);
_g_object_wait_for_single_ref (connection);
g_object_unref (connection); g_object_unref (connection);
session_bus_down (); session_bus_down ();
@ -201,7 +202,11 @@ main (int argc,
/* all the tests rely on a shared main loop */ /* all the tests rely on a shared main loop */
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
g_test_dbus_unset (); /* all the tests use a session bus with a well-known address that we can bring up and down
* using session_bus_up() and session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
g_test_add_func ("/gdbus/connection/flush", test_connection_flush); g_test_add_func ("/gdbus/connection/flush", test_connection_flush);
g_test_add_func ("/gdbus/connection/large_message", test_connection_large_message); g_test_add_func ("/gdbus/connection/large_message", test_connection_large_message);

View File

@ -267,14 +267,12 @@ test_connection_life_cycle (void)
*/ */
g_assert (!g_dbus_connection_is_closed (c)); g_assert (!g_dbus_connection_is_closed (c));
g_dbus_connection_set_exit_on_close (c, FALSE); g_dbus_connection_set_exit_on_close (c, FALSE);
session_bus_stop (); session_bus_down ();
_g_assert_signal_received (c, "closed"); _g_assert_signal_received (c, "closed");
g_assert (g_dbus_connection_is_closed (c)); g_assert (g_dbus_connection_is_closed (c));
_g_object_wait_for_single_ref (c); _g_object_wait_for_single_ref (c);
g_object_unref (c); g_object_unref (c);
session_bus_down ();
} }
/* ---------------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------------- */
@ -471,7 +469,7 @@ test_connection_send (void)
* Check that we get an error when sending to a connection that is disconnected. * Check that we get an error when sending to a connection that is disconnected.
*/ */
g_dbus_connection_set_exit_on_close (c, FALSE); g_dbus_connection_set_exit_on_close (c, FALSE);
session_bus_stop (); session_bus_down ();
_g_assert_signal_received (c, "closed"); _g_assert_signal_received (c, "closed");
g_assert (g_dbus_connection_is_closed (c)); g_assert (g_dbus_connection_is_closed (c));
@ -490,8 +488,6 @@ test_connection_send (void)
_g_object_wait_for_single_ref (c); _g_object_wait_for_single_ref (c);
g_object_unref (c); g_object_unref (c);
session_bus_down ();
} }
/* ---------------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------------- */
@ -709,6 +705,10 @@ test_connection_signals (void)
g_dbus_connection_signal_unsubscribe (c1, s3); g_dbus_connection_signal_unsubscribe (c1, s3);
g_dbus_connection_signal_unsubscribe (c1, s1b); g_dbus_connection_signal_unsubscribe (c1, s1b);
_g_object_wait_for_single_ref (c1);
_g_object_wait_for_single_ref (c2);
_g_object_wait_for_single_ref (c3);
g_object_unref (c1); g_object_unref (c1);
g_object_unref (c2); g_object_unref (c2);
g_object_unref (c3); g_object_unref (c3);
@ -992,6 +992,7 @@ test_connection_filter (void)
g_dbus_connection_remove_filter (c, filter_id); g_dbus_connection_remove_filter (c, filter_id);
_g_object_wait_for_single_ref (c);
g_object_unref (c); g_object_unref (c);
g_object_unref (m); g_object_unref (m);
@ -1045,6 +1046,7 @@ test_connection_basic (void)
g_free (name); g_free (name);
g_free (guid); g_free (guid);
_g_object_wait_for_single_ref (connection);
g_object_unref (connection); g_object_unref (connection);
session_bus_down (); session_bus_down ();
@ -1062,7 +1064,11 @@ main (int argc,
/* all the tests rely on a shared main loop */ /* all the tests rely on a shared main loop */
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
g_test_dbus_unset (); /* all the tests use a session bus with a well-known address that we can bring up and down
* using session_bus_up() and session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
g_test_add_func ("/gdbus/connection/basic", test_connection_basic); g_test_add_func ("/gdbus/connection/basic", test_connection_basic);
g_test_add_func ("/gdbus/connection/life-cycle", test_connection_life_cycle); g_test_add_func ("/gdbus/connection/life-cycle", test_connection_life_cycle);

View File

@ -117,7 +117,11 @@ test_exit_on_close (gconstpointer test_data)
/* all the tests rely on a shared main loop */ /* all the tests rely on a shared main loop */
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
g_test_dbus_unset (); /* all the tests use a session bus with a well-known address that we can bring up and down
* using session_bus_up() and session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
if (g_test_verbose ()) if (g_test_verbose ())
silence = 0; silence = 0;
@ -169,16 +173,12 @@ test_exit_on_close (gconstpointer test_data)
} }
else else
{ {
session_bus_stop (); session_bus_down ();
} }
g_main_loop_run (loop); g_main_loop_run (loop);
/* this is only reached when we turn off exit-on-close */ /* this is only reached when we turn off exit-on-close */
g_main_loop_unref (loop); g_main_loop_unref (loop);
g_object_unref (c);
session_bus_down ();
exit (0); exit (0);
} }

View File

@ -1341,7 +1341,7 @@ test_object_registration (void)
/* To prevent from exiting and attaching a D-Bus tool like D-Feet; uncomment: */ /* To prevent from exiting and attaching a D-Bus tool like D-Feet; uncomment: */
#if 0 #if 0
g_debug ("Point D-feet or other tool at: %s", g_test_dbus_get_temporary_address()); g_debug ("Point D-feet or other tool at: %s", session_bus_get_temporary_address());
g_main_loop_run (loop); g_main_loop_run (loop);
#endif #endif
@ -1537,8 +1537,19 @@ main (int argc,
/* all the tests rely on a shared main loop */ /* all the tests rely on a shared main loop */
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
/* all the tests use a session bus with a well-known address that we can bring up and down
* using session_bus_up() and session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
session_bus_up (); session_bus_up ();
/* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
* until one can connect to the bus but that's not how things work right now
*/
usleep (500 * 1000);
g_test_add_func ("/gdbus/object-registration", test_object_registration); g_test_add_func ("/gdbus/object-registration", test_object_registration);
g_test_add_func ("/gdbus/registered-interfaces", test_registered_interfaces); g_test_add_func ("/gdbus/registered-interfaces", test_registered_interfaces);

View File

@ -110,6 +110,11 @@ test_introspection_parser (void)
session_bus_up (); session_bus_up ();
/* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
* until one can connect to the bus but that's not how things work right now
*/
usleep (500 * 1000);
error = NULL; error = NULL;
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
NULL, NULL,
@ -309,7 +314,11 @@ main (int argc,
/* all the tests rely on a shared main loop */ /* all the tests rely on a shared main loop */
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
g_test_dbus_unset (); /* all the tests use a session bus with a well-known address that we can bring up and down
* using session_bus_up() and session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
g_test_add_func ("/gdbus/introspection-parser", test_introspection_parser); g_test_add_func ("/gdbus/introspection-parser", test_introspection_parser);
g_test_add_func ("/gdbus/introspection-generate", test_generate); g_test_add_func ("/gdbus/introspection-generate", test_generate);

View File

@ -462,7 +462,7 @@ test_bus_own_name (void)
* *
*/ */
data.expect_null_connection = TRUE; data.expect_null_connection = TRUE;
session_bus_stop (); session_bus_down ();
while (data.num_lost != 2) while (data.num_lost != 2)
g_main_loop_run (loop); g_main_loop_run (loop);
g_assert_cmpint (data.num_acquired, ==, 2); g_assert_cmpint (data.num_acquired, ==, 2);
@ -473,8 +473,6 @@ test_bus_own_name (void)
_g_object_wait_for_single_ref (c); _g_object_wait_for_single_ref (c);
g_object_unref (c); g_object_unref (c);
g_object_unref (c2); g_object_unref (c2);
session_bus_down ();
} }
/* ---------------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------------- */
@ -698,7 +696,7 @@ test_bus_watch_name (void)
* Nuke the bus and check that the name vanishes and is lost. * Nuke the bus and check that the name vanishes and is lost.
*/ */
data.expect_null_connection = TRUE; data.expect_null_connection = TRUE;
session_bus_stop (); session_bus_down ();
g_main_loop_run (loop); g_main_loop_run (loop);
g_assert_cmpint (data.num_lost, ==, 1); g_assert_cmpint (data.num_lost, ==, 1);
g_assert_cmpint (data.num_vanished, ==, 2); g_assert_cmpint (data.num_vanished, ==, 2);
@ -709,7 +707,6 @@ test_bus_watch_name (void)
g_bus_unown_name (owner_id); g_bus_unown_name (owner_id);
g_assert_cmpint (data.num_free_func, ==, 2); g_assert_cmpint (data.num_free_func, ==, 2);
session_bus_down ();
} }
/* ---------------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------------- */
@ -775,7 +772,11 @@ main (int argc,
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
g_test_dbus_unset (); /* all the tests use a session bus with a well-known address that we can bring up and down
* using session_bus_up() and session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
g_test_add_func ("/gdbus/validate-names", test_validate_names); g_test_add_func ("/gdbus/validate-names", test_validate_names);
g_test_add_func ("/gdbus/bus-own-name", test_bus_own_name); g_test_add_func ("/gdbus/bus-own-name", test_bus_own_name);

View File

@ -1804,6 +1804,8 @@ main (int argc,
g_type_init (); g_type_init ();
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL); introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
g_assert (introspection_data != NULL); g_assert (introspection_data != NULL);
test_interface_introspection_data = introspection_data->interfaces[0]; test_interface_introspection_data = introspection_data->interfaces[0];

View File

@ -205,6 +205,11 @@ test_proxy (void)
session_bus_up (); session_bus_up ();
/* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
* until one can connect to the bus but that's not how things work right now
*/
usleep (500 * 1000);
loop = g_main_loop_new (NULL, TRUE); loop = g_main_loop_new (NULL, TRUE);
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
@ -235,11 +240,12 @@ int
main (int argc, main (int argc,
char *argv[]) char *argv[])
{ {
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
g_type_init (); g_type_init ();
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_test_dbus_unset ();
g_test_add_func ("/gdbus/proxy/vs-threads", test_proxy); g_test_add_func ("/gdbus/proxy/vs-threads", test_proxy);
return g_test_run(); return g_test_run();

View File

@ -63,6 +63,11 @@ test_proxy_well_known_name (void)
session_bus_up (); session_bus_up ();
/* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
* until one can connect to the bus but that's not how things work right now
*/
usleep (500 * 1000);
error = NULL; error = NULL;
c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
@ -265,7 +270,11 @@ main (int argc,
/* all the tests rely on a shared main loop */ /* all the tests rely on a shared main loop */
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
g_test_dbus_unset (); /* all the tests use a session bus with a well-known address that we can bring up and down
* using session_bus_up() and session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name); g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name);

View File

@ -741,6 +741,11 @@ test_proxy (void)
session_bus_up (); session_bus_up ();
/* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
* until one can connect to the bus but that's not how things work right now
*/
usleep (500 * 1000);
error = NULL; error = NULL;
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
NULL, NULL,
@ -889,7 +894,11 @@ main (int argc,
/* all the tests rely on a shared main loop */ /* all the tests rely on a shared main loop */
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
g_test_dbus_unset (); /* all the tests use a session bus with a well-known address that we can bring up and down
* using session_bus_up() and session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
g_test_add_func ("/gdbus/proxy", test_proxy); g_test_add_func ("/gdbus/proxy", test_proxy);
g_test_add_func ("/gdbus/proxy/no-properties", test_no_properties); g_test_add_func ("/gdbus/proxy/no-properties", test_no_properties);

View File

@ -1,6 +1,6 @@
/* GLib testing framework examples and tests /* GLib testing framework examples and tests
* *
* Copyright (C) 2012 Collabora Ltd. <http://www.collabora.co.uk/> * Copyright (C) 2008-2010 Red Hat, Inc.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -17,33 +17,328 @@
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA. * Boston, MA 02111-1307, USA.
* *
* Author: Xavier Claessens <xavier.claessens@collabora.co.uk> * Author: David Zeuthen <davidz@redhat.com>
*/ */
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <gio/gio.h>
#include "gdbus-sessionbus.h" #include "gdbus-sessionbus.h"
static GTestDBus *singleton = NULL; /* ---------------------------------------------------------------------------------------------------- */
/* Utilities for bringing up and tearing down session message bus instances */
void static void
session_bus_up (void) watch_parent (gint fd)
{ {
g_assert (singleton == NULL); GPollFD fds[1];
singleton = g_test_dbus_new (G_TEST_DBUS_NONE); gint num_events;
g_test_dbus_up (singleton); gchar buf[512];
gssize bytes_read;
GArray *buses_to_kill_array;
fds[0].fd = fd;
fds[0].events = G_IO_HUP | G_IO_IN;
fds[0].revents = 0;
buses_to_kill_array = g_array_new (FALSE, TRUE, sizeof (guint));
do
{
guint pid;
guint n;
num_events = g_poll (fds, 1, -1);
if (num_events == 0)
continue;
if (fds[0].revents == G_IO_HUP)
{
for (n = 0; n < buses_to_kill_array->len; n++)
{
pid = g_array_index (buses_to_kill_array, guint, n);
g_print ("cleaning up bus with pid %d\n", pid);
kill (pid, SIGTERM);
}
g_array_free (buses_to_kill_array, TRUE);
exit (0);
}
//g_debug ("data from parent");
memset (buf, '\0', sizeof buf);
again:
bytes_read = read (fds[0].fd, buf, sizeof buf);
if (bytes_read < 0 && (errno == EAGAIN || errno == EINTR))
goto again;
if (sscanf (buf, "add %d\n", &pid) == 1)
{
g_array_append_val (buses_to_kill_array, pid);
}
else if (sscanf (buf, "remove %d\n", &pid) == 1)
{
for (n = 0; n < buses_to_kill_array->len; n++)
{
if (g_array_index (buses_to_kill_array, guint, n) == pid)
{
g_array_remove_index (buses_to_kill_array, n);
pid = 0;
break;
}
}
if (pid != 0)
{
g_warning ("unknown pid %d to remove", pid);
}
}
else
{
g_warning ("unknown command from parent '%s'", buf);
}
}
while (TRUE);
}
static GHashTable *session_bus_address_to_pid = NULL;
static gint pipe_fds[2];
const gchar *
session_bus_up_with_address (const gchar *given_address)
{
gchar *address;
int stdout_fd;
GError *error;
gchar *argv[] = {"dbus-daemon", "--print-address", "--config-file=foo", NULL};
GPid pid;
gchar buf[512];
ssize_t bytes_read;
gchar *config_file_name;
gint config_file_fd;
GString *config_file_contents;
address = NULL;
error = NULL;
config_file_name = NULL;
config_file_fd = -1;
argv[2] = NULL;
config_file_fd = g_file_open_tmp ("g-dbus-tests-XXXXXX",
&config_file_name,
&error);
if (config_file_fd < 0)
{
g_warning ("Error creating temporary config file: %s", error->message);
g_error_free (error);
goto out;
}
config_file_contents = g_string_new (NULL);
g_string_append (config_file_contents, "<busconfig>\n");
g_string_append (config_file_contents, " <type>session</type>\n");
g_string_append_printf (config_file_contents, " <listen>%s</listen>\n", given_address);
g_string_append (config_file_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");
g_string_append (config_file_contents, "</busconfig>\n");
if (write (config_file_fd, config_file_contents->str, config_file_contents->len) != (gssize) config_file_contents->len)
{
g_warning ("Error writing %d bytes to config file: %m", (gint) config_file_contents->len);
g_string_free (config_file_contents, TRUE);
goto out;
}
g_string_free (config_file_contents, TRUE);
argv[2] = g_strdup_printf ("--config-file=%s", config_file_name);
if (session_bus_address_to_pid == NULL)
{
/* keep a mapping from session bus address to the pid */
session_bus_address_to_pid = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
/* fork a child to clean up session buses when we are killed */
if (pipe (pipe_fds) != 0)
{
g_warning ("pipe() failed: %m");
g_assert_not_reached ();
}
switch (fork ())
{
case -1:
g_warning ("fork() failed: %m");
g_assert_not_reached ();
break;
case 0:
/* child */
close (pipe_fds[1]);
watch_parent (pipe_fds[0]);
break;
default:
/* parent */
close (pipe_fds[0]);
break;
}
//atexit (cleanup_session_buses);
/* TODO: need to handle the cases where we crash */
}
else
{
/* check if we already have a bus running for this address */
if (g_hash_table_lookup (session_bus_address_to_pid, given_address) != NULL)
{
g_warning ("Already have a bus instance for the given address %s", given_address);
goto out;
}
}
if (!g_spawn_async_with_pipes (NULL,
argv,
NULL,
G_SPAWN_SEARCH_PATH,
NULL,
NULL,
&pid,
NULL,
&stdout_fd,
NULL,
&error))
{
g_warning ("Error spawning dbus-daemon: %s", error->message);
g_error_free (error);
goto out;
}
memset (buf, '\0', sizeof buf);
again:
bytes_read = read (stdout_fd, buf, sizeof buf);
if (bytes_read < 0 && (errno == EAGAIN || errno == EINTR))
goto again;
close (stdout_fd);
if (bytes_read == 0 || bytes_read == sizeof buf)
{
g_warning ("Error reading address from dbus daemon, %d bytes read", (gint) bytes_read);
kill (SIGTERM, pid);
goto out;
}
address = g_strdup (buf);
g_strstrip (address);
/* write the pid to the child so it can kill it when we die */
g_snprintf (buf, sizeof buf, "add %d\n", (guint) pid);
write (pipe_fds[1], buf, strlen (buf));
/* start dbus-monitor */
if (g_getenv ("G_DBUS_MONITOR") != NULL)
{
g_spawn_command_line_async ("dbus-monitor --session", NULL);
usleep (500 * 1000);
}
g_hash_table_insert (session_bus_address_to_pid, address, GUINT_TO_POINTER (pid));
out:
if (config_file_fd > 0)
{
if (close (config_file_fd) != 0)
{
g_warning ("Error closing fd for config file %s: %m", config_file_name);
}
g_assert (config_file_name != NULL);
if (unlink (config_file_name) != 0)
{
g_warning ("Error unlinking config file %s: %m", config_file_name);
}
}
g_free (argv[2]);
g_free (config_file_name);
return address;
} }
void void
session_bus_stop (void) session_bus_down_with_address (const gchar *address)
{ {
g_assert (singleton != NULL); gpointer value;
g_test_dbus_stop (singleton); GPid pid;
gchar buf[512];
g_assert (address != NULL);
g_assert (session_bus_address_to_pid != NULL);
value = g_hash_table_lookup (session_bus_address_to_pid, address);
g_assert (value != NULL);
pid = GPOINTER_TO_UINT (g_hash_table_lookup (session_bus_address_to_pid, address));
kill (pid, SIGTERM);
/* write the pid to the child so it won't kill it when we die */
g_snprintf (buf, sizeof buf, "remove %d\n", (guint) pid);
write (pipe_fds[1], buf, strlen (buf));
g_hash_table_remove (session_bus_address_to_pid, address);
}
static gchar *temporary_address = NULL;
static gchar *temporary_address_used_by_bus = NULL;
const gchar *
session_bus_get_temporary_address (void)
{
if (temporary_address == NULL)
{
temporary_address = g_strdup_printf ("unix:path=/tmp/g-dbus-tests-pid-%d", getpid ());
}
return temporary_address;
}
const gchar *
session_bus_up (void)
{
if (temporary_address_used_by_bus != NULL)
{
g_warning ("There is already a session bus up");
goto out;
}
temporary_address_used_by_bus = g_strdup (session_bus_up_with_address (session_bus_get_temporary_address ()));
out:
return temporary_address_used_by_bus;
} }
void void
session_bus_down (void) session_bus_down (void)
{ {
g_assert (singleton != NULL); if (temporary_address_used_by_bus == NULL)
g_test_dbus_down (singleton); {
g_clear_object (&singleton); g_warning ("There is not a session bus up");
}
else
{
session_bus_down_with_address (temporary_address_used_by_bus);
g_free (temporary_address_used_by_bus);
temporary_address_used_by_bus = NULL;
}
} }

View File

@ -1,6 +1,6 @@
/* GLib testing framework examples and tests /* GLib testing framework examples and tests
* *
* Copyright (C) 2012 Collabora Ltd. <http://www.collabora.co.uk/> * Copyright (C) 2008-2009 Red Hat, Inc.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -17,7 +17,7 @@
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA. * Boston, MA 02111-1307, USA.
* *
* Author: Xavier Claessens <xavier.claessens@collabora.co.uk> * Author: David Zeuthen <davidz@redhat.com>
*/ */
#ifndef __SESSION_BUS_H__ #ifndef __SESSION_BUS_H__
@ -27,9 +27,11 @@
G_BEGIN_DECLS G_BEGIN_DECLS
void session_bus_up (void); const gchar *session_bus_up_with_address (const gchar *given_address);
void session_bus_stop (void); void session_bus_down_with_address (const gchar *address);
void session_bus_down (void); const gchar *session_bus_get_temporary_address (void);
const gchar *session_bus_up (void);
void session_bus_down (void);
G_END_DECLS G_END_DECLS

View File

@ -2299,8 +2299,19 @@ main (int argc,
g_type_init (); g_type_init ();
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
/* all the tests use a session bus with a well-known address that we can bring up and down
* using session_bus_up() and session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
session_bus_up (); session_bus_up ();
/* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
* until one can connect to the bus but that's not how things work right now
*/
usleep (500 * 1000);
g_test_add_func ("/gdbus/codegen/annotations", test_annotations); g_test_add_func ("/gdbus/codegen/annotations", test_annotations);
g_test_add_func ("/gdbus/codegen/interface_stability", test_interface_stability); g_test_add_func ("/gdbus/codegen/interface_stability", test_interface_stability);
g_test_add_func ("/gdbus/codegen/object-manager", test_object_manager); g_test_add_func ("/gdbus/codegen/object-manager", test_object_manager);

View File

@ -585,8 +585,19 @@ main (int argc,
g_type_init (); g_type_init ();
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
/* all the tests use a session bus with a well-known address that we can bring up and down
* using session_bus_up() and session_bus_down().
*/
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
session_bus_up (); session_bus_up ();
/* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
* until one can connect to the bus but that's not how things work right now
*/
usleep (500 * 1000);
/* this is safe; testserver will exit once the bus goes away */ /* this is safe; testserver will exit once the bus goes away */
g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL)); g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));

View File

@ -1027,6 +1027,9 @@ main (int argc, char **argv)
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_type_init (); g_type_init ();
g_unsetenv ("DISPLAY");
g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
session_bus_up (); session_bus_up ();
g_test_add_func ("/gmenu/equality", test_equality); g_test_add_func ("/gmenu/equality", test_equality);