source: mod_gnutls/test/runtests @ 1872744

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

Test suite: Rename bash function backend_apache to apache_service

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