source: mod_gnutls/test/runtests @ ae38a49

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

Test suite: Use PID file to manage MSVA process

  • Property mode set to 100755
File size: 5.6 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
9tests="${1##t-}"
10
11if [ -n "${TEST_LOCK}" ]; then
12    TEST_LOCK="$(realpath ${TEST_LOCK})"
13    flock_cmd="flock -w 10 ${TEST_LOCK}"
14fi
15
16BADVARS=0
17for v in APACHE2 TEST_HOST TEST_IP TEST_PORT TEST_QUERY_DELAY TEST_GAP MSVA_PORT; do
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
28function stop_msva()
29{
30    kill_by_pidfile "${msva_pidfile}"
31    unset msva_pidfile
32}
33
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.
38#
39# Parameters:
40# $1: path to expected output
41# $2: path to actual output
42# $3: additional options for diff (optional)
43function diff_output_filter_headers()
44{
45    local expected="$1"
46    local actual="$2"
47    diff $3 -u "${expected}" <( cat "${actual}" | \
48        grep -v -P '^Date:\s.*GMT\s?$' | \
49        grep -v -P '^Server:\sApache'  | \
50        tail -n "$(wc -l < ${expected})" )
51}
52
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
84function apache_down_err() {
85    printf "FAILURE: %s\n" "$TEST_NAME"
86    ${APACHE2} -f "${t}/apache.conf" -k stop || true
87    if [ -e output ]; then
88        printf "\ngnutls-cli outputs:\n"
89        diff_output_filter_headers "output" "$output" || true
90    fi
91
92    if [ -n "${sleep_pidfile}" ]; then
93        kill_by_pidfile "${sleep_pidfile}"
94    fi
95
96    local errlog="logs/${TEST_NAME}.error.log"
97    if [ -r "${errlog}" ]; then
98        printf "\nApache error logs:\n"
99        tail "${errlog}"
100    fi
101
102    if [ -n "${USE_MSVA}" ]; then
103        stop_msva
104    fi
105}
106
107if [ -z "$tests" ] ; then
108    tests=${srcdir}/tests/*
109else
110    tests=${srcdir}/tests/"$(printf "%02d" "$tests")"_*
111fi
112
113if [ -n "${USE_MSVA}" ]; then
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 &
116    trap stop_msva EXIT
117
118    sleep "$TEST_GAP"
119
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
124
125for t in $tests; do
126    if [ -z "${flock_cmd}" ]; then
127        echo "Warning: no lock file set"
128        sleep "$TEST_GAP"
129    fi
130    t="$(realpath ${t})"
131    export srcdir="$(realpath ${srcdir})"
132    export TEST_NAME="$(basename "$t")"
133    output="outputs/${TEST_NAME}.output"
134    rm -f "$output"
135
136    if [ -e ${t}/fail.* ]; then
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
143    if [ -n "${USE_MSVA}" ]; then
144        MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT" \
145            ${flock_cmd} \
146            ${APACHE2} -f "${t}/apache.conf" -k start \
147            || [ -e "${t}/fail.server" ]
148    else
149        ${flock_cmd} \
150            ${APACHE2} -f "${t}/apache.conf" -k start \
151            || [ -e "${t}/fail.server" ]
152    fi
153
154    # PID file for sleep command (explanation below)
155    sleep_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)"
156
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
161    # case to proceed instead of waiting for it to return. The sleep
162    # process is stopped after gnutls-cli terminates.
163    if (sed "s/__HOSTNAME__/${TEST_HOST}/" <${t}/input && \
164        run_with_pidfile "${sleep_pidfile}" sleep "${TEST_QUERY_DELAY}" &) | \
165        gnutls-cli -p "${TEST_PORT}" $(cat ${t}/gnutls-cli.args) "${TEST_HOST}" \
166        >"$output";
167    then
168        if [ -e ${t}/fail* ]; then
169            printf "%s should have failed but succeeded\n" "$(basename "$t")" >&2
170            exit 1
171        fi
172    else
173        if [ ! -e ${t}/fail* ]; then
174            printf "%s should have succeeded but failed\n" "$(basename "$t")" >&2
175            exit 1
176        fi
177    fi
178
179    kill_by_pidfile "${sleep_pidfile}"
180    unset sleep_pidfile
181
182    if [ -e ${t}/output ] ; then
183        diff_output_filter_headers "${t}/output" "$output" "-q"
184    fi
185    if [ -n "${USE_MSVA}" ]; then
186        trap stop_msva EXIT
187    else
188        trap - EXIT
189    fi
190    ${APACHE2} -f "${t}/apache.conf" -k stop || [ -e ${t}/fail.server ]
191    printf "SUCCESS: %s\n" "$TEST_NAME"
192done
193
194if [ -n "${USE_MSVA}" ]; then
195    stop_msva
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
201fi
Note: See TracBrowser for help on using the repository browser.