summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/mkstemp
diff options
context:
space:
mode:
authorguenther <>2010-02-11 07:35:38 +0000
committerguenther <>2010-02-11 07:35:38 +0000
commit387b67884b39e3f399ad34aa12a4e897ce8c1c71 (patch)
tree39704368cdd08a910db69863f93408adeda6b894 /src/regress/lib/libc/mkstemp
parentf6129285462a0eff1c43458954cfc5ac8b927444 (diff)
downloadopenbsd-387b67884b39e3f399ad34aa12a4e897ce8c1c71.tar.gz
openbsd-387b67884b39e3f399ad34aa12a4e897ce8c1c71.tar.bz2
openbsd-387b67884b39e3f399ad34aa12a4e897ce8c1c71.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/libc/mkstemp')
-rw-r--r--src/regress/lib/libc/mkstemp/Makefile19
-rw-r--r--src/regress/lib/libc/mkstemp/mkstemp_test.c95
2 files changed, 114 insertions, 0 deletions
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
3PROG=mkstemp_test
4CLEANFILES+= ; rm -rf output
5
6O = output
7
8# The ktrace/kdump/perl combo verifies that all open() calls that
9# were passed O_CREAT were also passed O_EXCL
10run-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
23long pg;
24
25void
26try(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
60int
61main(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}