[4b53371] | 1 | #!/bin/bash |
---|
| 2 | |
---|
[ae38a49] | 3 | # Authors: |
---|
| 4 | # Daniel Kahn Gillmor <dkg@fifthhorseman.net> |
---|
| 5 | # Thomas Klute <thomas2.klute@uni-dortmund.de> |
---|
[4b53371] | 6 | |
---|
| 7 | set -e |
---|
[412ee84] | 8 | . ${srcdir}/common.bash |
---|
[cf4e708] | 9 | netns_reexec ${@} |
---|
[4b53371] | 10 | |
---|
[56056de] | 11 | testid="${1##t-}" |
---|
| 12 | |
---|
| 13 | if [ -z "$testid" ] ; then |
---|
| 14 | echo -e "No test case selected.\nUsage: ${0} t-N" >&2 |
---|
| 15 | exit 1 |
---|
| 16 | else |
---|
| 17 | testid=${srcdir}/tests/"$(printf "%02d" "$testid")"_* |
---|
| 18 | fi |
---|
[4b53371] | 19 | |
---|
| 20 | BADVARS=0 |
---|
[4fb510d] | 21 | for v in APACHE2 TEST_HOST TEST_PORT TEST_QUERY_TIMEOUT TEST_SERVICE_WAIT \ |
---|
[412ee84] | 22 | MSVA_PORT; do |
---|
[4b53371] | 23 | if [ ! -v "$v" ]; then |
---|
| 24 | printf "You need to set the %s environment variable\n" "$v" >&2 |
---|
| 25 | BADVARS=1 |
---|
| 26 | fi |
---|
| 27 | done |
---|
| 28 | |
---|
| 29 | if [ 0 != "$BADVARS" ]; then |
---|
| 30 | exit 1 |
---|
| 31 | fi |
---|
| 32 | |
---|
[45ae2ef] | 33 | # write script file and line to stderr on error |
---|
| 34 | function pinpoint_error() |
---|
| 35 | { |
---|
[5f3222b] | 36 | echo "Command \"${BASH_COMMAND}\" failed. Call trace:" >&2 |
---|
| 37 | local stack=0 |
---|
| 38 | while caller $((stack++)) >&2; do true; done |
---|
[45ae2ef] | 39 | } |
---|
[5f3222b] | 40 | trap 'pinpoint_error' ERR |
---|
[45ae2ef] | 41 | |
---|
[ae38a49] | 42 | function stop_msva() |
---|
| 43 | { |
---|
| 44 | kill_by_pidfile "${msva_pidfile}" |
---|
| 45 | unset msva_pidfile |
---|
[e3cbda4] | 46 | } |
---|
| 47 | |
---|
[a213967] | 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. |
---|
[54aa269] | 52 | # |
---|
| 53 | # Parameters: |
---|
| 54 | # $1: path to expected output |
---|
| 55 | # $2: path to actual output |
---|
| 56 | # $3: additional options for diff (optional) |
---|
[a213967] | 57 | function diff_output_filter_headers() |
---|
[54aa269] | 58 | { |
---|
[232fb60] | 59 | local expected="$1" |
---|
| 60 | local actual="$2" |
---|
[54aa269] | 61 | diff $3 -u "${expected}" <( cat "${actual}" | \ |
---|
[a213967] | 62 | grep -v -P '^Date:\s.*GMT\s?$' | \ |
---|
| 63 | grep -v -P '^Server:\sApache' | \ |
---|
| 64 | tail -n "$(wc -l < ${expected})" ) |
---|
[54aa269] | 65 | } |
---|
| 66 | |
---|
[232fb60] | 67 | # Run a command, storing its PID in the given file |
---|
| 68 | # Usage: run_with_pidfile PIDFILE COMMAND [ARGS] |
---|
| 69 | function 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 |
---|
| 81 | function 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}" |
---|
[bbfcbb5] | 93 | else |
---|
| 94 | echo "No running process with PID ${pid} (${pidfile})." |
---|
[232fb60] | 95 | fi |
---|
| 96 | rm "${pidfile}" |
---|
| 97 | fi |
---|
| 98 | } |
---|
| 99 | |
---|
[4b53371] | 100 | function apache_down_err() { |
---|
| 101 | printf "FAILURE: %s\n" "$TEST_NAME" |
---|
[af7da2d] | 102 | ${APACHE2} -f "${t}/apache.conf" -k stop || true |
---|
[4b53371] | 103 | if [ -e output ]; then |
---|
[54aa269] | 104 | printf "\ngnutls-cli outputs:\n" |
---|
[a213967] | 105 | diff_output_filter_headers "output" "$output" || true |
---|
[4b53371] | 106 | fi |
---|
[232fb60] | 107 | |
---|
| 108 | if [ -n "${sleep_pidfile}" ]; then |
---|
| 109 | kill_by_pidfile "${sleep_pidfile}" |
---|
| 110 | fi |
---|
| 111 | |
---|
[03295a9] | 112 | local errlog="logs/${TEST_NAME}.error.log" |
---|
| 113 | if [ -r "${errlog}" ]; then |
---|
| 114 | printf "\nApache error logs:\n" |
---|
| 115 | tail "${errlog}" |
---|
| 116 | fi |
---|
[232fb60] | 117 | |
---|
[302965e] | 118 | if [ -n "${USE_MSVA}" ]; then |
---|
| 119 | stop_msva |
---|
| 120 | fi |
---|
[4b53371] | 121 | } |
---|
| 122 | |
---|
[302965e] | 123 | if [ -n "${USE_MSVA}" ]; then |
---|
[ae38a49] | 124 | msva_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)" |
---|
| 125 | GNUPGHOME=msva.gnupghome MSVA_KEYSERVER_POLICY=never run_with_pidfile "${msva_pidfile}" monkeysphere-validation-agent & |
---|
[302965e] | 126 | trap stop_msva EXIT |
---|
[e3cbda4] | 127 | |
---|
[302965e] | 128 | printf "TESTING: initial MSVA verification\n" |
---|
[7adbcd7] | 129 | export MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT" |
---|
[a61edfd] | 130 | |
---|
[4fb510d] | 131 | msva_test_cmd="msva-query-agent https \"$(cat client.uid)\" x509pem client < client/x509.pem" |
---|
[7adbcd7] | 132 | # check if MSVA is up, fail if not |
---|
[4fb510d] | 133 | if wait_ready "${msva_test_cmd}"; then |
---|
[7adbcd7] | 134 | printf "\nSUCCESS: initial MSVA verification\n" |
---|
| 135 | else |
---|
| 136 | printf "\nFAIL: initial MSVA verification\n" |
---|
| 137 | exit 1 |
---|
| 138 | fi |
---|
[302965e] | 139 | fi |
---|
[e3cbda4] | 140 | |
---|
[412ee84] | 141 | TEST_PID="apache2.pid" |
---|
[dbec528] | 142 | # configure locking for the Apache process |
---|
[cf4e708] | 143 | if [ -n "${USE_TEST_NAMESPACE}" ]; then |
---|
| 144 | echo "Using namespaces to isolate tests, no need for locking." |
---|
| 145 | flock_cmd="" |
---|
| 146 | elif [ -n "${TEST_LOCK}" ]; then |
---|
[4ae5b82] | 147 | flock_cmd="${FLOCK} -w ${TEST_LOCK_WAIT} $(realpath ${TEST_LOCK})" |
---|
[412ee84] | 148 | else |
---|
| 149 | echo "Locking disabled, using wait based on Apache PID file." |
---|
| 150 | wait_pid_gone "${TEST_PID}" |
---|
| 151 | flock_cmd="" |
---|
| 152 | fi |
---|
[dbec528] | 153 | |
---|
[5d85ad3] | 154 | t="$(realpath ${testid})" |
---|
| 155 | export srcdir="$(realpath ${srcdir})" |
---|
| 156 | export TEST_NAME="$(basename "$t")" |
---|
| 157 | output="outputs/${TEST_NAME}.output" |
---|
| 158 | rm -f "$output" |
---|
| 159 | |
---|
| 160 | if [ -e ${t}/fail.* ]; then |
---|
| 161 | EXPECTED_FAILURE="$(printf " (expected: %s)" fail.*)" |
---|
| 162 | else |
---|
| 163 | unset EXPECTED_FAILURE |
---|
| 164 | fi |
---|
| 165 | printf "TESTING: %s%s\n" "$TEST_NAME" "$EXPECTED_FAILURE" |
---|
| 166 | trap apache_down_err EXIT |
---|
| 167 | if [ -n "${USE_MSVA}" ]; then |
---|
[d39ea18] | 168 | export MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT" |
---|
| 169 | fi |
---|
[fb4da99] | 170 | |
---|
| 171 | # If VERBOSE is enabled, log the HTTPD build configuration |
---|
| 172 | if [ -n "${VERBOSE}" ]; then |
---|
| 173 | ${APACHE2} -f "${srcdir}/base_apache.conf" -V |
---|
| 174 | fi |
---|
| 175 | |
---|
[d39ea18] | 176 | if ! ${flock_cmd} ${APACHE2} -f "${t}/apache.conf" -k start; then |
---|
| 177 | if [ -e "${t}/fail.server" ]; then |
---|
| 178 | echo "Apache HTTPD failed to start as expected." |
---|
| 179 | exit 0 |
---|
| 180 | else |
---|
| 181 | echo "Apache HTTPD unexpectedly failed to start." |
---|
| 182 | exit 1 |
---|
| 183 | fi |
---|
[5d85ad3] | 184 | fi |
---|
[4b53371] | 185 | |
---|
[c4d6e77] | 186 | # check OCSP server |
---|
| 187 | if [ -n "${CHECK_OCSP_SERVER}" ]; then |
---|
[94cb972] | 188 | if [ -n "${OCSP_RESPONSE_FILE}" ]; then |
---|
| 189 | store_ocsp="--outfile ${OCSP_RESPONSE_FILE}" |
---|
| 190 | fi |
---|
[c4d6e77] | 191 | echo "---- Testing OCSP server ----" |
---|
[4fb510d] | 192 | wait_ready "ocsptool --ask --nonce --load-issuer authority/x509.pem --load-cert server/x509.pem ${store_ocsp}" |
---|
[c4d6e77] | 193 | echo "---- OCSP test done ----" |
---|
| 194 | fi |
---|
| 195 | |
---|
[5d85ad3] | 196 | # PID file for sleep command (explanation below) |
---|
| 197 | sleep_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)" |
---|
| 198 | |
---|
| 199 | # The sleep call keeps the pipe from the subshell to gnutls-cli |
---|
| 200 | # open. Without it gnutls-cli would terminate as soon as sed is |
---|
| 201 | # done, and not wait for a response from the server, leading to |
---|
| 202 | # failing tests. Sending sleep to the background allows the test |
---|
| 203 | # case to proceed instead of waiting for it to return. The sleep |
---|
| 204 | # process is stopped after gnutls-cli terminates. |
---|
[0a12ff8] | 205 | # |
---|
| 206 | # The line end manipulation in sed guarantees that all header lines |
---|
| 207 | # end with CRLF as required by RFC 7230, Section 3.1.1 regardless of |
---|
| 208 | # the line ends in the input file. |
---|
| 209 | if (sed -r "s/__HOSTNAME__/${TEST_HOST}/;s/\r?$/\r/" <${t}/input && \ |
---|
[6c030c1] | 210 | run_with_pidfile "${sleep_pidfile}" sleep "${TEST_QUERY_TIMEOUT}" &) | \ |
---|
[5d85ad3] | 211 | gnutls-cli -p "${TEST_PORT}" $(cat ${t}/gnutls-cli.args) "${TEST_HOST}" \ |
---|
[28fc74b] | 212 | | tee "$output" && test "${PIPESTATUS[1]}" -eq 0; |
---|
[5d85ad3] | 213 | then |
---|
| 214 | if [ -e ${t}/fail* ]; then |
---|
| 215 | printf "%s should have failed but succeeded\n" "$(basename "$t")" >&2 |
---|
| 216 | exit 1 |
---|
[4b53371] | 217 | fi |
---|
[5d85ad3] | 218 | else |
---|
| 219 | if [ ! -e ${t}/fail* ]; then |
---|
| 220 | printf "%s should have succeeded but failed\n" "$(basename "$t")" >&2 |
---|
| 221 | exit 1 |
---|
| 222 | fi |
---|
| 223 | fi |
---|
[dda3acf] | 224 | |
---|
[5d85ad3] | 225 | kill_by_pidfile "${sleep_pidfile}" |
---|
| 226 | unset sleep_pidfile |
---|
[232fb60] | 227 | |
---|
[5d85ad3] | 228 | if [ -e ${t}/output ] ; then |
---|
[13ffeff] | 229 | diff_output_filter_headers "${t}/output" "$output" >&2 |
---|
[5d85ad3] | 230 | fi |
---|
| 231 | if [ -n "${USE_MSVA}" ]; then |
---|
| 232 | trap stop_msva EXIT |
---|
| 233 | else |
---|
| 234 | trap - EXIT |
---|
| 235 | fi |
---|
| 236 | ${APACHE2} -f "${t}/apache.conf" -k stop || [ -e ${t}/fail.server ] |
---|
| 237 | printf "SUCCESS: %s\n" "$TEST_NAME" |
---|
[e3cbda4] | 238 | |
---|
[302965e] | 239 | if [ -n "${USE_MSVA}" ]; then |
---|
| 240 | stop_msva |
---|
[ae38a49] | 241 | # Without explicitly resetting the trap function, it would be |
---|
| 242 | # called again on exit. Of course, we could just not stop MSVA and |
---|
| 243 | # let the trap do the work, but I think the code is easier to |
---|
| 244 | # understand like this. |
---|
| 245 | trap - EXIT |
---|
[302965e] | 246 | fi |
---|