Skip to content

Commit

Permalink
Add more regression tests, mostly from ksh93v- and ksh2020
Browse files Browse the repository at this point in the history
src/cmd/ksh93/tests/arrays.sh,
src/cmd/ksh93/tests/arrays2.sh:
- Backport some regression tests from ksh93v- for associative arrays.

src/cmd/ksh93/tests/basic.sh:
- Add ksh93v- regression tests for background process output in
  backtick and shared-state command substitutions as well as functions
  used in command substitutions.
- Add regression tests for using EXIT traps in subshells. In ksh93v-
  and ksh2020 EXIT traps don't work in forked subshells:
  att#1452
- The trap builtin shouldn't segfault after receiving an invalid
  signal name. ksh2020 regression: att#1403
- Add a test to make sure invalid flags don't crash ksh.
  ksh2020 regression: att#1284
- Test for an illegal seek error when using the 'join' command with
  process substitutions. ksh93v- regression:
  https://www.mail-archive.com/[email protected]/msg00816.html

src/cmd/ksh93/tests/bracket.sh:
- Add some regression tests from ksh93v- for the -eq test operator.

src/cmd/ksh93/tests/builtins.sh:
- Move the regression test for 'exit' in an interactive shell to
  the exit.sh script.
- Test for assignments preceding the command builtin persisting
  after an error. ksh2020 regression: att#1402
- The chmod builtin should modify the permissions of all files passed
  to it. ksh2020 regression: att#949
- Add regression tests for the cd builtin. In ksh93v- 2013-10-10 alpha,
  using cd on a directory without an execute bit doesn't cause an
  error. The test for using cd on a normal file was backported
  from ksh93v-.
- Backport a ksh93v- regression test for the exit status
  from 'kill %'.

src/cmd/ksh93/tests/case.sh:
- Test for a segfault when ksh handles an invalid character
  class in a pattern. ksh2020 regression: att#1409

src/cmd/ksh93/tests/exit.sh:
- Add regression tests from ksh2020 for the 'exit' builtin:
  att@d9491d4

src/cmd/ksh93/tests/io.sh:
- Add a regression test from ksh93v- for a process substitution
  hang. This test fails in the 93v- 2013 alpha but succeeds in
  the 2014 beta.

src/cmd/ksh93/tests/math.sh:
- 'typeset -s foo=30000' adds garbage to $foo in ksh93u+, ksh93v-
  and ksh2020:
  $ typeset -s foo=30000
  $ echo $foo
  5#1430000
  This bug was fixed in commit 88a6baa, but that commit didn't
  add a regression test for it.

src/cmd/ksh93/tests/variables.sh:
- Add a regression test for $PS4 incorrectly unsetting ${.sh.subshell}:
  att#1092
  • Loading branch information
JohnoKing committed Mar 12, 2021
1 parent d4adc8f commit cb8a808
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/cmd/ksh93/tests/arrays.sh
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ typeset -m 'a[0]=a[1]'
typeset -m 'a[1]=j'
[[ ${a[@]} == 'bb aa cc' ]] || err_exit 'moving index array elements not working'
unset a j
[[ $(typeset -p a) ]] && err_exit 'unset associative array after typeset -m not working'
typeset -A a=( [0]="aa" [1]="bb" [2]="cc" )
typeset -m 'j=a[0]'
Expand Down
9 changes: 9 additions & 0 deletions src/cmd/ksh93/tests/arrays2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ print -v cx > /dev/null
print -v cx | read -C l 2> /dev/null || err_exit 'read -C fails from output of print -v'
((SHOPT_FIXEDARRAY)) && [[ ${cx%cx=} != "${l%l=}" ]] && err_exit 'print -v for compound variable with fixed 2d array not working'

unset foo
typeset -A foo
typeset -A foo[bar]
foo[bar][x]=2
(( foo[bar][x]++ ))
exp=3
[[ ${foo[bar][x]} == $exp ]] || err_ext "subscript gets added incorrectly to an associative array when ++ operator is called" \
"(expected '$exp', got '${foo[bar][x]}')"

