test-partial-file-support 4.1 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='./downloaded/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. local DOWN='./downloaded/testfile'
  84. copysource $TESTFILE 0 $DOWN
  85. testdownloadfile 'no data' "${1}/testfile" "$DOWN" '='
  86. testwebserverlaststatuscode '200' "$DOWNLOADLOG"
  87. copysource $TESTFILE 20 $DOWN
  88. testdownloadfile 'valid partial data' "${1}/testfile" "$DOWN" '='
  89. testwebserverlaststatuscode '206' "$DOWNLOADLOG"
  90. copysource /dev/zero 20 $DOWN
  91. testdownloadfile 'invalid partial data' "${1}/testfile" "$DOWN" '!='
  92. testwebserverlaststatuscode '206' "$DOWNLOADLOG"
  93. webserverconfig 'aptwebserver::closeOnError' 'false'
  94. followuprequest "$1"
  95. webserverconfig 'aptwebserver::closeOnError' 'true'
  96. followuprequest "$1"
  97. webserverconfig 'aptwebserver::closeOnError' 'false'
  98. copysource /dev/zero 1M $DOWN
  99. testdownloadfile 'too-big partial file' "${1}/testfile" "$DOWN" '='
  100. testwebserverlaststatuscode '200' "$DOWNLOADLOG"
  101. copysource /dev/zero 20 $DOWN
  102. touch $DOWN
  103. testdownloadfile 'old data' "${1}/testfile" "$DOWN" '='
  104. testwebserverlaststatuscode '200' "$DOWNLOADLOG"
  105. webserverconfig 'aptwebserver::support::range' 'false'
  106. copysource $TESTFILE 20 $DOWN
  107. testdownloadfile 'no server support' "${1}/testfile" "$DOWN" '='
  108. testwebserverlaststatuscode '200' "$DOWNLOADLOG"
  109. }
  110. serverconfigs() {
  111. msgmsg "${1%%:*}: Test with Content-Length"
  112. webserverconfig 'aptwebserver::chunked-transfer-encoding' 'false'
  113. testrun "$1"
  114. msgmsg "${1%%:*}: Test with Transfer-Encoding: chunked"
  115. webserverconfig 'aptwebserver::chunked-transfer-encoding' 'true'
  116. testrun "$1"
  117. }
  118. serverconfigs 'http://localhost:8080'
  119. changetohttpswebserver
  120. serverconfigs 'https://localhost:4433'