Merge branch 'wip/smcv/gdbus-tests' into 'master'

Fix GDBus test failures on non-Linux (in particular FreeBSD)

Closes #1920 and #1921

See merge request GNOME/glib!1197
This commit is contained in:
Philip Withnall
2019-10-31 12:38:12 +00:00
2 changed files with 70 additions and 41 deletions

View File

@@ -273,14 +273,9 @@ setup_test_address (void)
{ {
if (is_unix) if (is_unix)
{ {
g_test_message ("Testing with unix:tmpdir address"); g_test_message ("Testing with unix:dir address");
if (g_unix_socket_address_abstract_names_supported ()) tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-"); tmp_address = g_strdup_printf ("unix:dir=%s", tmpdir);
else
{
tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
}
} }
else else
tmp_address = g_strdup ("nonce-tcp:"); tmp_address = g_strdup ("nonce-tcp:");
@@ -288,11 +283,11 @@ setup_test_address (void)
#ifdef G_OS_UNIX #ifdef G_OS_UNIX
static void static void
setup_dir_test_address (void) setup_tmpdir_test_address (void)
{ {
g_test_message ("Testing with unix:dir address"); g_test_message ("Testing with unix:tmpdir address");
tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL); tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
tmp_address = g_strdup_printf ("unix:dir=%s", tmpdir); tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
} }
static void static void
@@ -313,7 +308,8 @@ teardown_test_address (void)
/* Ensuring the rmdir succeeds also ensures any sockets created on the /* Ensuring the rmdir succeeds also ensures any sockets created on the
* filesystem are also deleted. * filesystem are also deleted.
*/ */
g_assert_cmpint (g_rmdir (tmpdir), ==, 0); g_assert_cmpstr (g_rmdir (tmpdir) == 0 ? "OK" : g_strerror (errno),
==, "OK");
g_clear_pointer (&tmpdir, g_free); g_clear_pointer (&tmpdir, g_free);
} }
} }
@@ -1044,7 +1040,7 @@ test_peer (void)
teardown_test_address (); teardown_test_address ();
#ifdef G_OS_UNIX #ifdef G_OS_UNIX
setup_dir_test_address (); setup_tmpdir_test_address ();
do_test_peer (); do_test_peer ();
teardown_test_address (); teardown_test_address ();
@@ -1258,6 +1254,7 @@ dmp_thread_func (gpointer user_data)
data->loop = g_main_loop_new (data->context, FALSE); data->loop = g_main_loop_new (data->context, FALSE);
g_main_loop_run (data->loop); g_main_loop_run (data->loop);
g_dbus_server_stop (data->server);
g_main_context_pop_thread_default (data->context); g_main_context_pop_thread_default (data->context);
g_free (guid); g_free (guid);

View File

