summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/Makefile.am26
-rw-r--r--test/data/valid-service-files/org.freedesktop.DBus.TestSuite.PrivServer.service.in4
-rw-r--r--test/name-test/Makefile.am26
-rwxr-xr-xtest/name-test/run-test.sh11
-rw-r--r--test/name-test/test-privserver-client.c116
-rw-r--r--test/name-test/test-privserver.c118
-rw-r--r--test/name-test/test-shutdown.c67
-rw-r--r--test/test-utils.c154
-rw-r--r--test/test-utils.h6
9 files changed, 511 insertions, 17 deletions
diff --git a/test/Makefile.am b/test/Makefile.am
index 56ab66fc..a7f05970 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -1,10 +1,14 @@
## the "name-test" subdir in fact contains a bunch of tests now that need a temporary bus
## to be running to do stuff with. The directory should be renamed.
-SUBDIRS=name-test
+## We want to build the current directory first to pick up the testutils lib
+SUBDIRS= . name-test
DIST_SUBDIRS=name-test
INCLUDES=-I$(top_srcdir) $(DBUS_TEST_CFLAGS)
+libdbus_testutils_la_SOURCES = test-utils.h test-utils.c
+
+noinst_LTLIBRARIES = libdbus-testutils.la
if DBUS_BUILD_TESTS
## break-loader removed for now
@@ -28,22 +32,16 @@ endif
noinst_PROGRAMS= $(TEST_BINARIES) $(GCOV_BINARIES)
test_service_SOURCES= \
- test-service.c \
- test-utils.c \
- test-utils.h
+ test-service.c
test_names_SOURCES= \
- test-names.c \
- test-utils.c \
- test-utils.h
+ test-names.c
##break_loader_SOURCES= \
## break-loader.c
test_shell_service_SOURCES = \
- test-shell-service.c \
- test-utils.c \
- test-utils.h
+ test-shell-service.c
shell_test_SOURCES= \
shell-test.c
@@ -65,15 +63,15 @@ decode_gcov_SOURCES= \
TEST_LIBS=$(DBUS_TEST_LIBS) $(top_builddir)/dbus/libdbus-convenience.la
-test_service_LDADD=$(TEST_LIBS)
+test_service_LDADD=$(TEST_LIBS) libdbus-testutils.la
test_service_LDFLAGS=@R_DYNAMIC_LDFLAG@
-test_names_LDADD=$(TEST_LIBS)
+test_names_LDADD=$(TEST_LIBS) libdbus-testutils.la
test_names_LDFLAGS=@R_DYNAMIC_LDFLAG@
## break_loader_LDADD= $(TEST_LIBS)
## break_loader_LDFLAGS=@R_DYNAMIC_LDFLAG@
-test_shell_service_LDADD=$(TEST_LIBS)
+test_shell_service_LDADD=$(TEST_LIBS) libdbus-testutils.la
test_shell_service_LDFLAGS=@R_DYNAMIC_LDFLAG@
-shell_test_LDADD=$(TEST_LIBS)
+shell_test_LDADD=$(TEST_LIBS) libdbus-testutils.la
shell_test_LDFLAGS=@R_DYNAMIC_LDFLAG@
spawn_test_LDADD=$(TEST_LIBS)
spawn_test_LDFLAGS=@R_DYNAMIC_LDFLAG@
diff --git a/test/data/valid-service-files/org.freedesktop.DBus.TestSuite.PrivServer.service.in b/test/data/valid-service-files/org.freedesktop.DBus.TestSuite.PrivServer.service.in
new file mode 100644
index 00000000..2cbdaa8b
--- /dev/null
+++ b/test/data/valid-service-files/org.freedesktop.DBus.TestSuite.PrivServer.service.in
@@ -0,0 +1,4 @@
+[D-BUS Service]
+Name=org.freedesktop.DBus.TestSuite.PrivServer
+Exec=@TEST_PRIVSERVER_BINARY@
+
diff --git a/test/name-test/Makefile.am b/test/name-test/Makefile.am
index fbc80c42..17e05bec 100644
--- a/test/name-test/Makefile.am
+++ b/test/name-test/Makefile.am
@@ -1,4 +1,4 @@
-INCLUDES=-I$(top_srcdir) $(DBUS_CLIENT_CFLAGS) $(DBUS_GLIB_CFLAGS) -DDBUS_COMPILATION
+INCLUDES=-I$(top_srcdir) $(DBUS_CLIENT_CFLAGS) $(DBUS_GLIB_CFLAGS) $(DBUS_TEST_CFLAGS) -DDBUS_COMPILATION
## 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
@@ -16,7 +16,7 @@ if DBUS_BUILD_TESTS
## we use noinst_PROGRAMS not check_PROGRAMS for TESTS so that we
## build even when not doing "make check"
-noinst_PROGRAMS=test-names test-pending-call-dispatch test-threads-init test-ids
+noinst_PROGRAMS=test-names test-pending-call-dispatch test-threads-init test-ids test-shutdown test-privserver test-privserver-client
test_names_SOURCES= \
test-names.c
@@ -42,5 +42,25 @@ test_ids_SOURCES = \
test_ids_LDADD=$(top_builddir)/dbus/libdbus-convenience.la $(DBUS_TEST_LIBS)
test_ids_LDFLAGS=@R_DYNAMIC_LDFLAG@
-endif
+test_shutdown_SOURCES = \
+ test-shutdown.c
+
+test_shutdown_CFLAGS=
+test_shutdown_LDADD=$(top_builddir)/dbus/libdbus-convenience.la ../libdbus-testutils.la $(DBUS_TEST_LIBS)
+test_shutdown_LDFLAGS=@R_DYNAMIC_LDFLAG@
+
+test_privserver_SOURCES = \
+ test-privserver.c
+
+test_privserver_CFLAGS=
+test_privserver_LDADD=$(top_builddir)/dbus/libdbus-convenience.la ../libdbus-testutils.la $(DBUS_TEST_LIBS)
+test_privserver_LDFLAGS=@R_DYNAMIC_LDFLAG@
+test_privserver_client_SOURCES = \
+ test-privserver-client.c
+
+test_privserver_client_CFLAGS=
+test_privserver_client_LDADD=$(top_builddir)/dbus/libdbus-convenience.la ../libdbus-testutils.la $(DBUS_TEST_LIBS)
+test_privserver_client_LDFLAGS=@R_DYNAMIC_LDFLAG@
+
+endif
diff --git a/test/name-test/run-test.sh b/test/name-test/run-test.sh
index 5d9c584a..3699bc9a 100755
--- a/test/name-test/run-test.sh
+++ b/test/name-test/run-test.sh
@@ -25,6 +25,11 @@ if test -z "$DBUS_TEST_NAME_IN_RUN_TEST"; then
export DBUS_TEST_NAME_IN_RUN_TEST
exec $DBUS_TOP_SRCDIR/tools/run-with-tmp-session-bus.sh $SCRIPTNAME $MODE
fi
+
+if test -n "$DBUS_TEST_MONITOR"; then
+ dbus-monitor --session &
+fi
+
echo "running test-ids"
${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/name-test/test-ids || die "test-ids failed"
@@ -36,3 +41,9 @@ ${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/name-
echo "running test-threads-init"
${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/name-test/test-threads-init || die "test-threads-init failed"
+
+echo "running test-privserver-client"
+${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/name-test/test-privserver-client || die "test-privserver-client failed"
+
+echo "running test-shutdown"
+${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/name-test/test-shutdown || die "test-shutdown failed"
diff --git a/test/name-test/test-privserver-client.c b/test/name-test/test-privserver-client.c
new file mode 100644
index 00000000..239b7bdf
--- /dev/null
+++ b/test/name-test/test-privserver-client.c
@@ -0,0 +1,116 @@
+#include "../test-utils.h"
+
+static void
+die (const char *message, ...)
+{
+ va_list args;
+ va_start (args, message);
+ vfprintf (stderr, message, args);
+ va_end (args);
+ exit (1);
+}
+
+static DBusHandlerResult
+filter_private_message (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data)
+{
+ if (dbus_message_is_signal (message,
+ DBUS_INTERFACE_LOCAL,
+ "Disconnected"))
+ {
+ DBusLoop *loop = user_data;
+ _dbus_loop_quit (loop);
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static void
+open_shutdown_private_connection (dbus_bool_t use_guid)
+{
+ DBusError error;
+ DBusLoop *loop;
+ DBusConnection *session;
+ DBusMessage *msg;
+ DBusMessage *reply;
+ DBusConnection *privconn;
+ char *addr;
+ char *comma;
+
+ dbus_error_init (&error);
+
+ loop = _dbus_loop_new ();
+
+ session = dbus_bus_get (DBUS_BUS_SESSION, &error);
+ if (!session)
+ die ("couldn't access session bus\n");
+ dbus_connection_set_exit_on_disconnect (session, FALSE);
+ msg = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuite.TestServer",
+ "/",
+ "org.freedesktop.DBus.TestSuite.TestServer",
+ "GetPrivateAddress");
+ if (!(reply = dbus_connection_send_with_reply_and_block (session, msg, -1, &error)))
+ die ("couldn't send message: %s\n", error.message);
+ dbus_message_unref (msg);
+ if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &addr, DBUS_TYPE_INVALID))
+ die ("couldn't parse message replym\n");
+ printf ("got private temp address %s\n", addr);
+ addr = strdup (addr);
+ if (!use_guid)
+ {
+ char *comma = strrchr (addr, ',');
+ if (comma)
+ *comma = '\0';
+ }
+ privconn = dbus_connection_open (addr, &error);
+ free (addr);
+ if (!privconn)
+ die ("couldn't connect to server direct connection: %s\n", error.message);
+ dbus_message_unref (reply);
+
+ dbus_connection_set_exit_on_disconnect (privconn, FALSE);
+ dbus_connection_add_filter (privconn, filter_private_message, loop, NULL);
+ test_connection_setup (loop, privconn);
+
+ msg = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuite.TestServer",
+ "/",
+ "org.freedesktop.DBus.TestSuite.TestServer",
+ "Quit");
+ if (!dbus_connection_send (session, msg, NULL))
+ die ("couldn't send Quit message\n");
+ dbus_message_unref (msg);
+
+ _dbus_loop_run (loop);
+
+ test_connection_shutdown (loop, session);
+ dbus_connection_unref (session);
+
+ test_connection_shutdown (loop, privconn);
+ dbus_connection_remove_filter (privconn, filter_private_message, loop);
+ dbus_connection_unref (privconn);
+
+ _dbus_loop_unref (loop);
+}
+
+int
+main (int argc, char *argv[])
+{
+ open_shutdown_private_connection (TRUE);
+
+ dbus_shutdown ();
+
+ open_shutdown_private_connection (TRUE);
+
+ dbus_shutdown ();
+
+ open_shutdown_private_connection (FALSE);
+
+ dbus_shutdown ();
+
+ open_shutdown_private_connection (FALSE);
+
+ dbus_shutdown ();
+
+ return 0;
+}
diff --git a/test/name-test/test-privserver.c b/test/name-test/test-privserver.c
new file mode 100644
index 00000000..8a6efc4e
--- /dev/null
+++ b/test/name-test/test-privserver.c
@@ -0,0 +1,118 @@
+#include "../test-utils.h"
+
+static void
+die (const char *message, ...)
+{
+ va_list args;
+ va_start (args, message);
+ vfprintf (stderr, message, args);
+ va_end (args);
+ exit (1);
+}
+
+typedef struct TestServiceData TestServiceData;
+
+struct TestServiceData
+{
+ DBusLoop *loop;
+ char *private_addr;
+};
+
+static void
+new_connection_callback (DBusServer *server,
+ DBusConnection *new_connection,
+ void *data)
+{
+ TestServiceData *testdata = data;
+
+ if (!test_connection_setup (testdata->loop, new_connection))
+ dbus_connection_close (new_connection);
+}
+
+static DBusHandlerResult
+filter_session_message (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data)
+{
+ TestServiceData *testdata = user_data;
+
+ if (dbus_message_is_method_call (message,
+ "org.freedesktop.DBus.TestSuite.TestServer",
+ "GetPrivateAddress"))
+ {
+ DBusMessage *reply;
+ reply = dbus_message_new_method_return (message);
+ dbus_message_append_args (reply, DBUS_TYPE_STRING,
+ &(testdata->private_addr), DBUS_TYPE_INVALID);
+ dbus_connection_send (connection, reply, NULL);
+ dbus_message_unref (reply);
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ else if (dbus_message_is_method_call (message,
+ "org.freedesktop.DBus.TestSuite.TestServer",
+ "Quit"))
+ {
+ fprintf (stderr, "server exiting loop\n");
+ _dbus_loop_quit (testdata->loop);
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+int
+main (int argc, char *argv[])
+{
+ DBusServer *server;
+ DBusError error;
+ DBusLoop *loop;
+ DBusConnection *session;
+ TestServiceData *testdata;
+
+ dbus_error_init (&error);
+
+ loop = _dbus_loop_new ();
+
+ testdata = dbus_new (TestServiceData, 1);
+ testdata->loop = loop;
+
+ session = dbus_bus_get (DBUS_BUS_SESSION, &error);
+ if (!session)
+ die ("couldn't access session bus");
+
+ test_connection_setup (loop, session);
+
+ dbus_bus_request_name (session, "org.freedesktop.DBus.TestSuite.TestServer", 0, &error);
+ if (dbus_error_is_set (&error))
+ die ("couldn't request name: %s", error.message);
+
+ if (!dbus_connection_add_filter (session, filter_session_message, testdata, NULL))
+ die ("couldn't add filter");
+
+ server = dbus_server_listen ("unix:tmpdir=/tmp", &error);
+ if (!server)
+ die (error.message);
+ testdata->private_addr = dbus_server_get_address (server);
+ printf ("test server listening on %s\n", testdata->private_addr);
+
+ dbus_server_set_new_connection_function (server, new_connection_callback,
+ testdata, NULL);
+
+ if (!test_server_setup (loop, server))
+ die ("server setup failed");
+
+ fprintf (stderr, "server running mainloop\n");
+ _dbus_loop_run (loop);
+ fprintf (stderr, "server mainloop quit\n");
+
+ test_server_shutdown (loop, server);
+
+ test_connection_shutdown (loop, session);
+
+ dbus_connection_unref (session);
+
+ _dbus_loop_unref (loop);
+
+ dbus_free (testdata);
+
+ return 0;
+}
diff --git a/test/name-test/test-shutdown.c b/test/name-test/test-shutdown.c
new file mode 100644
index 00000000..c50ef4b6
--- /dev/null
+++ b/test/name-test/test-shutdown.c
@@ -0,0 +1,67 @@
+
+#include "../test-utils.h"
+
+static DBusLoop *loop;
+
+static void
+die (const char *message)
+{
+ fprintf (stderr, "*** test-shutdown: %s", message);
+ exit (1);
+}
+
+static void
+open_destroy_shared_session_bus_connection ()
+{
+ DBusError error;
+ DBusConnection *connection;
+ char *session_addr_no_guid;
+ char *comma;
+
+ dbus_error_init (&error);
+
+ session_addr_no_guid = strdup (getenv ("DBUS_SESSION_BUS_ADDRESS"));
+ comma = strchr (session_addr_no_guid, ',');
+ if (comma == NULL)
+ die ("Couldn't find GUID in session bus address");
+ *comma = '\0';
+
+ connection = dbus_connection_open (session_addr_no_guid, &error);
+ free (session_addr_no_guid);
+ if (connection == NULL)
+ die ("Failed to open connection to temp session bus\n");
+
+ loop = _dbus_loop_new ();
+ if (loop == NULL)
+ die ("No memory\n");
+
+ if (!test_connection_setup (loop, connection))
+ die ("No memory\n");
+
+ test_connection_shutdown (loop, connection);
+
+ _dbus_loop_unref (loop);
+
+ dbus_connection_unref (connection);
+}
+
+int
+main (int argc,
+ char **argv)
+{
+ open_destroy_shared_session_bus_connection ();
+
+ dbus_shutdown ();
+
+ open_destroy_shared_session_bus_connection ();
+
+ dbus_shutdown ();
+
+ open_destroy_shared_session_bus_connection ();
+
+ dbus_shutdown ();
+
+ _dbus_verbose ("*** Test shutdown exiting\n");
+
+ return 0;
+}
diff --git a/test/test-utils.c b/test/test-utils.c
index 9665eda3..d875cc95 100644
--- a/test/test-utils.c
+++ b/test/test-utils.c
@@ -187,3 +187,157 @@ test_connection_shutdown (DBusLoop *loop,
dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
}
+
+typedef struct
+{
+ DBusLoop *loop;
+ DBusServer *server;
+} ServerData;
+
+static void
+serverdata_free (void *data)
+{
+ ServerData *sd = data;
+
+ dbus_server_unref (sd->server);
+ _dbus_loop_unref (sd->loop);
+
+ dbus_free (sd);
+}
+
+static ServerData*
+serverdata_new (DBusLoop *loop,
+ DBusServer *server)
+{
+ ServerData *sd;
+
+ sd = dbus_new0 (ServerData, 1);
+ if (sd == NULL)
+ return NULL;
+
+ sd->loop = loop;
+ sd->server = server;
+
+ dbus_server_ref (sd->server);
+ _dbus_loop_ref (sd->loop);
+
+ return sd;
+}
+
+static dbus_bool_t
+server_watch_callback (DBusWatch *watch,
+ unsigned int condition,
+ void *data)
+{
+ /* FIXME this can be done in dbus-mainloop.c
+ * if the code in activation.c for the babysitter
+ * watch handler is fixed.
+ */
+
+ return dbus_watch_handle (watch, condition);
+}
+
+static dbus_bool_t
+add_server_watch (DBusWatch *watch,
+ void *data)
+{
+ ServerData *context = data;
+
+ return _dbus_loop_add_watch (context->loop,
+ watch, server_watch_callback, context,
+ NULL);
+}
+
+static void
+remove_server_watch (DBusWatch *watch,
+ void *data)
+{
+ ServerData *context = data;
+
+ _dbus_loop_remove_watch (context->loop,
+ watch, server_watch_callback, context);
+}
+
+static void
+server_timeout_callback (DBusTimeout *timeout,
+ void *data)
+{
+ /* can return FALSE on OOM but we just let it fire again later */
+ dbus_timeout_handle (timeout);
+}
+
+static dbus_bool_t
+add_server_timeout (DBusTimeout *timeout,
+ void *data)
+{
+ ServerData *context = data;
+
+ return _dbus_loop_add_timeout (context->loop,
+ timeout, server_timeout_callback, context, NULL);
+}
+
+static void
+remove_server_timeout (DBusTimeout *timeout,
+ void *data)
+{
+ ServerData *context = data;
+
+ _dbus_loop_remove_timeout (context->loop,
+ timeout, server_timeout_callback, context);
+}
+
+dbus_bool_t
+test_server_setup (DBusLoop *loop,
+ DBusServer *server)
+{
+ ServerData *sd;
+
+ sd = serverdata_new (loop, server);
+ if (sd == NULL)
+ goto nomem;
+
+ if (!dbus_server_set_watch_functions (server,
+ add_server_watch,
+ remove_server_watch,
+ NULL,
+ sd,
+ serverdata_free))
+ {
+ return FALSE;
+ }
+
+ if (!dbus_server_set_timeout_functions (server,
+ add_server_timeout,
+ remove_server_timeout,
+ NULL,
+ sd, serverdata_free))
+ {
+ return FALSE;
+ }
+ return TRUE;
+
+ nomem:
+ if (sd)
+ serverdata_free (sd);
+
+ test_server_shutdown (loop, server);
+
+ return FALSE;
+}
+
+void
+test_server_shutdown (DBusLoop *loop,
+ DBusServer *server)
+{
+ if (!dbus_server_set_watch_functions (server,
+ NULL, NULL, NULL,
+ NULL,
+ NULL))
+ _dbus_assert_not_reached ("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");
+}
diff --git a/test/test-utils.h b/test/test-utils.h
index f00a7181..90d87767 100644
--- a/test/test-utils.h
+++ b/test/test-utils.h
@@ -1,7 +1,9 @@
#ifndef TEST_UTILS_H
#define TEST_UTILS_H
#include <config.h>
+#ifndef DBUS_COMPILATION
#define DBUS_COMPILATION /* Cheat and use private stuff */
+#endif
#include <dbus/dbus.h>
#include <stdio.h>
#include <stdlib.h>
@@ -16,5 +18,9 @@ void test_connection_shutdown (DBusLoop *loop,
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,
+ DBusServer *server);
+void test_server_shutdown (DBusLoop *loop,
+ DBusServer *server);
#endif