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