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