source: mod_gnutls/test/runtests @ 849b87e

debian/master
Last change on this file since 849b87e was 849b87e, checked in by Fiona Klute <fiona.klute@…>, 13 months ago

Test suite: Add support for IP-based virtual hosts

  • Pass TEST_IP to the tests
  • Add IP addresses to the server certificate
  • Allow tests to access the server via an IP address instead of TEST_HOST
  • Property mode set to 100755
File size: 8.0 KB
RevLine 
[4b53371]1#!/bin/bash
2
[ae38a49]3# Authors:
4# Daniel Kahn Gillmor <dkg@fifthhorseman.net>
[3c123cd]5# Fiona Klute <fiona.klute@gmx.de>
[4b53371]6
7set -e
[412ee84]8. ${srcdir}/common.bash
[1872744]9. ${srcdir}/apache_service.bash
[cf4e708]10netns_reexec ${@}
[4b53371]11
[56056de]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
[b28158c]20testdir="$(realpath ${testid})"
[4b53371]21
22BADVARS=0
[4fb510d]23for v in APACHE2 TEST_HOST TEST_PORT TEST_QUERY_TIMEOUT TEST_SERVICE_WAIT \
[412ee84]24                 MSVA_PORT; do
[4b53371]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
[45ae2ef]35# write script file and line to stderr on error
36function pinpoint_error()
37{
[5f3222b]38    echo "Command \"${BASH_COMMAND}\" failed. Call trace:" >&2
39    local stack=0
40    while caller $((stack++)) >&2; do true; done
[45ae2ef]41}
[5f3222b]42trap 'pinpoint_error' ERR
[45ae2ef]43
[ae38a49]44function stop_msva()
45{
46    kill_by_pidfile "${msva_pidfile}"
47    unset msva_pidfile
[e3cbda4]48}
49
[a213967]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.
[54aa269]54#
55# Parameters:
56# $1: path to expected output
57# $2: path to actual output
58# $3: additional options for diff (optional)
[a213967]59function diff_output_filter_headers()
[54aa269]60{
[232fb60]61    local expected="$1"
62    local actual="$2"
[54aa269]63    diff $3 -u "${expected}" <( cat "${actual}" | \
[a213967]64        grep -v -P '^Date:\s.*GMT\s?$' | \
65        grep -v -P '^Server:\sApache'  | \
66        tail -n "$(wc -l < ${expected})" )
[54aa269]67}
68
[232fb60]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}"
[bbfcbb5]95        else
96            echo "No running process with PID ${pid} (${pidfile})."
[232fb60]97        fi
98        rm "${pidfile}"
99    fi
100}
101
[4b53371]102function apache_down_err() {
103    printf "FAILURE: %s\n" "$TEST_NAME"
[b28158c]104    ${APACHE2} -f "${testdir}/apache.conf" -k stop || true
[4b53371]105    if [ -e output ]; then
[54aa269]106        printf "\ngnutls-cli outputs:\n"
[a213967]107        diff_output_filter_headers "output" "$output" || true
[4b53371]108    fi
[232fb60]109
[b28158c]110    if [ -r "${testdir}/backend.conf" ]; then
[1872744]111        apache_service "${testdir}" "backend.conf" stop || true
[b28158c]112    fi
113
[321912b]114    if [ -r "${testdir}/ocsp.conf" ]; then
115        apache_service "${testdir}" "ocsp.conf" stop || true
116    fi
117
[232fb60]118    if [ -n "${sleep_pidfile}" ]; then
119        kill_by_pidfile "${sleep_pidfile}"
120    fi
121
[03295a9]122    local errlog="logs/${TEST_NAME}.error.log"
123    if [ -r "${errlog}" ]; then
124        printf "\nApache error logs:\n"
125        tail "${errlog}"
126    fi
[232fb60]127
[302965e]128    if [ -n "${USE_MSVA}" ]; then
129        stop_msva
130    fi
[4b53371]131}
132
[302965e]133if [ -n "${USE_MSVA}" ]; then
[ae38a49]134    msva_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)"
135    GNUPGHOME=msva.gnupghome MSVA_KEYSERVER_POLICY=never run_with_pidfile "${msva_pidfile}" monkeysphere-validation-agent &
[302965e]136    trap stop_msva EXIT
[e3cbda4]137
[302965e]138    printf "TESTING: initial MSVA verification\n"
[7adbcd7]139    export MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT"
[a61edfd]140
[4fb510d]141    msva_test_cmd="msva-query-agent https \"$(cat client.uid)\" x509pem client < client/x509.pem"
[7adbcd7]142    # check if MSVA is up, fail if not
[4fb510d]143    if wait_ready "${msva_test_cmd}"; then
[7adbcd7]144        printf "\nSUCCESS: initial MSVA verification\n"
145    else
146        printf "\nFAIL: initial MSVA verification\n"
147        exit 1
148    fi
[302965e]149fi
[e3cbda4]150
[dbec528]151# configure locking for the Apache process
[cf4e708]152if [ -n "${USE_TEST_NAMESPACE}" ]; then
153    echo "Using namespaces to isolate tests, no need for locking."
154    flock_cmd=""
[cdc6e4a]155elif [ -n "${FLOCK}" ]; then
[4ae5b82]156    flock_cmd="${FLOCK} -w ${TEST_LOCK_WAIT} $(realpath ${TEST_LOCK})"
[412ee84]157else
158    echo "Locking disabled, using wait based on Apache PID file."
[e00d91a]159    wait_pid_gone "${TEST_LOCK}"
[412ee84]160    flock_cmd=""
161fi
[dbec528]162
[5d85ad3]163export srcdir="$(realpath ${srcdir})"
[b28158c]164export TEST_NAME="$(basename "${testdir}")"
[5d85ad3]165output="outputs/${TEST_NAME}.output"
166rm -f "$output"
167
[b28158c]168if [ -e ${testdir}/fail.* ]; then
[5d85ad3]169    EXPECTED_FAILURE="$(printf " (expected: %s)" fail.*)"
170else
171    unset EXPECTED_FAILURE
172fi
173printf "TESTING: %s%s\n" "$TEST_NAME" "$EXPECTED_FAILURE"
174trap apache_down_err EXIT
175if [ -n "${USE_MSVA}" ]; then
[d39ea18]176    export MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT"
177fi
[fb4da99]178
179# If VERBOSE is enabled, log the HTTPD build configuration
180if [ -n "${VERBOSE}" ]; then
181    ${APACHE2} -f "${srcdir}/base_apache.conf" -V
182fi
183
[321912b]184# Start OCSP responder, if configured
185if [ -r "${testdir}/ocsp.conf" ]; then
186    apache_service "${testdir}" "ocsp.conf" start "${OCSP_LOCK}"
187    CHECK_OCSP_SERVER="true"
188    if [ -n "${VERBOSE}" ]; then
189        echo "OCSP index for the test CA:"
190        cat authority/ocsp_index.txt
191    fi
192fi
193
194# Start proxy backend server, if configured
[b28158c]195if [ -r "${testdir}/backend.conf" ]; then
[1872744]196    apache_service "${testdir}" "backend.conf" start "${BACKEND_LOCK}"
[b28158c]197fi
198
199if ! ${flock_cmd} ${APACHE2} -f "${testdir}/apache.conf" -k start; then
200    if [ -e "${testdir}/fail.server" ]; then
[d39ea18]201        echo "Apache HTTPD failed to start as expected."
202        exit 0
203    else
204        echo "Apache HTTPD unexpectedly failed to start."
205        exit 1
206    fi
[5d85ad3]207fi
[4b53371]208
[c4d6e77]209# check OCSP server
210if [ -n "${CHECK_OCSP_SERVER}" ]; then
[94cb972]211    if [ -n "${OCSP_RESPONSE_FILE}" ]; then
212        store_ocsp="--outfile ${OCSP_RESPONSE_FILE}"
213    fi
[c4d6e77]214    echo "---- Testing OCSP server ----"
[4fb510d]215    wait_ready "ocsptool --ask --nonce --load-issuer authority/x509.pem --load-cert server/x509.pem ${store_ocsp}"
[c4d6e77]216    echo "---- OCSP test done ----"
217fi
218
[849b87e]219if [ -n "${TARGET_IP}" ]; then
220    TARGET="${TARGET_IP}"
221else
222    TARGET="${TEST_HOST}"
223fi
224
[5d85ad3]225# PID file for sleep command (explanation below)
226sleep_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)"
227
228# The sleep call keeps the pipe from the subshell to gnutls-cli
229# open. Without it gnutls-cli would terminate as soon as sed is
230# done, and not wait for a response from the server, leading to
231# failing tests. Sending sleep to the background allows the test
232# case to proceed instead of waiting for it to return. The sleep
233# process is stopped after gnutls-cli terminates.
[0a12ff8]234#
235# The line end manipulation in sed guarantees that all header lines
236# end with CRLF as required by RFC 7230, Section 3.1.1 regardless of
237# the line ends in the input file.
[b28158c]238if (sed -r "s/__HOSTNAME__/${TEST_HOST}/;s/\r?$/\r/" <${testdir}/input && \
[6c030c1]239           run_with_pidfile "${sleep_pidfile}" sleep "${TEST_QUERY_TIMEOUT}" &) | \
[849b87e]240       gnutls-cli -p "${TEST_PORT}" $(cat ${testdir}/gnutls-cli.args) "${TARGET}" \
[28fc74b]241       | tee "$output" && test "${PIPESTATUS[1]}" -eq 0;
[5d85ad3]242then
[b28158c]243    if [ -e ${testdir}/fail* ]; then
244        printf "%s should have failed but succeeded\n" "$(basename "$testdir")" >&2
[5d85ad3]245        exit 1
[4b53371]246    fi
[5d85ad3]247else
[b28158c]248    if [ ! -e ${testdir}/fail* ]; then
249        printf "%s should have succeeded but failed\n" "$(basename "$testdir")" >&2
[5d85ad3]250        exit 1
251    fi
252fi
[dda3acf]253
[5d85ad3]254kill_by_pidfile "${sleep_pidfile}"
255unset sleep_pidfile
[232fb60]256
[b28158c]257if [ -e ${testdir}/output ] ; then
258    diff_output_filter_headers "${testdir}/output" "$output" >&2
[5d85ad3]259fi
260if [ -n "${USE_MSVA}" ]; then
261    trap stop_msva EXIT
262else
263    trap - EXIT
264fi
[b28158c]265${APACHE2} -f "${testdir}/apache.conf" -k stop || [ -e ${testdir}/fail.server ]
[5d85ad3]266printf "SUCCESS: %s\n" "$TEST_NAME"
[e3cbda4]267
[b28158c]268if [ -r "${testdir}/backend.conf" ]; then
[1872744]269    apache_service "${testdir}" "backend.conf" stop || true
[b28158c]270fi
271
[321912b]272if [ -r "${testdir}/ocsp.conf" ]; then
273    apache_service "${testdir}" "ocsp.conf" stop || true
274fi
275
[302965e]276if [ -n "${USE_MSVA}" ]; then
277    stop_msva
[ae38a49]278    # Without explicitly resetting the trap function, it would be
279    # called again on exit. Of course, we could just not stop MSVA and
280    # let the trap do the work, but I think the code is easier to
281    # understand like this.
282    trap - EXIT
[302965e]283fi
Note: See TracBrowser for help on using the repository browser.