Remove building with NOCRYPTO option
[minix.git] / tests / lib / libc / sys / t_stat.c
blob0312e2f2f6db9e2bffda5ec979392bcd4655b874
1 /* $NetBSD: t_stat.c,v 1.4 2012/03/17 08:37:08 jruoho Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_stat.c,v 1.4 2012/03/17 08:37:08 jruoho Exp $");
34 #include <sys/stat.h>
35 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <arpa/inet.h>
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>
48 #include <stdio.h>
50 static const char *path = "stat";
52 ATF_TC_WITH_CLEANUP(stat_chflags);
53 ATF_TC_HEAD(stat_chflags, tc)
55 atf_tc_set_md_var(tc, "descr", "Test chflags(2) with stat(2)");
58 ATF_TC_BODY(stat_chflags, tc)
60 struct stat sa, sb;
61 int fd;
63 (void)memset(&sa, 0, sizeof(struct stat));
64 (void)memset(&sb, 0, sizeof(struct stat));
66 fd = open(path, O_RDONLY | O_CREAT);
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);
73 if (sa.st_flags == sb.st_flags)
74 atf_tc_fail("stat(2) did not detect chflags(2)");
76 ATF_REQUIRE(close(fd) == 0);
77 ATF_REQUIRE(unlink(path) == 0);
80 ATF_TC_CLEANUP(stat_chflags, tc)
82 (void)unlink(path);
85 ATF_TC(stat_dir);
86 ATF_TC_HEAD(stat_dir, tc)
88 atf_tc_set_md_var(tc, "descr", "Test stat(2) with directories");
91 ATF_TC_BODY(stat_dir, tc)
93 const short depth = 2;
94 struct stat sa, sb;
95 char *argv[2];
96 FTSENT *ftse;
97 FTS *fts;
98 int ops;
100 argv[1] = NULL;
101 argv[0] = __UNCONST("/");
103 ops = FTS_NOCHDIR;
104 ops |= FTS_PHYSICAL;
106 fts = fts_open(argv, ops, NULL);
107 ATF_REQUIRE(fts != NULL);
109 while ((ftse = fts_read(fts)) != NULL) {
111 if (ftse->fts_level < 1)
112 continue;
114 if (ftse->fts_level > depth) {
115 (void)fts_set(fts, ftse, FTS_SKIP);
116 continue;
119 switch(ftse->fts_info) {
121 case FTS_DP:
123 (void)memset(&sa, 0, sizeof(struct stat));
124 (void)memset(&sb, 0, sizeof(struct stat));
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);
131 * The previous two stat(2) calls
132 * should be for the same directory.
134 if (sa.st_dev != sb.st_dev || sa.st_ino != sb.st_ino)
135 atf_tc_fail("inconsistent stat(2)");
138 * Check that fts(3)'s stat(2)
139 * call equals the manual one.
141 if (sb.st_ino != ftse->fts_statp->st_ino)
142 atf_tc_fail("stat(2) and fts(3) differ");
144 break;
146 default:
147 break;
151 (void)fts_close(fts);
154 ATF_TC(stat_err);
155 ATF_TC_HEAD(stat_err, tc)
157 atf_tc_set_md_var(tc, "descr", "Test errors from the stat(2) family");
160 ATF_TC_BODY(stat_err, tc)
162 char buf[NAME_MAX + 1];
163 struct stat st;
165 (void)memset(buf, 'x', sizeof(buf));
167 errno = 0;
168 ATF_REQUIRE_ERRNO(EBADF, fstat(-1, &st) == -1);
170 errno = 0;
171 ATF_REQUIRE_ERRNO(ENAMETOOLONG, stat(buf, &st) == -1);
173 errno = 0;
174 ATF_REQUIRE_ERRNO(ENAMETOOLONG, lstat(buf, &st) == -1);
176 errno = 0;
177 ATF_REQUIRE_ERRNO(EFAULT, stat((void *)-1, &st) == -1);
179 errno = 0;
180 ATF_REQUIRE_ERRNO(EFAULT, lstat((void *)-1, &st) == -1);
182 errno = 0;
183 ATF_REQUIRE_ERRNO(EFAULT, stat("/etc/passwd", (void *)-1) == -1);
185 errno = 0;
186 ATF_REQUIRE_ERRNO(EFAULT, lstat("/etc/passwd", (void *)-1) == -1);
188 errno = 0;
189 ATF_REQUIRE_ERRNO(ENOENT, stat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1);
191 errno = 0;
192 ATF_REQUIRE_ERRNO(ENOENT, lstat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1);
195 ATF_TC_WITH_CLEANUP(stat_mtime);
196 ATF_TC_HEAD(stat_mtime, tc)
198 atf_tc_set_md_var(tc, "descr", "Test modification times with stat(2)");
201 ATF_TC_BODY(stat_mtime, tc)
203 struct stat sa, sb;
204 int fd[3];
205 size_t i;
207 for (i = 0; i < __arraycount(fd); i++) {
209 (void)memset(&sa, 0, sizeof(struct stat));
210 (void)memset(&sb, 0, sizeof(struct stat));
212 fd[i] = open(path, O_WRONLY | O_CREAT);
214 ATF_REQUIRE(fd[i] != -1);
215 ATF_REQUIRE(write(fd[i], "X", 1) == 1);
216 ATF_REQUIRE(stat(path, &sa) == 0);
218 (void)sleep(1);
220 ATF_REQUIRE(write(fd[i], "X", 1) == 1);
221 ATF_REQUIRE(stat(path, &sb) == 0);
223 ATF_REQUIRE(close(fd[i]) == 0);
224 ATF_REQUIRE(unlink(path) == 0);
226 if (sa.st_mtime == sb.st_mtime)
227 atf_tc_fail("mtimes did not change");
231 ATF_TC_CLEANUP(stat_mtime, tc)
233 (void)unlink(path);
236 ATF_TC_WITH_CLEANUP(stat_perm);
237 ATF_TC_HEAD(stat_perm, tc)
239 atf_tc_set_md_var(tc, "descr", "Test permissions with stat(2)");
240 atf_tc_set_md_var(tc, "require.user", "root");
243 ATF_TC_BODY(stat_perm, tc)
245 struct stat sa, sb;
246 gid_t gid;
247 uid_t uid;
248 int fd;
250 (void)memset(&sa, 0, sizeof(struct stat));
251 (void)memset(&sb, 0, sizeof(struct stat));
253 uid = getuid();
254 gid = getgid();
256 fd = open(path, O_RDONLY | O_CREAT);
258 ATF_REQUIRE(fd != -1);
259 ATF_REQUIRE(fstat(fd, &sa) == 0);
260 ATF_REQUIRE(stat(path, &sb) == 0);
262 if (gid != sa.st_gid || sa.st_gid != sb.st_gid)
263 atf_tc_fail("invalid GID");
265 if (uid != sa.st_uid || sa.st_uid != sb.st_uid)
266 atf_tc_fail("invalid UID");
268 ATF_REQUIRE(close(fd) == 0);
269 ATF_REQUIRE(unlink(path) == 0);
272 ATF_TC_CLEANUP(stat_perm, tc)
274 (void)unlink(path);
277 ATF_TC_WITH_CLEANUP(stat_size);
278 ATF_TC_HEAD(stat_size, tc)
280 atf_tc_set_md_var(tc, "descr", "Test file sizes with stat(2)");
283 ATF_TC_BODY(stat_size, tc)
285 struct stat sa, sb, sc;
286 const size_t n = 10;
287 size_t i;
288 int fd;
290 fd = open(path, O_WRONLY | O_CREAT);
291 ATF_REQUIRE(fd >= 0);
293 for (i = 0; i < n; i++) {
295 (void)memset(&sa, 0, sizeof(struct stat));
296 (void)memset(&sb, 0, sizeof(struct stat));
297 (void)memset(&sc, 0, sizeof(struct stat));
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);
304 if (sa.st_size + 1 != sb.st_size)
305 atf_tc_fail("invalid file size");
307 if (sb.st_size != sc.st_size)
308 atf_tc_fail("stat(2) and fstat(2) mismatch");
311 ATF_REQUIRE(close(fd) == 0);
312 ATF_REQUIRE(unlink(path) == 0);
315 ATF_TC_CLEANUP(stat_size, tc)
317 (void)unlink(path);
320 ATF_TC(stat_socket);
321 ATF_TC_HEAD(stat_socket, tc)
323 atf_tc_set_md_var(tc, "descr", "Test fstat(2) with "
324 "a socket (PR kern/46077)");
327 ATF_TC_BODY(stat_socket, tc)
329 struct sockaddr_in addr;
330 struct stat st;
331 uint32_t iaddr;
332 int fd, flags;
334 (void)memset(&st, 0, sizeof(struct stat));
335 (void)memset(&addr, 0, sizeof(struct sockaddr_in));
337 fd = socket(AF_INET, SOCK_STREAM, 0);
338 ATF_REQUIRE(fd >= 0);
340 flags = fcntl(fd, F_GETFL);
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);
346 addr.sin_port = htons(42);
347 addr.sin_family = AF_INET;
348 addr.sin_addr.s_addr = iaddr;
350 errno = 0;
352 ATF_REQUIRE_ERRNO(EINPROGRESS,
353 connect(fd, (struct sockaddr *)&addr,
354 sizeof(struct sockaddr_in)) == -1);
356 errno = 0;
358 if (fstat(fd, &st) != 0 || errno != 0)
359 atf_tc_fail("fstat(2) failed for a EINPROGRESS socket");
361 (void)close(fd);
364 ATF_TC_WITH_CLEANUP(stat_symlink);
365 ATF_TC_HEAD(stat_symlink, tc)
367 atf_tc_set_md_var(tc, "descr", "Test symbolic links with stat(2)");
370 ATF_TC_BODY(stat_symlink, tc)
372 const char *pathlink = "pathlink";
373 struct stat sa, sb;
374 int fd;
376 (void)memset(&sa, 0, sizeof(struct stat));
377 (void)memset(&sb, 0, sizeof(struct stat));
379 fd = open(path, O_WRONLY | O_CREAT);
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);
386 if (S_ISLNK(sa.st_mode) != 0)
387 atf_tc_fail("stat(2) detected symbolic link");
389 if (S_ISLNK(sb.st_mode) == 0)
390 atf_tc_fail("lstat(2) did not detect symbolic link");
392 if (sa.st_mode == sb.st_mode)
393 atf_tc_fail("inconsistencies between stat(2) and lstat(2)");
395 ATF_REQUIRE(unlink(path) == 0);
396 ATF_REQUIRE(unlink(pathlink) == 0);
399 ATF_TC_CLEANUP(stat_symlink, tc)
401 (void)unlink(path);
404 ATF_TP_ADD_TCS(tp)
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);
416 return atf_no_error();