test: Do not hardcode root:root user and group names On some systems the root group is named wheel, and there is no root group.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
diff --git a/test/pwcache.c b/test/pwcache.c
index d68ea4c..ac1e67b 100644
--- a/test/pwcache.c
+++ b/test/pwcache.c
@@ -1,5 +1,5 @@
/*
- * Copyright © 2021 Guillem Jover <guillem@hadrons.org>
+ * Copyright © 2021, 2023 Guillem Jover <guillem@hadrons.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -26,24 +26,48 @@
#include <assert.h>
#include <string.h>
+#include <stdlib.h>
#include <pwd.h>
#include <grp.h>
+#define TEST_SKIP 77
+
int
main(int argc, char **argv)
{
+ struct group *gr;
+ struct passwd *pw;
+ char *uname;
+ char *gname;
uid_t uid;
gid_t gid;
- assert(uid_from_user("root", &uid) == 0);
+ /* Do not hardcode user or group names. */
+ pw = getpwuid(0);
+ if (pw == NULL)
+ return TEST_SKIP;
+ uname = strdup(pw->pw_name);
+ assert(uname != NULL);
+
+ gr = getgrgid(0);
+ if (gr == NULL)
+ return TEST_SKIP;
+ gname = strdup(gr->gr_name);
+ assert(gname != NULL);
+
+ /* Test the functions. */
+ assert(uid_from_user(uname, &uid) == 0);
assert(uid == 0);
- assert(strcmp(user_from_uid(0, 0), "root") == 0);
+ assert(strcmp(user_from_uid(0, 0), uname) == 0);
- assert(gid_from_group("root", &gid) == 0);
+ assert(gid_from_group(gname, &gid) == 0);
assert(gid == 0);
- assert(strcmp(group_from_gid(0, 0), "root") == 0);
+ assert(strcmp(group_from_gid(0, 0), gname) == 0);
+
+ free(uname);
+ free(gname);
return 0;
}