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