source: mod_gnutls/test/runtests @ 5f3222b

debian/masterdebian/stretch-backportsproxy-ticketupstream
Last change on this file since 5f3222b was 5f3222b, checked in by Thomas Klute <thomas2.klute@…>, 3 years ago

Test suite: Use "caller" builtin and BASH_COMMAND for error traces

  • Property mode set to 100755
File size: 7.4 KB
RevLine 
[4b53371]1#!/bin/bash
2
[ae38a49]3# Authors:
4# Daniel Kahn Gillmor <dkg@fifthhorseman.net>
5# Thomas Klute <thomas2.klute@uni-dortmund.de>
[4b53371]6
7set -e
[412ee84]8. ${srcdir}/common.bash
[cf4e708]9netns_reexec ${@}
[4b53371]10
[56056de]11testid="${1##t-}"
12
13if [ -z "$testid" ] ; then
14    echo -e "No test case selected.\nUsage: ${0} t-N" >&2
15    exit 1
16else
17    testid=${srcdir}/tests/"$(printf "%02d" "$testid")"_*
18fi
[4b53371]19
20BADVARS=0
[6c030c1]21for v in APACHE2 TEST_HOST TEST_PORT TEST_QUERY_TIMEOUT TEST_MSVA_WAIT \
[412ee84]22                 MSVA_PORT; do
[4b53371]23    if [ ! -v "$v" ]; then
24        printf "You need to set the %s environment variable\n" "$v" >&2
25        BADVARS=1
26    fi
27done
28
29if [ 0 != "$BADVARS" ]; then
30    exit 1
31fi
32
[45ae2ef]33# write script file and line to stderr on error
34function pinpoint_error()
35{
[5f3222b]36    echo "Command \"${BASH_COMMAND}\" failed. Call trace:" >&2
37    local stack=0
38    while caller $((stack++)) >&2; do true; done
[45ae2ef]39}
[5f3222b]40trap 'pinpoint_error' ERR
[45ae2ef]41
[ae38a49]42function stop_msva()
43{
44    kill_by_pidfile "${msva_pidfile}"
45    unset msva_pidfile
[e3cbda4]46}
47
[a213967]48# Compare expected/actual outputs, filtering out headers from actual
49# output that are expected to change between runs or builds (currently
50# "Date" and "Server"). The headers must be excluded in the expected
51# output.
[54aa269]52#
53# Parameters:
54# $1: path to expected output
55# $2: path to actual output
56# $3: additional options for diff (optional)
[a213967]57function diff_output_filter_headers()
[54aa269]58{
[232fb60]59    local expected="$1"
60    local actual="$2"
[54aa269]61    diff $3 -u "${expected}" <( cat "${actual}" | \
[a213967]62        grep -v -P '^Date:\s.*GMT\s?$' | \
63        grep -v -P '^Server:\sApache'  | \
64        tail -n "$(wc -l < ${expected})" )
[54aa269]65}
66
[232fb60]67# Run a command, storing its PID in the given file
68# Usage: run_with_pidfile PIDFILE COMMAND [ARGS]
69function run_with_pidfile()
70{
71    local pidfile=$1
72    local cmd=$2
73    shift 2
74    echo $BASHPID >${pidfile}
75    exec ${cmd} $*
76}
77
78# Kills the process with the PID contained in a given file, then
79# deletes the file.
80# Usage: kill_by_pidfile PIDFILE
81function kill_by_pidfile()
82{
83    local pidfile="${1}"
84    # In some testcases with expected failure, gnutls-cli sometimes
85    # failed before the subshell in front of the pipe (see gnutls-cli
86    # call below) got so far as to write the PID, much less exec
87    # sleep. So we need to check if there actually is anything to
88    # kill.
89    if [ -n "${pidfile}" ]; then
90        local pid=$(cat "${pidfile}")
91        if [ -n "${pid}" ] && ps -p "${pid}"; then
92            kill "${pid}"
[bbfcbb5]93        else
94            echo "No running process with PID ${pid} (${pidfile})."
[232fb60]95        fi
96        rm "${pidfile}"
97    fi
98}
99
[4b53371]100function apache_down_err() {
101    printf "FAILURE: %s\n" "$TEST_NAME"
[af7da2d]102    ${APACHE2} -f "${t}/apache.conf" -k stop || true
[4b53371]103    if [ -e output ]; then
[54aa269]104        printf "\ngnutls-cli outputs:\n"
[a213967]105        diff_output_filter_headers "output" "$output" || true
[4b53371]106    fi
[232fb60]107
108    if [ -n "${sleep_pidfile}" ]; then
109        kill_by_pidfile "${sleep_pidfile}"
110    fi
111
[03295a9]112    local errlog="logs/${TEST_NAME}.error.log"
113    if [ -r "${errlog}" ]; then
114        printf "\nApache error logs:\n"
115        tail "${errlog}"
116    fi
[232fb60]117
[302965e]118    if [ -n "${USE_MSVA}" ]; then
119        stop_msva
120    fi
[4b53371]121}
122
[302965e]123if [ -n "${USE_MSVA}" ]; then
[ae38a49]124    msva_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)"
125    GNUPGHOME=msva.gnupghome MSVA_KEYSERVER_POLICY=never run_with_pidfile "${msva_pidfile}" monkeysphere-validation-agent &
[302965e]126    trap stop_msva EXIT
[e3cbda4]127
[302965e]128    printf "TESTING: initial MSVA verification\n"
[7adbcd7]129    # set to 0 if MSVA is up
130    ret=1
131    export MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT"
[a61edfd]132
133    # convert TEST_MSVA_WAIT to seconds because that's what "sleep" expects
134    TEST_MSVA_SLEEP="$((${TEST_MSVA_WAIT} / 1000)).$((${TEST_MSVA_WAIT} % 1000))"
135    # wait at most TEST_MSVA_MAX_WAIT milliseconds for MSVA to get ready
136    waited=0
[7adbcd7]137    until [ ${ret} -eq 0 ] \
[a61edfd]138              || [ ${waited} -ge ${TEST_MSVA_MAX_WAIT} ]; do
[7adbcd7]139        if msva-query-agent https "$(cat client.uid)" x509pem client < client/x509.pem
140        then
141            ret=0
142        else
143            echo "MSVA not ready yet"
144        fi
[a61edfd]145        sleep "${TEST_MSVA_SLEEP}"
146        waited=$((${waited} + ${TEST_MSVA_WAIT}))
[7adbcd7]147    done
148
149    # check if MSVA is up, fail if not
150    if [ ${ret} -eq 0 ]; then
151        printf "\nSUCCESS: initial MSVA verification\n"
152    else
153        printf "\nFAIL: initial MSVA verification\n"
154        exit 1
155    fi
[302965e]156fi
[e3cbda4]157
[412ee84]158TEST_PID="apache2.pid"
[dbec528]159# configure locking for the Apache process
[cf4e708]160if [ -n "${USE_TEST_NAMESPACE}" ]; then
161    echo "Using namespaces to isolate tests, no need for locking."
162    flock_cmd=""
163elif [ -n "${TEST_LOCK}" ]; then
[4ae5b82]164    flock_cmd="${FLOCK} -w ${TEST_LOCK_WAIT} $(realpath ${TEST_LOCK})"
[412ee84]165else
166    echo "Locking disabled, using wait based on Apache PID file."
167    wait_pid_gone "${TEST_PID}"
168    flock_cmd=""
169fi
[dbec528]170
[5d85ad3]171t="$(realpath ${testid})"
172export srcdir="$(realpath ${srcdir})"
173export TEST_NAME="$(basename "$t")"
174output="outputs/${TEST_NAME}.output"
175rm -f "$output"
176
177if [ -e ${t}/fail.* ]; then
178    EXPECTED_FAILURE="$(printf " (expected: %s)" fail.*)"
179else
180    unset EXPECTED_FAILURE
181fi
182printf "TESTING: %s%s\n" "$TEST_NAME" "$EXPECTED_FAILURE"
183trap apache_down_err EXIT
184if [ -n "${USE_MSVA}" ]; then
[d39ea18]185    export MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT"
186fi
[fb4da99]187
188# If VERBOSE is enabled, log the HTTPD build configuration
189if [ -n "${VERBOSE}" ]; then
190    ${APACHE2} -f "${srcdir}/base_apache.conf" -V
191fi
192
[d39ea18]193if ! ${flock_cmd} ${APACHE2} -f "${t}/apache.conf" -k start; then
194    if [ -e "${t}/fail.server" ]; then
195        echo "Apache HTTPD failed to start as expected."
196        exit 0
197    else
198        echo "Apache HTTPD unexpectedly failed to start."
199        exit 1
200    fi
[5d85ad3]201fi
[4b53371]202
[c4d6e77]203# check OCSP server
204if [ -n "${CHECK_OCSP_SERVER}" ]; then
[94cb972]205    if [ -n "${OCSP_RESPONSE_FILE}" ]; then
206        store_ocsp="--outfile ${OCSP_RESPONSE_FILE}"
207    fi
[c4d6e77]208    echo "---- Testing OCSP server ----"
[94cb972]209    ocsptool --ask --nonce --load-issuer authority/x509.pem --load-cert server/x509.pem ${store_ocsp}
[c4d6e77]210    echo "---- OCSP test done ----"
211fi
212
[5d85ad3]213# PID file for sleep command (explanation below)
214sleep_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)"
215
216# The sleep call keeps the pipe from the subshell to gnutls-cli
217# open. Without it gnutls-cli would terminate as soon as sed is
218# done, and not wait for a response from the server, leading to
219# failing tests. Sending sleep to the background allows the test
220# case to proceed instead of waiting for it to return. The sleep
221# process is stopped after gnutls-cli terminates.
[0a12ff8]222#
223# The line end manipulation in sed guarantees that all header lines
224# end with CRLF as required by RFC 7230, Section 3.1.1 regardless of
225# the line ends in the input file.
226if (sed -r "s/__HOSTNAME__/${TEST_HOST}/;s/\r?$/\r/" <${t}/input && \
[6c030c1]227           run_with_pidfile "${sleep_pidfile}" sleep "${TEST_QUERY_TIMEOUT}" &) | \
[5d85ad3]228       gnutls-cli -p "${TEST_PORT}" $(cat ${t}/gnutls-cli.args) "${TEST_HOST}" \
[28fc74b]229       | tee "$output" && test "${PIPESTATUS[1]}" -eq 0;
[5d85ad3]230then
231    if [ -e ${t}/fail* ]; then
232        printf "%s should have failed but succeeded\n" "$(basename "$t")" >&2
233        exit 1
[4b53371]234    fi
[5d85ad3]235else
236    if [ ! -e ${t}/fail* ]; then
237        printf "%s should have succeeded but failed\n" "$(basename "$t")" >&2
238        exit 1
239    fi
240fi
[dda3acf]241
[5d85ad3]242kill_by_pidfile "${sleep_pidfile}"
243unset sleep_pidfile
[232fb60]244
[5d85ad3]245if [ -e ${t}/output ] ; then
[13ffeff]246    diff_output_filter_headers "${t}/output" "$output" >&2
[5d85ad3]247fi
248if [ -n "${USE_MSVA}" ]; then
249    trap stop_msva EXIT
250else
251    trap - EXIT
252fi
253${APACHE2} -f "${t}/apache.conf" -k stop || [ -e ${t}/fail.server ]
254printf "SUCCESS: %s\n" "$TEST_NAME"
[e3cbda4]255
[302965e]256if [ -n "${USE_MSVA}" ]; then
257    stop_msva
[ae38a49]258    # Without explicitly resetting the trap function, it would be
259    # called again on exit. Of course, we could just not stop MSVA and
260    # let the trap do the work, but I think the code is easier to
261    # understand like this.
262    trap - EXIT
[302965e]263fi
Note: See TracBrowser for help on using the repository browser.