2 * Copyright (c) 2018 Todd C. Miller <Todd.Miller@sudo.ws>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
24 #endif /* HAVE_STRING_H */
27 #endif /* HAVE_STRINGS_H */
31 # include "compat/stdbool.h"
36 #include "sudo_compat.h"
37 #include "sudo_fatal.h"
38 #include "sudo_util.h"
40 __dso_public int main(int argc, char *argv[]);
43 * Test that sudo_getgrouplist2() works as expected.
47 main(int argc, char *argv[])
50 #ifndef HAVE_GETGROUPLIST_2
51 GETGROUPS_T *groups = NULL;
58 initprogname(argc > 0 ? argv[0] : "getgrouplist_test");
60 if ((pw = getpwuid(0)) == NULL)
61 sudo_fatal_nodebug("getpwuid(0)");
63 if ((username = strdup(pw->pw_name)) == NULL)
64 sudo_fatal_nodebug(NULL);
66 if (sudo_getgrouplist2(username, basegid, &groups, &ngroups) == -1)
67 sudo_fatal_nodebug("sudo_getgroulist2");
69 for (i = 0; i < ngroups; i++) {
72 /* Verify group ID exists. */
73 if ((grp = getgrgid(groups[i])) == NULL) {
74 sudo_warnx_nodebug("unable to look up group ID %u",
75 (unsigned int)groups[i]);
80 /* Check user's primary gid from the passwd file. */
81 if (grp->gr_gid == basegid)
84 /* Verify group membership. */
85 for (j = 0; grp->gr_mem[j] != NULL; j++) {
86 if (strcmp(username, grp->gr_mem[j]) == 0) {
91 if (grp->gr_mem[j] == NULL) {
92 sudo_warnx_nodebug("unable to find %s in group %s",
93 username, grp->gr_name);
99 printf("%s: %d tests run, %d errors, %d%% success rate\n",
100 getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
102 #endif /* HAVE_GETGROUPLIST_2 */