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