test.test 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/bin/bash
  2. [ -f testing.sh ] && . testing.sh
  3. #testing "name" "command" "result" "infile" "stdin"
  4. testcmd '0 args' '; echo $?' '1\n' '' ''
  5. testcmd '1 arg' '== ; echo $?' '0\n' '' ''
  6. testcmd '2 args' '-e == ; echo $?' '1\n' '' ''
  7. testcmd '3 args' '-e == -e ; echo $?' '0\n' '' ''
  8. testcmd '' '\( == \) ; echo $?' '1\n' '' ''
  9. testcmd '' '\( == \( ; echo $?' '0\n' '' ''
  10. # TODO: Should also have device and socket files
  11. mkdir d
  12. touch f
  13. ln -s /dev/null L
  14. echo nonempty > s
  15. mkfifo p
  16. type_test()
  17. {
  18. for i in d f L s p n
  19. do
  20. "$C" $* $i && echo -n $i
  21. done
  22. }
  23. testing "-b" "type_test -b" "" "" ""
  24. testing "-c" "type_test -c" "L" "" ""
  25. testing "-d" "type_test -d" "d" "" ""
  26. testing "-f" "type_test -f" "fs" "" ""
  27. testing "-h" "type_test -h" "L" "" ""
  28. testing "-L" "type_test -L" "L" "" ""
  29. testing "-s" "type_test -s" "ds" "" ""
  30. testing "-S" "type_test -S" "" "" ""
  31. testing "-p" "type_test -p" "p" "" ""
  32. testing "-e" "type_test -e" "dfLsp" "" ""
  33. testing "! -e" 'type_test ! -e' "n" "" ""
  34. rm f L s p
  35. rmdir d
  36. # test -rwx each bit position and failure
  37. touch walrus
  38. MASK=111
  39. for i in x w r k g u; do
  40. [ $i == k ] && MASK=1000
  41. # test everything off produces "off"
  42. chmod 000 walrus
  43. testcmd "-$i 0" "-$i walrus || echo yes" "yes\n" "" ""
  44. chmod $((7777-$MASK)) walrus
  45. testcmd "-$i inverted" "-$i walrus || echo yes" "yes\n" "" ""
  46. MASK=$(($MASK<<1))
  47. done
  48. unset MASK
  49. # Test setuid setgid sticky enabled
  50. for i in uu+s gg+s k+t; do
  51. chmod 000 walrus
  52. chmod ${i:1}+s walrus
  53. testcmd "-${i:0:1}" "-${i:0:1} walrus && echo yes" "yes\n" "" ""
  54. done
  55. # test each ugo+rwx bit position individually
  56. for i in 1 10 100; do for j in x w r; do
  57. chmod $i walrus
  58. testcmd "-$j $i" "-$j walrus && echo yes" "yes\n" "" ""
  59. i=$((i<<1))
  60. done; done
  61. rm -f walrus
  62. testcmd "" "'' || echo yes" "yes\n" "" ""
  63. testcmd "" "a && echo yes" "yes\n" "" ""
  64. testcmd "-n" "-n '' || echo yes" "yes\n" "" ""
  65. testcmd "-n2" "-n a && echo yes" "yes\n" "" ""
  66. testcmd "-z" "-z '' && echo yes" "yes\n" "" ""
  67. testcmd "-z2" "-z a || echo yes" "yes\n" "" ""
  68. testcmd "" "a = b || echo yes" "yes\n" "" ""
  69. testcmd "" "'' = '' && echo yes" "yes\n" "" ""
  70. testcmd "a != b" "a != b && echo yes" "yes\n" "" ""
  71. testcmd "a != b" "a != a || echo yes" "yes\n" "" ""
  72. arith_test()
  73. {
  74. $C -1 $1 1 && echo -n l
  75. $C 0 $1 0 && echo -n e
  76. $C -3 $1 -5 && echo -n g
  77. }
  78. testing "-eq" "arith_test -eq" "e" "" ""
  79. testing "-ne" "arith_test -ne" "lg" "" ""
  80. testing "-gt" "arith_test -gt" "g" "" ""
  81. testing "-ge" "arith_test -ge" "eg" "" ""
  82. testing "-lt" "arith_test -lt" "l" "" ""
  83. testing "-le" "arith_test -le" "le" "" ""
  84. # test ! = -o a
  85. # test ! \( = -o a \)
  86. # test \( ! = \) -o a
  87. # test \( \)
  88. #testing "" "[ -a -eq -a ] && echo yes" "yes\n" "" ""
  89. # -e == -a
  90. # -e == -a -o -d != -o
  91. # \( "x" \) -a \) == \)
  92. # \( ! ! ! -e \) \)
  93. # // () -a (() -a () -o ()) -o ()
  94. # // x -a ( x -o x ) -a x
  95. # // x -o ( x -a x ) -a x -o x
  96. # trailing ! and (