mirror of
https://github.com/sudo-project/sudo.git
synced 2025-08-22 01:49:11 +00:00
Add my regress tests for fnmatch() and glob() from OpenBSD.
This commit is contained in:
parent
460f760ca2
commit
ac857d6e9d
@ -65,6 +65,12 @@ libreplace.la: $(LTLIBOBJS)
|
||||
./mksiglist: $(srcdir)/mksiglist.c $(srcdir)/mksiglist.h $(incdir)/missing.h $(top_builddir)/config.h
|
||||
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEFS) $(srcdir)/mksiglist.c -o $@
|
||||
|
||||
fnm_test: $(srcdir)/regress/fnmatch/fnm_test.c
|
||||
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEFS) $(srcdir)/regress/fnmatch/fnm_test.c -o $@
|
||||
|
||||
globtest: $(srcdir)/regress/glob/globtest.c
|
||||
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEFS) $(srcdir)/regress/glob/globtest.c -o $@
|
||||
|
||||
@DEV@$(srcdir)/mksiglist.h: $(srcdir)/siglist.in
|
||||
@DEV@ awk 'BEGIN {print "/* public domain */\n"} /^ [A-Z]/ {printf("#ifdef SIG%s\n if (my_sys_siglist[SIG%s] == NULL)\n\tmy_sys_siglist[SIG%s] = \"%s\";\n#endif\n", $$1, $$1, $$1, substr($$0, 13))}' < $(srcdir)/siglist.in > $@
|
||||
|
||||
@ -122,7 +128,15 @@ install-plugin:
|
||||
|
||||
uninstall:
|
||||
|
||||
check:
|
||||
check: fnm_test globtest
|
||||
@./fnm_test $(srcdir)/regress/fnmatch/fnm_test.in
|
||||
@mkdir -p `sed 's@/[^/]*$$@@' $(srcdir)/regress/glob/files | sort -u`
|
||||
@touch `cat $(srcdir)/regress/glob/files`
|
||||
@chmod 0755 `grep '/r[^/]*$$' $(srcdir)/regress/glob/files`
|
||||
@chmod 0444 `grep '/s[^/]*$$' $(srcdir)/regress/glob/files`
|
||||
@chmod 0711 `grep '/t[^/]*$$' $(srcdir)/regress/glob/files`
|
||||
@./globtest $(srcdir)/regress/glob/globtest.in
|
||||
@rm -rf fake
|
||||
|
||||
clean:
|
||||
-$(LIBTOOL) --mode=clean rm -f mksiglist siglist.c *.lo *.o *.la *.a stamp-* core *.core core.*
|
||||
|
59
compat/regress/fnmatch/fnm_test.c
Normal file
59
compat/regress/fnmatch/fnm_test.c
Normal file
@ -0,0 +1,59 @@
|
||||
/* $OpenBSD: fnm_test.c,v 1.1 2008/10/01 23:04:58 millert Exp $ */
|
||||
|
||||
/*
|
||||
* Public domain, 2008, Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#ifdef HAVE_FNMATCH
|
||||
# include <fnmatch.h>
|
||||
#else
|
||||
# include "compat/fnmatch.h"
|
||||
#endif
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
FILE *fp = stdin;
|
||||
char pattern[1024], string[1024];
|
||||
int errors = 0, tests = 0, flags, got, want;
|
||||
|
||||
if (argc > 1) {
|
||||
if ((fp = fopen(argv[1], "r")) == NULL) {
|
||||
perror(argv[1]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Read in test file, which is formatted thusly:
|
||||
*
|
||||
* pattern string flags expected_result
|
||||
*
|
||||
*/
|
||||
for (;;) {
|
||||
got = fscanf(fp, "%s %s 0x%x %d\n", pattern, string, &flags,
|
||||
&want);
|
||||
if (got == EOF)
|
||||
break;
|
||||
if (got == 4) {
|
||||
got = fnmatch(pattern, string, flags);
|
||||
if (got != want) {
|
||||
fprintf(stderr,
|
||||
"fnmatch: %s %s %d: want %d, got %d\n",
|
||||
pattern, string, flags, want, got);
|
||||
errors++;
|
||||
}
|
||||
tests++;
|
||||
}
|
||||
}
|
||||
if (tests != 0) {
|
||||
printf("fnmatch: %d test%s run, %d errors, %d%% success rate\n",
|
||||
tests, tests == 1 ? "" : "s", errors,
|
||||
(tests - errors) * 100 / tests);
|
||||
}
|
||||
exit(errors);
|
||||
}
|
5
compat/regress/fnmatch/fnm_test.in
Normal file
5
compat/regress/fnmatch/fnm_test.in
Normal file
@ -0,0 +1,5 @@
|
||||
/bin/[[:alpha:][:alnum:]]* /bin/ls 0x2 0
|
||||
/bin/[[:upper:]][[:alnum:]] /bin/ls 0x10 0
|
||||
/bin/[[:opper:][:alnum:]]* /bin/ls 0x0 1
|
||||
[[:alpha:][:alnum:]]*.c foo1.c 0x4 0
|
||||
[[:upper:]]* FOO 0x0 0
|
47
compat/regress/glob/files
Normal file
47
compat/regress/glob/files
Normal file
@ -0,0 +1,47 @@
|
||||
fake/bin/[
|
||||
fake/bin/cat
|
||||
fake/bin/chgrp
|
||||
fake/bin/chio
|
||||
fake/bin/chmod
|
||||
fake/bin/cksum
|
||||
fake/bin/cp
|
||||
fake/bin/cpio
|
||||
fake/bin/csh
|
||||
fake/bin/date
|
||||
fake/bin/dd
|
||||
fake/bin/df
|
||||
fake/bin/domainname
|
||||
fake/bin/echo
|
||||
fake/bin/ed
|
||||
fake/bin/eject
|
||||
fake/bin/expr
|
||||
fake/bin/hostname
|
||||
fake/bin/kill
|
||||
fake/bin/ksh
|
||||
fake/bin/ln
|
||||
fake/bin/ls
|
||||
fake/bin/md5
|
||||
fake/bin/mkdir
|
||||
fake/bin/mt
|
||||
fake/bin/mv
|
||||
fake/bin/pax
|
||||
fake/bin/ps
|
||||
fake/bin/pwd
|
||||
fake/bin/rcp
|
||||
fake/bin/rksh
|
||||
fake/bin/rm
|
||||
fake/bin/rmail
|
||||
fake/bin/rmd160
|
||||
fake/bin/rmdir
|
||||
fake/bin/sh
|
||||
fake/bin/sha1
|
||||
fake/bin/sha256
|
||||
fake/bin/sha384
|
||||
fake/bin/sha512
|
||||
fake/bin/sleep
|
||||
fake/bin/stty
|
||||
fake/bin/sum
|
||||
fake/bin/sync
|
||||
fake/bin/systrace
|
||||
fake/bin/tar
|
||||
fake/bin/test
|
192
compat/regress/glob/globtest.c
Normal file
192
compat/regress/glob/globtest.c
Normal file
@ -0,0 +1,192 @@
|
||||
/* $OpenBSD: globtest.c,v 1.2 2010/09/24 13:32:55 djm Exp $ */
|
||||
|
||||
/*
|
||||
* Public domain, 2008, Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#ifdef HAVE_STRING_H
|
||||
# include <string.h>
|
||||
#endif /* HAVE_STRING_H */
|
||||
#ifdef HAVE_STRINGS_H
|
||||
# include <strings.h>
|
||||
#endif /* HAVE_STRINGS_H */
|
||||
#ifdef HAVE_GLOB
|
||||
# include <glob.h>
|
||||
#else
|
||||
# include "compat/glob.h"
|
||||
#endif
|
||||
|
||||
#define MAX_RESULTS 256
|
||||
|
||||
struct gl_entry {
|
||||
int flags;
|
||||
int nresults;
|
||||
char pattern[1024];
|
||||
char *results[MAX_RESULTS];
|
||||
mode_t modes[MAX_RESULTS];
|
||||
};
|
||||
|
||||
int test_glob(struct gl_entry *);
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
FILE *fp = stdin;
|
||||
char *buf, *cp;
|
||||
int errors = 0, tests = 0, lineno, mode;
|
||||
struct gl_entry entry;
|
||||
size_t len;
|
||||
|
||||
if (argc > 1) {
|
||||
if ((fp = fopen(argv[1], "r")) == NULL) {
|
||||
perror(argv[1]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Read in test file, which is formatted thusly:
|
||||
*
|
||||
* [pattern] <flags>
|
||||
* result1 [mode]
|
||||
* result2 [mode]
|
||||
* result3 [mode]
|
||||
* ...
|
||||
*
|
||||
*/
|
||||
lineno = 0;
|
||||
memset(&entry, 0, sizeof(entry));
|
||||
while ((buf = fgetln(fp, &len)) != NULL) {
|
||||
lineno++;
|
||||
if (buf[len - 1] != '\n') {
|
||||
fprintf(stderr, "globtest: missing newline at EOF\n");
|
||||
exit(1);
|
||||
}
|
||||
buf[--len] = '\0';
|
||||
if (len == 0)
|
||||
continue; /* blank line */
|
||||
|
||||
if (buf[0] == '[') {
|
||||
/* check previous pattern */
|
||||
if (entry.pattern[0]) {
|
||||
errors += test_glob(&entry);
|
||||
tests++;
|
||||
}
|
||||
|
||||
/* start new entry */
|
||||
if ((cp = strrchr(buf + 1, ']')) == NULL) {
|
||||
fprintf(stderr,
|
||||
"globtest: invalid entry on line %d\n",
|
||||
lineno);
|
||||
exit(1);
|
||||
}
|
||||
len = cp - buf - 1;
|
||||
if (len >= sizeof(entry.pattern)) {
|
||||
fprintf(stderr,
|
||||
"globtest: pattern too big on line %d\n",
|
||||
lineno);
|
||||
exit(1);
|
||||
}
|
||||
memcpy(entry.pattern, buf + 1, len);
|
||||
entry.pattern[len] = '\0';
|
||||
|
||||
buf = cp + 2;
|
||||
if (*buf++ != '<') {
|
||||
fprintf(stderr,
|
||||
"globtest: invalid entry on line %d\n",
|
||||
lineno);
|
||||
exit(1);
|
||||
}
|
||||
if ((cp = strchr(buf, '>')) == NULL) {
|
||||
fprintf(stderr,
|
||||
"globtest: invalid entry on line %d\n",
|
||||
lineno);
|
||||
exit(1);
|
||||
}
|
||||
entry.flags = (int)strtol(buf, &cp, 0);
|
||||
if (*cp != '>' || entry.flags < 0 || entry.flags > 0x4000) {
|
||||
fprintf(stderr,
|
||||
"globtest: invalid flags: %s\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
entry.nresults = 0;
|
||||
continue;
|
||||
}
|
||||
if (!entry.pattern[0]) {
|
||||
fprintf(stderr, "globtest: missing entry on line %d\n",
|
||||
lineno);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (entry.nresults + 1 > MAX_RESULTS) {
|
||||
fprintf(stderr,
|
||||
"globtest: too many results for %s, max %d\n",
|
||||
entry.pattern, MAX_RESULTS);
|
||||
exit(1);
|
||||
}
|
||||
if ((cp = strchr(buf, ' ')) != NULL) {
|
||||
*cp++ = '\0';
|
||||
mode = strtol(cp, NULL, 8);
|
||||
} else
|
||||
mode = -1;
|
||||
entry.modes[entry.nresults] = (mode_t)mode;
|
||||
entry.results[entry.nresults++] = strdup(buf);
|
||||
}
|
||||
if (entry.pattern[0]) {
|
||||
errors += test_glob(&entry); /* test last pattern */
|
||||
tests++;
|
||||
}
|
||||
if (tests != 0) {
|
||||
printf("glob: %d test%s run, %d errors, %d%% success rate\n",
|
||||
tests, tests == 1 ? "" : "s", errors,
|
||||
(tests - errors) * 100 / tests);
|
||||
}
|
||||
exit(errors);
|
||||
}
|
||||
|
||||
int test_glob(struct gl_entry *entry)
|
||||
{
|
||||
glob_t gl;
|
||||
int i = 0;
|
||||
|
||||
if (glob(entry->pattern, entry->flags, NULL, &gl) != 0) {
|
||||
fprintf(stderr, "glob failed: %s", entry->pattern);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (gl.gl_matchc != entry->nresults)
|
||||
goto mismatch;
|
||||
|
||||
for (i = 0; i < gl.gl_matchc; i++) {
|
||||
if (strcmp(gl.gl_pathv[i], entry->results[i]) != 0)
|
||||
goto mismatch;
|
||||
if ((entry->flags & GLOB_KEEPSTAT) != 0) {
|
||||
if (entry->modes[i] == -1 ||
|
||||
gl.gl_statv[i] == NULL ||
|
||||
entry->modes[i] != gl.gl_statv[i]->st_mode)
|
||||
goto badmode;
|
||||
}
|
||||
free(entry->results[i]);
|
||||
}
|
||||
return (0);
|
||||
badmode:
|
||||
fprintf(stderr, "globtest: mismatch mode for pattern %s, flags 0x%x, "
|
||||
"file \"%s\" (found %07o, expected %07o)\n",
|
||||
entry->pattern, entry->flags,
|
||||
gl.gl_pathv[i], gl.gl_statv[i] ? gl.gl_statv[i]->st_mode : 0,
|
||||
entry->modes[i]);
|
||||
goto cleanup;
|
||||
mismatch:
|
||||
fprintf(stderr, "globtest: mismatch for pattern %s, flags 0x%x "
|
||||
"(found \"%s\", expected \"%s\")\n", entry->pattern, entry->flags,
|
||||
gl.gl_pathv[i], entry->results[i]);
|
||||
cleanup:
|
||||
while (i < gl.gl_matchc) {
|
||||
free(entry->results[i++]);
|
||||
}
|
||||
return (1);
|
||||
}
|
116
compat/regress/glob/globtest.in
Normal file
116
compat/regress/glob/globtest.in
Normal file
@ -0,0 +1,116 @@
|
||||
[fake/bin/[[:alpha:]]*] <0x0>
|
||||
fake/bin/cat
|
||||
fake/bin/chgrp
|
||||
fake/bin/chio
|
||||
fake/bin/chmod
|
||||
fake/bin/cksum
|
||||
fake/bin/cp
|
||||
fake/bin/cpio
|
||||
fake/bin/csh
|
||||
fake/bin/date
|
||||
fake/bin/dd
|
||||
fake/bin/df
|
||||
fake/bin/domainname
|
||||
fake/bin/echo
|
||||
fake/bin/ed
|
||||
fake/bin/eject
|
||||
fake/bin/expr
|
||||
fake/bin/hostname
|
||||
fake/bin/kill
|
||||
fake/bin/ksh
|
||||
fake/bin/ln
|
||||
fake/bin/ls
|
||||
fake/bin/md5
|
||||
fake/bin/mkdir
|
||||
fake/bin/mt
|
||||
fake/bin/mv
|
||||
fake/bin/pax
|
||||
fake/bin/ps
|
||||
fake/bin/pwd
|
||||
fake/bin/rcp
|
||||
fake/bin/rksh
|
||||
fake/bin/rm
|
||||
fake/bin/rmail
|
||||
fake/bin/rmd160
|
||||
fake/bin/rmdir
|
||||
fake/bin/sh
|
||||
fake/bin/sha1
|
||||
fake/bin/sha256
|
||||
fake/bin/sha384
|
||||
fake/bin/sha512
|
||||
fake/bin/sleep
|
||||
fake/bin/stty
|
||||
fake/bin/sum
|
||||
fake/bin/sync
|
||||
fake/bin/systrace
|
||||
fake/bin/tar
|
||||
fake/bin/test
|
||||
|
||||
[fake/bin/[[:alpha:]]*] <0x4000>
|
||||
fake/bin/cat 0100644
|
||||
fake/bin/chgrp 0100644
|
||||
fake/bin/chio 0100644
|
||||
fake/bin/chmod 0100644
|
||||
fake/bin/cksum 0100644
|
||||
fake/bin/cp 0100644
|
||||
fake/bin/cpio 0100644
|
||||
fake/bin/csh 0100644
|
||||
fake/bin/date 0100644
|
||||
fake/bin/dd 0100644
|
||||
fake/bin/df 0100644
|
||||
fake/bin/domainname 0100644
|
||||
fake/bin/echo 0100644
|
||||
fake/bin/ed 0100644
|
||||
fake/bin/eject 0100644
|
||||
fake/bin/expr 0100644
|
||||
fake/bin/hostname 0100644
|
||||
fake/bin/kill 0100644
|
||||
fake/bin/ksh 0100644
|
||||
fake/bin/ln 0100644
|
||||
fake/bin/ls 0100644
|
||||
fake/bin/md5 0100644
|
||||
fake/bin/mkdir 0100644
|
||||
fake/bin/mt 0100644
|
||||
fake/bin/mv 0100644
|
||||
fake/bin/pax 0100644
|
||||
fake/bin/ps 0100644
|
||||
fake/bin/pwd 0100644
|
||||
fake/bin/rcp 0100755
|
||||
fake/bin/rksh 0100755
|
||||
fake/bin/rm 0100755
|
||||
fake/bin/rmail 0100755
|
||||
fake/bin/rmd160 0100755
|
||||
fake/bin/rmdir 0100755
|
||||
fake/bin/sh 0100444
|
||||
fake/bin/sha1 0100444
|
||||
fake/bin/sha256 0100444
|
||||
fake/bin/sha384 0100444
|
||||
fake/bin/sha512 0100444
|
||||
fake/bin/sleep 0100444
|
||||
fake/bin/stty 0100444
|
||||
fake/bin/sum 0100444
|
||||
fake/bin/sync 0100444
|
||||
fake/bin/systrace 0100444
|
||||
fake/bin/tar 0100711
|
||||
fake/bin/test 0100711
|
||||
|
||||
[fake/bin/rm{,dir,ail}] <0x80>
|
||||
fake/bin/rm
|
||||
fake/bin/rmdir
|
||||
fake/bin/rmail
|
||||
|
||||
[fake/bin/sha[[:digit:]]] <0x0>
|
||||
fake/bin/sha1
|
||||
|
||||
[fake/bin/sha[[:digit:]]*] <0x0>
|
||||
fake/bin/sha1
|
||||
fake/bin/sha256
|
||||
fake/bin/sha384
|
||||
fake/bin/sha512
|
||||
|
||||
[fake/bin/ca[a-z]] <0x0>
|
||||
fake/bin/cat
|
||||
|
||||
[fake/b[a-z]*] <0x4000>
|
||||
fake/bin 0040755
|
||||
|
Loading…
x
Reference in New Issue
Block a user