2009-09-10 07:17:25 +08:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# testsuite runner for tinyproxy
|
|
|
|
#
|
2009-09-10 18:09:12 +08:00
|
|
|
# Copyright (C) 2009 Michael Adam <obnox@samba.org>
|
2009-09-10 07:17:25 +08:00
|
|
|
#
|
2009-09-10 18:09:12 +08:00
|
|
|
# This program is free software; you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU General Public License as published by the Free
|
|
|
|
# Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
# more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# this program; if not, see <http://www.gnu.org/licenses/>.
|
2009-09-10 07:17:25 +08:00
|
|
|
|
|
|
|
|
2021-09-22 09:46:40 +08:00
|
|
|
SCRIPTS_DIR=$(cd $(dirname $0) && pwd)
|
2009-09-10 07:17:25 +08:00
|
|
|
BASEDIR=$SCRIPTS_DIR/../..
|
|
|
|
TESTS_DIR=$SCRIPTS_DIR/..
|
|
|
|
TESTENV_DIR=$TESTS_DIR/env
|
2009-09-10 18:53:25 +08:00
|
|
|
LOG_DIR=$TESTENV_DIR/var/log
|
2009-09-10 07:17:25 +08:00
|
|
|
|
|
|
|
TINYPROXY_IP=127.0.0.2
|
|
|
|
TINYPROXY_PORT=12321
|
2010-04-21 23:31:59 +08:00
|
|
|
TINYPROXY_USER=$(id -un)
|
2009-09-10 07:17:25 +08:00
|
|
|
TINYPROXY_PID_DIR=$TESTENV_DIR/var/run/tinyproxy
|
|
|
|
TINYPROXY_PID_FILE=$TINYPROXY_PID_DIR/tinyproxy.pid
|
2010-03-03 06:11:57 +08:00
|
|
|
TINYPROXY_LOG_DIR=$LOG_DIR/tinyproxy
|
2020-09-07 00:48:14 +08:00
|
|
|
TINYPROXY_LOG_FILE=$TINYPROXY_LOG_DIR/tinyproxy.log
|
2009-09-10 07:17:25 +08:00
|
|
|
TINYPROXY_DATA_DIR=$TESTENV_DIR/usr/share/tinyproxy
|
2009-09-15 08:24:00 +08:00
|
|
|
TINYPROXY_CONF_DIR=$TESTENV_DIR/etc/tinyproxy
|
2009-09-10 07:17:25 +08:00
|
|
|
TINYPROXY_CONF_FILE=$TINYPROXY_CONF_DIR/tinyproxy.conf
|
2009-09-15 08:24:00 +08:00
|
|
|
TINYPROXY_FILTER_FILE=$TINYPROXY_CONF_DIR/filter
|
2009-09-10 18:53:25 +08:00
|
|
|
TINYPROXY_STDERR_LOG=$TINYPROXY_LOG_DIR/tinyproxy.stderr.log
|
|
|
|
TINYPROXY_BIN=$BASEDIR/src/tinyproxy
|
2009-09-13 07:48:42 +08:00
|
|
|
TINYPROXY_STATHOST_IP="127.0.0.127"
|
2009-09-10 07:17:25 +08:00
|
|
|
|
|
|
|
WEBSERVER_IP=127.0.0.3
|
|
|
|
WEBSERVER_PORT=32123
|
|
|
|
WEBSERVER_PID_DIR=$TESTENV_DIR/var/run/webserver
|
|
|
|
WEBSERVER_PID_FILE=$WEBSERVER_PID_DIR/webserver.pid
|
|
|
|
WEBSERVER_LOG_DIR=$TESTENV_DIR/var/log/webserver
|
|
|
|
WEBSERVER_BIN_FILE=webserver.pl
|
|
|
|
WEBSERVER_BIN=$SCRIPTS_DIR/$WEBSERVER_BIN_FILE
|
|
|
|
|
2009-09-10 18:53:25 +08:00
|
|
|
WEBCLIENT_LOG=$LOG_DIR/webclient.log
|
|
|
|
WEBCLIENT_BIN=$SCRIPTS_DIR/webclient.pl
|
2009-09-10 07:17:25 +08:00
|
|
|
|
2009-09-10 18:53:25 +08:00
|
|
|
provision_initial() {
|
|
|
|
if test -e $TESTENV_DIR ; then
|
|
|
|
TESTENV_DIR_OLD=$TESTENV_DIR.old
|
|
|
|
if test -e $TESTENV_DIR_OLD ; then
|
|
|
|
rm -rf $TESTENV_DIR_OLD
|
|
|
|
fi
|
|
|
|
mv $TESTENV_DIR $TESTENV_DIR.old
|
2009-09-10 07:17:25 +08:00
|
|
|
fi
|
|
|
|
|
2009-09-10 18:53:25 +08:00
|
|
|
mkdir -p $LOG_DIR
|
|
|
|
}
|
2009-09-10 07:17:25 +08:00
|
|
|
|
2009-09-10 18:53:25 +08:00
|
|
|
provision_tinyproxy() {
|
|
|
|
mkdir -p $TINYPROXY_DATA_DIR
|
2009-09-13 07:09:44 +08:00
|
|
|
cp $BASEDIR/data/templates/default.html $TINYPROXY_DATA_DIR
|
|
|
|
cp $BASEDIR/data/templates/debug.html $TINYPROXY_DATA_DIR
|
|
|
|
cp $BASEDIR/data/templates/stats.html $TINYPROXY_DATA_DIR
|
2009-09-10 18:53:25 +08:00
|
|
|
mkdir -p $TINYPROXY_PID_DIR
|
|
|
|
mkdir -p $TINYPROXY_LOG_DIR
|
|
|
|
mkdir -p $TINYPROXY_CONF_DIR
|
2009-09-10 07:17:25 +08:00
|
|
|
|
2009-09-10 18:53:25 +08:00
|
|
|
cat >>$TINYPROXY_CONF_FILE<<EOF
|
2009-09-10 07:17:25 +08:00
|
|
|
User $TINYPROXY_USER
|
|
|
|
#Group $TINYPROXY_GROUP
|
|
|
|
Port $TINYPROXY_PORT
|
|
|
|
#Bind $TINYPROXY_IP
|
|
|
|
Listen $TINYPROXY_IP
|
|
|
|
Timeout 600
|
2009-09-13 07:48:42 +08:00
|
|
|
StatHost "$TINYPROXY_STATHOST_IP"
|
2009-09-21 05:06:36 +08:00
|
|
|
DefaultErrorFile "$TINYPROXY_DATA_DIR/debug.html"
|
2020-09-13 08:04:00 +08:00
|
|
|
ErrorFile 400 "$TINYPROXY_DATA_DIR/debug.html"
|
|
|
|
ErrorFile 403 "$TINYPROXY_DATA_DIR/debug.html"
|
|
|
|
ErrorFile 501 "$TINYPROXY_DATA_DIR/debug.html"
|
2021-11-21 00:55:21 +08:00
|
|
|
ErrorFile 502 "$TINYPROXY_DATA_DIR/debug.html"
|
2009-09-10 07:17:25 +08:00
|
|
|
StatFile "$TINYPROXY_DATA_DIR/stats.html"
|
2020-09-07 00:48:14 +08:00
|
|
|
Logfile "$TINYPROXY_LOG_FILE"
|
2009-09-10 07:17:25 +08:00
|
|
|
PidFile "$TINYPROXY_PID_FILE"
|
|
|
|
LogLevel Info
|
|
|
|
MaxClients 100
|
|
|
|
Allow 127.0.0.0/8
|
|
|
|
ViaProxyName "tinyproxy"
|
2009-10-11 07:59:35 +08:00
|
|
|
#DisableViaHeader Yes
|
2009-09-10 07:17:25 +08:00
|
|
|
ConnectPort 443
|
|
|
|
ConnectPort 563
|
2020-09-13 08:04:00 +08:00
|
|
|
#FilterURLs On
|
2009-09-15 08:24:00 +08:00
|
|
|
Filter "$TINYPROXY_FILTER_FILE"
|
2009-09-27 18:52:39 +08:00
|
|
|
XTinyproxy Yes
|
2020-09-16 09:21:12 +08:00
|
|
|
AddHeader "X-My-Header1" "Powered by Tinyproxy"
|
|
|
|
AddHeader "X-My-Header2" "Powered by Tinyproxy"
|
|
|
|
AddHeader "X-My-Header3" "Powered by Tinyproxy"
|
2021-11-21 00:55:21 +08:00
|
|
|
Upstream http 255.255.255.255:65535 ".invalid"
|
2009-09-10 07:17:25 +08:00
|
|
|
EOF
|
2009-09-15 08:24:00 +08:00
|
|
|
|
2020-09-13 08:04:00 +08:00
|
|
|
cat << 'EOF' > $TINYPROXY_FILTER_FILE
|
|
|
|
.*\.google-analytics\.com$
|
|
|
|
EOF
|
2009-09-10 18:53:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
start_tinyproxy() {
|
|
|
|
echo -n "starting tinyproxy..."
|
2009-09-11 07:02:52 +08:00
|
|
|
$VALGRIND $TINYPROXY_BIN -c $TINYPROXY_CONF_FILE 2> $TINYPROXY_STDERR_LOG
|
2009-09-13 07:31:05 +08:00
|
|
|
echo " done (listening on $TINYPROXY_IP:$TINYPROXY_PORT)"
|
2009-09-10 18:53:25 +08:00
|
|
|
}
|
|
|
|
|
2020-09-13 08:04:00 +08:00
|
|
|
reload_config() {
|
|
|
|
echo -n "signaling tinyproxy to reload config..."
|
|
|
|
pid=$(cat $TINYPROXY_PID_FILE)
|
2020-09-13 08:31:08 +08:00
|
|
|
#1: SIGHUP
|
|
|
|
kill -1 $pid && echo "ok" || echo "fail"
|
2020-09-13 08:04:00 +08:00
|
|
|
}
|
|
|
|
|
2009-09-10 18:53:25 +08:00
|
|
|
stop_tinyproxy() {
|
|
|
|
echo -n "killing tinyproxy..."
|
2020-09-07 00:20:06 +08:00
|
|
|
pid=$(cat $TINYPROXY_PID_FILE)
|
|
|
|
kill $pid
|
2009-09-10 18:53:25 +08:00
|
|
|
if test "x$?" = "x0" ; then
|
|
|
|
echo " ok"
|
|
|
|
else
|
2020-09-07 00:20:06 +08:00
|
|
|
echo " error killing pid $pid"
|
2020-09-07 00:48:14 +08:00
|
|
|
ps aux | grep tinyproxy
|
|
|
|
echo "### printing logfile"
|
|
|
|
cat $TINYPROXY_LOG_FILE
|
|
|
|
echo "### printing stderr logfile"
|
|
|
|
cat $TINYPROXY_STDERR_LOG
|
2009-09-10 18:53:25 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
provision_webserver() {
|
|
|
|
mkdir -p $WEBSERVER_PID_DIR
|
|
|
|
mkdir -p $WEBSERVER_LOG_DIR
|
|
|
|
}
|
|
|
|
|
|
|
|
start_webserver() {
|
|
|
|
echo -n "starting web server..."
|
|
|
|
$WEBSERVER_BIN --port $WEBSERVER_PORT --log-dir $WEBSERVER_LOG_DIR --pid-file $WEBSERVER_PID_FILE
|
2009-09-13 07:31:05 +08:00
|
|
|
echo " done (listening on $WEBSERVER_IP:$WEBSERVER_PORT)"
|
2009-09-10 18:53:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
stop_webserver() {
|
|
|
|
echo -n "killing webserver..."
|
|
|
|
kill $(cat $WEBSERVER_PID_FILE)
|
|
|
|
if test "x$?" = "x0" ; then
|
|
|
|
echo " ok"
|
|
|
|
else
|
|
|
|
echo " error"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
wait_for_some_seconds() {
|
|
|
|
SECONDS=$1
|
|
|
|
if test "x$SECONDS" = "x" ; then
|
|
|
|
SECONDS=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo -n "waiting for $SECONDS seconds."
|
|
|
|
|
2009-09-27 18:00:40 +08:00
|
|
|
for COUNT in $(seq 1 $SECONDS) ; do
|
2009-09-10 18:53:25 +08:00
|
|
|
sleep 1
|
|
|
|
echo -n "."
|
|
|
|
done
|
|
|
|
echo " done"
|
|
|
|
}
|
|
|
|
|
|
|
|
run_basic_webclient_request() {
|
2020-09-13 08:04:00 +08:00
|
|
|
$WEBCLIENT_BIN $1 $2 > $WEBCLIENT_LOG 2>&1
|
2009-09-10 18:53:25 +08:00
|
|
|
WEBCLIENT_EXIT_CODE=$?
|
|
|
|
if test "x$WEBCLIENT_EXIT_CODE" = "x0" ; then
|
|
|
|
echo " ok"
|
|
|
|
else
|
2010-02-23 07:17:21 +08:00
|
|
|
echo "ERROR ($WEBCLIENT_EXIT_CODE)"
|
2009-09-10 18:53:25 +08:00
|
|
|
echo "webclient output:"
|
|
|
|
cat $WEBCLIENT_LOG
|
2020-09-13 08:04:00 +08:00
|
|
|
echo "######################################"
|
2009-09-10 18:53:25 +08:00
|
|
|
fi
|
2009-09-10 07:17:25 +08:00
|
|
|
|
2010-02-23 07:18:29 +08:00
|
|
|
return $WEBCLIENT_EXIT_CODE
|
|
|
|
}
|
2009-09-10 07:17:25 +08:00
|
|
|
|
2020-09-13 08:04:00 +08:00
|
|
|
run_failure_webclient_request() {
|
|
|
|
ec=$1
|
|
|
|
expected_error=$(($1 - 399))
|
|
|
|
shift
|
|
|
|
$WEBCLIENT_BIN "$1" "$2" "$3" "$4" > $WEBCLIENT_LOG 2>&1
|
|
|
|
WEBCLIENT_EXIT_CODE=$?
|
|
|
|
if test "x$WEBCLIENT_EXIT_CODE" = "x$expected_error" ; then
|
|
|
|
echo " ok, got expected error code $ec"
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
echo "ERROR ($WEBCLIENT_EXIT_CODE)"
|
|
|
|
echo "webclient output:"
|
|
|
|
cat $WEBCLIENT_LOG
|
|
|
|
echo "######################################"
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2009-09-10 18:53:25 +08:00
|
|
|
# "main"
|
2009-09-10 07:17:25 +08:00
|
|
|
|
2009-09-10 18:53:25 +08:00
|
|
|
provision_initial
|
|
|
|
provision_tinyproxy
|
|
|
|
provision_webserver
|
2009-09-10 07:17:25 +08:00
|
|
|
|
2009-09-10 18:53:25 +08:00
|
|
|
start_webserver
|
|
|
|
start_tinyproxy
|
|
|
|
|
2020-09-13 08:04:00 +08:00
|
|
|
wait_for_some_seconds 1
|
2009-09-10 07:17:25 +08:00
|
|
|
|
2010-02-23 07:18:29 +08:00
|
|
|
FAILED=0
|
|
|
|
|
2020-09-13 08:04:00 +08:00
|
|
|
basic_test() {
|
2009-09-10 07:17:25 +08:00
|
|
|
echo -n "checking direct connection to web server..."
|
2009-09-10 18:53:25 +08:00
|
|
|
run_basic_webclient_request "$WEBSERVER_IP:$WEBSERVER_PORT" /
|
2010-02-23 07:18:29 +08:00
|
|
|
test "x$?" = "x0" || FAILED=$((FAILED + 1))
|
2009-09-10 07:17:25 +08:00
|
|
|
|
|
|
|
echo -n "testing connection through tinyproxy..."
|
2009-09-10 18:53:25 +08:00
|
|
|
run_basic_webclient_request "$TINYPROXY_IP:$TINYPROXY_PORT" "http://$WEBSERVER_IP:$WEBSERVER_PORT/"
|
2010-02-23 07:18:29 +08:00
|
|
|
test "x$?" = "x0" || FAILED=$((FAILED + 1))
|
2009-09-10 07:17:25 +08:00
|
|
|
|
2009-09-13 07:52:26 +08:00
|
|
|
echo -n "requesting statspage via stathost url..."
|
|
|
|
run_basic_webclient_request "$TINYPROXY_IP:$TINYPROXY_PORT" "http://$TINYPROXY_STATHOST_IP"
|
2010-02-23 07:18:29 +08:00
|
|
|
test "x$?" = "x0" || FAILED=$((FAILED + 1))
|
2020-09-13 08:04:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ext_test() {
|
|
|
|
echo -n "checking bogus request..."
|
|
|
|
run_failure_webclient_request 400 --method="BIG FART" "$TINYPROXY_IP:$TINYPROXY_PORT" "http://$WEBSERVER_IP:$WEBSERVER_PORT"
|
|
|
|
test "x$?" = "x0" || FAILED=$((FAILED + 1))
|
|
|
|
|
|
|
|
echo -n "testing connection to filtered domain..."
|
|
|
|
run_failure_webclient_request 403 "$TINYPROXY_IP:$TINYPROXY_PORT" "http://badgoy.google-analytics.com/"
|
|
|
|
test "x$?" = "x0" || FAILED=$((FAILED + 1))
|
|
|
|
|
|
|
|
echo -n "requesting connect method to denied port..."
|
|
|
|
run_failure_webclient_request 403 --method=CONNECT "$TINYPROXY_IP:$TINYPROXY_PORT" "localhost:12345"
|
|
|
|
test "x$?" = "x0" || FAILED=$((FAILED + 1))
|
2021-11-21 00:55:21 +08:00
|
|
|
|
|
|
|
echo -n "testing unavailable backend..."
|
|
|
|
run_failure_webclient_request 502 "$TINYPROXY_IP:$TINYPROXY_PORT" "http://bogus.invalid"
|
|
|
|
test "x$?" = "x0" || FAILED=$((FAILED + 1))
|
2020-09-13 08:04:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
basic_test
|
|
|
|
reload_config
|
|
|
|
basic_test
|
|
|
|
ext_test
|
2010-02-23 07:18:29 +08:00
|
|
|
|
|
|
|
echo "$FAILED errors"
|
2009-09-13 07:52:26 +08:00
|
|
|
|
2010-02-23 06:12:51 +08:00
|
|
|
if test "x$TINYPROXY_TESTS_WAIT" = "xyes"; then
|
|
|
|
echo "You can continue using the webserver and tinyproxy."
|
|
|
|
echo -n "hit <enter> to stop the servers and exit: "
|
|
|
|
read READ
|
|
|
|
fi
|
2009-09-10 07:17:25 +08:00
|
|
|
|
2009-09-10 18:53:25 +08:00
|
|
|
stop_tinyproxy
|
|
|
|
stop_webserver
|
2009-09-10 07:17:25 +08:00
|
|
|
|
2009-09-10 18:53:25 +08:00
|
|
|
echo "done"
|
2009-09-10 07:17:25 +08:00
|
|
|
|
2010-02-23 07:18:29 +08:00
|
|
|
exit $FAILED
|