ftw_test.c revision 290572
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: head/lib/libc/tests/gen/ftw_test.c 290572 2015-11-09 06:24:11Z ngie $");
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 failures;
53239160Sjillesstatic int ftwflags;
54239160Sjilles
55239160Sjillesstatic int
56239160Sjillescb(const char *path, const struct stat *st, int type, struct FTW *f)
57239160Sjilles{
58239160Sjilles
59239160Sjilles	switch (type) {
60239160Sjilles	case FTW_D:
61239160Sjilles		if ((ftwflags & FTW_DEPTH) == 0)
62239160Sjilles			return (0);
63239160Sjilles		break;
64239160Sjilles	case FTW_DP:
65239160Sjilles		if ((ftwflags & FTW_DEPTH) != 0)
66239160Sjilles			return (0);
67239160Sjilles		break;
68239160Sjilles	case FTW_SL:
69239160Sjilles		if ((ftwflags & FTW_PHYS) != 0)
70239160Sjilles			return (0);
71239160Sjilles		break;
72239160Sjilles	}
73290572Sngie	ATF_CHECK_MSG(false,
74290572Sngie	    "unexpected path=%s type=%d f.level=%d\n",
75239160Sjilles	    path, type, f->level);
76239160Sjilles	return (0);
77239160Sjilles}
78239160Sjilles
79290572SngieATF_TC_WITHOUT_HEAD(ftw_test);
80290572SngieATF_TC_BODY(ftw_test, tc)
81239160Sjilles{
82239160Sjilles	int fd;
83239160Sjilles
84290572Sngie	ATF_REQUIRE_MSG(mkdtemp(template) != NULL, "mkdtemp failed");
85239160Sjilles
86290572Sngie	/* XXX: the path needs to be absolute for the 0/FTW_DEPTH testcases */
87290572Sngie	ATF_REQUIRE_MSG(realpath(template, dir) != NULL,
88290572Sngie	    "realpath failed; errno=%d", errno);
89239160Sjilles
90290572Sngie	fd = open(dir, O_DIRECTORY|O_RDONLY);
91290572Sngie	ATF_REQUIRE_MSG(fd != -1, "open failed; errno=%d", errno);
92239160Sjilles
93290572Sngie	ATF_REQUIRE_MSG(mkdirat(fd, "d1", 0777) == 0,
94290572Sngie	    "mkdirat failed; errno=%d", errno);
95239160Sjilles
96290572Sngie	ATF_REQUIRE_MSG(symlinkat(dir, fd, "d1/looper") == 0,
97290572Sngie	    "symlinkat failed; errno=%d", errno);
98239160Sjilles
99290572Sngie	printf("ftwflags=FTW_PHYS\n");
100239160Sjilles	ftwflags = FTW_PHYS;
101290572Sngie	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
102290572Sngie	    "nftw FTW_PHYS failed; errno=%d", errno);
103290572Sngie
104290572Sngie	printf("ftwflags=FTW_PHYS|FTW_DEPTH\n");
105290572Sngie	ftwflags = FTW_PHYS|FTW_DEPTH;
106290572Sngie	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
107290572Sngie	    "nftw FTW_PHYS|FTW_DEPTH failed; errno=%d", errno);
108290572Sngie
109290572Sngie	printf("ftwflags=0\n");
110239160Sjilles	ftwflags = 0;
111290572Sngie	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
112290572Sngie	    "nftw 0 failed; errno=%d", errno);
113290572Sngie
114290572Sngie	printf("ftwflags=FTW_DEPTH\n");
115239160Sjilles	ftwflags = FTW_DEPTH;
116290572Sngie	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
117290572Sngie	    "nftw FTW_DEPTH failed; errno=%d", errno);
118239160Sjilles
119239160Sjilles	close(fd);
120290572Sngie}
121239160Sjilles
122290572SngieATF_TP_ADD_TCS(tp)
123290572Sngie{
124239160Sjilles
125290572Sngie	ATF_TP_ADD_TC(tp, ftw_test);
126239160Sjilles
127290572Sngie	return (atf_no_error());
128239160Sjilles}
129