summaryrefslogtreecommitdiff
path: root/test/dbus-daemon-eavesdrop.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/dbus-daemon-eavesdrop.c')
-rw-r--r--test/dbus-daemon-eavesdrop.c169
1 files changed, 13 insertions, 156 deletions
diff --git a/test/dbus-daemon-eavesdrop.c b/test/dbus-daemon-eavesdrop.c
index a78d8888..79c1e903 100644
--- a/test/dbus-daemon-eavesdrop.c
+++ b/test/dbus-daemon-eavesdrop.c
@@ -27,21 +27,9 @@
#include <config.h>
-#include <glib.h>
-
-#include <dbus/dbus.h>
-
#include <string.h>
-#ifdef DBUS_WIN
-# include <io.h>
-# include <windows.h>
-#else
-# include <signal.h>
-# include <unistd.h>
-#endif
-
-#include "test-utils.h"
+#include "test-utils-glib.h"
#define SENDER_NAME "test.eavesdrop.sender"
#define SENDER_PATH "/test/eavesdrop/sender"
@@ -91,99 +79,6 @@ typedef struct {
dbus_bool_t politelistener_got_stopper;
} Fixture;
-#define assert_no_error(e) _assert_no_error (e, __FILE__, __LINE__)
-static void
-_assert_no_error (const DBusError *e,
- const char *file,
- int line)
-{
- if (G_UNLIKELY (dbus_error_is_set (e)))
- g_error ("%s:%d: expected success but got error: %s: %s",
- file, line, e->name, e->message);
-}
-
-static gchar *
-spawn_dbus_daemon (gchar *binary,
- gchar *configuration,
- GPid *daemon_pid)
-{
- GError *error = NULL;
- GString *address;
- gint address_fd;
- gchar *argv[] = {
- binary,
- configuration,
- "--nofork",
- "--print-address=1", /* stdout */
- NULL
- };
-
- g_spawn_async_with_pipes (NULL, /* working directory */
- argv,
- NULL, /* envp */
- G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
- NULL, /* child_setup */
- NULL, /* user data */
- daemon_pid,
- NULL, /* child's stdin = /dev/null */
- &address_fd,
- NULL, /* child's stderr = our stderr */
- &error);
- g_assert_no_error (error);
-
- address = g_string_new (NULL);
-
- /* polling until the dbus-daemon writes out its address is a bit stupid,
- * but at least it's simple, unlike dbus-launch... in principle we could
- * use select() here, but life's too short */
- while (1)
- {
- gssize bytes;
- gchar buf[4096];
- gchar *newline;
-
- bytes = read (address_fd, buf, sizeof (buf));
-
- if (bytes > 0)
- g_string_append_len (address, buf, bytes);
-
- newline = strchr (address->str, '\n');
-
- if (newline != NULL)
- {
- if ((newline > address->str) && ('\r' == newline[-1]))
- newline -= 1;
- g_string_truncate (address, newline - address->str);
- break;
- }
-
- g_usleep (G_USEC_PER_SEC / 10);
- }
-
- return g_string_free (address, FALSE);
-}
-
-static DBusConnection *
-connect_to_bus (Fixture *f,
- const gchar *address)
-{
- DBusConnection *conn;
- DBusError error = DBUS_ERROR_INIT;
- dbus_bool_t ok;
-
- conn = dbus_connection_open_private (address, &error);
- assert_no_error (&error);
- g_assert (conn != NULL);
-
- ok = dbus_bus_register (conn, &error);
- assert_no_error (&error);
- g_assert (ok);
- g_assert (dbus_bus_get_unique_name (conn) != NULL);
-
- test_connection_setup (f->ctx, conn);
- return conn;
-}
-
/* send a unicast signal to <self> to ensure that no other connection
* listening is the actual recipient for the signal */
static DBusHandlerResult
@@ -338,9 +233,9 @@ add_receiver_filter (Fixture *f)
DBusError e = DBUS_ERROR_INIT;
dbus_bus_add_match (f->receiver, RECEIVER_RULE, &e);
- assert_no_error (&e);
+ test_assert_no_error (&e);
dbus_bus_add_match (f->receiver, STOPPER_RULE, &e);
- assert_no_error (&e);
+ test_assert_no_error (&e);
if (!dbus_connection_add_filter (f->receiver,
signal_filter, f, NULL))
@@ -353,9 +248,9 @@ add_eavesdropper_filter (Fixture *f)
DBusError e = DBUS_ERROR_INIT;
dbus_bus_add_match (f->eavesdropper, EAVESDROPPER_RULE, &e);
- assert_no_error (&e);
+ test_assert_no_error (&e);
dbus_bus_add_match (f->eavesdropper, STOPPER_RULE, &e);
- assert_no_error (&e);
+ test_assert_no_error (&e);
if (!dbus_connection_add_filter (f->eavesdropper,
signal_filter, f, NULL))
@@ -368,9 +263,9 @@ add_politelistener_filter (Fixture *f)
DBusError e = DBUS_ERROR_INIT;
dbus_bus_add_match (f->politelistener, POLITELISTENER_RULE, &e);
- assert_no_error (&e);
+ test_assert_no_error (&e);
dbus_bus_add_match (f->politelistener, STOPPER_RULE, &e);
- assert_no_error (&e);
+ test_assert_no_error (&e);
if (!dbus_connection_add_filter (f->politelistener,
signal_filter, f, NULL))
@@ -381,8 +276,6 @@ static void
setup (Fixture *f,
gconstpointer context G_GNUC_UNUSED)
{
- gchar *dbus_daemon;
- gchar *config;
gchar *address;
f->ctx = test_main_context_get ();
@@ -390,45 +283,14 @@ setup (Fixture *f,
f->ge = NULL;
dbus_error_init (&f->e);
- dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
-
- if (dbus_daemon == NULL)
- dbus_daemon = g_strdup ("dbus-daemon");
-
- if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL)
- {
- config = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
- g_getenv ("DBUS_TEST_SYSCONFDIR"));
- }
- else if (g_getenv ("DBUS_TEST_DATA") != NULL)
- {
- config = g_strdup_printf (
- "--config-file=%s/valid-config-files/session.conf",
- g_getenv ("DBUS_TEST_DATA"));
- }
- else
- {
- config = g_strdup ("--session");
- }
-
- if (g_getenv ("DBUS_TEST_DAEMON_ADDRESS") != NULL)
- {
- address = g_strdup (g_getenv ("DBUS_TEST_DAEMON_ADDRESS"));
- }
- else
- {
- address = spawn_dbus_daemon (dbus_daemon, config, &f->daemon_pid);
- }
-
- g_free (dbus_daemon);
- g_free (config);
+ address = test_get_dbus_daemon (NULL, &f->daemon_pid);
- f->sender = connect_to_bus (f, address);
+ f->sender = test_connect_to_bus (f->ctx, address);
dbus_bus_request_name (f->sender, SENDER_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE,
&(f->e));
- f->receiver = connect_to_bus (f, address);
- f->eavesdropper = connect_to_bus (f, address);
- f->politelistener = connect_to_bus (f, address);
+ f->receiver = test_connect_to_bus (f->ctx, address);
+ f->eavesdropper = test_connect_to_bus (f->ctx, address);
+ f->politelistener = test_connect_to_bus (f->ctx, address);
add_receiver_filter (f);
add_politelistener_filter (f);
add_eavesdropper_filter (f);
@@ -541,12 +403,7 @@ teardown (Fixture *f,
f->eavesdropper = NULL;
}
-#ifdef DBUS_WIN
- TerminateProcess (f->daemon_pid, 1);
-#else
- kill (f->daemon_pid, SIGTERM);
-#endif
-
+ test_kill_pid (f->daemon_pid);
g_spawn_close_pid (f->daemon_pid);
test_main_context_unref (f->ctx);