source: mod_gnutls/test/runtests @ 5d85ad3

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

Test suite: Remove loop for multiple test cases in runtests script

It is not needed any more, and removing it simplifies the code.

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