source: mod_gnutls/test/runtests @ 5f3222b

debian/masterdebian/stretch-backportsupstream
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
Line 
1#!/bin/bash
2
3# Authors:
4# Daniel Kahn Gillmor <dkg@fifthhorseman.net>
5# Thomas Klute <thomas2.klute@uni-dortmund.de>
6
7set -e
8. ${srcdir}/common.bash
9netns_reexec ${@}
10
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
19
20BADVARS=0
21for v in APACHE2 TEST_HOST TEST_PORT TEST_QUERY_TIMEOUT TEST_MSVA_WAIT \
22                 MSVA_PORT; do
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
33# write script file and line to stderr on error
34function pinpoint_error()
35{
36    echo "Command \"${BASH_COMMAND}\" failed. Call trace:" >&2
37    local stack=0
38    while caller $((stack++)) >&2; do true; done
39}
40trap 'pinpoint_error' ERR
41
42function stop_msva()
43{
44    kill_by_pidfile "${msva_pidfile}"
45    unset msva_pidfile
46}
47
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.
52#
53# Parameters:
54# $1: path to expected output
55# $2: path to actual output
56# $3: additional options for diff (optional)
57function diff_output_filter_headers()
58{
59    local expected="$1"
60    local actual="$2"
61    diff $3 -u "${expected}" <( cat "${actual}" | \
62        grep -v -P '^Date:\s.*GMT\s?$' | \
63        grep -v -P '^Server:\sApache'  | \
64        tail -n "$(wc -l < ${expected})" )
65}
66
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}"
93        else
94            echo "No running process with PID ${pid} (${pidfile})."
95        fi
96        rm "${pidfile}"
97    fi
98}
99
100function apache_down_err() {
101    printf "FAILURE: %s\n" "$TEST_NAME"
102    ${APACHE2} -f "${t}/apache.conf" -k stop || true
103    if [ -e output ]; then
104        printf "\ngnutls-cli outputs:\n"
105        diff_output_filter_headers "output" "$output" || true
106    fi
107
108    if [ -n "${sleep_pidfile}" ]; then
109        kill_by_pidfile "${sleep_pidfile}"
110    fi
111
112    local errlog="logs/${TEST_NAME}.error.log"
113    if [ -r "${errlog}" ]; then
114        printf "\nApache error logs:\n"
115        tail "${errlog}"
116    fi
117
118    if [ -n "${USE_MSVA}" ]; then
119        stop_msva
120    fi
121}
122
123if [ -n "${USE_MSVA}" ]; then
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 &
126    trap stop_msva EXIT
127
128    printf "TESTING: initial MSVA verification\n"
129    # set to 0 if MSVA is up
130    ret=1
131    export MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT"
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
137    until [ ${ret} -eq 0 ] \
138              || [ ${waited} -ge ${TEST_MSVA_MAX_WAIT} ]; do
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
145        sleep "${TEST_MSVA_SLEEP}"
146        waited=$((${waited} + ${TEST_MSVA_WAIT}))
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
156fi
157
158TEST_PID="apache2.pid"
159# configure locking for the Apache process
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
164    flock_cmd="${FLOCK} -w ${TEST_LOCK_WAIT} $(realpath ${TEST_LOCK})"
165else
166    echo "Locking disabled, using wait based on Apache PID file."
167    wait_pid_gone "${TEST_PID}"
168    flock_cmd=""
169fi
170
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
185    export MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT"
186fi
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
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
201fi
202
203# check OCSP server
204if [ -n "${CHECK_OCSP_SERVER}" ]; then
205    if [ -n "${OCSP_RESPONSE_FILE}" ]; then
206        store_ocsp="--outfile ${OCSP_RESPONSE_FILE}"
207    fi
208    echo "---- Testing OCSP server ----"
209    ocsptool --ask --nonce --load-issuer authority/x509.pem --load-cert server/x509.pem ${store_ocsp}
210    echo "---- OCSP test done ----"
211fi
212
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.
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 && \
227           run_with_pidfile "${sleep_pidfile}" sleep "${TEST_QUERY_TIMEOUT}" &) | \
228       gnutls-cli -p "${TEST_PORT}" $(cat ${t}/gnutls-cli.args) "${TEST_HOST}" \
229       | tee "$output" && test "${PIPESTATUS[1]}" -eq 0;
230then
231    if [ -e ${t}/fail* ]; then
232        printf "%s should have failed but succeeded\n" "$(basename "$t")" >&2
233        exit 1
234    fi
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
241
242kill_by_pidfile "${sleep_pidfile}"
243unset sleep_pidfile
244
245if [ -e ${t}/output ] ; then
246    diff_output_filter_headers "${t}/output" "$output" >&2
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"
255
256if [ -n "${USE_MSVA}" ]; then
257    stop_msva
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
263fi
Note: See TracBrowser for help on using the repository browser.