diff options
author | guenther <> | 2010-02-11 07:35:38 +0000 |
---|---|---|
committer | guenther <> | 2010-02-11 07:35:38 +0000 |
commit | 53dc25964d1d5f5dffbf61f764b63a30be5f8947 (patch) | |
tree | 39704368cdd08a910db69863f93408adeda6b894 /src/regress/lib | |
parent | 2f8099199567a7110e7f8796aa42725ee02c3228 (diff) | |
download | openbsd-53dc25964d1d5f5dffbf61f764b63a30be5f8947.tar.gz openbsd-53dc25964d1d5f5dffbf61f764b63a30be5f8947.tar.bz2 openbsd-53dc25964d1d5f5dffbf61f764b63a30be5f8947.zip |
Add a regression suite for mkstemp(), verifying that it neither overruns
no underruns the supplied template buffer, that it can generate names that
don't contain any X's, and that all open() calls that pass O_CREAT also
pass O_EXCL
Diffstat (limited to 'src/regress/lib')
-rw-r--r-- | src/regress/lib/libc/Makefile | 4 | ||||
-rw-r--r-- | src/regress/lib/libc/mkstemp/Makefile | 19 | ||||
-rw-r--r-- | src/regress/lib/libc/mkstemp/mkstemp_test.c | 95 |
3 files changed, 116 insertions, 2 deletions
diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile index 3c24e3f634..ddbb9505fe 100644 --- a/src/regress/lib/libc/Makefile +++ b/src/regress/lib/libc/Makefile | |||
@@ -1,10 +1,10 @@ | |||
1 | # $OpenBSD: Makefile,v 1.30 2009/11/21 09:56:10 guenther Exp $ | 1 | # $OpenBSD: Makefile,v 1.31 2010/02/11 07:35:38 guenther Exp $ |
2 | 2 | ||
3 | SUBDIR+= _setjmp alloca atexit basename cxa-atexit db dirname fnmatch | 3 | SUBDIR+= _setjmp alloca atexit basename cxa-atexit db dirname fnmatch |
4 | SUBDIR+= fpclassify getaddrinfo getcap getopt_long glob hsearch longjmp | 4 | SUBDIR+= fpclassify getaddrinfo getcap getopt_long glob hsearch longjmp |
5 | SUBDIR+= locale malloc netdb popen printf regex setjmp setjmp-signal | 5 | SUBDIR+= locale malloc netdb popen printf regex setjmp setjmp-signal |
6 | SUBDIR+= sigreturn sigsetjmp sprintf strerror strtod strtonum telldir time vis | 6 | SUBDIR+= sigreturn sigsetjmp sprintf strerror strtod strtonum telldir time vis |
7 | SUBDIR+= orientation stdio_threading | 7 | SUBDIR+= orientation stdio_threading mkstemp |
8 | 8 | ||
9 | .if (${MACHINE_ARCH} != "vax") | 9 | .if (${MACHINE_ARCH} != "vax") |
10 | SUBDIR+= ieeefp | 10 | SUBDIR+= ieeefp |
diff --git a/src/regress/lib/libc/mkstemp/Makefile b/src/regress/lib/libc/mkstemp/Makefile new file mode 100644 index 0000000000..dc7eb281a9 --- /dev/null +++ b/src/regress/lib/libc/mkstemp/Makefile | |||
@@ -0,0 +1,19 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2010/02/11 07:35:38 guenther Exp $ | ||
2 | |||
3 | PROG=mkstemp_test | ||
4 | CLEANFILES+= ; rm -rf output | ||
5 | |||
6 | O = output | ||
7 | |||
8 | # The ktrace/kdump/perl combo verifies that all open() calls that | ||
9 | # were passed O_CREAT were also passed O_EXCL | ||
10 | run-regress-${PROG}: ${PROG} | ||
11 | mkdir -p $O && cd $O && ktrace ../${PROG} | ||
12 | cd $O && kdump | perl -mFcntl -nl \ | ||
13 | -e '/ open\([^,]*,([^,]+)/ or next;' \ | ||
14 | -e '$$f = oct($$1);' \ | ||
15 | -e 'if ($$f & O_CREAT && !($$f & O_EXCL)) {' \ | ||
16 | -e ' print "FAIL"; exit 1' \ | ||
17 | -e '}' | ||
18 | |||
19 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libc/mkstemp/mkstemp_test.c b/src/regress/lib/libc/mkstemp/mkstemp_test.c new file mode 100644 index 0000000000..d8311cbf35 --- /dev/null +++ b/src/regress/lib/libc/mkstemp/mkstemp_test.c | |||
@@ -0,0 +1,95 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2010 Philip Guenther <guenther@openbsd.org> | ||
3 | * | ||
4 | * Public domain. | ||
5 | * | ||
6 | * Verify that mkstemp() doesn't overrun or underrun the template buffer | ||
7 | * and that it can generate names that don't contain any X's | ||
8 | */ | ||
9 | |||
10 | #include <sys/param.h> | ||
11 | |||
12 | #include <err.h> | ||
13 | #include <stdio.h> | ||
14 | #include <stdlib.h> | ||
15 | #include <string.h> | ||
16 | #include <sys/mman.h> | ||
17 | #include <sys/stat.h> | ||
18 | #include <unistd.h> | ||
19 | |||
20 | #define MAX_TEMPLATE_LEN 10 | ||
21 | #define MAX_TRIES 100 | ||
22 | |||
23 | long pg; | ||
24 | |||
25 | void | ||
26 | try(char *p, const char *prefix, int len) | ||
27 | { | ||
28 | struct stat sb, fsb; | ||
29 | char *q; | ||
30 | size_t plen = strlen(prefix); | ||
31 | int tries, fd; | ||
32 | |||
33 | for (tries = 0; tries < MAX_TRIES; tries++) { | ||
34 | memcpy(p, prefix, plen); | ||
35 | memset(p + plen, 'X', len); | ||
36 | p[plen + len] = '\0'; | ||
37 | fd = mkstemp(p); | ||
38 | if (fd < 0) | ||
39 | err(1, "mkstemp"); | ||
40 | if (stat(p, &sb)) | ||
41 | err(1, "stat(%s)", p); | ||
42 | if (fstat(fd, &fsb)) | ||
43 | err(1, "fstat(%d==%s)", fd, p); | ||
44 | if (sb.st_dev != fsb.st_dev || sb.st_ino != fsb.st_ino) | ||
45 | errx(1, "stat mismatch"); | ||
46 | close(fd); | ||
47 | for (q = p + plen; *q != 'X'; q++) { | ||
48 | if (*q == '\0') { | ||
49 | if (q != p + plen + len) | ||
50 | errx(1, "unexpected truncation"); | ||
51 | return; | ||
52 | } | ||
53 | } | ||
54 | if (q >= p + plen + len) | ||
55 | errx(1, "overrun?"); | ||
56 | } | ||
57 | errx(1, "exceeded MAX_TRIES"); | ||
58 | } | ||
59 | |||
60 | int | ||
61 | main(void) | ||
62 | { | ||
63 | struct stat sb, fsb; | ||
64 | char cwd[MAXPATHLEN + 1]; | ||
65 | char *p; | ||
66 | size_t clen; | ||
67 | int i; | ||
68 | |||
69 | pg = sysconf(_SC_PAGESIZE); | ||
70 | if (getcwd(cwd, sizeof cwd - 1) == NULL) | ||
71 | err(1, "getcwd"); | ||
72 | clen = strlen(cwd); | ||
73 | cwd[clen++] = '/'; | ||
74 | cwd[clen] = '\0'; | ||
75 | p = mmap(NULL, pg * 3, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); | ||
76 | if (p == NULL) | ||
77 | err(1, "mmap"); | ||
78 | if (mprotect(p, pg, PROT_NONE) || mprotect(p + pg * 2, pg, PROT_NONE)) | ||
79 | err(1, "mprotect"); | ||
80 | p += pg; | ||
81 | |||
82 | i = MAX_TEMPLATE_LEN + 1; | ||
83 | while (i-- > 1) { | ||
84 | /* try first at the start of a page, no prefix */ | ||
85 | try(p, "", i); | ||
86 | /* now at the end of the page, no prefix */ | ||
87 | try(p + pg - i - 1, "", i); | ||
88 | /* start of the page, prefixed with the cwd */ | ||
89 | try(p, cwd, i); | ||
90 | /* how about at the end of the page, prefixed with cwd? */ | ||
91 | try(p + pg - i - 1 - clen, cwd, i); | ||
92 | } | ||
93 | |||
94 | return 0; | ||
95 | } | ||