xref: /freebsd/lib/libc/tests/stdio/mkostemp_test.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * Copyright (c) 2013 Jilles Tjoelker
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Test program for mkostemp().
29  */
30 
31 #include <sys/stat.h>
32 
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <paths.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include <atf-c.h>
42 
43 static const char template[] = "mkostemp.XXXXXXXX";
44 static int testnum;
45 
46 #define MISCFLAGS (O_APPEND | O_DIRECT | O_SHLOCK | O_EXLOCK | O_SYNC)
47 
48 static void
49 test_one(int oflags)
50 {
51 	char tmpf[sizeof(template)];
52 	struct stat st1, st2;
53 	int fd;
54 
55 	memcpy(tmpf, template, sizeof(tmpf));
56 	fd = mkostemp(tmpf, oflags);
57 	if (fd < 0) {
58 		printf("not ok %d - oflags=%#x "
59 		    "mkostemp() reported failure: %s\n",
60 		    testnum++, oflags, strerror(errno));
61 		return;
62 	}
63 	if (memcmp(tmpf, template, sizeof(tmpf) - 8 - 1) != 0) {
64 		printf("not ok %d - oflags=%#x "
65 		    "returned pathname does not match template: %s\n",
66 		    testnum++, oflags, tmpf);
67 		return;
68 	}
69 	do {
70 		if (fcntl(fd, F_GETFD) !=
71 		    (oflags & O_CLOEXEC ? FD_CLOEXEC : 0)) {
72 			printf("not ok %d - oflags=%#x "
73 			    "close-on-exec flag incorrect\n",
74 			    testnum++, oflags);
75 			break;
76 		}
77 		if ((fcntl(fd, F_GETFL) & MISCFLAGS) != (oflags & MISCFLAGS)) {
78 			printf("not ok %d - oflags=%#x "
79 			    "open flags incorrect\n",
80 			    testnum++, oflags);
81 			break;
82 		}
83 		if (stat(tmpf, &st1) == -1) {
84 			printf("not ok %d - oflags=%#x "
85 			    "cannot stat returned pathname %s: %s\n",
86 			    testnum++, oflags, tmpf, strerror(errno));
87 			break;
88 		}
89 		if (fstat(fd, &st2) == -1) {
90 			printf("not ok %d - oflags=%#x "
91 			    "cannot fstat returned fd %d: %s\n",
92 			    testnum++, oflags, fd, strerror(errno));
93 			break;
94 		}
95 		if (!S_ISREG(st1.st_mode) || (st1.st_mode & 0777) != 0600 ||
96 		    st1.st_nlink != 1 || st1.st_size != 0) {
97 			printf("not ok %d - oflags=%#x "
98 			    "named file attributes incorrect\n",
99 			    testnum++, oflags);
100 			break;
101 		}
102 		if (!S_ISREG(st2.st_mode) || (st2.st_mode & 0777) != 0600 ||
103 		    st2.st_nlink != 1 || st2.st_size != 0) {
104 			printf("not ok %d - oflags=%#x "
105 			    "opened file attributes incorrect\n",
106 			    testnum++, oflags);
107 			break;
108 		}
109 		if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) {
110 			printf("not ok %d - oflags=%#x "
111 			    "named and opened file do not match\n",
112 			    testnum++, oflags);
113 			break;
114 		}
115 		(void)unlink(tmpf);
116 		if (fstat(fd, &st2) == -1)
117 			printf("not ok %d - oflags=%#x "
118 			    "cannot fstat returned fd %d again: %s\n",
119 			    testnum++, oflags, fd, strerror(errno));
120 		else if (st2.st_nlink != 0)
121 			printf("not ok %d - oflags=%#x "
122 			    "st_nlink is not 0 after unlink\n",
123 			    testnum++, oflags);
124 		else
125 			printf("ok %d - oflags=%#x\n", testnum++, oflags);
126 		(void)close(fd);
127 		return;
128 	} while (0);
129 	(void)close(fd);
130 	(void)unlink(tmpf);
131 }
132 
133 ATF_TC_WITHOUT_HEAD(zero);
134 ATF_TC_BODY(zero, tc)
135 {
136 
137 	test_one(0);
138 }
139 
140 ATF_TC_WITHOUT_HEAD(O_CLOEXEC);
141 ATF_TC_BODY(O_CLOEXEC, tc)
142 {
143 
144 	test_one(O_CLOEXEC);
145 }
146 
147 ATF_TC_WITHOUT_HEAD(O_APPEND);
148 ATF_TC_BODY(O_APPEND, tc)
149 {
150 
151 	test_one(O_APPEND);
152 }
153 
154 ATF_TC_WITHOUT_HEAD(O_APPEND__O_CLOEXEC);
155 ATF_TC_BODY(O_APPEND__O_CLOEXEC, tc)
156 {
157 
158 	test_one(O_APPEND|O_CLOEXEC);
159 }
160 
161 ATF_TC_WITHOUT_HEAD(bad_flags);
162 ATF_TC_BODY(bad_flags, tc)
163 {
164 
165 	char tmpf[sizeof(template)];
166 
167 	memcpy(tmpf, template, sizeof(tmpf));
168 	ATF_REQUIRE_MSG(mkostemp(tmpf, O_CREAT) == -1,
169 		"mkostemp(O_CREAT) succeeded unexpectedly");
170 }
171 
172 ATF_TP_ADD_TCS(tp)
173 {
174 
175 	ATF_TP_ADD_TC(tp, zero);
176 	ATF_TP_ADD_TC(tp, O_CLOEXEC);
177 	ATF_TP_ADD_TC(tp, O_APPEND);
178 	ATF_TP_ADD_TC(tp, O_APPEND__O_CLOEXEC);
179 	ATF_TP_ADD_TC(tp, bad_flags);
180 
181 	return (atf_no_error());
182 }
183