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