source: mod_gnutls/test/runtests @ ae38a49

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

Test suite: Use PID file to manage MSVA process

  • Property mode set to 100755
File size: 5.6 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
8
9tests="${1##t-}"
10
[9a06bbd]11if [ -n "${TEST_LOCK}" ]; then
12    TEST_LOCK="$(realpath ${TEST_LOCK})"
13    flock_cmd="flock -w 10 ${TEST_LOCK}"
14fi
15
[4b53371]16BADVARS=0
[af7da2d]17for v in APACHE2 TEST_HOST TEST_IP TEST_PORT TEST_QUERY_DELAY TEST_GAP MSVA_PORT; do
[4b53371]18    if [ ! -v "$v" ]; then
19        printf "You need to set the %s environment variable\n" "$v" >&2
20        BADVARS=1
21    fi
22done
23
24if [ 0 != "$BADVARS" ]; then
25    exit 1
26fi
27
[ae38a49]28function stop_msva()
29{
30    kill_by_pidfile "${msva_pidfile}"
31    unset msva_pidfile
[e3cbda4]32}
33
[a213967]34# Compare expected/actual outputs, filtering out headers from actual
35# output that are expected to change between runs or builds (currently
36# "Date" and "Server"). The headers must be excluded in the expected
37# output.
[54aa269]38#
39# Parameters:
40# $1: path to expected output
41# $2: path to actual output
42# $3: additional options for diff (optional)
[a213967]43function diff_output_filter_headers()
[54aa269]44{
[232fb60]45    local expected="$1"
46    local actual="$2"
[54aa269]47    diff $3 -u "${expected}" <( cat "${actual}" | \
[a213967]48        grep -v -P '^Date:\s.*GMT\s?$' | \
49        grep -v -P '^Server:\sApache'  | \
50        tail -n "$(wc -l < ${expected})" )
[54aa269]51}
52
[232fb60]53# Run a command, storing its PID in the given file
54# Usage: run_with_pidfile PIDFILE COMMAND [ARGS]
55function run_with_pidfile()
56{
57    local pidfile=$1
58    local cmd=$2
59    shift 2
60    echo $BASHPID >${pidfile}
61    exec ${cmd} $*
62}
63
64# Kills the process with the PID contained in a given file, then
65# deletes the file.
66# Usage: kill_by_pidfile PIDFILE
67function kill_by_pidfile()
68{
69    local pidfile="${1}"
70    # In some testcases with expected failure, gnutls-cli sometimes
71    # failed before the subshell in front of the pipe (see gnutls-cli
72    # call below) got so far as to write the PID, much less exec
73    # sleep. So we need to check if there actually is anything to
74    # kill.
75    if [ -n "${pidfile}" ]; then
76        local pid=$(cat "${pidfile}")
77        if [ -n "${pid}" ] && ps -p "${pid}"; then
78            kill "${pid}"
79        fi
80        rm "${pidfile}"
81    fi
82}
83
[4b53371]84function apache_down_err() {
85    printf "FAILURE: %s\n" "$TEST_NAME"
[af7da2d]86    ${APACHE2} -f "${t}/apache.conf" -k stop || true
[4b53371]87    if [ -e output ]; then
[54aa269]88        printf "\ngnutls-cli outputs:\n"
[a213967]89        diff_output_filter_headers "output" "$output" || true
[4b53371]90    fi
[232fb60]91
92    if [ -n "${sleep_pidfile}" ]; then
93        kill_by_pidfile "${sleep_pidfile}"
94    fi
95
[03295a9]96    local errlog="logs/${TEST_NAME}.error.log"
97    if [ -r "${errlog}" ]; then
98        printf "\nApache error logs:\n"
99        tail "${errlog}"
100    fi
[232fb60]101
[302965e]102    if [ -n "${USE_MSVA}" ]; then
103        stop_msva
104    fi
[4b53371]105}
106
107if [ -z "$tests" ] ; then
[33af2b7]108    tests=${srcdir}/tests/*
[4b53371]109else
[33af2b7]110    tests=${srcdir}/tests/"$(printf "%02d" "$tests")"_*
[4b53371]111fi
112
[302965e]113if [ -n "${USE_MSVA}" ]; then
[ae38a49]114    msva_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)"
115    GNUPGHOME=msva.gnupghome MSVA_KEYSERVER_POLICY=never run_with_pidfile "${msva_pidfile}" monkeysphere-validation-agent &
[302965e]116    trap stop_msva EXIT
[e3cbda4]117
[302965e]118    sleep "$TEST_GAP"
[e3cbda4]119
[302965e]120    printf "TESTING: initial MSVA verification\n"
121    MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT" msva-query-agent https "$(cat client.uid)" x509pem client < client/x509.pem
122    printf "\nSUCCESS: initial MSVA verification\n"
123fi
[e3cbda4]124
[9a06bbd]125for t in $tests; do
126    if [ -z "${flock_cmd}" ]; then
127        echo "Warning: no lock file set"
128        sleep "$TEST_GAP"
129    fi
[33af2b7]130    t="$(realpath ${t})"
131    export srcdir="$(realpath ${srcdir})"
[4b53371]132    export TEST_NAME="$(basename "$t")"
[33af2b7]133    output="outputs/${TEST_NAME}.output"
[4b53371]134    rm -f "$output"
[33af2b7]135
136    if [ -e ${t}/fail.* ]; then
[4b53371]137        EXPECTED_FAILURE="$(printf " (expected: %s)" fail.*)"
138    else
139        unset EXPECTED_FAILURE
140    fi
141    printf "TESTING: %s%s\n" "$TEST_NAME" "$EXPECTED_FAILURE"
142    trap apache_down_err EXIT
[302965e]143    if [ -n "${USE_MSVA}" ]; then
[9e35c48]144        MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT" \
145            ${flock_cmd} \
[af7da2d]146            ${APACHE2} -f "${t}/apache.conf" -k start \
[33af2b7]147            || [ -e "${t}/fail.server" ]
[302965e]148    else
[9a06bbd]149        ${flock_cmd} \
[af7da2d]150            ${APACHE2} -f "${t}/apache.conf" -k start \
[33af2b7]151            || [ -e "${t}/fail.server" ]
[302965e]152    fi
[4b53371]153
[232fb60]154    # PID file for sleep command (explanation below)
155    sleep_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)"
156
[dda3acf]157    # The sleep call keeps the pipe from the subshell to gnutls-cli
158    # open. Without it gnutls-cli would terminate as soon as sed is
159    # done, and not wait for a response from the server, leading to
160    # failing tests. Sending sleep to the background allows the test
[232fb60]161    # case to proceed instead of waiting for it to return. The sleep
162    # process is stopped after gnutls-cli terminates.
[33af2b7]163    if (sed "s/__HOSTNAME__/${TEST_HOST}/" <${t}/input && \
[232fb60]164        run_with_pidfile "${sleep_pidfile}" sleep "${TEST_QUERY_DELAY}" &) | \
[33af2b7]165        gnutls-cli -p "${TEST_PORT}" $(cat ${t}/gnutls-cli.args) "${TEST_HOST}" \
[dda3acf]166        >"$output";
167    then
[33af2b7]168        if [ -e ${t}/fail* ]; then
[4b53371]169            printf "%s should have failed but succeeded\n" "$(basename "$t")" >&2
170            exit 1
171        fi
172    else
[33af2b7]173        if [ ! -e ${t}/fail* ]; then
[4b53371]174            printf "%s should have succeeded but failed\n" "$(basename "$t")" >&2
175            exit 1
176        fi
177    fi
[dda3acf]178
[232fb60]179    kill_by_pidfile "${sleep_pidfile}"
[49d25a9]180    unset sleep_pidfile
[232fb60]181
[33af2b7]182    if [ -e ${t}/output ] ; then
183        diff_output_filter_headers "${t}/output" "$output" "-q"
[4b53371]184    fi
[302965e]185    if [ -n "${USE_MSVA}" ]; then
186        trap stop_msva EXIT
187    else
188        trap - EXIT
189    fi
[af7da2d]190    ${APACHE2} -f "${t}/apache.conf" -k stop || [ -e ${t}/fail.server ]
[4b53371]191    printf "SUCCESS: %s\n" "$TEST_NAME"
192done
[e3cbda4]193
[302965e]194if [ -n "${USE_MSVA}" ]; then
195    stop_msva
[ae38a49]196    # Without explicitly resetting the trap function, it would be
197    # called again on exit. Of course, we could just not stop MSVA and
198    # let the trap do the work, but I think the code is easier to
199    # understand like this.
200    trap - EXIT
[302965e]201fi
Note: See TracBrowser for help on using the repository browser.