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