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 |
| 20 | # $2) Command line arguments to command" |
| 21 | # $3) Expected result (on stdout)" |
| 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 | { |
| 59 | if [ $# -ne 5 ] |
| 60 | then |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 61 | echo "Test $1 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 | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 65 | if [ -n "$DEBUG" ] ; then |
Mike Frysinger | 3f91d7a | 2005-09-24 00:52:58 +0000 | [diff] [blame] | 66 | set -x |
| 67 | fi |
| 68 | |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 69 | if [ -n "$SKIP" ] |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 70 | then |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 71 | echo "SKIPPED: $1" |
| 72 | return 0 |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 73 | fi |
| 74 | |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 75 | echo -ne "$3" > expected |
| 76 | echo -ne "$4" > input |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 77 | echo -ne "$5" | eval "$COMMAND $2" > actual |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 78 | RETVAL=$? |
| 79 | |
| 80 | cmp expected actual > /dev/null |
| 81 | if [ $? -ne 0 ] |
| 82 | then |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 83 | FAILCOUNT=$[$FAILCOUNT+1] |
| 84 | echo "FAIL: $1" |
| 85 | if [ -n "$VERBOSE" ] |
| 86 | then |
| 87 | diff -u expected actual |
| 88 | fi |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 89 | else |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 90 | echo "PASS: $1" |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 91 | fi |
| 92 | rm -f input expected actual |
| 93 | |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 94 | if [ -n "$DEBUG" ] |
| 95 | then |
Mike Frysinger | 3f91d7a | 2005-09-24 00:52:58 +0000 | [diff] [blame] | 96 | set +x |
| 97 | fi |
| 98 | |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 99 | return $RETVAL |
| 100 | } |
Rob Landley | 3a32475 | 2006-03-09 22:04:33 +0000 | [diff] [blame] | 101 | |
| 102 | # Recursively grab an executable and all the libraries needed to run it. |
| 103 | # Source paths beginning with / will be copied into destpath, otherwise |
| 104 | # the file is assumed to already be there and only its library dependencies |
| 105 | # are copied. |
| 106 | |
| 107 | function mkchroot |
| 108 | { |
| 109 | [ $# -lt 2 ] && return |
| 110 | |
| 111 | dest=$1 |
| 112 | shift |
| 113 | for i in "$@" |
| 114 | do |
| 115 | if [ "${i:0:1}" == "/" ] |
| 116 | then |
| 117 | [ -f "$dest/$i" ] && continue |
| 118 | d=`echo "$i" | grep -o '.*/'` && |
| 119 | mkdir -p "$dest/$d" && |
| 120 | cat "$i" > "$dest/$i" && |
| 121 | chmod +x "$dest/$i" |
| 122 | else |
| 123 | i="$dest/$i" |
| 124 | fi |
| 125 | mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ') |
| 126 | done |
| 127 | } |