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