-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.sh
executable file
·1320 lines (1087 loc) · 35.4 KB
/
tests.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# This script is created for testing of InfoZip utilities on Linux systems
# and probably will not work right on the other system.
# I didn't test it.
# - I hope that unzip will not be generator of errors :-)
# NOTE: may I will add some option for skipping unzip-tests
# or alternative results (as FAILED_CHECK or something similar)
#TODO: store results of tests to variable (array). add function for
# check of previous results
# add option for print of results s
#zip="../zip"
#unzip="../unzip"
#zipnote="../zipnote"
zipnote="$( which zipnote )"
zip="$(which zip)"
unzip="$(which unzip)"
scriptname="$(basename "$0")"
TEST_DIR="test_dir"
cd "${0%$scriptname}"
_SCRIPT_PWD="$PWD"
COMPACT=0
__tmp_output=""
only_test=""
SYSTEM=$(uname -s 2>/dev/null) || SYSTEM="unknown"
home_expand() {
echo "$1" | grep -q "^~/" && echo "$HOME${1#\~}" || echo "$1"
}
#################################################
# USAGE
#################################################
print_usage() {
echo "
tests.sh [--nocolors] [--unzip FILE] [--zip FILE] [--zipnote FILE]
[-c | --compact VAL] [ --run-test test_function ] [-h | --help]
--nocolors No colored output
--unzip FILE Will be used this script as unzip.
Default: $(which unzip)
--zip FILE Will be sed this script as zip.
Default: $(which zip)
--zipnote FILE Will be used this script as zipnote
Default: $(which zipnote)
--run-test test_function
Run only test with same name of function
-c, --compact VAL
Create compact output. VAL can be number 1..5.
1 - suppress output from utilities and error messages
and print only basic info about tests.
2 - print whole output and basic info about success of each
test print one more time after ends of tests
3 - similar to 2, but logged errors are print by between
compact output after tests are completed. In this case
these errors are printed to STDOUT istead of STDERR
4 - similar to 3 with suppressed output from utilities,
but prints basic info about test (it's like progress)
5 - similar to 4, but suppressed any output during testing
-h, --help Print this help.
"
}
#################################################
# PROCESS PARAMETERS #
#################################################
NOCOLORS=0
while [[ $1 != "" ]]; do
param="$(echo "$1" | sed -r "s/^(.*)=.*/\1/")"
if [[ "$param" != "$1" ]]; then
_VAL=$(echo "$1" | sed -r "s/^.*=(.*)/\1/g");
_USED_NEXT=0
else
_VAL="$2"
_USED_NEXT=1
fi
case "$param" in
--help | -h)
print_usage
exit 0
;;
--nocolors)
NOCOLORS=1
;;
--unzip)
unzip="$(home_expand "$_VAL")"
shift $_USED_NEXT
;;
--zip)
zip="$(home_expand "$_VAL")"
shift $_USED_NEXT
;;
--zipnote)
zipnote="$(home_expand "$_VAL")"
shift $_USED_NEXT
;;
--run-test)
only_test="$_VAL"
shift $_USED_NEXT
;;
-c | --compact)
echo "$_VAL" | grep -qE "^[1-5]$"
[ $? -ne 0 ] && {
echo "Wrong parameter of compact! Only value from 1 to 5 can be used!" >&2
exit 1;
}
COMPACT=$_VAL
shift $_USED_NEXT
;;
*)
echo "Unknown option '$param'" >&2
exit 1
;;
esac
shift
done
#################################################
#################################################
if [[ ! -e "$zip" ]]; then
echo "Script Error: File $zip doesn't exists." >&2
exit 1
fi
if [[ ! -e "$unzip" ]]; then
echo "Script Error : File $unzip doesn't exists." >&2
exit 1
fi
if [[ ! -e "$zipnote" ]]; then
echo "Script Error: File $zipnote doesn't exists." >&2
exit 1
fi
for i in "diff" "cmp"; do
which $i >/dev/null 2>/dev/null && [[ -f "$(which $i)" ]] || {
echo "Script error: Utility $i is missing! This utility is required for testing!" >&2
exit 1
}
done
[ -n "$TEST_DIR" ] || {
echo "EMPTY destination of TEST_DIR!! Threatens remove of all files on disk!" >2&
exit 2
}
rm -rf "$TEST_DIR"
mkdir "$TEST_DIR" || {
echo "Error: test directory wasn't created!"
exit 1
}
#################################################
# BASIC FUNCTIONS & VARS #
#################################################
# here are basic functions and variables for easier testing
# you can add here other functions which are helpfull for you
FAILED=0
PASSED=0
SKIPPED=0 # some functions don't have to be available (e.g. bzip,...)
ERRORS=0 # maybe will be able to used/implemented later
__zip_version="$($zip -v | head -n 2 | tail -n 1 | cut -d " " -f 4)"
__unzip_version="$($unzip -vqqqq)"
__TEST_COUNTER=1
if [[ $NOCOLORS -eq 0 ]]; then
green='\e[1;32m'
red='\e[1;31m'
cyan='\e[1;36m'
endColor='\e[0m'
else
green=""
red=""
cyan=""
endcolor=""
fi
TEST_TITLE=""
DTEST_DIR="$TEST_DIR/$TEST_DIR" # double testdir - for unzipped files
set_title() {
TEST_TITLE="$*"
}
clean_test_dir() {
[ -n "$TEST_DIR" ] || {
echo "EMPTY destination of TEST_DIR!! Threatens remove of all files on disk!" >2&
exit 2
}
rm -rf "$TEST_DIR"/* > /dev/null
}
test_failed() {
[ "$PWD" != "$_SCRIPT_PWD" ] && cd "$_SCRIPT_PWD"
clean_test_dir
[ $COMPACT -ne 5 ] && \
echo -e "[ ${red}FAIL${endColor} ] TEST ${__TEST_COUNTER}: $TEST_TITLE"
[ $COMPACT -gt 1 ] && \
__tmp_output="${__tmp_output}\n[ ${red}FAIL${endColor} ] TEST ${__TEST_COUNTER}: $TEST_TITLE"
__TEST_COUNTER=$[ $__TEST_COUNTER +1 ]
FAILED=$[ $FAILED +1 ]
}
test_passed() {
[ "$PWD" != "$_SCRIPT_PWD" ] && cd "$_SCRIPT_PWD"
clean_test_dir
[ $COMPACT -ne 5 ] && \
echo -e "[ ${green}PASS${endColor} ] TEST ${__TEST_COUNTER}: $TEST_TITLE"
[ $COMPACT -gt 1 ] && \
__tmp_output="${__tmp_output}\n[ ${green}PASS${endColor} ] TEST ${__TEST_COUNTER}: $TEST_TITLE"
__TEST_COUNTER=$[ $__TEST_COUNTER +1 ]
PASSED=$[ $PASSED +1 ]
}
test_skipped() {
[ "$PWD" != "$_SCRIPT_PWD" ] && cd "$_SCRIPT_PWD"
clean_test_dir
[ $COMPACT -ne 5 ] && \
echo -e "[ ${cyan}SKIP${endColor} ] TEST ${__TEST_COUNTER}: $TEST_TITLE"
[ $COMPACT -gt 1 ] && \
__tmp_output="${__tmp_output}\n[ ${cyan}SKIP${endColor} ] TEST ${__TEST_COUNTER}: $TEST_TITLE"
__TEST_COUNTER=$[ $__TEST_COUNTER +1 ]
SKIPPED=$[ $SKIPPED +1 ]
}
# use this if you want print some error message
log_error() {
echo "Error: TEST $__TEST_COUNTER: $*" >&2
[ $COMPACT -gt 2 ] && __tmp_output="${__tmp_output}\nError: TEST $__TEST_COUNTER: $*"
}
#################################################
# OTHER USABLE FUNCTIONS #
#################################################
# You could use and insert here functions which are helpfull for testing.
# However you SHOULDN'T modify them, without check of every function which
# use them!
is_integer() {
echo "$1" | grep -qE "^[0-9]+$"
return $?
}
create_text() {
# this nice generator is undertaken from Alan Skorkin
# http://www.skorks.com/2010/03/how-to-quickly-generate-a-large-file-on-the-command-line-with-linux/
# optional parameter for setting length of text
is_integer "$1" && chars=$1 || chars=100000
case "$SYSTEM" in
SunOS)
DICT_WORDS=/usr/share/lib/dict/words
;;
*)
DICT_WORDS=/usr/share/dict/words
;;
esac
echo $(ruby -e 'a=STDIN.readlines;500.times do;b=[];20.times do;
b << a[rand(a.size)].chomp end; puts b.join(" "); end' \
< $DICT_WORDS ) | head -c $chars
}
# create unique filename for files in $TEST_DIR
# $1 prefix
# $2 suffix
create_unique_filename() {
echo "$1" | grep -qE "^[a-zA-Z0-9_.]+$"
[ $? -eq 0 ] && prefix="$1" || prefix="tmp_"
echo "$2" | grep -qE "^[a-zA-Z0-9_.]+$"
[ $? -eq 0 ] && suffix="$2" || suffix=""
file_counter=$( ls "$TEST_DIR" | wc -l )
while [ 1 ]; do
filename="${prefix}${file_counter}${suffix}"
[ ! -e "$TEST_DIR/$filename" ] && {
echo $filename
return 0
}
file_counter=$[ $file_counter +1 ]
done
}
# long lines - 100k characters
# parameter sets length of file - default 10k
create_text_file() {
filename="$( echo -e "tmp_"$( ls $TEST_DIR | wc -l ))"
is_integer "$1" && chars=$1 || chars=10000
yes "$( create_text )" | head -c $chars > "$TEST_DIR/$filename"
echo $filename
}
# change 2 bits in compressed content
# and print filename of archive (without path)
create_easy_damaged_archive() {
filename=$( create_text_file 1000 )
eda="$TEST_DIR/$( create_unique_filename "eda_" ".tmp" )"
archive="$TEST_DIR/$( create_unique_filename "archive_" ".zip" )"
hex_file="$TEST_DIR/$( create_unique_filename "hex_" ".tmp" )"
$zip $archive $TEST_DIR/$filename >&2
xxd $archive $hex_file >&2
line_counter=0
while read line; do
line_counter=$[ $line_counter +1 ]
[ $line_counter -ne 10 ] && {
echo $line >> $eda
continue
}
# this line we easy damage
echo -n "$( echo $line | cut -d " " -f 1 ) " >> $eda
rest=$(echo $line | cut -d " " -f 1 --complement)
echo -n $(echo $rest | sed -r "s/^(..).*$/\1/" | tr "0123456789abcdef" "123456789abcdefe") \
>> $eda
echo $rest | sed -r "s/^..(.*)$/\1/" >> $eda
done < $hex_file
xxd -r $eda $archive >&2
echo ${archive#"$TEST_DIR/"}
}
# $1 - expected return value
# $2 - real return value
test_ecode() {
[ $# -ne 2 ] && {
log_error "test_ecode(): wrong count of arguments!"
return 2
}
[ $1 -eq $2 ] && return 0
log_error "Wrong exit code! Expected $1, but returned $2"
return 1
}
#################################################
# TESTS BEGIN #
#################################################
#TODO: proposed tests for implementation
#### test | expected result
# create archive with really many files | ? (limit)
# create archive with symlinks | success
# add file - not exists | ?
# add file - limit reached | ?
# add file - empty archive | success
# delete file - deleted already | ?
# delete file - deleted already and empty | ?
# test really big archive? | ?
## series of can't read zip and files | failed
## series zipfile format
# error writing | 14 (can't write) - I duno how test this now
# tests for zipnote and zipcloak
## invalid comment format | 7
#################################################
# Here add test functions.
# Do not add call of function! these will be called automatically
# by this script in section TESTINGS!
# Any helpfull functions add into the section above
#skeleton
# test_X () {
# set_title "title/label of test" # it's required! for right output report
# # do what you want
# ....
# return 0 # PASSED
# return 1 # FAILED
# return 2 # SKIPPED
#}
# create archive | success 0
test_1 () {
set_title "Create archive.zip - unzip, cmp verify"
filename=$( create_text_file )
$zip $TEST_DIR/archive.zip $TEST_DIR/$filename
test_ecode 0 $? || return 1
[ ! -e $TEST_DIR/archive.zip ] && return 1
$unzip -d $TEST_DIR $TEST_DIR/archive.zip
[ $? -ne 0 ] && {
log_error "Unzip failed."
return 1
}
[ ! -f "$DTEST_DIR/$filename" ] && {
log_error "Unzipped archive doesn't contain archived file."
return 1
}
cmp -s "$DTEST_DIR/$filename" "$TEST_DIR/$filename" || {
log_error "Unzipped file is different!"
return 1
}
return 0
}
# create archive - without extension | automatic adding of .zip
test_2 () {
set_title "Create archive - extension adding"
filename=$( create_text_file )
$zip $TEST_DIR/archive $TEST_DIR/$filename && \
[ -e "$TEST_DIR/archive.zip" ] && return 0
return 1
}
# create archive - file doesn't exists | nothing to do
test_3() {
set_title "Create archive - file doesn't exists"
$zip $TEST_DIR/archive $TEST_DIR/non_existing_file
test_ecode 12 $? || return 1
[ -e "$TEST_DIR/archive.zip" ] && {
log_error "Archive was created but it shouldn't!"
return 1
}
return 0
}
# Create archive - empty filelist | nothing to do
test_4() {
set_title "Create archive - without any file in list"
$zip $TEST_DIR/archive
test_ecode 12 $? || return 1
[ -e "$TEST_DIR/archive.zip" ] && {
log_error "Archive was created but it shouldn't!"
return 1
}
return 0
}
# update not exists archive | warning, create archive
test_5() {
set_title "Update - archive not exists"
filename=$( create_text_file )
$zip -u $TEST_DIR/archive $TEST_DIR/$filename 2> $TEST_DIR/log
test_ecode 0 $? || return 1
[ -e "$TEST_DIR/archive.zip" ] || \
{ log_error "Archive wasn't created"; return 1; }
cat $TEST_DIR/log | grep -iq "warning"
[ $? -ne 0 ] && { log_error "Warning missing"; return 1; }
return 0
}
# update archive - verify with unzip | success
test_6() {
set_title "Update archive - add new file and replace existing - unzip, cmp verify"
touch $TEST_DIR/tmp_0 $TEST_DIR/tmp_1
filename=$( create_text_file )
$zip $TEST_DIR/archive $TEST_DIR/tmp_0 $TEST_DIR/tmp_1
sleep 1 # MUST BE!
echo "Secret text" >> $TEST_DIR/tmp_0
$zip -u $TEST_DIR/archive $TEST_DIR/tmp_0 $TEST_DIR/$filename
test_ecode 0 $? || return 1
$unzip -d $TEST_DIR $TEST_DIR/archive.zip
[ -f $DTEST_DIR/tmp_0 -a -f $DTEST_DIR/tmp_1 -a -f $DTEST_DIR/$filename ] || {
log_error "unzipped: archive doesn't contain all expected files"
return 1
}
cmp -s "$DTEST_DIR/tmp_0" "$TEST_DIR/tmp_0" && \
cmp -s "$DTEST_DIR/$filename" "$TEST_DIR/$filename" &&
cmp -s "$DTEST_DIR/tmp_1" "$TEST_DIR/tmp_1" || {
log_error "unzipped files are different"
return 1
}
return 0
}
# update archive - nothing changed | nothing to do 12
test_7() {
set_title "Update archive - nothing to do"
echo "something" > $TEST_DIR/tmp_0
$zip $TEST_DIR/archive $TEST_DIR/tmp_0
$zip -u $TEST_DIR/archive
test_ecode 12 $? || return 1
return 0
}
# check -sf option | print all archived files
test_8() {
set_title "Check -sf option (print filelist)"
touch $TEST_DIR/archive $TEST_DIR/tmp_0 $TEST_DIR/tmp_1 $TEST_DIR/tmp_2
$zip $TEST_DIR/archive $TEST_DIR/tmp_0 $TEST_DIR/tmp_1 $TEST_DIR/tmp_2
lines=$( $zip -sf $TEST_DIR/archive | grep -c "tmp_[012]$" )
[ $lines -eq 3 ] || {
log_error "-sf option print wrong output"
return 1
}
return 0
}
# delete file | success
test_9() {
set_title "Delete file"
touch $TEST_DIR/archive $TEST_DIR/tmp_0 $TEST_DIR/tmp_2
echo "something" > $TEST_DIR/tmp_1
$zip $TEST_DIR/archive $TEST_DIR/*
$zip -d $TEST_DIR/archive $TEST_DIR/tmp_1
test_ecode 0 $? || return 1
lines=$( $zip -sf $TEST_DIR/archive | grep -c "tmp_[012]$" )
$zip -sf $TEST_DIR/archive | grep -q "tmp_1"
[ $? -eq 1 -a $lines -eq 2 ] || {
log_error "File was not removed or something is wrong with other files"
return 1
}
return 0
}
# update archive - empty | warning, 13
test_10() {
set_title "Update empty archive"
touch $TEST_DIR/tmp_0
$zip $TEST_DIR/archive $TEST_DIR/tmp_0
$zip -d $TEST_DIR/archive $TEST_DIR/tmp_0
$zip -u $TEST_DIR/archive > $TEST_DIR/log
test_ecode 13 $? || return 1
grep -qE "warning.*empty" $TEST_DIR/log || {
log_error "Warning missing or wrong warning message"
return 1
}
return 0
}
# updated archive doesn't exists
test_11() {
set_title "Updated archive doesn't exists"
$zip -u $TEST_DIR/nothing_exists
test_ecode 13 $? || return 1
return 0
}
# update archive - file (not archive) not found | 12
test_12() {
set_title "Update empty archive - file not exists and it's not part of archive"
touch $TEST_DIR/tmp_0
$zip $TEST_DIR/archive $TEST_DIR/tmp_0
$zip -u $TEST_DIR/archive $TEST_DIR/tmp_1 # > $TEST_DIR/log
test_ecode 12 $? || return 1
return 0
}
# update archive - original file removed | 12
test_13() {
set_title "Update empty archive - original file was removed"
touch $TEST_DIR/tmp_0
$zip $TEST_DIR/archive $TEST_DIR/tmp_0
rm -f $TEST_DIR/tmp_0
$zip -u $TEST_DIR/archive $TEST_DIR/tmp_0 # > $TEST_DIR/log
test_ecode 12 $? || return 1
return 0
}
# add already added file | success
test_14() {
set_title "Add already added file - without changes"
filename=$( create_text_file )
$zip $TEST_DIR/archive $TEST_DIR/$filename
$zip $TEST_DIR/archive $TEST_DIR/$filename
test_ecode 0 $? || return 1
return 0
}
# delete file - never exists | ?
test_15() {
set_title "Delete file which hasn't been part of archive"
touch $TEST_DIR/tmp_0
$zip $TEST_DIR/archive $TEST_DIR/tmp_0
$zip -d $TEST_DIR/archive $TEST_DIR/tmp_1 > $TEST_DIR/log
test_ecode 12 $? || return 1
grep -qE "warning.*not matched" $TEST_DIR/log || {
log_error "Wrong of missing warning"
return 1
}
return 0
}
# delete file - never exists and empty | 13
test_16() {
set_title "Delete file from empty archive"
touch $TEST_DIR/tmp_0
$zip $TEST_DIR/archive $TEST_DIR/tmp_0
$zip -d $TEST_DIR/archive $TEST_DIR/tmp_0
$zip -d $TEST_DIR/archive $TEST_DIR/never_ever
test_ecode 13 $? || return 1
return 0
}
# delete file - archive become empty
test_17() {
set_title "Delete file - archive become empty"
touch $TEST_DIR/tmp_0
$zip $TEST_DIR/archive $TEST_DIR/tmp_0
$zip -d $TEST_DIR/archive $TEST_DIR/tmp_0 > $TEST_DIR/log
test_ecode 0 $? || return 1
grep -E "warning.*empty" $TEST_DIR/log || {
log_error "Wrong of missing warning"
return 1
}
return 0
}
# wrong commandline parameters | 16
test_18() {
set_title "Wrong commandline parameters"
$zip -w $TEST_DIR/something
test_ecode 16 $? || return 1
return 0
}
# error writting | 15 (cant create file for write)
test_19() {
set_title "Can't create file for write"
filename=$( create_text_file )
mkdir $DTEST_DIR
chmod -x $DTEST_DIR
$zip $DTEST_DIR/archive $TEST_DIR/$filename
status=$?
chmod +x $DTEST_DIR
test_ecode 15 $status || return 1
return 0
}
test_20() {
set_title "Write permission denied - existing archive"
touch $TEST_DIR/tmp_0
$zip $TEST_DIR/archive $TEST_DIR/tmp_0
chmod -w $TEST_DIR/archive.zip
$zip -d $TEST_DIR/archive $TEST_DIR/tmp_0
status=$?
chmod +w $TEST_DIR/archive.zip
test_ecode 15 $status || return 1
return 0
}
# test archive -T | success
test_21() {
set_title "Test the integrity of archive (success)"
filename=$( create_text_file 1000 )
$zip $TEST_DIR/archive.zip $TEST_DIR/$filename
$zip -T $TEST_DIR/archive.zip -TT "$unzip -tqq"
test_ecode 0 $? || return 1
return 0
}
test_22() {
set_title "Test the integrity of the new archive (damaged file)"
archive=$( create_easy_damaged_archive )
$zip -T $TEST_DIR/$archive -TT "$unzip -tqq"
test_ecode 8 $? || return 1
return 0
}
test_23() {
set_title "Generic zipfile format error"
echo "Lorem ipsum, whatever..." > $TEST_DIR/archive.zip
$zip -T $TEST_DIR/archive.zip -TT "$unzip -tqq"
test_ecode 3 $? || return 1
return 0
}
test_24() {
set_title "Update archive with corrupted data inside (modify original file)"
archive=$(create_easy_damaged_archive)
orig_file=$($zip -sf $TEST_DIR/$archive | grep -o "test_dir/.*")
# update is possible only if original file is changed now
# IMHO it should be automatically when CRC is wrong in future
sleep 1
touch -m $orig_file
# file should be updated (repaired) to original
$zip -u $TEST_DIR/$archive
test_ecode 0 $? || return 1
# test the integrity of archive again
$zip -T $TEST_DIR/$archive -TT "$unzip -tqq"
test_ecode 0 $? || return 1
return 0
}
# zip was interrupted | 9
test_25() {
set_title "Interrupt zip"
filename=$( create_text_file 50000000 ) # 50 MB
( $zip $TEST_DIR/archive.zip $TEST_DIR/$filename ) &
pid=$!
( sleep 0.2; kill -s SIGINT $pid ) &
wait $pid
test_ecode 9 $? || return 1
return 0
}
test_26() {
set_title "Create temp file in chosen path"
filename=$( create_text_file 50000000) # 50 MB
mkdir $DTEST_DIR
( $zip -b $DTEST_DIR $TEST_DIR/archive $TEST_DIR/$filename ) &
pid=$!
# important - without sleep it's possible check dir before file is created
sleep 0.5
[ $( ls $DTEST_DIR | wc -l ) -eq 0 ] && {
log_error "Tempfile was not created in chosen directory."
kill -s SIGINT $pid
return 1
}
kill -s SIGINT $pid
return 0
}
# problem with tmp file | 10
test_27() {
set_title "Problem with tmp file (chosen path doesn't exists)"
filename=$( create_text_file 1000 )
$zip -b $TEST_DIR/not_existing_directory $TEST_DIR/archive $TEST_DIR/$filename
test_ecode 10 $? || return 1
return 0
}
test_28() {
set_title "Problem with tmp file (can't crate temp file)"
filename=$( create_text_file 1000 )
mkdir $DTEST_DIR
chmod -x $DTEST_DIR
$zip -b $DTEST_DIR $TEST_DIR/archive $TEST_DIR/$filename
test_ecode 10 $? || return 1
return 0
}
# series can't read errors
test_29() {
set_title "Create archive - can't read file"
filename=$( create_text_file )
chmod -r $TEST_DIR/$filename
$zip $DTEST_DIR $TEST_DIR/$filename
test_ecode 18 $? || return 1
return 0
}
test_30() {
set_title "Update archive - can't read archive (consult with upstream)"
filename=$( create_text_file )
$zip $TEST_DIR/archive $TEST_DIR/$filename
chmod -r $TEST_DIR/archive.zip
echo "aa" >> $TEST_DIR/$filename
$zip -u $TEST_DIR/archive.zip
test_ecode 18 $? || return 1
return 0
}
test_31() {
set_title "Update archive - can't read only some files"
echo "file1" > $TEST_DIR/tmp0
echo "file2" > $TEST_DIR/tmp1
echo "file3" > $TEST_DIR/tmp2
touch $TEST_DIR/tmp3
$zip $TEST_DIR/archive $TEST_DIR/{tmp0,tmp1,tmp2}
sleep 1
echo "Secret text" > $TEST_DIR/tmp0
echo "Yep" >> $TEST_DIR/tmp1
chmod -r $TEST_DIR/tmp0
$zip -u $TEST_DIR/archive.zip
test_ecode 18 $? || return 1
return 0
}
test_32() {
set_title "Create archive with symlinks"
echo "Some next not funny text" > $TEST_DIR/tmp_0
ln -s ./tmp_0 $TEST_DIR/symlink_file
$zip --symlinks $TEST_DIR/archive.zip $TEST_DIR/{tmp_0,symlink_file}
test_ecode 0 $? || return 1
$unzip -d $TEST_DIR $TEST_DIR/archive.zip
status=$?
[ $status -ne 0 ] && {
log_error "Unzip: return $status but expected is 0 (Archive contains symlink)"
return 1
}
[ ! -L $DTEST_DIR/symlink_file ] && {
log_error "Unzipped file is not symlink!"
return 1
}
cmp -s "$TEST_DIR/tmp_0" "$DTEST_DIR/symlink_file" || {
log_error "Unzipped files are different (symlink)"
return 1
}
return 0
}
test_33() {
set_title "Create archive without --symlinks (one input file is symlink)"
echo "Some next not funny text" > $TEST_DIR/tmp_0
ln -s ./tmp_0 $TEST_DIR/symlink_file
$zip $TEST_DIR/archive.zip $TEST_DIR/{tmp_0,symlink_file}
test_ecode 0 $? || return 1
$unzip -d $TEST_DIR $TEST_DIR/archive.zip
status=$?
[ $status -ne 0 ] && {
log_error "Unzip: return $status but expected is 0 (Archive contains symlink)"
return 1
}
[ -L $DTEST_DIR/symlink_file ] && {
log_error "Unzziped file is symlink, but it shouldn't (option \"--symlinks\" wasn't used)"
return 1
}
cmp -s "$TEST_DIR/tmp_0" "$DTEST_DIR/symlink_file" && \
cmp -s "$TEST_DIR/tmp_0" "$DTEST_DIR/tmp_0" || {
log_error "Unzipped files are different (symlink)"
return 1
}
return 0
}
test_34() {
set_title "Create archive with many files and symlinks"
text=$( create_text 1000 )
mkdir -p $TEST_DIR/files/some
for i in {0..10000}; do
echo "$text" > "$TEST_DIR/files/tmp_$i"
done
echo "$( create_text 16000)" >> $TEST_DIR/files/tmp_152
echo "$( create_text 16000)" >> $TEST_DIR/files/tmp_153
echo "$( create_text 16000)" >> $TEST_DIR/files/tmp_154
cp $TEST_DIR/files/tmp_154 $TEST_DIR/files/some/juhuu
ln -s tmp_152 $TEST_DIR/files/syml_0
ln -s some/juhuu $TEST_DIR/files/u_syml_1
for i in {160..260}; do
ln -s tmp_$i $TEST_DIR/files/tmp_${i}_0
done
$zip -r --symlinks $TEST_DIR/archive.zip $TEST_DIR/* >/dev/null
test_ecode 0 $? || return 1
$unzip -d $TEST_DIR $TEST_DIR/archive.zip > "$TEST_DIR/log_unzip" 2>&1
status=$?
[ $status -ne 0 ] && {
log_error "Unzip: return $status but expected is 0 (Archive contains symlink and many files)"
return 1
}
files_orig=$(ls $TEST_DIR/files/ | wc -l)
files_unzipped=$(ls $DTEST_DIR/files/ | wc -l)
[ $files_orig -ne $files_unzipped ] && {
log_error "Some files are missing. Before: $files_orig - After: $files_unzipped"
return 1
}
missing=0
for i in {160..260}; do
[ ! -L $DTEST_DIR/files/tmp_${i}_0 ] && {
missing=1
break
}
done
[ -L $DTEST_DIR/files/syml_0 -a -L $DTEST_DIR/files/u_syml_1 -a $missing -eq 0 ] || {
log_error "(some) Symlinks was not created"
return 1
}
lines=$(cat $TEST_DIR/log_unzip | grep -cE "\->\s*(tmp_[0-9]+|some/juhuu)$" )
[ $lines -ne 103 ] && {
## this archive is not affected by unzip bug https://bugzilla.redhat.com/show_bug.cgi?id=740012
## if someone can create such archive here, please write me or send me patch/new test
## e.g. this archive is affected https://github.com/mono/mono/archive/master.zip
log_error "Unzip decompress symlinks wrong. (Known bug in unzip 6.10b and older - patched on fedora 19 and newer)"
return 1
}
return 0
}
test_35() {
set_title "Freshen archive (empty archive)"
touch $TEST_DIR/tmpfile
$zip $TEST_DIR/archive $TEST_DIR/tmpfile
$zip -d $TEST_DIR/archive.zip $TEST_DIR/tmpfile
$zip -f $TEST_DIR/archive.zip
test_ecode 13 $? || return 1
return 0
}
test_36() {
set_title "Freshen archive (test not adding new files)"
touch $TEST_DIR/tmp_0 $TEST_DIR/tmp_1
$zip $TEST_DIR/archive $TEST_DIR/tmp_0
$zip -f $TEST_DIR/archive.zip $TEST_DIR/tmp_1
test_ecode 12 $? || return 1
sleep 1
echo "And rains, and rains.." >> $TEST_DIR/tmp_0
$zip -f $TEST_DIR/archive.zip $TEST_DIR/*
test_ecode 0 $?
lines=$( $zip -sf $TEST_DIR/archive.zip | wc -l )
[ $lines -ne 3 ] && {
log_error "Freshen - added files (or changed report!)"
return 1
}
return 0
}
# truncate archive - try repair and verify with unzip
test_37() {
set_title "Unexpected end of zipfile"
filename=$( create_text_file 1000 )
$zip $TEST_DIR/archive $TEST_DIR/$filename
size=$[ $(wc -c $TEST_DIR/archive.zip | cut -d " " -f 1) - 5 ]
truncate --size $size $TEST_DIR/archive.zip
$zip -T $TEST_DIR/archive -TT "$unzip -tqq"
test_ecode 2 $? || return 1
## -F and also -FF do not repair truncated end of archive
return 0