2005-09-02 06:11:53 +05:30
|
|
|
# Simple test harness infrastructurei for BusyBox
|
|
|
|
#
|
|
|
|
# Copyright 2005 by Rob Landley
|
|
|
|
#
|
|
|
|
# License is GPLv2, see LICENSE in the busybox tarball for full license text.
|
|
|
|
|
2008-05-03 03:16:30 +05:30
|
|
|
# This file defines two functions, "testing" and "optional"
|
|
|
|
# and a couple more...
|
2005-11-07 14:20:53 +05:30
|
|
|
|
|
|
|
# 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-03 03:16:30 +05:30
|
|
|
# SKIP - do not perform this test (this is set by "optional")
|
2005-09-02 06:11:53 +05:30
|
|
|
#
|
2005-11-07 14:20:53 +05:30
|
|
|
# The "testing" function takes five arguments:
|
2008-05-03 03:16:30 +05:30
|
|
|
# $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 06:11:53 +05:30
|
|
|
#
|
2008-05-03 03:16:30 +05:30
|
|
|
# The exit value of testing is the exit value of $2 it ran.
|
2005-09-02 06:11:53 +05:30
|
|
|
#
|
|
|
|
# The environment variable "FAILCOUNT" contains a cumulative total of the
|
2005-11-07 14:20:53 +05:30
|
|
|
# number of failed tests.
|
2005-09-02 06:11:53 +05:30
|
|
|
|
2005-11-07 14:20:53 +05:30
|
|
|
# The "optional" function is used to skip certain tests, ala:
|
2010-07-29 12:35:30 +05:30
|
|
|
# optional FEATURE_THINGY
|
2005-11-07 14:20:53 +05:30
|
|
|
#
|
|
|
|
# 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 06:11:53 +05:30
|
|
|
|
|
|
|
export FAILCOUNT=0
|
2005-11-07 14:20:53 +05:30
|
|
|
export SKIP=
|
2005-09-02 06:11:53 +05:30
|
|
|
|
2009-07-18 19:52:26 +05:30
|
|
|
# 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 21:14:46 +05:30
|
|
|
# Helper functions
|
|
|
|
|
2006-06-16 01:37:57 +05:30
|
|
|
optional()
|
2005-09-23 21:14:46 +05:30
|
|
|
{
|
2010-05-10 14:30:11 +05:30
|
|
|
SKIP=
|
|
|
|
while test "$1"; do
|
|
|
|
if test x"${OPTIONFLAGS/*:$1:*/y}" != x"y"; then
|
|
|
|
SKIP=1
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
done
|
2005-09-23 21:14:46 +05:30
|
|
|
}
|
|
|
|
|
2005-09-02 06:11:53 +05:30
|
|
|
# The testing function
|
|
|
|
|
2007-06-06 03:59:14 +05:30
|
|
|
testing()
|
2005-09-02 06:11:53 +05:30
|
|
|
{
|
2006-03-16 20:50:45 +05:30
|
|
|
NAME="$1"
|
2008-05-16 04:13:48 +05:30
|
|
|
[ -n "$1" ] || NAME="$2"
|
2006-03-16 20:50:45 +05:30
|
|
|
|
2005-09-02 06:11:53 +05:30
|
|
|
if [ $# -ne 5 ]
|
|
|
|
then
|
2010-05-12 05:19:04 +05:30
|
|
|
echo "Test $NAME has wrong number of arguments: $# (must be 5)" >&2
|
2008-05-16 04:13:48 +05:30
|
|
|
exit 1
|
2005-09-02 06:11:53 +05:30
|
|
|
fi
|
|
|
|
|
2008-05-16 04:13:48 +05:30
|
|
|
[ -z "$DEBUG" ] || set -x
|
2005-09-24 06:22:58 +05:30
|
|
|
|
2005-11-07 14:20:53 +05:30
|
|
|
if [ -n "$SKIP" ]
|
2005-09-23 21:14:46 +05:30
|
|
|
then
|
2006-03-16 20:50:45 +05:30
|
|
|
echo "SKIPPED: $NAME"
|
2005-11-07 14:20:53 +05:30
|
|
|
return 0
|
2005-09-23 21:14:46 +05:30
|
|
|
fi
|
|
|
|
|
2008-05-03 03:16:30 +05:30
|
|
|
$ECHO -ne "$3" > expected
|
|
|
|
$ECHO -ne "$4" > input
|
2009-07-18 19:52:26 +05:30
|
|
|
[ -z "$VERBOSE" ] || echo "echo -ne '$5' | $2"
|
2008-05-03 03:16:30 +05:30
|
|
|
$ECHO -ne "$5" | eval "$2" > actual
|
2005-09-02 06:11:53 +05:30
|
|
|
RETVAL=$?
|
|
|
|
|
2008-05-16 04:13:48 +05:30
|
|
|
if cmp expected actual >/dev/null 2>/dev/null
|
2005-09-02 06:11:53 +05:30
|
|
|
then
|
2008-05-16 04:13:48 +05:30
|
|
|
echo "PASS: $NAME"
|
|
|
|
else
|
2008-05-02 15:09:09 +05:30
|
|
|
FAILCOUNT=$(($FAILCOUNT + 1))
|
2006-03-16 20:50:45 +05:30
|
|
|
echo "FAIL: $NAME"
|
2008-05-16 04:13:48 +05:30
|
|
|
[ -z "$VERBOSE" ] || diff -u expected actual
|
2005-09-02 06:11:53 +05:30
|
|
|
fi
|
|
|
|
rm -f input expected actual
|
|
|
|
|
2008-05-16 04:13:48 +05:30
|
|
|
[ -z "$DEBUG" ] || set +x
|
2005-09-24 06:22:58 +05:30
|
|
|
|
2005-09-02 06:11:53 +05:30
|
|
|
return $RETVAL
|
|
|
|
}
|
2006-03-10 03:34:33 +05:30
|
|
|
|
|
|
|
# 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-06 03:59:14 +05:30
|
|
|
mkchroot()
|
2006-03-10 03:34:33 +05:30
|
|
|
{
|
|
|
|
[ $# -lt 2 ] && return
|
|
|
|
|
2008-05-03 03:16:30 +05:30
|
|
|
$ECHO -n .
|
2006-03-16 20:50:45 +05:30
|
|
|
|
2006-03-10 03:34:33 +05:30
|
|
|
dest=$1
|
|
|
|
shift
|
|
|
|
for i in "$@"
|
|
|
|
do
|
2008-05-02 15:09:09 +05:30
|
|
|
#bashism: [ "${i:0:1}" == "/" ] || i=$(which $i)
|
|
|
|
i=$(which $i) # no-op for /bin/prog
|
2006-03-18 08:31:57 +05:30
|
|
|
[ -f "$dest/$i" ] && continue
|
|
|
|
if [ -e "$i" ]
|
2006-03-10 03:34:33 +05:30
|
|
|
then
|
|
|
|
d=`echo "$i" | grep -o '.*/'` &&
|
|
|
|
mkdir -p "$dest/$d" &&
|
|
|
|
cat "$i" > "$dest/$i" &&
|
|
|
|
chmod +x "$dest/$i"
|
2006-03-18 08:31:57 +05:30
|
|
|
else
|
|
|
|
echo "Not found: $i"
|
2006-03-10 03:34:33 +05:30
|
|
|
fi
|
|
|
|
mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
|
|
|
|
done
|
|
|
|
}
|
2006-03-16 20:50:45 +05:30
|
|
|
|
|
|
|
# Set up a chroot environment and run commands within it.
|
|
|
|
# Needed commands listed on command line
|
|
|
|
# Script fed to stdin.
|
|
|
|
|
2007-06-06 03:59:14 +05:30
|
|
|
dochroot()
|
2006-03-16 20:50:45 +05:30
|
|
|
{
|
|
|
|
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-03 03:16:30 +05:30
|
|
|
$ECHO -n "Setup chroot"
|
2006-03-16 20:50:45 +05:30
|
|
|
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
|
|
|
|
}
|