1# Simple test harness infrastructure
2#
3# Copyright 2005 by Rob Landley
4
5# This file defines two functions, "testing" and "optionflag"
6
7# The following environment variables enable optional behavior in "testing":
8#    DEBUG - Show every command run by test script.
9#    VERBOSE - Print the diff -u of each failed test case.
10#              If equal to "fail", stop after first failed test.
11#    SKIP - do not perform this test (this is set by "optionflag")
12#
13# The "testing" function takes five arguments:
14#	$1) Description to display when running command
15#	$2) Command line arguments to command
16#	$3) Expected result (on stdout)
17#	$4) Data written to file "input"
18#	$5) Data written to stdin
19#
20# The exit value of testing is the exit value of the command it ran.
21#
22# The environment variable "FAILCOUNT" contains a cumulative total of the
23# number of failed tests.
24
25# The "optional" function is used to skip certain tests, ala:
26#   optionflag CFG_THINGY
27#
28# The "optional" function checks the environment variable "OPTIONFLAGS",
29# which is either empty (in which case it always clears SKIP) or
30# else contains a colon-separated list of features (in which case the function
31# clears SKIP if the flag was found, or sets it to 1 if the flag was not found).
32
33export FAILCOUNT=0
34export SKIP=
35
36# Helper functions
37
38# Check config to see if option is enabled, set SKIP if not.
39
40SHOWPASS=PASS
41SHOWFAIL=FAIL
42SHOWSKIP=SKIP
43
44if tty -s <&1
45then
46  SHOWPASS="$(echo -e "\033[1m\033[32m${SHOWPASS}\033[0m")"
47  SHOWFAIL="$(echo -e "\033[1m\033[31m${SHOWFAIL}\033[0m")"
48  SHOWSKIP="$(echo -e "\033[1m\033[33m${SHOWSKIP}\033[0m")"
49fi
50
51optional()
52{
53  option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"`
54  # Not set?
55  if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ]
56  then
57    SKIP=""
58    return
59  fi
60  SKIP=1
61}
62
63# The testing function
64
65testing()
66{
67  NAME="$1"
68  [ -z "$1" ] && NAME=$2
69
70  if [ $# -ne 5 ]
71  then
72    echo "Test $NAME has the wrong number of arguments ($# $*)" >&2
73    exit
74  fi
75
76  [ -n "$DEBUG" ] && set -x
77
78  if [ -n "$SKIP" ] || ( [ -n "$SKIP_HOST" ] && [ -n "$TEST_HOST" ])
79  then
80    [ ! -z "$VERBOSE" ] && echo "$SHOWSKIP: $NAME"
81    return 0
82  fi
83
84  echo -ne "$3" > expected
85  echo -ne "$4" > input
86  echo -ne "$5" | eval "$2" > actual
87  RETVAL=$?
88
89  cmp expected actual > /dev/null 2>&1
90  if [ $? -ne 0 ]
91  then
92    FAILCOUNT=$[$FAILCOUNT+1]
93    echo "$SHOWFAIL: $NAME"
94    if [ -n "$VERBOSE" ]
95    then
96      [ ! -z "$4" ] && echo "echo -ne \"$4\" > input"
97      echo "echo -ne '$5' | $2"
98      diff -au expected actual
99      [ "$VERBOSE" == fail ] && exit 1
100    fi
101  else
102    echo "$SHOWPASS: $NAME"
103  fi
104  rm -f input expected actual
105
106  [ -n "$DEBUG" ] && set +x
107
108  return $RETVAL
109}
110
111# Recursively grab an executable and all the libraries needed to run it.
112# Source paths beginning with / will be copied into destpath, otherwise
113# the file is assumed to already be there and only its library dependencies
114# are copied.
115
116mkchroot()
117{
118  [ $# -lt 2 ] && return
119
120  echo -n .
121
122  dest=$1
123  shift
124  for i in "$@"
125  do
126    [ "${i:0:1}" == "/" ] || i=$(which $i)
127    [ -f "$dest/$i" ] && continue
128    if [ -e "$i" ]
129    then
130      d=`echo "$i" | grep -o '.*/'` &&
131      mkdir -p "$dest/$d" &&
132      cat "$i" > "$dest/$i" &&
133      chmod +x "$dest/$i"
134    else
135      echo "Not found: $i"
136    fi
137    mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
138  done
139}
140
141# Set up a chroot environment and run commands within it.
142# Needed commands listed on command line
143# Script fed to stdin.
144
145dochroot()
146{
147  mkdir tmpdir4chroot
148  mount -t ramfs tmpdir4chroot tmpdir4chroot
149  mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
150  cp -L testing.sh tmpdir4chroot
151
152  # Copy utilities from command line arguments
153
154  echo -n "Setup chroot"
155  mkchroot tmpdir4chroot $*
156  echo
157
158  mknod tmpdir4chroot/dev/tty c 5 0
159  mknod tmpdir4chroot/dev/null c 1 3
160  mknod tmpdir4chroot/dev/zero c 1 5
161
162  # Copy script from stdin
163
164  cat > tmpdir4chroot/test.sh
165  chmod +x tmpdir4chroot/test.sh
166  chroot tmpdir4chroot /test.sh
167  umount -l tmpdir4chroot
168  rmdir tmpdir4chroot
169}
170
171