From b6b8b522a74d1e505dd2172f313a892ee12d3a87 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Thu, 4 Jun 2020 22:49:37 +0200 Subject: [PATCH] regress tests: remove use of unportable direct paths to commands Many tests used direct paths to some commands, mostly /bin/echo and /bin/cat. This is unportable (breaks on e.g. NixOS). The correct way is to obtain the direct path using 'whence -p'. There was also one use of '/usr/bin/pstack' in tests/comvario.sh that seemed bogus. Apparently this was supposed to analyse a core file after a crash. Even on Solaris and Linux, where that command exists, the argument is documented to be a PID, not a core file. If this ever worked anywhere, then it was system-specific enough to be useless here, so I've removed it. (cherry picked from commit 4563b8bc651cd9cb18dc73f56a041f7ac5534395) --- src/cmd/ksh93/tests/basic.sh | 42 ++++++++++++++++--------------- src/cmd/ksh93/tests/builtins.sh | 4 ++- src/cmd/ksh93/tests/comvario.sh | 5 ---- src/cmd/ksh93/tests/functions.sh | 3 ++- src/cmd/ksh93/tests/glob.sh | 2 +- src/cmd/ksh93/tests/heredoc.sh | 7 +++--- src/cmd/ksh93/tests/io.sh | 4 +-- src/cmd/ksh93/tests/options.sh | 3 ++- src/cmd/ksh93/tests/quoting.sh | 4 ++- src/cmd/ksh93/tests/restricted.sh | 5 ++-- src/cmd/ksh93/tests/sigchld.sh | 2 +- src/cmd/ksh93/tests/signal.sh | 13 +++++----- src/cmd/ksh93/tests/subshell.sh | 7 +++--- 13 files changed, 53 insertions(+), 48 deletions(-) diff --git a/src/cmd/ksh93/tests/basic.sh b/src/cmd/ksh93/tests/basic.sh index 7abbc9578..33b8d64f5 100755 --- a/src/cmd/ksh93/tests/basic.sh +++ b/src/cmd/ksh93/tests/basic.sh @@ -36,6 +36,8 @@ tmp=$( exit 1 } trap 'cd / && rm -rf "$tmp"' EXIT +bincat=$(whence -p cat) +binecho=$(whence -p echo) # test basic file operations like redirection, pipes, file expansion set -- \ @@ -155,7 +157,7 @@ then err_exit "$foobar is not foobar" fi { print foo - /bin/echo bar + "$binecho" bar print bam } > $tmp/foobar if [[ $( < $tmp/foobar) != $'foo\nbar\nbam' ]] @@ -163,17 +165,17 @@ then err_exit "output file pointer not shared correctly" fi cat > $tmp/foobar <<\! print foo - /bin/echo bar + "$binecho" bar print bam ! chmod +x $tmp/foobar -if [[ $($tmp/foobar) != $'foo\nbar\nbam' ]] +if [[ $(export binecho; $tmp/foobar) != $'foo\nbar\nbam' ]] then err_exit "script not working" fi -if [[ $($tmp/foobar | /bin/cat) != $'foo\nbar\nbam' ]] +if [[ $(export binecho; $tmp/foobar | "$bincat") != $'foo\nbar\nbam' ]] then err_exit "script | cat not working" fi -if [[ $( $tmp/foobar) != $'foo\nbar\nbam' ]] +if [[ $(export binecho; $tmp/foobar) != $'foo\nbar\nbam' ]] then err_exit "output file pointer not shared correctly" fi rm -f $tmp/foobar @@ -181,13 +183,13 @@ x=$( (print foo) ; (print bar) ) if [[ $x != $'foo\nbar' ]] then err_exit " ( (print foo);(print bar ) failed" fi -x=$( (/bin/echo foo) ; (print bar) ) +x=$( ("$binecho" foo) ; (print bar) ) if [[ $x != $'foo\nbar' ]] -then err_exit " ( (/bin/echo);(print bar ) failed" +then err_exit " ( ("$binecho");(print bar ) failed" fi -x=$( (/bin/echo foo) ; (/bin/echo bar) ) +x=$( ("$binecho" foo) ; ("$binecho" bar) ) if [[ $x != $'foo\nbar' ]] -then err_exit " ( (/bin/echo);(/bin/echo bar ) failed" +then err_exit " ( ("$binecho");("$binecho" bar ) failed" fi cat > $tmp/script <<\! if [[ -p /dev/fd/0 ]] @@ -221,7 +223,7 @@ if [[ $x != "hello there" ]] then err_exit "scripts in subshells fail" fi cd ~- || err_exit "cd back failed" -x=$( (/bin/echo foo) 2> /dev/null ) +x=$( ("$binecho" foo) 2> /dev/null ) if [[ $x != foo ]] then err_exit "subshell in command substitution fails" fi @@ -233,15 +235,15 @@ then err_exit "command substitution with stdout closed failed" fi exec >& 9 cd $pwd -x=$(cat <<\! | $SHELL -/bin/echo | /bin/cat -/bin/echo hello +x=$(export binecho bincat; cat <<\! | $SHELL +"$binecho" | "$bincat" +"$binecho" hello ! ) if [[ $x != $'\n'hello ]] then err_exit "$SHELL not working when standard input is a pipe" fi -x=$( (/bin/echo hello) 2> /dev/null ) +x=$( ("$binecho" hello) 2> /dev/null ) if [[ $x != hello ]] then err_exit "subshell in command substitution with 1 closed fails" fi @@ -265,22 +267,22 @@ fi trap - INT cat > $tmp/script <<- \! read line -/bin/cat +"$bincat" ! -if [[ $($SHELL $tmp/script < /dev/null + print $line | "$bincat" > /dev/null read line } read <<\! @@ -559,6 +560,7 @@ builtin cat out=$tmp/seq.out for ((i=1; i<=11; i++)); do print "$i"; done >$out cmp -s <(print -- "$($bincat<( $bincat $out ) )") <(print -- "$(cat <( cat $out ) )") || err_exit "builtin cat differs from $bincat" +builtin -d cat [[ $($SHELL -c '{ printf %R "["; print ok;}' 2> /dev/null) == ok ]] || err_exit $'\'printf %R "["\' causes shell to abort' diff --git a/src/cmd/ksh93/tests/comvario.sh b/src/cmd/ksh93/tests/comvario.sh index beee2adad..2069dd5aa 100755 --- a/src/cmd/ksh93/tests/comvario.sh +++ b/src/cmd/ksh93/tests/comvario.sh @@ -529,11 +529,6 @@ print -v c2 [[ "${out.stdout}" != '' ]] || err_exit "$0: Expected nonempty stdout." [[ "${out.stderr}" == '' ]] || err_exit "$0: Expected empty stderr, got $(printf '%q\n' "${out.stderr}")" - if [[ -f 'core' && -x '/usr/bin/pstack' ]] ; then - pstack 'core' - rm 'core' - fi - return 0 } diff --git a/src/cmd/ksh93/tests/functions.sh b/src/cmd/ksh93/tests/functions.sh index 06e94e5c0..83df8f10d 100755 --- a/src/cmd/ksh93/tests/functions.sh +++ b/src/cmd/ksh93/tests/functions.sh @@ -41,6 +41,7 @@ tmp=$( exit 1 } trap 'cd / && rm -rf "$tmp"' EXIT +binecho=$(whence -p echo) integer foo=33 bar=bye @@ -141,7 +142,7 @@ fi if [[ $PWD != "$dir" ]] then err_exit 'cd inside nested subshell changes $PWD' fi -fun() /bin/echo hello +fun() "$binecho" hello if [[ $(fun) != hello ]] then err_exit one line functions not working fi diff --git a/src/cmd/ksh93/tests/glob.sh b/src/cmd/ksh93/tests/glob.sh index 66f9c7ac6..c64093dbb 100755 --- a/src/cmd/ksh93/tests/glob.sh +++ b/src/cmd/ksh93/tests/glob.sh @@ -105,7 +105,7 @@ case $* in 'b B') contrary=1 ;; b|B) ignorant=1 ;; esac -set -- $(LC_ALL=C /bin/sh -c 'echo [a-c]') +set -- $(LC_ALL=C sh -c 'echo [a-c]') case $* in B) aware=1 ;; esac diff --git a/src/cmd/ksh93/tests/heredoc.sh b/src/cmd/ksh93/tests/heredoc.sh index db00d6838..9213eb486 100755 --- a/src/cmd/ksh93/tests/heredoc.sh +++ b/src/cmd/ksh93/tests/heredoc.sh @@ -36,6 +36,7 @@ tmp=$( exit 1 } trap 'cd / && rm -rf "$tmp"' EXIT +bincat=$(whence -p cat) f=$tmp/here1 g=$tmp/here2 @@ -125,7 +126,7 @@ done } > $f chmod +x "$f" $SHELL "$f" > /dev/null || err_exit "large here-doc with command substitution fails" -x=$(/bin/cat < "$f" <<- '!!!!' exec 0<&- foobar() { - /bin/cat <<- ! + "$bincat" <<- ! foobar ! } @@ -212,7 +213,7 @@ cat > "$f" <<- '!!!!' EOF print -r -- "$(foobar)" !!!! -if [[ $($SHELL "$f") != foobar ]] +if [[ $(export bincat; "$SHELL" "$f") != foobar ]] then err_exit 'here document with stdin closed failed' fi printf $'cat <<# \\!!!\n\thello\n\t\tworld\n!!!' > $f diff --git a/src/cmd/ksh93/tests/io.sh b/src/cmd/ksh93/tests/io.sh index 64d7ec673..d84491247 100755 --- a/src/cmd/ksh93/tests/io.sh +++ b/src/cmd/ksh93/tests/io.sh @@ -478,9 +478,9 @@ print hello there world > $tmp/foobar $SHELL -c "sed -e 's/there //' $tmp/foobar >; $tmp/foobar" [[ $(<$tmp/foobar) == 'hello world' ]] || err_exit '>; redirection not working with -c on a simple command' -rm -f "$tmp/junk" +binfalse=$(whence -p false) for (( i=1; i < 50; i++ )) -do out=$(/bin/ls "$tmp/junk" 2>/dev/null) +do out=$("$binfalse" 2>/dev/null) if (( $? == 0 )) then err_exit 'wrong error code with redirection' break diff --git a/src/cmd/ksh93/tests/options.sh b/src/cmd/ksh93/tests/options.sh index 6673137f8..b84c14744 100755 --- a/src/cmd/ksh93/tests/options.sh +++ b/src/cmd/ksh93/tests/options.sh @@ -41,6 +41,7 @@ tmp=$( exit 1 } trap 'cd / && rm -rf "$tmp"' EXIT +bincat=$(whence -p cat) if [[ $( ${SHELL-ksh} -s hello<<-\! print $1 @@ -367,7 +368,7 @@ pipeline=( ( nopipefail=1 pipefail=1 command='true|true|false' ) ( nopipefail=1 pipefail=1 command='false|false|false' ) ( nopipefail=0 pipefail=0 command='true|true|true' ) - ( nopipefail=0 pipefail=0 command='print hi|(sleep 1;/bin/cat)>/dev/null' ) + ( nopipefail=0 pipefail=0 command='print hi|(sleep 1;"$bincat")>/dev/null' ) ) set --nopipefail for ((i = 0; i < ${#pipeline[@]}; i++ )) diff --git a/src/cmd/ksh93/tests/quoting.sh b/src/cmd/ksh93/tests/quoting.sh index 80c47b375..60fbf3a0b 100755 --- a/src/cmd/ksh93/tests/quoting.sh +++ b/src/cmd/ksh93/tests/quoting.sh @@ -27,6 +27,8 @@ alias err_exit='err_exit $LINENO' Command=${0##*/} integer Errors=0 +binecho=$(whence -p echo) + if [[ 'hi there' != "hi there" ]] then err_exit "single quotes not the same as double quotes" fi @@ -184,7 +186,7 @@ fi if [[ $x$ != '$hi$' ]] then err_exit ' $x$, with x=$hi, does not expand to $hi$' fi -set -- $(/bin/echo foo;sleep 1;/bin/echo bar) +set -- $("$binecho" foo; sleep 1; "$binecho" bar) if [[ $# != 2 ]] then err_exit 'word splitting after command substitution not working' fi diff --git a/src/cmd/ksh93/tests/restricted.sh b/src/cmd/ksh93/tests/restricted.sh index 4665923ea..5d4571e1e 100755 --- a/src/cmd/ksh93/tests/restricted.sh +++ b/src/cmd/ksh93/tests/restricted.sh @@ -36,6 +36,7 @@ tmp=$( exit 1 } trap 'cd / && rm -rf "$tmp"' EXIT +binecho=$(whence -p echo) # test restricted shell pwd=$PWD @@ -57,7 +58,7 @@ ln -s $SHELL rksh PATH=$PWD:$PATH rksh -c '[[ -o restricted ]]' || err_exit 'restricted option not set' [[ $(rksh -c 'print hello') == hello ]] || err_exit 'unable to run print' -check_restricted /bin/echo || err_exit '/bin/echo not resticted' +check_restricted "$binecho" || err_exit "$binecho not resticted" check_restricted ./echo || err_exit './echo not resticted' check_restricted 'SHELL=ksh' || err_exit 'SHELL assignment not resticted' check_restricted 'PATH=/bin' || err_exit 'PATH assignment not resticted' @@ -70,7 +71,7 @@ print 'echo hello' > script chmod +x ./script ! check_restricted script || err_exit 'script without builtins should run in restricted mode' check_restricted ./script || err_exit 'script with / in name should not run in restricted mode' -print '/bin/echo hello' > script +printf '%q hello\n' "$binecho" > script ! check_restricted script || err_exit 'script with pathnames should run in restricted mode' print 'echo hello> file' > script ! check_restricted script || err_exit 'script with output redirection should run in restricted mode' diff --git a/src/cmd/ksh93/tests/sigchld.sh b/src/cmd/ksh93/tests/sigchld.sh index 0a5dcb050..3b352652c 100755 --- a/src/cmd/ksh93/tests/sigchld.sh +++ b/src/cmd/ksh93/tests/sigchld.sh @@ -155,7 +155,7 @@ done (( d==2000 )) || err_exit "trap '' CHLD causes side effects d=$d" trap - CHLD -x=$($SHELL 2> /dev/null -ic '/bin/notfound; sleep .5 & sleep 1;jobs') +x=$($SHELL 2> /dev/null -ic '/dev/null/notfound; sleep .5 & sleep 1;jobs') [[ $x == *Done* ]] || err_exit 'SIGCHLD blocked after notfound' x=$($SHELL 2> /dev/null -ic 'kill -0 12345678901234567876; sleep .5 & sleep 1;jobs') [[ $x == *Done* ]] || err_exit 'SIGCHLD blocked after error message' diff --git a/src/cmd/ksh93/tests/signal.sh b/src/cmd/ksh93/tests/signal.sh index d5effeca2..87cfd7d92 100755 --- a/src/cmd/ksh93/tests/signal.sh +++ b/src/cmd/ksh93/tests/signal.sh @@ -283,18 +283,18 @@ then float s=$SECONDS (( SECONDS-s < 4 )) && err_exit 'parent completes early' fi -yes=$(whence -p yes) -if [[ $yes ]] -then for exp in TERM VTALRM PIPE +yes() for ((;;)); do print y; done + + for exp in TERM VTALRM PIPE do if [[ ${SIG[$exp]} ]] then { - $SHELL <<- EOF + bindate=$(whence -p date) "$SHELL" <<- EOF foo() { return 0; } trap foo EXIT { sleep 2; kill -$exp \$\$; sleep 3; kill -0 \$\$ && kill -KILL \$\$; } & - $yes | + yes | while read yes - do (/bin/date; sleep .1) + do ("\$bindate"; sleep .1) done > /dev/null EOF } 2>> /dev/null @@ -302,7 +302,6 @@ then for exp in TERM VTALRM PIPE [[ $exp == $got ]] || err_exit "kill -$exp \$\$ failed, required termination by signal '$got'" fi done -fi SECONDS=0 $SHELL 2> /dev/null -c 'sleep 2 && kill $$ & trap "print done; exit 3" EXIT; (sleep 5); print finished' > $tmp/sig diff --git a/src/cmd/ksh93/tests/subshell.sh b/src/cmd/ksh93/tests/subshell.sh index 4a96c5e48..a393b8db5 100755 --- a/src/cmd/ksh93/tests/subshell.sh +++ b/src/cmd/ksh93/tests/subshell.sh @@ -39,6 +39,7 @@ trap 'cd / && rm -rf "$tmp"' EXIT builtin getconf bincat=$(PATH=$(getconf PATH) whence -p cat) +binecho=$(PATH=$(getconf PATH) whence -p echo) z=() z.foo=( [one]=hello [two]=(x=3 y=4) [three]=hi) @@ -466,9 +467,9 @@ exp=ok got=$($SHELL -c "$cmd" 2>&1) [[ $got == "$exp" ]] || err_exit "'$cmd' failed -- expected '$exp', got '$got'" -cmd='eval "for i in 1 2; do eval /bin/echo x; done"' +cmd='eval '\''for i in 1 2; do eval "\"\$binecho\" x"; done'\' exp=$'x\nx' -got=$($SHELL -c "$cmd") +got=$(export binecho; $SHELL -c "$cmd") if [[ $got != "$exp" ]] then EXP=$(printf %q "$exp") GOT=$(printf %q "$got") @@ -482,7 +483,7 @@ $SHELL -c 'sleep 20 & pid=$!; { x=$( ( seq 60000 ) );kill -9 $pid;}&;wait $pid' (.sh.foo=foobar) [[ ${.sh.foo} == foobar ]] && err_exit '.sh subvariables in subshells remain set' -[[ $($SHELL -c 'print 1 | : "$(/bin/cat <(/bin/cat))"') ]] && err_exit 'process substitution not working correctly in subshells' +[[ $($SHELL -c 'print 1 | : "$("$bincat" <("$bincat"))"') ]] && err_exit 'process substitution not working correctly in subshells' # config hang bug integer i