Add -v option parsing to regress tests, currently a no-op.

This will be used by a "check-verbose" target in the future.
This commit is contained in:
Todd C. Miller
2022-03-02 11:09:51 -07:00
parent dda14cb57a
commit 43cc80d795
26 changed files with 438 additions and 122 deletions

View File

@@ -36,27 +36,40 @@ sudo_dso_public int main(int argc, char *argv[]);
static void static void
usage(void) usage(void)
{ {
fprintf(stderr, "usage: %s inputfile\n", getprogname()); fprintf(stderr, "usage: %s [-v] inputfile\n", getprogname());
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int ch, lineno = 0, which = 0;
char *line, lines[2][2048];
const char *infile;
size_t len; size_t len;
FILE *fp; FILE *fp;
char *line, lines[2][2048];
int lineno = 0;
int which = 0;
initprogname(argc > 0 ? argv[0] : "check_wrap"); initprogname(argc > 0 ? argv[0] : "check_wrap");
if (argc != 2) while ((ch = getopt(argc, argv, "v")) != -1) {
usage(); switch (ch) {
case 'v':
/* ignored */
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
fp = fopen(argv[1], "r"); if (argc != 1)
usage();
infile = argv[0];
fp = fopen(infile, "r");
if (fp == NULL) if (fp == NULL)
sudo_fatalx("unable to open %s", argv[1]); sudo_fatalx("unable to open %s", infile);
/* /*
* Each test record consists of a log entry on one line and a list of * Each test record consists of a log entry on one line and a list of
@@ -89,8 +102,9 @@ main(int argc, char *argv[])
} else { } else {
len = maxlen = sudo_strtonum(cp, 0, INT_MAX, &errstr); len = maxlen = sudo_strtonum(cp, 0, INT_MAX, &errstr);
} }
if (errstr != NULL) if (errstr != NULL) {
sudo_fatalx("%s: invalid length on line %d\n", argv[1], lineno); sudo_fatalx("%s: invalid length on line %d\n", infile, lineno);
}
while (len <= maxlen) { while (len <= maxlen) {
if (len == 0) if (len == 0)
printf("# word wrap disabled\n"); printf("# word wrap disabled\n");
@@ -104,5 +118,5 @@ main(int argc, char *argv[])
which = !which; which = !which;
} }
exit(0); return EXIT_SUCCESS;
} }

View File

@@ -141,5 +141,5 @@ main(int argc, char *argv[])
printf("%s: %d tests run, %d errors, %d%% success rate\n", printf("%s: %d tests run, %d errors, %d%% success rate\n",
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
} }
exit(errors); return errors;
} }

View File

@@ -38,25 +38,38 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int dfd = -1, ttyin_fd = -1, ttyout_fd = -1, ttyin_ok_fd = -1; int dfd = -1, ttyin_fd = -1, ttyout_fd = -1, ttyin_ok_fd = -1;
int i, tests = 0, errors = 0; int ch, i, ntests = 0, errors = 0;
void *passprompt_regex = NULL; void *passprompt_regex = NULL;
initprogname(argc > 0 ? argv[0] : "check_iolog_filter"); initprogname(argc > 0 ? argv[0] : "check_iolog_filter");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v] iolog_dir ...\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
passprompt_regex = iolog_pwfilt_alloc(); passprompt_regex = iolog_pwfilt_alloc();
if (passprompt_regex == NULL) if (passprompt_regex == NULL)
sudo_fatalx("unable to allocate memory"); sudo_fatalx("unable to allocate memory");
if (!iolog_pwfilt_add(passprompt_regex, "(?i)password[: ]*")) if (!iolog_pwfilt_add(passprompt_regex, "(?i)password[: ]*"))
exit(1); exit(1);
for (i = 1; i < argc; i++) { for (i = 0; i < argc; i++) {
struct iolog_file iolog_timing = { true }; struct iolog_file iolog_timing = { true };
struct timing_closure timing; struct timing_closure timing;
const char *logdir = argv[i]; const char *logdir = argv[i];
char tbuf[8192], fbuf[8192]; char tbuf[8192], fbuf[8192];
ssize_t nread; ssize_t nread;
tests++; ntests++;
/* I/O logs consist of multiple files in a directory. */ /* I/O logs consist of multiple files in a directory. */
dfd = open(logdir, O_RDONLY); dfd = open(logdir, O_RDONLY);
@@ -180,11 +193,11 @@ next:
} }
iolog_pwfilt_free(passprompt_regex); iolog_pwfilt_free(passprompt_regex);
if (tests != 0) { if (ntests != 0) {
printf("%s: %d test%s run, %d errors, %d%% success rate\n", printf("iolog_filter: %d test%s run, %d errors, %d%% success rate\n",
getprogname(), tests, tests == 1 ? "" : "s", errors, ntests, ntests == 1 ? "" : "s", errors,
(tests - errors) * 100 / tests); (ntests - errors) * 100 / ntests);
} }
exit(errors); return errors;
} }

