1 /* $NetBSD: t_stat.c,v 1.5 2017/01/13 20:06:50 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by 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_stat.c,v 1.5 2017/01/13 20:06:50 christos Exp $");
33 
34 #include <sys/stat.h>
35 #include <sys/socket.h>
36 #include <sys/types.h>
37 
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
40 
41 #include <atf-c.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <fts.h>
45 #include <limits.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #include <stdio.h>
50 
51 static const char *path = "stat";
52 
53 ATF_TC_WITH_CLEANUP(stat_chflags);
54 ATF_TC_HEAD(stat_chflags, tc)
55 {
56 	atf_tc_set_md_var(tc, "descr", "Test chflags(2) with stat(2)");
57 }
58 
59 ATF_TC_BODY(stat_chflags, tc)
60 {
61 	struct stat sa, sb;
62 	int fd;
63 
64 	(void)memset(&sa, 0, sizeof(struct stat));
65 	(void)memset(&sb, 0, sizeof(struct stat));
66 
67 	fd = open(path, O_RDONLY | O_CREAT, 0600);
68 
69 	ATF_REQUIRE(fd != -1);
70 	ATF_REQUIRE(stat(path, &sa) == 0);
71 	ATF_REQUIRE(chflags(path, UF_NODUMP) == 0);
72 	ATF_REQUIRE(stat(path, &sb) == 0);
73 
74 	if (sa.st_flags == sb.st_flags)
75 		atf_tc_fail("stat(2) did not detect chflags(2)");
76 
77 	ATF_REQUIRE(close(fd) == 0);
78 	ATF_REQUIRE(unlink(path) == 0);
79 }
80 
81 ATF_TC_CLEANUP(stat_chflags, tc)
82 {
83 	(void)unlink(path);
84 }
85 
86 ATF_TC(stat_dir);
87 ATF_TC_HEAD(stat_dir, tc)
88 {
89 	atf_tc_set_md_var(tc, "descr", "Test stat(2) with directories");
90 }
91 
92 ATF_TC_BODY(stat_dir, tc)
93 {
94 	const short depth = 2;
95 	struct stat sa, sb;
96 	char *argv[2];
97 	FTSENT *ftse;
98 	FTS *fts;
99 	int ops;
100 
101 	argv[1] = NULL;
102 	argv[0] = __UNCONST("/");
103 
104 	ops = FTS_NOCHDIR;
105 	ops |= FTS_PHYSICAL;
106 
107 	fts = fts_open(argv, ops, NULL);
108 	ATF_REQUIRE(fts != NULL);
109 
110 	while ((ftse = fts_read(fts)) != NULL) {
111 
112 		if (ftse->fts_level < 1)
113 			continue;
114 
115 		if (ftse->fts_level > depth) {
116 			(void)fts_set(fts, ftse, FTS_SKIP);
117 			continue;
118 		}
119 
120 		switch(ftse->fts_info) {
121 
122 		case FTS_DP:
123 
124 			(void)memset(&sa, 0, sizeof(struct stat));
125 			(void)memset(&sb, 0, sizeof(struct stat));
126 
127 			ATF_REQUIRE(stat(ftse->fts_parent->fts_path,&sa) == 0);
128 			ATF_REQUIRE(chdir(ftse->fts_path) == 0);
129 			ATF_REQUIRE(stat(".", &sb) == 0);
130 
131 			/*
132 			 * The previous two stat(2) calls
133 			 * should be for the same directory.
134 			 */
135 			if (sa.st_dev != sb.st_dev || sa.st_ino != sb.st_ino)
136 				atf_tc_fail("inconsistent stat(2)");
137 
138 			/*
139 			 * Check that fts(3)'s stat(2)
140 			 * call equals the manual one.
141 			 */
142 			if (sb.st_ino != ftse->fts_statp->st_ino)
143 				atf_tc_fail("stat(2) and fts(3) differ");
144 
145 			break;
146 
147 		default:
148 			break;
149 		}
150 	}
151 
152 	(void)fts_close(fts);
153 }
154 
155 ATF_TC(stat_err);
156 ATF_TC_HEAD(stat_err, tc)
157 {
158 	atf_tc_set_md_var(tc, "descr", "Test errors from the stat(2) family");
159 }
160 
161 ATF_TC_BODY(stat_err, tc)
162 {
163 	char buf[NAME_MAX + 1];
164 	struct stat st;
165 
166 	(void)memset(buf, 'x', sizeof(buf));
167 
168 	errno = 0;
169 	ATF_REQUIRE_ERRNO(EBADF, fstat(-1, &st) == -1);
170 
171 	errno = 0;
172 	ATF_REQUIRE_ERRNO(ENAMETOOLONG, stat(buf, &st) == -1);
173 
174 	errno = 0;
175 	ATF_REQUIRE_ERRNO(ENAMETOOLONG, lstat(buf, &st) == -1);
176 
177 	errno = 0;
178 	ATF_REQUIRE_ERRNO(EFAULT, stat((void *)-1, &st) == -1);
179 
180 	errno = 0;
181 	ATF_REQUIRE_ERRNO(EFAULT, lstat((void *)-1, &st) == -1);
182 
183 	errno = 0;
184 	ATF_REQUIRE_ERRNO(EFAULT, stat("/etc/passwd", (void *)-1) == -1);
185 
186 	errno = 0;
187 	ATF_REQUIRE_ERRNO(EFAULT, lstat("/etc/passwd", (void *)-1) == -1);
188 
189 	errno = 0;
190 	ATF_REQUIRE_ERRNO(ENOENT, stat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1);
191 
192 	errno = 0;
193 	ATF_REQUIRE_ERRNO(ENOENT, lstat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1);
194 }
195 
196 ATF_TC_WITH_CLEANUP(stat_mtime);
197 ATF_TC_HEAD(stat_mtime, tc)
198 {
199 	atf_tc_set_md_var(tc, "descr", "Test modification times with stat(2)");
200 }
201 
202 ATF_TC_BODY(stat_mtime, tc)
203 {
204 	struct stat sa, sb;
205 	int fd[3];
206 	size_t i;
207 
208 	for (i = 0; i < __arraycount(fd); i++) {
209 
210 		(void)memset(&sa, 0, sizeof(struct stat));
211 		(void)memset(&sb, 0, sizeof(struct stat));
212 
213 		fd[i] = open(path, O_WRONLY | O_CREAT, 0600);
214 
215 		ATF_REQUIRE(fd[i] != -1);
216 		ATF_REQUIRE(write(fd[i], "X", 1) == 1);
217 		ATF_REQUIRE(stat(path, &sa) == 0);
218 
219 		(void)sleep(1);
220 
221 		ATF_REQUIRE(write(fd[i], "X", 1) == 1);
222 		ATF_REQUIRE(stat(path, &sb) == 0);
223 
224 		ATF_REQUIRE(close(fd[i]) == 0);
225 		ATF_REQUIRE(unlink(path) == 0);
226 
227 		if (sa.st_mtime == sb.st_mtime)
228 			atf_tc_fail("mtimes did not change");
229 	}
230 }
231 
232 ATF_TC_CLEANUP(stat_mtime, tc)
233 {
234 	(void)unlink(path);
235 }
236 
237 ATF_TC_WITH_CLEANUP(stat_perm);
238 ATF_TC_HEAD(stat_perm, tc)
239 {
240 	atf_tc_set_md_var(tc, "descr", "Test permissions with stat(2)");
241 	atf_tc_set_md_var(tc, "require.user", "root");
242 }
243 
244 ATF_TC_BODY(stat_perm, tc)
245 {
246 	struct stat sa, sb;
247 	gid_t gid;
248 	uid_t uid;
249 	int fd;
250 
251 	(void)memset(&sa, 0, sizeof(struct stat));
252 	(void)memset(&sb, 0, sizeof(struct stat));
253 
254 	uid = getuid();
255 	gid = getgid();
256 
257 	fd = open(path, O_RDONLY | O_CREAT, 0600);
258 
259 	ATF_REQUIRE(fd != -1);
260 	ATF_REQUIRE(fstat(fd, &sa) == 0);
261 	ATF_REQUIRE(stat(path, &sb) == 0);
262 
263 	if (gid != sa.st_gid || sa.st_gid != sb.st_gid)
264 		atf_tc_fail("invalid GID");
265 
266 	if (uid != sa.st_uid || sa.st_uid != sb.st_uid)
267 		atf_tc_fail("invalid UID");
268 
269 	ATF_REQUIRE(close(fd) == 0);
270 	ATF_REQUIRE(unlink(path) == 0);
271 }
272 
273 ATF_TC_CLEANUP(stat_perm, tc)
274 {
275 	(void)unlink(path);
276 }
277 
278 ATF_TC_WITH_CLEANUP(stat_size);
279 ATF_TC_HEAD(stat_size, tc)
280 {
281 	atf_tc_set_md_var(tc, "descr", "Test file sizes with stat(2)");
282 }
283 
284 ATF_TC_BODY(stat_size, tc)
285 {
286 	struct stat sa, sb, sc;
287 	const size_t n = 10;
288 	size_t i;
289 	int fd;
290 
291 	fd = open(path, O_WRONLY | O_CREAT, 0600);
292 	ATF_REQUIRE(fd >= 0);
293 
294 	for (i = 0; i < n; i++) {
295 
296 		(void)memset(&sa, 0, sizeof(struct stat));
297 		(void)memset(&sb, 0, sizeof(struct stat));
298 		(void)memset(&sc, 0, sizeof(struct stat));
299 
300 		ATF_REQUIRE(fstat(fd, &sa) == 0);
301 		ATF_REQUIRE(write(fd, "X", 1) == 1);
302 		ATF_REQUIRE(fstat(fd, &sb) == 0);
303 		ATF_REQUIRE(stat(path, &sc) == 0);
304 
305 		if (sa.st_size + 1 != sb.st_size)
306 			atf_tc_fail("invalid file size");
307 
308 		if (sb.st_size != sc.st_size)
309 			atf_tc_fail("stat(2) and fstat(2) mismatch");
310 	}
311 
312 	ATF_REQUIRE(close(fd) == 0);
313 	ATF_REQUIRE(unlink(path) == 0);
314 }
315 
316 ATF_TC_CLEANUP(stat_size, tc)
317 {
318 	(void)unlink(path);
319 }
320 
321 ATF_TC(stat_socket);
322 ATF_TC_HEAD(stat_socket, tc)
323 {
324 	atf_tc_set_md_var(tc, "descr", "Test fstat(2) with "
325 	    "a socket (PR kern/46077)");
326 }
327 
328 ATF_TC_BODY(stat_socket, tc)
329 {
330 	struct sockaddr_in addr;
331 	struct stat st;
332 	uint32_t iaddr;
333 	int fd, flags;
334 
335 	(void)memset(&st, 0, sizeof(struct stat));
336 	(void)memset(&addr, 0, sizeof(struct sockaddr_in));
337 
338 	fd = socket(AF_INET, SOCK_STREAM, 0);
339 	ATF_REQUIRE(fd >= 0);
340 
341 	flags = fcntl(fd, F_GETFL);
342 
343 	ATF_REQUIRE(flags != -1);
344 	ATF_REQUIRE(fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1);
345 	ATF_REQUIRE(inet_pton(AF_INET, "127.0.0.1", &iaddr) == 1);
346 
347 	addr.sin_port = htons(42);
348 	addr.sin_family = AF_INET;
349 	addr.sin_addr.s_addr = iaddr;
350 
351 	errno = 0;
352 
353 	ATF_REQUIRE_ERRNO(EINPROGRESS,
354 	    connect(fd, (struct sockaddr *)&addr,
355 		sizeof(struct sockaddr_in)) == -1);
356 
357 	errno = 0;
358 
359 	if (fstat(fd, &st) != 0 || errno != 0)
360 		atf_tc_fail("fstat(2) failed for a EINPROGRESS socket");
361 
362 	(void)close(fd);
363 }
364 
365 ATF_TC_WITH_CLEANUP(stat_symlink);
366 ATF_TC_HEAD(stat_symlink, tc)
367 {
368 	atf_tc_set_md_var(tc, "descr", "Test symbolic links with stat(2)");
369 }
370 
371 ATF_TC_BODY(stat_symlink, tc)
372 {
373 	const char *pathlink = "pathlink";
374 	struct stat sa, sb;
375 	int fd;
376 
377 	(void)memset(&sa, 0, sizeof(struct stat));
378 	(void)memset(&sb, 0, sizeof(struct stat));
379 
380 	fd = open(path, O_WRONLY | O_CREAT, 0600);
381 
382 	ATF_REQUIRE(fd >= 0);
383 	ATF_REQUIRE(symlink(path, pathlink) == 0);
384 	ATF_REQUIRE(stat(pathlink, &sa) == 0);
385 	ATF_REQUIRE(lstat(pathlink, &sb) == 0);
386 
387 	if (S_ISLNK(sa.st_mode) != 0)
388 		atf_tc_fail("stat(2) detected symbolic link");
389 
390 	if (S_ISLNK(sb.st_mode) == 0)
391 		atf_tc_fail("lstat(2) did not detect symbolic link");
392 
393 	if (sa.st_mode == sb.st_mode)
394 		atf_tc_fail("inconsistencies between stat(2) and lstat(2)");
395 
396 	(void)close(fd);
397 	ATF_REQUIRE(unlink(path) == 0);
398 	ATF_REQUIRE(unlink(pathlink) == 0);
399 }
400 
401 ATF_TC_CLEANUP(stat_symlink, tc)
402 {
403 	(void)unlink(path);
404 }
405 
406 ATF_TP_ADD_TCS(tp)
407 {
408 
409 	ATF_TP_ADD_TC(tp, stat_chflags);
410 	ATF_TP_ADD_TC(tp, stat_dir);
411 	ATF_TP_ADD_TC(tp, stat_err);
412 	ATF_TP_ADD_TC(tp, stat_mtime);
413 	ATF_TP_ADD_TC(tp, stat_perm);
414 	ATF_TP_ADD_TC(tp, stat_size);
415 	ATF_TP_ADD_TC(tp, stat_socket);
416 	ATF_TP_ADD_TC(tp, stat_symlink);
417 
418 	return atf_no_error();
419 }
420