1 /* $NetBSD: t_getcwd.c,v 1.3 2011/07/27 05:04:11 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_getcwd.c,v 1.3 2011/07/27 05:04:11 jruoho Exp $");
34 #include <sys/param.h>
45 ATF_TC_HEAD(getcwd_err
, tc
)
47 atf_tc_set_md_var(tc
, "descr", "Test error conditions in getcwd(3)");
50 ATF_TC_BODY(getcwd_err
, tc
)
56 ATF_REQUIRE(getcwd(buf
, 0) == NULL
);
57 ATF_REQUIRE(errno
== EINVAL
);
61 ATF_REQUIRE(getcwd((void *)-1, sizeof(buf
)) == NULL
);
62 ATF_REQUIRE(errno
== EFAULT
);
66 ATF_TC_HEAD(getcwd_fts
, tc
)
68 atf_tc_set_md_var(tc
, "descr", "A basic test of getcwd(3)");
71 ATF_TC_BODY(getcwd_fts
, tc
)
73 const char *str
= NULL
;
82 * Do not traverse too deep; cf. PR bin/45180.
87 argv
[0] = __UNCONST("/");
90 * Test that getcwd(3) works with basic
91 * system directories. Note that having
92 * no FTS_NOCHDIR specified should ensure
93 * that the current directory is visited.
95 ops
= FTS_PHYSICAL
| FTS_NOSTAT
;
96 fts
= fts_open(argv
, ops
, NULL
);
99 str
= "failed to initialize fts(3)";
103 while ((ftse
= fts_read(fts
)) != NULL
) {
105 if (ftse
->fts_level
< 1)
108 if (ftse
->fts_level
> depth
) {
109 (void)fts_set(fts
, ftse
, FTS_SKIP
);
113 switch(ftse
->fts_info
) {
117 (void)memset(buf
, 0, sizeof(buf
));
119 if (getcwd(buf
, sizeof(buf
)) == NULL
) {
120 str
= "getcwd(3) failed";
124 if (strstr(ftse
->fts_path
, buf
) == NULL
) {
125 str
= "getcwd(3) returned incorrect path";
138 (void)fts_close(fts
);
141 atf_tc_fail("%s", str
);
147 ATF_TP_ADD_TC(tp
, getcwd_err
);
148 ATF_TP_ADD_TC(tp
, getcwd_fts
);
150 return atf_no_error();