blob: 7897c1622a02cea51bf8623f5aa369185a8c481b [file] [log] [blame]
Rob Landley16890752005-09-02 00:41:53 +00001# 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 Landley48c61572005-11-07 08:50:53 +00007# This file defines two functions, "testing" and "optionflag"
8
Rob Landley48c61572005-11-07 08:50:53 +00009# 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 Landley16890752005-09-02 00:41:53 +000016# $1) Description to display when running command
Mike Frysinger81834532006-04-01 01:35:52 +000017# $2) Command line arguments to command
18# $3) Expected result (on stdout)
Rob Landley16890752005-09-02 00:41:53 +000019# $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 Landley48c61572005-11-07 08:50:53 +000025# number of failed tests.
Rob Landley16890752005-09-02 00:41:53 +000026
Rob Landley48c61572005-11-07 08:50:53 +000027# 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 Landley16890752005-09-02 00:41:53 +000034
35export FAILCOUNT=0
Rob Landley48c61572005-11-07 08:50:53 +000036export SKIP=
Rob Landley16890752005-09-02 00:41:53 +000037
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000038# Helper functions
39
Bernhard Reutner-Fischer89a22ea2006-05-25 13:24:02 +000040optional ()
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000041{
Rob Landley006fa2d2006-02-16 09:00:57 +000042 option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"`
Rob Landley48c61572005-11-07 08:50:53 +000043 # Not set?
Rob Landley006fa2d2006-02-16 09:00:57 +000044 if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ]
Rob Landley48c61572005-11-07 08:50:53 +000045 then
46 SKIP=""
47 return
48 fi
49 SKIP=1
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000050}
51
Rob Landley16890752005-09-02 00:41:53 +000052# The testing function
53
Bernhard Reutner-Fischere34e8782005-10-06 12:48:03 +000054testing ()
Rob Landley16890752005-09-02 00:41:53 +000055{
Rob Landley4bb1b042006-03-16 15:20:45 +000056 NAME="$1"
57 [ -z "$1" ] && NAME=$2
Bernhard Reutner-Fischer89a22ea2006-05-25 13:24:02 +000058 ret=0
Rob Landley4bb1b042006-03-16 15:20:45 +000059
Rob Landley16890752005-09-02 00:41:53 +000060 if [ $# -ne 5 ]
61 then
Rob Landley4bb1b042006-03-16 15:20:45 +000062 echo "Test $NAME has the wrong number of arguments ($# $*)" >&2
Rob Landley16890752005-09-02 00:41:53 +000063 exit
64 fi
65
Rob Landley4bb1b042006-03-16 15:20:45 +000066 [ -n "$DEBUG" ] && set -x
Mike Frysinger3f91d7a2005-09-24 00:52:58 +000067
Rob Landley48c61572005-11-07 08:50:53 +000068 if [ -n "$SKIP" ]
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000069 then
Rob Landley4bb1b042006-03-16 15:20:45 +000070 echo "SKIPPED: $NAME"
Rob Landley48c61572005-11-07 08:50:53 +000071 return 0
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000072 fi
73
Rob Landley16890752005-09-02 00:41:53 +000074 echo -ne "$3" > expected
75 echo -ne "$4" > input
Rob Landley67d5b8b2006-05-02 21:39:04 +000076 [ -z "$VERBOSE" ] || echo "echo '$5' | $2"
Rob Landley4bb1b042006-03-16 15:20:45 +000077 echo -ne "$5" | eval "$2" > actual
Rob Landley16890752005-09-02 00:41:53 +000078 RETVAL=$?
79
Bernhard Reutner-Fischer89a22ea2006-05-25 13:24:02 +000080 cmp expected actual > /dev/null || ret=$?
81 if [ $ret -ne 0 ]
Rob Landley16890752005-09-02 00:41:53 +000082 then
Rob Landley48c61572005-11-07 08:50:53 +000083 FAILCOUNT=$[$FAILCOUNT+1]
Rob Landley4bb1b042006-03-16 15:20:45 +000084 echo "FAIL: $NAME"
Bernhard Reutner-Fischer89a22ea2006-05-25 13:24:02 +000085 if [ -n "$VERBOSE" ]
86 then
87 diff -u expected actual || /bin/true
88 fi
Rob Landley16890752005-09-02 00:41:53 +000089 else
Rob Landley4bb1b042006-03-16 15:20:45 +000090 echo "PASS: $NAME"
Rob Landley16890752005-09-02 00:41:53 +000091 fi
92 rm -f input expected actual
93
Rob Landley4bb1b042006-03-16 15:20:45 +000094 [ -n "$DEBUG" ] && set +x
Mike Frysinger3f91d7a2005-09-24 00:52:58 +000095
Rob Landley16890752005-09-02 00:41:53 +000096 return $RETVAL
97}
Rob Landley3a324752006-03-09 22:04:33 +000098
99# Recursively grab an executable and all the libraries needed to run it.
100# Source paths beginning with / will be copied into destpath, otherwise
101# the file is assumed to already be there and only its library dependencies
102# are copied.
103
Bernhard Reutner-Fischer89a22ea2006-05-25 13:24:02 +0000104mkchroot ()
Rob Landley3a324752006-03-09 22:04:33 +0000105{
106 [ $# -lt 2 ] && return
107
Rob Landley4bb1b042006-03-16 15:20:45 +0000108 echo -n .
109
Rob Landley3a324752006-03-09 22:04:33 +0000110 dest=$1
111 shift
112 for i in "$@"
113 do
Rob Landley6bc10632006-03-18 03:01:57 +0000114 [ "${i:0:1}" == "/" ] || i=$(which $i)
115 [ -f "$dest/$i" ] && continue
116 if [ -e "$i" ]
Rob Landley3a324752006-03-09 22:04:33 +0000117 then
Rob Landley3a324752006-03-09 22:04:33 +0000118 d=`echo "$i" | grep -o '.*/'` &&
119 mkdir -p "$dest/$d" &&
120 cat "$i" > "$dest/$i" &&
121 chmod +x "$dest/$i"
Rob Landley6bc10632006-03-18 03:01:57 +0000122 else
123 echo "Not found: $i"
Rob Landley3a324752006-03-09 22:04:33 +0000124 fi
125 mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
126 done
127}
Rob Landley4bb1b042006-03-16 15:20:45 +0000128
129# Set up a chroot environment and run commands within it.
130# Needed commands listed on command line
131# Script fed to stdin.
132
Bernhard Reutner-Fischer89a22ea2006-05-25 13:24:02 +0000133dochroot ()
Rob Landley4bb1b042006-03-16 15:20:45 +0000134{
135 mkdir tmpdir4chroot
136 mount -t ramfs tmpdir4chroot tmpdir4chroot
137 mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
138 cp -L testing.sh tmpdir4chroot
139
140 # Copy utilities from command line arguments
141
142 echo -n "Setup chroot"
143 mkchroot tmpdir4chroot $*
144 echo
145
146 mknod tmpdir4chroot/dev/tty c 5 0
147 mknod tmpdir4chroot/dev/null c 1 3
148 mknod tmpdir4chroot/dev/zero c 1 5
149
150 # Copy script from stdin
151
152 cat > tmpdir4chroot/test.sh
153 chmod +x tmpdir4chroot/test.sh
154 chroot tmpdir4chroot /test.sh
155 umount -l tmpdir4chroot
156 rmdir tmpdir4chroot
157}
158