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 | |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 7 | # This file defines two functions, "testing" and "optionflag" |
| 8 | |
| 9 | # The "testing" function must have the following environment variable set: |
| 10 | # COMMAND = command to execute |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 11 | # |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 12 | # The following environment variables may be set to enable optional behavior |
| 13 | # in "testing": |
| 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") |
| 17 | # |
| 18 | # The "testing" function takes five arguments: |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 19 | # $1) Description to display when running command |
Mike Frysinger | 8183453 | 2006-04-01 01:35:52 +0000 | [diff] [blame] | 20 | # $2) Command line arguments to command |
| 21 | # $3) Expected result (on stdout) |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 22 | # $4) Data written to file "input" |
| 23 | # $5) Data written to stdin |
| 24 | # |
| 25 | # The exit value of testing is the exit value of the command it ran. |
| 26 | # |
| 27 | # The environment variable "FAILCOUNT" contains a cumulative total of the |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 28 | # number of failed tests. |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 29 | |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 30 | # The "optional" function is used to skip certain tests, ala: |
| 31 | # optionflag CONFIG_FEATURE_THINGY |
| 32 | # |
| 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). |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 37 | |
| 38 | export FAILCOUNT=0 |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 39 | export SKIP= |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 40 | |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 41 | # Helper functions |
| 42 | |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 43 | optional() |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 44 | { |
Rob Landley | 006fa2d | 2006-02-16 09:00:57 +0000 | [diff] [blame] | 45 | option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"` |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 46 | # Not set? |
Rob Landley | 006fa2d | 2006-02-16 09:00:57 +0000 | [diff] [blame] | 47 | if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ] |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 48 | then |
| 49 | SKIP="" |
| 50 | return |
| 51 | fi |
| 52 | SKIP=1 |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 55 | # The testing function |
| 56 | |
Bernhard Reutner-Fischer | e34e878 | 2005-10-06 12:48:03 +0000 | [diff] [blame] | 57 | testing () |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 58 | { |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 59 | NAME="$1" |
| 60 | [ -z "$1" ] && NAME=$2 |
| 61 | |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 62 | if [ $# -ne 5 ] |
| 63 | then |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 64 | echo "Test $NAME has the wrong number of arguments ($# $*)" >&2 |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 65 | exit |
| 66 | fi |
| 67 | |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 68 | [ -n "$DEBUG" ] && set -x |
Mike Frysinger | 3f91d7a | 2005-09-24 00:52:58 +0000 | [diff] [blame] | 69 | |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 70 | if [ -n "$SKIP" ] |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 71 | then |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 72 | echo "SKIPPED: $NAME" |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 73 | return 0 |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 74 | fi |
| 75 | |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 76 | echo -ne "$3" > expected |
| 77 | echo -ne "$4" > input |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 78 | [ -z "$VERBOSE" ] || echo "echo '$5' | $COMMAND $2" |
| 79 | echo -ne "$5" | eval "$2" > actual |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 80 | RETVAL=$? |
| 81 | |
| 82 | cmp expected actual > /dev/null |
| 83 | if [ $? -ne 0 ] |
| 84 | then |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 85 | FAILCOUNT=$[$FAILCOUNT+1] |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 86 | echo "FAIL: $NAME" |
| 87 | [ -n "$VERBOSE" ] && diff -u expected actual |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 88 | else |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 89 | echo "PASS: $NAME" |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 90 | fi |
| 91 | rm -f input expected actual |
| 92 | |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 93 | [ -n "$DEBUG" ] && set +x |
Mike Frysinger | 3f91d7a | 2005-09-24 00:52:58 +0000 | [diff] [blame] | 94 | |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 95 | return $RETVAL |
| 96 | } |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 97 | |
| 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 |
| 101 | # are copied. |
| 102 | |
| 103 | function mkchroot |
| 104 | { |
| 105 | [ $# -lt 2 ] && return |
| 106 | |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 107 | echo -n . |
| 108 | |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 109 | dest=$1 |
| 110 | shift |
| 111 | for i in "$@" |
| 112 | do |
Rob Landley | 6bc1063 | 2006-03-18 03:01:57 +0000 | [diff] [blame] | 113 | [ "${i:0:1}" == "/" ] || i=$(which $i) |
| 114 | [ -f "$dest/$i" ] && continue |
| 115 | if [ -e "$i" ] |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 116 | then |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 117 | d=`echo "$i" | grep -o '.*/'` && |
| 118 | mkdir -p "$dest/$d" && |
| 119 | cat "$i" > "$dest/$i" && |
| 120 | chmod +x "$dest/$i" |
Rob Landley | 6bc1063 | 2006-03-18 03:01:57 +0000 | [diff] [blame] | 121 | else |
| 122 | echo "Not found: $i" |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 123 | fi |
| 124 | mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ') |
| 125 | done |
| 126 | } |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 127 | |
| 128 | # Set up a chroot environment and run commands within it. |
| 129 | # Needed commands listed on command line |
| 130 | # Script fed to stdin. |
| 131 | |
| 132 | function dochroot |
| 133 | { |
| 134 | mkdir tmpdir4chroot |
| 135 | mount -t ramfs tmpdir4chroot tmpdir4chroot |
| 136 | mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev} |
| 137 | cp -L testing.sh tmpdir4chroot |
| 138 | |
| 139 | # Copy utilities from command line arguments |
| 140 | |
| 141 | echo -n "Setup chroot" |
| 142 | mkchroot tmpdir4chroot $* |
| 143 | echo |
| 144 | |
| 145 | mknod tmpdir4chroot/dev/tty c 5 0 |
| 146 | mknod tmpdir4chroot/dev/null c 1 3 |
| 147 | mknod tmpdir4chroot/dev/zero c 1 5 |
| 148 | |
| 149 | # Copy script from stdin |
| 150 | |
| 151 | cat > tmpdir4chroot/test.sh |
| 152 | chmod +x tmpdir4chroot/test.sh |
| 153 | chroot tmpdir4chroot /test.sh |
| 154 | umount -l tmpdir4chroot |
| 155 | rmdir tmpdir4chroot |
| 156 | } |
| 157 | |