1 # source this file; set up for tests
3 # Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 # This program 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.
10 # This program is distributed in the hope that it will be useful,
11 # but 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.
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/>.
18 # Using this file in a test
19 # =========================
21 # The typical skeleton of a test looks like this:
24 # . "${srcdir=.}/init.sh"; path_prepend_ .
25 # Execute some commands.
26 # Note that these commands are executed in a subdirectory, therefore you
27 # need to prepend "../" to relative filenames in the build directory.
28 # Note that the "path_prepend_ ." is useful only if the body of your
29 # test invokes programs residing in the initial directory.
30 # For example, if the programs you want to test are in src/, and this test
31 # script is named tests/test-1, then you would use "path_prepend_ ../src",
32 # or perhaps export PATH='$(abs_top_builddir)/src$(PATH_SEPARATOR)'"$$PATH"
33 # to all tests via automake's TESTS_ENVIRONMENT.
34 # Set the exit code 0 for success, 77 for skipped, or 1 or other for failure.
35 # Use the skip_ and fail_ functions to print a diagnostic and then exit
36 # with the corresponding exit code.
39 # Executing a test that uses this file
40 # ====================================
42 # Running a single test:
43 # $ make check TESTS=test-foo.sh
45 # Running a single test, with verbose output:
46 # $ make check TESTS=test-foo.sh VERBOSE=yes
48 # Running a single test, with single-stepping:
49 # 1. Go into a sub-shell:
51 # 2. Set relevant environment variables from TESTS_ENVIRONMENT in the
53 # $ export srcdir=../../tests # this is an example
54 # 3. Execute the commands from the test, copy&pasting them one by one:
55 # $ . "$srcdir/init.sh"; path_prepend_ .
60 # We require $(...) support unconditionally.
61 # We require a few additional shell features only when $EXEEXT is nonempty,
62 # in order to support automatic $EXEEXT emulation:
63 # - hyphen-containing alias names
64 # - we prefer to use ${var#...} substitution, rather than having
65 # to work around lack of support for that feature.
66 # The following code attempts to find a shell with support for these features.
67 # If the current shell passes the test, we're done. Otherwise, test other
68 # shells until we find one that passes. If one is found, re-exec it.
69 # If no acceptable shell is found, skip the current test.
71 # Use "9" to indicate success (rather than 0), in case some shell acts
72 # like Solaris 10's /bin/sh but exits successfully instead of with status 2.
74 gl_shell_test_script_='
75 test $(echo y) = y || exit 1
76 test -z "$EXEEXT" && exit 9
77 shopt -s expand_aliases
82 && test $(a-b) = zoo \
86 if test "x$1" = "x--no-reexec"; then
89 # 'eval'ing the above code makes Solaris 10's /bin/sh exit with $? set to 2.
90 # It does not evaluate any of the code after the "unexpected" `('. Thus,
91 # we must run it in a subshell.
92 ( eval "$gl_shell_test_script_" ) > /dev/null 2>&1
94 for re_shell_ in "${CONFIG_SHELL:-no_shell}" /bin/sh bash dash zsh pdksh fail
96 test "$re_shell_" = no_shell && continue
97 test "$re_shell_" = fail && skip_ failed to find an adequate shell
98 "$re_shell_" -c "$gl_shell_test_script_" 2>/dev/null
100 exec "$re_shell_" "$0" --no-reexec "$@"
101 echo "$ME_: exec failed" 1>&2
108 test -n "$EXEEXT" && shopt -s expand_aliases
110 # Enable glibc's malloc-perturbing option.
111 # This is cheap and useful for exposing code that depends on the fact that
112 # malloc-related functions often return memory that is mostly zeroed.
113 # If you have the time and cycles, use valgrind to do an even better job.
114 : ${MALLOC_PERTURB_=87}
115 export MALLOC_PERTURB_
117 # We use a trap below for cleanup. This requires us to go through
118 # hoops to get the right exit status transported through the handler.
119 # So use `Exit STATUS' instead of `exit STATUS' inside of the tests.
120 # Turn off errexit here so that we don't trip the bug with OSF1/Tru64
121 # sh inside this function.
122 Exit () { set +e; (exit $1); exit $1; }
124 # Print warnings (e.g., about skipped and failed tests) to this file number.
125 # Override by defining to say, 9, in init.cfg, and putting say,
126 # "export ...ENVVAR_SETTINGS...; exec 9>&2; $(SHELL)" in the definition
127 # of TESTS_ENVIRONMENT in your tests/Makefile.am file.
128 # This is useful when using automake's parallel tests mode, to print
129 # the reason for skip/failure to console, rather than to the .log files.
130 : ${stderr_fileno_=2}
132 warn_() { echo "$@" 1>&$stderr_fileno_; }
133 fail_() { warn_ "$ME_: failed test: $@"; Exit 1; }
134 skip_() { warn_ "$ME_: skipped test: $@"; Exit 77; }
135 framework_failure_() { warn_ "$ME_: set-up failure: $@"; Exit 1; }
137 # This is a stub function that is run upon trap (upon regular exit and
138 # interrupt). Override it with a per-test function, e.g., to unmount
139 # a partition, or to undo any other global state changes.
142 if ( diff --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
143 compare() { diff -u "$@"; }
144 elif ( cmp --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
145 compare() { cmp -s "$@"; }
147 compare() { cmp "$@"; }
150 # An arbitrary prefix to help distinguish test directories.
151 testdir_prefix_() { printf gt; }
153 # Run the user-overridable cleanup_ function, remove the temporary
154 # directory and exit with the incoming value of $?.
159 # cd out of the directory we're about to remove
160 cd "$initial_cwd_" || cd / || cd /tmp
161 chmod -R u+rwx "$test_dir_"
162 # If removal fails and exit status was to be 0, then change it to 1.
163 rm -rf "$test_dir_" || { test $__st = 0 && __st=1; }
167 # Given a directory name, DIR, if every entry in it that matches *.exe
168 # contains only the specified bytes (see the case stmt below), then print
169 # a space-separated list of those names and return 0. Otherwise, don't
170 # print anything and return 1. Naming constraints apply also to DIR.
171 find_exe_basenames_()
177 for feb_file_ in $feb_dir_/*.exe; do
179 *[!-a-zA-Z/0-9_.+]*) feb_fail_=1; break;;
180 *) # Remove leading file name components as well as the .exe suffix.
181 feb_file_=${feb_file_##*/}
182 feb_file_=${feb_file_%.exe}
183 feb_result_="$feb_result_$feb_sp_$feb_file_";;
187 test $feb_fail_ = 0 && printf %s "$feb_result_"
191 # Consider the files in directory, $1.
192 # For each file name of the form PROG.exe, create an alias named
193 # PROG that simply invokes PROG.exe, then return 0. If any selected
194 # file name or the directory name, $1, contains an unexpected character,
195 # define no function and return 1.
201 *) echo "$0: unexpected \$EXEEXT value: $EXEEXT" 1>&2; return 1 ;;
204 base_names_=`find_exe_basenames_ $1` \
205 || { echo "$0 (exe_shim): skipping directory: $1" 1>&2; return 1; }
207 if test -n "$base_names_"; then
208 for base_ in $base_names_; do
209 alias "$base_"="$base_$EXEEXT"
216 # Use this function to prepend to PATH an absolute name for each
217 # specified, possibly-$initial_cwd_-relative, directory.
220 while test $# != 0; do
223 '') fail_ "invalid path dir: '$1'";;
224 /*) abs_path_dir_=$path_dir_;;
225 *) abs_path_dir_=`cd "$initial_cwd_/$path_dir_" && echo "$PWD"` \
226 || fail_ "invalid path dir: $path_dir_";;
228 case $abs_path_dir_ in
229 *:*) fail_ "invalid path dir: '$abs_path_dir_'";;
231 PATH="$abs_path_dir_:$PATH"
233 # Create an alias, FOO, for each FOO.exe in this directory.
234 create_exe_shims_ "$abs_path_dir_" \
235 || fail_ "something failed (above): $abs_path_dir_"
243 test "$VERBOSE" = yes && set -x
246 ME_=`expr "./$0" : '.*/\(.*\)$'`
248 pfx_=`testdir_prefix_`
249 test_dir_=`mktempd_ "$initial_cwd_" "$pfx_-$ME_.XXXX"` \
250 || fail_ "failed to create temporary directory in $initial_cwd_"
253 # These trap statements ensure that the temporary directory, $test_dir_,
254 # is removed upon exit as well as upon receipt of any of the listed signals.
256 for sig_ in 1 2 3 13 15; do
257 eval "trap 'Exit $(expr $sig_ + 128)' $sig_"
261 # Create a temporary directory, much like mktemp -d does.
262 # Written by Jim Meyering.
264 # Usage: mktempd_ /tmp phoey.XXXXXXXXXX
266 # First, try to use the mktemp program.
267 # Failing that, we'll roll our own mktemp-like function:
268 # - try to get random bytes from /dev/urandom
269 # - failing that, generate output from a combination of quickly-varying
270 # sources and gzip. Ignore non-varying gzip header, and extract
271 # "random" bits from there.
272 # - given those bits, map to file-name bytes using tr, and try to create
273 # the desired directory.
274 # - make only $MAX_TRIES_ attempts
276 # Helper function. Print $N pseudo-random bytes from a-zA-Z0-9.
281 # Maybe try openssl rand -base64 $n_prime_|tr '+/=\012' abcd first?
282 # But if they have openssl, they probably have mktemp, too.
284 chars_=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
285 dev_rand_=/dev/urandom
286 if test -r "$dev_rand_"; then
287 # Note: 256-length($chars_) == 194; 3 copies of $chars_ is 186 + 8 = 194.
288 dd ibs=$n_ count=1 if=$dev_rand_ 2>/dev/null \
289 | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
293 n_plus_50_=`expr $n_ + 50`
294 cmds_='date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
295 data_=` (eval "$cmds_") 2>&1 | gzip `
297 # Ensure that $data_ has length at least 50+$n_
299 len_=`echo "$data_"|wc -c`
300 test $n_plus_50_ -le $len_ && break;
301 data_=` (echo "$data_"; eval "$cmds_") 2>&1 | gzip `
305 | dd bs=1 skip=50 count=$n_ 2>/dev/null \
306 | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
313 *) fail_ "Usage: $ME DIR TEMPLATE";;
321 # Disallow any trailing slash on specified destdir:
322 # it would subvert the post-mktemp "case"-based destdir test.
325 */) fail_ "invalid destination dir: remove trailing slash(es)";;
330 *) fail_ "invalid template: $template_ (must have a suffix of at least 4 X's)";;
335 # First, try to use mktemp.
336 d=`unset TMPDIR; mktemp -d -t -p "$destdir_" "$template_" 2>/dev/null` \
339 # The resulting name must be in the specified directory.
340 case $d in "$destdir_"*);; *) fail=1;; esac
342 # It must have created the directory.
343 test -d "$d" || fail=1
345 # It must have 0700 permissions. Handle sticky "S" bits.
346 perms=`ls -dgo "$d" 2>/dev/null|tr S -` || fail=1
347 case $perms in drwx------*) ;; *) fail=1;; esac
354 # If we reach this point, we'll have to create a directory manually.
356 # Get a copy of the template without its suffix of X's.
357 base_template_=`echo "$template_"|sed 's/XX*$//'`
359 # Calculate how many X's we've just removed.
360 template_length_=`echo "$template_" | wc -c`
361 nx_=`echo "$base_template_" | wc -c`
362 nx_=`expr $template_length_ - $nx_`
367 X_=`rand_bytes_ $nx_`
368 candidate_dir_="$destdir_/$base_template_$X_"
369 err_=`mkdir -m 0700 "$candidate_dir_" 2>&1` \
370 && { echo "$candidate_dir_"; return; }
371 test $MAX_TRIES_ -le $i_ && break;
377 # If you want to override the testdir_prefix_ function,
378 # or to add more utility functions, use this file.
379 test -f "$srcdir/init.cfg" \
380 && . "$srcdir/init.cfg"