xref: /netbsd-src/tests/lib/libc/stdlib/t_mktemp.c (revision e5d758f832e07a177fa24707c434b7ce26d0f762)
1 /* $NetBSD: t_mktemp.c,v 1.3 2020/11/01 18:19:54 gson Exp $ */
2 
3 /*-
4  * Copyright (c) 2013, 2020 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas and Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_mktemp.c,v 1.3 2020/11/01 18:19:54 gson Exp $");
33 
34 #include <sys/stat.h>
35 
36 #include <atf-c.h>
37 #include <fcntl.h>
38 #include <limits.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 
43 static int
44 check_mode(struct stat sa, const int mode, const int dir)
45 {
46 
47 	if (dir == 0 && S_ISREG(sa.st_mode) == 0)
48 		return -1;
49 
50 	if (dir != 0 && S_ISDIR(sa.st_mode) == 0)
51 		return -1;
52 
53 	if ((sa.st_mode & mode) == 0)
54 		return -1;
55 
56 	return 0;
57 }
58 
59 ATF_TC(mktemp_not_exist);
60 ATF_TC_HEAD(mktemp_not_exist, tc)
61 {
62 	atf_tc_set_md_var(tc, "descr", "Test that mktemp(3)"
63 	    " does not fail on a path that does not exist");
64 }
65 
66 ATF_TC_BODY(mktemp_not_exist, tc)
67 {
68 	char template[] = "I will barf/XXXXXX";
69 	ATF_REQUIRE(mktemp(template) != NULL);
70 }
71 
72 ATF_TC(mktemp_large_template);
73 ATF_TC_HEAD(mktemp_large_template, tc)
74 {
75 	atf_tc_set_md_var(tc, "descr", "Test that mktemp "
76 	    "accepts arbitrarily large templates (PR lib/55441)");
77 }
78 
79 ATF_TC_BODY(mktemp_large_template, tc)
80 {
81 	const char *path = "/tmp/mktemp.XXXXXX";
82 	char template[PATH_MAX] = { 0 };
83 	size_t i;
84 
85 	atf_tc_expect_fail("PR lib/55441");
86 
87 	ATF_REQUIRE(strcpy(template, path) != NULL);
88 	ATF_REQUIRE(mktemp(template) != NULL);
89 	ATF_REQUIRE(strlen(template) == strlen(path));
90 	ATF_REQUIRE(strcmp(template, path) != 0);
91 	ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
92 
93 	(void)memset(template, '\0', sizeof(template));
94 	(void)strcpy(template, "/tmp/mktemp.");
95 
96 	for (i = 12; i < sizeof(template) - 1; i++)
97 		template[i] = 'X';
98 
99 	ATF_REQUIRE(mktemp(template) != NULL);
100 	ATF_REQUIRE(strlen(template) == sizeof(template) - 1);
101 	ATF_REQUIRE(strcmp(template, path) != 0);
102 	ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
103 }
104 
105 ATF_TC(mkstemp_basic);
106 ATF_TC_HEAD(mkstemp_basic, tc)
107 {
108 	atf_tc_set_md_var(tc, "descr", "A basic test of mkstemp(3)");
109 }
110 
111 ATF_TC_BODY(mkstemp_basic, tc)
112 {
113 	char template[] = "/tmp/mktemp.XXXXXX";
114 	struct stat sa;
115 	int fd;
116 
117 	(void)memset(&sa, 0, sizeof(struct stat));
118 
119 	fd = mkstemp(template);
120 
121 	ATF_REQUIRE(fd != -1);
122 	ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
123 	ATF_REQUIRE(write(fd, "X", 1) == 1);
124 	ATF_REQUIRE(fstat(fd, &sa) == 0);
125 	ATF_REQUIRE(check_mode(sa, 0600, 0) == 0);
126 	ATF_REQUIRE(close(fd) == 0);
127 	ATF_REQUIRE(unlink(template) == 0);
128 }
129 
130 ATF_TC(mkstemps_basic);
131 ATF_TC_HEAD(mkstemps_basic, tc)
132 {
133 	atf_tc_set_md_var(tc, "descr", "A basic test of mkstemps(3)");
134 }
135 
136 ATF_TC_BODY(mkstemps_basic, tc)
137 {
138 	char template[] = "/tmp/mktemp.XXXyyy";
139 	char *suffix = strchr(template, 'y');
140 	struct stat sa;
141 	int fd;
142 
143 	(void)memset(&sa, 0, sizeof(struct stat));
144 
145 	fd = mkstemps(template, 3);
146 
147 	ATF_REQUIRE(fd != -1);
148 	ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
149 	ATF_REQUIRE(strcmp(suffix, "yyy") == 0);
150 	ATF_REQUIRE(write(fd, "X", 1) == 1);
151 	ATF_REQUIRE(fstat(fd, &sa) == 0);
152 	ATF_REQUIRE(check_mode(sa, 0600, 0) == 0);
153 	ATF_REQUIRE(close(fd) == 0);
154 	ATF_REQUIRE(unlink(template) == 0);
155 }
156 
157 ATF_TC(mkdtemp_basic);
158 ATF_TC_HEAD(mkdtemp_basic, tc)
159 {
160 	atf_tc_set_md_var(tc, "descr", "A basic test of mkdtemp(3)");
161 }
162 
163 ATF_TC_BODY(mkdtemp_basic, tc)
164 {
165 	char template[] = "/tmp/mktemp.XXXXXX";
166 	char *path = mkdtemp(template);
167 	struct stat sa;
168 
169 	(void)memset(&sa, 0, sizeof(struct stat));
170 
171 	ATF_REQUIRE(path != NULL);
172 	ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
173 	ATF_REQUIRE(stat(path, &sa) == 0);
174 	ATF_REQUIRE(check_mode(sa, 0700, 1) == 0);
175 	ATF_REQUIRE(rmdir(path) == 0);
176 }
177 
178 ATF_TC(mkostemp_basic);
179 ATF_TC_HEAD(mkostemp_basic, tc)
180 {
181 	atf_tc_set_md_var(tc, "descr", "A basic test of mkostemp(3)");
182 }
183 
184 ATF_TC_BODY(mkostemp_basic, tc)
185 {
186 	static const int flags[] = {
187 		O_APPEND, O_DIRECT,
188 		O_SHLOCK, O_EXLOCK,
189 		O_SYNC,   O_CLOEXEC
190 	};
191 
192 	char template[] = "/tmp/mktemp.XXXXXX";
193 	struct stat sa;
194 	size_t i;
195 	int fd;
196 
197 	for (i = 0; i < __arraycount(flags); i++) {
198 
199 		(void)memset(&sa, 0, sizeof(struct stat));
200 
201 		fd = mkostemp(template, flags[i]);
202 
203 		ATF_REQUIRE(fd != -1);
204 		ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
205 		ATF_REQUIRE(write(fd, "X", 1) == 1);
206 		ATF_REQUIRE(fstat(fd, &sa) == 0);
207 		ATF_REQUIRE(check_mode(sa, 0600 | flags[i], 0) == 0);
208 		ATF_REQUIRE(close(fd) == 0);
209 		ATF_REQUIRE(unlink(template) == 0);
210 	}
211 }
212 
213 ATF_TC(mkostemps_basic);
214 ATF_TC_HEAD(mkostemps_basic, tc)
215 {
216 	atf_tc_set_md_var(tc, "descr", "A basic test of mkostemps(3)");
217 }
218 
219 ATF_TC_BODY(mkostemps_basic, tc)
220 {
221 	static const int flags[] = {
222 		O_APPEND, O_DIRECT,
223 		O_SHLOCK, O_EXLOCK,
224 		O_SYNC,   O_CLOEXEC
225 	};
226 
227 	char template[] = "/tmp/mktemp.XXXyyy";
228 	char *suffix = strchr(template, 'y');
229 	struct stat sa;
230 	size_t i;
231 	int fd;
232 
233 	for (i = 0; i < __arraycount(flags); i++) {
234 
235 		(void)memset(&sa, 0, sizeof(struct stat));
236 
237 		fd = mkostemps(template, 3, flags[i]);
238 
239 		ATF_REQUIRE(fd != -1);
240 		ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0);
241 		ATF_REQUIRE(strcmp(suffix, "yyy") == 0);
242 		ATF_REQUIRE(write(fd, "X", 1) == 1);
243 		ATF_REQUIRE(fstat(fd, &sa) == 0);
244 		ATF_REQUIRE(check_mode(sa, 0600 | flags[i], 0) == 0);
245 		ATF_REQUIRE(close(fd) == 0);
246 		ATF_REQUIRE(unlink(template) == 0);
247 	}
248 }
249 
250 ATF_TP_ADD_TCS(tp)
251 {
252 
253 	ATF_TP_ADD_TC(tp, mktemp_not_exist);
254 	ATF_TP_ADD_TC(tp, mktemp_large_template);
255 	ATF_TP_ADD_TC(tp, mkstemp_basic);
256 	ATF_TP_ADD_TC(tp, mkstemps_basic);
257 	ATF_TP_ADD_TC(tp, mkdtemp_basic);
258 	ATF_TP_ADD_TC(tp, mkostemp_basic);
259 	ATF_TP_ADD_TC(tp, mkostemps_basic);
260 
261 	return atf_no_error();
262 }
263