source: mod_gnutls/test/runtests @ 49d25a9

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

Test suite: Unset sleep_pidfile before checking output

If the variable remains defined after stopping sleep, apache_down_err
tries to use a nonexistent PID file if the comparison fails.

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