t_expand.sh revision 1.6 1 1.5 christos # $NetBSD: t_expand.sh,v 1.6 2016/03/08 14:26:54 christos Exp $
2 1.1 jruoho #
3 1.1 jruoho # Copyright (c) 2007, 2009 The NetBSD Foundation, Inc.
4 1.1 jruoho # All rights reserved.
5 1.1 jruoho #
6 1.1 jruoho # Redistribution and use in source and binary forms, with or without
7 1.1 jruoho # modification, are permitted provided that the following conditions
8 1.1 jruoho # are met:
9 1.1 jruoho # 1. Redistributions of source code must retain the above copyright
10 1.1 jruoho # notice, this list of conditions and the following disclaimer.
11 1.1 jruoho # 2. Redistributions in binary form must reproduce the above copyright
12 1.1 jruoho # notice, this list of conditions and the following disclaimer in the
13 1.1 jruoho # documentation and/or other materials provided with the distribution.
14 1.1 jruoho #
15 1.1 jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1 jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1 jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1 jruoho # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1 jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1 jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1 jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1 jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1 jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1 jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 jruoho # POSSIBILITY OF SUCH DAMAGE.
26 1.1 jruoho #
27 1.6 christos # the implementation of "sh" to test
28 1.6 christos : ${TEST_SH:="/bin/sh"}
29 1.1 jruoho
30 1.1 jruoho #
31 1.1 jruoho # This file tests the functions in expand.c.
32 1.1 jruoho #
33 1.1 jruoho
34 1.1 jruoho delim_argv() {
35 1.1 jruoho str=
36 1.1 jruoho while [ $# -gt 0 ]; do
37 1.1 jruoho if [ -z "${str}" ]; then
38 1.1 jruoho str=">$1<"
39 1.1 jruoho else
40 1.1 jruoho str="${str} >$1<"
41 1.1 jruoho fi
42 1.2 ast shift
43 1.1 jruoho done
44 1.1 jruoho echo ${str}
45 1.1 jruoho }
46 1.1 jruoho
47 1.1 jruoho atf_test_case dollar_at
48 1.1 jruoho dollar_at_head() {
49 1.1 jruoho atf_set "descr" "Somewhere between 2.0.2 and 3.0 the expansion" \
50 1.1 jruoho "of the \$@ variable had been broken. Check for" \
51 1.1 jruoho "this behavior."
52 1.1 jruoho }
53 1.1 jruoho dollar_at_body() {
54 1.1 jruoho # This one should work everywhere.
55 1.6 christos atf_check -s exit:0 -o inline:' EOL\n' -e empty \
56 1.6 christos ${TEST_SH} -c 'echo "" "" | '" sed 's,\$,EOL,'"
57 1.1 jruoho
58 1.1 jruoho # This code triggered the bug.
59 1.6 christos atf_check -s exit:0 -o inline:' EOL\n' -e empty \
60 1.6 christos ${TEST_SH} -c 'set -- "" ""; echo "$@" | '" sed 's,\$,EOL,'"
61 1.6 christos
62 1.6 christos atf_check -s exit:0 -o inline:'0\n' -e empty ${TEST_SH} -c \
63 1.6 christos 'set -- -; shift; n_arg() { echo $#; }; n_arg "$@"'
64 1.1 jruoho }
65 1.1 jruoho
66 1.1 jruoho atf_test_case dollar_at_with_text
67 1.1 jruoho dollar_at_with_text_head() {
68 1.1 jruoho atf_set "descr" "Test \$@ expansion when it is surrounded by text" \
69 1.1 jruoho "within the quotes. PR bin/33956."
70 1.1 jruoho }
71 1.1 jruoho dollar_at_with_text_body() {
72 1.6 christos
73 1.6 christos cat <<'EOF' > h-f1
74 1.6 christos
75 1.6 christos delim_argv() {
76 1.6 christos str=
77 1.6 christos while [ $# -gt 0 ]; do
78 1.6 christos if [ -z "${str}" ]; then
79 1.6 christos str=">$1<"
80 1.6 christos else
81 1.6 christos str="${str} >$1<"
82 1.6 christos fi
83 1.6 christos shift
84 1.6 christos done
85 1.6 christos echo "${str}"
86 1.6 christos }
87 1.6 christos
88 1.6 christos EOF
89 1.6 christos cat <<'EOF' > h-f2
90 1.6 christos
91 1.6 christos delim_argv() {
92 1.6 christos str=
93 1.6 christos while [ $# -gt 0 ]; do
94 1.6 christos
95 1.6 christos str="${str}${str:+ }>$1<"
96 1.6 christos shift
97 1.6 christos
98 1.6 christos done
99 1.6 christos echo "${str}"
100 1.6 christos }
101 1.6 christos
102 1.6 christos EOF
103 1.6 christos
104 1.6 christos chmod +x h-f1 h-f2
105 1.6 christos
106 1.6 christos for f in 1 2
107 1.6 christos do
108 1.6 christos atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
109 1.6 christos ". ./h-f${f}; "'set -- ; delim_argv "$@"'
110 1.6 christos atf_check -s exit:0 -o inline:'>foobar<\n' -e empty \
111 1.6 christos ${TEST_SH} -c \
112 1.6 christos ". ./h-f${f}; "'set -- ; delim_argv "foo$@bar"'
113 1.6 christos atf_check -s exit:0 -o inline:'>foo bar<\n' -e empty \
114 1.6 christos ${TEST_SH} -c \
115 1.6 christos ". ./h-f${f}; "'set -- ; delim_argv "foo $@ bar"'
116 1.6 christos
117 1.6 christos atf_check -s exit:0 -o inline:'>a< >b< >c<\n' -e empty \
118 1.6 christos ${TEST_SH} -c \
119 1.6 christos ". ./h-f${f}; "'set -- a b c; delim_argv "$@"'
120 1.6 christos atf_check -s exit:0 -o inline:'>fooa< >b< >cbar<\n' -e empty \
121 1.6 christos ${TEST_SH} -c \
122 1.6 christos ". ./h-f${f}; "'set -- a b c; delim_argv "foo$@bar"'
123 1.6 christos atf_check -s exit:0 -o inline:'>foo a< >b< >c bar<\n' -e empty \
124 1.6 christos ${TEST_SH} -c \
125 1.6 christos ". ./h-f${f}; "'set -- a b c; delim_argv "foo $@ bar"'
126 1.6 christos done
127 1.1 jruoho }
128 1.1 jruoho
129 1.1 jruoho atf_test_case strip
130 1.1 jruoho strip_head() {
131 1.1 jruoho atf_set "descr" "Checks that the %% operator works and strips" \
132 1.1 jruoho "the contents of a variable from the given point" \
133 1.6 christos "to the end"
134 1.1 jruoho }
135 1.1 jruoho strip_body() {
136 1.1 jruoho line='#define bindir "/usr/bin" /* comment */'
137 1.1 jruoho stripped='#define bindir "/usr/bin" '
138 1.6 christos
139 1.6 christos # atf_expect_fail "PR bin/43469" -- now fixed
140 1.6 christos for exp in \
141 1.6 christos '${line%%/\**}' \
142 1.6 christos '${line%%"/*"*}' \
143 1.6 christos '${line%%'"'"'/*'"'"'*}' \
144 1.6 christos '"${line%%/\**}"' \
145 1.6 christos '"${line%%"/*"*}"' \
146 1.6 christos '"${line%%'"'"'/*'"'"'*}"' \
147 1.6 christos '${line%/\**}' \
148 1.6 christos '${line%"/*"*}' \
149 1.6 christos '${line%'"'"'/*'"'"'*}' \
150 1.6 christos '"${line%/\**}"' \
151 1.6 christos '"${line%"/*"*}"' \
152 1.6 christos '"${line%'"'"'/*'"'"'*}"'
153 1.6 christos do
154 1.6 christos atf_check -o inline:":$stripped:\n" -e empty ${TEST_SH} -c \
155 1.6 christos "line='${line}'; echo :${exp}:"
156 1.6 christos done
157 1.1 jruoho }
158 1.1 jruoho
159 1.1 jruoho atf_test_case varpattern_backslashes
160 1.1 jruoho varpattern_backslashes_head() {
161 1.1 jruoho atf_set "descr" "Tests that protecting wildcards with backslashes" \
162 1.1 jruoho "works in variable patterns."
163 1.1 jruoho }
164 1.1 jruoho varpattern_backslashes_body() {
165 1.1 jruoho line='/foo/bar/*/baz'
166 1.1 jruoho stripped='/foo/bar/'
167 1.6 christos atf_check -o inline:'/foo/bar/\n' -e empty ${TEST_SH} -c \
168 1.6 christos 'line="/foo/bar/*/baz"; echo ${line%%\**}'
169 1.1 jruoho }
170 1.1 jruoho
171 1.1 jruoho atf_test_case arithmetic
172 1.1 jruoho arithmetic_head() {
173 1.1 jruoho atf_set "descr" "POSIX requires shell arithmetic to use signed" \
174 1.1 jruoho "long or a wider type. We use intmax_t, so at" \
175 1.1 jruoho "least 64 bits should be available. Make sure" \
176 1.1 jruoho "this is true."
177 1.1 jruoho }
178 1.1 jruoho arithmetic_body() {
179 1.6 christos
180 1.6 christos atf_check -o inline:'3' -e empty ${TEST_SH} -c \
181 1.6 christos 'printf %s $((1 + 2))'
182 1.6 christos atf_check -o inline:'2147483647' -e empty ${TEST_SH} -c \
183 1.6 christos 'printf %s $((0x7fffffff))'
184 1.6 christos atf_check -o inline:'9223372036854775807' -e empty ${TEST_SH} -c \
185 1.6 christos 'printf %s $(((1 << 63) - 1))'
186 1.1 jruoho }
187 1.1 jruoho
188 1.2 ast atf_test_case iteration_on_null_parameter
189 1.2 ast iteration_on_null_parameter_head() {
190 1.2 ast atf_set "descr" "Check iteration of \$@ in for loop when set to null;" \
191 1.2 ast "the error \"sh: @: parameter not set\" is incorrect." \
192 1.2 ast "PR bin/48202."
193 1.2 ast }
194 1.2 ast iteration_on_null_parameter_body() {
195 1.6 christos atf_check -o empty -e empty ${TEST_SH} -c \
196 1.6 christos 'N=; set -- ${N}; for X; do echo "[$X]"; done'
197 1.6 christos }
198 1.6 christos
199 1.6 christos atf_test_case iteration_on_quoted_null_parameter
200 1.6 christos iteration_on_quoted_null_parameter_head() {
201 1.6 christos atf_set "descr" \
202 1.6 christos 'Check iteration of "$@" in for loop when set to null;'
203 1.6 christos }
204 1.6 christos iteration_on_quoted_null_parameter_body() {
205 1.6 christos atf_check -o inline:'[]\n' -e empty ${TEST_SH} -c \
206 1.6 christos 'N=; set -- "${N}"; for X; do echo "[$X]"; done'
207 1.6 christos }
208 1.6 christos
209 1.6 christos atf_test_case iteration_on_null_or_null_parameter
210 1.6 christos iteration_on_null_or_null_parameter_head() {
211 1.6 christos atf_set "descr" \
212 1.6 christos 'Check expansion of null parameter as default for another null'
213 1.6 christos }
214 1.6 christos iteration_on_null_or_null_parameter_body() {
215 1.6 christos atf_check -o empty -e empty ${TEST_SH} -c \
216 1.6 christos 'N=; E=; set -- ${N:-${E}}; for X; do echo "[$X]"; done'
217 1.6 christos }
218 1.6 christos
219 1.6 christos atf_test_case iteration_on_null_or_missing_parameter
220 1.6 christos iteration_on_null_or_missing_parameter_head() {
221 1.6 christos atf_set "descr" \
222 1.6 christos 'Check expansion of missing parameter as default for another null'
223 1.6 christos }
224 1.6 christos iteration_on_null_or_missing_parameter_body() {
225 1.6 christos # atf_expect_fail 'PR bin/50834'
226 1.6 christos atf_check -o empty -e empty ${TEST_SH} -c \
227 1.6 christos 'N=; set -- ${N:-}; for X; do echo "[$X]"; done'
228 1.2 ast }
229 1.2 ast
230 1.1 jruoho atf_init_test_cases() {
231 1.1 jruoho atf_add_test_case dollar_at
232 1.1 jruoho atf_add_test_case dollar_at_with_text
233 1.1 jruoho atf_add_test_case strip
234 1.1 jruoho atf_add_test_case varpattern_backslashes
235 1.1 jruoho atf_add_test_case arithmetic
236 1.2 ast atf_add_test_case iteration_on_null_parameter
237 1.6 christos atf_add_test_case iteration_on_quoted_null_parameter
238 1.6 christos atf_add_test_case iteration_on_null_or_null_parameter
239 1.6 christos atf_add_test_case iteration_on_null_or_missing_parameter
240 1.1 jruoho }
241