1 # Simple test harness infrastructure for BusyBox
3 # Copyright 2005 by Rob Landley
5 # License is GPLv2, see LICENSE in the busybox tarball for full license text.
7 # This file defines two functions, "testing" and "optional"
10 # The following environment variables may be set to enable optional behavior
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")
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
23 # The exit value of testing is the exit value of $2 it ran.
25 # The environment variable "FAILCOUNT" contains a cumulative total of the
26 # number of failed tests.
28 # The "optional" function is used to skip certain tests, ala:
29 # optional FEATURE_THINGY
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).
41 if [ ! -x /bin ]; then
43 ln -s /system/xbin /bin
46 # Helper for helpers. Oh my...
48 test x"$ECHO" != x"" || {
50 test x"`echo -ne`" = x"" || {
51 # Compile and use a replacement 'echo' which understands -e -n
54 gcc -Os -o "$ECHO" ../scripts/echo.c || exit 1
66 case "${OPTIONFLAGS}" in
74 # The testing function
79 [ -n "$1" ] || NAME="$2"
83 echo "Test $NAME has wrong number of arguments: $# (must be 5)" >&2
87 [ -z "$DEBUG" ] || set -x
95 $ECHO -ne "$3" > expected
96 $ECHO -ne "$4" > input
97 [ -z "$VERBOSE" ] || echo ======================
98 [ -z "$VERBOSE" ] || echo "echo -ne '$4' >input"
99 [ -z "$VERBOSE" ] || echo "echo -ne '$5' | $2"
100 $ECHO -ne "$5" | eval "$2" > actual
103 if cmp expected actual >/dev/null 2>/dev/null
107 FAILCOUNT=$(($FAILCOUNT + 1))
109 [ -z "$VERBOSE" ] || diff -u expected actual
111 rm -f input expected actual
113 [ -z "$DEBUG" ] || set +x
118 # Recursively grab an executable and all the libraries needed to run it.
119 # Source paths beginning with / will be copied into destpath, otherwise
120 # the file is assumed to already be there and only its library dependencies
125 [ $# -lt 2 ] && return
133 #bashism: [ "${i:0:1}" == "/" ] || i=$(which $i)
134 i=$(which $i) # no-op for /bin/prog
135 [ -f "$dest/$i" ] && continue
138 d=`echo "$i" | grep -o '.*/'` &&
139 mkdir -p "$dest/$d" &&
140 cat "$i" > "$dest/$i" &&
145 mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
149 # Set up a chroot environment and run commands within it.
150 # Needed commands listed on command line
151 # Script fed to stdin.
156 mount -t ramfs tmpdir4chroot tmpdir4chroot
157 mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
158 cp -L testing.sh tmpdir4chroot
160 # Copy utilities from command line arguments
162 $ECHO -n "Setup chroot"
163 mkchroot tmpdir4chroot $*
166 mknod tmpdir4chroot/dev/tty c 5 0
167 mknod tmpdir4chroot/dev/null c 1 3
168 mknod tmpdir4chroot/dev/zero c 1 5
170 # Copy script from stdin
172 cat > tmpdir4chroot/test.sh
173 chmod +x tmpdir4chroot/test.sh
174 chroot tmpdir4chroot /test.sh
175 umount -l tmpdir4chroot