Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 1 | # 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 Vlasenko | 687a26f | 2008-05-02 21:46:30 +0000 | [diff] [blame] | 7 | # This file defines two functions, "testing" and "optional" |
| 8 | # and a couple more... |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 9 | |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 10 | # 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 Vlasenko | 687a26f | 2008-05-02 21:46:30 +0000 | [diff] [blame] | 14 | # SKIP - do not perform this test (this is set by "optional") |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 15 | # |
| 16 | # The "testing" function takes five arguments: |
Denis Vlasenko | 687a26f | 2008-05-02 21:46:30 +0000 | [diff] [blame] | 17 | # $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 Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 22 | # |
Denis Vlasenko | 687a26f | 2008-05-02 21:46:30 +0000 | [diff] [blame] | 23 | # The exit value of testing is the exit value of $2 it ran. |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 24 | # |
| 25 | # The environment variable "FAILCOUNT" contains a cumulative total of the |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 26 | # number of failed tests. |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 27 | |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 28 | # The "optional" function is used to skip certain tests, ala: |
Denis Vlasenko | 687a26f | 2008-05-02 21:46:30 +0000 | [diff] [blame] | 29 | # optional CONFIG_FEATURE_THINGY |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 30 | # |
| 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 Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 35 | |
| 36 | export FAILCOUNT=0 |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 37 | export SKIP= |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 38 | |
Denys Vlasenko | 6ae6426 | 2009-07-18 16:22:26 +0200 | [diff] [blame] | 39 | # Helper for helpers. Oh my... |
| 40 | |
| 41 | test x"$ECHO" != x"" || { |
| 42 | ECHO="echo" |
| 43 | test x"`echo -ne`" = x"" || { |
| 44 | # Compile and use a replacement 'echo' which understands -e -n |
| 45 | ECHO="$PWD/echo-ne" |
| 46 | test -x "$ECHO" || { |
| 47 | gcc -Os -o "$ECHO" ../scripts/echo.c || exit 1 |
| 48 | } |
| 49 | } |
| 50 | export ECHO |
| 51 | } |
| 52 | |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 53 | # Helper functions |
| 54 | |
Rob Landley | cd82c3c | 2006-06-15 20:07:57 +0000 | [diff] [blame] | 55 | optional() |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 56 | { |
Denis Vlasenko | 687a26f | 2008-05-02 21:46:30 +0000 | [diff] [blame] | 57 | option=`echo ":$OPTIONFLAGS:" | grep ":$1:"` |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 58 | # Not set? |
Rob Landley | 006fa2d | 2006-02-16 09:00:57 +0000 | [diff] [blame] | 59 | if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ] |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 60 | then |
Denis Vlasenko | 687a26f | 2008-05-02 21:46:30 +0000 | [diff] [blame] | 61 | SKIP= |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 62 | return |
| 63 | fi |
| 64 | SKIP=1 |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 67 | # The testing function |
| 68 | |
Denis Vlasenko | cd0df46 | 2007-06-05 22:29:14 +0000 | [diff] [blame] | 69 | testing() |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 70 | { |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 71 | NAME="$1" |
Denis Vlasenko | 81e97a1 | 2008-05-15 22:43:48 +0000 | [diff] [blame] | 72 | [ -n "$1" ] || NAME="$2" |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 73 | |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 74 | if [ $# -ne 5 ] |
| 75 | then |
Denis Vlasenko | 4e1e720 | 2007-11-26 05:38:20 +0000 | [diff] [blame] | 76 | echo "Test $NAME has wrong number of arguments (must be 5) ($# $*)" >&2 |
Denis Vlasenko | 81e97a1 | 2008-05-15 22:43:48 +0000 | [diff] [blame] | 77 | exit 1 |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 78 | fi |
| 79 | |
Denis Vlasenko | 81e97a1 | 2008-05-15 22:43:48 +0000 | [diff] [blame] | 80 | [ -z "$DEBUG" ] || set -x |
Mike Frysinger | 3f91d7a | 2005-09-24 00:52:58 +0000 | [diff] [blame] | 81 | |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 82 | if [ -n "$SKIP" ] |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 83 | then |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 84 | echo "SKIPPED: $NAME" |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 85 | return 0 |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 86 | fi |
| 87 | |
Denis Vlasenko | 687a26f | 2008-05-02 21:46:30 +0000 | [diff] [blame] | 88 | $ECHO -ne "$3" > expected |
| 89 | $ECHO -ne "$4" > input |
Denys Vlasenko | 6ae6426 | 2009-07-18 16:22:26 +0200 | [diff] [blame] | 90 | [ -z "$VERBOSE" ] || echo "echo -ne '$5' | $2" |
Denis Vlasenko | 687a26f | 2008-05-02 21:46:30 +0000 | [diff] [blame] | 91 | $ECHO -ne "$5" | eval "$2" > actual |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 92 | RETVAL=$? |
| 93 | |
Denis Vlasenko | 81e97a1 | 2008-05-15 22:43:48 +0000 | [diff] [blame] | 94 | if cmp expected actual >/dev/null 2>/dev/null |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 95 | then |
Denis Vlasenko | 81e97a1 | 2008-05-15 22:43:48 +0000 | [diff] [blame] | 96 | echo "PASS: $NAME" |
| 97 | else |
Denis Vlasenko | 2dea01c | 2008-05-02 09:39:09 +0000 | [diff] [blame] | 98 | FAILCOUNT=$(($FAILCOUNT + 1)) |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 99 | echo "FAIL: $NAME" |
Denis Vlasenko | 81e97a1 | 2008-05-15 22:43:48 +0000 | [diff] [blame] | 100 | [ -z "$VERBOSE" ] || diff -u expected actual |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 101 | fi |
| 102 | rm -f input expected actual |
| 103 | |
Denis Vlasenko | 81e97a1 | 2008-05-15 22:43:48 +0000 | [diff] [blame] | 104 | [ -z "$DEBUG" ] || set +x |
Mike Frysinger | 3f91d7a | 2005-09-24 00:52:58 +0000 | [diff] [blame] | 105 | |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 106 | return $RETVAL |
| 107 | } |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 108 | |
| 109 | # Recursively grab an executable and all the libraries needed to run it. |
| 110 | # Source paths beginning with / will be copied into destpath, otherwise |
| 111 | # the file is assumed to already be there and only its library dependencies |
| 112 | # are copied. |
| 113 | |
Denis Vlasenko | cd0df46 | 2007-06-05 22:29:14 +0000 | [diff] [blame] | 114 | mkchroot() |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 115 | { |
| 116 | [ $# -lt 2 ] && return |
| 117 | |
Denis Vlasenko | 687a26f | 2008-05-02 21:46:30 +0000 | [diff] [blame] | 118 | $ECHO -n . |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 119 | |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 120 | dest=$1 |
| 121 | shift |
| 122 | for i in "$@" |
| 123 | do |
Denis Vlasenko | 2dea01c | 2008-05-02 09:39:09 +0000 | [diff] [blame] | 124 | #bashism: [ "${i:0:1}" == "/" ] || i=$(which $i) |
| 125 | i=$(which $i) # no-op for /bin/prog |
Rob Landley | 6bc1063 | 2006-03-18 03:01:57 +0000 | [diff] [blame] | 126 | [ -f "$dest/$i" ] && continue |
| 127 | if [ -e "$i" ] |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 128 | then |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 129 | d=`echo "$i" | grep -o '.*/'` && |
| 130 | mkdir -p "$dest/$d" && |
| 131 | cat "$i" > "$dest/$i" && |
| 132 | chmod +x "$dest/$i" |
Rob Landley | 6bc1063 | 2006-03-18 03:01:57 +0000 | [diff] [blame] | 133 | else |
| 134 | echo "Not found: $i" |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 135 | fi |
| 136 | mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ') |
| 137 | done |
| 138 | } |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 139 | |
| 140 | # Set up a chroot environment and run commands within it. |
| 141 | # Needed commands listed on command line |
| 142 | # Script fed to stdin. |
| 143 | |
Denis Vlasenko | cd0df46 | 2007-06-05 22:29:14 +0000 | [diff] [blame] | 144 | dochroot() |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 145 | { |
| 146 | mkdir tmpdir4chroot |
| 147 | mount -t ramfs tmpdir4chroot tmpdir4chroot |
| 148 | mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev} |
| 149 | cp -L testing.sh tmpdir4chroot |
| 150 | |
| 151 | # Copy utilities from command line arguments |
| 152 | |
Denis Vlasenko | 687a26f | 2008-05-02 21:46:30 +0000 | [diff] [blame] | 153 | $ECHO -n "Setup chroot" |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 154 | mkchroot tmpdir4chroot $* |
| 155 | echo |
| 156 | |
| 157 | mknod tmpdir4chroot/dev/tty c 5 0 |
| 158 | mknod tmpdir4chroot/dev/null c 1 3 |
| 159 | mknod tmpdir4chroot/dev/zero c 1 5 |
| 160 | |
| 161 | # Copy script from stdin |
| 162 | |
| 163 | cat > tmpdir4chroot/test.sh |
| 164 | chmod +x tmpdir4chroot/test.sh |
| 165 | chroot tmpdir4chroot /test.sh |
| 166 | umount -l tmpdir4chroot |
| 167 | rmdir tmpdir4chroot |
| 168 | } |