View File

@@ -171,7 +171,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct json_object root; struct json_object root;
int ch, i, tests = 0, errors = 0; int ch, i, ntests = 0, errors = 0;
bool cat = false; bool cat = false;
initprogname(argc > 0 ? argv[0] : "check_iolog_json"); initprogname(argc > 0 ? argv[0] : "check_iolog_json");
@@ -200,7 +200,7 @@ main(int argc, char *argv[])
FILE *infp = NULL; FILE *infp = NULL;
FILE *outfp = NULL; FILE *outfp = NULL;
tests++; ntests++;
if (!sudo_json_init(&json, 4, false, true)) { if (!sudo_json_init(&json, 4, false, true)) {
errors++; errors++;
@@ -255,11 +255,11 @@ next:
fclose(outfp); fclose(outfp);
} }
if (tests != 0) { if (ntests != 0) {
printf("iolog_json: %d test%s run, %d errors, %d%% success rate\n", printf("iolog_json: %d test%s run, %d errors, %d%% success rate\n",
tests, tests == 1 ? "" : "s", errors, ntests, ntests == 1 ? "" : "s", errors,
(tests - errors) * 100 / tests); (ntests - errors) * 100 / ntests);
} }
exit(errors); return errors;
} }

View File

@@ -67,25 +67,38 @@ main(int argc, char *argv[])
{ {
char testdir[] = "mkpath.XXXXXX"; char testdir[] = "mkpath.XXXXXX";
char *rmargs[] = { "rm", "-rf", NULL, NULL }; char *rmargs[] = { "rm", "-rf", NULL, NULL };
int status, tests = 0, errors = 0; int ch, status, ntests = 0, errors = 0;
initprogname(argc > 0 ? argv[0] : "check_iolog_mkpath"); initprogname(argc > 0 ? argv[0] : "check_iolog_mkpath");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
if (mkdtemp(testdir) == NULL) if (mkdtemp(testdir) == NULL)
sudo_fatal("unable to create test dir"); sudo_fatal("unable to create test dir");
rmargs[2] = testdir; rmargs[2] = testdir;
test_iolog_mkpath(testdir, &tests, &errors); test_iolog_mkpath(testdir, &ntests, &errors);
if (tests != 0) { if (ntests != 0) {
printf("iolog_mkpath: %d test%s run, %d errors, %d%% success rate\n", printf("iolog_mkpath: %d test%s run, %d errors, %d%% success rate\n",
tests, tests == 1 ? "" : "s", errors, ntests, ntests == 1 ? "" : "s", errors,
(tests - errors) * 100 / tests); (ntests - errors) * 100 / ntests);
} }
/* Clean up (avoid running via shell) */ /* Clean up (avoid running via shell) */
execvp("rm", rmargs); execvp("rm", rmargs);
wait(&status); wait(&status);
exit(errors); return errors;
} }

View File

