source: mod_gnutls/test/runtests @ 33af2b7

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

Test suite: Add tests to "dist" target and support VPATH builds

Supporting VPATH builds requires using $srcdir to find non-generated
data rather than fixed relative paths. If test are not called through
the make system, local defaults must be used. Not changing directories
during tests any more makes this easier.

A few files (e.g. templates, generated CRL) have been moved around to
better match the new structure.

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