summaryrefslogtreecommitdiffstats
path: root/test/integration/test-partial-file-support
blob: 88fa913246e83f8c8ecdfc1fd8c68fc6d9cf9010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/sh
set -e

TESTDIR="$(readlink -f "$(dirname "$0")")"
. "$TESTDIR/framework"
setupenvironment
configarchitecture 'amd64'

changetowebserver

copysource() {
	dd if="$1" bs=1 count="$2" of="$3" 2>/dev/null
	touch -d "$(stat --format '%y' "${TESTFILE}")" "$3"
}

DOWNLOADLOG='rootdir/tmp/testdownloadfile.log'

testdownloadfile() {
	rm -f "$DOWNLOADLOG"
	msgtest "Testing download of file $2 with" "$1 $5"
	if ! downloadfile "$2" "$3" "$5" > "$DOWNLOADLOG"; then
		cat >&2 "$DOWNLOADLOG"
		msgfail
	else
		msgpass
	fi
	sed -e '/^ <- / s#%20# #g' -e '/^ <- / s#%0a#\n#g' "$DOWNLOADLOG" | grep '^.*-Hash: ' > receivedhashes.log || true
	testsuccess test -s receivedhashes.log
	local HASHES_OK=0
	local HASHES_BAD=0
	while read field hash; do
		local EXPECTED
		case "$field" in
		'MD5Sum-Hash:') EXPECTED="$(md5sum "$TESTFILE" | cut -d' ' -f 1)";;
		'SHA1-Hash:') EXPECTED="$(sha1sum "$TESTFILE" | cut -d' ' -f 1)";;
		'SHA256-Hash:') EXPECTED="$(sha256sum "$TESTFILE" | cut -d' ' -f 1)";;
		'SHA512-Hash:') EXPECTED="$(sha512sum "$TESTFILE" | cut -d' ' -f 1)";;
		'Checksum-FileSize-Hash:')
			#filesize is too weak to check for !=
			if [ "$4" = '=' ]; then
				EXPECTED="$(stat -c '%s' "$TESTFILE")"
			else
				continue
			fi
			;;
		*) continue;;
		esac
		if [ "$4" = '=' ]; then
			msgtest 'Test downloaded file for correct' "$field"
		else
			msgtest 'Test downloaded file does not match in' "$field"
		fi
		if [ "$EXPECTED" "$4" "$hash" ]; then
			msgpass
			HASHES_OK=$((HASHES_OK+1));
		else
			msgfail "expected: $EXPECTED ; got: $hash"
			HASHES_BAD=$((HASHES_BAD+1));
		fi
	done < receivedhashes.log
	msgtest 'At least one good hash and no bad ones'
	if [ $HASHES_OK -eq 0 ] || [ $HASHES_BAD -ne 0 ]; then
		cat >&2 "$DOWNLOADLOG"
		msgfail
	else
		msgpass
	fi
}

TESTFILE='aptarchive/testfile'
cp -a "${TESTDIR}/framework" "$TESTFILE"
cp -a "${TESTDIR}/framework" "${TESTFILE}2"

followuprequest() {
	local DOWN='./downloaded/testfile'

	copysource $TESTFILE 1M $DOWN
	testdownloadfile 'completely downloaded file' "${1}/testfile" "$DOWN" '='
	testwebserverlaststatuscode '416' "$DOWNLOADLOG"

	webserverconfig 'aptwebserver::support::content-range' 'false'
	copysource $TESTFILE 1M $DOWN
	testdownloadfile 'completely downloaded file' "${1}/testfile" "$DOWN" '=' "SHA256:$(sha256sum "$TESTFILE" | cut -d' ' -f 1)"
	testwebserverlaststatuscode '416' "$DOWNLOADLOG"
	webserverconfig 'aptwebserver::support::content-range' 'true'

	copysource $TESTFILE 1M $DOWN
	copysource "${TESTFILE}2" 20 "${DOWN}2"
	msgtest 'Testing download of files with' 'completely downloaded file + partial file'
	testsuccess --nomsg apthelper -o Debug::Acquire::${1%%:*}=1 -o Debug::pkgAcquire::Worker=1 \
		download-file "$1/testfile" "$DOWN" '' "$1/testfile2" "${DOWN}2"
	testwebserverlaststatuscode '206' 'rootdir/tmp/testsuccess.output'
	testsuccess diff -u "$TESTFILE" "${DOWN}"
	testsuccess diff -u "${DOWN}" "${DOWN}2"
}

testrun() {
	webserverconfig 'aptwebserver::support::range' 'true'
	webserverconfig 'aptwebserver::response-header::Accept-Ranges' 'bytes'
	local DOWN='./downloaded/testfile'

	copysource $TESTFILE 0 $DOWN
	testdownloadfile 'no data' "${1}/testfile" "$DOWN" '='
	testwebserverlaststatuscode '200' "$DOWNLOADLOG"

	copysource $TESTFILE 20 $DOWN
	testdownloadfile 'valid partial data' "${1}/testfile" "$DOWN" '='
	testwebserverlaststatuscode '206' "$DOWNLOADLOG"

	copysource /dev/zero 20 $DOWN
	testdownloadfile 'invalid partial data' "${1}/testfile" "$DOWN" '!='
	testwebserverlaststatuscode '206' "$DOWNLOADLOG"

	webserverconfig 'aptwebserver::closeOnError' 'false'
	followuprequest "$1"
	webserverconfig 'aptwebserver::closeOnError' 'true'
	followuprequest "$1"
	webserverconfig 'aptwebserver::closeOnError' 'false'

	copysource /dev/zero 1M $DOWN
	testdownloadfile 'too-big partial file' "${1}/testfile" "$DOWN" '='
	testwebserverlaststatuscode '200' "$DOWNLOADLOG"

	copysource /dev/zero 20 $DOWN
	touch $DOWN
	testdownloadfile 'old data' "${1}/testfile" "$DOWN" '='
	testwebserverlaststatuscode '200' "$DOWNLOADLOG"

	if [ "${1%%:*}" = 'https' ] && expr match "$1" "^.*/redirectme$" >/dev/null; then
		webserverconfig 'aptwebserver::response-header::Accept-Ranges' 'none'
	else
		webserverconfig 'aptwebserver::support::range' 'false'
	fi

	copysource $TESTFILE 20 $DOWN
	testdownloadfile 'no server support' "${1}/testfile" "$DOWN" '='
	testwebserverlaststatuscode '200' "$DOWNLOADLOG"
}

serverconfigs() {
	msgmsg "${1%%:*}: Test with Content-Length"
	webserverconfig 'aptwebserver::chunked-transfer-encoding' 'false'
	testrun "$1"
	msgmsg "${1%%:*}: Test with Transfer-Encoding: chunked"
	webserverconfig 'aptwebserver::chunked-transfer-encoding' 'true'
	testrun "$1"
}

serverconfigs "http://localhost:${APTHTTPPORT}"

changetohttpswebserver

serverconfigs "https://localhost:${APTHTTPSPORT}"

webserverconfig 'aptwebserver::redirect::replace::/redirectme/' "https://localhost:${APTHTTPSPORT}/"
serverconfigs "https://localhost:${APTHTTPSPORT}/redirectme"
serverconfigs "http://localhost:${APTHTTPPORT}/redirectme"