1/*-
2 * Copyright (c) 2012 Jilles Tjoelker
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/*
28 * Limited test program for nftw() as specified by IEEE Std. 1003.1-2008.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include <sys/wait.h>
35#include <err.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <ftw.h>
39#include <limits.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <spawn.h>
44#include <unistd.h>
45
46#include <atf-c.h>
47
48extern char **environ;
49
50static char template[] = "testftw.XXXXXXXXXX";
51static char dir[PATH_MAX];
52static int ftwflags;
53
54static int
55cb(const char *path, const struct stat *st, int type, struct FTW *f)
56{
57
58	switch (type) {
59	case FTW_D:
60		if ((ftwflags & FTW_DEPTH) == 0)
61			return (0);
62		break;
63	case FTW_DP:
64		if ((ftwflags & FTW_DEPTH) != 0)
65			return (0);
66		break;
67	case FTW_SL:
68		if ((ftwflags & FTW_PHYS) != 0)
69			return (0);
70		break;
71	}
72	ATF_CHECK_MSG(false,
73	    "unexpected path=%s type=%d f.level=%d\n",
74	    path, type, f->level);
75	return (0);
76}
77
78ATF_TC_WITHOUT_HEAD(ftw_test);
79ATF_TC_BODY(ftw_test, tc)
80{
81	int fd;
82
83	ATF_REQUIRE_MSG(mkdtemp(template) != NULL, "mkdtemp failed");
84
85	/* XXX: the path needs to be absolute for the 0/FTW_DEPTH testcases */
86	ATF_REQUIRE_MSG(realpath(template, dir) != NULL,
87	    "realpath failed; errno=%d", errno);
88
89	fd = open(dir, O_DIRECTORY|O_RDONLY);
90	ATF_REQUIRE_MSG(fd != -1, "open failed; errno=%d", errno);
91
92	ATF_REQUIRE_MSG(mkdirat(fd, "d1", 0777) == 0,
93	    "mkdirat failed; errno=%d", errno);
94
95	ATF_REQUIRE_MSG(symlinkat(dir, fd, "d1/looper") == 0,
96	    "symlinkat failed; errno=%d", errno);
97
98	printf("ftwflags=FTW_PHYS\n");
99	ftwflags = FTW_PHYS;
100	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
101	    "nftw FTW_PHYS failed; errno=%d", errno);
102
103	printf("ftwflags=FTW_PHYS|FTW_DEPTH\n");
104	ftwflags = FTW_PHYS|FTW_DEPTH;
105	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
106	    "nftw FTW_PHYS|FTW_DEPTH failed; errno=%d", errno);
107
108	printf("ftwflags=0\n");
109	ftwflags = 0;
110	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
111	    "nftw 0 failed; errno=%d", errno);
112
113	printf("ftwflags=FTW_DEPTH\n");
114	ftwflags = FTW_DEPTH;
115	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
116	    "nftw FTW_DEPTH failed; errno=%d", errno);
117
118	close(fd);
119}
120
121ATF_TP_ADD_TCS(tp)
122{
123
124	ATF_TP_ADD_TC(tp, ftw_test);
125
126	return (atf_no_error());
127}
128