t_here.sh revision 1.7
1# $NetBSD: t_here.sh,v 1.7 2019/01/22 14:31:53 kre Exp $ 2# 3# Copyright (c) 2007 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 30nl=' 31' 32 33reset() 34{ 35 TEST_NUM=0 36 TEST_FAILURES='' 37 TEST_FAIL_COUNT=0 38 TEST_ID="$1" 39} 40 41check() 42{ 43 fail=false 44 TEMP_FILE=$( mktemp OUT.XXXXXX ) 45 TEST_NUM=$(( $TEST_NUM + 1 )) 46 47 # our local shell (ATF_SHELL) better do quoting correctly... 48 # some of the tests expect us to expand $nl internally... 49 CMD="nl='${nl}'; $1" 50 51 result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )" 52 STATUS=$? 53 54 if [ "${STATUS}" -ne "$3" ]; then 55 echo >&2 "[$TEST_NUM] expected exit code $3, got ${STATUS}" 56 57 # don't actually fail just because of wrong exit code 58 # unless we either expected, or received "good" 59 case "$3/${STATUS}" in 60 (*/0|0/*) fail=true;; 61 esac 62 fi 63 64 if [ "$3" -eq 0 ]; then 65 if [ -s "${TEMP_FILE}" ]; then 66 echo >&2 \ 67 "[$TEST_NUM] Messages produced on stderr unexpected..." 68 cat "${TEMP_FILE}" >&2 69 fail=true 70 fi 71 else 72 if ! [ -s "${TEMP_FILE}" ]; then 73 echo >&2 \ 74 "[$TEST_NUM] Expected messages on stderr, nothing produced" 75 fail=true 76 fi 77 fi 78 rm -f "${TEMP_FILE}" 79 80 # Remove newlines (use local shell for this) 81 result="$( 82 IFS="$nl" 83 set -f 84 set -- $result 85 IFS=' ' 86 printf %s "$*" 87 )" 88 if [ "$2" != "$result" ] 89 then 90 echo >&2 "[$TEST_NUM] Expected output '$2', received '$result'" 91 fail=true 92 fi 93 94 if $fail 95 then 96 echo >&2 "[$TEST_NUM] Full command: <<${CMD}>>" 97 fi 98 99 $fail && test -n "$TEST_ID" && { 100 TEST_FAILURES="${TEST_FAILURES}${TEST_FAILURES:+ 101}${TEST_ID}[$TEST_NUM]: test of '$1' failed"; 102 TEST_FAIL_COUNT=$(( $TEST_FAIL_COUNT + 1 )) 103 return 0 104 } 105 $fail && atf_fail "Test[$TEST_NUM] of '$1' failed" 106 return 0 107} 108 109results() 110{ 111 test -z "${TEST_ID}" && return 0 112 test -z "${TEST_FAILURES}" && return 0 113 114 echo >&2 "==========================================" 115 echo >&2 "While testing '${TEST_ID}'" 116 echo >&2 " - - - - - - - - - - - - - - - - -" 117 echo >&2 "${TEST_FAILURES}" 118 atf_fail \ 119 "Test ${TEST_ID}: $TEST_FAIL_COUNT subtests (of $TEST_NUM) failed - see stderr" 120} 121 122atf_test_case do_simple 123do_simple_head() { 124 atf_set "descr" "Basic tests for here documents" 125} 126do_simple_body() { 127 y=x 128 129 reset 'simple' 130 IFS=' ' 131 check 'x=`cat <<EOF'$nl'text'${nl}EOF$nl'`; echo $x' 'text' 0 132 check 'x=`cat <<\EOF'$nl'text'${nl}EOF$nl'`; echo $x' 'text' 0 133 134 check "y=${y};"'x=`cat <<EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \ 135 'text' 0 136 check "y=${y};"'x=`cat <<\EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \ 137 'te${y}t' 0 138 check "y=${y};"'x=`cat <<"EOF"'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \ 139 'te${y}t' 0 140 check "y=${y};"'x=`cat <<'"'EOF'"$nl'te${y}t'${nl}EOF$nl'`; echo $x' \ 141 'te${y}t' 0 142 143 # check that quotes in the here doc survive and cause no problems 144 check "cat <<EOF${nl}te'xt${nl}EOF$nl" "te'xt" 0 145 check "cat <<\EOF${nl}te'xt${nl}EOF$nl" "te'xt" 0 146 check "cat <<'EOF'${nl}te'xt${nl}EOF$nl" "te'xt" 0 147 check "cat <<EOF${nl}te\"xt${nl}EOF$nl" 'te"xt' 0 148 check "cat <<\EOF${nl}te\"xt${nl}EOF$nl" 'te"xt' 0 149 check "cat <<'EOF'${nl}te\"xt${nl}EOF$nl" 'te"xt' 0 150 check "cat <<'EO'F${nl}te\"xt${nl}EOF$nl" 'te"xt' 0 151 152 check "y=${y};"'x=`cat <<EOF'$nl'te'"'"'${y}t'${nl}EOF$nl'`; echo $x' \ 153 'te'"'"'xt' 0 154 check "y=${y};"'x=`cat <<EOF'$nl'te'"''"'${y}t'${nl}EOF$nl'`; echo $x' \ 155 'te'"''"'xt' 0 156 157 # note that the blocks of empty space in the following must 158 # be entirely tab characters, no spaces. 159 160 check 'x=`cat <<EOF'"$nl text${nl}EOF$nl"'`; echo "$x"' \ 161 ' text' 0 162 check 'x=`cat <<-EOF'"$nl text${nl}EOF$nl"'`; echo $x' \ 163 'text' 0 164 check 'x=`cat <<-EOF'"${nl}text${nl} EOF$nl"'`; echo $x' \ 165 'text' 0 166 check 'x=`cat <<-\EOF'"$nl text${nl} EOF$nl"'`; echo $x' \ 167 'text' 0 168 check 'x=`cat <<- "EOF"'"$nl text${nl}EOF$nl"'`; echo $x' \ 169 'text' 0 170 check 'x=`cat <<- '"'EOF'${nl}text${nl} EOF$nl"'`; echo $x' \ 171 'text' 0 172 results 173} 174 175atf_test_case end_markers 176end_markers_head() { 177 atf_set "descr" "Tests for various end markers of here documents" 178} 179end_markers_body() { 180 181 reset 'end_markers' 182 for end in EOF 1 \! '$$$' "string " a\\\ a\\\ \ '&' '' ' ' ' ' \ 183 --STRING-- . '~~~' ')' '(' '#' '()' '(\)' '(\/)' '--' '\' '{' '}' \ 184VERYVERYVERYVERYLONGLONGLONGin_fact_absurdly_LONG_LONG_HERE_DOCUMENT_TERMINATING_MARKER_THAT_goes_On_forever_and_ever_and_ever... 185 do 186 # check unquoted end markers 187 case "${end}" in 188 ('' | *[' ()\$&#*~']* ) ;; # skip unquoted endmark test for these 189 (*) check \ 190 'x=$(cat << '"${end}${nl}text${nl}${end}${nl}"'); printf %s "$x"' 'text' 0 191 ;; 192 esac 193 194 # and quoted end markers 195 check \ 196 'x=$(cat <<'"'${end}'${nl}text${nl}${end}${nl}"'); printf %s "$x"' 'text' 0 197 198 # and see what happens if we encounter "almost" an end marker 199 case "${#end}" in 200 (0|1) ;; # too short to try truncation tests 201 (*) check \ 202 'x=$(cat <<'"'${end}'${nl}text${nl}${end%?}${nl}${end}${nl}"'); printf %s "$x"' \ 203 "text ${end%?}" 0 204 check \ 205 'x=$(cat <<'"'${end}'${nl}text${nl}${end#?}${nl}${end}${nl}"'); printf %s "$x"' \ 206 "text ${end#?}" 0 207 check \ 208 'x=$(cat <<'"'${end}'${nl}text${nl}${end%?}+${nl}${end}${nl}"');printf %s "$x"' \ 209 "text ${end%?}+" 0 210 ;; 211 esac 212 213 # or something that is a little longer 214 check \ 215 'x=$(cat <<'"'${end}'${nl}text${nl}${end}x${nl}${end}${nl}"'); printf %s "$x"' \ 216 "text ${end}x" 0 217 check \ 218 'x=$(cat <<'"'${end}'${nl}text${nl}!${end}${nl}${end}${nl}"'); printf %s "$x"' \ 219 "text !${end}" 0 220 221 # or which does not begin at start of line 222 check \ 223 'x=$(cat <<'"'${end}'${nl}text${nl} ${end}${nl}${end}${nl}"'); printf %s "$x"' \ 224 "text ${end}" 0 225 check \ 226 'x=$(cat <<'"'${end}'${nl}text${nl} ${end}${nl}${end}${nl}"'); printf %s "$x"' \ 227 "text ${end}" 0 228 229 # or end at end of line 230 check \ 231 'x=$(cat <<'"'${end}'${nl}text${nl}${end} ${nl}${end}${nl}"'); printf %s "$x"' \ 232 "text ${end} " 0 233 234 # or something that is correct much of the way, but then... 235 236 case "${#end}" in 237 (0) ;; # cannot test this one 238 (1) check \ 239 'x=$(cat <<'"'${end}'${nl}text${nl}${end}${end}${nl}${end}${nl}"'); printf %s "$x"' \ 240 "text ${end}${end}" 0 241 ;; 242 (2-7) pfx="${end%?}" 243 check \ 244 'x=$(cat <<'"'${end}'${nl}text${nl}${end}${pfx}${nl}${end}${nl}"'); printf %s "$x"' \ 245 "text ${end}${pfx}" 0 246 check \ 247 'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${end}${nl}${end}${nl}"'); printf %s "$x"' \ 248 "text ${pfx}${end}" 0 249 ;; 250 (*) pfx=${end%??????}; sfx=${end#??????} 251 check \ 252 'x=$(cat <<'"'${end}'${nl}text${nl}${end}${sfx}${nl}${end}${nl}"'); printf %s "$x"' \ 253 "text ${end}${sfx}" 0 254 check \ 255 'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${end}${nl}${end}${nl}"'); printf %s "$x"' \ 256 "text ${pfx}${end}" 0 257 check \ 258 'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${sfx}${nl}${end}${nl}"'); printf %s "$x"' \ 259 "text ${pfx}${sfx}" 0 260 ;; 261 esac 262 done 263 264 # Add striptabs tests (in similar way) here one day... 265 266 results 267} 268 269atf_test_case incomplete 270incomplete_head() { 271 atf_set "descr" "Basic tests for incomplete here documents" 272} 273incomplete_body() { 274 reset incomplete 275 276 check 'cat <<EOF' '' 2 277 check 'cat <<- EOF' '' 2 278 check 'cat <<\EOF' '' 2 279 check 'cat <<- \EOF' '' 2 280 281 check 'cat <<EOF'"${nl}" '' 2 282 check 'cat <<- EOF'"${nl}" '' 2 283 check 'cat <<'"'EOF'${nl}" '' 2 284 check 'cat <<- "EOF"'"${nl}" '' 2 285 286 check 'cat << EOF'"${nl}${nl}" '' 2 287 check 'cat <<-EOF'"${nl}${nl}" '' 2 288 check 'cat << '"'EOF'${nl}${nl}" '' 2 289 check 'cat <<-"EOF"'"${nl}${nl}" '' 2 290 291 check 'cat << EOF'"${nl}"'line 1'"${nl}" '' 2 292 check 'cat <<-EOF'"${nl}"' line 1'"${nl}" '' 2 293 check 'cat << EOF'"${nl}"'line 1'"${nl}"' line 2'"${nl}" '' 2 294 check 'cat <<-EOF'"${nl}"' line 1'"${nl}"'line 2'"${nl}" '' 2 295 296 check 'cat << EOF'"${nl}line 1${nl}${nl}line3${nl}${nl}5!${nl}" '' 2 297 298 results 299} 300 301atf_test_case lineends 302lineends_head() { 303 atf_set "descr" "Tests for line endings in here documents" 304} 305lineends_body() { 306 reset lineends 307 308 # note that "check" removes newlines from stdout before comparing. 309 # (they become blanks, provided there is something before & after) 310 311 check 'cat << \echo'"${nl}"'\'"${nl}echo${nl}echo${nl}" '\' 0 312 check 'cat << echo'"${nl}"'\'"${nl}echo${nl}echo${nl}" 'echo' 0 313 check 'cat << echo'"${nl}"'\\'"${nl}echo${nl}echo${nl}" '\' 0 314 315 check 'X=3; cat << ec\ho'"${nl}"'$X\'"${nl}echo${nl}echo${nl}" \ 316 '$X\' 0 317 check 'X=3; cat << echo'"${nl}"'$X'"${nl}echo${nl}echo${nl}" \ 318 '3' 0 319 check 'X=3; cat << echo'"${nl}"'$X\'"${nl}echo${nl}echo${nl}" \ 320 '' 0 321 check 'X=3; cat << echo'"${nl}"'${X}\'"${nl}echo${nl}echo${nl}" \ 322 '3echo' 0 323 check 'X=3; cat << echo'"${nl}"'\$X\'"${nl}echo${nl}echo${nl}" \ 324 '$Xecho' 0 325 check 'X=3; cat << echo'"${nl}"'\\$X \'"${nl}echo${nl}echo${nl}" \ 326 '\3 echo' 0 327 328 check \ 329 'cat << "echo"'"${nl}"'line1\'"${nl}"'line2\'"${nl}echo${nl}echo${nl}" \ 330 'line1\ line2\' 0 331 check \ 332 'cat << echo'"${nl}"'line1\'"${nl}"'line2\'"${nl}echo${nl}echo${nl}" \ 333 'line1line2echo' 0 334 335 results 336} 337 338atf_test_case multiple 339multiple_head() { 340 atf_set "descr" "Tests for multiple here documents on one cmd line" 341} 342multiple_body() { 343 reset multiple 344 345 check \ 346 "(cat ; cat <&3) <<EOF0 3<<EOF3${nl}STDIN${nl}EOF0${nl}-3-${nl}EOF3${nl}" \ 347 'STDIN -3-' 0 348 349 check "(read line; echo \"\$line\"; cat <<EOF1; echo \"\$line\") <<EOF2 350The File 351EOF1 352The Line 353EOF2 354" 'The Line The File The Line' 0 355 356 check "(read line; echo \"\$line\"; cat <<EOF; echo \"\$line\") <<EOF 357The File 358EOF 359The Line 360EOF 361" 'The Line The File The Line' 0 362 363 check "V=1; W=2; cat <<-1; cat <<2; cat <<- 3; cat <<'4';"' cat <<\5 364 $V 365 $W 366 3 367 4 368 5 369 1 3702 371 5 372 4*$W+\$V 373 3 374$W 3751 3762 3773 3784 3797+$V 380$W+6 3815 382' '1 2 3 4 5 5 4*2+$V $W 1 2 3 7+$V $W+6' 0 383 384 results 385} 386 387atf_test_case nested 388nested_head() { 389 atf_set "descr" "Tests for nested here documents for one cmd" 390} 391nested_body() { 392 reset nested 393 394 check \ 395'cat << EOF1'"${nl}"'$(cat << EOF2'"${nl}LINE${nl}EOF2${nl}"')'"${nl}EOF1${nl}"\ 396 'LINE' 0 397 398# This next one fails ... and correctly, so we will omit it (bad test) 399# Reasoning is that the correct data "$(cat << EOF2)\nLINE\nEOF2\n" is 400# collected for the outer (EOF1) heredoc, when that is parsed, it looks 401# like 402# $(cat <<EOF2) 403# LINE 404# EOF2 405# which looks like a good command - except it is being parsed in "heredoc" 406# syntax, which means it is enclosed in double quotes, which means that 407# the newline after the ')' in the first line is not a newline token, but 408# just a character. The EOF2 heredoc cannot start until after the next 409# newline token, of which there are none here... LINE and EOF2 are just 410# more data in the outer EOF1 heredoc for its "cat" command to read & write. 411# 412# The previous sub-test works because there the \n comes inside the 413# $( ), and in there, the outside quoting rules are suspended, and it 414# all starts again - so that \n is a newline token, and the EOF2 heredoc 415# is processed. 416# 417# check \ 418# 'cat << EOF1'"${nl}"'$(cat << EOF2 )'"${nl}LINE${nl}EOF2${nl}EOF1${nl}" \ 419# 'LINE' 0 420 421 L='cat << EOF1'"${nl}"'LINE1$(cat << EOF2'"${nl}" 422 L="${L}"'LINE2$(cat << EOF3'"${nl}" 423 L="${L}"'LINE3$(cat << EOF4'"${nl}" 424 L="${L}"'LINE4$(cat << EOF5'"${nl}" 425 L="${L}LINE5${nl}EOF5${nl})4${nl}EOF4${nl})3${nl}" 426 L="${L}EOF3${nl})2${nl}EOF2${nl})1${nl}EOF1${nl}" 427 428 # That mess is ... 429 # 430 # cat <<EOF1 431 # LINE1$(cat << EOF2 432 # LINE2$(cat << EOF3 433 # LINE3$(cat << EOF4 434 # LINE4$(cat << EOF5 435 # LINE5 436 # EOF5 437 # )4 438 # EOF4 439 # )3 440 # EOF3 441 # )2 442 # EOF2 443 # )1 444 # EOF1 445 446 check "${L}" 'LINE1LINE2LINE3LINE4LINE54321' 0 447 448 results 449} 450 451atf_test_case quoting 452quoting_head() { 453 atf_set "descr" "Tests for use of quotes inside here documents" 454} 455quoting_body() { 456 reset quoting 457 458 check 'X=!; cat <<- E\0F 459 <'\''"'\'' \\$X\$X "'\''" \\> 460 E0F 461 ' '<'\''"'\'' \\$X\$X "'\''" \\>' 0 462 463 check 'X=!; cat <<- E0F 464 <'\''"'\'' \\$X\$X "'\''" \\> 465 E0F 466 ' '<'\''"'\'' \!$X "'\''" \>' 0 467 468 check 'cat <<- END 469 $( echo "'\''" ) $( echo '\''"'\'' ) $( echo \\ ) 470 END 471 ' "' \" \\" 0 472 473 check 'X=12345; Y="string1 line1?-line2"; Z=; unset W; cat <<-EOF 474 ${#X}${Z:-${Y}}${W+junk}${Y%%l*}${Y#*\?} 475 "$Z"'\''$W'\'' ${Y%" "*} $(( X + 54321 )) 476 EOF 477 ' '5string1 line1?-line2string1 -line2 ""'\'\'' string1 66666' 0 478 479 # check that \ only quotes the magic chars, otherwise is retained 480 check 'p=A; cat <<-EOF 481 ${p+\%$p\%} 482 ${p+%$p%} 483 EOF 484 ' '\%A\% %A%' 0 485 486 # and check that " is not magic, so \ does not quote it 487 check 'p=A; cat <<-EOF 488 ${p+\"$p\"} 489 ${p+"$p"} 490 EOF 491 ' '\"A\" "A"' 0 492 493 # except in a ${var%<word>} word, base syntax reapplies, and 494 # there quotes are magic again 495 check 'p=ABCD; cat <<-EOF 496 ${p%B?D} 497 ${p%B\?D} 498 ${p%"BCD"} 499 "${p%??}" 500 ${p#"${p%??}"} 501 "${p#"${p%?"?"}"}" 502 EOF 503 ' 'A ABCD A "AB" CD ""' 0 504 505 check 'p=AB??; cat <<-EOF 506 ${p%B?D} 507 ${p%B\??} 508 ${p%"B??"} 509 "${p%??}" 510 ${p#"${p%??}"} 511 "${p#"${p%?"?"}"}" 512 EOF 513 ' 'AB?? A A "AB" ?? "??"' 0 514 515 results 516} 517 518atf_test_case side_effects 519side_effects_head() { 520 atf_set "descr" "Tests how side effects in here documents are handled" 521} 522side_effects_body() { 523 524 atf_check -s exit:0 -o inline:'2\n1\n' -e empty ${TEST_SH} -c ' 525 unset X 526 cat <<-EOF 527 ${X=2} 528 EOF 529 echo "${X-1}" 530 ' 531} 532 533atf_test_case vicious 534vicious_head() { 535 atf_set "descr" "Tests for obscure and obnoxious uses of here docs" 536} 537vicious_body() { 538 reset 539 540 cat <<- \END_SCRIPT > script 541 cat <<ONE && cat \ 542 <<TWO 543 a 544 ONE 545 b 546 TWO 547 END_SCRIPT 548 549 atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} script 550 551 # This next one is causing discussion currently (late Feb 2016) 552 # amongst stds writers & implementors. Consequently we 553 # will not check what it produces. The eventual result 554 # seems unlikely to be what we currently output, which 555 # is: 556 # A:echo line 1 557 # B:echo line 2)" && prefix DASH_CODE <<DASH_CODE 558 # B:echo line 3 559 # line 4 560 # line 5 561 # 562 # The likely intended output is ... 563 # 564 # A:echo line 3 565 # B:echo line 1 566 # line 2 567 # DASH_CODE:echo line 4)" 568 # DASH_CODE:echo line 5 569 # 570 # The difference is explained by differing opinions on just 571 # when processing of a here doc should start 572 573 cat <<- \END_SCRIPT > script 574 prefix() { sed -e "s/^/$1:/"; } 575 DASH_CODE() { :; } 576 577 prefix A <<XXX && echo "$(prefix B <<XXX 578 echo line 1 579 XXX 580 echo line 2)" && prefix DASH_CODE <<DASH_CODE 581 echo line 3 582 XXX 583 echo line 4)" 584 echo line 5 585 DASH_CODE 586 END_SCRIPT 587 588 # we will just verify that the shell can parse the 589 # script somehow, and doesn't fall over completely... 590 591 atf_check -s exit:0 -o ignore -e empty ${TEST_SH} script 592} 593 594atf_init_test_cases() { 595 atf_add_test_case do_simple # not worthy of a comment 596 atf_add_test_case end_markers # the mundane, the weird, the bizarre 597 atf_add_test_case incomplete # where the end marker isn't... 598 atf_add_test_case lineends # test weird line endings in heredocs 599 atf_add_test_case multiple # multiple << operators on one cmd 600 atf_add_test_case nested # here docs inside here docs 601 atf_add_test_case quoting # stuff quoted inside 602 atf_add_test_case side_effects # here docs that modify environment 603 atf_add_test_case vicious # evil test from the austin-l list... 604} 605