From d6a2fd580a75146bcacf6b85c5f75c518c3be1e9 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 27 Aug 2013 19:17:10 +0100 Subject: [PATCH] Add a manual test for user/group info on Unix Bug: https://bugs.freedesktop.org/show_bug.cgi?id=30938 --- test/Makefile.am | 5 ++ test/internals/manual-sysdeps.c | 158 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 test/internals/manual-sysdeps.c diff --git a/test/Makefile.am b/test/Makefile.am index 8b2a525..ec338cd 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -104,6 +104,7 @@ installable_tests = \ test-printf \ $(NULL) installable_manual_tests = \ + manual-sysdeps \ $(NULL) if DBUS_WITH_GLIB @@ -142,6 +143,10 @@ manual_authz_LDADD = $(top_builddir)/dbus/libdbus-1.la \ $(GLIB_LIBS) \ $(DBUS_GLIB_LIBS) +manual_sysdeps_SOURCES = internals/manual-sysdeps.c +manual_sysdeps_CPPFLAGS = $(static_cppflags) +manual_sysdeps_LDADD = $(top_builddir)/dbus/libdbus-internal.la + test_corrupt_SOURCES = corrupt.c test_corrupt_LDADD = $(top_builddir)/dbus/libdbus-1.la \ $(GLIB_LIBS) \ diff --git a/test/internals/manual-sysdeps.c b/test/internals/manual-sysdeps.c new file mode 100644 index 0000000..73a9de5 --- /dev/null +++ b/test/internals/manual-sysdeps.c @@ -0,0 +1,158 @@ +/* Simple sanity-check for the userdb. + * + * Copyright © 2013 Collabora Ltd. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#define DBUS_COMPILATION + +#include + +#include + +#ifdef DBUS_UNIX +# include +# include +#endif + +#include +#include "dbus/dbus-internals.h" + +#ifdef DBUS_UNIX +# include "dbus/dbus-sysdeps.h" +# include "dbus/dbus-sysdeps-unix.h" +#endif + +#ifdef DBUS_UNIX +static void +test_user_info (void) +{ + DBusUserInfo info; + DBusGroupInfo group_info; + const char *username; + DBusString s; + DBusError error = DBUS_ERROR_INIT; + int i; + + if (_dbus_user_info_fill_uid (&info, _dbus_getuid (), &error)) + { + printf ("User info for my own uid:\n"); + printf ("\tuid: " DBUS_UID_FORMAT "\n", info.uid); + printf ("\tgid: " DBUS_GID_FORMAT "\n", info.primary_gid); + printf ("\tgroups: %d (including primary)\n", info.n_group_ids); + + for (i = 0; i < info.n_group_ids; i++) + printf ("\t\t" DBUS_GID_FORMAT "\n", info.group_ids[i]); + } + else + { + printf ("Could not get user info for my own uid '" DBUS_UID_FORMAT + "': %s: %s", _dbus_getuid (), error.name, error.message); + dbus_error_free (&error); + } + + if (_dbus_group_info_fill_gid (&group_info, info.primary_gid, &error)) + { + printf ("Group info for gid '" DBUS_GID_FORMAT "':\n", info.primary_gid); + printf ("\tgid: " DBUS_GID_FORMAT "\n", group_info.gid); + printf ("\tname: %s\n", group_info.groupname); + } + else + { + printf ("Could not get user info for group '" DBUS_GID_FORMAT + "': %s: %s", info.primary_gid, error.name, error.message); + dbus_error_free (&error); + } + + _dbus_user_info_free (&info); + + username = _dbus_getenv ("USER"); + + if (username == NULL) + { + printf ("$USER not set, skipping\n"); + } + else + { + _dbus_string_init_const (&s, username); + + if (_dbus_user_info_fill (&info, &s, &error)) + { + printf ("User info for $USER '%s':\n", username); + printf ("\tuid: " DBUS_UID_FORMAT "\n", info.uid); + printf ("\tgid: " DBUS_GID_FORMAT "\n", info.primary_gid); + printf ("\tgroups: %d (including primary)\n", info.n_group_ids); + + for (i = 0; i < info.n_group_ids; i++) + printf ("\t\t" DBUS_GID_FORMAT "\n", info.group_ids[i]); + } + else + { + printf ("Could not get user info for $USER '%s': %s: %s", + username, error.name, error.message); + dbus_error_free (&error); + } + + _dbus_user_info_free (&info); + } + + if (group_info.groupname == NULL || + !_dbus_string_init (&s) || + !_dbus_string_append (&s, group_info.groupname)) + { + printf ("No group name for reverse lookup, skipping\n"); + } + else + { + _dbus_group_info_free (&group_info); + + if (_dbus_group_info_fill (&group_info, &s, &error)) + { + printf ("Group info for group '%s':\n", + _dbus_string_get_const_data (&s)); + printf ("\tgid: " DBUS_GID_FORMAT "\n", group_info.gid); + printf ("\tname: %s\n", group_info.groupname); + } + else + { + printf ("Could not get group info for group '%s': %s: %s", + _dbus_string_get_const_data (&s), + error.name, error.message); + dbus_error_free (&error); + } + } + + _dbus_group_info_free (&group_info); + _dbus_string_free (&s); +} +#endif + +int +main (int argc, + char **argv) +{ +#ifdef DBUS_UNIX + test_user_info (); +#endif + + return 0; +} -- 1.8.4.rc3