t_here.sh revision 1.4
1# $NetBSD: t_here.sh,v 1.4 2016/03/08 14:21:02 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 33check() 34{ 35 fail=false 36 TEMP_FILE=$( mktemp OUT.XXXXXX ) 37 38 # our local shell (ATF_SHELL) better do quoting correctly... 39 # some of the tests expect us to expand $nl internally... 40 CMD="nl='${nl}'; $1" 41 42 rm -f trace.* 43 result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )" 44 STATUS=$? 45 46 if [ "${STATUS}" -ne "$3" ]; then 47 echo >&2 "expected exit code $3, got ${STATUS}" 48 49 # don't actually fail just because of wrong exit code 50 # unless we either expected, or received "good" 51 case "$3/${STATUS}" in 52 (*/0|0/*) fail=true;; 53 esac 54 fi 55 56 if [ "$3" -eq 0 ]; then 57 if [ -s "${TEMP_FILE}" ]; then 58 echo >&2 "Messages produced on stderr unexpected..." 59 cat "${TEMP_FILE}" >&2 60 fail=true 61 fi 62 else 63 if ! [ -s "${TEMP_FILE}" ]; then 64 echo >&2 "Expected messages on stderr, nothing produced" 65 fail=true 66 fi 67 fi 68 rm -f "${TEMP_FILE}" 69 70 # Remove newlines (use local shell for this) 71 oifs="$IFS" 72 IFS="$nl" 73 result="$(echo $result)" 74 IFS="$oifs" 75 if [ "$2" != "$result" ] 76 then 77 echo >&2 "Expected output '$2', received '$result'" 78 fail=true 79 fi 80 81 $fail && atf_fail "test of '$1' failed" 82 return 0 83} 84 85atf_test_case do_simple 86do_simple_head() { 87 atf_set "descr" "Basic tests for here documents" 88} 89do_simple_body() { 90 y=x 91 92 IFS= 93 check 'x=`cat <<EOF'$nl'text'${nl}EOF$nl'`; echo $x' 'text' 0 94 check 'x=`cat <<\EOF'$nl'text'${nl}EOF$nl'`; echo $x' 'text' 0 95 96 check "y=${y};"'x=`cat <<EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \ 97 'text' 0 98 check "y=${y};"'x=`cat <<\EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \ 99 'te${y}t' 0 100 check "y=${y};"'x=`cat <<"EOF"'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \ 101 'te${y}t' 0 102 check "y=${y};"'x=`cat <<'"'EOF'"$nl'te${y}t'${nl}EOF$nl'`; echo $x' \ 103 'te${y}t' 0 104 105 # check that quotes in the here doc survive and cause no problems 106 check "cat <<EOF${nl}te'xt${nl}EOF$nl" "te'xt" 0 107 check "cat <<\EOF${nl}te'xt${nl}EOF$nl" "te'xt" 0 108 check "cat <<'EOF'${nl}te'xt${nl}EOF$nl" "te'xt" 0 109 check "cat <<EOF${nl}te\"xt${nl}EOF$nl" 'te"xt' 0 110 check "cat <<\EOF${nl}te\"xt${nl}EOF$nl" 'te"xt' 0 111 check "cat <<'EOF'${nl}te\"xt${nl}EOF$nl" 'te"xt' 0 112 check "cat <<'EO'F${nl}te\"xt${nl}EOF$nl" 'te"xt' 0 113 114 check "y=${y};"'x=`cat <<EOF'$nl'te'"'"'${y}t'${nl}EOF$nl'`; echo $x' \ 115 'te'"'"'xt' 0 116 check "y=${y};"'x=`cat <<EOF'$nl'te'"''"'${y}t'${nl}EOF$nl'`; echo $x' \ 117 'te'"''"'xt' 0 118 119 # note that the blocks of empty space in the following must 120 # be entirely tab characters, no spaces. 121 122 check 'x=`cat <<EOF'"$nl text${nl}EOF$nl"'`; echo "$x"' \ 123 ' text' 0 124 check 'x=`cat <<-EOF'"$nl text${nl}EOF$nl"'`; echo $x' \ 125 'text' 0 126 check 'x=`cat <<-EOF'"${nl}text${nl} EOF$nl"'`; echo $x' \ 127 'text' 0 128 check 'x=`cat <<-\EOF'"$nl text${nl} EOF$nl"'`; echo $x' \ 129 'text' 0 130 check 'x=`cat <<- "EOF"'"$nl text${nl}EOF$nl"'`; echo $x' \ 131 'text' 0 132 check 'x=`cat <<- '"'EOF'${nl}text${nl} EOF$nl"'`; echo $x' \ 133 'text' 0 134} 135 136atf_test_case incomplete 137incomplete_head() { 138 atf_set "descr" "Basic tests for incomplete here documents" 139} 140incomplete_body() { 141 check 'cat <<EOF' '' 2 142 check 'cat <<- EOF' '' 2 143 check 'cat <<\EOF' '' 2 144 check 'cat <<- \EOF' '' 2 145 146 check 'cat <<EOF'"${nl}" '' 2 147 check 'cat <<- EOF'"${nl}" '' 2 148 check 'cat <<'"'EOF'${nl}" '' 2 149 check 'cat <<- "EOF"'"${nl}" '' 2 150 151 check 'cat << EOF'"${nl}${nl}" '' 2 152 check 'cat <<-EOF'"${nl}${nl}" '' 2 153 check 'cat << '"'EOF'${nl}${nl}" '' 2 154 check 'cat <<-"EOF"'"${nl}${nl}" '' 2 155 156 check 'cat << EOF'"${nl}"'line 1'"${nl}" '' 2 157 check 'cat <<-EOF'"${nl}"' line 1'"${nl}" '' 2 158 check 'cat << EOF'"${nl}"'line 1'"${nl}"' line 2'"${nl}" '' 2 159 check 'cat <<-EOF'"${nl}"' line 1'"${nl}"'line 2'"${nl}" '' 2 160 161 check 'cat << EOF'"${nl}line 1${nl}${nl}line3${nl}${nl}5!${nl}" '' 2 162} 163 164atf_test_case multiple 165multiple_head() { 166 atf_set "descr" "Tests for multiple here documents for one cmd" 167} 168multiple_body() { 169 check \ 170 "(cat ; cat <&3) <<EOF0 3<<EOF3${nl}STDIN${nl}EOF0${nl}-3-${nl}EOF3${nl}" \ 171 'STDIN -3-' 0 172 173 check "(read line; echo \"\$line\"; cat <<EOF1; echo \"\$line\") <<EOF2 174The File 175EOF1 176The Line 177EOF2 178" 'The Line The File The Line' 0 179 180 check "(read line; echo \"\$line\"; cat <<EOF; echo \"\$line\") <<EOF 181The File 182EOF 183The Line 184EOF 185" 'The Line The File The Line' 0 186 187} 188 189atf_test_case vicious 190vicious_head() { 191 atf_set "descr" "Tests for obscure and obnoxious uses of here docs" 192} 193vicious_body() { 194 195 cat <<- \END_SCRIPT > script 196 cat <<ONE && cat \ 197 <<TWO 198 a 199 ONE 200 b 201 TWO 202 END_SCRIPT 203 204 atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} script 205 206 # This next one is causing discussion currently (late Feb 2016) 207 # amongst stds writers & implementors. Consequently we 208 # will not check what it produces. The eventual result 209 # seems unlikely to be what we currently output, which 210 # is: 211 # A:echo line 1 212 # B:echo line 2)" && prefix DASH_CODE <<DASH_CODE 213 # B:echo line 3 214 # line 4 215 # line 5 216 # 217 # The likely intended output is ... 218 # 219 # A:echo line 3 220 # B:echo line 1 221 # line 2 222 # DASH_CODE:echo line 4)" 223 # DASH_CODE:echo line 5 224 # 225 # The difference is explained by differeng opinions on just 226 # when processing of a here doc should start 227 228 cat <<- \END_SCRIPT > script 229 prefix() { sed -e "s/^/$1:/"; } 230 DASH_CODE() { :; } 231 232 prefix A <<XXX && echo "$(prefix B <<XXX 233 echo line 1 234 XXX 235 echo line 2)" && prefix DASH_CODE <<DASH_CODE 236 echo line 3 237 XXX 238 echo line 4)" 239 echo line 5 240 DASH_CODE 241 END_SCRIPT 242 243 # we will just verify that the shell can parse the 244 # script somehow, and doesn't fall over completely... 245 246 atf_check -s exit:0 -o ignore -e empty ${TEST+SH} script 247} 248 249atf_init_test_cases() { 250 atf_add_test_case do_simple 251 atf_add_test_case incomplete 252 atf_add_test_case multiple # multiple << operators on one cmd 253 atf_add_test_case vicious # evil test from the austin-l list... 254} 255