From ddbb4c5924eab469757fb0590edbd382e402ecdb Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 2 Sep 2013 16:32:31 +0100 Subject: [PATCH 5/6] Tests: allow dbus-glib to be replaced with use of libdbus-internal We only use dbus-glib for its main loop; within dbus, DBusLoop is available as an alternative, although it isn't thread-safe and isn't public API. For tests that otherwise only use libdbus public API, it's desirable to be able to avoid DBusLoop, so we can run them against an installed libdbus as an integration test. However, if we don't have dbus-glib, we're going to have to use an in-tree main loop, which might as well be DBusLoop. The major disadvantage of using dbus-glib is that it isn't safe to link both dbus-1 and dbus-internal at the same time. This is awkward for a future test case that wants to use _dbus_getsid() in dbus-daemon.c, but only on Windows (fd.o #54445). If we use the same API wrapper around both dbus-glib and DBusLoop, we can compile that test against dbus-glib or against DBusLoop, depending on the platform. --- cmake/CMakeLists.txt | 4 +- cmake/test/name-test/CMakeLists.txt | 2 +- configure.ac | 31 +++++++++---- test/Makefile.am | 84 ++++++++++++++++++++++----------- test/corrupt.c | 21 +++++---- test/dbus-daemon-eavesdrop.c | 27 +++++++---- test/dbus-daemon.c | 21 ++++++--- test/loopback.c | 23 +++++---- test/manual-authz.c | 30 ++++++------ test/marshal.c | 1 - test/name-test/Makefile.am | 9 ++-- test/relay.c | 25 ++++++---- test/test-utils.c | 93 +++++++++++++++++++++++++++++++++---- test/test-utils.h | 35 ++++++++++---- 14 files changed, 287 insertions(+), 119 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 32edc51..fe11196 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -478,7 +478,9 @@ set(DBUS_INTERNAL_LIBRARIES dbus-internal) # important note: DBUS_INTERNAL_xxxxx_DEFINITIONS must *not* be set when building dbus-1 library set (DBUS_INTERNAL_ADD_LIBRARY_OPTIONS STATIC) set (DBUS_INTERNAL_LIBRARY_DEFINITIONS "-DDBUS_STATIC_BUILD") -set (DBUS_INTERNAL_CLIENT_DEFINITIONS "-DDBUS_STATIC_BUILD -DDBUS_COMPILATION") +# For now, the CMake build system doesn't support replacing the internal +# main loop with dbus-glib +set (DBUS_INTERNAL_CLIENT_DEFINITIONS "-DDBUS_STATIC_BUILD -DDBUS_COMPILATION -DDBUS_TEST_USE_INTERNAL") configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h ) diff --git a/cmake/test/name-test/CMakeLists.txt b/cmake/test/name-test/CMakeLists.txt index 44e4f6d..bf096ba 100644 --- a/cmake/test/name-test/CMakeLists.txt +++ b/cmake/test/name-test/CMakeLists.txt @@ -2,7 +2,7 @@ if (DBUS_ENABLE_EMBEDDED_TESTS) set (NAMEtest-DIR ../../../test/name-test) -add_definitions(-DDBUS_COMPILATION) +add_definitions(${DBUS_INTERNAL_CLIENT_DEFINITIONS}) add_executable(test-pending-call-dispatch ${NAMEtest-DIR}/test-pending-call-dispatch.c) target_link_libraries(test-pending-call-dispatch ${DBUS_INTERNAL_LIBRARIES}) diff --git a/configure.ac b/configure.ac index 8caea40..ab7b1e1 100644 --- a/configure.ac +++ b/configure.ac @@ -210,7 +210,8 @@ AC_DEFINE([GLIB_VERSION_MAX_ALLOWED], [GLIB_VERSION_2_32], [Prevent post-2.32 AP with_glib=yes -if test "x$enable_modular_tests" != xno; then +AS_IF([test "x$enable_modular_tests" != xno], + [ PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.24, gio-2.0 >= 2.24], [], [if test "x$enable_modular_tests" = xyes; then @@ -219,16 +220,25 @@ if test "x$enable_modular_tests" != xno; then else # assumed to be "auto" with_glib=no fi]) - # If dbus-gmain.[ch] returned to libdbus then we wouldn't need this - PKG_CHECK_MODULES([DBUS_GLIB], [dbus-glib-1], + ], + [with_glib=no]) + +# Not required, because we can use internal APIs (but that makes the +# "installable tests" less useful as integration tests) +AC_ARG_WITH([dbus_glib], + [AS_HELP_STRING([--with-dbus-glib], [Use dbus-glib for regression tests])], + [], + [with_dbus_glib=auto]) +AS_IF([test "x$with_dbus_glib" != xno], + [PKG_CHECK_MODULES([DBUS_GLIB], [dbus-glib-1], [], - [if test "x$enable_modular_tests" = xyes; then - AC_MSG_NOTICE([Full test coverage (--enable-modular-tests=yes or --enable-tests=yes) requires dbus-glib]) - AC_MSG_ERROR([$DBUS_GLIB_ERRORS]) - else # assumed to be "auto" - with_glib=no - fi]) -fi + [AS_IF([test "x$with_dbus_glib" = xyes], + dnl specifically requested, but not found + [AC_MSG_ERROR([$DBUS_GLIB_ERRORS])], + dnl else: assumed to be "auto" + [with_dbus_glib=no])])]) +AM_CONDITIONAL([DBUS_WITH_DBUS_GLIB], [test "x$with_dbus_glib" != xno]) + if test "x$enable_modular_tests" != xno; then AC_DEFINE([DBUS_ENABLE_MODULAR_TESTS], [1], [Define to build independent test binaries]) @@ -1839,6 +1849,7 @@ echo " Building embedded tests: ${enable_embedded_tests} Building modular tests: ${enable_modular_tests} - with GLib: ${with_glib} + - with dbus-glib: ${with_dbus_glib} Building verbose mode: ${enable_verbose_mode} Building assertions: ${enable_asserts} Building checks: ${enable_checks} diff --git a/test/Makefile.am b/test/Makefile.am index fe163c6..281b3e2 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -20,19 +20,40 @@ static_cppflags = \ $(AM_CPPFLAGS) \ -DDBUS_STATIC_BUILD \ -DDBUS_COMPILATION \ + -DDBUS_TEST_USE_INTERNAL \ $(NULL) -libdbus_testutils_la_CPPFLAGS = \ - $(static_cppflags) +noinst_LTLIBRARIES = libdbus-testutils-internal.la + +# 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-internal.la \ + $(top_builddir)/dbus/libdbus-1.la \ + $(GLIB_LIBS) \ + $(DBUS_GLIB_LIBS) \ $(NULL) +testutils_shared_if_possible = libdbus-testutils.la +else +testutils_shared_if_possible = libdbus-testutils-internal.la +endif -noinst_LTLIBRARIES = libdbus-testutils.la +libdbus_testutils_internal_la_CPPFLAGS = \ + $(static_cppflags) \ + $(NULL) +libdbus_testutils_internal_la_SOURCES = \ + test-utils.c \ + test-utils.h \ + $(NULL) +libdbus_testutils_internal_la_LIBADD = \ + $(top_builddir)/dbus/libdbus-internal.la \ + $(NULL) if DBUS_ENABLE_EMBEDDED_TESTS ## break-loader removed for now @@ -70,15 +91,15 @@ endif !DBUS_ENABLE_EMBEDDED_TESTS noinst_PROGRAMS= $(TEST_BINARIES) test_service_CPPFLAGS = $(static_cppflags) -test_service_LDADD = libdbus-testutils.la +test_service_LDADD = libdbus-testutils-internal.la test_names_CPPFLAGS = $(static_cppflags) -test_names_LDADD = libdbus-testutils.la +test_names_LDADD = libdbus-testutils-internal.la ## break_loader_CPPFLAGS = $(static_cppflags) ## break_loader_LDADD = $(top_builddir)/dbus/libdbus-internal.la test_shell_service_CPPFLAGS = $(static_cppflags) -test_shell_service_LDADD = libdbus-testutils.la +test_shell_service_LDADD = libdbus-testutils-internal.la shell_test_CPPFLAGS = $(static_cppflags) -shell_test_LDADD = libdbus-testutils.la +shell_test_LDADD = libdbus-testutils-internal.la spawn_test_CPPFLAGS = $(static_cppflags) spawn_test_LDADD = $(top_builddir)/dbus/libdbus-internal.la @@ -88,11 +109,11 @@ test_printf_LDADD = $(top_builddir)/dbus/libdbus-internal.la test_refs_SOURCES = internals/refs.c test_refs_CPPFLAGS = $(static_cppflags) -test_refs_LDADD = libdbus-testutils.la $(GLIB_LIBS) +test_refs_LDADD = libdbus-testutils-internal.la $(GLIB_LIBS) test_syslog_SOURCES = internals/syslog.c test_syslog_CPPFLAGS = $(static_cppflags) -test_syslog_LDADD = libdbus-testutils.la $(GLIB_LIBS) +test_syslog_LDADD = libdbus-testutils-internal.la $(GLIB_LIBS) EXTRA_DIST = dbus-test-runner @@ -139,45 +160,52 @@ TESTS_ENVIRONMENT = \ $(NULL) manual_authz_SOURCES = manual-authz.c -manual_authz_LDADD = $(top_builddir)/dbus/libdbus-1.la \ +manual_authz_LDADD = \ + $(testutils_shared_if_possible) \ $(GLIB_LIBS) \ - $(DBUS_GLIB_LIBS) + $(NULL) test_corrupt_SOURCES = corrupt.c -test_corrupt_LDADD = $(top_builddir)/dbus/libdbus-1.la \ +test_corrupt_LDADD = \ + $(testutils_shared_if_possible) \ $(GLIB_LIBS) \ - $(DBUS_GLIB_LIBS) + $(NULL) test_loopback_SOURCES = loopback.c -test_loopback_LDADD = $(top_builddir)/dbus/libdbus-1.la \ +test_loopback_LDADD = \ + $(testutils_shared_if_possible) \ $(GLIB_LIBS) \ - $(DBUS_GLIB_LIBS) + $(NULL) test_relay_SOURCES = relay.c -test_relay_LDADD = $(top_builddir)/dbus/libdbus-1.la \ +test_relay_LDADD = \ + $(testutils_shared_if_possible) \ $(GLIB_LIBS) \ - $(DBUS_GLIB_LIBS) + $(NULL) test_dbus_daemon_SOURCES = dbus-daemon.c -test_dbus_daemon_LDADD = $(top_builddir)/dbus/libdbus-1.la \ +test_dbus_daemon_LDADD = \ + $(testutils_shared_if_possible) \ $(GLIB_LIBS) \ - $(DBUS_GLIB_LIBS) + $(NULL) test_dbus_daemon_eavesdrop_SOURCES = dbus-daemon-eavesdrop.c -test_dbus_daemon_eavesdrop_CPPFLAGS = $(GLIB_CFLAGS) $(DBUS_GLIB_CFLAGS) -test_dbus_daemon_eavesdrop_LDFLAGS = @R_DYNAMIC_LDFLAG@ -test_dbus_daemon_eavesdrop_LDADD = $(top_builddir)/dbus/libdbus-1.la \ +test_dbus_daemon_eavesdrop_LDADD = \ + $(testutils_shared_if_possible) \ $(GLIB_LIBS) \ - $(DBUS_GLIB_LIBS) + $(NULL) test_marshal_SOURCES = marshal.c -test_marshal_LDADD = $(top_builddir)/dbus/libdbus-1.la \ +test_marshal_LDADD = \ + $(top_builddir)/dbus/libdbus-1.la \ $(GLIB_LIBS) \ - $(DBUS_GLIB_LIBS) + $(NULL) test_syntax_SOURCES = syntax.c -test_syntax_LDADD = $(top_builddir)/dbus/libdbus-1.la \ - $(GLIB_LIBS) +test_syntax_LDADD = \ + $(top_builddir)/dbus/libdbus-1.la \ + $(GLIB_LIBS) \ + $(NULL) if DBUS_ENABLE_MODULAR_TESTS TESTS += $(installable_tests) diff --git a/test/corrupt.c b/test/corrupt.c index be0e022..1a7d446 100644 --- a/test/corrupt.c +++ b/test/corrupt.c @@ -30,10 +30,12 @@ #include #include -#include + +#include "test-utils.h" typedef struct { DBusError e; + TestMainContext *ctx; DBusServer *server; DBusConnection *server_conn; @@ -72,13 +74,14 @@ new_conn_cb (DBusServer *server, g_assert (f->server_conn == NULL); f->server_conn = dbus_connection_ref (server_conn); - dbus_connection_setup_with_g_main (server_conn, NULL); + test_connection_setup (f->ctx, server_conn); } static void setup (Fixture *f, gconstpointer addr) { + f->ctx = test_main_context_get (); dbus_error_init (&f->e); g_queue_init (&f->client_messages); @@ -88,7 +91,7 @@ setup (Fixture *f, dbus_server_set_new_connection_function (f->server, new_conn_cb, f, NULL); - dbus_server_setup_with_g_main (f->server, NULL); + test_server_setup (f->ctx, f->server); } static void @@ -103,12 +106,12 @@ test_connect (Fixture *f, dbus_server_get_address (f->server), &f->e); assert_no_error (&f->e); g_assert (f->client_conn != NULL); - dbus_connection_setup_with_g_main (f->client_conn, NULL); + test_connection_setup (f->ctx, f->client_conn); while (f->server_conn == NULL) { g_print ("."); - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); } have_mem = dbus_connection_add_filter (f->client_conn, @@ -137,7 +140,7 @@ test_message (Fixture *f, while (g_queue_is_empty (&f->client_messages)) { g_print ("."); - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); } g_assert_cmpuint (g_queue_get_length (&f->client_messages), ==, 1); @@ -229,7 +232,7 @@ test_corrupt (Fixture *f, while (g_queue_is_empty (&f->client_messages)) { g_print ("."); - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); } incoming = g_queue_pop_head (&f->client_messages); @@ -318,7 +321,7 @@ test_byte_order (Fixture *f, while (g_queue_is_empty (&f->client_messages)) { g_print ("."); - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); } message = g_queue_pop_head (&f->client_messages); @@ -368,6 +371,8 @@ teardown (Fixture *f, dbus_server_unref (f->server); f->server = NULL; } + + test_main_context_unref (f->ctx); } int diff --git a/test/dbus-daemon-eavesdrop.c b/test/dbus-daemon-eavesdrop.c index 0bd923d..2c45f54 100644 --- a/test/dbus-daemon-eavesdrop.c +++ b/test/dbus-daemon-eavesdrop.c @@ -30,7 +30,6 @@ #include #include -#include #include @@ -42,6 +41,8 @@ # include #endif +#include "test-utils.h" + #define SENDER_NAME "test.eavesdrop.sender" #define SENDER_PATH "/test/eavesdrop/sender" #define SENDER_IFACE SENDER_NAME @@ -71,6 +72,7 @@ typedef enum { } SignalDst; typedef struct { + TestMainContext *ctx; DBusError e; GError *ge; @@ -160,7 +162,8 @@ spawn_dbus_daemon (gchar *binary, } static DBusConnection * -connect_to_bus (const gchar *address) +connect_to_bus (Fixture *f, + const gchar *address) { DBusConnection *conn; DBusError error = DBUS_ERROR_INIT; @@ -175,7 +178,7 @@ connect_to_bus (const gchar *address) g_assert (ok); g_assert (dbus_bus_get_unique_name (conn) != NULL); - dbus_connection_setup_with_g_main (conn, NULL); + test_connection_setup (f->ctx, conn); return conn; } @@ -380,6 +383,8 @@ setup (Fixture *f, gchar *config; gchar *address; + f->ctx = test_main_context_get (); + f->ge = NULL; dbus_error_init (&f->e); @@ -409,12 +414,12 @@ setup (Fixture *f, g_free (dbus_daemon); g_free (config); - f->sender = connect_to_bus (address); + f->sender = connect_to_bus (f, address); dbus_bus_request_name (f->sender, SENDER_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE, &(f->e)); - f->receiver = connect_to_bus (address); - f->eavesdropper = connect_to_bus (address); - f->politelistener = connect_to_bus (address); + f->receiver = connect_to_bus (f, address); + f->eavesdropper = connect_to_bus (f, address); + f->politelistener = connect_to_bus (f, address); add_receiver_filter (f); add_politelistener_filter (f); add_eavesdropper_filter (f); @@ -432,7 +437,7 @@ test_eavesdrop_broadcast (Fixture *f, while (!f->receiver_got_stopper || !f->politelistener_got_stopper || !f->eavesdropper_got_stopper) - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); /* all the three connection can receive a broadcast */ g_assert_cmpint (f->receiver_dst, ==, BROADCAST); @@ -452,7 +457,7 @@ test_eavesdrop_unicast_to_sender (Fixture *f, while (!f->receiver_got_stopper || !f->politelistener_got_stopper || !f->eavesdropper_got_stopper) - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); /* not directed to it and not broadcasted, they cannot receive it */ g_assert_cmpint (f->receiver_dst, ==, NONE_YET); @@ -472,7 +477,7 @@ test_eavesdrop_unicast_to_receiver (Fixture *f, while (!f->receiver_got_stopper || !f->politelistener_got_stopper || !f->eavesdropper_got_stopper) - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); /* direct to him */ g_assert_cmpint (f->receiver_dst, ==, TO_ME); @@ -534,6 +539,8 @@ teardown (Fixture *f, #endif g_spawn_close_pid (f->daemon_pid); + + test_main_context_unref (f->ctx); } int diff --git a/test/dbus-daemon.c b/test/dbus-daemon.c index 69b6791..22ea23e 100644 --- a/test/dbus-daemon.c +++ b/test/dbus-daemon.c @@ -29,7 +29,6 @@ #include #include -#include #include @@ -42,9 +41,13 @@ # include #endif +#include "test-utils.h" + typedef struct { gboolean skip; + TestMainContext *ctx; + DBusError e; GError *ge; @@ -127,7 +130,8 @@ spawn_dbus_daemon (gchar *binary, } static DBusConnection * -connect_to_bus (const gchar *address) +connect_to_bus (Fixture *f, + const gchar *address) { DBusConnection *conn; DBusError error = DBUS_ERROR_INIT; @@ -142,7 +146,7 @@ connect_to_bus (const gchar *address) g_assert (ok); g_assert (dbus_bus_get_unique_name (conn) != NULL); - dbus_connection_setup_with_g_main (conn, NULL); + test_connection_setup (f->ctx, conn); return conn; } @@ -184,6 +188,7 @@ setup (Fixture *f, gchar *arg; gchar *address; + f->ctx = test_main_context_get (); f->ge = NULL; dbus_error_init (&f->e); @@ -227,8 +232,8 @@ setup (Fixture *f, g_free (dbus_daemon); g_free (arg); - f->left_conn = connect_to_bus (address); - f->right_conn = connect_to_bus (address); + f->left_conn = connect_to_bus (f, address); + f->right_conn = connect_to_bus (f, address); g_free (address); } @@ -302,7 +307,7 @@ test_echo (Fixture *f, } while (received < count) - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); elapsed = g_test_timer_elapsed (); @@ -361,7 +366,7 @@ test_creds (Fixture *f, g_error ("OOM"); while (m == NULL) - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); g_assert_cmpstr (dbus_message_get_signature (m), ==, "a{sv}"); @@ -473,6 +478,8 @@ teardown (Fixture *f, g_spawn_close_pid (f->daemon_pid); f->daemon_pid = 0; } + + test_main_context_unref (f->ctx); } static Config limited_config = { diff --git a/test/loopback.c b/test/loopback.c index 39cf03a..7526d8d 100644 --- a/test/loopback.c +++ b/test/loopback.c @@ -29,11 +29,13 @@ #include #include -#include #include +#include "test-utils.h" + typedef struct { + TestMainContext *ctx; DBusError e; DBusServer *server; @@ -74,7 +76,7 @@ new_conn_cb (DBusServer *server, g_assert (f->server_conn == NULL); f->server_conn = dbus_connection_ref (server_conn); - dbus_connection_setup_with_g_main (server_conn, NULL); + test_connection_setup (f->ctx, server_conn); have_mem = dbus_connection_add_filter (server_conn, server_message_cb, f, NULL); @@ -85,6 +87,7 @@ static void setup (Fixture *f, gconstpointer addr) { + f->ctx = test_main_context_get (); dbus_error_init (&f->e); g_queue_init (&f->server_messages); @@ -94,7 +97,7 @@ setup (Fixture *f, dbus_server_set_new_connection_function (f->server, new_conn_cb, f, NULL); - dbus_server_setup_with_g_main (f->server, NULL); + test_server_setup (f->ctx, f->server); } static void @@ -107,12 +110,12 @@ test_connect (Fixture *f, dbus_server_get_address (f->server), &f->e); assert_no_error (&f->e); g_assert (f->client_conn != NULL); - dbus_connection_setup_with_g_main (f->client_conn, NULL); + test_connection_setup (f->ctx, f->client_conn); while (f->server_conn == NULL) { g_print ("."); - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); } } @@ -141,12 +144,12 @@ test_bad_guid (Fixture *f, f->client_conn = dbus_connection_open_private (address, &f->e); assert_no_error (&f->e); g_assert (f->client_conn != NULL); - dbus_connection_setup_with_g_main (f->client_conn, NULL); + test_connection_setup (f->ctx, f->client_conn); while (f->server_conn == NULL) { g_print ("."); - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); } /* We get disconnected */ @@ -154,7 +157,7 @@ test_bad_guid (Fixture *f, while (g_queue_is_empty (&f->server_messages)) { g_print ("."); - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); } g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1); @@ -197,7 +200,7 @@ test_message (Fixture *f, while (g_queue_is_empty (&f->server_messages)) { g_print ("."); - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); } g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1); @@ -244,6 +247,8 @@ teardown (Fixture *f, dbus_server_unref (f->server); f->server = NULL; } + + test_main_context_unref (f->ctx); } int diff --git a/test/manual-authz.c b/test/manual-authz.c index d228829..f9e3688 100644 --- a/test/manual-authz.c +++ b/test/manual-authz.c @@ -29,15 +29,17 @@ #include #include -#include #ifdef G_OS_UNIX #include #include #endif +#include "test-utils.h" + typedef struct { DBusError e; + TestMainContext *ctx; DBusServer *normal_server; DBusServer *anon_allowed_server; @@ -212,7 +214,7 @@ new_conn_cb (DBusServer *server, Fixture *f = data; dbus_connection_ref (conn); - dbus_connection_setup_with_g_main (conn, NULL); + test_connection_setup (f->ctx, conn); if (!dbus_connection_add_filter (conn, server_message_cb, f, NULL)) oom (); @@ -301,7 +303,7 @@ setup (Fixture *f, g_assert (f->normal_server != NULL); dbus_server_set_new_connection_function (f->normal_server, new_conn_cb, f, NULL); - dbus_server_setup_with_g_main (f->normal_server, NULL); + test_server_setup (f->ctx, f->normal_server); connect_addr = dbus_server_get_address (f->normal_server); g_message ("Normal server:\n%s", connect_addr); dbus_free (connect_addr); @@ -311,7 +313,7 @@ setup (Fixture *f, g_assert (f->anon_allowed_server != NULL); dbus_server_set_new_connection_function (f->anon_allowed_server, new_conn_cb, f, NULL); - dbus_server_setup_with_g_main (f->anon_allowed_server, NULL); + test_server_setup (f->ctx, f->anon_allowed_server); connect_addr = dbus_server_get_address (f->anon_allowed_server); g_message ("Anonymous-allowed server:\n%s", connect_addr); dbus_free (connect_addr); @@ -321,7 +323,7 @@ setup (Fixture *f, g_assert (f->anon_only_server != NULL); dbus_server_set_new_connection_function (f->anon_only_server, new_conn_cb, f, NULL); - dbus_server_setup_with_g_main (f->anon_only_server, NULL); + test_server_setup (f->ctx, f->anon_only_server); connect_addr = dbus_server_get_address (f->anon_only_server); g_message ("Anonymous-only server:\n%s", connect_addr); dbus_free (connect_addr); @@ -332,7 +334,7 @@ setup (Fixture *f, dbus_server_set_auth_mechanisms (f->anon_mech_only_server, only_anon); dbus_server_set_new_connection_function (f->anon_mech_only_server, new_conn_cb, f, NULL); - dbus_server_setup_with_g_main (f->anon_mech_only_server, NULL); + test_server_setup (f->ctx, f->anon_mech_only_server); connect_addr = dbus_server_get_address (f->anon_mech_only_server); g_message ("Anon mech only server:\n%s", connect_addr); dbus_free (connect_addr); @@ -343,7 +345,7 @@ setup (Fixture *f, dbus_server_set_auth_mechanisms (f->anon_disallowed_server, only_anon); dbus_server_set_new_connection_function (f->anon_disallowed_server, new_conn_cb, f, NULL); - dbus_server_setup_with_g_main (f->anon_disallowed_server, NULL); + test_server_setup (f->ctx, f->anon_disallowed_server); connect_addr = dbus_server_get_address (f->anon_disallowed_server); g_message ("Anonymous-disallowed server:\n%s", connect_addr); dbus_free (connect_addr); @@ -353,7 +355,7 @@ setup (Fixture *f, g_assert (f->permissive_server != NULL); dbus_server_set_new_connection_function (f->permissive_server, new_conn_cb, f, NULL); - dbus_server_setup_with_g_main (f->permissive_server, NULL); + test_server_setup (f->ctx, f->permissive_server); connect_addr = dbus_server_get_address (f->permissive_server); g_message ("Permissive server:\n%s", connect_addr); dbus_free (connect_addr); @@ -363,7 +365,7 @@ setup (Fixture *f, g_assert (f->unhappy_server != NULL); dbus_server_set_new_connection_function (f->unhappy_server, new_conn_cb, f, NULL); - dbus_server_setup_with_g_main (f->unhappy_server, NULL); + test_server_setup (f->ctx, f->unhappy_server); connect_addr = dbus_server_get_address (f->unhappy_server); g_message ("Unhappy server:\n%s", connect_addr); dbus_free (connect_addr); @@ -373,7 +375,7 @@ setup (Fixture *f, g_assert (f->same_uid_server != NULL); dbus_server_set_new_connection_function (f->same_uid_server, new_conn_cb, f, NULL); - dbus_server_setup_with_g_main (f->same_uid_server, NULL); + test_server_setup (f->ctx, f->same_uid_server); connect_addr = dbus_server_get_address (f->same_uid_server); g_message ("Same-UID server:\n%s", connect_addr); dbus_free (connect_addr); @@ -383,7 +385,7 @@ setup (Fixture *f, g_assert (f->same_uid_or_anon_server != NULL); dbus_server_set_new_connection_function (f->same_uid_or_anon_server, new_conn_cb, f, NULL); - dbus_server_setup_with_g_main (f->same_uid_or_anon_server, NULL); + test_server_setup (f->ctx, f->same_uid_or_anon_server); connect_addr = dbus_server_get_address (f->same_uid_or_anon_server); g_message ("Same-UID-or-anon server:\n%s", connect_addr); dbus_free (connect_addr); @@ -393,7 +395,7 @@ int main (int argc, char **argv) { - Fixture f = { DBUS_ERROR_INIT }; + Fixture f = { DBUS_ERROR_INIT, test_main_context_get () }; if (argc >= 2) setup (&f, argv[1]); @@ -401,5 +403,7 @@ main (int argc, setup (&f, "tcp:host=127.0.0.1"); for (;;) - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f.ctx, TRUE); + + /* never returns */ } diff --git a/test/marshal.c b/test/marshal.c index e65ee7c..d74e767 100644 --- a/test/marshal.c +++ b/test/marshal.c @@ -30,7 +30,6 @@ #include #include -#include typedef struct { DBusError e; diff --git a/test/name-test/Makefile.am b/test/name-test/Makefile.am index 424dad3..54b02af 100644 --- a/test/name-test/Makefile.am +++ b/test/name-test/Makefile.am @@ -3,6 +3,7 @@ AM_CPPFLAGS = \ -I$(top_srcdir) \ -DDBUS_COMPILATION \ -DDBUS_STATIC_BUILD \ + -DDBUS_TEST_USE_INTERNAL \ $(NULL) # if assertions are enabled, improve backtraces @@ -31,9 +32,9 @@ test_pending_call_timeout_LDADD=$(top_builddir)/dbus/libdbus-internal.la test_threads_init_LDADD=$(top_builddir)/dbus/libdbus-internal.la test_ids_LDADD=$(top_builddir)/dbus/libdbus-internal.la -test_shutdown_LDADD=../libdbus-testutils.la -test_privserver_LDADD=../libdbus-testutils.la -test_privserver_client_LDADD=../libdbus-testutils.la -test_autolaunch_LDADD=../libdbus-testutils.la +test_shutdown_LDADD=../libdbus-testutils-internal.la +test_privserver_LDADD=../libdbus-testutils-internal.la +test_privserver_client_LDADD=../libdbus-testutils-internal.la +test_autolaunch_LDADD=../libdbus-testutils-internal.la endif diff --git a/test/relay.c b/test/relay.c index f4129d0..ecfe4c8 100644 --- a/test/relay.c +++ b/test/relay.c @@ -29,7 +29,8 @@ #include #include -#include + +#include "test-utils.h" /* This is basically a miniature dbus-daemon. We relay messages from the client * on the left to the client on the right. @@ -43,6 +44,7 @@ */ typedef struct { + TestMainContext *ctx; DBusError e; DBusServer *server; @@ -113,13 +115,14 @@ new_conn_cb (DBusServer *server, f->right_server_conn = dbus_connection_ref (server_conn); } - dbus_connection_setup_with_g_main (server_conn, NULL); + test_connection_setup (f->ctx, server_conn); } static void setup (Fixture *f, gconstpointer data G_GNUC_UNUSED) { + f->ctx = test_main_context_get (); dbus_error_init (&f->e); g_queue_init (&f->messages); @@ -129,7 +132,7 @@ setup (Fixture *f, dbus_server_set_new_connection_function (f->server, new_conn_cb, f, NULL); - dbus_server_setup_with_g_main (f->server, NULL); + test_server_setup (f->ctx, f->server); } static void @@ -148,25 +151,25 @@ test_connect (Fixture *f, f->left_client_conn = dbus_connection_open_private (address, &f->e); assert_no_error (&f->e); g_assert (f->left_client_conn != NULL); - dbus_connection_setup_with_g_main (f->left_client_conn, NULL); + test_connection_setup (f->ctx, f->left_client_conn); while (f->left_server_conn == NULL) { g_print ("."); - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); } f->right_client_conn = dbus_connection_open_private (address, &f->e); assert_no_error (&f->e); g_assert (f->right_client_conn != NULL); - dbus_connection_setup_with_g_main (f->right_client_conn, NULL); + test_connection_setup (f->ctx, f->right_client_conn); dbus_free (address); while (f->right_server_conn == NULL) { g_print ("."); - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); } have_mem = dbus_connection_add_filter (f->right_client_conn, @@ -208,7 +211,7 @@ test_relay (Fixture *f, while (g_queue_get_length (&f->messages) < 2) { g_print ("."); - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); } g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 2); @@ -237,7 +240,7 @@ test_limit (Fixture *f, /* This was an attempt to reproduce fd.o #34393. It didn't work. */ g_test_bug ("34393"); dbus_connection_set_max_received_size (f->left_server_conn, 1); - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); for (i = 0; i < MANY; i++) { @@ -253,7 +256,7 @@ test_limit (Fixture *f, { while (g_queue_is_empty (&f->messages)) { - g_main_context_iteration (NULL, TRUE); + test_main_context_iterate (f->ctx, TRUE); } while ((incoming = g_queue_pop_head (&f->messages)) != NULL) @@ -302,6 +305,8 @@ teardown (Fixture *f, dbus_server_unref (f->server); f->server = NULL; } + + test_main_context_unref (f->ctx); } int diff --git a/test/test-utils.c b/test/test-utils.c index c3c3ed3..9a4f358 100644 --- a/test/test-utils.c +++ b/test/test-utils.c @@ -1,6 +1,13 @@ #include #include "test-utils.h" +#ifndef DBUS_TEST_USE_INTERNAL +# include +# include +#endif + +#ifdef DBUS_TEST_USE_INTERNAL + typedef struct { DBusLoop *loop; @@ -97,10 +104,14 @@ cdata_new (DBusLoop *loop, return cd; } +#endif /* DBUS_TEST_USE_INTERNAL */ + dbus_bool_t -test_connection_setup (DBusLoop *loop, +test_connection_setup (TestMainContext *ctx, DBusConnection *connection) { +#ifdef DBUS_TEST_USE_INTERNAL + DBusLoop *loop = ctx; CData *cd; cd = NULL; @@ -148,10 +159,23 @@ test_connection_setup (DBusLoop *loop, dbus_connection_set_timeout_functions (connection, NULL, NULL, NULL, NULL, NULL); return FALSE; +#else /* !DBUS_TEST_USE_INTERNAL */ + + dbus_connection_setup_with_g_main (connection, ctx); + return TRUE; + +#endif /* !DBUS_TEST_USE_INTERNAL */ +} + +static void +die (const char *message) +{ + fprintf (stderr, "*** %s", message); + exit (1); } void -test_connection_shutdown (DBusLoop *loop, +test_connection_shutdown (TestMainContext *ctx, DBusConnection *connection) { if (!dbus_connection_set_watch_functions (connection, @@ -159,18 +183,20 @@ test_connection_shutdown (DBusLoop *loop, NULL, NULL, NULL, NULL)) - _dbus_assert_not_reached ("setting watch functions to NULL failed"); + die ("setting watch functions to NULL failed"); if (!dbus_connection_set_timeout_functions (connection, NULL, NULL, NULL, NULL, NULL)) - _dbus_assert_not_reached ("setting timeout functions to NULL failed"); + die ("setting timeout functions to NULL failed"); dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL); } +#ifdef DBUS_TEST_USE_INTERNAL + typedef struct { DBusLoop *loop; @@ -252,10 +278,14 @@ remove_server_timeout (DBusTimeout *timeout, _dbus_loop_remove_timeout (context->loop, timeout); } +#endif /* DBUS_TEST_USE_INTERNAL */ + dbus_bool_t -test_server_setup (DBusLoop *loop, +test_server_setup (TestMainContext *ctx, DBusServer *server) { +#ifdef DBUS_TEST_USE_INTERNAL + DBusLoop *loop = ctx; ServerData *sd; sd = serverdata_new (loop, server); @@ -293,10 +323,17 @@ test_server_setup (DBusLoop *loop, test_server_shutdown (loop, server); return FALSE; + +#else /* !DBUS_TEST_USE_INTERNAL */ + + dbus_server_setup_with_g_main (server, ctx); + return TRUE; + +#endif /* !DBUS_TEST_USE_INTERNAL */ } void -test_server_shutdown (DBusLoop *loop, +test_server_shutdown (TestMainContext *ctx, DBusServer *server) { dbus_server_disconnect (server); @@ -305,11 +342,51 @@ test_server_shutdown (DBusLoop *loop, NULL, NULL, NULL, NULL, NULL)) - _dbus_assert_not_reached ("setting watch functions to NULL failed"); + die ("setting watch functions to NULL failed"); if (!dbus_server_set_timeout_functions (server, NULL, NULL, NULL, NULL, NULL)) - _dbus_assert_not_reached ("setting timeout functions to NULL failed"); + die ("setting timeout functions to NULL failed"); +} + +TestMainContext * +test_main_context_get (void) +{ +#ifdef DBUS_TEST_USE_INTERNAL + return _dbus_loop_new (); +#else + /* I suspect dbus-glib relies the default main context in some places */ + return g_main_context_ref (g_main_context_default ()); +#endif +} + +TestMainContext * +test_main_context_ref (TestMainContext *ctx) +{ +#ifdef DBUS_TEST_USE_INTERNAL + return _dbus_loop_ref (ctx); +#else + return g_main_context_ref (ctx); +#endif +} + +void test_main_context_unref (TestMainContext *ctx) +{ +#ifdef DBUS_TEST_USE_INTERNAL + _dbus_loop_unref (ctx); +#else + g_main_context_unref (ctx); +#endif +} + +void test_main_context_iterate (TestMainContext *ctx, + dbus_bool_t may_block) +{ +#ifdef DBUS_TEST_USE_INTERNAL + _dbus_loop_iterate (ctx, may_block); +#else + g_main_context_iteration (ctx, may_block); +#endif } diff --git a/test/test-utils.h b/test/test-utils.h index 8d5357e..0d3f369 100644 --- a/test/test-utils.h +++ b/test/test-utils.h @@ -1,21 +1,38 @@ #ifndef TEST_UTILS_H #define TEST_UTILS_H -#include + #include #include -#include -#include -dbus_bool_t test_connection_setup (DBusLoop *loop, +#include + +#ifdef DBUS_TEST_USE_INTERNAL + +# include +# include + typedef DBusLoop TestMainContext; + +#else /* !DBUS_TEST_USE_INTERNAL */ + +# include + typedef GMainContext TestMainContext; + +#endif /* !DBUS_TEST_USE_INTERNAL */ + +TestMainContext *test_main_context_get (void); +TestMainContext *test_main_context_ref (TestMainContext *ctx); +void test_main_context_unref (TestMainContext *ctx); +void test_main_context_iterate (TestMainContext *ctx, + dbus_bool_t may_block); + +dbus_bool_t test_connection_setup (TestMainContext *ctx, DBusConnection *connection); -void test_connection_shutdown (DBusLoop *loop, +void test_connection_shutdown (TestMainContext *ctx, DBusConnection *connection); -void test_connection_dispatch_all_messages (DBusConnection *connection); -dbus_bool_t test_connection_dispatch_one_message (DBusConnection *connection); -dbus_bool_t test_server_setup (DBusLoop *loop, +dbus_bool_t test_server_setup (TestMainContext *ctx, DBusServer *server); -void test_server_shutdown (DBusLoop *loop, +void test_server_shutdown (TestMainContext *ctx, DBusServer *server); #endif -- 1.8.4.rc3