1*11be35a1SLionel Sambuc /* $NetBSD: t_getcwd.c,v 1.3 2011/07/27 05:04:11 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_getcwd.c,v 1.3 2011/07/27 05:04:11 jruoho Exp $");
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuc #include <sys/param.h>
35*11be35a1SLionel Sambuc #include <sys/stat.h>
36*11be35a1SLionel Sambuc
37*11be35a1SLionel Sambuc #include <atf-c.h>
38*11be35a1SLionel Sambuc #include <errno.h>
39*11be35a1SLionel Sambuc #include <fts.h>
40*11be35a1SLionel Sambuc #include <limits.h>
41*11be35a1SLionel Sambuc #include <string.h>
42*11be35a1SLionel Sambuc #include <unistd.h>
43*11be35a1SLionel Sambuc
44*11be35a1SLionel Sambuc ATF_TC(getcwd_err);
ATF_TC_HEAD(getcwd_err,tc)45*11be35a1SLionel Sambuc ATF_TC_HEAD(getcwd_err, tc)
46*11be35a1SLionel Sambuc {
47*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test error conditions in getcwd(3)");
48*11be35a1SLionel Sambuc }
49*11be35a1SLionel Sambuc
ATF_TC_BODY(getcwd_err,tc)50*11be35a1SLionel Sambuc ATF_TC_BODY(getcwd_err, tc)
51*11be35a1SLionel Sambuc {
52*11be35a1SLionel Sambuc char buf[MAXPATHLEN];
53*11be35a1SLionel Sambuc
54*11be35a1SLionel Sambuc errno = 0;
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc ATF_REQUIRE(getcwd(buf, 0) == NULL);
57*11be35a1SLionel Sambuc ATF_REQUIRE(errno == EINVAL);
58*11be35a1SLionel Sambuc
59*11be35a1SLionel Sambuc errno = 0;
60*11be35a1SLionel Sambuc
61*11be35a1SLionel Sambuc ATF_REQUIRE(getcwd((void *)-1, sizeof(buf)) == NULL);
62*11be35a1SLionel Sambuc ATF_REQUIRE(errno == EFAULT);
63*11be35a1SLionel Sambuc }
64*11be35a1SLionel Sambuc
65*11be35a1SLionel Sambuc ATF_TC(getcwd_fts);
ATF_TC_HEAD(getcwd_fts,tc)66*11be35a1SLionel Sambuc ATF_TC_HEAD(getcwd_fts, tc)
67*11be35a1SLionel Sambuc {
68*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "A basic test of getcwd(3)");
69*11be35a1SLionel Sambuc }
70*11be35a1SLionel Sambuc
ATF_TC_BODY(getcwd_fts,tc)71*11be35a1SLionel Sambuc ATF_TC_BODY(getcwd_fts, tc)
72*11be35a1SLionel Sambuc {
73*11be35a1SLionel Sambuc const char *str = NULL;
74*11be35a1SLionel Sambuc char buf[MAXPATHLEN];
75*11be35a1SLionel Sambuc char *argv[2];
76*11be35a1SLionel Sambuc FTSENT *ftse;
77*11be35a1SLionel Sambuc FTS *fts;
78*11be35a1SLionel Sambuc int ops;
79*11be35a1SLionel Sambuc short depth;
80*11be35a1SLionel Sambuc
81*11be35a1SLionel Sambuc /*
82*11be35a1SLionel Sambuc * Do not traverse too deep; cf. PR bin/45180.
83*11be35a1SLionel Sambuc */
84*11be35a1SLionel Sambuc depth = 2;
85*11be35a1SLionel Sambuc
86*11be35a1SLionel Sambuc argv[1] = NULL;
87*11be35a1SLionel Sambuc argv[0] = __UNCONST("/");
88*11be35a1SLionel Sambuc
89*11be35a1SLionel Sambuc /*
90*11be35a1SLionel Sambuc * Test that getcwd(3) works with basic
91*11be35a1SLionel Sambuc * system directories. Note that having
92*11be35a1SLionel Sambuc * no FTS_NOCHDIR specified should ensure
93*11be35a1SLionel Sambuc * that the current directory is visited.
94*11be35a1SLionel Sambuc */
95*11be35a1SLionel Sambuc ops = FTS_PHYSICAL | FTS_NOSTAT;
96*11be35a1SLionel Sambuc fts = fts_open(argv, ops, NULL);
97*11be35a1SLionel Sambuc
98*11be35a1SLionel Sambuc if (fts == NULL) {
99*11be35a1SLionel Sambuc str = "failed to initialize fts(3)";
100*11be35a1SLionel Sambuc goto out;
101*11be35a1SLionel Sambuc }
102*11be35a1SLionel Sambuc
103*11be35a1SLionel Sambuc while ((ftse = fts_read(fts)) != NULL) {
104*11be35a1SLionel Sambuc
105*11be35a1SLionel Sambuc if (ftse->fts_level < 1)
106*11be35a1SLionel Sambuc continue;
107*11be35a1SLionel Sambuc
108*11be35a1SLionel Sambuc if (ftse->fts_level > depth) {
109*11be35a1SLionel Sambuc (void)fts_set(fts, ftse, FTS_SKIP);
110*11be35a1SLionel Sambuc continue;
111*11be35a1SLionel Sambuc }
112*11be35a1SLionel Sambuc
113*11be35a1SLionel Sambuc switch(ftse->fts_info) {
114*11be35a1SLionel Sambuc
115*11be35a1SLionel Sambuc case FTS_DP:
116*11be35a1SLionel Sambuc
117*11be35a1SLionel Sambuc (void)memset(buf, 0, sizeof(buf));
118*11be35a1SLionel Sambuc
119*11be35a1SLionel Sambuc if (getcwd(buf, sizeof(buf)) == NULL) {
120*11be35a1SLionel Sambuc str = "getcwd(3) failed";
121*11be35a1SLionel Sambuc goto out;
122*11be35a1SLionel Sambuc }
123*11be35a1SLionel Sambuc
124*11be35a1SLionel Sambuc if (strstr(ftse->fts_path, buf) == NULL) {
125*11be35a1SLionel Sambuc str = "getcwd(3) returned incorrect path";
126*11be35a1SLionel Sambuc goto out;
127*11be35a1SLionel Sambuc }
128*11be35a1SLionel Sambuc
129*11be35a1SLionel Sambuc break;
130*11be35a1SLionel Sambuc
131*11be35a1SLionel Sambuc default:
132*11be35a1SLionel Sambuc break;
133*11be35a1SLionel Sambuc }
134*11be35a1SLionel Sambuc }
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambuc out:
137*11be35a1SLionel Sambuc if (fts != NULL)
138*11be35a1SLionel Sambuc (void)fts_close(fts);
139*11be35a1SLionel Sambuc
140*11be35a1SLionel Sambuc if (str != NULL)
141*11be35a1SLionel Sambuc atf_tc_fail("%s", str);
142*11be35a1SLionel Sambuc }
143*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)144*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
145*11be35a1SLionel Sambuc {
146*11be35a1SLionel Sambuc
147*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, getcwd_err);
148*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, getcwd_fts);
149*11be35a1SLionel Sambuc
150*11be35a1SLionel Sambuc return atf_no_error();
151*11be35a1SLionel Sambuc }
152