191 lines
4.8 KiB
Diff
191 lines
4.8 KiB
Diff
From 5337c629e945dec1b53884aa16daceb0500dea9b Mon Sep 17 00:00:00 2001
|
|
From: Simon McVittie <smcv@collabora.com>
|
|
Date: Thu, 29 Jun 2023 16:54:46 +0100
|
|
Subject: [PATCH] test: Add a targeted test for _dbus_unix_groups_from_uid()
|
|
|
|
Helps: https://gitlab.freedesktop.org/dbus/dbus/-/issues/343
|
|
Signed-off-by: Simon McVittie <smcv@collabora.com>
|
|
|
|
Conflict:Adapt context
|
|
Reference:https://gitlab.freedesktop.org/dbus/dbus/-/commit/5337c629e945dec1b53884aa16daceb0500dea9b
|
|
---
|
|
test/Makefile.am | 4 ++
|
|
test/internals/userdb.c | 143 ++++++++++++++++++++++++++++++++++++++++
|
|
2 files changed, 147 insertions(+)
|
|
create mode 100644 test/internals/userdb.c
|
|
|
|
diff --git a/test/Makefile.am b/test/Makefile.am
|
|
index 3bbf7f73..ffba702a 100644
|
|
--- a/test/Makefile.am
|
|
+++ b/test/Makefile.am
|
|
@@ -121,6 +121,9 @@ test_server_oom_LDADD = libdbus-testutils.la $(GLIB_LIBS)
|
|
test_syslog_SOURCES = internals/syslog.c
|
|
test_syslog_LDADD = libdbus-testutils.la $(GLIB_LIBS)
|
|
|
|
+test_userdb_SOURCES = internals/userdb.c
|
|
+test_userdb_LDADD = libdbus-testutils.la $(GLIB_LIBS)
|
|
+
|
|
test_variant_SOURCES = internals/variant.c
|
|
test_variant_LDADD = libdbus-testutils.la $(GLIB_LIBS)
|
|
|
|
@@ -181,6 +184,7 @@ installable_tests += \
|
|
test-sysdeps \
|
|
test-syslog \
|
|
test-uid-permissions \
|
|
+ test-userdb \
|
|
test-variant \
|
|
$(NULL)
|
|
|
|
diff --git a/test/internals/userdb.c b/test/internals/userdb.c
|
|
new file mode 100644
|
|
index 00000000..905791b3
|
|
--- /dev/null
|
|
+++ b/test/internals/userdb.c
|
|
@@ -0,0 +1,143 @@
|
|
+/*
|
|
+ * Copyright © 2023 Collabora Ltd.
|
|
+ * SPDX-License-Identifier: MIT
|
|
+ */
|
|
+
|
|
+#include <config.h>
|
|
+
|
|
+#include <glib.h>
|
|
+
|
|
+#include <dbus/dbus.h>
|
|
+#include "dbus/dbus-sysdeps.h"
|
|
+#include "test-utils-glib.h"
|
|
+
|
|
+#ifdef DBUS_UNIX
|
|
+#include <errno.h>
|
|
+#include <pwd.h>
|
|
+#include <sys/types.h>
|
|
+#include <unistd.h>
|
|
+
|
|
+#include "dbus/dbus-sysdeps-unix.h"
|
|
+#include "dbus/dbus-userdb.h"
|
|
+#endif
|
|
+
|
|
+typedef struct
|
|
+{
|
|
+ int dummy;
|
|
+} Fixture;
|
|
+
|
|
+static void
|
|
+setup (Fixture *f G_GNUC_UNUSED,
|
|
+ gconstpointer context G_GNUC_UNUSED)
|
|
+{
|
|
+}
|
|
+
|
|
+static void
|
|
+test_groups_from_uid (Fixture *f,
|
|
+ gconstpointer context G_GNUC_UNUSED)
|
|
+{
|
|
+ DBusError error = DBUS_ERROR_INIT;
|
|
+ dbus_gid_t *gids = NULL;
|
|
+ int n_gids = -1;
|
|
+ dbus_bool_t ret;
|
|
+#ifdef DBUS_UNIX
|
|
+ int i;
|
|
+#endif
|
|
+
|
|
+ /* We assume that uid 0 (root) is available on all Unix systems,
|
|
+ * so this should succeed */
|
|
+ ret = _dbus_unix_groups_from_uid (0, &gids, &n_gids, &error);
|
|
+
|
|
+#ifdef DBUS_UNIX
|
|
+ test_assert_no_error (&error);
|
|
+ g_assert_true (ret);
|
|
+ g_assert_cmpint (n_gids, >=, 0);
|
|
+
|
|
+ g_test_message ("Groups of uid 0:");
|
|
+
|
|
+ for (i = 0; i < n_gids; i++)
|
|
+ {
|
|
+ g_test_message ("[%d]: %ld", i, (long) gids[i]);
|
|
+ g_assert_cmpint (gids[i], >=, 0);
|
|
+ }
|
|
+#else
|
|
+ g_assert_cmpstr (error.name, ==, DBUS_ERROR_NOT_SUPPORTED);
|
|
+ g_assert_false (ret);
|
|
+ g_test_message ("Getting Unix groups on Windows failed as expected: %s: %s",
|
|
+ error.name, error.message);
|
|
+ g_assert_null (gids);
|
|
+ g_assert_cmpint (n_gids, <=, 0);
|
|
+#endif
|
|
+
|
|
+ dbus_free (gids);
|
|
+ dbus_error_free (&error);
|
|
+
|
|
+#ifdef DBUS_UNIX
|
|
+ /* Assume that the current uid is something sensible */
|
|
+ ret = _dbus_unix_groups_from_uid (geteuid (), &gids, &n_gids, &error);
|
|
+ test_assert_no_error (&error);
|
|
+ g_assert_true (ret);
|
|
+ g_assert_cmpint (n_gids, >=, 0);
|
|
+
|
|
+ g_test_message ("Groups of uid %ld:", (long) geteuid ());
|
|
+
|
|
+ for (i = 0; i < n_gids; i++)
|
|
+ {
|
|
+ g_test_message ("[%d]: %ld", i, (long) gids[i]);
|
|
+ g_assert_cmpint (gids[i], >=, 0);
|
|
+ }
|
|
+
|
|
+ g_test_message ("Total: %i groups", n_gids);
|
|
+
|
|
+ dbus_free (gids);
|
|
+ dbus_error_free (&error);
|
|
+
|
|
+ errno = 0;
|
|
+
|
|
+ /* arbitrarily chosen, probably isn't a valid uid */
|
|
+ if (getpwuid (31337) == NULL)
|
|
+ {
|
|
+ g_test_message ("uid 31337 doesn't exist: %s",
|
|
+ errno == 0 ? "(no errno)" : g_strerror (errno));
|
|
+ ret = _dbus_unix_groups_from_uid (31337, &gids, &n_gids, &error);
|
|
+ g_assert_nonnull (error.name);
|
|
+ g_assert_nonnull (error.message);
|
|
+ g_assert_false (ret);
|
|
+ g_test_message ("Getting groups from non-uid failed as expected: %s: %s",
|
|
+ error.name, error.message);
|
|
+ /* The Unix implementation always clears gids/n_gids,
|
|
+ * even on failure, and even if they were uninitialized */
|
|
+ g_assert_null (gids);
|
|
+ g_assert_cmpint (n_gids, ==, 0);
|
|
+
|
|
+ dbus_free (gids);
|
|
+ dbus_error_free (&error);
|
|
+ }
|
|
+ else
|
|
+ {
|
|
+ g_test_skip ("against our expectations, uid 31337 exists on this system");
|
|
+ }
|
|
+#endif
|
|
+}
|
|
+
|
|
+static void
|
|
+teardown (Fixture *f G_GNUC_UNUSED,
|
|
+ gconstpointer context G_GNUC_UNUSED)
|
|
+{
|
|
+}
|
|
+
|
|
+int
|
|
+main (int argc,
|
|
+ char **argv)
|
|
+{
|
|
+ int ret;
|
|
+
|
|
+ test_init (&argc, &argv);
|
|
+
|
|
+ g_test_add ("/userdb/groups_from_uid",
|
|
+ Fixture, NULL, setup, test_groups_from_uid, teardown);
|
|
+
|
|
+ ret = g_test_run ();
|
|
+ dbus_shutdown ();
|
|
+ return ret;
|
|
+}
|
|
--
|
|
2.27.0
|
|
|