2 * Copyright (c) 2013 Jilles Tjoelker
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Test program for mkostemp().
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
44 static const char template[] = _PATH_TMP "mkostemp.XXXXXXXX";
47 #define MISCFLAGS (O_APPEND | O_DIRECT | O_SHLOCK | O_EXLOCK | O_SYNC)
52 char tmpf[sizeof(template)];
56 memcpy(tmpf, template, sizeof(tmpf));
57 fd = mkostemp(tmpf, oflags);
59 printf("not ok %d - oflags=%#x "
60 "mkostemp() reported failure: %s\n",
61 testnum++, oflags, strerror(errno));
64 if (memcmp(tmpf, template, sizeof(tmpf) - 8 - 1) != 0) {
65 printf("not ok %d - oflags=%#x "
66 "returned pathname does not match template: %s\n",
67 testnum++, oflags, tmpf);
71 if (fcntl(fd, F_GETFD) !=
72 (oflags & O_CLOEXEC ? FD_CLOEXEC : 0)) {
73 printf("not ok %d - oflags=%#x "
74 "close-on-exec flag incorrect\n",
78 if ((fcntl(fd, F_GETFL) & MISCFLAGS) != (oflags & MISCFLAGS)) {
79 printf("not ok %d - oflags=%#x "
80 "open flags incorrect\n",
84 if (stat(tmpf, &st1) == -1) {
85 printf("not ok %d - oflags=%#x "
86 "cannot stat returned pathname %s: %s\n",
87 testnum++, oflags, tmpf, strerror(errno));
90 if (fstat(fd, &st2) == -1) {
91 printf("not ok %d - oflags=%#x "
92 "cannot fstat returned fd %d: %s\n",
93 testnum++, oflags, fd, strerror(errno));
96 if (!S_ISREG(st1.st_mode) || (st1.st_mode & 0777) != 0600 ||
97 st1.st_nlink != 1 || st1.st_size != 0) {
98 printf("not ok %d - oflags=%#x "
99 "named file attributes incorrect\n",
103 if (!S_ISREG(st2.st_mode) || (st2.st_mode & 0777) != 0600 ||
104 st2.st_nlink != 1 || st2.st_size != 0) {
105 printf("not ok %d - oflags=%#x "
106 "opened file attributes incorrect\n",
110 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) {
111 printf("not ok %d - oflags=%#x "
112 "named and opened file do not match\n",
117 if (fstat(fd, &st2) == -1)
118 printf("not ok %d - oflags=%#x "
119 "cannot fstat returned fd %d again: %s\n",
120 testnum++, oflags, fd, strerror(errno));
121 else if (st2.st_nlink != 0)
122 printf("not ok %d - oflags=%#x "
123 "st_nlink is not 0 after unlink\n",
126 printf("ok %d - oflags=%#x\n", testnum++, oflags);
137 char tmpf[sizeof(template)];
139 memcpy(tmpf, template, sizeof(tmpf));
140 if (mkostemp(tmpf, O_CREAT) == -1)
141 printf("ok %d - mkostemp(O_CREAT) correctly failed\n",
144 printf("not ok %d - mkostemp(O_CREAT) wrongly succeeded\n",
149 main(int argc, char *argv[])
160 test_one(O_APPEND | O_CLOEXEC);