New upstream version 1.8
[debian/gzip] / tests / init.sh
1 # source this file; set up for tests
2
3 # Copyright (C) 2009-2016 Free Software Foundation, Inc.
4
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.
9
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.
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 # Using this file in a test
19 # =========================
20 #
21 # The typical skeleton of a test looks like this:
22 #
23 #   #!/bin/sh
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.
37 #   Exit $?
38
39 # Executing a test that uses this file
40 # ====================================
41 #
42 # Running a single test:
43 #   $ make check TESTS=test-foo.sh
44 #
45 # Running a single test, with verbose output:
46 #   $ make check TESTS=test-foo.sh VERBOSE=yes
47 #
48 # Running a single test, with single-stepping:
49 #   1. Go into a sub-shell:
50 #   $ bash
51 #   2. Set relevant environment variables from TESTS_ENVIRONMENT in the
52 #      Makefile:
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_ .
56 #   ...
57 #   4. Finally
58 #   $ exit
59
60 ME_=`expr "./$0" : '.*/\(.*\)$'`
61
62 # We use a trap below for cleanup.  This requires us to go through
63 # hoops to get the right exit status transported through the handler.
64 # So use 'Exit STATUS' instead of 'exit STATUS' inside of the tests.
65 # Turn off errexit here so that we don't trip the bug with OSF1/Tru64
66 # sh inside this function.
67 Exit () { set +e; (exit $1); exit $1; }
68
69 # Print warnings (e.g., about skipped and failed tests) to this file number.
70 # Override by defining to say, 9, in init.cfg, and putting say,
71 #   export ...ENVVAR_SETTINGS...; $(SHELL) 9>&2
72 # in the definition of TESTS_ENVIRONMENT in your tests/Makefile.am file.
73 # This is useful when using automake's parallel tests mode, to print
74 # the reason for skip/failure to console, rather than to the .log files.
75 : ${stderr_fileno_=2}
76
77 # Note that correct expansion of "$*" depends on IFS starting with ' '.
78 # Always write the full diagnostic to stderr.
79 # When stderr_fileno_ is not 2, also emit the first line of the
80 # diagnostic to that file descriptor.
81 warn_ ()
82 {
83   # If IFS does not start with ' ', set it and emit the warning in a subshell.
84   case $IFS in
85     ' '*) printf '%s\n' "$*" >&2
86           test $stderr_fileno_ = 2 \
87             || { printf '%s\n' "$*" | sed 1q >&$stderr_fileno_ ; } ;;
88     *) (IFS=' '; warn_ "$@");;
89   esac
90 }
91 fail_ () { warn_ "$ME_: failed test: $@"; Exit 1; }
92 skip_ () { warn_ "$ME_: skipped test: $@"; Exit 77; }
93 fatal_ () { warn_ "$ME_: hard error: $@"; Exit 99; }
94 framework_failure_ () { warn_ "$ME_: set-up failure: $@"; Exit 99; }
95
96 # This is used to simplify checking of the return value
97 # which is useful when ensuring a command fails as desired.
98 # I.e., just doing `command ... &&fail=1` will not catch
99 # a segfault in command for example.  With this helper you
100 # instead check an explicit exit code like
101 #   returns_ 1 command ... || fail
102 returns_ () {
103   # Disable tracing so it doesn't interfere with stderr of the wrapped command
104   { set +x; } 2>/dev/null
105
106   local exp_exit="$1"
107   shift
108   "$@"
109   test $? -eq $exp_exit && ret_=0 || ret_=1
110
111   if test "$VERBOSE" = yes && test "$gl_set_x_corrupts_stderr_" = false; then
112     set -x
113   fi
114   { return $ret_; } 2>/dev/null
115 }
116
117 # Sanitize this shell to POSIX mode, if possible.
118 DUALCASE=1; export DUALCASE
119 if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
120   emulate sh
121   NULLCMD=:
122   alias -g '${1+"$@"}'='"$@"'
123   setopt NO_GLOB_SUBST
124 else
125   case `(set -o) 2>/dev/null` in
126     *posix*) set -o posix ;;
127   esac
128 fi
129
130 # We require $(...) support unconditionally.
131 # We require a few additional shell features only when $EXEEXT is nonempty,
132 # in order to support automatic $EXEEXT emulation:
133 # - hyphen-containing alias names
134 # - we prefer to use ${var#...} substitution, rather than having
135 #   to work around lack of support for that feature.
136 # The following code attempts to find a shell with support for these features.
137 # If the current shell passes the test, we're done.  Otherwise, test other
138 # shells until we find one that passes.  If one is found, re-exec it.
139 # If no acceptable shell is found, skip the current test.
140 #
141 # The "...set -x; P=1 true 2>err..." test is to disqualify any shell that
142 # emits "P=1" into err, as /bin/sh from SunOS 5.11 and OpenBSD 4.7 do.
143 #
144 # Use "9" to indicate success (rather than 0), in case some shell acts
145 # like Solaris 10's /bin/sh but exits successfully instead of with status 2.
146
147 # Eval this code in a subshell to determine a shell's suitability.
148 # 10 - passes all tests; ok to use
149 #  9 - ok, but enabling "set -x" corrupts app stderr; prefer higher score
150 #  ? - not ok
151 gl_shell_test_script_='
152 test $(echo y) = y || exit 1
153 f_local_() { local v=1; }; f_local_ || exit 1
154 score_=10
155 if test "$VERBOSE" = yes; then
156   test -n "$( (exec 3>&1; set -x; P=1 true 2>&3) 2> /dev/null)" && score_=9
157 fi
158 test -z "$EXEEXT" && exit $score_
159 shopt -s expand_aliases
160 alias a-b="echo zoo"
161 v=abx
162      test ${v%x} = ab \
163   && test ${v#a} = bx \
164   && test $(a-b) = zoo \
165   && exit $score_
166 '
167
168 if test "x$1" = "x--no-reexec"; then
169   shift
170 else
171   # Assume a working shell.  Export to subshells (setup_ needs this).
172   gl_set_x_corrupts_stderr_=false
173   export gl_set_x_corrupts_stderr_
174
175   # Record the first marginally acceptable shell.
176   marginal_=
177
178   # Search for a shell that meets our requirements.
179   for re_shell_ in __current__ "${CONFIG_SHELL:-no_shell}" \
180       /bin/sh bash dash zsh pdksh fail
181   do
182     test "$re_shell_" = no_shell && continue
183
184     # If we've made it all the way to the sentinel, "fail" without
185     # finding even a marginal shell, skip this test.
186     if test "$re_shell_" = fail; then
187       test -z "$marginal_" && skip_ failed to find an adequate shell
188       re_shell_=$marginal_
189       break
190     fi
191
192     # When testing the current shell, simply "eval" the test code.
193     # Otherwise, run it via $re_shell_ -c ...
194     if test "$re_shell_" = __current__; then
195       # 'eval'ing this code makes Solaris 10's /bin/sh exit with
196       # $? set to 2.  It does not evaluate any of the code after the
197       # "unexpected" first '('.  Thus, we must run it in a subshell.
198       ( eval "$gl_shell_test_script_" ) > /dev/null 2>&1
199     else
200       "$re_shell_" -c "$gl_shell_test_script_" 2>/dev/null
201     fi
202
203     st_=$?
204
205     # $re_shell_ works just fine.  Use it.
206     if test $st_ = 10; then
207       gl_set_x_corrupts_stderr_=false
208       break
209     fi
210
211     # If this is our first marginally acceptable shell, remember it.
212     if test "$st_:$marginal_" = 9: ; then
213       marginal_="$re_shell_"
214       gl_set_x_corrupts_stderr_=true
215     fi
216   done
217
218   if test "$re_shell_" != __current__; then
219     # Found a usable shell.  Preserve -v and -x.
220     case $- in
221       *v*x* | *x*v*) opts_=-vx ;;
222       *v*) opts_=-v ;;
223       *x*) opts_=-x ;;
224       *) opts_= ;;
225     esac
226     re_shell=$re_shell_
227     export re_shell
228     exec "$re_shell_" $opts_ "$0" --no-reexec "$@"
229     echo "$ME_: exec failed" 1>&2
230     exit 127
231   fi
232 fi
233
234 # If this is bash, turn off all aliases.
235 test -n "$BASH_VERSION" && unalias -a
236
237 # Note that when supporting $EXEEXT (transparently mapping from PROG_NAME to
238 # PROG_NAME.exe), we want to support hyphen-containing names like test-acos.
239 # That is part of the shell-selection test above.  Why use aliases rather
240 # than functions?  Because support for hyphen-containing aliases is more
241 # widespread than that for hyphen-containing function names.
242 test -n "$EXEEXT" && shopt -s expand_aliases
243
244 # Enable glibc's malloc-perturbing option.
245 # This is useful for exposing code that depends on the fact that
246 # malloc-related functions often return memory that is mostly zeroed.
247 # If you have the time and cycles, use valgrind to do an even better job.
248 : ${MALLOC_PERTURB_=87}
249 export MALLOC_PERTURB_
250
251 # This is a stub function that is run upon trap (upon regular exit and
252 # interrupt).  Override it with a per-test function, e.g., to unmount
253 # a partition, or to undo any other global state changes.
254 cleanup_ () { :; }
255
256 # Emit a header similar to that from diff -u;  Print the simulated "diff"
257 # command so that the order of arguments is clear.  Don't bother with @@ lines.
258 emit_diff_u_header_ ()
259 {
260   printf '%s\n' "diff -u $*" \
261     "--- $1     1970-01-01" \
262     "+++ $2     1970-01-01"
263 }
264
265 # Arrange not to let diff or cmp operate on /dev/null,
266 # since on some systems (at least OSF/1 5.1), that doesn't work.
267 # When there are not two arguments, or no argument is /dev/null, return 2.
268 # When one argument is /dev/null and the other is not empty,
269 # cat the nonempty file to stderr and return 1.
270 # Otherwise, return 0.
271 compare_dev_null_ ()
272 {
273   test $# = 2 || return 2
274
275   if test "x$1" = x/dev/null; then
276     test -s "$2" || return 0
277     emit_diff_u_header_ "$@"; sed 's/^/+/' "$2"
278     return 1
279   fi
280
281   if test "x$2" = x/dev/null; then
282     test -s "$1" || return 0
283     emit_diff_u_header_ "$@"; sed 's/^/-/' "$1"
284     return 1
285   fi
286
287   return 2
288 }
289
290 if diff_out_=`exec 2>/dev/null; diff -u "$0" "$0" < /dev/null` \
291    && diff -u Makefile "$0" 2>/dev/null | grep '^[+]#!' >/dev/null; then
292   # diff accepts the -u option and does not (like AIX 7 'diff') produce an
293   # extra space on column 1 of every content line.
294   if test -z "$diff_out_"; then
295     compare_ () { diff -u "$@"; }
296   else
297     compare_ ()
298     {
299       if diff -u "$@" > diff.out; then
300         # No differences were found, but Solaris 'diff' produces output
301         # "No differences encountered". Hide this output.
302         rm -f diff.out
303         true
304       else
305         cat diff.out
306         rm -f diff.out
307         false
308       fi
309     }
310   fi
311 elif
312   for diff_opt_ in -U3 -c '' no; do
313     test "$diff_opt_" = no && break
314     diff_out_=`exec 2>/dev/null; diff $diff_opt_ "$0" "$0" </dev/null` && break
315   done
316   test "$diff_opt_" != no
317 then
318   if test -z "$diff_out_"; then
319     compare_ () { diff $diff_opt_ "$@"; }
320   else
321     compare_ ()
322     {
323       if diff $diff_opt_ "$@" > diff.out; then
324         # No differences were found, but AIX and HP-UX 'diff' produce output
325         # "No differences encountered" or "There are no differences between the
326         # files.". Hide this output.
327         rm -f diff.out
328         true
329       else
330         cat diff.out
331         rm -f diff.out
332         false
333       fi
334     }
335   fi
336 elif cmp -s /dev/null /dev/null 2>/dev/null; then
337   compare_ () { cmp -s "$@"; }
338 else
339   compare_ () { cmp "$@"; }
340 fi
341
342 # Usage: compare EXPECTED ACTUAL
343 #
344 # Given compare_dev_null_'s preprocessing, defer to compare_ if 2 or more.
345 # Otherwise, propagate $? to caller: any diffs have already been printed.
346 compare ()
347 {
348   # This looks like it can be factored to use a simple "case $?"
349   # after unchecked compare_dev_null_ invocation, but that would
350   # fail in a "set -e" environment.
351   if compare_dev_null_ "$@"; then
352     return 0
353   else
354     case $? in
355       1) return 1;;
356       *) compare_ "$@";;
357     esac
358   fi
359 }
360
361 # An arbitrary prefix to help distinguish test directories.
362 testdir_prefix_ () { printf gt; }
363
364 # Run the user-overridable cleanup_ function, remove the temporary
365 # directory and exit with the incoming value of $?.
366 remove_tmp_ ()
367 {
368   __st=$?
369   cleanup_
370   # cd out of the directory we're about to remove
371   cd "$initial_cwd_" || cd / || cd /tmp
372   chmod -R u+rwx "$test_dir_"
373   # If removal fails and exit status was to be 0, then change it to 1.
374   rm -rf "$test_dir_" || { test $__st = 0 && __st=1; }
375   exit $__st
376 }
377
378 # Given a directory name, DIR, if every entry in it that matches *.exe
379 # contains only the specified bytes (see the case stmt below), then print
380 # a space-separated list of those names and return 0.  Otherwise, don't
381 # print anything and return 1.  Naming constraints apply also to DIR.
382 find_exe_basenames_ ()
383 {
384   feb_dir_=$1
385   feb_fail_=0
386   feb_result_=
387   feb_sp_=
388   for feb_file_ in $feb_dir_/*.exe; do
389     # If there was no *.exe file, or there existed a file named "*.exe" that
390     # was deleted between the above glob expansion and the existence test
391     # below, just skip it.
392     test "x$feb_file_" = "x$feb_dir_/*.exe" && test ! -f "$feb_file_" \
393       && continue
394     # Exempt [.exe, since we can't create a function by that name, yet
395     # we can't invoke [ by PATH search anyways due to shell builtins.
396     test "x$feb_file_" = "x$feb_dir_/[.exe" && continue
397     case $feb_file_ in
398       *[!-a-zA-Z/0-9_.+]*) feb_fail_=1; break;;
399       *) # Remove leading file name components as well as the .exe suffix.
400          feb_file_=${feb_file_##*/}
401          feb_file_=${feb_file_%.exe}
402          feb_result_="$feb_result_$feb_sp_$feb_file_";;
403     esac
404     feb_sp_=' '
405   done
406   test $feb_fail_ = 0 && printf %s "$feb_result_"
407   return $feb_fail_
408 }
409
410 # Consider the files in directory, $1.
411 # For each file name of the form PROG.exe, create an alias named
412 # PROG that simply invokes PROG.exe, then return 0.  If any selected
413 # file name or the directory name, $1, contains an unexpected character,
414 # define no alias and return 1.
415 create_exe_shims_ ()
416 {
417   case $EXEEXT in
418     '') return 0 ;;
419     .exe) ;;
420     *) echo "$0: unexpected \$EXEEXT value: $EXEEXT" 1>&2; return 1 ;;
421   esac
422
423   base_names_=`find_exe_basenames_ $1` \
424     || { echo "$0 (exe_shim): skipping directory: $1" 1>&2; return 0; }
425
426   if test -n "$base_names_"; then
427     for base_ in $base_names_; do
428       alias "$base_"="$base_$EXEEXT"
429     done
430   fi
431
432   return 0
433 }
434
435 # Use this function to prepend to PATH an absolute name for each
436 # specified, possibly-$initial_cwd_-relative, directory.
437 path_prepend_ ()
438 {
439   while test $# != 0; do
440     path_dir_=$1
441     case $path_dir_ in
442       '') fail_ "invalid path dir: '$1'";;
443       /*) abs_path_dir_=$path_dir_;;
444       *) abs_path_dir_=$initial_cwd_/$path_dir_;;
445     esac
446     case $abs_path_dir_ in
447       *:*) fail_ "invalid path dir: '$abs_path_dir_'";;
448     esac
449     PATH="$abs_path_dir_:$PATH"
450
451     # Create an alias, FOO, for each FOO.exe in this directory.
452     create_exe_shims_ "$abs_path_dir_" \
453       || fail_ "something failed (above): $abs_path_dir_"
454     shift
455   done
456   export PATH
457 }
458
459 setup_ ()
460 {
461   if test "$VERBOSE" = yes; then
462     # Test whether set -x may cause the selected shell to corrupt an
463     # application's stderr.  Many do, including zsh-4.3.10 and the /bin/sh
464     # from SunOS 5.11, OpenBSD 4.7 and Irix 5.x and 6.5.
465     # If enabling verbose output this way would cause trouble, simply
466     # issue a warning and refrain.
467     if $gl_set_x_corrupts_stderr_; then
468       warn_ "using SHELL=$SHELL with 'set -x' corrupts stderr"
469     else
470       set -x
471     fi
472   fi
473
474   initial_cwd_=$PWD
475
476   pfx_=`testdir_prefix_`
477   test_dir_=`mktempd_ "$initial_cwd_" "$pfx_-$ME_.XXXX"` \
478     || fail_ "failed to create temporary directory in $initial_cwd_"
479   cd "$test_dir_" || fail_ "failed to cd to temporary directory"
480
481   # As autoconf-generated configure scripts do, ensure that IFS
482   # is defined initially, so that saving and restoring $IFS works.
483   gl_init_sh_nl_='
484 '
485   IFS=" ""      $gl_init_sh_nl_"
486
487   # This trap statement, along with a trap on 0 below, ensure that the
488   # temporary directory, $test_dir_, is removed upon exit as well as
489   # upon receipt of any of the listed signals.
490   for sig_ in 1 2 3 13 15; do
491     eval "trap 'Exit $(expr $sig_ + 128)' $sig_"
492   done
493 }
494
495 # Create a temporary directory, much like mktemp -d does.
496 # Written by Jim Meyering.
497 #
498 # Usage: mktempd_ /tmp phoey.XXXXXXXXXX
499 #
500 # First, try to use the mktemp program.
501 # Failing that, we'll roll our own mktemp-like function:
502 #  - try to get random bytes from /dev/urandom
503 #  - failing that, generate output from a combination of quickly-varying
504 #      sources and gzip.  Ignore non-varying gzip header, and extract
505 #      "random" bits from there.
506 #  - given those bits, map to file-name bytes using tr, and try to create
507 #      the desired directory.
508 #  - make only $MAX_TRIES_ attempts
509
510 # Helper function.  Print $N pseudo-random bytes from a-zA-Z0-9.
511 rand_bytes_ ()
512 {
513   n_=$1
514
515   # Maybe try openssl rand -base64 $n_prime_|tr '+/=\012' abcd first?
516   # But if they have openssl, they probably have mktemp, too.
517
518   chars_=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
519   dev_rand_=/dev/urandom
520   if test -r "$dev_rand_"; then
521     # Note: 256-length($chars_) == 194; 3 copies of $chars_ is 186 + 8 = 194.
522     dd ibs=$n_ count=1 if=$dev_rand_ 2>/dev/null \
523       | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
524     return
525   fi
526
527   n_plus_50_=`expr $n_ + 50`
528   cmds_='date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
529   data_=` (eval "$cmds_") 2>&1 | gzip `
530
531   # Ensure that $data_ has length at least 50+$n_
532   while :; do
533     len_=`echo "$data_"|wc -c`
534     test $n_plus_50_ -le $len_ && break;
535     data_=` (echo "$data_"; eval "$cmds_") 2>&1 | gzip `
536   done
537
538   echo "$data_" \
539     | dd bs=1 skip=50 count=$n_ 2>/dev/null \
540     | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
541 }
542
543 mktempd_ ()
544 {
545   case $# in
546   2);;
547   *) fail_ "Usage: mktempd_ DIR TEMPLATE";;
548   esac
549
550   destdir_=$1
551   template_=$2
552
553   MAX_TRIES_=4
554
555   # Disallow any trailing slash on specified destdir:
556   # it would subvert the post-mktemp "case"-based destdir test.
557   case $destdir_ in
558   / | //) destdir_slash_=$destdir;;
559   */) fail_ "invalid destination dir: remove trailing slash(es)";;
560   *) destdir_slash_=$destdir_/;;
561   esac
562
563   case $template_ in
564   *XXXX) ;;
565   *) fail_ \
566        "invalid template: $template_ (must have a suffix of at least 4 X's)";;
567   esac
568
569   # First, try to use mktemp.
570   d=`unset TMPDIR; { mktemp -d -t -p "$destdir_" "$template_"; } 2>/dev/null` &&
571
572   # The resulting name must be in the specified directory.
573   case $d in "$destdir_slash_"*) :;; *) false;; esac &&
574
575   # It must have created the directory.
576   test -d "$d" &&
577
578   # It must have 0700 permissions.  Handle sticky "S" bits.
579   perms=`ls -dgo "$d" 2>/dev/null` &&
580   case $perms in drwx--[-S]---*) :;; *) false;; esac && {
581     echo "$d"
582     return
583   }
584
585   # If we reach this point, we'll have to create a directory manually.
586
587   # Get a copy of the template without its suffix of X's.
588   base_template_=`echo "$template_"|sed 's/XX*$//'`
589
590   # Calculate how many X's we've just removed.
591   template_length_=`echo "$template_" | wc -c`
592   nx_=`echo "$base_template_" | wc -c`
593   nx_=`expr $template_length_ - $nx_`
594
595   err_=
596   i_=1
597   while :; do
598     X_=`rand_bytes_ $nx_`
599     candidate_dir_="$destdir_slash_$base_template_$X_"
600     err_=`mkdir -m 0700 "$candidate_dir_" 2>&1` \
601       && { echo "$candidate_dir_"; return; }
602     test $MAX_TRIES_ -le $i_ && break;
603     i_=`expr $i_ + 1`
604   done
605   fail_ "$err_"
606 }
607
608 # If you want to override the testdir_prefix_ function,
609 # or to add more utility functions, use this file.
610 test -f "$srcdir/init.cfg" \
611   && . "$srcdir/init.cfg"
612
613 setup_ "$@"
614 # This trap is here, rather than in the setup_ function, because some
615 # shells run the exit trap at shell function exit, rather than script exit.
616 trap remove_tmp_ 0