blob: ea59f317a61505f54030f7f339c2206a972dff73 [file] [log] [blame]
Rob Landley16890752005-09-02 00:41:53 +00001# Simple test harness infrastructurei for BusyBox
2#
3# Copyright 2005 by Rob Landley
4#
5# License is GPLv2, see LICENSE in the busybox tarball for full license text.
6
Denis Vlasenko687a26f2008-05-02 21:46:30 +00007# This file defines two functions, "testing" and "optional"
8# and a couple more...
Rob Landley48c61572005-11-07 08:50:53 +00009
Rob Landley48c61572005-11-07 08:50:53 +000010# The following environment variables may be set to enable optional behavior
11# in "testing":
12# VERBOSE - Print the diff -u of each failed test case.
13# DEBUG - Enable command tracing.
Denis Vlasenko687a26f2008-05-02 21:46:30 +000014# SKIP - do not perform this test (this is set by "optional")
Rob Landley48c61572005-11-07 08:50:53 +000015#
16# The "testing" function takes five arguments:
Denis Vlasenko687a26f2008-05-02 21:46:30 +000017# $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
Rob Landley16890752005-09-02 00:41:53 +000022#
Denis Vlasenko687a26f2008-05-02 21:46:30 +000023# The exit value of testing is the exit value of $2 it ran.
Rob Landley16890752005-09-02 00:41:53 +000024#
25# The environment variable "FAILCOUNT" contains a cumulative total of the
Rob Landley48c61572005-11-07 08:50:53 +000026# number of failed tests.
Rob Landley16890752005-09-02 00:41:53 +000027
Rob Landley48c61572005-11-07 08:50:53 +000028# The "optional" function is used to skip certain tests, ala:
Denis Vlasenko687a26f2008-05-02 21:46:30 +000029# optional CONFIG_FEATURE_THINGY
Rob Landley48c61572005-11-07 08:50:53 +000030#
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).
Rob Landley16890752005-09-02 00:41:53 +000035
36export FAILCOUNT=0
Rob Landley48c61572005-11-07 08:50:53 +000037export SKIP=
Rob Landley16890752005-09-02 00:41:53 +000038
Denis Vlasenko687a26f2008-05-02 21:46:30 +000039# Helper for helpers. Oh my...
Denis Vlasenko81e97a12008-05-15 22:43:48 +000040test x"$ECHO" != x"" || {
Denis Vlasenko687a26f2008-05-02 21:46:30 +000041 ECHO="echo"
42 test x"`echo -ne`" = x"" || {
43 # Compile and use a replacement 'echo' which understands -e -n
44 ECHO="$PWD/echo-ne"
45 test -x "$ECHO" || {
46 gcc -Os -o "$ECHO" ../scripts/echo.c || exit 1
47 }
48 }
49 export ECHO
50}
51
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000052# Helper functions
53
Rob Landleycd82c3c2006-06-15 20:07:57 +000054optional()
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000055{
Denis Vlasenko687a26f2008-05-02 21:46:30 +000056 option=`echo ":$OPTIONFLAGS:" | grep ":$1:"`
Rob Landley48c61572005-11-07 08:50:53 +000057 # Not set?
Rob Landley006fa2d2006-02-16 09:00:57 +000058 if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ]
Rob Landley48c61572005-11-07 08:50:53 +000059 then
Denis Vlasenko687a26f2008-05-02 21:46:30 +000060 SKIP=
Rob Landley48c61572005-11-07 08:50:53 +000061 return
62 fi
63 SKIP=1
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000064}
65
Rob Landley16890752005-09-02 00:41:53 +000066# The testing function
67
Denis Vlasenkocd0df462007-06-05 22:29:14 +000068testing()
Rob Landley16890752005-09-02 00:41:53 +000069{
Rob Landley4bb1b042006-03-16 15:20:45 +000070 NAME="$1"
Denis Vlasenko81e97a12008-05-15 22:43:48 +000071 [ -n "$1" ] || NAME="$2"
Rob Landley4bb1b042006-03-16 15:20:45 +000072
Rob Landley16890752005-09-02 00:41:53 +000073 if [ $# -ne 5 ]
74 then
Denis Vlasenko4e1e7202007-11-26 05:38:20 +000075 echo "Test $NAME has wrong number of arguments (must be 5) ($# $*)" >&2
Denis Vlasenko81e97a12008-05-15 22:43:48 +000076 exit 1
Rob Landley16890752005-09-02 00:41:53 +000077 fi
78
Denis Vlasenko81e97a12008-05-15 22:43:48 +000079 [ -z "$DEBUG" ] || set -x
Mike Frysinger3f91d7a2005-09-24 00:52:58 +000080
Rob Landley48c61572005-11-07 08:50:53 +000081 if [ -n "$SKIP" ]
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000082 then
Rob Landley4bb1b042006-03-16 15:20:45 +000083 echo "SKIPPED: $NAME"
Rob Landley48c61572005-11-07 08:50:53 +000084 return 0
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000085 fi
86
Denis Vlasenko687a26f2008-05-02 21:46:30 +000087 $ECHO -ne "$3" > expected
88 $ECHO -ne "$4" > input
Rob Landley67d5b8b2006-05-02 21:39:04 +000089 [ -z "$VERBOSE" ] || echo "echo '$5' | $2"
Denis Vlasenko687a26f2008-05-02 21:46:30 +000090 $ECHO -ne "$5" | eval "$2" > actual
Rob Landley16890752005-09-02 00:41:53 +000091 RETVAL=$?
92
Denis Vlasenko81e97a12008-05-15 22:43:48 +000093 if cmp expected actual >/dev/null 2>/dev/null
Rob Landley16890752005-09-02 00:41:53 +000094 then
Denis Vlasenko81e97a12008-05-15 22:43:48 +000095 echo "PASS: $NAME"
96 else
Denis Vlasenko2dea01c2008-05-02 09:39:09 +000097 FAILCOUNT=$(($FAILCOUNT + 1))
Rob Landley4bb1b042006-03-16 15:20:45 +000098 echo "FAIL: $NAME"
Denis Vlasenko81e97a12008-05-15 22:43:48 +000099 [ -z "$VERBOSE" ] || diff -u expected actual
Rob Landley16890752005-09-02 00:41:53 +0000100 fi
101 rm -f input expected actual
102
Denis Vlasenko81e97a12008-05-15 22:43:48 +0000103 [ -z "$DEBUG" ] || set +x
Mike Frysinger3f91d7a2005-09-24 00:52:58 +0000104
Rob Landley16890752005-09-02 00:41:53 +0000105 return $RETVAL
106}
Rob Landley3a324752006-03-09 22:04:33 +0000107
108# Recursively grab an executable and all the libraries needed to run it.
109# Source paths beginning with / will be copied into destpath, otherwise
110# the file is assumed to already be there and only its library dependencies
111# are copied.
112
Denis Vlasenkocd0df462007-06-05 22:29:14 +0000113mkchroot()
Rob Landley3a324752006-03-09 22:04:33 +0000114{
115 [ $# -lt 2 ] && return
116
Denis Vlasenko687a26f2008-05-02 21:46:30 +0000117 $ECHO -n .
Rob Landley4bb1b042006-03-16 15:20:45 +0000118
Rob Landley3a324752006-03-09 22:04:33 +0000119 dest=$1
120 shift
121 for i in "$@"
122 do
Denis Vlasenko2dea01c2008-05-02 09:39:09 +0000123 #bashism: [ "${i:0:1}" == "/" ] || i=$(which $i)
124 i=$(which $i) # no-op for /bin/prog
Rob Landley6bc10632006-03-18 03:01:57 +0000125 [ -f "$dest/$i" ] && continue
126 if [ -e "$i" ]
Rob Landley3a324752006-03-09 22:04:33 +0000127 then
Rob Landley3a324752006-03-09 22:04:33 +0000128 d=`echo "$i" | grep -o '.*/'` &&
129 mkdir -p "$dest/$d" &&
130 cat "$i" > "$dest/$i" &&
131 chmod +x "$dest/$i"
Rob Landley6bc10632006-03-18 03:01:57 +0000132 else
133 echo "Not found: $i"
Rob Landley3a324752006-03-09 22:04:33 +0000134 fi
135 mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
136 done
137}
Rob Landley4bb1b042006-03-16 15:20:45 +0000138
139# Set up a chroot environment and run commands within it.
140# Needed commands listed on command line
141# Script fed to stdin.
142
Denis Vlasenkocd0df462007-06-05 22:29:14 +0000143dochroot()
Rob Landley4bb1b042006-03-16 15:20:45 +0000144{
145 mkdir tmpdir4chroot
146 mount -t ramfs tmpdir4chroot tmpdir4chroot
147 mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
148 cp -L testing.sh tmpdir4chroot
149
150 # Copy utilities from command line arguments
151
Denis Vlasenko687a26f2008-05-02 21:46:30 +0000152 $ECHO -n "Setup chroot"
Rob Landley4bb1b042006-03-16 15:20:45 +0000153 mkchroot tmpdir4chroot $*
154 echo
155
156 mknod tmpdir4chroot/dev/tty c 5 0
157 mknod tmpdir4chroot/dev/null c 1 3
158 mknod tmpdir4chroot/dev/zero c 1 5
159
160 # Copy script from stdin
161
162 cat > tmpdir4chroot/test.sh
163 chmod +x tmpdir4chroot/test.sh
164 chroot tmpdir4chroot /test.sh
165 umount -l tmpdir4chroot
166 rmdir tmpdir4chroot
167}