wolfSSL: Use LIBWOLFSSL_VERSION_HEX to decide whether or not to use WOLFSSL_X509_V_OK
[privoxy.git] / tests / cts / run-privoxy-tests.sh
1 #!/bin/sh
2 ################################################################################
3 #
4 # run-privoxy-tests.sh
5 #
6 # Runs the Privoxy tests based on curl's runtests.pl framework.
7 #
8 # Copyright (c) 2021 Fabian Keil <fk@fabiankeil.de>
9 #
10 # Permission to use, copy, modify, and distribute this software for any
11 # purpose with or without fee is hereby granted, provided that the above
12 # copyright notice and this permission notice appear in all copies.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 ################################################################################
22
23 UPSTREAM_TEST_SCENARIO=upstream-tests
24
25 # Delaying the test run by a whole second is annoying on fast systems
26 # and may not be long enough on slow systems. There must be a more
27 # elegant method to verify that Privoxy is running or failed to start.
28 SECONDS_TO_WAIT_FOR_PRIVOXY_TO_START=1
29
30 start_privoxy() {
31     local test_dir test_scenario
32     local privoxy_config privoxy_binary pid_file
33     test_dir="${1}"
34     test_scenario="${2}"
35
36     privoxy_config_dir="${test_dir}/${test_scenario}"
37     privoxy_binary="$(realpath "${test_dir}/../../privoxy")"
38     pid_file="${test_dir}/${test_scenario}/../privoxy.pid"
39     log_file="${test_dir}/logs/${test_scenario}.log"
40
41     (
42         cd "${privoxy_config_dir}" || exit 1
43         "${privoxy_binary}" --no-daemon \
44                             --pidfile "${pid_file}" \
45                             privoxy.conf > "${log_file}" 2>&1 || exit 1 &
46     )
47
48     sleep "${SECONDS_TO_WAIT_FOR_PRIVOXY_TO_START}"
49
50     if [ ! -f "${pid_file}" ]; then
51         echo "Privoxy failed to start or did not start in time"
52         if [ -f "${log_file}" ]; then
53             tail -n 1 "${log_file}"
54         fi
55         exit 1
56     fi
57 }
58
59 stop_privoxy() {
60     local test_dir test_scenario
61     local pid_file
62     test_dir="${1}"
63     test_scenario="${2}"
64     pid_file="${test_dir}/${test_scenario}/../privoxy.pid"
65     if [ -f "${pid_file}" ]; then
66         kill "$(cat "${pid_file}")"
67     fi
68 }
69
70 run_privoxy_tests() {
71     local start_privoxy="$1"
72     local test_scenario="$2"
73     local directory_name="$(dirname "$0")"
74     local test_dir="$(realpath "${directory_name}")"
75     local ret
76
77     echo "Test scenario: ${test_scenario}"
78     $start_privoxy && start_privoxy "${test_dir}" "${test_scenario}"
79
80     "${test_dir}/runtests-wrapper.sh" -A -E -t "${test_dir}/${test_scenario}/data" HTTP HTTPS
81     ret=$?
82
83     $start_privoxy && stop_privoxy "${test_dir}" "${test_scenario}"
84
85     return $ret
86 }
87
88 run_upstream_tests() {
89     local start_privoxy="$1"
90     local directory_name="$(dirname "$0")"
91     local test_dir="$(realpath "${directory_name}")"
92     local ret
93
94     echo "Test scenario: ${UPSTREAM_TEST_SCENARIO}"
95     $start_privoxy && start_privoxy "${test_dir}" "${UPSTREAM_TEST_SCENARIO}"
96
97     "${test_dir}/runtests-wrapper.sh" -A -T HTTP
98     ret=$?
99
100     $start_privoxy && stop_privoxy "${test_dir}" "${UPSTREAM_TEST_SCENARIO}"
101
102     return $ret
103 }
104
105 get_test_scenarios() {
106     local directory_name="$(dirname "$0")"
107     local test_dir="$(realpath "${directory_name}")"
108     local test_scenario
109     local privoxy_config
110
111     for privoxy_config in "${test_dir}"/*/privoxy.conf; do
112         test_scenario="${privoxy_config%%/privoxy.conf}"
113         test_scenario="${test_scenario##$test_dir/}"
114         echo "${test_scenario}"
115     done
116 }
117
118 main() {
119     local test_scenario=""
120     local test_scenarios=""
121     local start_privoxy=true
122
123     while [ -n "$1" ];
124     do
125         case "$1" in
126             "-r")
127                 echo "Not starting privoxy."
128                 start_privoxy=false
129                 shift
130                 ;;
131             "-t")
132                 shift
133                 test_scenarios="$1"
134                 shift
135                 ;;
136             *)
137                 echo "Invalid parameter: $1"
138                 exit 1
139                 ;;
140         esac
141     done
142
143     if [ -z "${test_scenarios}" ]; then
144         test_scenarios="$(get_test_scenarios)"
145     fi
146
147     for test_scenario in ${test_scenarios}; do
148         if [ "${test_scenario}" = "${UPSTREAM_TEST_SCENARIO}" ]; then
149             run_upstream_tests ${start_privoxy} || exit 1
150         else
151             run_privoxy_tests ${start_privoxy} "${test_scenario}" || exit 1
152         fi
153     done
154
155     exit 0
156 }
157
158 main "$@"