2016-01-24 08:39:32 +03:00
|
|
|
#!/bin/bash
|
2011-01-17 15:59:41 +03:00
|
|
|
#
|
2013-06-10 12:52:38 +04:00
|
|
|
# Build a coverage-enabled darkhttpd, run unit tests and calculate coverage.
|
2011-01-17 15:59:41 +03:00
|
|
|
#
|
2013-04-28 15:45:14 +04:00
|
|
|
cd $(dirname $0)
|
2016-01-24 08:39:32 +03:00
|
|
|
declare -r DIR=tmp.httpd.tests
|
|
|
|
declare -r PORT=12346
|
2011-01-17 15:59:41 +03:00
|
|
|
|
|
|
|
if [ ! -e test.py ]; then
|
2016-01-24 08:39:32 +03:00
|
|
|
echo "fatal: can't find test.py. are you in the right directory?" >&2
|
2011-01-17 15:59:41 +03:00
|
|
|
exit 1
|
|
|
|
fi
|
2018-12-10 13:12:24 +03:00
|
|
|
if [[ -z "$CC" ]]; then
|
|
|
|
CC="$(which gcc)"
|
|
|
|
fi
|
|
|
|
if [[ -z "$CLANG" ]]; then
|
|
|
|
CLANG="$(which clang)"
|
|
|
|
fi
|
2013-06-10 14:41:46 +04:00
|
|
|
|
2016-01-24 08:39:32 +03:00
|
|
|
runtests() {
|
|
|
|
if [ -e $DIR ]; then
|
|
|
|
rm -rf $DIR || exit 1
|
|
|
|
fi
|
|
|
|
mkdir $DIR || exit 1
|
|
|
|
mkdir $DIR/forbidden || exit 1
|
|
|
|
chmod 0 $DIR/forbidden || exit 1
|
|
|
|
mkdir $DIR/unreadable || exit 1
|
|
|
|
chmod 0100 $DIR/unreadable || exit 1
|
|
|
|
rm -f darkhttpd.gcda test.out.log test.out.stdout test.out.stderr
|
|
|
|
|
|
|
|
echo "===> run usage statement"
|
|
|
|
# Early exit if we can't even survive usage.
|
|
|
|
./a.out >/dev/null 2>>test.out.stderr || exit 1
|
|
|
|
|
|
|
|
echo "===> run tests against a basic instance (generates darkhttpd.gcda)"
|
|
|
|
./a.out $DIR --port $PORT --log test.out.log \
|
|
|
|
>>test.out.stdout 2>>test.out.stderr &
|
|
|
|
PID=$!
|
|
|
|
kill -0 $PID || exit 1
|
2021-01-17 09:29:23 +03:00
|
|
|
python3 test.py
|
2016-01-24 08:39:32 +03:00
|
|
|
kill $PID
|
|
|
|
wait $PID
|
|
|
|
|
|
|
|
echo "===> run --forward tests"
|
|
|
|
./a.out $DIR --port $PORT \
|
|
|
|
--forward example.com http://www.example.com \
|
|
|
|
--forward secure.example.com https://www.example.com/secure \
|
|
|
|
>>test.out.stdout 2>>test.out.stderr &
|
|
|
|
PID=$!
|
|
|
|
kill -0 $PID || exit 1
|
2021-01-17 09:29:23 +03:00
|
|
|
python3 test_forward.py
|
2016-01-24 08:39:32 +03:00
|
|
|
kill $PID
|
|
|
|
wait $PID
|
|
|
|
|
|
|
|
echo "===> run --forward-all tests"
|
|
|
|
./a.out $DIR --port $PORT \
|
|
|
|
--forward example.com http://www.example.com \
|
|
|
|
--forward-all http://catchall.example.com \
|
|
|
|
>>test.out.stdout 2>>test.out.stderr &
|
|
|
|
PID=$!
|
|
|
|
kill -0 $PID || exit 1
|
2021-01-17 09:29:23 +03:00
|
|
|
python3 test_forward_all.py
|
2016-01-24 08:39:32 +03:00
|
|
|
kill $PID
|
|
|
|
wait $PID
|
|
|
|
|
|
|
|
echo "===> run --no-server-id tests"
|
|
|
|
./a.out $DIR --port $PORT --no-server-id \
|
|
|
|
>>test.out.stdout 2>>test.out.stderr &
|
|
|
|
PID=$!
|
|
|
|
kill -0 $PID || exit 1
|
2021-01-17 09:29:23 +03:00
|
|
|
python3 test_server_id.py
|
2016-01-24 08:39:32 +03:00
|
|
|
kill $PID
|
|
|
|
wait $PID
|
|
|
|
|
|
|
|
echo "===> run mimemap tests"
|
|
|
|
echo "test/type1 a1" > $DIR/mimemap
|
|
|
|
echo "test/this-gets-replaced ap2" >> $DIR/mimemap
|
|
|
|
echo "# this is a comment" >> $DIR/mimemap
|
|
|
|
printf "test/type3\\tapp3\r\n" >> $DIR/mimemap
|
|
|
|
echo "test/type2 ap2" >> $DIR/mimemap
|
|
|
|
./a.out $DIR --port $PORT \
|
|
|
|
--mimetypes $DIR/mimemap \
|
|
|
|
--default-mimetype test/default \
|
|
|
|
>>test.out.stdout 2>>test.out.stderr &
|
|
|
|
PID=$!
|
|
|
|
kill -0 $PID || exit 1
|
2021-01-17 09:29:23 +03:00
|
|
|
python3 test_mimemap.py
|
2016-01-24 08:39:32 +03:00
|
|
|
kill $PID
|
|
|
|
wait $PID
|
|
|
|
|
|
|
|
echo "===> run --no-listing tests"
|
|
|
|
./a.out $DIR --port $PORT --no-listing \
|
|
|
|
>>test.out.stdout 2>>test.out.stderr &
|
|
|
|
PID=$!
|
|
|
|
kill -0 $PID || exit 1
|
2021-01-17 09:29:23 +03:00
|
|
|
python3 test_no_listing.py
|
2016-01-24 08:39:32 +03:00
|
|
|
kill $PID
|
|
|
|
wait $PID
|
|
|
|
|
2018-12-10 15:52:42 +03:00
|
|
|
echo "===> run --timeout tests"
|
|
|
|
./a.out $DIR --port $PORT --timeout 1 \
|
|
|
|
>>test.out.stdout 2>>test.out.stderr &
|
|
|
|
PID=$!
|
|
|
|
kill -0 $PID || exit 1
|
2021-01-17 09:29:23 +03:00
|
|
|
python3 test_timeout.py
|
2018-12-10 15:52:42 +03:00
|
|
|
kill $PID
|
|
|
|
wait $PID
|
|
|
|
|
2020-07-01 14:00:46 +03:00
|
|
|
echo "===> run --auth tests"
|
|
|
|
# Wrong flags:
|
|
|
|
./a.out . --auth >/dev/null 2>/dev/null
|
|
|
|
./a.out . --auth missing_colon >/dev/null 2>/dev/null
|
|
|
|
# Correct flags:
|
|
|
|
./a.out $DIR --port $PORT --auth myuser:mypass \
|
|
|
|
>>test.out.stdout 2>>test.out.stderr &
|
|
|
|
PID=$!
|
|
|
|
kill -0 $PID || exit 1
|
2021-01-17 09:29:23 +03:00
|
|
|
python3 test_auth.py
|
2020-07-01 14:00:46 +03:00
|
|
|
kill $PID
|
|
|
|
wait $PID
|
|
|
|
|
2016-01-24 08:39:32 +03:00
|
|
|
if [[ -s test.out.stderr ]]; then
|
|
|
|
echo "FAIL: stderr should have been empty."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# --- main ---
|
|
|
|
|
|
|
|
# Unit test.
|
2014-03-14 15:50:44 +04:00
|
|
|
echo "===> test_make_safe_uri"
|
2016-01-24 08:39:32 +03:00
|
|
|
$CC -g -O2 -fsanitize=address -fsanitize=undefined \
|
|
|
|
test_make_safe_uri.c -o test_make_safe_uri || exit 1
|
2014-03-14 15:50:44 +04:00
|
|
|
if ./test_make_safe_uri | egrep '^FAIL:'; then
|
|
|
|
echo test_make_safe_uri failed >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2016-01-24 08:39:32 +03:00
|
|
|
# Check that the code builds with various defines.
|
|
|
|
echo "===> building without -DDEBUG"
|
2016-01-27 17:05:27 +03:00
|
|
|
$CC -O2 -Wall ../darkhttpd.c || exit 1
|
2016-01-24 08:39:32 +03:00
|
|
|
echo "===> building with -DNO_IPV6"
|
2016-01-27 17:05:27 +03:00
|
|
|
$CC -O2 -Wall -DNO_IPV6 ../darkhttpd.c || exit 1
|
2016-01-24 08:39:32 +03:00
|
|
|
|
|
|
|
# Do coverage and sanitizers.
|
2020-07-01 13:43:49 +03:00
|
|
|
# In the case of an error being found:
|
2016-01-24 08:39:32 +03:00
|
|
|
# -fsanitize=undefined produces stderr.
|
|
|
|
# -fsanitize=address produces stderr and crashes.
|
2016-01-27 16:41:34 +03:00
|
|
|
# -fsanitize=memory produces stderr and crashes.
|
|
|
|
|
|
|
|
# msan first.
|
2018-12-10 13:12:24 +03:00
|
|
|
if ! $CLANG -v 2>/dev/null; then
|
|
|
|
echo "***WARNING*** Can't find clang."
|
|
|
|
echo "Skipping memory sanitizer. Try setting the \$CLANG env var."
|
2016-01-27 16:41:34 +03:00
|
|
|
else
|
2018-12-10 13:02:46 +03:00
|
|
|
echo "===> building a.out for msan"
|
|
|
|
$CLANG -g -O2 -fsanitize=memory -DDEBUG -DAPBUF_INIT=1 \
|
2016-01-27 16:41:34 +03:00
|
|
|
../darkhttpd.c || exit 1
|
|
|
|
(runtests) || {
|
|
|
|
echo "FAILED! stderr was:"
|
|
|
|
echo "---"
|
|
|
|
cat test.out.stderr
|
|
|
|
echo "---"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# asan and coverage next.
|
2016-01-24 08:39:32 +03:00
|
|
|
echo "===> building a.out and darkhttpd.gcno for coverage + asan + ubsan"
|
|
|
|
$CC -g -O2 -fprofile-arcs -ftest-coverage -fsanitize=address \
|
2020-07-01 13:46:10 +03:00
|
|
|
-fsanitize=undefined -fno-omit-frame-pointer -DDEBUG -DAPBUF_INIT=1 \
|
|
|
|
../darkhttpd.c || exit 1
|
2020-07-01 13:43:49 +03:00
|
|
|
(export ASAN_OPTIONS=detect_leaks=1; runtests) || {
|
2016-01-24 08:39:32 +03:00
|
|
|
echo "FAILED! stderr was:"
|
|
|
|
echo "---"
|
|
|
|
cat test.out.stderr
|
|
|
|
echo "---"
|
|
|
|
exit 1
|
|
|
|
}
|
2016-01-23 12:10:10 +03:00
|
|
|
echo "===> generating report"
|
2011-01-17 15:59:41 +03:00
|
|
|
gcov darkhttpd
|
2013-04-28 15:45:14 +04:00
|
|
|
rm -rf $DIR
|
2015-01-01 12:32:52 +03:00
|
|
|
rm -f darkhttpd.gcda darkhttpd.gcno a.out
|
2016-01-24 08:39:32 +03:00
|
|
|
echo "===> PASSED!"
|
2016-01-23 12:10:10 +03:00
|
|
|
echo "===> read the report: less darkhttpd.c.gcov"
|