1 # $NetBSD: t_here.sh,v 1.6 2016/03/31 16:21:52 christos Exp $
3 # Copyright (c) 2007 The NetBSD Foundation, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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.
27 # the implementation of "sh" to test
28 : ${TEST_SH:="/bin/sh"}
44 TEMP_FILE=$( mktemp OUT.XXXXXX )
45 TEST_NUM=$(( $TEST_NUM + 1 ))
47 # our local shell (ATF_SHELL) better do quoting correctly...
48 # some of the tests expect us to expand $nl internally...
51 result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )"
54 if [ "${STATUS}" -ne "$3" ]; then
55 echo >&2 "[$TEST_NUM] expected exit code $3, got ${STATUS}"
57 # don't actually fail just because of wrong exit code
58 # unless we either expected, or received "good"
59 case "$3/${STATUS}" in
64 if [ "$3" -eq 0 ]; then
65 if [ -s "${TEMP_FILE}" ]; then
67 "[$TEST_NUM] Messages produced on stderr unexpected..."
68 cat "${TEMP_FILE}" >&2
72 if ! [ -s "${TEMP_FILE}" ]; then
74 "[$TEST_NUM] Expected messages on stderr, nothing produced"
80 # Remove newlines (use local shell for this)
83 result="$(echo $result)"
85 if [ "$2" != "$result" ]
87 echo >&2 "[$TEST_NUM] Expected output '$2', received '$result'"
93 echo >&2 "[$TEST_NUM] Full command: <<${CMD}>>"
96 $fail && test -n "$TEST_ID" && {
97 TEST_FAILURES="${TEST_FAILURES}${TEST_FAILURES:+
98 }${TEST_ID}[$TEST_NUM]: test of '$1' failed";
99 TEST_FAIL_COUNT=$(( $TEST_FAIL_COUNT + 1 ))
102 $fail && atf_fail "Test[$TEST_NUM] of '$1' failed"
108 test -z "${TEST_ID}" && return 0
109 test -z "${TEST_FAILURES}" && return 0
111 echo >&2 "=========================================="
112 echo >&2 "While testing '${TEST_ID}'"
113 echo >&2 " - - - - - - - - - - - - - - - - -"
114 echo >&2 "${TEST_FAILURES}"
116 "Test ${TEST_ID}: $TEST_FAIL_COUNT subtests (of $TEST_NUM) failed - see stderr"
119 atf_test_case do_simple
121 atf_set "descr" "Basic tests for here documents"
128 check 'x=`cat <<EOF'$nl'text'${nl}EOF$nl'`; echo $x' 'text' 0
129 check 'x=`cat <<\EOF'$nl'text'${nl}EOF$nl'`; echo $x' 'text' 0
131 check "y=${y};"'x=`cat <<EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \
133 check "y=${y};"'x=`cat <<\EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \
135 check "y=${y};"'x=`cat <<"EOF"'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \
137 check "y=${y};"'x=`cat <<'"'EOF'"$nl'te${y}t'${nl}EOF$nl'`; echo $x' \
140 # check that quotes in the here doc survive and cause no problems
141 check "cat <<EOF${nl}te'xt${nl}EOF$nl" "te'xt" 0
142 check "cat <<\EOF${nl}te'xt${nl}EOF$nl" "te'xt" 0
143 check "cat <<'EOF'${nl}te'xt${nl}EOF$nl" "te'xt" 0
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 <<'EO'F${nl}te\"xt${nl}EOF$nl" 'te"xt' 0
149 check "y=${y};"'x=`cat <<EOF'$nl'te'"'"'${y}t'${nl}EOF$nl'`; echo $x' \
151 check "y=${y};"'x=`cat <<EOF'$nl'te'"''"'${y}t'${nl}EOF$nl'`; echo $x' \
154 # note that the blocks of empty space in the following must
155 # be entirely tab characters, no spaces.
157 check 'x=`cat <<EOF'"$nl text${nl}EOF$nl"'`; echo "$x"' \
159 check 'x=`cat <<-EOF'"$nl text${nl}EOF$nl"'`; echo $x' \
161 check 'x=`cat <<-EOF'"${nl}text${nl} EOF$nl"'`; echo $x' \
163 check 'x=`cat <<-\EOF'"$nl text${nl} EOF$nl"'`; echo $x' \
165 check 'x=`cat <<- "EOF"'"$nl text${nl}EOF$nl"'`; echo $x' \
167 check 'x=`cat <<- '"'EOF'${nl}text${nl} EOF$nl"'`; echo $x' \
172 atf_test_case end_markers
174 atf_set "descr" "Tests for various end markers of here documents"
179 for end in EOF 1 \! '$$$' "string " a\\\ a\\\ \ '&' '' ' ' ' ' \
180 --STRING-- . '~~~' ')' '(' '#' '()' '(\)' '(\/)' '--' '\' '{' '}' \
181 VERYVERYVERYVERYLONGLONGLONGin_fact_absurdly_LONG_LONG_HERE_DOCUMENT_TERMINATING_MARKER_THAT_goes_On_forever_and_ever_and_ever...
183 # check unquoted end markers
185 ('' | *[' ()\$&#*~']* ) ;; # skip unquoted endmark test for these
187 'x=$(cat << '"${end}${nl}text${nl}${end}${nl}"'); printf %s "$x"' 'text' 0
191 # and quoted end markers
193 'x=$(cat <<'"'${end}'${nl}text${nl}${end}${nl}"'); printf %s "$x"' 'text' 0
195 # and see what happens if we encounter "almost" an end marker
197 (0|1) ;; # too short to try truncation tests
199 'x=$(cat <<'"'${end}'${nl}text${nl}${end%?}${nl}${end}${nl}"'); printf %s "$x"' \
202 'x=$(cat <<'"'${end}'${nl}text${nl}${end#?}${nl}${end}${nl}"'); printf %s "$x"' \
205 'x=$(cat <<'"'${end}'${nl}text${nl}${end%?}+${nl}${end}${nl}"');printf %s "$x"' \
210 # or something that is a little longer
212 'x=$(cat <<'"'${end}'${nl}text${nl}${end}x${nl}${end}${nl}"'); printf %s "$x"' \
215 'x=$(cat <<'"'${end}'${nl}text${nl}!${end}${nl}${end}${nl}"'); printf %s "$x"' \
218 # or which does not begin at start of line
220 'x=$(cat <<'"'${end}'${nl}text${nl} ${end}${nl}${end}${nl}"'); printf %s "$x"' \
223 'x=$(cat <<'"'${end}'${nl}text${nl} ${end}${nl}${end}${nl}"'); printf %s "$x"' \
226 # or end at end of line
228 'x=$(cat <<'"'${end}'${nl}text${nl}${end} ${nl}${end}${nl}"'); printf %s "$x"' \
231 # or something that is correct much of the way, but then...
234 (0) ;; # cannot test this one
236 'x=$(cat <<'"'${end}'${nl}text${nl}${end}${end}${nl}${end}${nl}"'); printf %s "$x"' \
237 "text ${end}${end}" 0
241 'x=$(cat <<'"'${end}'${nl}text${nl}${end}${pfx}${nl}${end}${nl}"'); printf %s "$x"' \
242 "text ${end}${pfx}" 0
244 'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${end}${nl}${end}${nl}"'); printf %s "$x"' \
245 "text ${pfx}${end}" 0
247 (*) pfx=${end%??????}; sfx=${end#??????}
249 'x=$(cat <<'"'${end}'${nl}text${nl}${end}${sfx}${nl}${end}${nl}"'); printf %s "$x"' \
250 "text ${end}${sfx}" 0
252 'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${end}${nl}${end}${nl}"'); printf %s "$x"' \
253 "text ${pfx}${end}" 0
255 'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${sfx}${nl}${end}${nl}"'); printf %s "$x"' \
256 "text ${pfx}${sfx}" 0
261 # Add striptabs tests (in similar way) here one day...
266 atf_test_case incomplete
268 atf_set "descr" "Basic tests for incomplete here documents"
273 check 'cat <<EOF' '' 2
274 check 'cat <<- EOF' '' 2
275 check 'cat <<\EOF' '' 2
276 check 'cat <<- \EOF' '' 2
278 check 'cat <<EOF'"${nl}" '' 2
279 check 'cat <<- EOF'"${nl}" '' 2
280 check 'cat <<'"'EOF'${nl}" '' 2
281 check 'cat <<- "EOF"'"${nl}" '' 2
283 check 'cat << EOF'"${nl}${nl}" '' 2
284 check 'cat <<-EOF'"${nl}${nl}" '' 2
285 check 'cat << '"'EOF'${nl}${nl}" '' 2
286 check 'cat <<-"EOF"'"${nl}${nl}" '' 2
288 check 'cat << EOF'"${nl}"'line 1'"${nl}" '' 2
289 check 'cat <<-EOF'"${nl}"' line 1'"${nl}" '' 2
290 check 'cat << EOF'"${nl}"'line 1'"${nl}"' line 2'"${nl}" '' 2
291 check 'cat <<-EOF'"${nl}"' line 1'"${nl}"'line 2'"${nl}" '' 2
293 check 'cat << EOF'"${nl}line 1${nl}${nl}line3${nl}${nl}5!${nl}" '' 2
298 atf_test_case lineends
300 atf_set "descr" "Tests for line endings in here documents"
305 # note that "check" removes newlines from stdout before comparing.
306 # (they become blanks, provided there is something before & after)
308 check 'cat << \echo'"${nl}"'\'"${nl}echo${nl}echo${nl}" '\' 0
309 check 'cat << echo'"${nl}"'\'"${nl}echo${nl}echo${nl}" 'echo' 0
310 check 'cat << echo'"${nl}"'\\'"${nl}echo${nl}echo${nl}" '\' 0
312 check 'X=3; cat << ec\ho'"${nl}"'$X\'"${nl}echo${nl}echo${nl}" \
314 check 'X=3; cat << echo'"${nl}"'$X'"${nl}echo${nl}echo${nl}" \
316 check 'X=3; cat << echo'"${nl}"'$X\'"${nl}echo${nl}echo${nl}" \
318 check 'X=3; cat << echo'"${nl}"'${X}\'"${nl}echo${nl}echo${nl}" \
320 check 'X=3; cat << echo'"${nl}"'\$X\'"${nl}echo${nl}echo${nl}" \
322 check 'X=3; cat << echo'"${nl}"'\\$X \'"${nl}echo${nl}echo${nl}" \
326 'cat << "echo"'"${nl}"'line1\'"${nl}"'line2\'"${nl}echo${nl}echo${nl}" \
329 'cat << echo'"${nl}"'line1\'"${nl}"'line2\'"${nl}echo${nl}echo${nl}" \
335 atf_test_case multiple
337 atf_set "descr" "Tests for multiple here documents on one cmd line"
343 "(cat ; cat <&3) <<EOF0 3<<EOF3${nl}STDIN${nl}EOF0${nl}-3-${nl}EOF3${nl}" \
346 check "(read line; echo \"\$line\"; cat <<EOF1; echo \"\$line\") <<EOF2
351 " 'The Line The File The Line' 0
353 check "(read line; echo \"\$line\"; cat <<EOF; echo \"\$line\") <<EOF
358 " 'The Line The File The Line' 0
360 check "V=1; W=2; cat <<-1; cat <<2; cat <<- 3; cat <<'4';"' cat <<\5
379 ' '1 2 3 4 5 5 4*2+$V $W 1 2 3 7+$V $W+6' 0
386 atf_set "descr" "Tests for nested here documents for one cmd"
392 'cat << EOF1'"${nl}"'$(cat << EOF2'"${nl}LINE${nl}EOF2${nl}"')'"${nl}EOF1${nl}"\
395 # This next one fails ... and correctly, so we will omit it (bad test)
396 # Reasoning is that the correct data "$(cat << EOF2)\nLINE\nEOF2\n" is
397 # collected for the outer (EOF1) heredoc, when that is parsed, it looks
402 # which looks like a good command - except it is being parsed in "heredoc"
403 # syntax, which means it is enclosed in double quotes, which means that
404 # the newline after the ')' in the first line is not a newline token, but
405 # just a character. The EOF2 heredoc cannot start until after the next
406 # newline token, of which there are none here... LINE and EOF2 are just
407 # more data in the outer EOF1 heredoc for its "cat" command to read & write.
409 # The previous sub-test works because there the \n comes inside the
410 # $( ), and in there, the outside quoting rules are suspended, and it
411 # all starts again - so that \n is a newline token, and the EOF2 heredoc
415 # 'cat << EOF1'"${nl}"'$(cat << EOF2 )'"${nl}LINE${nl}EOF2${nl}EOF1${nl}" \
418 L='cat << EOF1'"${nl}"'LINE1$(cat << EOF2'"${nl}"
419 L="${L}"'LINE2$(cat << EOF3'"${nl}"
420 L="${L}"'LINE3$(cat << EOF4'"${nl}"
421 L="${L}"'LINE4$(cat << EOF5'"${nl}"
422 L="${L}LINE5${nl}EOF5${nl})4${nl}EOF4${nl})3${nl}"
423 L="${L}EOF3${nl})2${nl}EOF2${nl})1${nl}EOF1${nl}"
443 check "${L}" 'LINE1LINE2LINE3LINE4LINE54321' 0
448 atf_test_case quoting
450 atf_set "descr" "Tests for use of quotes inside here documents"
455 check 'X=!; cat <<- E\0F
456 <'\''"'\'' \\$X\$X "'\''" \\>
458 ' '<'\''"'\'' \\$X\$X "'\''" \\>' 0
460 check 'X=!; cat <<- E0F
461 <'\''"'\'' \\$X\$X "'\''" \\>
463 ' '<'\''"'\'' \!$X "'\''" \>' 0
466 $( echo "'\''" ) $( echo '\''"'\'' ) $( echo \\ )
470 check 'X=12345; Y="string1 line1?-line2"; Z=; unset W; cat <<-EOF
471 ${#X}${Z:-${Y}}${W+junk}${Y%%l*}${Y#*\?}
472 "$Z"'\''$W'\'' ${Y%" "*} $(( X + 54321 ))
474 ' '5string1 line1?-line2string1 -line2 ""'\'\'' string1 66666' 0
479 atf_test_case side_effects
480 side_effects_head() {
481 atf_set "descr" "Tests how side effects in here documents are handled"
483 side_effects_body() {
485 atf_check -s exit:0 -o inline:'2\n1\n' -e empty ${TEST_SH} -c '
494 atf_test_case vicious
496 atf_set "descr" "Tests for obscure and obnoxious uses of here docs"
501 cat <<- \END_SCRIPT > script
510 atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} script
512 # This next one is causing discussion currently (late Feb 2016)
513 # amongst stds writers & implementors. Consequently we
514 # will not check what it produces. The eventual result
515 # seems unlikely to be what we currently output, which
518 # B:echo line 2)" && prefix DASH_CODE <<DASH_CODE
523 # The likely intended output is ...
528 # DASH_CODE:echo line 4)"
529 # DASH_CODE:echo line 5
531 # The difference is explained by differing opinions on just
532 # when processing of a here doc should start
534 cat <<- \END_SCRIPT > script
535 prefix() { sed -e "s/^/$1:/"; }
538 prefix A <<XXX && echo "$(prefix B <<XXX
541 echo line 2)" && prefix DASH_CODE <<DASH_CODE
549 # we will just verify that the shell can parse the
550 # script somehow, and doesn't fall over completely...
552 atf_check -s exit:0 -o ignore -e empty ${TEST_SH} script
555 atf_init_test_cases() {
556 atf_add_test_case do_simple # not worthy of a comment
557 atf_add_test_case end_markers # the mundane, the weird, the bizarre
558 atf_add_test_case incomplete # where the end marker isn't...
559 atf_add_test_case lineends # test weird line endings in heredocs
560 atf_add_test_case multiple # multiple << operators on one cmd
561 atf_add_test_case nested # here docs inside here docs
562 atf_add_test_case quoting # stuff quoted inside
563 atf_add_test_case side_effects # here docs that modify environment
564 atf_add_test_case vicious # evil test from the austin-l list...