@@ -180,18 +180,29 @@ main(int argc, char *argv[])
char line[2048]; char line[2048];
char *file_in = NULL, *file_out = NULL; char *file_in = NULL, *file_out = NULL;
char *dir_in = NULL, *dir_out = NULL; char *dir_in = NULL, *dir_out = NULL;
int state = 0; int ch, state = 0, errors = 0, ntests = 0;
int errors = 0;
int tests = 0;
initprogname(argc > 0 ? argv[0] : "check_iolog_path"); initprogname(argc > 0 ? argv[0] : "check_iolog_path");
if (argc != 2) while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v] data\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
if (argc != 1)
usage(); usage();
fp = fopen(argv[1], "r"); fp = fopen(argv[0], "r");
if (fp == NULL) if (fp == NULL)
sudo_fatalx("unable to open %s", argv[1]); sudo_fatalx("unable to open %s", argv[0]);
/* /*
* Input consists of 12 lines: * Input consists of 12 lines:
@@ -262,7 +273,7 @@ main(int argc, char *argv[])
break; break;
case 11: case 11:
errors += do_check(dir_in, file_in, dir_out, file_out); errors += do_check(dir_in, file_in, dir_out, file_out);
tests++; ntests++;
reset_escape_data(&escape_data); reset_escape_data(&escape_data);
break; break;
default: default:
@@ -271,11 +282,11 @@ main(int argc, char *argv[])
state = (state + 1) % MAX_STATE; state = (state + 1) % MAX_STATE;
} }
if (tests != 0) { if (ntests != 0) {
printf("iolog_path: %d test%s run, %d errors, %d%% success rate\n", printf("iolog_path: %d test%s run, %d errors, %d%% success rate\n",
tests, tests == 1 ? "" : "s", errors, ntests, ntests == 1 ? "" : "s", errors,
(tests - errors) * 100 / tests); (ntests - errors) * 100 / ntests);
} }
exit(errors); return errors;
} }

View File

@@ -130,19 +130,32 @@ test_adjust_delay(int *ntests, int *nerrors)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int tests = 0, errors = 0; int ch, ntests = 0, errors = 0;
initprogname(argc > 0 ? argv[0] : "check_iolog_timing"); initprogname(argc > 0 ? argv[0] : "check_iolog_timing");
test_parse_delay(&tests, &errors); while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
test_adjust_delay(&tests, &errors); test_parse_delay(&ntests, &errors);
if (tests != 0) { test_adjust_delay(&ntests, &errors);
if (ntests != 0) {
printf("iolog_timing: %d test%s run, %d errors, %d%% success rate\n", printf("iolog_timing: %d test%s run, %d errors, %d%% success rate\n",
tests, tests == 1 ? "" : "s", errors, ntests, ntests == 1 ? "" : "s", errors,
(tests - errors) * 100 / tests); (ntests - errors) * 100 / ntests);
} }
exit(errors); return errors;
} }

View File

@@ -39,9 +39,22 @@ sudo_dso_public int main(int argc, char *argv[]);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int fds[2], flag, maxfd, minfd, errors = 0, ntests = 0; int ch, fds[2], flag, maxfd, minfd, errors = 0, ntests = 0;
initprogname(argc > 0 ? argv[0] : "closefrom_test"); initprogname(argc > 0 ? argv[0] : "closefrom_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
/* We use pipe() because it doesn't rely on the filesystem. */ /* We use pipe() because it doesn't rely on the filesystem. */
ntests++; ntests++;
if (pipe(fds) == -1) { if (pipe(fds) == -1) {
@@ -103,5 +116,5 @@ done:
printf("%s: %d tests run, %d errors, %d%% success rate\n", printf("%s: %d tests run, %d errors, %d%% success rate\n",
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
} }
exit(errors); return errors;
} }

View File

