| 1 | #! /bin/sh
|
|---|
| 2 | # posix-2.sh - Simple identification tests for POSIX.2 features
|
|---|
| 3 | # commonly missing or incorrectly implemented.
|
|---|
| 4 | # Time-stamp: <96/04/10 16:43:48 gildea>
|
|---|
| 5 | # By Stephen Gildea <[email protected]> March 1995
|
|---|
| 6 | #
|
|---|
| 7 | # Copyright (c) 1995 Stephen Gildea
|
|---|
| 8 | # Permission is hereby granted to deal in this Software without restriction.
|
|---|
| 9 | # THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
|
|---|
| 10 | #
|
|---|
| 11 | # MODIFIED BY [email protected] to make part of the bash test suite.
|
|---|
| 12 | # last change: Wed Jun 19 12:24:24 EDT 1996
|
|---|
| 13 | #
|
|---|
| 14 | # some of the tests:
|
|---|
| 15 | #
|
|---|
| 16 | # shell functions (do we care?)
|
|---|
| 17 | # var=${var:-val}
|
|---|
| 18 | # unset
|
|---|
| 19 | # set --
|
|---|
| 20 | # IFS parsing
|
|---|
| 21 | ## not exiting with -e and failed "if", the way Ultrix does (Ultrix 4.2?)
|
|---|
| 22 | # "$@" expands to zero arguments if passed zero arguments
|
|---|
| 23 | # $SHELL -c 'echo $1' bad good
|
|---|
| 24 | # test -x
|
|---|
| 25 | # positional parameters greater than 9
|
|---|
| 26 | # arithmetic expansion $(( ... ))
|
|---|
| 27 | # getopts
|
|---|
| 28 |
|
|---|
| 29 | # For some tests we must run a sub-shell; $TESTSHELL says what to use.
|
|---|
| 30 | # If set, TESTSHELL must be an absolute pathname.
|
|---|
| 31 | # For example, on HP-UX 9, /bin/posix/sh is the supposedly-compliant shell.
|
|---|
| 32 | TESTSHELL=${THIS_SH:-$PWD/../bash}
|
|---|
| 33 |
|
|---|
| 34 | # these tests create temp files with names $TMPDIR/conf*
|
|---|
| 35 | : ${TMPDIR:=/tmp}
|
|---|
| 36 |
|
|---|
| 37 | exitval=0
|
|---|
| 38 | numtests=0
|
|---|
| 39 |
|
|---|
| 40 | echo "Testing for POSIX.2 conformance"
|
|---|
| 41 |
|
|---|
| 42 | newtest()
|
|---|
| 43 | {
|
|---|
| 44 | numtests=$(($numtests + 1))
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | testfail()
|
|---|
| 48 | {
|
|---|
| 49 | echo "$1 test failed"
|
|---|
| 50 | exitval=$(($exitval + 1))
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | newtest
|
|---|
| 54 | empty=""
|
|---|
| 55 | test "${empty:-ok}" = ok || testfail "empty var colon"
|
|---|
| 56 | newtest
|
|---|
| 57 | test "${empty-bad}" = "" || testfail "got \"${empty-bad}\": empty var nocolon"
|
|---|
| 58 | newtest
|
|---|
| 59 | test "${unsetvar-ok}" = ok || testfail "unset var"
|
|---|
| 60 | newtest
|
|---|
| 61 | unset empty
|
|---|
| 62 | test "${empty-ok}" = ok || testfail "unset"
|
|---|
| 63 |
|
|---|
| 64 | newtest
|
|---|
| 65 | set -- -Z
|
|---|
| 66 | test "x$1" = x-Z || testfail '\"set -- arg\"'
|
|---|
| 67 | # this should empty the argument list
|
|---|
| 68 | newtest
|
|---|
| 69 | set --
|
|---|
| 70 | test $# = 0 || testfail "still $# args: \"set --\""
|
|---|
| 71 |
|
|---|
| 72 | # IFS parsing:
|
|---|
| 73 | newtest
|
|---|
| 74 | names=one/good/three
|
|---|
| 75 | saved_ifs="$IFS"
|
|---|
| 76 | IFS=/
|
|---|
| 77 | set $names lose
|
|---|
| 78 | test "$2" = good || testfail "got \"$2\": IFS parsing"
|
|---|
| 79 | IFS="$saved_ifs"
|
|---|
| 80 |
|
|---|
| 81 | # "$@" with 0 arguments should expand to 0 arguments
|
|---|
| 82 | newtest
|
|---|
| 83 | cat > $TMPDIR/conftest1 << EOF
|
|---|
| 84 | $TMPDIR/conftest2 "\$@"
|
|---|
| 85 | EOF
|
|---|
| 86 | cat > $TMPDIR/conftest2 << "EOF"
|
|---|
| 87 | #! /bin/sh
|
|---|
| 88 | echo $#
|
|---|
| 89 | EOF
|
|---|
| 90 | chmod +x $TMPDIR/conftest1 $TMPDIR/conftest2
|
|---|
| 91 | numargs=$($TESTSHELL $TMPDIR/conftest1)
|
|---|
| 92 | if [ "$?" != 0 ]; then
|
|---|
| 93 | testfail 'running $@'
|
|---|
| 94 | else
|
|---|
| 95 | test "$numargs" = 0 || testfail '"$@" got '"$numargs args: expansion w 0 args"
|
|---|
| 96 | fi
|
|---|
| 97 | rm -f $TMPDIR/conftest1 $TMPDIR/conftest2
|
|---|
| 98 |
|
|---|
| 99 | newtest
|
|---|
| 100 | val=$("$TESTSHELL" -c 'echo $1' csh good)
|
|---|
| 101 | test "$val" = good || testfail "got \"$val\": sh -c"
|
|---|
| 102 |
|
|---|
| 103 | newtest
|
|---|
| 104 | # do these tests in a sub-shell because failure will exit
|
|---|
| 105 | val=$("$TESTSHELL" -c 'echo ${10}' 0 1 2 3 4 5 6 7 8 9 ten 11 2> /dev/null)
|
|---|
| 106 | test "$val" = ten || testfail "accessing more than 9 positional params"
|
|---|
| 107 |
|
|---|
| 108 | a=abc_def_ghi
|
|---|
| 109 | export a
|
|---|
| 110 | newtest; val=`"$TESTSHELL" -c 'echo "${a%_*}"' 2> /dev/null`
|
|---|
| 111 | test "$val" = abc_def || testfail "parameter % op"
|
|---|
| 112 | newtest; val=`"$TESTSHELL" -c 'echo "${a%%_*}"' 2> /dev/null`
|
|---|
| 113 | test "$val" = abc || testfail "parameter %% op"
|
|---|
| 114 | newtest; val=`"$TESTSHELL" -c 'echo "${a#*_}"' 2> /dev/null`
|
|---|
| 115 | test "$val" = def_ghi || testfail "parameter # op"
|
|---|
| 116 | newtest; val=`"$TESTSHELL" -c 'echo "${a##*_}"' 2> /dev/null`
|
|---|
| 117 | test "$val" = ghi || testfail "parameter ## op"
|
|---|
| 118 |
|
|---|
| 119 | newtest
|
|---|
| 120 | "$TESTSHELL" -c 'export a=value' 2> /dev/null || testfail "export with value"
|
|---|
| 121 |
|
|---|
| 122 | newtest
|
|---|
| 123 | a=5; test "$(( ($a+1)/2 ))" = 3 || testfail "arithmetic expansion"
|
|---|
| 124 |
|
|---|
| 125 | # does "test" support the -x switch?
|
|---|
| 126 | newtest
|
|---|
| 127 | touch $TMPDIR/conftest
|
|---|
| 128 | chmod -x $TMPDIR/conftest
|
|---|
| 129 | test -x $TMPDIR/conftest && testfail "negative test -x"
|
|---|
| 130 | chmod +x $TMPDIR/conftest
|
|---|
| 131 | test -x $TMPDIR/conftest || testfail "positive test -x"
|
|---|
| 132 | rm -f $TMPDIR/conftest
|
|---|
| 133 |
|
|---|
| 134 | newtest
|
|---|
| 135 | test "$OPTIND" = 1 || testfail "OPTIND initial value"
|
|---|
| 136 |
|
|---|
| 137 | newtest
|
|---|
| 138 | getopts a: store -a aoptval
|
|---|
| 139 | if [ "$OPTIND" != 3 ] || [ "$store" != a ] || [ "$OPTARG" != aoptval ]; then
|
|---|
| 140 | testfail "getopts"
|
|---|
| 141 | fi
|
|---|
| 142 |
|
|---|
| 143 | # if I change the default quoting style for variable values, these
|
|---|
| 144 | # next four must change
|
|---|
| 145 |
|
|---|
| 146 | newtest
|
|---|
| 147 | SQUOTE="'"
|
|---|
| 148 | val1=$(set | sed -n 's:^SQUOTE=::p')
|
|---|
| 149 | if [ "$val1" != "''\\'''" ]; then
|
|---|
| 150 | testfail "variable quoting 1"
|
|---|
| 151 | fi
|
|---|
| 152 |
|
|---|
| 153 | newtest
|
|---|
| 154 | VTILDE='~'
|
|---|
| 155 | val1=$(set | sed -n 's:^VTILDE=::p')
|
|---|
| 156 | if [ "$val1" != "'~'" ]; then
|
|---|
| 157 | testfail "variable quoting 2"
|
|---|
| 158 | fi
|
|---|
| 159 |
|
|---|
| 160 | newtest
|
|---|
| 161 | VHASH=ab#cd
|
|---|
| 162 | val1=$(set | sed -n 's:^VHASH=::p')
|
|---|
| 163 | if [ "$val1" != "ab#cd" ]; then
|
|---|
| 164 | testfail "variable quoting 3"
|
|---|
| 165 | fi
|
|---|
| 166 |
|
|---|
| 167 | newtest
|
|---|
| 168 | VHASH2=#abcd
|
|---|
| 169 | val1=$(set | sed -n 's:^VHASH2=::p')
|
|---|
| 170 | if [ "$val1" != "'#abcd'" ]; then
|
|---|
| 171 | testfail "variable quoting 4"
|
|---|
| 172 | fi
|
|---|
| 173 |
|
|---|
| 174 | if [ $exitval = 0 ]; then
|
|---|
| 175 | echo "All tests passed"
|
|---|
| 176 | else
|
|---|
| 177 | echo "$exitval of $numtests tests failed"
|
|---|
| 178 | fi
|
|---|
| 179 | exit $exitval
|
|---|