# ======
# Multidimensional arrays with an unset method shouldn't cause a crash.
# The test itself must be run inside of a function.
Expand Down
51 changes: 50 additions & 1 deletion src/cmd/ksh93/tests/basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,16 @@ unset foo
unset foo
foo=$(false) > /dev/null && err_exit 'failed command substitution with redirection not returning false'
expected=foreback
got=`print -n fore; (sleep 2;print back)&`
[[ $got == $expected ]] || err_exit "\`\` command substitution background process output error (expected '$expected', got '$got')"
got=$(print -n fore; (sleep .2;print back)&)
[[ $got == $expected ]] || err_exit "command substitution background process output error -- got '$got', expected '$expected'"
[[ $got == $expected ]] || err_exit "\$() command substitution background process output error (expected '$expected', got '$got')"
got=${ print -n fore; (sleep 2;print back)& }
[[ $got == $expected ]] || err_exit "\${} shared-state command substitution background process output error (expected '$expected', got '$got')"
function abc { sleep 2; print back; }
function abcd { abc & }
got=$(print -n fore;abcd)
[[ $got == $expected ]] || err_exit "\$() command substitution background with function process output error (expected '$expected', got '$got')"

for false in false $binfalse
do x=$($false) && err_exit "x=\$($false) should fail"
Expand Down Expand Up @@ -790,5 +798,46 @@ trap - DEBUG # bug compat
[[ $got == "$exp" ]] || err_exit "DEBUG trap did not trigger return from POSIX function on status 255" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
# ======
# In ksh93v- and ksh2020 EXIT traps don't work in forked subshells
# https://github.com/att/ast/issues/1452
exp="forked subshell EXIT trap okay"
got="$(ulimit -t unlimited; trap 'echo forked subshell EXIT trap okay' EXIT)"
[[ $got == $exp ]] || err_exit "EXIT trap did not trigger in forked subshell" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
exp="virtual subshell EXIT trap okay"
got="$(trap 'echo virtual subshell EXIT trap okay' EXIT)"
[[ $got == $exp ]] || err_exit "EXIT trap did not trigger in virtual subshell" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
# ======
# Regression test for https://github.com/att/ast/issues/1403
for t in {A..Z}; do
$SHELL -c "trap - $t 2> /dev/null"
[[ $? == 1 ]] || err_exit "'trap' throws segfault when given invalid signal name '$t'"
done
# ======
# Is an invalid flag handled correctly?
# ksh2020 regression: https://github.com/att/ast/issues/1284
actual=$($SHELL --verson 2>&1)
actual_status=$?
expect='ksh: verson: bad option(s)'
expect_status=2
[[ "$actual" == ${expect}* ]] || err_exit "failed to get version string" \
"(expected $(printf %q ${expect}*), got $(printf %q "$actual"))"
[[ $actual_status == $expect_status ]] ||
err_exit "wrong exit status (expected '$expect_status', got '$actual_status')"
# ======
# Test for illegal seek error (ksh93v- regression)
# https://www.mail-archive.com/[email protected]/msg00816.html
exp='1
2'
got="$(join <(printf '%d\n' 1 2) <(printf '%d\n' 1 2))"
[[ $exp == $got ]] || err_exit "pipeline fails with illegal seek error" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
# ======
exit $((Errors<125?Errors:125))
8 changes: 8 additions & 0 deletions src/cmd/ksh93/tests/bracket.sh
Original file line number Diff line number Diff line change
Expand Up @@ -408,5 +408,13 @@ do unset var
[[ -n ${var+s} ]] || err_exit "[[ -n \${var+s} ]] should be true for empty var with attribute -$flag"
done

# ======
# Tests from ksh93v- for the -eq operator
[[ 010 -eq 10 ]] || err_exit '010 is not 10 in [[...]]'

unset foo
foo=10
([[ foo -eq 10 ]]) || err_exit 'foo -eq 10 fails in [[...]] with foo=10'

