Wed Jul 27 05:04:11 2011 UTC ()
As per PR bin/45180, do not traverse too deep.


(jruoho)
diff -r1.2 -r1.3 src/tests/lib/libc/gen/t_getcwd.c

cvs diff -r1.2 -r1.3 src/tests/lib/libc/gen/t_getcwd.c (switch to unified diff)

--- src/tests/lib/libc/gen/t_getcwd.c 2011/06/16 15:33:25 1.2
+++ src/tests/lib/libc/gen/t_getcwd.c 2011/07/27 05:04:11 1.3
@@ -1,146 +1,151 @@ @@ -1,146 +1,151 @@
1/* $NetBSD: t_getcwd.c,v 1.2 2011/06/16 15:33:25 joerg Exp $ */ 1/* $NetBSD: t_getcwd.c,v 1.3 2011/07/27 05:04:11 jruoho Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen. 8 * by Jukka Ruohonen.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 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 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 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#include <sys/cdefs.h> 31#include <sys/cdefs.h>
32__RCSID("$NetBSD: t_getcwd.c,v 1.2 2011/06/16 15:33:25 joerg Exp $"); 32__RCSID("$NetBSD: t_getcwd.c,v 1.3 2011/07/27 05:04:11 jruoho Exp $");
33 33
34#include <sys/param.h> 34#include <sys/param.h>
35#include <sys/stat.h> 35#include <sys/stat.h>
36 36
37#include <atf-c.h> 37#include <atf-c.h>
38#include <errno.h> 38#include <errno.h>
39#include <fts.h> 39#include <fts.h>
40#include <limits.h> 40#include <limits.h>
41#include <string.h> 41#include <string.h>
42#include <unistd.h> 42#include <unistd.h>
43 43
44ATF_TC(getcwd_err); 44ATF_TC(getcwd_err);
45ATF_TC_HEAD(getcwd_err, tc) 45ATF_TC_HEAD(getcwd_err, tc)
46{ 46{
47 atf_tc_set_md_var(tc, "descr", "Test error conditions in getcwd(3)"); 47 atf_tc_set_md_var(tc, "descr", "Test error conditions in getcwd(3)");
48} 48}
49 49
50ATF_TC_BODY(getcwd_err, tc) 50ATF_TC_BODY(getcwd_err, tc)
51{ 51{
52 char buf[MAXPATHLEN]; 52 char buf[MAXPATHLEN];
53 53
54 errno = 0; 54 errno = 0;
55 55
56 ATF_REQUIRE(getcwd(buf, 0) == NULL); 56 ATF_REQUIRE(getcwd(buf, 0) == NULL);
57 ATF_REQUIRE(errno == EINVAL); 57 ATF_REQUIRE(errno == EINVAL);
58 58
59 errno = 0; 59 errno = 0;
60 60
61 ATF_REQUIRE(getcwd((void *)-1, sizeof(buf)) == NULL); 61 ATF_REQUIRE(getcwd((void *)-1, sizeof(buf)) == NULL);
62 ATF_REQUIRE(errno == EFAULT); 62 ATF_REQUIRE(errno == EFAULT);
63} 63}
64 64
65ATF_TC(getcwd_fts); 65ATF_TC(getcwd_fts);
66ATF_TC_HEAD(getcwd_fts, tc) 66ATF_TC_HEAD(getcwd_fts, tc)
67{ 67{
68 atf_tc_set_md_var(tc, "descr", "A basic test of getcwd(3)"); 68 atf_tc_set_md_var(tc, "descr", "A basic test of getcwd(3)");
69} 69}
70 70
71ATF_TC_BODY(getcwd_fts, tc) 71ATF_TC_BODY(getcwd_fts, tc)
72{ 72{
73 const char *str = NULL; 73 const char *str = NULL;
74 const short depth = 3; 
75 char buf[MAXPATHLEN]; 74 char buf[MAXPATHLEN];
76 char *argv[2]; 75 char *argv[2];
77 FTSENT *ftse; 76 FTSENT *ftse;
78 FTS *fts; 77 FTS *fts;
79 int ops; 78 int ops;
 79 short depth;
 80
 81 /*
 82 * Do not traverse too deep; cf. PR bin/45180.
 83 */
 84 depth = 2;
80 85
81 argv[1] = NULL; 86 argv[1] = NULL;
82 argv[0] = __UNCONST("/"); 87 argv[0] = __UNCONST("/");
83 88
84 /* 89 /*
85 * Test that getcwd(3) works with basic 90 * Test that getcwd(3) works with basic
86 * system directories. Note that having 91 * system directories. Note that having
87 * no FTS_NOCHDIR specified should ensure 92 * no FTS_NOCHDIR specified should ensure
88 * that the current directory is visited. 93 * that the current directory is visited.
89 */ 94 */
90 ops = FTS_PHYSICAL | FTS_NOSTAT; 95 ops = FTS_PHYSICAL | FTS_NOSTAT;
91 fts = fts_open(argv, ops, NULL); 96 fts = fts_open(argv, ops, NULL);
92 97
93 if (fts == NULL) { 98 if (fts == NULL) {
94 str = "failed to initialize fts(3)"; 99 str = "failed to initialize fts(3)";
95 goto out; 100 goto out;
96 } 101 }
97 102
98 while ((ftse = fts_read(fts)) != NULL) { 103 while ((ftse = fts_read(fts)) != NULL) {
99 104
100 if (ftse->fts_level < 1) 105 if (ftse->fts_level < 1)
101 continue; 106 continue;
102 107
103 if (ftse->fts_level > depth) { 108 if (ftse->fts_level > depth) {
104 (void)fts_set(fts, ftse, FTS_SKIP); 109 (void)fts_set(fts, ftse, FTS_SKIP);
105 continue; 110 continue;
106 } 111 }
107 112
108 switch(ftse->fts_info) { 113 switch(ftse->fts_info) {
109 114
110 case FTS_DP: 115 case FTS_DP:
111 116
112 (void)memset(buf, 0, sizeof(buf)); 117 (void)memset(buf, 0, sizeof(buf));
113 118
114 if (getcwd(buf, sizeof(buf)) == NULL) { 119 if (getcwd(buf, sizeof(buf)) == NULL) {
115 str = "getcwd(3) failed"; 120 str = "getcwd(3) failed";
116 goto out; 121 goto out;
117 } 122 }
118 123
119 if (strstr(ftse->fts_path, buf) == NULL) { 124 if (strstr(ftse->fts_path, buf) == NULL) {
120 str = "getcwd(3) returned incorrect path"; 125 str = "getcwd(3) returned incorrect path";
121 goto out; 126 goto out;
122 } 127 }
123 128
124 break; 129 break;
125 130
126 default: 131 default:
127 break; 132 break;
128 } 133 }
129 } 134 }
130 135
131out: 136out:
132 if (fts != NULL) 137 if (fts != NULL)
133 (void)fts_close(fts); 138 (void)fts_close(fts);
134 139
135 if (str != NULL) 140 if (str != NULL)
136 atf_tc_fail("%s", str); 141 atf_tc_fail("%s", str);
137} 142}
138 143
139ATF_TP_ADD_TCS(tp) 144ATF_TP_ADD_TCS(tp)
140{ 145{
141 146
142 ATF_TP_ADD_TC(tp, getcwd_err); 147 ATF_TP_ADD_TC(tp, getcwd_err);
143 ATF_TP_ADD_TC(tp, getcwd_fts); 148 ATF_TP_ADD_TC(tp, getcwd_fts);
144 149
145 return atf_no_error(); 150 return atf_no_error();
146} 151}