source: mod_gnutls/test/common.bash

mod_gnutls/0.9.1
Last change on this file was afebb9b, checked in by Krista Karppinen <krista.celestia@…>, 13 months ago

Add gnutls-cli version requirement to SNI tests

Since the --sni-hostname option doesn't exist in gnutls-cli client
versions prior to 3.5.12, skip the tests using it if an older version
(or no version) is encountered.

  • Property mode set to 100644
File size: 3.6 KB
Line 
1#!/bin/bash
2
3# Usage: wait_pid_gone ${FILE}
4#
5# Wait until $FILE disappears, but no longer than $TEST_LOCK_WAIT
6# seconds
7function wait_pid_gone
8{
9    local pid_file="${1}"
10    local pid_wait=0
11    while [ -e "${pid_file}" ]; do
12        if [ "$((pid_wait++))" -gt "${TEST_LOCK_WAIT}" ]; then
13            return 1
14        fi
15        sleep 1
16    done
17}
18
19
20
21# Usage: verbose_log [...]
22#
23# If VERBOSE is not empty, write a log message prefixed with the name
24# of the calling function. The function is defined to a no-op
25# otherwise.
26if [ -n "${VERBOSE}" ]; then
27    function verbose_log
28    {
29        echo "${FUNCNAME[1]}: ${@}"
30    }
31else
32    function verbose_log
33    {
34        return
35    }
36fi
37
38
39
40# Usage: wait_ready COMMAND [TIMEOUT] [STEP]
41#
42# Wait until COMMAND terminates with success (zero exit code), or
43# until the TIMEOUT (in milliseconds) expires. TIMEOUT defaults to
44# $TEST_SERVICE_MAX_WAIT if unset. A TIMEOUT of zero means to try
45# once.
46#
47# COMMAND is retried every STEP milliseconds, the default is
48# $TEST_SERVICE_WAIT. Note that the last try may happen a little after
49# TIMEOUT expires if STEP does not evenly divide it.
50function wait_ready
51{
52    local command="${1}"
53    if [ -z "${2}" ]; then
54        local -i timeout="${TEST_SERVICE_MAX_WAIT}"
55    else
56        local -i timeout="${2}"
57    fi
58    local -i step="${3}"
59    [ ${step} -gt 0 ] || step="${TEST_SERVICE_WAIT}"
60    # convert step to seconds because that's what "sleep" needs
61    local sec_step="$((${step} / 1000)).$((${step} % 1000))"
62
63    verbose_log "Waiting for \"${command}\" ..."
64    local -i waited=0
65    until eval "${command}"; do
66        if [ "${waited}" -ge "${timeout}" ]; then
67            echo "${FUNCNAME[0]}: Timed out waiting for \"${command}\"" \
68                 "to succeed (waited ${waited} ms)." >&2
69            return 1
70        fi
71        waited=$((waited + step));
72        sleep "${sec_step}"
73        verbose_log "waiting (${waited} ms)"
74    done
75    verbose_log "done (waited ${waited} ms)"
76}
77
78
79
80# Usage: netns_reexec ${@}
81#
82# If USE_TEST_NAMESPACE is set and MGS_NETNS_ACTIVE is not, exec the
83# running command inside a new namespace with active loopback
84# interface and MGS_NETNS_ACTIVE defined. This function can be used to
85# isolate each testcase inside its own network namespace. Since
86# MGS_NETNS_ACTIVE is used to track status, there's no harm in calling
87# it multiple times (e.g. in the test-* script and runtests).
88#
89# Note that once the network is up, the reexec is wrapped in another
90# user namespace to get rid of pseudo "root" access. The reason for
91# this is that Apache tries to switch permissions to a non-root user
92# when apparently started as root, and fails because no such user
93# exists inside the namespace. Changing to a non-root user beforehand
94# avoids that issue.
95function netns_reexec
96{
97    if [ -n "${USE_TEST_NAMESPACE}" ] && [ -z "${MGS_NETNS_ACTIVE}" ]; then
98        exec "${UNSHARE}" --net --ipc -r /bin/bash -c \
99             "export MGS_NETNS_ACTIVE=1; ip link set up lo; exec ${UNSHARE} --user ${0} ${@}"
100    fi
101    return 0
102}
103
104# Usage: require_gnutls_cli ${REQUIRED_VERSION_NUMBER} || exit ${ERROR_CODE}
105# Require the gnutls-cli binary to be of a given version or newer.
106# Return error code 1 if older, 2 if not found.
107function require_gnutls_cli
108{
109    local required_version=(${1//./ })
110
111    if [[ $(gnutls-cli --version) =~ gnutls-cli[[:space:]]([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+) ]]
112    then
113        for i in {0..2}
114        do
115            if [ ${BASH_REMATCH[i+1]} -gt ${required_version[i]} ]
116            then
117                break;
118            elif [ ${BASH_REMATCH[i+1]} -lt ${required_version[i]} ]
119            then
120                return 1
121            fi
122        done
123        return 0
124    else
125        return 2
126    fi
127}
Note: See TracBrowser for help on using the repository browser.