blob: 7c642d729880a7bdc6d802b8b63389941b89b225 [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
9# The "testing" function must have the following environment variable set:
10# COMMAND = command to execute
Rob Landley16890752005-09-02 00:41:53 +000011#
Rob Landley48c61572005-11-07 08:50:53 +000012# 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 Landley16890752005-09-02 00:41:53 +000019# $1) Description to display when running command
Mike Frysinger81834532006-04-01 01:35:52 +000020# $2) Command line arguments to command
21# $3) Expected result (on stdout)
Rob Landley16890752005-09-02 00:41:53 +000022# $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 Landley48c61572005-11-07 08:50:53 +000028# number of failed tests.
Rob Landley16890752005-09-02 00:41:53 +000029
Rob Landley48c61572005-11-07 08:50:53 +000030# 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 Landley16890752005-09-02 00:41:53 +000037
38export FAILCOUNT=0
Rob Landley48c61572005-11-07 08:50:53 +000039export SKIP=
Rob Landley16890752005-09-02 00:41:53 +000040
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000041# Helper functions
42
Rob Landley48c61572005-11-07 08:50:53 +000043optional()
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000044{
Rob Landley006fa2d2006-02-16 09:00:57 +000045 option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"`
Rob Landley48c61572005-11-07 08:50:53 +000046 # Not set?
Rob Landley006fa2d2006-02-16 09:00:57 +000047 if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ]
Rob Landley48c61572005-11-07 08:50:53 +000048 then
49 SKIP=""
50 return
51 fi
52 SKIP=1
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000053}
54
Rob Landley16890752005-09-02 00:41:53 +000055# The testing function
56
Bernhard Reutner-Fischere34e8782005-10-06 12:48:03 +000057testing ()
Rob Landley16890752005-09-02 00:41:53 +000058{
Rob Landley4bb1b042006-03-16 15:20:45 +000059 NAME="$1"
60 [ -z "$1" ] && NAME=$2
61
Rob Landley16890752005-09-02 00:41:53 +000062 if [ $# -ne 5 ]
63 then
Rob Landley4bb1b042006-03-16 15:20:45 +000064 echo "Test $NAME has the wrong number of arguments ($# $*)" >&2
Rob Landley16890752005-09-02 00:41:53 +000065 exit
66 fi
67
Rob Landley4bb1b042006-03-16 15:20:45 +000068 [ -n "$DEBUG" ] && set -x
Mike Frysinger3f91d7a2005-09-24 00:52:58 +000069
Rob Landley48c61572005-11-07 08:50:53 +000070 if [ -n "$SKIP" ]
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000071 then
Rob Landley4bb1b042006-03-16 15:20:45 +000072 echo "SKIPPED: $NAME"
Rob Landley48c61572005-11-07 08:50:53 +000073 return 0
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000074 fi
75
Rob Landley16890752005-09-02 00:41:53 +000076 echo -ne "$3" > expected
77 echo -ne "$4" > input
Rob Landley4bb1b042006-03-16 15:20:45 +000078 [ -z "$VERBOSE" ] || echo "echo '$5' | $COMMAND $2"
79 echo -ne "$5" | eval "$2" > actual
Rob Landley16890752005-09-02 00:41:53 +000080 RETVAL=$?
81
82 cmp expected actual > /dev/null
83 if [ $? -ne 0 ]
84 then
Rob Landley48c61572005-11-07 08:50:53 +000085 FAILCOUNT=$[$FAILCOUNT+1]
Rob Landley4bb1b042006-03-16 15:20:45 +000086 echo "FAIL: $NAME"
87 [ -n "$VERBOSE" ] && diff -u expected actual
Rob Landley16890752005-09-02 00:41:53 +000088 else
Rob Landley4bb1b042006-03-16 15:20:45 +000089 echo "PASS: $NAME"
Rob Landley16890752005-09-02 00:41:53 +000090 fi
91 rm -f input expected actual
92
Rob Landley4bb1b042006-03-16 15:20:45 +000093 [ -n "$DEBUG" ] && set +x
Mike Frysinger3f91d7a2005-09-24 00:52:58 +000094
Rob Landley16890752005-09-02 00:41:53 +000095 return $RETVAL
96}
Rob Landley3a324752006-03-09 22:04:33 +000097
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
103function mkchroot
104{
105 [ $# -lt 2 ] && return
106
Rob Landley4bb1b042006-03-16 15:20:45 +0000107 echo -n .
108
Rob Landley3a324752006-03-09 22:04:33 +0000109 dest=$1
110 shift
111 for i in "$@"
112 do
Rob Landley6bc10632006-03-18 03:01:57 +0000113 [ "${i:0:1}" == "/" ] || i=$(which $i)
114 [ -f "$dest/$i" ] && continue
115 if [ -e "$i" ]
Rob Landley3a324752006-03-09 22:04:33 +0000116 then
Rob Landley3a324752006-03-09 22:04:33 +0000117 d=`echo "$i" | grep -o '.*/'` &&
118 mkdir -p "$dest/$d" &&
119 cat "$i" > "$dest/$i" &&
120 chmod +x "$dest/$i"
Rob Landley6bc10632006-03-18 03:01:57 +0000121 else
122 echo "Not found: $i"
Rob Landley3a324752006-03-09 22:04:33 +0000123 fi
124 mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
125 done
126}
Rob Landley4bb1b042006-03-16 15:20:45 +0000127
128# Set up a chroot environment and run commands within it.
129# Needed commands listed on command line
130# Script fed to stdin.
131
132function 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