test-partial-file-support 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/sh
  2. set -e
  3. TESTDIR=$(readlink -f $(dirname $0))
  4. . $TESTDIR/framework
  5. setupenvironment
  6. configarchitecture 'amd64'
  7. changetowebserver
  8. copysource() {
  9. dd if="$1" bs=1 count="$2" of="$3" 2>/dev/null
  10. touch -d "$(stat --format '%y' "${TESTFILE}")" "$3"
  11. }
  12. testdownloadfile() {
  13. local DOWNLOG='download-testfile.log'
  14. rm -f "$DOWNLOG"
  15. msgtest "Testing download of file $2 with" "$1"
  16. if ! downloadfile "$2" "$3" > "$DOWNLOG"; then
  17. cat "$DOWNLOG"
  18. msgfail
  19. else
  20. msgpass
  21. fi
  22. cat "$DOWNLOG" | while read field hash; do
  23. local EXPECTED
  24. case "$field" in
  25. 'MD5Sum-Hash:') EXPECTED="$(md5sum "$TESTFILE" | cut -d' ' -f 1)";;
  26. 'SHA1-Hash:') EXPECTED="$(sha1sum "$TESTFILE" | cut -d' ' -f 1)";;
  27. 'SHA256-Hash:') EXPECTED="$(sha256sum "$TESTFILE" | cut -d' ' -f 1)";;
  28. 'SHA512-Hash:') EXPECTED="$(sha512sum "$TESTFILE" | cut -d' ' -f 1)";;
  29. *) continue;;
  30. esac
  31. if [ "$4" = '=' ]; then
  32. msgtest 'Test downloaded file for correct' "$field"
  33. else
  34. msgtest 'Test downloaded file does not match in' "$field"
  35. fi
  36. if [ "$EXPECTED" "$4" "$hash" ]; then
  37. msgpass
  38. else
  39. cat "$DOWNLOG"
  40. msgfail "expected: $EXPECTED ; got: $hash"
  41. fi
  42. done
  43. }
  44. testwebserverlaststatuscode() {
  45. STATUS="$(mktemp)"
  46. addtrap "rm $STATUS;"
  47. msgtest 'Test last status code from the webserver was' "$1"
  48. downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" >/dev/null
  49. if [ "$(cat "$STATUS")" = "$1" ]; then
  50. msgpass
  51. else
  52. cat download-testfile.log
  53. msgfail "Status was $(cat "$STATUS")"
  54. fi
  55. }
  56. TESTFILE='aptarchive/testfile'
  57. cp -a ${TESTDIR}/framework $TESTFILE
  58. testrun() {
  59. downloadfile "$1/_config/set/aptwebserver::support::range/true" '/dev/null' >/dev/null
  60. testwebserverlaststatuscode '200'
  61. copysource $TESTFILE 0 ./testfile
  62. testdownloadfile 'no data' "${1}/testfile" './testfile' '='
  63. testwebserverlaststatuscode '200'
  64. copysource $TESTFILE 20 ./testfile
  65. testdownloadfile 'valid partial data' "${1}/testfile" './testfile' '='
  66. testwebserverlaststatuscode '206'
  67. copysource /dev/zero 20 ./testfile
  68. testdownloadfile 'invalid partial data' "${1}/testfile" './testfile' '!='
  69. testwebserverlaststatuscode '206'
  70. copysource $TESTFILE 1M ./testfile
  71. testdownloadfile 'completely downloaded file' "${1}/testfile" './testfile' '='
  72. testwebserverlaststatuscode '416'
  73. copysource /dev/zero 1M ./testfile
  74. testdownloadfile 'too-big partial file' "${1}/testfile" './testfile' '='
  75. testwebserverlaststatuscode '200'
  76. copysource /dev/zero 20 ./testfile
  77. touch ./testfile
  78. testdownloadfile 'old data' "${1}/testfile" './testfile' '='
  79. testwebserverlaststatuscode '200'
  80. downloadfile "$1/_config/set/aptwebserver::support::range/false" '/dev/null' >/dev/null
  81. testwebserverlaststatuscode '200'
  82. copysource $TESTFILE 20 ./testfile
  83. testdownloadfile 'no server support' "${1}/testfile" './testfile' '='
  84. testwebserverlaststatuscode '200'
  85. }
  86. testrun 'http://localhost:8080'
  87. changetohttpswebserver
  88. testrun 'https://localhost:4433'