source: mod_gnutls/test/runtests @ cdc6e4a

debian/masterdebian/stretch-backportsupstream
Last change on this file since cdc6e4a was cdc6e4a, checked in by Thomas Klute <thomas2.klute@…>, 2 years ago

Choose flock or PID wait based on availability, not file parameter presence

  • Property mode set to 100755
File size: 6.9 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_SERVICE_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    export MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT"
130
131    msva_test_cmd="msva-query-agent https \"$(cat client.uid)\" x509pem client < client/x509.pem"
132    # check if MSVA is up, fail if not
133    if wait_ready "${msva_test_cmd}"; then
134        printf "\nSUCCESS: initial MSVA verification\n"
135    else
136        printf "\nFAIL: initial MSVA verification\n"
137        exit 1
138    fi
139fi
140
141TEST_PID="apache2.pid"
142# configure locking for the Apache process
143if [ -n "${USE_TEST_NAMESPACE}" ]; then
144    echo "Using namespaces to isolate tests, no need for locking."
145    flock_cmd=""
146elif [ -n "${FLOCK}" ]; then
147    flock_cmd="${FLOCK} -w ${TEST_LOCK_WAIT} $(realpath ${TEST_LOCK})"
148else
149    echo "Locking disabled, using wait based on Apache PID file."
150    wait_pid_gone "${TEST_PID}"
151    flock_cmd=""
152fi
153
154t="$(realpath ${testid})"
155export srcdir="$(realpath ${srcdir})"
156export TEST_NAME="$(basename "$t")"
157output="outputs/${TEST_NAME}.output"
158rm -f "$output"
159
160if [ -e ${t}/fail.* ]; then
161    EXPECTED_FAILURE="$(printf " (expected: %s)" fail.*)"
162else
163    unset EXPECTED_FAILURE
164fi
165printf "TESTING: %s%s\n" "$TEST_NAME" "$EXPECTED_FAILURE"
166trap apache_down_err EXIT
167if [ -n "${USE_MSVA}" ]; then
168    export MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT"
169fi
170
171# If VERBOSE is enabled, log the HTTPD build configuration
172if [ -n "${VERBOSE}" ]; then
173    ${APACHE2} -f "${srcdir}/base_apache.conf" -V
174fi
175
176if ! ${flock_cmd} ${APACHE2} -f "${t}/apache.conf" -k start; then
177    if [ -e "${t}/fail.server" ]; then
178        echo "Apache HTTPD failed to start as expected."
179        exit 0
180    else
181        echo "Apache HTTPD unexpectedly failed to start."
182        exit 1
183    fi
184fi
185
186# check OCSP server
187if [ -n "${CHECK_OCSP_SERVER}" ]; then
188    if [ -n "${OCSP_RESPONSE_FILE}" ]; then
189        store_ocsp="--outfile ${OCSP_RESPONSE_FILE}"
190    fi
191    echo "---- Testing OCSP server ----"
192    wait_ready "ocsptool --ask --nonce --load-issuer authority/x509.pem --load-cert server/x509.pem ${store_ocsp}"
193    echo "---- OCSP test done ----"
194fi
195
196# PID file for sleep command (explanation below)
197sleep_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)"
198
199# The sleep call keeps the pipe from the subshell to gnutls-cli
200# open. Without it gnutls-cli would terminate as soon as sed is
201# done, and not wait for a response from the server, leading to
202# failing tests. Sending sleep to the background allows the test
203# case to proceed instead of waiting for it to return. The sleep
204# process is stopped after gnutls-cli terminates.
205#
206# The line end manipulation in sed guarantees that all header lines
207# end with CRLF as required by RFC 7230, Section 3.1.1 regardless of
208# the line ends in the input file.
209if (sed -r "s/__HOSTNAME__/${TEST_HOST}/;s/\r?$/\r/" <${t}/input && \
210           run_with_pidfile "${sleep_pidfile}" sleep "${TEST_QUERY_TIMEOUT}" &) | \
211       gnutls-cli -p "${TEST_PORT}" $(cat ${t}/gnutls-cli.args) "${TEST_HOST}" \
212       | tee "$output" && test "${PIPESTATUS[1]}" -eq 0;
213then
214    if [ -e ${t}/fail* ]; then
215        printf "%s should have failed but succeeded\n" "$(basename "$t")" >&2
216        exit 1
217    fi
218else
219    if [ ! -e ${t}/fail* ]; then
220        printf "%s should have succeeded but failed\n" "$(basename "$t")" >&2
221        exit 1
222    fi
223fi
224
225kill_by_pidfile "${sleep_pidfile}"
226unset sleep_pidfile
227
228if [ -e ${t}/output ] ; then
229    diff_output_filter_headers "${t}/output" "$output" >&2
230fi
231if [ -n "${USE_MSVA}" ]; then
232    trap stop_msva EXIT
233else
234    trap - EXIT
235fi
236${APACHE2} -f "${t}/apache.conf" -k stop || [ -e ${t}/fail.server ]
237printf "SUCCESS: %s\n" "$TEST_NAME"
238
239if [ -n "${USE_MSVA}" ]; then
240    stop_msva
241    # Without explicitly resetting the trap function, it would be
242    # called again on exit. Of course, we could just not stop MSVA and
243    # let the trap do the work, but I think the code is easier to
244    # understand like this.
245    trap - EXIT
246fi
Note: See TracBrowser for help on using the repository browser.