summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorguenther <>2010-02-11 16:39:05 +0000
committerguenther <>2010-02-11 16:39:05 +0000
commite314bdbb854870b396eaaee165b72a3e280b18bb (patch)
tree2796affb5c1ad2835f9516412c1ca57da631a11d /src
parent53dc25964d1d5f5dffbf61f764b63a30be5f8947 (diff)
downloadopenbsd-e314bdbb854870b396eaaee165b72a3e280b18bb.tar.gz
openbsd-e314bdbb854870b396eaaee165b72a3e280b18bb.tar.bz2
openbsd-e314bdbb854870b396eaaee165b72a3e280b18bb.zip
Test mkstemps() too
Diffstat (limited to 'src')
-rw-r--r--src/regress/lib/libc/mkstemp/mkstemp_test.c113
1 files changed, 87 insertions, 26 deletions
diff --git a/src/regress/lib/libc/mkstemp/mkstemp_test.c b/src/regress/lib/libc/mkstemp/mkstemp_test.c
index d8311cbf35..fd133d8368 100644
--- a/src/regress/lib/libc/mkstemp/mkstemp_test.c
+++ b/src/regress/lib/libc/mkstemp/mkstemp_test.c
@@ -3,8 +3,9 @@
3 * 3 *
4 * Public domain. 4 * Public domain.
5 * 5 *
6 * Verify that mkstemp() doesn't overrun or underrun the template buffer 6 * Verify that mkstemp() and mkstemps() doesn't overrun or underrun
7 * and that it can generate names that don't contain any X's 7 * the template buffer and that it can generate names that don't
8 * contain any X's
8 */ 9 */
9 10
10#include <sys/param.h> 11#include <sys/param.h>
@@ -20,12 +21,48 @@
20#define MAX_TEMPLATE_LEN 10 21#define MAX_TEMPLATE_LEN 10
21#define MAX_TRIES 100 22#define MAX_TRIES 100
22 23
24#define SUFFIX ".suff"
25#define SLEN (sizeof SUFFIX - 1)
26
23long pg; 27long pg;
24 28
25void 29/*
26try(char *p, const char *prefix, int len) 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)
27{ 37{
28 struct stat sb, fsb; 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{
29 char *q; 66 char *q;
30 size_t plen = strlen(prefix); 67 size_t plen = strlen(prefix);
31 int tries, fd; 68 int tries, fd;
@@ -35,24 +72,27 @@ try(char *p, const char *prefix, int len)
35 memset(p + plen, 'X', len); 72 memset(p + plen, 'X', len);
36 p[plen + len] = '\0'; 73 p[plen + len] = '\0';
37 fd = mkstemp(p); 74 fd = mkstemp(p);
38 if (fd < 0) 75 if (check(fd, p, prefix, plen, "", 0, len))
39 err(1, "mkstemp"); 76 return;
40 if (stat(p, &sb)) 77 }
41 err(1, "stat(%s)", p); 78 errx(1, "exceeded MAX_TRIES");
42 if (fstat(fd, &fsb)) 79}
43 err(1, "fstat(%d==%s)", fd, p); 80
44 if (sb.st_dev != fsb.st_dev || sb.st_ino != fsb.st_ino) 81void
45 errx(1, "stat mismatch"); 82try_mkstemps(char *p, char const *prefix, int len, char const *suffix)
46 close(fd); 83{
47 for (q = p + plen; *q != 'X'; q++) { 84 char *q;
48 if (*q == '\0') { 85 size_t plen = strlen(prefix);
49 if (q != p + plen + len) 86 size_t slen = strlen(suffix);
50 errx(1, "unexpected truncation"); 87 int tries, fd;
51 return; 88
52 } 89 for (tries = 0; tries < MAX_TRIES; tries++) {
53 } 90 memcpy(p, prefix, plen);
54 if (q >= p + plen + len) 91 memset(p + plen, 'X', len);
55 errx(1, "overrun?"); 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;
56 } 96 }
57 errx(1, "exceeded MAX_TRIES"); 97 errx(1, "exceeded MAX_TRIES");
58} 98}
@@ -82,13 +122,34 @@ main(void)
82 i = MAX_TEMPLATE_LEN + 1; 122 i = MAX_TEMPLATE_LEN + 1;
83 while (i-- > 1) { 123 while (i-- > 1) {
84 /* try first at the start of a page, no prefix */ 124 /* try first at the start of a page, no prefix */
85 try(p, "", i); 125 try_mkstemp(p, "", i);
86 /* now at the end of the page, no prefix */ 126 /* now at the end of the page, no prefix */
87 try(p + pg - i - 1, "", i); 127 try_mkstemp(p + pg - i - 1, "", i);
88 /* start of the page, prefixed with the cwd */ 128 /* start of the page, prefixed with the cwd */
89 try(p, cwd, i); 129 try_mkstemp(p, cwd, i);
90 /* how about at the end of the page, prefixed with cwd? */ 130 /* how about at the end of the page, prefixed with cwd? */
91 try(p + pg - i - 1 - clen, cwd, i); 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
92 } 153 }
93 154
94 return 0; 155 return 0;