@@ -25,14 +25,27 @@ main(int argc, char *argv[])
{ {
FILE *fp = stdin; FILE *fp = stdin;
char pattern[1024], string[1024], flagstr[1024]; char pattern[1024], string[1024], flagstr[1024];
int errors = 0, tests = 0, flags, got, want; int ch, errors = 0, ntests = 0, flags, got, want;
initprogname(argc > 0 ? argv[0] : "fnm_test"); initprogname(argc > 0 ? argv[0] : "fnm_test");
if (argc > 1) { while ((ch = getopt(argc, argv, "v")) != -1) {
if ((fp = fopen(argv[1], "r")) == NULL) { switch (ch) {
perror(argv[1]); case 'v':
exit(EXIT_FAILURE); /* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
if (argc > 0) {
if ((fp = fopen(argv[0], "r")) == NULL) {
perror(argv[0]);
return EXIT_FAILURE;
} }
} }
@@ -66,13 +79,13 @@ main(int argc, char *argv[])
pattern, string, flags, want, got); pattern, string, flags, want, got);
errors++; errors++;
} }
tests++; ntests++;
} }
} }
if (tests != 0) { if (ntests != 0) {
printf("fnmatch: %d test%s run, %d errors, %d%% success rate\n", printf("fnmatch: %d test%s run, %d errors, %d%% success rate\n",
tests, tests == 1 ? "" : "s", errors, ntests, ntests == 1 ? "" : "s", errors,
(tests - errors) * 100 / tests); (ntests - errors) * 100 / ntests);
} }
exit(errors); return errors;
} }

View File

@@ -154,9 +154,23 @@ main(int argc, char *argv[])
{ {
size_t buflen = 0; size_t buflen = 0;
char *buf = NULL; char *buf = NULL;
int ch;
initprogname(argc > 0 ? argv[0] : "getdelim_test"); initprogname(argc > 0 ? argv[0] : "getdelim_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
runtests(&buf, &buflen); runtests(&buf, &buflen);
/* XXX - redo tests with preallocated buffer filled with junk */ /* XXX - redo tests with preallocated buffer filled with junk */
@@ -164,5 +178,5 @@ main(int argc, char *argv[])
printf("%s: %d tests run, %d errors, %d%% success rate\n", printf("%s: %d tests run, %d errors, %d%% success rate\n",
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
} }
exit(errors); return errors;
} }

View File

@@ -48,16 +48,26 @@ main(int argc, char *argv[])
char *username = NULL; char *username = NULL;
GETGROUPS_T *groups = NULL; GETGROUPS_T *groups = NULL;
struct passwd *pw; struct passwd *pw;
int i, ngroups; int ch, i, ngroups;
gid_t basegid; gid_t basegid;
initprogname(argc > 0 ? argv[0] : "getgids"); initprogname(argc > 0 ? argv[0] : "getgids");
if (getopt(argc, argv, "") != -1) { while ((ch = getopt(argc, argv, "v")) != -1) {
fprintf(stderr, "usage: %s [user]\n", getprogname()); switch (ch) {
return EXIT_FAILURE; case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v] [user]\n", getprogname());
return EXIT_FAILURE;
}
} }
if (argc > 1) argc -= optind;
username = argv[1]; argv += optind;
if (argc > 0)
username = argv[0];
if (username != NULL) { if (username != NULL) {
if ((pw = getpwnam(username)) == NULL) if ((pw = getpwnam(username)) == NULL)

View File

@@ -48,11 +48,25 @@ main(int argc, char *argv[])
struct passwd *pw; struct passwd *pw;
struct group *grp; struct group *grp;
char *username; char *username;
int i, j, ntests = 0; int ch, i, j, ntests = 0;
int ngroups; int ngroups;
gid_t basegid; gid_t basegid;
initprogname(argc > 0 ? argv[0] : "getgrouplist_test"); initprogname(argc > 0 ? argv[0] : "getgrouplist_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
if ((pw = getpwuid(0)) == NULL) if ((pw = getpwuid(0)) == NULL)
sudo_fatal_nodebug("getpwuid(0)"); sudo_fatal_nodebug("getpwuid(0)");
basegid = pw->pw_gid; basegid = pw->pw_gid;
@@ -96,5 +110,5 @@ main(int argc, char *argv[])
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
} }
#endif /* HAVE_GETGROUPLIST_2 */ #endif /* HAVE_GETGROUPLIST_2 */
exit(errors); return errors;
} }

View File

@@ -36,16 +36,29 @@ main(int argc, char **argv)
{ {
FILE *fp = stdin; FILE *fp = stdin;
char buf[2048], *cp, *ep; char buf[2048], *cp, *ep;
int errors = 0, tests = 0, lineno; int ch, errors = 0, ntests = 0, lineno;
struct gl_entry entry; struct gl_entry entry;
size_t len; size_t len;
initprogname(argc > 0 ? argv[0] : "globtest"); initprogname(argc > 0 ? argv[0] : "globtest");
if (argc > 1) { while ((ch = getopt(argc, argv, "v")) != -1) {
if ((fp = fopen(argv[1], "r")) == NULL) { switch (ch) {
perror(argv[1]); case 'v':
exit(EXIT_FAILURE); /* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
if (argc > 0) {
if ((fp = fopen(argv[0], "r")) == NULL) {
perror(argv[0]);
return EXIT_FAILURE;
} }
} }
@@ -68,7 +81,7 @@ main(int argc, char **argv)
if (buf[len - 1] != '\n') { if (buf[len - 1] != '\n') {
fprintf(stderr, fprintf(stderr,
"globtest: missing newline at EOF\n"); "globtest: missing newline at EOF\n");
exit(EXIT_FAILURE); return EXIT_FAILURE;
} }
buf[--len] = '\0'; buf[--len] = '\0';
} }
@@ -79,7 +92,7 @@ main(int argc, char **argv)
/* check previous pattern */ /* check previous pattern */
if (entry.pattern[0]) { if (entry.pattern[0]) {
errors += test_glob(&entry); errors += test_glob(&entry);
tests++; ntests++;
} }
/* start new entry */ /* start new entry */
@@ -87,14 +100,14 @@ main(int argc, char **argv)
fprintf(stderr, fprintf(stderr,
"globtest: invalid entry on line %d\n", "globtest: invalid entry on line %d\n",
lineno); lineno);
exit(EXIT_FAILURE); return EXIT_FAILURE;
} }
len = cp - buf - 1; len = cp - buf - 1;
if (len >= sizeof(entry.pattern)) { if (len >= sizeof(entry.pattern)) {
fprintf(stderr, fprintf(stderr,
"globtest: pattern too big on line %d\n", "globtest: pattern too big on line %d\n",
lineno); lineno);
exit(EXIT_FAILURE); return EXIT_FAILURE;
} }
memcpy(entry.pattern, buf + 1, len); memcpy(entry.pattern, buf + 1, len);
entry.pattern[len] = '\0'; entry.pattern[len] = '\0';
@@ -104,14 +117,14 @@ main(int argc, char **argv)
fprintf(stderr, fprintf(stderr,
"globtest: invalid entry on line %d\n", "globtest: invalid entry on line %d\n",
lineno); lineno);
exit(EXIT_FAILURE); return EXIT_FAILURE;
} }
ep = strchr(cp, '>'); ep = strchr(cp, '>');
if (ep == NULL) { if (ep == NULL) {
fprintf(stderr, fprintf(stderr,
"globtest: invalid entry on line %d\n", "globtest: invalid entry on line %d\n",
lineno); lineno);
exit(EXIT_FAILURE); return EXIT_FAILURE;
} }
*ep = '\0'; *ep = '\0';
entry.flags = 0; entry.flags = 0;
@@ -138,7 +151,7 @@ main(int argc, char **argv)
fprintf(stderr, fprintf(stderr,
"globtest: invalid flags on line %d\n", "globtest: invalid flags on line %d\n",
lineno); lineno);
exit(EXIT_FAILURE); return EXIT_FAILURE;
} }
} }
entry.nresults = 0; entry.nresults = 0;
@@ -147,27 +160,27 @@ main(int argc, char **argv)
if (!entry.pattern[0]) { if (!entry.pattern[0]) {
fprintf(stderr, "globtest: missing entry on line %d\n", fprintf(stderr, "globtest: missing entry on line %d\n",
lineno); lineno);
exit(EXIT_FAILURE); return EXIT_FAILURE;
} }
if (entry.nresults + 1 > MAX_RESULTS) { if (entry.nresults + 1 > MAX_RESULTS) {
fprintf(stderr, fprintf(stderr,
"globtest: too many results for %s, max %d\n", "globtest: too many results for %s, max %d\n",
entry.pattern, MAX_RESULTS); entry.pattern, MAX_RESULTS);
exit(EXIT_FAILURE); return EXIT_FAILURE;
} }
entry.results[entry.nresults++] = strdup(buf); entry.results[entry.nresults++] = strdup(buf);
} }
if (entry.pattern[0]) { if (entry.pattern[0]) {
errors += test_glob(&entry); /* test last pattern */ errors += test_glob(&entry); /* test last pattern */
tests++; ntests++;
} }
if (tests != 0) { if (ntests != 0) {
printf("glob: %d test%s run, %d errors, %d%% success rate\n", printf("glob: %d test%s run, %d errors, %d%% success rate\n",
tests, tests == 1 ? "" : "s", errors, ntests, ntests == 1 ? "" : "s", errors,
(tests - errors) * 100 / tests); (ntests - errors) * 100 / ntests);
} }
exit(errors); return errors;
} }
int test_glob(struct gl_entry *entry) int test_glob(struct gl_entry *entry)
@@ -179,7 +192,7 @@ int test_glob(struct gl_entry *entry)
if (glob(entry->pattern, entry->flags, NULL, &gl) != 0) { if (glob(entry->pattern, entry->flags, NULL, &gl) != 0) {
fprintf(stderr, "glob failed: %s: %s\n", entry->pattern, fprintf(stderr, "glob failed: %s: %s\n", entry->pattern,
strerror(errno)); strerror(errno));
exit(EXIT_FAILURE); return 1;
} }
for (ap = gl.gl_pathv; *ap != NULL; ap++) for (ap = gl.gl_pathv; *ap != NULL; ap++)

