1#!/bin/bash 2 3[ -f testing.sh ] && . testing.sh 4 5#testing "name" "command" "result" "infile" "stdin" 6 7testcmd "-- isn't parsed" "-- == -- && echo yes" "yes\n" "" "" 8 9# Number and position of args is important 10testcmd 'no args is false' '; echo $?' '1\n' '' '' 11testcmd 'empty string is false' '""; echo $?' '1\n' '' '' 12testcmd '1 arg is true if not empty string' '== ; echo $?' '0\n' '' '' 13testcmd "1 arg isn't an operand" '-t 2>&1; echo $?' '0\n' '' '' 14testcmd '2 args' '-e == ; echo $?' '1\n' '' '' 15testcmd '3 args' '-e == -e ; echo $?' '0\n' '' '' 16 17# parse as operator before parsing as parentheses around one argument 18testcmd '' '\( == \) ; echo $?' '1\n' '' '' 19testcmd '' '\( == \( ; echo $?' '0\n' '' '' 20testcmd '' '\( "" \) ; echo $?' '1\n' '' '' 21testcmd '' '\( x \) ; echo $?' '0\n' '' '' 22 23# TODO: Should also have device and socket files, but requires root 24 25mkdir d 26touch f 27ln -s /dev/null L 28echo nonempty > s 29mkfifo p 30 31type_test() 32{ 33 for i in d f L s p n 34 do 35 "$C" $* $i && echo -n $i 36 done 37} 38 39testing "-b" "type_test -b" "" "" "" 40testing "-c" "type_test -c" "L" "" "" 41testing "-d" "type_test -d" "d" "" "" 42testing "-e" "type_test -e" "dfLsp" "" "" 43testing "-f" "type_test -f" "fs" "" "" 44testing "-h" "type_test -h" "L" "" "" 45testing "-L" "type_test -L" "L" "" "" 46testing "-p" "type_test -p" "p" "" "" 47testing "-S" "type_test -S" "" "" "" 48testing "-s" "type_test -s" "ds" "" "" 49testing "! -e" 'type_test ! -e' "n" "" "" 50 51rm f L s p 52rmdir d 53 54# Alas can't expand to a redirect, so just test one success/fail 55testcmd "-t" '-t 0 < /dev/null; echo $?' '1\n' '' '' 56testcmd "-t2" '-t 0 < /dev/ptmx; echo $?' '0\n' '' '' 57 58# test -rwx each bit position and failure 59touch walrus 60MASK=111 61for i in x w r k g u; do 62 [ $i == k ] && MASK=1000 63 XX=no 64 [ $(id -u) -eq 0 ] && [ $i == r -o $i == w ] && XX=yes # Root always has access 65 # test everything off produces "off" 66 chmod 000 walrus 67 testcmd "-$i 0" "-$i walrus && echo yes || echo no" "$XX\n" "" "" 68 chmod $((7777-$MASK)) walrus 69 testcmd "-$i inverted" "-$i walrus && echo yes || echo no" "$XX\n" "" "" 70 MASK=$(($MASK<<1)) 71done 72unset MASK 73# Test setuid setgid sticky enabled 74for i in uu+s gg+s k+t; do 75 chmod 000 walrus 76 chmod ${i:1}+s walrus 77 testcmd "-${i:0:1}" "-${i:0:1} walrus && echo yes" "yes\n" "" "" 78done 79# test each ugo+rwx bit position individually 80XX=no 81# Note: chmod 007 means everybody EXCEPT owner/group can access it. (Unix!) 82[ $(id -u) -eq 0 ] && XX=yes # Root always has access 83for i in 1 10 100; do for j in x w r; do 84 chmod $i walrus 85 86 [ $i == 100 ] && XX=yes 87 testcmd "-$j $i" "-$j walrus && echo yes || echo no" "$XX\n" "" "" 88 i=$((i<<1)) 89done; done 90rm -f walrus 91 92# Not zero length, zero length, equals, not equals 93testcmd "-n" "-n '' || echo yes" "yes\n" "" "" 94testcmd "-n2" "-n a && echo yes" "yes\n" "" "" 95testcmd "-z" "-z '' && echo yes" "yes\n" "" "" 96testcmd "-z2" "-z a || echo yes" "yes\n" "" "" 97testcmd "" "a = b || echo yes" "yes\n" "" "" 98testcmd "" "'' = '' && echo yes" "yes\n" "" "" 99testcmd "a != b" "a != b && echo yes" "yes\n" "" "" 100testcmd "a != b" "a != a || echo yes" "yes\n" "" "" 101 102arith_test() 103{ 104 $C -1 $1 1 && echo -n l 105 $C 0 $1 0 && echo -n e 106 $C -3 $1 -5 && echo -n g 107} 108 109testing "-eq" "arith_test -eq" "e" "" "" 110testing "-ne" "arith_test -ne" "lg" "" "" 111testing "-gt" "arith_test -gt" "g" "" "" 112testing "-ge" "arith_test -ge" "eg" "" "" 113testing "-lt" "arith_test -lt" "l" "" "" 114testing "-le" "arith_test -le" "le" "" "" 115 116testing "positional" "test -a == -a && echo yes" "yes\n" "" "" 117testing "! stacks" 'test \! \! \! \! 2 -eq 2 && echo yes' "yes\n" "" "" 118 119# bash builtin "test" has these, but /usr/bin/test does not. 120testing "<1" 'test abc \< def && echo yes' "yes\n" "" "" 121testing "<2" 'test def \< abc || echo yes' "yes\n" "" "" 122testing ">1" 'test abc \> def || echo yes' "yes\n" "" "" 123testing ">2" 'test def \> abc && echo yes' "yes\n" "" "" 124# bash only has this for [[ ]] but extra tests to _exclude_ silly... 125toyonly testcmd "=~" 'abc =~ a.c && echo yes' "yes\n" "" "" 126toyonly testcmd "=~ fail" 'abc =~ d.c; echo $?' '1\n' "" "" 127toyonly testcmd "=~ zero length match" 'abc =~ "1*" && echo yes' 'yes\n' '' '' 128 129# test ! = -o a 130# test ! \( = -o a \) 131# test \( ! = \) -o a 132# test \( \) 133 134 135# -e == -a 136# -e == -a -o -d != -o 137# \( "x" \) -a \) == \) 138# \( ! ! ! -e \) \) 139 140# // () -a (() -a () -o ()) -o () 141# // x -a ( x -o x ) -a x 142# // x -o ( x -a x ) -a x -o x 143 144# trailing ! and ( 145# test \( ! ! ! -e \) \) 146# test \( \( "" \) -a "" \) -a "" 147# test ! 148# test \( \) == \) \) -a x 149 150# test \( "" \) -a \) == \) 151# test \( "x" \) -a \) == \) 152# test -e == -a 153# test \( "" \) 154