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