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