1 # Simple test harness infrastructurei 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 "optionflag"
9 # The following environment variables may be set to enable optional behavior
11 # VERBOSE - Print the diff -u of each failed test case.
12 # DEBUG - Enable command tracing.
13 # SKIP - do not perform this test (this is set by "optionflag")
15 # The "testing" function takes five arguments:
16 # $1) Description to display when running command
17 # $2) Command line arguments to command
18 # $3) Expected result (on stdout)
19 # $4) Data written to file "input"
20 # $5) Data written to stdin
22 # The exit value of testing is the exit value of the command it ran.
24 # The environment variable "FAILCOUNT" contains a cumulative total of the
25 # number of failed tests.
27 # The "optional" function is used to skip certain tests, ala:
28 # optionflag CONFIG_FEATURE_THINGY
30 # The "optional" function checks the environment variable "OPTIONFLAGS",
31 # which is either empty (in which case it always clears SKIP) or
32 # else contains a colon-separated list of features (in which case the function
33 # clears SKIP if the flag was found, or sets it to 1 if the flag was not found).
42 option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"`
44 if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ]
52 # The testing function
57 [ -z "$1" ] && NAME=$2
62 echo "Test $NAME has the wrong number of arguments ($# $*)" >&2
66 [ -n "$DEBUG" ] && set -x
74 echo -ne "$3" > expected
76 [ -z "$VERBOSE" ] || echo "echo '$5' | $2"
77 echo -ne "$5" | eval "$2" > actual
80 cmp expected actual > /dev/null || ret=$?
83 FAILCOUNT=$[$FAILCOUNT+1]
87 diff -u expected actual || /bin/true
92 rm -f input expected actual
94 [ -n "$DEBUG" ] && set +x
99 # Recursively grab an executable and all the libraries needed to run it.
100 # Source paths beginning with / will be copied into destpath, otherwise
101 # the file is assumed to already be there and only its library dependencies
106 [ $# -lt 2 ] && return
114 [ "${i:0:1}" == "/" ] || i=$(which $i)
115 [ -f "$dest/$i" ] && continue
118 d=`echo "$i" | grep -o '.*/'` &&
119 mkdir -p "$dest/$d" &&
120 cat "$i" > "$dest/$i" &&
125 mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
129 # Set up a chroot environment and run commands within it.
130 # Needed commands listed on command line
131 # Script fed to stdin.
136 mount -t ramfs tmpdir4chroot tmpdir4chroot
137 mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
138 cp -L testing.sh tmpdir4chroot
140 # Copy utilities from command line arguments
142 echo -n "Setup chroot"
143 mkchroot tmpdir4chroot $*
146 mknod tmpdir4chroot/dev/tty c 5 0
147 mknod tmpdir4chroot/dev/null c 1 3
148 mknod tmpdir4chroot/dev/zero c 1 5
150 # Copy script from stdin
152 cat > tmpdir4chroot/test.sh
153 chmod +x tmpdir4chroot/test.sh
154 chroot tmpdir4chroot /test.sh
155 umount -l tmpdir4chroot