testminiwget.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/sh
  2. # $Id: testminiwget.sh,v 1.13 2015/09/03 17:57:44 nanard Exp $
  3. # project miniupnp : http://miniupnp.free.fr/
  4. # (c) 2011-2015 Thomas Bernard
  5. #
  6. # test program for miniwget.c
  7. # is usually invoked by "make check"
  8. #
  9. # This test program :
  10. # 1 - launches a local HTTP server (minihttptestserver)
  11. # 2 - uses testminiwget to retreive data from this server
  12. # 3 - compares served and received data
  13. # 4 - kills the local HTTP server and exits
  14. #
  15. # The script was tested and works with ksh, bash
  16. # it should now also run with dash
  17. TMPD=`mktemp -d -t miniwgetXXXXXXXXXX`
  18. HTTPSERVEROUT="${TMPD}/httpserverout"
  19. EXPECTEDFILE="${TMPD}/expectedfile"
  20. DOWNLOADEDFILE="${TMPD}/downloadedfile"
  21. PORT=
  22. RET=0
  23. case "$HAVE_IPV6" in
  24. n|no|0)
  25. ADDR=localhost
  26. SERVERARGS=""
  27. ;;
  28. *)
  29. ADDR="[::1]"
  30. SERVERARGS="-6"
  31. ;;
  32. esac
  33. #make minihttptestserver
  34. #make testminiwget
  35. # launching the test HTTP server
  36. ./minihttptestserver $SERVERARGS -e $EXPECTEDFILE > $HTTPSERVEROUT &
  37. SERVERPID=$!
  38. while [ -z "$PORT" ]; do
  39. sleep 1
  40. PORT=`cat $HTTPSERVEROUT | sed 's/Listening on port \([0-9]*\)/\1/' `
  41. done
  42. echo "Test HTTP server is listening on $PORT"
  43. URL1="http://$ADDR:$PORT/index.html"
  44. URL2="http://$ADDR:$PORT/chunked"
  45. URL3="http://$ADDR:$PORT/addcrap"
  46. echo "standard test ..."
  47. ./testminiwget $URL1 "${DOWNLOADEDFILE}.1"
  48. if cmp $EXPECTEDFILE "${DOWNLOADEDFILE}.1" ; then
  49. echo "ok"
  50. else
  51. echo "standard test FAILED"
  52. RET=1
  53. fi
  54. echo "chunked transfert encoding test ..."
  55. ./testminiwget $URL2 "${DOWNLOADEDFILE}.2"
  56. if cmp $EXPECTEDFILE "${DOWNLOADEDFILE}.2" ; then
  57. echo "ok"
  58. else
  59. echo "chunked transfert encoding test FAILED"
  60. RET=1
  61. fi
  62. echo "response too long test ..."
  63. ./testminiwget $URL3 "${DOWNLOADEDFILE}.3"
  64. if cmp $EXPECTEDFILE "${DOWNLOADEDFILE}.3" ; then
  65. echo "ok"
  66. else
  67. echo "response too long test FAILED"
  68. RET=1
  69. fi
  70. # kill the test HTTP server
  71. kill $SERVERPID
  72. wait $SERVERPID
  73. # remove temporary files (for success cases)
  74. if [ $RET -eq 0 ]; then
  75. rm -f "${DOWNLOADEDFILE}.1"
  76. rm -f "${DOWNLOADEDFILE}.2"
  77. rm -f "${DOWNLOADEDFILE}.3"
  78. rm -f $EXPECTEDFILE $HTTPSERVEROUT
  79. rmdir ${TMPD}
  80. else
  81. echo "at least one of the test FAILED"
  82. echo "directory ${TMPD} is left intact"
  83. fi
  84. exit $RET