]>
git.saurik.com Git - apple/libc.git/blob - tests/netbsd_stat.c
1 /* $NetBSD: t_stat.c,v 1.4 2012/03/17 08:37:08 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 $");
35 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <arpa/inet.h>
49 #include <netinet/in.h>
51 #include <darwintest.h>
53 static const char *path
= "/tmp/stat";
55 T_DECL(stat_chflags
, "Test chflags(2) with stat(2)")
60 (void)memset(&sa
, 0, sizeof(struct stat
));
61 (void)memset(&sb
, 0, sizeof(struct stat
));
64 T_ASSERT_POSIX_SUCCESS((fd
= open(path
, O_RDONLY
| O_CREAT
)), NULL
);
66 T_ASSERT_POSIX_ZERO(stat(path
, &sa
), NULL
);
67 T_ASSERT_POSIX_ZERO(chflags(path
, UF_NODUMP
), NULL
);
68 T_ASSERT_POSIX_ZERO(stat(path
, &sb
), NULL
);
70 T_EXPECT_NE(sa
.st_flags
, sb
.st_flags
| UF_NODUMP
, "stat(2) detects chflags(2)");
72 T_ASSERT_POSIX_ZERO(close(fd
), NULL
);
73 T_ASSERT_POSIX_ZERO(unlink(path
), NULL
);
76 T_DECL(stat_dir
, "Test stat(2) with directories")
78 const short depth
= 2;
80 char *args
[] = {"/System", NULL
};
88 T_ASSERT_NOTNULL(fts
= fts_open(args
, ops
, NULL
), NULL
);
90 while ((ftse
= fts_read(fts
)) != NULL
) {
92 if (ftse
->fts_level
< 1)
95 if (ftse
->fts_level
> depth
) {
96 (void)fts_set(fts
, ftse
, FTS_SKIP
);
100 switch(ftse
->fts_info
) {
104 (void)memset(&sa
, 0, sizeof(struct stat
));
105 (void)memset(&sb
, 0, sizeof(struct stat
));
107 T_ASSERT_POSIX_ZERO(stat(ftse
->fts_parent
->fts_path
,&sa
), NULL
);
108 T_ASSERT_POSIX_ZERO(chdir(ftse
->fts_path
), NULL
);
109 T_ASSERT_POSIX_ZERO(stat(".", &sb
), NULL
);
112 * The previous two stat(2) calls
113 * should be for the same directory.
115 T_EXPECT_EQ(sa
.st_dev
, sb
.st_dev
, "stat(2) should return consistent device");
116 T_EXPECT_EQ(sa
.st_ino
, sb
.st_ino
, "stat(2) should return consistent inode");
118 T_EXPECT_EQ(sb
.st_ino
, ftse
->fts_statp
->st_ino
, "stat(2) and fts(3) should not differ");
127 (void)fts_close(fts
);
130 #if 0 // TODO: port me
132 ATF_TC_HEAD(stat_err
, tc
)
134 atf_tc_set_md_var(tc
, "descr", "Test errors from the stat(2) family");
139 char buf
[NAME_MAX
+ 1];
142 (void)memset(buf
, 'x', sizeof(buf
));
145 ATF_REQUIRE_ERRNO(EBADF
, fstat(-1, &st
) == -1);
148 ATF_REQUIRE_ERRNO(ENAMETOOLONG
, stat(buf
, &st
) == -1);
151 ATF_REQUIRE_ERRNO(ENAMETOOLONG
, lstat(buf
, &st
) == -1);
154 ATF_REQUIRE_ERRNO(EFAULT
, stat((void *)-1, &st
) == -1);
157 ATF_REQUIRE_ERRNO(EFAULT
, lstat((void *)-1, &st
) == -1);
160 ATF_REQUIRE_ERRNO(EFAULT
, stat("/etc/passwd", (void *)-1) == -1);
163 ATF_REQUIRE_ERRNO(EFAULT
, lstat("/etc/passwd", (void *)-1) == -1);
166 ATF_REQUIRE_ERRNO(ENOENT
, stat("/a/b/c/d/e/f/g/h/i/j/k", &st
) == -1);
169 ATF_REQUIRE_ERRNO(ENOENT
, lstat("/a/b/c/d/e/f/g/h/i/j/k", &st
) == -1);
172 ATF_TC_WITH_CLEANUP(stat_mtime
);
173 ATF_TC_HEAD(stat_mtime
, tc
)
175 atf_tc_set_md_var(tc
, "descr", "Test modification times with stat(2)");
178 T_DECL(stat_mtime
, "")
184 for (i
= 0; i
< __arraycount(fd
); i
++) {
186 (void)memset(&sa
, 0, sizeof(struct stat
));
187 (void)memset(&sb
, 0, sizeof(struct stat
));
189 fd
[i
] = open(path
, O_WRONLY
| O_CREAT
);
191 T_ASSERT_POSIX_SUCCESS(fd
[i
], NULL
);
192 T_ASSERT_EQ(write(fd
[i
], "X", 1), 1, NULL
);
193 T_ASSERT_POSIX_ZERO(stat(path
, &sa
), NULL
);
197 T_ASSERT_EQ(write(fd
[i
], "X", 1), 1, NULL
);
198 T_ASSERT_POSIX_ZERO(stat(path
, &sb
), NULL
);
200 T_ASSERT_POSIX_ZERO(close(fd
[i
]), NULL
);
201 T_ASSERT_POSIX_ZERO(unlink(path
), NULL
);
203 if (sa
.st_mtime
== sb
.st_mtime
)
204 atf_tc_fail("mtimes did not change");
208 ATF_TC_CLEANUP(stat_mtime
, tc
)
213 ATF_TC_WITH_CLEANUP(stat_perm
);
214 ATF_TC_HEAD(stat_perm
, tc
)
216 atf_tc_set_md_var(tc
, "descr", "Test permissions with stat(2)");
217 atf_tc_set_md_var(tc
, "require.user", "root");
220 T_DECL(stat_perm
, "")
227 (void)memset(&sa
, 0, sizeof(struct stat
));
228 (void)memset(&sb
, 0, sizeof(struct stat
));
233 fd
= open(path
, O_RDONLY
| O_CREAT
);
235 T_ASSERT_POSIX_SUCCESS(fd
, NULL
);
236 T_ASSERT_POSIX_ZERO(fstat(fd
, &sa
), NULL
);
237 T_ASSERT_POSIX_ZERO(stat(path
, &sb
), NULL
);
239 if (gid
!= sa
.st_gid
|| sa
.st_gid
!= sb
.st_gid
)
240 atf_tc_fail("invalid GID");
242 if (uid
!= sa
.st_uid
|| sa
.st_uid
!= sb
.st_uid
)
243 atf_tc_fail("invalid UID");
245 T_ASSERT_POSIX_ZERO(close(fd
), NULL
);
246 T_ASSERT_POSIX_ZERO(unlink(path
), NULL
);
249 ATF_TC_CLEANUP(stat_perm
, tc
)
254 ATF_TC_WITH_CLEANUP(stat_size
);
255 ATF_TC_HEAD(stat_size
, tc
)
257 atf_tc_set_md_var(tc
, "descr", "Test file sizes with stat(2)");
260 T_DECL(stat_size
, "")
262 struct stat sa
, sb
, sc
;
267 fd
= open(path
, O_WRONLY
| O_CREAT
);
268 ATF_REQUIRE(fd
>= 0);
270 for (i
= 0; i
< n
; i
++) {
272 (void)memset(&sa
, 0, sizeof(struct stat
));
273 (void)memset(&sb
, 0, sizeof(struct stat
));
274 (void)memset(&sc
, 0, sizeof(struct stat
));
276 T_ASSERT_POSIX_ZERO(fstat(fd
, &sa
), NULL
);
277 T_ASSERT_EQ(write(fd
, "X", 1), 1, NULL
);
278 T_ASSERT_POSIX_ZERO(fstat(fd
, &sb
), NULL
);
279 T_ASSERT_POSIX_ZERO(stat(path
, &sc
), NULL
);
281 if (sa
.st_size
+ 1 != sb
.st_size
)
282 atf_tc_fail("invalid file size");
284 if (sb
.st_size
!= sc
.st_size
)
285 atf_tc_fail("stat(2) and fstat(2) mismatch");
288 T_ASSERT_POSIX_ZERO(close(fd
), NULL
);
289 T_ASSERT_POSIX_ZERO(unlink(path
), NULL
);
292 ATF_TC_CLEANUP(stat_size
, tc
)
298 ATF_TC_HEAD(stat_socket
, tc
)
300 atf_tc_set_md_var(tc
, "descr", "Test fstat(2) with "
301 "a socket (PR kern/46077)");
304 T_DECL(stat_socket
, "")
306 struct sockaddr_in addr
;
311 (void)memset(&st
, 0, sizeof(struct stat
));
312 (void)memset(&addr
, 0, sizeof(struct sockaddr_in
));
314 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
315 ATF_REQUIRE(fd
>= 0);
317 flags
= fcntl(fd
, F_GETFL
);
319 T_ASSERT_POSIX_SUCCESS(flags
, NULL
);
320 T_ASSERT_POSIX_SUCCESS(fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
), NULL
);
321 T_ASSERT_EQ(inet_pton(AF_INET
, "127.0.0.1", &iaddr
), 1, NULL
);
323 addr
.sin_port
= htons(42);
324 addr
.sin_family
= AF_INET
;
325 addr
.sin_addr
.s_addr
= iaddr
;
329 ATF_REQUIRE_ERRNO(EINPROGRESS
,
330 connect(fd
, (struct sockaddr
*)&addr
,
331 sizeof(struct sockaddr_in
)) == -1);
335 if (fstat(fd
, &st
) != 0 || errno
!= 0)
336 atf_tc_fail("fstat(2) failed for a EINPROGRESS socket");
341 ATF_TC_WITH_CLEANUP(stat_symlink
);
342 ATF_TC_HEAD(stat_symlink
, tc
)
344 atf_tc_set_md_var(tc
, "descr", "Test symbolic links with stat(2)");
347 T_DECL(stat_symlink
, "")
349 const char *pathlink
= "pathlink";
353 (void)memset(&sa
, 0, sizeof(struct stat
));
354 (void)memset(&sb
, 0, sizeof(struct stat
));
356 fd
= open(path
, O_WRONLY
| O_CREAT
);
358 ATF_REQUIRE(fd
>= 0);
359 T_ASSERT_POSIX_ZERO(symlink(path
, pathlink
), NULL
);
360 T_ASSERT_POSIX_ZERO(stat(pathlink
, &sa
), NULL
);
361 T_ASSERT_POSIX_ZERO(lstat(pathlink
, &sb
), NULL
);
363 if (S_ISLNK(sa
.st_mode
) != 0)
364 atf_tc_fail("stat(2) detected symbolic link");
366 if (S_ISLNK(sb
.st_mode
) == 0)
367 atf_tc_fail("lstat(2) did not detect symbolic link");
369 if (sa
.st_mode
== sb
.st_mode
)
370 atf_tc_fail("inconsistencies between stat(2) and lstat(2)");
372 T_ASSERT_POSIX_ZERO(unlink(path
), NULL
);
373 T_ASSERT_POSIX_ZERO(unlink(pathlink
), NULL
);
376 ATF_TC_CLEANUP(stat_symlink
, tc
)