Add "PrivServer" test which exercises DBusServer and dbus_shutdown

* test/data/valid-service-files/org.freedesktop.DBus.TestSuite.PrivServer.service.in:
	New service file for PrivServer.
	* configure.in: Generate it.
	* test/name-test/Makefile.am: Build test-privserver and
	test-privserver-client.
	* test/name-test/test-privserver.c: Use DBusServer to
	serve a private connection.
	* test/name-test/test-privserver-client.c: Connect
	via session bus and get address of private server,
	exercise dbus_shutdown().
	* test/name-test/run-test.sh: Run it.
This commit is contained in:
Colin Walters 2008-05-30 17:11:15 -04:00
parent a73a79a155
commit 5e359834f2
6 changed files with 249 additions and 1 deletions

View file

@ -1275,6 +1275,7 @@ TEST_PATH(SHELL_SERVICE_BINARY, test-shell-service)
TEST_PATH(EXIT_BINARY, test-exit)
TEST_PATH(SEGFAULT_BINARY, test-segfault)
TEST_PATH(SLEEP_FOREVER_BINARY, test-sleep-forever)
TEST_PATH(PRIVSERVER_BINARY, name-test/test-privserver)
AC_DEFINE_UNQUOTED(TEST_BUS_BINARY, "$DBUS_PWD/bus/dbus-daemon",
[Full path to the daemon in the builddir])
@ -1347,6 +1348,7 @@ test/data/valid-config-files/debug-allow-all.conf
test/data/valid-config-files/debug-allow-all-sha1.conf
test/data/valid-config-files-system/debug-allow-all-pass.conf
test/data/valid-config-files-system/debug-allow-all-fail.conf
test/data/valid-service-files/org.freedesktop.DBus.TestSuite.PrivServer.service
test/data/valid-service-files/org.freedesktop.DBus.TestSuiteEchoService.service
test/data/valid-service-files/org.freedesktop.DBus.TestSuiteSegfaultService.service
test/data/valid-service-files/org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess.service

View file

@ -0,0 +1,4 @@
[D-BUS Service]
Name=org.freedesktop.DBus.TestSuite.PrivServer
Exec=@TEST_PRIVSERVER_BINARY@

View file

@ -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 test-shutdown
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
@ -49,4 +49,18 @@ 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

View file

@ -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"
@ -37,5 +42,8 @@ ${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"

View file

@ -0,0 +1,102 @@
#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 ()
{
DBusError error;
DBusLoop *loop;
DBusConnection *session;
DBusMessage *msg;
DBusMessage *reply;
DBusConnection *privconn;
const char *addr;
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);
privconn = dbus_connection_open (addr, &error);
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 ();
dbus_shutdown ();
/*
open_shutdown_private_connection ();
dbus_shutdown ();
*/
return 0;
}

View file

@ -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;
}