blob: ccfebc805527051250ebb85fbd18560cc5a311ee [file] [log] [blame]
Rob Landley1bbc0cd2010-08-13 15:50:26 +02001# Simple test harness infrastructure for BusyBox
Rob Landley16890752005-09-02 00:41:53 +00002#
3# Copyright 2005 by Rob Landley
4#
5# License is GPLv2, see LICENSE in the busybox tarball for full license text.
6
Denis Vlasenko687a26f2008-05-02 21:46:30 +00007# This file defines two functions, "testing" and "optional"
8# and a couple more...
Rob Landley48c61572005-11-07 08:50:53 +00009
Rob Landley48c61572005-11-07 08:50:53 +000010# The following environment variables may be set to enable optional behavior
11# in "testing":
12# VERBOSE - Print the diff -u of each failed test case.
13# DEBUG - Enable command tracing.
Denis Vlasenko687a26f2008-05-02 21:46:30 +000014# SKIP - do not perform this test (this is set by "optional")
Rob Landley48c61572005-11-07 08:50:53 +000015#
16# The "testing" function takes five arguments:
Denis Vlasenko687a26f2008-05-02 21:46:30 +000017# $1) Test description
18# $2) Command(s) to run. May have pipes, redirects, etc
19# $3) Expected result on stdout
20# $4) Data to be written to file "input"
21# $5) Data to be written to stdin
Rob Landley16890752005-09-02 00:41:53 +000022#
Denis Vlasenko687a26f2008-05-02 21:46:30 +000023# The exit value of testing is the exit value of $2 it ran.
Rob Landley16890752005-09-02 00:41:53 +000024#
25# The environment variable "FAILCOUNT" contains a cumulative total of the
Rob Landley48c61572005-11-07 08:50:53 +000026# number of failed tests.
Rob Landley16890752005-09-02 00:41:53 +000027
Rob Landley48c61572005-11-07 08:50:53 +000028# The "optional" function is used to skip certain tests, ala:
Leonid Lisovskiy1538c972010-07-29 11:05:30 +040029# optional FEATURE_THINGY
Rob Landley48c61572005-11-07 08:50:53 +000030#
31# The "optional" function checks the environment variable "OPTIONFLAGS",
32# which is either empty (in which case it always clears SKIP) or
33# else contains a colon-separated list of features (in which case the function
34# 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 +000035
36export FAILCOUNT=0
Rob Landley48c61572005-11-07 08:50:53 +000037export SKIP=
Rob Landley16890752005-09-02 00:41:53 +000038
Tanguy Pruvot8aeb3712011-06-30 08:59:26 +020039# Android Specific
40
41if [ ! -x /bin ]; then
42 mount -o remount,rw /
43 ln -s /system/xbin /bin
44fi
45
Denys Vlasenko6ae64262009-07-18 16:22:26 +020046# Helper for helpers. Oh my...
47
48test x"$ECHO" != x"" || {
49 ECHO="echo"
50 test x"`echo -ne`" = x"" || {
51 # Compile and use a replacement 'echo' which understands -e -n
52 ECHO="$PWD/echo-ne"
53 test -x "$ECHO" || {
54 gcc -Os -o "$ECHO" ../scripts/echo.c || exit 1
55 }
56 }
57 export ECHO
58}
59
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000060# Helper functions
61
Rob Landleycd82c3c2006-06-15 20:07:57 +000062optional()
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000063{
Denys Vlasenko8e3aff02010-05-10 11:00:11 +020064 SKIP=
65 while test "$1"; do
maxwen27116ba2015-08-14 21:41:28 +020066 case "${OPTIONFLAGS}" in
67 *:$1:*) ;;
68 *) SKIP=1; return ;;
69 esac
Denys Vlasenko8e3aff02010-05-10 11:00:11 +020070 shift
71 done
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000072}
73
Rob Landley16890752005-09-02 00:41:53 +000074# The testing function
75
Denis Vlasenkocd0df462007-06-05 22:29:14 +000076testing()
Rob Landley16890752005-09-02 00:41:53 +000077{
Rob Landley4bb1b042006-03-16 15:20:45 +000078 NAME="$1"
Denis Vlasenko81e97a12008-05-15 22:43:48 +000079 [ -n "$1" ] || NAME="$2"
Rob Landley4bb1b042006-03-16 15:20:45 +000080
Rob Landley16890752005-09-02 00:41:53 +000081 if [ $# -ne 5 ]
82 then
Denys Vlasenkoa2215b92010-05-12 01:49:04 +020083 echo "Test $NAME has wrong number of arguments: $# (must be 5)" >&2
Denis Vlasenko81e97a12008-05-15 22:43:48 +000084 exit 1
Rob Landley16890752005-09-02 00:41:53 +000085 fi
86
Denis Vlasenko81e97a12008-05-15 22:43:48 +000087 [ -z "$DEBUG" ] || set -x
Mike Frysinger3f91d7a2005-09-24 00:52:58 +000088
Rob Landley48c61572005-11-07 08:50:53 +000089 if [ -n "$SKIP" ]
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000090 then
Rob Landley4bb1b042006-03-16 15:20:45 +000091 echo "SKIPPED: $NAME"
Rob Landley48c61572005-11-07 08:50:53 +000092 return 0
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000093 fi
94
Denis Vlasenko687a26f2008-05-02 21:46:30 +000095 $ECHO -ne "$3" > expected
96 $ECHO -ne "$4" > input
Denys Vlasenkofe9403a2010-11-28 01:41:40 +010097 [ -z "$VERBOSE" ] || echo ======================
Rob Landley1bbc0cd2010-08-13 15:50:26 +020098 [ -z "$VERBOSE" ] || echo "echo -ne '$4' >input"
Denys Vlasenko6ae64262009-07-18 16:22:26 +020099 [ -z "$VERBOSE" ] || echo "echo -ne '$5' | $2"
Denis Vlasenko687a26f2008-05-02 21:46:30 +0000100 $ECHO -ne "$5" | eval "$2" > actual
Rob Landley16890752005-09-02 00:41:53 +0000101 RETVAL=$?
102
Denis Vlasenko81e97a12008-05-15 22:43:48 +0000103 if cmp expected actual >/dev/null 2>/dev/null
Rob Landley16890752005-09-02 00:41:53 +0000104 then
Denis Vlasenko81e97a12008-05-15 22:43:48 +0000105 echo "PASS: $NAME"
106 else
Denis Vlasenko2dea01c2008-05-02 09:39:09 +0000107 FAILCOUNT=$(($FAILCOUNT + 1))
Rob Landley4bb1b042006-03-16 15:20:45 +0000108 echo "FAIL: $NAME"
Denis Vlasenko81e97a12008-05-15 22:43:48 +0000109 [ -z "$VERBOSE" ] || diff -u expected actual
Rob Landley16890752005-09-02 00:41:53 +0000110 fi
111 rm -f input expected actual
112
Denis Vlasenko81e97a12008-05-15 22:43:48 +0000113 [ -z "$DEBUG" ] || set +x
Mike Frysinger3f91d7a2005-09-24 00:52:58 +0000114
Rob Landley16890752005-09-02 00:41:53 +0000115 return $RETVAL
116}
Rob Landley3a324752006-03-09 22:04:33 +0000117
118# Recursively grab an executable and all the libraries needed to run it.
119# Source paths beginning with / will be copied into destpath, otherwise
120# the file is assumed to already be there and only its library dependencies
121# are copied.
122
Denis Vlasenkocd0df462007-06-05 22:29:14 +0000123mkchroot()
Rob Landley3a324752006-03-09 22:04:33 +0000124{
125 [ $# -lt 2 ] && return
126
Denis Vlasenko687a26f2008-05-02 21:46:30 +0000127 $ECHO -n .
Rob Landley4bb1b042006-03-16 15:20:45 +0000128
Rob Landley3a324752006-03-09 22:04:33 +0000129 dest=$1
130 shift
131 for i in "$@"
132 do
Denis Vlasenko2dea01c2008-05-02 09:39:09 +0000133 #bashism: [ "${i:0:1}" == "/" ] || i=$(which $i)
134 i=$(which $i) # no-op for /bin/prog
Rob Landley6bc10632006-03-18 03:01:57 +0000135 [ -f "$dest/$i" ] && continue
136 if [ -e "$i" ]
Rob Landley3a324752006-03-09 22:04:33 +0000137 then
Rob Landley3a324752006-03-09 22:04:33 +0000138 d=`echo "$i" | grep -o '.*/'` &&
139 mkdir -p "$dest/$d" &&
140 cat "$i" > "$dest/$i" &&
141 chmod +x "$dest/$i"
Rob Landley6bc10632006-03-18 03:01:57 +0000142 else
143 echo "Not found: $i"
Rob Landley3a324752006-03-09 22:04:33 +0000144 fi
145 mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
146 done
147}
Rob Landley4bb1b042006-03-16 15:20:45 +0000148
149# Set up a chroot environment and run commands within it.
150# Needed commands listed on command line
151# Script fed to stdin.
152
Denis Vlasenkocd0df462007-06-05 22:29:14 +0000153dochroot()
Rob Landley4bb1b042006-03-16 15:20:45 +0000154{
155 mkdir tmpdir4chroot
156 mount -t ramfs tmpdir4chroot tmpdir4chroot
157 mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
158 cp -L testing.sh tmpdir4chroot
159
160 # Copy utilities from command line arguments
161
Denis Vlasenko687a26f2008-05-02 21:46:30 +0000162 $ECHO -n "Setup chroot"
Rob Landley4bb1b042006-03-16 15:20:45 +0000163 mkchroot tmpdir4chroot $*
164 echo
165
166 mknod tmpdir4chroot/dev/tty c 5 0
167 mknod tmpdir4chroot/dev/null c 1 3
168 mknod tmpdir4chroot/dev/zero c 1 5
169
170 # Copy script from stdin
171
172 cat > tmpdir4chroot/test.sh
173 chmod +x tmpdir4chroot/test.sh
174 chroot tmpdir4chroot /test.sh
175 umount -l tmpdir4chroot
176 rmdir tmpdir4chroot
177}