Imported Upstream version 0.155
[platform/upstream/elfutils.git] / tests / test-subr.sh
1 #! /bin/sh
2 # Copyright (C) 2005-2012 Red Hat, Inc.
3 # This file is part of elfutils.
4 #
5 # This file is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # elfutils is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # This file is sourced by ". $srcdir/test-subr.sh" at the start of
20 # each test script.  It defines some functions they use and sets up
21 # canonical sh state for test runs.
22
23 set -e
24
25 #LC_ALL=C
26 #export LC_ALL
27
28 remove_files=
29 trap 'rm -f $remove_files' 0
30
31 tempfiles()
32 {
33   remove_files="$remove_files $*"
34 }
35
36 testfiles()
37 {
38   for file; do
39     bunzip2 -c $srcdir/${file}.bz2 > ${file} 2>/dev/null || exit 77
40     remove_files="$remove_files $file"
41   done
42 }
43
44 testrun_out()
45 {
46   outfile="$1"
47   shift
48   remove_files="$remove_files $outfile"
49   testrun "$@" > $outfile 2>&1 || :
50 }
51
52 testrun_compare()
53 {
54   outfile="${1##*/}.out"
55   testrun_out $outfile "$@"
56   diff -u $outfile -
57   # diff's exit status will kill the script.
58 }
59
60 test_cleanup()
61 {
62   rm -f $remove_files
63   remove_files=
64 }
65
66 # See test-wrapper.sh, which sets the environment for this.
67 testrun()
68 {
69   ${elfutils_testrun}_testrun "$@"
70 }
71
72 built_testrun()
73 {
74   LD_LIBRARY_PATH="${built_library_path}${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH"\
75   "$@"
76 }
77
78 installed_testrun()
79 {
80   program="$1"
81   shift
82   case "$program" in
83   ./*)
84     if [ "x$elfutils_tests_rpath" != xno ]; then
85       echo >&2 installcheck not possible with --enable-tests-rpath
86       exit 77
87     fi
88     ;;
89   ../*)
90     program=${bindir}/`program_transform ${program##*/}`
91     ;;
92   esac
93   if [ "${libdir}" != /usr/lib ] && [ "${libdir}" != /usr/lib64 ]; then
94     LD_LIBRARY_PATH="${libdir}:${libdir}/elfutils\
95 ${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH" \
96     $program ${1+"$@"}
97   else
98     $program ${1+"$@"}
99   fi
100 }
101
102 program_transform()
103 {
104   echo "$*" | sed "${program_transform_name}"
105 }
106
107 self_test_files=`echo ../src/addr2line ../src/elfcmp ../src/elflint \
108 ../src/findtextrel ../src/ld ../src/nm ../src/objdump ../src/readelf \
109 ../src/size ../src/strip ../libelf/libelf.so ../libdw/libdw.so \
110 ../libasm/libasm.so ../backends/libebl_*.so`
111
112 # Provide a command to run on all self-test files with testrun.
113 testrun_on_self()
114 {
115   exit_status=0
116
117   for file in $self_test_files; do
118       testrun $* $file \
119           || { echo "*** failure in $* $file"; exit_status=1; }
120   done
121
122   # Only exit if something failed
123   if test $exit_status != 0; then exit $exit_status; fi
124 }
125
126 # Same as above, but redirects stdout to /dev/null
127 testrun_on_self_quiet()
128 {
129   exit_status=0
130
131   for file in $self_test_files; do
132       testrun $* $file > /dev/null \
133           || { echo "*** failure in $* $file"; exit_status=1; }
134   done
135
136   # Only exit if something failed
137   if test $exit_status != 0; then exit $exit_status; fi
138 }