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