diff options
Diffstat (limited to 'corebinutils/sh/tests')
571 files changed, 8086 insertions, 0 deletions
diff --git a/corebinutils/sh/tests/builtins/Makefile b/corebinutils/sh/tests/builtins/Makefile new file mode 100644 index 0000000000..0246009cce --- /dev/null +++ b/corebinutils/sh/tests/builtins/Makefile @@ -0,0 +1,194 @@ +PACKAGE= tests + +.include <src.opts.mk> + +TESTSDIR= ${TESTSBASE}/bin/sh/${.CURDIR:T} + +.PATH: ${.CURDIR:H} +ATF_TESTS_SH= functional_test + +${PACKAGE}FILES+= alias.0 alias.0.stdout +${PACKAGE}FILES+= alias.1 alias.1.stderr +${PACKAGE}FILES+= alias3.0 alias3.0.stdout +${PACKAGE}FILES+= alias4.0 +${PACKAGE}FILES+= break1.0 +${PACKAGE}FILES+= break2.0 break2.0.stdout +${PACKAGE}FILES+= break3.0 +${PACKAGE}FILES+= break4.4 +${PACKAGE}FILES+= break5.4 +${PACKAGE}FILES+= break6.0 +${PACKAGE}FILES+= builtin1.0 +${PACKAGE}FILES+= case1.0 +${PACKAGE}FILES+= case2.0 +${PACKAGE}FILES+= case3.0 +${PACKAGE}FILES+= case4.0 +${PACKAGE}FILES+= case5.0 +${PACKAGE}FILES+= case6.0 +${PACKAGE}FILES+= case7.0 +${PACKAGE}FILES+= case8.0 +${PACKAGE}FILES+= case9.0 +${PACKAGE}FILES+= case10.0 +${PACKAGE}FILES+= case11.0 +${PACKAGE}FILES+= case12.0 +${PACKAGE}FILES+= case13.0 +${PACKAGE}FILES+= case14.0 +${PACKAGE}FILES+= case15.0 +${PACKAGE}FILES+= case16.0 +${PACKAGE}FILES+= case17.0 +${PACKAGE}FILES+= case18.0 +${PACKAGE}FILES+= case19.0 +${PACKAGE}FILES+= case20.0 +${PACKAGE}FILES+= case21.0 +${PACKAGE}FILES+= case22.0 +${PACKAGE}FILES+= case23.0 +${PACKAGE}FILES+= cd1.0 +${PACKAGE}FILES+= cd2.0 +${PACKAGE}FILES+= cd3.0 +${PACKAGE}FILES+= cd4.0 +${PACKAGE}FILES+= cd5.0 +${PACKAGE}FILES+= cd6.0 +${PACKAGE}FILES+= cd7.0 +${PACKAGE}FILES+= cd8.0 +${PACKAGE}FILES+= cd9.0 cd9.0.stdout +${PACKAGE}FILES+= cd10.0 +${PACKAGE}FILES+= cd11.0 +${PACKAGE}FILES+= cd12.0 cd12.0.stderr +${PACKAGE}FILES+= command1.0 +${PACKAGE}FILES+= command2.0 +${PACKAGE}FILES+= command3.0 +${PACKAGE}FILES+= command3.0.stdout +${PACKAGE}FILES+= command4.0 +${PACKAGE}FILES+= command5.0 +${PACKAGE}FILES+= command5.0.stdout +${PACKAGE}FILES+= command6.0 +${PACKAGE}FILES+= command6.0.stdout +${PACKAGE}FILES+= command7.0 +${PACKAGE}FILES+= command8.0 +${PACKAGE}FILES+= command9.0 +${PACKAGE}FILES+= command10.0 +${PACKAGE}FILES+= command11.0 +${PACKAGE}FILES+= command12.0 +${PACKAGE}FILES+= command13.0 +${PACKAGE}FILES+= command14.0 +${PACKAGE}FILES+= dot1.0 +${PACKAGE}FILES+= dot2.0 +${PACKAGE}FILES+= dot3.0 +${PACKAGE}FILES+= dot4.0 +${PACKAGE}FILES+= echo1.0 +${PACKAGE}FILES+= echo2.0 +${PACKAGE}FILES+= echo3.0 +${PACKAGE}FILES+= eval1.0 +${PACKAGE}FILES+= eval2.0 +${PACKAGE}FILES+= eval3.0 +${PACKAGE}FILES+= eval4.0 +${PACKAGE}FILES+= eval5.0 +${PACKAGE}FILES+= eval6.0 +${PACKAGE}FILES+= eval7.0 +${PACKAGE}FILES+= eval8.7 +${PACKAGE}FILES+= exec1.0 +${PACKAGE}FILES+= exec2.0 +${PACKAGE}FILES+= exit1.0 +${PACKAGE}FILES+= exit2.8 +${PACKAGE}FILES+= exit3.0 +${PACKAGE}FILES+= export1.0 +${PACKAGE}FILES+= fc1.0 +${PACKAGE}FILES+= fc2.0 +${PACKAGE}FILES+= fc3.0 fc3.0.stdout fc3.0.stderr +${PACKAGE}FILES+= for1.0 +${PACKAGE}FILES+= for2.0 +${PACKAGE}FILES+= for3.0 +${PACKAGE}FILES+= getopts1.0 getopts1.0.stdout +${PACKAGE}FILES+= getopts2.0 getopts2.0.stdout +${PACKAGE}FILES+= getopts3.0 +${PACKAGE}FILES+= getopts4.0 +${PACKAGE}FILES+= getopts5.0 +${PACKAGE}FILES+= getopts6.0 +${PACKAGE}FILES+= getopts7.0 +${PACKAGE}FILES+= getopts8.0 getopts8.0.stdout +${PACKAGE}FILES+= getopts9.0 getopts9.0.stdout +${PACKAGE}FILES+= getopts10.0 +${PACKAGE}FILES+= hash1.0 hash1.0.stdout +${PACKAGE}FILES+= hash2.0 hash2.0.stdout +${PACKAGE}FILES+= hash3.0 hash3.0.stdout +${PACKAGE}FILES+= hash4.0 +${PACKAGE}FILES+= jobid1.0 +${PACKAGE}FILES+= jobid2.0 +${PACKAGE}FILES+= kill1.0 kill2.0 +${PACKAGE}FILES+= lineno.0 lineno.0.stdout +${PACKAGE}FILES+= lineno2.0 +${PACKAGE}FILES+= lineno3.0 lineno3.0.stdout +${PACKAGE}FILES+= local1.0 +${PACKAGE}FILES+= local2.0 +${PACKAGE}FILES+= local3.0 +${PACKAGE}FILES+= local4.0 +${PACKAGE}FILES+= local5.0 +${PACKAGE}FILES+= local6.0 +${PACKAGE}FILES+= local7.0 +.if ${MK_NLS} != "no" +${PACKAGE}FILES+= locale1.0 +.endif +${PACKAGE}FILES+= locale2.0 +${PACKAGE}FILES+= printf1.0 +${PACKAGE}FILES+= printf2.0 +${PACKAGE}FILES+= printf3.0 +${PACKAGE}FILES+= printf4.0 +${PACKAGE}FILES+= read1.0 read1.0.stdout +${PACKAGE}FILES+= read2.0 +${PACKAGE}FILES+= read3.0 read3.0.stdout +${PACKAGE}FILES+= read4.0 read4.0.stdout +${PACKAGE}FILES+= read5.0 +${PACKAGE}FILES+= read6.0 +${PACKAGE}FILES+= read7.0 +${PACKAGE}FILES+= read8.0 +${PACKAGE}FILES+= read9.0 +${PACKAGE}FILES+= read10.0 +${PACKAGE}FILES+= read11.0 +${PACKAGE}FILES+= read12.0 +${PACKAGE}FILES+= return1.0 +${PACKAGE}FILES+= return2.1 +${PACKAGE}FILES+= return3.1 +${PACKAGE}FILES+= return4.0 +${PACKAGE}FILES+= return5.0 +${PACKAGE}FILES+= return6.4 +${PACKAGE}FILES+= return7.4 +${PACKAGE}FILES+= return8.0 +${PACKAGE}FILES+= set1.0 +${PACKAGE}FILES+= set2.0 +${PACKAGE}FILES+= set3.0 +${PACKAGE}FILES+= trap1.0 +${PACKAGE}FILES+= trap10.0 +${PACKAGE}FILES+= trap11.0 +${PACKAGE}FILES+= trap12.0 +${PACKAGE}FILES+= trap13.0 +${PACKAGE}FILES+= trap14.0 +${PACKAGE}FILES+= trap15.0 +${PACKAGE}FILES+= trap16.0 +${PACKAGE}FILES+= trap17.0 +${PACKAGE}FILES+= trap2.0 +${PACKAGE}FILES+= trap3.0 +${PACKAGE}FILES+= trap4.0 +${PACKAGE}FILES+= trap5.0 +${PACKAGE}FILES+= trap6.0 +${PACKAGE}FILES+= trap7.0 +${PACKAGE}FILES+= trap8.0 +${PACKAGE}FILES+= trap9.0 +${PACKAGE}FILES+= type1.0 type1.0.stderr +${PACKAGE}FILES+= type2.0 +${PACKAGE}FILES+= type3.0 +${PACKAGE}FILES+= type4.0 +${PACKAGE}FILES+= unalias.0 +${PACKAGE}FILES+= var-assign.0 +${PACKAGE}FILES+= var-assign2.0 +${PACKAGE}FILES+= wait1.0 +${PACKAGE}FILES+= wait2.0 +${PACKAGE}FILES+= wait3.0 +${PACKAGE}FILES+= wait4.0 +${PACKAGE}FILES+= wait5.0 +${PACKAGE}FILES+= wait6.0 +${PACKAGE}FILES+= wait7.0 +${PACKAGE}FILES+= wait8.0 +${PACKAGE}FILES+= wait9.127 +${PACKAGE}FILES+= wait10.0 +${PACKAGE}FILES+= wait11.0 + +.include <bsd.test.mk> diff --git a/corebinutils/sh/tests/builtins/Makefile.depend b/corebinutils/sh/tests/builtins/Makefile.depend new file mode 100644 index 0000000000..11aba52f82 --- /dev/null +++ b/corebinutils/sh/tests/builtins/Makefile.depend @@ -0,0 +1,10 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/corebinutils/sh/tests/builtins/alias.0 b/corebinutils/sh/tests/builtins/alias.0 new file mode 100644 index 0000000000..0da2587e3b --- /dev/null +++ b/corebinutils/sh/tests/builtins/alias.0 @@ -0,0 +1,8 @@ +set -e + +unalias -a +alias foo=bar +alias bar= +alias quux="1 2 3" +alias +alias foo diff --git a/corebinutils/sh/tests/builtins/alias.0.stdout b/corebinutils/sh/tests/builtins/alias.0.stdout new file mode 100644 index 0000000000..52efaf0b72 --- /dev/null +++ b/corebinutils/sh/tests/builtins/alias.0.stdout @@ -0,0 +1,4 @@ +bar='' +foo=bar +quux='1 2 3' +foo=bar diff --git a/corebinutils/sh/tests/builtins/alias.1 b/corebinutils/sh/tests/builtins/alias.1 new file mode 100644 index 0000000000..755d1f59ae --- /dev/null +++ b/corebinutils/sh/tests/builtins/alias.1 @@ -0,0 +1,2 @@ +unalias -a +alias foo diff --git a/corebinutils/sh/tests/builtins/alias.1.stderr b/corebinutils/sh/tests/builtins/alias.1.stderr new file mode 100644 index 0000000000..c9f4011bea --- /dev/null +++ b/corebinutils/sh/tests/builtins/alias.1.stderr @@ -0,0 +1 @@ +alias: foo: not found diff --git a/corebinutils/sh/tests/builtins/alias3.0 b/corebinutils/sh/tests/builtins/alias3.0 new file mode 100644 index 0000000000..b0edcbc1b8 --- /dev/null +++ b/corebinutils/sh/tests/builtins/alias3.0 @@ -0,0 +1,11 @@ +set -e + +unalias -a +alias foo=bar +alias bar= +alias quux="1 2 3" +alias foo=bar +alias bar= +alias quux="1 2 3" +alias +alias foo diff --git a/corebinutils/sh/tests/builtins/alias3.0.stdout b/corebinutils/sh/tests/builtins/alias3.0.stdout new file mode 100644 index 0000000000..52efaf0b72 --- /dev/null +++ b/corebinutils/sh/tests/builtins/alias3.0.stdout @@ -0,0 +1,4 @@ +bar='' +foo=bar +quux='1 2 3' +foo=bar diff --git a/corebinutils/sh/tests/builtins/alias4.0 b/corebinutils/sh/tests/builtins/alias4.0 new file mode 100644 index 0000000000..0e93513546 --- /dev/null +++ b/corebinutils/sh/tests/builtins/alias4.0 @@ -0,0 +1,3 @@ + +unalias -a +alias -- diff --git a/corebinutils/sh/tests/builtins/break1.0 b/corebinutils/sh/tests/builtins/break1.0 new file mode 100644 index 0000000000..910a003c60 --- /dev/null +++ b/corebinutils/sh/tests/builtins/break1.0 @@ -0,0 +1,15 @@ + +if [ "$1" != nested ]; then + while :; do + set -- nested + . "$0" + echo bad2 + exit 2 + done + exit 0 +fi +# To trigger the bug, the following commands must be at the top level, +# with newlines in between. +break +echo bad1 +exit 1 diff --git a/corebinutils/sh/tests/builtins/break2.0 b/corebinutils/sh/tests/builtins/break2.0 new file mode 100644 index 0000000000..43d037c10e --- /dev/null +++ b/corebinutils/sh/tests/builtins/break2.0 @@ -0,0 +1,11 @@ + +# It is not immediately obvious that this should work, and someone probably +# relies on it. + +while :; do + trap 'break' USR1 + kill -USR1 $$ + echo bad + exit 1 +done +echo good diff --git a/corebinutils/sh/tests/builtins/break2.0.stdout b/corebinutils/sh/tests/builtins/break2.0.stdout new file mode 100644 index 0000000000..12799ccbe7 --- /dev/null +++ b/corebinutils/sh/tests/builtins/break2.0.stdout @@ -0,0 +1 @@ +good diff --git a/corebinutils/sh/tests/builtins/break3.0 b/corebinutils/sh/tests/builtins/break3.0 new file mode 100644 index 0000000000..fe73dac6be --- /dev/null +++ b/corebinutils/sh/tests/builtins/break3.0 @@ -0,0 +1,14 @@ + +# We accept this and people might rely on it. +# However, various other shells do not accept it. + +f() { + break + echo bad1 +} + +while :; do + f + echo bad2 + exit 2 +done diff --git a/corebinutils/sh/tests/builtins/break4.4 b/corebinutils/sh/tests/builtins/break4.4 new file mode 100644 index 0000000000..24f804cff3 --- /dev/null +++ b/corebinutils/sh/tests/builtins/break4.4 @@ -0,0 +1,6 @@ + +# Although this is not specified by POSIX, some configure scripts (gawk 4.1.0) +# appear to depend on it. + +break +exit 4 diff --git a/corebinutils/sh/tests/builtins/break5.4 b/corebinutils/sh/tests/builtins/break5.4 new file mode 100644 index 0000000000..161c376823 --- /dev/null +++ b/corebinutils/sh/tests/builtins/break5.4 @@ -0,0 +1,11 @@ + +# Although this is not specified by POSIX, some configure scripts (gawk 4.1.0) +# appear to depend on it. +# In some uncommitted code, the subshell environment corrupted the outer +# shell environment's state. + +(for i in a b c; do + exit 3 +done) +break +exit 4 diff --git a/corebinutils/sh/tests/builtins/break6.0 b/corebinutils/sh/tests/builtins/break6.0 new file mode 100644 index 0000000000..6d8a0f2b3f --- /dev/null +++ b/corebinutils/sh/tests/builtins/break6.0 @@ -0,0 +1,7 @@ +# Per POSIX, this need only work if LONG_MAX > 4294967295. + +while :; do + break 4294967296 + echo bad + exit 3 +done diff --git a/corebinutils/sh/tests/builtins/builtin1.0 b/corebinutils/sh/tests/builtins/builtin1.0 new file mode 100644 index 0000000000..e52abf27c7 --- /dev/null +++ b/corebinutils/sh/tests/builtins/builtin1.0 @@ -0,0 +1,30 @@ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +builtin : || echo "Bad return code at $LINENO" +builtin true || echo "Bad return code at $LINENO" +builtin ls 2>/dev/null && echo "Bad return code at $LINENO" +check '"$(builtin pwd)" = "$(pwd)"' +check '-z "$(builtin :)"' +check '-z "$(builtin true)"' +check '-z "$( (builtin nosuchtool) 2>/dev/null)"' +check '-z "$(builtin nosuchtool 2>/dev/null)"' +check '-z "$(builtin nosuchtool 2>/dev/null; :)"' +check '-z "$( (builtin ls) 2>/dev/null)"' +check '-z "$(builtin ls 2>/dev/null)"' +check '-z "$(builtin ls 2>/dev/null; :)"' +check '-n "$( (builtin nosuchtool) 2>&1)"' +check '-n "$(builtin nosuchtool 2>&1)"' +check '-n "$(builtin nosuchtool 2>&1; :)"' +check '-n "$( (builtin ls) 2>&1)"' +check '-n "$(builtin ls 2>&1)"' +check '-n "$(builtin ls 2>&1; :)"' + +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/builtins/case1.0 b/corebinutils/sh/tests/builtins/case1.0 new file mode 100644 index 0000000000..3a89fd2c37 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case1.0 @@ -0,0 +1,12 @@ +f() +{ + false + case $1 in + foo) true ;; + bar) false ;; + esac +} + +f foo || exit 1 +f bar && exit 1 +f quux || exit 1 diff --git a/corebinutils/sh/tests/builtins/case10.0 b/corebinutils/sh/tests/builtins/case10.0 new file mode 100644 index 0000000000..a0f2a98829 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case10.0 @@ -0,0 +1,15 @@ + +case ! in +[\!!]) ;; +*) echo Failed at $LINENO ;; +esac + +case ! in +['!'!]) ;; +*) echo Failed at $LINENO ;; +esac + +case ! in +["!"!]) ;; +*) echo Failed at $LINENO ;; +esac diff --git a/corebinutils/sh/tests/builtins/case11.0 b/corebinutils/sh/tests/builtins/case11.0 new file mode 100644 index 0000000000..50acd5db8e --- /dev/null +++ b/corebinutils/sh/tests/builtins/case11.0 @@ -0,0 +1,5 @@ + +false +case x in +*) +esac diff --git a/corebinutils/sh/tests/builtins/case12.0 b/corebinutils/sh/tests/builtins/case12.0 new file mode 100644 index 0000000000..9a4975b819 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case12.0 @@ -0,0 +1,5 @@ + +false +case x in +y) +esac diff --git a/corebinutils/sh/tests/builtins/case13.0 b/corebinutils/sh/tests/builtins/case13.0 new file mode 100644 index 0000000000..83a26869da --- /dev/null +++ b/corebinutils/sh/tests/builtins/case13.0 @@ -0,0 +1,11 @@ + +case ^ in +[\^^]) ;; +*) echo Failed at $LINENO ;; +esac + +case s in +[\^^]) echo Failed at $LINENO ;; +[s\]]) ;; +*) echo Failed at $LINENO ;; +esac diff --git a/corebinutils/sh/tests/builtins/case14.0 b/corebinutils/sh/tests/builtins/case14.0 new file mode 100644 index 0000000000..b3cf26c922 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case14.0 @@ -0,0 +1,4 @@ + +case `false` in +no) exit 3 ;; +esac diff --git a/corebinutils/sh/tests/builtins/case15.0 b/corebinutils/sh/tests/builtins/case15.0 new file mode 100644 index 0000000000..e0b2d70e58 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case15.0 @@ -0,0 +1,4 @@ + +case x in +`false`) exit 3 ;; +esac diff --git a/corebinutils/sh/tests/builtins/case16.0 b/corebinutils/sh/tests/builtins/case16.0 new file mode 100644 index 0000000000..e1ec8f1c92 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case16.0 @@ -0,0 +1,6 @@ + +f() { return 42; } +f +case x in +x) [ $? = 42 ] ;; +esac diff --git a/corebinutils/sh/tests/builtins/case17.0 b/corebinutils/sh/tests/builtins/case17.0 new file mode 100644 index 0000000000..42ff0ee4a9 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case17.0 @@ -0,0 +1,2 @@ + +! case x in x) false ;& y) esac diff --git a/corebinutils/sh/tests/builtins/case18.0 b/corebinutils/sh/tests/builtins/case18.0 new file mode 100644 index 0000000000..76f4d67435 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case18.0 @@ -0,0 +1,6 @@ + +case x$(false) in +x) ;& +y) [ $? != 0 ] ;; +z) false ;; +esac diff --git a/corebinutils/sh/tests/builtins/case19.0 b/corebinutils/sh/tests/builtins/case19.0 new file mode 100644 index 0000000000..2e8a2ec77f --- /dev/null +++ b/corebinutils/sh/tests/builtins/case19.0 @@ -0,0 +1,6 @@ + +[ "`case x in +x) false ;& +y) ;& +z) echo $? ;; +esac`" != 0 ] diff --git a/corebinutils/sh/tests/builtins/case2.0 b/corebinutils/sh/tests/builtins/case2.0 new file mode 100644 index 0000000000..37d5f8c285 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case2.0 @@ -0,0 +1,105 @@ +# Generated by ./test-fnmatch -s 1, do not edit. +failures= +failed() { printf '%s\n' "Failed: $1 '$2' '$3'"; failures=x$failures; } +testmatch() { eval "case \$2 in ''$1) ;; *) failed testmatch \"\$@\";; esac"; } +testnomatch() { eval "case \$2 in ''$1) failed testnomatch \"\$@\";; esac"; } +testmatch '' '' +testmatch 'a' 'a' +testnomatch 'a' 'b' +testnomatch 'a' 'A' +testmatch '*' 'a' +testmatch '*' 'aa' +testmatch '*a' 'a' +testnomatch '*a' 'b' +testnomatch '*a*' 'b' +testmatch '*a*b*' 'ab' +testmatch '*a*b*' 'qaqbq' +testmatch '*a*bb*' 'qaqbqbbq' +testmatch '*a*bc*' 'qaqbqbcq' +testmatch '*a*bb*' 'qaqbqbb' +testmatch '*a*bc*' 'qaqbqbc' +testmatch '*a*bb' 'qaqbqbb' +testmatch '*a*bc' 'qaqbqbc' +testnomatch '*a*bb' 'qaqbqbbq' +testnomatch '*a*bc' 'qaqbqbcq' +testnomatch '*a*a*a*a*a*a*a*a*a*a*' 'aaaaaaaaa' +testmatch '*a*a*a*a*a*a*a*a*a*a*' 'aaaaaaaaaa' +testmatch '*a*a*a*a*a*a*a*a*a*a*' 'aaaaaaaaaaa' +testnomatch '.*.*.*.*.*.*.*.*.*.*' '.........' +testmatch '.*.*.*.*.*.*.*.*.*.*' '..........' +testmatch '.*.*.*.*.*.*.*.*.*.*' '...........' +testnomatch '*?*?*?*?*?*?*?*?*?*?*' '123456789' +testnomatch '??????????*' '123456789' +testnomatch '*??????????' '123456789' +testmatch '*?*?*?*?*?*?*?*?*?*?*' '1234567890' +testmatch '??????????*' '1234567890' +testmatch '*??????????' '1234567890' +testmatch '*?*?*?*?*?*?*?*?*?*?*' '12345678901' +testmatch '??????????*' '12345678901' +testmatch '*??????????' '12345678901' +testmatch '[x]' 'x' +testmatch '[*]' '*' +testmatch '[?]' '?' +testmatch '[' '[' +testmatch '[[]' '[' +testnomatch '[[]' 'x' +testnomatch '[*]' '' +testnomatch '[*]' 'x' +testnomatch '[?]' 'x' +testmatch '*[*]*' 'foo*foo' +testnomatch '*[*]*' 'foo' +testmatch '[0-9]' '0' +testmatch '[0-9]' '5' +testmatch '[0-9]' '9' +testnomatch '[0-9]' '/' +testnomatch '[0-9]' ':' +testnomatch '[0-9]' '*' +testnomatch '[!0-9]' '0' +testnomatch '[!0-9]' '5' +testnomatch '[!0-9]' '9' +testmatch '[!0-9]' '/' +testmatch '[!0-9]' ':' +testmatch '[!0-9]' '*' +testmatch '*[0-9]' 'a0' +testmatch '*[0-9]' 'a5' +testmatch '*[0-9]' 'a9' +testnomatch '*[0-9]' 'a/' +testnomatch '*[0-9]' 'a:' +testnomatch '*[0-9]' 'a*' +testnomatch '*[!0-9]' 'a0' +testnomatch '*[!0-9]' 'a5' +testnomatch '*[!0-9]' 'a9' +testmatch '*[!0-9]' 'a/' +testmatch '*[!0-9]' 'a:' +testmatch '*[!0-9]' 'a*' +testmatch '*[0-9]' 'a00' +testmatch '*[0-9]' 'a55' +testmatch '*[0-9]' 'a99' +testmatch '*[0-9]' 'a0a0' +testmatch '*[0-9]' 'a5a5' +testmatch '*[0-9]' 'a9a9' +testmatch '\*' '*' +testmatch '\?' '?' +testmatch '\[x]' '[x]' +testmatch '\[' '[' +testmatch '\\' '\' +testmatch '*\**' 'foo*foo' +testnomatch '*\**' 'foo' +testmatch '*\\*' 'foo\foo' +testnomatch '*\\*' 'foo' +testmatch '\(' '(' +testmatch '\a' 'a' +testnomatch '\*' 'a' +testnomatch '\?' 'a' +testnomatch '\*' '\*' +testnomatch '\?' '\?' +testnomatch '\[x]' '\[x]' +testnomatch '\[x]' '\x' +testnomatch '\[' '\[' +testnomatch '\(' '\(' +testnomatch '\a' '\a' +testmatch '.*' '.' +testmatch '.*' '..' +testmatch '.*' '.a' +testmatch 'a*' 'a.' +[ -z "$failures" ] diff --git a/corebinutils/sh/tests/builtins/case20.0 b/corebinutils/sh/tests/builtins/case20.0 new file mode 100644 index 0000000000..800afa0db9 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case20.0 @@ -0,0 +1,8 @@ + +# Shells do not agree about what this pattern should match, but it is +# certain that it must not crash and the missing close bracket must not +# be simply ignored. + +case B in +[[:alpha:]) echo bad ;; +esac diff --git a/corebinutils/sh/tests/builtins/case21.0 b/corebinutils/sh/tests/builtins/case21.0 new file mode 100644 index 0000000000..426ce8b4b0 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case21.0 @@ -0,0 +1,9 @@ + +case 5 in +[0$((-9))]) ;; +*) echo bad1 ;; +esac + +case - in +[0$((-9))]) echo bad2 ;; +esac diff --git a/corebinutils/sh/tests/builtins/case22.0 b/corebinutils/sh/tests/builtins/case22.0 new file mode 100644 index 0000000000..5c36c483da --- /dev/null +++ b/corebinutils/sh/tests/builtins/case22.0 @@ -0,0 +1,9 @@ + +case 5 in +[0"$((-9))"]) echo bad1 ;; +esac + +case - in +[0"$((-9))"]) ;; +*) echo bad2 ;; +esac diff --git a/corebinutils/sh/tests/builtins/case23.0 b/corebinutils/sh/tests/builtins/case23.0 new file mode 100644 index 0000000000..27fc79a65a --- /dev/null +++ b/corebinutils/sh/tests/builtins/case23.0 @@ -0,0 +1,4 @@ + +case [ in +[[:alpha:]]) echo bad +esac diff --git a/corebinutils/sh/tests/builtins/case3.0 b/corebinutils/sh/tests/builtins/case3.0 new file mode 100644 index 0000000000..06080f9ec3 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case3.0 @@ -0,0 +1,94 @@ +# Generated by ./test-fnmatch -s 2, do not edit. +failures= +failed() { printf '%s\n' "Failed: $1 '$2' '$3'"; failures=x$failures; } +# We do not treat a backslash specially in this case, +# but this is not the case in all shells. +netestmatch() { case $2 in $1) ;; *) failed netestmatch "$@";; esac; } +netestnomatch() { case $2 in $1) failed netestnomatch "$@";; esac; } +netestmatch '' '' +netestmatch 'a' 'a' +netestnomatch 'a' 'b' +netestnomatch 'a' 'A' +netestmatch '*' 'a' +netestmatch '*' 'aa' +netestmatch '*a' 'a' +netestnomatch '*a' 'b' +netestnomatch '*a*' 'b' +netestmatch '*a*b*' 'ab' +netestmatch '*a*b*' 'qaqbq' +netestmatch '*a*bb*' 'qaqbqbbq' +netestmatch '*a*bc*' 'qaqbqbcq' +netestmatch '*a*bb*' 'qaqbqbb' +netestmatch '*a*bc*' 'qaqbqbc' +netestmatch '*a*bb' 'qaqbqbb' +netestmatch '*a*bc' 'qaqbqbc' +netestnomatch '*a*bb' 'qaqbqbbq' +netestnomatch '*a*bc' 'qaqbqbcq' +netestnomatch '*a*a*a*a*a*a*a*a*a*a*' 'aaaaaaaaa' +netestmatch '*a*a*a*a*a*a*a*a*a*a*' 'aaaaaaaaaa' +netestmatch '*a*a*a*a*a*a*a*a*a*a*' 'aaaaaaaaaaa' +netestnomatch '.*.*.*.*.*.*.*.*.*.*' '.........' +netestmatch '.*.*.*.*.*.*.*.*.*.*' '..........' +netestmatch '.*.*.*.*.*.*.*.*.*.*' '...........' +netestnomatch '*?*?*?*?*?*?*?*?*?*?*' '123456789' +netestnomatch '??????????*' '123456789' +netestnomatch '*??????????' '123456789' +netestmatch '*?*?*?*?*?*?*?*?*?*?*' '1234567890' +netestmatch '??????????*' '1234567890' +netestmatch '*??????????' '1234567890' +netestmatch '*?*?*?*?*?*?*?*?*?*?*' '12345678901' +netestmatch '??????????*' '12345678901' +netestmatch '*??????????' '12345678901' +netestmatch '[x]' 'x' +netestmatch '[*]' '*' +netestmatch '[?]' '?' +netestmatch '[' '[' +netestmatch '[[]' '[' +netestnomatch '[[]' 'x' +netestnomatch '[*]' '' +netestnomatch '[*]' 'x' +netestnomatch '[?]' 'x' +netestmatch '*[*]*' 'foo*foo' +netestnomatch '*[*]*' 'foo' +netestmatch '[0-9]' '0' +netestmatch '[0-9]' '5' +netestmatch '[0-9]' '9' +netestnomatch '[0-9]' '/' +netestnomatch '[0-9]' ':' +netestnomatch '[0-9]' '*' +netestnomatch '[!0-9]' '0' +netestnomatch '[!0-9]' '5' +netestnomatch '[!0-9]' '9' +netestmatch '[!0-9]' '/' +netestmatch '[!0-9]' ':' +netestmatch '[!0-9]' '*' +netestmatch '*[0-9]' 'a0' +netestmatch '*[0-9]' 'a5' +netestmatch '*[0-9]' 'a9' +netestnomatch '*[0-9]' 'a/' +netestnomatch '*[0-9]' 'a:' +netestnomatch '*[0-9]' 'a*' +netestnomatch '*[!0-9]' 'a0' +netestnomatch '*[!0-9]' 'a5' +netestnomatch '*[!0-9]' 'a9' +netestmatch '*[!0-9]' 'a/' +netestmatch '*[!0-9]' 'a:' +netestmatch '*[!0-9]' 'a*' +netestmatch '*[0-9]' 'a00' +netestmatch '*[0-9]' 'a55' +netestmatch '*[0-9]' 'a99' +netestmatch '*[0-9]' 'a0a0' +netestmatch '*[0-9]' 'a5a5' +netestmatch '*[0-9]' 'a9a9' +netestmatch '\*' '\*' +netestmatch '\?' '\?' +netestmatch '\' '\' +netestnomatch '\\' '\' +netestmatch '\\' '\\' +netestmatch '*\*' 'foo\foo' +netestnomatch '*\*' 'foo' +netestmatch '.*' '.' +netestmatch '.*' '..' +netestmatch '.*' '.a' +netestmatch 'a*' 'a.' +[ -z "$failures" ] diff --git a/corebinutils/sh/tests/builtins/case4.0 b/corebinutils/sh/tests/builtins/case4.0 new file mode 100644 index 0000000000..abac705a84 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case4.0 @@ -0,0 +1,5 @@ + +set -- "*" +case x in +"$1") echo failed ;; +esac diff --git a/corebinutils/sh/tests/builtins/case5.0 b/corebinutils/sh/tests/builtins/case5.0 new file mode 100644 index 0000000000..118e001b8a --- /dev/null +++ b/corebinutils/sh/tests/builtins/case5.0 @@ -0,0 +1,56 @@ + +unset LC_ALL +LC_CTYPE=en_US.UTF-8 +export LC_CTYPE + +c1=e +# a umlaut +c2=$(printf '\303\244') +# euro sign +c3=$(printf '\342\202\254') +# some sort of 't' outside BMP +c4=$(printf '\360\235\225\245') + +ok=0 +case $c1$c2$c3$c4 in +*) ok=1 ;; +esac +if [ $ok = 0 ]; then + echo wrong at $LINENO + exit 3 +fi + +case $c1$c2$c3$c4 in +$c1$c2$c3$c4) ;; +*) echo wrong at $LINENO ;; +esac + +case $c1$c2$c3$c4 in +"$c1$c2$c3$c4") ;; +*) echo wrong at $LINENO ;; +esac + +case $c1$c2$c3$c4 in +????) ;; +*) echo wrong at $LINENO ;; +esac + +case $c1.$c2.$c3.$c4 in +?.?.?.?) ;; +*) echo wrong at $LINENO ;; +esac + +case $c1$c2$c3$c4 in +[!a][!b][!c][!d]) ;; +*) echo wrong at $LINENO ;; +esac + +case $c1$c2$c3$c4 in +[$c1][$c2][$c3][$c4]) ;; +*) echo wrong at $LINENO ;; +esac + +case $c1$c2$c3$c4 in +["$c1"]["$c2"]["$c3"]["$c4"]) ;; +*) echo wrong at $LINENO ;; +esac diff --git a/corebinutils/sh/tests/builtins/case6.0 b/corebinutils/sh/tests/builtins/case6.0 new file mode 100644 index 0000000000..8cf8847b9b --- /dev/null +++ b/corebinutils/sh/tests/builtins/case6.0 @@ -0,0 +1,53 @@ + +unset LC_CTYPE LC_COLLATE LANG LANGUAGE +LC_ALL=C +export LC_ALL + +c1=e +# o umlaut +c2=$(printf '\366') +# non-break space +c3=$(printf '\240') +c4=$(printf '\240') +# In the C locale these are matched bytewise. This avoids depending on +# optional host locale catalogs while still exercising non-ASCII case +# pattern handling. + +ok=0 +case $c1$c2$c3$c4 in +*) ok=1 ;; +esac +if [ $ok = 0 ]; then + echo wrong at $LINENO + exit 3 +fi + +case $c1$c2$c3$c4 in +$c1$c2$c3$c4) ;; +*) echo wrong at $LINENO ;; +esac + +case $c1$c2$c3$c4 in +"$c1$c2$c3$c4") ;; +*) echo wrong at $LINENO ;; +esac + +case $c1$c2$c3$c4 in +????) ;; +*) echo wrong at $LINENO ;; +esac + +case $c1$c2$c3$c4 in +[!$c2][!b][!c][!d]) ;; +*) echo wrong at $LINENO ;; +esac + +case $c1$c2$c3$c4 in +[$c1][$c2][$c3][$c4]) ;; +*) echo wrong at $LINENO ;; +esac + +case $c1$c2$c3$c4 in +["$c1"]["$c2"]["$c3"]["$c4"]) ;; +*) echo wrong at $LINENO ;; +esac diff --git a/corebinutils/sh/tests/builtins/case7.0 b/corebinutils/sh/tests/builtins/case7.0 new file mode 100644 index 0000000000..0372f7387e --- /dev/null +++ b/corebinutils/sh/tests/builtins/case7.0 @@ -0,0 +1,20 @@ + +# Character ranges outside the POSIX locale are unspecified. Keep this test +# in the C locale so it remains deterministic across libc implementations. + +unset LC_CTYPE LC_COLLATE LANG LANGUAGE +LC_ALL=C +export LC_ALL + +c1=e +c2=o + +case $c1$c2 in +[a-z][a-z]) ;; +*) echo wrong at $LINENO ;; +esac + +case $c1$c2 in +[a-f][n-p]) ;; +*) echo wrong at $LINENO ;; +esac diff --git a/corebinutils/sh/tests/builtins/case8.0 b/corebinutils/sh/tests/builtins/case8.0 new file mode 100644 index 0000000000..01b5fdf35a --- /dev/null +++ b/corebinutils/sh/tests/builtins/case8.0 @@ -0,0 +1,31 @@ + +case aZ_ in +[[:alpha:]_][[:upper:]_][[:alpha:]_]) ;; +*) echo Failed at $LINENO ;; +esac + +case ' ' in +[[:alpha:][:digit:]]) echo Failed at $LINENO ;; +[![:alpha:][:digit:]]) ;; +*) echo Failed at $LINENO ;; +esac + +case '.X.' in +*[[:lower:]]*) echo Failed at $LINENO ;; +*[[:upper:]]*) ;; +*) echo Failed at $LINENO ;; +esac + +case ' ' in +[![:print:]]) echo Failed at $LINENO ;; +[![:alnum:][:punct:]]) ;; +*) echo Failed at $LINENO ;; +esac + +case ' +' in +[[:print:]]) echo Failed at $LINENO ;; +[' +'[:digit:]]) ;; +*) echo Failed at $LINENO ;; +esac diff --git a/corebinutils/sh/tests/builtins/case9.0 b/corebinutils/sh/tests/builtins/case9.0 new file mode 100644 index 0000000000..7719ba1951 --- /dev/null +++ b/corebinutils/sh/tests/builtins/case9.0 @@ -0,0 +1,38 @@ + +errors=0 + +f() { + result= + case $1 in + a) result=${result}a ;; + b) result=${result}b ;& + c) result=${result}c ;& + d) result=${result}d ;; + e) result=${result}e ;& + esac +} + +check() { + f "$1" + if [ "$result" != "$2" ]; then + printf "For %s, expected %s got %s\n" "$1" "$2" "$result" + errors=$((errors + 1)) + fi +} + +check '' '' +check a a +check b bcd +check c cd +check d d +check e e + +if ! (case 1 in + 1) false ;& + 2) true ;; +esac) then + echo "Subshell bad" + errors=$((errors + 1)) +fi + +exit $((errors != 0)) diff --git a/corebinutils/sh/tests/builtins/cd1.0 b/corebinutils/sh/tests/builtins/cd1.0 new file mode 100644 index 0000000000..52d165df5a --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd1.0 @@ -0,0 +1,29 @@ +set -e + +P=${TMPDIR:-/tmp} +cd $P +T=$(mktemp -d sh-test.XXXXXX) + +chmod 0 $T +if [ `id -u` -ne 0 ]; then + # Root can always cd, regardless of directory permissions. + cd -L $T 2>/dev/null && exit 1 + [ "$PWD" = "$P" ] + [ "$(pwd)" = "$P" ] + cd -P $T 2>/dev/null && exit 1 + [ "$PWD" = "$P" ] + [ "$(pwd)" = "$P" ] +fi + +chmod 755 $T +cd $T +mkdir -p 1/2/3 +ln -s 1/2 link1 +ln -s 2/3 1/link2 +(cd -L 1/../1 && [ "$(pwd -L)" = "$P/$T/1" ]) +(cd -L link1 && [ "$(pwd -L)" = "$P/$T/link1" ]) +(cd -L link1 && [ "$(pwd -P)" = "$P/$T/1/2" ]) +(cd -P link1 && [ "$(pwd -L)" = "$P/$T/1/2" ]) +(cd -P link1 && [ "$(pwd -P)" = "$P/$T/1/2" ]) + +rm -rf ${P}/${T} diff --git a/corebinutils/sh/tests/builtins/cd10.0 b/corebinutils/sh/tests/builtins/cd10.0 new file mode 100644 index 0000000000..07396e8e04 --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd10.0 @@ -0,0 +1,5 @@ + +# Precondition +(cd /bin) || exit +# Verify write error is ignored. +ENV= $SH +m -ic 'CDPATH=/:; cd bin 1</dev/null' diff --git a/corebinutils/sh/tests/builtins/cd11.0 b/corebinutils/sh/tests/builtins/cd11.0 new file mode 100644 index 0000000000..04e3469f8f --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd11.0 @@ -0,0 +1,23 @@ + +set -e +T=$(mktemp -d "${TMPDIR:-/tmp}/sh-test.XXXXXX") +trap 'rm -rf "$T"' 0 + +mkdir "$T/%?^&*" +cd -P "$T/%?^&*" +D=$(pwd) + +mkdir a a/1 b b/1 b/2 + +CDPATH=$D/a: +# Basic test. +cd 1 >/dev/null +[ "$(pwd)" = "$D/a/1" ] +# Test that the current directory is not checked before CDPATH. +cd "$D/b" +cd 1 >/dev/null +[ "$(pwd)" = "$D/a/1" ] +# Test not using a CDPATH entry. +cd "$D/b" +cd 2 +[ "$(pwd)" = "$D/b/2" ] diff --git a/corebinutils/sh/tests/builtins/cd12.0 b/corebinutils/sh/tests/builtins/cd12.0 new file mode 100644 index 0000000000..34bc053afd --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd12.0 @@ -0,0 +1,3 @@ +(cd /bin) || exit 1 +cd "" && exit 1 +exit 0 diff --git a/corebinutils/sh/tests/builtins/cd12.0.stderr b/corebinutils/sh/tests/builtins/cd12.0.stderr new file mode 100644 index 0000000000..cce88588b9 --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd12.0.stderr @@ -0,0 +1 @@ +cd: : No such file or directory diff --git a/corebinutils/sh/tests/builtins/cd2.0 b/corebinutils/sh/tests/builtins/cd2.0 new file mode 100644 index 0000000000..eea39115c3 --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd2.0 @@ -0,0 +1,15 @@ +set -e + +L=$(getconf PATH_MAX / 2>/dev/null) || L=4096 +[ "$L" -lt 100000 ] 2>/dev/null || L=4096 +L=$((L+100)) +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) +trap 'rm -rf ${T}' 0 +cd $T +D=$T +while [ ${#D} -lt $L ]; do + mkdir veryverylongdirectoryname + cd veryverylongdirectoryname + D=$D/veryverylongdirectoryname +done +[ $(pwd | wc -c) -eq $((${#D} + 1)) ] # +\n diff --git a/corebinutils/sh/tests/builtins/cd3.0 b/corebinutils/sh/tests/builtins/cd3.0 new file mode 100644 index 0000000000..d7211f5c50 --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd3.0 @@ -0,0 +1,20 @@ + +# If fully successful, cd -Pe must be like cd -P. + +set -e + +cd "${TMPDIR:-/tmp}" +cd -Pe / +[ "$PWD" = / ] +[ "$(pwd)" = / ] +cd "${TMPDIR:-/tmp}" +cd -eP / +[ "$PWD" = / ] +[ "$(pwd)" = / ] + +set +e + +# If cd -Pe cannot chdir, the exit status must be greater than 1. + +v=$( (cd -Pe /var/empty/nonexistent) 2>&1 >/dev/null) +[ $? -gt 1 ] && [ -n "$v" ] diff --git a/corebinutils/sh/tests/builtins/cd4.0 b/corebinutils/sh/tests/builtins/cd4.0 new file mode 100644 index 0000000000..33cf736322 --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd4.0 @@ -0,0 +1,37 @@ + +# This test assumes that whatever mechanism cd -P uses to determine the +# pathname to the current directory if it is longer than PATH_MAX requires +# read permission on all parent directories. It also works if this +# requirement always applies. + +set -e +L=$(getconf PATH_MAX / 2>/dev/null) || L=4096 +[ "$L" -lt 100000 ] 2>/dev/null || L=4096 +L=$((L+100)) +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) +trap 'chmod u+r ${T}; rm -rf ${T}' 0 +cd -Pe $T +D=$(pwd) +chmod u-r "$D" +if [ -r "$D" ]; then + # Running as root, cannot test. + exit 0 +fi +set +e +while [ ${#D} -lt $L ]; do + mkdir veryverylongdirectoryname || exit + cd -Pe veryverylongdirectoryname 2>/dev/null + r=$? + [ $r -gt 1 ] && exit $r + if [ $r -eq 1 ]; then + # Verify that the directory was changed correctly. + cd -Pe .. || exit + [ "$(pwd)" = "$D" ] || exit + # Verify that omitting -e results in success. + cd -P veryverylongdirectoryname 2>/dev/null || exit + exit 0 + fi + D=$D/veryverylongdirectoryname +done +echo "cd -Pe never returned 1" +exit 0 diff --git a/corebinutils/sh/tests/builtins/cd5.0 b/corebinutils/sh/tests/builtins/cd5.0 new file mode 100644 index 0000000000..ecb0d55a85 --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd5.0 @@ -0,0 +1,22 @@ + +set -e +T=$(mktemp -d "${TMPDIR:-/tmp}/sh-test.XXXXXX") +trap 'rm -rf "$T"' 0 + +cd -P "$T" +D=$(pwd) + +mkdir a a/1 b b/1 b/2 + +CDPATH=$D/a: +# Basic test. +cd 1 >/dev/null +[ "$(pwd)" = "$D/a/1" ] +# Test that the current directory is not checked before CDPATH. +cd "$D/b" +cd 1 >/dev/null +[ "$(pwd)" = "$D/a/1" ] +# Test not using a CDPATH entry. +cd "$D/b" +cd 2 +[ "$(pwd)" = "$D/b/2" ] diff --git a/corebinutils/sh/tests/builtins/cd6.0 b/corebinutils/sh/tests/builtins/cd6.0 new file mode 100644 index 0000000000..27f14172be --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd6.0 @@ -0,0 +1,9 @@ + +set -e +cd -P /bin +d=$PWD +CDPATH=/: +cd -P . +[ "$d" = "$PWD" ] +cd -P ./ +[ "$d" = "$PWD" ] diff --git a/corebinutils/sh/tests/builtins/cd7.0 b/corebinutils/sh/tests/builtins/cd7.0 new file mode 100644 index 0000000000..5b656f422f --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd7.0 @@ -0,0 +1,14 @@ + +set -e +cd /usr/bin +[ "$PWD" = /usr/bin ] +CDPATH=/: +cd . +[ "$PWD" = /usr/bin ] +cd ./ +[ "$PWD" = /usr/bin ] +cd .. +[ "$PWD" = /usr ] +cd /usr/bin +cd ../ +[ "$PWD" = /usr ] diff --git a/corebinutils/sh/tests/builtins/cd8.0 b/corebinutils/sh/tests/builtins/cd8.0 new file mode 100644 index 0000000000..3aff675d8d --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd8.0 @@ -0,0 +1,25 @@ + +# The exact wording of the error message is not standardized, but giving +# a description of the errno is useful. + +LC_ALL=C +export LC_ALL +r=0 + +t() { + exec 3>&1 + errmsg=`cd "$1" 2>&1 >&3 3>&-` + exec 3>&- + case $errmsg in + *[Nn]ot\ a\ directory*) + ;; + *) + printf "Wrong error message for %s: %s\n" "$1" "$errmsg" + r=3 + ;; + esac +} + +t /dev/tty +t /dev/tty/x +exit $r diff --git a/corebinutils/sh/tests/builtins/cd9.0 b/corebinutils/sh/tests/builtins/cd9.0 new file mode 100644 index 0000000000..811d75fff0 --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd9.0 @@ -0,0 +1,7 @@ + +cd /dev +cd /bin +cd - >/dev/null +pwd +cd - >/dev/null +pwd diff --git a/corebinutils/sh/tests/builtins/cd9.0.stdout b/corebinutils/sh/tests/builtins/cd9.0.stdout new file mode 100644 index 0000000000..dac16a768d --- /dev/null +++ b/corebinutils/sh/tests/builtins/cd9.0.stdout @@ -0,0 +1,2 @@ +/dev +/bin diff --git a/corebinutils/sh/tests/builtins/command1.0 b/corebinutils/sh/tests/builtins/command1.0 new file mode 100644 index 0000000000..2f2b36bb6d --- /dev/null +++ b/corebinutils/sh/tests/builtins/command1.0 @@ -0,0 +1,4 @@ +true() { + false +} +command true diff --git a/corebinutils/sh/tests/builtins/command10.0 b/corebinutils/sh/tests/builtins/command10.0 new file mode 100644 index 0000000000..add9e8372b --- /dev/null +++ b/corebinutils/sh/tests/builtins/command10.0 @@ -0,0 +1,13 @@ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +check '"$(f() { shift x; }; { command eval f 2>/dev/null; } >/dev/null; echo hi)" = hi' + +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/builtins/command11.0 b/corebinutils/sh/tests/builtins/command11.0 new file mode 100644 index 0000000000..9dc793d2f4 --- /dev/null +++ b/corebinutils/sh/tests/builtins/command11.0 @@ -0,0 +1,13 @@ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +check '"$({ command eval \{ shift x\; \} 2\>/dev/null; } >/dev/null; echo hi)" = hi' + +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/builtins/command12.0 b/corebinutils/sh/tests/builtins/command12.0 new file mode 100644 index 0000000000..a602a48cfc --- /dev/null +++ b/corebinutils/sh/tests/builtins/command12.0 @@ -0,0 +1,6 @@ + +alias aa=echo\ \'\"\' +cmd=$(command -v aa) +alias aa=echo\ bad +eval "$cmd" +[ "$(eval aa)" = \" ] diff --git a/corebinutils/sh/tests/builtins/command13.0 b/corebinutils/sh/tests/builtins/command13.0 new file mode 100644 index 0000000000..668664780a --- /dev/null +++ b/corebinutils/sh/tests/builtins/command13.0 @@ -0,0 +1,20 @@ + +failures=0 + +check() { + if [ "$1" != "$2" ] && { [ "$#" -lt 3 ] || [ "$1" != "$3" ]; } then + echo "Mismatch found" + echo "Expected: $2" + if [ "$#" -ge 3 ]; then + echo "Alternative expected: $3" + fi + echo "Actual: $1" + : $((failures += 1)) + fi +} + +check "$(cd /bin && PATH=. command -v ls)" /bin/ls /bin/./ls +check "$(cd /bin && PATH=:/var/empty/nosuch command -v ls)" /bin/ls /bin/./ls +check "$(cd / && PATH=bin command -v ls)" /bin/ls +check "$(cd / && command -v bin/ls)" /bin/ls +check "$(cd /bin && command -v ./ls)" /bin/ls /bin/./ls diff --git a/corebinutils/sh/tests/builtins/command14.0 b/corebinutils/sh/tests/builtins/command14.0 new file mode 100644 index 0000000000..6e4f0fd582 --- /dev/null +++ b/corebinutils/sh/tests/builtins/command14.0 @@ -0,0 +1,8 @@ + +r=`cd /bin && PATH=. command -V ls` +case $r in +*/bin/ls*|*/bin/./ls*) ;; +*) + echo "Unexpected result: $r" + exit 1 +esac diff --git a/corebinutils/sh/tests/builtins/command2.0 b/corebinutils/sh/tests/builtins/command2.0 new file mode 100644 index 0000000000..0f6c76bf89 --- /dev/null +++ b/corebinutils/sh/tests/builtins/command2.0 @@ -0,0 +1,2 @@ +PATH= +command -p cat < /dev/null diff --git a/corebinutils/sh/tests/builtins/command3.0 b/corebinutils/sh/tests/builtins/command3.0 new file mode 100644 index 0000000000..d9e85caff7 --- /dev/null +++ b/corebinutils/sh/tests/builtins/command3.0 @@ -0,0 +1,13 @@ +command -v ls +command -v true +command -v /bin/ls + +fun() { + : +} +command -v fun +command -v break +command -v if + +alias foo=bar +command -v foo diff --git a/corebinutils/sh/tests/builtins/command3.0.stdout b/corebinutils/sh/tests/builtins/command3.0.stdout new file mode 100644 index 0000000000..67b869156a --- /dev/null +++ b/corebinutils/sh/tests/builtins/command3.0.stdout @@ -0,0 +1,7 @@ +/bin/ls +true +/bin/ls +fun +break +if +alias foo=bar diff --git a/corebinutils/sh/tests/builtins/command4.0 b/corebinutils/sh/tests/builtins/command4.0 new file mode 100644 index 0000000000..b2b0146fb5 --- /dev/null +++ b/corebinutils/sh/tests/builtins/command4.0 @@ -0,0 +1 @@ +! command -v nonexisting diff --git a/corebinutils/sh/tests/builtins/command5.0 b/corebinutils/sh/tests/builtins/command5.0 new file mode 100644 index 0000000000..050b44913f --- /dev/null +++ b/corebinutils/sh/tests/builtins/command5.0 @@ -0,0 +1,14 @@ +command -V ls +command -V true +command -V /bin/ls + +fun() { + : +} +command -V fun +command -V break +command -V if +command -V { + +alias foo=bar +command -V foo diff --git a/corebinutils/sh/tests/builtins/command5.0.stdout b/corebinutils/sh/tests/builtins/command5.0.stdout new file mode 100644 index 0000000000..523f7b22a1 --- /dev/null +++ b/corebinutils/sh/tests/builtins/command5.0.stdout @@ -0,0 +1,8 @@ +ls is /bin/ls +true is a shell builtin +/bin/ls is /bin/ls +fun is a shell function +break is a special shell builtin +if is a shell keyword +{ is a shell keyword +foo is an alias for bar diff --git a/corebinutils/sh/tests/builtins/command6.0 b/corebinutils/sh/tests/builtins/command6.0 new file mode 100644 index 0000000000..56d65a095a --- /dev/null +++ b/corebinutils/sh/tests/builtins/command6.0 @@ -0,0 +1,21 @@ +PATH=/var/empty +case $(command -pV ls) in +*/var/empty/ls*) + echo "Failed: \$(command -pV ls) should not match */var/empty/ls*" ;; +"ls is"*" "/*/ls) ;; +*) + echo "Failed: \$(command -pV ls) match \"ls is\"*\" \"/*/ls" ;; +esac +command -pV true +command -pV /bin/ls + +fun() { + : +} +command -pV fun +command -pV break +command -pV if +command -pV { + +alias foo=bar +command -pV foo diff --git a/corebinutils/sh/tests/builtins/command6.0.stdout b/corebinutils/sh/tests/builtins/command6.0.stdout new file mode 100644 index 0000000000..3180207a4e --- /dev/null +++ b/corebinutils/sh/tests/builtins/command6.0.stdout @@ -0,0 +1,7 @@ +true is a shell builtin +/bin/ls is /bin/ls +fun is a shell function +break is a special shell builtin +if is a shell keyword +{ is a shell keyword +foo is an alias for bar diff --git a/corebinutils/sh/tests/builtins/command7.0 b/corebinutils/sh/tests/builtins/command7.0 new file mode 100644 index 0000000000..09ff98ef9d --- /dev/null +++ b/corebinutils/sh/tests/builtins/command7.0 @@ -0,0 +1,75 @@ + +failures=0 + +check_eq() { + actual=$1 + expected=$2 + label=$3 + if [ "$actual" != "$expected" ]; then + echo "Failed: $label" + echo " expected: $expected" + echo " actual: $actual" + failures=$((failures + 1)) + fi +} + +check_match() { + actual=$1 + pattern=$2 + label=$3 + case $actual in + $pattern) + ;; + *) + echo "Failed: $label" + echo " expected pattern: $pattern" + echo " actual: $actual" + failures=$((failures + 1)) + ;; + esac +} + +mkdir pathbin +cat >pathbin/cmdprobe <<'EOF' +#!/bin/sh +exit 0 +EOF +chmod +x pathbin/cmdprobe + +probe_dir=$PWD/pathbin +probe_path=$probe_dir/cmdprobe + +check_eq "$(PATH=$probe_dir command -V cmdprobe)" \ + "cmdprobe is $probe_path" \ + 'PATH=$probe_dir command -V cmdprobe' +check_eq "$(PATH=$probe_dir command -V cmdprobe; :)" \ + "cmdprobe is $probe_path" \ + 'PATH=$probe_dir command -V cmdprobe; :' +check_eq "$(PATH=$probe_dir command -pv cmdprobe)" \ + "" \ + 'PATH=$probe_dir command -pv cmdprobe' +check_eq "$(PATH=$probe_dir command -pv cmdprobe; :)" \ + "" \ + 'PATH=$probe_dir command -pv cmdprobe; :' + +PATH=$probe_dir:$PATH + +check_eq "$(command -V cmdprobe)" \ + "cmdprobe is $probe_path" \ + 'PATH prefixed command -V cmdprobe' +check_eq "$(command -V cmdprobe; :)" \ + "cmdprobe is $probe_path" \ + 'PATH prefixed command -V cmdprobe; :' +check_eq "$(command -pv cmdprobe)" \ + "" \ + 'PATH prefixed command -pv cmdprobe' +check_eq "$(command -pv cmdprobe; :)" \ + "" \ + 'PATH prefixed command -pv cmdprobe; :' + +PATH=$probe_dir + +check_eq "$(command -v ls)" "" 'PATH isolated command -v ls' +check_match "$(command -pv ls)" '/*/ls' 'PATH isolated command -pv ls' + +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/builtins/command8.0 b/corebinutils/sh/tests/builtins/command8.0 new file mode 100644 index 0000000000..df50cb4f97 --- /dev/null +++ b/corebinutils/sh/tests/builtins/command8.0 @@ -0,0 +1,44 @@ +IFS=, + +SPECIAL="break,\ + :,\ + continue,\ + . /dev/null,\ + eval,\ + exec,\ + export -p,\ + readonly -p,\ + set,\ + shift 0,\ + times,\ + trap,\ + unset foo" + +set -e + +# Check that special builtins can be executed via "command". + +set -- ${SPECIAL} +for cmd in "$@" +do + ${SH} -c "v=:; while \$v; do v=false; command ${cmd}; done" >/dev/null +done + +while :; do + command break + echo Error on line $LINENO +done + +set p q r +command shift 2 +if [ $# -ne 1 ]; then + echo Error on line $LINENO +fi + +( + command exec >/dev/null + echo Error on line $LINENO +) + +set +e +! command shift 2 2>/dev/null diff --git a/corebinutils/sh/tests/builtins/command9.0 b/corebinutils/sh/tests/builtins/command9.0 new file mode 100644 index 0000000000..3dbc1e0a43 --- /dev/null +++ b/corebinutils/sh/tests/builtins/command9.0 @@ -0,0 +1,13 @@ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +check '"$({ command eval shift x 2>/dev/null; } >/dev/null; echo hi)" = hi' + +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/builtins/dot1.0 b/corebinutils/sh/tests/builtins/dot1.0 new file mode 100644 index 0000000000..0ecfb7ef48 --- /dev/null +++ b/corebinutils/sh/tests/builtins/dot1.0 @@ -0,0 +1,20 @@ + +failures= +failure() { + echo "Error at line $1" >&2 + failures=x$failures +} + +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) || exit +trap 'rm -rf $T' 0 +cd $T || exit 3 +unset x +echo 'x=2' >testscript +. ./testscript +[ "$x" = 2 ] || failure $LINENO +cd / || exit 3 +x=1 +PATH=$T:$PATH . testscript +[ "$x" = 2 ] || failure $LINENO + +test -z "$failures" diff --git a/corebinutils/sh/tests/builtins/dot2.0 b/corebinutils/sh/tests/builtins/dot2.0 new file mode 100644 index 0000000000..497e213951 --- /dev/null +++ b/corebinutils/sh/tests/builtins/dot2.0 @@ -0,0 +1,20 @@ + +failures= +failure() { + echo "Error at line $1" >&2 + failures=x$failures +} + +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) || exit +trap 'rm -rf $T' 0 +cd $T || exit 3 +unset x +echo 'x=2' >testscript +. -- ./testscript +[ "$x" = 2 ] || failure $LINENO +cd / || exit 3 +x=1 +PATH=$T:$PATH . -- testscript +[ "$x" = 2 ] || failure $LINENO + +test -z "$failures" diff --git a/corebinutils/sh/tests/builtins/dot3.0 b/corebinutils/sh/tests/builtins/dot3.0 new file mode 100644 index 0000000000..9017a52d81 --- /dev/null +++ b/corebinutils/sh/tests/builtins/dot3.0 @@ -0,0 +1,9 @@ + +# . should return 0 if no command was executed. + +if false; then + exit 3 +else + . /dev/null + exit $? +fi diff --git a/corebinutils/sh/tests/builtins/dot4.0 b/corebinutils/sh/tests/builtins/dot4.0 new file mode 100644 index 0000000000..c9cf7ec67f --- /dev/null +++ b/corebinutils/sh/tests/builtins/dot4.0 @@ -0,0 +1,11 @@ + +v=abcd +v=$v$v$v$v +v=$v$v$v$v +v=$v$v$v$v +v=$v$v$v$v +v=$v$v$v$v +r=$( ( + trap 'exit 0' 0 + . "$v" +) 2>&1 >/dev/null) && [ -n "$r" ] diff --git a/corebinutils/sh/tests/builtins/echo1.0 b/corebinutils/sh/tests/builtins/echo1.0 new file mode 100644 index 0000000000..6b9b14da83 --- /dev/null +++ b/corebinutils/sh/tests/builtins/echo1.0 @@ -0,0 +1,5 @@ + +# Not specified by POSIX. + +[ "`echo -n a b; echo c d; echo e f`" = "a bc d +e f" ] diff --git a/corebinutils/sh/tests/builtins/echo2.0 b/corebinutils/sh/tests/builtins/echo2.0 new file mode 100644 index 0000000000..9d8d9817e9 --- /dev/null +++ b/corebinutils/sh/tests/builtins/echo2.0 @@ -0,0 +1,6 @@ + +# Not specified by POSIX. + +a=`echo -e '\a\b\e\f\n\r\t\v\\\\\0041\c'; echo .` +b=`printf '\a\b\033\f\n\r\t\v\\\\!.'` +[ "$a" = "$b" ] diff --git a/corebinutils/sh/tests/builtins/echo3.0 b/corebinutils/sh/tests/builtins/echo3.0 new file mode 100644 index 0000000000..a56d3da07a --- /dev/null +++ b/corebinutils/sh/tests/builtins/echo3.0 @@ -0,0 +1,4 @@ + +# Not specified by POSIX. + +[ "`echo -e 'a\cb' c; echo d`" = "ad" ] diff --git a/corebinutils/sh/tests/builtins/eval1.0 b/corebinutils/sh/tests/builtins/eval1.0 new file mode 100644 index 0000000000..9019d3b9bb --- /dev/null +++ b/corebinutils/sh/tests/builtins/eval1.0 @@ -0,0 +1,8 @@ +set -e + +eval +eval "" "" +eval "true" +! eval "false + +" diff --git a/corebinutils/sh/tests/builtins/eval2.0 b/corebinutils/sh/tests/builtins/eval2.0 new file mode 100644 index 0000000000..b762ac2eea --- /dev/null +++ b/corebinutils/sh/tests/builtins/eval2.0 @@ -0,0 +1,6 @@ + +eval ' +false + +' && exit 1 +exit 0 diff --git a/corebinutils/sh/tests/builtins/eval3.0 b/corebinutils/sh/tests/builtins/eval3.0 new file mode 100644 index 0000000000..12f36dc00d --- /dev/null +++ b/corebinutils/sh/tests/builtins/eval3.0 @@ -0,0 +1,8 @@ + +eval 'false;' && exit 1 +eval 'true;' || exit 1 +eval 'false; +' && exit 1 +eval 'true; +' || exit 1 +exit 0 diff --git a/corebinutils/sh/tests/builtins/eval4.0 b/corebinutils/sh/tests/builtins/eval4.0 new file mode 100644 index 0000000000..3eb4e3679f --- /dev/null +++ b/corebinutils/sh/tests/builtins/eval4.0 @@ -0,0 +1,4 @@ + +# eval should preserve $? from command substitutions when starting +# the parsed command. +[ $(eval 'echo $?' $(false)) = 1 ] diff --git a/corebinutils/sh/tests/builtins/eval5.0 b/corebinutils/sh/tests/builtins/eval5.0 new file mode 100644 index 0000000000..44d125408d --- /dev/null +++ b/corebinutils/sh/tests/builtins/eval5.0 @@ -0,0 +1,3 @@ + +# eval should return 0 if no command was executed. +eval $(false) diff --git a/corebinutils/sh/tests/builtins/eval6.0 b/corebinutils/sh/tests/builtins/eval6.0 new file mode 100644 index 0000000000..84584f1c1b --- /dev/null +++ b/corebinutils/sh/tests/builtins/eval6.0 @@ -0,0 +1,4 @@ + +# eval should preserve $? from command substitutions when starting +# the parsed command. +[ $(false; eval 'echo $?' $(:)) = 0 ] diff --git a/corebinutils/sh/tests/builtins/eval7.0 b/corebinutils/sh/tests/builtins/eval7.0 new file mode 100644 index 0000000000..238353fb5c --- /dev/null +++ b/corebinutils/sh/tests/builtins/eval7.0 @@ -0,0 +1,8 @@ +# Assumes that break can break out of a loop outside eval. + +while :; do + eval "break +echo bad1" + echo bad2 + exit 3 +done diff --git a/corebinutils/sh/tests/builtins/eval8.7 b/corebinutils/sh/tests/builtins/eval8.7 new file mode 100644 index 0000000000..671fcb5cb1 --- /dev/null +++ b/corebinutils/sh/tests/builtins/eval8.7 @@ -0,0 +1,6 @@ + +f() { + eval "return 7 +echo bad2" +} +f diff --git a/corebinutils/sh/tests/builtins/exec1.0 b/corebinutils/sh/tests/builtins/exec1.0 new file mode 100644 index 0000000000..a3d993a739 --- /dev/null +++ b/corebinutils/sh/tests/builtins/exec1.0 @@ -0,0 +1,24 @@ + +failures= +failure() { + echo "Error at line $1" >&2 + failures=x$failures +} + +( + exec >/dev/null + echo bad +) +[ $? = 0 ] || failure $LINENO +( + exec ${SH} -c 'exit 42' + echo bad +) +[ $? = 42 ] || failure $LINENO +( + exec /var/empty/nosuch + echo bad +) 2>/dev/null +[ $? = 127 ] || failure $LINENO + +test -z "$failures" diff --git a/corebinutils/sh/tests/builtins/exec2.0 b/corebinutils/sh/tests/builtins/exec2.0 new file mode 100644 index 0000000000..9ed1013acd --- /dev/null +++ b/corebinutils/sh/tests/builtins/exec2.0 @@ -0,0 +1,24 @@ + +failures= +failure() { + echo "Error at line $1" >&2 + failures=x$failures +} + +( + exec -- >/dev/null + echo bad +) +[ $? = 0 ] || failure $LINENO +( + exec -- ${SH} -c 'exit 42' + echo bad +) +[ $? = 42 ] || failure $LINENO +( + exec -- /var/empty/nosuch + echo bad +) 2>/dev/null +[ $? = 127 ] || failure $LINENO + +test -z "$failures" diff --git a/corebinutils/sh/tests/builtins/exit1.0 b/corebinutils/sh/tests/builtins/exit1.0 new file mode 100644 index 0000000000..fb6486cebe --- /dev/null +++ b/corebinutils/sh/tests/builtins/exit1.0 @@ -0,0 +1,5 @@ + +# exit with an argument should overwrite the exit status in an EXIT trap. + +trap 'true; exit $?' 0 +false diff --git a/corebinutils/sh/tests/builtins/exit2.8 b/corebinutils/sh/tests/builtins/exit2.8 new file mode 100644 index 0000000000..327b620c3f --- /dev/null +++ b/corebinutils/sh/tests/builtins/exit2.8 @@ -0,0 +1,6 @@ + +# exit without arguments is the same as exit $? outside a trap. + +trap 'true; true' 0 +(exit 8) +exit diff --git a/corebinutils/sh/tests/builtins/exit3.0 b/corebinutils/sh/tests/builtins/exit3.0 new file mode 100644 index 0000000000..996074c101 --- /dev/null +++ b/corebinutils/sh/tests/builtins/exit3.0 @@ -0,0 +1,4 @@ + +# exit without arguments differs from exit $? in an EXIT trap. + +trap 'false; exit' 0 diff --git a/corebinutils/sh/tests/builtins/export1.0 b/corebinutils/sh/tests/builtins/export1.0 new file mode 100644 index 0000000000..e2802c4f8e --- /dev/null +++ b/corebinutils/sh/tests/builtins/export1.0 @@ -0,0 +1,2 @@ + +env @badness=1 ${SH} -c 'v=`export -p`; eval "$v"' diff --git a/corebinutils/sh/tests/builtins/fc1.0 b/corebinutils/sh/tests/builtins/fc1.0 new file mode 100644 index 0000000000..c84fb05490 --- /dev/null +++ b/corebinutils/sh/tests/builtins/fc1.0 @@ -0,0 +1,26 @@ +set -e +trap 'echo Broken pipe -- test failed' PIPE + +P=${TMPDIR:-/tmp} +cd $P +T=$(mktemp -d sh-test.XXXXXX) +cd $T + +mkfifo input output error +ENV= HISTFILE=/dev/null ${SH} +m -i <input >output 2>error & +{ + # Syntax error + echo ')' >&3 + # Read error message, shell will read new input now + read dummy <&5 + # Execute bad command again + echo 'fc -e true' >&3 + # Verify that the shell is still running + echo 'echo continued' >&3 || rc=3 + echo 'exit' >&3 || rc=3 + read line <&4 && [ "$line" = continued ] && : ${rc:=0} +} 3>input 4<output 5<error + +rm input output error +rmdir ${P}/${T} +exit ${rc:-3} diff --git a/corebinutils/sh/tests/builtins/fc2.0 b/corebinutils/sh/tests/builtins/fc2.0 new file mode 100644 index 0000000000..53c0f786cd --- /dev/null +++ b/corebinutils/sh/tests/builtins/fc2.0 @@ -0,0 +1,33 @@ +set -e +trap 'echo Broken pipe -- test failed' PIPE + +P=${TMPDIR:-/tmp} +cd $P +T=$(mktemp -d sh-test.XXXXXX) +cd $T + +mkfifo input output error +HISTFILE=/dev/null ${SH} +m -i <input >output 2>error & +exec 3>input +{ + # Command not found, containing slash + echo '/var/empty/nonexistent' >&3 + # Read error message, shell will read new input now + read dummy <&5 + # Execute bad command again + echo 'fc -e true; echo continued' >&3 + read dummy <&5 + read line <&4 && [ "$line" = continued ] && : ${rc:=0} + exec 3>&- + # Old sh duplicates itself after the fc, producing another line + # of output. + if read line <&4; then + echo "Extraneous output: $line" + rc=1 + fi +} 4<output 5<error +exec 3>&- + +rm input output error +rmdir ${P}/${T} +exit ${rc:-3} diff --git a/corebinutils/sh/tests/builtins/fc3.0 b/corebinutils/sh/tests/builtins/fc3.0 new file mode 100644 index 0000000000..daa615bcc3 --- /dev/null +++ b/corebinutils/sh/tests/builtins/fc3.0 @@ -0,0 +1,9 @@ +export PS1='_ ' # cannot predict whether ran by root or not + +echo ': command1 +: command2 +: command3 +: command4 +fc -l -n -1 +fc -ln 2 3 +' | ENV= HISTFILE=/dev/null ${SH} +m -i diff --git a/corebinutils/sh/tests/builtins/fc3.0.stderr b/corebinutils/sh/tests/builtins/fc3.0.stderr new file mode 100644 index 0000000000..e05147fb82 --- /dev/null +++ b/corebinutils/sh/tests/builtins/fc3.0.stderr @@ -0,0 +1 @@ +_ _ _ _ _ _ _ _ diff --git a/corebinutils/sh/tests/builtins/fc3.0.stdout b/corebinutils/sh/tests/builtins/fc3.0.stdout new file mode 100644 index 0000000000..8c23c91363 --- /dev/null +++ b/corebinutils/sh/tests/builtins/fc3.0.stdout @@ -0,0 +1,3 @@ +: command4 +: command2 +: command3 diff --git a/corebinutils/sh/tests/builtins/for1.0 b/corebinutils/sh/tests/builtins/for1.0 new file mode 100644 index 0000000000..f1a64f412b --- /dev/null +++ b/corebinutils/sh/tests/builtins/for1.0 @@ -0,0 +1,3 @@ + +false +for i in `false`; do exit 3; done diff --git a/corebinutils/sh/tests/builtins/for2.0 b/corebinutils/sh/tests/builtins/for2.0 new file mode 100644 index 0000000000..0ed4e1158e --- /dev/null +++ b/corebinutils/sh/tests/builtins/for2.0 @@ -0,0 +1,8 @@ + +r=x +f() { return 42; } +f +for i in x; do + r=$? +done +[ "$r" = 42 ] diff --git a/corebinutils/sh/tests/builtins/for3.0 b/corebinutils/sh/tests/builtins/for3.0 new file mode 100644 index 0000000000..010606b16c --- /dev/null +++ b/corebinutils/sh/tests/builtins/for3.0 @@ -0,0 +1,7 @@ + +r=x +f() { return 42; } +for i in x`f`; do + r=$? +done +[ "$r" = 42 ] diff --git a/corebinutils/sh/tests/builtins/getopts1.0 b/corebinutils/sh/tests/builtins/getopts1.0 new file mode 100644 index 0000000000..928f472900 --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts1.0 @@ -0,0 +1,24 @@ + +printf -- '-1-\n' +set -- -abc +getopts "ab:" OPTION +printf '%s\n' "${OPTION}" + +# In this case 'getopts' should realize that we have not provided the +# required argument for "-b". +# Note that Solaris 10's (UNIX 03) /usr/xpg4/bin/sh, /bin/sh, and /bin/ksh; +# ksh93 20090505; pdksh 5.2.14p2; mksh R39c; bash 4.1 PL7; and zsh 4.3.10. +# all recognize that "b" is missing its argument on the *first* iteration +# of 'getopts' and do not produce the "a" in $OPTION. +printf -- '-2-\n' +set -- -ab +getopts "ab:" OPTION +printf '%s\n' "${OPTION}" +getopts "ab:" OPTION 3>&2 2>&1 >&3 3>&- +printf '%s\n' "${OPTION}" + +# The 'shift' is aimed at causing an error. +printf -- '-3-\n' +shift 1 +getopts "ab:" OPTION +printf '%s\n' "${OPTION}" diff --git a/corebinutils/sh/tests/builtins/getopts1.0.stdout b/corebinutils/sh/tests/builtins/getopts1.0.stdout new file mode 100644 index 0000000000..a0a347eea8 --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts1.0.stdout @@ -0,0 +1,8 @@ +-1- +a +-2- +a +No arg for -b option +? +-3- +? diff --git a/corebinutils/sh/tests/builtins/getopts10.0 b/corebinutils/sh/tests/builtins/getopts10.0 new file mode 100644 index 0000000000..5182da24a0 --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts10.0 @@ -0,0 +1,10 @@ + +set -- -x arg +opt=not +getopts x opt +r1=$? OPTIND1=$OPTIND opt1=$opt +: $(: $((OPTIND = 1))) +getopts x opt +r2=$? OPTIND2=$OPTIND +[ "$r1" = 0 ] && [ "$OPTIND1" = 2 ] && [ "$opt1" = x ] && [ "$r2" != 0 ] && + [ "$OPTIND2" = 2 ] diff --git a/corebinutils/sh/tests/builtins/getopts2.0 b/corebinutils/sh/tests/builtins/getopts2.0 new file mode 100644 index 0000000000..b12813e624 --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts2.0 @@ -0,0 +1,5 @@ +set - -ax +getopts ax option +set -C +getopts ax option +printf '%s\n' "$option" diff --git a/corebinutils/sh/tests/builtins/getopts2.0.stdout b/corebinutils/sh/tests/builtins/getopts2.0.stdout new file mode 100644 index 0000000000..587be6b4c3 --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts2.0.stdout @@ -0,0 +1 @@ +x diff --git a/corebinutils/sh/tests/builtins/getopts3.0 b/corebinutils/sh/tests/builtins/getopts3.0 new file mode 100644 index 0000000000..c52e60ada8 --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts3.0 @@ -0,0 +1,5 @@ + +shift $# +getopts x opt +r=$? +[ "$r" != 0 ] && [ "$OPTIND" = 1 ] diff --git a/corebinutils/sh/tests/builtins/getopts4.0 b/corebinutils/sh/tests/builtins/getopts4.0 new file mode 100644 index 0000000000..f4df551cb5 --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts4.0 @@ -0,0 +1,9 @@ + +set -- -x +opt=not +getopts x opt +r1=$? OPTIND1=$OPTIND opt1=$opt +getopts x opt +r2=$? OPTIND2=$OPTIND +[ "$r1" = 0 ] && [ "$OPTIND1" = 2 ] && [ "$opt1" = x ] && [ "$r2" != 0 ] && + [ "$OPTIND2" = 2 ] diff --git a/corebinutils/sh/tests/builtins/getopts5.0 b/corebinutils/sh/tests/builtins/getopts5.0 new file mode 100644 index 0000000000..d037b97058 --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts5.0 @@ -0,0 +1,9 @@ + +set -- -x arg +opt=not +getopts x opt +r1=$? OPTIND1=$OPTIND opt1=$opt +getopts x opt +r2=$? OPTIND2=$OPTIND +[ "$r1" = 0 ] && [ "$OPTIND1" = 2 ] && [ "$opt1" = x ] && [ "$r2" != 0 ] && + [ "$OPTIND2" = 2 ] diff --git a/corebinutils/sh/tests/builtins/getopts6.0 b/corebinutils/sh/tests/builtins/getopts6.0 new file mode 100644 index 0000000000..5cf2bc4629 --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts6.0 @@ -0,0 +1,6 @@ + +set -- -x -y +getopts :x var || echo "First getopts bad: $?" +getopts :x var +r=$? +[ r != 0 ] && [ "$OPTIND" = 3 ] diff --git a/corebinutils/sh/tests/builtins/getopts7.0 b/corebinutils/sh/tests/builtins/getopts7.0 new file mode 100644 index 0000000000..5dd5ed593c --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts7.0 @@ -0,0 +1,5 @@ + +set -- -x +getopts :x: var +r=$? +[ r != 0 ] && [ "$OPTIND" = 2 ] diff --git a/corebinutils/sh/tests/builtins/getopts8.0 b/corebinutils/sh/tests/builtins/getopts8.0 new file mode 100644 index 0000000000..30b36630c7 --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts8.0 @@ -0,0 +1,7 @@ + +set -- -yz -wx +opt=wrong1 OPTARG=wrong2 +while getopts :x opt; do + echo "$opt:${OPTARG-unset}" +done +echo "OPTIND=$OPTIND" diff --git a/corebinutils/sh/tests/builtins/getopts8.0.stdout b/corebinutils/sh/tests/builtins/getopts8.0.stdout new file mode 100644 index 0000000000..f10cefcd8c --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts8.0.stdout @@ -0,0 +1,5 @@ +?:y +?:z +?:w +x:unset +OPTIND=3 diff --git a/corebinutils/sh/tests/builtins/getopts9.0 b/corebinutils/sh/tests/builtins/getopts9.0 new file mode 100644 index 0000000000..5e91f92614 --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts9.0 @@ -0,0 +1,8 @@ + +args='-ab' +getopts ab opt $args +printf '%s\n' "$?:$opt:$OPTARG" +for dummy in dummy1 dummy2; do + getopts ab opt $args + printf '%s\n' "$?:$opt:$OPTARG" +done diff --git a/corebinutils/sh/tests/builtins/getopts9.0.stdout b/corebinutils/sh/tests/builtins/getopts9.0.stdout new file mode 100644 index 0000000000..4d32063faa --- /dev/null +++ b/corebinutils/sh/tests/builtins/getopts9.0.stdout @@ -0,0 +1,3 @@ +0:a: +0:b: +1:?: diff --git a/corebinutils/sh/tests/builtins/hash1.0 b/corebinutils/sh/tests/builtins/hash1.0 new file mode 100644 index 0000000000..750fd40caf --- /dev/null +++ b/corebinutils/sh/tests/builtins/hash1.0 @@ -0,0 +1,4 @@ +cat /dev/null +hash +hash -r +hash diff --git a/corebinutils/sh/tests/builtins/hash1.0.stdout b/corebinutils/sh/tests/builtins/hash1.0.stdout new file mode 100644 index 0000000000..3afc3e7b38 --- /dev/null +++ b/corebinutils/sh/tests/builtins/hash1.0.stdout @@ -0,0 +1 @@ +/bin/cat diff --git a/corebinutils/sh/tests/builtins/hash2.0 b/corebinutils/sh/tests/builtins/hash2.0 new file mode 100644 index 0000000000..ec072b93a1 --- /dev/null +++ b/corebinutils/sh/tests/builtins/hash2.0 @@ -0,0 +1,3 @@ +hash +hash cat +hash diff --git a/corebinutils/sh/tests/builtins/hash2.0.stdout b/corebinutils/sh/tests/builtins/hash2.0.stdout new file mode 100644 index 0000000000..3afc3e7b38 --- /dev/null +++ b/corebinutils/sh/tests/builtins/hash2.0.stdout @@ -0,0 +1 @@ +/bin/cat diff --git a/corebinutils/sh/tests/builtins/hash3.0 b/corebinutils/sh/tests/builtins/hash3.0 new file mode 100644 index 0000000000..3ef33dc4bb --- /dev/null +++ b/corebinutils/sh/tests/builtins/hash3.0 @@ -0,0 +1,2 @@ +hash -v cat +hash diff --git a/corebinutils/sh/tests/builtins/hash3.0.stdout b/corebinutils/sh/tests/builtins/hash3.0.stdout new file mode 100644 index 0000000000..a34864cd6d --- /dev/null +++ b/corebinutils/sh/tests/builtins/hash3.0.stdout @@ -0,0 +1,2 @@ +/bin/cat +/bin/cat diff --git a/corebinutils/sh/tests/builtins/hash4.0 b/corebinutils/sh/tests/builtins/hash4.0 new file mode 100644 index 0000000000..3e99930bad --- /dev/null +++ b/corebinutils/sh/tests/builtins/hash4.0 @@ -0,0 +1,5 @@ + +exec 3>&1 +m=`hash nosuchtool 2>&1 >&3` +r=$? +[ "$r" != 0 ] && [ -n "$m" ] diff --git a/corebinutils/sh/tests/builtins/jobid1.0 b/corebinutils/sh/tests/builtins/jobid1.0 new file mode 100644 index 0000000000..093b2c2107 --- /dev/null +++ b/corebinutils/sh/tests/builtins/jobid1.0 @@ -0,0 +1,6 @@ +# Non-standard builtin. + +: & +p1=$! +p2=$(jobid) +[ "${p1:?}" = "${p2:?}" ] diff --git a/corebinutils/sh/tests/builtins/jobid2.0 b/corebinutils/sh/tests/builtins/jobid2.0 new file mode 100644 index 0000000000..bf3de17d01 --- /dev/null +++ b/corebinutils/sh/tests/builtins/jobid2.0 @@ -0,0 +1,8 @@ + +: & +p1=$(jobid) +p2=$(jobid --) +p3=$(jobid %+) +p4=$(jobid -- %+) +[ "${p1:?}" = "${p2:?}" ] && [ "${p2:?}" = "${p3:?}" ] && +[ "${p3:?}" = "${p4:?}" ] && [ "${p4:?}" = "${p1:?}" ] diff --git a/corebinutils/sh/tests/builtins/kill1.0 b/corebinutils/sh/tests/builtins/kill1.0 new file mode 100644 index 0000000000..85b8b579b3 --- /dev/null +++ b/corebinutils/sh/tests/builtins/kill1.0 @@ -0,0 +1,7 @@ + +: & +p1=$! +: & +p2=$! +wait $p2 +kill %1 diff --git a/corebinutils/sh/tests/builtins/kill2.0 b/corebinutils/sh/tests/builtins/kill2.0 new file mode 100644 index 0000000000..13d2a4a896 --- /dev/null +++ b/corebinutils/sh/tests/builtins/kill2.0 @@ -0,0 +1,6 @@ + +sleep 1 | sleep 1 & +kill %+ +wait "$!" +r=$? +[ "$r" -gt 128 ] && [ "$(kill -l "$r")" = TERM ] diff --git a/corebinutils/sh/tests/builtins/lineno.0 b/corebinutils/sh/tests/builtins/lineno.0 new file mode 100644 index 0000000000..d314eef0f8 --- /dev/null +++ b/corebinutils/sh/tests/builtins/lineno.0 @@ -0,0 +1,15 @@ +echo $LINENO +echo $LINENO + +f() { + echo $LINENO + echo $LINENO +} + +f + +echo ${LINENO:-foo} +echo ${LINENO=foo} +echo ${LINENO:+foo} +echo ${LINENO+foo} +echo ${#LINENO} diff --git a/corebinutils/sh/tests/builtins/lineno.0.stdout b/corebinutils/sh/tests/builtins/lineno.0.stdout new file mode 100644 index 0000000000..ab5ad9cbef --- /dev/null +++ b/corebinutils/sh/tests/builtins/lineno.0.stdout @@ -0,0 +1,9 @@ +1 +2 +2 +3 +11 +12 +foo +foo +2 diff --git a/corebinutils/sh/tests/builtins/lineno2.0 b/corebinutils/sh/tests/builtins/lineno2.0 new file mode 100644 index 0000000000..db2a6c8c7b --- /dev/null +++ b/corebinutils/sh/tests/builtins/lineno2.0 @@ -0,0 +1,9 @@ + +f() { + : ${LINENO+${x?}} +} + +unset -v x +command eval f 2>/dev/null && exit 3 +x=1 +f diff --git a/corebinutils/sh/tests/builtins/lineno3.0 b/corebinutils/sh/tests/builtins/lineno3.0 new file mode 100644 index 0000000000..66ede861dc --- /dev/null +++ b/corebinutils/sh/tests/builtins/lineno3.0 @@ -0,0 +1,5 @@ + +echo before: $LINENO +dummy=$'a\0 +' +echo after: $LINENO diff --git a/corebinutils/sh/tests/builtins/lineno3.0.stdout b/corebinutils/sh/tests/builtins/lineno3.0.stdout new file mode 100644 index 0000000000..f8b44636cb --- /dev/null +++ b/corebinutils/sh/tests/builtins/lineno3.0.stdout @@ -0,0 +1,2 @@ +before: 2 +after: 5 diff --git a/corebinutils/sh/tests/builtins/local1.0 b/corebinutils/sh/tests/builtins/local1.0 new file mode 100644 index 0000000000..e5278d4926 --- /dev/null +++ b/corebinutils/sh/tests/builtins/local1.0 @@ -0,0 +1,12 @@ +# A commonly used but non-POSIX builtin. + +f() { + local x + x=2 + [ "$x" = 2 ] +} +x=1 +f || exit 3 +[ "$x" = 1 ] || exit 3 +f || exit 3 +[ "$x" = 1 ] || exit 3 diff --git a/corebinutils/sh/tests/builtins/local2.0 b/corebinutils/sh/tests/builtins/local2.0 new file mode 100644 index 0000000000..24cef4c1bc --- /dev/null +++ b/corebinutils/sh/tests/builtins/local2.0 @@ -0,0 +1,16 @@ + +f() { + local - + set -a + case $- in + *a*) : ;; + *) echo In-function \$- bad + esac +} +case $- in +*a*) echo Initial \$- bad +esac +f +case $- in +*a*) echo Final \$- bad +esac diff --git a/corebinutils/sh/tests/builtins/local3.0 b/corebinutils/sh/tests/builtins/local3.0 new file mode 100644 index 0000000000..e947b7c495 --- /dev/null +++ b/corebinutils/sh/tests/builtins/local3.0 @@ -0,0 +1,25 @@ + +f() { + local "$@" + set -a + x=7 + case $- in + *a*) : ;; + *) echo In-function \$- bad + esac + [ "$x" = 7 ] || echo In-function \$x bad +} +x=1 +case $- in +*a*) echo Initial \$- bad +esac +f x - +case $- in +*a*) echo Intermediate \$- bad +esac +[ "$x" = 1 ] || echo Intermediate \$x bad +f - x +case $- in +*a*) echo Final \$- bad +esac +[ "$x" = 1 ] || echo Final \$x bad diff --git a/corebinutils/sh/tests/builtins/local4.0 b/corebinutils/sh/tests/builtins/local4.0 new file mode 100644 index 0000000000..bd5a576519 --- /dev/null +++ b/corebinutils/sh/tests/builtins/local4.0 @@ -0,0 +1,11 @@ + +f() { + local -- x + x=2 + [ "$x" = 2 ] +} +x=1 +f || exit 3 +[ "$x" = 1 ] || exit 3 +f || exit 3 +[ "$x" = 1 ] || exit 3 diff --git a/corebinutils/sh/tests/builtins/local5.0 b/corebinutils/sh/tests/builtins/local5.0 new file mode 100644 index 0000000000..82b4014e8e --- /dev/null +++ b/corebinutils/sh/tests/builtins/local5.0 @@ -0,0 +1,14 @@ + +f() { + local PATH IFS elem + IFS=: + for elem in ''$PATH''; do + PATH=/var/empty/$elem:$PATH + done + ls -d / >/dev/null +} + +p1=$(command -v ls) +f +p2=$(command -v ls) +[ "$p1" = "$p2" ] diff --git a/corebinutils/sh/tests/builtins/local6.0 b/corebinutils/sh/tests/builtins/local6.0 new file mode 100644 index 0000000000..f179fe00f9 --- /dev/null +++ b/corebinutils/sh/tests/builtins/local6.0 @@ -0,0 +1,9 @@ + +f() { + local x + readonly x=2 +} +x=3 +f +x=4 +[ "$x" = 4 ] diff --git a/corebinutils/sh/tests/builtins/local7.0 b/corebinutils/sh/tests/builtins/local7.0 new file mode 100644 index 0000000000..591606d9bd --- /dev/null +++ b/corebinutils/sh/tests/builtins/local7.0 @@ -0,0 +1,9 @@ + +f() { + local x + readonly x=2 +} +unset x +f +x=4 +[ "$x" = 4 ] diff --git a/corebinutils/sh/tests/builtins/locale1.0 b/corebinutils/sh/tests/builtins/locale1.0 new file mode 100644 index 0000000000..c5357a7996 --- /dev/null +++ b/corebinutils/sh/tests/builtins/locale1.0 @@ -0,0 +1,146 @@ +# Note: strerror() translations depend on libc locale data. The shell should +# honor locale assignments, but translated messages may be unavailable on the +# host (especially on musl), so translation-specific checks are conditional. + +failures=0 + +check() { + if ! eval "[ $1 ]"; then + echo "Failed: $1 at $2" + : $((failures += 1)) + fi +} + +unset LANG LC_ALL LC_COLLATE LC_CTYPE LC_MONETARY LC_NUMERIC LC_TIME LC_MESSAGES +unset LANGUAGE + +msgeng="No such file or directory" +msgdut="Bestand of map niet gevonden" +missing=$PWD/does-not-exist + +run_missing() { + command . "$missing" 2>&1 +} + +# Verify C locale error message. +case $(run_missing) in + *"$msgeng"*) ok=1 ;; + *) ok=0 ;; +esac +check '$ok -eq 1' $LINENO + +# Various locale variables that should not affect the message. +case $(LC_ALL=C run_missing) in + *"$msgeng"*) ok=1 ;; + *) ok=0 ;; +esac +check '$ok -eq 1' $LINENO + +case $(LC_ALL=C LANG=nl_NL.ISO8859-1 run_missing) in + *"$msgeng"*) ok=1 ;; + *) ok=0 ;; +esac +check '$ok -eq 1' $LINENO + +case $(LC_ALL=C LC_MESSAGES=nl_NL.ISO8859-1 run_missing) in + *"$msgeng"*) ok=1 ;; + *) ok=0 ;; +esac +check '$ok -eq 1' $LINENO + +case $(LC_CTYPE=nl_NL.ISO8859-1 run_missing) in + *"$msgeng"*) ok=1 ;; + *) ok=0 ;; +esac +check '$ok -eq 1' $LINENO + +case $(export LC_ALL=nl_NL.ISO8859-1; run_missing) in + *"$msgdut"*) have_dutch=1 ;; + *) have_dutch=0 ;; +esac + +# Verify that command assignments do not set the locale persistently. +case $(run_missing) in + *"$msgeng"*) ok=1 ;; + *) ok=0 ;; +esac +check '$ok -eq 1' $LINENO + +if [ "$have_dutch" -eq 1 ]; then + case $(export LANG=nl_NL.ISO8859-1; run_missing) in + *"$msgdut"*) ok=1 ;; + *) ok=0 ;; + esac + check '$ok -eq 1' $LINENO + + case $(export LC_MESSAGES=nl_NL.ISO8859-1; run_missing) in + *"$msgdut"*) ok=1 ;; + *) ok=0 ;; + esac + check '$ok -eq 1' $LINENO + + case $(export LC_ALL=nl_NL.ISO8859-1; run_missing) in + *"$msgdut"*) ok=1 ;; + *) ok=0 ;; + esac + check '$ok -eq 1' $LINENO + + case $(LANG=nl_NL.ISO8859-1 run_missing) in + *"$msgdut"*) ok=1 ;; + *) ok=0 ;; + esac + check '$ok -eq 1' $LINENO + + case $(LC_MESSAGES=nl_NL.ISO8859-1 run_missing) in + *"$msgdut"*) ok=1 ;; + *) ok=0 ;; + esac + check '$ok -eq 1' $LINENO + + case $(LC_ALL=nl_NL.ISO8859-1 run_missing) in + *"$msgdut"*) ok=1 ;; + *) ok=0 ;; + esac + check '$ok -eq 1' $LINENO + + case $(LANG=nl_NL.ISO8859-1 run_missing; run_missing) in + *"$msgdut"*"$msgeng"*) ok=1 ;; + *) ok=0 ;; + esac + check '$ok -eq 1' $LINENO + + case $(LC_MESSAGES=nl_NL.ISO8859-1 run_missing; run_missing) in + *"$msgdut"*"$msgeng"*) ok=1 ;; + *) ok=0 ;; + esac + check '$ok -eq 1' $LINENO + + case $(LC_ALL=nl_NL.ISO8859-1 run_missing; run_missing) in + *"$msgdut"*"$msgeng"*) ok=1 ;; + *) ok=0 ;; + esac + check '$ok -eq 1' $LINENO + + # Check special builtin; add colon invocation to avoid depending on certain fix. + case $(LC_ALL=nl_NL.ISO8859-1 . "$missing" 2>&1; :) in + *"$msgdut"*) ok=1 ;; + *) ok=0 ;; + esac + check '$ok -eq 1' $LINENO + + # Assignments on special builtins are exported to that builtin; the export + # is not persistent. + case $(LC_ALL=nl_NL.ISO8859-1 . /dev/null; run_missing) in + *"$msgeng"*) ok=1 ;; + *) ok=0 ;; + esac + check '$ok -eq 1' $LINENO + + case $(export LC_ALL; LC_ALL=nl_NL.ISO8859-1 . /dev/null; run_missing) in + *"$msgdut"*) ok=1 ;; + *) ok=0 ;; + esac + check '$ok -eq 1' $LINENO +fi + +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/builtins/locale2.0 b/corebinutils/sh/tests/builtins/locale2.0 new file mode 100644 index 0000000000..050d7fc9fb --- /dev/null +++ b/corebinutils/sh/tests/builtins/locale2.0 @@ -0,0 +1,4 @@ + +$SH -c 'LC_ALL=C true; kill -INT $$; echo continued' +r=$? +[ "$r" -gt 128 ] && [ "$(kill -l "$r")" = INT ] diff --git a/corebinutils/sh/tests/builtins/printf1.0 b/corebinutils/sh/tests/builtins/printf1.0 new file mode 100644 index 0000000000..1dfbf7a9be --- /dev/null +++ b/corebinutils/sh/tests/builtins/printf1.0 @@ -0,0 +1,2 @@ + +[ "$(printf '%c\0%s%d' x '\' 010 | tr '\0' Z)" = 'xZ\8' ] diff --git a/corebinutils/sh/tests/builtins/printf2.0 b/corebinutils/sh/tests/builtins/printf2.0 new file mode 100644 index 0000000000..8376505c9d --- /dev/null +++ b/corebinutils/sh/tests/builtins/printf2.0 @@ -0,0 +1,2 @@ + +[ "$(printf '%cZ%s%d' x '\' 010)" = 'xZ\8' ] diff --git a/corebinutils/sh/tests/builtins/printf3.0 b/corebinutils/sh/tests/builtins/printf3.0 new file mode 100644 index 0000000000..b431cc6d3d --- /dev/null +++ b/corebinutils/sh/tests/builtins/printf3.0 @@ -0,0 +1,4 @@ + +set -e +v=$(! printf "%d" @wrong 2>/dev/null) +[ "$v" = "0" ] diff --git a/corebinutils/sh/tests/builtins/printf4.0 b/corebinutils/sh/tests/builtins/printf4.0 new file mode 100644 index 0000000000..15d57f7827 --- /dev/null +++ b/corebinutils/sh/tests/builtins/printf4.0 @@ -0,0 +1,4 @@ + +set -e +v=$(! printf "%d" 4wrong 2>/dev/null) +[ "$v" = "4" ] diff --git a/corebinutils/sh/tests/builtins/read1.0 b/corebinutils/sh/tests/builtins/read1.0 new file mode 100644 index 0000000000..b1617c7f48 --- /dev/null +++ b/corebinutils/sh/tests/builtins/read1.0 @@ -0,0 +1,25 @@ +set -e + +echo "1 2 3" | { read a; echo "x${a}x"; } +echo "1 2 3" | { read a b; echo "x${a}x${b}x"; } +echo "1 2 3" | { read a b c; echo "x${a}x${b}x${c}x"; } +echo "1 2 3" | { read a b c d; echo "x${a}x${b}x${c}x${d}x"; } + +echo " 1 2 3 " | { read a b c; echo "x${a}x${b}x${c}x"; } +echo " 1 2 3 " | { unset IFS; read a b c; echo "x${a}x${b}x${c}x"; } +echo " 1 2 3 " | { IFS=$(printf ' \t\n') read a b c; echo "x${a}x${b}x${c}x"; } +echo " 1 2 3 " | { IFS= read a b; echo "x${a}x${b}x"; } + +echo " 1,2 3 " | { IFS=' ,' read a b c; echo "x${a}x${b}x${c}x"; } +echo ", 2 ,3" | { IFS=' ,' read a b c; echo "x${a}x${b}x${c}x"; } +echo " 1 ,,3" | { IFS=' ,' read a b c; echo "x${a}x${b}x${c}x"; } +echo " 1 , , 3" | { IFS=' ,' read a b c; echo "x${a}x${b}x${c}x"; } +echo " 1 ,2 3," | { IFS=' ,' read a b c; echo "x${a}x${b}x${c}x"; } +echo " 1 ,2 3,," | { IFS=' ,' read a b c; echo "x${a}x${b}x${c}x"; } + +echo " 1,2 3 " | { IFS=', ' read a b c; echo "x${a}x${b}x${c}x"; } +echo ", 2 ,3" | { IFS=', ' read a b c; echo "x${a}x${b}x${c}x"; } +echo " 1 ,,3" | { IFS=', ' read a b c; echo "x${a}x${b}x${c}x"; } +echo " 1 , , 3" | { IFS=', ' read a b c; echo "x${a}x${b}x${c}x"; } +echo " 1 ,2 3," | { IFS=', ' read a b c; echo "x${a}x${b}x${c}x"; } +echo " 1 ,2 3,," | { IFS=', ' read a b c; echo "x${a}x${b}x${c}x"; } diff --git a/corebinutils/sh/tests/builtins/read1.0.stdout b/corebinutils/sh/tests/builtins/read1.0.stdout new file mode 100644 index 0000000000..dbcb1af9ba --- /dev/null +++ b/corebinutils/sh/tests/builtins/read1.0.stdout @@ -0,0 +1,20 @@ +x1 2 3x +x1x2 3x +x1x2x3x +x1x2x3xx +x1x2x3x +x1x2x3x +x1x2x3x +x 1 2 3 xx +x1x2x3x +xx2x3x +x1xx3x +x1xx3x +x1x2x3x +x1x2x3,,x +x1x2x3x +xx2x3x +x1xx3x +x1xx3x +x1x2x3x +x1x2x3,,x diff --git a/corebinutils/sh/tests/builtins/read10.0 b/corebinutils/sh/tests/builtins/read10.0 new file mode 100644 index 0000000000..5fc1989639 --- /dev/null +++ b/corebinutils/sh/tests/builtins/read10.0 @@ -0,0 +1,8 @@ + +set -e + +v=original_value +r=0 +read v < /dev/null || r=$? +[ "$r" -eq 1 ] +[ -z "$v" ] diff --git a/corebinutils/sh/tests/builtins/read11.0 b/corebinutils/sh/tests/builtins/read11.0 new file mode 100644 index 0000000000..07bd3e7064 --- /dev/null +++ b/corebinutils/sh/tests/builtins/read11.0 @@ -0,0 +1,32 @@ +# Verify that `read -t 0 v` succeeds immediately if input is available +# and fails immediately if not + +set -e + +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) +trap 'rm -rf "$T"' 0 +cd $T +mkfifo fifo1 +# Open fifo1 for writing +{ echo new_value; sleep 10; } >fifo1 & +# Wait for the child to open fifo1 for writing +exec 3<fifo1 + +v=original_value +r=0 +ts=$(date +%s%3N) +read -t 0 v <&3 || r=$? +te=$(date +%s%3N) +[ "$r" -eq 0 ] +[ $((te-ts)) -lt 250 ] +[ "$v" = "new_value" ] + +v=original_value +r=0 +ts=$(date +%s%3N) +read -t 0 v <&3 || r=$? +te=$(date +%s%3N) +kill -TERM "$!" || : +[ "$r" -gt 128 ] && [ "$(kill -l "$r")" = ALRM ] +[ $((te-ts)) -lt 250 ] +[ -z "$v" ] diff --git a/corebinutils/sh/tests/builtins/read12.0 b/corebinutils/sh/tests/builtins/read12.0 new file mode 100644 index 0000000000..4551555adf --- /dev/null +++ b/corebinutils/sh/tests/builtins/read12.0 @@ -0,0 +1,32 @@ +# Verify that `read -t 3 v` succeeds immediately if input is available +# and times out after 3 s if not + +set -e + +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) +trap 'rm -rf "$T"' 0 +cd $T +mkfifo fifo1 +# Open fifo1 for writing +{ echo new_value; sleep 10; } >fifo1 & +# Wait for the child to open fifo1 for writing +exec 3<fifo1 + +v=original_value +r=0 +ts=$(date +%s%3N) +read -t 3 v <&3 || r=$? +te=$(date +%s%3N) +[ "$r" -eq 0 ] +[ $((te-ts)) -lt 250 ] +[ "$v" = "new_value" ] + +v=original_value +r=0 +ts=$(date +%s%3N) +read -t 3 v <&3 || r=$? +te=$(date +%s%3N) +kill -TERM "$!" || : +[ "$r" -gt 128 ] && [ "$(kill -l "$r")" = ALRM ] +[ $((te-ts)) -gt 3000 ] && [ $((te-ts)) -lt 3250 ] +[ -z "$v" ] diff --git a/corebinutils/sh/tests/builtins/read2.0 b/corebinutils/sh/tests/builtins/read2.0 new file mode 100644 index 0000000000..da7c48553a --- /dev/null +++ b/corebinutils/sh/tests/builtins/read2.0 @@ -0,0 +1,30 @@ + +set -e +{ + echo 1 + echo two + echo three +} | { + read x + [ "$x" = 1 ] + (read x + [ "$x" = two ]) + read x + [ "$x" = three ] +} + +T=`mktemp sh-test.XXXXXX` +trap 'rm -f "$T"' 0 +{ + echo 1 + echo two + echo three +} >$T +{ + read x + [ "$x" = 1 ] + (read x + [ "$x" = two ]) + read x + [ "$x" = three ] +} <$T diff --git a/corebinutils/sh/tests/builtins/read3.0 b/corebinutils/sh/tests/builtins/read3.0 new file mode 100644 index 0000000000..3674ec9f40 --- /dev/null +++ b/corebinutils/sh/tests/builtins/read3.0 @@ -0,0 +1,10 @@ + +printf '%s\n' 'a\ b c' | { read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' 'a b\ c' | { read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' 'a\:b:c' | { IFS=: read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' 'a:b\:c' | { IFS=: read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' '\ a' | { read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' '\:a' | { IFS=: read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' '\\' | { read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' '\\\ a' | { read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' '\\\ a' | { read -r a b; printf '%s\n' "x${a}x${b}x"; } diff --git a/corebinutils/sh/tests/builtins/read3.0.stdout b/corebinutils/sh/tests/builtins/read3.0.stdout new file mode 100644 index 0000000000..8ed98ca9d8 --- /dev/null +++ b/corebinutils/sh/tests/builtins/read3.0.stdout @@ -0,0 +1,9 @@ +xa bxcx +xaxb cx +xa:bxcx +xaxb:cx +x axx +x:axx +x\xx +x\ axx +x\\\xax diff --git a/corebinutils/sh/tests/builtins/read4.0 b/corebinutils/sh/tests/builtins/read4.0 new file mode 100644 index 0000000000..4856feddb5 --- /dev/null +++ b/corebinutils/sh/tests/builtins/read4.0 @@ -0,0 +1,9 @@ + +printf '%s\n' '\a\ b c' | { read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' '\a b\ c' | { read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' '\a\:b:c' | { IFS=: read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' '\a:b\:c' | { IFS=: read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' '\\ a' | { read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' '\\:a' | { IFS=: read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' '\\\ a' | { read a b; printf '%s\n' "x${a}x${b}x"; } +printf '%s\n' '\\\:a' | { IFS=: read a b; printf '%s\n' "x${a}x${b}x"; } diff --git a/corebinutils/sh/tests/builtins/read4.0.stdout b/corebinutils/sh/tests/builtins/read4.0.stdout new file mode 100644 index 0000000000..a8747a46ff --- /dev/null +++ b/corebinutils/sh/tests/builtins/read4.0.stdout @@ -0,0 +1,8 @@ +xa bxcx +xaxb cx +xa:bxcx +xaxb:cx +x\xax +x\xax +x\ axx +x\:axx diff --git a/corebinutils/sh/tests/builtins/read5.0 b/corebinutils/sh/tests/builtins/read5.0 new file mode 100644 index 0000000000..a61d1258d4 --- /dev/null +++ b/corebinutils/sh/tests/builtins/read5.0 @@ -0,0 +1,31 @@ + +unset LC_CTYPE LC_COLLATE LANG LANGUAGE +LC_ALL=C +export LC_ALL + +# Note: the first and last characters are not whitespace. +# Exclude backslash and newline. +bad1=`printf %03o \'\\\\` +bad2=`printf %03o \'' +'` +e= +for i in 0 1 2 3; do + for j in 0 1 2 3 4 5 6 7; do + for k in 0 1 2 3 4 5 6 7; do + case $i$j$k in + 000|$bad1|$bad2) continue ;; + esac + e="$e\\$i$j$k" + done + done +done +e=`printf "$e"` +[ "${#e}" = 253 ] || echo length bad + +r1=`printf '%s\n' "$e" | { read -r x; printf '%s' "$x"; }` +[ "$r1" = "$e" ] || echo "read with -r bad" +r2=`printf '%s\n' "$e" | { read x; printf '%s' "$x"; }` +[ "$r2" = "$e" ] || echo "read without -r bad 1" +IFS= +r3=`printf '%s\n' "$e" | { read x; printf '%s' "$x"; }` +[ "$r3" = "$e" ] || echo "read without -r bad 2" diff --git a/corebinutils/sh/tests/builtins/read6.0 b/corebinutils/sh/tests/builtins/read6.0 new file mode 100644 index 0000000000..60df35f7ee --- /dev/null +++ b/corebinutils/sh/tests/builtins/read6.0 @@ -0,0 +1,4 @@ + +: | read x +r=$? +[ "$r" = 1 ] diff --git a/corebinutils/sh/tests/builtins/read7.0 b/corebinutils/sh/tests/builtins/read7.0 new file mode 100644 index 0000000000..1e159dca61 --- /dev/null +++ b/corebinutils/sh/tests/builtins/read7.0 @@ -0,0 +1,4 @@ + +{ errmsg=`read x <&- 2>&1 >&3`; } 3>&1 +r=$? +[ "$r" -ge 2 ] && [ "$r" -le 128 ] && [ -n "$errmsg" ] diff --git a/corebinutils/sh/tests/builtins/read8.0 b/corebinutils/sh/tests/builtins/read8.0 new file mode 100644 index 0000000000..3de0f72c46 --- /dev/null +++ b/corebinutils/sh/tests/builtins/read8.0 @@ -0,0 +1,16 @@ + +read a b c <<\EOF +\ +A\ + \ + \ + \ +B\ + \ + \ +C\ + \ + \ + \ +EOF +[ "$a.$b.$c" = "A.B.C" ] diff --git a/corebinutils/sh/tests/builtins/read9.0 b/corebinutils/sh/tests/builtins/read9.0 new file mode 100644 index 0000000000..23d814934d --- /dev/null +++ b/corebinutils/sh/tests/builtins/read9.0 @@ -0,0 +1,9 @@ + +empty='' +read a b c <<EOF +\ \ A B\ \ B C\ \ $empty +EOF +read d e <<EOF +D\ $empty +EOF +[ "$a.$b.$c.$d.$e" = " A.B B.C .D ." ] diff --git a/corebinutils/sh/tests/builtins/return1.0 b/corebinutils/sh/tests/builtins/return1.0 new file mode 100644 index 0000000000..b3820b4597 --- /dev/null +++ b/corebinutils/sh/tests/builtins/return1.0 @@ -0,0 +1,6 @@ +f() { + return 0 + exit 1 +} + +f diff --git a/corebinutils/sh/tests/builtins/return2.1 b/corebinutils/sh/tests/builtins/return2.1 new file mode 100644 index 0000000000..8cd5ca58a4 --- /dev/null +++ b/corebinutils/sh/tests/builtins/return2.1 @@ -0,0 +1,6 @@ +f() { + true && return 1 + return 0 +} + +f diff --git a/corebinutils/sh/tests/builtins/return3.1 b/corebinutils/sh/tests/builtins/return3.1 new file mode 100644 index 0000000000..e871c055af --- /dev/null +++ b/corebinutils/sh/tests/builtins/return3.1 @@ -0,0 +1,2 @@ +return 1 +exit 0 diff --git a/corebinutils/sh/tests/builtins/return4.0 b/corebinutils/sh/tests/builtins/return4.0 new file mode 100644 index 0000000000..a2edab53c2 --- /dev/null +++ b/corebinutils/sh/tests/builtins/return4.0 @@ -0,0 +1,15 @@ + +failures= +failure() { + echo "Error at line $1" >&2 + failures=x$failures +} + +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) || exit +trap 'rm -rf $T' 0 +cd $T || exit 3 +echo 'return 42; exit 4' >testscript +. ./testscript +[ "$?" = 42 ] || failure $LINENO + +test -z "$failures" diff --git a/corebinutils/sh/tests/builtins/return5.0 b/corebinutils/sh/tests/builtins/return5.0 new file mode 100644 index 0000000000..46243b6b20 --- /dev/null +++ b/corebinutils/sh/tests/builtins/return5.0 @@ -0,0 +1,16 @@ + +if [ "$1" != nested ]; then + f() { + set -- nested + . "$0" + # Allow return to return from the function or the dot script. + return 4 + } + f + exit $(($? ^ 4)) +fi +# To trigger the bug, the following commands must be at the top level, +# with newlines in between. +return 4 +echo bad +exit 1 diff --git a/corebinutils/sh/tests/builtins/return6.4 b/corebinutils/sh/tests/builtins/return6.4 new file mode 100644 index 0000000000..172459f40c --- /dev/null +++ b/corebinutils/sh/tests/builtins/return6.4 @@ -0,0 +1,2 @@ + +while return 4; do exit 3; done diff --git a/corebinutils/sh/tests/builtins/return7.4 b/corebinutils/sh/tests/builtins/return7.4 new file mode 100644 index 0000000000..9f914d70e9 --- /dev/null +++ b/corebinutils/sh/tests/builtins/return7.4 @@ -0,0 +1,5 @@ + +f() { + while return 4; do exit 3; done +} +f diff --git a/corebinutils/sh/tests/builtins/return8.0 b/corebinutils/sh/tests/builtins/return8.0 new file mode 100644 index 0000000000..c9f5b0a03c --- /dev/null +++ b/corebinutils/sh/tests/builtins/return8.0 @@ -0,0 +1,12 @@ + +if [ "$1" = nested ]; then + return 17 +fi + +f() { + set -- nested + . "$0" + return $(($? ^ 1)) +} +f +exit $(($? ^ 16)) diff --git a/corebinutils/sh/tests/builtins/set1.0 b/corebinutils/sh/tests/builtins/set1.0 new file mode 100644 index 0000000000..3f311fc028 --- /dev/null +++ b/corebinutils/sh/tests/builtins/set1.0 @@ -0,0 +1,31 @@ + +set +C +set +f +set -e + +settings=$(set +o) +set -C +set -f +set +e +case $- in +*C*) ;; +*) echo missing C ;; +esac +case $- in +*f*) ;; +*) echo missing C ;; +esac +case $- in +*e*) echo bad e ;; +esac +eval "$settings" +case $- in +*C*) echo bad C ;; +esac +case $- in +*f*) echo bad f ;; +esac +case $- in +*e*) ;; +*) echo missing e ;; +esac diff --git a/corebinutils/sh/tests/builtins/set2.0 b/corebinutils/sh/tests/builtins/set2.0 new file mode 100644 index 0000000000..f959cf797f --- /dev/null +++ b/corebinutils/sh/tests/builtins/set2.0 @@ -0,0 +1,2 @@ + +! env @badness=1 ${SH} -c 'v=`set`; eval "$v"' 2>&1 | grep @badness diff --git a/corebinutils/sh/tests/builtins/set3.0 b/corebinutils/sh/tests/builtins/set3.0 new file mode 100644 index 0000000000..44255adc4b --- /dev/null +++ b/corebinutils/sh/tests/builtins/set3.0 @@ -0,0 +1,3 @@ + +settings1=$(set +o) && set -o nolog && settings2=$(set +o) && +[ "$settings1" != "$settings2" ] diff --git a/corebinutils/sh/tests/builtins/trap1.0 b/corebinutils/sh/tests/builtins/trap1.0 new file mode 100644 index 0000000000..7f39faba31 --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap1.0 @@ -0,0 +1,21 @@ + +test "$(trap 'echo trapped' EXIT; :)" = trapped || exit 1 + +test "$(trap 'echo trapped' EXIT; /usr/bin/true)" = trapped || exit 1 + +result=$(${SH} -c 'trap "echo trapped" EXIT; /usr/bin/false') +test $? -eq 1 || exit 1 +test "$result" = trapped || exit 1 + +result=$(${SH} -c 'trap "echo trapped" EXIT; exec /usr/bin/false') +test $? -eq 1 || exit 1 +test -z "$result" || exit 1 + +result=0 +trap 'result=$((result+1))' INT +kill -INT $$ +test "$result" -eq 1 || exit 1 +(kill -INT $$) +test "$result" -eq 2 || exit 1 + +exit 0 diff --git a/corebinutils/sh/tests/builtins/trap10.0 b/corebinutils/sh/tests/builtins/trap10.0 new file mode 100644 index 0000000000..96baa65322 --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap10.0 @@ -0,0 +1,5 @@ + +# Check that the return statement will not break the EXIT trap, ie. all +# trap commands are executed before the script exits. + +test "$(trap 'printf trap; echo ped' EXIT; f() { return; }; f)" = trapped || exit 1 diff --git a/corebinutils/sh/tests/builtins/trap11.0 b/corebinutils/sh/tests/builtins/trap11.0 new file mode 100644 index 0000000000..354698d90e --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap11.0 @@ -0,0 +1,7 @@ + +# Check that the return statement will not break the USR1 trap, ie. all +# trap commands are executed before the script resumes. + +result=$(${SH} -c 'trap "printf trap; echo ped" USR1; f() { return $(kill -USR1 $$); }; f') +test $? -eq 0 || exit 1 +test "$result" = trapped || exit 1 diff --git a/corebinutils/sh/tests/builtins/trap12.0 b/corebinutils/sh/tests/builtins/trap12.0 new file mode 100644 index 0000000000..87592af250 --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap12.0 @@ -0,0 +1,9 @@ + +f() { + trap 'return 42' USR1 + kill -USR1 $$ + return 3 +} +f +r=$? +[ "$r" = 42 ] diff --git a/corebinutils/sh/tests/builtins/trap13.0 b/corebinutils/sh/tests/builtins/trap13.0 new file mode 100644 index 0000000000..81db991a49 --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap13.0 @@ -0,0 +1,7 @@ + +{ + trap 'exit 0' INT + ${SH} -c 'kill -INT $PPID' + exit 3 +} & +wait $! diff --git a/corebinutils/sh/tests/builtins/trap14.0 b/corebinutils/sh/tests/builtins/trap14.0 new file mode 100644 index 0000000000..cf22b15b46 --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap14.0 @@ -0,0 +1,9 @@ + +{ + trap - INT + ${SH} -c 'kill -INT $PPID' & + wait +} & +wait $! +r=$? +[ "$r" -gt 128 ] && [ "$(kill -l "$r")" = INT ] diff --git a/corebinutils/sh/tests/builtins/trap15.0 b/corebinutils/sh/tests/builtins/trap15.0 new file mode 100644 index 0000000000..a7e8d6475f --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap15.0 @@ -0,0 +1,4 @@ + +(${SH} -c 'term(){ exit 5;}; trap term TERM; kill -TERM $$') & +wait >/dev/null 2>&1 $! +[ $? -eq 5 ] diff --git a/corebinutils/sh/tests/builtins/trap16.0 b/corebinutils/sh/tests/builtins/trap16.0 new file mode 100644 index 0000000000..23c615ab41 --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap16.0 @@ -0,0 +1,19 @@ + +traps=$(${SH} -c 'trap "echo bad" 0; trap - 0; trap') +[ -z "$traps" ] || exit 1 +traps=$(${SH} -c 'trap "echo bad" 0; trap "" 0; trap') +expected_traps=$(${SH} -c 'trap "" EXIT; trap') +[ "$traps" = "$expected_traps" ] || exit 2 +traps=$(${SH} -c 'trap "echo bad" 0; trap 0; trap') +[ -z "$traps" ] || exit 3 +traps=$(${SH} -c 'trap "echo bad" 0; trap -- 0; trap') +[ -z "$traps" ] || exit 4 +traps=$(${SH} -c 'trap "echo bad" 0 1 2; trap - 0 1 2; trap') +[ -z "$traps" ] || exit 5 +traps=$(${SH} -c 'trap "echo bad" 0 1 2; trap "" 0 1 2; trap') +expected_traps=$(${SH} -c 'trap "" EXIT HUP INT; trap') +[ "$traps" = "$expected_traps" ] || exit 6 +traps=$(${SH} -c 'trap "echo bad" 0 1 2; trap 0 1 2; trap') +[ -z "$traps" ] || exit 7 +traps=$(${SH} -c 'trap "echo bad" 0 1 2; trap -- 0 1 2; trap') +[ -z "$traps" ] || exit 8 diff --git a/corebinutils/sh/tests/builtins/trap17.0 b/corebinutils/sh/tests/builtins/trap17.0 new file mode 100644 index 0000000000..580c41fc24 --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap17.0 @@ -0,0 +1,9 @@ +# This use-after-free bug probably needs non-default settings to show up. + +v1=nothing v2=nothing +trap 'trap "echo bad" USR1 +v1=trap_received +v2=trap_invoked +:' USR1 +kill -USR1 "$$" +[ "$v1.$v2" = trap_received.trap_invoked ] diff --git a/corebinutils/sh/tests/builtins/trap2.0 b/corebinutils/sh/tests/builtins/trap2.0 new file mode 100644 index 0000000000..d2ea059994 --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap2.0 @@ -0,0 +1,51 @@ +# This is really a test for outqstr(), which is readily accessible via trap. + +runtest() +{ + teststring=$1 + trap -- "$teststring" USR1 + traps=$(trap) + if [ "$teststring" != "-" ] && [ -z "$traps" ]; then + # One possible reading of POSIX requires the above to return an + # empty string because backquote commands are executed in a + # subshell and subshells shall reset traps. However, an example + # in the normative description of the trap builtin shows the + # same usage as here, it is useful and our /bin/sh allows it. + echo '$(trap) is broken' + exit 1 + fi + trap - USR1 + eval "$traps" + traps2=$(trap) + if [ "$traps" != "$traps2" ]; then + echo "Mismatch for '$teststring'" + exit 1 + fi +} + +runtest 'echo' +runtest 'echo hi' +runtest "'echo' 'hi'" +runtest '"echo" $PATH' +runtest '\echo "$PATH"' +runtest ' 0' +runtest '0 ' +runtest ' 1' +runtest '1 ' +i=1 +while [ $i -le 127 ]; do + c=$(printf \\"$(printf %o $i)") + if [ $i -lt 48 ] || [ $i -gt 57 ]; then + runtest "$c" + fi + runtest " $c$c" + runtest "a$c" + i=$((i+1)) +done +IFS=, +runtest ' ' +runtest ',' +unset IFS +runtest ' ' + +exit 0 diff --git a/corebinutils/sh/tests/builtins/trap3.0 b/corebinutils/sh/tests/builtins/trap3.0 new file mode 100644 index 0000000000..5f6bd73654 --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap3.0 @@ -0,0 +1,10 @@ + +{ + trap '' garbage && exit 3 + trap - garbage && exit 3 + trap true garbage && exit 3 + trap '' 99999 && exit 3 + trap - 99999 && exit 3 + trap true 99999 && exit 3 +} 2>/dev/null +exit 0 diff --git a/corebinutils/sh/tests/builtins/trap4.0 b/corebinutils/sh/tests/builtins/trap4.0 new file mode 100644 index 0000000000..52ca5c7773 --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap4.0 @@ -0,0 +1,16 @@ + +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) +trap 'rm -rf $T' 0 +cd $T || exit 3 +mkfifo fifo1 + +v=$( + exec 3>&1 + : <fifo1 & + { + wait $! + trap 'trap "" PIPE; echo trapped >&3 2>/dev/null' PIPE + echo x 2>/dev/null + } >fifo1 +) +test "$v" = trapped diff --git a/corebinutils/sh/tests/builtins/trap5.0 b/corebinutils/sh/tests/builtins/trap5.0 new file mode 100644 index 0000000000..7dd9bffa1f --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap5.0 @@ -0,0 +1,18 @@ + +set -e +trap - USR1 +initial=$(trap) +trap -- -l USR1 +added=$(trap) +[ -n "$added" ] +trap - USR1 +second=$(trap) +[ "$initial" = "$second" ] +eval "$added" +added2=$(trap) +added3=$(trap --) +[ "$added" = "$added2" ] +[ "$added2" = "$added3" ] +trap -- - USR1 +third=$(trap) +[ "$initial" = "$third" ] diff --git a/corebinutils/sh/tests/builtins/trap6.0 b/corebinutils/sh/tests/builtins/trap6.0 new file mode 100644 index 0000000000..aca7e4c633 --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap6.0 @@ -0,0 +1,8 @@ + +v=$( + ${SH} -c 'trap "echo ok; exit" USR1; kill -USR1 $$' & + # Suppress possible message about exit on signal + wait $! >/dev/null 2>&1 +) +r=$(kill -l $?) +[ "$v" = "ok" ] && { [ "$r" = "USR1" ] || [ "$r" = "usr1" ]; } diff --git a/corebinutils/sh/tests/builtins/trap7.0 b/corebinutils/sh/tests/builtins/trap7.0 new file mode 100644 index 0000000000..0f20855101 --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap7.0 @@ -0,0 +1,2 @@ + +[ "$(trap 'echo trapped' EXIT)" = trapped ] diff --git a/corebinutils/sh/tests/builtins/trap8.0 b/corebinutils/sh/tests/builtins/trap8.0 new file mode 100644 index 0000000000..b04f53079e --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap8.0 @@ -0,0 +1,6 @@ + +# I am not sure if POSIX requires the shell to continue processing +# further trap names in the same trap command after an invalid one. + +test -n "$(trap true garbage TERM 2>/dev/null || trap)" || exit 3 +exit 0 diff --git a/corebinutils/sh/tests/builtins/trap9.0 b/corebinutils/sh/tests/builtins/trap9.0 new file mode 100644 index 0000000000..187249bf7e --- /dev/null +++ b/corebinutils/sh/tests/builtins/trap9.0 @@ -0,0 +1,2 @@ + +test "$(trap 'printf trap; echo ped' EXIT; f() { :; }; f)" = trapped || exit 1 diff --git a/corebinutils/sh/tests/builtins/type1.0 b/corebinutils/sh/tests/builtins/type1.0 new file mode 100644 index 0000000000..35c9f7bed2 --- /dev/null +++ b/corebinutils/sh/tests/builtins/type1.0 @@ -0,0 +1,7 @@ +command -v not-here && exit 1 +command -v /not-here && exit 1 +command -V not-here && exit 1 +command -V /not-here && exit 1 +type not-here && exit 1 +type /not-here && exit 1 +exit 0 diff --git a/corebinutils/sh/tests/builtins/type1.0.stderr b/corebinutils/sh/tests/builtins/type1.0.stderr new file mode 100644 index 0000000000..7853418a48 --- /dev/null +++ b/corebinutils/sh/tests/builtins/type1.0.stderr @@ -0,0 +1,4 @@ +not-here: not found +/not-here: No such file or directory +not-here: not found +/not-here: No such file or directory diff --git a/corebinutils/sh/tests/builtins/type2.0 b/corebinutils/sh/tests/builtins/type2.0 new file mode 100644 index 0000000000..1b9ec5daf6 --- /dev/null +++ b/corebinutils/sh/tests/builtins/type2.0 @@ -0,0 +1,34 @@ + +failures=0 + +check() { + if ! eval "$*"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +mkdir pathbin +cat >pathbin/typeprobe <<'EOF' +#!/bin/sh +exit 0 +EOF +chmod +x pathbin/typeprobe + +probe_dir=$PWD/pathbin + +check "PATH=$probe_dir type typeprobe >/dev/null" +check "! PATH=$probe_dir type ls 2>/dev/null" + +PATH=$probe_dir:$PATH + +check 'type typeprobe >/dev/null' + +PATH=$probe_dir + +check 'type typeprobe >/dev/null' +check '! type ls 2>/dev/null' +check 'PATH=/bin type ls >/dev/null' +check '! PATH=/bin type typeprobe 2>/dev/null' + +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/builtins/type3.0 b/corebinutils/sh/tests/builtins/type3.0 new file mode 100644 index 0000000000..ccca26b2d7 --- /dev/null +++ b/corebinutils/sh/tests/builtins/type3.0 @@ -0,0 +1,2 @@ + +[ "$(type type)" = "$(type -- type)" ] diff --git a/corebinutils/sh/tests/builtins/type4.0 b/corebinutils/sh/tests/builtins/type4.0 new file mode 100644 index 0000000000..570c67d52c --- /dev/null +++ b/corebinutils/sh/tests/builtins/type4.0 @@ -0,0 +1,8 @@ + +r=`cd /bin && PATH=. type ls` +case $r in +*/bin/ls*|*/bin/./ls*) ;; +*) + echo "Unexpected result: $r" + exit 1 +esac diff --git a/corebinutils/sh/tests/builtins/unalias.0 b/corebinutils/sh/tests/builtins/unalias.0 new file mode 100644 index 0000000000..3e5ae88e54 --- /dev/null +++ b/corebinutils/sh/tests/builtins/unalias.0 @@ -0,0 +1,20 @@ +set -e + +alias false=true +false +unalias false +false && exit 1 +unalias false && exit 1 + +alias a1=foo a2=bar +unalias a1 a2 +unalias a1 && exit 1 +unalias a2 && exit 1 +alias a2=bar +unalias a1 a2 && exit 1 + +alias a1=foo a2=bar +unalias -a +unalias a1 && exit 1 +unalias a2 && exit 1 +exit 0 diff --git a/corebinutils/sh/tests/builtins/var-assign.0 b/corebinutils/sh/tests/builtins/var-assign.0 new file mode 100644 index 0000000000..06b13875e6 --- /dev/null +++ b/corebinutils/sh/tests/builtins/var-assign.0 @@ -0,0 +1,54 @@ +IFS=, + +SPECIAL="break,\ + :,\ + continue,\ + . /dev/null, + eval, + exec, + export -p, + readonly -p, + set, + shift 0, + times, + trap, + unset foo" + +UTILS="alias,\ + bg,\ + bind,\ + cd,\ + command echo,\ + echo,\ + false,\ + fc -l,\ + fg,\ + getopts a var,\ + hash,\ + jobs,\ + printf a,\ + pwd,\ + read var < /dev/null,\ + test,\ + true,\ + type ls,\ + ulimit,\ + umask,\ + unalias -a,\ + wait" + +set -e + +# For special built-ins variable assignments affect the shell environment. +set -- ${SPECIAL} +for cmd in "$@" +do + ${SH} -c "VAR=1; VAR=0 ${cmd}; exit \${VAR}" >/dev/null 2>&1 +done + +# For other built-ins and utilities they do not. +set -- ${UTILS} +for cmd in "$@" +do + ${SH} -c "VAR=0; VAR=1 ${cmd}; exit \${VAR}" >/dev/null 2>&1 +done diff --git a/corebinutils/sh/tests/builtins/var-assign2.0 b/corebinutils/sh/tests/builtins/var-assign2.0 new file mode 100644 index 0000000000..e8b14d3832 --- /dev/null +++ b/corebinutils/sh/tests/builtins/var-assign2.0 @@ -0,0 +1,54 @@ +IFS=, + +SPECIAL="break,\ + :,\ + continue,\ + . /dev/null,\ + eval,\ + exec,\ + export -p,\ + readonly -p,\ + set,\ + shift 0,\ + times,\ + trap,\ + unset foo" + +UTILS="alias,\ + bg,\ + bind,\ + cd,\ + command echo,\ + echo,\ + false,\ + fc -l,\ + fg,\ + getopts a var,\ + hash,\ + jobs,\ + printf a,\ + pwd,\ + read var < /dev/null,\ + test,\ + true,\ + type ls,\ + ulimit,\ + umask,\ + unalias -a,\ + wait" + +set -e + +# With 'command', variable assignments do not affect the shell environment. + +set -- ${SPECIAL} +for cmd in "$@" +do + ${SH} -c "VAR=0; VAR=1 command ${cmd}; exit \${VAR}" >/dev/null 2>&1 +done + +set -- ${UTILS} +for cmd in "$@" +do + ${SH} -c "VAR=0; VAR=1 command ${cmd}; exit \${VAR}" >/dev/null 2>&1 +done diff --git a/corebinutils/sh/tests/builtins/wait1.0 b/corebinutils/sh/tests/builtins/wait1.0 new file mode 100644 index 0000000000..8bd48fefde --- /dev/null +++ b/corebinutils/sh/tests/builtins/wait1.0 @@ -0,0 +1,22 @@ + +failures= +failure() { + echo "Error at line $1" >&2 + failures=x$failures +} + +exit 4 & p4=$! +exit 8 & p8=$! +wait $p4 +[ $? = 4 ] || failure $LINENO +wait $p8 +[ $? = 8 ] || failure $LINENO + +exit 3 & p3=$! +exit 7 & p7=$! +wait $p7 +[ $? = 7 ] || failure $LINENO +wait $p3 +[ $? = 3 ] || failure $LINENO + +test -z "$failures" diff --git a/corebinutils/sh/tests/builtins/wait10.0 b/corebinutils/sh/tests/builtins/wait10.0 new file mode 100644 index 0000000000..e45d087272 --- /dev/null +++ b/corebinutils/sh/tests/builtins/wait10.0 @@ -0,0 +1,4 @@ +# Init cannot be a child of the shell. +exit 49 & p49=$! +wait 1 "$p49" +[ "$?" = 49 ] diff --git a/corebinutils/sh/tests/builtins/wait11.0 b/corebinutils/sh/tests/builtins/wait11.0 new file mode 100644 index 0000000000..d5fab26fb6 --- /dev/null +++ b/corebinutils/sh/tests/builtins/wait11.0 @@ -0,0 +1,6 @@ +sleep 3 | sleep 2 & +sleep 3 & +kill %1 +wait %1 +r=$? +[ "$r" -gt 128 ] && [ "$(kill -l "$r")" = TERM ] diff --git a/corebinutils/sh/tests/builtins/wait2.0 b/corebinutils/sh/tests/builtins/wait2.0 new file mode 100644 index 0000000000..9763257521 --- /dev/null +++ b/corebinutils/sh/tests/builtins/wait2.0 @@ -0,0 +1,14 @@ + +failures= +failure() { + echo "Error at line $1" >&2 + failures=x$failures +} + +for i in 1 2 3 4 5 6 7 8 9 10; do + exit $i & +done +wait || failure $LINENO +wait || failure $LINENO + +test -z "$failures" diff --git a/corebinutils/sh/tests/builtins/wait3.0 b/corebinutils/sh/tests/builtins/wait3.0 new file mode 100644 index 0000000000..4b62c1b897 --- /dev/null +++ b/corebinutils/sh/tests/builtins/wait3.0 @@ -0,0 +1,20 @@ + +failures= +failure() { + echo "Error at line $1" >&2 + failures=x$failures +} + +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) +trap 'rm -rf $T' 0 +cd $T || exit 3 +mkfifo fifo1 +for i in 1 2 3 4 5 6 7 8 9 10; do + exit $i 4<fifo1 & +done +exec 3>fifo1 +wait || failure $LINENO +(${SH} -c echo >&3) 2>/dev/null && failure $LINENO +wait || failure $LINENO + +test -z "$failures" diff --git a/corebinutils/sh/tests/builtins/wait4.0 b/corebinutils/sh/tests/builtins/wait4.0 new file mode 100644 index 0000000000..972a27e3c3 --- /dev/null +++ b/corebinutils/sh/tests/builtins/wait4.0 @@ -0,0 +1,11 @@ + +T=`mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX` +trap 'rm -rf $T' 0 +cd $T || exit 3 +mkfifo fifo1 +trapped= +trap trapped=1 QUIT +{ kill -QUIT $$; sleep 1; exit 4; } >fifo1 & +wait $! <fifo1 +r=$? +[ "$r" -gt 128 ] && [ -n "$trapped" ] diff --git a/corebinutils/sh/tests/builtins/wait5.0 b/corebinutils/sh/tests/builtins/wait5.0 new file mode 100644 index 0000000000..71ecbe5163 --- /dev/null +++ b/corebinutils/sh/tests/builtins/wait5.0 @@ -0,0 +1,11 @@ + +T=`mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX` +trap 'rm -rf $T' 0 +cd $T || exit 3 +mkfifo fifo1 +trapped= +trap trapped=1 QUIT +{ kill -QUIT $$; sleep 1; exit 4; } >fifo1 & +wait <fifo1 +r=$? +[ "$r" -gt 128 ] && [ -n "$trapped" ] diff --git a/corebinutils/sh/tests/builtins/wait6.0 b/corebinutils/sh/tests/builtins/wait6.0 new file mode 100644 index 0000000000..757fee5ce5 --- /dev/null +++ b/corebinutils/sh/tests/builtins/wait6.0 @@ -0,0 +1,2 @@ + +wait -- diff --git a/corebinutils/sh/tests/builtins/wait7.0 b/corebinutils/sh/tests/builtins/wait7.0 new file mode 100644 index 0000000000..db535e1c40 --- /dev/null +++ b/corebinutils/sh/tests/builtins/wait7.0 @@ -0,0 +1,3 @@ + +: & +wait -- $! diff --git a/corebinutils/sh/tests/builtins/wait8.0 b/corebinutils/sh/tests/builtins/wait8.0 new file mode 100644 index 0000000000..3f6cdc72ab --- /dev/null +++ b/corebinutils/sh/tests/builtins/wait8.0 @@ -0,0 +1,6 @@ + +exit 44 & p44=$! +exit 45 & p45=$! +exit 7 & p7=$! +wait "$p44" "$p7" "$p45" +[ "$?" = 45 ] diff --git a/corebinutils/sh/tests/builtins/wait9.127 b/corebinutils/sh/tests/builtins/wait9.127 new file mode 100644 index 0000000000..676973dc97 --- /dev/null +++ b/corebinutils/sh/tests/builtins/wait9.127 @@ -0,0 +1,2 @@ +# Init cannot be a child of the shell. +wait 1 diff --git a/corebinutils/sh/tests/errors/Makefile b/corebinutils/sh/tests/errors/Makefile new file mode 100644 index 0000000000..26b1ccbef4 --- /dev/null +++ b/corebinutils/sh/tests/errors/Makefile @@ -0,0 +1,34 @@ +PACKAGE= tests + +TESTSDIR= ${TESTSBASE}/bin/sh/${.CURDIR:T} + +.PATH: ${.CURDIR:H} +ATF_TESTS_SH= functional_test + +${PACKAGE}FILES+= assignment-error1.0 +${PACKAGE}FILES+= assignment-error2.0 +${PACKAGE}FILES+= backquote-error1.0 +${PACKAGE}FILES+= backquote-error2.0 +${PACKAGE}FILES+= bad-binary1.126 +${PACKAGE}FILES+= bad-keyword1.0 +${PACKAGE}FILES+= bad-parm-exp1.0 +${PACKAGE}FILES+= bad-parm-exp2.2 bad-parm-exp2.2.stderr +${PACKAGE}FILES+= bad-parm-exp3.2 bad-parm-exp3.2.stderr +${PACKAGE}FILES+= bad-parm-exp4.2 bad-parm-exp4.2.stderr +${PACKAGE}FILES+= bad-parm-exp5.2 bad-parm-exp5.2.stderr +${PACKAGE}FILES+= bad-parm-exp6.2 bad-parm-exp6.2.stderr +${PACKAGE}FILES+= bad-parm-exp7.0 +${PACKAGE}FILES+= bad-parm-exp8.0 +${PACKAGE}FILES+= option-error.0 +${PACKAGE}FILES+= redirection-error.0 +${PACKAGE}FILES+= redirection-error2.2 +${PACKAGE}FILES+= redirection-error3.0 +${PACKAGE}FILES+= redirection-error4.0 +${PACKAGE}FILES+= redirection-error5.0 +${PACKAGE}FILES+= redirection-error6.0 +${PACKAGE}FILES+= redirection-error7.0 +${PACKAGE}FILES+= redirection-error8.0 +${PACKAGE}FILES+= script-error1.0 +${PACKAGE}FILES+= write-error1.0 + +.include <bsd.test.mk> diff --git a/corebinutils/sh/tests/errors/Makefile.depend b/corebinutils/sh/tests/errors/Makefile.depend new file mode 100644 index 0000000000..11aba52f82 --- /dev/null +++ b/corebinutils/sh/tests/errors/Makefile.depend @@ -0,0 +1,10 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/corebinutils/sh/tests/errors/assignment-error1.0 b/corebinutils/sh/tests/errors/assignment-error1.0 new file mode 100644 index 0000000000..27f3b06fbc --- /dev/null +++ b/corebinutils/sh/tests/errors/assignment-error1.0 @@ -0,0 +1,29 @@ +IFS=, + +SPECIAL="break,\ + :,\ + continue,\ + . /dev/null,\ + eval,\ + exec,\ + export -p,\ + readonly -p,\ + set,\ + shift,\ + times,\ + trap,\ + unset foo" + +# If there is no command word, the shell must abort on an assignment error. +${SH} -c "readonly a=0; a=2; exit 0" 2>/dev/null && exit 1 + +# Special built-in utilities must abort on an assignment error. +set -- ${SPECIAL} +for cmd in "$@" +do + ${SH} -c "readonly a=0; a=2 ${cmd}; exit 0" 2>/dev/null && exit 1 +done + +# Other utilities must not abort; we currently still execute them. +${SH} -c 'readonly a=0; a=1 true; exit $a' 2>/dev/null || exit 1 +${SH} -c 'readonly a=0; a=1 command :; exit $a' 2>/dev/null || exit 1 diff --git a/corebinutils/sh/tests/errors/assignment-error2.0 b/corebinutils/sh/tests/errors/assignment-error2.0 new file mode 100644 index 0000000000..d9844b25cd --- /dev/null +++ b/corebinutils/sh/tests/errors/assignment-error2.0 @@ -0,0 +1,7 @@ + +set -e +HOME=/ +readonly HOME +cd /sbin +{ HOME=/bin cd; } 2>/dev/null || : +[ "$(pwd)" != /bin ] diff --git a/corebinutils/sh/tests/errors/backquote-error1.0 b/corebinutils/sh/tests/errors/backquote-error1.0 new file mode 100644 index 0000000000..c4bdb6722d --- /dev/null +++ b/corebinutils/sh/tests/errors/backquote-error1.0 @@ -0,0 +1,3 @@ + +echo 'echo `for` echo ".BAD"CODE.' | ${SH} +m -i 2>&1 | grep -q BADCODE && exit 1 +exit 0 diff --git a/corebinutils/sh/tests/errors/backquote-error2.0 b/corebinutils/sh/tests/errors/backquote-error2.0 new file mode 100644 index 0000000000..05fcd96bb1 --- /dev/null +++ b/corebinutils/sh/tests/errors/backquote-error2.0 @@ -0,0 +1,6 @@ + +${SH} -c 'echo `echo .BA"DCODE.` +echo ".BAD"CODE.' 2>&1 | grep -q BADCODE && exit 1 +echo '`"`' | ${SH} -n 2>/dev/null && exit 1 +echo '`'"'"'`' | ${SH} -n 2>/dev/null && exit 1 +exit 0 diff --git a/corebinutils/sh/tests/errors/bad-binary1.126 b/corebinutils/sh/tests/errors/bad-binary1.126 new file mode 100644 index 0000000000..76ab2aa5a8 --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-binary1.126 @@ -0,0 +1,11 @@ +# Checking for binary "scripts" without magic number is permitted but not +# required by POSIX. However, it is preferable to getting errors like +# Syntax error: word unexpected (expecting ")") +# from trying to execute ELF binaries for the wrong architecture. + +T=`mktemp -d "${TMPDIR:-/tmp}/sh-test.XXXXXXXX"` || exit +trap 'rm -rf "${T}"' 0 +printf '\0echo bad\n' >"$T/testshellproc" +chmod 755 "$T/testshellproc" +PATH=$T:$PATH +testshellproc 2>/dev/null diff --git a/corebinutils/sh/tests/errors/bad-keyword1.0 b/corebinutils/sh/tests/errors/bad-keyword1.0 new file mode 100644 index 0000000000..f754e02c12 --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-keyword1.0 @@ -0,0 +1,3 @@ + +echo ':; fi' | ${SH} -n 2>/dev/null && exit 1 +exit 0 diff --git a/corebinutils/sh/tests/errors/bad-parm-exp1.0 b/corebinutils/sh/tests/errors/bad-parm-exp1.0 new file mode 100644 index 0000000000..0cefdac9ea --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-parm-exp1.0 @@ -0,0 +1,6 @@ +false && { + ${} + ${foo/} + ${foo@bar} +} +: diff --git a/corebinutils/sh/tests/errors/bad-parm-exp2.2 b/corebinutils/sh/tests/errors/bad-parm-exp2.2 new file mode 100644 index 0000000000..1de22a5c20 --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-parm-exp2.2 @@ -0,0 +1 @@ +eval '${}' diff --git a/corebinutils/sh/tests/errors/bad-parm-exp2.2.stderr b/corebinutils/sh/tests/errors/bad-parm-exp2.2.stderr new file mode 100644 index 0000000000..51ea69ca97 --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-parm-exp2.2.stderr @@ -0,0 +1 @@ +eval: ${}: Bad substitution diff --git a/corebinutils/sh/tests/errors/bad-parm-exp3.2 b/corebinutils/sh/tests/errors/bad-parm-exp3.2 new file mode 100644 index 0000000000..6f7b934756 --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-parm-exp3.2 @@ -0,0 +1 @@ +eval '${foo/}' diff --git a/corebinutils/sh/tests/errors/bad-parm-exp3.2.stderr b/corebinutils/sh/tests/errors/bad-parm-exp3.2.stderr new file mode 100644 index 0000000000..70473f9a87 --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-parm-exp3.2.stderr @@ -0,0 +1 @@ +eval: ${foo/}: Bad substitution diff --git a/corebinutils/sh/tests/errors/bad-parm-exp4.2 b/corebinutils/sh/tests/errors/bad-parm-exp4.2 new file mode 100644 index 0000000000..9f4efa88c4 --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-parm-exp4.2 @@ -0,0 +1 @@ +eval '${foo:@abc}' diff --git a/corebinutils/sh/tests/errors/bad-parm-exp4.2.stderr b/corebinutils/sh/tests/errors/bad-parm-exp4.2.stderr new file mode 100644 index 0000000000..3363f5177f --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-parm-exp4.2.stderr @@ -0,0 +1 @@ +eval: ${foo:@...}: Bad substitution diff --git a/corebinutils/sh/tests/errors/bad-parm-exp5.2 b/corebinutils/sh/tests/errors/bad-parm-exp5.2 new file mode 100644 index 0000000000..3c0d10296f --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-parm-exp5.2 @@ -0,0 +1 @@ +eval '${/}' diff --git a/corebinutils/sh/tests/errors/bad-parm-exp5.2.stderr b/corebinutils/sh/tests/errors/bad-parm-exp5.2.stderr new file mode 100644 index 0000000000..13763f8ed9 --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-parm-exp5.2.stderr @@ -0,0 +1 @@ +eval: ${/}: Bad substitution diff --git a/corebinutils/sh/tests/errors/bad-parm-exp6.2 b/corebinutils/sh/tests/errors/bad-parm-exp6.2 new file mode 100644 index 0000000000..179aa5d06b --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-parm-exp6.2 @@ -0,0 +1 @@ +eval '${#foo^}' diff --git a/corebinutils/sh/tests/errors/bad-parm-exp6.2.stderr b/corebinutils/sh/tests/errors/bad-parm-exp6.2.stderr new file mode 100644 index 0000000000..cc56f65b62 --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-parm-exp6.2.stderr @@ -0,0 +1 @@ +eval: ${foo...}: Bad substitution diff --git a/corebinutils/sh/tests/errors/bad-parm-exp7.0 b/corebinutils/sh/tests/errors/bad-parm-exp7.0 new file mode 100644 index 0000000000..d82b2eade8 --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-parm-exp7.0 @@ -0,0 +1,3 @@ + +v=1 +eval ": $(printf '${v-${\372}}')" diff --git a/corebinutils/sh/tests/errors/bad-parm-exp8.0 b/corebinutils/sh/tests/errors/bad-parm-exp8.0 new file mode 100644 index 0000000000..9f983f1dad --- /dev/null +++ b/corebinutils/sh/tests/errors/bad-parm-exp8.0 @@ -0,0 +1,3 @@ + +v=1 +eval ": $(printf '${v-${w\372}}')" diff --git a/corebinutils/sh/tests/errors/option-error.0 b/corebinutils/sh/tests/errors/option-error.0 new file mode 100644 index 0000000000..d5eac6f4c0 --- /dev/null +++ b/corebinutils/sh/tests/errors/option-error.0 @@ -0,0 +1,45 @@ +IFS=, + +SPECIAL="break abc,\ + continue abc,\ + ., + exit abc, + export -x, + readonly -x, + return abc, + set -z, + shift abc, + trap -y, + unset -y" + +UTILS="alias -y,\ + cat -z,\ + cd abc def,\ + command break abc,\ + expr 1 +,\ + fc -z,\ + getopts,\ + hash -z,\ + jobs -z,\ + printf,\ + pwd abc,\ + read,\ + test abc =,\ + ulimit -z,\ + umask -z,\ + unalias -z,\ + wait abc" + +# Special built-in utilities must abort on an option or operand error. +set -- ${SPECIAL} +for cmd in "$@" +do + ${SH} -c "${cmd}; exit 0" 2>/dev/null && exit 1 +done + +# Other utilities must not abort. +set -- ${UTILS} +for cmd in "$@" +do + ${SH} -c "${cmd}; exit 0" 2>/dev/null || exit 1 +done diff --git a/corebinutils/sh/tests/errors/redirection-error.0 b/corebinutils/sh/tests/errors/redirection-error.0 new file mode 100644 index 0000000000..dfb9f1bfb1 --- /dev/null +++ b/corebinutils/sh/tests/errors/redirection-error.0 @@ -0,0 +1,52 @@ +IFS=, + +SPECIAL="break,\ + :,\ + continue,\ + . /dev/null, + eval, + exec, + export -p, + readonly -p, + set, + shift, + times, + trap, + unset foo" + +UTILS="alias,\ + bg,\ + bind,\ + cd,\ + command echo,\ + echo,\ + false,\ + fc -l,\ + fg,\ + getopts a -a,\ + hash,\ + jobs,\ + printf a,\ + pwd,\ + read var < /dev/null,\ + test,\ + true,\ + type ls,\ + ulimit,\ + umask,\ + unalias -a,\ + wait" + +# Special built-in utilities must abort on a redirection error. +set -- ${SPECIAL} +for cmd in "$@" +do + ${SH} -c "${cmd} > /; exit 0" 2>/dev/null && exit 1 +done + +# Other utilities must not abort. +set -- ${UTILS} +for cmd in "$@" +do + ${SH} -c "${cmd} > /; exit 0" 2>/dev/null || exit 1 +done diff --git a/corebinutils/sh/tests/errors/redirection-error2.2 b/corebinutils/sh/tests/errors/redirection-error2.2 new file mode 100644 index 0000000000..0fb47d2091 --- /dev/null +++ b/corebinutils/sh/tests/errors/redirection-error2.2 @@ -0,0 +1,3 @@ + +# sh should fail gracefully on this bad redirect +${SH} -c 'echo 1 >&$a' 2>/dev/null diff --git a/corebinutils/sh/tests/errors/redirection-error3.0 b/corebinutils/sh/tests/errors/redirection-error3.0 new file mode 100644 index 0000000000..d37ba82135 --- /dev/null +++ b/corebinutils/sh/tests/errors/redirection-error3.0 @@ -0,0 +1,53 @@ +IFS=, + +SPECIAL="break,\ + :,\ + continue,\ + . /dev/null,\ + eval,\ + exec,\ + export -p,\ + readonly -p,\ + set,\ + shift,\ + times,\ + trap,\ + unset foo" + +UTILS="alias,\ + bg,\ + bind,\ + cd,\ + command echo,\ + echo,\ + false,\ + fc -l,\ + fg,\ + getopts a -a,\ + hash,\ + jobs,\ + printf a,\ + pwd,\ + read var < /dev/null,\ + test,\ + true,\ + type ls,\ + ulimit,\ + umask,\ + unalias -a,\ + wait" + +# When used with 'command', neither special built-in utilities nor other +# utilities must abort on a redirection error. + +set -- ${SPECIAL} +for cmd in "$@" +do + ${SH} -c "command ${cmd} > /; exit 0" 2>/dev/null || exit 1 +done + +set -- ${UTILS} +for cmd in "$@" +do + ${SH} -c "command ${cmd} > /; exit 0" 2>/dev/null || exit 1 +done diff --git a/corebinutils/sh/tests/errors/redirection-error4.0 b/corebinutils/sh/tests/errors/redirection-error4.0 new file mode 100644 index 0000000000..b7ce514a9f --- /dev/null +++ b/corebinutils/sh/tests/errors/redirection-error4.0 @@ -0,0 +1,6 @@ +# A redirection error should not abort the shell if there is no command word. +exec 2>/dev/null +</var/empty/x +</var/empty/x y=2 +y=2 </var/empty/x +exit 0 diff --git a/corebinutils/sh/tests/errors/redirection-error5.0 b/corebinutils/sh/tests/errors/redirection-error5.0 new file mode 100644 index 0000000000..1af2937839 --- /dev/null +++ b/corebinutils/sh/tests/errors/redirection-error5.0 @@ -0,0 +1,4 @@ +# A redirection error on a subshell should not abort the shell. +exec 2>/dev/null +( echo bad ) </var/empty/x +exit 0 diff --git a/corebinutils/sh/tests/errors/redirection-error6.0 b/corebinutils/sh/tests/errors/redirection-error6.0 new file mode 100644 index 0000000000..f0ac4a5025 --- /dev/null +++ b/corebinutils/sh/tests/errors/redirection-error6.0 @@ -0,0 +1,11 @@ +# A redirection error on a compound command should not abort the shell. +exec 2>/dev/null +{ echo bad; } </var/empty/x +if :; then echo bad; fi </var/empty/x +for i in 1; do echo bad; done </var/empty/x +i=0 +while [ $i = 0 ]; do echo bad; i=1; done </var/empty/x +i=0 +until [ $i != 0 ]; do echo bad; i=1; done </var/empty/x +case i in *) echo bad ;; esac </var/empty/x +exit 0 diff --git a/corebinutils/sh/tests/errors/redirection-error7.0 b/corebinutils/sh/tests/errors/redirection-error7.0 new file mode 100644 index 0000000000..d853aee52f --- /dev/null +++ b/corebinutils/sh/tests/errors/redirection-error7.0 @@ -0,0 +1,6 @@ + +! dummy=$( + exec 3>&1 >&2 2>&3 + ulimit -n 9 + exec 9<. +) && [ -n "$dummy" ] diff --git a/corebinutils/sh/tests/errors/redirection-error8.0 b/corebinutils/sh/tests/errors/redirection-error8.0 new file mode 100644 index 0000000000..4d2693f263 --- /dev/null +++ b/corebinutils/sh/tests/errors/redirection-error8.0 @@ -0,0 +1,4 @@ + +$SH -c '{ { :; } </var/empty/x; } 2>/dev/null || kill -INT $$; echo continued' +r=$? +[ "$r" -gt 128 ] && [ "$(kill -l "$r")" = INT ] diff --git a/corebinutils/sh/tests/errors/script-error1.0 b/corebinutils/sh/tests/errors/script-error1.0 new file mode 100644 index 0000000000..0020218cfd --- /dev/null +++ b/corebinutils/sh/tests/errors/script-error1.0 @@ -0,0 +1,4 @@ + +{ stderr=$(${SH} /var/empty/nosuchscript 2>&1 >&3); } 3>&1 +r=$? +[ -n "$stderr" ] && [ "$r" = 127 ] diff --git a/corebinutils/sh/tests/errors/write-error1.0 b/corebinutils/sh/tests/errors/write-error1.0 new file mode 100644 index 0000000000..027108f9ec --- /dev/null +++ b/corebinutils/sh/tests/errors/write-error1.0 @@ -0,0 +1,2 @@ + +! echo >&- 2>/dev/null diff --git a/corebinutils/sh/tests/execution/Makefile b/corebinutils/sh/tests/execution/Makefile new file mode 100644 index 0000000000..dde562a082 --- /dev/null +++ b/corebinutils/sh/tests/execution/Makefile @@ -0,0 +1,74 @@ +PACKAGE= tests + +TESTSDIR= ${TESTSBASE}/bin/sh/${.CURDIR:T} + +.PATH: ${.CURDIR:H} +ATF_TESTS_SH= functional_test + +${PACKAGE}FILES+= bg1.0 +${PACKAGE}FILES+= bg2.0 +${PACKAGE}FILES+= bg3.0 +${PACKAGE}FILES+= bg4.0 +${PACKAGE}FILES+= bg5.0 +${PACKAGE}FILES+= bg6.0 bg6.0.stdout +${PACKAGE}FILES+= bg7.0 +${PACKAGE}FILES+= bg8.0 +${PACKAGE}FILES+= bg9.0 +${PACKAGE}FILES+= bg10.0 bg10.0.stdout +${PACKAGE}FILES+= bg11.0 +${PACKAGE}FILES+= bg12.0 +${PACKAGE}FILES+= bg13.0 +${PACKAGE}FILES+= bg14.0 +${PACKAGE}FILES+= env1.0 +${PACKAGE}FILES+= fork1.0 +${PACKAGE}FILES+= fork2.0 +${PACKAGE}FILES+= fork3.0 +${PACKAGE}FILES+= func1.0 +${PACKAGE}FILES+= func2.0 +${PACKAGE}FILES+= func3.0 +${PACKAGE}FILES+= hash1.0 +${PACKAGE}FILES+= int-cmd1.0 +${PACKAGE}FILES+= killed1.0 +${PACKAGE}FILES+= killed2.0 +${PACKAGE}FILES+= not1.0 +${PACKAGE}FILES+= not2.0 +${PACKAGE}FILES+= path1.0 +${PACKAGE}FILES+= pipefail1.0 +${PACKAGE}FILES+= pipefail2.42 +${PACKAGE}FILES+= pipefail3.42 +${PACKAGE}FILES+= pipefail4.42 +${PACKAGE}FILES+= pipefail5.42 +${PACKAGE}FILES+= pipefail6.42 +${PACKAGE}FILES+= pipefail7.0 +${PACKAGE}FILES+= redir1.0 +${PACKAGE}FILES+= redir2.0 +${PACKAGE}FILES+= redir3.0 +${PACKAGE}FILES+= redir4.0 +${PACKAGE}FILES+= redir5.0 +${PACKAGE}FILES+= redir6.0 +${PACKAGE}FILES+= redir7.0 +${PACKAGE}FILES+= set-C1.0 +${PACKAGE}FILES+= set-n1.0 +${PACKAGE}FILES+= set-n2.0 +${PACKAGE}FILES+= set-n3.0 +${PACKAGE}FILES+= set-n4.0 +${PACKAGE}FILES+= set-x1.0 +${PACKAGE}FILES+= set-x2.0 +${PACKAGE}FILES+= set-x3.0 +${PACKAGE}FILES+= set-x4.0 +${PACKAGE}FILES+= shellproc1.0 +${PACKAGE}FILES+= shellproc2.0 +${PACKAGE}FILES+= shellproc3.0 +${PACKAGE}FILES+= shellproc4.0 +${PACKAGE}FILES+= shellproc5.0 +${PACKAGE}FILES+= shellproc6.0 +${PACKAGE}FILES+= shellproc7.0 +${PACKAGE}FILES+= subshell1.0 subshell1.0.stdout +${PACKAGE}FILES+= subshell2.0 +${PACKAGE}FILES+= subshell3.0 +${PACKAGE}FILES+= subshell4.0 +${PACKAGE}FILES+= unknown1.0 +${PACKAGE}FILES+= unknown2.0 +${PACKAGE}FILES+= var-assign1.0 + +.include <bsd.test.mk> diff --git a/corebinutils/sh/tests/execution/Makefile.depend b/corebinutils/sh/tests/execution/Makefile.depend new file mode 100644 index 0000000000..11aba52f82 --- /dev/null +++ b/corebinutils/sh/tests/execution/Makefile.depend @@ -0,0 +1,10 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/corebinutils/sh/tests/execution/bg1.0 b/corebinutils/sh/tests/execution/bg1.0 new file mode 100644 index 0000000000..eae1e2a6ee --- /dev/null +++ b/corebinutils/sh/tests/execution/bg1.0 @@ -0,0 +1,2 @@ + +: `false` & diff --git a/corebinutils/sh/tests/execution/bg10.0 b/corebinutils/sh/tests/execution/bg10.0 new file mode 100644 index 0000000000..3e825d9eda --- /dev/null +++ b/corebinutils/sh/tests/execution/bg10.0 @@ -0,0 +1,3 @@ +# The redirection overrides the </dev/null implicit in a background command. + +echo yes | ${SH} -c '{ cat & wait; } <&0' diff --git a/corebinutils/sh/tests/execution/bg10.0.stdout b/corebinutils/sh/tests/execution/bg10.0.stdout new file mode 100644 index 0000000000..7cfab5b05d --- /dev/null +++ b/corebinutils/sh/tests/execution/bg10.0.stdout @@ -0,0 +1 @@ +yes diff --git a/corebinutils/sh/tests/execution/bg11.0 b/corebinutils/sh/tests/execution/bg11.0 new file mode 100644 index 0000000000..380d2180fe --- /dev/null +++ b/corebinutils/sh/tests/execution/bg11.0 @@ -0,0 +1,15 @@ + +T=`mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXXXX` +trap 'rm -rf $T' 0 +cd $T || exit 3 +mkfifo fifo1 +# Use a trap, not the default action, since the shell may catch SIGINT and +# therefore its processing may be delayed. +{ trap 'exit 5' TERM; read dummy <fifo1; exit 4; } & +exec 3>fifo1 +kill -INT "$!" +kill -TERM "$!" +exec 3>&- +wait "$!" +r=$? +[ "$r" = 5 ] diff --git a/corebinutils/sh/tests/execution/bg12.0 b/corebinutils/sh/tests/execution/bg12.0 new file mode 100644 index 0000000000..ed1e0065b2 --- /dev/null +++ b/corebinutils/sh/tests/execution/bg12.0 @@ -0,0 +1,11 @@ + +T=`mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXXXX` +trap 'rm -rf $T' 0 +cd $T || exit 3 +mkfifo fifo1 +{ trap - INT; : >fifo1; sleep 5 & wait; exit 4; } & +: <fifo1 +kill -INT "$!" +wait "$!" +r=$? +[ "$r" -gt 128 ] && [ "$(kill -l "$r")" = INT ] diff --git a/corebinutils/sh/tests/execution/bg13.0 b/corebinutils/sh/tests/execution/bg13.0 new file mode 100644 index 0000000000..7f6fb87af6 --- /dev/null +++ b/corebinutils/sh/tests/execution/bg13.0 @@ -0,0 +1,15 @@ + +T=`mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXXXX` +trap 'rm -rf $T' 0 +cd $T || exit 3 +mkfifo fifo1 +# Use a trap, not the default action, since the shell may catch SIGINT and +# therefore its processing may be delayed. +{ set -C; trap 'exit 5' TERM; read dummy <fifo1; exit 4; } & +exec 3>fifo1 +kill -INT "$!" +kill -TERM "$!" +exec 3>&- +wait "$!" +r=$? +[ "$r" = 5 ] diff --git a/corebinutils/sh/tests/execution/bg14.0 b/corebinutils/sh/tests/execution/bg14.0 new file mode 100644 index 0000000000..e27f77e9b7 --- /dev/null +++ b/corebinutils/sh/tests/execution/bg14.0 @@ -0,0 +1,9 @@ +T=`mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXXXX` +trap 'rm -rf "$T"' 0 +cd "$T" || exit 3 +mkfifo fifo1 || exit 3 +set -T +trap "for i in 1 2 3 4; do sleep 1 & done" USR1 +sleep 1 & +{ kill -USR1 "$$"; echo .; } >fifo1 & +(read dummy <fifo1) diff --git a/corebinutils/sh/tests/execution/bg2.0 b/corebinutils/sh/tests/execution/bg2.0 new file mode 100644 index 0000000000..29bdffb0b1 --- /dev/null +++ b/corebinutils/sh/tests/execution/bg2.0 @@ -0,0 +1,4 @@ + +f() { return 42; } +f +: | : & diff --git a/corebinutils/sh/tests/execution/bg3.0 b/corebinutils/sh/tests/execution/bg3.0 new file mode 100644 index 0000000000..0a411f611e --- /dev/null +++ b/corebinutils/sh/tests/execution/bg3.0 @@ -0,0 +1,4 @@ + +f() { return 42; } +f +(:) & diff --git a/corebinutils/sh/tests/execution/bg4.0 b/corebinutils/sh/tests/execution/bg4.0 new file mode 100644 index 0000000000..4d63fa8b45 --- /dev/null +++ b/corebinutils/sh/tests/execution/bg4.0 @@ -0,0 +1,5 @@ + +x='' +: ${x:=1} & +wait +exit ${x:-0} diff --git a/corebinutils/sh/tests/execution/bg5.0 b/corebinutils/sh/tests/execution/bg5.0 new file mode 100644 index 0000000000..94c45dba37 --- /dev/null +++ b/corebinutils/sh/tests/execution/bg5.0 @@ -0,0 +1,3 @@ +# A background command has an implicit </dev/null redirection. + +echo bad | ${SH} -c '{ cat & wait; }' diff --git a/corebinutils/sh/tests/execution/bg6.0 b/corebinutils/sh/tests/execution/bg6.0 new file mode 100644 index 0000000000..ce6d88461f --- /dev/null +++ b/corebinutils/sh/tests/execution/bg6.0 @@ -0,0 +1,3 @@ +# The redirection overrides the </dev/null implicit in a background command. + +echo yes | ${SH} -c '{ cat & wait; } </dev/stdin' diff --git a/corebinutils/sh/tests/execution/bg6.0.stdout b/corebinutils/sh/tests/execution/bg6.0.stdout new file mode 100644 index 0000000000..7cfab5b05d --- /dev/null +++ b/corebinutils/sh/tests/execution/bg6.0.stdout @@ -0,0 +1 @@ +yes diff --git a/corebinutils/sh/tests/execution/bg7.0 b/corebinutils/sh/tests/execution/bg7.0 new file mode 100644 index 0000000000..01a0a1a333 --- /dev/null +++ b/corebinutils/sh/tests/execution/bg7.0 @@ -0,0 +1,4 @@ +# The redirection does not apply to the background command, and therefore +# does not override the implicit </dev/null. + +echo bad | ${SH} -c '</dev/null; { cat & wait; }' diff --git a/corebinutils/sh/tests/execution/bg8.0 b/corebinutils/sh/tests/execution/bg8.0 new file mode 100644 index 0000000000..4eeb235411 --- /dev/null +++ b/corebinutils/sh/tests/execution/bg8.0 @@ -0,0 +1,4 @@ +# The redirection does not apply to the background command, and therefore +# does not override the implicit </dev/null. + +echo bad | ${SH} -c 'command eval \) </dev/null 2>/dev/null; { cat & wait; }' diff --git a/corebinutils/sh/tests/execution/bg9.0 b/corebinutils/sh/tests/execution/bg9.0 new file mode 100644 index 0000000000..805c90a9ca --- /dev/null +++ b/corebinutils/sh/tests/execution/bg9.0 @@ -0,0 +1,4 @@ +# The redirection does not apply to the background command, and therefore +# does not override the implicit </dev/null. + +echo bad | ${SH} -c 'command eval eval \\\) \</dev/null 2>/dev/null; { cat & wait; }' diff --git a/corebinutils/sh/tests/execution/env1.0 b/corebinutils/sh/tests/execution/env1.0 new file mode 100644 index 0000000000..f1abad21ba --- /dev/null +++ b/corebinutils/sh/tests/execution/env1.0 @@ -0,0 +1,4 @@ + +unset somestrangevar +export somestrangevar +[ "`$SH -c 'echo ${somestrangevar-unset}'`" = unset ] diff --git a/corebinutils/sh/tests/execution/fork1.0 b/corebinutils/sh/tests/execution/fork1.0 new file mode 100644 index 0000000000..bf47ca0443 --- /dev/null +++ b/corebinutils/sh/tests/execution/fork1.0 @@ -0,0 +1,9 @@ + +shname=${SH%% *} +shname=${shname##*/} + +result=$(${SH} -c 'ps -p $$ -o comm=') +test "$result" = "ps" || exit 1 + +result=$(${SH} -c 'ps -p $$ -o comm=; :') +test "$result" = "$shname" || exit 1 diff --git a/corebinutils/sh/tests/execution/fork2.0 b/corebinutils/sh/tests/execution/fork2.0 new file mode 100644 index 0000000000..1b2ed28672 --- /dev/null +++ b/corebinutils/sh/tests/execution/fork2.0 @@ -0,0 +1,8 @@ + +result=$(${SH} -c '(/bin/sleep 1)& sleep 0.1; ps -p $! -o comm=; kill $!') +test "$result" = sleep || exit 1 + +result=$(${SH} -c '{ trap "echo trapped" EXIT; (/usr/bin/true); } & wait') +test "$result" = trapped || exit 1 + +exit 0 diff --git a/corebinutils/sh/tests/execution/fork3.0 b/corebinutils/sh/tests/execution/fork3.0 new file mode 100644 index 0000000000..d9b2efb235 --- /dev/null +++ b/corebinutils/sh/tests/execution/fork3.0 @@ -0,0 +1,3 @@ + +result=$(${SH} -c 'f() { ps -p $$ -o comm=; }; f') +test "$result" = "ps" diff --git a/corebinutils/sh/tests/execution/func1.0 b/corebinutils/sh/tests/execution/func1.0 new file mode 100644 index 0000000000..bfcdd67ceb --- /dev/null +++ b/corebinutils/sh/tests/execution/func1.0 @@ -0,0 +1,3 @@ + +MALLOC_CONF=junk:true ${SH} -c 'g() { g() { :; }; :; }; g' && +MALLOC_CONF=junk:true ${SH} -c 'g() { unset -f g; :; }; g' diff --git a/corebinutils/sh/tests/execution/func2.0 b/corebinutils/sh/tests/execution/func2.0 new file mode 100644 index 0000000000..d0f505e304 --- /dev/null +++ b/corebinutils/sh/tests/execution/func2.0 @@ -0,0 +1,11 @@ +# The empty pairs of braces here are to test that this does not cause a crash. + +f() { } +f +hash -v f >/dev/null +f() { { }; } +f +hash -v f >/dev/null +f() { { } } +f +hash -v f >/dev/null diff --git a/corebinutils/sh/tests/execution/func3.0 b/corebinutils/sh/tests/execution/func3.0 new file mode 100644 index 0000000000..51e70bfa8a --- /dev/null +++ b/corebinutils/sh/tests/execution/func3.0 @@ -0,0 +1,6 @@ + +# This may fail when parsing or when defining the function, or the definition +# may silently do nothing. In no event may the function be executed. + +${SH} -c 'unset() { echo overriding function executed, bad; }; v=1; unset v; exit "${v-0}"' 2>/dev/null +: diff --git a/corebinutils/sh/tests/execution/hash1.0 b/corebinutils/sh/tests/execution/hash1.0 new file mode 100644 index 0000000000..f41d38eb4a --- /dev/null +++ b/corebinutils/sh/tests/execution/hash1.0 @@ -0,0 +1,11 @@ + +T=`mktemp -d "${TMPDIR:-/tmp}/sh-test.XXXXXXXX"` || exit +trap 'rm -rf "${T}"' 0 +PATH=$T:$PATH +ls -ld . >/dev/null +cat <<EOF >"$T/ls" +: +EOF +chmod 755 "$T/ls" +PATH=$PATH +ls -ld . diff --git a/corebinutils/sh/tests/execution/int-cmd1.0 b/corebinutils/sh/tests/execution/int-cmd1.0 new file mode 100644 index 0000000000..05d08dc3d6 --- /dev/null +++ b/corebinutils/sh/tests/execution/int-cmd1.0 @@ -0,0 +1,2 @@ + +! echo echo bad | ENV= $SH -ic 'fi' 2>/dev/null diff --git a/corebinutils/sh/tests/execution/killed1.0 b/corebinutils/sh/tests/execution/killed1.0 new file mode 100644 index 0000000000..a3977b6ce1 --- /dev/null +++ b/corebinutils/sh/tests/execution/killed1.0 @@ -0,0 +1,7 @@ +# Sometimes the "Killed" message is not flushed soon enough and it +# is redirected along with the output of a builtin. +# Do not change the semicolon to a newline as it would hide the bug. + +exec 3>&1 +exec >/dev/null 2>&1 +${SH} -c 'kill -9 $$'; : >&3 2>&3 diff --git a/corebinutils/sh/tests/execution/killed2.0 b/corebinutils/sh/tests/execution/killed2.0 new file mode 100644 index 0000000000..2b9a8fa628 --- /dev/null +++ b/corebinutils/sh/tests/execution/killed2.0 @@ -0,0 +1,9 @@ +# Most shells print a message when a foreground job is killed by a signal. +# POSIX allows this, provided the message is sent to stderr, not stdout. +# Some trickery is needed to capture the message as redirecting stderr of +# the command itself does not affect it. The colon command ensures that +# the subshell forks for ${SH}. + +exec 3>&1 +r=`(${SH} -c 'kill $$'; :) 2>&1 >&3` +[ -n "$r" ] diff --git a/corebinutils/sh/tests/execution/not1.0 b/corebinutils/sh/tests/execution/not1.0 new file mode 100644 index 0000000000..86bd3fbe04 --- /dev/null +++ b/corebinutils/sh/tests/execution/not1.0 @@ -0,0 +1,3 @@ + +f() { ! return $1; } +f 0 && ! f 1 diff --git a/corebinutils/sh/tests/execution/not2.0 b/corebinutils/sh/tests/execution/not2.0 new file mode 100644 index 0000000000..896f59528c --- /dev/null +++ b/corebinutils/sh/tests/execution/not2.0 @@ -0,0 +1,5 @@ + +while :; do + ! break + exit 3 +done diff --git a/corebinutils/sh/tests/execution/path1.0 b/corebinutils/sh/tests/execution/path1.0 new file mode 100644 index 0000000000..72537dac49 --- /dev/null +++ b/corebinutils/sh/tests/execution/path1.0 @@ -0,0 +1,14 @@ +# Some builtins should not be overridable via PATH. + +set -e +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) +trap 'rm -rf ${T}' 0 +echo '#!/bin/sh +echo bad' >"$T/cd" +chmod 755 "$T/cd" +cd /bin +oPATH=$PATH +PATH=$T:$PATH:%builtin +cd / +PATH=$oPATH +[ "$(pwd)" = / ] diff --git a/corebinutils/sh/tests/execution/pipefail1.0 b/corebinutils/sh/tests/execution/pipefail1.0 new file mode 100644 index 0000000000..d3ce51b304 --- /dev/null +++ b/corebinutils/sh/tests/execution/pipefail1.0 @@ -0,0 +1,3 @@ + +set -o pipefail +: && : | : && : | : | : && : | : | : | : diff --git a/corebinutils/sh/tests/execution/pipefail2.42 b/corebinutils/sh/tests/execution/pipefail2.42 new file mode 100644 index 0000000000..93eded1ded --- /dev/null +++ b/corebinutils/sh/tests/execution/pipefail2.42 @@ -0,0 +1,3 @@ + +set -o pipefail +(exit 42) | : diff --git a/corebinutils/sh/tests/execution/pipefail3.42 b/corebinutils/sh/tests/execution/pipefail3.42 new file mode 100644 index 0000000000..996a404a29 --- /dev/null +++ b/corebinutils/sh/tests/execution/pipefail3.42 @@ -0,0 +1,3 @@ + +set -o pipefail +: | (exit 42) diff --git a/corebinutils/sh/tests/execution/pipefail4.42 b/corebinutils/sh/tests/execution/pipefail4.42 new file mode 100644 index 0000000000..d18d36bf47 --- /dev/null +++ b/corebinutils/sh/tests/execution/pipefail4.42 @@ -0,0 +1,3 @@ + +set -o pipefail +(exit 43) | (exit 42) diff --git a/corebinutils/sh/tests/execution/pipefail5.42 b/corebinutils/sh/tests/execution/pipefail5.42 new file mode 100644 index 0000000000..3ccfb5d249 --- /dev/null +++ b/corebinutils/sh/tests/execution/pipefail5.42 @@ -0,0 +1,4 @@ + +set -o pipefail +(exit 42) | : & +wait %+ diff --git a/corebinutils/sh/tests/execution/pipefail6.42 b/corebinutils/sh/tests/execution/pipefail6.42 new file mode 100644 index 0000000000..4fbca1b09a --- /dev/null +++ b/corebinutils/sh/tests/execution/pipefail6.42 @@ -0,0 +1,5 @@ + +set -o pipefail +(exit 42) | : & +set +o pipefail +wait %+ diff --git a/corebinutils/sh/tests/execution/pipefail7.0 b/corebinutils/sh/tests/execution/pipefail7.0 new file mode 100644 index 0000000000..84c551bdb6 --- /dev/null +++ b/corebinutils/sh/tests/execution/pipefail7.0 @@ -0,0 +1,4 @@ + +(exit 42) | : & +set -o pipefail +wait %+ diff --git a/corebinutils/sh/tests/execution/redir1.0 b/corebinutils/sh/tests/execution/redir1.0 new file mode 100644 index 0000000000..ffdc9c82a2 --- /dev/null +++ b/corebinutils/sh/tests/execution/redir1.0 @@ -0,0 +1,26 @@ +trap ': $((brokenpipe+=1))' PIPE + +P=${TMPDIR:-/tmp} +cd $P +T=$(mktemp -d sh-test.XXXXXX) +cd $T + +brokenpipe=0 +mkfifo fifo1 fifo2 +read dummy >fifo2 <fifo1 & +{ + exec 4>fifo2 +} 3<fifo2 # Formerly, sh would keep fd 3 and a duplicate of it open. +echo dummy >fifo1 +if [ $brokenpipe -ne 0 ]; then + rc=3 +fi +wait +echo dummy >&4 2>/dev/null +if [ $brokenpipe -eq 1 ]; then + : ${rc:=0} +fi + +rm fifo1 fifo2 +rmdir ${P}/${T} +exit ${rc:-3} diff --git a/corebinutils/sh/tests/execution/redir2.0 b/corebinutils/sh/tests/execution/redir2.0 new file mode 100644 index 0000000000..4eb44aa3a5 --- /dev/null +++ b/corebinutils/sh/tests/execution/redir2.0 @@ -0,0 +1,28 @@ +trap ': $((brokenpipe+=1))' PIPE + +P=${TMPDIR:-/tmp} +cd $P +T=$(mktemp -d sh-test.XXXXXX) +cd $T + +brokenpipe=0 +mkfifo fifo1 fifo2 +{ + { + exec ${SH} -c 'exec <fifo1; read dummy' + } 7<&- # fifo2 should be kept open, but not passed to programs + true +} 7<fifo2 & + +exec 4>fifo2 +exec 3>fifo1 +echo dummy >&4 2>/dev/null +if [ $brokenpipe -eq 1 ]; then + : ${rc:=0} +fi +echo dummy >&3 +wait + +rm fifo1 fifo2 +rmdir ${P}/${T} +exit ${rc:-3} diff --git a/corebinutils/sh/tests/execution/redir3.0 b/corebinutils/sh/tests/execution/redir3.0 new file mode 100644 index 0000000000..3129abbee0 --- /dev/null +++ b/corebinutils/sh/tests/execution/redir3.0 @@ -0,0 +1,2 @@ + +3>&- 3>&- diff --git a/corebinutils/sh/tests/execution/redir4.0 b/corebinutils/sh/tests/execution/redir4.0 new file mode 100644 index 0000000000..bee2bd78f5 --- /dev/null +++ b/corebinutils/sh/tests/execution/redir4.0 @@ -0,0 +1,3 @@ + +{ echo bad 0>&3; } 2>/dev/null 3>/dev/null 3>&- +exit 0 diff --git a/corebinutils/sh/tests/execution/redir5.0 b/corebinutils/sh/tests/execution/redir5.0 new file mode 100644 index 0000000000..d810475bfb --- /dev/null +++ b/corebinutils/sh/tests/execution/redir5.0 @@ -0,0 +1,2 @@ + +{ (echo bad) >/dev/null; } </dev/null diff --git a/corebinutils/sh/tests/execution/redir6.0 b/corebinutils/sh/tests/execution/redir6.0 new file mode 100644 index 0000000000..d9a90a9752 --- /dev/null +++ b/corebinutils/sh/tests/execution/redir6.0 @@ -0,0 +1,20 @@ + +failures=0 + +check() { + if [ "$2" != "$3" ]; then + echo "Failure at $1" >&2 + failures=$((failures + 1)) + fi +} + +check $LINENO "$(trap "echo bye" EXIT; : >/dev/null)" bye +check $LINENO "$(trap "echo bye" EXIT; { :; } >/dev/null)" bye +check $LINENO "$(trap "echo bye" EXIT; (:) >/dev/null)" bye +check $LINENO "$(trap "echo bye" EXIT; (: >/dev/null))" bye +check $LINENO "$(${SH} -c 'trap "echo bye" EXIT; : >/dev/null')" bye +check $LINENO "$(${SH} -c 'trap "echo bye" EXIT; { :; } >/dev/null')" bye +check $LINENO "$(${SH} -c 'trap "echo bye" EXIT; (:) >/dev/null')" bye +check $LINENO "$(${SH} -c 'trap "echo bye" EXIT; (: >/dev/null)')" bye + +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/execution/redir7.0 b/corebinutils/sh/tests/execution/redir7.0 new file mode 100644 index 0000000000..59abf06984 --- /dev/null +++ b/corebinutils/sh/tests/execution/redir7.0 @@ -0,0 +1,20 @@ + +failures=0 + +check() { + if [ "$2" != "$3" ]; then + echo "Failure at $1" >&2 + failures=$((failures + 1)) + fi +} + +check $LINENO "$(trap "echo bye" EXIT; f() { :; }; f >/dev/null)" bye +check $LINENO "$(trap "echo bye" EXIT; f() { :; }; { f; } >/dev/null)" bye +check $LINENO "$(trap "echo bye" EXIT; f() { :; }; (f) >/dev/null)" bye +check $LINENO "$(trap "echo bye" EXIT; f() { :; }; (f >/dev/null))" bye +check $LINENO "$(${SH} -c 'trap "echo bye" EXIT; f() { :; }; f >/dev/null')" bye +check $LINENO "$(${SH} -c 'trap "echo bye" EXIT; f() { :; }; { f; } >/dev/null')" bye +check $LINENO "$(${SH} -c 'trap "echo bye" EXIT; f() { :; }; (f) >/dev/null')" bye +check $LINENO "$(${SH} -c 'trap "echo bye" EXIT; f() { :; }; (f >/dev/null)')" bye + +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/execution/set-C1.0 b/corebinutils/sh/tests/execution/set-C1.0 new file mode 100644 index 0000000000..50ce8483c2 --- /dev/null +++ b/corebinutils/sh/tests/execution/set-C1.0 @@ -0,0 +1,11 @@ + +T=$(mktemp -d "${TMPDIR:-/tmp}/sh-test.XXXXXXXX") || exit +trap 'rm -rf "$T"' 0 + +set -C +echo . >"$T/a" && +[ -s "$T/a" ] && +{ ! true >"$T/a"; } 2>/dev/null && +[ -s "$T/a" ] && +ln -s /dev/null "$T/b" && +true >"$T/b" diff --git a/corebinutils/sh/tests/execution/set-n1.0 b/corebinutils/sh/tests/execution/set-n1.0 new file mode 100644 index 0000000000..e3ee7d2c10 --- /dev/null +++ b/corebinutils/sh/tests/execution/set-n1.0 @@ -0,0 +1,6 @@ + +v=$( ($SH -n <<'EOF' +for +EOF +) 2>&1 >/dev/null) +[ $? -ne 0 ] && [ -n "$v" ] diff --git a/corebinutils/sh/tests/execution/set-n2.0 b/corebinutils/sh/tests/execution/set-n2.0 new file mode 100644 index 0000000000..1f309d59f6 --- /dev/null +++ b/corebinutils/sh/tests/execution/set-n2.0 @@ -0,0 +1,4 @@ + +$SH -n <<'EOF' +echo bad +EOF diff --git a/corebinutils/sh/tests/execution/set-n3.0 b/corebinutils/sh/tests/execution/set-n3.0 new file mode 100644 index 0000000000..362dd121a1 --- /dev/null +++ b/corebinutils/sh/tests/execution/set-n3.0 @@ -0,0 +1,3 @@ + +v=$( ($SH -nc 'for') 2>&1 >/dev/null) +[ $? -ne 0 ] && [ -n "$v" ] diff --git a/corebinutils/sh/tests/execution/set-n4.0 b/corebinutils/sh/tests/execution/set-n4.0 new file mode 100644 index 0000000000..80a8c2bbcc --- /dev/null +++ b/corebinutils/sh/tests/execution/set-n4.0 @@ -0,0 +1,2 @@ + +$SH -nc 'echo bad' diff --git a/corebinutils/sh/tests/execution/set-x1.0 b/corebinutils/sh/tests/execution/set-x1.0 new file mode 100644 index 0000000000..31dffe45d8 --- /dev/null +++ b/corebinutils/sh/tests/execution/set-x1.0 @@ -0,0 +1,7 @@ + +key='must_contain_this' +{ r=`set -x; { : "$key"; } 2>&1 >/dev/null`; } 2>/dev/null +case $r in +*"$key"*) true ;; +*) false ;; +esac diff --git a/corebinutils/sh/tests/execution/set-x2.0 b/corebinutils/sh/tests/execution/set-x2.0 new file mode 100644 index 0000000000..a1b8d6d37f --- /dev/null +++ b/corebinutils/sh/tests/execution/set-x2.0 @@ -0,0 +1,8 @@ + +key='must contain this' +PS4="$key+ " +{ r=`set -x; { :; } 2>&1 >/dev/null`; } 2>/dev/null +case $r in +*"$key"*) true ;; +*) false ;; +esac diff --git a/corebinutils/sh/tests/execution/set-x3.0 b/corebinutils/sh/tests/execution/set-x3.0 new file mode 100644 index 0000000000..11f79ea5fc --- /dev/null +++ b/corebinutils/sh/tests/execution/set-x3.0 @@ -0,0 +1,8 @@ + +key='must contain this' +PS4='$key+ ' +{ r=`set -x; { :; } 2>&1 >/dev/null`; } 2>/dev/null +case $r in +*"$key"*) true ;; +*) false ;; +esac diff --git a/corebinutils/sh/tests/execution/set-x4.0 b/corebinutils/sh/tests/execution/set-x4.0 new file mode 100644 index 0000000000..495a2d9c30 --- /dev/null +++ b/corebinutils/sh/tests/execution/set-x4.0 @@ -0,0 +1,6 @@ + +key=`printf '\r\t\001\200\300'` +r=`{ set -x; : "$key"; } 2>&1 >/dev/null` +case $r in +*[![:print:]]*) echo fail; exit 3 +esac diff --git a/corebinutils/sh/tests/execution/shellproc1.0 b/corebinutils/sh/tests/execution/shellproc1.0 new file mode 100644 index 0000000000..125a177f84 --- /dev/null +++ b/corebinutils/sh/tests/execution/shellproc1.0 @@ -0,0 +1,10 @@ + +T=`mktemp -d "${TMPDIR:-/tmp}/sh-test.XXXXXXXX"` || exit +trap 'rm -rf "${T}"' 0 +cat <<EOF >"$T/testshellproc" +printf 'this ' +echo is a test +EOF +chmod 755 "$T/testshellproc" +PATH=$T:$PATH +[ "`testshellproc`" = "this is a test" ] diff --git a/corebinutils/sh/tests/execution/shellproc2.0 b/corebinutils/sh/tests/execution/shellproc2.0 new file mode 100644 index 0000000000..b599d16527 --- /dev/null +++ b/corebinutils/sh/tests/execution/shellproc2.0 @@ -0,0 +1,17 @@ +# This tests a quality of implementation issue. +# Shells are not required to reject executing binary files as shell scripts +# but executing, for example, ELF files for a different architecture as +# shell scripts may have annoying side effects. + +T=`mktemp -d "${TMPDIR:-/tmp}/sh-test.XXXXXXXX"` || exit +trap 'rm -rf "${T}"' 0 +printf '\0' >"$T/testshellproc" +chmod 755 "$T/testshellproc" +if [ ! -s "$T/testshellproc" ]; then + printf "printf did not write a NUL character\n" >&2 + exit 2 +fi +PATH=$T:$PATH +errout=`testshellproc 3>&2 2>&1 >&3 3>&-` +r=$? +[ "$r" = 126 ] && [ -n "$errout" ] diff --git a/corebinutils/sh/tests/execution/shellproc3.0 b/corebinutils/sh/tests/execution/shellproc3.0 new file mode 100644 index 0000000000..17026480a9 --- /dev/null +++ b/corebinutils/sh/tests/execution/shellproc3.0 @@ -0,0 +1,13 @@ +# This tests a quality of implementation issue. +# Shells are not required to reject executing binary files as shell scripts +# but executing, for example, ELF files for a different architecture as +# shell scripts may have annoying side effects. + +T=`mktemp -d "${TMPDIR:-/tmp}/sh-test.XXXXXXXX"` || exit +trap 'rm -rf "${T}"' 0 +printf '\177ELF\001!!\011\0\0\0\0\0\0\0\0' >"$T/testshellproc" +chmod 755 "$T/testshellproc" +PATH=$T:$PATH +errout=`testshellproc 3>&2 2>&1 >&3 3>&-` +r=$? +[ "$r" = 126 ] && [ -n "$errout" ] diff --git a/corebinutils/sh/tests/execution/shellproc4.0 b/corebinutils/sh/tests/execution/shellproc4.0 new file mode 100644 index 0000000000..4aa944b55a --- /dev/null +++ b/corebinutils/sh/tests/execution/shellproc4.0 @@ -0,0 +1,13 @@ +# This tests a quality of implementation issue. +# Shells are not required to reject executing binary files as shell scripts +# but executing, for example, ELF files for a different architecture as +# shell scripts may have annoying side effects. + +T=`mktemp -d "${TMPDIR:-/tmp}/sh-test.XXXXXXXX"` || exit +trap 'rm -rf "${T}"' 0 +printf '\211PNG\015\012\032\012\0\0\0\015IHDR' >"$T/testshellproc" +chmod 755 "$T/testshellproc" +PATH=$T:$PATH +errout=`testshellproc 3>&2 2>&1 >&3 3>&-` +r=$? +[ "$r" = 126 ] && [ -n "$errout" ] diff --git a/corebinutils/sh/tests/execution/shellproc5.0 b/corebinutils/sh/tests/execution/shellproc5.0 new file mode 100644 index 0000000000..535e5dabc4 --- /dev/null +++ b/corebinutils/sh/tests/execution/shellproc5.0 @@ -0,0 +1,13 @@ +# This tests a quality of implementation issue. +# Shells are not required to reject executing binary files as shell scripts +# but executing, for example, ELF files for a different architecture as +# shell scripts may have annoying side effects. + +T=`mktemp -d "${TMPDIR:-/tmp}/sh-test.XXXXXXXX"` || exit +trap 'rm -rf "${T}"' 0 +printf '\177ELF\001!!\012\0\0\0\0\0\0\0\0' >"$T/testshellproc" +chmod 755 "$T/testshellproc" +PATH=$T:$PATH +errout=`testshellproc 3>&2 2>&1 >&3 3>&-` +r=$? +[ "$r" = 126 ] && [ -n "$errout" ] diff --git a/corebinutils/sh/tests/execution/shellproc6.0 b/corebinutils/sh/tests/execution/shellproc6.0 new file mode 100644 index 0000000000..f1527f6d85 --- /dev/null +++ b/corebinutils/sh/tests/execution/shellproc6.0 @@ -0,0 +1,7 @@ + +T=`mktemp -d "${TMPDIR:-/tmp}/sh-test.XXXXXXXX"` || exit +trap 'rm -rf "${T}"' 0 +printf 'printf "this "\necho is a test\nexit\n\0' >"$T/testshellproc" +chmod 755 "$T/testshellproc" +PATH=$T:$PATH +[ "`testshellproc`" = "this is a test" ] diff --git a/corebinutils/sh/tests/execution/shellproc7.0 b/corebinutils/sh/tests/execution/shellproc7.0 new file mode 100644 index 0000000000..b5e543826c --- /dev/null +++ b/corebinutils/sh/tests/execution/shellproc7.0 @@ -0,0 +1,9 @@ +# Non-POSIX trickery that is widely supported, +# used by https://justine.lol/ape.html + +T=`mktemp -d "${TMPDIR:-/tmp}/sh-test.XXXXXXXX"` || exit +trap 'rm -rf "${T}"' 0 +printf "MZqFpD='\n\0'\n#'\"\necho this is a test\n" >"$T/testshellproc" +chmod 755 "$T/testshellproc" +PATH=$T:$PATH +[ "`testshellproc`" = "this is a test" ] diff --git a/corebinutils/sh/tests/execution/subshell1.0 b/corebinutils/sh/tests/execution/subshell1.0 new file mode 100644 index 0000000000..ddff2fee49 --- /dev/null +++ b/corebinutils/sh/tests/execution/subshell1.0 @@ -0,0 +1,5 @@ + +(eval "cd / +v=$(printf %0100000d 1) +echo \${#v}") +echo end diff --git a/corebinutils/sh/tests/execution/subshell1.0.stdout b/corebinutils/sh/tests/execution/subshell1.0.stdout new file mode 100644 index 0000000000..8c71af3cd7 --- /dev/null +++ b/corebinutils/sh/tests/execution/subshell1.0.stdout @@ -0,0 +1,2 @@ +100000 +end diff --git a/corebinutils/sh/tests/execution/subshell2.0 b/corebinutils/sh/tests/execution/subshell2.0 new file mode 100644 index 0000000000..ad5a3b87af --- /dev/null +++ b/corebinutils/sh/tests/execution/subshell2.0 @@ -0,0 +1,9 @@ + +f() { + x=2 +} +( + x=1 + f + [ "$x" = 2 ] +) diff --git a/corebinutils/sh/tests/execution/subshell3.0 b/corebinutils/sh/tests/execution/subshell3.0 new file mode 100644 index 0000000000..4d1916dfa1 --- /dev/null +++ b/corebinutils/sh/tests/execution/subshell3.0 @@ -0,0 +1,3 @@ + +(false; exit) && exit 3 +exit 0 diff --git a/corebinutils/sh/tests/execution/subshell4.0 b/corebinutils/sh/tests/execution/subshell4.0 new file mode 100644 index 0000000000..841186b5e6 --- /dev/null +++ b/corebinutils/sh/tests/execution/subshell4.0 @@ -0,0 +1,2 @@ + +(eval "set v=1"; false) && echo bad; : diff --git a/corebinutils/sh/tests/execution/unknown1.0 b/corebinutils/sh/tests/execution/unknown1.0 new file mode 100644 index 0000000000..c41243454d --- /dev/null +++ b/corebinutils/sh/tests/execution/unknown1.0 @@ -0,0 +1,28 @@ + +nosuchtool 2>/dev/null +[ $? -ne 127 ] && exit 1 +/var/empty/nosuchtool 2>/dev/null +[ $? -ne 127 ] && exit 1 +(nosuchtool) 2>/dev/null +[ $? -ne 127 ] && exit 1 +(/var/empty/nosuchtool) 2>/dev/null +[ $? -ne 127 ] && exit 1 +/ 2>/dev/null +[ $? -ne 126 ] && exit 1 +PATH=/usr bin 2>/dev/null +[ $? -ne 126 ] && exit 1 + +dummy=$(nosuchtool 2>/dev/null) +[ $? -ne 127 ] && exit 1 +dummy=$(/var/empty/nosuchtool 2>/dev/null) +[ $? -ne 127 ] && exit 1 +dummy=$( (nosuchtool) 2>/dev/null) +[ $? -ne 127 ] && exit 1 +dummy=$( (/var/empty/nosuchtool) 2>/dev/null) +[ $? -ne 127 ] && exit 1 +dummy=$(/ 2>/dev/null) +[ $? -ne 126 ] && exit 1 +dummy=$(PATH=/usr bin 2>/dev/null) +[ $? -ne 126 ] && exit 1 + +exit 0 diff --git a/corebinutils/sh/tests/execution/unknown2.0 b/corebinutils/sh/tests/execution/unknown2.0 new file mode 100644 index 0000000000..eb7ab0175a --- /dev/null +++ b/corebinutils/sh/tests/execution/unknown2.0 @@ -0,0 +1,5 @@ + +{ + : $(/var/empty/nosuchtool) + : $(:) +} 2>/dev/null diff --git a/corebinutils/sh/tests/execution/var-assign1.0 b/corebinutils/sh/tests/execution/var-assign1.0 new file mode 100644 index 0000000000..8d49498d84 --- /dev/null +++ b/corebinutils/sh/tests/execution/var-assign1.0 @@ -0,0 +1,2 @@ + +[ "$(HOME=/etc HOME=/ cd && pwd)" = / ] diff --git a/corebinutils/sh/tests/expansion/Makefile b/corebinutils/sh/tests/expansion/Makefile new file mode 100644 index 0000000000..af4b369915 --- /dev/null +++ b/corebinutils/sh/tests/expansion/Makefile @@ -0,0 +1,110 @@ +PACKAGE= tests + +TESTSDIR= ${TESTSBASE}/bin/sh/${.CURDIR:T} + +.PATH: ${.CURDIR:H} +ATF_TESTS_SH= functional_test + +${PACKAGE}FILES+= arith1.0 +${PACKAGE}FILES+= arith2.0 +${PACKAGE}FILES+= arith3.0 +${PACKAGE}FILES+= arith4.0 +${PACKAGE}FILES+= arith5.0 +${PACKAGE}FILES+= arith6.0 +${PACKAGE}FILES+= arith7.0 +${PACKAGE}FILES+= arith8.0 +${PACKAGE}FILES+= arith9.0 +${PACKAGE}FILES+= arith10.0 +${PACKAGE}FILES+= arith11.0 +${PACKAGE}FILES+= arith12.0 +${PACKAGE}FILES+= arith13.0 +${PACKAGE}FILES+= arith14.0 +${PACKAGE}FILES+= arith15.0 +${PACKAGE}FILES+= arith16.0 +${PACKAGE}FILES+= arith17.0 +${PACKAGE}FILES+= assign1.0 +${PACKAGE}FILES+= cmdsubst1.0 +${PACKAGE}FILES+= cmdsubst2.0 +${PACKAGE}FILES+= cmdsubst3.0 +${PACKAGE}FILES+= cmdsubst4.0 +${PACKAGE}FILES+= cmdsubst5.0 +${PACKAGE}FILES+= cmdsubst6.0 +${PACKAGE}FILES+= cmdsubst7.0 +${PACKAGE}FILES+= cmdsubst8.0 +${PACKAGE}FILES+= cmdsubst9.0 +${PACKAGE}FILES+= cmdsubst10.0 +${PACKAGE}FILES+= cmdsubst11.0 +${PACKAGE}FILES+= cmdsubst12.0 +${PACKAGE}FILES+= cmdsubst13.0 +${PACKAGE}FILES+= cmdsubst14.0 +${PACKAGE}FILES+= cmdsubst15.0 +${PACKAGE}FILES+= cmdsubst16.0 +${PACKAGE}FILES+= cmdsubst17.0 +${PACKAGE}FILES+= cmdsubst18.0 +${PACKAGE}FILES+= cmdsubst19.0 +${PACKAGE}FILES+= cmdsubst20.0 +${PACKAGE}FILES+= cmdsubst21.0 +${PACKAGE}FILES+= cmdsubst22.0 +${PACKAGE}FILES+= cmdsubst23.0 +${PACKAGE}FILES+= cmdsubst24.0 +${PACKAGE}FILES+= cmdsubst25.0 +${PACKAGE}FILES+= cmdsubst26.0 +${PACKAGE}FILES+= export1.0 +${PACKAGE}FILES+= export2.0 +${PACKAGE}FILES+= export3.0 +${PACKAGE}FILES+= heredoc1.0 +${PACKAGE}FILES+= heredoc2.0 +${PACKAGE}FILES+= ifs1.0 +${PACKAGE}FILES+= ifs2.0 +${PACKAGE}FILES+= ifs3.0 +${PACKAGE}FILES+= ifs4.0 +${PACKAGE}FILES+= ifs5.0 +${PACKAGE}FILES+= ifs6.0 +${PACKAGE}FILES+= ifs7.0 +${PACKAGE}FILES+= length1.0 +${PACKAGE}FILES+= length2.0 +${PACKAGE}FILES+= length3.0 +${PACKAGE}FILES+= length4.0 +${PACKAGE}FILES+= length5.0 +${PACKAGE}FILES+= length6.0 +${PACKAGE}FILES+= length7.0 +${PACKAGE}FILES+= length8.0 +${PACKAGE}FILES+= local1.0 +${PACKAGE}FILES+= local2.0 +${PACKAGE}FILES+= pathname1.0 +${PACKAGE}FILES+= pathname2.0 +${PACKAGE}FILES+= pathname3.0 +${PACKAGE}FILES+= pathname4.0 +${PACKAGE}FILES+= pathname5.0 +${PACKAGE}FILES+= pathname6.0 +${PACKAGE}FILES+= plus-minus1.0 +${PACKAGE}FILES+= plus-minus2.0 +${PACKAGE}FILES+= plus-minus3.0 +${PACKAGE}FILES+= plus-minus4.0 +${PACKAGE}FILES+= plus-minus5.0 +${PACKAGE}FILES+= plus-minus6.0 +${PACKAGE}FILES+= plus-minus7.0 +${PACKAGE}FILES+= plus-minus8.0 +${PACKAGE}FILES+= plus-minus9.0 +${PACKAGE}FILES+= question1.0 +${PACKAGE}FILES+= question2.0 +${PACKAGE}FILES+= readonly1.0 +${PACKAGE}FILES+= redir1.0 +${PACKAGE}FILES+= set-u1.0 +${PACKAGE}FILES+= set-u2.0 +${PACKAGE}FILES+= set-u3.0 +${PACKAGE}FILES+= tilde1.0 +${PACKAGE}FILES+= tilde2.0 +${PACKAGE}FILES+= trim1.0 +${PACKAGE}FILES+= trim2.0 +${PACKAGE}FILES+= trim3.0 +${PACKAGE}FILES+= trim4.0 +${PACKAGE}FILES+= trim5.0 +${PACKAGE}FILES+= trim6.0 +${PACKAGE}FILES+= trim7.0 +${PACKAGE}FILES+= trim8.0 +${PACKAGE}FILES+= trim9.0 +${PACKAGE}FILES+= trim10.0 +${PACKAGE}FILES+= trim11.0 + +.include <bsd.test.mk> diff --git a/corebinutils/sh/tests/expansion/Makefile.depend b/corebinutils/sh/tests/expansion/Makefile.depend new file mode 100644 index 0000000000..11aba52f82 --- /dev/null +++ b/corebinutils/sh/tests/expansion/Makefile.depend @@ -0,0 +1,10 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/corebinutils/sh/tests/expansion/arith1.0 b/corebinutils/sh/tests/expansion/arith1.0 new file mode 100644 index 0000000000..c1338fc349 --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith1.0 @@ -0,0 +1,29 @@ + +failures=0 + +check() { + if [ $(($1)) != $2 ]; then + failures=$((failures+1)) + echo "For $1, expected $2 actual $(($1))" + fi +} + +check "0&&0" 0 +check "1&&0" 0 +check "0&&1" 0 +check "1&&1" 1 +check "2&&2" 1 +check "1&&2" 1 +check "1<<40&&1<<40" 1 +check "1<<40&&4" 1 + +check "0||0" 0 +check "1||0" 1 +check "0||1" 1 +check "1||1" 1 +check "2||2" 1 +check "1||2" 1 +check "1<<40||1<<40" 1 +check "1<<40||4" 1 + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/expansion/arith10.0 b/corebinutils/sh/tests/expansion/arith10.0 new file mode 100644 index 0000000000..1e0c10dfda --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith10.0 @@ -0,0 +1,34 @@ + +failures=0 + +check() { + if [ $(($1)) != $2 ]; then + failures=$((failures+1)) + echo "For $1, expected $2 actual $(($1))" + fi +} + +readonly ro=4 +rw=1 +check "0 && 0 / 0" 0 +check "1 || 0 / 0" 1 +check "0 && (ro = 2)" 0 +check "ro" 4 +check "1 || (ro = -1)" 1 +check "ro" 4 +check "0 && (rw += 1)" 0 +check "rw" 1 +check "1 || (rw += 1)" 1 +check "rw" 1 +check "0 ? 44 / 0 : 51" 51 +check "0 ? ro = 3 : 52" 52 +check "ro" 4 +check "0 ? rw += 1 : 52" 52 +check "rw" 1 +check "1 ? 68 : 30 / 0" 68 +check "2 ? 1 : (ro += 2)" 1 +check "ro" 4 +check "4 ? 1 : (rw += 1)" 1 +check "rw" 1 + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/expansion/arith11.0 b/corebinutils/sh/tests/expansion/arith11.0 new file mode 100644 index 0000000000..52c5a4420e --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith11.0 @@ -0,0 +1,11 @@ +# Try to divide the smallest integer by -1. +# On amd64 this causes SIGFPE, so make sure the shell checks. + +# Calculate the minimum possible value, assuming two's complement and +# a certain interpretation of overflow when shifting left. +minint=1 +while [ $((minint <<= 1)) -gt 0 ]; do + : +done +v=$( eval ': $((minint / -1))' 2>&1 >/dev/null) +[ $? -ne 0 ] && [ -n "$v" ] diff --git a/corebinutils/sh/tests/expansion/arith12.0 b/corebinutils/sh/tests/expansion/arith12.0 new file mode 100644 index 0000000000..3d54970d84 --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith12.0 @@ -0,0 +1,3 @@ + +_x=4 y_=5 z_z=6 +[ "$((_x*100+y_*10+z_z))" = 456 ] diff --git a/corebinutils/sh/tests/expansion/arith13.0 b/corebinutils/sh/tests/expansion/arith13.0 new file mode 100644 index 0000000000..897e55f702 --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith13.0 @@ -0,0 +1,5 @@ +# Pre-increment and pre-decrement in arithmetic expansion are not in POSIX. +# Require either an error or a correct implementation. + +! (eval 'x=4; [ $((++x)) != 5 ] || [ $x != 5 ]') 2>/dev/null && +! (eval 'x=2; [ $((--x)) != 1 ] || [ $x != 1 ]') 2>/dev/null diff --git a/corebinutils/sh/tests/expansion/arith14.0 b/corebinutils/sh/tests/expansion/arith14.0 new file mode 100644 index 0000000000..73f4ef3cab --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith14.0 @@ -0,0 +1,39 @@ +# Check that <</>> use the low bits of the shift count. + +if [ $((1<<16<<16)) = 0 ]; then + width=32 +elif [ $((1<<32<<32)) = 0 ]; then + width=64 +elif [ $((1<<64<<64)) = 0 ]; then + width=128 +elif [ $((1<<64>>64)) = 1 ]; then + # Integers are wider than 128 bits; assume arbitrary precision. + # Nothing to test here. + exit 0 +else + echo "Cannot determine integer width" + exit 2 +fi + +twowidth=$((width * 2)) +j=43 k=$((1 << (width - 2))) r=0 + +i=0 +while [ $i -lt $twowidth ]; do + if [ "$((j << i))" != "$((j << (i + width)))" ]; then + echo "Problem with $j << $i" + r=2 + fi + i=$((i + 1)) +done + +i=0 +while [ $i -lt $twowidth ]; do + if [ "$((k >> i))" != "$((k >> (i + width)))" ]; then + echo "Problem with $k >> $i" + r=2 + fi + i=$((i + 1)) +done + +exit $r diff --git a/corebinutils/sh/tests/expansion/arith15.0 b/corebinutils/sh/tests/expansion/arith15.0 new file mode 100644 index 0000000000..198004aedb --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith15.0 @@ -0,0 +1,19 @@ + +failures=0 + +check() { + if [ $(($1)) != $2 ]; then + failures=$((failures+1)) + echo "For $1, expected $2 actual $(($1))" + fi +} + +XXX=-9223372036854775808 +check "XXX" -9223372036854775808 +check "XXX - 1" 9223372036854775807 +check "$XXX - 1" 9223372036854775807 +check "$XXX - 2" 9223372036854775806 +check "0x8000000000000000 == 0x7fffffffffffffff" \ + 0 + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/expansion/arith16.0 b/corebinutils/sh/tests/expansion/arith16.0 new file mode 100644 index 0000000000..4956aebbc6 --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith16.0 @@ -0,0 +1,25 @@ + +failures=0 + +for x in \ + 0x10000000000000000 \ + -0x8000000000000001 \ + 0xfffffffffffffffffffffffffffffffff \ + -0xfffffffffffffffffffffffffffffffff \ + 02000000000000000000000 \ + 9223372036854775808 \ + 9223372036854775809 \ + -9223372036854775809 \ + 9999999999999999999999999 \ + -9999999999999999999999999 +do + msg=$({ + v=$((x)) || : + } 3>&1 >&2 2>&3 3>&-) + r=$? + if [ "$r" = 0 ] || [ -z "$msg" ]; then + printf 'Failed: %s\n' "$x" + : $((failures += 1)) + fi +done +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/expansion/arith17.0 b/corebinutils/sh/tests/expansion/arith17.0 new file mode 100644 index 0000000000..72e0eec8a1 --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith17.0 @@ -0,0 +1,2 @@ + +[ $((9223372036854775809)) -gt 0 ] diff --git a/corebinutils/sh/tests/expansion/arith2.0 b/corebinutils/sh/tests/expansion/arith2.0 new file mode 100644 index 0000000000..46aa968aa0 --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith2.0 @@ -0,0 +1,76 @@ + +failures=0 + +check() { + if [ $(($1)) != $2 ]; then + failures=$((failures+1)) + echo "For $1, expected $2 actual $(($1))" + fi +} + +# variables +unset v +check "v=2" 2 +check "v" 2 +check "$(($v))" 2 +check "v+=1" 3 +check "v" 3 + +# constants +check "4611686018427387904" 4611686018427387904 +check "0x4000000000000000" 4611686018427387904 +check "0400000000000000000000" 4611686018427387904 +check "0x4Ab0000000000000" 5381801554707742720 +check "010" 8 + +# try out all operators +v=42 +check "!v" 0 +check "!!v" 1 +check "!0" 1 +check "~0" -1 +check "~(-1)" 0 +check "-0" 0 +check "-v" -42 +check "v*v" 1764 +check "v/2" 21 +check "v%10" 2 +check "v+v" 84 +check "v-4" 38 +check "v<<1" 84 +check "v>>1" 21 +check "v<43" 1 +check "v>42" 0 +check "v<=43" 1 +check "v>=43" 0 +check "v==41" 0 +check "v!=42" 0 +check "v&3" 2 +check "v^3" 41 +check "v|3" 43 +check "v>=40&&v<=44" 1 +check "v<40||v>44" 0 +check "(v=42)&&(v+=1)==43" 1 +check "v" 43 +check "(v=42)&&(v-=1)==41" 1 +check "v" 41 +check "(v=42)&&(v*=2)==84" 1 +check "v" 84 +check "(v=42)&&(v/=10)==4" 1 +check "v" 4 +check "(v=42)&&(v%=10)==2" 1 +check "v" 2 +check "(v=42)&&(v<<=1)==84" 1 +check "v" 84 +check "(v=42)&&(v>>=2)==10" 1 +check "v" 10 +check "(v=42)&&(v&=32)==32" 1 +check "v" 32 +check "(v=42)&&(v^=32)==10" 1 +check "v" 10 +check "(v=42)&&(v|=32)==42" 1 +check "v" 42 + +# missing: ternary + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/expansion/arith3.0 b/corebinutils/sh/tests/expansion/arith3.0 new file mode 100644 index 0000000000..622f8d1c8f --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith3.0 @@ -0,0 +1,13 @@ + +failures=0 + +check() { + if [ $(($1)) != $2 ]; then + failures=$((failures+1)) + echo "For $1, expected $2 actual $(($1))" + fi +} + +check "1 << 1 + 1 | 1" 5 + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/expansion/arith4.0 b/corebinutils/sh/tests/expansion/arith4.0 new file mode 100644 index 0000000000..5a7e4d419c --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith4.0 @@ -0,0 +1,19 @@ + +failures=0 + +check() { + if [ $(($1)) != $2 ]; then + failures=$((failures+1)) + echo "For $1, expected $2 actual $(($1))" + fi +} + +check '20 / 2 / 2' 5 +check '20 - 2 - 2' 16 +unset a b c d +check "a = b = c = d = 1" 1 +check "a == 1 && b == 1 && c == 1 && d == 1" 1 +check "a += b += c += d" 4 +check "a == 4 && b == 3 && c == 2 && d == 1" 1 + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/expansion/arith5.0 b/corebinutils/sh/tests/expansion/arith5.0 new file mode 100644 index 0000000000..c290a2426f --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith5.0 @@ -0,0 +1,16 @@ + +failures=0 + +check() { + if [ "$2" != "$3" ]; then + failures=$((failures+1)) + echo "For $1, expected $3 actual $2" + fi +} + +unset a +check '$((1+${a:-$((7+2))}))' "$((1+${a:-$((7+2))}))" 10 +check '$((1+${a:=$((2+2))}))' "$((1+${a:=$((2+2))}))" 5 +check '$a' "$a" 4 + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/expansion/arith6.0 b/corebinutils/sh/tests/expansion/arith6.0 new file mode 100644 index 0000000000..f3d7ca10b5 --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith6.0 @@ -0,0 +1,15 @@ + +v1=1\ +\ 1 +v2=D +v3=C123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 +f() { v4="$*"; } + +while [ ${#v2} -lt 1250 ]; do + eval $v2=$((3+${#v2})) $v3=$((4-${#v2})) + eval f $(($v2+ $v1 +$v3)) + if [ $v4 -ne 9 ]; then + echo bad: $v4 -ne 9 at ${#v2} + fi + v2=x$v2 + v3=y$v3 +done diff --git a/corebinutils/sh/tests/expansion/arith7.0 b/corebinutils/sh/tests/expansion/arith7.0 new file mode 100644 index 0000000000..8578436cb6 --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith7.0 @@ -0,0 +1,11 @@ + +v=1+ +v=$v$v$v$v +v=$v$v$v$v +v=$v$v$v$v +v=$v$v$v$v +v=$v$v$v$v +[ "$(cat <<EOF +$(($v 1)) +EOF +)" = 1025 ] diff --git a/corebinutils/sh/tests/expansion/arith8.0 b/corebinutils/sh/tests/expansion/arith8.0 new file mode 100644 index 0000000000..7c77f3e7e4 --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith8.0 @@ -0,0 +1,3 @@ + +v=$( (eval ': $((08))') 2>&1 >/dev/null) +[ $? -ne 0 ] && [ -n "$v" ] diff --git a/corebinutils/sh/tests/expansion/arith9.0 b/corebinutils/sh/tests/expansion/arith9.0 new file mode 100644 index 0000000000..94da20b177 --- /dev/null +++ b/corebinutils/sh/tests/expansion/arith9.0 @@ -0,0 +1,19 @@ + +failures=0 + +check() { + if [ $(($1)) != $2 ]; then + failures=$((failures+1)) + echo "For $1, expected $2 actual $(($1))" + fi +} + +check "0 ? 44 : 51" 51 +check "1 ? 68 : 30" 68 +check "2 ? 1 : -5" 1 +check "0 ? 4 : 0 ? 5 : 6" 6 +check "0 ? 4 : 1 ? 5 : 6" 5 +check "1 ? 4 : 0 ? 5 : 6" 4 +check "1 ? 4 : 1 ? 5 : 6" 4 + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/expansion/assign1.0 b/corebinutils/sh/tests/expansion/assign1.0 new file mode 100644 index 0000000000..16bca926bc --- /dev/null +++ b/corebinutils/sh/tests/expansion/assign1.0 @@ -0,0 +1,36 @@ + +e= q='?' a='*' t=texttext s='ast*que?non' p='/et[c]/' w='a b c' b='{{(#)}}' +h='##' +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'v=; set -- ${v=a b} $v' '0|' +testcase 'unset v; set -- ${v=a b} $v' '4|a|b|a|b' +testcase 'v=; set -- ${v:=a b} $v' '4|a|b|a|b' +testcase 'v=; set -- "${v:=a b}" "$v"' '2|a b|a b' +# expect sensible behaviour, although it disagrees with POSIX +testcase 'v=; set -- ${v:=a\ b} $v' '4|a|b|a|b' +testcase 'v=; set -- ${v:=$p} $v' '2|/etc/|/etc/' +testcase 'v=; set -- "${v:=$p}" "$v"' '2|/et[c]/|/et[c]/' +testcase 'v=; set -- "${v:=a\ b}" "$v"' '2|a\ b|a\ b' +testcase 'v=; set -- ${v:="$p"} $v' '2|/etc/|/etc/' +# whether $p is quoted or not shouldn't really matter +testcase 'v=; set -- "${v:="$p"}" "$v"' '2|/et[c]/|/et[c]/' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/cmdsubst1.0 b/corebinutils/sh/tests/expansion/cmdsubst1.0 new file mode 100644 index 0000000000..dd0a3f2f16 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst1.0 @@ -0,0 +1,47 @@ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +check '"$(echo abcde)" = "abcde"' +check '"$(echo abcde; :)" = "abcde"' + +check '"$(printf abcde)" = "abcde"' +check '"$(printf abcde; :)" = "abcde"' + +# regular +check '-n "$(umask)"' +check '-n "$(umask; :)"' +check '-n "$(umask 2>&1)"' +check '-n "$(umask 2>&1; :)"' + +# special +check '-n "$(times)"' +check '-n "$(times; :)"' +check '-n "$(times 2>&1)"' +check '-n "$(times 2>&1; :)"' + +# regular +check '".$(umask -@ 2>&1)." = ".umask: Illegal option -@."' +check '".$(umask -@ 2>&1; :)." = ".umask: Illegal option -@."' +check '".$({ umask -@; } 2>&1)." = ".umask: Illegal option -@."' + +# special +check '".$(shift xyz 2>&1)." = ".shift: Illegal number: xyz."' +check '".$(shift xyz 2>&1; :)." = ".shift: Illegal number: xyz."' +check '".$({ shift xyz; } 2>&1)." = ".shift: Illegal number: xyz."' + +v=1 +check '-z "$(v=2 :)"' +check '"$v" = 1' +check '-z "$(v=3)"' +check '"$v" = 1' +check '"$(v=4 eval echo \$v)" = 4' +check '"$v" = 1' + +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/expansion/cmdsubst10.0 b/corebinutils/sh/tests/expansion/cmdsubst10.0 new file mode 100644 index 0000000000..da78234418 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst10.0 @@ -0,0 +1,50 @@ + +a1=$(alias) +: $(alias testalias=abcd) +a2=$(alias) +[ "$a1" = "$a2" ] || echo Error at line $LINENO + +alias testalias2=abcd +a1=$(alias) +: $(unalias testalias2) +a2=$(alias) +[ "$a1" = "$a2" ] || echo Error at line $LINENO + +[ "$(command -V pwd)" = "$(command -V pwd; exit $?)" ] || echo Error at line $LINENO + +v=1 +: $(export v=2) +[ "$v" = 1 ] || echo Error at line $LINENO + +rotest=1 +: $(readonly rotest=2) +[ "$rotest" = 1 ] || echo Error at line $LINENO + +set +u +: $(set -u) +case $- in +*u*) echo Error at line $LINENO ;; +esac +set +u + +set +u +: $(set -o nounset) +case $- in +*u*) echo Error at line $LINENO ;; +esac +set +u + +set +u +: $(command set -u) +case $- in +*u*) echo Error at line $LINENO ;; +esac +set +u + +umask 77 +u1=$(umask) +: $(umask 022) +u2=$(umask) +[ "$u1" = "$u2" ] || echo Error at line $LINENO + +dummy=$(exit 3); [ $? -eq 3 ] || echo Error at line $LINENO diff --git a/corebinutils/sh/tests/expansion/cmdsubst11.0 b/corebinutils/sh/tests/expansion/cmdsubst11.0 new file mode 100644 index 0000000000..57d0114b19 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst11.0 @@ -0,0 +1,4 @@ + +# Not required by POSIX but useful for efficiency. + +[ $$ = $(eval '${SH} -c echo\ \$PPID') ] diff --git a/corebinutils/sh/tests/expansion/cmdsubst12.0 b/corebinutils/sh/tests/expansion/cmdsubst12.0 new file mode 100644 index 0000000000..3f4947dff7 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst12.0 @@ -0,0 +1,5 @@ + +f() { + echo x$(printf foo >&2)y +} +[ "$(f 2>&1)" = "fooxy" ] diff --git a/corebinutils/sh/tests/expansion/cmdsubst13.0 b/corebinutils/sh/tests/expansion/cmdsubst13.0 new file mode 100644 index 0000000000..d6b55ea8a3 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst13.0 @@ -0,0 +1,11 @@ + +x=1 y=2 +[ "$( + case $((x+=1)) in + ($((y+=1))) echo bad1 ;; + ($((y-1))) echo $x.$y ;; + ($((y=2))) echo bad2 ;; + (*) echo bad3 ;; + esac +)" = "2.3" ] || echo "Error at $LINENO" +[ "$x.$y" = "1.2" ] || echo "Error at $LINENO" diff --git a/corebinutils/sh/tests/expansion/cmdsubst14.0 b/corebinutils/sh/tests/expansion/cmdsubst14.0 new file mode 100644 index 0000000000..b09a6b0a25 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst14.0 @@ -0,0 +1,4 @@ + +! v=`false + +` diff --git a/corebinutils/sh/tests/expansion/cmdsubst15.0 b/corebinutils/sh/tests/expansion/cmdsubst15.0 new file mode 100644 index 0000000000..b9857775a3 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst15.0 @@ -0,0 +1,4 @@ + +! v=`false; + +` diff --git a/corebinutils/sh/tests/expansion/cmdsubst16.0 b/corebinutils/sh/tests/expansion/cmdsubst16.0 new file mode 100644 index 0000000000..bbce408f4c --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst16.0 @@ -0,0 +1,4 @@ + +f() { return 3; } +f +[ `echo $?` = 3 ] diff --git a/corebinutils/sh/tests/expansion/cmdsubst17.0 b/corebinutils/sh/tests/expansion/cmdsubst17.0 new file mode 100644 index 0000000000..8157a730f1 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst17.0 @@ -0,0 +1,4 @@ + +f() { return 3; } +f +[ `echo $?; :` = 3 ] diff --git a/corebinutils/sh/tests/expansion/cmdsubst18.0 b/corebinutils/sh/tests/expansion/cmdsubst18.0 new file mode 100644 index 0000000000..f320ca01f0 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst18.0 @@ -0,0 +1,5 @@ + +x=X +unset n +r=${x+$(echo a)}${x-$(echo b)}${n+$(echo c)}${n-$(echo d)}$(echo e) +[ "$r" = aXde ] diff --git a/corebinutils/sh/tests/expansion/cmdsubst19.0 b/corebinutils/sh/tests/expansion/cmdsubst19.0 new file mode 100644 index 0000000000..d4ae49e54d --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst19.0 @@ -0,0 +1,4 @@ + +b=200 c=30 d=5 x=4 +r=$(echo a)$(($(echo b) + ${x+$(echo c)} + ${x-$(echo d)}))$(echo e) +[ "$r" = a234e ] diff --git a/corebinutils/sh/tests/expansion/cmdsubst2.0 b/corebinutils/sh/tests/expansion/cmdsubst2.0 new file mode 100644 index 0000000000..af4b594c6f --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst2.0 @@ -0,0 +1,42 @@ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +check '`echo /et[c]/` = "/etc/"' +check '`printf /var/empty%s /et[c]/` = "/var/empty/etc/"' +check '"`echo /et[c]/`" = "/etc/"' +check '`echo "/et[c]/"` = "/etc/"' +check '`printf /var/empty%s "/et[c]/"` = "/var/empty/et[c]/"' +check '`printf /var/empty/%s \"/et[c]/\"` = "/var/empty/\"/et[c]/\""' +check '"`echo \"/et[c]/\"`" = "/et[c]/"' +check '"`echo "/et[c]/"`" = "/et[c]/"' +check '`echo $$` = $$' +check '"`echo $$`" = $$' +check '`echo \$\$` = $$' +check '"`echo \$\$`" = $$' + +# Command substitutions consisting of a single builtin may be treated +# differently. +check '`:; echo /et[c]/` = "/etc/"' +check '`:; printf /var/empty%s /et[c]/` = "/var/empty/etc/"' +check '"`:; echo /et[c]/`" = "/etc/"' +check '`:; echo "/et[c]/"` = "/etc/"' +check '`:; printf /var/empty%s "/et[c]/"` = "/var/empty/et[c]/"' +check '`:; printf /var/empty/%s \"/et[c]/\"` = "/var/empty/\"/et[c]/\""' +check '"`:; echo \"/et[c]/\"`" = "/et[c]/"' +check '"`:; echo "/et[c]/"`" = "/et[c]/"' +check '`:; echo $$` = $$' +check '"`:; echo $$`" = $$' +check '`:; echo \$\$` = $$' +check '"`:; echo \$\$`" = $$' + +check '`set -f; echo /et[c]/` = "/etc/"' +check '"`set -f; echo /et[c]/`" = "/et[c]/"' + +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/expansion/cmdsubst20.0 b/corebinutils/sh/tests/expansion/cmdsubst20.0 new file mode 100644 index 0000000000..8b759f46a0 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst20.0 @@ -0,0 +1,5 @@ + +set -T +trapped='' +trap "trapped=x$trapped" USR1 +[ "x$(kill -USR1 $$)y" = xy ] && [ "$trapped" = x ] diff --git a/corebinutils/sh/tests/expansion/cmdsubst21.0 b/corebinutils/sh/tests/expansion/cmdsubst21.0 new file mode 100644 index 0000000000..ed38a4e1d7 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst21.0 @@ -0,0 +1,5 @@ + +set -T +trapped='' +trap "trapped=x$trapped" TERM +[ "x$($SH -c "kill $$")y" = xy ] && [ "$trapped" = x ] diff --git a/corebinutils/sh/tests/expansion/cmdsubst22.0 b/corebinutils/sh/tests/expansion/cmdsubst22.0 new file mode 100644 index 0000000000..c647fb205f --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst22.0 @@ -0,0 +1,5 @@ + +set -T +trapped='' +trap "trapped=x$trapped" TERM +[ "x$(:; kill $$)y" = xy ] && [ "$trapped" = x ] diff --git a/corebinutils/sh/tests/expansion/cmdsubst23.0 b/corebinutils/sh/tests/expansion/cmdsubst23.0 new file mode 100644 index 0000000000..97fcad3f9b --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst23.0 @@ -0,0 +1,4 @@ + +unset n +x=abcd +[ "X${n#$(echo a)}X${x#$(echo ab)}X$(echo abc)X" = XXcdXabcX ] diff --git a/corebinutils/sh/tests/expansion/cmdsubst24.0 b/corebinutils/sh/tests/expansion/cmdsubst24.0 new file mode 100644 index 0000000000..baf7841a49 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst24.0 @@ -0,0 +1,23 @@ +# POSIX leaves the effect of NUL bytes in command substitution output +# unspecified but we have always discarded them. + +failures=0 + +check() { + if [ "$2" != "$3" ]; then + printf "Failed at line %s: got \"%s\" expected \"%s\"\n" "$1" "$2" "$3" + : $((failures += 1)) + fi +} + +fmt='\0a\0 \0b\0c d\0' +assign_builtin=$(printf "$fmt") +check "$LINENO" "$assign_builtin" "a bc d" +assign_pipeline=$(printf "$fmt" | cat) +check "$LINENO" "$assign_pipeline" "a bc d" +set -- $(printf "$fmt") $(printf "$fmt" | cat) "$(printf "$fmt")" "$(printf "$fmt" | cat)" +IFS=@ +splits="$*" +check "$LINENO" "$splits" "a@bc@d@a@bc@d@a bc d@a bc d" + +[ "$failures" = 0 ] diff --git a/corebinutils/sh/tests/expansion/cmdsubst25.0 b/corebinutils/sh/tests/expansion/cmdsubst25.0 new file mode 100644 index 0000000000..4804602b9c --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst25.0 @@ -0,0 +1,6 @@ + +IFS=' ' +set -- `printf '\n '` +IFS=: +[ "$*" = ' +' ] diff --git a/corebinutils/sh/tests/expansion/cmdsubst26.0 b/corebinutils/sh/tests/expansion/cmdsubst26.0 new file mode 100644 index 0000000000..25aa4b5a6e --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst26.0 @@ -0,0 +1,5 @@ + +nl=' +' +v=$nl`printf '\n'` +[ "$v" = "$nl" ] diff --git a/corebinutils/sh/tests/expansion/cmdsubst3.0 b/corebinutils/sh/tests/expansion/cmdsubst3.0 new file mode 100644 index 0000000000..48dcf028b5 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst3.0 @@ -0,0 +1,22 @@ + +unset LC_CTYPE LC_COLLATE LANG LANGUAGE +export LC_ALL=C + +e= +for i in 0 1 2 3; do + for j in 0 1 2 3 4 5 6 7; do + for k in 0 1 2 3 4 5 6 7; do + case $i$j$k in + 000) continue ;; + esac + e="$e\n\\$i$j$k" + done + done +done +e1=$(printf "$e") +e2="$(printf "$e")" +[ "${#e1}" = 510 ] || echo length bad +[ "$e1" = "$e2" ] || echo e1 != e2 +[ "$e1" = "$(printf "$e")" ] || echo quoted bad +IFS= +[ "$e1" = $(printf "$e") ] || echo unquoted bad diff --git a/corebinutils/sh/tests/expansion/cmdsubst4.0 b/corebinutils/sh/tests/expansion/cmdsubst4.0 new file mode 100644 index 0000000000..95db034ba7 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst4.0 @@ -0,0 +1,3 @@ + +exec 2>/dev/null +! y=$(: </var/empty/nonexistent) diff --git a/corebinutils/sh/tests/expansion/cmdsubst5.0 b/corebinutils/sh/tests/expansion/cmdsubst5.0 new file mode 100644 index 0000000000..a6b467ebf9 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst5.0 @@ -0,0 +1,4 @@ + +unset v +exec 2>/dev/null +! y=$(: ${v?}) diff --git a/corebinutils/sh/tests/expansion/cmdsubst6.0 b/corebinutils/sh/tests/expansion/cmdsubst6.0 new file mode 100644 index 0000000000..43a5d6240f --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst6.0 @@ -0,0 +1,52 @@ +# This tests if the cmdsubst optimization is still used if possible. + +failures='' +ok='' + +testcase() { + code="$1" + + unset v + eval "pid=\$(dummy=$code echo \$(\$SH -c echo\ \\\$PPID))" + + if [ "$pid" = "$$" ]; then + ok=x$ok + else + failures=x$failures + echo "Failure for $code" + fi +} + +unset v +w=1 +testcase '$w' +testcase '1${w+1}' +testcase '1${w-1}' +testcase '1${v+1}' +testcase '1${v-1}' +testcase '1${w:+1}' +testcase '1${w:-1}' +testcase '1${v:+1}' +testcase '1${v:-1}' +testcase '${w?}' +testcase '${w:?}' +testcase '${w#x}' +testcase '${w##x}' +testcase '${w%x}' +testcase '${w%%x}' + +testcase '$((w))' +testcase '$(((w+4)*2/3))' +testcase '$((w==1))' +testcase '$((w>=0 && w<=5 && w!=2))' +testcase '$((${#w}))' +testcase '$((${#IFS}))' +testcase '$((${#w}>=1))' +testcase '$(($$))' +testcase '$(($#))' +testcase '$(($?))' + +testcase '$(: $((w=4)))' +testcase '$(: ${v=2})' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/cmdsubst7.0 b/corebinutils/sh/tests/expansion/cmdsubst7.0 new file mode 100644 index 0000000000..c767ac465f --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst7.0 @@ -0,0 +1,30 @@ + +failures='' +ok='' + +testcase() { + code="$1" + + unset v + eval ": \$($code)" + + if [ "${v:+bad}" = "" ]; then + ok=x$ok + else + failures=x$failures + echo "Failure for $code" + fi +} + +testcase ': ${v=0}' +testcase ': ${v:=0}' +testcase ': $((v=1))' +testcase ': $((v+=1))' +w='v=1' +testcase ': $(($w))' +testcase ': $((${$+v=1}))' +testcase ': $((v${$+=1}))' +testcase ': $((v $(echo =) 1))' +testcase ': $(($(echo $w)))' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/cmdsubst8.0 b/corebinutils/sh/tests/expansion/cmdsubst8.0 new file mode 100644 index 0000000000..fe8330a1bd --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst8.0 @@ -0,0 +1,16 @@ +# Not required by POSIX (although referenced in a non-normative section), +# but possibly useful. + +: hi there & +p=$! +q=$(jobs -l $p) + +# Change tabs to spaces. +set -f +set -- $q +r="$*" + +case $r in +*" $p "*) ;; +*) echo Pid missing; exit 3 ;; +esac diff --git a/corebinutils/sh/tests/expansion/cmdsubst9.0 b/corebinutils/sh/tests/expansion/cmdsubst9.0 new file mode 100644 index 0000000000..e512c6b869 --- /dev/null +++ b/corebinutils/sh/tests/expansion/cmdsubst9.0 @@ -0,0 +1,10 @@ + +set -e + +cd / +dummy=$(cd /bin) +[ "$(pwd)" = / ] + +v=1 +dummy=$(eval v=2) +[ "$v" = 1 ] diff --git a/corebinutils/sh/tests/expansion/export1.0 b/corebinutils/sh/tests/expansion/export1.0 new file mode 100644 index 0000000000..6b969c79e3 --- /dev/null +++ b/corebinutils/sh/tests/expansion/export1.0 @@ -0,0 +1,12 @@ + +w='@ vv=6' + +v=0 vv=0 +export \v=$w +[ "$v" = "@" ] || echo "Expected @ got $v" +[ "$vv" = "6" ] || echo "Expected 6 got $vv" + +HOME=/known/value + +export \v=~ +[ "$v" = \~ ] || echo "Expected ~ got $v" diff --git a/corebinutils/sh/tests/expansion/export2.0 b/corebinutils/sh/tests/expansion/export2.0 new file mode 100644 index 0000000000..2959e5684a --- /dev/null +++ b/corebinutils/sh/tests/expansion/export2.0 @@ -0,0 +1,23 @@ + +w='@ @' +check() { + [ "$v" = "$w" ] || echo "Expected $w got $v" +} + +export v=$w +check + +HOME=/known/value +check() { + [ "$v" = ~ ] || echo "Expected $HOME got $v" +} + +export v=~ +check + +check() { + [ "$v" = "x:$HOME" ] || echo "Expected x:$HOME got $v" +} + +export v=x:~ +check diff --git a/corebinutils/sh/tests/expansion/export3.0 b/corebinutils/sh/tests/expansion/export3.0 new file mode 100644 index 0000000000..94f55c9bd6 --- /dev/null +++ b/corebinutils/sh/tests/expansion/export3.0 @@ -0,0 +1,29 @@ + +w='@ @' +check() { + [ "$v" = "$w" ] || echo "Expected $w got $v" +} + +command export v=$w +check +command command export v=$w +check + +HOME=/known/value +check() { + [ "$v" = ~ ] || echo "Expected $HOME got $v" +} + +command export v=~ +check +command command export v=~ +check + +check() { + [ "$v" = "x:$HOME" ] || echo "Expected x:$HOME got $v" +} + +command export v=x:~ +check +command command export v=x:~ +check diff --git a/corebinutils/sh/tests/expansion/heredoc1.0 b/corebinutils/sh/tests/expansion/heredoc1.0 new file mode 100644 index 0000000000..ade91b091b --- /dev/null +++ b/corebinutils/sh/tests/expansion/heredoc1.0 @@ -0,0 +1,24 @@ + +f() { return $1; } + +[ `f 42; { cat; } <<EOF +$? +EOF +` = 42 ] || echo compound command bad + +[ `f 42; (cat) <<EOF +$? +EOF +` = 42 ] || echo subshell bad + +long=`printf %08192d 0` + +[ `f 42; { cat; } <<EOF +$long.$? +EOF +` = $long.42 ] || echo long compound command bad + +[ `f 42; (cat) <<EOF +$long.$? +EOF +` = $long.42 ] || echo long subshell bad diff --git a/corebinutils/sh/tests/expansion/heredoc2.0 b/corebinutils/sh/tests/expansion/heredoc2.0 new file mode 100644 index 0000000000..b8ce605590 --- /dev/null +++ b/corebinutils/sh/tests/expansion/heredoc2.0 @@ -0,0 +1,14 @@ + +f() { return $1; } + +[ `f 42; cat <<EOF +$? +EOF +` = 42 ] || echo simple command bad + +long=`printf %08192d 0` + +[ `f 42; cat <<EOF +$long.$? +EOF +` = $long.42 ] || echo long simple command bad diff --git a/corebinutils/sh/tests/expansion/ifs1.0 b/corebinutils/sh/tests/expansion/ifs1.0 new file mode 100644 index 0000000000..e803d63ce6 --- /dev/null +++ b/corebinutils/sh/tests/expansion/ifs1.0 @@ -0,0 +1,34 @@ + +c=: e= s=' ' +failures='' +ok='' + +check_result() { + if [ "x$2" = "x$3" ]; then + ok=x$ok + else + failures=x$failures + echo "For $1, expected $3 actual $2" + fi +} + +IFS=' +' +set -- a '' +set -- "$@" +check_result 'set -- "$@"' "($#)($1)($2)" "(2)(a)()" + +set -- a '' +set -- "$@"$e +check_result 'set -- "$@"$e' "($#)($1)($2)" "(2)(a)()" + +set -- a '' +set -- "$@"$s +check_result 'set -- "$@"$s' "($#)($1)($2)" "(2)(a)()" + +IFS="$c" +set -- a '' +set -- "$@"$c +check_result 'set -- "$@"$c' "($#)($1)($2)" "(2)(a)()" + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/ifs2.0 b/corebinutils/sh/tests/expansion/ifs2.0 new file mode 100644 index 0000000000..35e76943de --- /dev/null +++ b/corebinutils/sh/tests/expansion/ifs2.0 @@ -0,0 +1,23 @@ + +failures=0 +i=1 +set -f +while [ "$i" -le 127 ]; do + # A different byte still in the range 1..127. + i2=$((i^2+(i==2))) + # Add a character to work around command substitution's removal of + # final newlines, then remove it again. + c=$(printf \\"$(printf %o@ "$i")") + c=${c%@} + c2=$(printf \\"$(printf %o@ "$i2")") + c2=${c2%@} + IFS=$c + set -- $c2$c$c2$c$c2 + if [ "$#" -ne 3 ] || [ "$1" != "$c2" ] || [ "$2" != "$c2" ] || + [ "$3" != "$c2" ]; then + echo "Bad results for separator $i (word $i2)" >&2 + : $((failures += 1)) + fi + i=$((i+1)) +done +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/expansion/ifs3.0 b/corebinutils/sh/tests/expansion/ifs3.0 new file mode 100644 index 0000000000..c5665d56ba --- /dev/null +++ b/corebinutils/sh/tests/expansion/ifs3.0 @@ -0,0 +1,20 @@ + +failures=0 +unset LC_CTYPE LC_COLLATE LANG LANGUAGE +export LC_ALL=C +i=128 +set -f +while [ "$i" -le 255 ]; do + i2=$((i^2)) + c=$(printf \\"$(printf %o "$i")") + c2=$(printf \\"$(printf %o "$i2")") + IFS=$c + set -- $c2$c$c2$c$c2 + if [ "$#" -ne 3 ] || [ "$1" != "$c2" ] || [ "$2" != "$c2" ] || + [ "$3" != "$c2" ]; then + echo "Bad results for separator $i (word $i2)" >&2 + : $((failures += 1)) + fi + i=$((i+1)) +done +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/expansion/ifs4.0 b/corebinutils/sh/tests/expansion/ifs4.0 new file mode 100644 index 0000000000..6ce097ede6 --- /dev/null +++ b/corebinutils/sh/tests/expansion/ifs4.0 @@ -0,0 +1,38 @@ + +c=: e= s=' ' +failures='' +ok='' + +check_result() { + if [ "x$2" = "x$3" ]; then + ok=x$ok + else + failures=x$failures + echo "For $1, expected $3 actual $2" + fi +} + +IFS=' +' +set -- a b '' c +set -- $@ +check_result 'set -- $@' "($#)($1)($2)($3)($4)" "(3)(a)(b)(c)()" + +IFS='' +set -- a b '' c +set -- $@ +check_result 'set -- $@' "($#)($1)($2)($3)($4)" "(3)(a)(b)(c)()" + +set -- a b '' c +set -- $* +check_result 'set -- $*' "($#)($1)($2)($3)($4)" "(3)(a)(b)(c)()" + +set -- a b '' c +set -- "$@" +check_result 'set -- "$@"' "($#)($1)($2)($3)($4)" "(4)(a)(b)()(c)" + +set -- a b '' c +set -- "$*" +check_result 'set -- "$*"' "($#)($1)($2)($3)($4)" "(1)(abc)()()()" + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/ifs5.0 b/corebinutils/sh/tests/expansion/ifs5.0 new file mode 100644 index 0000000000..6fdf6ebbdf --- /dev/null +++ b/corebinutils/sh/tests/expansion/ifs5.0 @@ -0,0 +1,3 @@ + +set -- $(echo a b c d) +[ "$#" = 4 ] diff --git a/corebinutils/sh/tests/expansion/ifs6.0 b/corebinutils/sh/tests/expansion/ifs6.0 new file mode 100644 index 0000000000..e11a678cf4 --- /dev/null +++ b/corebinutils/sh/tests/expansion/ifs6.0 @@ -0,0 +1,5 @@ + +IFS=': ' +x=': :' +set -- $x +[ "$#|$1|$2|$3" = "2|||" ] diff --git a/corebinutils/sh/tests/expansion/ifs7.0 b/corebinutils/sh/tests/expansion/ifs7.0 new file mode 100644 index 0000000000..13e9419ce4 --- /dev/null +++ b/corebinutils/sh/tests/expansion/ifs7.0 @@ -0,0 +1,4 @@ + +IFS=2 +set -- $((123)) +[ "$#|$1|$2|$3" = "2|1|3|" ] diff --git a/corebinutils/sh/tests/expansion/length1.0 b/corebinutils/sh/tests/expansion/length1.0 new file mode 100644 index 0000000000..48317c44f8 --- /dev/null +++ b/corebinutils/sh/tests/expansion/length1.0 @@ -0,0 +1,11 @@ + +v=abcd +[ "${#v}" = 4 ] || echo '${#v} wrong' +v=$$ +[ "${#$}" = "${#v}" ] || echo '${#$} wrong' +[ "${#!}" = 0 ] || echo '${#!} wrong' +set -- 01 2 3 4 5 6 7 8 9 10 11 12 0013 +[ "${#1}" = 2 ] || echo '${#1} wrong' +[ "${#13}" = 4 ] || echo '${#13} wrong' +v=$0 +[ "${#0}" = "${#v}" ] || echo '${#0} wrong' diff --git a/corebinutils/sh/tests/expansion/length2.0 b/corebinutils/sh/tests/expansion/length2.0 new file mode 100644 index 0000000000..1e74564eba --- /dev/null +++ b/corebinutils/sh/tests/expansion/length2.0 @@ -0,0 +1,3 @@ + +v=$- +[ "${#-}" = "${#v}" ] || echo '${#-} wrong' diff --git a/corebinutils/sh/tests/expansion/length3.0 b/corebinutils/sh/tests/expansion/length3.0 new file mode 100644 index 0000000000..fe3e38e5c3 --- /dev/null +++ b/corebinutils/sh/tests/expansion/length3.0 @@ -0,0 +1,9 @@ + +set -- 1 2 3 4 5 6 7 8 9 10 11 12 13 +[ "$#" = 13 ] || echo '$# wrong' +[ "${#}" = 13 ] || echo '${#} wrong' +[ "${##}" = 2 ] || echo '${##} wrong' +set -- +[ "$#" = 0 ] || echo '$# wrong' +[ "${#}" = 0 ] || echo '${#} wrong' +[ "${##}" = 1 ] || echo '${##} wrong' diff --git a/corebinutils/sh/tests/expansion/length4.0 b/corebinutils/sh/tests/expansion/length4.0 new file mode 100644 index 0000000000..5c032d47ff --- /dev/null +++ b/corebinutils/sh/tests/expansion/length4.0 @@ -0,0 +1,10 @@ + +# The construct ${#?} is ambiguous in POSIX.1-2008: it could be the length +# of $? or it could be $# giving an error in the (impossible) case that it +# is not set. +# We use the former interpretation; it seems more useful. + +: +[ "${#?}" = 1 ] || echo '${#?} wrong' +(exit 42) +[ "${#?}" = 2 ] || echo '${#?} wrong' diff --git a/corebinutils/sh/tests/expansion/length5.0 b/corebinutils/sh/tests/expansion/length5.0 new file mode 100644 index 0000000000..b8aefa5244 --- /dev/null +++ b/corebinutils/sh/tests/expansion/length5.0 @@ -0,0 +1,26 @@ + +unset LC_CTYPE LC_COLLATE LANG LANGUAGE +LC_ALL=C +export LC_ALL + +e= +for i in 0 1 2 3; do + for j in 0 1 2 3 4 5 6 7; do + for k in 0 1 2 3 4 5 6 7; do + case $i$j$k in + 000) continue ;; + esac + e="$e\\$i$j$k" + done + done +done +ee=`printf "$e"` +[ ${#ee} = 255 ] || echo bad 1 +[ "${#ee}" = 255 ] || echo bad 2 +[ $((${#ee})) = 255 ] || echo bad 3 +[ "$((${#ee}))" = 255 ] || echo bad 4 +set -- "$ee" +[ ${#1} = 255 ] || echo bad 5 +[ "${#1}" = 255 ] || echo bad 6 +[ $((${#1})) = 255 ] || echo bad 7 +[ "$((${#1}))" = 255 ] || echo bad 8 diff --git a/corebinutils/sh/tests/expansion/length6.0 b/corebinutils/sh/tests/expansion/length6.0 new file mode 100644 index 0000000000..be719874dc --- /dev/null +++ b/corebinutils/sh/tests/expansion/length6.0 @@ -0,0 +1,7 @@ + +x='!@#$%^&*()[]' +[ ${#x} = 12 ] || echo bad 1 +[ "${#x}" = 12 ] || echo bad 2 +IFS=2 +[ ${#x} = 1 ] || echo bad 3 +[ "${#x}" = 12 ] || echo bad 4 diff --git a/corebinutils/sh/tests/expansion/length7.0 b/corebinutils/sh/tests/expansion/length7.0 new file mode 100644 index 0000000000..8bb128ba26 --- /dev/null +++ b/corebinutils/sh/tests/expansion/length7.0 @@ -0,0 +1,13 @@ + +unset LC_ALL +LC_CTYPE=en_US.UTF-8 +export LC_CTYPE + +# a umlaut +s=$(printf '\303\244') +# euro sign +s=$s$(printf '\342\202\254') +# some sort of 't' outside BMP +s=$s$(printf '\360\235\225\245') +set -- "$s" +[ ${#s} = 3 ] && [ ${#1} = 3 ] diff --git a/corebinutils/sh/tests/expansion/length8.0 b/corebinutils/sh/tests/expansion/length8.0 new file mode 100644 index 0000000000..a161bb1374 --- /dev/null +++ b/corebinutils/sh/tests/expansion/length8.0 @@ -0,0 +1,13 @@ + +unset LC_CTYPE LC_COLLATE LANG LANGUAGE +LC_ALL=C +export LC_ALL + +# a umlaut +s=$(printf '\303\244') +# euro sign +s=$s$(printf '\342\202\254') +# some sort of 't' outside BMP +s=$s$(printf '\360\235\225\245') +set -- "$s" +[ ${#s} = 9 ] && [ ${#1} = 9 ] diff --git a/corebinutils/sh/tests/expansion/local1.0 b/corebinutils/sh/tests/expansion/local1.0 new file mode 100644 index 0000000000..745f49f2d5 --- /dev/null +++ b/corebinutils/sh/tests/expansion/local1.0 @@ -0,0 +1,27 @@ + +run_test() { + w='@ @' + check() { + [ "$v" = "$w" ] || echo "Expected $w got $v" + } + + local v=$w + check + + HOME=/known/value + check() { + [ "$v" = ~ ] || echo "Expected $HOME got $v" + } + + local v=~ + check + + check() { + [ "$v" = "x:$HOME" ] || echo "Expected x:$HOME got $v" + } + + local v=x:~ + check +} + +run_test diff --git a/corebinutils/sh/tests/expansion/local2.0 b/corebinutils/sh/tests/expansion/local2.0 new file mode 100644 index 0000000000..365366e399 --- /dev/null +++ b/corebinutils/sh/tests/expansion/local2.0 @@ -0,0 +1,33 @@ + +run_test() { + w='@ @' + check() { + [ "$v" = "$w" ] || echo "Expected $w got $v" + } + + command local v=$w + check + command command local v=$w + check + + HOME=/known/value + check() { + [ "$v" = ~ ] || echo "Expected $HOME got $v" + } + + command local v=~ + check + command command local v=~ + check + + check() { + [ "$v" = "x:$HOME" ] || echo "Expected x:$HOME got $v" + } + + command local v=x:~ + check + command command local v=x:~ + check +} + +run_test diff --git a/corebinutils/sh/tests/expansion/pathname1.0 b/corebinutils/sh/tests/expansion/pathname1.0 new file mode 100644 index 0000000000..1a3227d0d5 --- /dev/null +++ b/corebinutils/sh/tests/expansion/pathname1.0 @@ -0,0 +1,64 @@ + +unset LC_ALL +LC_COLLATE=C +export LC_COLLATE + +failures=0 + +check() { + testcase=$1 + expect=$2 + eval "set -- $testcase" + actual="$*" + if [ "$actual" != "$expect" ]; then + failures=$((failures+1)) + printf '%s\n' "For $testcase, expected $expect actual $actual" + fi +} + +set -e +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) +trap 'rm -rf $T' 0 +cd -P $T + +mkdir testdir testdir2 'testdir/*' 'testdir/?' testdir/a testdir/b testdir2/b +mkdir testdir2/.c +touch testf 'testdir/*/1' 'testdir/?/1' testdir/a/1 testdir/b/1 testdir2/b/.a + +check '' '' +check 'testdir/b' 'testdir/b' +check 'testdir/c' 'testdir/c' +check '\*' '*' +check '\?' '?' +check '*' 'testdir testdir2 testf' +check '*""' 'testdir testdir2 testf' +check '""*' 'testdir testdir2 testf' +check '*/' 'testdir/ testdir2/' +check 'testdir*/a' 'testdir/a' +check 'testdir*/b' 'testdir/b testdir2/b' +check '*/.c' 'testdir2/.c' +check 'testdir2/*' 'testdir2/b' +check 'testdir2/b/*' 'testdir2/b/*' +check 'testdir/*' 'testdir/* testdir/? testdir/a testdir/b' +check 'testdir/*/1' 'testdir/*/1 testdir/?/1 testdir/a/1 testdir/b/1' +check '"testdir/"*/1' 'testdir/*/1 testdir/?/1 testdir/a/1 testdir/b/1' +check 'testdir/\*/*' 'testdir/*/1' +check 'testdir/\?/*' 'testdir/?/1' +check 'testdir/"?"/*' 'testdir/?/1' +check '"testdir"/"?"/*' 'testdir/?/1' +check '"testdir"/"?"*/*' 'testdir/?/1' +check '"testdir"/*"?"/*' 'testdir/?/1' +check '"testdir/?"*/*' 'testdir/?/1' +check 'testdir/\*/' 'testdir/*/' +check 'testdir/\?/' 'testdir/?/' +check 'testdir/"?"/' 'testdir/?/' +check '"testdir"/"?"/' 'testdir/?/' +check '"testdir"/"?"*/' 'testdir/?/' +check '"testdir"/*"?"/' 'testdir/?/' +check '"testdir/?"*/' 'testdir/?/' +check 'testdir/[*]/' 'testdir/*/' +check 'testdir/[?]/' 'testdir/?/' +check 'testdir/[*?]/' 'testdir/*/ testdir/?/' +check '[tz]estdir/[*]/' 'testdir/*/' + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/expansion/pathname2.0 b/corebinutils/sh/tests/expansion/pathname2.0 new file mode 100644 index 0000000000..69b8f7050c --- /dev/null +++ b/corebinutils/sh/tests/expansion/pathname2.0 @@ -0,0 +1,34 @@ + +unset LC_ALL +LC_COLLATE=C +export LC_COLLATE + +failures=0 + +check() { + testcase=$1 + expect=$2 + eval "set -- $testcase" + actual="$*" + if [ "$actual" != "$expect" ]; then + failures=$((failures+1)) + printf '%s\n' "For $testcase, expected $expect actual $actual" + fi +} + +set -e +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) +trap 'rm -rf $T' 0 +cd -P $T + +mkdir testdir testdir2 'testdir/*' 'testdir/?' testdir/a testdir/b testdir2/b +mkdir testdir2/.c +touch testf 'testdir/*/1' 'testdir/?/1' testdir/a/1 testdir/b/1 testdir2/b/.a + +check '*\/' 'testdir/ testdir2/' +check '"testdir/"*"/1"' 'testdir/*/1 testdir/?/1 testdir/a/1 testdir/b/1' +check '"testdir/"*"/"*' 'testdir/*/1 testdir/?/1 testdir/a/1 testdir/b/1' +check '"testdir/"*\/*' 'testdir/*/1 testdir/?/1 testdir/a/1 testdir/b/1' +check '"testdir"*"/"*"/"*' 'testdir/*/1 testdir/?/1 testdir/a/1 testdir/b/1' + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/expansion/pathname3.0 b/corebinutils/sh/tests/expansion/pathname3.0 new file mode 100644 index 0000000000..51f223dcf7 --- /dev/null +++ b/corebinutils/sh/tests/expansion/pathname3.0 @@ -0,0 +1,28 @@ + +v=12345678 +v=$v$v$v$v +v=$v$v$v$v +v=$v$v$v$v +v=$v$v$v$v +v=$v$v$v$v +# 8192 bytes +v=${v##???} +[ /*/$v = "/*/$v" ] || exit 1 + +s=//// +s=$s$s$s$s +s=$s$s$s$s +s=$s$s$s$s +s=$s$s$s$s +# 1024 bytes +s=${s##??????????} +[ /var/empt[y]/$s/$v = "/var/empt[y]/$s/$v" ] || exit 2 +while [ ${#s} -lt 1034 ]; do + set -- /.${s}et[c] + [ ${#s} -gt 1018 ] || [ "$1" = /.${s}etc ] || exit 3 + set -- /.${s}et[c]/ + [ ${#s} -gt 1017 ] || [ "$1" = /.${s}etc/ ] || exit 4 + set -- /.${s}et[c]/. + [ ${#s} -gt 1016 ] || [ "$1" = /.${s}etc/. ] || exit 5 + s=$s/ +done diff --git a/corebinutils/sh/tests/expansion/pathname4.0 b/corebinutils/sh/tests/expansion/pathname4.0 new file mode 100644 index 0000000000..5f6b541743 --- /dev/null +++ b/corebinutils/sh/tests/expansion/pathname4.0 @@ -0,0 +1,27 @@ + +failures=0 + +check() { + testcase=$1 + expect=$2 + eval "set -- $testcase" + actual="$*" + if [ "$actual" != "$expect" ]; then + failures=$((failures+1)) + printf '%s\n' "For $testcase, expected $expect actual $actual" + fi +} + +set -e +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) +trap 'rm -rf $T' 0 +cd -P $T + +mkdir !!a +touch !!a/fff + +chmod u-r . +check '!!a/ff*' '!!a/fff' +chmod u+r . + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/expansion/pathname5.0 b/corebinutils/sh/tests/expansion/pathname5.0 new file mode 100644 index 0000000000..7277f7bc1e --- /dev/null +++ b/corebinutils/sh/tests/expansion/pathname5.0 @@ -0,0 +1,2 @@ + +[ `echo '/[e]tc'` = /etc ] diff --git a/corebinutils/sh/tests/expansion/pathname6.0 b/corebinutils/sh/tests/expansion/pathname6.0 new file mode 100644 index 0000000000..41a8860d4a --- /dev/null +++ b/corebinutils/sh/tests/expansion/pathname6.0 @@ -0,0 +1,41 @@ + +failures=0 + +check() { + testcase=$1 + expect=$2 + eval "set -- $testcase" + actual="$*" + if [ "$actual" != "$expect" ]; then + failures=$((failures+1)) + printf '%s\n' "For $testcase, expected $expect actual $actual" + fi +} + +set -e +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) +trap 'rm -rf $T' 0 +cd -P $T + +touch A B a b + +LC_ALL=C +export LC_ALL +unset LC_COLLATE +check '*' 'A B a b' + +unset LC_ALL +LC_COLLATE=en_US.US-ASCII +export LC_COLLATE +set -- * +case "$*" in +'a A b B'|'A B a b') + : + ;; +*) + failures=$((failures + 1)) + printf '%s\n' "For *, expected locale-aware or C collation fallback actual $*" + ;; +esac + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/expansion/plus-minus1.0 b/corebinutils/sh/tests/expansion/plus-minus1.0 new file mode 100644 index 0000000000..6b7f22f612 --- /dev/null +++ b/corebinutils/sh/tests/expansion/plus-minus1.0 @@ -0,0 +1,75 @@ + +e= q='?' a='*' t=texttext s='ast*que?non' p='/et[c]/' w='a b c' b='{{(#)}}' +h='##' +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'set -- a b' '2|a|b' +testcase 'set --' '0|' +testcase 'set -- ${e}' '0|' +testcase 'set -- "${e}"' '1|' + +testcase 'set -- $p' '1|/etc/' +testcase 'set -- "$p"' '1|/et[c]/' +testcase 'set -- ${s+$p}' '1|/etc/' +testcase 'set -- "${s+$p}"' '1|/et[c]/' +testcase 'set -- ${s+"$p"}' '1|/et[c]/' +# Dquotes in dquotes is undefined for Bourne shell operators +#testcase 'set -- "${s+"$p"}"' '1|/et[c]/' +testcase 'set -- ${e:-$p}' '1|/etc/' +testcase 'set -- "${e:-$p}"' '1|/et[c]/' +testcase 'set -- ${e:-"$p"}' '1|/et[c]/' +# Dquotes in dquotes is undefined for Bourne shell operators +#testcase 'set -- "${e:-"$p"}"' '1|/et[c]/' +testcase 'set -- ${e:+"$e"}' '0|' +testcase 'set -- ${e:+$w"$e"}' '0|' +testcase 'set -- ${w:+"$w"}' '1|a b c' +testcase 'set -- ${w:+$w"$w"}' '3|a|b|ca b c' + +testcase 'set -- "${s+a b}"' '1|a b' +testcase 'set -- "${e:-a b}"' '1|a b' +testcase 'set -- ${e:-\}}' '1|}' +testcase 'set -- ${e:+{}}' '1|}' +testcase 'set -- "${e:+{}}"' '1|}' + +testcase 'set -- ${e+x}${e+x}' '1|xx' +testcase 'set -- "${e+x}"${e+x}' '1|xx' +testcase 'set -- ${e+x}"${e+x}"' '1|xx' +testcase 'set -- "${e+x}${e+x}"' '1|xx' +testcase 'set -- "${e+x}""${e+x}"' '1|xx' + +testcase 'set -- ${e:-${e:-$p}}' '1|/etc/' +testcase 'set -- "${e:-${e:-$p}}"' '1|/et[c]/' +testcase 'set -- ${e:-"${e:-$p}"}' '1|/et[c]/' +testcase 'set -- ${e:-${e:-"$p"}}' '1|/et[c]/' +testcase 'set -- ${e:-${e:-${e:-$w}}}' '3|a|b|c' +testcase 'set -- ${e:-${e:-${e:-"$w"}}}' '1|a b c' +testcase 'set -- ${e:-${e:-"${e:-$w}"}}' '1|a b c' +testcase 'set -- ${e:-"${e:-${e:-$w}}"}' '1|a b c' +testcase 'set -- "${e:-${e:-${e:-$w}}}"' '1|a b c' + +testcase 'shift $#; set -- ${1+"$@"}' '0|' +testcase 'set -- ""; set -- ${1+"$@"}' '1|' +testcase 'set -- "" a; set -- ${1+"$@"}' '2||a' +testcase 'set -- a ""; set -- ${1+"$@"}' '2|a|' +testcase 'set -- a b; set -- ${1+"$@"}' '2|a|b' +testcase 'set -- a\ b; set -- ${1+"$@"}' '1|a b' +testcase 'set -- " " ""; set -- ${1+"$@"}' '2| |' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/plus-minus2.0 b/corebinutils/sh/tests/expansion/plus-minus2.0 new file mode 100644 index 0000000000..b2794c5e70 --- /dev/null +++ b/corebinutils/sh/tests/expansion/plus-minus2.0 @@ -0,0 +1,3 @@ + +e= +test "${e:-\}}" = '}' diff --git a/corebinutils/sh/tests/expansion/plus-minus3.0 b/corebinutils/sh/tests/expansion/plus-minus3.0 new file mode 100644 index 0000000000..59c4326bbf --- /dev/null +++ b/corebinutils/sh/tests/expansion/plus-minus3.0 @@ -0,0 +1,43 @@ + +e= q='?' a='*' t=texttext s='ast*que?non' p='/et[c]/' w='a b c' b='{{(#)}}' +h='##' +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +# We follow original ash behaviour for quoted ${var+-=?} expansions: +# a double-quote in one switches back to unquoted state. +# This allows expanding a variable as a single word if it is set +# and substituting multiple words otherwise. +# It is also close to the Bourne and Korn shells. +# POSIX leaves this undefined, and various other shells treat +# such double-quotes as introducing a second level of quoting +# which does not do much except quoting close braces. + +testcase 'set -- "${p+"/et[c]/"}"' '1|/etc/' +testcase 'set -- "${p-"/et[c]/"}"' '1|/et[c]/' +testcase 'set -- "${p+"$p"}"' '1|/etc/' +testcase 'set -- "${p-"$p"}"' '1|/et[c]/' +testcase 'set -- "${p+"""/et[c]/"}"' '1|/etc/' +testcase 'set -- "${p-"""/et[c]/"}"' '1|/et[c]/' +testcase 'set -- "${p+"""$p"}"' '1|/etc/' +testcase 'set -- "${p-"""$p"}"' '1|/et[c]/' +testcase 'set -- "${p+"\@"}"' '1|@' +testcase 'set -- "${p+"'\''/et[c]/'\''"}"' '1|/et[c]/' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/plus-minus4.0 b/corebinutils/sh/tests/expansion/plus-minus4.0 new file mode 100644 index 0000000000..fc845418b0 --- /dev/null +++ b/corebinutils/sh/tests/expansion/plus-minus4.0 @@ -0,0 +1,37 @@ + +# These may be a bit unclear in the POSIX spec or the proposed revisions, +# and conflict with bash's interpretation, but I think ksh93's interpretation +# makes most sense. In particular, it makes no sense to me that single-quotes +# must match but are not removed. + +e= q='?' a='*' t=texttext s='ast*que?non' p='/et[c]/' w='a b c' b='{{(#)}}' +h='##' +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'set -- ${e:-'"'"'}'"'"'}' '1|}' +testcase "set -- \${e:-\\'}" "1|'" +testcase "set -- \${e:-\\'\\'}" "1|''" +testcase "set -- \"\${e:-'}\"" "1|'" +testcase "set -- \"\${e:-'}'}\"" "1|''}" +testcase "set -- \"\${e:-''}\"" "1|''" +testcase 'set -- ${e:-\a}' '1|a' +testcase 'set -- "${e:-\a}"' '1|\a' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/plus-minus5.0 b/corebinutils/sh/tests/expansion/plus-minus5.0 new file mode 100644 index 0000000000..aa717c74ab --- /dev/null +++ b/corebinutils/sh/tests/expansion/plus-minus5.0 @@ -0,0 +1,30 @@ + +e= q='?' a='*' t=texttext s='ast*que?non' p='/et[c]/' w='a b c' b='{{(#)}}' +h='##' +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'set -- ${e:-"{x}"}' '1|{x}' +testcase 'set -- "${e:-"{x}"}"' '1|{x}' +testcase 'set -- ${h+"{x}"}' '1|{x}' +testcase 'set -- "${h+"{x}"}"' '1|{x}' +testcase 'set -- ${h:-"{x}"}' '1|##' +testcase 'set -- "${h:-"{x}"}"' '1|##' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/plus-minus6.0 b/corebinutils/sh/tests/expansion/plus-minus6.0 new file mode 100644 index 0000000000..24301627de --- /dev/null +++ b/corebinutils/sh/tests/expansion/plus-minus6.0 @@ -0,0 +1,33 @@ + +failures=0 +unset LC_CTYPE LC_COLLATE LANG LANGUAGE +export LC_ALL=C +nl=' +' +i=1 +set -f +while [ "$i" -le 255 ]; do + # A different byte still in the range 1..255. + i2=$((i^2+(i==2))) + # Add a character to work around command substitution's removal of + # final newlines, then remove it again. + c=$(printf \\"$(printf %o@ "$i")") + c=${c%@} + c2=$(printf \\"$(printf %o@ "$i2")") + c2=${c2%@} + case $c in + [\'$nl'$}();&|\"`']) c=M + esac + case $c2 in + [\'$nl'$}();&|\"`']) c2=N + esac + IFS=$c + command eval "set -- \${\$+$c2$c$c2$c$c2}" + if [ "$#" -ne 3 ] || [ "$1" != "$c2" ] || [ "$2" != "$c2" ] || + [ "$3" != "$c2" ]; then + echo "Bad results for separator $i (word $i2)" >&2 + : $((failures += 1)) + fi + i=$((i+1)) +done +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/expansion/plus-minus7.0 b/corebinutils/sh/tests/expansion/plus-minus7.0 new file mode 100644 index 0000000000..65c6eb98a3 --- /dev/null +++ b/corebinutils/sh/tests/expansion/plus-minus7.0 @@ -0,0 +1,25 @@ + +e= s='foo' +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'set -- ${s+a b}' '2|a|b' +testcase 'set -- ${e:-a b}' '2|a|b' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/plus-minus8.0 b/corebinutils/sh/tests/expansion/plus-minus8.0 new file mode 100644 index 0000000000..ef8de8a469 --- /dev/null +++ b/corebinutils/sh/tests/expansion/plus-minus8.0 @@ -0,0 +1,4 @@ + +set -- 1 2 3 4 5 6 7 8 9 10 11 12 13 +[ "${#+hi}" = hi ] || echo '${#+hi} wrong' +[ "${#-hi}" = 13 ] || echo '${#-hi} wrong' diff --git a/corebinutils/sh/tests/expansion/plus-minus9.0 b/corebinutils/sh/tests/expansion/plus-minus9.0 new file mode 100644 index 0000000000..1fa2c92d14 --- /dev/null +++ b/corebinutils/sh/tests/expansion/plus-minus9.0 @@ -0,0 +1,7 @@ + +a=1 +b=${a+ +} +n=' +' +[ "$b" = "$n" ] diff --git a/corebinutils/sh/tests/expansion/question1.0 b/corebinutils/sh/tests/expansion/question1.0 new file mode 100644 index 0000000000..b9d1b72be6 --- /dev/null +++ b/corebinutils/sh/tests/expansion/question1.0 @@ -0,0 +1,21 @@ + +x=a\ b +[ "$x" = "${x?}" ] || exit 1 +set -- ${x?} +{ [ "$#" = 2 ] && [ "$1" = a ] && [ "$2" = b ]; } || exit 1 +unset x +(echo ${x?abcdefg}) 2>&1 | grep -q abcdefg || exit 1 +${SH} -c 'unset foo; echo ${foo?}' 2>/dev/null && exit 1 +${SH} -c 'foo=; echo ${foo:?}' 2>/dev/null && exit 1 +${SH} -c 'foo=; echo ${foo?}' >/dev/null || exit 1 +${SH} -c 'foo=1; echo ${foo:?}' >/dev/null || exit 1 +${SH} -c 'echo ${!?}' 2>/dev/null && exit 1 +${SH} -c ':& echo ${!?}' >/dev/null || exit 1 +${SH} -c 'echo ${#?}' >/dev/null || exit 1 +${SH} -c 'echo ${*?}' 2>/dev/null && exit 1 +${SH} -c 'echo ${*?}' ${SH} x >/dev/null || exit 1 +${SH} -c 'echo ${1?}' 2>/dev/null && exit 1 +${SH} -c 'echo ${1?}' ${SH} x >/dev/null || exit 1 +${SH} -c 'echo ${2?}' ${SH} x 2>/dev/null && exit 1 +${SH} -c 'echo ${2?}' ${SH} x y >/dev/null || exit 1 +exit 0 diff --git a/corebinutils/sh/tests/expansion/question2.0 b/corebinutils/sh/tests/expansion/question2.0 new file mode 100644 index 0000000000..af7d0db992 --- /dev/null +++ b/corebinutils/sh/tests/expansion/question2.0 @@ -0,0 +1,10 @@ + +unset dummyvar +msg=`(: ${dummyvar?}) 2>&1` +r=$? +[ "$r" != 0 ] && case $msg in +*dummyvar?* | *?dummyvar*) : ;; +*) + printf 'Bad message: [%s]\n' "$msg" + exit 1 +esac diff --git a/corebinutils/sh/tests/expansion/readonly1.0 b/corebinutils/sh/tests/expansion/readonly1.0 new file mode 100644 index 0000000000..5ced727a4f --- /dev/null +++ b/corebinutils/sh/tests/expansion/readonly1.0 @@ -0,0 +1,6 @@ + +w='@ @' + +v=0 HOME=/known/value +readonly v=~:~/:$w +[ "$v" = "$HOME:$HOME/:$w" ] || echo "Expected $HOME/:$w got $v" diff --git a/corebinutils/sh/tests/expansion/redir1.0 b/corebinutils/sh/tests/expansion/redir1.0 new file mode 100644 index 0000000000..50b841e474 --- /dev/null +++ b/corebinutils/sh/tests/expansion/redir1.0 @@ -0,0 +1,25 @@ + +bad=0 +for i in 0 1 2 3; do + for j in 0 1 2 3 4 5 6 7; do + for k in 0 1 2 3 4 5 6 7; do + case $i$j$k in + 000) continue ;; + esac + set -- "$(printf \\$i$j$k@)" + set -- "${1%@}" + ff= + for f in /dev/null /dev/zero /; do + if [ -e "$f" ] && [ ! -e "$f$1" ]; then + ff=$f + fi + done + [ -n "$ff" ] || continue + if { true <$ff$1; } 2>/dev/null; then + echo "Bad: $i$j$k ($ff)" >&2 + : $((bad += 1)) + fi + done + done +done +exit $((bad ? 2 : 0)) diff --git a/corebinutils/sh/tests/expansion/set-u1.0 b/corebinutils/sh/tests/expansion/set-u1.0 new file mode 100644 index 0000000000..da31fe6bac --- /dev/null +++ b/corebinutils/sh/tests/expansion/set-u1.0 @@ -0,0 +1,28 @@ + +${SH} -uc 'unset foo; echo $foo' 2>/dev/null && exit 1 +${SH} -uc 'foo=; echo $foo' >/dev/null || exit 1 +${SH} -uc 'foo=1; echo $foo' >/dev/null || exit 1 +# -/+/= are unaffected by set -u +${SH} -uc 'unset foo; echo ${foo-}' >/dev/null || exit 1 +${SH} -uc 'unset foo; echo ${foo+}' >/dev/null || exit 1 +${SH} -uc 'unset foo; echo ${foo=}' >/dev/null || exit 1 +# length/trimming are affected +${SH} -uc 'unset foo; echo ${#foo}' 2>/dev/null && exit 1 +${SH} -uc 'foo=; echo ${#foo}' >/dev/null || exit 1 +${SH} -uc 'unset foo; echo ${foo#?}' 2>/dev/null && exit 1 +${SH} -uc 'foo=1; echo ${foo#?}' >/dev/null || exit 1 +${SH} -uc 'unset foo; echo ${foo##?}' 2>/dev/null && exit 1 +${SH} -uc 'foo=1; echo ${foo##?}' >/dev/null || exit 1 +${SH} -uc 'unset foo; echo ${foo%?}' 2>/dev/null && exit 1 +${SH} -uc 'foo=1; echo ${foo%?}' >/dev/null || exit 1 +${SH} -uc 'unset foo; echo ${foo%%?}' 2>/dev/null && exit 1 +${SH} -uc 'foo=1; echo ${foo%%?}' >/dev/null || exit 1 + +${SH} -uc 'echo $!' 2>/dev/null && exit 1 +${SH} -uc ':& echo $!' >/dev/null || exit 1 +${SH} -uc 'echo $#' >/dev/null || exit 1 +${SH} -uc 'echo $1' 2>/dev/null && exit 1 +${SH} -uc 'echo $1' ${SH} x >/dev/null || exit 1 +${SH} -uc 'echo $2' ${SH} x 2>/dev/null && exit 1 +${SH} -uc 'echo $2' ${SH} x y >/dev/null || exit 1 +exit 0 diff --git a/corebinutils/sh/tests/expansion/set-u2.0 b/corebinutils/sh/tests/expansion/set-u2.0 new file mode 100644 index 0000000000..138f7bc275 --- /dev/null +++ b/corebinutils/sh/tests/expansion/set-u2.0 @@ -0,0 +1,11 @@ + +set -u +: $* $@ "$@" "$*" +set -- x +: $* $@ "$@" "$*" +shift $# +: $* $@ "$@" "$*" +set -- y +set -- +: $* $@ "$@" "$*" +exit 0 diff --git a/corebinutils/sh/tests/expansion/set-u3.0 b/corebinutils/sh/tests/expansion/set-u3.0 new file mode 100644 index 0000000000..bd36445035 --- /dev/null +++ b/corebinutils/sh/tests/expansion/set-u3.0 @@ -0,0 +1,5 @@ + +set -u +unset x +v=$( (eval ': $((x))') 2>&1 >/dev/null) +[ $? -ne 0 ] && [ -n "$v" ] diff --git a/corebinutils/sh/tests/expansion/tilde1.0 b/corebinutils/sh/tests/expansion/tilde1.0 new file mode 100644 index 0000000000..66af698016 --- /dev/null +++ b/corebinutils/sh/tests/expansion/tilde1.0 @@ -0,0 +1,55 @@ + +HOME=/tmp +roothome=~root +if [ "$roothome" = "~root" ]; then + echo "~root is not expanded!" + exit 2 +fi + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'set -- ~' '1|/tmp' +testcase 'set -- ~/foo' '1|/tmp/foo' +testcase 'set -- x~' '1|x~' +testcase 'set -- ~root' "1|$roothome" +h=~ +testcase 'set -- "$h"' '1|/tmp' +ooIFS=$IFS +IFS=m +testcase 'set -- ~' '1|/tmp' +testcase 'set -- ~/foo' '1|/tmp/foo' +testcase 'set -- $h' '2|/t|p' +IFS=$ooIFS +t=\~ +testcase 'set -- $t' '1|~' +r=$(cat <<EOF +~ +EOF +) +testcase 'set -- $r' '1|~' +r=$(cat <<EOF +${t+~} +EOF +) +testcase 'set -- $r' '1|~' +r=$(cat <<EOF +${t+~/.} +EOF +) +testcase 'set -- $r' '1|~/.' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/tilde2.0 b/corebinutils/sh/tests/expansion/tilde2.0 new file mode 100644 index 0000000000..0d0cb1c95d --- /dev/null +++ b/corebinutils/sh/tests/expansion/tilde2.0 @@ -0,0 +1,89 @@ + +HOME=/tmp +roothome=~root +if [ "$roothome" = "~root" ]; then + echo "~root is not expanded!" + exit 2 +fi + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'set -- ${$+~}' '1|/tmp' +testcase 'set -- ${$+~/}' '1|/tmp/' +testcase 'set -- ${$+~/foo}' '1|/tmp/foo' +testcase 'set -- ${$+x~}' '1|x~' +testcase 'set -- ${$+~root}' "1|$roothome" +testcase 'set -- ${$+"~"}' '1|~' +testcase 'set -- ${$+"~/"}' '1|~/' +testcase 'set -- ${$+"~/foo"}' '1|~/foo' +testcase 'set -- ${$+"x~"}' '1|x~' +testcase 'set -- ${$+"~root"}' "1|~root" +testcase 'set -- "${$+~}"' '1|~' +testcase 'set -- "${$+~/}"' '1|~/' +testcase 'set -- "${$+~/foo}"' '1|~/foo' +testcase 'set -- "${$+x~}"' '1|x~' +testcase 'set -- "${$+~root}"' "1|~root" +testcase 'set -- ${HOME#~}' '0|' +h=~ +testcase 'set -- "$h"' '1|/tmp' +f=~/foo +testcase 'set -- "$f"' '1|/tmp/foo' +testcase 'set -- ${f#~}' '1|/foo' +testcase 'set -- ${f#~/}' '1|foo' + +ooIFS=$IFS +IFS=m +testcase 'set -- ${$+~}' '1|/tmp' +testcase 'set -- ${$+~/foo}' '1|/tmp/foo' +testcase 'set -- ${$+$h}' '2|/t|p' +testcase 'set -- ${HOME#~}' '0|' +IFS=$ooIFS + +t=\~ +testcase 'set -- ${$+$t}' '1|~' +r=$(cat <<EOF +${HOME#~} +EOF +) +testcase 'set -- $r' '0|' +r=$(cat <<EOF +${HOME#'~'} +EOF +) +testcase 'set -- $r' '1|/tmp' +r=$(cat <<EOF +${t#'~'} +EOF +) +testcase 'set -- $r' '0|' +r=$(cat <<EOF +${roothome#~root} +EOF +) +testcase 'set -- $r' '0|' +r=$(cat <<EOF +${f#~} +EOF +) +testcase 'set -- $r' '1|/foo' +r=$(cat <<EOF +${f#~/} +EOF +) +testcase 'set -- $r' '1|foo' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/trim1.0 b/corebinutils/sh/tests/expansion/trim1.0 new file mode 100644 index 0000000000..186c087062 --- /dev/null +++ b/corebinutils/sh/tests/expansion/trim1.0 @@ -0,0 +1,84 @@ + +e= q='?' a='*' t=texttext s='ast*que?non' p='/et[c]/' w='a b c' b='{{(#)}}' +h='##' +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'set -- ${t%t}' '1|texttex' +testcase 'set -- "${t%t}"' '1|texttex' +testcase 'set -- ${t%e*}' '1|textt' +testcase 'set -- "${t%e*}"' '1|textt' +testcase 'set -- ${t%%e*}' '1|t' +testcase 'set -- "${t%%e*}"' '1|t' +testcase 'set -- ${t%%*}' '0|' +testcase 'set -- "${t%%*}"' '1|' +testcase 'set -- ${t#t}' '1|exttext' +testcase 'set -- "${t#t}"' '1|exttext' +testcase 'set -- ${t#*x}' '1|ttext' +testcase 'set -- "${t#*x}"' '1|ttext' +testcase 'set -- ${t##*x}' '1|t' +testcase 'set -- "${t##*x}"' '1|t' +testcase 'set -- ${t##*}' '0|' +testcase 'set -- "${t##*}"' '1|' +testcase 'set -- ${t%e$a}' '1|textt' + +set -f +testcase 'set -- ${s%[?]*}' '1|ast*que' +testcase 'set -- "${s%[?]*}"' '1|ast*que' +testcase 'set -- ${s%[*]*}' '1|ast' +testcase 'set -- "${s%[*]*}"' '1|ast' +set +f + +testcase 'set -- $b' '1|{{(#)}}' +testcase 'set -- ${b%\}}' '1|{{(#)}' +testcase 'set -- ${b#{}' '1|{(#)}}' +testcase 'set -- "${b#{}"' '1|{(#)}}' +# Parentheses are special in ksh, check that they can be escaped +testcase 'set -- ${b%\)*}' '1|{{(#' +testcase 'set -- ${b#{}' '1|{(#)}}' +testcase 'set -- $h' '1|##' +testcase 'set -- ${h#\#}' '1|#' +testcase 'set -- ${h###}' '1|#' +testcase 'set -- "${h###}"' '1|#' +testcase 'set -- ${h%#}' '1|#' +testcase 'set -- "${h%#}"' '1|#' + +set -f +testcase 'set -- ${s%"${s#?}"}' '1|a' +testcase 'set -- ${s%"${s#????}"}' '1|ast*' +testcase 'set -- ${s%"${s#????????}"}' '1|ast*que?' +testcase 'set -- ${s#"${s%?}"}' '1|n' +testcase 'set -- ${s#"${s%????}"}' '1|?non' +testcase 'set -- ${s#"${s%????????}"}' '1|*que?non' +set +f +testcase 'set -- "${s%"${s#?}"}"' '1|a' +testcase 'set -- "${s%"${s#????}"}"' '1|ast*' +testcase 'set -- "${s%"${s#????????}"}"' '1|ast*que?' +testcase 'set -- "${s#"${s%?}"}"' '1|n' +testcase 'set -- "${s#"${s%????}"}"' '1|?non' +testcase 'set -- "${s#"${s%????????}"}"' '1|*que?non' +testcase 'set -- ${p#${p}}' '1|/etc/' +testcase 'set -- "${p#${p}}"' '1|/et[c]/' +testcase 'set -- ${p#*[[]}' '1|c]/' +testcase 'set -- "${p#*[[]}"' '1|c]/' +testcase 'set -- ${p#*\[}' '1|c]/' +testcase 'set -- ${p#*"["}' '1|c]/' +testcase 'set -- "${p#*"["}"' '1|c]/' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/trim10.0 b/corebinutils/sh/tests/expansion/trim10.0 new file mode 100644 index 0000000000..1814af1b17 --- /dev/null +++ b/corebinutils/sh/tests/expansion/trim10.0 @@ -0,0 +1,6 @@ + +a='z +' +b=${a% +} +[ "$b" = z ] diff --git a/corebinutils/sh/tests/expansion/trim11.0 b/corebinutils/sh/tests/expansion/trim11.0 new file mode 100644 index 0000000000..041095b66e --- /dev/null +++ b/corebinutils/sh/tests/expansion/trim11.0 @@ -0,0 +1,6 @@ + +a='z +' +b="${a% +}" +[ "$b" = z ] diff --git a/corebinutils/sh/tests/expansion/trim2.0 b/corebinutils/sh/tests/expansion/trim2.0 new file mode 100644 index 0000000000..337bbd4e46 --- /dev/null +++ b/corebinutils/sh/tests/expansion/trim2.0 @@ -0,0 +1,54 @@ + +e= q='?' a='*' t=texttext s='ast*que?non' p='/et[c]/' w='a b c' b='{{(#)}}' +h='##' +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +set -f +testcase 'set -- $s' '1|ast*que?non' +testcase 'set -- ${s%\?*}' '1|ast*que' +testcase 'set -- "${s%\?*}"' '1|ast*que' +testcase 'set -- ${s%\**}' '1|ast' +testcase 'set -- "${s%\**}"' '1|ast' +testcase 'set -- ${s%"$q"*}' '1|ast*que' +testcase 'set -- "${s%"$q"*}"' '1|ast*que' +testcase 'set -- ${s%"$a"*}' '1|ast' +testcase 'set -- "${s%"$a"*}"' '1|ast' +testcase 'set -- ${s%"$q"$a}' '1|ast*que' +testcase 'set -- "${s%"$q"$a}"' '1|ast*que' +testcase 'set -- ${s%"$a"$a}' '1|ast' +testcase 'set -- "${s%"$a"$a}"' '1|ast' +set +f + +testcase 'set -- "${b%\}}"' '1|{{(#)}' +# Parentheses are special in ksh, check that they can be escaped +testcase 'set -- "${b%\)*}"' '1|{{(#' +testcase 'set -- "${h#\#}"' '1|#' + +testcase 'set -- ${p%"${p#?}"}' '1|/' +testcase 'set -- ${p%"${p#??????}"}' '1|/etc' +testcase 'set -- ${p%"${p#???????}"}' '1|/etc/' +testcase 'set -- "${p%"${p#?}"}"' '1|/' +testcase 'set -- "${p%"${p#??????}"}"' '1|/et[c]' +testcase 'set -- "${p%"${p#???????}"}"' '1|/et[c]/' +testcase 'set -- ${p#"${p}"}' '0|' +testcase 'set -- "${p#"${p}"}"' '1|' +testcase 'set -- "${p#*\[}"' '1|c]/' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/trim3.0 b/corebinutils/sh/tests/expansion/trim3.0 new file mode 100644 index 0000000000..d58ae50c78 --- /dev/null +++ b/corebinutils/sh/tests/expansion/trim3.0 @@ -0,0 +1,45 @@ + +e= q='?' a='*' t=texttext s='ast*que?non' p='/et[c]/' w='a b c' b='{{(#)}}' +h='##' c='\\\\' +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +# This doesn't make much sense, but it fails in dash so I'm adding it here: +testcase 'set -- "${w%${w#???}}"' '1|a b' + +testcase 'set -- ${p#/et[}' '1|c]/' +testcase 'set -- "${p#/et[}"' '1|c]/' +testcase 'set -- "${p%${p#????}}"' '1|/et[' + +testcase 'set -- ${b%'\'}\''}' '1|{{(#)}' + +testcase 'set -- ${c#\\}' '1|\\\' +testcase 'set -- ${c#\\\\}' '1|\\' +testcase 'set -- ${c#\\\\\\}' '1|\' +testcase 'set -- ${c#\\\\\\\\}' '0|' +testcase 'set -- "${c#\\}"' '1|\\\' +testcase 'set -- "${c#\\\\}"' '1|\\' +testcase 'set -- "${c#\\\\\\}"' '1|\' +testcase 'set -- "${c#\\\\\\\\}"' '1|' +testcase 'set -- "${c#"$c"}"' '1|' +testcase 'set -- ${c#"$c"}' '0|' +testcase 'set -- "${c%"$c"}"' '1|' +testcase 'set -- ${c%"$c"}' '0|' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/trim4.0 b/corebinutils/sh/tests/expansion/trim4.0 new file mode 100644 index 0000000000..2b0766abcd --- /dev/null +++ b/corebinutils/sh/tests/expansion/trim4.0 @@ -0,0 +1,14 @@ + +v1=/homes/SOME_USER +v2= +v3=C123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 + +# Trigger bug in VSTRIMRIGHT processing STADJUST() call in expand.c:subevalvar() +while [ ${#v2} -lt 2000 ]; do + v4="${v2} ${v1%/*} $v3" + if [ ${#v4} -ne $((${#v2} + ${#v3} + 8)) ]; then + echo bad: ${#v4} -ne $((${#v2} + ${#v3} + 8)) + fi + v2=x$v2 + v3=y$v3 +done diff --git a/corebinutils/sh/tests/expansion/trim5.0 b/corebinutils/sh/tests/expansion/trim5.0 new file mode 100644 index 0000000000..a6131b321e --- /dev/null +++ b/corebinutils/sh/tests/expansion/trim5.0 @@ -0,0 +1,27 @@ + +e= q='?' a='*' t=texttext s='ast*que?non' p='/et[c]/' w='a b c' b='{{(#)}}' +h='##' +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'set -- "${b%'\'}\''}"' '1|{{(#)}' +testcase 'set -- ${b%"}"}' '1|{{(#)}' +testcase 'set -- "${b%"}"}"' '1|{{(#)}' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/trim6.0 b/corebinutils/sh/tests/expansion/trim6.0 new file mode 100644 index 0000000000..b0566b90d4 --- /dev/null +++ b/corebinutils/sh/tests/expansion/trim6.0 @@ -0,0 +1,21 @@ + +e= +for i in 0 1 2 3; do + for j in 0 1 2 3 4 5 6 7; do + for k in 0 1 2 3 4 5 6 7; do + case $i$j$k in + 000) continue ;; + esac + e="$e\\$i$j$k" + done + done +done +e=$(printf "$e") +v=@$e@$e@ +y=${v##*"$e"} +yq="${v##*"$e"}" +[ "$y" = @ ] || echo "error when unquoted in non-splitting context" +[ "$yq" = @ ] || echo "error when quoted in non-splitting context" +[ "${v##*"$e"}" = @ ] || echo "error when quoted in splitting context" +IFS= +[ ${v##*"$e"} = @ ] || echo "error when unquoted in splitting context" diff --git a/corebinutils/sh/tests/expansion/trim7.0 b/corebinutils/sh/tests/expansion/trim7.0 new file mode 100644 index 0000000000..2fcb41535c --- /dev/null +++ b/corebinutils/sh/tests/expansion/trim7.0 @@ -0,0 +1,15 @@ + +set -- 1 2 3 4 5 6 7 8 9 10 11 12 13 +[ "${##1}" = 3 ] || echo '${##1} wrong' +[ "${###1}" = 3 ] || echo '${###1} wrong' +[ "${###}" = 13 ] || echo '${###} wrong' +[ "${#%3}" = 1 ] || echo '${#%3} wrong' +[ "${#%%3}" = 1 ] || echo '${#%%3} wrong' +[ "${#%%}" = 13 ] || echo '${#%%} wrong' +set -- +[ "${##0}" = "" ] || echo '${##0} wrong' +[ "${###0}" = "" ] || echo '${###0} wrong' +[ "${###}" = 0 ] || echo '${###} wrong' +[ "${#%0}" = "" ] || echo '${#%0} wrong' +[ "${#%%0}" = "" ] || echo '${#%%0} wrong' +[ "${#%%}" = 0 ] || echo '${#%%} wrong' diff --git a/corebinutils/sh/tests/expansion/trim8.0 b/corebinutils/sh/tests/expansion/trim8.0 new file mode 100644 index 0000000000..579d78012c --- /dev/null +++ b/corebinutils/sh/tests/expansion/trim8.0 @@ -0,0 +1,74 @@ + +unset LC_ALL +LC_CTYPE=en_US.UTF-8 +export LC_CTYPE + +c1=e +# a umlaut +c2=$(printf '\303\244') +# euro sign +c3=$(printf '\342\202\254') +# some sort of 't' outside BMP +c4=$(printf '\360\235\225\245') + +s=$c1$c2$c3$c4 + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'set -- "$s"' "1|$s" +testcase 'set -- "${s#$c2}"' "1|$s" +testcase 'set -- "${s#*}"' "1|$s" +testcase 'set -- "${s#$c1}"' "1|$c2$c3$c4" +testcase 'set -- "${s#$c1$c2}"' "1|$c3$c4" +testcase 'set -- "${s#$c1$c2$c3}"' "1|$c4" +testcase 'set -- "${s#$c1$c2$c3$c4}"' "1|" +testcase 'set -- "${s#?}"' "1|$c2$c3$c4" +testcase 'set -- "${s#??}"' "1|$c3$c4" +testcase 'set -- "${s#???}"' "1|$c4" +testcase 'set -- "${s#????}"' "1|" +testcase 'set -- "${s#*$c3}"' "1|$c4" +testcase 'set -- "${s%$c4}"' "1|$c1$c2$c3" +testcase 'set -- "${s%$c3$c4}"' "1|$c1$c2" +testcase 'set -- "${s%$c2$c3$c4}"' "1|$c1" +testcase 'set -- "${s%$c1$c2$c3$c4}"' "1|" +testcase 'set -- "${s%?}"' "1|$c1$c2$c3" +testcase 'set -- "${s%??}"' "1|$c1$c2" +testcase 'set -- "${s%???}"' "1|$c1" +testcase 'set -- "${s%????}"' "1|" +testcase 'set -- "${s%$c2*}"' "1|$c1" +testcase 'set -- "${s##$c2}"' "1|$s" +testcase 'set -- "${s##*}"' "1|" +testcase 'set -- "${s##$c1}"' "1|$c2$c3$c4" +testcase 'set -- "${s##$c1$c2}"' "1|$c3$c4" +testcase 'set -- "${s##$c1$c2$c3}"' "1|$c4" +testcase 'set -- "${s##$c1$c2$c3$c4}"' "1|" +testcase 'set -- "${s##?}"' "1|$c2$c3$c4" +testcase 'set -- "${s##??}"' "1|$c3$c4" +testcase 'set -- "${s##???}"' "1|$c4" +testcase 'set -- "${s##????}"' "1|" +testcase 'set -- "${s##*$c3}"' "1|$c4" +testcase 'set -- "${s%%$c4}"' "1|$c1$c2$c3" +testcase 'set -- "${s%%$c3$c4}"' "1|$c1$c2" +testcase 'set -- "${s%%$c2$c3$c4}"' "1|$c1" +testcase 'set -- "${s%%$c1$c2$c3$c4}"' "1|" +testcase 'set -- "${s%%?}"' "1|$c1$c2$c3" +testcase 'set -- "${s%%??}"' "1|$c1$c2" +testcase 'set -- "${s%%???}"' "1|$c1" +testcase 'set -- "${s%%????}"' "1|" +testcase 'set -- "${s%%$c2*}"' "1|$c1" + +test "x$failures" = x diff --git a/corebinutils/sh/tests/expansion/trim9.0 b/corebinutils/sh/tests/expansion/trim9.0 new file mode 100644 index 0000000000..a6314381d3 --- /dev/null +++ b/corebinutils/sh/tests/expansion/trim9.0 @@ -0,0 +1,60 @@ + +# POSIX does not specify these but they occasionally occur in the wild. +# This just serves to keep working what currently works. + +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'shift $#; set -- "${*#Q}"' '1|' +testcase 'shift $#; set -- "${*##Q}"' '1|' +testcase 'shift $#; set -- "${*%Q}"' '1|' +testcase 'shift $#; set -- "${*%%Q}"' '1|' +testcase 'set -- Q R; set -- "${*#Q}"' '1| R' +testcase 'set -- Q R; set -- "${*##Q}"' '1| R' +testcase 'set -- Q R; set -- "${*%R}"' '1|Q ' +testcase 'set -- Q R; set -- "${*%%R}"' '1|Q ' +testcase 'set -- Q R; set -- "${*#S}"' '1|Q R' +testcase 'set -- Q R; set -- "${*##S}"' '1|Q R' +testcase 'set -- Q R; set -- "${*%S}"' '1|Q R' +testcase 'set -- Q R; set -- "${*%%S}"' '1|Q R' +testcase 'set -- Q R; set -- ${*#Q}' '1|R' +testcase 'set -- Q R; set -- ${*##Q}' '1|R' +testcase 'set -- Q R; set -- ${*%R}' '1|Q' +testcase 'set -- Q R; set -- ${*%%R}' '1|Q' +testcase 'set -- Q R; set -- ${*#S}' '2|Q|R' +testcase 'set -- Q R; set -- ${*##S}' '2|Q|R' +testcase 'set -- Q R; set -- ${*%S}' '2|Q|R' +testcase 'set -- Q R; set -- ${*%%S}' '2|Q|R' +testcase 'set -- Q R; set -- ${@#Q}' '1|R' +testcase 'set -- Q R; set -- ${@##Q}' '1|R' +testcase 'set -- Q R; set -- ${@%R}' '1|Q' +testcase 'set -- Q R; set -- ${@%%R}' '1|Q' +testcase 'set -- Q R; set -- ${@#S}' '2|Q|R' +testcase 'set -- Q R; set -- ${@##S}' '2|Q|R' +testcase 'set -- Q R; set -- ${@%S}' '2|Q|R' +testcase 'set -- Q R; set -- ${@%%S}' '2|Q|R' +testcase 'set -- Q R; set -- "${@#Q}"' '2||R' +testcase 'set -- Q R; set -- "${@%R}"' '2|Q|' +testcase 'set -- Q R; set -- "${@%%R}"' '2|Q|' +testcase 'set -- Q R; set -- "${@#S}"' '2|Q|R' +testcase 'set -- Q R; set -- "${@##S}"' '2|Q|R' +testcase 'set -- Q R; set -- "${@%S}"' '2|Q|R' +testcase 'set -- Q R; set -- "${@%%S}"' '2|Q|R' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/invocation/Makefile b/corebinutils/sh/tests/invocation/Makefile new file mode 100644 index 0000000000..e1d0601e57 --- /dev/null +++ b/corebinutils/sh/tests/invocation/Makefile @@ -0,0 +1,14 @@ +PACKAGE= tests + +TESTSDIR= ${TESTSBASE}/bin/sh/${.CURDIR:T} + +.PATH: ${.CURDIR:H} +ATF_TESTS_SH= functional_test + +${PACKAGE}FILES+= sh-ac1.0 +${PACKAGE}FILES+= sh-c-missing1.0 +${PACKAGE}FILES+= sh-c1.0 +${PACKAGE}FILES+= sh-ca1.0 +${PACKAGE}FILES+= sh-fca1.0 + +.include <bsd.test.mk> diff --git a/corebinutils/sh/tests/invocation/Makefile.depend b/corebinutils/sh/tests/invocation/Makefile.depend new file mode 100644 index 0000000000..11aba52f82 --- /dev/null +++ b/corebinutils/sh/tests/invocation/Makefile.depend @@ -0,0 +1,10 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/corebinutils/sh/tests/invocation/sh-ac1.0 b/corebinutils/sh/tests/invocation/sh-ac1.0 new file mode 100644 index 0000000000..8386679a66 --- /dev/null +++ b/corebinutils/sh/tests/invocation/sh-ac1.0 @@ -0,0 +1,6 @@ +# Test that attached options before c are processed + +case `${SH} -ac 'echo $-:$0' moo` in +*a*:moo) true ;; +*) false ;; +esac diff --git a/corebinutils/sh/tests/invocation/sh-c-missing1.0 b/corebinutils/sh/tests/invocation/sh-c-missing1.0 new file mode 100644 index 0000000000..630158a754 --- /dev/null +++ b/corebinutils/sh/tests/invocation/sh-c-missing1.0 @@ -0,0 +1,2 @@ + +! echo echo bad | ${SH} -c 2>/dev/null diff --git a/corebinutils/sh/tests/invocation/sh-c1.0 b/corebinutils/sh/tests/invocation/sh-c1.0 new file mode 100644 index 0000000000..07dc0d8401 --- /dev/null +++ b/corebinutils/sh/tests/invocation/sh-c1.0 @@ -0,0 +1,3 @@ +# Test that -c executes command_string with the given name and arg + +${SH} -c 'echo $0 $@' moo foo | grep -qx -- "moo foo" diff --git a/corebinutils/sh/tests/invocation/sh-ca1.0 b/corebinutils/sh/tests/invocation/sh-ca1.0 new file mode 100644 index 0000000000..6fe78f1c57 --- /dev/null +++ b/corebinutils/sh/tests/invocation/sh-ca1.0 @@ -0,0 +1,6 @@ +# Test that attached options after c are processed + +case `${SH} -ca 'echo $-:$0' moo` in +*a*:moo) true ;; +*) false ;; +esac diff --git a/corebinutils/sh/tests/invocation/sh-fca1.0 b/corebinutils/sh/tests/invocation/sh-fca1.0 new file mode 100644 index 0000000000..f31312d779 --- /dev/null +++ b/corebinutils/sh/tests/invocation/sh-fca1.0 @@ -0,0 +1,6 @@ +# Test that attached options before and after c are processed + +case `${SH} -fca 'echo $-:$-:$0:$@' foo -bar` in +*f*:*a*:foo:-bar) true ;; +*) false ;; +esac diff --git a/corebinutils/sh/tests/parameters/Makefile b/corebinutils/sh/tests/parameters/Makefile new file mode 100644 index 0000000000..50352c91b6 --- /dev/null +++ b/corebinutils/sh/tests/parameters/Makefile @@ -0,0 +1,27 @@ +PACKAGE= tests + +TESTSDIR= ${TESTSBASE}/bin/sh/${.CURDIR:T} + +.PATH: ${.CURDIR:H} +ATF_TESTS_SH= functional_test + +${PACKAGE}FILES+= env1.0 +${PACKAGE}FILES+= exitstatus1.0 +${PACKAGE}FILES+= ifs1.0 +${PACKAGE}FILES+= mail1.0 +${PACKAGE}FILES+= mail2.0 +${PACKAGE}FILES+= optind1.0 +${PACKAGE}FILES+= optind2.0 +${PACKAGE}FILES+= positional1.0 +${PACKAGE}FILES+= positional2.0 +${PACKAGE}FILES+= positional3.0 +${PACKAGE}FILES+= positional4.0 +${PACKAGE}FILES+= positional5.0 +${PACKAGE}FILES+= positional6.0 +${PACKAGE}FILES+= positional7.0 +${PACKAGE}FILES+= positional8.0 +${PACKAGE}FILES+= positional9.0 +${PACKAGE}FILES+= pwd1.0 +${PACKAGE}FILES+= pwd2.0 + +.include <bsd.test.mk> diff --git a/corebinutils/sh/tests/parameters/Makefile.depend b/corebinutils/sh/tests/parameters/Makefile.depend new file mode 100644 index 0000000000..11aba52f82 --- /dev/null +++ b/corebinutils/sh/tests/parameters/Makefile.depend @@ -0,0 +1,10 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/corebinutils/sh/tests/parameters/env1.0 b/corebinutils/sh/tests/parameters/env1.0 new file mode 100644 index 0000000000..ade1ed6249 --- /dev/null +++ b/corebinutils/sh/tests/parameters/env1.0 @@ -0,0 +1,10 @@ + +export key='must contain this' +unset x +r=$(ENV="\${x?\$key}" ${SH} -i +m 2>&1 >/dev/null <<\EOF +exit 0 +EOF +) && case $r in +*"$key"*) true ;; +*) false ;; +esac diff --git a/corebinutils/sh/tests/parameters/exitstatus1.0 b/corebinutils/sh/tests/parameters/exitstatus1.0 new file mode 100644 index 0000000000..f324392681 --- /dev/null +++ b/corebinutils/sh/tests/parameters/exitstatus1.0 @@ -0,0 +1,8 @@ +f() { + [ $? = $1 ] || exit 1 +} + +true +f 0 +false +f 1 diff --git a/corebinutils/sh/tests/parameters/ifs1.0 b/corebinutils/sh/tests/parameters/ifs1.0 new file mode 100644 index 0000000000..9462a46e1a --- /dev/null +++ b/corebinutils/sh/tests/parameters/ifs1.0 @@ -0,0 +1,9 @@ + +env IFS=_ ${SH} -c ' +rc=2 +nosuchtool_function() { + rc=0 +} +v=nosuchtool_function +$v && exit "$rc" +' diff --git a/corebinutils/sh/tests/parameters/mail1.0 b/corebinutils/sh/tests/parameters/mail1.0 new file mode 100644 index 0000000000..42e712e31a --- /dev/null +++ b/corebinutils/sh/tests/parameters/mail1.0 @@ -0,0 +1,14 @@ +# Test that a non-interactive shell does not access $MAIL. + +goodfile=/var/empty/sh-test-goodfile +mailfile=/var/empty/sh-test-mailfile +T=$(mktemp sh-test.XXXXXX) || exit +MAIL=$mailfile ktrace -t n -i -f "$T" ${SH} -c "[ -s $goodfile ]" 3>/dev/null +if ! grep -q $goodfile "$T"; then + # ktrace problem + rc=0 +elif ! grep -q $mailfile "$T"; then + rc=0 +fi +rm "$T" +exit ${rc:-3} diff --git a/corebinutils/sh/tests/parameters/mail2.0 b/corebinutils/sh/tests/parameters/mail2.0 new file mode 100644 index 0000000000..b1f48e0b29 --- /dev/null +++ b/corebinutils/sh/tests/parameters/mail2.0 @@ -0,0 +1,14 @@ +# Test that an interactive shell accesses $MAIL. + +goodfile=/var/empty/sh-test-goodfile +mailfile=/var/empty/sh-test-mailfile +T=$(mktemp sh-test.XXXXXX) || exit +ENV=$goodfile MAIL=$mailfile ktrace -t n -i -f "$T" ${SH} +m -i </dev/null >/dev/null 2>&1 +if ! grep -q $goodfile "$T"; then + # ktrace problem + rc=0 +elif grep -q $mailfile "$T"; then + rc=0 +fi +rm "$T" +exit ${rc:-3} diff --git a/corebinutils/sh/tests/parameters/optind1.0 b/corebinutils/sh/tests/parameters/optind1.0 new file mode 100644 index 0000000000..516f124c38 --- /dev/null +++ b/corebinutils/sh/tests/parameters/optind1.0 @@ -0,0 +1,2 @@ + +unset OPTIND && [ -z "$OPTIND" ] diff --git a/corebinutils/sh/tests/parameters/optind2.0 b/corebinutils/sh/tests/parameters/optind2.0 new file mode 100644 index 0000000000..3ca60ba6f7 --- /dev/null +++ b/corebinutils/sh/tests/parameters/optind2.0 @@ -0,0 +1,2 @@ + +[ "$(OPTIND=42 ${SH} -c 'printf %s "$OPTIND"')" = 1 ] diff --git a/corebinutils/sh/tests/parameters/positional1.0 b/corebinutils/sh/tests/parameters/positional1.0 new file mode 100644 index 0000000000..0186ffbd6d --- /dev/null +++ b/corebinutils/sh/tests/parameters/positional1.0 @@ -0,0 +1,12 @@ + +set -- a b c d e f g h i j +[ "$1" = a ] || echo "error at line $LINENO" +[ "${1}" = a ] || echo "error at line $LINENO" +[ "${1-foo}" = a ] || echo "error at line $LINENO" +[ "${1+foo}" = foo ] || echo "error at line $LINENO" +[ "$1+foo" = a+foo ] || echo "error at line $LINENO" +[ "$10" = a0 ] || echo "error at line $LINENO" +[ "$100" = a00 ] || echo "error at line $LINENO" +[ "${10}" = j ] || echo "error at line $LINENO" +[ "${10-foo}" = j ] || echo "error at line $LINENO" +[ "${100-foo}" = foo ] || echo "error at line $LINENO" diff --git a/corebinutils/sh/tests/parameters/positional2.0 b/corebinutils/sh/tests/parameters/positional2.0 new file mode 100644 index 0000000000..88bd93dcfe --- /dev/null +++ b/corebinutils/sh/tests/parameters/positional2.0 @@ -0,0 +1,64 @@ + +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'set -- a b; set -- p$@q' '2|pa|bq' +testcase 'set -- a b; set -- $@q' '2|a|bq' +testcase 'set -- a b; set -- p$@' '2|pa|b' +testcase 'set -- a b; set -- p$@q' '2|pa|bq' +testcase 'set -- a b; set -- $@q' '2|a|bq' +testcase 'set -- a b; set -- p$@' '2|pa|b' +testcase 'set -- a b; set -- p$*q' '2|pa|bq' +testcase 'set -- a b; set -- $*q' '2|a|bq' +testcase 'set -- a b; set -- p$*' '2|pa|b' +testcase 'set -- a b; set -- p$*q' '2|pa|bq' +testcase 'set -- a b; set -- $*q' '2|a|bq' +testcase 'set -- a b; set -- p$*' '2|pa|b' +testcase 'set -- a b; set -- "p$@q"' '2|pa|bq' +testcase 'set -- a b; set -- "$@q"' '2|a|bq' +testcase 'set -- a b; set -- "p$@"' '2|pa|b' +testcase 'set -- a b; set -- p"$@"q' '2|pa|bq' +testcase 'set -- a b; set -- "$@"q' '2|a|bq' +testcase 'set -- a b; set -- p"$@"' '2|pa|b' +testcase 'set -- "" a b; set -- "p$@q"' '3|p|a|bq' +testcase 'set -- "" a b; set -- "$@q"' '3||a|bq' +testcase 'set -- "" a b; set -- "p$@"' '3|p|a|b' +testcase 'set -- "" a b; set -- p"$@"q' '3|p|a|bq' +testcase 'set -- "" a b; set -- "$@"q' '3||a|bq' +testcase 'set -- "" a b; set -- p"$@"' '3|p|a|b' +testcase 'set -- a; set -- p$@q' '1|paq' +testcase 'set -- a; set -- $@q' '1|aq' +testcase 'set -- a; set -- p$@' '1|pa' +testcase 'set -- a; set -- p$@q' '1|paq' +testcase 'set -- a; set -- $@q' '1|aq' +testcase 'set -- a; set -- p$@' '1|pa' +testcase 'set -- a; set -- p$*q' '1|paq' +testcase 'set -- a; set -- $*q' '1|aq' +testcase 'set -- a; set -- p$*' '1|pa' +testcase 'set -- a; set -- p$*q' '1|paq' +testcase 'set -- a; set -- $*q' '1|aq' +testcase 'set -- a; set -- p$*' '1|pa' +testcase 'set -- a; set -- "p$@q"' '1|paq' +testcase 'set -- a; set -- "$@q"' '1|aq' +testcase 'set -- a; set -- "p$@"' '1|pa' +testcase 'set -- a; set -- p"$@"q' '1|paq' +testcase 'set -- a; set -- "$@"q' '1|aq' +testcase 'set -- a; set -- p"$@"' '1|pa' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/parameters/positional3.0 b/corebinutils/sh/tests/parameters/positional3.0 new file mode 100644 index 0000000000..28311fa2ee --- /dev/null +++ b/corebinutils/sh/tests/parameters/positional3.0 @@ -0,0 +1,3 @@ + +r=$(${SH} -c 'echo ${01:+yes}${010:+yes}' '' a '' '' '' '' '' '' '' '' b) +[ "$r" = yesyes ] diff --git a/corebinutils/sh/tests/parameters/positional4.0 b/corebinutils/sh/tests/parameters/positional4.0 new file mode 100644 index 0000000000..1b290b99da --- /dev/null +++ b/corebinutils/sh/tests/parameters/positional4.0 @@ -0,0 +1,3 @@ + +set -- "x$0" 2 3 4 5 6 7 8 9 "y$0" +[ "${01}.${010}" = "$1.${10}" ] diff --git a/corebinutils/sh/tests/parameters/positional5.0 b/corebinutils/sh/tests/parameters/positional5.0 new file mode 100644 index 0000000000..ac6471ed57 --- /dev/null +++ b/corebinutils/sh/tests/parameters/positional5.0 @@ -0,0 +1,13 @@ + +i=1 +r=0 +while [ $i -lt $((0x100000000)) ]; do + t= + eval t=\${$i-x} + case $t in + x) ;; + *) echo "Problem with \${$i}" >&2; r=1 ;; + esac + i=$((i + 0x10000000)) +done +exit $r diff --git a/corebinutils/sh/tests/parameters/positional6.0 b/corebinutils/sh/tests/parameters/positional6.0 new file mode 100644 index 0000000000..43df962106 --- /dev/null +++ b/corebinutils/sh/tests/parameters/positional6.0 @@ -0,0 +1,6 @@ + +IFS=? +set p r +v=pqrs +r=${v#"$*"} +[ "$r" = pqrs ] diff --git a/corebinutils/sh/tests/parameters/positional7.0 b/corebinutils/sh/tests/parameters/positional7.0 new file mode 100644 index 0000000000..8a21da9797 --- /dev/null +++ b/corebinutils/sh/tests/parameters/positional7.0 @@ -0,0 +1,7 @@ + +set -- / '' +IFS=* +set -- "$*" +IFS=: +args="$*" +[ "$#:$args" = "1:/*" ] diff --git a/corebinutils/sh/tests/parameters/positional8.0 b/corebinutils/sh/tests/parameters/positional8.0 new file mode 100644 index 0000000000..1b1b941c8e --- /dev/null +++ b/corebinutils/sh/tests/parameters/positional8.0 @@ -0,0 +1,30 @@ + +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'shift $#; set -- ""$*' '1|' +testcase 'shift $#; set -- $*""' '1|' +testcase 'shift $#; set -- ""$@' '1|' +testcase 'shift $#; set -- $@""' '1|' +testcase 'shift $#; set -- """$*"' '1|' +testcase 'shift $#; set -- "$*"""' '1|' +testcase 'shift $#; set -- """$@"' '1|' +testcase 'shift $#; set -- "$@"""' '1|' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/parameters/positional9.0 b/corebinutils/sh/tests/parameters/positional9.0 new file mode 100644 index 0000000000..823995f0c7 --- /dev/null +++ b/corebinutils/sh/tests/parameters/positional9.0 @@ -0,0 +1,17 @@ +# Although POSIX leaves the result of expanding ${#@} and ${#*} unspecified, +# make sure it is at least numeric. + +set -- bb cc ddd +set -f +lengths=${#*}${#@}"${#*}${#@}"$(echo ${#*}${#@}"${#*}${#@}") +IFS= +lengths=$lengths${#*}${#@}"${#*}${#@}"$(echo ${#*}${#@}"${#*}${#@}") +case $lengths in +*[!0-9]*) + printf 'bad: %s\n' "$lengths" + exit 3 ;; +????????????????*) ;; +*) + printf 'too short: %s\n' "$lengths" + exit 3 ;; +esac diff --git a/corebinutils/sh/tests/parameters/pwd1.0 b/corebinutils/sh/tests/parameters/pwd1.0 new file mode 100644 index 0000000000..cd0d8329cd --- /dev/null +++ b/corebinutils/sh/tests/parameters/pwd1.0 @@ -0,0 +1,10 @@ +# Check that bogus PWD values are not accepted from the environment. + +cd / || exit 3 +failures=0 +[ "$(PWD=foo ${SH} -c 'pwd')" = / ] || : $((failures += 1)) +[ "$(PWD=/var/empty ${SH} -c 'pwd')" = / ] || : $((failures += 1)) +[ "$(PWD=/var/empty/foo ${SH} -c 'pwd')" = / ] || : $((failures += 1)) +[ "$(PWD=/bin/ls ${SH} -c 'pwd')" = / ] || : $((failures += 1)) + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/parameters/pwd2.0 b/corebinutils/sh/tests/parameters/pwd2.0 new file mode 100644 index 0000000000..69ab5c896f --- /dev/null +++ b/corebinutils/sh/tests/parameters/pwd2.0 @@ -0,0 +1,23 @@ +# Check that PWD is exported and accepted from the environment. +set -e + +T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX) +trap 'rm -rf $T' 0 +cd -P $T +TP=$(pwd) +mkdir test1 +ln -s test1 link +cd link +[ "$PWD" = "$TP/link" ] +[ "$(pwd)" = "$TP/link" ] +[ "$(pwd -P)" = "$TP/test1" ] +[ "$(${SH} -c pwd)" = "$TP/link" ] +[ "$(${SH} -c pwd\ -P)" = "$TP/test1" ] +cd .. +[ "$(pwd)" = "$TP" ] +cd -P link +[ "$PWD" = "$TP/test1" ] +[ "$(pwd)" = "$TP/test1" ] +[ "$(pwd -P)" = "$TP/test1" ] +[ "$(${SH} -c pwd)" = "$TP/test1" ] +[ "$(${SH} -c pwd\ -P)" = "$TP/test1" ] diff --git a/corebinutils/sh/tests/parser/Makefile b/corebinutils/sh/tests/parser/Makefile new file mode 100644 index 0000000000..c22af54145 --- /dev/null +++ b/corebinutils/sh/tests/parser/Makefile @@ -0,0 +1,98 @@ +PACKAGE= tests + +TESTSDIR= ${TESTSBASE}/bin/sh/${.CURDIR:T} + +.PATH: ${.CURDIR:H} +ATF_TESTS_SH= functional_test + +${PACKAGE}FILES+= alias1.0 +${PACKAGE}FILES+= alias2.0 +${PACKAGE}FILES+= alias3.0 +${PACKAGE}FILES+= alias4.0 +${PACKAGE}FILES+= alias5.0 +${PACKAGE}FILES+= alias6.0 +${PACKAGE}FILES+= alias7.0 +${PACKAGE}FILES+= alias8.0 +${PACKAGE}FILES+= alias9.0 +${PACKAGE}FILES+= alias10.0 +${PACKAGE}FILES+= alias11.0 +${PACKAGE}FILES+= alias12.0 +${PACKAGE}FILES+= alias13.0 +${PACKAGE}FILES+= alias14.0 +${PACKAGE}FILES+= alias15.0 alias15.0.stdout +${PACKAGE}FILES+= alias16.0 +${PACKAGE}FILES+= alias17.0 +${PACKAGE}FILES+= alias18.0 +${PACKAGE}FILES+= alias19.0 alias19.0.stdout +${PACKAGE}FILES+= alias20.0 alias20.0.stdout +${PACKAGE}FILES+= and-pipe-not.0 +${PACKAGE}FILES+= case1.0 +${PACKAGE}FILES+= case2.0 +${PACKAGE}FILES+= comment1.0 +${PACKAGE}FILES+= comment2.42 +${PACKAGE}FILES+= dollar-quote1.0 +${PACKAGE}FILES+= dollar-quote2.0 +${PACKAGE}FILES+= dollar-quote3.0 +${PACKAGE}FILES+= dollar-quote4.0 +${PACKAGE}FILES+= dollar-quote5.0 +${PACKAGE}FILES+= dollar-quote6.0 +${PACKAGE}FILES+= dollar-quote7.0 +${PACKAGE}FILES+= dollar-quote8.0 +${PACKAGE}FILES+= dollar-quote9.0 +${PACKAGE}FILES+= dollar-quote10.0 +${PACKAGE}FILES+= dollar-quote11.0 +${PACKAGE}FILES+= dollar-quote12.0 +${PACKAGE}FILES+= dollar-quote13.0 +${PACKAGE}FILES+= empty-braces1.0 +${PACKAGE}FILES+= empty-cmd1.0 +${PACKAGE}FILES+= for1.0 +${PACKAGE}FILES+= for2.0 +${PACKAGE}FILES+= func1.0 +${PACKAGE}FILES+= func2.0 +${PACKAGE}FILES+= func3.0 +${PACKAGE}FILES+= heredoc1.0 +${PACKAGE}FILES+= heredoc2.0 +${PACKAGE}FILES+= heredoc3.0 +${PACKAGE}FILES+= heredoc4.0 +${PACKAGE}FILES+= heredoc5.0 +${PACKAGE}FILES+= heredoc6.0 +${PACKAGE}FILES+= heredoc7.0 +${PACKAGE}FILES+= heredoc8.0 +${PACKAGE}FILES+= heredoc9.0 +${PACKAGE}FILES+= heredoc10.0 +${PACKAGE}FILES+= heredoc11.0 +${PACKAGE}FILES+= heredoc12.0 +${PACKAGE}FILES+= heredoc13.0 +${PACKAGE}FILES+= heredoc14.0 +${PACKAGE}FILES+= heredoc15.0 +${PACKAGE}FILES+= heredoc16.0 +${PACKAGE}FILES+= line-cont1.0 +${PACKAGE}FILES+= line-cont2.0 +${PACKAGE}FILES+= line-cont3.0 +${PACKAGE}FILES+= line-cont4.0 +${PACKAGE}FILES+= line-cont5.0 +${PACKAGE}FILES+= line-cont6.0 +${PACKAGE}FILES+= line-cont7.0 +${PACKAGE}FILES+= line-cont8.0 +${PACKAGE}FILES+= line-cont9.0 +${PACKAGE}FILES+= line-cont10.0 +${PACKAGE}FILES+= line-cont11.0 +${PACKAGE}FILES+= line-cont12.0 +${PACKAGE}FILES+= no-space1.0 +${PACKAGE}FILES+= no-space2.0 +${PACKAGE}FILES+= nul1.0 +${PACKAGE}FILES+= only-redir1.0 +${PACKAGE}FILES+= only-redir2.0 +${PACKAGE}FILES+= only-redir3.0 +${PACKAGE}FILES+= only-redir4.0 +${PACKAGE}FILES+= pipe-not1.0 +${PACKAGE}FILES+= ps1-expand1.0 +${PACKAGE}FILES+= ps1-expand2.0 +${PACKAGE}FILES+= ps1-expand3.0 +${PACKAGE}FILES+= ps1-expand4.0 +${PACKAGE}FILES+= ps1-expand5.0 +${PACKAGE}FILES+= ps2-expand1.0 +${PACKAGE}FILES+= set-v1.0 set-v1.0.stderr +${PACKAGE}FILES+= var-assign1.0 + +.include <bsd.test.mk> diff --git a/corebinutils/sh/tests/parser/Makefile.depend b/corebinutils/sh/tests/parser/Makefile.depend new file mode 100644 index 0000000000..11aba52f82 --- /dev/null +++ b/corebinutils/sh/tests/parser/Makefile.depend @@ -0,0 +1,10 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/corebinutils/sh/tests/parser/alias1.0 b/corebinutils/sh/tests/parser/alias1.0 new file mode 100644 index 0000000000..2380bb95e4 --- /dev/null +++ b/corebinutils/sh/tests/parser/alias1.0 @@ -0,0 +1,4 @@ + +alias alias0=exit +eval 'alias0 0' +exit 1 diff --git a/corebinutils/sh/tests/parser/alias10.0 b/corebinutils/sh/tests/parser/alias10.0 new file mode 100644 index 0000000000..d8c77691b2 --- /dev/null +++ b/corebinutils/sh/tests/parser/alias10.0 @@ -0,0 +1,8 @@ + +# This test may start consuming memory indefinitely if it fails. +ulimit -t 5 2>/dev/null +ulimit -v 100000 2>/dev/null + +alias echo='echo' +alias echo='echo' +[ "`eval echo b`" = b ] diff --git a/corebinutils/sh/tests/parser/alias11.0 b/corebinutils/sh/tests/parser/alias11.0 new file mode 100644 index 0000000000..286323028a --- /dev/null +++ b/corebinutils/sh/tests/parser/alias11.0 @@ -0,0 +1,5 @@ + +alias alias0=alias1 +alias alias1=exit +eval 'alias0 0' +exit 3 diff --git a/corebinutils/sh/tests/parser/alias12.0 b/corebinutils/sh/tests/parser/alias12.0 new file mode 100644 index 0000000000..30be135c0c --- /dev/null +++ b/corebinutils/sh/tests/parser/alias12.0 @@ -0,0 +1,5 @@ + +unalias -a +alias alias0=command +alias true='echo bad' +eval 'alias0 true' diff --git a/corebinutils/sh/tests/parser/alias13.0 b/corebinutils/sh/tests/parser/alias13.0 new file mode 100644 index 0000000000..df35c5045a --- /dev/null +++ b/corebinutils/sh/tests/parser/alias13.0 @@ -0,0 +1,5 @@ + +unalias -a +alias command=command +alias true='echo bad' +eval 'command true' diff --git a/corebinutils/sh/tests/parser/alias14.0 b/corebinutils/sh/tests/parser/alias14.0 new file mode 100644 index 0000000000..20acd59d4d --- /dev/null +++ b/corebinutils/sh/tests/parser/alias14.0 @@ -0,0 +1,5 @@ + +alias command='command ' +alias alias0=exit +eval 'command alias0 0' +exit 3 diff --git a/corebinutils/sh/tests/parser/alias15.0 b/corebinutils/sh/tests/parser/alias15.0 new file mode 100644 index 0000000000..19a1a36eaa --- /dev/null +++ b/corebinutils/sh/tests/parser/alias15.0 @@ -0,0 +1,11 @@ + +f_echoanddo() { + printf '%s\n' "$*" + "$@" +} + +alias echoanddo='f_echoanddo ' +alias alias0='echo test2' +eval 'echoanddo echo test1' +eval 'echoanddo alias0' +exit 0 diff --git a/corebinutils/sh/tests/parser/alias15.0.stdout b/corebinutils/sh/tests/parser/alias15.0.stdout new file mode 100644 index 0000000000..6dd179c065 --- /dev/null +++ b/corebinutils/sh/tests/parser/alias15.0.stdout @@ -0,0 +1,4 @@ +echo test1 +test1 +echo test2 +test2 diff --git a/corebinutils/sh/tests/parser/alias16.0 b/corebinutils/sh/tests/parser/alias16.0 new file mode 100644 index 0000000000..b611c69ab0 --- /dev/null +++ b/corebinutils/sh/tests/parser/alias16.0 @@ -0,0 +1,6 @@ + +v=1 +alias a='unalias a +v=2' +eval a +[ "$v" = 2 ] diff --git a/corebinutils/sh/tests/parser/alias17.0 b/corebinutils/sh/tests/parser/alias17.0 new file mode 100644 index 0000000000..c2e24c68aa --- /dev/null +++ b/corebinutils/sh/tests/parser/alias17.0 @@ -0,0 +1,6 @@ + +v=1 +alias a='unalias -a +v=2' +eval a +[ "$v" = 2 ] diff --git a/corebinutils/sh/tests/parser/alias18.0 b/corebinutils/sh/tests/parser/alias18.0 new file mode 100644 index 0000000000..05117c5a83 --- /dev/null +++ b/corebinutils/sh/tests/parser/alias18.0 @@ -0,0 +1,7 @@ + +v=1 +alias a='alias a=v=2 +v=3 +a' +eval a +[ "$v" = 2 ] diff --git a/corebinutils/sh/tests/parser/alias19.0 b/corebinutils/sh/tests/parser/alias19.0 new file mode 100644 index 0000000000..c35930e983 --- /dev/null +++ b/corebinutils/sh/tests/parser/alias19.0 @@ -0,0 +1,7 @@ + +alias begin={ end=} +begin +cat <<EOF +$(echo ok) +EOF +end diff --git a/corebinutils/sh/tests/parser/alias19.0.stdout b/corebinutils/sh/tests/parser/alias19.0.stdout new file mode 100644 index 0000000000..9766475a41 --- /dev/null +++ b/corebinutils/sh/tests/parser/alias19.0.stdout @@ -0,0 +1 @@ +ok diff --git a/corebinutils/sh/tests/parser/alias2.0 b/corebinutils/sh/tests/parser/alias2.0 new file mode 100644 index 0000000000..e92d62eaef --- /dev/null +++ b/corebinutils/sh/tests/parser/alias2.0 @@ -0,0 +1,5 @@ + +alias alias0=exit +x=alias0 +eval 'case $x in alias0) exit 0;; esac' +exit 1 diff --git a/corebinutils/sh/tests/parser/alias20.0 b/corebinutils/sh/tests/parser/alias20.0 new file mode 100644 index 0000000000..7e1767eacc --- /dev/null +++ b/corebinutils/sh/tests/parser/alias20.0 @@ -0,0 +1,8 @@ + +alias begin={ end=} +: <<EOF && +$(echo bad1) +EOF +begin +echo ok +end diff --git a/corebinutils/sh/tests/parser/alias20.0.stdout b/corebinutils/sh/tests/parser/alias20.0.stdout new file mode 100644 index 0000000000..9766475a41 --- /dev/null +++ b/corebinutils/sh/tests/parser/alias20.0.stdout @@ -0,0 +1 @@ +ok diff --git a/corebinutils/sh/tests/parser/alias3.0 b/corebinutils/sh/tests/parser/alias3.0 new file mode 100644 index 0000000000..4651ba149c --- /dev/null +++ b/corebinutils/sh/tests/parser/alias3.0 @@ -0,0 +1,5 @@ + +alias alias0=exit +x=alias0 +eval 'case $x in "alias0") alias0 0;; esac' +exit 1 diff --git a/corebinutils/sh/tests/parser/alias4.0 b/corebinutils/sh/tests/parser/alias4.0 new file mode 100644 index 0000000000..a46923d1eb --- /dev/null +++ b/corebinutils/sh/tests/parser/alias4.0 @@ -0,0 +1,4 @@ + +alias alias0=exit +eval 'x=1 alias0 0' +exit 1 diff --git a/corebinutils/sh/tests/parser/alias5.0 b/corebinutils/sh/tests/parser/alias5.0 new file mode 100644 index 0000000000..4c393cd116 --- /dev/null +++ b/corebinutils/sh/tests/parser/alias5.0 @@ -0,0 +1,4 @@ + +alias alias0=exit +eval '</dev/null alias0 0' +exit 1 diff --git a/corebinutils/sh/tests/parser/alias6.0 b/corebinutils/sh/tests/parser/alias6.0 new file mode 100644 index 0000000000..23c3d39de0 --- /dev/null +++ b/corebinutils/sh/tests/parser/alias6.0 @@ -0,0 +1,5 @@ + +alias alias0='| cat >/dev/null' + +eval '{ echo bad; } alias0' +eval '(echo bad)alias0' diff --git a/corebinutils/sh/tests/parser/alias7.0 b/corebinutils/sh/tests/parser/alias7.0 new file mode 100644 index 0000000000..f644ceaa7a --- /dev/null +++ b/corebinutils/sh/tests/parser/alias7.0 @@ -0,0 +1,3 @@ + +alias echo='echo a' +[ "`eval echo b`" = "a b" ] diff --git a/corebinutils/sh/tests/parser/alias8.0 b/corebinutils/sh/tests/parser/alias8.0 new file mode 100644 index 0000000000..c71997f362 --- /dev/null +++ b/corebinutils/sh/tests/parser/alias8.0 @@ -0,0 +1,3 @@ + +alias echo='echo' +[ "`eval echo b`" = b ] diff --git a/corebinutils/sh/tests/parser/alias9.0 b/corebinutils/sh/tests/parser/alias9.0 new file mode 100644 index 0000000000..50fe86e2b8 --- /dev/null +++ b/corebinutils/sh/tests/parser/alias9.0 @@ -0,0 +1,5 @@ + +alias alias0=: +alias alias0=exit +eval 'alias0 0' +exit 1 diff --git a/corebinutils/sh/tests/parser/and-pipe-not.0 b/corebinutils/sh/tests/parser/and-pipe-not.0 new file mode 100644 index 0000000000..90889bfe62 --- /dev/null +++ b/corebinutils/sh/tests/parser/and-pipe-not.0 @@ -0,0 +1 @@ +true && ! true | false diff --git a/corebinutils/sh/tests/parser/case1.0 b/corebinutils/sh/tests/parser/case1.0 new file mode 100644 index 0000000000..8b4e1c342a --- /dev/null +++ b/corebinutils/sh/tests/parser/case1.0 @@ -0,0 +1,13 @@ + +keywords='if then else elif fi while until for do done { } case esac ! in' + +# Keywords can be used unquoted in case statements, except the keyword +# esac as the first pattern of a '|' alternation without a starting '('. +# (POSIX doesn't seem to require (esac) to work.) +for k in $keywords; do + eval "case $k in (foo|$k) ;; *) echo bad ;; esac" + eval "case $k in ($k) ;; *) echo bad ;; esac" + eval "case $k in foo|$k) ;; *) echo bad ;; esac" + [ "$k" = esac ] && continue + eval "case $k in $k) ;; *) echo bad ;; esac" +done diff --git a/corebinutils/sh/tests/parser/case2.0 b/corebinutils/sh/tests/parser/case2.0 new file mode 100644 index 0000000000..578187a5a3 --- /dev/null +++ b/corebinutils/sh/tests/parser/case2.0 @@ -0,0 +1,31 @@ + +# Pretty much only ash derivatives can parse all of this. + +f1() { + x=$(case x in + (x|esac) ;; + (*) echo bad >&2 ;; + esac) +} +f1 +f2() { + x=$(case x in + (x|esac) ;; + (*) echo bad >&2 + esac) +} +f2 +f3() { + x=$(case x in + x|esac) ;; + *) echo bad >&2 ;; + esac) +} +f3 +f4() { + x=$(case x in + x|esac) ;; + *) echo bad >&2 + esac) +} +f4 diff --git a/corebinutils/sh/tests/parser/comment1.0 b/corebinutils/sh/tests/parser/comment1.0 new file mode 100644 index 0000000000..59904fb98c --- /dev/null +++ b/corebinutils/sh/tests/parser/comment1.0 @@ -0,0 +1,2 @@ + +${SH} -c '#' diff --git a/corebinutils/sh/tests/parser/comment2.42 b/corebinutils/sh/tests/parser/comment2.42 new file mode 100644 index 0000000000..30ab4c4d1d --- /dev/null +++ b/corebinutils/sh/tests/parser/comment2.42 @@ -0,0 +1,3 @@ + +${SH} -c '# +exit 42' diff --git a/corebinutils/sh/tests/parser/dollar-quote1.0 b/corebinutils/sh/tests/parser/dollar-quote1.0 new file mode 100644 index 0000000000..2862b8a20b --- /dev/null +++ b/corebinutils/sh/tests/parser/dollar-quote1.0 @@ -0,0 +1,11 @@ + +set -e + +[ $'hi' = hi ] +[ $'hi +there' = 'hi +there' ] +[ $'\"\'\\\a\b\f\t\v' = "\"'\\$(printf "\a\b\f\t\v")" ] +[ $'hi\nthere' = 'hi +there' ] +[ $'a\rb' = "$(printf "a\rb")" ] diff --git a/corebinutils/sh/tests/parser/dollar-quote10.0 b/corebinutils/sh/tests/parser/dollar-quote10.0 new file mode 100644 index 0000000000..e28ea482ac --- /dev/null +++ b/corebinutils/sh/tests/parser/dollar-quote10.0 @@ -0,0 +1,9 @@ + +# a umlaut +s=$(printf '\303\244') +# euro sign +s=$s$(printf '\342\202\254') + +# Start a new shell so the locale change is picked up. +ss="$(LC_ALL=en_US.UTF-8 ${SH} -c "printf %s \$'\u00e4\u20ac'")" +[ "$s" = "$ss" ] diff --git a/corebinutils/sh/tests/parser/dollar-quote11.0 b/corebinutils/sh/tests/parser/dollar-quote11.0 new file mode 100644 index 0000000000..de567ebeed --- /dev/null +++ b/corebinutils/sh/tests/parser/dollar-quote11.0 @@ -0,0 +1,7 @@ + +# some sort of 't' outside BMP +s=$s$(printf '\360\235\225\245') + +# Start a new shell so the locale change is picked up. +ss="$(LC_ALL=en_US.UTF-8 ${SH} -c "printf %s \$'\U0001d565'")" +[ "$s" = "$ss" ] diff --git a/corebinutils/sh/tests/parser/dollar-quote12.0 b/corebinutils/sh/tests/parser/dollar-quote12.0 new file mode 100644 index 0000000000..a6207d29a2 --- /dev/null +++ b/corebinutils/sh/tests/parser/dollar-quote12.0 @@ -0,0 +1,6 @@ + +# \u without any digits at all remains invalid. +# Our choice is a parse error. + +v=$( (eval ": \$'\u'") 2>&1 >/dev/null) +[ $? -ne 0 ] && [ -n "$v" ] diff --git a/corebinutils/sh/tests/parser/dollar-quote13.0 b/corebinutils/sh/tests/parser/dollar-quote13.0 new file mode 100644 index 0000000000..d042ad7416 --- /dev/null +++ b/corebinutils/sh/tests/parser/dollar-quote13.0 @@ -0,0 +1,7 @@ + +# This Unicode escape sequence that has never been in range should either +# fail to expand or expand to a fallback. + +c=$(eval printf %s \$\'\\Uffffff41\' 2>/dev/null) +r=$(($? != 0)) +[ "$r.$c" = '1.' ] || [ "$r.$c" = '0.?' ] || [ "$r.$c" = $'0.\u2222' ] diff --git a/corebinutils/sh/tests/parser/dollar-quote2.0 b/corebinutils/sh/tests/parser/dollar-quote2.0 new file mode 100644 index 0000000000..87a1b39a62 --- /dev/null +++ b/corebinutils/sh/tests/parser/dollar-quote2.0 @@ -0,0 +1,4 @@ + +# This depends on the ASCII character set. + +[ $'\e' = "$(printf "\033")" ] diff --git a/corebinutils/sh/tests/parser/dollar-quote3.0 b/corebinutils/sh/tests/parser/dollar-quote3.0 new file mode 100644 index 0000000000..3e7dc390b3 --- /dev/null +++ b/corebinutils/sh/tests/parser/dollar-quote3.0 @@ -0,0 +1,21 @@ + +unset LC_CTYPE LC_COLLATE LANG LANGUAGE +LC_ALL=C +export LC_ALL + +e= +for i in 0 1 2 3; do + for j in 0 1 2 3 4 5 6 7; do + for k in 0 1 2 3 4 5 6 7; do + case $i$j$k in + 000) continue ;; + esac + e="$e\\$i$j$k" + done + done +done +ee=`printf "$e"` +[ "${#ee}" = 255 ] || echo length bad + +# Start a new shell so the locale change is picked up. +[ "$(${SH} -c "printf %s \$'$e'")" = "$ee" ] diff --git a/corebinutils/sh/tests/parser/dollar-quote4.0 b/corebinutils/sh/tests/parser/dollar-quote4.0 new file mode 100644 index 0000000000..c28e026026 --- /dev/null +++ b/corebinutils/sh/tests/parser/dollar-quote4.0 @@ -0,0 +1,18 @@ + +unset LC_CTYPE LC_COLLATE LANG LANGUAGE +LC_ALL=C +export LC_ALL + +e= +for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do + for j in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do + case $i$j in + 00) continue ;; + esac + e="$e\x$i$j" + done +done + +# Start a new shell so the locale change is picked up. +ee="$(${SH} -c "printf %s \$'$e'")" +[ "${#ee}" = 255 ] || echo length bad diff --git a/corebinutils/sh/tests/parser/dollar-quote5.0 b/corebinutils/sh/tests/parser/dollar-quote5.0 new file mode 100644 index 0000000000..2bf6a0ea21 --- /dev/null +++ b/corebinutils/sh/tests/parser/dollar-quote5.0 @@ -0,0 +1,11 @@ + +# This depends on the ASCII character set. + +set -e + +[ $'\ca\cb\cc\cd\ce\cf\cg\ch\ci\cj\ck\cl\cm\cn\co\cp\cq\cr\cs\ct\cu\cv\cw\cx\cy\cz' = $'\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032' ] +[ $'\cA\cB\cC\cD\cE\cF\cG\cH\cI\cJ\cK\cL\cM\cN\cO\cP\cQ\cR\cS\cT\cU\cV\cW\cX\cY\cZ' = $'\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032' ] +[ $'\c[' = $'\033' ] +[ $'\c]' = $'\035' ] +[ $'\c^' = $'\036' ] +[ $'\c_' = $'\037' ] diff --git a/corebinutils/sh/tests/parser/dollar-quote6.0 b/corebinutils/sh/tests/parser/dollar-quote6.0 new file mode 100644 index 0000000000..dc6f8ab45c --- /dev/null +++ b/corebinutils/sh/tests/parser/dollar-quote6.0 @@ -0,0 +1,4 @@ + +# This depends on the ASCII character set. + +[ $'\c\\' = $'\034' ] diff --git a/corebinutils/sh/tests/parser/dollar-quote7.0 b/corebinutils/sh/tests/parser/dollar-quote7.0 new file mode 100644 index 0000000000..5108fd4eb1 --- /dev/null +++ b/corebinutils/sh/tests/parser/dollar-quote7.0 @@ -0,0 +1,5 @@ + +set -e + +[ $'\u0024\u0040\u0060' = '$@`' ] +[ $'\U00000024\U00000040\U00000060' = '$@`' ] diff --git a/corebinutils/sh/tests/parser/dollar-quote8.0 b/corebinutils/sh/tests/parser/dollar-quote8.0 new file mode 100644 index 0000000000..81dcb9b394 --- /dev/null +++ b/corebinutils/sh/tests/parser/dollar-quote8.0 @@ -0,0 +1,10 @@ + +[ $'hello\0' = hello ] +[ $'hello\0world' = hello ] +[ $'hello\0'$'world' = helloworld ] +[ $'hello\000' = hello ] +[ $'hello\000world' = hello ] +[ $'hello\000'$'world' = helloworld ] +[ $'hello\x00' = hello ] +[ $'hello\x00world' = hello ] +[ $'hello\x00'$'world' = helloworld ] diff --git a/corebinutils/sh/tests/parser/dollar-quote9.0 b/corebinutils/sh/tests/parser/dollar-quote9.0 new file mode 100644 index 0000000000..845f8c2843 --- /dev/null +++ b/corebinutils/sh/tests/parser/dollar-quote9.0 @@ -0,0 +1,7 @@ + +# POSIX and C99 say D800-DFFF are undefined in a universal character name. +# We reject this but many other shells expand to something that looks like +# CESU-8. + +v=$( (eval ": \$'\uD800'") 2>&1 >/dev/null) +[ $? -ne 0 ] && [ -n "$v" ] diff --git a/corebinutils/sh/tests/parser/empty-braces1.0 b/corebinutils/sh/tests/parser/empty-braces1.0 new file mode 100644 index 0000000000..11032811c8 --- /dev/null +++ b/corebinutils/sh/tests/parser/empty-braces1.0 @@ -0,0 +1,6 @@ + +# Unfortunately, some scripts depend on the extension of allowing an empty +# pair of braces. + +{ } & +wait $! diff --git a/corebinutils/sh/tests/parser/empty-cmd1.0 b/corebinutils/sh/tests/parser/empty-cmd1.0 new file mode 100644 index 0000000000..15f4f4ac99 --- /dev/null +++ b/corebinutils/sh/tests/parser/empty-cmd1.0 @@ -0,0 +1,2 @@ + +! (eval ': || f()') 2>/dev/null diff --git a/corebinutils/sh/tests/parser/for1.0 b/corebinutils/sh/tests/parser/for1.0 new file mode 100644 index 0000000000..1633385068 --- /dev/null +++ b/corebinutils/sh/tests/parser/for1.0 @@ -0,0 +1,28 @@ + +nl=' +' +list=' a b c' +for s1 in "$nl" " "; do + for s2 in "$nl" ";" ";$nl"; do + for s3 in "$nl" " "; do + r='' + eval "for i${s1}in ${list}${s2}do${s3}r=\"\$r \$i\"; done" + [ "$r" = "$list" ] || exit 1 + done + done +done +set -- $list +for s2 in "$nl" " "; do + for s3 in "$nl" " "; do + r='' + eval "for i${s2}do${s3}r=\"\$r \$i\"; done" + [ "$r" = "$list" ] || exit 1 + done +done +for s1 in "$nl" " "; do + for s2 in "$nl" ";" ";$nl"; do + for s3 in "$nl" " "; do + eval "for i${s1}in${s2}do${s3}exit 1; done" + done + done +done diff --git a/corebinutils/sh/tests/parser/for2.0 b/corebinutils/sh/tests/parser/for2.0 new file mode 100644 index 0000000000..2dd2d5acd1 --- /dev/null +++ b/corebinutils/sh/tests/parser/for2.0 @@ -0,0 +1,14 @@ + +# Common extensions to the 'for' syntax. + +nl=' +' +list=' a b c' +set -- $list +for s2 in ";" ";$nl"; do + for s3 in "$nl" " "; do + r='' + eval "for i${s2}do${s3}r=\"\$r \$i\"; done" + [ "$r" = "$list" ] || exit 1 + done +done diff --git a/corebinutils/sh/tests/parser/func1.0 b/corebinutils/sh/tests/parser/func1.0 new file mode 100644 index 0000000000..90f1083d48 --- /dev/null +++ b/corebinutils/sh/tests/parser/func1.0 @@ -0,0 +1,24 @@ +# POSIX does not require these bytes to work in function names, +# but making them all work seems a good goal. + +failures=0 +unset LC_CTYPE LC_COLLATE LANG LANGUAGE +export LC_ALL=C +i=128 +set -f +while [ "$i" -le 255 ]; do + c=$(printf \\"$(printf %o "$i")") + ok=0 + eval "$c() { ok=1; }" + $c + ok1=$ok + ok=0 + "$c" + if [ "$ok" != 1 ] || [ "$ok1" != 1 ]; then + echo "Bad results for character $i" >&2 + : $((failures += 1)) + fi + unset -f $c + i=$((i+1)) +done +exit $((failures > 0)) diff --git a/corebinutils/sh/tests/parser/func2.0 b/corebinutils/sh/tests/parser/func2.0 new file mode 100644 index 0000000000..dc9eb6ed15 --- /dev/null +++ b/corebinutils/sh/tests/parser/func2.0 @@ -0,0 +1,5 @@ + +f() { return 42; } +f() { return 3; } & +f +[ $? -eq 42 ] diff --git a/corebinutils/sh/tests/parser/func3.0 b/corebinutils/sh/tests/parser/func3.0 new file mode 100644 index 0000000000..c1462a33c8 --- /dev/null +++ b/corebinutils/sh/tests/parser/func3.0 @@ -0,0 +1,5 @@ + +name=/var/empty/nosuch +f() { true; } <$name +name=/dev/null +f diff --git a/corebinutils/sh/tests/parser/heredoc1.0 b/corebinutils/sh/tests/parser/heredoc1.0 new file mode 100644 index 0000000000..6bfee60563 --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc1.0 @@ -0,0 +1,84 @@ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +check '"$(cat <<EOF +hi +EOF +)" = hi' + +check '"$(cat <<EOF +${$+hi} +EOF +)" = hi' + +unset yy +check '"$(cat <<EOF +${yy-hi} +EOF +)" = hi' + +check '"$(cat <<EOF +${$+hi +there} +EOF +)" = "hi +there"' + +check '"$(cat <<EOF +$((1+1)) +EOF +)" = 2' + +check '"$(cat <<EOF +$(echo hi) +EOF +)" = hi' + +check '"$(cat <<EOF +`echo hi` +EOF +)" = hi' + +check '"$(cat <<\EOF +${$+hi} +EOF +)" = "\${\$+hi}"' + +check '"$(cat <<\EOF +$( +EOF +)" = \$\(' + +check '"$(cat <<\EOF +` +EOF +)" = \`' + +check '"$(cat <<EOF +" +EOF +)" = \"' + +check '"$(cat <<\EOF +" +EOF +)" = \"' + +check '"$(cat <<esac +'"'"' +esac +)" = "'"'"'"' + +check '"$(cat <<\) +'"'"' +) +)" = "'"'"'"' + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/parser/heredoc10.0 b/corebinutils/sh/tests/parser/heredoc10.0 new file mode 100644 index 0000000000..95e280381b --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc10.0 @@ -0,0 +1,48 @@ + +# It may be argued that +# x=$(cat <<EOF +# foo +# EOF) +# is a valid complete command that sets x to foo, because +# cat <<EOF +# foo +# EOF +# is a valid script even without the final newline. +# However, if the here-document is not within a new-style command substitution +# or there are other constructs nested inside the command substitution that +# need terminators, the delimiter at the start of a line followed by a close +# parenthesis is clearly a literal part of the here-document. + +# This file contains tests that may not work with simplistic $(...) parsers. +# The open parentheses in comments help mksh, but not zsh. + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +check '"$(cat <<EOF # ( +EOF ) +EOF +)" = "EOF )"' + +check '"$({ cat <<EOF # ( +EOF) +EOF +})" = "EOF)"' + +check '"$(if :; then cat <<EOF # ( +EOF) +EOF +fi)" = "EOF)"' + +check '"$( (cat <<EOF # ( +EOF) +EOF +))" = "EOF)"' + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/parser/heredoc11.0 b/corebinutils/sh/tests/parser/heredoc11.0 new file mode 100644 index 0000000000..71bf7c017d --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc11.0 @@ -0,0 +1,25 @@ + +failures='' + +check() { + if eval "[ $* ]"; then + : + else + echo "Failed: $*" + failures=x$failures + fi +} + +check '`cat <<EOF +foo +EOF` = foo' + +check '"`cat <<EOF +foo +EOF`" = foo' + +check '`eval "cat <<EOF +foo +EOF"` = foo' + +test "x$failures" = x diff --git a/corebinutils/sh/tests/parser/heredoc12.0 b/corebinutils/sh/tests/parser/heredoc12.0 new file mode 100644 index 0000000000..0209a94d5f --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc12.0 @@ -0,0 +1,46 @@ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +longmark=`printf %01000d 4` +longmarkstripped=`printf %0999d 0` + +check '"$(cat <<'"$longmark +$longmark"' +echo yes)" = "yes"' + +check '"$(cat <<\'"$longmark +$longmark"' +echo yes)" = "yes"' + +check '"$(cat <<'"$longmark +yes +$longmark"' +)" = "yes"' + +check '"$(cat <<\'"$longmark +yes +$longmark"' +)" = "yes"' + +check '"$(cat <<'"$longmark +$longmarkstripped +$longmark. +$longmark"' +)" = "'"$longmarkstripped +$longmark."'"' + +check '"$(cat <<\'"$longmark +$longmarkstripped +$longmark. +$longmark"' +)" = "'"$longmarkstripped +$longmark."'"' + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/parser/heredoc13.0 b/corebinutils/sh/tests/parser/heredoc13.0 new file mode 100644 index 0000000000..1a3de05721 --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc13.0 @@ -0,0 +1,20 @@ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +check '"$(cat <<"" + +echo yes)" = "yes"' + +check '"$(cat <<"" +yes + +)" = "yes"' + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/parser/heredoc14.0 b/corebinutils/sh/tests/parser/heredoc14.0 new file mode 100644 index 0000000000..036be53dc0 --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc14.0 @@ -0,0 +1,8 @@ +# +read x <<EOF; for i in "$x" +value +EOF +do + x=$x.$i +done +[ "$x" = value.value ] diff --git a/corebinutils/sh/tests/parser/heredoc15.0 b/corebinutils/sh/tests/parser/heredoc15.0 new file mode 100644 index 0000000000..94c5c5f31b --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc15.0 @@ -0,0 +1,9 @@ +# +set -- dummy +read x <<EOF; for i +value +EOF +do + x=$x.$i +done +[ "$x" = value.dummy ] diff --git a/corebinutils/sh/tests/parser/heredoc16.0 b/corebinutils/sh/tests/parser/heredoc16.0 new file mode 100644 index 0000000000..84e5e02ae3 --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc16.0 @@ -0,0 +1,8 @@ +# +read x <<EOF; case $x +value +EOF +in + value) x=$x.extended +esac +[ "$x" = value.extended ] diff --git a/corebinutils/sh/tests/parser/heredoc2.0 b/corebinutils/sh/tests/parser/heredoc2.0 new file mode 100644 index 0000000000..8b936bcb37 --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc2.0 @@ -0,0 +1,38 @@ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +s='ast*que?non' sq=\' dq=\" + +check '"$(cat <<EOF +${s} +EOF +)" = "ast*que?non"' + +check '"$(cat <<EOF +${s+'$sq'x'$sq'} +EOF +)" = ${sq}x${sq}' + +check '"$(cat <<EOF +${s#ast} +EOF +)" = "*que?non"' + +check '"$(cat <<EOF +${s##"ast"} +EOF +)" = "*que?non"' + +check '"$(cat <<EOF +${s##'$sq'ast'$sq'} +EOF +)" = "*que?non"' + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/parser/heredoc3.0 b/corebinutils/sh/tests/parser/heredoc3.0 new file mode 100644 index 0000000000..dabd0650bd --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc3.0 @@ -0,0 +1,6 @@ + +# This may be expected to work, but pretty much only ash derivatives allow it. + +test "$(cat <<EOF)" = "hi there" +hi there +EOF diff --git a/corebinutils/sh/tests/parser/heredoc4.0 b/corebinutils/sh/tests/parser/heredoc4.0 new file mode 100644 index 0000000000..46262887d7 --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc4.0 @@ -0,0 +1,43 @@ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +f() { + cat <<EOF && echo `echo bar` +foo +EOF +} +check '"`f`" = "foo +bar"' + +f() { + cat <<EOF && echo $(echo bar) +foo +EOF +} +check '"$(f)" = "foo +bar"' + +f() { + echo `echo bar` && cat <<EOF +foo +EOF +} +check '"`f`" = "bar +foo"' + +f() { + echo $(echo bar) && cat <<EOF +foo +EOF +} +check '"$(f)" = "bar +foo"' + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/parser/heredoc5.0 b/corebinutils/sh/tests/parser/heredoc5.0 new file mode 100644 index 0000000000..a336205acf --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc5.0 @@ -0,0 +1,55 @@ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +f() { + cat <<EOF && echo `cat <<EOF +bar +EOF +` +foo +EOF +} +check '"`f`" = "foo +bar"' + +f() { + cat <<EOF && echo $(cat <<EOF +bar +EOF +) +foo +EOF +} +check '"$(f)" = "foo +bar"' + +f() { + echo `cat <<EOF +bar +EOF +` && cat <<EOF +foo +EOF +} +check '"`f`" = "bar +foo"' + +f() { + echo $(cat <<EOF +bar +EOF +) && cat <<EOF +foo +EOF +} +check '"$(f)" = "bar +foo"' + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/parser/heredoc6.0 b/corebinutils/sh/tests/parser/heredoc6.0 new file mode 100644 index 0000000000..24570bcfd4 --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc6.0 @@ -0,0 +1,4 @@ + +r= +! command eval ": <<EOF; )" 2>/dev/null; command eval : hi \${r:=0} +exit ${r:-3} diff --git a/corebinutils/sh/tests/parser/heredoc7.0 b/corebinutils/sh/tests/parser/heredoc7.0 new file mode 100644 index 0000000000..821e897003 --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc7.0 @@ -0,0 +1,18 @@ + +# Some of these created malformed parse trees with null pointers for here +# documents, causing the here document writing process to segfault. +eval ': <<EOF' +eval ': <<EOF;' +eval '`: <<EOF`' +eval '`: <<EOF;`' +eval '`: <<EOF`;' +eval '`: <<EOF;`;' + +# Some of these created malformed parse trees with null pointers for here +# documents, causing sh to segfault. +eval ': <<\EOF' +eval ': <<\EOF;' +eval '`: <<\EOF`' +eval '`: <<\EOF;`' +eval '`: <<\EOF`;' +eval '`: <<\EOF;`;' diff --git a/corebinutils/sh/tests/parser/heredoc8.0 b/corebinutils/sh/tests/parser/heredoc8.0 new file mode 100644 index 0000000000..673fc270fa --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc8.0 @@ -0,0 +1,19 @@ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +s='ast*que?non' sq=\' dq=\" + +# This is possibly useful but differs from other shells. +check '"$(cat <<EOF +${s+"x"} +EOF +)" = ${dq}x${dq}' + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/parser/heredoc9.0 b/corebinutils/sh/tests/parser/heredoc9.0 new file mode 100644 index 0000000000..d071bf51ef --- /dev/null +++ b/corebinutils/sh/tests/parser/heredoc9.0 @@ -0,0 +1,57 @@ + +# It may be argued that +# x=$(cat <<EOF +# foo +# EOF) +# is a valid complete command that sets x to foo, because +# cat <<EOF +# foo +# EOF +# is a valid script even without the final newline. +# However, if the here-document is not within a new-style command substitution +# or there are other constructs nested inside the command substitution that +# need terminators, the delimiter at the start of a line followed by a close +# parenthesis is clearly a literal part of the here-document. + +# This file contains tests that also work with simplistic $(...) parsers. + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +check '`${SH} -c "cat <<EOF +EOF) +EOF +"` = "EOF)"' + +check '`${SH} -c "(cat <<EOF +EOF) +EOF +)"` = "EOF)"' + +check '"`cat <<EOF +EOF x +EOF +`" = "EOF x"' + +check '"`cat <<EOF +EOF ) +EOF +`" = "EOF )"' + +check '"`cat <<EOF +EOF) +EOF +`" = "EOF)"' + +check '"$(cat <<EOF +EOF x +EOF +)" = "EOF x"' + +exit $((failures != 0)) diff --git a/corebinutils/sh/tests/parser/line-cont1.0 b/corebinutils/sh/tests/parser/line-cont1.0 new file mode 100644 index 0000000000..c130070e37 --- /dev/null +++ b/corebinutils/sh/tests/parser/line-cont1.0 @@ -0,0 +1,15 @@ + +i\ +f +t\ +r\ +u\ +e +t\ +h\ +e\ +n +: +\ +f\ +i diff --git a/corebinutils/sh/tests/parser/line-cont10.0 b/corebinutils/sh/tests/parser/line-cont10.0 new file mode 100644 index 0000000000..d416e42414 --- /dev/null +++ b/corebinutils/sh/tests/parser/line-cont10.0 @@ -0,0 +1,17 @@ + +v=XaaaXbbbX +[ "${v\ +#\ +*\ +a}.${v\ +#\ +#\ +*\ +a}.${v\ +%\ +b\ +*}.${v\ +%\ +%\ +b\ +*}" = aaXbbbX.XbbbX.XaaaXbb.XaaaX ] diff --git a/corebinutils/sh/tests/parser/line-cont11.0 b/corebinutils/sh/tests/parser/line-cont11.0 new file mode 100644 index 0000000000..e1c2245b75 --- /dev/null +++ b/corebinutils/sh/tests/parser/line-cont11.0 @@ -0,0 +1,22 @@ + +T=$(mktemp "${TMPDIR:-/tmp}/sh-test.XXXXXXXX") || exit +trap 'rm -f -- "$T"' 0 +w='#A' +# A naive pgetc_linecont() would push back two characters here, which +# fails if a new buffer is read between the two characters. +c='${w#\#}' +c=$c$c$c$c +c=$c$c$c$c +c=$c$c$c$c +c=$c$c$c$c +c=$c$c$c$c +c=$c$c$c$c +printf 'v=%s\n' "$c" >"$T" +. "$T" +if [ "${#v}" != 4096 ]; then + echo "Length is bad (${#v})" + exit 3 +fi +case $v in +*[!A]*) echo "Content is bad"; exit 3 ;; +esac diff --git a/corebinutils/sh/tests/parser/line-cont12.0 b/corebinutils/sh/tests/parser/line-cont12.0 new file mode 100644 index 0000000000..2028a2ee6d --- /dev/null +++ b/corebinutils/sh/tests/parser/line-cont12.0 @@ -0,0 +1,4 @@ + +[ '\ +' = "\\ +" ] diff --git a/corebinutils/sh/tests/parser/line-cont2.0 b/corebinutils/sh/tests/parser/line-cont2.0 new file mode 100644 index 0000000000..d5f7ae9037 --- /dev/null +++ b/corebinutils/sh/tests/parser/line-cont2.0 @@ -0,0 +1,3 @@ + +[ "a\ +b" = ab ] diff --git a/corebinutils/sh/tests/parser/line-cont3.0 b/corebinutils/sh/tests/parser/line-cont3.0 new file mode 100644 index 0000000000..3f9d83a141 --- /dev/null +++ b/corebinutils/sh/tests/parser/line-cont3.0 @@ -0,0 +1,6 @@ + +v=`printf %s 'a\ +b'` +w="`printf %s 'c\ +d'`" +[ "$v$w" = abcd ] diff --git a/corebinutils/sh/tests/parser/line-cont4.0 b/corebinutils/sh/tests/parser/line-cont4.0 new file mode 100644 index 0000000000..0a58847b2c --- /dev/null +++ b/corebinutils/sh/tests/parser/line-cont4.0 @@ -0,0 +1,7 @@ + +v=abcd +[ "$\ +v.$\ +{v}.${\ +v}.${v\ +}" = abcd.abcd.abcd.abcd ] diff --git a/corebinutils/sh/tests/parser/line-cont5.0 b/corebinutils/sh/tests/parser/line-cont5.0 new file mode 100644 index 0000000000..d8b5f08d7b --- /dev/null +++ b/corebinutils/sh/tests/parser/line-cont5.0 @@ -0,0 +1,13 @@ + +bad=1 +case x in +x\ +) ;\ +; *) exit 7 +esac &\ +& bad= &\ +& : >\ +>/dev/null + +false |\ +| [ -z "$bad" ] diff --git a/corebinutils/sh/tests/parser/line-cont6.0 b/corebinutils/sh/tests/parser/line-cont6.0 new file mode 100644 index 0000000000..c5da14929e --- /dev/null +++ b/corebinutils/sh/tests/parser/line-cont6.0 @@ -0,0 +1,22 @@ + +v0\ +=abc + +v=$(cat <\ +<\ +E\ +O\ +F +${v0}d +EOF +) + +w=$(cat <\ +<\ +-\ +EOF + efgh +EOF +) + +[ "$v.$w" = "abcd.efgh" ] diff --git a/corebinutils/sh/tests/parser/line-cont7.0 b/corebinutils/sh/tests/parser/line-cont7.0 new file mode 100644 index 0000000000..ccc7fe4f02 --- /dev/null +++ b/corebinutils/sh/tests/parser/line-cont7.0 @@ -0,0 +1,6 @@ + +[ "$(\ +( +1\ ++ 1)\ +)" = 2 ] diff --git a/corebinutils/sh/tests/parser/line-cont8.0 b/corebinutils/sh/tests/parser/line-cont8.0 new file mode 100644 index 0000000000..7db2c9d653 --- /dev/null +++ b/corebinutils/sh/tests/parser/line-cont8.0 @@ -0,0 +1,5 @@ + +set -- a b c d e f g h i j +[ "${1\ +0\ +}" = j ] diff --git a/corebinutils/sh/tests/parser/line-cont9.0 b/corebinutils/sh/tests/parser/line-cont9.0 new file mode 100644 index 0000000000..e0f8c2b061 --- /dev/null +++ b/corebinutils/sh/tests/parser/line-cont9.0 @@ -0,0 +1,5 @@ + +[ "${$\ +:\ ++\ +xyz}" = xyz ] diff --git a/corebinutils/sh/tests/parser/no-space1.0 b/corebinutils/sh/tests/parser/no-space1.0 new file mode 100644 index 0000000000..2baba77520 --- /dev/null +++ b/corebinutils/sh/tests/parser/no-space1.0 @@ -0,0 +1,17 @@ + +# These are ugly but are required to work. + +set -e + +while(false)do(:)done +if(false)then(:)fi +if(false)then(:)else(:)fi +(:&&:)||: +until(:)do(:)done +case x in(x);;*)exit 1;(:)esac +case x in(x);;*)exit 1;;esac +for i do(:)done +{(:)} +f(){(:)} +:|: +(:)|(:) diff --git a/corebinutils/sh/tests/parser/no-space2.0 b/corebinutils/sh/tests/parser/no-space2.0 new file mode 100644 index 0000000000..d03d7aa815 --- /dev/null +++ b/corebinutils/sh/tests/parser/no-space2.0 @@ -0,0 +1,6 @@ + +# This conflicts with ksh extended patterns but occurs in the wild. + +set -e + +!(false) diff --git a/corebinutils/sh/tests/parser/nul1.0 b/corebinutils/sh/tests/parser/nul1.0 new file mode 100644 index 0000000000..292669003a --- /dev/null +++ b/corebinutils/sh/tests/parser/nul1.0 @@ -0,0 +1,11 @@ +# Although POSIX does not specify the effect of NUL bytes in scripts, +# we ignore them. + +{ + printf 'v=%03000d\0%02000d' 7 2 + dd if=/dev/zero bs=1000 count=1 status=none + printf '1 w=%03000d%02000d1\0\n' 7 2 + printf '\0l\0v\0=\0$\0{\0#\0v\0}\n' + printf '\0l\0w\0=\0\0$\0{\0#\0w}\0\0\0\n' + printf '[ "$lv.$lw.$v" = "5001.5001.$w" ]\n' +} | ${SH} diff --git a/corebinutils/sh/tests/parser/only-redir1.0 b/corebinutils/sh/tests/parser/only-redir1.0 new file mode 100644 index 0000000000..1d8aff62e7 --- /dev/null +++ b/corebinutils/sh/tests/parser/only-redir1.0 @@ -0,0 +1,2 @@ +</dev/null & +wait $! diff --git a/corebinutils/sh/tests/parser/only-redir2.0 b/corebinutils/sh/tests/parser/only-redir2.0 new file mode 100644 index 0000000000..efe4c3947d --- /dev/null +++ b/corebinutils/sh/tests/parser/only-redir2.0 @@ -0,0 +1 @@ +</dev/null | : diff --git a/corebinutils/sh/tests/parser/only-redir3.0 b/corebinutils/sh/tests/parser/only-redir3.0 new file mode 100644 index 0000000000..549873d15b --- /dev/null +++ b/corebinutils/sh/tests/parser/only-redir3.0 @@ -0,0 +1 @@ +case x in x) </dev/null ;; esac diff --git a/corebinutils/sh/tests/parser/only-redir4.0 b/corebinutils/sh/tests/parser/only-redir4.0 new file mode 100644 index 0000000000..f85e9da727 --- /dev/null +++ b/corebinutils/sh/tests/parser/only-redir4.0 @@ -0,0 +1 @@ +case x in x) </dev/null ;& esac diff --git a/corebinutils/sh/tests/parser/pipe-not1.0 b/corebinutils/sh/tests/parser/pipe-not1.0 new file mode 100644 index 0000000000..12ad172881 --- /dev/null +++ b/corebinutils/sh/tests/parser/pipe-not1.0 @@ -0,0 +1,2 @@ + +: | ! : | false diff --git a/corebinutils/sh/tests/parser/ps1-expand1.0 b/corebinutils/sh/tests/parser/ps1-expand1.0 new file mode 100644 index 0000000000..351e6437a0 --- /dev/null +++ b/corebinutils/sh/tests/parser/ps1-expand1.0 @@ -0,0 +1,7 @@ +# Test simple variable expansion in PS1 +testvar=abcdef +output=$(testvar=abcdef PS1='$testvar:' ENV=/dev/null ${SH} +m -i </dev/null 2>&1) +case $output in +*abcdef*) exit 0 ;; +*) echo "Expected 'abcdef' in prompt output"; exit 1 ;; +esac diff --git a/corebinutils/sh/tests/parser/ps1-expand2.0 b/corebinutils/sh/tests/parser/ps1-expand2.0 new file mode 100644 index 0000000000..ed31a7c171 --- /dev/null +++ b/corebinutils/sh/tests/parser/ps1-expand2.0 @@ -0,0 +1,7 @@ +# Test braced variable expansion in PS1 +testvar=xyz123 +output=$(testvar=xyz123 PS1='prefix-${testvar}-suffix:' ENV=/dev/null ${SH} +m -i </dev/null 2>&1) +case $output in +*xyz123*) exit 0 ;; +*) echo "Expected 'xyz123' in prompt output"; exit 1 ;; +esac diff --git a/corebinutils/sh/tests/parser/ps1-expand3.0 b/corebinutils/sh/tests/parser/ps1-expand3.0 new file mode 100644 index 0000000000..0b6270c300 --- /dev/null +++ b/corebinutils/sh/tests/parser/ps1-expand3.0 @@ -0,0 +1,8 @@ +# Test special parameter $$ (PID) in PS1 +output=$(PS1='pid:$$:' ENV=/dev/null ${SH} +m -i </dev/null 2>&1) +# Check that output contains "pid:" followed by a number (not literal $$) +case $output in +*pid:\$\$:*) echo "PID not expanded, got literal \$\$"; exit 1 ;; +*pid:[0-9]*) exit 0 ;; +*) echo "Expected PID after 'pid:' in output"; exit 1 ;; +esac diff --git a/corebinutils/sh/tests/parser/ps1-expand4.0 b/corebinutils/sh/tests/parser/ps1-expand4.0 new file mode 100644 index 0000000000..623c52707e --- /dev/null +++ b/corebinutils/sh/tests/parser/ps1-expand4.0 @@ -0,0 +1,8 @@ +# Test special parameter $? (exit status) in PS1 +output=$(PS1='status:$?:' ENV=/dev/null ${SH} +m -i </dev/null 2>&1) +# Should start with exit status 0 +case $output in +*status:\$?:*) echo "Exit status not expanded, got literal \$?"; exit 1 ;; +*status:0:*) exit 0 ;; +*) echo "Expected 'status:0:' in initial prompt"; exit 1 ;; +esac diff --git a/corebinutils/sh/tests/parser/ps1-expand5.0 b/corebinutils/sh/tests/parser/ps1-expand5.0 new file mode 100644 index 0000000000..73fe3ba5a3 --- /dev/null +++ b/corebinutils/sh/tests/parser/ps1-expand5.0 @@ -0,0 +1,8 @@ +# Test positional parameter $0 in PS1 +output=$(PS1='shell:$0:' ENV=/dev/null ${SH} +m -i </dev/null 2>&1) +# $0 should contain the shell name/path +case $output in +*shell:\$0:*) echo "Positional parameter not expanded, got literal \$0"; exit 1 ;; +*shell:*sh*:*) exit 0 ;; +*) echo "Expected shell name after 'shell:' in output"; exit 1 ;; +esac diff --git a/corebinutils/sh/tests/parser/ps2-expand1.0 b/corebinutils/sh/tests/parser/ps2-expand1.0 new file mode 100644 index 0000000000..f0a3a77ded --- /dev/null +++ b/corebinutils/sh/tests/parser/ps2-expand1.0 @@ -0,0 +1,12 @@ +# Test variable expansion in PS2 (continuation prompt) +testvar=continue +# Send incomplete command (backslash at end) to trigger PS2 +output=$(testvar=continue PS2='$testvar>' ENV=/dev/null ${SH} +m -i <<EOF 2>&1 +echo \\ +done +EOF +) +case $output in +*continue\>*) exit 0 ;; +*) echo "Expected 'continue>' in PS2 output"; exit 1 ;; +esac diff --git a/corebinutils/sh/tests/parser/set-v1.0 b/corebinutils/sh/tests/parser/set-v1.0 new file mode 100644 index 0000000000..65106bc70b --- /dev/null +++ b/corebinutils/sh/tests/parser/set-v1.0 @@ -0,0 +1,7 @@ + +${SH} <<\EOF +echo one >&2 +set -v +echo two >&2 +echo three >&2 +EOF diff --git a/corebinutils/sh/tests/parser/set-v1.0.stderr b/corebinutils/sh/tests/parser/set-v1.0.stderr new file mode 100644 index 0000000000..d904fa5ffd --- /dev/null +++ b/corebinutils/sh/tests/parser/set-v1.0.stderr @@ -0,0 +1,5 @@ +one +echo two >&2 +two +echo three >&2 +three diff --git a/corebinutils/sh/tests/parser/var-assign1.0 b/corebinutils/sh/tests/parser/var-assign1.0 new file mode 100644 index 0000000000..55034f2ccc --- /dev/null +++ b/corebinutils/sh/tests/parser/var-assign1.0 @@ -0,0 +1,18 @@ +# In a variable assignment, both the name and the equals sign must be entirely +# unquoted. Therefore, there is only one assignment below; the other words +# containing equals signs are command words. + +abc=0 +\abc=1 2>/dev/null +a\bc=2 2>/dev/null +abc\=3 2>/dev/null +a\bc\=4 2>/dev/null +'abc'=5 2>/dev/null +a'b'c=6 2>/dev/null +abc'='7 2>/dev/null +'abc=8' 2>/dev/null +"abc"=9 2>/dev/null +a"b"c=10 2>/dev/null +abc"="11 2>/dev/null +"abc=12" 2>/dev/null +[ "$abc" = 0 ] diff --git a/corebinutils/sh/tests/run-default-sigpipe.c b/corebinutils/sh/tests/run-default-sigpipe.c new file mode 100644 index 0000000000..4acc6f192a --- /dev/null +++ b/corebinutils/sh/tests/run-default-sigpipe.c @@ -0,0 +1,82 @@ +/* + +SPDX-License-Identifier: BSD-3-Clause + +Copyright (c) 2026 + Project Tick. All rights reserved. + +This code is derived from software contributed to Berkeley by +the Institute of Electrical and Electronics Engineers, Inc. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. Neither the name of the University nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +*/ + +#include <errno.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static void +reset_sigpipe(void) +{ + struct sigaction sa; + sigset_t set; + + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = SIG_DFL; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGPIPE, &sa, NULL) != 0) { + fprintf(stderr, "run-default-sigpipe: sigaction(SIGPIPE): %s\n", + strerror(errno)); + exit(125); + } + + sigemptyset(&set); + sigaddset(&set, SIGPIPE); + if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { + fprintf(stderr, "run-default-sigpipe: sigprocmask(SIGPIPE): %s\n", + strerror(errno)); + exit(125); + } +} + +int +main(int argc, char **argv) +{ + if (argc < 2) { + fprintf(stderr, "usage: %s program [args...]\n", argv[0]); + return (125); + } + + reset_sigpipe(); + execv(argv[1], argv + 1); + + fprintf(stderr, "run-default-sigpipe: execv(%s): %s\n", argv[1], + strerror(errno)); + return (125); +} diff --git a/corebinutils/sh/tests/set-e/Makefile b/corebinutils/sh/tests/set-e/Makefile new file mode 100644 index 0000000000..6b5b9a2280 --- /dev/null +++ b/corebinutils/sh/tests/set-e/Makefile @@ -0,0 +1,44 @@ +PACKAGE= tests + +TESTSDIR= ${TESTSBASE}/bin/sh/${.CURDIR:T} + +.PATH: ${.CURDIR:H} +ATF_TESTS_SH= functional_test + +${PACKAGE}FILES+= and1.0 +${PACKAGE}FILES+= and2.1 +${PACKAGE}FILES+= and3.0 +${PACKAGE}FILES+= and4.0 +${PACKAGE}FILES+= background1.0 +${PACKAGE}FILES+= cmd1.0 +${PACKAGE}FILES+= cmd2.1 +${PACKAGE}FILES+= elif1.0 +${PACKAGE}FILES+= elif2.0 +${PACKAGE}FILES+= eval1.0 +${PACKAGE}FILES+= eval2.1 +${PACKAGE}FILES+= for1.0 +${PACKAGE}FILES+= func1.0 +${PACKAGE}FILES+= func2.1 +${PACKAGE}FILES+= if1.0 +${PACKAGE}FILES+= if2.0 +${PACKAGE}FILES+= if3.0 +${PACKAGE}FILES+= not1.0 +${PACKAGE}FILES+= not2.0 +${PACKAGE}FILES+= or1.0 +${PACKAGE}FILES+= or2.0 +${PACKAGE}FILES+= or3.1 +${PACKAGE}FILES+= pipe1.1 +${PACKAGE}FILES+= pipe2.0 +${PACKAGE}FILES+= return1.0 +${PACKAGE}FILES+= semi1.1 +${PACKAGE}FILES+= semi2.1 +${PACKAGE}FILES+= subshell1.0 +${PACKAGE}FILES+= subshell2.1 +${PACKAGE}FILES+= until1.0 +${PACKAGE}FILES+= until2.0 +${PACKAGE}FILES+= until3.0 +${PACKAGE}FILES+= while1.0 +${PACKAGE}FILES+= while2.0 +${PACKAGE}FILES+= while3.0 + +.include <bsd.test.mk> diff --git a/corebinutils/sh/tests/set-e/Makefile.depend b/corebinutils/sh/tests/set-e/Makefile.depend new file mode 100644 index 0000000000..11aba52f82 --- /dev/null +++ b/corebinutils/sh/tests/set-e/Makefile.depend @@ -0,0 +1,10 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/corebinutils/sh/tests/set-e/and1.0 b/corebinutils/sh/tests/set-e/and1.0 new file mode 100644 index 0000000000..c5b91a5faf --- /dev/null +++ b/corebinutils/sh/tests/set-e/and1.0 @@ -0,0 +1,2 @@ +set -e +true && true diff --git a/corebinutils/sh/tests/set-e/and2.1 b/corebinutils/sh/tests/set-e/and2.1 new file mode 100644 index 0000000000..c6994aaa1b --- /dev/null +++ b/corebinutils/sh/tests/set-e/and2.1 @@ -0,0 +1,3 @@ +set -e +true && false +exit 0 diff --git a/corebinutils/sh/tests/set-e/and3.0 b/corebinutils/sh/tests/set-e/and3.0 new file mode 100644 index 0000000000..da983d7a24 --- /dev/null +++ b/corebinutils/sh/tests/set-e/and3.0 @@ -0,0 +1,3 @@ +set -e +false && true +exit 0 diff --git a/corebinutils/sh/tests/set-e/and4.0 b/corebinutils/sh/tests/set-e/and4.0 new file mode 100644 index 0000000000..1ca44a840d --- /dev/null +++ b/corebinutils/sh/tests/set-e/and4.0 @@ -0,0 +1,3 @@ +set -e +false && false +exit 0 diff --git a/corebinutils/sh/tests/set-e/background1.0 b/corebinutils/sh/tests/set-e/background1.0 new file mode 100644 index 0000000000..288f9c4076 --- /dev/null +++ b/corebinutils/sh/tests/set-e/background1.0 @@ -0,0 +1,2 @@ +set -e +false & diff --git a/corebinutils/sh/tests/set-e/cmd1.0 b/corebinutils/sh/tests/set-e/cmd1.0 new file mode 100644 index 0000000000..5f06bd404a --- /dev/null +++ b/corebinutils/sh/tests/set-e/cmd1.0 @@ -0,0 +1,2 @@ +set -e +true diff --git a/corebinutils/sh/tests/set-e/cmd2.1 b/corebinutils/sh/tests/set-e/cmd2.1 new file mode 100644 index 0000000000..1f677ce66b --- /dev/null +++ b/corebinutils/sh/tests/set-e/cmd2.1 @@ -0,0 +1,3 @@ +set -e +false +exit 0 diff --git a/corebinutils/sh/tests/set-e/elif1.0 b/corebinutils/sh/tests/set-e/elif1.0 new file mode 100644 index 0000000000..5f438bab1c --- /dev/null +++ b/corebinutils/sh/tests/set-e/elif1.0 @@ -0,0 +1,6 @@ +set -e +if false; then + : +elif false; then + : +fi diff --git a/corebinutils/sh/tests/set-e/elif2.0 b/corebinutils/sh/tests/set-e/elif2.0 new file mode 100644 index 0000000000..f691da5583 --- /dev/null +++ b/corebinutils/sh/tests/set-e/elif2.0 @@ -0,0 +1,6 @@ +set -e +if false; then + : +elif false; false; then + : +fi diff --git a/corebinutils/sh/tests/set-e/eval1.0 b/corebinutils/sh/tests/set-e/eval1.0 new file mode 100644 index 0000000000..60bb4e237a --- /dev/null +++ b/corebinutils/sh/tests/set-e/eval1.0 @@ -0,0 +1,2 @@ +set -e +eval false || true diff --git a/corebinutils/sh/tests/set-e/eval2.1 b/corebinutils/sh/tests/set-e/eval2.1 new file mode 100644 index 0000000000..83171c5aed --- /dev/null +++ b/corebinutils/sh/tests/set-e/eval2.1 @@ -0,0 +1,3 @@ +set -e +eval false +exit 0 diff --git a/corebinutils/sh/tests/set-e/for1.0 b/corebinutils/sh/tests/set-e/for1.0 new file mode 100644 index 0000000000..345bf14bda --- /dev/null +++ b/corebinutils/sh/tests/set-e/for1.0 @@ -0,0 +1,8 @@ +set -e +f() { + for i in a b c; do + false + true + done +} +f || true diff --git a/corebinutils/sh/tests/set-e/func1.0 b/corebinutils/sh/tests/set-e/func1.0 new file mode 100644 index 0000000000..a06e7978bc --- /dev/null +++ b/corebinutils/sh/tests/set-e/func1.0 @@ -0,0 +1,6 @@ +set -e +f() { + false + true +} +f || true diff --git a/corebinutils/sh/tests/set-e/func2.1 b/corebinutils/sh/tests/set-e/func2.1 new file mode 100644 index 0000000000..9fb0ee3919 --- /dev/null +++ b/corebinutils/sh/tests/set-e/func2.1 @@ -0,0 +1,6 @@ +set -e +f() { + false + exit 0 +} +f diff --git a/corebinutils/sh/tests/set-e/if1.0 b/corebinutils/sh/tests/set-e/if1.0 new file mode 100644 index 0000000000..a4bd95763b --- /dev/null +++ b/corebinutils/sh/tests/set-e/if1.0 @@ -0,0 +1,4 @@ +set -e +if false; then + : +fi diff --git a/corebinutils/sh/tests/set-e/if2.0 b/corebinutils/sh/tests/set-e/if2.0 new file mode 100644 index 0000000000..2323448ef7 --- /dev/null +++ b/corebinutils/sh/tests/set-e/if2.0 @@ -0,0 +1,6 @@ +set -e +# PR 28852 +if true; then + false && true +fi +exit 0 diff --git a/corebinutils/sh/tests/set-e/if3.0 b/corebinutils/sh/tests/set-e/if3.0 new file mode 100644 index 0000000000..3dd39380a7 --- /dev/null +++ b/corebinutils/sh/tests/set-e/if3.0 @@ -0,0 +1,4 @@ +set -e +if false; false; then + : +fi diff --git a/corebinutils/sh/tests/set-e/not1.0 b/corebinutils/sh/tests/set-e/not1.0 new file mode 100644 index 0000000000..eba0792db7 --- /dev/null +++ b/corebinutils/sh/tests/set-e/not1.0 @@ -0,0 +1,3 @@ +set -e +! true +exit 0 diff --git a/corebinutils/sh/tests/set-e/not2.0 b/corebinutils/sh/tests/set-e/not2.0 new file mode 100644 index 0000000000..40a551c664 --- /dev/null +++ b/corebinutils/sh/tests/set-e/not2.0 @@ -0,0 +1,3 @@ +set -e +! false +! eval false diff --git a/corebinutils/sh/tests/set-e/or1.0 b/corebinutils/sh/tests/set-e/or1.0 new file mode 100644 index 0000000000..b556313d4b --- /dev/null +++ b/corebinutils/sh/tests/set-e/or1.0 @@ -0,0 +1,2 @@ +set -e +true || false diff --git a/corebinutils/sh/tests/set-e/or2.0 b/corebinutils/sh/tests/set-e/or2.0 new file mode 100644 index 0000000000..3606cc173c --- /dev/null +++ b/corebinutils/sh/tests/set-e/or2.0 @@ -0,0 +1,2 @@ +set -e +false || true diff --git a/corebinutils/sh/tests/set-e/or3.1 b/corebinutils/sh/tests/set-e/or3.1 new file mode 100644 index 0000000000..ae6c5cdd64 --- /dev/null +++ b/corebinutils/sh/tests/set-e/or3.1 @@ -0,0 +1,3 @@ +set -e +false || false +exit 0 diff --git a/corebinutils/sh/tests/set-e/pipe1.1 b/corebinutils/sh/tests/set-e/pipe1.1 new file mode 100644 index 0000000000..ce3f84861e --- /dev/null +++ b/corebinutils/sh/tests/set-e/pipe1.1 @@ -0,0 +1,3 @@ +set -e +true | false +exit 0 diff --git a/corebinutils/sh/tests/set-e/pipe2.0 b/corebinutils/sh/tests/set-e/pipe2.0 new file mode 100644 index 0000000000..987d0f1a3c --- /dev/null +++ b/corebinutils/sh/tests/set-e/pipe2.0 @@ -0,0 +1,2 @@ +set -e +false | true diff --git a/corebinutils/sh/tests/set-e/return1.0 b/corebinutils/sh/tests/set-e/return1.0 new file mode 100644 index 0000000000..66a58cba78 --- /dev/null +++ b/corebinutils/sh/tests/set-e/return1.0 @@ -0,0 +1,10 @@ +set -e + +# PR 77067, 85267 +f() { + return 1 + true +} + +f || true +exit 0 diff --git a/corebinutils/sh/tests/set-e/semi1.1 b/corebinutils/sh/tests/set-e/semi1.1 new file mode 100644 index 0000000000..74a6a0d725 --- /dev/null +++ b/corebinutils/sh/tests/set-e/semi1.1 @@ -0,0 +1,3 @@ +set -e +false; true +exit 0 diff --git a/corebinutils/sh/tests/set-e/semi2.1 b/corebinutils/sh/tests/set-e/semi2.1 new file mode 100644 index 0000000000..3a37f73fbf --- /dev/null +++ b/corebinutils/sh/tests/set-e/semi2.1 @@ -0,0 +1,3 @@ +set -e +true; false +exit 0 diff --git a/corebinutils/sh/tests/set-e/subshell1.0 b/corebinutils/sh/tests/set-e/subshell1.0 new file mode 100644 index 0000000000..10561215e6 --- /dev/null +++ b/corebinutils/sh/tests/set-e/subshell1.0 @@ -0,0 +1,2 @@ +set -e +(true) diff --git a/corebinutils/sh/tests/set-e/subshell2.1 b/corebinutils/sh/tests/set-e/subshell2.1 new file mode 100644 index 0000000000..e5d2976d35 --- /dev/null +++ b/corebinutils/sh/tests/set-e/subshell2.1 @@ -0,0 +1,3 @@ +set -e +(false) +exit 0 diff --git a/corebinutils/sh/tests/set-e/until1.0 b/corebinutils/sh/tests/set-e/until1.0 new file mode 100644 index 0000000000..6a6c10dbfc --- /dev/null +++ b/corebinutils/sh/tests/set-e/until1.0 @@ -0,0 +1,4 @@ +set -e +until false; do + break +done diff --git a/corebinutils/sh/tests/set-e/until2.0 b/corebinutils/sh/tests/set-e/until2.0 new file mode 100644 index 0000000000..628ebb27b1 --- /dev/null +++ b/corebinutils/sh/tests/set-e/until2.0 @@ -0,0 +1,4 @@ +set -e +until false; false; do + break +done diff --git a/corebinutils/sh/tests/set-e/until3.0 b/corebinutils/sh/tests/set-e/until3.0 new file mode 100644 index 0000000000..54f233537d --- /dev/null +++ b/corebinutils/sh/tests/set-e/until3.0 @@ -0,0 +1,8 @@ +set -e +f() { + until false; do + false + break + done +} +f || true diff --git a/corebinutils/sh/tests/set-e/while1.0 b/corebinutils/sh/tests/set-e/while1.0 new file mode 100644 index 0000000000..367609b692 --- /dev/null +++ b/corebinutils/sh/tests/set-e/while1.0 @@ -0,0 +1,4 @@ +set -e +while false; do + : +done diff --git a/corebinutils/sh/tests/set-e/while2.0 b/corebinutils/sh/tests/set-e/while2.0 new file mode 100644 index 0000000000..7ec5dff283 --- /dev/null +++ b/corebinutils/sh/tests/set-e/while2.0 @@ -0,0 +1,4 @@ +set -e +while false; false; do + : +done diff --git a/corebinutils/sh/tests/set-e/while3.0 b/corebinutils/sh/tests/set-e/while3.0 new file mode 100644 index 0000000000..b0cf7ab171 --- /dev/null +++ b/corebinutils/sh/tests/set-e/while3.0 @@ -0,0 +1,8 @@ +set -e +f() { + while true; do + false + break + done +} +f || true diff --git a/corebinutils/sh/tests/test.sh b/corebinutils/sh/tests/test.sh new file mode 100644 index 0000000000..36613a663f --- /dev/null +++ b/corebinutils/sh/tests/test.sh @@ -0,0 +1,126 @@ +#!/bin/sh +# SPDX-License-Identifier: BSD-3-Clause +# Copyright (c) 2026 Project Tick. All rights reserved. +set -eu + +ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +TEST_ROOT="$ROOT/tests" +SH_BIN=${SH_BIN:-"$ROOT/out/sh"} +SH_RUNNER=${SH_RUNNER:-} +TMPDIR=${TMPDIR:-/tmp} +WORKDIR=$(mktemp -d "$TMPDIR/sh-test.XXXXXX") +trap 'rm -rf "$WORKDIR"' EXIT INT TERM + +SKIP_CASES=' +parameters/mail1.0 +parameters/mail2.0 +' + +fail() { + printf '%s\n' "FAIL: $1" >&2 + exit 1 +} + +should_skip() { + case " +$SKIP_CASES +" in + *" +$1 +"*) return 0 ;; + esac + return 1 +} + +assert_eq() { + name=$1 + expected_file=$2 + actual_file=$3 + if ! cmp -s "$expected_file" "$actual_file"; then + printf '%s\n' "FAIL: $name" >&2 + printf '%s\n' "--- expected ---" >&2 + cat "$expected_file" >&2 + printf '\n%s\n' "--- actual ---" >&2 + cat "$actual_file" >&2 + printf '\n' >&2 + exit 1 + fi +} + +[ -x "$SH_BIN" ] || fail "missing binary: $SH_BIN" + +export SH="$SH_BIN" +export LC_ALL=C +export PATH=/bin:/usr/bin + +cases_run=0 +cases_skipped=0 +case_list="$WORKDIR/cases.list" + +find "$TEST_ROOT" -type f \ + ! -name '*.stdout' \ + ! -name '*.stderr' \ + ! -name 'test.sh' \ + ! -name 'functional_test.sh' \ + | sed "s#^$TEST_ROOT/##" \ + | sort >"$case_list" + +while IFS= read -r rel; do + case_name=${rel##*/} + case $case_name in + *.[0-9]*) + : + ;; + *) + continue + ;; + esac + + if should_skip "$rel"; then + cases_skipped=$((cases_skipped + 1)) + printf '%s\n' "SKIP $rel" + continue + fi + + status_expected=${case_name##*.} + stdout_expected="$TEST_ROOT/$rel.stdout" + stderr_expected="$TEST_ROOT/$rel.stderr" + safe_name=$(printf '%s' "$rel" | tr '/\n' '__') + stdout_actual="$WORKDIR/$safe_name.stdout" + stderr_actual="$WORKDIR/$safe_name.stderr" + case_workdir="$WORKDIR/$safe_name.wd" + + mkdir -p "$case_workdir" + ( + cd "$case_workdir" + set +e + if [ -n "$SH_RUNNER" ]; then + "$SH_RUNNER" "$SH_BIN" "$TEST_ROOT/$rel" >"$stdout_actual" 2>"$stderr_actual" + else + "$SH_BIN" "$TEST_ROOT/$rel" >"$stdout_actual" 2>"$stderr_actual" + fi + rc=$? + set -e + [ "$rc" -eq "$status_expected" ] || { + printf '%s\n' "FAIL: $rel exit $rc expected $status_expected" >&2 + exit 1 + } + ) || exit 1 + + if [ -f "$stdout_expected" ]; then + assert_eq "$rel stdout" "$stdout_expected" "$stdout_actual" + else + [ ! -s "$stdout_actual" ] || fail "$rel unexpected stdout" + fi + + if [ -f "$stderr_expected" ]; then + assert_eq "$rel stderr" "$stderr_expected" "$stderr_actual" + else + [ ! -s "$stderr_actual" ] || fail "$rel unexpected stderr" + fi + + cases_run=$((cases_run + 1)) + printf '%s\n' "PASS $rel" +done <"$case_list" + +printf '%s\n' "PASS: ran $cases_run cases, skipped $cases_skipped" |
