ftw_test.c revision 290572
1145479Smp/*-
2145479Smp * Copyright (c) 2012 Jilles Tjoelker
3145479Smp * All rights reserved.
4145479Smp *
5145479Smp * Redistribution and use in source and binary forms, with or without
6145479Smp * modification, are permitted provided that the following conditions
7145479Smp * are met:
8145479Smp * 1. Redistributions of source code must retain the above copyright
9145479Smp *    notice, this list of conditions and the following disclaimer.
10145479Smp * 2. Redistributions in binary form must reproduce the above copyright
11145479Smp *    notice, this list of conditions and the following disclaimer in the
12145479Smp *    documentation and/or other materials provided with the distribution.
13167465Smp *
14167465Smp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15167465Smp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16167465Smp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17167465Smp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18167465Smp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19167465Smp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20167465Smp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21167465Smp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22167465Smp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23167465Smp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24145479Smp * SUCH DAMAGE.
25145479Smp */
26145479Smp
27145479Smp/*
28167465Smp * Limited test program for nftw() as specified by IEEE Std. 1003.1-2008.
29167465Smp */
30167465Smp
31167465Smp#include <sys/cdefs.h>
32145479Smp__FBSDID("$FreeBSD: head/lib/libc/tests/gen/ftw_test.c 290572 2015-11-09 06:24:11Z ngie $");
33145479Smp
34145479Smp#include <sys/wait.h>
35145479Smp#include <err.h>
36145479Smp#include <errno.h>
37145479Smp#include <fcntl.h>
38145479Smp#include <ftw.h>
39232633Smp#include <limits.h>
40232633Smp#include <stdio.h>
41232633Smp#include <stdlib.h>
42167465Smp#include <string.h>
43167465Smp#include <spawn.h>
44167465Smp#include <unistd.h>
45145479Smp
46145479Smp#include <atf-c.h>
47145479Smp
48145479Smpextern char **environ;
49145479Smp
50145479Smpstatic char template[] = "testftw.XXXXXXXXXX";
51145479Smpstatic char dir[PATH_MAX];
52145479Smpstatic int failures;
53145479Smpstatic int ftwflags;
54145479Smp
55145479Smpstatic int
56145479Smpcb(const char *path, const struct stat *st, int type, struct FTW *f)
57232633Smp{
58232633Smp
59232633Smp	switch (type) {
60232633Smp	case FTW_D:
61145479Smp		if ((ftwflags & FTW_DEPTH) == 0)
62145479Smp			return (0);
63145479Smp		break;
64145479Smp	case FTW_DP:
65145479Smp		if ((ftwflags & FTW_DEPTH) != 0)
66145479Smp			return (0);
67145479Smp		break;
68145479Smp	case FTW_SL:
69232633Smp		if ((ftwflags & FTW_PHYS) != 0)
70232633Smp			return (0);
71232633Smp		break;
72167465Smp	}
73167465Smp	ATF_CHECK_MSG(false,
74167465Smp	    "unexpected path=%s type=%d f.level=%d\n",
75145479Smp	    path, type, f->level);
76145479Smp	return (0);
77145479Smp}
78145479Smp
79145479SmpATF_TC_WITHOUT_HEAD(ftw_test);
80145479SmpATF_TC_BODY(ftw_test, tc)
81145479Smp{
82145479Smp	int fd;
83145479Smp
84232633Smp	ATF_REQUIRE_MSG(mkdtemp(template) != NULL, "mkdtemp failed");
85232633Smp
86232633Smp	/* XXX: the path needs to be absolute for the 0/FTW_DEPTH testcases */
87145479Smp	ATF_REQUIRE_MSG(realpath(template, dir) != NULL,
88145479Smp	    "realpath failed; errno=%d", errno);
89145479Smp
90145479Smp	fd = open(dir, O_DIRECTORY|O_RDONLY);
91145479Smp	ATF_REQUIRE_MSG(fd != -1, "open failed; errno=%d", errno);
92145479Smp
93145479Smp	ATF_REQUIRE_MSG(mkdirat(fd, "d1", 0777) == 0,
94145479Smp	    "mkdirat failed; errno=%d", errno);
95145479Smp
96232633Smp	ATF_REQUIRE_MSG(symlinkat(dir, fd, "d1/looper") == 0,
97232633Smp	    "symlinkat failed; errno=%d", errno);
98232633Smp
99145479Smp	printf("ftwflags=FTW_PHYS\n");
100145479Smp	ftwflags = FTW_PHYS;
101145479Smp	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
102145479Smp	    "nftw FTW_PHYS failed; errno=%d", errno);
103145479Smp
104145479Smp	printf("ftwflags=FTW_PHYS|FTW_DEPTH\n");
105145479Smp	ftwflags = FTW_PHYS|FTW_DEPTH;
106145479Smp	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
107145479Smp	    "nftw FTW_PHYS|FTW_DEPTH failed; errno=%d", errno);
108145479Smp
109145479Smp	printf("ftwflags=0\n");
110145479Smp	ftwflags = 0;
111145479Smp	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
112145479Smp	    "nftw 0 failed; errno=%d", errno);
113145479Smp
114145479Smp	printf("ftwflags=FTW_DEPTH\n");
115145479Smp	ftwflags = FTW_DEPTH;
116145479Smp	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
117167465Smp	    "nftw FTW_DEPTH failed; errno=%d", errno);
118167465Smp
119167465Smp	close(fd);
120167465Smp}
121145479Smp
122145479SmpATF_TP_ADD_TCS(tp)
123145479Smp{
124145479Smp
125145479Smp	ATF_TP_ADD_TC(tp, ftw_test);
126145479Smp
127145479Smp	return (atf_no_error());
128145479Smp}
129145479Smp