t_expand.sh revision 1.12 1 # $NetBSD: t_expand.sh,v 1.12 2017/05/14 11:28:49 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_with_text
67 dollar_at_with_text_head() {
68 atf_set "descr" "Test \$@ expansion when it is surrounded by text" \
69 "within the quotes. PR bin/33956."
70 }
71 dollar_at_with_text_body() {
72
73 cat <<'EOF' > h-f1
74
75 delim_argv() {
76 str=
77 while [ $# -gt 0 ]; do
78 if [ -z "${str}" ]; then
79 str=">$1<"
80 else
81 str="${str} >$1<"
82 fi
83 shift
84 done
85 echo "${str}"
86 }
87
88 EOF
89 cat <<'EOF' > h-f2
90
91 delim_argv() {
92 str=
93 while [ $# -gt 0 ]; do
94
95 str="${str}${str:+ }>$1<"
96 shift
97
98 done
99 echo "${str}"
100 }
101
102 EOF
103
104 chmod +x h-f1 h-f2
105
106 for f in 1 2
107 do
108 atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
109 ". ./h-f${f}; "'set -- ; delim_argv "$@"'
110 atf_check -s exit:0 -o inline:'>foobar<\n' -e empty \
111 ${TEST_SH} -c \
112 ". ./h-f${f}; "'set -- ; delim_argv "foo$@bar"'
113 atf_check -s exit:0 -o inline:'>foo bar<\n' -e empty \
114 ${TEST_SH} -c \
115 ". ./h-f${f}; "'set -- ; delim_argv "foo $@ bar"'
116
117 atf_check -s exit:0 -o inline:'>a< >b< >c<\n' -e empty \
118 ${TEST_SH} -c \
119 ". ./h-f${f}; "'set -- a b c; delim_argv "$@"'
120 atf_check -s exit:0 -o inline:'>fooa< >b< >cbar<\n' -e empty \
121 ${TEST_SH} -c \
122 ". ./h-f${f}; "'set -- a b c; delim_argv "foo$@bar"'
123 atf_check -s exit:0 -o inline:'>foo a< >b< >c bar<\n' -e empty \
124 ${TEST_SH} -c \
125 ". ./h-f${f}; "'set -- a b c; delim_argv "foo $@ bar"'
126 done
127 }
128
129 atf_test_case strip
130 strip_head() {
131 atf_set "descr" "Checks that the %% operator works and strips" \
132 "the contents of a variable from the given point" \
133 "to the end"
134 }
135 strip_body() {
136 line='#define bindir "/usr/bin" /* comment */'
137 stripped='#define bindir "/usr/bin" '
138
139 # atf_expect_fail "PR bin/43469" -- now fixed
140 for exp in \
141 '${line%%/\**}' \
142 '${line%%"/*"*}' \
143 '${line%%'"'"'/*'"'"'*}' \
144 '"${line%%/\**}"' \
145 '"${line%%"/*"*}"' \
146 '"${line%%'"'"'/*'"'"'*}"' \
147 '${line%/\**}' \
148 '${line%"/*"*}' \
149 '${line%'"'"'/*'"'"'*}' \
150 '"${line%/\**}"' \
151 '"${line%"/*"*}"' \
152 '"${line%'"'"'/*'"'"'*}"'
153 do
154 atf_check -o inline:":$stripped:\n" -e empty ${TEST_SH} -c \
155 "line='${line}'; echo :${exp}:"
156 done
157 }
158
159 atf_test_case varpattern_backslashes
160 varpattern_backslashes_head() {
161 atf_set "descr" "Tests that protecting wildcards with backslashes" \
162 "works in variable patterns."
163 }
164 varpattern_backslashes_body() {
165 line='/foo/bar/*/baz'
166 stripped='/foo/bar/'
167 atf_check -o inline:'/foo/bar/\n' -e empty ${TEST_SH} -c \
168 'line="/foo/bar/*/baz"; echo ${line%%\**}'
169 }
170
171 atf_test_case arithmetic
172 arithmetic_head() {
173 atf_set "descr" "POSIX requires shell arithmetic to use signed" \
174 "long or a wider type. We use intmax_t, so at" \
175 "least 64 bits should be available. Make sure" \
176 "this is true."
177 }
178 arithmetic_body() {
179
180 atf_check -o inline:'3' -e empty ${TEST_SH} -c \
181 'printf %s $((1 + 2))'
182 atf_check -o inline:'2147483647' -e empty ${TEST_SH} -c \
183 'printf %s $((0x7fffffff))'
184 atf_check -o inline:'9223372036854775807' -e empty ${TEST_SH} -c \
185 'printf %s $(((1 << 63) - 1))'
186 }
187
188 atf_test_case iteration_on_null_parameter
189 iteration_on_null_parameter_head() {
190 atf_set "descr" "Check iteration of \$@ in for loop when set to null;" \
191 "the error \"sh: @: parameter not set\" is incorrect." \
192 "PR bin/48202."
193 }
194 iteration_on_null_parameter_body() {
195 atf_check -o empty -e empty ${TEST_SH} -c \
196 'N=; set -- ${N}; for X; do echo "[$X]"; done'
197 }
198
199 atf_test_case iteration_on_quoted_null_parameter
200 iteration_on_quoted_null_parameter_head() {
201 atf_set "descr" \
202 'Check iteration of "$@" in for loop when set to null;'
203 }
204 iteration_on_quoted_null_parameter_body() {
205 atf_check -o inline:'[]\n' -e empty ${TEST_SH} -c \
206 'N=; set -- "${N}"; for X; do echo "[$X]"; done'
207 }
208
209 atf_test_case iteration_on_null_or_null_parameter
210 iteration_on_null_or_null_parameter_head() {
211 atf_set "descr" \
212 'Check expansion of null parameter as default for another null'
213 }
214 iteration_on_null_or_null_parameter_body() {
215 atf_check -o empty -e empty ${TEST_SH} -c \
216 'N=; E=; set -- ${N:-${E}}; for X; do echo "[$X]"; done'
217 }
218
219 atf_test_case iteration_on_null_or_missing_parameter
220 iteration_on_null_or_missing_parameter_head() {
221 atf_set "descr" \
222 'Check expansion of missing parameter as default for another null'
223 }
224 iteration_on_null_or_missing_parameter_body() {
225 # atf_expect_fail 'PR bin/50834'
226 atf_check -o empty -e empty ${TEST_SH} -c \
227 'N=; set -- ${N:-}; for X; do echo "[$X]"; done'
228 }
229
230 ####### The remaining tests use the following helper functions ...
231
232 nl='
233 '
234 reset()
235 {
236 TEST_NUM=0
237 TEST_FAILURES=''
238 TEST_FAIL_COUNT=0
239 TEST_ID="$1"
240 }
241
242 check()
243 {
244 fail=false
245 TEMP_FILE=$( mktemp OUT.XXXXXX )
246 TEST_NUM=$(( $TEST_NUM + 1 ))
247 MSG=
248
249 # our local shell (ATF_SHELL) better do quoting correctly...
250 # some of the tests expect us to expand $nl internally...
251 CMD="$1"
252
253 result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )"
254 STATUS=$?
255
256 if [ "${STATUS}" -ne "$3" ]; then
257 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
258 MSG="${MSG} expected exit code $3, got ${STATUS}"
259
260 # don't actually fail just because of wrong exit code
261 # unless we either expected, or received "good"
262 case "$3/${STATUS}" in
263 (*/0|0/*) fail=true;;
264 esac
265 fi
266
267 if [ "$3" -eq 0 ]; then
268 if [ -s "${TEMP_FILE}" ]; then
269 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
270 MSG="${MSG} Messages produced on stderr unexpected..."
271 MSG="${MSG}${nl}$( cat "${TEMP_FILE}" )"
272 fail=true
273 fi
274 else
275 if ! [ -s "${TEMP_FILE}" ]; then
276 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
277 MSG="${MSG} Expected messages on stderr,"
278 MSG="${MSG} nothing produced"
279 fail=true
280 fi
281 fi
282 rm -f "${TEMP_FILE}"
283
284 # Remove newlines (use local shell for this)
285 oifs="$IFS"
286 IFS="$nl"
287 result="$(echo $result)"
288 IFS="$oifs"
289 if [ "$2" != "$result" ]
290 then
291 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
292 MSG="${MSG} Expected output '$2', received '$result'"
293 fail=true
294 fi
295
296 if $fail
297 then
298 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
299 MSG="${MSG} Full command: <<${CMD}>>"
300 fi
301
302 $fail && test -n "$TEST_ID" && {
303 TEST_FAILURES="${TEST_FAILURES}${TEST_FAILURES:+${nl}}"
304 TEST_FAILURES="${TEST_FAILURES}${TEST_ID}[$TEST_NUM]:"
305 TEST_FAILURES="${TEST_FAILURES} Test of '$1' failed.";
306 TEST_FAILURES="${TEST_FAILURES}${nl}${MSG}"
307 TEST_FAIL_COUNT=$(( $TEST_FAIL_COUNT + 1 ))
308 return 0
309 }
310 $fail && atf_fail "Test[$TEST_NUM] of '$1' failed${nl}${MSG}"
311 return 0
312 }
313
314 results()
315 {
316 test -n "$1" && atf_expect_fail "$1"
317
318 test -z "${TEST_ID}" && return 0
319 test -z "${TEST_FAILURES}" && return 0
320
321 echo >&2 "=========================================="
322 echo >&2 "While testing '${TEST_ID}'"
323 echo >&2 " - - - - - - - - - - - - - - - - -"
324 echo >&2 "${TEST_FAILURES}"
325
326 atf_fail \
327 "Test ${TEST_ID}: $TEST_FAIL_COUNT (of $TEST_NUM) subtests failed - see stderr"
328 }
329
330 ####### End helpers
331
332 atf_test_case shell_params
333 shell_params_head() {
334 atf_set "descr" "Test correct operation of the numeric parameters"
335 }
336 shell_params_body() {
337 atf_require_prog mktemp
338
339 reset shell_params
340
341 check 'set -- a b c; echo "$#: $1 $2 $3"' '3: a b c' 0
342 check 'set -- a b c d e f g h i j k l m; echo "$#: ${1}0 ${10} $10"' \
343 '13: a0 j a0' 0
344 check 'x="$0"; set -- a b; y="$0";
345 [ "x${x}y" = "x${y}y" ] && echo OK || echo x="$x" y="$y"' \
346 'OK' 0
347 check "${TEST_SH} -c 'echo 0=\$0 1=\$1 2=\$2' a b c" '0=a 1=b 2=c' 0
348
349 echo 'echo 0="$0" 1="$1" 2="$2"' > helper.sh
350 check "${TEST_SH} helper.sh a b c" '0=helper.sh 1=a 2=b' 0
351
352 check 'set -- a bb ccc dddd eeeee ffffff ggggggg hhhhhhhh \
353 iiiiiiiii jjjjjjjjjj kkkkkkkkkkk
354 echo "${#}: ${#1} ${#2} ${#3} ${#4} ... ${#9} ${#10} ${#11}"' \
355 '11: 1 2 3 4 ... 9 10 11' 0
356
357 check 'set -- a b c; echo "$#: ${1-A} ${2-B} ${3-C} ${4-D} ${5-E}"' \
358 '3: a b c D E' 0
359 check 'set -- a "" c "" e
360 echo "$#: ${1:-A} ${2:-B} ${3:-C} ${4:-D} ${5:-E}"' \
361 '5: a B c D e' 0
362 check 'set -- a "" c "" e
363 echo "$#: ${1:+A} ${2:+B} ${3:+C} ${4:+D} ${5:+E}"' \
364 '5: A C E' 0
365 check 'set -- "abab*cbb"
366 echo "${1} ${1#a} ${1%b} ${1##ab} ${1%%b} ${1#*\*} ${1%\**}"' \
367 'abab*cbb bab*cbb abab*cb ab*cbb abab*cb cbb abab' 0
368 check 'set -- "abab?cbb"
369 echo "${1}:${1#*a}+${1%b*}-${1##*a}_${1%%b*}%${1#[ab]}=${1%?*}/${1%\?*}"' \
370 'abab?cbb:bab?cbb+abab?cb-b?cbb_a%bab?cbb=abab?cb/abab' 0
371 check 'set -- a "" c "" e; echo "${2:=b}"' '' 1
372
373 results
374 }
375
376 atf_test_case var_with_embedded_cmdsub
377 var_with_embedded_cmdsub_head() {
378 atf_set "descr" "Test expansion of vars with embedded cmdsub"
379 }
380 var_with_embedded_cmdsub_body() {
381
382 reset var_with_embedded_cmdsub
383
384 check 'unset x; echo ${x-$(echo a)}$(echo b)' 'ab' 0 #1
385 check 'unset x; echo ${x:-$(echo a)}$(echo b)' 'ab' 0 #2
386 check 'x=""; echo ${x-$(echo a)}$(echo b)' 'b' 0 #3
387 check 'x=""; echo ${x:-$(echo a)}$(echo b)' 'ab' 0 #4
388 check 'x=c; echo ${x-$(echo a)}$(echo b)' 'cb' 0 #5
389 check 'x=c; echo ${x:-$(echo a)}$(echo b)' 'cb' 0 #6
390
391 check 'unset x; echo ${x+$(echo a)}$(echo b)' 'b' 0 #7
392 check 'unset x; echo ${x:+$(echo a)}$(echo b)' 'b' 0 #8
393 check 'x=""; echo ${x+$(echo a)}$(echo b)' 'ab' 0 #9
394 check 'x=""; echo ${x:+$(echo a)}$(echo b)' 'b' 0 #10
395 check 'x=c; echo ${x+$(echo a)}$(echo b)' 'ab' 0 #11
396 check 'x=c; echo ${x:+$(echo a)}$(echo b)' 'ab' 0 #12
397
398 check 'unset x; echo ${x=$(echo a)}$(echo b)' 'ab' 0 #13
399 check 'unset x; echo ${x:=$(echo a)}$(echo b)' 'ab' 0 #14
400 check 'x=""; echo ${x=$(echo a)}$(echo b)' 'b' 0 #15
401 check 'x=""; echo ${x:=$(echo a)}$(echo b)' 'ab' 0 #16
402 check 'x=c; echo ${x=$(echo a)}$(echo b)' 'cb' 0 #17
403 check 'x=c; echo ${x:=$(echo a)}$(echo b)' 'cb' 0 #18
404
405 check 'unset x; echo ${x?$(echo a)}$(echo b)' '' 2 #19
406 check 'unset x; echo ${x:?$(echo a)}$(echo b)' '' 2 #20
407 check 'x=""; echo ${x?$(echo a)}$(echo b)' 'b' 0 #21
408 check 'x=""; echo ${x:?$(echo a)}$(echo b)' '' 2 #22
409 check 'x=c; echo ${x?$(echo a)}$(echo b)' 'cb' 0 #23
410 check 'x=c; echo ${x:?$(echo a)}$(echo b)' 'cb' 0 #24
411
412 check 'unset x; echo ${x%$(echo a)}$(echo b)' 'b' 0 #25
413 check 'unset x; echo ${x%%$(echo a)}$(echo b)' 'b' 0 #26
414 check 'x=""; echo ${x%$(echo a)}$(echo b)' 'b' 0 #27
415 check 'x=""; echo ${x%%$(echo a)}$(echo b)' 'b' 0 #28
416 check 'x=c; echo ${x%$(echo a)}$(echo b)' 'cb' 0 #29
417 check 'x=c; echo ${x%%$(echo a)}$(echo b)' 'cb' 0 #30
418 check 'x=aa; echo ${x%$(echo "*a")}$(echo b)' 'ab' 0 #31
419 check 'x=aa; echo ${x%%$(echo "*a")}$(echo b)' 'b' 0 #32
420
421 check 'unset x; echo ${x#$(echo a)}$(echo b)' 'b' 0 #33
422 check 'unset x; echo ${x##$(echo a)}$(echo b)' 'b' 0 #34
423 check 'x=""; echo ${x#$(echo a)}$(echo b)' 'b' 0 #35
424 check 'x=""; echo ${x##$(echo a)}$(echo b)' 'b' 0 #36
425 check 'x=c; echo ${x#$(echo a)}$(echo b)' 'cb' 0 #37
426 check 'x=c; echo ${x##$(echo a)}$(echo b)' 'cb' 0 #38
427 check 'x=aa; echo ${x#$(echo "*a")}$(echo b)' 'ab' 0 #39
428 check 'x=aa; echo ${x##$(echo "*a")}$(echo b)' 'b' 0 #40
429
430 results
431 }
432
433 atf_test_case dollar_hash
434 dollar_hash_head() {
435 atf_set "descr" 'Test expansion of various aspects of $#'
436 }
437 dollar_hash_body() {
438
439 #
440 # $# looks like it should be so simple that it doesn't really
441 # need a test of its own, and used in that way, it really doesn't.
442 # But when we add braces ${#} we need to deal with the three
443 # (almost 4) different meanings of a # inside a ${} expansion...
444 #
445 # 1. ${#} is just $# (number of params)
446 # 1.a ${\#} is nothing at all (error: invalid expansion)
447 # 1.b ${\#...} (anything after) is the same (invalid)
448 # 2. ${#VAR} is the length of the value VAR
449 # 2.a Including ${##} - the length of ${#}
450 # 3 ${VAR#pat} is the value of VAR with leading pat removed
451 # 3.a Including ${VAR#} which just removes leading nothing
452 # This is relevant in case of ${VAR#${X}} with X=''
453 # 3.b ${##} is not a case of 3.a but rather 2.a
454 # 3.c Yet ${##pat} is a case of 3.a
455 # Including ${##${X}} where X='' or X='#'
456 # 3.d And ${#\#} is invalid (error)
457 # 3.e But ${##\#} removes a leading # from the value of $#
458 # (so is just $# as there is no leading # there)
459 # 4 ${VAR##pat} is the value of VAR with longest pat removed
460 # 4.a Including ${VAR##} which removes the longest nothing
461 # 4.b Which in this case includes ${###} (so is == $#)
462 # 4.c But not ${##\#} which is $# with a leading '#' removed
463 # (and so is also == $#), i.e.: like ${###} but different.
464 # 4.d As is ${###\#} or just ${####} - remove # (so just $#)
465 #
466
467 reset dollar_hash
468
469 check 'set -- ; echo $#' '0' 0 # 1
470 check 'set -- a b c; echo $#' '3' 0 # 2
471 check 'set -- a b c d e f g h i j; echo $#' '10' 0 # 3
472 # rule 1
473 check 'set -- ; echo ${#}' '0' 0 # 4
474 check 'set -- a b c; echo ${#}' '3' 0 # 5
475 check 'set -- a b c d e f g h i j; echo ${#}' '10' 0 # 6
476 # rule 1.a
477 check 'set -- a b c; echo ${\#}' '' 2 # 7
478 # rule 1.b
479 check 'set -- a b c; echo ${\#:-foo}' '' 2 # 8
480 # rule 2
481 check 'VAR=12345; echo ${#VAR}' '5' 0 # 9
482 check 'VAR=123456789012; echo ${#VAR}' '12' 0 #10
483 # rule 2.a
484 check 'set -- ; echo ${##}' '1' 0 #11
485 check 'set -- a b c; echo ${##}' '1' 0 #12
486 check 'set -- a b c d e f g h i j; echo ${##}' '2' 0 #13
487 # rule 3
488 check 'VAR=12345; echo ${VAR#1}' '2345' 0 #14
489 check 'VAR=12345; echo ${VAR#2}' '12345' 0 #15
490 check 'VAR=#2345; echo ${VAR#\#}' '2345' 0 #16
491 check 'X=1; VAR=12345; echo ${VAR#${X}}' '2345' 0 #17
492 check 'X=1; VAR=#2345; echo ${VAR#${X}}' '#2345' 0 #18
493 # rule 3.a
494 check 'VAR=12345; echo ${VAR#}' '12345' 0 #19
495 check 'X=; VAR=12345; echo ${VAR#${X}}' '12345' 0 #20
496 # rule 3.b (tested above, rule 2.a)
497 # rule 3.c
498 check 'set -- ; echo ${##0}' '' 0 #21
499 check 'set -- a b c; echo ${##1}' '3' 0 #22
500 check 'set -- a b c d e f g h i j; echo ${##1}' '0' 0 #23
501 check 'X=0; set -- ; echo ${##${X}}' '' 0 #24
502 check 'X=; set -- ; echo ${##${X}}' '0' 0 #25
503 check 'X=1; set -- a b c; echo ${##${X}}' '3' 0 #26
504 check 'X=1; set -- a b c d e f g h i j; echo ${##${X}}' '0' 0 #27
505 check 'X=; set -- a b c d e f g h i j; echo ${##${X}}' '10' 0 #28
506 check 'X=#; VAR=#2345; echo ${VAR#${X}}' '2345' 0 #29
507 check 'X=#; VAR=12345; echo ${VAR#${X}}' '12345' 0 #30
508 # rule 3.d
509 check 'set -- a b c; echo ${#\#}' '' 2 #31
510 # rule 3.e
511 check 'set -- ; echo ${##\#}' '0' 0 #32
512 check 'set -- a b c d e f g h i j; echo ${##\#}' '10' 0 #33
513
514 # rule 4
515 check 'VAR=12345; echo ${VAR##1}' '2345' 0 #34
516 check 'VAR=12345; echo ${VAR##\1}' '2345' 0 #35
517 # rule 4.a
518 check 'VAR=12345; echo ${VAR##}' '12345' 0 #36
519 # rule 4.b
520 check 'set -- ; echo ${###}' '0' 0 #37
521 check 'set -- a b c d e f g h i j; echo ${###}' '10' 0 #38
522 # rule 4.c
523 check 'VAR=12345; echo ${VAR#\#}' '12345' 0 #39
524 check 'VAR=12345; echo ${VAR#\#1}' '12345' 0 #40
525 check 'VAR=#2345; echo ${VAR#\#}' '2345' 0 #41
526 check 'VAR=#12345; echo ${VAR#\#1}' '2345' 0 #42
527 check 'VAR=#2345; echo ${VAR#\#1}' '#2345' 0 #43
528 check 'set -- ; echo ${####}' '0' 0 #44
529 check 'set -- ; echo ${###\#}' '0' 0 #45
530 check 'set -- a b c d e f g h i j; echo ${####}' '10' 0 #46
531 check 'set -- a b c d e f g h i j; echo ${###\#}' '10' 0 #47
532
533 # now check for some more utter nonsense, not mentioned in the rules
534 # above (doesn't need to be)
535
536 check 'x=hello; set -- a b c; echo ${#x:-1}' '' 2 #48
537 check 'x=hello; set -- a b c; echo ${#x-1}' '' 2 #49
538 check 'x=hello; set -- a b c; echo ${#x:+1}' '' 2 #50
539 check 'x=hello; set -- a b c; echo ${#x+1}' '' 2 #51
540 check 'x=hello; set -- a b c; echo ${#x+1}' '' 2 #52
541 check 'x=hello; set -- a b c; echo ${#x:?msg}' '' 2 #53
542 check 'x=hello; set -- a b c; echo ${#x?msg}' '' 2 #54
543 check 'x=hello; set -- a b c; echo ${#x:=val}' '' 2 #55
544 check 'x=hello; set -- a b c; echo ${#x=val}' '' 2 #56
545 check 'x=hello; set -- a b c; echo ${#x#h}' '' 2 #57
546 check 'x=hello; set -- a b c; echo ${#x#*l}' '' 2 #58
547 check 'x=hello; set -- a b c; echo ${#x##*l}' '' 2 #59
548 check 'x=hello; set -- a b c; echo ${#x%o}' '' 2 #60
549 check 'x=hello; set -- a b c; echo ${#x%l*}' '' 2 #61
550 check 'x=hello; set -- a b c; echo ${#x%%l*}' '' 2 #62
551
552 # but just to be complete, these ones should work
553
554 check 'x=hello; set -- a b c; echo ${#%5}' '3' 0 #63
555 check 'x=hello; set -- a b c; echo ${#%3}' '' 0 #64
556 check 'x=hello; set -- a b c; echo ${#%?}' '' 0 #65
557 check 'X=#; set -- a b c; echo ${#%${X}}' '3' 0 #66
558 check 'X=3; set -- a b c; echo ${#%${X}}' '' 0 #67
559 check 'set -- a b c; echo ${#%%5}' '3' 0 #68
560 check 'set -- a b c; echo ${#%%3}' '' 0 #69
561 check 'set -- a b c d e f g h i j k l; echo ${#%1}' '12' 0 #70
562 check 'set -- a b c d e f g h i j k l; echo ${#%2}' '1' 0 #71
563 check 'set -- a b c d e f g h i j k l; echo ${#%?}' '1' 0 #72
564 check 'set -- a b c d e f g h i j k l; echo ${#%[012]}' '1' 0 #73
565 check 'set -- a b c d e f g h i j k l; echo ${#%[0-4]}' '1' 0 #74
566 check 'set -- a b c d e f g h i j k l; echo ${#%?2}' '' 0 #75
567 check 'set -- a b c d e f g h i j k l; echo ${#%1*}' '' 0 #76
568 check 'set -- a b c d e f g h i j k l; echo ${#%%2}' '1' 0 #77
569 check 'set -- a b c d e f g h i j k l; echo ${#%%1*}' '' 0 #78
570
571 # and this lot are stupid, as $# is never unset or null, but they do work...
572
573 check 'set -- a b c; echo ${#:-99}' '3' 0 #79
574 check 'set -- a b c; echo ${#-99}' '3' 0 #80
575 check 'set -- a b c; echo ${#:+99}' '99' 0 #81
576 check 'set -- a b c; echo ${#+99}' '99' 0 #82
577 check 'set -- a b c; echo ${#:?bogus}' '3' 0 #83
578 check 'set -- a b c; echo ${#?bogus}' '3' 0 #84
579
580 # even this utter nonsense is OK, as while special params cannot be
581 # set this way, here, as $# is not unset, or null, the assignment
582 # never happens (isn't even attempted)
583
584 check 'set -- a b c; echo ${#:=bogus}' '3' 0 #85
585 check 'set -- a b c; echo ${#=bogus}' '3' 0 #86
586
587 results
588 }
589
590 atf_test_case dollar_star
591 dollar_star_head() {
592 atf_set "descr" 'Test expansion of various aspects of $*'
593 }
594 dollar_star_body() {
595
596 reset dollar_star
597
598 check 'set -- a b c; echo $# $*' '3 a b c' 0 # 1
599 check 'set -- a b c; echo $# "$*"' '3 a b c' 0 # 2
600 check 'set -- a "b c"; echo $# $*' '2 a b c' 0 # 3
601 check 'set -- a "b c"; echo $# "$*"' '2 a b c' 0 # 4
602 check 'set -- a b c; set -- $* ; echo $# $*' '3 a b c' 0 # 5
603 check 'set -- a b c; set -- "$*" ; echo $# $*' '1 a b c' 0 # 6
604 check 'set -- a "b c"; set -- $* ; echo $# $*' '3 a b c' 0 # 7
605 check 'set -- a "b c"; set -- "$*" ; echo $# $*' \
606 '1 a b c' 0 # 8
607
608 check 'IFS=". "; set -- a b c; echo $# $*' '3 a b c' 0 # 9
609 check 'IFS=". "; set -- a b c; echo $# "$*"' '3 a.b.c' 0 #10
610 check 'IFS=". "; set -- a "b c"; echo $# $*' '2 a b c' 0 #11
611 check 'IFS=". "; set -- a "b c"; echo $# "$*"' '2 a.b c' 0 #12
612 check 'IFS=". "; set -- a "b.c"; echo $# $*' '2 a b c' 0 #13
613 check 'IFS=". "; set -- a "b.c"; echo $# "$*"' '2 a.b.c' 0 #14
614 check 'IFS=". "; set -- a b c; set -- $* ; echo $# $*' \
615 '3 a b c' 0 #15
616 check 'IFS=". "; set -- a b c; set -- "$*" ; echo $# $*' \
617 '1 a b c' 0 #16
618 check 'IFS=". "; set -- a "b c"; set -- $* ; echo $# $*' \
619 '3 a b c' 0 #17
620 check 'IFS=". "; set -- a "b c"; set -- "$*" ; echo $# $*' \
621 '1 a b c' 0 #18
622 check 'IFS=". "; set -- a b c; set -- $* ; echo $# "$*"' \
623 '3 a.b.c' 0 #19
624 check 'IFS=". "; set -- a b c; set -- "$*" ; echo $# "$*"' \
625 '1 a.b.c' 0 #20
626 check 'IFS=". "; set -- a "b c"; set -- $* ; echo $# "$*"' \
627 '3 a.b.c' 0 #21
628 check 'IFS=". "; set -- a "b c"; set -- "$*" ; echo $# "$*"' \
629 '1 a.b c' 0 #22
630
631 results
632 }
633
634 atf_test_case dollar_star_in_word
635 dollar_star_in_word_head() {
636 atf_set "descr" 'Test expansion $* occurring in word of ${var:-word}'
637 }
638 dollar_star_in_word_body() {
639
640 reset dollar_star_in_word
641
642 unset xXx ; # just in case!
643
644 # Note that the expected results for these tests are identical
645 # to those from the dollar_star test. It should never make
646 # a difference whether we expand $* or ${unset:-$*}
647
648 # (note expanding ${unset:-"$*"} is different, that is not tested here)
649
650 check 'set -- a b c; echo $# ${xXx:-$*}' '3 a b c' 0 # 1
651 check 'set -- a b c; echo $# "${xXx:-$*}"' '3 a b c' 0 # 2
652 check 'set -- a "b c"; echo $# ${xXx:-$*}' '2 a b c' 0 # 3
653 check 'set -- a "b c"; echo $# "${xXx:-$*}"' '2 a b c' 0 # 4
654 check 'set -- a b c; set -- ${xXx:-$*} ; echo $# $*' '3 a b c' 0 # 5
655 check 'set -- a b c; set -- "${xXx:-$*}" ; echo $# $*' '1 a b c' 0 # 6
656 check 'set -- a "b c"; set -- ${xXx:-$*} ; echo $# $*' '3 a b c' 0 # 7
657 check 'set -- a "b c"; set -- "${xXx:-$*}" ; echo $# $*' \
658 '1 a b c' 0 # 8
659
660 check 'IFS=". "; set -- a b c; echo $# ${xXx:-$*}' '3 a b c' 0 # 9
661 check 'IFS=". "; set -- a b c; echo $# "${xXx:-$*}"' '3 a.b.c' 0 #10
662 check 'IFS=". "; set -- a "b c"; echo $# ${xXx:-$*}' '2 a b c' 0 #11
663 check 'IFS=". "; set -- a "b c"; echo $# "${xXx:-$*}"' '2 a.b c' 0 #12
664 check 'IFS=". "; set -- a "b.c"; echo $# ${xXx:-$*}' '2 a b c' 0 #13
665 check 'IFS=". "; set -- a "b.c"; echo $# "${xXx:-$*}"' '2 a.b.c' 0 #14
666 check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
667 '3 a b c' 0 #15
668 check 'IFS=". ";set -- a b c;set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
669 '1 a b c' 0 #16
670 check 'IFS=". ";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
671 '3 a b c' 0 #17
672 check 'IFS=". ";set -- a "b c";set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
673 '1 a b c' 0 #18
674 check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
675 '3 a.b.c' 0 #19
676 check 'IFS=". ";set -- a b c;set -- "$*";echo $# "$*"' \
677 '1 a.b.c' 0 #20
678 check 'IFS=". ";set -- a "b c";set -- $*;echo $# "$*"' \
679 '3 a.b.c' 0 #21
680 check 'IFS=". ";set -- a "b c";set -- "$*";echo $# "$*"' \
681 '1 a.b c' 0 #22
682
683 results
684 }
685
686 atf_test_case dollar_star_with_empty_ifs
687 dollar_star_with_empty_ifs_head() {
688 atf_set "descr" 'Test expansion of $* with IFS=""'
689 }
690 dollar_star_with_empty_ifs_body() {
691
692 reset dollar_star_with_empty_ifs
693
694 check 'IFS=""; set -- a b c; echo $# $*' '3 a b c' 0 # 1
695 check 'IFS=""; set -- a b c; echo $# "$*"' '3 abc' 0 # 2
696 check 'IFS=""; set -- a "b c"; echo $# $*' '2 a b c' 0 # 3
697 check 'IFS=""; set -- a "b c"; echo $# "$*"' '2 ab c' 0 # 4
698 check 'IFS=""; set -- a "b.c"; echo $# $*' '2 a b.c' 0 # 5
699 check 'IFS=""; set -- a "b.c"; echo $# "$*"' '2 ab.c' 0 # 6
700 check 'IFS=""; set -- a b c; set -- $* ; echo $# $*' \
701 '3 a b c' 0 # 7
702 check 'IFS=""; set -- a b c; set -- "$*" ; echo $# $*' \
703 '1 abc' 0 # 8
704 check 'IFS=""; set -- a "b c"; set -- $* ; echo $# $*' \
705 '2 a b c' 0 # 9
706 check 'IFS=""; set -- a "b c"; set -- "$*" ; echo $# $*' \
707 '1 ab c' 0 #10
708 check 'IFS=""; set -- a b c; set -- $* ; echo $# "$*"' \
709 '3 abc' 0 #11
710 check 'IFS=""; set -- a b c; set -- "$*" ; echo $# "$*"' \
711 '1 abc' 0 #12
712 check 'IFS=""; set -- a "b c"; set -- $* ; echo $# "$*"' \
713 '2 ab c' 0 #13
714 check 'IFS=""; set -- a "b c"; set -- "$*" ; echo $# "$*"' \
715 '1 ab c' 0 #14
716
717 results # FIXED: 'PR bin/52090 expect 7 of 14 subtests to fail'
718 }
719
720 atf_test_case dollar_star_in_word_empty_ifs
721 dollar_star_in_word_empty_ifs_head() {
722 atf_set "descr" 'Test expansion of ${unset:-$*} with IFS=""'
723 }
724 dollar_star_in_word_empty_ifs_body() {
725
726 reset dollar_star_in_word_empty_ifs
727
728 unset xXx ; # just in case
729
730 # Note that the expected results for these tests are identical
731 # to those from the dollar_star_with_empty_ifs test. It should
732 # never make a difference whether we expand $* or ${unset:-$*}
733
734 # (note expanding ${unset:-"$*"} is different, that is not tested here)
735
736 check 'IFS="";set -- a b c;echo $# ${xXx:-$*}' '3 a b c' 0 # 1
737 check 'IFS="";set -- a b c;echo $# "${xXx:-$*}"' '3 abc' 0 # 2
738 check 'IFS="";set -- a "b c";echo $# ${xXx:-$*}' '2 a b c' 0 # 3
739 check 'IFS="";set -- a "b c";echo $# "${xXx:-$*}"' '2 ab c' 0 # 4
740 check 'IFS="";set -- a "b.c";echo $# ${xXx:-$*}' '2 a b.c' 0 # 5
741 check 'IFS="";set -- a "b.c";echo $# "${xXx:-$*}"' '2 ab.c' 0 # 6
742 check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
743 '3 a b c' 0 # 7
744 check 'IFS="";set -- a b c;set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
745 '1 abc' 0 # 8
746 check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
747 '2 a b c' 0 # 9
748 check 'IFS="";set -- a "b c";set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
749 '1 ab c' 0 #10
750 check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
751 '3 abc' 0 #11
752 check 'IFS="";set -- a b c;set -- "${xXx:-$*}";echo $# "${xXx:-$*}"' \
753 '1 abc' 0 #12
754 check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
755 '2 ab c' 0 #13
756 check 'IFS="";set -- a "b c";set -- "${xXx:-$*}";echo $# "${xXx:-$*}"' \
757 '1 ab c' 0 #14
758
759 results # FIXED: 'PR bin/52090 expect 7 of 14 subtests to fail'
760 }
761
762 atf_test_case dollar_star_in_quoted_word
763 dollar_star_in_quoted_word_head() {
764 atf_set "descr" 'Test expansion $* occurring in word of ${var:-"word"}'
765 }
766 dollar_star_in_quoted_word_body() {
767
768 reset dollar_star_in_quoted_word
769
770 unset xXx ; # just in case!
771
772 check 'set -- a b c; echo $# ${xXx:-"$*"}' '3 a b c' 0 # 1
773 check 'set -- a "b c"; echo $# ${xXx:-"$*"}' '2 a b c' 0 # 2
774 check 'set -- a b c; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
775 '1 a b c' 0 # 3
776 check 'set -- a "b c"; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
777 '1 a b c' 0 # 4
778 check 'set -- a b c; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
779 '1 a b c' 0 # 5
780 check 'set -- a "b c"; set -- ${xXx:-"$*"} ; echo $# ${xXx-$*}' \
781 '1 a b c' 0 # 6
782 check 'set -- a b c; set -- ${xXx:-$*} ; echo $# ${xXx-"$*"}' \
783 '3 a b c' 0 # 7
784 check 'set -- a "b c"; set -- ${xXx:-$*} ; echo $# ${xXx-"$*"}' \
785 '3 a b c' 0 # 8
786
787 check 'IFS=". "; set -- a b c; echo $# ${xXx:-"$*"}' '3 a.b.c' 0 # 9
788 check 'IFS=". "; set -- a "b c"; echo $# ${xXx:-"$*"}' '2 a.b c' 0 #10
789 check 'IFS=". "; set -- a "b.c"; echo $# ${xXx:-"$*"}' '2 a.b.c' 0 #11
790 check 'IFS=". ";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
791 '1 a.b.c' 0 #12
792 check 'IFS=". ";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
793 '1 a.b c' 0 #13
794 check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
795 '3 a.b.c' 0 #14
796 check 'IFS=". ";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
797 '3 a.b.c' 0 #15
798 check 'IFS=". ";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
799 '1 a b c' 0 #16
800 check 'IFS=". ";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
801 '1 a b c' 0 #17
802
803 check 'IFS="";set -- a b c;echo $# ${xXx:-"$*"}' '3 abc' 0 #18
804 check 'IFS="";set -- a "b c";echo $# ${xXx:-"$*"}' '2 ab c' 0 #19
805 check 'IFS="";set -- a "b.c";echo $# ${xXx:-"$*"}' '2 ab.c' 0 #20
806 check 'IFS="";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
807 '1 abc' 0 #21
808 check 'IFS="";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
809 '1 ab c' 0 #22
810 check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
811 '3 abc' 0 #23
812 check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
813 '2 ab c' 0 #24
814 check 'IFS="";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
815 '1 abc' 0 #25
816 check 'IFS="";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
817 '1 ab c' 0 #26
818
819 results # FIXED: 'PR bin/52090 - 2 of 26 subtests expected to fail'
820 }
821
822 atf_init_test_cases() {
823 # Listed here in the order ATF runs them, not the order from above
824
825 atf_add_test_case arithmetic
826 atf_add_test_case dollar_at
827 atf_add_test_case dollar_at_with_text
828 atf_add_test_case dollar_hash
829 atf_add_test_case dollar_star
830 atf_add_test_case dollar_star_in_quoted_word
831 atf_add_test_case dollar_star_in_word
832 atf_add_test_case dollar_star_in_word_empty_ifs
833 atf_add_test_case dollar_star_with_empty_ifs
834 atf_add_test_case iteration_on_null_parameter
835 atf_add_test_case iteration_on_quoted_null_parameter
836 atf_add_test_case iteration_on_null_or_null_parameter
837 atf_add_test_case iteration_on_null_or_missing_parameter
838 atf_add_test_case shell_params
839 atf_add_test_case strip
840 atf_add_test_case var_with_embedded_cmdsub
841 atf_add_test_case varpattern_backslashes
842 }
843