remove bogus "
[oweals/busybox.git] / testsuite / testing.sh
1 # Simple test harness infrastructurei for BusyBox
2 #
3 # Copyright 2005 by Rob Landley
4 #
5 # License is GPLv2, see LICENSE in the busybox tarball for full license text.
6
7 # This file defines two functions, "testing" and "optionflag"
8
9 # The "testing" function must have the following environment variable set:
10 #    COMMAND = command to execute
11 #
12 # The following environment variables may be set to enable optional behavior
13 # in "testing":
14 #    VERBOSE - Print the diff -u of each failed test case.
15 #    DEBUG - Enable command tracing.
16 #    SKIP - do not perform this test (this is set by "optionflag")
17 #
18 # The "testing" function takes five arguments:
19 #       $1) Description to display when running command
20 #       $2) Command line arguments to command
21 #       $3) Expected result (on stdout)
22 #       $4) Data written to file "input"
23 #       $5) Data written to stdin
24 #
25 # The exit value of testing is the exit value of the command it ran.
26 #
27 # The environment variable "FAILCOUNT" contains a cumulative total of the
28 # number of failed tests.
29
30 # The "optional" function is used to skip certain tests, ala:
31 #   optionflag CONFIG_FEATURE_THINGY
32 #
33 # The "optional" function checks the environment variable "OPTIONFLAGS",
34 # which is either empty (in which case it always clears SKIP) or
35 # else contains a colon-separated list of features (in which case the function
36 # clears SKIP if the flag was found, or sets it to 1 if the flag was not found).
37
38 export FAILCOUNT=0
39 export SKIP=
40
41 # Helper functions
42
43 optional()
44 {
45   option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"`
46   # Not set?
47   if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ]
48   then
49     SKIP=""
50     return
51   fi
52   SKIP=1
53 }
54
55 # The testing function
56
57 testing ()
58 {
59   NAME="$1"
60   [ -z "$1" ] && NAME=$2
61
62   if [ $# -ne 5 ]
63   then
64     echo "Test $NAME has the wrong number of arguments ($# $*)" >&2
65     exit
66   fi
67
68   [ -n "$DEBUG" ] && set -x
69
70   if [ -n "$SKIP" ]
71   then
72     echo "SKIPPED: $NAME"
73     return 0
74   fi
75
76   echo -ne "$3" > expected
77   echo -ne "$4" > input
78   [ -z "$VERBOSE" ] || echo "echo '$5' | $COMMAND $2"
79   echo -ne "$5" | eval "$2" > actual
80   RETVAL=$?
81
82   cmp expected actual > /dev/null
83   if [ $? -ne 0 ]
84   then
85     FAILCOUNT=$[$FAILCOUNT+1]
86     echo "FAIL: $NAME"
87     [ -n "$VERBOSE" ] && diff -u expected actual
88   else
89     echo "PASS: $NAME"
90   fi
91   rm -f input expected actual
92
93   [ -n "$DEBUG" ] && set +x
94
95   return $RETVAL
96 }
97
98 # Recursively grab an executable and all the libraries needed to run it.
99 # Source paths beginning with / will be copied into destpath, otherwise
100 # the file is assumed to already be there and only its library dependencies
101 # are copied.
102
103 function mkchroot
104 {
105   [ $# -lt 2 ] && return
106
107   echo -n .
108
109   dest=$1
110   shift
111   for i in "$@"
112   do
113     [ "${i:0:1}" == "/" ] || i=$(which $i)
114     [ -f "$dest/$i" ] && continue
115     if [ -e "$i" ]
116     then
117       d=`echo "$i" | grep -o '.*/'` &&
118       mkdir -p "$dest/$d" &&
119       cat "$i" > "$dest/$i" &&
120       chmod +x "$dest/$i"
121     else
122       echo "Not found: $i"
123     fi
124     mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
125   done
126 }
127
128 # Set up a chroot environment and run commands within it.
129 # Needed commands listed on command line
130 # Script fed to stdin.
131
132 function dochroot
133 {
134   mkdir tmpdir4chroot
135   mount -t ramfs tmpdir4chroot tmpdir4chroot
136   mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
137   cp -L testing.sh tmpdir4chroot
138
139   # Copy utilities from command line arguments
140
141   echo -n "Setup chroot"
142   mkchroot tmpdir4chroot $*
143   echo
144
145   mknod tmpdir4chroot/dev/tty c 5 0
146   mknod tmpdir4chroot/dev/null c 1 3
147   mknod tmpdir4chroot/dev/zero c 1 5
148
149   # Copy script from stdin
150
151   cat > tmpdir4chroot/test.sh
152   chmod +x tmpdir4chroot/test.sh
153   chroot tmpdir4chroot /test.sh
154   umount -l tmpdir4chroot
155   rmdir tmpdir4chroot
156 }
157