@@ -17,6 +17,9 @@
#include "config.h" #include "config.h"
#include <errno.h>
#include <glib/gstdio.h>
#include <gio/gio.h> #include <gio/gio.h>
#ifdef HAVE_DBUS1 #ifdef HAVE_DBUS1
@@ -30,6 +33,7 @@ typedef enum
INTEROP_FLAGS_SHA1 = (1 << 2), INTEROP_FLAGS_SHA1 = (1 << 2),
INTEROP_FLAGS_TCP = (1 << 3), INTEROP_FLAGS_TCP = (1 << 3),
INTEROP_FLAGS_LIBDBUS = (1 << 4), INTEROP_FLAGS_LIBDBUS = (1 << 4),
INTEROP_FLAGS_ABSTRACT = (1 << 5),
INTEROP_FLAGS_NONE = 0 INTEROP_FLAGS_NONE = 0
} InteropFlags; } InteropFlags;
@@ -245,18 +249,19 @@ assert_expected_uid_pid (InteropFlags flags,
} }
static void static void
do_test_server_auth (const char *listenable_address, do_test_server_auth (InteropFlags flags)
InteropFlags flags)
{ {
GError *error = NULL; GError *error = NULL;
GDBusServer *server; gchar *tmpdir = NULL;
GDBusAuthObserver *observer; gchar *listenable_address = NULL;
GDBusServer *server = NULL;
GDBusAuthObserver *observer = NULL;
GDBusServerFlags server_flags = G_DBUS_SERVER_FLAGS_RUN_IN_THREAD; GDBusServerFlags server_flags = G_DBUS_SERVER_FLAGS_RUN_IN_THREAD;
gchar *guid; gchar *guid = NULL;
const char *connectable_address; const char *connectable_address;
GDBusConnection *client; GDBusConnection *client = NULL;
GAsyncResult *result = NULL; GAsyncResult *result = NULL;
GVariant *tuple; GVariant *tuple = NULL;
gint64 uid, pid; gint64 uid, pid;
#ifdef HAVE_DBUS1 #ifdef HAVE_DBUS1
/* GNOME/glib#1831 seems to involve a race condition, so try a few times /* GNOME/glib#1831 seems to involve a race condition, so try a few times
@@ -265,37 +270,47 @@ do_test_server_auth (const char *listenable_address,
gsize n = 20; gsize n = 20;
#endif #endif
if (g_str_has_prefix (listenable_address, "tcp:") || if (flags & INTEROP_FLAGS_TCP)
g_str_has_prefix (listenable_address, "nonce-tcp:")) {
g_assert_cmpint (flags & INTEROP_FLAGS_TCP, !=, 0); listenable_address = g_strdup ("tcp:host=127.0.0.1");
}
else else
g_assert_cmpint (flags & INTEROP_FLAGS_TCP, ==, 0); {
#ifdef G_OS_UNIX
gchar *escaped;
tmpdir = g_dir_make_tmp ("gdbus-server-auth-XXXXXX", &error);
g_assert_no_error (error);
escaped = g_dbus_address_escape_value (tmpdir);
listenable_address = g_strdup_printf ("unix:%s=%s",
(flags & INTEROP_FLAGS_ABSTRACT) ? "tmpdir" : "dir",
escaped);
g_free (escaped);
#else
g_test_skip ("unix: addresses only work on Unix");
goto out;
#endif
}
g_test_message ("Testing GDBus server at %s / libdbus client, with flags: " g_test_message ("Testing GDBus server at %s / libdbus client, with flags: "
"external:%s " "external:%s "
"anonymous:%s " "anonymous:%s "
"sha1:%s " "sha1:%s "
"abstract:%s "
"tcp:%s", "tcp:%s",
listenable_address, listenable_address,
(flags & INTEROP_FLAGS_EXTERNAL) ? "true" : "false", (flags & INTEROP_FLAGS_EXTERNAL) ? "true" : "false",
(flags & INTEROP_FLAGS_ANONYMOUS) ? "true" : "false", (flags & INTEROP_FLAGS_ANONYMOUS) ? "true" : "false",
(flags & INTEROP_FLAGS_SHA1) ? "true" : "false", (flags & INTEROP_FLAGS_SHA1) ? "true" : "false",
(flags & INTEROP_FLAGS_ABSTRACT) ? "true" : "false",
(flags & INTEROP_FLAGS_TCP) ? "true" : "false"); (flags & INTEROP_FLAGS_TCP) ? "true" : "false");
#ifndef G_OS_UNIX
if (g_str_has_prefix (listenable_address, "unix:"))
{
g_test_skip ("unix: addresses only work on Unix");
return;
}
#endif
#if !defined(G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED) \ #if !defined(G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED) \
&& !defined(G_CREDENTIALS_SOCKET_GET_CREDENTIALS_SUPPORTED) && !defined(G_CREDENTIALS_SOCKET_GET_CREDENTIALS_SUPPORTED)
if (flags & INTEROP_FLAGS_EXTERNAL) if (flags & INTEROP_FLAGS_EXTERNAL)
{ {
g_test_skip ("EXTERNAL authentication not implemented on this platform"); g_test_skip ("EXTERNAL authentication not implemented on this platform");
return; goto out;
} }
#endif #endif
@@ -333,6 +348,7 @@ do_test_server_auth (const char *listenable_address,
g_signal_connect (server, "new-connection", G_CALLBACK (new_connection_cb), NULL); g_signal_connect (server, "new-connection", G_CALLBACK (new_connection_cb), NULL);
g_dbus_server_start (server); g_dbus_server_start (server);
connectable_address = g_dbus_server_get_client_address (server); connectable_address = g_dbus_server_get_client_address (server);
g_test_message ("Connectable address: %s", connectable_address);
result = NULL; result = NULL;
g_dbus_connection_new_for_address (connectable_address, g_dbus_connection_new_for_address (connectable_address,
@@ -425,52 +441,67 @@ do_test_server_auth (const char *listenable_address,
g_test_skip ("Testing interop with libdbus not supported"); g_test_skip ("Testing interop with libdbus not supported");
#endif /* !HAVE_DBUS1 */ #endif /* !HAVE_DBUS1 */
g_dbus_server_stop (server); out:
if (server != NULL)
g_dbus_server_stop (server);
if (tmpdir != NULL)
g_assert_cmpstr (g_rmdir (tmpdir) == 0 ? "OK" : g_strerror (errno),
==, "OK");
g_clear_object (&server); g_clear_object (&server);
g_clear_object (&observer); g_clear_object (&observer);
g_free (guid); g_free (guid);
g_free (listenable_address);
g_free (tmpdir);
} }
static void static void
test_server_auth (void) test_server_auth (void)
{ {
do_test_server_auth ("unix:tmpdir=/tmp/gdbus-test", INTEROP_FLAGS_NONE); do_test_server_auth (INTEROP_FLAGS_NONE);
}
static void
test_server_auth_abstract (void)
{
do_test_server_auth (INTEROP_FLAGS_ABSTRACT);
} }
static void static void
test_server_auth_tcp (void) test_server_auth_tcp (void)
{ {
do_test_server_auth ("tcp:host=127.0.0.1", INTEROP_FLAGS_TCP); do_test_server_auth (INTEROP_FLAGS_TCP);
} }
static void static void
test_server_auth_anonymous (void) test_server_auth_anonymous (void)
{ {
do_test_server_auth ("unix:tmpdir=/tmp/gdbus-test", INTEROP_FLAGS_ANONYMOUS); do_test_server_auth (INTEROP_FLAGS_ANONYMOUS);
} }
static void static void
test_server_auth_anonymous_tcp (void) test_server_auth_anonymous_tcp (void)
{ {
do_test_server_auth ("tcp:host=127.0.0.1", INTEROP_FLAGS_ANONYMOUS | INTEROP_FLAGS_TCP); do_test_server_auth (INTEROP_FLAGS_ANONYMOUS | INTEROP_FLAGS_TCP);
} }
static void static void
test_server_auth_external (void) test_server_auth_external (void)
{ {
do_test_server_auth ("unix:tmpdir=/tmp/gdbus-test", INTEROP_FLAGS_EXTERNAL); do_test_server_auth (INTEROP_FLAGS_EXTERNAL);
} }
static void static void
test_server_auth_sha1 (void) test_server_auth_sha1 (void)
{ {
do_test_server_auth ("unix:tmpdir=/tmp/gdbus-test", INTEROP_FLAGS_SHA1); do_test_server_auth (INTEROP_FLAGS_SHA1);
} }
static void static void
test_server_auth_sha1_tcp (void) test_server_auth_sha1_tcp (void)
{ {
do_test_server_auth ("tcp:host=127.0.0.1", INTEROP_FLAGS_SHA1 | INTEROP_FLAGS_TCP); do_test_server_auth (INTEROP_FLAGS_SHA1 | INTEROP_FLAGS_TCP);
} }
int int
@@ -480,6 +511,7 @@ main (int argc,
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_test_add_func ("/gdbus/server-auth", test_server_auth); g_test_add_func ("/gdbus/server-auth", test_server_auth);
g_test_add_func ("/gdbus/server-auth/abstract", test_server_auth_abstract);
g_test_add_func ("/gdbus/server-auth/tcp", test_server_auth_tcp); g_test_add_func ("/gdbus/server-auth/tcp", test_server_auth_tcp);
g_test_add_func ("/gdbus/server-auth/anonymous", test_server_auth_anonymous); g_test_add_func ("/gdbus/server-auth/anonymous", test_server_auth_anonymous);
g_test_add_func ("/gdbus/server-auth/anonymous/tcp", test_server_auth_anonymous_tcp); g_test_add_func ("/gdbus/server-auth/anonymous/tcp", test_server_auth_anonymous_tcp);