test-partial-file-support 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. DOWNLOADLOG='rootdir/tmp/testdownloadfile.log'
  13. testdownloadfile() {
  14. rm -f "$DOWNLOADLOG"
  15. msgtest "Testing download of file $2 with" "$1"
  16. if ! downloadfile "$2" "$3" > "$DOWNLOADLOG"; then
  17. cat >&2 "$DOWNLOADLOG"
  18. msgfail
  19. else
  20. msgpass
  21. fi
  22. sed -e '/^ <- / s#%20# #g' -e '/^ <- / s#%0a#\n#g' "$DOWNLOADLOG" | grep '^.*-Hash: ' > receivedhashes.log
  23. testsuccess test -s receivedhashes.log
  24. local HASHES_OK=0
  25. local HASHES_BAD=0
  26. while read field hash; do
  27. local EXPECTED
  28. case "$field" in
  29. 'MD5Sum-Hash:') EXPECTED="$(md5sum "$TESTFILE" | cut -d' ' -f 1)";;
  30. 'SHA1-Hash:') EXPECTED="$(sha1sum "$TESTFILE" | cut -d' ' -f 1)";;
  31. 'SHA256-Hash:') EXPECTED="$(sha256sum "$TESTFILE" | cut -d' ' -f 1)";;
  32. 'SHA512-Hash:') EXPECTED="$(sha512sum "$TESTFILE" | cut -d' ' -f 1)";;
  33. 'Checksum-FileSize-Hash:')
  34. #filesize is too weak to check for !=
  35. if [ "$4" = '=' ]; then
  36. EXPECTED="$(stat -c '%s' "$TESTFILE")"
  37. else
  38. continue
  39. fi
  40. ;;
  41. *) continue;;
  42. esac
  43. if [ "$4" = '=' ]; then
  44. msgtest 'Test downloaded file for correct' "$field"
  45. else
  46. msgtest 'Test downloaded file does not match in' "$field"
  47. fi
  48. if [ "$EXPECTED" "$4" "$hash" ]; then
  49. msgpass
  50. HASHES_OK=$((HASHES_OK+1));
  51. else
  52. msgfail "expected: $EXPECTED ; got: $hash"
  53. HASHES_BAD=$((HASHES_BAD+1));
  54. fi
  55. done < receivedhashes.log
  56. msgtest 'At least one good hash and no bad ones'
  57. if [ $HASHES_OK -eq 0 ] || [ $HASHES_BAD -ne 0 ]; then
  58. cat >&2 "$DOWNLOADLOG"
  59. msgfail
  60. else
  61. msgpass
  62. fi
  63. }
  64. TESTFILE='aptarchive/testfile'
  65. cp -a ${TESTDIR}/framework $TESTFILE
  66. cp -a ${TESTDIR}/framework "${TESTFILE}2"
  67. followuprequest() {
  68. local DOWN='./testfile'
  69. copysource $TESTFILE 1M $DOWN
  70. testdownloadfile 'completely downloaded file' "${1}/testfile" "$DOWN" '='
  71. testwebserverlaststatuscode '416' "$DOWNLOADLOG"
  72. copysource $TESTFILE 1M $DOWN
  73. copysource "${TESTFILE}2" 20 "${DOWN}2"
  74. msgtest 'Testing download of files with' 'completely downloaded file + partial file'
  75. testsuccess --nomsg apthelper -o Debug::Acquire::${1%%:*}=1 -o Debug::pkgAcquire::Worker=1 \
  76. download-file "$1/testfile" "$DOWN" '' "$1/testfile2" "${DOWN}2"
  77. testwebserverlaststatuscode '206' 'rootdir/tmp/testsuccess.output'
  78. testsuccess diff -u "$TESTFILE" "${DOWN}"
  79. testsuccess diff -u "${DOWN}" "${DOWN}2"
  80. }
  81. testrun() {
  82. webserverconfig 'aptwebserver::support::range' 'true'
  83. copysource $TESTFILE 0 ./testfile
  84. testdownloadfile 'no data' "${1}/testfile" './testfile' '='
  85. testwebserverlaststatuscode '200' "$DOWNLOADLOG"
  86. copysource $TESTFILE 20 ./testfile
  87. testdownloadfile 'valid partial data' "${1}/testfile" './testfile' '='
  88. testwebserverlaststatuscode '206' "$DOWNLOADLOG"
  89. copysource /dev/zero 20 ./testfile
  90. testdownloadfile 'invalid partial data' "${1}/testfile" './testfile' '!='
  91. testwebserverlaststatuscode '206' "$DOWNLOADLOG"
  92. webserverconfig 'aptwebserver::closeOnError' 'false'
  93. followuprequest "$1"
  94. webserverconfig 'aptwebserver::closeOnError' 'true'
  95. followuprequest "$1"
  96. webserverconfig 'aptwebserver::closeOnError' 'false'
  97. copysource /dev/zero 1M ./testfile
  98. testdownloadfile 'too-big partial file' "${1}/testfile" './testfile' '='
  99. testwebserverlaststatuscode '200' "$DOWNLOADLOG"
  100. copysource /dev/zero 20 ./testfile
  101. touch ./testfile
  102. testdownloadfile 'old data' "${1}/testfile" './testfile' '='
  103. testwebserverlaststatuscode '200' "$DOWNLOADLOG"
  104. webserverconfig 'aptwebserver::support::range' 'false'
  105. copysource $TESTFILE 20 ./testfile
  106. testdownloadfile 'no server support' "${1}/testfile" './testfile' '='
  107. testwebserverlaststatuscode '200' "$DOWNLOADLOG"
  108. }
  109. msgmsg 'http: Test with Content-Length'
  110. webserverconfig 'aptwebserver::chunked-transfer-encoding' 'false'
  111. testrun 'http://localhost:8080'
  112. msgmsg 'http: Test with Transfer-Encoding: chunked'
  113. webserverconfig 'aptwebserver::chunked-transfer-encoding' 'true'
  114. testrun 'http://localhost:8080'
  115. changetohttpswebserver
  116. msgmsg 'https: Test with Content-Length'
  117. webserverconfig 'aptwebserver::chunked-transfer-encoding' 'false'
  118. testrun 'https://localhost:4433'
  119. msgmsg 'https: Test with Transfer-Encoding: chunked'
  120. webserverconfig 'aptwebserver::chunked-transfer-encoding' 'true'
  121. testrun 'https://localhost:4433'