forked from xedakini/replicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit-test
executable file
·298 lines (282 loc) · 8.74 KB
/
unit-test
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#! /bin/bash
PORT=8090
NUM="$1"
PREFIX="/tmp/unittest$NUM"
URL_HTTP="www.w3.org:80/Protocols/HTTP/1.1/rfc2616bis/draft-lafon-rfc2616bis-03.txt"
URL_CHUNKED="jigsaw.w3.org:80/HTTP/ChunkedScript"
URL_FTP="ftp.debian.org:21/debian/doc/FAQ/debian-faq.en.pdf.gz"
PID="/tmp/unittest.$PORT"
if test -z "$1"; then
while $0 $((++i)); do true; done
exit 0
fi
########## AUXILIARY FUNCTIONS ################################################
function startserver
{
test -e $PID && kill -int `cat $PID`
rm -rf $PREFIX.*
mkdir $PREFIX.cache
if ! ./http-replicator -p $PORT -r $PREFIX.cache -v -v $@ --daemon $PREFIX.log > $PID; then
cat $PID
rm $PID
exit 1
fi
}
if which wget > /dev/null; then
function download
{
if test -e $PREFIX.$2; then
WGETARGS="-c"
fi
wget -O $PREFIX.$2 $1 $WGETARGS 1>&2
}
elif which curl > /dev/null; then
function download
{
if test -e $PREFIX.$2; then
CURLARGS="-C -"
fi
curl -o $PREFIX.$2 $CURLARGS $1 1>&2
}
else
echo "error: no download tool available"
exit 1
fi
function abort
{
sleep $1
if kill %%; then
echo
sleep 1
else
echo "error: download finished unexpectedly soon"
exit 1
fi
}
function touchfile
{
FILENAME=$PREFIX.$1
shift
mkdir -p `dirname $FILENAME`
touch $@ $FILENAME
}
function summary
{
sleep .5
echo "============================================================================="
echo " UNIT-TEST $NUM: $1"
echo " ---------------------------------------------------------------------------"
}
function check
{
printf " * %-67s %5s %s\n" "$1" "$2" "$3"
}
function check_exists
{
if test -e $PREFIX.$2; then
check "$1" OK
else
check "$1" ERROR
fi
}
if which md5 > /dev/null; then
function checksum
{
md5 -q $1
}
elif which md5sum cut > /dev/null; then
function checksum
{
md5sum $1 | cut -d ' ' -f 1
}
else
echo "error: no checksum tool available"
exit 1
fi
function check_equal
{
if test ! -e $PREFIX.$2; then
check "$1" ERROR "1st file missing"
elif test ! -e $PREFIX.$3; then
check "$1" ERROR "2nd file missing"
elif test `checksum $PREFIX.$2` != `checksum $PREFIX.$3`; then
check "$1" ERROR "files not equal"
else
check "$1" OK
fi
}
function check_log
{
if grep -q "$2" $PREFIX.log; then
check "$1" OK
else
check "$1" ERROR
fi
}
function stopserver
{
echo "============================================================================="
echo
if test -e $PID; then
kill -int `cat $PID`
rm $PID
fi
}
########## UNIT TESTS #########################################################
set -m
case $1 in
1)
startserver
download http://$URL_HTTP out1
http_proxy=localhost:$PORT download http://$URL_HTTP out2
summary "DOWNLOADING NEW FILE"
check_exists "file cached and finalized" cache/$URL_HTTP
check_equal "separate download and served file are equal" out1 out2
check_equal "cached and served file are equal" cache/$URL_HTTP out2
stopserver
;;
2)
startserver
http_proxy=localhost:$PORT download http://$URL_HTTP out & abort 1
summary "LEAVING PARTIAL FILE IN CACHE"
check_exists "file cached, not finalized" cache/$URL_HTTP.incomplete
stopserver
;;
3)
startserver
http_proxy=localhost:$PORT download http://$URL_HTTP out1
http_proxy=localhost:$PORT download http://$URL_HTTP out2
summary "SERVING FILE FROM CACHE"
check_exists "first file cached and finalized" cache/$URL_HTTP
check_log "second file served from cache" "Reading complete file from cache"
check_equal "cached and first served file are equal" cache/$URL_HTTP out1
check_equal "cached and second served file are equal" cache/$URL_HTTP out2
stopserver
;;
4)
startserver
download http://$URL_HTTP out & abort 1
http_proxy=localhost:$PORT download http://$URL_HTTP out
summary "RESUMING PARTIAL FILE BY CLIENT"
check_log "received complete file" "Server responds HTTP/1.1 200 OK"
check_log "served partial file" "Replicator responds HTTP/1.1 206 Partial Content"
check_equal "cached and served file are equal" cache/$URL_HTTP out
stopserver
;;
5)
startserver
touchfile cache/$URL_HTTP -m -t 190112140000
http_proxy=localhost:$PORT download http://$URL_HTTP out
summary "REDOWNLOADING CHANGED FILE"
check_log "detected complete file in cache" "Checking complete file in cache"
check_log "downloading new file" "Preparing new file in cache"
check_equal "cached and served file are equal" cache/$URL_HTTP out
stopserver
;;
6)
startserver
http_proxy=localhost:$PORT download http://$URL_HTTP out1 & abort 1
http_proxy=localhost:$PORT download http://$URL_HTTP out2
summary "RESUMING PARTIAL UNCHANGED FILE IN CACHE"
check_log "replicator asks for missing part" "Requesting resume of partial file in cache"
check_log "received partial file" "Server responds HTTP/1.1 206 Partial Content"
check_equal "cached and served file are equal" cache/$URL_HTTP out2
stopserver
;;
7)
startserver
touchfile cache/$URL_HTTP.incomplete
http_proxy=localhost:$PORT download http://$URL_HTTP out
summary "RESUMING PARTIAL CHANGED FILE IN CACHE"
check_log "replicator asks for missing part" "Requesting resume of partial file in cache"
check_log "received complete file" "Server responds HTTP/1.1 200 OK"
check_equal "cached and served file are equal" cache/$URL_HTTP out
stopserver
;;
8)
startserver
http_proxy=localhost:$PORT download http://$URL_HTTP out1 > /dev/null & sleep 1
http_proxy=localhost:$PORT download http://$URL_HTTP out2
summary "JOINING DOWNLOADS"
check_log "downloads are joined" "Joined running download"
check_equal "cached and first served file are equal" cache/$URL_HTTP out1
check_equal "cached and second served file are equal" cache/$URL_HTTP out2
stopserver
;;
9)
startserver
download http://$URL_CHUNKED out1
http_proxy=localhost:$PORT download http://$URL_CHUNKED out2
summary "DOWNLOADING NEW FILE, CHUNKED TRANSFER"
check_equal "separate download and served file are equal" out1 out2
check_log "server sends chunked data" "Transfer-Encoding: chunked"
check_log "processing chunked data" "Switching to ChunkedDataResponse"
check_equal "cached and served file are equal" cache/$URL_CHUNKED out2
stopserver
;;
10)
startserver
download ftp://$URL_FTP out1
ftp_proxy=localhost:$PORT download ftp://$URL_FTP out2
summary "DOWNLOADING NEW FILE, FTP TRANSFER"
check_equal "separate download and served file are equal" out1 out2
check_equal "cached and served file are equal" cache/$URL_FTP out2
stopserver
;;
11)
startserver
ftp_proxy=localhost:$PORT download ftp://$URL_FTP out1
ftp_proxy=localhost:$PORT download ftp://$URL_FTP out2
summary "SERVING FILE FROM CACHE, FTP TRANSFER"
check_exists "first file cached and finalized" cache/$URL_FTP
check_log "second file served from cache" "Reading complete file from cache"
check_equal "cached and first served file are equal" cache/$URL_FTP out1
check_equal "cached and second served file are equal" cache/$URL_FTP out2
stopserver
;;
12)
startserver
ftp_proxy=localhost:$PORT download ftp://$URL_FTP out1 & abort 1
ftp_proxy=localhost:$PORT download ftp://$URL_FTP out2
summary "RESUMING PARTIAL UNCHANGED FILE IN CACHE, FTP TRANSFER"
check_log "replicator resumes file" "Resuming partial file in cache"
check_equal "cached and served file are equal" cache/$URL_FTP out2
stopserver
;;
13)
startserver --limit 10
http_proxy=localhost:$PORT download http://$URL_HTTP out
summary "RATE CONTROL"
check "download speed; should be approximately 10240" CHECK
check_equal "cached and served file are equal" cache/$URL_HTTP out
stopserver
;;
14)
startserver --static
http_proxy=localhost:$PORT download http://$URL_HTTP out1
http_proxy=localhost:$PORT download http://$URL_HTTP out2
summary "STATIC MODE"
check_log "serving directly from cache without consulting server" "Static mode; serving file directly from cache"
check_equal "cached and served file are equal" cache/$URL_HTTP out2
stopserver
;;
15)
startserver --offline
http_proxy=localhost:$PORT download http://$URL_HTTP out
summary "OFF-LINE MODE"
check_log "refusing to connect to server" "AssertionError: operating in off-line mode"
stopserver
;;
16)
startserver --flat
http_proxy=localhost:$PORT download http://$URL_HTTP out
summary "DOWNLOADING NEW FILE, FLAT MODE"
check_log "serving complete file" "Replicator responds HTTP/1.1 200 OK"
check_exists "file cached and finalized" cache/`basename $URL_HTTP`
stopserver
;;
*)
exit 1
;;
esac
exit 0