diff options
author | Simon McVittie <smcv@collabora.com> | 2017-07-06 15:57:18 +0100 |
---|---|---|
committer | Simon McVittie <smcv@collabora.com> | 2017-11-06 16:41:05 +0000 |
commit | f5afec02f20b49ca14300d4075f0dce58988ed42 (patch) | |
tree | 153c118f8c2560e3aa29aa41760ecd687ee8c262 /test/test-utils-glib.c | |
parent | f58b77f968a66b5a86fcc7697bc6d46cd656fbb7 (diff) | |
download | dbus-f5afec02f20b49ca14300d4075f0dce58988ed42.tar.gz |
test-utils-glib: Add failable functions to connect to a bus
Instead of calling g_test_skip() internally, raise a distinctive error
and let the caller handle it.
Signed-off-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=101354
Diffstat (limited to 'test/test-utils-glib.c')
-rw-r--r-- | test/test-utils-glib.c | 59 |
1 files changed, 43 insertions, 16 deletions
diff --git a/test/test-utils-glib.c b/test/test-utils-glib.c index 9dc58b97..a7543a9d 100644 --- a/test/test-utils-glib.c +++ b/test/test-utils-glib.c @@ -42,6 +42,7 @@ #include <glib.h> #include <glib/gstdio.h> +#include <gio/gio.h> #include <dbus/dbus.h> @@ -319,27 +320,52 @@ DBusConnection * test_connect_to_bus (TestMainContext *ctx, const gchar *address) { + GError *error = NULL; + DBusConnection *conn = test_try_connect_to_bus (ctx, address, &error); + + g_assert_no_error (error); + g_assert (conn != NULL); + return conn; +} + +DBusConnection * +test_try_connect_to_bus (TestMainContext *ctx, + const gchar *address, + GError **gerror) +{ DBusConnection *conn; DBusError error = DBUS_ERROR_INIT; - dbus_bool_t ok; conn = dbus_connection_open_private (address, &error); - test_assert_no_error (&error); - g_assert (conn != NULL); - ok = dbus_bus_register (conn, &error); - test_assert_no_error (&error); - g_assert (ok); + if (conn == NULL) + goto fail; + + if (!dbus_bus_register (conn, &error)) + goto fail; + g_assert (dbus_bus_get_unique_name (conn) != NULL); test_connection_setup (ctx, conn); return conn; + +fail: + if (gerror != NULL) + *gerror = g_dbus_error_new_for_dbus_error (error.name, error.message); + + dbus_error_free (&error); + return FALSE; } +/* + * Raise G_IO_ERROR_NOT_SUPPORTED if the requested user is impossible. + * Do not mark the test as skipped: we might have more to test anyway. + */ DBusConnection * -test_connect_to_bus_as_user (TestMainContext *ctx, +test_try_connect_to_bus_as_user (TestMainContext *ctx, const char *address, - TestUser user) + TestUser user, + GError **error) { /* For now we only do tests like this on Linux, because I don't know how * safe this use of setresuid() is on other platforms */ @@ -352,7 +378,7 @@ test_connect_to_bus_as_user (TestMainContext *ctx, switch (user) { case TEST_USER_ME: - return test_connect_to_bus (ctx, address); + return test_try_connect_to_bus (ctx, address, error); case TEST_USER_ROOT: username = "root"; @@ -375,9 +401,9 @@ test_connect_to_bus_as_user (TestMainContext *ctx, if (ruid != 0 || euid != 0 || suid != 0) { - g_test_message ("not uid 0 (ruid=%ld euid=%ld suid=%ld)", + g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, + "not uid 0 (ruid=%ld euid=%ld suid=%ld)", (unsigned long) ruid, (unsigned long) euid, (unsigned long) suid); - g_test_skip ("not uid 0"); return NULL; } @@ -385,13 +411,13 @@ test_connect_to_bus_as_user (TestMainContext *ctx, if (pwd == NULL) { - g_test_message ("getpwnam(\"%s\"): %s", username, g_strerror (errno)); - g_test_skip ("not uid 0"); + g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, + "getpwnam(\"%s\"): %s", username, g_strerror (errno)); return NULL; } /* Impersonate the desired user while we connect to the bus. - * This should work, because we're root. */ + * This should work, because we're root; so if it fails, we just crash. */ if (setresuid (pwd->pw_uid, pwd->pw_uid, 0) != 0) g_error ("setresuid(%ld, (same), 0): %s", (unsigned long) pwd->pw_uid, g_strerror (errno)); @@ -409,12 +435,13 @@ test_connect_to_bus_as_user (TestMainContext *ctx, switch (user) { case TEST_USER_ME: - return test_connect_to_bus (ctx, address); + return test_try_connect_to_bus (ctx, address, error); case TEST_USER_ROOT: case TEST_USER_MESSAGEBUS: case TEST_USER_OTHER: - g_test_skip ("setresuid() not available, or unsure about " + g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, + "setresuid() not available, or unsure about " "credentials-passing semantics on this platform"); return NULL; |