xref: /freebsd/contrib/netbsd-tests/bin/sh/t_here.sh (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1# $NetBSD: t_here.sh,v 1.6 2016/03/31 16:21:52 christos 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	oifs="$IFS"
82	IFS="$nl"
83	result="$(echo $result)"
84	IFS="$oifs"
85	if [ "$2" != "$result" ]
86	then
87		echo >&2 "[$TEST_NUM] Expected output '$2', received '$result'"
88		fail=true
89	fi
90
91	if $fail
92	then
93		echo >&2 "[$TEST_NUM] Full command: <<${CMD}>>"
94	fi
95
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 ))
100		return 0
101	}
102	$fail && atf_fail "Test[$TEST_NUM] of '$1' failed"
103	return 0
104}
105
106results()
107{
108	test -z "${TEST_ID}" && return 0
109	test -z "${TEST_FAILURES}" && return 0
110
111	echo >&2 "=========================================="
112	echo >&2 "While testing '${TEST_ID}'"
113	echo >&2 " - - - - - - - - - - - - - - - - -"
114	echo >&2 "${TEST_FAILURES}"
115	atf_fail \
116 "Test ${TEST_ID}: $TEST_FAIL_COUNT subtests (of $TEST_NUM) failed - see stderr"
117}
118
119atf_test_case do_simple
120do_simple_head() {
121	atf_set "descr" "Basic tests for here documents"
122}
123do_simple_body() {
124	y=x
125
126	reset 'simple'
127	IFS=' 	'
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
130
131	check "y=${y};"'x=`cat <<EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \
132			'text' 0
133	check "y=${y};"'x=`cat <<\EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x'  \
134			'te${y}t' 0
135	check "y=${y};"'x=`cat <<"EOF"'$nl'te${y}t'${nl}EOF$nl'`; echo $x'  \
136			'te${y}t' 0
137	check "y=${y};"'x=`cat <<'"'EOF'"$nl'te${y}t'${nl}EOF$nl'`; echo $x'  \
138			'te${y}t' 0
139
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
148
149	check "y=${y};"'x=`cat <<EOF'$nl'te'"'"'${y}t'${nl}EOF$nl'`; echo $x' \
150			'te'"'"'xt' 0
151	check "y=${y};"'x=`cat <<EOF'$nl'te'"''"'${y}t'${nl}EOF$nl'`; echo $x' \
152			'te'"''"'xt' 0
153
154	# note that the blocks of empty space in the following must
155	# be entirely tab characters, no spaces.
156
157	check 'x=`cat <<EOF'"$nl	text${nl}EOF$nl"'`; echo "$x"' \
158			'	text' 0
159	check 'x=`cat <<-EOF'"$nl	text${nl}EOF$nl"'`; echo $x' \
160			'text' 0
161	check 'x=`cat <<-EOF'"${nl}text${nl}	EOF$nl"'`; echo $x' \
162			'text' 0
163	check 'x=`cat <<-\EOF'"$nl	text${nl}	EOF$nl"'`; echo $x' \
164			'text' 0
165	check 'x=`cat <<- "EOF"'"$nl	text${nl}EOF$nl"'`; echo $x' \
166			'text' 0
167	check 'x=`cat <<- '"'EOF'${nl}text${nl}	EOF$nl"'`; echo $x' \
168			'text' 0
169	results
170}
171
172atf_test_case end_markers
173end_markers_head() {
174	atf_set "descr" "Tests for various end markers of here documents"
175}
176end_markers_body() {
177
178	reset 'end_markers'
179	for end in EOF 1 \! '$$$' "string " a\\\ a\\\ \   '&' '' ' ' '  ' \
180	    --STRING-- . '~~~' ')' '(' '#' '()' '(\)' '(\/)' '--' '\' '{' '}' \
181VERYVERYVERYVERYLONGLONGLONGin_fact_absurdly_LONG_LONG_HERE_DOCUMENT_TERMINATING_MARKER_THAT_goes_On_forever_and_ever_and_ever...
182	do
183		# check unquoted end markers
184		case "${end}" in
185		('' | *[' ()\$&#*~']* ) ;;	# skip unquoted endmark test for these
186		(*)	check \
187	'x=$(cat << '"${end}${nl}text${nl}${end}${nl}"'); printf %s "$x"' 'text' 0
188			;;
189		esac
190
191		# and quoted end markers
192		check \
193	'x=$(cat <<'"'${end}'${nl}text${nl}${end}${nl}"'); printf %s "$x"' 'text' 0
194
195		# and see what happens if we encounter "almost" an end marker
196		case "${#end}" in
197		(0|1)	;;		# too short to try truncation tests
198		(*)	check \
199   'x=$(cat <<'"'${end}'${nl}text${nl}${end%?}${nl}${end}${nl}"'); printf %s "$x"' \
200				"text ${end%?}" 0
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			;;
208		esac
209
210		# or something that is a little longer
211		check \
212   'x=$(cat <<'"'${end}'${nl}text${nl}${end}x${nl}${end}${nl}"'); printf %s "$x"' \
213				"text ${end}x" 0
214		check \
215    'x=$(cat <<'"'${end}'${nl}text${nl}!${end}${nl}${end}${nl}"'); printf %s "$x"' \
216				"text !${end}" 0
217
218		# or which does not begin at start of line
219		check \
220    'x=$(cat <<'"'${end}'${nl}text${nl} ${end}${nl}${end}${nl}"'); printf %s "$x"' \
221				"text  ${end}" 0
222		check \
223    'x=$(cat <<'"'${end}'${nl}text${nl}	${end}${nl}${end}${nl}"'); printf %s "$x"' \
224				"text 	${end}" 0
225
226		# or end at end of line
227		check \
228    'x=$(cat <<'"'${end}'${nl}text${nl}${end} ${nl}${end}${nl}"'); printf %s "$x"' \
229				"text ${end} " 0
230
231		# or something that is correct much of the way, but then...
232
233		case "${#end}" in
234		(0)	;;		# cannot test this one
235		(1)	check \
236    'x=$(cat <<'"'${end}'${nl}text${nl}${end}${end}${nl}${end}${nl}"'); printf %s "$x"' \
237				"text ${end}${end}" 0
238			;;
239		(2-7)	pfx="${end%?}"
240			check \
241    'x=$(cat <<'"'${end}'${nl}text${nl}${end}${pfx}${nl}${end}${nl}"'); printf %s "$x"' \
242				"text ${end}${pfx}" 0
243			check \
244    'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${end}${nl}${end}${nl}"'); printf %s "$x"' \
245				"text ${pfx}${end}" 0
246			;;
247		(*)	pfx=${end%??????}; sfx=${end#??????}
248			check \
249    'x=$(cat <<'"'${end}'${nl}text${nl}${end}${sfx}${nl}${end}${nl}"'); printf %s "$x"' \
250				"text ${end}${sfx}" 0
251			check \
252    'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${end}${nl}${end}${nl}"'); printf %s "$x"' \
253				"text ${pfx}${end}" 0
254			check \
255    'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${sfx}${nl}${end}${nl}"'); printf %s "$x"' \
256				"text ${pfx}${sfx}" 0
257			;;
258		esac
259	done
260
261	# Add striptabs tests (in similar way) here one day...
262
263	results
264}
265
266atf_test_case incomplete
267incomplete_head() {
268	atf_set "descr" "Basic tests for incomplete here documents"
269}
270incomplete_body() {
271	reset incomplete
272
273	check 'cat <<EOF' '' 2
274	check 'cat <<- EOF' '' 2
275	check 'cat <<\EOF' '' 2
276	check 'cat <<- \EOF' '' 2
277
278	check 'cat <<EOF'"${nl}" '' 2
279	check 'cat <<- EOF'"${nl}" '' 2
280	check 'cat <<'"'EOF'${nl}" '' 2
281	check 'cat <<- "EOF"'"${nl}" '' 2
282
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
287
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
292
293	check 'cat << EOF'"${nl}line 1${nl}${nl}line3${nl}${nl}5!${nl}" '' 2
294
295	results
296}
297
298atf_test_case lineends
299lineends_head() {
300	atf_set "descr" "Tests for line endings in here documents"
301}
302lineends_body() {
303	reset lineends
304
305	# note that "check" removes newlines from stdout before comparing.
306	# (they become blanks, provided there is something before & after)
307
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
311
312	check 'X=3; cat << ec\ho'"${nl}"'$X\'"${nl}echo${nl}echo${nl}" \
313		'$X\'  0
314	check 'X=3; cat <<  echo'"${nl}"'$X'"${nl}echo${nl}echo${nl}" \
315		'3'  0
316	check 'X=3; cat <<  echo'"${nl}"'$X\'"${nl}echo${nl}echo${nl}" \
317		''  0
318	check 'X=3; cat <<  echo'"${nl}"'${X}\'"${nl}echo${nl}echo${nl}" \
319		'3echo'  0
320	check 'X=3; cat <<  echo'"${nl}"'\$X\'"${nl}echo${nl}echo${nl}" \
321		'$Xecho'  0
322	check 'X=3; cat <<  echo'"${nl}"'\\$X \'"${nl}echo${nl}echo${nl}" \
323		'\3 echo'  0
324
325	check \
326  'cat << "echo"'"${nl}"'line1\'"${nl}"'line2\'"${nl}echo${nl}echo${nl}" \
327		 'line1\ line2\'  0
328	check \
329	  'cat << echo'"${nl}"'line1\'"${nl}"'line2\'"${nl}echo${nl}echo${nl}" \
330	  'line1line2echo'  0
331
332	results
333}
334
335atf_test_case multiple
336multiple_head() {
337	atf_set "descr" "Tests for multiple here documents on one cmd line"
338}
339multiple_body() {
340	reset multiple
341
342	check \
343    "(cat ; cat <&3) <<EOF0 3<<EOF3${nl}STDIN${nl}EOF0${nl}-3-${nl}EOF3${nl}" \
344		'STDIN -3-' 0
345
346	check "(read line; echo \"\$line\"; cat <<EOF1; echo \"\$line\") <<EOF2
347The File
348EOF1
349The Line
350EOF2
351"			'The Line The File The Line' 0
352
353	check "(read line; echo \"\$line\"; cat <<EOF; echo \"\$line\") <<EOF
354The File
355EOF
356The Line
357EOF
358"			'The Line The File The Line' 0
359
360	check "V=1; W=2; cat <<-1; cat <<2; cat <<- 3; cat <<'4';"' cat <<\5
361		$V
362		$W
363		3
364	4
365	5
366			1
3672
368	5
369					4*$W+\$V
370	3
371$W
3721
3732
3743
3754
3767+$V
377$W+6
3785
379'			'1 2 3 4 5 5 4*2+$V $W 1 2 3 7+$V $W+6'	0
380
381	results
382}
383
384atf_test_case nested
385nested_head() {
386	atf_set "descr" "Tests for nested here documents for one cmd"
387}
388nested_body() {
389	reset nested
390
391	check \
392'cat << EOF1'"${nl}"'$(cat << EOF2'"${nl}LINE${nl}EOF2${nl}"')'"${nl}EOF1${nl}"\
393	'LINE' 0
394
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
398# like
399#	$(cat <<EOF2)
400#	LINE
401#	EOF2
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.
408#
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
412# is processed.
413#
414#	check \
415#   'cat << EOF1'"${nl}"'$(cat << EOF2 )'"${nl}LINE${nl}EOF2${nl}EOF1${nl}" \
416#	'LINE' 0
417
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}"
424
425	# That mess is ...
426	#
427	#	cat <<EOF1
428	#	LINE1$(cat << EOF2
429	#	LINE2$(cat << EOF3
430	#	LINE3$(cat << EOF4
431	#	LINE4$(cat << EOF5
432	#	LINE5
433	#	EOF5
434	#	)4
435	#	EOF4
436	#	)3
437	#	EOF3
438	#	)2
439	#	EOF2
440	#	)1
441	#	EOF1
442
443	check "${L}" 'LINE1LINE2LINE3LINE4LINE54321' 0
444
445	results
446}
447
448atf_test_case quoting
449quoting_head() {
450	atf_set "descr" "Tests for use of quotes inside here documents"
451}
452quoting_body() {
453	reset quoting
454
455	check 'X=!; cat <<- E\0F
456		<'\''"'\'' \\$X\$X  "'\''" \\>
457	E0F
458	'	'<'\''"'\'' \\$X\$X  "'\''" \\>'	0
459
460	check 'X=!; cat <<- E0F
461		<'\''"'\'' \\$X\$X  "'\''" \\>
462	E0F
463	'	'<'\''"'\'' \!$X  "'\''" \>'	0
464
465	check 'cat <<- END
466		$( echo "'\''" ) $( echo '\''"'\'' ) $( echo \\ )
467	END
468	'	"' \" \\"		0
469
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 ))
473	EOF
474	'	'5string1 line1?-line2string1 -line2 ""'\'\'' string1 66666' 0
475
476	results
477}
478
479atf_test_case side_effects
480side_effects_head() {
481	atf_set "descr" "Tests how side effects in here documents are handled"
482}
483side_effects_body() {
484
485	atf_check -s exit:0 -o inline:'2\n1\n' -e empty ${TEST_SH} -c '
486		unset X
487		cat <<-EOF
488		${X=2}
489		EOF
490		echo "${X-1}"
491		'
492}
493
494atf_test_case vicious
495vicious_head() {
496	atf_set "descr" "Tests for obscure and obnoxious uses of here docs"
497}
498vicious_body() {
499	reset
500
501	cat <<- \END_SCRIPT > script
502		cat <<ONE && cat \
503		<<TWO
504		a
505		ONE
506		b
507		TWO
508	END_SCRIPT
509
510	atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} script
511
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
516	# is:
517	#	A:echo line 1
518	#	B:echo line 2)" && prefix DASH_CODE <<DASH_CODE
519	#	B:echo line 3
520	#	line 4
521	#	line 5
522	#
523	# The likely intended output is ...
524	#
525	#	A:echo line 3
526	#	B:echo line 1
527	#	line 2
528	#	DASH_CODE:echo line 4)"
529	#	DASH_CODE:echo line 5
530	#
531	# The difference is explained by differing opinions on just
532	# when processing of a here doc should start
533
534	cat <<- \END_SCRIPT > script
535		prefix() { sed -e "s/^/$1:/"; }
536		DASH_CODE() { :; }
537
538		prefix A <<XXX && echo "$(prefix B <<XXX
539		echo line 1
540		XXX
541		echo line 2)" && prefix DASH_CODE <<DASH_CODE
542		echo line 3
543		XXX
544		echo line 4)"
545		echo line 5
546		DASH_CODE
547	END_SCRIPT
548
549	# we will just verify that the shell can parse the
550	# script somehow, and doesn't fall over completely...
551
552	atf_check -s exit:0 -o ignore -e empty ${TEST_SH} script
553}
554
555atf_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...
565}
566