source: mod_gnutls/test/runtests @ 54aa269

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

Test suite: Exclude "Date" header when comparing actual and expected output

The "Date" header may be sent with Apache error pages, e.g. in case of a failed
proxy request. It is expected to change and must thus be excluded from the
comparison, otherwise tests expecting such an error page will always fail.

  • Property mode set to 100755
File size: 4.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
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 the "Date" header
36# from actual output because it is expected to change. It must have
37# been manually excluded from the expected output.
38#
39# Parameters:
40# $1: path to expected output
41# $2: path to actual output
42# $3: additional options for diff (optional)
43function diff_output_filter_date()
44{
45    expected="$1"
46    actual="$2"
47    diff $3 -u "${expected}" <( cat "${actual}" | \
48        grep -v -P '^Date:\s.*GMT\s?$' | tail -n "$(wc -l < ${expected})" )
49}
50
51function apache_down_err() {
52    printf "FAILURE: %s\n" "$TEST_NAME"
53    /usr/sbin/apache2 -f "$(pwd)/apache.conf" -k stop || true
54    if [ -e output ]; then
55        printf "\ngnutls-cli outputs:\n"
56        diff_output_filter_date "output" "$output" || true
57    fi
58    printf "\nApache error logs:\n"
59    tail "../../logs/${TEST_NAME}.error.log"
60    if [ -n "${USE_MSVA}" ]; then
61        stop_msva
62    fi
63}
64
65if [ -z "$tests" ] ; then
66    tests=./tests/*
67else
68    tests=./tests/"$(printf "%02d" "$tests")"_*
69fi
70
71if [ -n "${USE_MSVA}" ]; then
72    GNUPGHOME=$(pwd)/msva.gnupghome MSVA_KEYSERVER_POLICY=never monkeysphere-validation-agent &
73    trap stop_msva EXIT
74
75    sleep "$TEST_GAP"
76
77    printf "TESTING: initial MSVA verification\n"
78    MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT" msva-query-agent https "$(cat client.uid)" x509pem client < client/x509.pem
79    printf "\nSUCCESS: initial MSVA verification\n"
80fi
81
82for t in $tests; do
83    if [ -z "${flock_cmd}" ]; then
84        echo "Warning: no lock file set"
85        sleep "$TEST_GAP"
86    fi
87    export TEST_NAME="$(basename "$t")"
88    output="../../outputs/${TEST_NAME}.output"
89    rm -f "$output"
90    cd "$t"
91    if [ -e fail.* ]; then
92        EXPECTED_FAILURE="$(printf " (expected: %s)" fail.*)"
93    else
94        unset EXPECTED_FAILURE
95    fi
96    printf "TESTING: %s%s\n" "$TEST_NAME" "$EXPECTED_FAILURE"
97    trap apache_down_err EXIT
98    if [ -n "${USE_MSVA}" ]; then
99        MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT" \
100            ${flock_cmd} \
101            /usr/sbin/apache2 -f "$(pwd)/apache.conf" -k start \
102            || [ -e fail.server ]
103    else
104        ${flock_cmd} \
105            /usr/sbin/apache2 -f "$(pwd)/apache.conf" -k start \
106            || [ -e fail.server ]
107    fi
108
109    # The sleep call keeps the pipe from the subshell to gnutls-cli
110    # open. Without it gnutls-cli would terminate as soon as sed is
111    # done, and not wait for a response from the server, leading to
112    # failing tests. Sending sleep to the background allows the test
113    # case to proceed instead of waiting for it to return, but has the
114    # disadvantage of leaving the sleep process dangling until it
115    # eventually times out. Still preferable to a fixed delay.
116    if (sed "s/__HOSTNAME__/${TEST_HOST}/" <./input && \
117        sleep "${TEST_QUERY_DELAY}" &) | \
118        gnutls-cli -p "${TEST_PORT}" $(cat ./gnutls-cli.args) "${TEST_HOST}" \
119        >"$output";
120    then
121        if [ -e fail* ]; then
122            printf "%s should have failed but succeeded\n" "$(basename "$t")" >&2
123            exit 1
124        fi
125    else
126        if [ ! -e fail* ]; then
127            printf "%s should have succeeded but failed\n" "$(basename "$t")" >&2
128            exit 1
129        fi
130    fi
131
132    if [ -e output ] ; then
133        diff_output_filter_date "output" "$output" "-q"
134    fi
135    /usr/sbin/apache2 -f "$(pwd)/apache.conf" -k stop || [ -e fail.server ]
136    if [ -n "${USE_MSVA}" ]; then
137        trap stop_msva EXIT
138    else
139        trap - EXIT
140    fi
141    printf "SUCCESS: %s\n" "$TEST_NAME"
142    cd ../..
143done
144
145if [ -n "${USE_MSVA}" ]; then
146    stop_msva
147fi
Note: See TracBrowser for help on using the repository browser.