View File

@@ -15,6 +15,7 @@
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
@@ -132,10 +133,23 @@ main(int argc, char *argv[])
char *p; char *p;
size_t clen; size_t clen;
long pg; long pg;
int i; int ch, i;
initprogname(argc > 0 ? argv[0] : "mktemp_test"); initprogname(argc > 0 ? argv[0] : "mktemp_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
pg = sysconf(_SC_PAGESIZE); pg = sysconf(_SC_PAGESIZE);
if (getcwd(cwd, sizeof cwd - 1) == NULL) if (getcwd(cwd, sizeof cwd - 1) == NULL)
sudo_fatal("getcwd"); sudo_fatal("getcwd");

View File

@@ -69,14 +69,28 @@ main(int argc, char *argv[])
{ {
GETGROUPS_T *gidlist = NULL; GETGROUPS_T *gidlist = NULL;
int i, j, errors = 0, ntests = 0; int i, j, errors = 0, ntests = 0;
int ngids; int ch, ngids;
initprogname(argc > 0 ? argv[0] : "parse_gids_test"); initprogname(argc > 0 ? argv[0] : "parse_gids_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
for (i = 0; test_data[i].gids != NULL; i++) { for (i = 0; test_data[i].gids != NULL; i++) {
free(gidlist); free(gidlist);
ngids = sudo_parse_gids(test_data[i].gids, test_data[i].baseptr, &gidlist); ngids = sudo_parse_gids(test_data[i].gids, test_data[i].baseptr, &gidlist);
if (ngids == -1) if (ngids == -1)
exit(EXIT_FAILURE); /* out of memory? */ sudo_fatal_nodebug("sudo_parse_gids");
ntests++; ntests++;
if (ngids != test_data[i].ngids) { if (ngids != test_data[i].ngids) {
sudo_warnx_nodebug("test #%d: expected %d gids, got %d", sudo_warnx_nodebug("test #%d: expected %d gids, got %d",
@@ -101,5 +115,5 @@ main(int argc, char *argv[])
printf("%s: %d tests run, %d errors, %d%% success rate\n", printf("%s: %d tests run, %d errors, %d%% success rate\n",
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
} }
exit(errors); return errors;
} }

View File

@@ -36,17 +36,31 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *progbase = "progname_test"; char *progbase = "progname_test";
int ch;
if (argc > 0) if (argc > 0)
progbase = sudo_basename(argv[0]); progbase = sudo_basename(argv[0]);
initprogname(progbase); initprogname(progbase);
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", progbase);
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
/* Make sure getprogname() matches basename of argv[0]. */ /* Make sure getprogname() matches basename of argv[0]. */
if (strcmp(getprogname(), progbase) != 0) { if (strcmp(getprogname(), progbase) != 0) {
printf("%s: FAIL: incorrect program name \"%s\"\n", printf("%s: FAIL: incorrect program name \"%s\"\n",
progbase, getprogname()); progbase, getprogname());
exit(EXIT_FAILURE); return EXIT_FAILURE;
} }
exit(EXIT_SUCCESS); return EXIT_SUCCESS;
} }

View File

@@ -288,11 +288,23 @@ void init_sigrt(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int errors = 0; int ch, errors = 0, ntests = 0;
int ntests = 0;
initprogname(argc > 0 ? argv[0] : "strsig_test"); initprogname(argc > 0 ? argv[0] : "strsig_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
init_sigrt(); init_sigrt();
errors += test_sig2str(&ntests); errors += test_sig2str(&ntests);
errors += test_str2sig(&ntests); errors += test_str2sig(&ntests);
@@ -302,5 +314,5 @@ main(int argc, char *argv[])
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
} }
exit(errors); return errors;
} }

View File

@@ -62,10 +62,24 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
const char *cp, *ep, *input_end; const char *cp, *ep, *input_end;
int i, j, errors = 0, ntests = 0; int ch, i, j, errors = 0, ntests = 0;
size_t len; size_t len;
initprogname(argc > 0 ? argv[0] : "strsplit_test"); initprogname(argc > 0 ? argv[0] : "strsplit_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
for (i = 0; test_data[i].input != NULL; i++) { for (i = 0; test_data[i].input != NULL; i++) {
input_end = test_data[i].input + test_data[i].input_len; input_end = test_data[i].input + test_data[i].input_len;
cp = sudo_strsplit(test_data[i].input, input_end, " \t", &ep); cp = sudo_strsplit(test_data[i].input, input_end, " \t", &ep);

View File

@@ -61,12 +61,24 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct strtobool_data *d; struct strtobool_data *d;
int errors = 0; int errors = 0, ntests = 0;
int ntests = 0; int ch, value;
int value;
initprogname(argc > 0 ? argv[0] : "strtobool_test"); initprogname(argc > 0 ? argv[0] : "strtobool_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
for (d = strtobool_data; d->bool_str != NULL; d++) { for (d = strtobool_data; d->bool_str != NULL; d++) {
ntests++; ntests++;
value = sudo_strtobool(d->bool_str); value = sudo_strtobool(d->bool_str);

View File

@@ -56,15 +56,27 @@ static struct strtoidx_data {
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int ch, errors = 0, ntests = 0;
struct strtoidx_data *d; struct strtoidx_data *d;
const char *errstr; const char *errstr;
char *ep; char *ep;
int errors = 0;
int ntests = 0;
id_t value; id_t value;
initprogname(argc > 0 ? argv[0] : "strtoid_test"); initprogname(argc > 0 ? argv[0] : "strtoid_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
for (d = strtoidx_data; d->idstr != NULL; d++) { for (d = strtoidx_data; d->idstr != NULL; d++) {
ntests++; ntests++;
errstr = "some error"; errstr = "some error";

View File

@@ -47,12 +47,24 @@ main(int argc, char *argv[])
{ {
struct strtomode_data *d; struct strtomode_data *d;
const char *errstr; const char *errstr;
int errors = 0; int ch, errors = 0, ntests = 0;
int ntests = 0;
mode_t mode; mode_t mode;
initprogname(argc > 0 ? argv[0] : "strtomode_test"); initprogname(argc > 0 ? argv[0] : "strtomode_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
for (d = strtomode_data; d->mode_str != NULL; d++) { for (d = strtomode_data; d->mode_str != NULL; d++) {
ntests++; ntests++;
errstr = "some error"; errstr = "some error";

View File

@@ -78,14 +78,26 @@ static struct strtonum_data {
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int ch, errors = 0, ntests = 0;
struct strtonum_data *d; struct strtonum_data *d;
const char *errstr; const char *errstr;
int errors = 0;
int ntests = 0;
long long value; long long value;
initprogname(argc > 0 ? argv[0] : "strtonum_test"); initprogname(argc > 0 ? argv[0] : "strtonum_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
for (d = strtonum_data; d->str != NULL; d++) { for (d = strtonum_data; d->str != NULL; d++) {
ntests++; ntests++;
errstr = "some error"; errstr = "some error";

View File

@@ -37,6 +37,13 @@ sudo_dso_public int main(int argc, char *argv[]);
# define GROUP_SOURCE_ADAPTIVE GROUP_SOURCE_DYNAMIC # define GROUP_SOURCE_ADAPTIVE GROUP_SOURCE_DYNAMIC
#endif #endif
static void
usage(void)
{
fprintf(stderr, "usage: %s [-v] conf_file\n", getprogname());
exit(EXIT_FAILURE);
}
/* /*
* Simple test driver for sudo_conf(). * Simple test driver for sudo_conf().
* Parses the given configuration file and dumps the resulting * Parses the given configuration file and dumps the resulting
@@ -45,17 +52,31 @@ sudo_dso_public int main(int argc, char *argv[]);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int ch;
initprogname(argc > 0 ? argv[0] : "conf_test"); initprogname(argc > 0 ? argv[0] : "conf_test");
if (argc != 2) {
fprintf(stderr, "usage: %s conf_file\n", getprogname()); while ((ch = getopt(argc, argv, "v")) != -1) {
exit(EXIT_FAILURE); switch (ch) {
case 'v':
/* ignore */
break;
default:
usage();
}
} }
argc -= optind;
argv += optind;
if (argc != 1)
usage();
sudo_conf_clear_paths(); sudo_conf_clear_paths();
if (sudo_conf_read(argv[1], SUDO_CONF_ALL) == -1) if (sudo_conf_read(argv[0], SUDO_CONF_ALL) == -1)
exit(EXIT_FAILURE); return EXIT_FAILURE;
sudo_conf_dump(); sudo_conf_dump();
exit(EXIT_SUCCESS); return EXIT_SUCCESS;
} }
static void static void

View File

@@ -39,11 +39,25 @@ main(int argc, char *argv[])
unsigned int lineno = 0; unsigned int lineno = 0;
size_t linesize = 0; size_t linesize = 0;
char *line = NULL; char *line = NULL;
int ch;
initprogname(argc > 0 ? argv[0] : "parseln_test"); initprogname(argc > 0 ? argv[0] : "parseln_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
while (sudo_parseln(&line, &linesize, &lineno, stdin, 0) != -1) while (sudo_parseln(&line, &linesize, &lineno, stdin, 0) != -1)
printf("%6u\t%s\n", lineno, line); printf("%6u\t%s\n", lineno, line);
free(line); free(line);
exit(EXIT_SUCCESS); return EXIT_SUCCESS;
} }

View File

@@ -51,11 +51,23 @@ main(int argc, char *argv[])
struct test_data d1, d2, d3; struct test_data d1, d2, d3;
struct test_data *hltq; struct test_data *hltq;
struct test_data_list tq; struct test_data_list tq;
int errors = 0; int ch, errors = 0, ntests = 0;
int ntests = 0;
initprogname(argc > 0 ? argv[0] : "hltq_test"); initprogname(argc > 0 ? argv[0] : "hltq_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
/* /*
* Initialize three data elements and concatenate them in order. * Initialize three data elements and concatenate them in order.
*/ */

View File

@@ -51,15 +51,27 @@ struct uuid {
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int ch, errors = 0, ntests = 0;
union { union {
struct uuid id; struct uuid id;
unsigned char u8[16]; unsigned char u8[16];
} uuid; } uuid;
int errors = 0;
int ntests = 0;
initprogname(argc > 0 ? argv[0] : "uuid_test"); initprogname(argc > 0 ? argv[0] : "uuid_test");
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
/* ignore */
break;
default:
fprintf(stderr, "usage: %s [-v]\n", getprogname());
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
/* Do 16 passes. */ /* Do 16 passes. */
for (ntests = 0; ntests < 16; ntests++) { for (ntests = 0; ntests < 16; ntests++) {
sudo_uuid_create(uuid.u8); sudo_uuid_create(uuid.u8);
@@ -88,5 +100,5 @@ main(int argc, char *argv[])
printf("%s: %d tests run, %d errors, %d%% success rate\n", printf("%s: %d tests run, %d errors, %d%% success rate\n",
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
} }
return 0; return errors;
} }