From 7c652a94ea429576b4c10869a7474d40f202b9f3 Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Mon, 10 Apr 2023 20:57:14 +0200 Subject: [PATCH] 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. --- test/pwcache.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) 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 + * Copyright © 2021, 2023 Guillem Jover * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -26,24 +26,48 @@ #include #include +#include #include #include +#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; }