glib-unix: Add g_unix_get_passwd_entry() function

This is a convenience wrapper around getpwnam_r() which handles all the
memory allocation faff.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Helps: #1687
This commit is contained in:
Philip Withnall
2019-06-11 13:52:48 +01:00
parent ae49ff0903
commit 94a800fb9d
4 changed files with 165 additions and 0 deletions

View File

@@ -25,6 +25,7 @@
#include "glib-unix.h"
#include <string.h>
#include <pwd.h>
static void
test_pipe (void)
@@ -295,6 +296,40 @@ test_callback_after_signal (void)
g_main_context_unref (context);
}
static void
test_get_passwd_entry_root (void)
{
struct passwd *pwd;
GError *local_error = NULL;
g_test_summary ("Tests that g_unix_get_passwd_entry() works for a "
"known-existing username.");
pwd = g_unix_get_passwd_entry ("root", &local_error);
g_assert_no_error (local_error);
g_assert_cmpstr (pwd->pw_name, ==, "root");
g_assert_cmpuint (pwd->pw_uid, ==, 0);
g_free (pwd);
}
static void
test_get_passwd_entry_nonexistent (void)
{
struct passwd *pwd;
GError *local_error = NULL;
g_test_summary ("Tests that g_unix_get_passwd_entry() returns an error for a "
"nonexistent username.");
pwd = g_unix_get_passwd_entry ("thisusernamedoesntexist", &local_error);
g_assert_error (local_error, G_UNIX_ERROR, 0);
g_assert_null (pwd);
g_clear_error (&local_error);
}
int
main (int argc,
char *argv[])
@@ -310,6 +345,8 @@ main (int argc,
g_test_add_func ("/glib-unix/sighup_add_remove", test_sighup_add_remove);
g_test_add_func ("/glib-unix/sighup_nested", test_sighup_nested);
g_test_add_func ("/glib-unix/callback_after_signal", test_callback_after_signal);
g_test_add_func ("/glib-unix/get-passwd-entry/root", test_get_passwd_entry_root);
g_test_add_func ("/glib-unix/get-passwd-entry/nonexistent", test_get_passwd_entry_nonexistent);
return g_test_run();
}