# ======
exit $((Errors<125?Errors:125))
44 changes: 38 additions & 6 deletions src/cmd/ksh93/tests/builtins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -686,11 +686,6 @@ then (
fi
cd "$tmp"
$SHELL +E -i 2>/dev/null <<- \! && err_exit 'interactive shell should not exit 0 after false'
false
exit
!
if kill -L > /dev/null 2>&1
then [[ $(kill -l HUP) == "$(kill -L HUP)" ]] || err_exit 'kill -l and kill -L are not the same when given a signal name'
[[ $(kill -l 9) == "$(kill -L 9)" ]] || err_exit 'kill -l and kill -L are not the same when given a signal number'
Expand Down Expand Up @@ -974,7 +969,7 @@ fi
EOF
"$SHELL" -i "$sleepsig" 2> /dev/null || err_exit "'sleep -s' doesn't work with intervals of more than 30 seconds"
# ==========
# ======
# Builtins should handle unrecognized options correctly
while IFS= read -r bltin <&3
do case $bltin in
Expand Down Expand Up @@ -1013,5 +1008,42 @@ then got=$( { "$SHELL" -c '
"(got status $e$( ((e>128)) && print -n / && kill -l "$e"), $(printf %q "$got"))"
fi
# ======
# Regression test for https://github.com/att/ast/issues/1402
#
# We throw away stderr because we only want the value of '$t', not the error text from running
# 'command' with an invalid flag.
exp='good'
got=$($SHELL -c 't=good; t=bad command -@; print $t' 2>/dev/null)
[[ $exp == $got ]] || err_exit "temp var assignment with 'command'" \
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"
# ======
# Regression test for https://github.com/att/ast/issues/949
foo_script='#!/bin/sh
exit 0'
echo "$foo_script" > "$tmp/foo1.sh"
echo "$foo_script" > "$tmp/foo2.sh"
builtin chmod
chmod +x "$tmp/foo1.sh" "$tmp/foo2.sh"
$SHELL "$tmp/foo1.sh" || err_exit "builtin 'chmod +x' doesn't work on first script"
$SHELL "$tmp/foo2.sh" || err_exit "builtin 'chmod +x' doesn't work on second script"
# ======
# In ksh93v- 2013-10-10 alpha cd doesn't fail on directories without execute permission.
# Additionally, ksh93v- added a regression test for attempting to use cd on a file.
mkdir "$tmp/noexecute"
chmod -x "$tmp/noexecute"
$SHELL -c "cd $tmp/noexecute" 2> /dev/null && err_exit "'cd' on directories without an execute bit doesn't fail"
touch "$tmp/notadir"
$SHELL -c "cd $tmp/notadir" 2> /dev/null && err_exit "'cd' on a normal file doesn't fail"
# ======
# 'kill %' should fail with exit status 1
{ $SHELL -c 'kill %' ;} 2> /dev/null
got=$?
exp=1
[[ $got == $exp ]] || err_exit "'kill %' has the wrong exit status (expected '$exp'; got '$got')"
# ======
exit $((Errors<125?Errors:125))
6 changes: 6 additions & 0 deletions src/cmd/ksh93/tests/case.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,11 @@ got=$(eval 'case x in (if);; esac' 2>&1) || err_exit "'(' + 'if' as first patter
got=$(eval 'case x in foo);; if);; esac' 2>&1) || err_exit "'if' as nth pattern fails: got $(printf %q "$got")"
got=$(eval 'case x in (foo);; (if);; esac' 2>&1) || err_exit "'(' + 'if' as nth pattern fails: got $(printf %q "$got")"

# ======
# Verify an invalid character class name is handled without a SIGSEGV or similar failure
# https://github.com/att/ast/issues/1409
got="$($SHELL -c 'case x in [x[:bogus:]]) echo x ;; esac')"
[[ -z $got ]] || err_exit "invalid char class name (got $(printf %q "$got"))"

# ======
exit $((Errors<125?Errors:125))
60 changes: 60 additions & 0 deletions src/cmd/ksh93/tests/exit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,64 @@ cd /
cd ~- || err_exit "cd back failed"
$SHELL -c 'builtin -f cmd getconf; getconf --"?-version"; exit 0' >/dev/null 2>&1 || err_exit 'ksh plugin exit failed -- was ksh built with CCFLAGS+=$(CC.EXPORT.DYNAMIC)?'

# ======
# Verify the 'exit' command behaves as expected

got=$($SHELL -c 'exit' 2>&1)
status=$?
exp=0
[[ -z $got ]] || err_exit 'bare exit' \
"(got $(printf %q "$got"))"
[[ $exp == $status ]] || err_exit 'bare exit' \
"(expected '$exp', got '$status')"

got=$($SHELL -c 'exit 0' 2>&1)
status=$?
exp=0
[[ -z $got ]] || err_exit 'exit 0' \
"(got $(printf %q "$got"))"
[[ $exp == $status ]] || err_exit 'exit 0' \
"(expected '$exp', got '$status')"

got=$($SHELL -c 'exit 1' 2>&1)
status=$?
exp=1
[[ -z $got ]] || err_exit 'exit 1' \
"(got $(printf %q "$got"))"
[[ $exp == $status ]] || err_exit 'exit 1' \
"(expected '$exp', got '$status')"

got=$($SHELL -c 'function e37 { return 37; } ; e37' 2>&1)
status=$?
exp=37
[[ -z $got ]] || err_exit 'exit 37' \
"(got $(printf %q "$got"))"
[[ $exp == $status ]] || err_exit 'exit 37' \
"(expected '$exp', got '$status')"

got=$($SHELL -c 'exit -1' 2>&1)
status=$?
exp=255
[[ -z $got ]] || err_exit 'exit -1' \
"(got $(printf %q "$got"))"
[[ $exp == $status ]] || err_exit 'exit -1' \
"(expected '$exp', got '$status')"

got=$($SHELL -c 'exit -2' 2>&1)
status=$?
exp=254
[[ -z $got ]] || err_exit 'exit -2' \
"(got $(printf %q "$got"))"
[[ $exp == $status ]] || err_exit 'exit -2' \
"(expected '$exp', got '$status')"

$SHELL +E -i 2>/dev/null <<- \! && err_exit 'interactive shell should not exit 0 after false'
false
exit
!
status=$?
exp=1
[[ $exp == $status ]] || err_exit 'bare exit after false' \
"(expected '$exp', got '$status')"

exit $((Errors<125?Errors:125))
18 changes: 18 additions & 0 deletions src/cmd/ksh93/tests/io.sh
Original file line number Diff line number Diff line change
Expand Up @@ -715,5 +715,23 @@ got=$(export tmp; "$SHELL" -ec \
(redirect {v}>$tmp/v.out; echo ok2 >&$v) 2>/dev/null
[[ -r $tmp/v.out && $(<$tmp/v.out) == ok2 ]] || err_exit 'redirect {varname}>file not working in a subshell'
# ======
# Regression test for a process substitution hang in ksh93v- 2013-10-10 alpha
{
producer() {
for ((i = 0; i < 20000; i++ )) do
print xxxxx${i}xxxxx
done
}
consumer() {
while read var; do
print ${var}
done < ${1}
}
consumer <(producer) > /dev/null
} & pid=$!
(sleep 5; kill -HUP $pid) 2> /dev/null &
wait $pid 2> /dev/null || err_exit "process substitution hangs"
# ======
exit $((Errors<125?Errors:125))
7 changes: 7 additions & 0 deletions src/cmd/ksh93/tests/math.sh
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,12 @@ unset x
[[ $(typeset -lE 0 x=5.67; typeset -p x) == 'typeset -l -E 0 x=6' ]] || err_exit 'typeset -lE 0 with assignment failed to round.'
[[ $(typeset -lX 0 x=5.67; typeset -p x) == 'typeset -l -X 0 x=0x1p+2' ]] || err_exit 'typeset -lX 0 with assignment failed to round.'

# ======
# typeset -s used without -i shouldn't set foo to garbage
exp=30000
got="$(typeset -s foo=30000; echo $foo)"
[[ $exp == $got ]] || err_exit "unexpected output from typeset -s without -i" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"

# ======
exit $((Errors<125?Errors:125))
8 changes: 8 additions & 0 deletions src/cmd/ksh93/tests/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1258,5 +1258,13 @@ exp='foo/foobar/fool'
got=$(IFS=/; foo=bar foobar=fbar fool=pity; print -r -- "${!foo*}")
[[ $got == "$exp" ]] || err_exit "\${!foo*}: expected $(printf %q "$exp"), got $(printf %q "$got")"

# ======
# In ksh93v- ${.sh.subshell} is unset by the $PS4 prompt
# https://github.com/att/ast/issues/1092
exp='0'
got="$($SHELL -c 'PS4="${.sh.subshell}"; echo ${.sh.subshell}')"
[[ "$exp" == "$got" ]] || err_exit "\${.sh.subshell} is wrongly unset in the \$PS4 prompt" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"

# ======
exit $((Errors<125?Errors:125))

0 comments on commit cb8a808

Please sign in to comment.