summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/mkstemp
diff options
context:
space:
mode:
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.c156
2 files changed, 0 insertions, 175 deletions
diff --git a/src/regress/lib/libc/mkstemp/Makefile b/src/regress/lib/libc/mkstemp/Makefile
deleted file mode 100644
index dc7eb281a9..0000000000
--- a/src/regress/lib/libc/mkstemp/Makefile
+++ /dev/null
@@ -1,19 +0,0 @@
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
deleted file mode 100644
index fd133d8368..0000000000
--- a/src/regress/lib/libc/mkstemp/mkstemp_test.c
+++ /dev/null
@@ -1,156 +0,0 @@
1/*
2 * Copyright (c) 2010 Philip Guenther <guenther@openbsd.org>
3 *
4 * Public domain.
5 *
6 * Verify that mkstemp() and mkstemps() doesn't overrun or underrun
7 * the template buffer and that it can generate names that don't
8 * contain any X's
9 */
10
11#include <sys/param.h>
12
13#include <err.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <sys/mman.h>
18#include <sys/stat.h>
19#include <unistd.h>
20
21#define MAX_TEMPLATE_LEN 10
22#define MAX_TRIES 100
23
24#define SUFFIX ".suff"
25#define SLEN (sizeof SUFFIX - 1)
26
27long pg;
28
29/*
30 * verify that a path generated by mkstemp() or mkstemp() looks like a
31 * reasonable expansion of the template and matches the fd. Returns true
32 * if all the X's were replaced with non-X's
33 */
34int
35check(int fd, char const *path, char const *prefix, size_t plen,
36 char const *suffix, size_t slen, int tlen)
37{
38 struct stat sb, fsb;
39 char const *p;
40
41 if (fd < 0)
42 err(1, "mkstemp");
43 if (stat(path, &sb))
44 err(1, "stat(%s)", path);
45 if (fstat(fd, &fsb))
46 err(1, "fstat(%d==%s)", fd, path);
47 if (sb.st_dev != fsb.st_dev || sb.st_ino != fsb.st_ino)
48 errx(1, "stat mismatch");
49 close(fd);
50 if (memcmp(path, prefix, plen) != 0)
51 errx(1, "prefix changed! %s vs %s", prefix, path);
52 if (memcmp(path + plen + tlen, suffix, slen + 1) != 0)
53 errx(1, "suffix changed! %s vs %s", suffix, path);
54 for (p = path + plen; p < path + plen + tlen; p++)
55 if (*p == '\0')
56 errx(1, "unexpected truncation");
57 else if (*p == 'X')
58 return 0;
59 return 1;
60}
61
62
63void
64try_mkstemp(char *p, char const *prefix, int len)
65{
66 char *q;
67 size_t plen = strlen(prefix);
68 int tries, fd;
69
70 for (tries = 0; tries < MAX_TRIES; tries++) {
71 memcpy(p, prefix, plen);
72 memset(p + plen, 'X', len);
73 p[plen + len] = '\0';
74 fd = mkstemp(p);
75 if (check(fd, p, prefix, plen, "", 0, len))
76 return;
77 }
78 errx(1, "exceeded MAX_TRIES");
79}
80
81void
82try_mkstemps(char *p, char const *prefix, int len, char const *suffix)
83{
84 char *q;
85 size_t plen = strlen(prefix);
86 size_t slen = strlen(suffix);
87 int tries, fd;
88
89 for (tries = 0; tries < MAX_TRIES; tries++) {
90 memcpy(p, prefix, plen);
91 memset(p + plen, 'X', len);
92 memcpy(p + plen + len, suffix, slen + 1);
93 fd = mkstemps(p, slen);
94 if (check(fd, p, prefix, plen, suffix, slen, len))
95 return;
96 }
97 errx(1, "exceeded MAX_TRIES");
98}
99
100int
101main(void)
102{
103 struct stat sb, fsb;
104 char cwd[MAXPATHLEN + 1];
105 char *p;
106 size_t clen;
107 int i;
108
109 pg = sysconf(_SC_PAGESIZE);
110 if (getcwd(cwd, sizeof cwd - 1) == NULL)
111 err(1, "getcwd");
112 clen = strlen(cwd);
113 cwd[clen++] = '/';
114 cwd[clen] = '\0';
115 p = mmap(NULL, pg * 3, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
116 if (p == NULL)
117 err(1, "mmap");
118 if (mprotect(p, pg, PROT_NONE) || mprotect(p + pg * 2, pg, PROT_NONE))
119 err(1, "mprotect");
120 p += pg;
121
122 i = MAX_TEMPLATE_LEN + 1;
123 while (i-- > 1) {
124 /* try first at the start of a page, no prefix */
125 try_mkstemp(p, "", i);
126 /* now at the end of the page, no prefix */
127 try_mkstemp(p + pg - i - 1, "", i);
128 /* start of the page, prefixed with the cwd */
129 try_mkstemp(p, cwd, i);
130 /* how about at the end of the page, prefixed with cwd? */
131 try_mkstemp(p + pg - clen - i - 1, cwd, i);
132
133 /* again, with mkstemps() and an empty suffix */
134 /* try first at the start of a page, no prefix */
135 try_mkstemps(p, "", i, "");
136 /* now at the end of the page, no prefix */
137 try_mkstemps(p + pg - i - 1, "", i, "");
138 /* start of the page, prefixed with the cwd */
139 try_mkstemps(p, cwd, i, "");
140 /* how about at the end of the page, prefixed with cwd? */
141 try_mkstemps(p + pg - clen - i - 1, cwd, i, "");
142
143 /* mkstemps() and a non-empty suffix */
144 /* try first at the start of a page, no prefix */
145 try_mkstemps(p, "", i, SUFFIX);
146 /* now at the end of the page, no prefix */
147 try_mkstemps(p + pg - i - SLEN - 1, "", i, SUFFIX);
148 /* start of the page, prefixed with the cwd */
149 try_mkstemps(p, cwd, i, SUFFIX);
150 /* how about at the end of the page, prefixed with cwd? */
151 try_mkstemps(p + pg - clen - i - SLEN - 1, cwd, i, SUFFIX);
152
153 }
154
155 return 0;
156}