diff options
author | Pedro Alvarez <pedro.alvarez@codethink.co.uk> | 2014-03-31 14:51:51 +0000 |
---|---|---|
committer | Pedro Alvarez <pedro.alvarez@codethink.co.uk> | 2014-03-31 14:51:51 +0000 |
commit | f2e4718bf42f507684949ebf484aa94df42abbbe (patch) | |
tree | 7f6266a7e45144490b18eb6c71a953e8e0196eb9 /test | |
parent | a0f3687d650c8c73134f318820f4978a4ff3e9e2 (diff) | |
parent | 6528b0973bb09b0b407a5e169fdcc519087cc58d (diff) | |
download | dbus-baserock/v1.8.0.tar.gz |
Merge tag 'dbus-1.8.0' into baserock/pedroalvarez/genivibaserock/v1.8.0
dbus-1.8.0
Diffstat (limited to 'test')
-rw-r--r-- | test/.gitignore | 6 | ||||
-rw-r--r-- | test/Makefile.am | 161 | ||||
-rw-r--r-- | test/break-loader.c | 2 | ||||
-rw-r--r-- | test/corrupt.c | 37 | ||||
-rw-r--r-- | test/data/valid-config-files/incoming-limit.conf.in (renamed from test/data/valid-config-files/incoming-limit.conf) | 2 | ||||
-rw-r--r-- | test/dbus-daemon-eavesdrop.c | 36 | ||||
-rw-r--r-- | test/dbus-daemon.c | 163 | ||||
-rw-r--r-- | test/internals/printf.c | 88 | ||||
-rw-r--r-- | test/internals/refs.c | 1 | ||||
-rw-r--r-- | test/internals/syslog.c | 10 | ||||
-rw-r--r-- | test/loopback.c | 85 | ||||
-rw-r--r-- | test/manual-authz.c | 409 | ||||
-rw-r--r-- | test/marshal.c | 28 | ||||
-rw-r--r-- | test/name-test/.gitignore | 2 | ||||
-rw-r--r-- | test/name-test/Makefile.am | 23 | ||||
-rwxr-xr-x | test/name-test/run-test-systemserver.sh | 5 | ||||
-rwxr-xr-x | test/name-test/run-test.sh | 5 | ||||
-rw-r--r-- | test/name-test/test-autolaunch.c | 11 | ||||
-rw-r--r-- | test/name-test/test-threads-init.c | 14 | ||||
-rw-r--r-- | test/relay.c | 25 | ||||
-rw-r--r-- | test/shell-test.c | 2 | ||||
-rw-r--r-- | test/spawn-test.c | 4 | ||||
-rw-r--r-- | test/syntax.c | 6 | ||||
-rw-r--r-- | test/test-exit.c | 2 | ||||
-rw-r--r-- | test/test-utils.c | 93 | ||||
-rw-r--r-- | test/test-utils.h | 39 |
26 files changed, 1101 insertions, 158 deletions
diff --git a/test/.gitignore b/test/.gitignore index 1337de64..de1dfe14 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -7,21 +7,21 @@ Makefile.in *.o echo-server echo-client -bus-test +test-bus unbase64 *.bb *.bbg *.da *.gcov break-loader -spawn-test +test-spawn test-corrupt test-exit test-segfault test-service test-sleep-forever decode-gcov -shell-test +test-shell test-shell-service test-names test-loopback diff --git a/test/Makefile.am b/test/Makefile.am index 88a0e8cd..cec5cdab 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -4,8 +4,10 @@ SUBDIRS= . name-test DIST_SUBDIRS=name-test +# CPPFLAGS for binaries that are normally dynamic AM_CPPFLAGS = \ -I$(top_srcdir) \ + $(DBUS_STATIC_BUILD_CPPFLAGS) \ $(GLIB_CFLAGS) \ $(DBUS_GLIB_CFLAGS) \ $(NULL) @@ -13,28 +15,53 @@ AM_CPPFLAGS = \ # improve backtraces from test stuff AM_LDFLAGS = @R_DYNAMIC_LDFLAG@ +# CPPFLAGS for binaries that are always static 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_cppflags = $(AM_CPPFLAGS) +testutils_shared_if_possible_libs = libdbus-testutils.la +else +testutils_shared_if_possible_cppflags = $(static_cppflags) +testutils_shared_if_possible_libs = 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_BUILD_TESTS +if DBUS_ENABLE_EMBEDDED_TESTS ## break-loader removed for now ## these binaries are used in tests but are not themselves tests TEST_BINARIES = \ - spawn-test \ + test-spawn \ test-exit \ test-names \ test-segfault \ @@ -47,44 +74,50 @@ TEST_BINARIES = \ ## order, but we don't want to run them til we arrive in this directory, ## since they depend on stuff from this directory TESTS = \ - ../bus/bus-test$(EXEEXT) \ - ../bus/bus-test-system$(EXEEXT) \ - ../dbus/dbus-test$(EXEEXT) \ + ../bus/test-bus$(EXEEXT) \ + ../bus/test-bus-system$(EXEEXT) \ + ../dbus/test-dbus$(EXEEXT) \ $(NULL) if DBUS_UNIX -TESTS += ../bus/bus-test-launch-helper$(EXEEXT) +TESTS += ../bus/test-bus-launch-helper$(EXEEXT) endif -else !DBUS_BUILD_TESTS +else !DBUS_ENABLE_EMBEDDED_TESTS TEST_BINARIES= TESTS= -endif !DBUS_BUILD_TESTS +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 -shell_test_CPPFLAGS = $(static_cppflags) -shell_test_LDADD = libdbus-testutils.la -spawn_test_CPPFLAGS = $(static_cppflags) -spawn_test_LDADD = $(top_builddir)/dbus/libdbus-internal.la +test_shell_service_LDADD = libdbus-testutils-internal.la +test_shell_SOURCES = shell-test.c +test_shell_CPPFLAGS = $(static_cppflags) +test_shell_LDADD = libdbus-testutils-internal.la +test_spawn_SOURCES = spawn-test.c +test_spawn_CPPFLAGS = $(static_cppflags) +test_spawn_LDADD = $(top_builddir)/dbus/libdbus-internal.la + +test_printf_SOURCES = internals/printf.c +test_printf_CPPFLAGS = $(static_cppflags) +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 @@ -93,7 +126,10 @@ testexecdir = $(libdir)/dbus-1.0/test testexec_PROGRAMS = installable_tests = \ - shell-test \ + test-shell \ + test-printf \ + $(NULL) +installable_manual_tests = \ $(NULL) if DBUS_WITH_GLIB @@ -108,66 +144,88 @@ installable_tests += \ test-syntax \ test-syslog \ $(NULL) +installable_manual_tests += \ + manual-authz \ + $(NULL) endif DBUS_WITH_GLIB installcheck_tests = installcheck_environment = \ + XDG_RUNTIME_DIR=@abs_top_builddir@/test/XDG_RUNTIME_DIR \ DBUS_TEST_DAEMON=$(DESTDIR)$(DBUS_DAEMONDIR)/dbus-daemon$(EXEEXT) \ DBUS_TEST_HOMEDIR=@abs_top_builddir@/dbus \ DBUS_TEST_SYSCONFDIR=$(DESTDIR)$(sysconfdir) TESTS_ENVIRONMENT = \ - DBUS_BLOCK_ON_ABORT=1 \ + XDG_RUNTIME_DIR=@abs_top_builddir@/test/XDG_RUNTIME_DIR \ DBUS_FATAL_WARNINGS=1 \ DBUS_TEST_DAEMON=@abs_top_builddir@/bus/dbus-daemon$(EXEEXT) \ DBUS_TEST_DATA=@abs_top_builddir@/test/data \ DBUS_TEST_HOMEDIR=@abs_top_builddir@/dbus \ $(NULL) +manual_authz_SOURCES = manual-authz.c +manual_authz_CPPFLAGS = $(testutils_shared_if_possible_cppflags) +manual_authz_LDADD = \ + $(testutils_shared_if_possible_libs) \ + $(GLIB_LIBS) \ + $(NULL) + test_corrupt_SOURCES = corrupt.c -test_corrupt_LDADD = $(top_builddir)/dbus/libdbus-1.la \ +test_corrupt_CPPFLAGS = $(testutils_shared_if_possible_cppflags) +test_corrupt_LDADD = \ + $(testutils_shared_if_possible_libs) \ $(GLIB_LIBS) \ - $(DBUS_GLIB_LIBS) + $(NULL) test_loopback_SOURCES = loopback.c -test_loopback_LDADD = $(top_builddir)/dbus/libdbus-1.la \ +test_loopback_CPPFLAGS = $(testutils_shared_if_possible_cppflags) +test_loopback_LDADD = \ + $(testutils_shared_if_possible_libs) \ $(GLIB_LIBS) \ - $(DBUS_GLIB_LIBS) + $(NULL) test_relay_SOURCES = relay.c -test_relay_LDADD = $(top_builddir)/dbus/libdbus-1.la \ +test_relay_CPPFLAGS = $(testutils_shared_if_possible_cppflags) +test_relay_LDADD = \ + $(testutils_shared_if_possible_libs) \ $(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_CPPFLAGS = $(testutils_shared_if_possible_cppflags) +test_dbus_daemon_LDADD = \ + $(testutils_shared_if_possible_libs) \ $(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_CPPFLAGS = $(testutils_shared_if_possible_cppflags) +test_dbus_daemon_eavesdrop_LDADD = \ + $(testutils_shared_if_possible_libs) \ $(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) installcheck_tests += $(installable_tests) if DBUS_ENABLE_INSTALLED_TESTS - testexec_PROGRAMS += $(installable_tests) + testexec_PROGRAMS += $(installable_tests) $(installable_manual_tests) else !DBUS_ENABLE_INSTALLED_TESTS - noinst_PROGRAMS += $(installable_tests) + noinst_PROGRAMS += $(installable_tests) $(installable_manual_tests) endif !DBUS_ENABLE_INSTALLED_TESTS endif DBUS_ENABLE_MODULAR_TESTS @@ -191,6 +249,7 @@ in_data = \ data/valid-config-files-system/debug-allow-all-pass.conf.in \ data/valid-config-files/debug-allow-all-sha1.conf.in \ data/valid-config-files/debug-allow-all.conf.in \ + data/valid-config-files/incoming-limit.conf.in \ data/invalid-service-files-system/org.freedesktop.DBus.TestSuiteNoExec.service.in \ data/invalid-service-files-system/org.freedesktop.DBus.TestSuiteNoService.service.in \ data/invalid-service-files-system/org.freedesktop.DBus.TestSuiteNoUser.service.in \ @@ -262,7 +321,6 @@ static_data = \ data/valid-config-files/basic.conf \ data/valid-config-files/basic.d/basic.conf \ data/valid-config-files/entities.conf \ - data/valid-config-files/incoming-limit.conf \ data/valid-config-files/many-rules.conf \ data/valid-config-files/system.d/test.conf \ data/valid-messages/array-of-array-of-uint32.message \ @@ -287,14 +345,15 @@ EXTRA_DIST += $(static_data) ## copy tests to builddir so that generated tests and static tests ## are all in one place. all-local: - $(AM_V_at)$(INSTALL) -d data/valid-config-files/session.d + $(AM_V_at)$(MKDIR_P) data/valid-config-files/session.d $(AM_V_at)set -e && \ if test $(srcdir) = . || test $(srcdir) -ef .; then \ echo '-- No need to copy test data as srcdir = builddir'; \ else \ for F in $(static_data); do \ - $(INSTALL) -d $${F%/*}; \ - $(INSTALL_DATA) $(srcdir)/$$F $$F; \ + $(MKDIR_P) $${F%/*}; \ + rm -f $$F; \ + cp $(srcdir)/$$F $$F; \ done; \ fi @@ -312,12 +371,8 @@ imported_data = \ $(NULL) noinst_DATA = $(imported_data) -CLEANFILES = $(noinst_DATA) - -data/valid-config-files/session.conf: $(top_builddir)/bus/session.conf - $(AM_V_at)$(INSTALL) -d data/valid-config-files - $(AM_V_GEN)$(INSTALL_DATA) $< $@ +CLEANFILES = $(noinst_DATA) XDG_RUNTIME_DIR -data/valid-config-files/system.conf: $(top_builddir)/bus/system.conf - $(AM_V_at)$(INSTALL) -d data/valid-config-files - $(AM_V_GEN)$(INSTALL_DATA) $< $@ +$(imported_data): data/valid-config-files/%.conf: $(top_builddir)/bus/%.conf + $(AM_V_at)$(MKDIR_P) data/valid-config-files + $(AM_V_GEN)cp $< $@ diff --git a/test/break-loader.c b/test/break-loader.c index e62b8c20..1e406fc3 100644 --- a/test/break-loader.c +++ b/test/break-loader.c @@ -33,12 +33,10 @@ #include <sys/wait.h> #include <string.h> -#define DBUS_COMPILATION #include <dbus/dbus-string.h> #include <dbus/dbus-internals.h> #include <dbus/dbus-test.h> #include <dbus/dbus-marshal-basic.h> -#undef DBUS_COMPILATION static DBusString failure_dir; static int total_attempts; diff --git a/test/corrupt.c b/test/corrupt.c index 02495901..1a7d4460 100644 --- a/test/corrupt.c +++ b/test/corrupt.c @@ -30,10 +30,12 @@ #include <gio/gio.h> #include <dbus/dbus.h> -#include <dbus/dbus-glib-lowlevel.h> + +#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); @@ -246,6 +249,16 @@ test_corrupt (Fixture *f, "/org/freedesktop/DBus/Local"); dbus_message_unref (incoming); + + /* Free the DBusConnection before the GSocket, because GSocket is + * going to close our fd. GSocket tolerates closing an already-closed + * fd, whereas DBusLoop + DBusSocketSetEpoll doesn't. On Unix + * we could use dup() but that isn't portable to Windows :-( + */ + dbus_connection_close (f->server_conn); + dbus_connection_unref (f->server_conn); + f->server_conn = NULL; + g_object_unref (socket); } @@ -308,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); @@ -325,6 +338,12 @@ test_byte_order (Fixture *f, "/org/freedesktop/DBus/Local"); dbus_message_unref (message); + + /* Free the DBusConnection before the GSocket, as above. */ + dbus_connection_close (f->server_conn); + dbus_connection_unref (f->server_conn); + f->server_conn = NULL; + g_object_unref (socket); } @@ -352,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/data/valid-config-files/incoming-limit.conf b/test/data/valid-config-files/incoming-limit.conf.in index abfab3f7..40ff2244 100644 --- a/test/data/valid-config-files/incoming-limit.conf +++ b/test/data/valid-config-files/incoming-limit.conf.in @@ -3,7 +3,7 @@ <busconfig> <!-- Our well-known bus type, don't change this --> <type>session</type> - <listen>unix:tmpdir=/tmp</listen> + <listen>@TEST_LISTEN@</listen> <policy context="default"> <!-- Allow everything to be sent --> diff --git a/test/dbus-daemon-eavesdrop.c b/test/dbus-daemon-eavesdrop.c index 0bd923d2..bc5ba121 100644 --- a/test/dbus-daemon-eavesdrop.c +++ b/test/dbus-daemon-eavesdrop.c @@ -30,7 +30,6 @@ #include <glib.h> #include <dbus/dbus.h> -#include <dbus/dbus-glib-lowlevel.h> #include <string.h> @@ -42,6 +41,8 @@ # include <unistd.h> #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); @@ -404,17 +409,24 @@ setup (Fixture *f, config = g_strdup ("--session"); } - address = spawn_dbus_daemon (dbus_daemon, config, &f->daemon_pid); + 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); - 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 +444,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 +464,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 +484,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 +546,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 cc871530..c883425e 100644 --- a/test/dbus-daemon.c +++ b/test/dbus-daemon.c @@ -29,7 +29,6 @@ #include <glib.h> #include <dbus/dbus.h> -#include <dbus/dbus-glib-lowlevel.h> #include <string.h> @@ -39,11 +38,16 @@ #else # include <signal.h> # include <unistd.h> +# include <sys/types.h> #endif +#include "test-utils.h" + typedef struct { gboolean skip; + TestMainContext *ctx; + DBusError e; GError *ge; @@ -126,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; @@ -141,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; } @@ -183,11 +188,20 @@ setup (Fixture *f, 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", @@ -221,13 +235,20 @@ setup (Fixture *f, if (dbus_daemon == NULL) dbus_daemon = g_strdup ("dbus-daemon"); - address = spawn_dbus_daemon (dbus_daemon, arg, &f->daemon_pid); + 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 (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); } @@ -301,7 +322,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 (); @@ -310,6 +331,131 @@ test_echo (Fixture *f, } static void +pending_call_store_reply (DBusPendingCall *pc, + void *data) +{ + DBusMessage **message_p = data; + + *message_p = dbus_pending_call_steal_reply (pc); + g_assert (*message_p != NULL); +} + +static void +test_creds (Fixture *f, + gconstpointer context) +{ + const char *unique = dbus_bus_get_unique_name (f->left_conn); + DBusMessage *m = dbus_message_new_method_call (DBUS_SERVICE_DBUS, + DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetConnectionCredentials"); + DBusPendingCall *pc; + DBusMessageIter args_iter; + DBusMessageIter arr_iter; + DBusMessageIter pair_iter; + DBusMessageIter var_iter; + enum { + SEEN_UNIX_USER = 1, + SEEN_PID = 2, + SEEN_WINDOWS_SID = 4 + } seen = 0; + + if (m == NULL) + g_error ("OOM"); + + if (!dbus_message_append_args (m, + DBUS_TYPE_STRING, &unique, + DBUS_TYPE_INVALID)) + g_error ("OOM"); + + if (!dbus_connection_send_with_reply (f->left_conn, m, &pc, + DBUS_TIMEOUT_USE_DEFAULT) || + pc == NULL) + g_error ("OOM"); + + dbus_message_unref (m); + m = NULL; + + if (dbus_pending_call_get_completed (pc)) + pending_call_store_reply (pc, &m); + else if (!dbus_pending_call_set_notify (pc, pending_call_store_reply, + &m, NULL)) + g_error ("OOM"); + + while (m == NULL) + test_main_context_iterate (f->ctx, TRUE); + + g_assert_cmpstr (dbus_message_get_signature (m), ==, "a{sv}"); + + dbus_message_iter_init (m, &args_iter); + g_assert_cmpuint (dbus_message_iter_get_arg_type (&args_iter), ==, + DBUS_TYPE_ARRAY); + g_assert_cmpuint (dbus_message_iter_get_element_type (&args_iter), ==, + DBUS_TYPE_DICT_ENTRY); + dbus_message_iter_recurse (&args_iter, &arr_iter); + + while (dbus_message_iter_get_arg_type (&arr_iter) != DBUS_TYPE_INVALID) + { + const char *name; + + dbus_message_iter_recurse (&arr_iter, &pair_iter); + g_assert_cmpuint (dbus_message_iter_get_arg_type (&pair_iter), ==, + DBUS_TYPE_STRING); + dbus_message_iter_get_basic (&pair_iter, &name); + dbus_message_iter_next (&pair_iter); + g_assert_cmpuint (dbus_message_iter_get_arg_type (&pair_iter), ==, + DBUS_TYPE_VARIANT); + dbus_message_iter_recurse (&pair_iter, &var_iter); + + if (g_strcmp0 (name, "UnixUserID") == 0) + { +#ifdef G_OS_UNIX + guint32 u32; + + g_assert (!(seen & SEEN_UNIX_USER)); + g_assert_cmpuint (dbus_message_iter_get_arg_type (&var_iter), ==, + DBUS_TYPE_UINT32); + dbus_message_iter_get_basic (&var_iter, &u32); + g_message ("%s of this process is %u", name, u32); + g_assert_cmpuint (u32, ==, geteuid ()); + seen |= SEEN_UNIX_USER; +#else + g_assert_not_reached (); +#endif + } + else if (g_strcmp0 (name, "ProcessID") == 0) + { + guint32 u32; + + g_assert (!(seen & SEEN_PID)); + g_assert_cmpuint (dbus_message_iter_get_arg_type (&var_iter), ==, + DBUS_TYPE_UINT32); + dbus_message_iter_get_basic (&var_iter, &u32); + g_message ("%s of this process is %u", name, u32); +#ifdef G_OS_UNIX + g_assert_cmpuint (u32, ==, getpid ()); +#elif defined(G_OS_WIN32) + g_assert_cmpuint (u32, ==, GetCurrentProcessId ()); +#else + g_assert_not_reached (); +#endif + seen |= SEEN_PID; + } + + dbus_message_iter_next (&arr_iter); + } + +#ifdef G_OS_UNIX + g_assert (seen & SEEN_UNIX_USER); + g_assert (seen & SEEN_PID); +#endif + +#ifdef G_OS_WIN32 + /* FIXME: when implemented: + g_assert (seen & SEEN_WINDOWS_SID); + */ +#endif +} + +static void teardown (Fixture *f, gconstpointer context G_GNUC_UNUSED) { @@ -347,6 +493,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 = { @@ -363,6 +511,7 @@ main (int argc, g_test_add ("/echo/session", Fixture, NULL, setup, test_echo, teardown); g_test_add ("/echo/limited", Fixture, &limited_config, setup, test_echo, teardown); + g_test_add ("/creds", Fixture, NULL, setup, test_creds, teardown); return g_test_run (); } diff --git a/test/internals/printf.c b/test/internals/printf.c new file mode 100644 index 00000000..db151518 --- /dev/null +++ b/test/internals/printf.c @@ -0,0 +1,88 @@ +/* Regression test for _dbus_printf_string_upper_bound + * + * Author: Simon McVittie <simon.mcvittie@collabora.co.uk> + * Copyright © 2013 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include <config.h> + +#include <dbus/dbus.h> +#include <dbus/dbus-internals.h> +#include <dbus/dbus-string.h> +#include "test-utils.h" + +#include <stdio.h> +#include <stdlib.h> + +static void +do_test (int minimum, + const char *format, + ...) +{ + va_list ap; + int result; + + va_start (ap, format); + result = _dbus_printf_string_upper_bound (format, ap); + va_end (ap); + + if (result < minimum) + { + fprintf (stderr, "expected at least %d, got %d\n", minimum, result); + abort (); + } +} + +#define X_TIMES_8 "XXXXXXXX" +#define X_TIMES_16 X_TIMES_8 X_TIMES_8 +#define X_TIMES_32 X_TIMES_16 X_TIMES_16 +#define X_TIMES_64 X_TIMES_32 X_TIMES_32 +#define X_TIMES_128 X_TIMES_64 X_TIMES_64 +#define X_TIMES_256 X_TIMES_128 X_TIMES_128 +#define X_TIMES_512 X_TIMES_256 X_TIMES_256 +#define X_TIMES_1024 X_TIMES_512 X_TIMES_512 + +int +main (int argc, + char **argv) +{ + char buf[] = X_TIMES_1024 X_TIMES_1024 X_TIMES_1024 X_TIMES_1024; + int i; + + do_test (1, "%d", 0); + do_test (7, "%d", 1234567); + do_test (3, "%f", 3.5); + + do_test (0, "%s", ""); + do_test (1024, "%s", X_TIMES_1024); + do_test (1025, "%s", X_TIMES_1024 "Y"); + + for (i = 4096; i > 0; i--) + { + buf[i] = '\0'; + do_test (i, "%s", buf); + do_test (i + 3, "%s:%d", buf, 42); + } + + return 0; +} diff --git a/test/internals/refs.c b/test/internals/refs.c index db43a4da..202dc043 100644 --- a/test/internals/refs.c +++ b/test/internals/refs.c @@ -29,7 +29,6 @@ #include <glib.h> #include <glib-object.h> -#define DBUS_COMPILATION /* this test uses libdbus-internal */ #include <dbus/dbus.h> #include <dbus/dbus-connection-internal.h> #include <dbus/dbus-mainloop.h> diff --git a/test/internals/syslog.c b/test/internals/syslog.c index 4f6b7c22..7e0eae79 100644 --- a/test/internals/syslog.c +++ b/test/internals/syslog.c @@ -30,7 +30,6 @@ #include <glib.h> -#define DBUS_COMPILATION /* this test uses libdbus-internal */ #include <dbus/dbus.h> #include <dbus/dbus-sysdeps.h> @@ -52,9 +51,10 @@ static void test_syslog (Fixture *f, gconstpointer data) { +#ifndef G_OS_WIN32 if (g_test_trap_fork (0, 0)) { - _dbus_init_system_log (); + _dbus_init_system_log (FALSE); _dbus_system_log (DBUS_SYSTEM_LOG_FATAL, MESSAGE "%d", 23); /* should not be reached: exit 0 so the assertion in the main process * will fail */ @@ -66,7 +66,7 @@ test_syslog (Fixture *f, if (g_test_trap_fork (0, 0)) { - _dbus_init_system_log (); + _dbus_init_system_log (FALSE); _dbus_system_log (DBUS_SYSTEM_LOG_INFO, MESSAGE "%d", 42); _dbus_system_log (DBUS_SYSTEM_LOG_SECURITY, MESSAGE "%d", 666); exit (0); @@ -74,9 +74,9 @@ test_syslog (Fixture *f, g_test_trap_assert_passed (); g_test_trap_assert_stderr ("*" MESSAGE "42\n*" MESSAGE "666\n*"); - +#endif /* manual test (this is the best we can do on Windows) */ - _dbus_init_system_log (); + _dbus_init_system_log (FALSE); _dbus_system_log (DBUS_SYSTEM_LOG_INFO, MESSAGE "%d", 42); _dbus_system_log (DBUS_SYSTEM_LOG_SECURITY, MESSAGE "%d", 666); } diff --git a/test/loopback.c b/test/loopback.c index d0d69c82..7526d8d2 100644 --- a/test/loopback.c +++ b/test/loopback.c @@ -1,7 +1,7 @@ /* Simple sanity-check for loopback through TCP and Unix sockets. * * Author: Simon McVittie <simon.mcvittie@collabora.co.uk> - * Copyright © 2010-2011 Nokia Corporation + * Copyright © 2010-2012 Nokia Corporation * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation files @@ -29,9 +29,13 @@ #include <glib.h> #include <dbus/dbus.h> -#include <dbus/dbus-glib-lowlevel.h> + +#include <string.h> + +#include "test-utils.h" typedef struct { + TestMainContext *ctx; DBusError e; DBusServer *server; @@ -72,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); @@ -83,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); @@ -92,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 @@ -105,13 +110,73 @@ 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); + } +} + +static void +test_bad_guid (Fixture *f, + gconstpointer addr G_GNUC_UNUSED) +{ + DBusMessage *incoming; + gchar *address = g_strdup (dbus_server_get_address (f->server)); + gchar *guid; + + g_test_bug ("39720"); + + g_assert (f->server_conn == NULL); + + g_assert (strstr (address, "guid=") != NULL); + guid = strstr (address, "guid="); + g_assert_cmpuint (strlen (guid), >=, 5 + 32); + + /* Change the first char of the guid to something different */ + if (guid[5] == '0') + guid[5] = 'f'; + else + guid[5] = '0'; + + f->client_conn = dbus_connection_open_private (address, &f->e); + assert_no_error (&f->e); + g_assert (f->client_conn != NULL); + test_connection_setup (f->ctx, f->client_conn); + + while (f->server_conn == NULL) + { + g_print ("."); + test_main_context_iterate (f->ctx, TRUE); + } + + /* We get disconnected */ + + while (g_queue_is_empty (&f->server_messages)) + { + g_print ("."); + test_main_context_iterate (f->ctx, TRUE); } + + g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1); + + incoming = g_queue_pop_head (&f->server_messages); + + g_assert (!dbus_message_contains_unix_fds (incoming)); + g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL); + g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL); + g_assert_cmpstr (dbus_message_get_interface (incoming), ==, + DBUS_INTERFACE_LOCAL); + g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Disconnected"); + g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL); + g_assert_cmpstr (dbus_message_get_signature (incoming), ==, ""); + g_assert_cmpstr (dbus_message_get_path (incoming), ==, DBUS_PATH_LOCAL); + + dbus_message_unref (incoming); + + g_free (address); } static void @@ -135,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); @@ -182,6 +247,8 @@ teardown (Fixture *f, dbus_server_unref (f->server); f->server = NULL; } + + test_main_context_unref (f->ctx); } int @@ -189,6 +256,7 @@ main (int argc, char **argv) { g_test_init (&argc, &argv, NULL); + g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id="); g_test_add ("/connect/tcp", Fixture, "tcp:host=127.0.0.1", setup, test_connect, teardown); @@ -207,5 +275,8 @@ main (int argc, test_message, teardown); #endif + g_test_add ("/message/bad-guid", Fixture, "tcp:host=127.0.0.1", setup, + test_bad_guid, teardown); + return g_test_run (); } diff --git a/test/manual-authz.c b/test/manual-authz.c new file mode 100644 index 00000000..f9e3688e --- /dev/null +++ b/test/manual-authz.c @@ -0,0 +1,409 @@ +/* Simple sanity-check for authentication and authorization. + * + * Copyright © 2010-2011 Nokia Corporation + * Copyright © 2012 Collabora Ltd. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include <config.h> + +#include <glib.h> + +#include <dbus/dbus.h> + +#ifdef G_OS_UNIX +#include <unistd.h> +#include <sys/types.h> +#endif + +#include "test-utils.h" + +typedef struct { + DBusError e; + TestMainContext *ctx; + + DBusServer *normal_server; + DBusServer *anon_allowed_server; + DBusServer *anon_only_server; + DBusServer *anon_mech_only_server; + DBusServer *anon_disallowed_server; + DBusServer *permissive_server; + DBusServer *unhappy_server; + DBusServer *same_uid_server; + DBusServer *same_uid_or_anon_server; +} Fixture; + +static void oom (void) G_GNUC_NORETURN; +static void +oom (void) +{ + g_error ("out of memory"); +} + +static void +assert_no_error (const DBusError *e) +{ + if (G_UNLIKELY (dbus_error_is_set (e))) + g_error ("expected success but got error: %s: %s", e->name, e->message); +} + +static DBusHandlerResult +server_message_cb (DBusConnection *conn, + DBusMessage *message, + void *data) +{ + if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected")) + { + dbus_connection_unref (conn); + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL) + { + DBusMessage *reply = dbus_message_new_method_return (message); + const char *hello = "Hello, world!"; + unsigned long uid; + char *sid; + + if (dbus_connection_get_unix_user (conn, &uid)) + { + g_message ("message from uid %lu", uid); + } + else if (dbus_connection_get_windows_user (conn, &sid)) + { + if (sid == NULL) + oom (); + + g_message ("message from sid \"%s\"", sid); + dbus_free (sid); + } + else if (dbus_connection_get_is_anonymous (conn)) + { + g_message ("message from Anonymous"); + } + else + { + g_message ("message from ... someone?"); + } + + if (reply == NULL) + oom (); + + if (!dbus_message_append_args (reply, + DBUS_TYPE_STRING, &hello, + DBUS_TYPE_INVALID)) + oom (); + + if (!dbus_connection_send (conn, reply, NULL)) + oom (); + + dbus_message_unref (reply); + + return DBUS_HANDLER_RESULT_HANDLED; + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + +static dbus_bool_t +permissive_unix_func (DBusConnection *conn, + unsigned long uid, + void *data) +{ + g_message ("accepting Unix user %lu", uid); + return TRUE; +} + +static dbus_bool_t +permissive_win_func (DBusConnection *conn, + const char *sid, + void *data) +{ + g_message ("accepting Windows user \"%s\"", sid); + return TRUE; +} + +static dbus_bool_t +broken_unix_func (DBusConnection *conn, + unsigned long uid, + void *data) +{ + g_error ("libdbus called the Unix user function for an ANONYMOUS-only " + "connection"); + return FALSE; +} + +static dbus_bool_t +broken_win_func (DBusConnection *conn, + const char *sid, + void *data) +{ + g_error ("libdbus called the Windows user function for an ANONYMOUS-only " + "connection"); + return FALSE; +} + +static dbus_bool_t +unhappy_unix_func (DBusConnection *conn, + unsigned long uid, + void *data) +{ + g_message ("rejecting Unix user %lu", uid); + return FALSE; +} + +static dbus_bool_t +unhappy_win_func (DBusConnection *conn, + const char *sid, + void *data) +{ + g_message ("rejecting Windows user \"%s\"", sid); + return FALSE; +} + +static dbus_bool_t +same_uid_unix_func (DBusConnection *conn, + unsigned long uid, + void *data) +{ + g_message ("checking whether Unix user %lu owns this process", uid); + /* I'd use _dbus_unix_user_is_process_owner(), but it's private... */ +#ifdef G_OS_UNIX + return (geteuid () == uid); +#else + return FALSE; +#endif +} + +static dbus_bool_t +same_uid_win_func (DBusConnection *conn, + const char *sid, + void *data) +{ + g_message ("checking whether Windows user \"%s\" owns this process", sid); + g_message ("Stub implementation consistent with dbus-sysdeps-util-win: " + "assume they do"); + return TRUE; +} + +static void +new_conn_cb (DBusServer *server, + DBusConnection *conn, + void *data) +{ + Fixture *f = data; + + dbus_connection_ref (conn); + test_connection_setup (f->ctx, conn); + + if (!dbus_connection_add_filter (conn, server_message_cb, f, NULL)) + oom (); + + if (server == f->normal_server) + { + } + else if (server == f->anon_allowed_server) + { + dbus_connection_set_allow_anonymous (conn, TRUE); + } + else if (server == f->anon_only_server) + { + dbus_connection_set_allow_anonymous (conn, TRUE); + + dbus_connection_set_unix_user_function (conn, unhappy_unix_func, + f, NULL); + dbus_connection_set_windows_user_function (conn, unhappy_win_func, + f, NULL); + } + else if (server == f->anon_mech_only_server) + { + dbus_connection_set_allow_anonymous (conn, TRUE); + + /* should never get called */ + dbus_connection_set_unix_user_function (conn, broken_unix_func, + f, NULL); + dbus_connection_set_windows_user_function (conn, broken_win_func, + f, NULL); + } + else if (server == f->anon_disallowed_server) + { + dbus_connection_set_allow_anonymous (conn, FALSE); + + /* should never get called */ + dbus_connection_set_unix_user_function (conn, broken_unix_func, + f, NULL); + dbus_connection_set_windows_user_function (conn, broken_win_func, + f, NULL); + } + else if (server == f->permissive_server) + { + dbus_connection_set_unix_user_function (conn, permissive_unix_func, + f, NULL); + dbus_connection_set_windows_user_function (conn, permissive_win_func, + f, NULL); + } + else if (server == f->unhappy_server) + { + dbus_connection_set_unix_user_function (conn, unhappy_unix_func, + f, NULL); + dbus_connection_set_windows_user_function (conn, unhappy_win_func, + f, NULL); + } + else if (server == f->same_uid_server) + { + dbus_connection_set_unix_user_function (conn, same_uid_unix_func, + f, NULL); + dbus_connection_set_windows_user_function (conn, same_uid_win_func, + f, NULL); + } + else if (server == f->same_uid_or_anon_server) + { + dbus_connection_set_allow_anonymous (conn, TRUE); + + dbus_connection_set_unix_user_function (conn, same_uid_unix_func, + f, NULL); + dbus_connection_set_windows_user_function (conn, same_uid_win_func, + f, NULL); + } + else + { + g_assert_not_reached (); + } +} + +static void +setup (Fixture *f, + const gchar *listen_addr) +{ + const char *only_anon[] = { "ANONYMOUS", NULL }; + char *connect_addr; + + f->normal_server = dbus_server_listen (listen_addr, &f->e); + assert_no_error (&f->e); + g_assert (f->normal_server != NULL); + dbus_server_set_new_connection_function (f->normal_server, + new_conn_cb, f, 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); + + f->anon_allowed_server = dbus_server_listen (listen_addr, &f->e); + assert_no_error (&f->e); + g_assert (f->anon_allowed_server != NULL); + dbus_server_set_new_connection_function (f->anon_allowed_server, + new_conn_cb, f, 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); + + f->anon_only_server = dbus_server_listen (listen_addr, &f->e); + assert_no_error (&f->e); + g_assert (f->anon_only_server != NULL); + dbus_server_set_new_connection_function (f->anon_only_server, + new_conn_cb, f, 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); + + f->anon_mech_only_server = dbus_server_listen (listen_addr, &f->e); + assert_no_error (&f->e); + g_assert (f->anon_mech_only_server != NULL); + 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); + 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); + + f->anon_disallowed_server = dbus_server_listen (listen_addr, &f->e); + assert_no_error (&f->e); + g_assert (f->anon_disallowed_server != NULL); + 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); + 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); + + f->permissive_server = dbus_server_listen (listen_addr, &f->e); + assert_no_error (&f->e); + g_assert (f->permissive_server != NULL); + dbus_server_set_new_connection_function (f->permissive_server, + new_conn_cb, f, 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); + + f->unhappy_server = dbus_server_listen (listen_addr, &f->e); + assert_no_error (&f->e); + g_assert (f->unhappy_server != NULL); + dbus_server_set_new_connection_function (f->unhappy_server, + new_conn_cb, f, 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); + + f->same_uid_server = dbus_server_listen (listen_addr, &f->e); + assert_no_error (&f->e); + g_assert (f->same_uid_server != NULL); + dbus_server_set_new_connection_function (f->same_uid_server, + new_conn_cb, f, 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); + + f->same_uid_or_anon_server = dbus_server_listen (listen_addr, &f->e); + assert_no_error (&f->e); + 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); + 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); +} + +int +main (int argc, + char **argv) +{ + Fixture f = { DBUS_ERROR_INIT, test_main_context_get () }; + + if (argc >= 2) + setup (&f, argv[1]); + else + setup (&f, "tcp:host=127.0.0.1"); + + for (;;) + test_main_context_iterate (f.ctx, TRUE); + + /* never returns */ +} diff --git a/test/marshal.c b/test/marshal.c index e9ac7e30..d74e7671 100644 --- a/test/marshal.c +++ b/test/marshal.c @@ -27,9 +27,9 @@ #include <config.h> #include <glib.h> +#include <string.h> #include <dbus/dbus.h> -#include <dbus/dbus-glib-lowlevel.h> typedef struct { DBusError e; @@ -244,14 +244,30 @@ int main (int argc, char **argv) { + int ret; + char *aligned_le_blob; + char *aligned_be_blob; + g_test_init (&argc, &argv, NULL); - g_test_add ("/demarshal/le", Fixture, le_blob, setup, test_endian, teardown); - g_test_add ("/demarshal/be", Fixture, be_blob, setup, test_endian, teardown); - g_test_add ("/demarshal/needed/le", Fixture, le_blob, setup, test_needed, + /* We have to pass in a buffer that's at least "default aligned", + * i.e. on GNU systems to 8 or 16. The linker may have only given + * us byte-alignment for the char[] static variables. + */ + aligned_le_blob = g_malloc (sizeof (le_blob)); + memcpy (aligned_le_blob, le_blob, sizeof (le_blob)); + aligned_be_blob = g_malloc (sizeof (be_blob)); + memcpy (aligned_be_blob, be_blob, sizeof (be_blob)); + + g_test_add ("/demarshal/le", Fixture, aligned_le_blob, setup, test_endian, teardown); + g_test_add ("/demarshal/be", Fixture, aligned_be_blob, setup, test_endian, teardown); + g_test_add ("/demarshal/needed/le", Fixture, aligned_le_blob, setup, test_needed, teardown); - g_test_add ("/demarshal/needed/be", Fixture, be_blob, setup, test_needed, + g_test_add ("/demarshal/needed/be", Fixture, aligned_be_blob, setup, test_needed, teardown); - return g_test_run (); + ret = g_test_run (); + g_free (aligned_le_blob); + g_free (aligned_be_blob); + return ret; } diff --git a/test/name-test/.gitignore b/test/name-test/.gitignore index 09f7ad36..2bf313a5 100644 --- a/test/name-test/.gitignore +++ b/test/name-test/.gitignore @@ -3,7 +3,7 @@ Makefile Makefile.in echo-error-output.tmp -run-with-tmp-session-bus.conf +tmp-session-bus.*.conf test-ids test-names test-pending-call-dispatch diff --git a/test/name-test/Makefile.am b/test/name-test/Makefile.am index 6aaf1783..8ed1e160 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 @@ -11,8 +12,16 @@ AM_LDFLAGS = @R_DYNAMIC_LDFLAG@ ## note that TESTS has special meaning (stuff to use in make check) ## so if adding tests not to be run in make check, don't add them to ## TESTS -if DBUS_BUILD_TESTS -TESTS_ENVIRONMENT=DBUS_TOP_BUILDDIR=@abs_top_builddir@ DBUS_TOP_SRCDIR=@abs_top_srcdir@ PYTHON=@PYTHON@ +if DBUS_ENABLE_EMBEDDED_TESTS +TESTS_ENVIRONMENT = \ + DBUS_TOP_BUILDDIR=@abs_top_builddir@ \ + DBUS_TOP_SRCDIR=@abs_top_srcdir@ \ + PYTHON=@PYTHON@ \ + DBUS_TEST_DATA=@abs_top_builddir@/test/data \ + DBUS_TEST_DAEMON=@abs_top_builddir@/bus/dbus-daemon$(EXEEXT) \ + XDG_RUNTIME_DIR=@abs_top_builddir@/test/XDG_RUNTIME_DIR \ + $(NULL) + TESTS=run-test.sh run-test-systemserver.sh else TESTS= @@ -20,7 +29,7 @@ endif EXTRA_DIST=run-test.sh run-test-systemserver.sh test-wait-for-echo.py test-activation-forking.py -if DBUS_BUILD_TESTS +if DBUS_ENABLE_EMBEDDED_TESTS ## we use noinst_PROGRAMS not check_PROGRAMS for TESTS so that we ## build even when not doing "make check" @@ -31,9 +40,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/name-test/run-test-systemserver.sh b/test/name-test/run-test-systemserver.sh index afd1f045..90c03723 100755 --- a/test/name-test/run-test-systemserver.sh +++ b/test/name-test/run-test-systemserver.sh @@ -30,6 +30,11 @@ if test -n "$DBUS_TEST_MONITOR"; then dbus-monitor --session & fi +XDG_RUNTIME_DIR="$DBUS_TOP_BUILDDIR"/test/XDG_RUNTIME_DIR +test -d "$XDG_RUNTIME_DIR" || mkdir "$XDG_RUNTIME_DIR" +chmod 0700 "$XDG_RUNTIME_DIR" +export XDG_RUNTIME_DIR + echo "running test-expected-echo-fail" ${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/tools/dbus-send --print-reply --dest=org.freedesktop.DBus.TestSuiteEchoService /org/freedesktop/TestSuite org.freedesktop.TestSuite.Echo string:hi >echo-error-output.tmp 2>&1 if ! grep -q 'DBus.Error' echo-error-output.tmp; then diff --git a/test/name-test/run-test.sh b/test/name-test/run-test.sh index cad5937e..84379c4a 100755 --- a/test/name-test/run-test.sh +++ b/test/name-test/run-test.sh @@ -30,6 +30,11 @@ if test -n "$DBUS_TEST_MONITOR"; then dbus-monitor --session & fi +XDG_RUNTIME_DIR="$DBUS_TOP_BUILDDIR"/test/XDG_RUNTIME_DIR +test -d "$XDG_RUNTIME_DIR" || mkdir "$XDG_RUNTIME_DIR" +chmod 0700 "$XDG_RUNTIME_DIR" +export XDG_RUNTIME_DIR + echo "running test-ids" ${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/name-test/test-ids || die "test-ids failed" diff --git a/test/name-test/test-autolaunch.c b/test/name-test/test-autolaunch.c index 5e519895..8b5206a6 100644 --- a/test/name-test/test-autolaunch.c +++ b/test/name-test/test-autolaunch.c @@ -1,3 +1,5 @@ +#include "config.h" + #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -14,21 +16,23 @@ main (int argc, char *argv[]) DBusConnection *conn = NULL; DBusError error; - _dbus_setenv ("DBUS_SESSION_BUS_ADDRESS", NULL); + dbus_setenv ("DBUS_SESSION_BUS_ADDRESS", NULL); dbus_error_init (&error); conn = dbus_bus_get (DBUS_BUS_SESSION, &error); #ifdef DBUS_ENABLE_X11_AUTOLAUNCH - if (dbus_error_is_set (&error)) + /* If X11 autolaunch was enabled, we expect dbus-launch to have worked. */ + if (_dbus_getenv ("DISPLAY") != NULL && dbus_error_is_set (&error)) { fprintf (stderr, "*** Failed to autolaunch session bus: %s\n", error.message); dbus_error_free (&error); return 1; } -#else +#endif + /* We don't necessarily expect it to *work* without X (although it might - * for instance on Mac OS it might have used launchd). Just check that the * results are consistent. */ @@ -38,7 +42,6 @@ main (int argc, char *argv[]) fprintf (stderr, "*** Autolaunched session bus, but an error was set!\n"); return 1; } -#endif if (!dbus_error_is_set (&error) && conn == NULL) { diff --git a/test/name-test/test-threads-init.c b/test/name-test/test-threads-init.c index 5e22852a..580ffe14 100644 --- a/test/name-test/test-threads-init.c +++ b/test/name-test/test-threads-init.c @@ -149,11 +149,15 @@ main (int argc, char *argv[]) &dispatch_cond1, &io_path_cond1); - check_mutex_lock (mutex1, mutex2, FALSE); - check_mutex_lock (dispatch_mutex1, dispatch_mutex2, FALSE); - check_mutex_lock (io_path_mutex1, io_path_mutex2, FALSE); - check_condvar_lock (dispatch_cond1, dispatch_cond2, FALSE); - check_condvar_lock (io_path_cond1, io_path_cond2, FALSE); + /* Since 1.7 it is no longer the case that mutex1 != mutex2, because + * initializing global locks automatically initializes locks + * in general. However, it is true that the mutex is not the dummy + * implementation, which is what we really wanted to check here. */ + _dbus_assert (mutex1 != (DBusMutex *) 0xABCDEF); + _dbus_assert (dispatch_mutex1 != (DBusMutex *) 0xABCDEF); + _dbus_assert (dispatch_cond1 != (DBusCondVar *) 0xABCDEF2); + _dbus_assert (io_path_mutex1 != (DBusMutex *) 0xABCDEF); + _dbus_assert (io_path_cond1 != (DBusCondVar *) 0xABCDEF2); _run_iteration (conn); _dbus_connection_test_get_locks (conn, &mutex2, diff --git a/test/relay.c b/test/relay.c index f4129d0a..ecfe4c82 100644 --- a/test/relay.c +++ b/test/relay.c @@ -29,7 +29,8 @@ #include <glib.h> #include <dbus/dbus.h> -#include <dbus/dbus-glib-lowlevel.h> + +#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/shell-test.c b/test/shell-test.c index b479a9c6..d1dc5b5b 100644 --- a/test/shell-test.c +++ b/test/shell-test.c @@ -1,7 +1,7 @@ #include <config.h> #include <stdio.h> #include <stdlib.h> -#define DBUS_COMPILATION + #include <dbus/dbus-internals.h> #include <dbus/dbus-list.h> #include <dbus/dbus-memory.h> diff --git a/test/spawn-test.c b/test/spawn-test.c index f1a55051..723a4889 100644 --- a/test/spawn-test.c +++ b/test/spawn-test.c @@ -1,10 +1,8 @@ #include <config.h> #include <dbus/dbus.h> -#define DBUS_COMPILATION /* cheat and use dbus-sysdeps */ #include <dbus/dbus-sysdeps.h> #include <dbus/dbus-spawn.h> -#undef DBUS_COMPILATION #include <stdio.h> static void @@ -32,7 +30,7 @@ main (int argc, char **argv) argv_copy [i] = argv[i + 1]; argv_copy[argc - 1] = NULL; - if (!_dbus_spawn_async_with_babysitter (NULL, argv_copy, NULL, setup_func, NULL, &error)) + if (!_dbus_spawn_async_with_babysitter (NULL, argv_copy[0], argv_copy, NULL, setup_func, NULL, &error)) { fprintf (stderr, "Could not launch application: \"%s\"\n", error.message); diff --git a/test/syntax.c b/test/syntax.c index 88db9638..e26b3643 100644 --- a/test/syntax.c +++ b/test/syntax.c @@ -178,12 +178,14 @@ const char * const invalid_single_signatures[] = { const char * const valid_strings[] = { "", - "\xc2\xa9", + "\xc2\xa9", /* UTF-8 (c) symbol */ + "\xef\xbf\xbe", /* U+FFFE is reserved but Corrigendum 9 says it's OK */ NULL }; const char * const invalid_strings[] = { - "\xa9", + "\xa9", /* Latin-1 (c) symbol */ + "\xed\xa0\x80", /* UTF-16 surrogates are not valid in UTF-8 */ NULL }; diff --git a/test/test-exit.c b/test/test-exit.c index f3358185..b4f967ae 100644 --- a/test/test-exit.c +++ b/test/test-exit.c @@ -1,3 +1,5 @@ +#include "config.h" + /* This is a process that just exits with a failure code */ int main (int argc, char **argv) diff --git a/test/test-utils.c b/test/test-utils.c index c3c3ed34..9a4f3584 100644 --- a/test/test-utils.c +++ b/test/test-utils.c @@ -1,6 +1,13 @@ #include <config.h> #include "test-utils.h" +#ifndef DBUS_TEST_USE_INTERNAL +# include <dbus/dbus.h> +# include <dbus/dbus-glib-lowlevel.h> +#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 3e1e55e6..0d3f3690 100644 --- a/test/test-utils.h +++ b/test/test-utils.h @@ -1,25 +1,38 @@ #ifndef TEST_UTILS_H #define TEST_UTILS_H -#ifndef DBUS_COMPILATION -#define DBUS_COMPILATION /* Cheat and use private stuff */ -#endif -#include <dbus/dbus.h> + #include <stdio.h> #include <stdlib.h> -#include <dbus/dbus-mainloop.h> -#include <dbus/dbus-internals.h> -#undef DBUS_COMPILATION -dbus_bool_t test_connection_setup (DBusLoop *loop, +#include <dbus/dbus.h> + +#ifdef DBUS_TEST_USE_INTERNAL + +# include <dbus/dbus-mainloop.h> +# include <dbus/dbus-internals.h> + typedef DBusLoop TestMainContext; + +#else /* !DBUS_TEST_USE_INTERNAL */ + +# include <glib.h> + 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 |