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 "testing" function must have the following environment variable set:
10 # COMMAND = command to execute
12 # The following environment variables may be set to enable optional behavior
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")
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
25 # The exit value of testing is the exit value of the command it ran.
27 # The environment variable "FAILCOUNT" contains a cumulative total of the
28 # number of failed tests.
30 # The "optional" function is used to skip certain tests, ala:
31 # optionflag CONFIG_FEATURE_THINGY
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).
45 option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"`
47 if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ]
55 # The testing function
60 [ -z "$1" ] && NAME=$2
64 echo "Test $NAME has the wrong number of arguments ($# $*)" >&2
68 [ -n "$DEBUG" ] && set -x
76 echo -ne "$3" > expected
78 [ -z "$VERBOSE" ] || echo "echo '$5' | $COMMAND $2"
79 echo -ne "$5" | eval "$2" > actual
82 cmp expected actual > /dev/null
85 FAILCOUNT=$[$FAILCOUNT+1]
87 [ -n "$VERBOSE" ] && diff -u expected actual
91 rm -f input expected actual
93 [ -n "$DEBUG" ] && set +x
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
105 [ $# -lt 2 ] && return
113 [ "${i:0:1}" == "/" ] || i=$(which $i)
114 [ -f "$dest/$i" ] && continue
117 d=`echo "$i" | grep -o '.*/'` &&
118 mkdir -p "$dest/$d" &&
119 cat "$i" > "$dest/$i" &&
124 mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
128 # Set up a chroot environment and run commands within it.
129 # Needed commands listed on command line
130 # Script fed to stdin.
135 mount -t ramfs tmpdir4chroot tmpdir4chroot
136 mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
137 cp -L testing.sh tmpdir4chroot
139 # Copy utilities from command line arguments
141 echo -n "Setup chroot"
142 mkchroot tmpdir4chroot $*
145 mknod tmpdir4chroot/dev/tty c 5 0
146 mknod tmpdir4chroot/dev/null c 1 3
147 mknod tmpdir4chroot/dev/zero c 1 5
149 # Copy script from stdin
151 cat > tmpdir4chroot/test.sh
152 chmod +x tmpdir4chroot/test.sh
153 chroot tmpdir4chroot /test.sh
154 umount -l tmpdir4chroot