1239160Sjilles/*-
2239160Sjilles * Copyright (c) 2012 Jilles Tjoelker
3239160Sjilles * All rights reserved.
4239160Sjilles *
5239160Sjilles * Redistribution and use in source and binary forms, with or without
6239160Sjilles * modification, are permitted provided that the following conditions
7239160Sjilles * are met:
8239160Sjilles * 1. Redistributions of source code must retain the above copyright
9239160Sjilles *    notice, this list of conditions and the following disclaimer.
10239160Sjilles * 2. Redistributions in binary form must reproduce the above copyright
11239160Sjilles *    notice, this list of conditions and the following disclaimer in the
12239160Sjilles *    documentation and/or other materials provided with the distribution.
13239160Sjilles *
14239160Sjilles * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15239160Sjilles * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16239160Sjilles * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17239160Sjilles * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18239160Sjilles * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19239160Sjilles * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20239160Sjilles * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21239160Sjilles * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22239160Sjilles * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23239160Sjilles * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24239160Sjilles * SUCH DAMAGE.
25239160Sjilles */
26239160Sjilles
27239160Sjilles/*
28239160Sjilles * Limited test program for nftw() as specified by IEEE Std. 1003.1-2008.
29239160Sjilles */
30239160Sjilles
31239160Sjilles#include <sys/cdefs.h>
32239160Sjilles__FBSDID("$FreeBSD$");
33239160Sjilles
34239160Sjilles#include <sys/wait.h>
35253403Skevlo#include <err.h>
36239160Sjilles#include <errno.h>
37239160Sjilles#include <fcntl.h>
38239160Sjilles#include <ftw.h>
39290572Sngie#include <limits.h>
40239160Sjilles#include <stdio.h>
41239160Sjilles#include <stdlib.h>
42239160Sjilles#include <string.h>
43239160Sjilles#include <spawn.h>
44253403Skevlo#include <unistd.h>
45239160Sjilles
46290572Sngie#include <atf-c.h>
47290572Sngie
48239160Sjillesextern char **environ;
49239160Sjilles
50290572Sngiestatic char template[] = "testftw.XXXXXXXXXX";
51290572Sngiestatic char dir[PATH_MAX];
52239160Sjillesstatic int ftwflags;
53239160Sjilles
54239160Sjillesstatic int
55239160Sjillescb(const char *path, const struct stat *st, int type, struct FTW *f)
56239160Sjilles{
57239160Sjilles
58239160Sjilles	switch (type) {
59239160Sjilles	case FTW_D:
60239160Sjilles		if ((ftwflags & FTW_DEPTH) == 0)
61239160Sjilles			return (0);
62239160Sjilles		break;
63239160Sjilles	case FTW_DP:
64239160Sjilles		if ((ftwflags & FTW_DEPTH) != 0)
65239160Sjilles			return (0);
66239160Sjilles		break;
67239160Sjilles	case FTW_SL:
68239160Sjilles		if ((ftwflags & FTW_PHYS) != 0)
69239160Sjilles			return (0);
70239160Sjilles		break;
71239160Sjilles	}
72290572Sngie	ATF_CHECK_MSG(false,
73290572Sngie	    "unexpected path=%s type=%d f.level=%d\n",
74239160Sjilles	    path, type, f->level);
75239160Sjilles	return (0);
76239160Sjilles}
77239160Sjilles
78290572SngieATF_TC_WITHOUT_HEAD(ftw_test);
79290572SngieATF_TC_BODY(ftw_test, tc)
80239160Sjilles{
81239160Sjilles	int fd;
82239160Sjilles
83290572Sngie	ATF_REQUIRE_MSG(mkdtemp(template) != NULL, "mkdtemp failed");
84239160Sjilles
85290572Sngie	/* XXX: the path needs to be absolute for the 0/FTW_DEPTH testcases */
86290572Sngie	ATF_REQUIRE_MSG(realpath(template, dir) != NULL,
87290572Sngie	    "realpath failed; errno=%d", errno);
88239160Sjilles
89290572Sngie	fd = open(dir, O_DIRECTORY|O_RDONLY);
90290572Sngie	ATF_REQUIRE_MSG(fd != -1, "open failed; errno=%d", errno);
91239160Sjilles
92290572Sngie	ATF_REQUIRE_MSG(mkdirat(fd, "d1", 0777) == 0,
93290572Sngie	    "mkdirat failed; errno=%d", errno);
94239160Sjilles
95290572Sngie	ATF_REQUIRE_MSG(symlinkat(dir, fd, "d1/looper") == 0,
96290572Sngie	    "symlinkat failed; errno=%d", errno);
97239160Sjilles
98290572Sngie	printf("ftwflags=FTW_PHYS\n");
99239160Sjilles	ftwflags = FTW_PHYS;
100290572Sngie	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
101290572Sngie	    "nftw FTW_PHYS failed; errno=%d", errno);
102290572Sngie
103290572Sngie	printf("ftwflags=FTW_PHYS|FTW_DEPTH\n");
104290572Sngie	ftwflags = FTW_PHYS|FTW_DEPTH;
105290572Sngie	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
106290572Sngie	    "nftw FTW_PHYS|FTW_DEPTH failed; errno=%d", errno);
107290572Sngie
108290572Sngie	printf("ftwflags=0\n");
109239160Sjilles	ftwflags = 0;
110290572Sngie	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
111290572Sngie	    "nftw 0 failed; errno=%d", errno);
112290572Sngie
113290572Sngie	printf("ftwflags=FTW_DEPTH\n");
114239160Sjilles	ftwflags = FTW_DEPTH;
115290572Sngie	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
116290572Sngie	    "nftw FTW_DEPTH failed; errno=%d", errno);
117239160Sjilles
118239160Sjilles	close(fd);
119290572Sngie}
120239160Sjilles
121290572SngieATF_TP_ADD_TCS(tp)
122290572Sngie{
123239160Sjilles
124290572Sngie	ATF_TP_ADD_TC(tp, ftw_test);
125239160Sjilles
126290572Sngie	return (atf_no_error());
127239160Sjilles}
128