-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctional_test.sh
executable file
·503 lines (381 loc) · 13.8 KB
/
functional_test.sh
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/bin/bash
echo -----------------------
echo httpsrv functional test
echo -----------------------
# Server is expected to run with the following configuration
host_and_port="localhost:8080"
if [ ! -z $1 ]; then
host_and_port="$1"
fi
working_dir="$HOME/.httpsrv"
# ------------------------------------------------------------------------------
# Utils
# ------------------------------------------------------------------------------
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
checkCommand() {
commandName=$1
if ! [ -x "$(command -v $commandName)" ]; then
echo 'Error: $commandName is not installed.' >&2
exit 1
fi
}
cleanUpFile () {
fileToClean=$1
sed -i "s/\"//g" $fileToClean
sed -i "s/,//g" $fileToClean
sed -i '/^$/d' $fileToClean
}
fail() {
failMsg=$1
set +x
printf "\n[$RED Error $NC] $failMsg\n" >&2
exit 1
}
success() {
timestamp=`date`
echo "[ ${GREEN}OK${NC} $timestamp] $1" >> $resultfile
}
# ------------------------------------------------------------------------------
# Checks needed tools are there
# ------------------------------------------------------------------------------
checkCommand "curl"
checkCommand "sed"
checkCommand "awk"
checkCommand "unzip"
checkCommand "sha256sum"
checkCommand "diff"
jsonvalidator="jsonlint-php"
checkCommand $jsonvalidator
set -x
tmp_dir=$(mktemp -d -t resources-XXXXXXXXXX)
tmp_dir2=$(mktemp -d -t resources-XXXXXXXXXX)
rm -f $working_dir/File*.txt
if [ -d "$tmp_dir" ]; then
echo "Created ${tmp_dir}..."
else
echo "Error: ${tmp_dir} not found. Can not continue." >&2
exit 1
fi
listfile=$tmp_dir"/list.tmp"
resultfile=$tmp_dir"/result.tmp"
allidsfile=$tmp_dir"/allids.tmp"
mruidsfile=$tmp_dir"/mruids.tmp"
threeidsfile=$tmp_dir"/threeids.tmp"
sortedmruidsfile=$tmp_dir"/sortedmruids.tmp"
NUMOFFILES=50
#Create a bunch of text files with different growing sizes
CONTENT=""
echo "Creating $NUMOFFILES files in $tmp_dir ..."
for i in `seq -f "%02g" 1 $NUMOFFILES`
do
ADDCONTENT=" Content"
CONTENT+=$ADDCONTENT
echo $CONTENT
echo $CONTENT > "$tmp_dir/File${i}.txt"
done
rm -f $resultfile
# ------------------------------------------------------------------------------
# Upload $NUMOFFILES files via http onto remote repository
# ------------------------------------------------------------------------------
listcontent=`find $tmp_dir -type f -name *.txt -exec curl -F file=@{} $host_and_port/store \;`
echo $listcontent | sed -r "s/\}/\\n/g" > $listfile
chk=`cat $listfile | grep id | grep name | grep timestamp | wc -l`
if [ $chk = "$NUMOFFILES" ]; then
success "POST /store: all $NUMOFFILES files uploaded without errors"
else
fail "POST /store: Expected $NUMOFFILES notifications"
fi
cleanUpFile $listfile
# ------------------------------------------------------------------------------
# Get the list of ids in the local temporary dir
# ------------------------------------------------------------------------------
rm -f $allidsfile
echo "Searching for 3 most recent files in $tmp_dir"
while read p; do
ok=0
curl $host_and_port/files/`echo "$p" | awk '{print $3}';` | grep id | awk '{print $2}' >> $allidsfile && ok=1
id=`echo $p | awk '{print $3 " for " $5}';`
[[ ! -z "$first_id" ]] && [[ -z "$second_id" ]] && second_id=`echo $p | awk '{print $3}'`
[[ -z "$first_id" ]] && first_id=`echo $p | awk '{print $3}'`
if [ $ok = "1" ]; then
success "GET /files/$id: succeded"
else
fail "GET /files/<${id}>: can't get a correct answer"
fi
done < $listfile
cleanUpFile $allidsfile
# ------------------------------------------------------------------------------
# Select 3 mru from local repository
# ------------------------------------------------------------------------------
tail -n 3 $allidsfile | sort > $threeidsfile
echo "Getting the mrufils from http server"
rm -f $mruidsfile
# ------------------------------------------------------------------------------
# Query the server for getting the mrufiles and check if the content matches
# with 3 mru repository files
# ------------------------------------------------------------------------------
curl $host_and_port/mrufiles | grep id >> $mruidsfile
cleanUpFile $mruidsfile
cp -f $mruidsfile $sortedmruidsfile
cat $sortedmruidsfile | awk '{print $2}' > $mruidsfile
cat $mruidsfile | sort > $sortedmruidsfile
ok=0
diff $threeidsfile $sortedmruidsfile && ok=1
if [ $ok = "1" ]; then
success "GET /mrufiles: Get OK, content validated"
else
fail "GET /mrufiles: response and actual f/s files look different"
fi
# ------------------------------------------------------------------------------
# Get a single zip file
# ------------------------------------------------------------------------------
ok=0
curl --output $tmp_dir2/$first_id.zip $host_and_port/files/$first_id/zip && ok=1
if [ $ok = "1" ]; then
success "GET /files/$first_id/zip: zip file downloaded"
else
fail "GET /files/$first_id/zip: Can't download the zip file"
fi
filename=`zipinfo $tmp_dir2/$first_id.zip | grep File | awk '{ print $9 }'`
ok=0
cd $tmp_dir2 && unzip $tmp_dir2/$first_id.zip && cd - && ok=1
if [ $ok = "0" ]; then
fail "GET /files/$first_id/zip: failed downloading the zip file"
fi
diff $tmp_dir2/$filename $tmp_dir/$filename || ok=0
if [ $ok = "0" ]; then
fail "GET /files/$first_id/zip: unexpected zip archive content"
fi
# ------------------------------------------------------------------------------
# Get mrufiles zip file
# ------------------------------------------------------------------------------
ok=0
rm -f $tmp_dir2/*
curl --output $tmp_dir2/mrufiles.zip $host_and_port/mrufiles/zip && ok=1
if [ $ok = "0" ]; then
fail "GET /mrufiles/zip: Can't download the mrufiles archive"
fi
ok=0
cd $tmp_dir2 && unzip ./mrufiles.zip && rm mrufiles.zip && cd - && ok=1
if [ $ok = "0" ]; then
cd -
fail "GET /mrufiles/zip: Failed uncompress the mrufiles.zip"
fi
ok=0
cd $tmp_dir2
ls `curl $host_and_port/mrufiles | grep File | sed "s/\"//g" | sed "s/,//g" | awk '{print $2}'` && ok=1
filescount=`ls *.txt | wc -w`
cd -
if [ $ok = "1" ] && [ $filescount = "3" ]; then
success "GET /mrufiles/zip: mrufiles entry count verified"
else
fail "GET /mrufiles/zip: Unexpected number of entries in mrufiles.zip archive"
fi
# ------------------------------------------------------------------------------
# Verify the JSON validity of JSON httpsrv resposese to fils and mrufils
# command
# ------------------------------------------------------------------------------
function getRequest() {
getRequestType=$1
ok=0
curl $host_and_port/$getRequestType > $tmp_dir/$getRequestType.json && ok=1
if [ $ok = "0" ]; then
fail "GET /$getRequestType: Can't get an answer"
fi
ok=0
eval "$jsonvalidator $tmp_dir/$getRequestType.json && ok=1" 2>/dev/null
if [ $ok = "0" ]; then
fail "GET /$getRequestType: Can't validate JSON"
fi
success "GET /$getRequestType: JSON Valid"
}
getRequest files
getRequest mrufiles
# ------------------------------------------------------------------------------
# TIMESTAMP validations
# ------------------------------------------------------------------------------
#GET /file/<id>/zip
#check that the first_id related entry now is into mrufiles list
#got in a previous GET /file/<id>/id is part of mrufiles.json
sleep 1.5
touch $working_dir/File01.txt
touch $working_dir/File02.txt
touch $working_dir/File03.txt
sync
id=`echo -n File04.txt | sha256sum | awk '{print $1}'`
ok=0
curl $host_and_port/mrufiles | grep $id >/dev/null || ok=1
if [ $ok = "0" ]; then
fail "GET /files/$first_id: %first_id should not be in the mrulist now"
fi
# make sure timestamp distance at least is 1 sec
sleep 1.5
ok=0
curl --output $tmp_dir2/$id.zip $host_and_port/files/$id/zip && ok=1
if [ $ok = "0" ]; then
fail "GET /files/$id: Can't download zip file"
fi
ok=0
curl $host_and_port/mrufiles | grep $id >/dev/null && ok=1
if [ $ok = "0" ]; then
fail "GET /files/$id: Can't get the mru list"
fi
success "GET /files/$id/zip: Timestamp updated correctly"
#GET /file/<id>
#check that the first_id related entry now is into mrufiles list
id=`echo -n File05.txt | sha256sum | awk '{print $1}'`
ok=1
curl $host_and_port/mrufiles | grep $id && ok=0
if [ $ok = "0" ]; then
fail "GET /files/$id: the id is not supposed to be in the MRU list"
fi
# make sure timestamp distance at least is 1 sec
sleep 1.5
ok=0
curl $host_and_port/files/$id > $tmp_dir/aFile.json && ok=1
if [ $ok = "0" ]; then
fail "GET /files/$second_id: Can't get the file descriptor"
fi
ok=0
eval "$jsonvalidator $tmp_dir/aFile.json && ok=1" 2>/dev/null
if [ $ok = "0" ]; then
fail "GET /files/$id : Can't validate the JSON output"
else
success "GET /files/$id: JSON response for single id is Valid"
fi
ok=0
curl $host_and_port/mrufiles | grep $id >/dev/null && ok=1
if [ $ok = "0" ]; then
fail "GET /files/$second_id: Can't get the mru list"
fi
success "GET /files/$second_id: timestamp looks OK"
# ------------------------------------------------------------------------------
# Big File Test
# ------------------------------------------------------------------------------
bigFileSrc="/usr/share/dict/words"
bigFileName="bigFile.txt"
cp -f $bigFileSrc $tmp_dir/$bigFileName
bigfileid=`echo -n ${bigFileName} | sha256sum | awk '{print $1}'`
ok=0
cd $tmp_dir && curl -F file=@${bigFileName} $host_and_port/store > $bigFileName.json && cd - && ok=1
if [ $ok = "0" ]; then
fail "POST $bigFileName/store: Cannot transfer ${tmp_dir}/${bigFileName}"
fi
ok=0
grep $bigfileid $tmp_dir/$bigFileName.json && ok=1
if [ $ok = "0" ]; then
fail "POST $bigFileName/store: Invalid response for ${tmp_dir}/${bigFileName}"
fi
success "POST store/$bigFileName: the 'big file' has been uploaded correctly"
# ------------------------------------------------------------------------------
# Zero File Test
# ------------------------------------------------------------------------------
zeroFileName="zeroFile.txt"
touch $tmp_dir/$zeroFileName
zerofileid=`echo -n ${zeroFileName} | sha256sum | awk '{print $1}'`
ok=0
cd $tmp_dir && curl -F file=@${zeroFileName} $host_and_port/store > $zeroFileName.json && cd - && ok=1
if [ $ok = "0" ]; then
fail "POST $zeroFileName/store: Cannot transfer ${tmp_dir}/${zeroFileName}"
fi
ok=0
grep $zerofileid $tmp_dir/$zeroFileName.json && grep '"size": 0,' $tmp_dir/$zeroFileName.json && ok=1
if [ $ok = "0" ]; then
fail "POST $zeroFileName/store: Invalid response for ${tmp_dir}/${zeroFileName}"
fi
rm -f $tmp_dir/$zeroFileName
ok=0
curl --output $tmp_dir/$zerofileid.zip $host_and_port/files/$zerofileid/zip && ok=1
if [ $ok = "0" ]; then
fail "GET /files/$id/zip: Zero size file download failed"
fi
ok=0
cd $tmp_dir && unzip $tmp_dir/$zerofileid.zip && cd - && ok=1
if [ $ok = "0" ]; then
fail "GET /files/$id/zip: Zip file not found"
fi
FILESIZE=$(stat -c%s "$zeroFileName")
if [ $FILESIZE -ne "0" ]; then
fail "GET /files/$id/zip: Zip file size $FILESIZE != 0"
fi
success "POST store/$zeroFileName: zero-size file is handled correctly"
# ------------------------------------------------------------------------------
# Evil Requests
# ------------------------------------------------------------------------------
sendWrongRequest() {
uriToSend=$1
errorExpected=$2
ok=0
curl $host_and_port/$uriToSend > $tmp_dir/err.html && ok=1
if [ $ok = "0" ]; then
fail "GET $uriToSend: Error in Server Response"
fi
ok=0
grep "$errorExpected" $tmp_dir/err.html && ok=1
if [ $ok = "0" ]; then
fail "GET $uriToSend: Wrong error message detected"
fi
success "GET $uriToSend: expected HTTP error checked"
}
sendWrongRequest mrufiles/ThisIsInvalid "400 Bad Request"
sendWrongRequest files/InvalidID "404 Not Found"
sendWrongRequest files/invalidID/zip "404 Not Found"
sendWrongRequest mrufiles/zip/thisIsInvalid "400 Bad Request"
# ------------------------------------------------------------------------------
# Check if we are happy with puntuactors chars, and spaces, escapes, etc.
# Since we do not support a complete set of UTF-8 in such implementation
# the following file name is strange enough for our purposes
# ------------------------------------------------------------------------------
strangeFname="StrangeName \!@#$%^&*()'\"\\,><~.txt"
cd $tmp_dir
id=`echo -n $strangeFname | sha256sum | awk '{print $1}'`
touch $strangeFname # creating an empty unusual named file!
ok=0
curl -F file=@$strangeFname $host_and_port/store > strange.json && ok=1
if [ $ok = "0" ]; then
cd -
fail "POST $strangeFname: File not uploaded"
fi
ok = 0
grep $id $tmp_dir/strange.json && ok=1
if [ $ok = "0" ]; then
cd -
fail "POST $strangeFname: $id does not match with JSON content"
fi
cd -
success "POST $strangeFname: File uploaded"
ok = 0
cd $tmp_dir2
curl $host_and_port/files/$id > strange.json && diff $tmp_dir/strange.json . && ok=1
if [ $ok = "0" ]; then
cd -
fail "GET files/$id: $strangeFname JSON descriptor does't match with POST one"
fi
ok = 0
rm -f strange.zip
curl $host_and_port/files/$id/zip --output strange.zip && ok=1
if [ $ok = "0" ]; then
cd -
fail "GET files/$id/zip: $strangeFname JSON descriptor does't match with POST one"
fi
success "GET files/$id/zip: zipped $strangeFname downloaded"
ok = 0
rm -f $strangeFname
unzip strange.zip && ls $strangeFname && ok=1
success "GET files/$id/zip: $strangeFname successfully uncompressed"
set +x
# ------------------------------------------------------------------------------
# Show results
# ------------------------------------------------------------------------------
clear
echo ------------------------------------
printf "${GREEN}TEST SUCCEDED${NC}\n"
echo ------------------------------------
while read -r line; do echo -e $line; done < $resultfile
# ------------------------------------------------------------------------------
# Cleanup temporary directories and related files
# ------------------------------------------------------------------------------
rm -rf $tmp_dir
rm -rf $tmp_dir2