t_expand.sh revision 1.20 1 # $NetBSD: t_expand.sh,v 1.20 2018/11/27 09:59:30 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 \
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 # Testing ~user is harder, so, perhaps later...
488 }
489
490 atf_test_case varpattern_backslashes
491 varpattern_backslashes_head() {
492 atf_set "descr" "Tests that protecting wildcards with backslashes" \
493 "works in variable patterns."
494 }
495 varpattern_backslashes_body() {
496 line='/foo/bar/*/baz'
497 stripped='/foo/bar/'
498 atf_check -o inline:'/foo/bar/\n' -e empty ${TEST_SH} -c \
499 'line="/foo/bar/*/baz"; echo ${line%%\**}'
500 }
501
502 atf_test_case arithmetic
503 arithmetic_head() {
504 atf_set "descr" "POSIX requires shell arithmetic to use signed" \
505 "long or a wider type. We use intmax_t, so at" \
506 "least 64 bits should be available. Make sure" \
507 "this is true."
508 }
509 arithmetic_body() {
510
511 atf_check -o inline:'3' -e empty ${TEST_SH} -c \
512 'printf %s $((1 + 2))'
513 atf_check -o inline:'2147483647' -e empty ${TEST_SH} -c \
514 'printf %s $((0x7fffffff))'
515 atf_check -o inline:'9223372036854775807' -e empty ${TEST_SH} -c \
516 'printf %s $(((1 << 63) - 1))'
517 }
518
519 atf_test_case iteration_on_null_parameter
520 iteration_on_null_parameter_head() {
521 atf_set "descr" "Check iteration of \$@ in for loop when set to null;" \
522 "the error \"sh: @: parameter not set\" is incorrect." \
523 "PR bin/48202."
524 }
525 iteration_on_null_parameter_body() {
526 atf_check -o empty -e empty ${TEST_SH} -c \
527 'N=; set -- ${N}; for X; do echo "[$X]"; done'
528 }
529
530 atf_test_case iteration_on_quoted_null_parameter
531 iteration_on_quoted_null_parameter_head() {
532 atf_set "descr" \
533 'Check iteration of "$@" in for loop when set to null;'
534 }
535 iteration_on_quoted_null_parameter_body() {
536 atf_check -o inline:'[]\n' -e empty ${TEST_SH} -c \
537 'N=; set -- "${N}"; for X; do echo "[$X]"; done'
538 }
539
540 atf_test_case iteration_on_null_or_null_parameter
541 iteration_on_null_or_null_parameter_head() {
542 atf_set "descr" \
543 'Check expansion of null parameter as default for another null'
544 }
545 iteration_on_null_or_null_parameter_body() {
546 atf_check -o empty -e empty ${TEST_SH} -c \
547 'N=; E=; set -- ${N:-${E}}; for X; do echo "[$X]"; done'
548 }
549
550 atf_test_case iteration_on_null_or_missing_parameter
551 iteration_on_null_or_missing_parameter_head() {
552 atf_set "descr" \
553 'Check expansion of missing parameter as default for another null'
554 }
555 iteration_on_null_or_missing_parameter_body() {
556 # atf_expect_fail 'PR bin/50834'
557 atf_check -o empty -e empty ${TEST_SH} -c \
558 'N=; set -- ${N:-}; for X; do echo "[$X]"; done'
559 }
560
561 ####### The remaining tests use the following helper functions ...
562
563 nl='
564 '
565 reset()
566 {
567 TEST_NUM=0
568 TEST_FAILURES=''
569 TEST_FAIL_COUNT=0
570 TEST_ID="$1"
571 }
572
573 check()
574 {
575 fail=false
576 TEMP_FILE=$( mktemp OUT.XXXXXX )
577 TEST_NUM=$(( $TEST_NUM + 1 ))
578 MSG=
579
580 # our local shell (ATF_SHELL) better do quoting correctly...
581 # some of the tests expect us to expand $nl internally...
582 CMD="$1"
583
584 result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )"
585 STATUS=$?
586
587 if [ "${STATUS}" -ne "$3" ]; then
588 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
589 MSG="${MSG} expected exit code $3, got ${STATUS}"
590
591 # don't actually fail just because of wrong exit code
592 # unless we either expected, or received "good"
593 # or something else is detected as incorrect as well.
594 case "$3/${STATUS}" in
595 (*/0|0/*) fail=true;;
596 esac
597 fi
598
599 if [ "$3" -eq 0 ]; then
600 if [ -s "${TEMP_FILE}" ]; then
601 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
602 MSG="${MSG} Messages produced on stderr unexpected..."
603 MSG="${MSG}${nl}$( cat "${TEMP_FILE}" )"
604 fail=true
605 fi
606 else
607 if ! [ -s "${TEMP_FILE}" ]; then
608 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
609 MSG="${MSG} Expected messages on stderr,"
610 MSG="${MSG} nothing produced"
611 fail=true
612 fi
613 fi
614 rm -f "${TEMP_FILE}"
615
616 # Remove newlines (use local shell for this)
617 oifs="$IFS"
618 IFS="$nl"
619 result="$(echo $result)"
620 IFS="$oifs"
621 if [ "$2" != "$result" ]
622 then
623 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
624 MSG="${MSG} Expected output '$2', received '$result'"
625 fail=true
626 fi
627
628 if $fail
629 then
630 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
631 MSG="${MSG} Full command: <<${CMD}>>"
632 fi
633
634 $fail && test -n "$TEST_ID" && {
635 TEST_FAILURES="${TEST_FAILURES}${TEST_FAILURES:+${nl}}"
636 TEST_FAILURES="${TEST_FAILURES}${TEST_ID}[$TEST_NUM]:"
637 TEST_FAILURES="${TEST_FAILURES} Test of '$1' failed.";
638 TEST_FAILURES="${TEST_FAILURES}${nl}${MSG}"
639 TEST_FAIL_COUNT=$(( $TEST_FAIL_COUNT + 1 ))
640 return 0
641 }
642 $fail && atf_fail "Test[$TEST_NUM] failed: $(
643 # ATF does not like newlines in messages, so change them...
644 printf '%s' "${MSG}" | tr '\n' ';'
645 )"
646 return 0
647 }
648
649 results()
650 {
651 test -n "$1" && atf_expect_fail "$1"
652
653 test -z "${TEST_ID}" && return 0
654 test -z "${TEST_FAILURES}" && return 0
655
656 echo >&2 "=========================================="
657 echo >&2 "While testing '${TEST_ID}'"
658 echo >&2 " - - - - - - - - - - - - - - - - -"
659 echo >&2 "${TEST_FAILURES}"
660
661 atf_fail \
662 "Test ${TEST_ID}: $TEST_FAIL_COUNT (of $TEST_NUM) subtests failed - see stderr"
663 }
664
665 ####### End helpers
666
667 atf_test_case shell_params
668 shell_params_head() {
669 atf_set "descr" "Test correct operation of the numeric parameters"
670 }
671 shell_params_body() {
672 atf_require_prog mktemp
673
674 reset shell_params
675
676 check 'set -- a b c; echo "$#: $1 $2 $3"' '3: a b c' 0
677 check 'set -- a b c d e f g h i j k l m; echo "$#: ${1}0 ${10} $10"' \
678 '13: a0 j a0' 0
679 check 'x="$0"; set -- a b; y="$0";
680 [ "x${x}y" = "x${y}y" ] && echo OK || echo x="$x" y="$y"' \
681 'OK' 0
682 check "${TEST_SH} -c 'echo 0=\$0 1=\$1 2=\$2' a b c" '0=a 1=b 2=c' 0
683
684 echo 'echo 0="$0" 1="$1" 2="$2"' > helper.sh
685 check "${TEST_SH} helper.sh a b c" '0=helper.sh 1=a 2=b' 0
686
687 check 'set -- a bb ccc dddd eeeee ffffff ggggggg hhhhhhhh \
688 iiiiiiiii jjjjjjjjjj kkkkkkkkkkk
689 echo "${#}: ${#1} ${#2} ${#3} ${#4} ... ${#9} ${#10} ${#11}"' \
690 '11: 1 2 3 4 ... 9 10 11' 0
691
692 check 'set -- a b c; echo "$#: ${1-A} ${2-B} ${3-C} ${4-D} ${5-E}"' \
693 '3: a b c D E' 0
694 check 'set -- a "" c "" e
695 echo "$#: ${1:-A} ${2:-B} ${3:-C} ${4:-D} ${5:-E}"' \
696 '5: a B c D e' 0
697 check 'set -- a "" c "" e
698 echo "$#: ${1:+A} ${2:+B} ${3:+C} ${4:+D} ${5:+E}"' \
699 '5: A C E' 0
700 check 'set -- "abab*cbb"
701 echo "${1} ${1#a} ${1%b} ${1##ab} ${1%%b} ${1#*\*} ${1%\**}"' \
702 'abab*cbb bab*cbb abab*cb ab*cbb abab*cb cbb abab' 0
703 check 'set -- "abab?cbb"
704 echo "${1}:${1#*a}+${1%b*}-${1##*a}_${1%%b*}%${1#[ab]}=${1%?*}/${1%\?*}"' \
705 'abab?cbb:bab?cbb+abab?cb-b?cbb_a%bab?cbb=abab?cb/abab' 0
706 check 'set -- a "" c "" e; echo "${2:=b}"' '' 1
707
708 check 'set -- a b c d; echo ${4294967297}' '' 0 # result 'a' => ${1}
709 check 'set -- a b c; echo ${01}' 'a' 0
710 check "${TEST_SH} -c 'echo 0=\${00} 1=\${01} 2=\${02}' a b c" \
711 '0=a 1=b 2=c' 0
712
713 # by special request, for PaulG... (${0...} is not octal!)
714
715 # Posix XCU 2.5.1 (Issue 7 TC2 pg 2349 lines 74835..6):
716 # The digits denoting the positional parameters shall always
717 # be interpreted as a decimal value, even if there is a leading zero.
718
719 check \
720 'set -- a b c d e f g h i j k l m; echo "$#: ${08} ${010} ${011}"' \
721 '13: h j k' 0
722
723 results
724 }
725
726 atf_test_case var_with_embedded_cmdsub
727 var_with_embedded_cmdsub_head() {
728 atf_set "descr" "Test expansion of vars with embedded cmdsub"
729 }
730 var_with_embedded_cmdsub_body() {
731
732 reset var_with_embedded_cmdsub
733
734 check 'unset x; echo ${x-$(echo a)}$(echo b)' 'ab' 0 #1
735 check 'unset x; echo ${x:-$(echo a)}$(echo b)' 'ab' 0 #2
736 check 'x=""; echo ${x-$(echo a)}$(echo b)' 'b' 0 #3
737 check 'x=""; echo ${x:-$(echo a)}$(echo b)' 'ab' 0 #4
738 check 'x=c; echo ${x-$(echo a)}$(echo b)' 'cb' 0 #5
739 check 'x=c; echo ${x:-$(echo a)}$(echo b)' 'cb' 0 #6
740
741 check 'unset x; echo ${x+$(echo a)}$(echo b)' 'b' 0 #7
742 check 'unset x; echo ${x:+$(echo a)}$(echo b)' 'b' 0 #8
743 check 'x=""; echo ${x+$(echo a)}$(echo b)' 'ab' 0 #9
744 check 'x=""; echo ${x:+$(echo a)}$(echo b)' 'b' 0 #10
745 check 'x=c; echo ${x+$(echo a)}$(echo b)' 'ab' 0 #11
746 check 'x=c; echo ${x:+$(echo a)}$(echo b)' 'ab' 0 #12
747
748 check 'unset x; echo ${x=$(echo a)}$(echo b)' 'ab' 0 #13
749 check 'unset x; echo ${x:=$(echo a)}$(echo b)' 'ab' 0 #14
750 check 'x=""; echo ${x=$(echo a)}$(echo b)' 'b' 0 #15
751 check 'x=""; echo ${x:=$(echo a)}$(echo b)' 'ab' 0 #16
752 check 'x=c; echo ${x=$(echo a)}$(echo b)' 'cb' 0 #17
753 check 'x=c; echo ${x:=$(echo a)}$(echo b)' 'cb' 0 #18
754
755 check 'unset x; echo ${x?$(echo a)}$(echo b)' '' 2 #19
756 check 'unset x; echo ${x:?$(echo a)}$(echo b)' '' 2 #20
757 check 'x=""; echo ${x?$(echo a)}$(echo b)' 'b' 0 #21
758 check 'x=""; echo ${x:?$(echo a)}$(echo b)' '' 2 #22
759 check 'x=c; echo ${x?$(echo a)}$(echo b)' 'cb' 0 #23
760 check 'x=c; echo ${x:?$(echo a)}$(echo b)' 'cb' 0 #24
761
762 check 'unset x; echo ${x%$(echo a)}$(echo b)' 'b' 0 #25
763 check 'unset x; echo ${x%%$(echo a)}$(echo b)' 'b' 0 #26
764 check 'x=""; echo ${x%$(echo a)}$(echo b)' 'b' 0 #27
765 check 'x=""; echo ${x%%$(echo a)}$(echo b)' 'b' 0 #28
766 check 'x=c; echo ${x%$(echo a)}$(echo b)' 'cb' 0 #29
767 check 'x=c; echo ${x%%$(echo a)}$(echo b)' 'cb' 0 #30
768 check 'x=aa; echo ${x%$(echo "*a")}$(echo b)' 'ab' 0 #31
769 check 'x=aa; echo ${x%%$(echo "*a")}$(echo b)' 'b' 0 #32
770
771 check 'unset x; echo ${x#$(echo a)}$(echo b)' 'b' 0 #33
772 check 'unset x; echo ${x##$(echo a)}$(echo b)' 'b' 0 #34
773 check 'x=""; echo ${x#$(echo a)}$(echo b)' 'b' 0 #35
774 check 'x=""; echo ${x##$(echo a)}$(echo b)' 'b' 0 #36
775 check 'x=c; echo ${x#$(echo a)}$(echo b)' 'cb' 0 #37
776 check 'x=c; echo ${x##$(echo a)}$(echo b)' 'cb' 0 #38
777 check 'x=aa; echo ${x#$(echo "*a")}$(echo b)' 'ab' 0 #39
778 check 'x=aa; echo ${x##$(echo "*a")}$(echo b)' 'b' 0 #40
779
780 results
781 }
782
783 atf_test_case dollar_hash
784 dollar_hash_head() {
785 atf_set "descr" 'Test expansion of various aspects of $#'
786 }
787 dollar_hash_body() {
788
789 #
790 # $# looks like it should be so simple that it doesn't really
791 # need a test of its own, and used in that way, it really doesn't.
792 # But when we add braces ${#} we need to deal with the three
793 # (almost 4) different meanings of a # inside a ${} expansion...
794 #
795 # Note that some of these are just how we treat expansions that
796 # are unspecified by posix (as noted below.)
797 #
798 # 1. ${#} is just $# (number of params)
799 # 1.a ${\#} is nothing at all (error: invalid expansion)
800 # 1.b ${\#...} (anything after) is the same (invalid)
801 # 2. ${#VAR} is the length of the value VAR
802 # 2.a Including ${##} - the length of ${#}
803 # 3 ${VAR#pat} is the value of VAR with leading pat removed
804 # 3.a Including ${VAR#} which just removes leading nothing
805 # This is relevant in case of ${VAR#${X}} with X=''
806 # nb: not required by posix, see XCU 2.6.2
807 # 3.b ${##} is not a case of 3.a but rather 2.a
808 # 3.c Yet ${##pat} is a case of 3.a
809 # Including ${##${X}} where X='' or X='#'
810 # nb: not required by posix, see XCU 2.6.2
811 # 3.d And ${#\#} is invalid (error)
812 # 3.e But ${##\#} removes a leading # from the value of $#
813 # (so is just $# as there is no leading # there)
814 # nb: not required by posix, see XCU 2.6.2
815 # 4 ${VAR##pat} is the value of VAR with longest pat removed
816 # 4.a Including ${VAR##} which removes the longest nothing
817 # 4.b Which in this case includes ${###} (so is == $#)
818 # nb: not required by posix, see XCU 2.6.2
819 # 4.c But not ${##\#} which is $# with a leading '#' removed
820 # (and so is also == $#), i.e.: like ${###} but different.
821 # nb: not required by posix, see XCU 2.6.2
822 # 4.d As is ${###\#} or just ${####} - remove # (so just $#)
823 # nb: not required by posix, see XCU 2.6.2
824 #
825
826 reset dollar_hash
827
828 check 'set -- ; echo $#' '0' 0 # 1
829 check 'set -- a b c; echo $#' '3' 0 # 2
830 check 'set -- a b c d e f g h i j; echo $#' '10' 0 # 3
831 # rule 1
832 check 'set -- ; echo ${#}' '0' 0 # 4
833 check 'set -- a b c; echo ${#}' '3' 0 # 5
834 check 'set -- a b c d e f g h i j; echo ${#}' '10' 0 # 6
835 # rule 1.a
836 check 'set -- a b c; echo ${\#}' '' 2 # 7
837 # rule 1.b
838 check 'set -- a b c; echo ${\#:-foo}' '' 2 # 8
839 # rule 2
840 check 'VAR=12345; echo ${#VAR}' '5' 0 # 9
841 check 'VAR=123456789012; echo ${#VAR}' '12' 0 #10
842 # rule 2.a
843 check 'set -- ; echo ${##}' '1' 0 #11
844 check 'set -- a b c; echo ${##}' '1' 0 #12
845 check 'set -- a b c d e f g h i j; echo ${##}' '2' 0 #13
846 # rule 3
847 check 'VAR=12345; echo ${VAR#1}' '2345' 0 #14
848 check 'VAR=12345; echo ${VAR#2}' '12345' 0 #15
849 check 'VAR=#2345; echo ${VAR#\#}' '2345' 0 #16
850 check 'X=1; VAR=12345; echo ${VAR#${X}}' '2345' 0 #17
851 check 'X=1; VAR=#2345; echo ${VAR#${X}}' '#2345' 0 #18
852 # rule 3.a
853 check 'VAR=12345; echo ${VAR#}' '12345' 0 #19
854 check 'X=; VAR=12345; echo ${VAR#${X}}' '12345' 0 #20
855 # rule 3.b (tested above, rule 2.a)
856 # rule 3.c
857 check 'set -- ; echo ${##0}' '' 0 #21
858 check 'set -- a b c; echo ${##1}' '3' 0 #22
859 check 'set -- a b c d e f g h i j; echo ${##1}' '0' 0 #23
860 check 'X=0; set -- ; echo ${##${X}}' '' 0 #24
861 check 'X=; set -- ; echo ${##${X}}' '0' 0 #25
862 check 'X=1; set -- a b c; echo ${##${X}}' '3' 0 #26
863 check 'X=1; set -- a b c d e f g h i j; echo ${##${X}}' '0' 0 #27
864 check 'X=; set -- a b c d e f g h i j; echo ${##${X}}' '10' 0 #28
865 check 'X=#; VAR=#2345; echo ${VAR#${X}}' '2345' 0 #29
866 check 'X=#; VAR=12345; echo ${VAR#${X}}' '12345' 0 #30
867 # rule 3.d
868 check 'set -- a b c; echo ${#\#}' '' 2 #31
869 # rule 3.e
870 check 'set -- ; echo ${##\#}' '0' 0 #32
871 check 'set -- a b c d e f g h i j; echo ${##\#}' '10' 0 #33
872
873 # rule 4
874 check 'VAR=12345; echo ${VAR##1}' '2345' 0 #34
875 check 'VAR=12345; echo ${VAR##\1}' '2345' 0 #35
876 # rule 4.a
877 check 'VAR=12345; echo ${VAR##}' '12345' 0 #36
878 # rule 4.b
879 check 'set -- ; echo ${###}' '0' 0 #37
880 check 'set -- a b c d e f g h i j; echo ${###}' '10' 0 #38
881 # rule 4.c
882 check 'VAR=12345; echo ${VAR#\#}' '12345' 0 #39
883 check 'VAR=12345; echo ${VAR#\#1}' '12345' 0 #40
884 check 'VAR=#2345; echo ${VAR#\#}' '2345' 0 #41
885 check 'VAR=#12345; echo ${VAR#\#1}' '2345' 0 #42
886 check 'VAR=#2345; echo ${VAR#\#1}' '#2345' 0 #43
887 check 'set -- ; echo ${####}' '0' 0 #44
888 check 'set -- ; echo ${###\#}' '0' 0 #45
889 check 'set -- a b c d e f g h i j; echo ${####}' '10' 0 #46
890 check 'set -- a b c d e f g h i j; echo ${###\#}' '10' 0 #47
891
892 # now check for some more utter nonsense, not mentioned in the rules
893 # above (doesn't need to be)
894
895 check 'x=hello; set -- a b c; echo ${#x:-1}' '' 2 #48
896 check 'x=hello; set -- a b c; echo ${#x-1}' '' 2 #49
897 check 'x=hello; set -- a b c; echo ${#x:+1}' '' 2 #50
898 check 'x=hello; set -- a b c; echo ${#x+1}' '' 2 #51
899 check 'x=hello; set -- a b c; echo ${#x+1}' '' 2 #52
900 check 'x=hello; set -- a b c; echo ${#x:?msg}' '' 2 #53
901 check 'x=hello; set -- a b c; echo ${#x?msg}' '' 2 #54
902 check 'x=hello; set -- a b c; echo ${#x:=val}' '' 2 #55
903 check 'x=hello; set -- a b c; echo ${#x=val}' '' 2 #56
904 check 'x=hello; set -- a b c; echo ${#x#h}' '' 2 #57
905 check 'x=hello; set -- a b c; echo ${#x#*l}' '' 2 #58
906 check 'x=hello; set -- a b c; echo ${#x##*l}' '' 2 #59
907 check 'x=hello; set -- a b c; echo ${#x%o}' '' 2 #60
908 check 'x=hello; set -- a b c; echo ${#x%l*}' '' 2 #61
909 check 'x=hello; set -- a b c; echo ${#x%%l*}' '' 2 #62
910
911 # but just to be complete, these ones should work
912
913 check 'x=hello; set -- a b c; echo ${#%5}' '3' 0 #63
914 check 'x=hello; set -- a b c; echo ${#%3}' '' 0 #64
915 check 'x=hello; set -- a b c; echo ${#%?}' '' 0 #65
916 check 'X=#; set -- a b c; echo ${#%${X}}' '3' 0 #66
917 check 'X=3; set -- a b c; echo ${#%${X}}' '' 0 #67
918 check 'set -- a b c; echo ${#%%5}' '3' 0 #68
919 check 'set -- a b c; echo ${#%%3}' '' 0 #69
920 check 'set -- a b c d e f g h i j k l; echo ${#%1}' '12' 0 #70
921 check 'set -- a b c d e f g h i j k l; echo ${#%2}' '1' 0 #71
922 check 'set -- a b c d e f g h i j k l; echo ${#%?}' '1' 0 #72
923 check 'set -- a b c d e f g h i j k l; echo ${#%[012]}' '1' 0 #73
924 check 'set -- a b c d e f g h i j k l; echo ${#%[0-4]}' '1' 0 #74
925 check 'set -- a b c d e f g h i j k l; echo ${#%?2}' '' 0 #75
926 check 'set -- a b c d e f g h i j k l; echo ${#%1*}' '' 0 #76
927 check 'set -- a b c d e f g h i j k l; echo ${#%%2}' '1' 0 #77
928 check 'set -- a b c d e f g h i j k l; echo ${#%%1*}' '' 0 #78
929
930 # and this lot are stupid, as $# is never unset or null, but they do work...
931
932 check 'set -- a b c; echo ${#:-99}' '3' 0 #79
933 check 'set -- a b c; echo ${#-99}' '3' 0 #80
934 check 'set -- a b c; echo ${#:+99}' '99' 0 #81
935 check 'set -- a b c; echo ${#+99}' '99' 0 #82
936 check 'set -- a b c; echo ${#:?bogus}' '3' 0 #83
937 check 'set -- a b c; echo ${#?bogus}' '3' 0 #84
938
939 # even this utter nonsense is OK, as while special params cannot be
940 # set this way, here, as $# is not unset, or null, the assignment
941 # never happens (isn't even attempted)
942
943 check 'set -- a b c; echo ${#:=bogus}' '3' 0 #85
944 check 'set -- a b c; echo ${#=bogus}' '3' 0 #86
945
946 for n in 0 1 10 25 100 #87 #88 #89 #90 #91
947 do
948 check "(exit $n)"'; echo ${#?}' "${#n}" 0
949 done
950
951 results # results so far anyway...
952
953 # now we have some harder to verify cases, as they (must) check unknown values
954 # and hence the resuls cannot just be built into the script, but we have
955 # to use some tricks to validate them, so for these, just do regular testing
956 # and don't attempt to use the check helper function, nor to include
957 # these tests in the result summary. If anything has already failed, we
958 # do not get this far... From here on, first failure ends this test.
959
960 for opts in '' '-a' '-au' '-auf' '-aufe' # options safe enough to set
961 do
962 # Note the shell might have other (or these) opts set already
963
964 RES=$(${TEST_SH} -c "test -n '${opts}' && set ${opts};
965 printf '%s' \"\$-\";printf ' %s\\n' \"\${#-}\"") ||
966 atf_fail '${#-} test exited with status '"$?"
967 LEN="${RES##* }"
968 DMINUS="${RES% ${LEN}}"
969 if [ "${#DMINUS}" != "${LEN}" ]
970 then
971 atf_fail \
972 '${#-} test'" produced ${LEN} for opts ${DMINUS} (${RES})"
973 fi
974 done
975
976 for seq in a b c d e f g h i j
977 do
978 # now we are tryin to generate different pids for $$ and $!
979 # so we just run the test a number of times, and hope...
980 # On NetBSD pid randomisation will usually help the tests
981
982 eval "$(${TEST_SH} -c \
983 '(exit 0)& BG=$! LBG=${#!};
984 printf "SH=%s BG=%s LSH=%s LBG=%s" "$$" "$BG" "${#$}" "$LBG";
985 wait')"
986
987 if [ "${#SH}" != "${LSH}" ] || [ "${#BG}" != "${LBG}" ]
988 then
989 atf_fail \
990 '${#!] of '"${BG} was ${LBG}, expected ${#BG}"'; ${#$} of '"${SH} was ${LSH}, expected ${#SH}"
991 fi
992 done
993 }
994
995 atf_test_case dollar_star
996 dollar_star_head() {
997 atf_set "descr" 'Test expansion of various aspects of $*'
998 }
999 dollar_star_body() {
1000
1001 reset dollar_star
1002
1003 check 'set -- a b c; echo $# $*' '3 a b c' 0 # 1
1004 check 'set -- a b c; echo $# "$*"' '3 a b c' 0 # 2
1005 check 'set -- a "b c"; echo $# $*' '2 a b c' 0 # 3
1006 check 'set -- a "b c"; echo $# "$*"' '2 a b c' 0 # 4
1007 check 'set -- a b c; set -- $* ; echo $# $*' '3 a b c' 0 # 5
1008 check 'set -- a b c; set -- "$*" ; echo $# $*' '1 a b c' 0 # 6
1009 check 'set -- a "b c"; set -- $* ; echo $# $*' '3 a b c' 0 # 7
1010 check 'set -- a "b c"; set -- "$*" ; echo $# $*' \
1011 '1 a b c' 0 # 8
1012
1013 check 'IFS=". "; set -- a b c; echo $# $*' '3 a b c' 0 # 9
1014 check 'IFS=". "; set -- a b c; echo $# "$*"' '3 a.b.c' 0 #10
1015 check 'IFS=". "; set -- a "b c"; echo $# $*' '2 a b c' 0 #11
1016 check 'IFS=". "; set -- a "b c"; echo $# "$*"' '2 a.b c' 0 #12
1017 check 'IFS=". "; set -- a "b.c"; echo $# $*' '2 a b c' 0 #13
1018 check 'IFS=". "; set -- a "b.c"; echo $# "$*"' '2 a.b.c' 0 #14
1019 check 'IFS=". "; set -- a b c; set -- $* ; echo $# $*' \
1020 '3 a b c' 0 #15
1021 check 'IFS=". "; set -- a b c; set -- "$*" ; echo $# $*' \
1022 '1 a b c' 0 #16
1023 check 'IFS=". "; set -- a "b c"; set -- $* ; echo $# $*' \
1024 '3 a b c' 0 #17
1025 check 'IFS=". "; set -- a "b c"; set -- "$*" ; echo $# $*' \
1026 '1 a b c' 0 #18
1027 check 'IFS=". "; set -- a b c; set -- $* ; echo $# "$*"' \
1028 '3 a.b.c' 0 #19
1029 check 'IFS=". "; set -- a b c; set -- "$*" ; echo $# "$*"' \
1030 '1 a.b.c' 0 #20
1031 check 'IFS=". "; set -- a "b c"; set -- $* ; echo $# "$*"' \
1032 '3 a.b.c' 0 #21
1033 check 'IFS=". "; set -- a "b c"; set -- "$*" ; echo $# "$*"' \
1034 '1 a.b c' 0 #22
1035
1036 results
1037 }
1038
1039 atf_test_case dollar_star_in_word
1040 dollar_star_in_word_head() {
1041 atf_set "descr" 'Test expansion $* occurring in word of ${var:-word}'
1042 }
1043 dollar_star_in_word_body() {
1044
1045 reset dollar_star_in_word
1046
1047 unset xXx ; # just in case!
1048
1049 # Note that the expected results for these tests are identical
1050 # to those from the dollar_star test. It should never make
1051 # a difference whether we expand $* or ${unset:-$*}
1052
1053 # (note expanding ${unset:-"$*"} is different, that is not tested here)
1054
1055 check 'set -- a b c; echo $# ${xXx:-$*}' '3 a b c' 0 # 1
1056 check 'set -- a b c; echo $# "${xXx:-$*}"' '3 a b c' 0 # 2
1057 check 'set -- a "b c"; echo $# ${xXx:-$*}' '2 a b c' 0 # 3
1058 check 'set -- a "b c"; echo $# "${xXx:-$*}"' '2 a b c' 0 # 4
1059 check 'set -- a b c; set -- ${xXx:-$*} ; echo $# $*' '3 a b c' 0 # 5
1060 check 'set -- a b c; set -- "${xXx:-$*}" ; echo $# $*' '1 a b c' 0 # 6
1061 check 'set -- a "b c"; set -- ${xXx:-$*} ; echo $# $*' '3 a b c' 0 # 7
1062 check 'set -- a "b c"; set -- "${xXx:-$*}" ; echo $# $*' \
1063 '1 a b c' 0 # 8
1064
1065 check 'IFS=". "; set -- a b c; echo $# ${xXx:-$*}' '3 a b c' 0 # 9
1066 check 'IFS=". "; set -- a b c; echo $# "${xXx:-$*}"' '3 a.b.c' 0 #10
1067 check 'IFS=". "; set -- a "b c"; echo $# ${xXx:-$*}' '2 a b c' 0 #11
1068 check 'IFS=". "; set -- a "b c"; echo $# "${xXx:-$*}"' '2 a.b c' 0 #12
1069 check 'IFS=". "; set -- a "b.c"; echo $# ${xXx:-$*}' '2 a b c' 0 #13
1070 check 'IFS=". "; set -- a "b.c"; echo $# "${xXx:-$*}"' '2 a.b.c' 0 #14
1071 check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
1072 '3 a b c' 0 #15
1073 check 'IFS=". ";set -- a b c;set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
1074 '1 a b c' 0 #16
1075 check 'IFS=". ";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
1076 '3 a b c' 0 #17
1077 check 'IFS=". ";set -- a "b c";set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
1078 '1 a b c' 0 #18
1079 check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
1080 '3 a.b.c' 0 #19
1081 check 'IFS=". ";set -- a b c;set -- "$*";echo $# "$*"' \
1082 '1 a.b.c' 0 #20
1083 check 'IFS=". ";set -- a "b c";set -- $*;echo $# "$*"' \
1084 '3 a.b.c' 0 #21
1085 check 'IFS=". ";set -- a "b c";set -- "$*";echo $# "$*"' \
1086 '1 a.b c' 0 #22
1087
1088 results
1089 }
1090
1091 atf_test_case dollar_star_with_empty_ifs
1092 dollar_star_with_empty_ifs_head() {
1093 atf_set "descr" 'Test expansion of $* with IFS=""'
1094 }
1095 dollar_star_with_empty_ifs_body() {
1096
1097 reset dollar_star_with_empty_ifs
1098
1099 check 'IFS=""; set -- a b c; echo $# $*' '3 a b c' 0 # 1
1100 check 'IFS=""; set -- a b c; echo $# "$*"' '3 abc' 0 # 2
1101 check 'IFS=""; set -- a "b c"; echo $# $*' '2 a b c' 0 # 3
1102 check 'IFS=""; set -- a "b c"; echo $# "$*"' '2 ab c' 0 # 4
1103 check 'IFS=""; set -- a "b.c"; echo $# $*' '2 a b.c' 0 # 5
1104 check 'IFS=""; set -- a "b.c"; echo $# "$*"' '2 ab.c' 0 # 6
1105 check 'IFS=""; set -- a b c; set -- $* ; echo $# $*' \
1106 '3 a b c' 0 # 7
1107 check 'IFS=""; set -- a b c; set -- "$*" ; echo $# $*' \
1108 '1 abc' 0 # 8
1109 check 'IFS=""; set -- a "b c"; set -- $* ; echo $# $*' \
1110 '2 a b c' 0 # 9
1111 check 'IFS=""; set -- a "b c"; set -- "$*" ; echo $# $*' \
1112 '1 ab c' 0 #10
1113 check 'IFS=""; set -- a b c; set -- $* ; echo $# "$*"' \
1114 '3 abc' 0 #11
1115 check 'IFS=""; set -- a b c; set -- "$*" ; echo $# "$*"' \
1116 '1 abc' 0 #12
1117 check 'IFS=""; set -- a "b c"; set -- $* ; echo $# "$*"' \
1118 '2 ab c' 0 #13
1119 check 'IFS=""; set -- a "b c"; set -- "$*" ; echo $# "$*"' \
1120 '1 ab c' 0 #14
1121
1122 results # FIXED: 'PR bin/52090 expect 7 of 14 subtests to fail'
1123 }
1124
1125 atf_test_case dollar_star_in_word_empty_ifs
1126 dollar_star_in_word_empty_ifs_head() {
1127 atf_set "descr" 'Test expansion of ${unset:-$*} with IFS=""'
1128 }
1129 dollar_star_in_word_empty_ifs_body() {
1130
1131 reset dollar_star_in_word_empty_ifs
1132
1133 unset xXx ; # just in case
1134
1135 # Note that the expected results for these tests are identical
1136 # to those from the dollar_star_with_empty_ifs test. It should
1137 # never make a difference whether we expand $* or ${unset:-$*}
1138
1139 # (note expanding ${unset:-"$*"} is different, that is not tested here)
1140
1141 check 'IFS="";set -- a b c;echo $# ${xXx:-$*}' '3 a b c' 0 # 1
1142 check 'IFS="";set -- a b c;echo $# "${xXx:-$*}"' '3 abc' 0 # 2
1143 check 'IFS="";set -- a "b c";echo $# ${xXx:-$*}' '2 a b c' 0 # 3
1144 check 'IFS="";set -- a "b c";echo $# "${xXx:-$*}"' '2 ab c' 0 # 4
1145 check 'IFS="";set -- a "b.c";echo $# ${xXx:-$*}' '2 a b.c' 0 # 5
1146 check 'IFS="";set -- a "b.c";echo $# "${xXx:-$*}"' '2 ab.c' 0 # 6
1147 check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
1148 '3 a b c' 0 # 7
1149 check 'IFS="";set -- a b c;set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
1150 '1 abc' 0 # 8
1151 check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
1152 '2 a b c' 0 # 9
1153 check 'IFS="";set -- a "b c";set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
1154 '1 ab c' 0 #10
1155 check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
1156 '3 abc' 0 #11
1157 check 'IFS="";set -- a b c;set -- "${xXx:-$*}";echo $# "${xXx:-$*}"' \
1158 '1 abc' 0 #12
1159 check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
1160 '2 ab c' 0 #13
1161 check 'IFS="";set -- a "b c";set -- "${xXx:-$*}";echo $# "${xXx:-$*}"' \
1162 '1 ab c' 0 #14
1163
1164 results # FIXED: 'PR bin/52090 expect 7 of 14 subtests to fail'
1165 }
1166
1167 atf_test_case dollar_star_in_quoted_word
1168 dollar_star_in_quoted_word_head() {
1169 atf_set "descr" 'Test expansion $* occurring in word of ${var:-"word"}'
1170 }
1171 dollar_star_in_quoted_word_body() {
1172
1173 reset dollar_star_in_quoted_word
1174
1175 unset xXx ; # just in case!
1176
1177 check 'set -- a b c; echo $# ${xXx:-"$*"}' '3 a b c' 0 # 1
1178 check 'set -- a "b c"; echo $# ${xXx:-"$*"}' '2 a b c' 0 # 2
1179 check 'set -- a b c; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
1180 '1 a b c' 0 # 3
1181 check 'set -- a "b c"; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
1182 '1 a b c' 0 # 4
1183 check 'set -- a b c; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
1184 '1 a b c' 0 # 5
1185 check 'set -- a "b c"; set -- ${xXx:-"$*"} ; echo $# ${xXx-$*}' \
1186 '1 a b c' 0 # 6
1187 check 'set -- a b c; set -- ${xXx:-$*} ; echo $# ${xXx-"$*"}' \
1188 '3 a b c' 0 # 7
1189 check 'set -- a "b c"; set -- ${xXx:-$*} ; echo $# ${xXx-"$*"}' \
1190 '3 a b c' 0 # 8
1191
1192 check 'IFS=". "; set -- a b c; echo $# ${xXx:-"$*"}' '3 a.b.c' 0 # 9
1193 check 'IFS=". "; set -- a "b c"; echo $# ${xXx:-"$*"}' '2 a.b c' 0 #10
1194 check 'IFS=". "; set -- a "b.c"; echo $# ${xXx:-"$*"}' '2 a.b.c' 0 #11
1195 check 'IFS=". ";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
1196 '1 a.b.c' 0 #12
1197 check 'IFS=". ";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
1198 '1 a.b c' 0 #13
1199 check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
1200 '3 a.b.c' 0 #14
1201 check 'IFS=". ";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
1202 '3 a.b.c' 0 #15
1203 check 'IFS=". ";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
1204 '1 a b c' 0 #16
1205 check 'IFS=". ";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
1206 '1 a b c' 0 #17
1207
1208 check 'IFS="";set -- a b c;echo $# ${xXx:-"$*"}' '3 abc' 0 #18
1209 check 'IFS="";set -- a "b c";echo $# ${xXx:-"$*"}' '2 ab c' 0 #19
1210 check 'IFS="";set -- a "b.c";echo $# ${xXx:-"$*"}' '2 ab.c' 0 #20
1211 check 'IFS="";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
1212 '1 abc' 0 #21
1213 check 'IFS="";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
1214 '1 ab c' 0 #22
1215 check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
1216 '3 abc' 0 #23
1217 check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
1218 '2 ab c' 0 #24
1219 check 'IFS="";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
1220 '1 abc' 0 #25
1221 check 'IFS="";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
1222 '1 ab c' 0 #26
1223
1224 results # FIXED: 'PR bin/52090 - 2 of 26 subtests expected to fail'
1225 }
1226
1227 atf_test_case embedded_nl
1228 embedded_nl_head() {
1229 atf_set "descr" 'Test literal \n in xxx string in ${var-xxx}'
1230 }
1231 embedded_nl_body() {
1232
1233 atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} <<- 'EOF'
1234 unset V
1235 X="${V-a
1236 b}"
1237 printf '%s\n' "${X}"
1238 EOF
1239
1240 atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} <<- 'EOF'
1241 unset V
1242 X=${V-"a
1243 b"}
1244 printf '%s\n' "${X}"
1245 EOF
1246
1247 # This should not generate a syntax error, see PR bin/53201
1248 atf_check -s exit:0 -o inline:'abc\n' -e empty ${TEST_SH} <<- 'EOF'
1249 V=abc
1250 X=${V-a
1251 b}
1252 printf '%s\n' "${X}"
1253 EOF
1254
1255 # Nor should any of these...
1256 atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} <<- 'EOF'
1257 unset V
1258 X=${V-a
1259 b}
1260 printf '%s\n' "${X}"
1261 EOF
1262
1263 atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} <<- 'EOF'
1264 unset V
1265 X=${V:=a
1266 b}
1267 printf '%s\n' "${X}"
1268 EOF
1269
1270 atf_check -s exit:0 -o inline:'xa\nby\na\nb\n' -e empty \
1271 ${TEST_SH} <<- 'EOF'
1272 unset V
1273 X=x${V:=a
1274 b}y
1275 printf '%s\n' "${X}" "${V}"
1276 EOF
1277 }
1278
1279 atf_init_test_cases() {
1280 # Listed here in the order ATF runs them, not the order from above
1281
1282 atf_add_test_case arithmetic
1283 atf_add_test_case dollar_at
1284 atf_add_test_case dollar_at_unquoted_or_conditional
1285 atf_add_test_case dollar_at_with_text
1286 atf_add_test_case dollar_at_empty_and_conditional
1287 atf_add_test_case dollar_hash
1288 atf_add_test_case dollar_star
1289 atf_add_test_case dollar_star_in_quoted_word
1290 atf_add_test_case dollar_star_in_word
1291 atf_add_test_case dollar_star_in_word_empty_ifs
1292 atf_add_test_case dollar_star_with_empty_ifs
1293 atf_add_test_case embedded_nl
1294 atf_add_test_case iteration_on_null_parameter
1295 atf_add_test_case iteration_on_quoted_null_parameter
1296 atf_add_test_case iteration_on_null_or_null_parameter
1297 atf_add_test_case iteration_on_null_or_missing_parameter
1298 atf_add_test_case shell_params
1299 atf_add_test_case strip
1300 atf_add_test_case tilde
1301 atf_add_test_case wrap_strip
1302 atf_add_test_case var_with_embedded_cmdsub
1303 atf_add_test_case varpattern_backslashes
1304 }
1305