Add asan and ubsan to tests, watch stderr.

This commit is contained in:
Emil Mikulic 2016-01-24 16:39:32 +11:00
parent 27c899c165
commit 26e7adea15
2 changed files with 118 additions and 84 deletions

View File

@ -3,8 +3,8 @@ all:
clean: clean:
rm -f *.gcov \ rm -f *.gcov \
cover.out.log \ test.out.log \
cover.out.stderr \ test.out.stderr \
cover.out.stdout \ test.out.stdout \
test.pyc \ test.pyc \
test_make_safe_uri test_make_safe_uri

View File

@ -1,22 +1,18 @@
#!/bin/sh #!/bin/bash
# #
# Build a coverage-enabled darkhttpd, run unit tests and calculate coverage. # Build a coverage-enabled darkhttpd, run unit tests and calculate coverage.
# #
cd $(dirname $0) cd $(dirname $0)
DIR=tmp.httpd.tests declare -r DIR=tmp.httpd.tests
PORT=12346 declare -r PORT=12346
CC=gcc declare -r CC=gcc
if [ ! -e test.py ]; then if [ ! -e test.py ]; then
echo "can't find test.py, aborting" >&2 echo "fatal: can't find test.py. are you in the right directory?" >&2
exit 1 exit 1
fi fi
echo "===> building without -DDEBUG"
$CC -O2 ../darkhttpd.c || exit 1 runtests() {
echo "===> building with -DNO_IPV6"
$CC -O2 -DNO_IPV6 ../darkhttpd.c || exit 1
echo "===> building a.out and darkhttpd.gcno for coverage"
$CC -g -O2 -fprofile-arcs -ftest-coverage -DDEBUG -DAPBUF_INIT=1 ../darkhttpd.c || exit 1
if [ -e $DIR ]; then if [ -e $DIR ]; then
rm -rf $DIR || exit 1 rm -rf $DIR || exit 1
fi fi
@ -25,20 +21,15 @@ mkdir $DIR/forbidden || exit 1
chmod 0 $DIR/forbidden || exit 1 chmod 0 $DIR/forbidden || exit 1
mkdir $DIR/unreadable || exit 1 mkdir $DIR/unreadable || exit 1
chmod 0100 $DIR/unreadable || exit 1 chmod 0100 $DIR/unreadable || exit 1
rm -f darkhttpd.gcda darkhttpd.log rm -f darkhttpd.gcda test.out.log test.out.stdout test.out.stderr
echo "===> test_make_safe_uri"
$CC -g -O2 test_make_safe_uri.c -o test_make_safe_uri || exit 1
if ./test_make_safe_uri | egrep '^FAIL:'; then
echo test_make_safe_uri failed >&2
exit 1
fi
echo "===> run usage statement" echo "===> run usage statement"
./a.out >/dev/null # 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)" echo "===> run tests against a basic instance (generates darkhttpd.gcda)"
./a.out $DIR --port $PORT --log cover.out.log >cover.out.stdout 2>cover.out.stderr & ./a.out $DIR --port $PORT --log test.out.log \
>>test.out.stdout 2>>test.out.stderr &
PID=$! PID=$!
kill -0 $PID || exit 1 kill -0 $PID || exit 1
python test.py python test.py
@ -48,7 +39,8 @@ wait $PID
echo "===> run --forward tests" echo "===> run --forward tests"
./a.out $DIR --port $PORT \ ./a.out $DIR --port $PORT \
--forward example.com http://www.example.com \ --forward example.com http://www.example.com \
--forward secure.example.com https://www.example.com/secure >/dev/null & --forward secure.example.com https://www.example.com/secure \
>>test.out.stdout 2>>test.out.stderr &
PID=$! PID=$!
kill -0 $PID || exit 1 kill -0 $PID || exit 1
python test_forward.py python test_forward.py
@ -58,7 +50,8 @@ wait $PID
echo "===> run --forward-all tests" echo "===> run --forward-all tests"
./a.out $DIR --port $PORT \ ./a.out $DIR --port $PORT \
--forward example.com http://www.example.com \ --forward example.com http://www.example.com \
--forward-all http://catchall.example.com >/dev/null & --forward-all http://catchall.example.com \
>>test.out.stdout 2>>test.out.stderr &
PID=$! PID=$!
kill -0 $PID || exit 1 kill -0 $PID || exit 1
python test_forward_all.py python test_forward_all.py
@ -66,7 +59,8 @@ kill $PID
wait $PID wait $PID
echo "===> run --no-server-id tests" echo "===> run --no-server-id tests"
./a.out $DIR --port $PORT --no-server-id >/dev/null & ./a.out $DIR --port $PORT --no-server-id \
>>test.out.stdout 2>>test.out.stderr &
PID=$! PID=$!
kill -0 $PID || exit 1 kill -0 $PID || exit 1
python test_server_id.py python test_server_id.py
@ -81,7 +75,8 @@ printf "test/type3\\tapp3\r\n" >> $DIR/mimemap
echo "test/type2 ap2" >> $DIR/mimemap echo "test/type2 ap2" >> $DIR/mimemap
./a.out $DIR --port $PORT \ ./a.out $DIR --port $PORT \
--mimetypes $DIR/mimemap \ --mimetypes $DIR/mimemap \
--default-mimetype test/default >/dev/null & --default-mimetype test/default \
>>test.out.stdout 2>>test.out.stderr &
PID=$! PID=$!
kill -0 $PID || exit 1 kill -0 $PID || exit 1
python test_mimemap.py python test_mimemap.py
@ -89,16 +84,55 @@ kill $PID
wait $PID wait $PID
echo "===> run --no-listing tests" echo "===> run --no-listing tests"
./a.out $DIR --port $PORT --no-listing >/dev/null & ./a.out $DIR --port $PORT --no-listing \
>>test.out.stdout 2>>test.out.stderr &
PID=$! PID=$!
kill -0 $PID || exit 1 kill -0 $PID || exit 1
python test_no_listing.py python test_no_listing.py
kill $PID kill $PID
wait $PID wait $PID
if [[ -s test.out.stderr ]]; then
echo "FAIL: stderr should have been empty."
exit 1
fi
}
# --- main ---
# Unit test.
echo "===> test_make_safe_uri"
$CC -g -O2 -fsanitize=address -fsanitize=undefined \
test_make_safe_uri.c -o test_make_safe_uri || exit 1
if ./test_make_safe_uri | egrep '^FAIL:'; then
echo test_make_safe_uri failed >&2
exit 1
fi
# Check that the code builds with various defines.
echo "===> building without -DDEBUG"
$CC -O2 ../darkhttpd.c || exit 1
echo "===> building with -DNO_IPV6"
$CC -O2 -DNO_IPV6 ../darkhttpd.c || exit 1
# Do coverage and sanitizers.
# -fsanitize=undefined produces stderr.
# -fsanitize=address produces stderr and crashes.
echo "===> building a.out and darkhttpd.gcno for coverage + asan + ubsan"
$CC -g -O2 -fprofile-arcs -ftest-coverage -fsanitize=address \
-fsanitize=undefined -DDEBUG -DAPBUF_INIT=1 ../darkhttpd.c || exit 1
(runtests) || {
echo "FAILED! stderr was:"
echo "---"
cat test.out.stderr
echo "---"
exit 1
}
echo "===> generating report" echo "===> generating report"
gcov darkhttpd gcov darkhttpd
rm -rf $DIR rm -rf $DIR
rm -f darkhttpd.gcda darkhttpd.gcno a.out rm -f darkhttpd.gcda darkhttpd.gcno a.out
echo "===> done!" echo "===> PASSED!"
echo "===> read the report: less darkhttpd.c.gcov" echo "===> read the report: less darkhttpd.c.gcov"