test-partial-file-support 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 $5"
  16. if ! downloadfile "$2" "$3" "$5" > "$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='./downloaded/testfile'
  69. copysource $TESTFILE 1M $DOWN
  70. testdownloadfile 'completely downloaded file' "${1}/testfile" "$DOWN" '='
  71. testwebserverlaststatuscode '416' "$DOWNLOADLOG"
  72. webserverconfig 'aptwebserver::support::content-range' 'false'
  73. copysource $TESTFILE 1M $DOWN
  74. testdownloadfile 'completely downloaded file' "${1}/testfile" "$DOWN" '=' "SHA256:$(sha256sum "$TESTFILE" | cut -d' ' -f 1)"
  75. testwebserverlaststatuscode '416' "$DOWNLOADLOG"
  76. webserverconfig 'aptwebserver::support::content-range' 'true'
  77. copysource $TESTFILE 1M $DOWN
  78. copysource "${TESTFILE}2" 20 "${DOWN}2"
  79. msgtest 'Testing download of files with' 'completely downloaded file + partial file'
  80. testsuccess --nomsg apthelper -o Debug::Acquire::${1%%:*}=1 -o Debug::pkgAcquire::Worker=1 \
  81. download-file "$1/testfile" "$DOWN" '' "$1/testfile2" "${DOWN}2"
  82. testwebserverlaststatuscode '206' 'rootdir/tmp/testsuccess.output'
  83. testsuccess diff -u "$TESTFILE" "${DOWN}"
  84. testsuccess diff -u "${DOWN}" "${DOWN}2"
  85. }
  86. testrun() {
  87. webserverconfig 'aptwebserver::support::range' 'true'
  88. webserverconfig 'aptwebserver::response-header::Accept-Ranges' 'bytes'
  89. local DOWN='./downloaded/testfile'
  90. copysource $TESTFILE 0 $DOWN
  91. testdownloadfile 'no data' "${1}/testfile" "$DOWN" '='
  92. testwebserverlaststatuscode '200' "$DOWNLOADLOG"
  93. copysource $TESTFILE 20 $DOWN
  94. testdownloadfile 'valid partial data' "${1}/testfile" "$DOWN" '='
  95. testwebserverlaststatuscode '206' "$DOWNLOADLOG"
  96. copysource /dev/zero 20 $DOWN
  97. testdownloadfile 'invalid partial data' "${1}/testfile" "$DOWN" '!='
  98. testwebserverlaststatuscode '206' "$DOWNLOADLOG"
  99. webserverconfig 'aptwebserver::closeOnError' 'false'
  100. followuprequest "$1"
  101. webserverconfig 'aptwebserver::closeOnError' 'true'
  102. followuprequest "$1"
  103. webserverconfig 'aptwebserver::closeOnError' 'false'
  104. copysource /dev/zero 1M $DOWN
  105. testdownloadfile 'too-big partial file' "${1}/testfile" "$DOWN" '='
  106. testwebserverlaststatuscode '200' "$DOWNLOADLOG"
  107. copysource /dev/zero 20 $DOWN
  108. touch $DOWN
  109. testdownloadfile 'old data' "${1}/testfile" "$DOWN" '='
  110. testwebserverlaststatuscode '200' "$DOWNLOADLOG"
  111. if [ "${1%%:*}" = 'https' ] && expr match "$1" "^.*/redirectme$" >/dev/null; then
  112. webserverconfig 'aptwebserver::response-header::Accept-Ranges' 'none'
  113. else
  114. webserverconfig 'aptwebserver::support::range' 'false'
  115. fi
  116. copysource $TESTFILE 20 $DOWN
  117. testdownloadfile 'no server support' "${1}/testfile" "$DOWN" '='
  118. testwebserverlaststatuscode '200' "$DOWNLOADLOG"
  119. }
  120. serverconfigs() {
  121. msgmsg "${1%%:*}: Test with Content-Length"
  122. webserverconfig 'aptwebserver::chunked-transfer-encoding' 'false'
  123. testrun "$1"
  124. msgmsg "${1%%:*}: Test with Transfer-Encoding: chunked"
  125. webserverconfig 'aptwebserver::chunked-transfer-encoding' 'true'
  126. testrun "$1"
  127. }
  128. serverconfigs "http://localhost:${APTHTTPPORT}"
  129. changetohttpswebserver
  130. serverconfigs "https://localhost:${APTHTTPSPORT}"
  131. webserverconfig 'aptwebserver::redirect::replace::/redirectme/' "https://localhost:${APTHTTPSPORT}/"
  132. serverconfigs "https://localhost:${APTHTTPSPORT}/redirectme"
  133. serverconfigs "http://localhost:${APTHTTPPORT}/redirectme"