1 # $NetBSD: t_redir.sh,v 1.9 2016/05/14 00:33:02 kre Exp $
3 # Copyright (c) 2016 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"}
30 # Any failures in this first test means it is not worth bothering looking
31 # for causes of failures in any other tests, make this one work first.
33 # Problems with this test usually mean inadequate ATF_SHELL used for testing.
34 # (though if all pass but the last, it might be a TEST_SH problem.)
36 atf_test_case basic_test_method_test
37 basic_test_method_test_head()
39 atf_set "descr" "Tests that test method works as expected"
41 basic_test_method_test_body()
45 atf_check -s exit:0 -o empty -e empty ${TEST_SH}
48 atf_check -s exit:0 -o match:0 -e empty ${TEST_SH} -c 'wc -l'
53 atf_check -s exit:0 -o match:hello -e empty ${TEST_SH}
57 atf_check -s exit:0 -o match:1 -e empty ${TEST_SH} -c 'wc -l'
63 atf_check -s exit:0 -o match:helloworld -e empty ${TEST_SH}
68 atf_check -s exit:0 -o match:2 -e empty ${TEST_SH} -c 'wc -l'
70 printf '%s\n%s\n%s\n' Line1 Line2 Line3 > File
71 atf_check -s exit:0 -o inline:'Line1\nLine2\nLine3\n' -e empty \
72 ${TEST_SH} -c 'cat File'
77 echo '' -$1- -$2- -$3- -$4-
85 atf_check -s exit:0 -o match:ARGS=4 -o match:'-X- -- -- -Y-' \
86 -o match:X=X -o match:'Y=\$4' -e empty ${TEST_SH}
89 atf_test_case do_input_redirections
90 do_input_redirections_head()
92 atf_set "descr" "Tests that simple input redirection works"
94 do_input_redirections_body()
96 printf '%s\n%s\n%s\nEND\n' 'First Line' 'Second Line' 'Line 3' >File
98 atf_check -s exit:0 -e empty \
99 -o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
100 ${TEST_SH} -c 'cat < File'
101 atf_check -s exit:0 -e empty \
102 -o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
103 ${TEST_SH} -c 'cat <File'
104 atf_check -s exit:0 -e empty \
105 -o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
106 ${TEST_SH} -c 'cat< File'
107 atf_check -s exit:0 -e empty \
108 -o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
109 ${TEST_SH} -c 'cat < "File"'
110 atf_check -s exit:0 -e empty \
111 -o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
112 ${TEST_SH} -c '< File cat'
115 atf_check -s exit:0 -e empty \
116 -o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
117 ${TEST_SH} -c '< wc cat'
120 atf_check -s exit:0 -e empty -o match:4 \
121 ${TEST_SH} -c '< cat wc'
130 atf_check -s exit:0 -e empty \
131 -o inline:'First Line\nFirst Line\nFirst Line\n' \
140 atf_check -s exit:0 -e empty \
141 -o inline:'First Line\nSecond Line\nLine 3\n' \
150 atf_check -s exit:0 -e empty \
151 -o inline:'First Line\nFirst Line\nFirst Line\n' \
156 while [ "$line" != END ]; do
161 atf_check -s exit:0 -e empty \
162 -o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
171 atf_check -s exit:0 -e empty \
172 -o inline:'First Line\nSecond Line\nLine 3\nEND\n' \
177 while read line < File
181 [ ${#l} -ge 3 ] && break
185 atf_check -s exit:0 -e empty \
186 -o inline:'First Line\nFirst Line\nFirst Line\nDONE\n' \
196 atf_check -s exit:0 -e empty \
197 -o inline:'First Line\nSecond Line\nLine 3\nEND\nDONE\n' \
206 [ ${#l} -ge 3 ] && break
210 atf_check -s exit:0 -e empty \
211 -o inline:'First Line\nSecond Line\nLine 3\nDONE\n' ${TEST_SH}
215 while read line1 <File
218 echo "$line1":"$line2"
220 [ ${#l} -ge 2 ] && break
224 atf_check -s exit:0 -e empty \
225 -o inline:'First Line:First Line\nFirst Line:Second Line\nDONE\n' \
229 atf_test_case do_output_redirections
230 do_output_redirections_head()
232 atf_set "descr" "Test Output redirections"
234 do_output_redirections_body()
239 i() { T=$(expr "$T" + 1); }
241 rm -f Output 2>/dev/null || :
242 test -f Output && atf_fail "Unable to remove Output file"
244 i; atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '> Output'
245 test -f Output || atf_fail "#$T: Did not make Output file"
247 rm -f Output 2>/dev/null || :
248 i; atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '>> Output'
249 test -f Output || atf_fail "#$T: Did not make Output file"
251 rm -f Output 2>/dev/null || :
252 i; atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '>| Output'
253 test -f Output || atf_fail "#$T: Did not make Output file"
256 rm -f Output 2>/dev/null || :
258 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c 'echo Hello >Output'
259 test -s Output || atf_fail "#$T: Did not make non-empty Output file"
260 test "$(cat Output)" = "Hello" ||
261 atf_fail "#$T: Incorrect Output: Should be 'Hello' is '$(cat Output)'"
264 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c 'echo Hello>!Output'
265 test -s Output || atf_fail "#$T: Did not make non-empty Output file"
266 test "$(cat Output)" = "Hello" ||
267 atf_fail "#$T: Incorrect Output: Should be 'Hello' is '$(cat Output)'"
270 atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c 'echo Bye >>Output'
271 test -s Output || atf_fail "#$T: Removed Output file"
272 test "$(cat Output)" = "Hello${nl}Bye" || atf_fail \
273 "#$T: Incorrect Output: Should be 'Hello\\nBye' is '$(cat Output)'"
275 i; atf_check -s exit:0 -o inline:'line 1\nline 2\n' -e empty \
277 'echo line 1 > Output; echo line 2 >> Output; cat Output'
278 test "$(cat Output)" = "line 1${nl}line 2" || atf_fail \
279 "#$T: Incorrect Output: Should be 'line 1\\nline 2' is '$(cat Output)'"
281 i; atf_check -s exit:0 -o inline:'line 2\n' -e empty \
282 ${TEST_SH} -c 'echo line 1 > Output; echo line 2'
283 test "$(cat Output)" = "line 1" || atf_fail \
284 "#$T: Incorrect Output: Should be 'line 1' is '$(cat Output)'"
286 i; atf_check -s exit:0 -o empty -e empty \
287 ${TEST_SH} -c '(echo line 1; echo line 2 > Out2) > Out1'
288 test "$(cat Out1)" = "line 1" || atf_fail \
289 "#$T: Incorrect Out1: Should be 'line 1' is '$(cat Out1)'"
290 test "$(cat Out2)" = "line 2" || atf_fail \
291 "#$T: Incorrect Out2: Should be 'line 2' is '$(cat Out2)'"
293 i; atf_check -s exit:0 -o empty -e empty \
294 ${TEST_SH} -c '{ echo line 1; echo line 2 > Out2;} > Out1'
295 test "$(cat Out1)" = "line 1" || atf_fail \
296 "#$T: Incorrect Out1: Should be 'line 1' is '$(cat Out1)'"
297 test "$(cat Out2)" = "line 2" || atf_fail \
298 "#$T: Incorrect Out2: Should be 'line 2' is '$(cat Out2)'"
300 i; rm -f Out1 Out2 2>/dev/null || :
302 for arg in 'line 1' 'line 2' 'line 3'
308 atf_check -s exit:0 -o empty -e empty ${TEST_SH}
309 test "$(cat Out1)" = "line 3" || atf_fail \
310 "#$T: Incorrect Out1: Should be 'line 3' is '$(cat Out1)'"
311 test "$(cat Out2)" = "line 1${nl}line 2${nl}line 3" || atf_fail \
312 "#$T: Incorrect Out2: Should be 'line 1\\nline 2\\nline 3' is '$(cat Out2)'"
314 i; rm -f Out1 Out2 2>/dev/null || :
316 for arg in 'line 1' 'line 2' 'line 3'
322 atf_check -s exit:0 -o empty -e empty ${TEST_SH}
323 test "$(cat Out1)" = "line 1${nl}line 2${nl}line 3" || atf_fail \
324 "#$T: Incorrect Out1: Should be 'line 1\\nline 2\\nline 3' is '$(cat Out1)'"
325 test "$(cat Out2)" = "line 1${nl}line 2${nl}line 3" || atf_fail \
326 "#$T: Incorrect Out2: Should be 'line 1\\nline 2\\nline 3' is '$(cat Out2)'"
329 atf_test_case fd_redirections
330 fd_redirections_head()
332 atf_set "descr" "Tests redirections to/from specific descriptors"
334 fd_redirections_body()
336 atf_require_prog /bin/echo
338 cat <<- 'DONE' > helper.sh
340 /bin/echo nothing "$1" >& "$1"
344 eval "f $n $n"'> file-$n'
347 cat <<- 'DONE' > reread.sh
349 (read -r var; echo "${var}") <&"$1"
353 x=$( eval "f $n $n"'< file-$n' )
354 test "${x}" = "nothing $n" || echo "$n"
362 test -e "file-$n" || atf_fail "file-$n not created"
364 test "$C" = "nothing $n" ||
365 atf_fail "file-$n contains '$C' not 'nothing $n'"
369 atf_check -s exit:0 -e empty -o empty \
370 ${TEST_SH} helper.sh 1 2 3 4 5 6 7 8 9
371 validate 1 2 3 4 5 6 7 8 9
372 atf_check -s exit:0 -e empty -o empty \
373 ${TEST_SH} reread.sh 3 4 5 6 7 8 9
378 atf_check -s exit:0 -e empty -o empty \
379 ${TEST_SH} helper.sh 10 15 19 20 25 29
380 validate 10 15 19 20 25 29
381 atf_check -s exit:0 -e empty -o empty \
382 ${TEST_SH} reread.sh 10 15 19 20 25 29
386 atf_check -s exit:0 -e empty -o empty \
387 ${TEST_SH} helper.sh 32 33 49 50 51 63 64 65 77 88 99
388 validate 32 33 49 50 51 63 64 65 77 88 99
389 atf_check -s exit:0 -e empty -o empty \
390 ${TEST_SH} reread.sh 32 33 49 50 51 63 64 65 77 88 99
394 atf_check -s exit:0 -e empty -o empty \
395 ${TEST_SH} helper.sh 100 101 199 200 222 333 444 499
396 validate 100 101 199 200 222 333 444 499
397 atf_check -s exit:0 -e empty -o empty \
398 ${TEST_SH} reread.sh 100 101 199 200 222 333 444 499
402 atf_check -s exit:0 -e empty -o empty \
403 ${TEST_SH} helper.sh 1000 1001 1002 1003 1004 1005
404 validate 1000 1001 1002 1003 1004 1005
405 atf_check -s exit:0 -e empty -o empty \
406 ${TEST_SH} reread.sh 1000 1001 1002 1003 1004 1005
410 atf_test_case local_redirections
411 local_redirections_head()
414 "Tests that exec can reassign file descriptors in the shell itself"
416 local_redirections_body()
418 cat <<- 'DONE' > helper.sh
421 eval "exec $f"'> file-$f'
426 printf '%s\n' "Hello $f" >&"$f"
436 eval "exec $f"'< file-$f'
442 read -r var || echo >&2 "No data in file-$f"
443 read -r x && echo >&2 "Too much data in file-${f}: $x"
444 test "${var}" = "Hello $f" ||
445 echo >&2 "file-$f contains '${var}' not 'Hello $f'"
449 atf_check -s exit:0 -o empty -e empty \
450 ${TEST_SH} helper.sh 3 4 5 6 7 8 9
455 atf_check -s exit:0 -o empty -e empty \
456 ${TEST_SH} helper.sh 10 11 13 15 16 19 20 28 29
460 atf_check -s exit:0 -o empty -e empty \
461 ${TEST_SH} helper.sh 30 31 32 63 64 65 77 88 99
465 atf_check -s exit:0 -o empty -e empty \
466 ${TEST_SH} helper.sh 100 101 111 199 200 201 222 333 499
470 atf_check -s exit:0 -o empty -e empty \
471 ${TEST_SH} helper.sh 1000 1001 1002 1003 1004 1005
475 atf_test_case named_fd_redirections
476 named_fd_redirections_head()
478 atf_set "descr" "Tests redirections to /dev/stdout (etc)"
481 named_fd_redirections_body()
483 if test -c /dev/stdout
485 atf_check -s exit:0 -o inline:'OK\n' -e empty \
486 ${TEST_SH} -c 'echo OK >/dev/stdout'
487 atf_check -s exit:0 -o inline:'OK\n' -e empty \
488 ${TEST_SH} -c '/bin/echo OK >/dev/stdout'
491 if test -c /dev/stdin
495 echo GOOD | atf_check -s exit:0 -o inline:'GOOD\n' -e empty \
496 ${TEST_SH} -c 'read var </dev/stdin; echo $var'
497 echo GOOD | atf_check -s exit:0 -o inline:'GOOD\n' -e empty \
498 ${TEST_SH} -c 'cat </dev/stdin'
501 if test -c /dev/stderr
503 atf_check -s exit:0 -e inline:'OK\n' -o empty \
504 ${TEST_SH} -c 'echo OK 2>/dev/stderr >&2'
505 atf_check -s exit:0 -e inline:'OK\n' -o empty \
506 ${TEST_SH} -c '/bin/echo OK 2>/dev/stderr >&2'
509 if test -c /dev/fd/8 && test -c /dev/fd/9
511 atf_check -s exit:0 -o inline:'EIGHT\n' -e empty \
512 ${TEST_SH} -c 'printf "%s\n" EIGHT 8>&1 >/dev/fd/8 |
519 atf_test_case redir_in_case
522 atf_set "descr" "Tests that sh(1) allows just redirections " \
523 "in case statements. (PR bin/48631)"
527 atf_check -s exit:0 -o empty -e empty \
528 ${TEST_SH} -c 'case x in (whatever) >foo;; esac'
530 atf_check -s exit:0 -o empty -e empty \
531 ${TEST_SH} -c 'case x in (whatever) >foo 2>&1;; esac'
533 atf_check -s exit:0 -o empty -e empty \
534 ${TEST_SH} -c 'case x in (whatever) >foo 2>&1 </dev/null;; esac'
536 atf_check -s exit:0 -o empty -e empty \
537 ${TEST_SH} -c 'case x in (whatever) >${somewhere};; esac'
540 atf_test_case incorrect_redirections
541 incorrect_redirections_head()
543 atf_set "descr" "Tests that sh(1) correctly ignores non-redirections"
545 incorrect_redirections_body() {
547 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c 'echo foo>'
548 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c 'read foo<'
549 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c 'echo foo<>'
550 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
552 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
554 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
556 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
558 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
559 'echo x >>< anything'
560 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
561 'echo x >|< anything'
562 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
563 'echo x > ; read x < /dev/null || echo bad'
564 atf_check -s not-exit:0 -o empty -e not-empty ${TEST_SH} -c \
565 'read x < & echo y > /dev/null; wait && echo bad'
567 rm -f Output 2>/dev/null || :
568 atf_check -s exit:0 -e empty -o inline:'A Line > Output\n' \
569 ${TEST_SH} -c 'echo A Line \> Output'
570 test -f Output && atf_file "File 'Output' appeared and should not have"
572 rm -f Output 2>/dev/null || :
573 atf_check -s exit:0 -e empty -o empty \
574 ${TEST_SH} -c 'echo A Line \>> Output'
575 test -f Output || atf_file "File 'Output' not created when it should"
576 test "$(cat Output)" = 'A Line >' || atf_fail \
577 "Output file contains '$(cat Output)' instead of '"'A Line >'\'
579 rm -f Output \> 2>/dev/null || :
580 atf_check -s exit:0 -e empty -o empty \
581 ${TEST_SH} -c 'echo A Line >\> Output'
582 test -f Output && atf_file "File 'Output' appeared and should not have"
583 test -f '>' || atf_file "File '>' not created when it should"
584 test "$(cat '>')" = 'A Line Output' || atf_fail \
585 "Output file ('>') contains '$(cat '>')' instead of 'A Line Output'"
588 # Many more tests in t_here, so here we have just rudimentary checks
589 atf_test_case redir_here_doc
590 redir_here_doc_head()
592 atf_set "descr" "Tests that sh(1) correctly processes 'here' doc " \
595 redir_here_doc_body()
597 # nb: the printf is not executed, it is data
600 printf '%s\n' 'hello\n'
603 atf_check -s exit:0 -o match:printf -o match:'hello\\n' \
607 atf_test_case subshell_redirections
608 subshell_redirections_head()
610 atf_set "descr" "Tests redirection interactions between shell and " \
613 subshell_redirections_body()
622 ( printf "hello\n" >&6 )
626 ( read hello <&8 ; test hello = "$hello" || echo >&2 Hello )
628 ( printf "bye-bye\n" >&6 )
631 read bye <&8 || echo >&2 "Closed?"
634 atf_check -s exit:0 -o match:Bye=bye-bye -e empty \
638 for arg in one-4 two-24 three-14
642 eval "exec ${fd}>${file}"
645 for arg in one-5 two-7 three-19
649 eval "exec ${fd}<${file}"
663 printf "%s\n" "${x}" "${y}" "${z}"
666 atf_check -s exit:0 -o inline:'line-1\nline-2\nline-3\n' \
670 for arg in one-4-5 two-6-7 three-8-9 four-11-10 five-3-12
676 eval "exec ${ofd}>${file}"
677 eval "exec ${ifd}<${file}"
680 ( ( ( echo line-1 >& 13 ) 13>&12 ) 12>&5 ) >stdout 2>errout
681 ( ( ( echo line-2 >& 4) 13>&12 ) 4>&7 ) >>stdout 2>>errout
682 ( ( ( echo line-3 >& 6) 8>&1 6>&11 >&12) 11>&9 >&7 ) >>stdout
684 ( ( ( cat <&13 >&12 ) 13<&8 12>&10 ) 10>&1 8<&6 ) 6<&4
685 ( ( ( cat <&4 ) <&4 6<&8 8<&11 )
686 <&4 4<&6 6<&8 8<&11 ) <&4 4<&6 6<&8 8<&11 11<&3
687 ( ( ( cat <&7 >&1 ) 7<&6 >&10 ) 10>&2 6<&8 ) 2>&1
689 atf_check -s exit:0 -o inline:'line-1\nline-2\nline-3\n' \
693 atf_test_case ulimit_redirection_interaction
694 ulimit_redirection_interaction_head()
696 atf_set "descr" "Tests interactions between redirect and ulimit -n "
698 ulimit_redirection_interaction_body()
702 cat <<- 'DONE' > helper.sh
705 test "${oLIM}" -lt "${HRD}" && ulimit -n "${HRD}"
710 while [ ${LIM} -gt 16 ]
713 if [ "${FD}" -eq "${LFD}" ]; then
714 echo >&2 "Infinite loop... (busted $(( )) ??)"
719 eval "exec ${FD}"'> /dev/null'
720 FDs="${FD}${FDs:+ }${FDs}"
724 eval "exec ${FD}"'> /dev/null'
725 echo "Reached unreachable command"
726 ) 2>/dev/null && echo >&2 "Opened beyond limit!"
728 (eval 'ls 2>&1 3>&1 4>&1 5>&1 '"${FD}"'>&1') >&"${FD}"
730 LIM=$(( ${LIM} / 2 ))
731 ulimit -S -n "${LIM}"
734 # Even though ulimit has been reduced, open fds should work
737 echo ${FD} in ${FDs} >&"${FD}" || exit 1
740 ulimit -S -n "${oLIM}"
742 # maybe more later...
746 atf_check -s exit:0 -o empty -e empty ${TEST_SH} helper.sh
749 atf_test_case validate_fn_redirects
750 validate_fn_redirects_head()
752 # These test cases inspired by PR bin/48875 and the sh
753 # changes that were required to fix it.
755 atf_set "descr" "Tests various redirections applied to functions " \
758 validate_fn_redirects_body()
760 cat <<- 'DONE' > f-def
762 printf '%s\n' In-Func
766 atf_check -s exit:0 -o inline:'In-Func\nsuccess1\n' -e empty \
767 ${TEST_SH} -c ". ./f-def; f ; printf '%s\n' success1"
768 atf_check -s exit:0 -o inline:'success2\n' -e empty \
769 ${TEST_SH} -c ". ./f-def; f >/dev/null; printf '%s\n' success2"
770 atf_check -s exit:0 -o inline:'success3\n' -e empty \
771 ${TEST_SH} -c ". ./f-def; f >&- ; printf '%s\n' success3"
772 atf_check -s exit:0 -o inline:'In-Func\nsuccess4\n' -e empty \
773 ${TEST_SH} -c ". ./f-def; f & wait; printf '%s\n' success4"
774 atf_check -s exit:0 -o inline:'success5\n' -e empty \
775 ${TEST_SH} -c ". ./f-def; f >&- & wait; printf '%s\n' success5"
776 atf_check -s exit:0 -o inline:'In-Func\nIn-Func\nsuccess6\n' -e empty \
777 ${TEST_SH} -c ". ./f-def; f;f; printf '%s\n' success6"
778 atf_check -s exit:0 -o inline:'In-Func\nIn-Func\nsuccess7\n' -e empty \
779 ${TEST_SH} -c ". ./f-def; { f;f;}; printf '%s\n' success7"
780 atf_check -s exit:0 -o inline:'In-Func\nIn-Func\nsuccess8\n' -e empty \
781 ${TEST_SH} -c ". ./f-def; { f;f;}& wait; printf '%s\n' success8"
782 atf_check -s exit:0 -o inline:'In-Func\nsuccess9\n' -e empty \
784 ". ./f-def; { f>/dev/null;f;}& wait; printf '%s\n' success9"
785 atf_check -s exit:0 -o inline:'In-Func\nsuccess10\n' -e empty \
787 ". ./f-def; { f;f>/dev/null;}& wait; printf '%s\n' success10"
789 # This one tests the issue etcupdate had with the original 48875 fix
790 atf_check -s exit:0 -o inline:'Func a\nFunc b\nFunc c\n' -e empty \
796 ( echo x-a; echo y-b; echo z-c ) |
803 # And this tests a similar condition with that same fix
804 cat <<- 'DONE' >Script
806 printf '%s' " hello $1"
809 echo $( for i in a b c
810 do printf '%s' @$i; f $i >&3; done >foo
812 printf '%s\n' foo=$(cat foo)
814 atf_check -s exit:0 -e empty \
815 -o inline:' hello a hello b hello c\nfoo=@a@b@c\n' \
818 # Tests with sh reading stdin, which is not quite the same internal
820 echo ". ./f-def || echo >&2 FAIL
823 "| atf_check -s exit:0 -o inline:'In-Func\nstdin1\n' -e empty ${TEST_SH}
826 . ./f-def || echo >&2 FAIL
829 ' | atf_check -s exit:0 -o inline:'stdin2\n' -e empty ${TEST_SH}
831 cat <<- 'DONE' > fgh.def
847 atf_check -s exit:0 -o inline:'fFgGh' -e empty \
848 ${TEST_SH} -c '. ./fgh.def || echo >&2 FAIL
852 atf_check -s exit:0 -o inline:'fghGF' -e empty \
853 ${TEST_SH} -c '. ./fgh.def || echo >&2 FAIL
855 f & sleep 1; g & sleep 1; h; wait'
857 atf_check -s exit:0 -o inline:'fFgGhX Y\n' -e empty \
858 ${TEST_SH} -c '. ./fgh.def || echo >&2 FAIL
860 echo X $( f ; g ; h ) Y'
862 # This one is the real test for PR bin/48875. If the
863 # cmdsub does not complete before f g (and h) exit,
864 # then the 'F' & 'G' will precede 'X Y' in the output.
865 # If the cmdsub finishes while f & g are still running,
866 # then the X Y will appear before the F and G.
867 # The trailing "sleep 3" is just so we catch all the
868 # output (otherwise atf_check will be finished while
869 # f & g are still sleeping).
871 atf_check -s exit:0 -o inline:'fghX Y\nGF' -e empty \
872 ${TEST_SH} -c '. ./fgh.def || echo >&2 FAIL
874 echo X $( f >&- & sleep 1; g >&- & sleep 1 ; h ) Y
876 exec 4>&1 || echo FD_FAIL
879 # Do the test again to verify it also all works reading stdin
880 # (which is a slightly different path through the shell)
882 . ./fgh.def || echo >&2 FAIL
884 echo X $( f >&- & sleep 1; g >&- & sleep 1 ; h ) Y
886 exec 4>&1 || echo FD_FAIL
887 ' | atf_check -s exit:0 -o inline:'fghX Y\nGF' -e empty ${TEST_SH}
890 atf_init_test_cases() {
891 atf_add_test_case basic_test_method_test
892 atf_add_test_case do_input_redirections
893 atf_add_test_case do_output_redirections
894 atf_add_test_case fd_redirections
895 atf_add_test_case local_redirections
896 atf_add_test_case incorrect_redirections
897 atf_add_test_case named_fd_redirections
898 atf_add_test_case redir_here_doc
899 atf_add_test_case redir_in_case
900 atf_add_test_case subshell_redirections
901 atf_add_test_case ulimit_redirection_interaction
902 atf_add_test_case validate_fn_redirects