Home | History | Annotate | Line # | Download | only in sh
t_redir.sh revision 1.6
      1  1.6  christos # $NetBSD: t_redir.sh,v 1.6 2016/05/02 01:47:14 christos Exp $
      2  1.1  christos #
      3  1.1  christos # Copyright (c) 2016 The NetBSD Foundation, Inc.
      4  1.1  christos # All rights reserved.
      5  1.1  christos #
      6  1.1  christos # Redistribution and use in source and binary forms, with or without
      7  1.1  christos # modification, are permitted provided that the following conditions
      8  1.1  christos # are met:
      9  1.1  christos # 1. Redistributions of source code must retain the above copyright
     10  1.1  christos #    notice, this list of conditions and the following disclaimer.
     11  1.1  christos # 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  christos #    notice, this list of conditions and the following disclaimer in the
     13  1.1  christos #    documentation and/or other materials provided with the distribution.
     14  1.1  christos #
     15  1.1  christos # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16  1.1  christos # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17  1.1  christos # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  1.1  christos # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19  1.1  christos # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20  1.1  christos # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21  1.1  christos # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22  1.1  christos # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23  1.1  christos # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24  1.1  christos # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.1  christos # POSSIBILITY OF SUCH DAMAGE.
     26  1.1  christos #
     27  1.2  christos # the implementation of "sh" to test
     28  1.2  christos : ${TEST_SH:="/bin/sh"}
     29  1.1  christos 
     30  1.4  christos # Any failures in this first test means it is not worth bothering looking
     31  1.4  christos # for causes of failures in any other tests, make this one work first.
     32  1.4  christos 
     33  1.4  christos # Problems with this test usually mean inadequate ATF_SHELL used for testing.
     34  1.4  christos # (though if all pass but the last, it might be a TEST_SH problem.)
     35  1.4  christos 
     36  1.4  christos atf_test_case basic_test_method_test
     37  1.4  christos basic_test_method_test_head()
     38  1.4  christos {
     39  1.4  christos 	atf_set "descr" "Tests that test method works as expected"
     40  1.4  christos }
     41  1.4  christos basic_test_method_test_body()
     42  1.4  christos {
     43  1.4  christos 	cat <<- 'DONE' |
     44  1.4  christos 	DONE
     45  1.4  christos 	atf_check -s exit:0 -o empty -e empty ${TEST_SH}
     46  1.4  christos 	cat <<- 'DONE' |
     47  1.4  christos 	DONE
     48  1.4  christos 	atf_check -s exit:0 -o match:0 -e empty ${TEST_SH} -c 'wc -l'
     49  1.4  christos 
     50  1.4  christos 	cat <<- 'DONE' |
     51  1.4  christos 		echo hello
     52  1.4  christos 	DONE
     53  1.4  christos 	atf_check -s exit:0 -o match:hello -e empty ${TEST_SH} 
     54  1.4  christos 	cat <<- 'DONE' |
     55  1.4  christos 		echo hello
     56  1.4  christos 	DONE
     57  1.4  christos 	atf_check -s exit:0 -o match:1 -e empty ${TEST_SH} -c 'wc -l'
     58  1.4  christos 
     59  1.4  christos 	cat <<- 'DONE' |
     60  1.4  christos 		echo hello\
     61  1.4  christos 					world
     62  1.4  christos 	DONE
     63  1.4  christos 	atf_check -s exit:0 -o match:helloworld -e empty ${TEST_SH} 
     64  1.4  christos 	cat <<- 'DONE' |
     65  1.4  christos 		echo hello\
     66  1.4  christos 					world
     67  1.4  christos 	DONE
     68  1.4  christos 	atf_check -s exit:0 -o match:2 -e empty ${TEST_SH} -c 'wc -l'
     69  1.4  christos 
     70  1.4  christos 	printf '%s\n%s\n%s\n' Line1 Line2 Line3 > File
     71  1.4  christos 	atf_check -s exit:0 -o inline:'Line1\nLine2\nLine3\n' -e empty \
     72  1.4  christos 		${TEST_SH} -c 'cat File'
     73  1.4  christos 
     74  1.4  christos 	cat <<- 'DONE' |
     75  1.4  christos 		set -- X "" '' Y
     76  1.4  christos 		echo ARGS="${#}"
     77  1.4  christos 		echo '' -$1- -$2- -$3- -$4-
     78  1.4  christos 		cat <<EOF
     79  1.4  christos 			X=$1
     80  1.4  christos 		EOF
     81  1.4  christos 		cat <<\EOF
     82  1.4  christos 			Y=$4
     83  1.4  christos 		EOF
     84  1.4  christos 	DONE
     85  1.4  christos 	atf_check -s exit:0 -o match:ARGS=4 -o match:'-X- -- -- -Y-' \
     86  1.4  christos 		-o match:X=X -o match:'Y=\$4' -e empty ${TEST_SH} 
     87  1.4  christos }
     88  1.4  christos 
     89  1.4  christos atf_test_case do_input_redirections
     90  1.4  christos do_input_redirections_head()
     91  1.4  christos {
     92  1.4  christos 	atf_set "descr" "Tests that simple input redirection works"
     93  1.4  christos }
     94  1.4  christos do_input_redirections_body()
     95  1.4  christos {
     96  1.4  christos 	printf '%s\n%s\n%s\nEND\n' 'First Line' 'Second Line' 'Line 3' >File
     97  1.4  christos 
     98  1.4  christos 	atf_check -s exit:0 -e empty \
     99  1.4  christos 		-o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
    100  1.4  christos 		${TEST_SH} -c 'cat < File'
    101  1.4  christos 	atf_check -s exit:0 -e empty \
    102  1.4  christos 		-o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
    103  1.4  christos 		${TEST_SH} -c 'cat <File'
    104  1.4  christos 	atf_check -s exit:0 -e empty \
    105  1.4  christos 		-o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
    106  1.4  christos 		${TEST_SH} -c 'cat< File'
    107  1.4  christos 	atf_check -s exit:0 -e empty \
    108  1.4  christos 		-o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
    109  1.4  christos 		${TEST_SH} -c 'cat < "File"'
    110  1.4  christos 	atf_check -s exit:0 -e empty \
    111  1.4  christos 		-o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
    112  1.4  christos 		${TEST_SH} -c '< File cat'
    113  1.4  christos 
    114  1.4  christos 	ln File wc
    115  1.4  christos 	atf_check -s exit:0 -e empty \
    116  1.4  christos 		-o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
    117  1.4  christos 		${TEST_SH} -c '< wc cat'
    118  1.4  christos 
    119  1.4  christos 	mv wc cat
    120  1.4  christos 	atf_check -s exit:0 -e empty -o match:4 \
    121  1.4  christos 		${TEST_SH} -c '< cat wc'
    122  1.4  christos 
    123  1.4  christos 
    124  1.4  christos 	cat <<- 'EOF' |
    125  1.5  christos 		for l in 1 2 3; do
    126  1.4  christos 			read line < File
    127  1.4  christos 			echo "$line"
    128  1.4  christos 		done
    129  1.4  christos 	EOF
    130  1.4  christos 	atf_check -s exit:0 -e empty \
    131  1.4  christos 		-o inline:'First Line\nFirst Line\nFirst Line\n' \
    132  1.4  christos 		${TEST_SH}
    133  1.4  christos 
    134  1.4  christos 	cat <<- 'EOF' |
    135  1.5  christos 		for l in 1 2 3; do
    136  1.4  christos 			read line
    137  1.4  christos 			echo "$line"
    138  1.4  christos 		done <File
    139  1.4  christos 	EOF
    140  1.4  christos 	atf_check -s exit:0 -e empty \
    141  1.4  christos 		-o inline:'First Line\nSecond Line\nLine 3\n' \
    142  1.4  christos 		${TEST_SH}
    143  1.4  christos 
    144  1.4  christos 	cat <<- 'EOF' |
    145  1.5  christos 		for l in 1 2 3; do
    146  1.4  christos 			read line < File
    147  1.4  christos 			echo "$line"
    148  1.4  christos 		done <File
    149  1.4  christos 	EOF
    150  1.4  christos 	atf_check -s exit:0 -e empty \
    151  1.4  christos 		-o inline:'First Line\nFirst Line\nFirst Line\n' \
    152  1.4  christos 		${TEST_SH}
    153  1.4  christos 
    154  1.4  christos 	cat <<- 'EOF' |
    155  1.4  christos 		line=
    156  1.4  christos 		while [ "$line" != END ]; do
    157  1.4  christos 			read line || exit 1
    158  1.4  christos 			echo "$line"
    159  1.4  christos 		done <File
    160  1.4  christos 	EOF
    161  1.4  christos 	atf_check -s exit:0 -e empty \
    162  1.4  christos 		-o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
    163  1.4  christos 		${TEST_SH}
    164  1.4  christos 
    165  1.4  christos 	cat <<- 'EOF' |
    166  1.4  christos 		while :; do
    167  1.4  christos 			read line || exit 0
    168  1.4  christos 			echo "$line"
    169  1.4  christos 		done <File
    170  1.4  christos 	EOF
    171  1.4  christos 	atf_check -s exit:0 -e empty \
    172  1.4  christos 		-o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
    173  1.4  christos 		${TEST_SH}
    174  1.4  christos 
    175  1.4  christos 	cat <<- 'EOF' |
    176  1.5  christos 		l=''
    177  1.4  christos 		while read line < File
    178  1.4  christos 		do
    179  1.4  christos 			echo "$line"
    180  1.5  christos 			l="${l}x"
    181  1.5  christos 			[ ${#l} -ge 3 ] && break
    182  1.4  christos 		done
    183  1.4  christos 		echo DONE
    184  1.4  christos 	EOF
    185  1.4  christos 	atf_check -s exit:0 -e empty \
    186  1.4  christos 		-o inline:'First Line\nFirst Line\nFirst Line\nDONE\n' \
    187  1.4  christos 		${TEST_SH}
    188  1.4  christos 
    189  1.4  christos 	cat <<- 'EOF' |
    190  1.4  christos 		while read line
    191  1.4  christos 		do
    192  1.4  christos 			echo "$line"
    193  1.4  christos 		done <File
    194  1.4  christos 		echo DONE
    195  1.4  christos 	EOF
    196  1.4  christos 	atf_check -s exit:0 -e empty \
    197  1.4  christos 		-o inline:'First Line\nSecond Line\nLine 3\nEND\nDONE\n' \
    198  1.4  christos 		${TEST_SH}
    199  1.4  christos 
    200  1.4  christos 	cat <<- 'EOF' |
    201  1.5  christos 		l=''
    202  1.4  christos 		while read line
    203  1.4  christos 		do
    204  1.4  christos 			echo "$line"
    205  1.5  christos 			l="${l}x"
    206  1.5  christos 			[ ${#l} -ge 3 ] && break
    207  1.4  christos 		done <File
    208  1.4  christos 		echo DONE
    209  1.4  christos 	EOF
    210  1.4  christos 	atf_check -s exit:0 -e empty \
    211  1.4  christos 		-o inline:'First Line\nSecond Line\nLine 3\nDONE\n' ${TEST_SH}
    212  1.4  christos 
    213  1.4  christos 	cat <<- 'EOF' |
    214  1.5  christos 		l=''
    215  1.4  christos 		while read line1 <File
    216  1.4  christos 		do
    217  1.4  christos 			read line2
    218  1.4  christos 			echo "$line1":"$line2"
    219  1.5  christos 			l="${l}x"
    220  1.5  christos 			[ ${#l} -ge 2 ] && break
    221  1.4  christos 		done <File
    222  1.4  christos 		echo DONE
    223  1.4  christos 	EOF
    224  1.4  christos 	atf_check -s exit:0 -e empty \
    225  1.4  christos 	    -o inline:'First Line:First Line\nFirst Line:Second Line\nDONE\n' \
    226  1.4  christos 		${TEST_SH}
    227  1.4  christos }
    228  1.4  christos 
    229  1.4  christos atf_test_case do_output_redirections
    230  1.4  christos do_output_redirections_head()
    231  1.4  christos {
    232  1.4  christos 	atf_set "descr" "Test Output redirections"
    233  1.4  christos }
    234  1.4  christos do_output_redirections_body()
    235  1.4  christos {
    236  1.4  christos nl='
    237  1.4  christos '
    238  1.4  christos 	T=0
    239  1.5  christos 	i() { T=$(expr "$T" + 1); }
    240  1.4  christos 
    241  1.4  christos 	rm -f Output 2>/dev/null || :
    242  1.4  christos 	test -f Output && atf_fail "Unable to remove Output file"
    243  1.4  christos #1
    244  1.4  christos 	i; atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '> Output'
    245  1.4  christos 	test -f Output || atf_fail "#$T: Did not make Output file"
    246  1.4  christos #2
    247  1.4  christos 	rm -f Output 2>/dev/null || :
    248  1.4  christos 	i; atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '>> Output'
    249  1.4  christos 	test -f Output || atf_fail "#$T: Did not make Output file"
    250  1.4  christos #3
    251  1.4  christos 	rm -f Output 2>/dev/null || :
    252  1.4  christos 	i; atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '>| Output'
    253  1.4  christos 	test -f Output || atf_fail "#$T: Did not make Output file"
    254  1.4  christos 
    255  1.4  christos #4
    256  1.4  christos 	rm -f Output 2>/dev/null || :
    257  1.4  christos 	i
    258  1.4  christos 	atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c 'echo Hello >Output'
    259  1.4  christos 	test -s Output || atf_fail "#$T: Did not make non-empty Output file"
    260  1.4  christos 	test "$(cat Output)" = "Hello" ||
    261  1.4  christos 	  atf_fail "#$T: Incorrect Output: Should be 'Hello' is '$(cat Output)'"
    262  1.4  christos #5
    263  1.4  christos 	i
    264  1.4  christos 	atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c 'echo Hello>!Output'
    265  1.4  christos 	test -s Output || atf_fail "#$T: Did not make non-empty Output file"
    266  1.4  christos 	test "$(cat Output)" = "Hello" ||
    267  1.4  christos 	  atf_fail "#$T: Incorrect Output: Should be 'Hello' is '$(cat Output)'"
    268  1.4  christos #6
    269  1.4  christos 	i
    270  1.4  christos 	atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c 'echo Bye >>Output'
    271  1.4  christos 	test -s Output || atf_fail "#$T: Removed Output file"
    272  1.4  christos 	test "$(cat Output)" = "Hello${nl}Bye" || atf_fail \
    273  1.4  christos 	  "#$T: Incorrect Output: Should be 'Hello\\nBye' is '$(cat Output)'"
    274  1.4  christos #7
    275  1.4  christos 	i; atf_check -s exit:0 -o inline:'line 1\nline 2\n' -e empty \
    276  1.4  christos 		${TEST_SH} -c \
    277  1.4  christos 		'echo line 1 > Output; echo line 2 >> Output; cat Output'
    278  1.4  christos 	test "$(cat Output)" = "line 1${nl}line 2" || atf_fail \
    279  1.4  christos 	 "#$T: Incorrect Output: Should be 'line 1\\nline 2' is '$(cat Output)'"
    280  1.4  christos #8
    281  1.4  christos 	i; atf_check -s exit:0 -o inline:'line 2\n' -e empty \
    282  1.4  christos 		${TEST_SH} -c 'echo line 1 > Output; echo line 2'
    283  1.4  christos 	test "$(cat Output)" = "line 1" || atf_fail \
    284  1.4  christos 	    "#$T: Incorrect Output: Should be 'line 1' is '$(cat Output)'"
    285  1.4  christos #9
    286  1.4  christos 	i; atf_check -s exit:0 -o empty -e empty \
    287  1.4  christos 		${TEST_SH} -c '(echo line 1; echo line 2 > Out2) > Out1'
    288  1.4  christos 	test "$(cat Out1)" = "line 1" || atf_fail \
    289  1.4  christos 	    "#$T: Incorrect Out1: Should be 'line 1' is '$(cat Out1)'"
    290  1.4  christos 	test "$(cat Out2)" = "line 2" || atf_fail \
    291  1.4  christos 	    "#$T: Incorrect Out2: Should be 'line 2' is '$(cat Out2)'"
    292  1.4  christos #10
    293  1.4  christos 	i; atf_check -s exit:0 -o empty -e empty \
    294  1.4  christos 		${TEST_SH} -c '{ echo line 1; echo line 2 > Out2;} > Out1'
    295  1.4  christos 	test "$(cat Out1)" = "line 1" || atf_fail \
    296  1.4  christos 	    "#$T: Incorrect Out1: Should be 'line 1' is '$(cat Out1)'"
    297  1.4  christos 	test "$(cat Out2)" = "line 2" || atf_fail \
    298  1.4  christos 	    "#$T: Incorrect Out2: Should be 'line 2' is '$(cat Out2)'"
    299  1.4  christos #11
    300  1.4  christos 	i; rm -f Out1 Out2 2>/dev/null || :
    301  1.4  christos 	cat <<- 'EOF' |
    302  1.4  christos 		for arg in 'line 1' 'line 2' 'line 3'
    303  1.4  christos 		do
    304  1.4  christos 			echo "$arg"
    305  1.4  christos 			echo "$arg" > Out1
    306  1.4  christos 		done > Out2
    307  1.4  christos 	EOF
    308  1.4  christos 	atf_check -s exit:0 -o empty -e empty ${TEST_SH} 
    309  1.4  christos 	test "$(cat Out1)" = "line 3" || atf_fail \
    310  1.4  christos 		"#$T:  Incorrect Out1: Should be 'line 3' is '$(cat Out1)'"
    311  1.4  christos 	test "$(cat Out2)" = "line 1${nl}line 2${nl}line 3" || atf_fail \
    312  1.4  christos     "#$T: Incorrect Out2: Should be 'line 1\\nline 2\\nline 3' is '$(cat Out2)'"
    313  1.4  christos #12
    314  1.4  christos 	i; rm -f Out1 Out2 2>/dev/null || :
    315  1.4  christos 	cat <<- 'EOF' |
    316  1.4  christos 		for arg in 'line 1' 'line 2' 'line 3'
    317  1.4  christos 		do
    318  1.4  christos 			echo "$arg"
    319  1.4  christos 			echo "$arg" >> Out1
    320  1.4  christos 		done > Out2
    321  1.4  christos 	EOF
    322  1.4  christos 	atf_check -s exit:0 -o empty -e empty ${TEST_SH} 
    323  1.4  christos 	test "$(cat Out1)" = "line 1${nl}line 2${nl}line 3" || atf_fail \
    324  1.4  christos     "#$T: Incorrect Out1: Should be 'line 1\\nline 2\\nline 3' is '$(cat Out1)'"
    325  1.4  christos 	test "$(cat Out2)" = "line 1${nl}line 2${nl}line 3" || atf_fail \
    326  1.4  christos     "#$T: Incorrect Out2: Should be 'line 1\\nline 2\\nline 3' is '$(cat Out2)'"
    327  1.4  christos }
    328  1.4  christos 
    329  1.4  christos atf_test_case fd_redirections
    330  1.4  christos fd_redirections_head()
    331  1.4  christos {
    332  1.4  christos 	atf_set "descr" "Tests redirections to/from specific descriptors"
    333  1.4  christos }
    334  1.4  christos fd_redirections_body()
    335  1.4  christos {
    336  1.6  christos 	atf_require_prog /bin/echo
    337  1.6  christos 
    338  1.6  christos 	cat <<- 'DONE' > helper.sh
    339  1.6  christos 		f() {
    340  1.6  christos 			/bin/echo nothing "$1" >& "$1"
    341  1.6  christos 		}
    342  1.6  christos 		for n
    343  1.6  christos 		do
    344  1.6  christos 			eval "f $n $n"'> file-$n'
    345  1.6  christos 		done
    346  1.6  christos 	DONE
    347  1.6  christos 	cat <<- 'DONE' > reread.sh
    348  1.6  christos 		f() {
    349  1.6  christos 			(read -r var; echo "${var}") <&"$1"
    350  1.6  christos 		}
    351  1.6  christos 		for n
    352  1.6  christos 		do
    353  1.6  christos 			x=$( eval "f $n $n"'< file-$n' )
    354  1.6  christos 			test "${x}" = "nothing $n" || echo "$n"
    355  1.6  christos 		done
    356  1.6  christos 	DONE
    357  1.6  christos 
    358  1.6  christos 	validate()
    359  1.6  christos 	{
    360  1.6  christos 	    for n
    361  1.6  christos 	    do
    362  1.6  christos 		test -e "file-$n" || atf_fail "file-$n not created"
    363  1.6  christos 		C=$(cat file-"$n")
    364  1.6  christos 		test "$C" = "nothing $n" ||
    365  1.6  christos 			atf_fail "file-$n contains '$C' not 'nothing $n'"
    366  1.6  christos 	    done
    367  1.6  christos 	}
    368  1.6  christos 
    369  1.6  christos 	atf_check -s exit:0 -e empty -o empty \
    370  1.6  christos 		${TEST_SH} helper.sh 1 2 3 4 5 6 7 8 9
    371  1.6  christos 	validate 1 2 3 4 5 6 7 8 9
    372  1.6  christos 	atf_check -s exit:0 -e empty -o empty \
    373  1.6  christos 		${TEST_SH} reread.sh 3 4 5 6 7 8 9
    374  1.6  christos 
    375  1.6  christos 	L=$(ulimit -n)
    376  1.6  christos 	if [ "$L" -ge 30 ]
    377  1.6  christos 	then
    378  1.6  christos 		atf_check -s exit:0 -e empty -o empty \
    379  1.6  christos 			${TEST_SH} helper.sh 10 15 19 20 25 29
    380  1.6  christos 		validate 10 15 19 20 25 29
    381  1.6  christos 		atf_check -s exit:0 -e empty -o empty \
    382  1.6  christos 			${TEST_SH} reread.sh 10 15 19 20 25 29
    383  1.6  christos 	fi
    384  1.6  christos 	if [ "$L" -ge 100 ]
    385  1.6  christos 	then
    386  1.6  christos 		atf_check -s exit:0 -e empty -o empty \
    387  1.6  christos 			${TEST_SH} helper.sh 32 33 49 50 51 63 64 65 77 88 99
    388  1.6  christos 		validate 32 33 49 50 51 63 64 65 77 88 99
    389  1.6  christos 		atf_check -s exit:0 -e empty -o empty \
    390  1.6  christos 			${TEST_SH} reread.sh 32 33 49 50 51 63 64 65 77 88 99
    391  1.6  christos 	fi
    392  1.6  christos 	if [ "$L" -ge 500 ]
    393  1.6  christos 	then
    394  1.6  christos 		atf_check -s exit:0 -e empty -o empty \
    395  1.6  christos 			${TEST_SH} helper.sh 100 101 199 200 222 333 444 499
    396  1.6  christos 		validate 100 101 199 200 222 333 444 499
    397  1.6  christos 		atf_check -s exit:0 -e empty -o empty \
    398  1.6  christos 			${TEST_SH} reread.sh 100 101 199 200 222 333 444 499
    399  1.6  christos 	fi
    400  1.6  christos 	if [ "$L" -gt 1005 ]
    401  1.6  christos 	then
    402  1.6  christos 		atf_check -s exit:0 -e empty -o empty \
    403  1.6  christos 			${TEST_SH} helper.sh 1000 1001 1002 1003 1004 1005
    404  1.6  christos 		validate 1000 1001 1002 1003 1004 1005
    405  1.6  christos 		atf_check -s exit:0 -e empty -o empty \
    406  1.6  christos 			${TEST_SH} reread.sh 1000 1001 1002 1003 1004 1005
    407  1.6  christos 	fi
    408  1.4  christos } 
    409  1.4  christos 
    410  1.4  christos atf_test_case local_redirections
    411  1.4  christos local_redirections_head()
    412  1.4  christos {
    413  1.4  christos 	atf_set "descr" \
    414  1.4  christos 	    "Tests that exec can reassign file descriptors in the shell itself"
    415  1.4  christos }
    416  1.4  christos local_redirections_body()
    417  1.4  christos {
    418  1.6  christos 	cat <<- 'DONE' > helper.sh
    419  1.6  christos 		for f
    420  1.6  christos 		do
    421  1.6  christos 			eval "exec $f"'> file-$f'
    422  1.6  christos 		done
    423  1.6  christos 
    424  1.6  christos 		for f
    425  1.6  christos 		do
    426  1.6  christos 			printf '%s\n' "Hello $f" >&"$f"
    427  1.6  christos 		done
    428  1.6  christos 
    429  1.6  christos 		for f
    430  1.6  christos 		do
    431  1.6  christos 			eval "exec $f"'>&-'
    432  1.6  christos 		done
    433  1.6  christos 
    434  1.6  christos 		for f
    435  1.6  christos 		do
    436  1.6  christos 			eval "exec $f"'< file-$f'
    437  1.6  christos 		done
    438  1.6  christos 
    439  1.6  christos 		for f
    440  1.6  christos 		do
    441  1.6  christos 			exec <& "$f"
    442  1.6  christos 			read -r var || echo >&2 "No data in file-$f"
    443  1.6  christos 			read -r x && echo >&2 "Too much data in file-${f}: $x"
    444  1.6  christos 			test "${var}" = "Hello $f" ||
    445  1.6  christos 			    echo >&2 "file-$f contains '${var}' not 'Hello $f'"
    446  1.6  christos 		done
    447  1.6  christos 	DONE
    448  1.6  christos 
    449  1.6  christos 	atf_check -s exit:0 -o empty -e empty \
    450  1.6  christos 		${TEST_SH} helper.sh 3 4 5 6 7 8 9
    451  1.6  christos 
    452  1.6  christos 	L=$(ulimit -n)
    453  1.6  christos 	if [ "$L" -ge 30 ]
    454  1.6  christos 	then
    455  1.6  christos 		atf_check -s exit:0 -o empty -e empty \
    456  1.6  christos 			${TEST_SH} helper.sh 10 11 13 15 16 19 20 28 29
    457  1.6  christos 	fi
    458  1.6  christos 	if [ "$L" -ge 100 ]
    459  1.6  christos 	then
    460  1.6  christos 		atf_check -s exit:0 -o empty -e empty \
    461  1.6  christos 			${TEST_SH} helper.sh 30 31 32 63 64 65 77 88 99
    462  1.6  christos 	fi
    463  1.6  christos 	if [ "$L" -ge 500 ]
    464  1.6  christos 	then
    465  1.6  christos 		atf_check -s exit:0 -o empty -e empty \
    466  1.6  christos 			${TEST_SH} helper.sh 100 101 111 199 200 201 222 333 499
    467  1.6  christos 	fi
    468  1.6  christos 	if [ "$L" -ge 1005 ]
    469  1.6  christos 	then
    470  1.6  christos 		atf_check -s exit:0 -o empty -e empty \
    471  1.6  christos 			${TEST_SH} helper.sh 1000 1001 1002 1003 1004 1005
    472  1.6  christos 	fi
    473  1.4  christos }
    474  1.4  christos 
    475  1.1  christos atf_test_case redir_in_case
    476  1.4  christos redir_in_case_head()
    477  1.4  christos {
    478  1.4  christos 	atf_set "descr" "Tests that sh(1) allows just redirections " \
    479  1.1  christos 	                "in case statements. (PR bin/48631)"
    480  1.1  christos }
    481  1.4  christos redir_in_case_body()
    482  1.4  christos {
    483  1.2  christos 	atf_check -s exit:0 -o empty -e empty \
    484  1.2  christos 	    ${TEST_SH} -c 'case x in (whatever) >foo;; esac'
    485  1.4  christos 
    486  1.4  christos 	atf_check -s exit:0 -o empty -e empty \
    487  1.4  christos 	    ${TEST_SH} -c 'case x in (whatever) >foo 2>&1;; esac'
    488  1.4  christos 
    489  1.4  christos 	atf_check -s exit:0 -o empty -e empty \
    490  1.4  christos 	    ${TEST_SH} -c 'case x in (whatever) >foo 2>&1 </dev/null;; esac'
    491  1.4  christos 
    492  1.4  christos 	atf_check -s exit:0 -o empty -e empty \
    493  1.4  christos 	    ${TEST_SH} -c 'case x in (whatever) >${somewhere};; esac'
    494  1.2  christos }
    495  1.2  christos 
    496  1.4  christos atf_test_case incorrect_redirections
    497  1.4  christos incorrect_redirections_head()
    498  1.4  christos {
    499  1.4  christos 	atf_set "descr" "Tests that sh(1) correctly ignores non-redirections"
    500  1.4  christos }
    501  1.4  christos incorrect_redirections_body() {
    502  1.4  christos 
    503  1.4  christos 	atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c 'echo foo>'
    504  1.4  christos 	atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c 'read foo<'
    505  1.4  christos 	atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c 'echo foo<>'
    506  1.4  christos 	atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
    507  1.4  christos 		'echo x > '"$nl"
    508  1.4  christos 	atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
    509  1.4  christos 		'read x < '"$nl"
    510  1.4  christos 	atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
    511  1.4  christos 		'echo x <> '"$nl"
    512  1.4  christos 	atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
    513  1.4  christos 		'echo x >< anything'
    514  1.4  christos 	atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
    515  1.4  christos 		'echo x >>< anything'
    516  1.4  christos 	atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
    517  1.4  christos 		'echo x >|< anything'
    518  1.4  christos 	atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
    519  1.4  christos 		'echo x > ; read x < /dev/null || echo bad'
    520  1.4  christos 	atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
    521  1.4  christos 		'read x < & echo y > /dev/null; wait && echo bad'
    522  1.4  christos 
    523  1.4  christos 	rm -f Output 2>/dev/null || :
    524  1.4  christos 	atf_check -s exit:0 -e empty -o inline:'A Line > Output\n' \
    525  1.4  christos 		${TEST_SH} -c 'echo A Line \> Output'
    526  1.4  christos 	test -f Output && atf_file "File 'Output' appeared and should not have"
    527  1.4  christos 
    528  1.4  christos 	rm -f Output 2>/dev/null || :
    529  1.4  christos 	atf_check -s exit:0 -e empty -o empty \
    530  1.4  christos 		${TEST_SH} -c 'echo A Line \>> Output'
    531  1.4  christos 	test -f Output || atf_file "File 'Output' not created when it should"
    532  1.4  christos 	test "$(cat Output)" = 'A Line >' || atf_fail \
    533  1.4  christos 		"Output file contains '$(cat Output)' instead of '"'A Line >'\'
    534  1.4  christos 
    535  1.4  christos 	rm -f Output \> 2>/dev/null || :
    536  1.4  christos 	atf_check -s exit:0 -e empty -o empty \
    537  1.4  christos 		${TEST_SH} -c 'echo A Line >\> Output'
    538  1.4  christos 	test -f Output && atf_file "File 'Output' appeared and should not have"
    539  1.4  christos 	test -f '>' || atf_file "File '>' not created when it should"
    540  1.4  christos 	test "$(cat '>')" = 'A Line Output' || atf_fail \
    541  1.4  christos 	    "Output file ('>') contains '$(cat '>')' instead of 'A Line Output'"
    542  1.4  christos }
    543  1.4  christos 
    544  1.4  christos # Many more tests in t_here, so here we have just rudimentary checks
    545  1.2  christos atf_test_case redir_here_doc
    546  1.4  christos redir_here_doc_head()
    547  1.4  christos {
    548  1.2  christos 	atf_set "descr" "Tests that sh(1) correctly processes 'here' doc " \
    549  1.2  christos 	                "input redirections"
    550  1.2  christos }
    551  1.4  christos redir_here_doc_body()
    552  1.4  christos {
    553  1.4  christos 	# nb: the printf is not executed, it is data
    554  1.3  christos 	cat <<- 'DONE' |
    555  1.3  christos 		cat <<EOF
    556  1.3  christos 			printf '%s\n' 'hello\n'
    557  1.3  christos 		EOF
    558  1.3  christos 	DONE
    559  1.4  christos 	atf_check -s exit:0 -o match:printf -o match:'hello\\n' \
    560  1.4  christos 		-e empty ${TEST_SH} 
    561  1.4  christos }
    562  1.4  christos 
    563  1.4  christos atf_test_case subshell_redirections
    564  1.4  christos subshell_redirections_head()
    565  1.4  christos {
    566  1.4  christos 	atf_set "descr" "Tests redirection interactions between shell and " \
    567  1.4  christos 			"its sub-shell(s)"
    568  1.4  christos }
    569  1.4  christos subshell_redirections_body()
    570  1.4  christos {
    571  1.6  christos 	atf_require_prog cat
    572  1.6  christos 
    573  1.6  christos 	LIM=$(ulimit -n)
    574  1.6  christos 
    575  1.6  christos 	cat <<- 'DONE' |
    576  1.6  christos 		exec 6>output-file
    577  1.6  christos 
    578  1.6  christos 		( printf "hello\n" >&6 )
    579  1.6  christos 
    580  1.6  christos 		exec 8<output-file
    581  1.6  christos 
    582  1.6  christos 		( read hello <&8 ; test hello = "$hello" || echo >&2 Hello )
    583  1.6  christos 
    584  1.6  christos 		( printf "bye-bye\n" >&6 )
    585  1.6  christos 
    586  1.6  christos 		( exec 8<&- )
    587  1.6  christos 		read bye <&8 || echo >&2 "Closed?"
    588  1.6  christos 		echo Bye="$bye"
    589  1.6  christos 	DONE
    590  1.6  christos 	atf_check -s exit:0 -o match:Bye=bye-bye -e empty \
    591  1.6  christos 		${TEST_SH}
    592  1.6  christos 
    593  1.6  christos 	cat <<- 'DONE' |
    594  1.6  christos 		for arg in one-4 two-24 three-14
    595  1.6  christos 		do
    596  1.6  christos 			fd=${arg#*-}
    597  1.6  christos 			file=${arg%-*}
    598  1.6  christos 			eval "exec ${fd}>${file}"
    599  1.6  christos 		done
    600  1.6  christos 
    601  1.6  christos 		for arg in one-5 two-7 three-19
    602  1.6  christos 		do
    603  1.6  christos 			fd=${arg#*-}
    604  1.6  christos 			file=${arg%-*}
    605  1.6  christos 			eval "exec ${fd}<${file}"
    606  1.6  christos 		done
    607  1.6  christos 
    608  1.6  christos 		(
    609  1.6  christos 			echo line-1 >&4
    610  1.6  christos 			echo line-2 >&24
    611  1.6  christos 			echo line-3 >&14
    612  1.6  christos 			echo go
    613  1.6  christos 		) | (
    614  1.6  christos 			read go
    615  1.6  christos 			read x <&5
    616  1.6  christos 			read y <&7
    617  1.6  christos 			read z <&19
    618  1.6  christos 
    619  1.6  christos 			printf "%s\n" "${x}" "${y}" "${z}"
    620  1.6  christos 		)
    621  1.6  christos 	DONE
    622  1.6  christos 	atf_check -s exit:0 -o inline:'line-1\nline-2\nline-3\n' \
    623  1.6  christos 		-e empty ${TEST_SH}
    624  1.6  christos 
    625  1.6  christos 	cat <<- 'DONE' |
    626  1.6  christos 		for arg in one-4-5 two-6-7 three-8-9 four-11-10 five-3-12
    627  1.6  christos 		do
    628  1.6  christos 			ofd=${arg##*-}
    629  1.6  christos 			file=${arg%-*}
    630  1.6  christos 			ifd=${file#*-}
    631  1.6  christos 			file=${file%-*}
    632  1.6  christos 			eval "exec ${ofd}>${file}"
    633  1.6  christos 			eval "exec ${ifd}<${file}"
    634  1.6  christos 		done
    635  1.6  christos 
    636  1.6  christos 		( ( ( echo line-1 >& 13 ) 13>&12 ) 12>&5 ) >stdout 2>errout
    637  1.6  christos 		( ( ( echo line-2 >& 4) 13>&12 ) 4>&7 ) >>stdout 2>>errout
    638  1.6  christos 		( ( ( echo line-3 >& 6) 8>&1 6>&11 >&12) 11>&9 >&7 ) >>stdout
    639  1.6  christos 
    640  1.6  christos 		( ( ( cat <&13 >&12 ) 13<&8 12>&10 ) 10>&1 8<&6 ) 6<&4
    641  1.6  christos 		( ( ( cat <&4 ) <&4 6<&8 8<&11  )
    642  1.6  christos 			<&4 4<&6 6<&8 8<&11 ) <&4 4<&6 6<&8 8<&11 11<&3
    643  1.6  christos 		( ( ( cat <&7 >&1 ) 7<&6 >&10 ) 10>&2 6<&8 ) 2>&1
    644  1.6  christos 	DONE
    645  1.6  christos 	atf_check -s exit:0 -o inline:'line-1\nline-2\nline-3\n' \
    646  1.6  christos 		-e empty ${TEST_SH}
    647  1.6  christos }
    648  1.6  christos 
    649  1.6  christos atf_test_case ulimit_redirection_interaction
    650  1.6  christos ulimit_redirection_interaction_head()
    651  1.6  christos {
    652  1.6  christos 	atf_set "descr" "Tests interactions between redirect and ulimit -n "
    653  1.6  christos }
    654  1.6  christos ulimit_redirection_interaction_body()
    655  1.6  christos {
    656  1.6  christos 	atf_require_prog ls
    657  1.6  christos 
    658  1.6  christos 	cat <<- 'DONE' > helper.sh
    659  1.6  christos 		oLIM=$(ulimit -n)
    660  1.6  christos 		HRD=$(ulimit -H -n)
    661  1.6  christos 		test "${oLIM}" -lt "${HRD}"  && ulimit -n "${HRD}"
    662  1.6  christos 		LIM=$(ulimit -n)
    663  1.6  christos 
    664  1.6  christos 		FDs=
    665  1.6  christos 		LFD=-1
    666  1.6  christos 		while [ ${LIM} -gt 16 ]
    667  1.6  christos 		do
    668  1.6  christos 			FD=$(( ${LIM} - 1 ))
    669  1.6  christos 			if [ "${FD}" -eq "${LFD}" ]; then
    670  1.6  christos 				echo >&2 "Infinite loop... (busted $(( )) ??)"
    671  1.6  christos 				exit 1
    672  1.6  christos 			fi
    673  1.6  christos 			LFD="${FD}"
    674  1.6  christos 
    675  1.6  christos 			eval "exec ${FD}"'> /dev/null'
    676  1.6  christos 			FDs="${FD}${FDs:+ }${FDs}"
    677  1.6  christos 
    678  1.6  christos 			(
    679  1.6  christos 				FD=$(( ${LIM} + 1 ))
    680  1.6  christos 				eval "exec ${FD}"'> /dev/null'
    681  1.6  christos 				echo "Reached unreachable command"
    682  1.6  christos 			) 2>/dev/null && echo >&2 "Opened beyond limit!"
    683  1.6  christos 
    684  1.6  christos 			(eval 'ls 2>&1 3>&1 4>&1 5>&1 '"${FD}"'>&1') >&"${FD}"
    685  1.6  christos 
    686  1.6  christos 			LIM=$(( ${LIM} / 2 ))
    687  1.6  christos 			ulimit -S -n "${LIM}"
    688  1.6  christos 		done
    689  1.6  christos 
    690  1.6  christos 		# Even though ulimit has been reduced, open fds should work
    691  1.6  christos 		for FD in ${FDs}
    692  1.6  christos 		do
    693  1.6  christos 			echo ${FD} in ${FDs} >&"${FD}" || exit 1
    694  1.6  christos 		done
    695  1.6  christos 
    696  1.6  christos 		ulimit -S -n "${oLIM}"
    697  1.6  christos 
    698  1.6  christos 		# maybe more later...
    699  1.6  christos 
    700  1.6  christos 	DONE
    701  1.6  christos 
    702  1.6  christos 	atf_check -s exit:0 -o empty -e empty ${TEST_SH} helper.sh
    703  1.1  christos }
    704  1.1  christos 
    705  1.1  christos atf_init_test_cases() {
    706  1.4  christos 	atf_add_test_case basic_test_method_test
    707  1.4  christos 	atf_add_test_case do_input_redirections
    708  1.4  christos 	atf_add_test_case do_output_redirections
    709  1.4  christos 	atf_add_test_case fd_redirections
    710  1.4  christos 	atf_add_test_case local_redirections
    711  1.4  christos 	atf_add_test_case incorrect_redirections
    712  1.4  christos 	atf_add_test_case redir_here_doc
    713  1.1  christos 	atf_add_test_case redir_in_case
    714  1.4  christos 	atf_add_test_case subshell_redirections
    715  1.6  christos 	atf_add_test_case ulimit_redirection_interaction
    716  1.1  christos }
    717