test-partial-file-support 2.8 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 >&2 "$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 >&2 "$DOWNLOG"
  40. msgfail "expected: $EXPECTED ; got: $hash"
  41. fi
  42. done
  43. }
  44. testwebserverlaststatuscode() {
  45. local DOWNLOG='download-testfile.log'
  46. rm -f "$DOWNLOG"
  47. local STATUS="$(mktemp)"
  48. addtrap "rm $STATUS;"
  49. msgtest 'Test last status code from the webserver was' "$1"
  50. downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG"
  51. if [ "$(cat "$STATUS")" = "$1" ]; then
  52. msgpass
  53. else
  54. cat >&2 "$DOWNLOG"
  55. msgfail "Status was $(cat "$STATUS")"
  56. fi
  57. }
  58. TESTFILE='aptarchive/testfile'
  59. cp -a ${TESTDIR}/framework $TESTFILE
  60. testrun() {
  61. webserverconfig 'aptwebserver::support::range' 'true'
  62. copysource $TESTFILE 0 ./testfile
  63. testdownloadfile 'no data' "${1}/testfile" './testfile' '='
  64. testwebserverlaststatuscode '200'
  65. copysource $TESTFILE 20 ./testfile
  66. testdownloadfile 'valid partial data' "${1}/testfile" './testfile' '='
  67. testwebserverlaststatuscode '206'
  68. copysource /dev/zero 20 ./testfile
  69. testdownloadfile 'invalid partial data' "${1}/testfile" './testfile' '!='
  70. testwebserverlaststatuscode '206'
  71. copysource $TESTFILE 1M ./testfile
  72. testdownloadfile 'completely downloaded file' "${1}/testfile" './testfile' '='
  73. testwebserverlaststatuscode '416'
  74. copysource /dev/zero 1M ./testfile
  75. testdownloadfile 'too-big partial file' "${1}/testfile" './testfile' '='
  76. testwebserverlaststatuscode '200'
  77. copysource /dev/zero 20 ./testfile
  78. touch ./testfile
  79. testdownloadfile 'old data' "${1}/testfile" './testfile' '='
  80. testwebserverlaststatuscode '200'
  81. webserverconfig 'aptwebserver::support::range' 'false'
  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'