2014-03-15 16:59:03 +00:00
|
|
|
#!/bin/sh
|
2023-12-30 16:35:35 +00:00
|
|
|
# $Id: testminiwget.sh,v 1.20 2022/10/21 21:09:42 nanard Exp $
|
|
|
|
# vim: tabstop=4 shiftwidth=4 noexpandtab
|
2014-03-15 16:59:03 +00:00
|
|
|
# project miniupnp : http://miniupnp.free.fr/
|
2023-12-30 16:35:35 +00:00
|
|
|
# or https://miniupnp.tuxfamily.org/
|
|
|
|
# (c) 2011-2022 Thomas Bernard
|
2014-03-15 16:59:03 +00:00
|
|
|
#
|
|
|
|
# test program for miniwget.c
|
|
|
|
# is usually invoked by "make check"
|
|
|
|
#
|
|
|
|
# This test program :
|
|
|
|
# 1 - launches a local HTTP server (minihttptestserver)
|
2023-12-30 16:35:35 +00:00
|
|
|
# 2 - uses testminiwget to retrieve data from this server
|
2014-03-15 16:59:03 +00:00
|
|
|
# 3 - compares served and received data
|
|
|
|
# 4 - kills the local HTTP server and exits
|
|
|
|
#
|
2023-12-30 16:35:35 +00:00
|
|
|
# The script was tested and works with ksh, bash
|
|
|
|
# it should now also run with dash
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2023-12-30 16:35:35 +00:00
|
|
|
TMPD=`mktemp -d -t miniwgetXXXXXXXXXX`
|
|
|
|
if [ -z "$TESTSERVER" ] ; then
|
|
|
|
TESTSERVER=./build/minihttptestserver
|
|
|
|
fi
|
|
|
|
if [ -z "$TESTMINIWGET" ] ; then
|
|
|
|
TESTMINIWGET=./build/testminiwget
|
|
|
|
fi
|
|
|
|
HTTPSERVEROUT="${TMPD}/httpserverout"
|
|
|
|
EXPECTEDFILE="${TMPD}/expectedfile"
|
|
|
|
DOWNLOADEDFILE="${TMPD}/downloadedfile"
|
2014-03-15 16:59:03 +00:00
|
|
|
PORT=
|
|
|
|
RET=0
|
2023-12-30 16:35:35 +00:00
|
|
|
IPCONFIG=$(which ifconfig)
|
|
|
|
IP=$(which ip)
|
|
|
|
if [ "$IP" ] ; then
|
|
|
|
if ! $IP addr | grep inet6 ; then
|
|
|
|
HAVE_IPV6=no
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [ -z "$IPCONFIG" ] ; then
|
|
|
|
IPCONFIG="/sbin/ifconfig"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! $IPCONFIG -a | grep inet6 ; then
|
|
|
|
HAVE_IPV6=no
|
|
|
|
fi
|
|
|
|
fi
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2023-12-30 16:35:35 +00:00
|
|
|
case "$HAVE_IPV6" in
|
|
|
|
n|no|0)
|
|
|
|
ADDR=localhost
|
|
|
|
SERVERARGS=""
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
ADDR="[::1]"
|
|
|
|
SERVERARGS="-6"
|
|
|
|
;;
|
|
|
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
if [ ! -x "$TESTSERVER" ] || [ ! -x "$TESTMINIWGET" ] ; then
|
|
|
|
echo "Please build $TESTSERVER and $TESTMINIWGET"
|
|
|
|
#make minihttptestserver
|
|
|
|
#make testminiwget
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-03-15 16:59:03 +00:00
|
|
|
|
|
|
|
# launching the test HTTP server
|
2023-12-30 16:35:35 +00:00
|
|
|
$TESTSERVER $SERVERARGS -e $EXPECTEDFILE > $HTTPSERVEROUT &
|
|
|
|
SERVERPID=$!
|
|
|
|
while [ -z "$PORT" ]; do
|
|
|
|
sleep 1
|
2014-03-15 16:59:03 +00:00
|
|
|
PORT=`cat $HTTPSERVEROUT | sed 's/Listening on port \([0-9]*\)/\1/' `
|
|
|
|
done
|
2023-12-30 16:35:35 +00:00
|
|
|
if [ "$PORT" = "*** ERROR ***" ]; then
|
|
|
|
echo "HTTP test server error"
|
|
|
|
echo "Network config :"
|
|
|
|
$IPCONFIG -a
|
|
|
|
exit 2
|
|
|
|
fi
|
2014-03-15 16:59:03 +00:00
|
|
|
echo "Test HTTP server is listening on $PORT"
|
|
|
|
|
|
|
|
URL1="http://$ADDR:$PORT/index.html"
|
|
|
|
URL2="http://$ADDR:$PORT/chunked"
|
|
|
|
URL3="http://$ADDR:$PORT/addcrap"
|
2023-12-30 16:35:35 +00:00
|
|
|
URL4="http://$ADDR:$PORT/malformed"
|
2014-03-15 16:59:03 +00:00
|
|
|
|
|
|
|
echo "standard test ..."
|
2023-12-30 16:35:35 +00:00
|
|
|
$TESTMINIWGET $URL1 "${DOWNLOADEDFILE}.1"
|
2014-03-15 16:59:03 +00:00
|
|
|
if cmp $EXPECTEDFILE "${DOWNLOADEDFILE}.1" ; then
|
|
|
|
echo "ok"
|
|
|
|
else
|
|
|
|
echo "standard test FAILED"
|
|
|
|
RET=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "chunked transfert encoding test ..."
|
2023-12-30 16:35:35 +00:00
|
|
|
$TESTMINIWGET $URL2 "${DOWNLOADEDFILE}.2"
|
2014-03-15 16:59:03 +00:00
|
|
|
if cmp $EXPECTEDFILE "${DOWNLOADEDFILE}.2" ; then
|
|
|
|
echo "ok"
|
|
|
|
else
|
|
|
|
echo "chunked transfert encoding test FAILED"
|
|
|
|
RET=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "response too long test ..."
|
2023-12-30 16:35:35 +00:00
|
|
|
$TESTMINIWGET $URL3 "${DOWNLOADEDFILE}.3"
|
2014-03-15 16:59:03 +00:00
|
|
|
if cmp $EXPECTEDFILE "${DOWNLOADEDFILE}.3" ; then
|
|
|
|
echo "ok"
|
|
|
|
else
|
|
|
|
echo "response too long test FAILED"
|
|
|
|
RET=1
|
|
|
|
fi
|
|
|
|
|
2023-12-30 16:35:35 +00:00
|
|
|
echo "malformed response test ..."
|
|
|
|
$TESTMINIWGET $URL4 "${DOWNLOADEDFILE}.4"
|
|
|
|
|
2014-03-15 16:59:03 +00:00
|
|
|
# kill the test HTTP server
|
2023-12-30 16:35:35 +00:00
|
|
|
kill $SERVERPID
|
|
|
|
wait $SERVERPID
|
2014-03-15 16:59:03 +00:00
|
|
|
|
|
|
|
# remove temporary files (for success cases)
|
|
|
|
if [ $RET -eq 0 ]; then
|
|
|
|
rm -f "${DOWNLOADEDFILE}.1"
|
|
|
|
rm -f "${DOWNLOADEDFILE}.2"
|
|
|
|
rm -f "${DOWNLOADEDFILE}.3"
|
|
|
|
rm -f $EXPECTEDFILE $HTTPSERVEROUT
|
2023-12-30 16:35:35 +00:00
|
|
|
rmdir ${TMPD}
|
2014-03-15 16:59:03 +00:00
|
|
|
else
|
|
|
|
echo "at least one of the test FAILED"
|
2023-12-30 16:35:35 +00:00
|
|
|
echo "directory ${TMPD} is left intact"
|
2014-03-15 16:59:03 +00:00
|
|
|
fi
|
|
|
|
exit $RET
|
|
|
|
|