1 /* $NetBSD: t_fstatat.c,v 1.3 2017/01/10 15:13:56 christos Exp $ */
4 * Copyright (c) 2012 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_fstatat.c,v 1.3 2017/01/10 15:13:56 christos Exp $");
34 #include <sys/param.h>
46 #define FILE "dir/fstatat"
47 #define BASEFILE "fstatat"
48 #define LINK "dir/symlink"
49 #define BASELINK "symlink"
50 #define FILEERR "dir/symlink"
53 ATF_TC_HEAD(fstatat_fd, tc)
55 atf_tc_set_md_var(tc, "descr", "See that fstatat works with fd");
57 ATF_TC_BODY(fstatat_fd, tc)
63 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
64 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
65 ATF_REQUIRE(close(fd) == 0);
67 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
68 ATF_REQUIRE(fstatat(dfd, BASEFILE, &st1, 0) == 0);
69 ATF_REQUIRE(close(dfd) == 0);
71 ATF_REQUIRE(stat(FILE, &st2) == 0);
72 ATF_REQUIRE(memcmp(&st1, &st2, sizeof(st1)) == 0);
75 ATF_TC(fstatat_fdcwd);
76 ATF_TC_HEAD(fstatat_fdcwd, tc)
78 atf_tc_set_md_var(tc, "descr",
79 "See that fstatat works with fd as AT_FDCWD");
81 ATF_TC_BODY(fstatat_fdcwd, tc)
86 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
87 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
88 ATF_REQUIRE(close(fd) == 0);
90 ATF_REQUIRE(chdir(DIR) == 0);
91 ATF_REQUIRE(fstatat(AT_FDCWD, BASEFILE, &st, 0) == 0);
94 ATF_TC(fstatat_fdcwderr);
95 ATF_TC_HEAD(fstatat_fdcwderr, tc)
97 atf_tc_set_md_var(tc, "descr",
98 "See that fstatat fails with fd as AT_FDCWD and bad path");
100 ATF_TC_BODY(fstatat_fdcwderr, tc)
104 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
105 ATF_REQUIRE(fstatat(AT_FDCWD, FILEERR, &st, 0) == -1);
108 ATF_TC(fstatat_fderr1);
109 ATF_TC_HEAD(fstatat_fderr1, tc)
111 atf_tc_set_md_var(tc, "descr", "See that fstatat fail with bad path");
113 ATF_TC_BODY(fstatat_fderr1, tc)
118 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
119 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
120 ATF_REQUIRE(fstatat(dfd, FILEERR, &st, 0) == -1);
121 ATF_REQUIRE(close(dfd) == 0);
124 ATF_TC(fstatat_fderr2);
125 ATF_TC_HEAD(fstatat_fderr2, tc)
127 atf_tc_set_md_var(tc, "descr", "See that fstatat fails with bad fdat");
129 ATF_TC_BODY(fstatat_fderr2, tc)
133 char cwd[MAXPATHLEN];
136 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
137 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
138 ATF_REQUIRE(close(fd) == 0);
140 ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1);
141 ATF_REQUIRE(fstatat(dfd, BASEFILE, &st, 0) == -1);
142 ATF_REQUIRE(close(dfd) == 0);
145 ATF_TC(fstatat_fderr3);
146 ATF_TC_HEAD(fstatat_fderr3, tc)
148 atf_tc_set_md_var(tc, "descr", "See that fstatat fails with fd as -1");
150 ATF_TC_BODY(fstatat_fderr3, tc)
155 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
156 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
157 ATF_REQUIRE(close(fd) == 0);
159 ATF_REQUIRE(fstatat(-1, FILE, &st, 0) == -1);
162 ATF_TC(fstatat_fdlink);
163 ATF_TC_HEAD(fstatat_fdlink, tc)
165 atf_tc_set_md_var(tc, "descr", "See that fstatat works on symlink");
167 ATF_TC_BODY(fstatat_fdlink, tc)
172 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
173 ATF_REQUIRE(symlink(FILE, LINK) == 0); /* target does not exists */
175 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
177 ATF_REQUIRE(fstatat(dfd, BASELINK, &st, 0) == -1);
178 ATF_REQUIRE(errno == ENOENT);
180 ATF_REQUIRE(fstatat(dfd, BASELINK, &st, AT_SYMLINK_NOFOLLOW) == 0);
182 ATF_REQUIRE(close(dfd) == 0);
188 ATF_TP_ADD_TC(tp, fstatat_fd);
189 ATF_TP_ADD_TC(tp, fstatat_fdcwd);
190 ATF_TP_ADD_TC(tp, fstatat_fdcwderr);
191 ATF_TP_ADD_TC(tp, fstatat_fderr1);
192 ATF_TP_ADD_TC(tp, fstatat_fderr2);
193 ATF_TP_ADD_TC(tp, fstatat_fderr3);
194 ATF_TP_ADD_TC(tp, fstatat_fdlink);
196 return atf_no_error();