2 * Copyright (c) 2015 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"
34 #include "sudo_compat.h"
35 #include "sudo_fatal.h"
36 #include "sudo_util.h"
38 __dso_public int main(int argc, char *argv[]);
41 * Test that sudo_parse_gids() works as expected.
44 struct parse_gids_test {
49 const GETGROUPS_T *gidlist;
52 static const GETGROUPS_T test1_out[] = { 0, 1, 2, 3, 4 };
53 static const GETGROUPS_T test2_out[] = { 1, 2, 3, 4 };
54 static const GETGROUPS_T test3_out[] = { 0, 1, (gid_t)-2, 3, 4 };
56 /* XXX - test syntax errors too */
57 static struct parse_gids_test test_data[] = {
58 { "1,2,3,4", &test_data[0].basegid, 0, 5, test1_out },
59 { "1,2,3,4", NULL, 0, 4, test2_out },
60 { "1,-2,3,4", &test_data[2].basegid, 0, 5, test3_out },
61 { NULL, false, 0, 0, NULL }
65 dump_gids(const char *prefix, int ngids, const GETGROUPS_T *gidlist)
69 fprintf(stderr, "%s: %s: ", getprogname(), prefix);
70 for (i = 0; i < ngids; i++) {
71 fprintf(stderr, "%s%d", i ? ", " : "", (int)gidlist[i]);
77 main(int argc, char *argv[])
79 GETGROUPS_T *gidlist = NULL;
80 int i, j, errors = 0, ntests = 0;
82 initprogname(argc > 0 ? argv[0] : "parse_gids_test");
84 for (i = 0; test_data[i].gids != NULL; i++) {
86 ngids = sudo_parse_gids(test_data[i].gids, test_data[i].baseptr, &gidlist);
88 exit(1); /* out of memory? */
90 if (ngids != test_data[i].ngids) {
91 sudo_warnx_nodebug("test #%d: expected %d gids, got %d",
92 ntests, test_data[i].ngids, ngids);
93 dump_gids("expected", test_data[i].ngids, test_data[i].gidlist);
94 dump_gids("received", ngids, gidlist);
99 for (j = 0; j < ngids; j++) {
100 if (test_data[i].gidlist[j] != gidlist[j]) {
101 sudo_warnx_nodebug("test #%d: gid mismatch", ntests);
102 dump_gids("expected", test_data[i].ngids, test_data[i].gidlist);
103 dump_gids("received", ngids, gidlist);
110 printf("%s: %d tests run, %d errors, %d%% success rate\n",
111 getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);