From 5dc97c7201ce4eab60e5f037ed7af09ceaf95f6f Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 23 Jan 2015 19:12:11 +0000 Subject: [PATCH 05/10] Factor out some utility functions from test/dbus-daemon* --- test/Makefile.am | 27 +++++-- test/dbus-daemon-eavesdrop.c | 169 ++++------------------------------------ test/dbus-daemon.c | 178 +++--------------------------------------- test/test-utils-glib.c | 182 +++++++++++++++++++++++++++++++++++++++++++ test/test-utils-glib.h | 23 ++++++ 5 files changed, 248 insertions(+), 331 deletions(-) create mode 100644 test/test-utils-glib.c create mode 100644 test/test-utils-glib.h diff --git a/test/Makefile.am b/test/Makefile.am index 173df74..a8327d0 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -25,15 +25,23 @@ static_cppflags = \ noinst_LTLIBRARIES = libdbus-testutils-internal.la +libdbus_testutils_la_SOURCES = \ + test-utils.c \ + test-utils.h \ + $(NULL) + +if DBUS_WITH_GLIB +libdbus_testutils_la_SOURCES += \ + test-utils-glib.c \ + test-utils-glib.h \ + $(NULL) +endif + # You can link either libdbus-testutils, dbus-glib and libdbus-1, # or libdbus-testutils-internal and libdbus-internal - never both in the # same binary. if DBUS_WITH_DBUS_GLIB noinst_LTLIBRARIES += libdbus-testutils.la -libdbus_testutils_la_SOURCES = \ - test-utils.c \ - test-utils.h \ - $(NULL) libdbus_testutils_la_LIBADD = \ $(top_builddir)/dbus/libdbus-1.la \ $(GLIB_LIBS) \ @@ -50,8 +58,7 @@ libdbus_testutils_internal_la_CPPFLAGS = \ $(static_cppflags) \ $(NULL) libdbus_testutils_internal_la_SOURCES = \ - test-utils.c \ - test-utils.h \ + $(libdbus_testutils_la_SOURCES) \ $(NULL) libdbus_testutils_internal_la_LIBADD = \ $(top_builddir)/dbus/libdbus-internal.la \ @@ -197,14 +204,18 @@ test_relay_LDADD = \ $(GLIB_LIBS) \ $(NULL) -test_dbus_daemon_SOURCES = dbus-daemon.c +test_dbus_daemon_SOURCES = \ + dbus-daemon.c \ + $(NULL) test_dbus_daemon_CPPFLAGS = $(testutils_shared_if_possible_cppflags) test_dbus_daemon_LDADD = \ $(testutils_shared_if_possible_libs) \ $(GLIB_LIBS) \ $(NULL) -test_dbus_daemon_eavesdrop_SOURCES = dbus-daemon-eavesdrop.c +test_dbus_daemon_eavesdrop_SOURCES = \ + dbus-daemon-eavesdrop.c \ + $(NULL) test_dbus_daemon_eavesdrop_CPPFLAGS = $(testutils_shared_if_possible_cppflags) test_dbus_daemon_eavesdrop_LDADD = \ $(testutils_shared_if_possible_libs) \ diff --git a/test/dbus-daemon-eavesdrop.c b/test/dbus-daemon-eavesdrop.c index a78d888..79c1e90 100644 --- a/test/dbus-daemon-eavesdrop.c +++ b/test/dbus-daemon-eavesdrop.c @@ -27,21 +27,9 @@ #include -#include - -#include - #include -#ifdef DBUS_WIN -# include -# include -#else -# include -# include -#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 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); diff --git a/test/dbus-daemon.c b/test/dbus-daemon.c index 1cd51d2..ef0d27d 100644 --- a/test/dbus-daemon.c +++ b/test/dbus-daemon.c @@ -26,23 +26,13 @@ #include -#include +#include "test-utils-glib.h" -#include - -#include - -#ifdef DBUS_WIN -# include -# include -#else -# include +#ifdef DBUS_UNIX # include # include #endif -#include "test-utils.h" - /* Platforms where we know that credentials-passing passes both the * uid and the pid. Please keep these in alphabetical order. * @@ -85,99 +75,6 @@ typedef struct { gboolean wait_forever_called; } 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; -} - static DBusHandlerResult echo_filter (DBusConnection *connection, DBusMessage *message, @@ -220,71 +117,23 @@ setup (Fixture *f, gconstpointer context) { const Config *config = context; - gchar *dbus_daemon; - gchar *arg; gchar *address; f->ctx = test_main_context_get (); f->ge = NULL; dbus_error_init (&f->e); - if (config != NULL && config->config_file != NULL) - { - if (g_getenv ("DBUS_TEST_DAEMON_ADDRESS") != NULL) - { - g_message ("SKIP: cannot use DBUS_TEST_DAEMON_ADDRESS for " - "unusally-configured dbus-daemon"); - f->skip = TRUE; - return; - } - - if (g_getenv ("DBUS_TEST_DATA") == NULL) - { - g_message ("SKIP: set DBUS_TEST_DATA to a directory containing %s", - config->config_file); - f->skip = TRUE; - return; - } + address = test_get_dbus_daemon (config ? config->config_file : NULL, + &f->daemon_pid); - arg = g_strdup_printf ( - "--config-file=%s/%s", - g_getenv ("DBUS_TEST_DATA"), config->config_file); - } - else if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL) + if (address == NULL) { - arg = g_strdup_printf ("--config-file=%s/dbus-1/session.conf", - g_getenv ("DBUS_TEST_SYSCONFDIR")); + f->skip = TRUE; + return; } - else if (g_getenv ("DBUS_TEST_DATA") != NULL) - { - arg = g_strdup_printf ( - "--config-file=%s/valid-config-files/session.conf", - g_getenv ("DBUS_TEST_DATA")); - } - else - { - arg = g_strdup ("--session"); - } - - dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON")); - if (dbus_daemon == NULL) - dbus_daemon = g_strdup ("dbus-daemon"); - - if (g_getenv ("DBUS_TEST_DAEMON_ADDRESS") != NULL) - { - address = g_strdup (g_getenv ("DBUS_TEST_DAEMON_ADDRESS")); - } - else - { - address = spawn_dbus_daemon (dbus_daemon, arg, &f->daemon_pid); - } - - g_free (dbus_daemon); - g_free (arg); - - f->left_conn = connect_to_bus (f, address); - f->right_conn = connect_to_bus (f, address); + f->left_conn = test_connect_to_bus (f->ctx, address); + f->right_conn = test_connect_to_bus (f->ctx, address); g_free (address); } @@ -609,7 +458,7 @@ test_processid (Fixture *f, DBUS_TYPE_INVALID)) { g_assert_cmpstr (dbus_message_get_signature (m), ==, "u"); - assert_no_error (&error); + test_assert_no_error (&error); g_message ("GetConnectionUnixProcessID returned %u", pid); @@ -748,12 +597,7 @@ teardown (Fixture *f, if (f->daemon_pid != 0) { -#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); f->daemon_pid = 0; } diff --git a/test/test-utils-glib.c b/test/test-utils-glib.c new file mode 100644 index 0000000..10eedf6 --- /dev/null +++ b/test/test-utils-glib.c @@ -0,0 +1,182 @@ +#include +#include "test-utils-glib.h" + +#include + +#ifdef DBUS_WIN +# include +# include +#else +# include +# include +# include +#endif + +#include + +#include + +void +_test_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); +} + +gchar * +test_get_dbus_daemon (const gchar *config_file, + GPid *daemon_pid) +{ + gchar *dbus_daemon; + gchar *arg; + gchar *address; + + if (config_file != NULL) + { + if (g_getenv ("DBUS_TEST_DAEMON_ADDRESS") != NULL) + { + g_message ("SKIP: cannot use DBUS_TEST_DAEMON_ADDRESS for " + "unusally-configured dbus-daemon"); + return NULL; + } + + if (g_getenv ("DBUS_TEST_DATA") == NULL) + { + g_message ("SKIP: set DBUS_TEST_DATA to a directory containing %s", + config_file); + return NULL; + } + + arg = g_strdup_printf ( + "--config-file=%s/%s", + g_getenv ("DBUS_TEST_DATA"), config_file); + } + else if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL) + { + arg = g_strdup_printf ("--config-file=%s/dbus-1/session.conf", + g_getenv ("DBUS_TEST_SYSCONFDIR")); + } + else if (g_getenv ("DBUS_TEST_DATA") != NULL) + { + arg = g_strdup_printf ( + "--config-file=%s/valid-config-files/session.conf", + g_getenv ("DBUS_TEST_DATA")); + } + else + { + arg = g_strdup ("--session"); + } + + dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON")); + + if (dbus_daemon == NULL) + dbus_daemon = g_strdup ("dbus-daemon"); + + if (g_getenv ("DBUS_TEST_DAEMON_ADDRESS") != NULL) + { + address = g_strdup (g_getenv ("DBUS_TEST_DAEMON_ADDRESS")); + } + else + { + address = spawn_dbus_daemon (dbus_daemon, arg, daemon_pid); + } + + g_free (dbus_daemon); + g_free (arg); + return address; +} + +DBusConnection * +test_connect_to_bus (TestMainContext *ctx, + const gchar *address) +{ + DBusConnection *conn; + DBusError error = DBUS_ERROR_INIT; + dbus_bool_t ok; + + conn = dbus_connection_open_private (address, &error); + test_assert_no_error (&error); + g_assert (conn != NULL); + + ok = dbus_bus_register (conn, &error); + test_assert_no_error (&error); + g_assert (ok); + g_assert (dbus_bus_get_unique_name (conn) != NULL); + + test_connection_setup (ctx, conn); + return conn; +} + +void +test_kill_pid (GPid pid) +{ +#ifdef DBUS_WIN + TerminateProcess (pid, 1); +#else + kill (pid, SIGTERM); +#endif +} diff --git a/test/test-utils-glib.h b/test/test-utils-glib.h new file mode 100644 index 0000000..380fe95 --- /dev/null +++ b/test/test-utils-glib.h @@ -0,0 +1,23 @@ +#ifndef TEST_UTILS_GLIB_H +#define TEST_UTILS_GLIB_H + +#include + +#include + +#include "test-utils.h" + +#define test_assert_no_error(e) _test_assert_no_error (e, __FILE__, __LINE__) +void _test_assert_no_error (const DBusError *e, + const char *file, + int line); + +gchar *test_get_dbus_daemon (const gchar *config_file, + GPid *daemon_pid); + +DBusConnection *test_connect_to_bus (TestMainContext *ctx, + const gchar *address); + +void test_kill_pid (GPid pid); + +#endif -- 2.1.4