source: mod_gnutls/test/runtests @ 34e5dc7

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

Test suite: Call "runtests" directly from test scripts

The test scripts used to call runtests through TestMakefile?, which set
up the environment. Moving the environment configuration to
AM_TESTS_ENVIRONMENT in Makefile.am avoids this additional call to
make.

TEST_GAP has been renamed to TEST_MSVA_WAIT because the sleep time in
the MSVA startup loop is its only remaining use.

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