t_expand.sh revision 1.21 1 1.21 kre # $NetBSD: t_expand.sh,v 1.21 2019/04/10 08:13:11 kre 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.21 kre 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.20 kre atf_test_case dollar_at_unquoted_or_conditional
67 1.20 kre dollar_at_unquoted_or_conditional_head() {
68 1.21 kre atf_set descr 'Sometime during 2013 the expansion of "${1+$@}"' \
69 1.20 kre ' (where $1 and $2 (and maybe more) are set)' \
70 1.20 kre ' seems to have broken. Check for this bug.'
71 1.20 kre }
72 1.20 kre dollar_at_unquoted_or_conditional_body() {
73 1.20 kre
74 1.20 kre atf_check -s exit:0 -o inline:'a\na\nb\nb\n' -e empty \
75 1.20 kre ${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n $@'
76 1.20 kre atf_check -s exit:0 -o inline:'a\na\nb\nb\n' -e empty \
77 1.20 kre ${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n ${1+$@}'
78 1.20 kre atf_check -s exit:0 -o inline:'a a\nb b\n' -e empty \
79 1.20 kre ${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n "$@"'
80 1.20 kre atf_check -s exit:0 -o inline:'a a\nb b\n' -e empty \
81 1.20 kre ${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n ${1+"$@"}'
82 1.20 kre
83 1.20 kre # This is the one that fails when the bug is present
84 1.20 kre atf_check -s exit:0 -o inline:'a a\nb b\n' -e empty \
85 1.20 kre ${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n "${1+$@}"'
86 1.20 kre }
87 1.20 kre
88 1.1 jruoho atf_test_case dollar_at_with_text
89 1.1 jruoho dollar_at_with_text_head() {
90 1.21 kre atf_set descr "Test \$@ expansion when it is surrounded by text" \
91 1.1 jruoho "within the quotes. PR bin/33956."
92 1.1 jruoho }
93 1.1 jruoho dollar_at_with_text_body() {
94 1.6 christos
95 1.6 christos cat <<'EOF' > h-f1
96 1.6 christos
97 1.6 christos delim_argv() {
98 1.6 christos str=
99 1.6 christos while [ $# -gt 0 ]; do
100 1.6 christos if [ -z "${str}" ]; then
101 1.6 christos str=">$1<"
102 1.6 christos else
103 1.6 christos str="${str} >$1<"
104 1.6 christos fi
105 1.6 christos shift
106 1.6 christos done
107 1.6 christos echo "${str}"
108 1.6 christos }
109 1.6 christos
110 1.6 christos EOF
111 1.6 christos cat <<'EOF' > h-f2
112 1.6 christos
113 1.6 christos delim_argv() {
114 1.6 christos str=
115 1.6 christos while [ $# -gt 0 ]; do
116 1.6 christos
117 1.6 christos str="${str}${str:+ }>$1<"
118 1.6 christos shift
119 1.6 christos
120 1.6 christos done
121 1.6 christos echo "${str}"
122 1.6 christos }
123 1.6 christos
124 1.6 christos EOF
125 1.6 christos
126 1.6 christos chmod +x h-f1 h-f2
127 1.6 christos
128 1.6 christos for f in 1 2
129 1.6 christos do
130 1.6 christos atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
131 1.20 kre ". ./h-f${f}; "'set -- ; delim_argv $@'
132 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
133 1.6 christos ". ./h-f${f}; "'set -- ; delim_argv "$@"'
134 1.6 christos atf_check -s exit:0 -o inline:'>foobar<\n' -e empty \
135 1.6 christos ${TEST_SH} -c \
136 1.6 christos ". ./h-f${f}; "'set -- ; delim_argv "foo$@bar"'
137 1.20 kre atf_check -s exit:0 -o inline:'>foobar<\n' -e empty \
138 1.20 kre ${TEST_SH} -c \
139 1.20 kre ". ./h-f${f}; "'set -- ; delim_argv foo"$@"bar'
140 1.6 christos atf_check -s exit:0 -o inline:'>foo bar<\n' -e empty \
141 1.6 christos ${TEST_SH} -c \
142 1.6 christos ". ./h-f${f}; "'set -- ; delim_argv "foo $@ bar"'
143 1.20 kre atf_check -s exit:0 -o inline:'>foo bar<\n' -e empty \
144 1.20 kre ${TEST_SH} -c \
145 1.20 kre ". ./h-f${f}; "'set -- ; delim_argv foo" $@ "bar'
146 1.6 christos
147 1.6 christos atf_check -s exit:0 -o inline:'>a< >b< >c<\n' -e empty \
148 1.6 christos ${TEST_SH} -c \
149 1.6 christos ". ./h-f${f}; "'set -- a b c; delim_argv "$@"'
150 1.20 kre
151 1.6 christos atf_check -s exit:0 -o inline:'>fooa< >b< >cbar<\n' -e empty \
152 1.6 christos ${TEST_SH} -c \
153 1.6 christos ". ./h-f${f}; "'set -- a b c; delim_argv "foo$@bar"'
154 1.20 kre
155 1.6 christos atf_check -s exit:0 -o inline:'>foo a< >b< >c bar<\n' -e empty \
156 1.6 christos ${TEST_SH} -c \
157 1.6 christos ". ./h-f${f}; "'set -- a b c; delim_argv "foo $@ bar"'
158 1.6 christos done
159 1.1 jruoho }
160 1.1 jruoho
161 1.20 kre atf_test_case dollar_at_empty_and_conditional
162 1.20 kre dollar_at_empty_and_conditional_head() {
163 1.21 kre atf_set descr 'Test $@ expansion when there are no args, and ' \
164 1.20 kre 'when conditionally expanded.'
165 1.20 kre }
166 1.20 kre dollar_at_empty_and_conditional_body() {
167 1.20 kre
168 1.20 kre # same task, implementation different from previous,
169 1.20 kre # that these work is also a test...
170 1.20 kre
171 1.20 kre cat <<'EOF' > h-f3
172 1.20 kre
173 1.20 kre delim_argv() {
174 1.20 kre str=
175 1.20 kre for Arg; do
176 1.20 kre str="${str:+${str} }>${Arg}<"
177 1.20 kre done
178 1.20 kre printf '%s\n' "${str}"
179 1.20 kre }
180 1.20 kre
181 1.20 kre EOF
182 1.20 kre
183 1.20 kre chmod +x h-f3
184 1.20 kre
185 1.20 kre # in these we give printf a first arg of "", which makes
186 1.20 kre # the first output char be \n, then the $@ produces anything else
187 1.20 kre # (we need to make sure we don't end up with:
188 1.20 kre # printf %s\\n
189 1.20 kre # -- that is, no operands for the %s, that's unspecified)
190 1.20 kre
191 1.20 kre atf_check -s exit:0 -o inline:'\na a\nb b\n' -e empty \
192 1.20 kre ${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n "" "$@"'
193 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty \
194 1.20 kre ${TEST_SH} -c 'set -- ; printf %s\\n "" "$@"'
195 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty \
196 1.20 kre ${TEST_SH} -c 'set -- ; printf %s\\n ""${1+"$@"}'
197 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty \
198 1.20 kre ${TEST_SH} -c 'set -- ; printf %s\\n """${1+$@}"'
199 1.20 kre
200 1.20 kre # in these we prefix (concat) the $@ expansion with "" to make
201 1.20 kre # sure there is always at least one arg for the %s in printf
202 1.20 kre # If there is anything else there, the prepended nothing vanishes
203 1.20 kre atf_check -s exit:0 -o inline:'a a\nb b\n' -e empty \
204 1.20 kre ${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n """$@"'
205 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty \
206 1.20 kre ${TEST_SH} -c 'set -- ; printf %s\\n """$@"'
207 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty \
208 1.20 kre ${TEST_SH} -c 'set -- ; printf %s\\n ""${1+"$@"}'
209 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty \
210 1.20 kre ${TEST_SH} -c 'set -- ; printf %s\\n """${1+$@}"'
211 1.20 kre
212 1.20 kre atf_check -s exit:0 -o inline:'>a< >b< >c<\n' -e empty ${TEST_SH} -c \
213 1.20 kre '. ./h-f3; set -- a b c; delim_argv "${1+$@}"'
214 1.20 kre atf_check -s exit:0 -o inline:'>a< >b< >c<\n' -e empty ${TEST_SH} -c \
215 1.20 kre '. ./h-f3; set -- a b c; delim_argv ${1+"$@"}'
216 1.20 kre atf_check -s exit:0 -o inline:'>fooa< >b< >cbar<\n' -e empty \
217 1.20 kre ${TEST_SH} -c \
218 1.20 kre '. ./h-f3; set -- a b c; delim_argv "foo${1+$@}bar"'
219 1.20 kre atf_check -s exit:0 -o inline:'>fooa< >b< >cbar<\n' -e empty \
220 1.20 kre ${TEST_SH} -c \
221 1.20 kre '. ./h-f3; set -- a b c; delim_argv foo${1+"$@"}bar'
222 1.20 kre atf_check -s exit:0 -o inline:'>foo a< >b< >c bar<\n' -e empty \
223 1.20 kre ${TEST_SH} -c \
224 1.20 kre '. ./h-f3; set -- a b c; delim_argv "foo ${1+$@} bar"'
225 1.20 kre atf_check -s exit:0 -o inline:'>foo a< >b< >c bar<\n' -e empty \
226 1.20 kre ${TEST_SH} -c \
227 1.20 kre '. ./h-f3; set -- a b c; delim_argv "foo "${1+"$@"}" bar"'
228 1.20 kre
229 1.20 kre # since $1 is not set, we get nothing ($@ is irrelevant)
230 1.20 kre # (note here we are not using printf, don't need to guarantee an arg)
231 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
232 1.20 kre '. ./h-f3; set -- ; delim_argv ${1+"$@"}'
233 1.20 kre
234 1.20 kre # here since $1 is not set we get "" as the special $@ properties
235 1.20 kre # do not apply, and ${foo+anything} generates nothing if foo is unset
236 1.20 kre atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
237 1.20 kre '. ./h-f3; set -- ; delim_argv "${1+$@}"'
238 1.20 kre
239 1.20 kre # in this one we get the initial "" followed by nothing
240 1.20 kre atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
241 1.20 kre '. ./h-f3; set -- ; delim_argv ""${1+"$@"}'
242 1.20 kre # which we verify by changing the "" to X, and including Y
243 1.20 kre atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
244 1.20 kre '. ./h-f3; set -- ; delim_argv X${1+"$@"Y}'
245 1.20 kre # and again, done differently (the ${1+...} produces nothing at all
246 1.20 kre atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
247 1.20 kre '. ./h-f3; set -- ; delim_argv X ${1+"$@"}'
248 1.20 kre # in these two we get the initial "" and then nothing
249 1.20 kre atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
250 1.20 kre '. ./h-f3; set -- ; delim_argv """${1+$@}"'
251 1.20 kre atf_check -s exit:0 -o inline:'>< ><\n' -e empty ${TEST_SH} -c \
252 1.20 kre '. ./h-f3; set -- ; delim_argv "" "${1+$@}"'
253 1.20 kre
254 1.20 kre # now we repeat all those with $1 set (so we eval the $@)
255 1.20 kre atf_check -s exit:0 -o inline:'>a<\n' -e empty ${TEST_SH} -c \
256 1.20 kre '. ./h-f3; set -- a ; delim_argv ""${1+"$@"}'
257 1.20 kre atf_check -s exit:0 -o inline:'>XaY<\n' -e empty ${TEST_SH} -c \
258 1.20 kre '. ./h-f3; set -- a ; delim_argv X${1+"$@"Y}'
259 1.20 kre atf_check -s exit:0 -o inline:'>X< >a<\n' -e empty ${TEST_SH} -c \
260 1.20 kre '. ./h-f3; set -- a ; delim_argv X ${1+"$@"}'
261 1.20 kre atf_check -s exit:0 -o inline:'>a<\n' -e empty ${TEST_SH} -c \
262 1.20 kre '. ./h-f3; set -- a ; delim_argv """${1+$@}"'
263 1.20 kre atf_check -s exit:0 -o inline:'>< >a<\n' -e empty ${TEST_SH} -c \
264 1.20 kre '. ./h-f3; set -- a ; delim_argv "" "${1+$@}"'
265 1.20 kre
266 1.20 kre # now we do all of those again, but testing $X instead of $1 (X set)
267 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
268 1.20 kre '. ./h-f3; X=1; set -- ; delim_argv ${X+"$@"}'
269 1.20 kre atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
270 1.20 kre '. ./h-f3; X=1; set -- ; delim_argv ""${X+"$@"}'
271 1.20 kre atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
272 1.20 kre '. ./h-f3; X=1; set -- ; delim_argv X${X+"$@"}'
273 1.20 kre atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
274 1.20 kre '. ./h-f3; X=1; set -- ; delim_argv X ${X+"$@"}'
275 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
276 1.20 kre '. ./h-f3; X=1; set -- ; delim_argv "${X+$@}"'
277 1.20 kre atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
278 1.20 kre '. ./h-f3; X=1; set -- ; delim_argv """${X+$@}"'
279 1.20 kre atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
280 1.20 kre '. ./h-f3; X=1; set -- ; delim_argv "" "${X+$@}"'
281 1.20 kre
282 1.20 kre atf_check -s exit:0 -o inline:'>a<\n' -e empty ${TEST_SH} -c \
283 1.20 kre '. ./h-f3; X=1; set -- a; delim_argv ${X+"$@"}'
284 1.20 kre atf_check -s exit:0 -o inline:'>a< >b<\n' -e empty ${TEST_SH} -c \
285 1.20 kre '. ./h-f3; X=1; set -- a b; delim_argv ${X+"$@"}'
286 1.20 kre atf_check -s exit:0 -o inline:'>a<\n' -e empty ${TEST_SH} -c \
287 1.20 kre '. ./h-f3; X=1; set -- a ; delim_argv ""${X+"$@"}'
288 1.20 kre atf_check -s exit:0 -o inline:'>Xa<\n' -e empty ${TEST_SH} -c \
289 1.20 kre '. ./h-f3; X=1; set -- a ; delim_argv X${X+"$@"}'
290 1.20 kre atf_check -s exit:0 -o inline:'>X< >a<\n' -e empty ${TEST_SH} -c \
291 1.20 kre '. ./h-f3; X=1; set -- a ; delim_argv X ${X+"$@"}'
292 1.20 kre atf_check -s exit:0 -o inline:'>a<\n' -e empty ${TEST_SH} -c \
293 1.20 kre '. ./h-f3; X=1; set -- a ; delim_argv """${X+$@}"'
294 1.20 kre atf_check -s exit:0 -o inline:'>a< >b<\n' -e empty ${TEST_SH} -c \
295 1.20 kre '. ./h-f3; X=1; set -- a b ; delim_argv """${X+$@}"'
296 1.20 kre atf_check -s exit:0 -o inline:'>< >a<\n' -e empty ${TEST_SH} -c \
297 1.20 kre '. ./h-f3; X=1; set -- a ; delim_argv "" "${X+$@}"'
298 1.20 kre atf_check -s exit:0 -o inline:'>< >a< >b<\n' -e empty ${TEST_SH} -c \
299 1.20 kre '. ./h-f3; X=1; set -- a b ; delim_argv "" "${X+$@}"'
300 1.20 kre
301 1.20 kre # and again, but testing $X where X is unset
302 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
303 1.20 kre '. ./h-f3; unset X; set -- ; delim_argv ${X+"$@"}'
304 1.20 kre atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
305 1.20 kre '. ./h-f3; unset X; set -- ; delim_argv ""${X+"$@"}'
306 1.20 kre atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
307 1.20 kre '. ./h-f3; unset X; set -- ; delim_argv X${X+"$@"}'
308 1.20 kre atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
309 1.20 kre '. ./h-f3; unset X; set -- ; delim_argv X ${X+"$@"}'
310 1.20 kre atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
311 1.20 kre '. ./h-f3; unset X; set -- ; delim_argv "${X+$@}"'
312 1.20 kre atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
313 1.20 kre '. ./h-f3; unset X; set -- ; delim_argv """${X+$@}"'
314 1.20 kre atf_check -s exit:0 -o inline:'>< ><\n' -e empty ${TEST_SH} -c \
315 1.20 kre '. ./h-f3; unset X; set -- ; delim_argv "" "${X+$@}"'
316 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
317 1.20 kre '. ./h-f3; unset X; set -- a; delim_argv ${X+"$@"}'
318 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
319 1.20 kre '. ./h-f3; unset X; set -- a b; delim_argv ${X+"$@"}'
320 1.20 kre atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
321 1.20 kre '. ./h-f3; unset X; set -- a ; delim_argv ""${X+"$@"}'
322 1.20 kre atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
323 1.20 kre '. ./h-f3; unset X; set -- a ; delim_argv X${X+"$@"}'
324 1.20 kre atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
325 1.20 kre '. ./h-f3; unset X; set -- a ; delim_argv X ${X+"$@"}'
326 1.20 kre atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
327 1.20 kre '. ./h-f3; unset X; set -- a ; delim_argv """${X+$@}"'
328 1.20 kre atf_check -s exit:0 -o inline:'>< ><\n' -e empty ${TEST_SH} -c \
329 1.20 kre '. ./h-f3; unset X; set -- a; delim_argv "" "${X+$@}"'
330 1.20 kre
331 1.20 kre # a few that stretch belief...
332 1.20 kre
333 1.20 kre atf_check -s exit:0 -o inline:'>a< >b<\n' -e empty ${TEST_SH} -c \
334 1.20 kre '. ./h-f3; X=1; set -- a b ; delim_argv ${X+${1+"$@"}}'
335 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
336 1.20 kre '. ./h-f3; X=1; set -- ; delim_argv ${X+${1+"$@"}}'
337 1.20 kre atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
338 1.20 kre '. ./h-f3; unset X; set -- a b ; delim_argv ${X+${1+"$@"}}'
339 1.20 kre
340 1.20 kre # and now for something completely different
341 1.20 kre
342 1.20 kre atf_check -s exit:0 -o inline:'>a< >b< >ca< >b< >c<\n' -e empty \
343 1.20 kre ${TEST_SH} -c '. ./h-f3; set -- a b c; delim_argv "$@$@"'
344 1.20 kre atf_check -s exit:0 -o inline:'>a< >b< >ca b c<\n' -e empty \
345 1.20 kre ${TEST_SH} -c '. ./h-f3; set -- a b c; delim_argv "$@$*"'
346 1.20 kre atf_check -s exit:0 -o inline:'>a b ca< >b< >c<\n' -e empty \
347 1.20 kre ${TEST_SH} -c '. ./h-f3; set -- a b c; delim_argv "$*$@"'
348 1.20 kre atf_check -s exit:0 -o inline:'>a a++b + ca a< >< >b < > c<\n' \
349 1.20 kre -e empty ${TEST_SH} -c \
350 1.20 kre '. ./h-f3; set -- "a a" "" "b " " c"; IFS=+; delim_argv "$*$@"'
351 1.20 kre atf_check -s exit:0 -o inline:'>a< >a< >< >b < > ca+a< >< >b < > c<\n' \
352 1.20 kre -e empty ${TEST_SH} -c \
353 1.20 kre '. ./h-f3; set -- "a+a" "" "b " " c"; IFS=+; delim_argv $*"$@"'
354 1.20 kre }
355 1.20 kre
356 1.1 jruoho atf_test_case strip
357 1.1 jruoho strip_head() {
358 1.21 kre atf_set descr "Checks that the %% operator works and strips" \
359 1.1 jruoho "the contents of a variable from the given point" \
360 1.6 christos "to the end"
361 1.1 jruoho }
362 1.1 jruoho strip_body() {
363 1.1 jruoho line='#define bindir "/usr/bin" /* comment */'
364 1.1 jruoho stripped='#define bindir "/usr/bin" '
365 1.6 christos
366 1.6 christos # atf_expect_fail "PR bin/43469" -- now fixed
367 1.6 christos for exp in \
368 1.6 christos '${line%%/\**}' \
369 1.6 christos '${line%%"/*"*}' \
370 1.6 christos '${line%%'"'"'/*'"'"'*}' \
371 1.6 christos '"${line%%/\**}"' \
372 1.6 christos '"${line%%"/*"*}"' \
373 1.6 christos '"${line%%'"'"'/*'"'"'*}"' \
374 1.6 christos '${line%/\**}' \
375 1.6 christos '${line%"/*"*}' \
376 1.6 christos '${line%'"'"'/*'"'"'*}' \
377 1.6 christos '"${line%/\**}"' \
378 1.6 christos '"${line%"/*"*}"' \
379 1.6 christos '"${line%'"'"'/*'"'"'*}"'
380 1.6 christos do
381 1.6 christos atf_check -o inline:":$stripped:\n" -e empty ${TEST_SH} -c \
382 1.6 christos "line='${line}'; echo :${exp}:"
383 1.6 christos done
384 1.1 jruoho }
385 1.1 jruoho
386 1.14 kre atf_test_case wrap_strip
387 1.14 kre wrap_strip_head() {
388 1.21 kre atf_set descr "Checks that the %% operator works and strips" \
389 1.14 kre "the contents of a variable from the given point" \
390 1.14 kre 'to the end, and that \ \n sequences do not break it'
391 1.14 kre }
392 1.14 kre wrap_strip_body() {
393 1.14 kre line='#define bindir "/usr/bin" /* comment */'
394 1.14 kre stripped='#define bindir "/usr/bin" '
395 1.14 kre
396 1.14 kre for exp in \
397 1.14 kre '${line\
398 1.14 kre %%/\**}' \
399 1.14 kre '${line%%"/\
400 1.14 kre *"*}' \
401 1.14 kre '${line%%'"'"'/*'"'"'\
402 1.14 kre *}' \
403 1.14 kre '"${li\
404 1.14 kre ne%%/\**}"' \
405 1.14 kre '"${line%%"\
406 1.14 kre /*"*}"' \
407 1.14 kre '"${line%\
408 1.14 kre %'"'"'/*'"'"'*}"' \
409 1.14 kre '${line\
410 1.14 kre %\
411 1.14 kre /\*\
412 1.14 kre *\
413 1.14 kre }' \
414 1.14 kre '${line%"/*\
415 1.14 kre "*\
416 1.14 kre }' \
417 1.14 kre '${line\
418 1.14 kre %\
419 1.14 kre '"'"'/*'"'"'*}' \
420 1.14 kre '"$\
421 1.14 kre {li\
422 1.14 kre ne%\
423 1.14 kre '"'"'/*'"'"'*}"'
424 1.14 kre do
425 1.14 kre atf_check -o inline:":$stripped:\n" -e empty ${TEST_SH} -c \
426 1.14 kre "line='${line}'; echo :${exp}:"
427 1.14 kre done
428 1.14 kre }
429 1.14 kre
430 1.18 kre atf_test_case tilde
431 1.18 kre tilde_head() {
432 1.21 kre atf_set descr "Checks that the ~ expansions work"
433 1.18 kre }
434 1.18 kre tilde_body() {
435 1.18 kre for HOME in '' / /home/foo \
436 1.18 kre /a/very/long/home/directory/path/that/might/push/the/tilde/expansion/code/beyond/what/it/would/normally/ever/see/on/any/sane/system/and/perhaps/expose/some/bugs
437 1.18 kre do
438 1.18 kre export HOME
439 1.18 kre
440 1.18 kre atf_check -s exit:0 -e empty \
441 1.18 kre -o inline:'HOME\t'"${HOME}"'
442 1.18 kre ~\t'"${HOME}"'
443 1.18 kre ~/foobar\t'"${HOME}"'/foobar
444 1.18 kre "$V"\t'"${HOME}"'
445 1.18 kre "$X"\t~
446 1.18 kre "$Y"\t'"${HOME}"':'"${HOME}"'
447 1.18 kre "$YY"\t'"${HOME}"'/foo:'"${HOME}"'/bar
448 1.18 kre "$Z"\t'"${HOME}"'/~
449 1.18 kre ${U:-~}\t'"${HOME}"'
450 1.18 kre $V\t'"${HOME}"'
451 1.18 kre $X\t~
452 1.18 kre $Y\t'"${HOME}"':'"${HOME}"'
453 1.18 kre $YY\t'"${HOME}"'/foo:'"${HOME}"'/bar
454 1.18 kre $Z\t'"${HOME}"'/~
455 1.18 kre ${U:=~}\t'"${HOME}"'
456 1.18 kre ${UU:=~:~}\t'"${HOME}"':'"${HOME}"'
457 1.18 kre ${UUU:=~/:~}\t'"${HOME}"'/:'"${HOME}"'
458 1.18 kre ${U4:=~/:~/}\t'"${HOME}"'/:'"${HOME}"'/\n' \
459 1.18 kre ${TEST_SH} -s <<- \EOF
460 1.18 kre unset -v U UU UUU U4
461 1.18 kre V=~
462 1.18 kre X="~"
463 1.18 kre Y=~:~ YY=~/foo:~/bar
464 1.18 kre Z=~/~
465 1.18 kre printf '%s\t%s\n' \
466 1.18 kre 'HOME' "${HOME}" \
467 1.18 kre '~' ~ \
468 1.18 kre '~/foobar' ~/foobar \
469 1.18 kre '"$V"' "$V" \
470 1.18 kre '"$X"' "$X" \
471 1.18 kre '"$Y"' "$Y" \
472 1.18 kre '"$YY"' "$YY" \
473 1.18 kre '"$Z"' "$Z" \
474 1.18 kre '${U:-~}' ''${U:-~} \
475 1.18 kre '$V' ''$V \
476 1.18 kre '$X' ''$X \
477 1.18 kre '$Y' ''$Y \
478 1.18 kre '$YY' ''$YY \
479 1.18 kre '$Z' ''$Z \
480 1.18 kre '${U:=~}' ''${U:=~} \
481 1.18 kre '${UU:=~:~}' ''${UU:=~:~} \
482 1.18 kre '${UUU:=~/:~}' ''${UUU:=~/:~} \
483 1.18 kre '${U4:=~/:~/}' ''${U4:=~/:~/}
484 1.18 kre EOF
485 1.18 kre done
486 1.18 kre
487 1.18 kre # Testing ~user is harder, so, perhaps later...
488 1.18 kre }
489 1.18 kre
490 1.1 jruoho atf_test_case varpattern_backslashes
491 1.1 jruoho varpattern_backslashes_head() {
492 1.21 kre atf_set descr "Tests that protecting wildcards with backslashes" \
493 1.1 jruoho "works in variable patterns."
494 1.1 jruoho }
495 1.1 jruoho varpattern_backslashes_body() {
496 1.1 jruoho line='/foo/bar/*/baz'
497 1.1 jruoho stripped='/foo/bar/'
498 1.6 christos atf_check -o inline:'/foo/bar/\n' -e empty ${TEST_SH} -c \
499 1.6 christos 'line="/foo/bar/*/baz"; echo ${line%%\**}'
500 1.1 jruoho }
501 1.1 jruoho
502 1.1 jruoho atf_test_case arithmetic
503 1.1 jruoho arithmetic_head() {
504 1.21 kre atf_set descr "POSIX requires shell arithmetic to use signed" \
505 1.1 jruoho "long or a wider type. We use intmax_t, so at" \
506 1.1 jruoho "least 64 bits should be available. Make sure" \
507 1.1 jruoho "this is true."
508 1.1 jruoho }
509 1.1 jruoho arithmetic_body() {
510 1.6 christos
511 1.6 christos atf_check -o inline:'3' -e empty ${TEST_SH} -c \
512 1.6 christos 'printf %s $((1 + 2))'
513 1.6 christos atf_check -o inline:'2147483647' -e empty ${TEST_SH} -c \
514 1.6 christos 'printf %s $((0x7fffffff))'
515 1.6 christos atf_check -o inline:'9223372036854775807' -e empty ${TEST_SH} -c \
516 1.6 christos 'printf %s $(((1 << 63) - 1))'
517 1.1 jruoho }
518 1.1 jruoho
519 1.2 ast atf_test_case iteration_on_null_parameter
520 1.2 ast iteration_on_null_parameter_head() {
521 1.21 kre atf_set descr "Check iteration of \$@ in for loop when set to null;" \
522 1.2 ast "the error \"sh: @: parameter not set\" is incorrect." \
523 1.2 ast "PR bin/48202."
524 1.2 ast }
525 1.2 ast iteration_on_null_parameter_body() {
526 1.6 christos atf_check -o empty -e empty ${TEST_SH} -c \
527 1.6 christos 'N=; set -- ${N}; for X; do echo "[$X]"; done'
528 1.6 christos }
529 1.6 christos
530 1.6 christos atf_test_case iteration_on_quoted_null_parameter
531 1.6 christos iteration_on_quoted_null_parameter_head() {
532 1.21 kre atf_set descr \
533 1.6 christos 'Check iteration of "$@" in for loop when set to null;'
534 1.6 christos }
535 1.6 christos iteration_on_quoted_null_parameter_body() {
536 1.6 christos atf_check -o inline:'[]\n' -e empty ${TEST_SH} -c \
537 1.6 christos 'N=; set -- "${N}"; for X; do echo "[$X]"; done'
538 1.6 christos }
539 1.6 christos
540 1.6 christos atf_test_case iteration_on_null_or_null_parameter
541 1.6 christos iteration_on_null_or_null_parameter_head() {
542 1.21 kre atf_set descr \
543 1.6 christos 'Check expansion of null parameter as default for another null'
544 1.6 christos }
545 1.6 christos iteration_on_null_or_null_parameter_body() {
546 1.6 christos atf_check -o empty -e empty ${TEST_SH} -c \
547 1.6 christos 'N=; E=; set -- ${N:-${E}}; for X; do echo "[$X]"; done'
548 1.6 christos }
549 1.6 christos
550 1.6 christos atf_test_case iteration_on_null_or_missing_parameter
551 1.6 christos iteration_on_null_or_missing_parameter_head() {
552 1.21 kre atf_set descr \
553 1.6 christos 'Check expansion of missing parameter as default for another null'
554 1.6 christos }
555 1.6 christos iteration_on_null_or_missing_parameter_body() {
556 1.6 christos # atf_expect_fail 'PR bin/50834'
557 1.6 christos atf_check -o empty -e empty ${TEST_SH} -c \
558 1.6 christos 'N=; set -- ${N:-}; for X; do echo "[$X]"; done'
559 1.2 ast }
560 1.2 ast
561 1.10 kre ####### The remaining tests use the following helper functions ...
562 1.10 kre
563 1.7 christos nl='
564 1.7 christos '
565 1.7 christos reset()
566 1.7 christos {
567 1.7 christos TEST_NUM=0
568 1.7 christos TEST_FAILURES=''
569 1.7 christos TEST_FAIL_COUNT=0
570 1.7 christos TEST_ID="$1"
571 1.7 christos }
572 1.7 christos
573 1.7 christos check()
574 1.7 christos {
575 1.7 christos fail=false
576 1.7 christos TEMP_FILE=$( mktemp OUT.XXXXXX )
577 1.7 christos TEST_NUM=$(( $TEST_NUM + 1 ))
578 1.7 christos MSG=
579 1.7 christos
580 1.7 christos # our local shell (ATF_SHELL) better do quoting correctly...
581 1.7 christos # some of the tests expect us to expand $nl internally...
582 1.7 christos CMD="$1"
583 1.7 christos
584 1.7 christos result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )"
585 1.7 christos STATUS=$?
586 1.7 christos
587 1.7 christos if [ "${STATUS}" -ne "$3" ]; then
588 1.7 christos MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
589 1.7 christos MSG="${MSG} expected exit code $3, got ${STATUS}"
590 1.7 christos
591 1.7 christos # don't actually fail just because of wrong exit code
592 1.7 christos # unless we either expected, or received "good"
593 1.13 kre # or something else is detected as incorrect as well.
594 1.7 christos case "$3/${STATUS}" in
595 1.7 christos (*/0|0/*) fail=true;;
596 1.7 christos esac
597 1.7 christos fi
598 1.7 christos
599 1.7 christos if [ "$3" -eq 0 ]; then
600 1.7 christos if [ -s "${TEMP_FILE}" ]; then
601 1.7 christos MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
602 1.7 christos MSG="${MSG} Messages produced on stderr unexpected..."
603 1.7 christos MSG="${MSG}${nl}$( cat "${TEMP_FILE}" )"
604 1.7 christos fail=true
605 1.7 christos fi
606 1.7 christos else
607 1.7 christos if ! [ -s "${TEMP_FILE}" ]; then
608 1.7 christos MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
609 1.7 christos MSG="${MSG} Expected messages on stderr,"
610 1.7 christos MSG="${MSG} nothing produced"
611 1.7 christos fail=true
612 1.7 christos fi
613 1.7 christos fi
614 1.7 christos rm -f "${TEMP_FILE}"
615 1.7 christos
616 1.7 christos # Remove newlines (use local shell for this)
617 1.7 christos oifs="$IFS"
618 1.7 christos IFS="$nl"
619 1.7 christos result="$(echo $result)"
620 1.7 christos IFS="$oifs"
621 1.7 christos if [ "$2" != "$result" ]
622 1.7 christos then
623 1.7 christos MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
624 1.7 christos MSG="${MSG} Expected output '$2', received '$result'"
625 1.7 christos fail=true
626 1.7 christos fi
627 1.7 christos
628 1.7 christos if $fail
629 1.7 christos then
630 1.7 christos MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
631 1.7 christos MSG="${MSG} Full command: <<${CMD}>>"
632 1.7 christos fi
633 1.7 christos
634 1.7 christos $fail && test -n "$TEST_ID" && {
635 1.7 christos TEST_FAILURES="${TEST_FAILURES}${TEST_FAILURES:+${nl}}"
636 1.7 christos TEST_FAILURES="${TEST_FAILURES}${TEST_ID}[$TEST_NUM]:"
637 1.7 christos TEST_FAILURES="${TEST_FAILURES} Test of '$1' failed.";
638 1.7 christos TEST_FAILURES="${TEST_FAILURES}${nl}${MSG}"
639 1.7 christos TEST_FAIL_COUNT=$(( $TEST_FAIL_COUNT + 1 ))
640 1.7 christos return 0
641 1.7 christos }
642 1.13 kre $fail && atf_fail "Test[$TEST_NUM] failed: $(
643 1.13 kre # ATF does not like newlines in messages, so change them...
644 1.13 kre printf '%s' "${MSG}" | tr '\n' ';'
645 1.13 kre )"
646 1.7 christos return 0
647 1.7 christos }
648 1.7 christos
649 1.7 christos results()
650 1.7 christos {
651 1.10 kre test -n "$1" && atf_expect_fail "$1"
652 1.10 kre
653 1.7 christos test -z "${TEST_ID}" && return 0
654 1.7 christos test -z "${TEST_FAILURES}" && return 0
655 1.7 christos
656 1.7 christos echo >&2 "=========================================="
657 1.7 christos echo >&2 "While testing '${TEST_ID}'"
658 1.7 christos echo >&2 " - - - - - - - - - - - - - - - - -"
659 1.7 christos echo >&2 "${TEST_FAILURES}"
660 1.10 kre
661 1.7 christos atf_fail \
662 1.9 kre "Test ${TEST_ID}: $TEST_FAIL_COUNT (of $TEST_NUM) subtests failed - see stderr"
663 1.7 christos }
664 1.7 christos
665 1.10 kre ####### End helpers
666 1.10 kre
667 1.7 christos atf_test_case shell_params
668 1.7 christos shell_params_head() {
669 1.21 kre atf_set descr "Test correct operation of the numeric parameters"
670 1.7 christos }
671 1.7 christos shell_params_body() {
672 1.7 christos atf_require_prog mktemp
673 1.7 christos
674 1.7 christos reset shell_params
675 1.7 christos
676 1.7 christos check 'set -- a b c; echo "$#: $1 $2 $3"' '3: a b c' 0
677 1.7 christos check 'set -- a b c d e f g h i j k l m; echo "$#: ${1}0 ${10} $10"' \
678 1.7 christos '13: a0 j a0' 0
679 1.7 christos check 'x="$0"; set -- a b; y="$0";
680 1.7 christos [ "x${x}y" = "x${y}y" ] && echo OK || echo x="$x" y="$y"' \
681 1.7 christos 'OK' 0
682 1.7 christos check "${TEST_SH} -c 'echo 0=\$0 1=\$1 2=\$2' a b c" '0=a 1=b 2=c' 0
683 1.7 christos
684 1.7 christos echo 'echo 0="$0" 1="$1" 2="$2"' > helper.sh
685 1.8 christos check "${TEST_SH} helper.sh a b c" '0=helper.sh 1=a 2=b' 0
686 1.7 christos
687 1.7 christos check 'set -- a bb ccc dddd eeeee ffffff ggggggg hhhhhhhh \
688 1.7 christos iiiiiiiii jjjjjjjjjj kkkkkkkkkkk
689 1.7 christos echo "${#}: ${#1} ${#2} ${#3} ${#4} ... ${#9} ${#10} ${#11}"' \
690 1.7 christos '11: 1 2 3 4 ... 9 10 11' 0
691 1.7 christos
692 1.7 christos check 'set -- a b c; echo "$#: ${1-A} ${2-B} ${3-C} ${4-D} ${5-E}"' \
693 1.7 christos '3: a b c D E' 0
694 1.7 christos check 'set -- a "" c "" e
695 1.7 christos echo "$#: ${1:-A} ${2:-B} ${3:-C} ${4:-D} ${5:-E}"' \
696 1.7 christos '5: a B c D e' 0
697 1.7 christos check 'set -- a "" c "" e
698 1.7 christos echo "$#: ${1:+A} ${2:+B} ${3:+C} ${4:+D} ${5:+E}"' \
699 1.7 christos '5: A C E' 0
700 1.7 christos check 'set -- "abab*cbb"
701 1.7 christos echo "${1} ${1#a} ${1%b} ${1##ab} ${1%%b} ${1#*\*} ${1%\**}"' \
702 1.7 christos 'abab*cbb bab*cbb abab*cb ab*cbb abab*cb cbb abab' 0
703 1.7 christos check 'set -- "abab?cbb"
704 1.7 christos echo "${1}:${1#*a}+${1%b*}-${1##*a}_${1%%b*}%${1#[ab]}=${1%?*}/${1%\?*}"' \
705 1.7 christos 'abab?cbb:bab?cbb+abab?cb-b?cbb_a%bab?cbb=abab?cb/abab' 0
706 1.7 christos check 'set -- a "" c "" e; echo "${2:=b}"' '' 1
707 1.7 christos
708 1.15 kre check 'set -- a b c d; echo ${4294967297}' '' 0 # result 'a' => ${1}
709 1.15 kre check 'set -- a b c; echo ${01}' 'a' 0
710 1.15 kre check "${TEST_SH} -c 'echo 0=\${00} 1=\${01} 2=\${02}' a b c" \
711 1.15 kre '0=a 1=b 2=c' 0
712 1.15 kre
713 1.16 kre # by special request, for PaulG... (${0...} is not octal!)
714 1.17 kre
715 1.17 kre # Posix XCU 2.5.1 (Issue 7 TC2 pg 2349 lines 74835..6):
716 1.17 kre # The digits denoting the positional parameters shall always
717 1.17 kre # be interpreted as a decimal value, even if there is a leading zero.
718 1.17 kre
719 1.16 kre check \
720 1.16 kre 'set -- a b c d e f g h i j k l m; echo "$#: ${08} ${010} ${011}"' \
721 1.16 kre '13: h j k' 0
722 1.16 kre
723 1.7 christos results
724 1.7 christos }
725 1.7 christos
726 1.9 kre atf_test_case var_with_embedded_cmdsub
727 1.9 kre var_with_embedded_cmdsub_head() {
728 1.21 kre atf_set descr "Test expansion of vars with embedded cmdsub"
729 1.9 kre }
730 1.9 kre var_with_embedded_cmdsub_body() {
731 1.9 kre
732 1.9 kre reset var_with_embedded_cmdsub
733 1.9 kre
734 1.9 kre check 'unset x; echo ${x-$(echo a)}$(echo b)' 'ab' 0 #1
735 1.9 kre check 'unset x; echo ${x:-$(echo a)}$(echo b)' 'ab' 0 #2
736 1.9 kre check 'x=""; echo ${x-$(echo a)}$(echo b)' 'b' 0 #3
737 1.9 kre check 'x=""; echo ${x:-$(echo a)}$(echo b)' 'ab' 0 #4
738 1.9 kre check 'x=c; echo ${x-$(echo a)}$(echo b)' 'cb' 0 #5
739 1.9 kre check 'x=c; echo ${x:-$(echo a)}$(echo b)' 'cb' 0 #6
740 1.9 kre
741 1.9 kre check 'unset x; echo ${x+$(echo a)}$(echo b)' 'b' 0 #7
742 1.9 kre check 'unset x; echo ${x:+$(echo a)}$(echo b)' 'b' 0 #8
743 1.9 kre check 'x=""; echo ${x+$(echo a)}$(echo b)' 'ab' 0 #9
744 1.9 kre check 'x=""; echo ${x:+$(echo a)}$(echo b)' 'b' 0 #10
745 1.9 kre check 'x=c; echo ${x+$(echo a)}$(echo b)' 'ab' 0 #11
746 1.9 kre check 'x=c; echo ${x:+$(echo a)}$(echo b)' 'ab' 0 #12
747 1.9 kre
748 1.9 kre check 'unset x; echo ${x=$(echo a)}$(echo b)' 'ab' 0 #13
749 1.9 kre check 'unset x; echo ${x:=$(echo a)}$(echo b)' 'ab' 0 #14
750 1.9 kre check 'x=""; echo ${x=$(echo a)}$(echo b)' 'b' 0 #15
751 1.9 kre check 'x=""; echo ${x:=$(echo a)}$(echo b)' 'ab' 0 #16
752 1.9 kre check 'x=c; echo ${x=$(echo a)}$(echo b)' 'cb' 0 #17
753 1.9 kre check 'x=c; echo ${x:=$(echo a)}$(echo b)' 'cb' 0 #18
754 1.9 kre
755 1.9 kre check 'unset x; echo ${x?$(echo a)}$(echo b)' '' 2 #19
756 1.9 kre check 'unset x; echo ${x:?$(echo a)}$(echo b)' '' 2 #20
757 1.9 kre check 'x=""; echo ${x?$(echo a)}$(echo b)' 'b' 0 #21
758 1.9 kre check 'x=""; echo ${x:?$(echo a)}$(echo b)' '' 2 #22
759 1.9 kre check 'x=c; echo ${x?$(echo a)}$(echo b)' 'cb' 0 #23
760 1.9 kre check 'x=c; echo ${x:?$(echo a)}$(echo b)' 'cb' 0 #24
761 1.9 kre
762 1.9 kre check 'unset x; echo ${x%$(echo a)}$(echo b)' 'b' 0 #25
763 1.9 kre check 'unset x; echo ${x%%$(echo a)}$(echo b)' 'b' 0 #26
764 1.9 kre check 'x=""; echo ${x%$(echo a)}$(echo b)' 'b' 0 #27
765 1.9 kre check 'x=""; echo ${x%%$(echo a)}$(echo b)' 'b' 0 #28
766 1.9 kre check 'x=c; echo ${x%$(echo a)}$(echo b)' 'cb' 0 #29
767 1.9 kre check 'x=c; echo ${x%%$(echo a)}$(echo b)' 'cb' 0 #30
768 1.9 kre check 'x=aa; echo ${x%$(echo "*a")}$(echo b)' 'ab' 0 #31
769 1.9 kre check 'x=aa; echo ${x%%$(echo "*a")}$(echo b)' 'b' 0 #32
770 1.9 kre
771 1.9 kre check 'unset x; echo ${x#$(echo a)}$(echo b)' 'b' 0 #33
772 1.9 kre check 'unset x; echo ${x##$(echo a)}$(echo b)' 'b' 0 #34
773 1.9 kre check 'x=""; echo ${x#$(echo a)}$(echo b)' 'b' 0 #35
774 1.9 kre check 'x=""; echo ${x##$(echo a)}$(echo b)' 'b' 0 #36
775 1.9 kre check 'x=c; echo ${x#$(echo a)}$(echo b)' 'cb' 0 #37
776 1.9 kre check 'x=c; echo ${x##$(echo a)}$(echo b)' 'cb' 0 #38
777 1.9 kre check 'x=aa; echo ${x#$(echo "*a")}$(echo b)' 'ab' 0 #39
778 1.9 kre check 'x=aa; echo ${x##$(echo "*a")}$(echo b)' 'b' 0 #40
779 1.9 kre
780 1.9 kre results
781 1.9 kre }
782 1.9 kre
783 1.12 kre atf_test_case dollar_hash
784 1.12 kre dollar_hash_head() {
785 1.21 kre atf_set descr 'Test expansion of various aspects of $#'
786 1.12 kre }
787 1.12 kre dollar_hash_body() {
788 1.12 kre
789 1.12 kre #
790 1.12 kre # $# looks like it should be so simple that it doesn't really
791 1.12 kre # need a test of its own, and used in that way, it really doesn't.
792 1.12 kre # But when we add braces ${#} we need to deal with the three
793 1.12 kre # (almost 4) different meanings of a # inside a ${} expansion...
794 1.13 kre #
795 1.13 kre # Note that some of these are just how we treat expansions that
796 1.13 kre # are unspecified by posix (as noted below.)
797 1.12 kre #
798 1.12 kre # 1. ${#} is just $# (number of params)
799 1.12 kre # 1.a ${\#} is nothing at all (error: invalid expansion)
800 1.12 kre # 1.b ${\#...} (anything after) is the same (invalid)
801 1.12 kre # 2. ${#VAR} is the length of the value VAR
802 1.12 kre # 2.a Including ${##} - the length of ${#}
803 1.12 kre # 3 ${VAR#pat} is the value of VAR with leading pat removed
804 1.12 kre # 3.a Including ${VAR#} which just removes leading nothing
805 1.12 kre # This is relevant in case of ${VAR#${X}} with X=''
806 1.13 kre # nb: not required by posix, see XCU 2.6.2
807 1.12 kre # 3.b ${##} is not a case of 3.a but rather 2.a
808 1.12 kre # 3.c Yet ${##pat} is a case of 3.a
809 1.12 kre # Including ${##${X}} where X='' or X='#'
810 1.13 kre # nb: not required by posix, see XCU 2.6.2
811 1.12 kre # 3.d And ${#\#} is invalid (error)
812 1.12 kre # 3.e But ${##\#} removes a leading # from the value of $#
813 1.12 kre # (so is just $# as there is no leading # there)
814 1.13 kre # nb: not required by posix, see XCU 2.6.2
815 1.12 kre # 4 ${VAR##pat} is the value of VAR with longest pat removed
816 1.12 kre # 4.a Including ${VAR##} which removes the longest nothing
817 1.12 kre # 4.b Which in this case includes ${###} (so is == $#)
818 1.13 kre # nb: not required by posix, see XCU 2.6.2
819 1.12 kre # 4.c But not ${##\#} which is $# with a leading '#' removed
820 1.12 kre # (and so is also == $#), i.e.: like ${###} but different.
821 1.13 kre # nb: not required by posix, see XCU 2.6.2
822 1.12 kre # 4.d As is ${###\#} or just ${####} - remove # (so just $#)
823 1.13 kre # nb: not required by posix, see XCU 2.6.2
824 1.12 kre #
825 1.12 kre
826 1.12 kre reset dollar_hash
827 1.12 kre
828 1.12 kre check 'set -- ; echo $#' '0' 0 # 1
829 1.12 kre check 'set -- a b c; echo $#' '3' 0 # 2
830 1.12 kre check 'set -- a b c d e f g h i j; echo $#' '10' 0 # 3
831 1.12 kre # rule 1
832 1.12 kre check 'set -- ; echo ${#}' '0' 0 # 4
833 1.12 kre check 'set -- a b c; echo ${#}' '3' 0 # 5
834 1.12 kre check 'set -- a b c d e f g h i j; echo ${#}' '10' 0 # 6
835 1.12 kre # rule 1.a
836 1.12 kre check 'set -- a b c; echo ${\#}' '' 2 # 7
837 1.12 kre # rule 1.b
838 1.12 kre check 'set -- a b c; echo ${\#:-foo}' '' 2 # 8
839 1.12 kre # rule 2
840 1.12 kre check 'VAR=12345; echo ${#VAR}' '5' 0 # 9
841 1.12 kre check 'VAR=123456789012; echo ${#VAR}' '12' 0 #10
842 1.12 kre # rule 2.a
843 1.12 kre check 'set -- ; echo ${##}' '1' 0 #11
844 1.12 kre check 'set -- a b c; echo ${##}' '1' 0 #12
845 1.12 kre check 'set -- a b c d e f g h i j; echo ${##}' '2' 0 #13
846 1.12 kre # rule 3
847 1.12 kre check 'VAR=12345; echo ${VAR#1}' '2345' 0 #14
848 1.12 kre check 'VAR=12345; echo ${VAR#2}' '12345' 0 #15
849 1.12 kre check 'VAR=#2345; echo ${VAR#\#}' '2345' 0 #16
850 1.12 kre check 'X=1; VAR=12345; echo ${VAR#${X}}' '2345' 0 #17
851 1.12 kre check 'X=1; VAR=#2345; echo ${VAR#${X}}' '#2345' 0 #18
852 1.12 kre # rule 3.a
853 1.12 kre check 'VAR=12345; echo ${VAR#}' '12345' 0 #19
854 1.12 kre check 'X=; VAR=12345; echo ${VAR#${X}}' '12345' 0 #20
855 1.12 kre # rule 3.b (tested above, rule 2.a)
856 1.12 kre # rule 3.c
857 1.12 kre check 'set -- ; echo ${##0}' '' 0 #21
858 1.12 kre check 'set -- a b c; echo ${##1}' '3' 0 #22
859 1.12 kre check 'set -- a b c d e f g h i j; echo ${##1}' '0' 0 #23
860 1.12 kre check 'X=0; set -- ; echo ${##${X}}' '' 0 #24
861 1.12 kre check 'X=; set -- ; echo ${##${X}}' '0' 0 #25
862 1.12 kre check 'X=1; set -- a b c; echo ${##${X}}' '3' 0 #26
863 1.12 kre check 'X=1; set -- a b c d e f g h i j; echo ${##${X}}' '0' 0 #27
864 1.12 kre check 'X=; set -- a b c d e f g h i j; echo ${##${X}}' '10' 0 #28
865 1.12 kre check 'X=#; VAR=#2345; echo ${VAR#${X}}' '2345' 0 #29
866 1.12 kre check 'X=#; VAR=12345; echo ${VAR#${X}}' '12345' 0 #30
867 1.12 kre # rule 3.d
868 1.12 kre check 'set -- a b c; echo ${#\#}' '' 2 #31
869 1.12 kre # rule 3.e
870 1.12 kre check 'set -- ; echo ${##\#}' '0' 0 #32
871 1.12 kre check 'set -- a b c d e f g h i j; echo ${##\#}' '10' 0 #33
872 1.12 kre
873 1.12 kre # rule 4
874 1.12 kre check 'VAR=12345; echo ${VAR##1}' '2345' 0 #34
875 1.12 kre check 'VAR=12345; echo ${VAR##\1}' '2345' 0 #35
876 1.12 kre # rule 4.a
877 1.12 kre check 'VAR=12345; echo ${VAR##}' '12345' 0 #36
878 1.12 kre # rule 4.b
879 1.12 kre check 'set -- ; echo ${###}' '0' 0 #37
880 1.12 kre check 'set -- a b c d e f g h i j; echo ${###}' '10' 0 #38
881 1.12 kre # rule 4.c
882 1.12 kre check 'VAR=12345; echo ${VAR#\#}' '12345' 0 #39
883 1.12 kre check 'VAR=12345; echo ${VAR#\#1}' '12345' 0 #40
884 1.12 kre check 'VAR=#2345; echo ${VAR#\#}' '2345' 0 #41
885 1.12 kre check 'VAR=#12345; echo ${VAR#\#1}' '2345' 0 #42
886 1.12 kre check 'VAR=#2345; echo ${VAR#\#1}' '#2345' 0 #43
887 1.12 kre check 'set -- ; echo ${####}' '0' 0 #44
888 1.12 kre check 'set -- ; echo ${###\#}' '0' 0 #45
889 1.12 kre check 'set -- a b c d e f g h i j; echo ${####}' '10' 0 #46
890 1.12 kre check 'set -- a b c d e f g h i j; echo ${###\#}' '10' 0 #47
891 1.12 kre
892 1.12 kre # now check for some more utter nonsense, not mentioned in the rules
893 1.12 kre # above (doesn't need to be)
894 1.12 kre
895 1.12 kre check 'x=hello; set -- a b c; echo ${#x:-1}' '' 2 #48
896 1.12 kre check 'x=hello; set -- a b c; echo ${#x-1}' '' 2 #49
897 1.12 kre check 'x=hello; set -- a b c; echo ${#x:+1}' '' 2 #50
898 1.12 kre check 'x=hello; set -- a b c; echo ${#x+1}' '' 2 #51
899 1.12 kre check 'x=hello; set -- a b c; echo ${#x+1}' '' 2 #52
900 1.12 kre check 'x=hello; set -- a b c; echo ${#x:?msg}' '' 2 #53
901 1.12 kre check 'x=hello; set -- a b c; echo ${#x?msg}' '' 2 #54
902 1.12 kre check 'x=hello; set -- a b c; echo ${#x:=val}' '' 2 #55
903 1.12 kre check 'x=hello; set -- a b c; echo ${#x=val}' '' 2 #56
904 1.12 kre check 'x=hello; set -- a b c; echo ${#x#h}' '' 2 #57
905 1.12 kre check 'x=hello; set -- a b c; echo ${#x#*l}' '' 2 #58
906 1.12 kre check 'x=hello; set -- a b c; echo ${#x##*l}' '' 2 #59
907 1.12 kre check 'x=hello; set -- a b c; echo ${#x%o}' '' 2 #60
908 1.12 kre check 'x=hello; set -- a b c; echo ${#x%l*}' '' 2 #61
909 1.12 kre check 'x=hello; set -- a b c; echo ${#x%%l*}' '' 2 #62
910 1.12 kre
911 1.12 kre # but just to be complete, these ones should work
912 1.12 kre
913 1.12 kre check 'x=hello; set -- a b c; echo ${#%5}' '3' 0 #63
914 1.12 kre check 'x=hello; set -- a b c; echo ${#%3}' '' 0 #64
915 1.12 kre check 'x=hello; set -- a b c; echo ${#%?}' '' 0 #65
916 1.12 kre check 'X=#; set -- a b c; echo ${#%${X}}' '3' 0 #66
917 1.12 kre check 'X=3; set -- a b c; echo ${#%${X}}' '' 0 #67
918 1.12 kre check 'set -- a b c; echo ${#%%5}' '3' 0 #68
919 1.12 kre check 'set -- a b c; echo ${#%%3}' '' 0 #69
920 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%1}' '12' 0 #70
921 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%2}' '1' 0 #71
922 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%?}' '1' 0 #72
923 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%[012]}' '1' 0 #73
924 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%[0-4]}' '1' 0 #74
925 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%?2}' '' 0 #75
926 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%1*}' '' 0 #76
927 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%%2}' '1' 0 #77
928 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%%1*}' '' 0 #78
929 1.12 kre
930 1.12 kre # and this lot are stupid, as $# is never unset or null, but they do work...
931 1.12 kre
932 1.12 kre check 'set -- a b c; echo ${#:-99}' '3' 0 #79
933 1.12 kre check 'set -- a b c; echo ${#-99}' '3' 0 #80
934 1.12 kre check 'set -- a b c; echo ${#:+99}' '99' 0 #81
935 1.12 kre check 'set -- a b c; echo ${#+99}' '99' 0 #82
936 1.12 kre check 'set -- a b c; echo ${#:?bogus}' '3' 0 #83
937 1.12 kre check 'set -- a b c; echo ${#?bogus}' '3' 0 #84
938 1.12 kre
939 1.12 kre # even this utter nonsense is OK, as while special params cannot be
940 1.12 kre # set this way, here, as $# is not unset, or null, the assignment
941 1.12 kre # never happens (isn't even attempted)
942 1.12 kre
943 1.12 kre check 'set -- a b c; echo ${#:=bogus}' '3' 0 #85
944 1.12 kre check 'set -- a b c; echo ${#=bogus}' '3' 0 #86
945 1.12 kre
946 1.13 kre for n in 0 1 10 25 100 #87 #88 #89 #90 #91
947 1.13 kre do
948 1.13 kre check "(exit $n)"'; echo ${#?}' "${#n}" 0
949 1.13 kre done
950 1.13 kre
951 1.13 kre results # results so far anyway...
952 1.13 kre
953 1.13 kre # now we have some harder to verify cases, as they (must) check unknown values
954 1.13 kre # and hence the resuls cannot just be built into the script, but we have
955 1.13 kre # to use some tricks to validate them, so for these, just do regular testing
956 1.13 kre # and don't attempt to use the check helper function, nor to include
957 1.13 kre # these tests in the result summary. If anything has already failed, we
958 1.13 kre # do not get this far... From here on, first failure ends this test.
959 1.13 kre
960 1.13 kre for opts in '' '-a' '-au' '-auf' '-aufe' # options safe enough to set
961 1.13 kre do
962 1.13 kre # Note the shell might have other (or these) opts set already
963 1.13 kre
964 1.13 kre RES=$(${TEST_SH} -c "test -n '${opts}' && set ${opts};
965 1.13 kre printf '%s' \"\$-\";printf ' %s\\n' \"\${#-}\"") ||
966 1.13 kre atf_fail '${#-} test exited with status '"$?"
967 1.13 kre LEN="${RES##* }"
968 1.13 kre DMINUS="${RES% ${LEN}}"
969 1.13 kre if [ "${#DMINUS}" != "${LEN}" ]
970 1.13 kre then
971 1.13 kre atf_fail \
972 1.13 kre '${#-} test'" produced ${LEN} for opts ${DMINUS} (${RES})"
973 1.13 kre fi
974 1.13 kre done
975 1.13 kre
976 1.13 kre for seq in a b c d e f g h i j
977 1.13 kre do
978 1.13 kre # now we are tryin to generate different pids for $$ and $!
979 1.13 kre # so we just run the test a number of times, and hope...
980 1.13 kre # On NetBSD pid randomisation will usually help the tests
981 1.13 kre
982 1.13 kre eval "$(${TEST_SH} -c \
983 1.13 kre '(exit 0)& BG=$! LBG=${#!};
984 1.13 kre printf "SH=%s BG=%s LSH=%s LBG=%s" "$$" "$BG" "${#$}" "$LBG";
985 1.13 kre wait')"
986 1.13 kre
987 1.13 kre if [ "${#SH}" != "${LSH}" ] || [ "${#BG}" != "${LBG}" ]
988 1.13 kre then
989 1.13 kre atf_fail \
990 1.13 kre '${#!] of '"${BG} was ${LBG}, expected ${#BG}"'; ${#$} of '"${SH} was ${LSH}, expected ${#SH}"
991 1.13 kre fi
992 1.13 kre done
993 1.12 kre }
994 1.12 kre
995 1.10 kre atf_test_case dollar_star
996 1.10 kre dollar_star_head() {
997 1.21 kre atf_set descr 'Test expansion of various aspects of $*'
998 1.10 kre }
999 1.10 kre dollar_star_body() {
1000 1.10 kre
1001 1.12 kre reset dollar_star
1002 1.10 kre
1003 1.10 kre check 'set -- a b c; echo $# $*' '3 a b c' 0 # 1
1004 1.10 kre check 'set -- a b c; echo $# "$*"' '3 a b c' 0 # 2
1005 1.10 kre check 'set -- a "b c"; echo $# $*' '2 a b c' 0 # 3
1006 1.10 kre check 'set -- a "b c"; echo $# "$*"' '2 a b c' 0 # 4
1007 1.10 kre check 'set -- a b c; set -- $* ; echo $# $*' '3 a b c' 0 # 5
1008 1.10 kre check 'set -- a b c; set -- "$*" ; echo $# $*' '1 a b c' 0 # 6
1009 1.10 kre check 'set -- a "b c"; set -- $* ; echo $# $*' '3 a b c' 0 # 7
1010 1.10 kre check 'set -- a "b c"; set -- "$*" ; echo $# $*' \
1011 1.10 kre '1 a b c' 0 # 8
1012 1.10 kre
1013 1.10 kre check 'IFS=". "; set -- a b c; echo $# $*' '3 a b c' 0 # 9
1014 1.10 kre check 'IFS=". "; set -- a b c; echo $# "$*"' '3 a.b.c' 0 #10
1015 1.10 kre check 'IFS=". "; set -- a "b c"; echo $# $*' '2 a b c' 0 #11
1016 1.10 kre check 'IFS=". "; set -- a "b c"; echo $# "$*"' '2 a.b c' 0 #12
1017 1.10 kre check 'IFS=". "; set -- a "b.c"; echo $# $*' '2 a b c' 0 #13
1018 1.10 kre check 'IFS=". "; set -- a "b.c"; echo $# "$*"' '2 a.b.c' 0 #14
1019 1.10 kre check 'IFS=". "; set -- a b c; set -- $* ; echo $# $*' \
1020 1.10 kre '3 a b c' 0 #15
1021 1.10 kre check 'IFS=". "; set -- a b c; set -- "$*" ; echo $# $*' \
1022 1.10 kre '1 a b c' 0 #16
1023 1.10 kre check 'IFS=". "; set -- a "b c"; set -- $* ; echo $# $*' \
1024 1.10 kre '3 a b c' 0 #17
1025 1.10 kre check 'IFS=". "; set -- a "b c"; set -- "$*" ; echo $# $*' \
1026 1.10 kre '1 a b c' 0 #18
1027 1.10 kre check 'IFS=". "; set -- a b c; set -- $* ; echo $# "$*"' \
1028 1.10 kre '3 a.b.c' 0 #19
1029 1.10 kre check 'IFS=". "; set -- a b c; set -- "$*" ; echo $# "$*"' \
1030 1.10 kre '1 a.b.c' 0 #20
1031 1.10 kre check 'IFS=". "; set -- a "b c"; set -- $* ; echo $# "$*"' \
1032 1.10 kre '3 a.b.c' 0 #21
1033 1.10 kre check 'IFS=". "; set -- a "b c"; set -- "$*" ; echo $# "$*"' \
1034 1.10 kre '1 a.b c' 0 #22
1035 1.10 kre
1036 1.10 kre results
1037 1.10 kre }
1038 1.10 kre
1039 1.10 kre atf_test_case dollar_star_in_word
1040 1.10 kre dollar_star_in_word_head() {
1041 1.21 kre atf_set descr 'Test expansion $* occurring in word of ${var:-word}'
1042 1.10 kre }
1043 1.10 kre dollar_star_in_word_body() {
1044 1.10 kre
1045 1.10 kre reset dollar_star_in_word
1046 1.10 kre
1047 1.10 kre unset xXx ; # just in case!
1048 1.10 kre
1049 1.10 kre # Note that the expected results for these tests are identical
1050 1.10 kre # to those from the dollar_star test. It should never make
1051 1.10 kre # a difference whether we expand $* or ${unset:-$*}
1052 1.10 kre
1053 1.10 kre # (note expanding ${unset:-"$*"} is different, that is not tested here)
1054 1.10 kre
1055 1.10 kre check 'set -- a b c; echo $# ${xXx:-$*}' '3 a b c' 0 # 1
1056 1.10 kre check 'set -- a b c; echo $# "${xXx:-$*}"' '3 a b c' 0 # 2
1057 1.10 kre check 'set -- a "b c"; echo $# ${xXx:-$*}' '2 a b c' 0 # 3
1058 1.10 kre check 'set -- a "b c"; echo $# "${xXx:-$*}"' '2 a b c' 0 # 4
1059 1.10 kre check 'set -- a b c; set -- ${xXx:-$*} ; echo $# $*' '3 a b c' 0 # 5
1060 1.10 kre check 'set -- a b c; set -- "${xXx:-$*}" ; echo $# $*' '1 a b c' 0 # 6
1061 1.10 kre check 'set -- a "b c"; set -- ${xXx:-$*} ; echo $# $*' '3 a b c' 0 # 7
1062 1.10 kre check 'set -- a "b c"; set -- "${xXx:-$*}" ; echo $# $*' \
1063 1.10 kre '1 a b c' 0 # 8
1064 1.10 kre
1065 1.10 kre check 'IFS=". "; set -- a b c; echo $# ${xXx:-$*}' '3 a b c' 0 # 9
1066 1.10 kre check 'IFS=". "; set -- a b c; echo $# "${xXx:-$*}"' '3 a.b.c' 0 #10
1067 1.10 kre check 'IFS=". "; set -- a "b c"; echo $# ${xXx:-$*}' '2 a b c' 0 #11
1068 1.10 kre check 'IFS=". "; set -- a "b c"; echo $# "${xXx:-$*}"' '2 a.b c' 0 #12
1069 1.10 kre check 'IFS=". "; set -- a "b.c"; echo $# ${xXx:-$*}' '2 a b c' 0 #13
1070 1.10 kre check 'IFS=". "; set -- a "b.c"; echo $# "${xXx:-$*}"' '2 a.b.c' 0 #14
1071 1.10 kre check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
1072 1.10 kre '3 a b c' 0 #15
1073 1.10 kre check 'IFS=". ";set -- a b c;set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
1074 1.10 kre '1 a b c' 0 #16
1075 1.10 kre check 'IFS=". ";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
1076 1.10 kre '3 a b c' 0 #17
1077 1.10 kre check 'IFS=". ";set -- a "b c";set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
1078 1.10 kre '1 a b c' 0 #18
1079 1.10 kre check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
1080 1.10 kre '3 a.b.c' 0 #19
1081 1.10 kre check 'IFS=". ";set -- a b c;set -- "$*";echo $# "$*"' \
1082 1.10 kre '1 a.b.c' 0 #20
1083 1.10 kre check 'IFS=". ";set -- a "b c";set -- $*;echo $# "$*"' \
1084 1.10 kre '3 a.b.c' 0 #21
1085 1.10 kre check 'IFS=". ";set -- a "b c";set -- "$*";echo $# "$*"' \
1086 1.10 kre '1 a.b c' 0 #22
1087 1.10 kre
1088 1.10 kre results
1089 1.10 kre }
1090 1.10 kre
1091 1.10 kre atf_test_case dollar_star_with_empty_ifs
1092 1.10 kre dollar_star_with_empty_ifs_head() {
1093 1.21 kre atf_set descr 'Test expansion of $* with IFS=""'
1094 1.10 kre }
1095 1.10 kre dollar_star_with_empty_ifs_body() {
1096 1.10 kre
1097 1.10 kre reset dollar_star_with_empty_ifs
1098 1.10 kre
1099 1.10 kre check 'IFS=""; set -- a b c; echo $# $*' '3 a b c' 0 # 1
1100 1.10 kre check 'IFS=""; set -- a b c; echo $# "$*"' '3 abc' 0 # 2
1101 1.10 kre check 'IFS=""; set -- a "b c"; echo $# $*' '2 a b c' 0 # 3
1102 1.10 kre check 'IFS=""; set -- a "b c"; echo $# "$*"' '2 ab c' 0 # 4
1103 1.10 kre check 'IFS=""; set -- a "b.c"; echo $# $*' '2 a b.c' 0 # 5
1104 1.10 kre check 'IFS=""; set -- a "b.c"; echo $# "$*"' '2 ab.c' 0 # 6
1105 1.10 kre check 'IFS=""; set -- a b c; set -- $* ; echo $# $*' \
1106 1.10 kre '3 a b c' 0 # 7
1107 1.10 kre check 'IFS=""; set -- a b c; set -- "$*" ; echo $# $*' \
1108 1.10 kre '1 abc' 0 # 8
1109 1.10 kre check 'IFS=""; set -- a "b c"; set -- $* ; echo $# $*' \
1110 1.10 kre '2 a b c' 0 # 9
1111 1.10 kre check 'IFS=""; set -- a "b c"; set -- "$*" ; echo $# $*' \
1112 1.10 kre '1 ab c' 0 #10
1113 1.10 kre check 'IFS=""; set -- a b c; set -- $* ; echo $# "$*"' \
1114 1.10 kre '3 abc' 0 #11
1115 1.10 kre check 'IFS=""; set -- a b c; set -- "$*" ; echo $# "$*"' \
1116 1.10 kre '1 abc' 0 #12
1117 1.10 kre check 'IFS=""; set -- a "b c"; set -- $* ; echo $# "$*"' \
1118 1.10 kre '2 ab c' 0 #13
1119 1.10 kre check 'IFS=""; set -- a "b c"; set -- "$*" ; echo $# "$*"' \
1120 1.10 kre '1 ab c' 0 #14
1121 1.10 kre
1122 1.11 kre results # FIXED: 'PR bin/52090 expect 7 of 14 subtests to fail'
1123 1.10 kre }
1124 1.10 kre
1125 1.10 kre atf_test_case dollar_star_in_word_empty_ifs
1126 1.10 kre dollar_star_in_word_empty_ifs_head() {
1127 1.21 kre atf_set descr 'Test expansion of ${unset:-$*} with IFS=""'
1128 1.10 kre }
1129 1.10 kre dollar_star_in_word_empty_ifs_body() {
1130 1.10 kre
1131 1.10 kre reset dollar_star_in_word_empty_ifs
1132 1.10 kre
1133 1.10 kre unset xXx ; # just in case
1134 1.10 kre
1135 1.10 kre # Note that the expected results for these tests are identical
1136 1.10 kre # to those from the dollar_star_with_empty_ifs test. It should
1137 1.10 kre # never make a difference whether we expand $* or ${unset:-$*}
1138 1.10 kre
1139 1.10 kre # (note expanding ${unset:-"$*"} is different, that is not tested here)
1140 1.10 kre
1141 1.10 kre check 'IFS="";set -- a b c;echo $# ${xXx:-$*}' '3 a b c' 0 # 1
1142 1.10 kre check 'IFS="";set -- a b c;echo $# "${xXx:-$*}"' '3 abc' 0 # 2
1143 1.10 kre check 'IFS="";set -- a "b c";echo $# ${xXx:-$*}' '2 a b c' 0 # 3
1144 1.10 kre check 'IFS="";set -- a "b c";echo $# "${xXx:-$*}"' '2 ab c' 0 # 4
1145 1.10 kre check 'IFS="";set -- a "b.c";echo $# ${xXx:-$*}' '2 a b.c' 0 # 5
1146 1.10 kre check 'IFS="";set -- a "b.c";echo $# "${xXx:-$*}"' '2 ab.c' 0 # 6
1147 1.10 kre check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
1148 1.10 kre '3 a b c' 0 # 7
1149 1.10 kre check 'IFS="";set -- a b c;set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
1150 1.10 kre '1 abc' 0 # 8
1151 1.10 kre check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
1152 1.10 kre '2 a b c' 0 # 9
1153 1.10 kre check 'IFS="";set -- a "b c";set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
1154 1.10 kre '1 ab c' 0 #10
1155 1.10 kre check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
1156 1.10 kre '3 abc' 0 #11
1157 1.10 kre check 'IFS="";set -- a b c;set -- "${xXx:-$*}";echo $# "${xXx:-$*}"' \
1158 1.10 kre '1 abc' 0 #12
1159 1.10 kre check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
1160 1.10 kre '2 ab c' 0 #13
1161 1.10 kre check 'IFS="";set -- a "b c";set -- "${xXx:-$*}";echo $# "${xXx:-$*}"' \
1162 1.10 kre '1 ab c' 0 #14
1163 1.10 kre
1164 1.11 kre results # FIXED: 'PR bin/52090 expect 7 of 14 subtests to fail'
1165 1.10 kre }
1166 1.10 kre
1167 1.10 kre atf_test_case dollar_star_in_quoted_word
1168 1.10 kre dollar_star_in_quoted_word_head() {
1169 1.21 kre atf_set descr 'Test expansion $* occurring in word of ${var:-"word"}'
1170 1.10 kre }
1171 1.10 kre dollar_star_in_quoted_word_body() {
1172 1.10 kre
1173 1.10 kre reset dollar_star_in_quoted_word
1174 1.10 kre
1175 1.10 kre unset xXx ; # just in case!
1176 1.10 kre
1177 1.10 kre check 'set -- a b c; echo $# ${xXx:-"$*"}' '3 a b c' 0 # 1
1178 1.10 kre check 'set -- a "b c"; echo $# ${xXx:-"$*"}' '2 a b c' 0 # 2
1179 1.10 kre check 'set -- a b c; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
1180 1.10 kre '1 a b c' 0 # 3
1181 1.10 kre check 'set -- a "b c"; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
1182 1.10 kre '1 a b c' 0 # 4
1183 1.10 kre check 'set -- a b c; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
1184 1.10 kre '1 a b c' 0 # 5
1185 1.10 kre check 'set -- a "b c"; set -- ${xXx:-"$*"} ; echo $# ${xXx-$*}' \
1186 1.10 kre '1 a b c' 0 # 6
1187 1.10 kre check 'set -- a b c; set -- ${xXx:-$*} ; echo $# ${xXx-"$*"}' \
1188 1.10 kre '3 a b c' 0 # 7
1189 1.10 kre check 'set -- a "b c"; set -- ${xXx:-$*} ; echo $# ${xXx-"$*"}' \
1190 1.10 kre '3 a b c' 0 # 8
1191 1.10 kre
1192 1.10 kre check 'IFS=". "; set -- a b c; echo $# ${xXx:-"$*"}' '3 a.b.c' 0 # 9
1193 1.10 kre check 'IFS=". "; set -- a "b c"; echo $# ${xXx:-"$*"}' '2 a.b c' 0 #10
1194 1.10 kre check 'IFS=". "; set -- a "b.c"; echo $# ${xXx:-"$*"}' '2 a.b.c' 0 #11
1195 1.10 kre check 'IFS=". ";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
1196 1.10 kre '1 a.b.c' 0 #12
1197 1.10 kre check 'IFS=". ";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
1198 1.10 kre '1 a.b c' 0 #13
1199 1.10 kre check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
1200 1.10 kre '3 a.b.c' 0 #14
1201 1.10 kre check 'IFS=". ";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
1202 1.10 kre '3 a.b.c' 0 #15
1203 1.10 kre check 'IFS=". ";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
1204 1.10 kre '1 a b c' 0 #16
1205 1.10 kre check 'IFS=". ";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
1206 1.10 kre '1 a b c' 0 #17
1207 1.10 kre
1208 1.10 kre check 'IFS="";set -- a b c;echo $# ${xXx:-"$*"}' '3 abc' 0 #18
1209 1.10 kre check 'IFS="";set -- a "b c";echo $# ${xXx:-"$*"}' '2 ab c' 0 #19
1210 1.10 kre check 'IFS="";set -- a "b.c";echo $# ${xXx:-"$*"}' '2 ab.c' 0 #20
1211 1.10 kre check 'IFS="";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
1212 1.10 kre '1 abc' 0 #21
1213 1.10 kre check 'IFS="";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
1214 1.10 kre '1 ab c' 0 #22
1215 1.10 kre check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
1216 1.10 kre '3 abc' 0 #23
1217 1.10 kre check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
1218 1.10 kre '2 ab c' 0 #24
1219 1.10 kre check 'IFS="";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
1220 1.10 kre '1 abc' 0 #25
1221 1.10 kre check 'IFS="";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
1222 1.10 kre '1 ab c' 0 #26
1223 1.10 kre
1224 1.11 kre results # FIXED: 'PR bin/52090 - 2 of 26 subtests expected to fail'
1225 1.10 kre }
1226 1.10 kre
1227 1.21 kre atf_test_case dollar_at_in_field_split_context
1228 1.21 kre dollar_at_in_field_split_context_head() {
1229 1.21 kre atf_set descr 'Test "$@" wth field splitting -- PR bin/54112'
1230 1.21 kre }
1231 1.21 kre dollar_at_in_field_split_context_body() {
1232 1.21 kre reset dollar_at_in_field_split_context
1233 1.21 kre
1234 1.21 kre # the simple case (no field split) which always worked
1235 1.21 kre check 'set -- ""; set -- ${0+"$@"}; echo $#' 1 0 #1
1236 1.21 kre
1237 1.21 kre # The original failure case from the bash-bug list
1238 1.21 kre check 'set -- ""; set -- ${0+"$@" "$@"}; echo $#' 2 0 #2
1239 1.21 kre
1240 1.21 kre # slightly simpler cases that triggered the same issue
1241 1.21 kre check 'set -- ""; set -- ${0+"$@" }; echo $#' 1 0 #3
1242 1.21 kre check 'set -- ""; set -- ${0+ "$@"}; echo $#' 1 0 #4
1243 1.21 kre check 'set -- ""; set -- ${0+ "$@" }; echo $#' 1 0 #5
1244 1.21 kre
1245 1.21 kre # and the bizarre
1246 1.21 kre check 'set -- ""; set -- ${0+"$@" "$@" "$@"}; echo $#' 3 0 #6
1247 1.21 kre
1248 1.21 kre # repeat tests when there is more than one set empty numeric param
1249 1.21 kre
1250 1.21 kre check 'set -- "" ""; set -- ${0+"$@"}; echo $#' 2 0 #7
1251 1.21 kre check 'set -- "" ""; set -- ${0+"$@" "$@"}; echo $#' 4 0 #8
1252 1.21 kre check 'set -- "" ""; set -- ${0+"$@" }; echo $#' 2 0 #9
1253 1.21 kre check 'set -- "" ""; set -- ${0+ "$@"}; echo $#' 2 0 #10
1254 1.21 kre check 'set -- "" ""; set -- ${0+ "$@" }; echo $#' 2 0 #11
1255 1.21 kre check 'set -- "" ""; set -- ${0+"$@" "$@" "$@"}; echo $#' \
1256 1.21 kre 6 0 #12
1257 1.21 kre
1258 1.21 kre # Next some checks of the way the NetBSD shell
1259 1.21 kre # interprets some expressions that are POSIX unspecified.
1260 1.21 kre # Other shells might fail these tests, without that
1261 1.21 kre # being a problem. We retain these tests so accidental
1262 1.21 kre # changes in our behaviour can be detected.
1263 1.21 kre
1264 1.21 kre check 'set --; X=; set -- "$X$@"; echo $#' 0 0 #13
1265 1.21 kre check 'set --; X=; set -- "$@$X"; echo $#' 0 0 #14
1266 1.21 kre check 'set --; X=; set -- "$X$@$X"; echo $#' 0 0 #15
1267 1.21 kre check 'set --; X=; set -- "$@$@"; echo $#' 0 0 #16
1268 1.21 kre
1269 1.21 kre check 'set -- ""; X=; set -- "$X$@"; echo $#' 1 0 #17
1270 1.21 kre check 'set -- ""; X=; set -- "$@$X"; echo $#' 1 0 #19
1271 1.21 kre check 'set -- ""; X=; set -- "$X$@$X"; echo $#' 1 0 #19
1272 1.21 kre check 'set -- ""; X=; set -- "$@$@"; echo $#' 1 0 #20
1273 1.21 kre
1274 1.21 kre check 'set -- "" ""; X=; set -- "$X$@"; echo $#' 2 0 #21
1275 1.21 kre check 'set -- "" ""; X=; set -- "$@$X"; echo $#' 2 0 #22
1276 1.21 kre check 'set -- "" ""; X=; set -- "$X$@$X"; echo $#' 2 0 #23
1277 1.21 kre # Yes, this next one really is (and should be) 3...
1278 1.21 kre check 'set -- "" ""; X=; set -- "$@$@"; echo $#' 3 0 #24
1279 1.21 kre
1280 1.21 kre results
1281 1.21 kre }
1282 1.21 kre
1283 1.19 kre atf_test_case embedded_nl
1284 1.19 kre embedded_nl_head() {
1285 1.21 kre atf_set descr 'Test literal \n in xxx string in ${var-xxx}'
1286 1.19 kre }
1287 1.19 kre embedded_nl_body() {
1288 1.19 kre
1289 1.19 kre atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} <<- 'EOF'
1290 1.19 kre unset V
1291 1.19 kre X="${V-a
1292 1.19 kre b}"
1293 1.19 kre printf '%s\n' "${X}"
1294 1.19 kre EOF
1295 1.19 kre
1296 1.19 kre atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} <<- 'EOF'
1297 1.19 kre unset V
1298 1.19 kre X=${V-"a
1299 1.19 kre b"}
1300 1.19 kre printf '%s\n' "${X}"
1301 1.19 kre EOF
1302 1.19 kre
1303 1.19 kre # This should not generate a syntax error, see PR bin/53201
1304 1.19 kre atf_check -s exit:0 -o inline:'abc\n' -e empty ${TEST_SH} <<- 'EOF'
1305 1.19 kre V=abc
1306 1.19 kre X=${V-a
1307 1.19 kre b}
1308 1.19 kre printf '%s\n' "${X}"
1309 1.19 kre EOF
1310 1.19 kre
1311 1.19 kre # Nor should any of these...
1312 1.19 kre atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} <<- 'EOF'
1313 1.19 kre unset V
1314 1.19 kre X=${V-a
1315 1.19 kre b}
1316 1.19 kre printf '%s\n' "${X}"
1317 1.19 kre EOF
1318 1.19 kre
1319 1.19 kre atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} <<- 'EOF'
1320 1.19 kre unset V
1321 1.19 kre X=${V:=a
1322 1.19 kre b}
1323 1.19 kre printf '%s\n' "${X}"
1324 1.19 kre EOF
1325 1.19 kre
1326 1.19 kre atf_check -s exit:0 -o inline:'xa\nby\na\nb\n' -e empty \
1327 1.19 kre ${TEST_SH} <<- 'EOF'
1328 1.19 kre unset V
1329 1.19 kre X=x${V:=a
1330 1.19 kre b}y
1331 1.19 kre printf '%s\n' "${X}" "${V}"
1332 1.19 kre EOF
1333 1.19 kre }
1334 1.19 kre
1335 1.1 jruoho atf_init_test_cases() {
1336 1.10 kre # Listed here in the order ATF runs them, not the order from above
1337 1.10 kre
1338 1.10 kre atf_add_test_case arithmetic
1339 1.1 jruoho atf_add_test_case dollar_at
1340 1.21 kre atf_add_test_case dollar_at_empty_and_conditional
1341 1.21 kre atf_add_test_case dollar_at_in_field_split_context
1342 1.20 kre atf_add_test_case dollar_at_unquoted_or_conditional
1343 1.1 jruoho atf_add_test_case dollar_at_with_text
1344 1.12 kre atf_add_test_case dollar_hash
1345 1.10 kre atf_add_test_case dollar_star
1346 1.10 kre atf_add_test_case dollar_star_in_quoted_word
1347 1.10 kre atf_add_test_case dollar_star_in_word
1348 1.10 kre atf_add_test_case dollar_star_in_word_empty_ifs
1349 1.10 kre atf_add_test_case dollar_star_with_empty_ifs
1350 1.19 kre atf_add_test_case embedded_nl
1351 1.2 ast atf_add_test_case iteration_on_null_parameter
1352 1.6 christos atf_add_test_case iteration_on_quoted_null_parameter
1353 1.6 christos atf_add_test_case iteration_on_null_or_null_parameter
1354 1.6 christos atf_add_test_case iteration_on_null_or_missing_parameter
1355 1.7 christos atf_add_test_case shell_params
1356 1.10 kre atf_add_test_case strip
1357 1.18 kre atf_add_test_case tilde
1358 1.14 kre atf_add_test_case wrap_strip
1359 1.9 kre atf_add_test_case var_with_embedded_cmdsub
1360 1.10 kre atf_add_test_case varpattern_backslashes
1361 1.1 jruoho }
1362