xref: /freebsd/contrib/netbsd-tests/lib/libc/c063/t_fstatat.c (revision a812392203d7c4c3f0db9d8a0f3391374c49c71f)
1 /*	$NetBSD: t_fstatat.c,v 1.2 2013/03/17 04:46:06 jmmv Exp $ */
2 
3 /*-
4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Emmanuel Dreyfus.
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_fstatat.c,v 1.2 2013/03/17 04:46:06 jmmv Exp $");
33 
34 #include <atf-c.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <limits.h>
38 #include <paths.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <sys/param.h>
43 #ifdef __FreeBSD__
44 #include <sys/stat.h>
45 #endif
46 
47 #define DIR "dir"
48 #define FILE "dir/fstatat"
49 #define BASEFILE "fstatat"
50 #define LINK "dir/symlink"
51 #define BASELINK "symlink"
52 #define FILEERR "dir/symlink"
53 
54 ATF_TC(fstatat_fd);
55 ATF_TC_HEAD(fstatat_fd, tc)
56 {
57 	atf_tc_set_md_var(tc, "descr", "See that fstatat works with fd");
58 }
59 ATF_TC_BODY(fstatat_fd, tc)
60 {
61 	int dfd;
62 	int fd;
63 	struct stat st1, st2;
64 
65 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
66 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
67 	ATF_REQUIRE(close(fd) == 0);
68 
69 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
70 	ATF_REQUIRE(fstatat(dfd, BASEFILE, &st1, 0) == 0);
71 	ATF_REQUIRE(close(dfd) == 0);
72 
73 	ATF_REQUIRE(stat(FILE, &st2) == 0);
74 	ATF_REQUIRE(memcmp(&st1, &st2, sizeof(st1)) == 0);
75 }
76 
77 ATF_TC(fstatat_fdcwd);
78 ATF_TC_HEAD(fstatat_fdcwd, tc)
79 {
80 	atf_tc_set_md_var(tc, "descr",
81 			  "See that fstatat works with fd as AT_FDCWD");
82 }
83 ATF_TC_BODY(fstatat_fdcwd, tc)
84 {
85 	int fd;
86 	struct stat st;
87 
88 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
89 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
90 	ATF_REQUIRE(close(fd) == 0);
91 
92 	ATF_REQUIRE(chdir(DIR) == 0);
93 	ATF_REQUIRE(fstatat(AT_FDCWD, BASEFILE, &st, 0) == 0);
94 }
95 
96 ATF_TC(fstatat_fdcwderr);
97 ATF_TC_HEAD(fstatat_fdcwderr, tc)
98 {
99 	atf_tc_set_md_var(tc, "descr",
100 		  "See that fstatat fails with fd as AT_FDCWD and bad path");
101 }
102 ATF_TC_BODY(fstatat_fdcwderr, tc)
103 {
104 	struct stat st;
105 
106 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
107 	ATF_REQUIRE(fstatat(AT_FDCWD, FILEERR, &st, 0) == -1);
108 }
109 
110 ATF_TC(fstatat_fderr1);
111 ATF_TC_HEAD(fstatat_fderr1, tc)
112 {
113 	atf_tc_set_md_var(tc, "descr", "See that fstatat fail with bad path");
114 }
115 ATF_TC_BODY(fstatat_fderr1, tc)
116 {
117 	int dfd;
118 	struct stat st;
119 
120 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
121 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
122 	ATF_REQUIRE(fstatat(dfd, FILEERR, &st, 0) == -1);
123 	ATF_REQUIRE(close(dfd) == 0);
124 }
125 
126 ATF_TC(fstatat_fderr2);
127 ATF_TC_HEAD(fstatat_fderr2, tc)
128 {
129 	atf_tc_set_md_var(tc, "descr", "See that fstatat fails with bad fdat");
130 }
131 ATF_TC_BODY(fstatat_fderr2, tc)
132 {
133 	int dfd;
134 	int fd;
135 	char cwd[MAXPATHLEN];
136 	struct stat st;
137 
138 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
139 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
140 	ATF_REQUIRE(close(fd) == 0);
141 
142 	ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1);
143 	ATF_REQUIRE(fstatat(dfd, BASEFILE, &st, 0) == -1);
144 	ATF_REQUIRE(close(dfd) == 0);
145 }
146 
147 ATF_TC(fstatat_fderr3);
148 ATF_TC_HEAD(fstatat_fderr3, tc)
149 {
150 	atf_tc_set_md_var(tc, "descr", "See that fstatat fails with fd as -1");
151 }
152 ATF_TC_BODY(fstatat_fderr3, tc)
153 {
154 	int fd;
155 	struct stat st;
156 
157 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
158 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
159 	ATF_REQUIRE(close(fd) == 0);
160 
161 	ATF_REQUIRE(fstatat(-1, FILE, &st, 0) == -1);
162 }
163 
164 ATF_TC(fstatat_fdlink);
165 ATF_TC_HEAD(fstatat_fdlink, tc)
166 {
167 	atf_tc_set_md_var(tc, "descr", "See that fstatat works on symlink");
168 }
169 ATF_TC_BODY(fstatat_fdlink, tc)
170 {
171 	int dfd;
172 	struct stat st;
173 
174 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
175 	ATF_REQUIRE(symlink(FILE, LINK) == 0); /* target does not exists */
176 
177 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
178 
179 	ATF_REQUIRE(fstatat(dfd, BASELINK, &st, 0) == -1);
180 	ATF_REQUIRE(errno == ENOENT);
181 
182 	ATF_REQUIRE(fstatat(dfd, BASELINK, &st, AT_SYMLINK_NOFOLLOW) == 0);
183 
184 	ATF_REQUIRE(close(dfd) == 0);
185 }
186 
187 ATF_TP_ADD_TCS(tp)
188 {
189 
190 	ATF_TP_ADD_TC(tp, fstatat_fd);
191 	ATF_TP_ADD_TC(tp, fstatat_fdcwd);
192 	ATF_TP_ADD_TC(tp, fstatat_fdcwderr);
193 	ATF_TP_ADD_TC(tp, fstatat_fderr1);
194 	ATF_TP_ADD_TC(tp, fstatat_fderr2);
195 	ATF_TP_ADD_TC(tp, fstatat_fderr3);
196 	ATF_TP_ADD_TC(tp, fstatat_fdlink);
197 
198 	return atf_no_error();
199 }
200