1#! /bin/sh
2# Copyright (C) 2011-2021 Free Software Foundation, Inc.
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2, or (at your option)
7# any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17# TAP support:
18#  - a test script terminated by a signal causes an hard error
19
20. test-init.sh
21
22fetch_tap_driver
23
24plan_ 10
25
26cat >> configure.ac <<END
27AC_OUTPUT
28END
29
30cat > Makefile.am << END
31TEST_LOG_DRIVER = \$(srcdir)/tap-driver
32TEST_LOG_COMPILER = $PERL -w
33## Will be updated later.
34TESTS =
35END
36
37all_signals='1 2 3 9 13 15'
38blocked_signals=''
39for sig in $all_signals; do
40  # Ignore blocked signals
41  if is_blocked_signal $sig; then
42    blocked_signals="$blocked_signals $sig"
43    continue
44  fi
45  # Write the dummy test scripts in perl, not as shell scripts, to work
46  # around unportabilities in the handling of signals (in fact, even
47  # with bash, the older script were unable to properly deliver a SIGQUIT
48  # to themselves consistently).  The shebang is dummy here, as we prefer
49  # to rely on the definition of TEST_LOG_COMPILER instead.
50  unindent > signal-$sig.test <<END
51    #! /usr/bin/env perl
52    # We need autoflush to avoid losing output, which could cause spurious
53    # "no test plan seen" in the TAP driver.
54    BEGIN { $| = 1 }
55    use warnings FATAL => "all";
56    print "1..1\\n";
57    print "ok 1\\n";
58    kill $sig, \$\$;
59    print "Bail out! \$0 not killed?\\n";
60END
61  echo TESTS += signal-$sig.test >> Makefile.am
62done
63results_count=$(ls *.test | wc -l | tr -d "$tab$sp")
64
65chmod a+x *.test
66
67$ACLOCAL
68$AUTOCONF
69$AUTOMAKE
70
71./configure
72
73system=$(uname -s -r || echo unknown) # Needed later.
74
75signal_caught ()
76{
77  numeric=$1
78  case $numeric in
79     1) symbolic=HUP;;
80     2) symbolic=INT;;
81     3) symbolic=QUIT;;
82     9) symbolic=KILL;;
83    13) symbolic=PIPE;;
84    15) symbolic=TERM;;
85     *) fatal_ "unexpected signal number '$numeric'"
86  esac
87  # Sending a SIGQUIT on Cygwin 1.5 can cause a segmentation fault
88  # instead (sometimes).  Don't let this older bug pollute the results
89  # of our testsuite.
90  case $numeric,$system in
91    3,CYGWIN*\ 1.5.*) sig_re="((SIG)?($symbolic|SEGV)|$numeric|11)";;
92    *) sig_re="((SIG)?$symbolic|$numeric)";;
93  esac
94  wbound_re="($|[^a-zA-Z0-9_-])"
95  pfx_re="^ERROR: signal-$numeric\\.test"
96  rx="${pfx_re} .*terminated by signal ${sig_re}${wbound_re}"
97  desc="TAP driver catch test termination by signal SIG${symbolic}"
98  case " $blocked_signals " in
99    *" $numeric "*) skip_ -r "SIG$symbolic is blocked" "$desc" ;;
100    *) command_ok_ "$desc" env LC_ALL=C $EGREP "$rx" stdout ;;
101  esac
102}
103
104command_ok_ '"make check" fails' eval '
105  (
106    run_make -e IGNORE -O check
107    # Extra "echo" and silencing of xtraces required to avoid possible
108    # garbled output with NetBSD make, which would miss some final
109    # newlines in the expected places and thus mess up our TAP output.
110    set +x; echo
111    test $am_make_rc -gt 0
112  )
113'
114cat stdout # For debugging.
115
116command_ok_ "count of test results" count_test_results \
117  total=$(($results_count * 2)) \
118  pass=$results_count error=$results_count \
119  fail=0 xpass=0 xfail=0 skip=0
120
121for sig in $all_signals; do
122  signal_caught $sig
123done
124
125echo 'TEST_LOG_DRIVER_FLAGS = --ignore-exit' >> Makefile
126
127command_ok_ '"make check" passes [--ignore-exit]' run_make -O check
128
129command_ok_ "count of test results [--ignore-exit]" count_test_results \
130  total=$results_count pass=$results_count \
131  fail=0 xpass=0 xfail=0 skip=0 error=0
132
133:
134