2010-08-13 13:50:26 +00:00
|
|
|
# Simple test harness infrastructure for BusyBox
|
2005-09-02 00:41:53 +00:00
|
|
|
#
|
|
|
|
# Copyright 2005 by Rob Landley
|
|
|
|
#
|
|
|
|
# License is GPLv2, see LICENSE in the busybox tarball for full license text.
|
|
|
|
|
2008-05-02 21:46:30 +00:00
|
|
|
# This file defines two functions, "testing" and "optional"
|
|
|
|
# and a couple more...
|
2005-11-07 08:50:53 +00:00
|
|
|
|
|
|
|
# The following environment variables may be set to enable optional behavior
|
|
|
|
# in "testing":
|
|
|
|
# VERBOSE - Print the diff -u of each failed test case.
|
|
|
|
# DEBUG - Enable command tracing.
|
2008-05-02 21:46:30 +00:00
|
|
|
# SKIP - do not perform this test (this is set by "optional")
|
2005-09-02 00:41:53 +00:00
|
|
|
#
|
2005-11-07 08:50:53 +00:00
|
|
|
# The "testing" function takes five arguments:
|
2008-05-02 21:46:30 +00:00
|
|
|
# $1) Test description
|
|
|
|
# $2) Command(s) to run. May have pipes, redirects, etc
|
|
|
|
# $3) Expected result on stdout
|
|
|
|
# $4) Data to be written to file "input"
|
|
|
|
# $5) Data to be written to stdin
|
2005-09-02 00:41:53 +00:00
|
|
|
#
|
2008-05-02 21:46:30 +00:00
|
|
|
# The exit value of testing is the exit value of $2 it ran.
|
2005-09-02 00:41:53 +00:00
|
|
|
#
|
|
|
|
# The environment variable "FAILCOUNT" contains a cumulative total of the
|
2005-11-07 08:50:53 +00:00
|
|
|
# number of failed tests.
|
2005-09-02 00:41:53 +00:00
|
|
|
|
2005-11-07 08:50:53 +00:00
|
|
|
# The "optional" function is used to skip certain tests, ala:
|
2010-07-29 07:05:30 +00:00
|
|
|
# optional FEATURE_THINGY
|
2005-11-07 08:50:53 +00:00
|
|
|
#
|
|
|
|
# The "optional" function checks the environment variable "OPTIONFLAGS",
|
|
|
|
# which is either empty (in which case it always clears SKIP) or
|
|
|
|
# else contains a colon-separated list of features (in which case the function
|
|
|
|
# clears SKIP if the flag was found, or sets it to 1 if the flag was not found).
|
2005-09-02 00:41:53 +00:00
|
|
|
|
|
|
|
export FAILCOUNT=0
|
2005-11-07 08:50:53 +00:00
|
|
|
export SKIP=
|
2005-09-02 00:41:53 +00:00
|
|
|
|
2009-07-18 14:22:26 +00:00
|
|
|
# Helper for helpers. Oh my...
|
|
|
|
|
|
|
|
test x"$ECHO" != x"" || {
|
|
|
|
ECHO="echo"
|
|
|
|
test x"`echo -ne`" = x"" || {
|
|
|
|
# Compile and use a replacement 'echo' which understands -e -n
|
|
|
|
ECHO="$PWD/echo-ne"
|
|
|
|
test -x "$ECHO" || {
|
|
|
|
gcc -Os -o "$ECHO" ../scripts/echo.c || exit 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export ECHO
|
|
|
|
}
|
|
|
|
|
2005-09-23 15:44:46 +00:00
|
|
|
# Helper functions
|
|
|
|
|
2006-06-15 20:07:57 +00:00
|
|
|
optional()
|
2005-09-23 15:44:46 +00:00
|
|
|
{
|
2010-05-10 09:00:11 +00:00
|
|
|
SKIP=
|
|
|
|
while test "$1"; do
|
|
|
|
if test x"${OPTIONFLAGS/*:$1:*/y}" != x"y"; then
|
|
|
|
SKIP=1
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
done
|
2005-09-23 15:44:46 +00:00
|
|
|
}
|
|
|
|
|
2005-09-02 00:41:53 +00:00
|
|
|
# The testing function
|
|
|
|
|
2007-06-05 22:29:14 +00:00
|
|
|
testing()
|
2005-09-02 00:41:53 +00:00
|
|
|
{
|
2006-03-16 15:20:45 +00:00
|
|
|
NAME="$1"
|
2008-05-15 22:43:48 +00:00
|
|
|
[ -n "$1" ] || NAME="$2"
|
2006-03-16 15:20:45 +00:00
|
|
|
|
2005-09-02 00:41:53 +00:00
|
|
|
if [ $# -ne 5 ]
|
|
|
|
then
|
2010-05-11 23:49:04 +00:00
|
|
|
echo "Test $NAME has wrong number of arguments: $# (must be 5)" >&2
|
2008-05-15 22:43:48 +00:00
|
|
|
exit 1
|
2005-09-02 00:41:53 +00:00
|
|
|
fi
|
|
|
|
|
2008-05-15 22:43:48 +00:00
|
|
|
[ -z "$DEBUG" ] || set -x
|
2005-09-24 00:52:58 +00:00
|
|
|
|
2005-11-07 08:50:53 +00:00
|
|
|
if [ -n "$SKIP" ]
|
2005-09-23 15:44:46 +00:00
|
|
|
then
|
2006-03-16 15:20:45 +00:00
|
|
|
echo "SKIPPED: $NAME"
|
2005-11-07 08:50:53 +00:00
|
|
|
return 0
|
2005-09-23 15:44:46 +00:00
|
|
|
fi
|
|
|
|
|
2008-05-02 21:46:30 +00:00
|
|
|
$ECHO -ne "$3" > expected
|
|
|
|
$ECHO -ne "$4" > input
|
2010-11-28 00:41:40 +00:00
|
|
|
[ -z "$VERBOSE" ] || echo ======================
|
2010-08-13 13:50:26 +00:00
|
|
|
[ -z "$VERBOSE" ] || echo "echo -ne '$4' >input"
|
2009-07-18 14:22:26 +00:00
|
|
|
[ -z "$VERBOSE" ] || echo "echo -ne '$5' | $2"
|
2008-05-02 21:46:30 +00:00
|
|
|
$ECHO -ne "$5" | eval "$2" > actual
|
2005-09-02 00:41:53 +00:00
|
|
|
RETVAL=$?
|
|
|
|
|
2008-05-15 22:43:48 +00:00
|
|
|
if cmp expected actual >/dev/null 2>/dev/null
|
2005-09-02 00:41:53 +00:00
|
|
|
then
|
2008-05-15 22:43:48 +00:00
|
|
|
echo "PASS: $NAME"
|
|
|
|
else
|
2008-05-02 09:39:09 +00:00
|
|
|
FAILCOUNT=$(($FAILCOUNT + 1))
|
2006-03-16 15:20:45 +00:00
|
|
|
echo "FAIL: $NAME"
|
2008-05-15 22:43:48 +00:00
|
|
|
[ -z "$VERBOSE" ] || diff -u expected actual
|
2005-09-02 00:41:53 +00:00
|
|
|
fi
|
|
|
|
rm -f input expected actual
|
|
|
|
|
2008-05-15 22:43:48 +00:00
|
|
|
[ -z "$DEBUG" ] || set +x
|
2005-09-24 00:52:58 +00:00
|
|
|
|
2005-09-02 00:41:53 +00:00
|
|
|
return $RETVAL
|
|
|
|
}
|
2006-03-09 22:04:33 +00:00
|
|
|
|
|
|
|
# Recursively grab an executable and all the libraries needed to run it.
|
|
|
|
# Source paths beginning with / will be copied into destpath, otherwise
|
|
|
|
# the file is assumed to already be there and only its library dependencies
|
|
|
|
# are copied.
|
|
|
|
|
2007-06-05 22:29:14 +00:00
|
|
|
mkchroot()
|
2006-03-09 22:04:33 +00:00
|
|
|
{
|
|
|
|
[ $# -lt 2 ] && return
|
|
|
|
|
2008-05-02 21:46:30 +00:00
|
|
|
$ECHO -n .
|
2006-03-16 15:20:45 +00:00
|
|
|
|
2006-03-09 22:04:33 +00:00
|
|
|
dest=$1
|
|
|
|
shift
|
|
|
|
for i in "$@"
|
|
|
|
do
|
2008-05-02 09:39:09 +00:00
|
|
|
#bashism: [ "${i:0:1}" == "/" ] || i=$(which $i)
|
|
|
|
i=$(which $i) # no-op for /bin/prog
|
2006-03-18 03:01:57 +00:00
|
|
|
[ -f "$dest/$i" ] && continue
|
|
|
|
if [ -e "$i" ]
|
2006-03-09 22:04:33 +00:00
|
|
|
then
|
|
|
|
d=`echo "$i" | grep -o '.*/'` &&
|
|
|
|
mkdir -p "$dest/$d" &&
|
|
|
|
cat "$i" > "$dest/$i" &&
|
|
|
|
chmod +x "$dest/$i"
|
2006-03-18 03:01:57 +00:00
|
|
|
else
|
|
|
|
echo "Not found: $i"
|
2006-03-09 22:04:33 +00:00
|
|
|
fi
|
|
|
|
mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
|
|
|
|
done
|
|
|
|
}
|
2006-03-16 15:20:45 +00:00
|
|
|
|
|
|
|
# Set up a chroot environment and run commands within it.
|
|
|
|
# Needed commands listed on command line
|
|
|
|
# Script fed to stdin.
|
|
|
|
|
2007-06-05 22:29:14 +00:00
|
|
|
dochroot()
|
2006-03-16 15:20:45 +00:00
|
|
|
{
|
|
|
|
mkdir tmpdir4chroot
|
|
|
|
mount -t ramfs tmpdir4chroot tmpdir4chroot
|
|
|
|
mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
|
|
|
|
cp -L testing.sh tmpdir4chroot
|
|
|
|
|
|
|
|
# Copy utilities from command line arguments
|
|
|
|
|
2008-05-02 21:46:30 +00:00
|
|
|
$ECHO -n "Setup chroot"
|
2006-03-16 15:20:45 +00:00
|
|
|
mkchroot tmpdir4chroot $*
|
|
|
|
echo
|
|
|
|
|
|
|
|
mknod tmpdir4chroot/dev/tty c 5 0
|
|
|
|
mknod tmpdir4chroot/dev/null c 1 3
|
|
|
|
mknod tmpdir4chroot/dev/zero c 1 5
|
|
|
|
|
|
|
|
# Copy script from stdin
|
|
|
|
|
|
|
|
cat > tmpdir4chroot/test.sh
|
|
|
|
chmod +x tmpdir4chroot/test.sh
|
|
|
|
chroot tmpdir4chroot /test.sh
|
|
|
|
umount -l tmpdir4chroot
|
|
|
|
rmdir tmpdir4chroot
|
|
|
|
}
|