source: mod_gnutls/test/runtests @ 412ee84

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

Test suite: Allow the user to disable use of "flock"

Using "flock" causes trouble on some exotic architectures (e.g. Hurd),
so allow disabling it. Running without lock files requires serial
execution of test cases to achieve reliable results.

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