-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_archive.py
More file actions
2733 lines (2345 loc) · 125 KB
/
test_archive.py
File metadata and controls
2733 lines (2345 loc) · 125 KB
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
# -*- coding: utf-8 -*-
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/extractcode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import os
from pathlib import Path
import pytest
from commoncode import date as commoncode_date
from commoncode import fileutils
from commoncode.system import on_linux
from commoncode.system import on_mac
from commoncode.system import on_macos_arm64
from commoncode.system import on_windows
from commoncode.testcase import is_same
from extractcode_assert_utils import BaseArchiveTestCase
from extractcode_assert_utils import check_files
from extractcode_assert_utils import check_size
from extractcode_assert_utils import to_posix
import extractcode
from extractcode import ExtractErrorFailedToExtract
from extractcode import archive
from extractcode import libarchive2
from extractcode import sevenzip
"""
For each archive type --when possible-- we are testing extraction of:
- basic, plain archive, no tricks
- with trailing data appended to archive
- broken, either truncated or with extra junk inserted
- with hardlinks and symlinks, either valid or broken when supported
- with hardlinks and symlinks loops (aka. tarbomb) when supported
- with FIFO, character, sparse and other special files when supported
- with relative paths pointing outside of the archive when supported
- with absolute paths when supported
- with invalid paths or mixed slash paths when supported
- with unicode or binary path names
- with duplicate names or paths when case is ignored
- password-protected when supported
"""
project_root = os.path.dirname(os.path.dirname(__file__))
class TestGetExtractorTest(BaseArchiveTestCase):
def test_get_extractors_1(self):
test_file = 'archive/zip/basic.zip'
expected = [archive.extract_zip]
self.check_get_extractors(test_file, expected)
def test_get_extractors_2(self):
test_file = 'archive/rar/basic.rar'
expected = [archive.extract_rar]
self.check_get_extractors(test_file, expected)
def test_get_extractors_3(self):
test_file = 'archive/deb/adduser_3.112ubuntu1_all.deb'
expected = [archive.extract_deb]
self.check_get_extractors(test_file, expected)
def test_get_extractors_4(self):
test_file = 'archive/cpio/elfinfo-1.0-1.fc9.src.cpio'
expected = [archive.extract_cpio]
self.check_get_extractors(test_file, expected)
def test_get_extractors_5(self):
test_file = 'archive/rpm/elfinfo-1.0-1.fc9.src.rpm'
expected = [archive.extract_rpm, archive.extract_cpio]
self.check_get_extractors(test_file, expected)
def test_get_extractors_6(self):
test_file = 'archive/gzip/file_4.26-1.diff.gz'
expected = [archive.uncompress_gzip]
self.check_get_extractors(test_file, expected)
def test_get_extractors_7(self):
test_file = 'archive/ar/liby.a'
expected = [archive.extract_ar]
self.check_get_extractors(test_file, expected)
def test_get_extractors_8(self):
test_file = 'archive/bz2/single_file_not_tarred.bz2'
expected = [archive.uncompress_bzip2]
self.check_get_extractors(test_file, expected)
def test_get_extractors_9(self):
test_file = 'archive/tar/tarred.tar'
expected = [archive.extract_tar]
self.check_get_extractors(test_file, expected)
def test_get_extractors_10(self):
test_file = 'archive/tbz/tarred_bzipped.bz'
expected = [archive.uncompress_bzip2]
self.check_get_extractors(test_file, expected)
def test_get_extractors_11(self):
test_file = 'archive/tbz/tarred_bzipped.tar.bz2'
expected = [archive.extract_tar]
self.check_get_extractors(test_file, expected)
def test_get_extractors_12(self):
test_file = 'archive/tbz/tarred_bzipped.tbz'
expected = [archive.extract_tar]
self.check_get_extractors(test_file, expected)
def test_get_extractors_13(self):
test_file = 'archive/tgz/tarred_gzipped.gz'
expected = [archive.uncompress_gzip]
self.check_get_extractors(test_file, expected)
def test_get_extractors_14(self):
test_file = 'archive/tgz/tarred_gzipped.tar.gz'
expected = [archive.extract_tar]
self.check_get_extractors(test_file, expected)
def test_get_extractors_15(self):
test_file = 'archive/tgz/tarred_gzipped.tgz'
expected = [archive.extract_tar]
self.check_get_extractors(test_file, expected)
def test_get_extractors_16(self):
test_file = 'archive/7z/z.7z'
expected = [archive.extract_7z]
self.check_get_extractors(test_file, expected)
def test_get_extractors_17(self):
test_file = 'archive/Z/tr2tex.Z'
expected = [archive.extract_Z, ]
self.check_get_extractors(test_file, expected)
def test_get_extractors_18(self):
test_file = 'archive/Z/tkWWW-0.11.tar.Z'
expected = [archive.extract_Z, archive.extract_tar]
self.check_get_extractors(test_file, expected)
def test_get_extractors_19(self):
test_file = 'archive/xar/xar-1.4.xar'
expected = [archive.extract_xarpkg]
self.check_get_extractors(test_file, expected)
def test_get_extractor_with_kinds_deb(self):
test_file = 'archive/deb/adduser_3.112ubuntu1_all.deb'
expected = [archive.extract_deb]
self.check_get_extractors(test_file, expected, (archive.package,))
def test_get_extractor_with_kinds_rpm(self):
test_file = 'archive/rpm/elfinfo-1.0-1.fc9.src.rpm'
kinds = (archive.regular, archive.file_system, archive.docs)
expected = []
self.check_get_extractors(test_file, expected, kinds)
def test_get_extractor_with_kinds_rpm_2(self):
test_file = 'archive/rpm/elfinfo-1.0-1.fc9.src.rpm'
kinds = (archive.regular, archive.file_system, archive.docs, archive.package)
expected = [sevenzip.extract, libarchive2.extract]
self.check_get_extractors(test_file, expected, kinds)
def test_get_extractor_with_kinds_deb2(self):
test_file = 'archive/deb/adduser_3.112ubuntu1_all.deb'
expected = []
self.check_get_extractors(test_file, expected, (archive.regular,))
def test_get_extractor_with_kinds_ar(self):
test_file = 'archive/ar/liby.a'
kinds = (archive.regular, archive.file_system, archive.docs)
expected = []
self.check_get_extractors(test_file, expected, kinds)
def test_get_extractor_with_kinds_bzip(self):
test_file = 'archive/tbz/tarred_bzipped.tar.bz2'
expected = []
self.check_get_extractors(test_file, expected, (archive.package,))
def test_get_extractor_with_kinds_plain_tar(self):
test_file = 'archive/tar/tarred.tar'
expected = []
self.check_get_extractors(test_file, expected, (archive.package,))
expected = [archive.extract_tar]
self.check_get_extractors(test_file, expected, (archive.regular,))
def test_get_extractor_for_graffle_docs(self):
test_file = 'archive/graffle/example.graffle'
expected = [archive.uncompress_gzip]
self.check_get_extractors(test_file, expected, (archive.docs,))
expected = []
self.check_get_extractors(test_file, expected, kinds=extractcode.default_kinds)
def test_get_extractor_for_compressed_svgz_docs(self):
test_file = 'archive/svgz/insert-emptyframe.svgz'
expected = [archive.uncompress_gzip]
self.check_get_extractors(test_file, expected, (archive.docs,))
expected = []
self.check_get_extractors(test_file, expected, kinds=extractcode.default_kinds)
def test_get_extractor_qcow2(self):
test_file = self.extract_test_tar('vmimage/foobar.qcow2.tar.gz')
test_file = str(Path(test_file) / 'foobar.qcow2')
expected = []
self.check_get_extractors(test_file, expected, kinds=extractcode.default_kinds)
expected = [archive.extract_vm_image]
self.check_get_extractors(test_file, expected, kinds=())
self.check_get_extractors(test_file, expected, kinds=(extractcode.file_system,))
self.check_get_extractors(test_file, expected, kinds=extractcode.all_kinds)
def test_get_extractor_for_dia(self):
test_file = self.get_test_loc('archive/dia/dia.dia', copy=True)
expected = [archive.uncompress_gzip]
self.check_get_extractors(test_file, expected, kinds=extractcode.all_kinds)
expected = []
self.check_get_extractors(test_file, expected, kinds=extractcode.default_kinds)
def test_get_handlers(self):
test_data = [
('archive/deb/adduser_3.112ubuntu1_all.deb', ['Tar', 'Debian package']),
('archive/rpm/elfinfo-1.0-1.fc9.src.rpm', ['RPM package']),
('archive/ar/liby.a', ['ar archive', 'Static Library']),
('archive/tar/tarred.tar', ['Tar']),
('archive/tbz/tarred_bzipped.tar.bz2', ['bzip2', 'Tar bzip2']),
('archive/tbz/tarred_bzipped.bz', ['bzip2', 'Tar bzip2']),
('archive/tgz/tarred_gzipped.gz', ['Tar gzip', 'Gzip']),
('archive/gzip/mysql-arch.ARZ', ['Tar gzip', 'Gzip']),
]
for test_file, expected in test_data:
test_loc = self.get_test_loc(test_file)
handlers = archive.get_handlers(test_loc)
assert expected == [h[0].name for h in handlers]
def test_score_handlers(self):
test_data = [
('archive/deb/adduser_3.112ubuntu1_all.deb', [(31, 'Debian package'), (11, 'Tar')]),
('archive/rpm/elfinfo-1.0-1.fc9.src.rpm', [(32, 'RPM package')]),
('archive/ar/liby.a', [(31, 'Static Library'), (17, 'ar archive')]),
('archive/tar/tarred.tar', [(29, 'Tar')]),
('archive/tar/gem/panchira-0.1.1.gem', [(31, 'Ruby Gem package'), (17, 'Tar')]),
('archive/tbz/tarred_bzipped.tar.bz2', [(30, 'Tar bzip2'), (29, 'bzip2')]),
('archive/tbz/tarred_bzipped.bz', [(29, 'bzip2'), (18, 'Tar bzip2')]),
('archive/tgz/tarred_gzipped.gz', [(29, 'Gzip'), (18, 'Tar gzip')]),
('archive/gzip/mysql-arch.ARZ', [(29, 'Gzip'), (18, 'Tar gzip')]),
]
for test_file, expected in test_data:
test_loc = self.get_test_loc(test_file)
handlers = archive.get_handlers(test_loc)
scored = archive.score_handlers(handlers)
assert expected == sorted([(h[0], h[1].name) for h in scored], reverse=True)
def test_no_handler_is_selected_for_a_non_archive(self):
# failed because of libmagic bug: http://bugs.gw.com/view.php?id=467
# passing by introducing strict flag for handlers
test_loc = self.get_test_loc('archive/not_archive/hashfile')
assert [] == list(archive.get_handlers(test_loc))
assert None == archive.get_extractor(test_loc)
assert None == archive.get_extractor(test_loc, kinds=extractcode.all_kinds)
assert not archive.should_extract(test_loc, kinds=extractcode.default_kinds)
def test_no_handler_is_selected_for_a_non_archive2(self):
# FWIW there is a related libmagic bug: http://bugs.gw.com/view.php?id=473
test_loc = self.get_test_loc('archive/not_archive/wildtest.txt')
assert [] == list(archive.get_handlers(test_loc))
assert None == archive.get_extractor(test_loc)
assert None == archive.get_extractor(test_loc, kinds=extractcode.all_kinds)
assert not archive.should_extract(test_loc, kinds=extractcode.default_kinds)
def test_no_handler_is_selected_for_a_non_archive3(self):
test_loc = self.get_test_loc('archive/not_archive/savetransfer.c')
assert [] == list(archive.get_handlers(test_loc))
assert None == archive.get_extractor(test_loc)
assert None == archive.get_extractor(test_loc, kinds=extractcode.all_kinds)
assert not archive.should_extract(test_loc, kinds=extractcode.default_kinds)
def test_7zip_extract_can_extract_to_relative_paths(self):
# The setup is a tad complex because we want to have a relative dir
# to the base dir where we run tests from, i.e. the git checkout dir.
# To use relative paths, we use our tmp dir at the root of the code tree
import shutil
import tempfile
from os.path import abspath
from os.path import join
from extractcode.sevenzip import extract
test_file = self.get_test_loc('archive/relative_path/basic.zip', copy=True)
project_tmp = join(project_root, 'tmp')
fileutils.create_dir(project_tmp)
project_root_abs = abspath(project_root)
test_src_dir = tempfile.mkdtemp(dir=project_tmp).replace(project_root_abs, '').strip('\\/')
test_tgt_dir = tempfile.mkdtemp(dir=project_tmp).replace(project_root_abs, '').strip('\\/')
shutil.copy(test_file, test_src_dir)
test_src_file = join(test_src_dir, 'basic.zip')
result = list(extract(test_src_file, test_tgt_dir))
assert [] == result
expected = ['c/a/a.txt', 'c/b/a.txt', 'c/c/a.txt']
check_files(test_tgt_dir, expected)
def test_windows_media_player_skins_are_zip(self):
test_file = self.get_test_loc('archive/wmz/Go.wmz')
extractors = archive.get_extractors(test_file)
assert [archive.extract_zip] == extractors
def test_windows_ntfs_wmz_are_sometimes_gzip(self):
test_file = self.get_test_loc('archive/wmz/image003.wmz')
extractors = archive.get_extractors(test_file)
assert [archive.uncompress_gzip] == extractors
class TestTarGzip(BaseArchiveTestCase):
def test_extract_targz_basic(self):
test_file = self.get_test_loc('archive/tgz/tarred_gzipped.tar.gz')
test_dir = self.get_temp_dir()
archive.extract_tar(test_file, test_dir)
result = os.path.join(test_dir, 'e/a/b.txt')
assert os.path.exists(result)
def test_extract_targz_with_trailing_data(self):
test_file = self.get_test_loc('archive/tgz/trailing.tar.gz')
test_dir = self.get_temp_dir()
archive.extract_tar(test_file, test_dir)
result = os.path.join(test_dir, 'a.txt')
assert os.path.exists(result)
def test_extract_targz_broken(self):
test_file = self.get_test_loc('archive/tgz/broken.tar.gz')
test_dir = self.get_temp_dir()
expected = Exception('Unrecognized archive format')
self.assertRaisesInstance(expected, archive.extract_tar, test_file, test_dir)
def test_extract_targz_with_absolute_path(self):
non_result = '/tmp/subdir'
assert not os.path.exists(non_result)
test_dir = self.get_temp_dir()
test_file = self.get_test_loc('archive/tgz/absolute_path.tar.gz')
archive.extract_tar(test_file, test_dir)
assert not os.path.exists(non_result)
result = os.path.join(test_dir, 'tmp/subdir/a.txt')
assert os.path.exists(result)
def test_extract_targz_with_relative_path(self):
test_file = self.get_test_loc('archive/tgz/relative.tar.gz')
"""
This test file was created with:
import tarfile
tar = tarfile.open("TarTest.tar.gz", "w:gz")
tar.add('a.txt', '../a_parent_folder.txt')
tar.add('b.txt', '../../another_folder/b_two_root.txt')
tar.add('b.txt', '../folder/subfolder/b_subfolder.txt')
tar.close()
"""
test_dir = self.get_temp_dir()
archive.extract_tar(test_file, test_dir)
non_result = os.path.join(test_dir, '../a_parent_folder.txt')
assert not os.path.exists(non_result)
expected = [
'dotdot/dotdot/another_folder/b_two_root.txt',
'dotdot/a_parent_folder.txt',
'dotdot/folder/subfolder/b_subfolder.txt'
]
check_files(test_dir, expected)
def test_extract_targz_with_trailing_data2(self):
test_dir1 = self.get_temp_dir()
test_file = self.get_test_loc('archive/tgz/trailing2.tar.gz')
archive.extract_tar(test_file, test_dir1)
test_dir2 = self.get_temp_dir()
test_file2 = self.get_test_loc('archive/tgz/no_trailing.tar.gz')
archive.extract_tar(test_file2, test_dir2)
assert is_same(test_dir1, test_dir2)
def test_extract_targz_with_mixed_case_and_symlink(self):
test_file = self.get_test_loc('archive/tgz/mixed_case_and_symlink.tgz')
test_dir = self.get_temp_dir()
result = archive.extract_tar(test_file, test_dir)
assert [] == result
exp_file = self.get_test_loc('archive/tgz/mixed_case_and_symlink.tgz.expected.json')
check_files(test_dir, exp_file, regen=False)
def test_extract_targz_symlinks(self):
test_file = self.get_test_loc('archive/tgz/symlink.tar.gz')
test_dir = self.get_temp_dir()
archive.extract_tar(test_file, test_dir)
expected = [
'z/x/a',
# these are skipped
# this is a link: a -> ../x/a
# 'z/y/a',
# this is a broken link: x.a -> ../x.a
# 'z/y/x.a',
# this is a broken link: broken -> ../x/broken
# 'z/z/broken',
]
check_files(test_dir, expected)
def test_extract_targz_from_apache_should_not_return_errors(self):
# from http://archive.apache.org/dist/commons/logging/source/commons-logging-1.1.2-src.tar.gz
# failed with ReadError('not a bzip2 file',)
test_file = self.get_test_loc('archive/tgz/commons-logging-1.1.2-src.tar.gz')
test_dir = self.get_temp_dir()
extractor = archive.get_extractor(test_file)
assert archive.extract_tar == extractor
result = archive.extract_tar(test_file, test_dir)
assert [] == result
assert os.listdir(test_dir)
def test_extract_targz_with_unicode_path_should_extract_without_error(self):
test_file = self.get_test_loc('archive/tgz/tgz_unicode.tgz')
test_dir = self.get_temp_dir()
extractor = archive.get_extractor(test_file)
assert archive.extract_tar == extractor
result = archive.extract_tar(test_file, test_dir)
assert [] == result
assert os.listdir(test_dir)
class TestUncompressGzip(BaseArchiveTestCase):
def test_uncompress_gzip_basic(self):
test_file = self.get_test_loc('archive/gzip/file_4.26-1.diff.gz')
test_dir = self.get_temp_dir()
archive.uncompress_gzip(test_file, test_dir)
result = os.path.join(test_dir, 'file_4.26-1.diff.gz-extract')
assert os.path.exists(result)
def test_uncompress_concatenated_gzip(self):
# Archive created with:
# echo "f1content" > f1
# echo "f2content" > f2
# gzip -k f1
# gzip -k -c f2 >> twofiles.gz
test_file = self.get_test_loc('archive/gzip/twofiles.gz')
test_dir = self.get_temp_dir()
warnings = archive.uncompress_gzip(test_file, test_dir)
result = os.path.join(test_dir, 'twofiles.gz-extract')
assert os.path.exists(result)
assert b'f1content\nf2content\n' == open(result, 'rb').read()
assert [] == warnings
@pytest.mark.xfail(reason='Fails for now on Python 3')
def test_uncompress_gzip_with_trailing_data(self):
test_file = self.get_test_loc('archive/gzip/trailing_data.gz')
test_dir = self.get_temp_dir()
warnings = archive.uncompress_gzip(test_file, test_dir)
result = os.path.join(test_dir, 'trailing_data.gz-extract')
assert os.path.exists(result)
assert [] == warnings
def test_uncompress_gzip_with_leading_data(self):
# even though we do not fail when there is invalid trailing data we
# should still fail on invalid leading data
test_file = self.get_test_loc('archive/gzip/leading_data.gz')
test_dir = self.get_temp_dir()
expected = Exception('Not a gzipped file')
self.assertRaisesInstance(expected, archive.uncompress_gzip, test_file, test_dir)
def test_uncompress_gzip_with_random_data(self):
test_file = self.get_test_loc('archive/gzip/random_binary.data')
test_dir = self.get_temp_dir()
expected = Exception('Not a gzipped file')
self.assertRaisesInstance(expected, archive.uncompress_gzip, test_file, test_dir)
def test_uncompress_gzip_with_backslash_in_path(self):
# weirdly enough, gzip keeps the original path/name
test_file = self.get_test_loc('archive/gzip/backslash_path.gz')
test_dir = self.get_temp_dir()
archive.uncompress_gzip(test_file, test_dir)
result = os.path.join(test_dir, 'backslash_path.gz-extract')
assert os.path.exists(result)
def test_uncompress_gzip_can_uncompress_windows_ntfs_wmz(self):
test_file = self.get_test_loc('archive/wmz/image003.wmz')
test_dir = self.get_temp_dir()
archive.uncompress_gzip(test_file, test_dir)
result = os.path.join(test_dir, 'image003.wmz-extract')
assert os.path.exists(result)
def test_uncompress_gzip_can_uncompress_mysql_arz(self):
test_file = self.get_test_loc('archive/gzip/mysql-arch.ARZ')
test_dir = self.get_temp_dir()
archive.uncompress_gzip(test_file, test_dir)
result = os.path.join(test_dir, 'mysql-arch.ARZ-extract')
assert os.path.exists(result)
class TestTarBz2(BaseArchiveTestCase):
def test_extract_tar_bz2_basic(self):
test_file = self.get_test_loc('archive/tbz/tarred_bzipped.tar.bz2')
test_dir = self.get_temp_dir()
archive.extract_tar(test_file, test_dir)
result = os.path.join(test_dir, 'e/a/b.txt')
assert os.path.exists(result)
def test_extract_tar_bz2_basic_bz(self):
test_file = self.get_test_loc('archive/tbz/tarred_bzipped.bz')
test_dir = self.get_temp_dir()
archive.extract_tar(test_file, test_dir)
result = os.path.join(test_dir, 'e/a/b.txt')
assert os.path.exists(result)
def test_extract_tar_bz2_with_trailing_data__and_wrong_extension(self):
test_file = self.get_test_loc('archive/tbz/single_file_trailing_data.tar.gz')
test_dir = self.get_temp_dir()
archive.extract_tar(test_file, test_dir)
result = os.path.join(test_dir, 'a.txt')
assert os.path.exists(result)
def test_extract_tar_bz2_broken(self):
test_file = self.get_test_loc('archive/tbz/tarred_bzipped_broken.tar.bz2')
test_dir = self.get_temp_dir()
expected = Exception('bzip decompression failed')
self.assertRaisesInstance(expected, archive.extract_tar, test_file, test_dir)
def test_extract_tar_bz2_absolute_path(self):
assert not os.path.exists('/tmp/subdir')
test_dir = self.get_temp_dir()
test_file = self.get_test_loc('archive/tbz/absolute_path.tar.bz2')
archive.extract_tar(test_file, test_dir)
assert not os.path.exists('/tmp/subdir')
result = os.path.join(test_dir, 'tmp/subdir/a.txt')
assert os.path.exists(result)
def test_extract_tar_bz2_relative_path(self):
"""
This test file was created with:
import tarfile
tar = tarfile.open("TarTest.tar.gz", "w:bz")
tar.add('a.txt', '../a_parent_folder.txt')
tar.add('b.txt', '../../another_folder/b_two_root.txt')
tar.add('b.txt', '../folder/subfolder/b_subfolder.txt')
tar.close()
"""
test_file = self.get_test_loc('archive/tbz/bz2withtar_relative.tar.bz2')
test_dir = self.get_temp_dir()
archive.extract_tar(test_file, test_dir)
non_result = os.path.join(test_dir, '../a_parent_folder.txt')
assert not os.path.exists(non_result)
result = os.path.join(test_dir, 'dotdot/folder/subfolder/b_subfolder.txt')
assert os.path.exists(result)
result = os.path.join(test_dir, 'dotdot', 'a_parent_folder.txt')
assert os.path.exists(result)
def test_extract_tar_bz2_iproute(self):
test_file = self.get_test_loc('archive/tbz/iproute2.tar.bz2')
test_dir = self.get_temp_dir()
archive.extract_tar(test_file, test_dir)
result = os.path.join(test_dir, 'iproute2/README')
assert os.path.exists(result)
def test_extract_tar_bz2_multistream(self):
test_file = self.get_test_loc('archive/tbz/bzip2_multistream/example-file.csv.tar.bz2')
test_dir = self.get_temp_dir()
archive.extract_tar(test_file, test_dir)
expected = self.get_test_loc('archive/tbz/bzip2_multistream/example-file.csv')
result = os.path.join(test_dir, 'example-file.csv')
assert open(expected, 'rb').read() == open(result, 'rb').read()
class TestUncompressBz2(BaseArchiveTestCase):
def test_uncompress_bzip2_basic(self):
test_file = self.get_test_loc('archive/bz2/single_file_not_tarred.bz2')
test_dir = self.get_temp_dir()
archive.uncompress_bzip2(test_file, test_dir)
result = os.path.join(test_dir, 'single_file_not_tarred.bz2-extract')
assert os.path.exists(result)
def test_uncompress_bzip2_with_trailing_data(self):
test_file = self.get_test_loc('archive/bz2/single_file_trailing_data.bz2')
test_dir = self.get_temp_dir()
archive.uncompress_bzip2(test_file, test_dir)
result = os.path.join(test_dir, 'single_file_trailing_data.bz2-extract')
assert os.path.exists(result)
def test_uncompress_bzip2_broken(self):
test_file = self.get_test_loc('archive/bz2/bz2_not_tarred_broken.bz2')
test_dir = self.get_temp_dir()
expected = Exception('Invalid data stream')
self.assertRaisesInstance(expected, archive.uncompress_bzip2, test_file, test_dir)
def test_uncompress_bzip2_with_invalid_path(self):
test_file = self.get_test_loc('archive/bz2/bz_invalidpath.bz2')
test_dir = self.get_temp_dir()
archive.uncompress_bzip2(test_file, test_dir)
result = os.path.join(test_dir, 'bz_invalidpath.bz2-extract')
assert os.path.exists(result)
def test_uncompress_bzip2_multistream(self):
test_file = self.get_test_loc('archive/bz2/bzip2_multistream/example-file.csv.bz2')
test_dir = self.get_temp_dir()
archive.uncompress_bzip2(test_file, test_dir)
expected = self.get_test_loc('archive/bz2/bzip2_multistream/expected.csv')
result = os.path.join(test_dir, 'example-file.csv.bz2-extract')
assert open(expected, 'rb').read() == open(result, 'rb').read()
class TestSevenzipBz2(BaseArchiveTestCase):
def test_sevenzip_extract_can_handle_bz2_multistream_differently(self):
test_file = self.get_test_loc('archive/bz2/bzip2_multistream/example-file.csv.bz2')
test_dir = self.get_temp_dir()
sevenzip.extract(test_file, test_dir)
expected = self.get_test_loc('archive/bz2/bzip2_multistream/expected.csv')
# the extraction dir is not created with suffix by 7z
result = os.path.join(test_dir, 'example-file.csv')
expected_extracted = open(expected, 'rb').read()
expected_result = open(result, 'rb').read()
assert expected_extracted == expected_result
class TestShellArchives(BaseArchiveTestCase):
def test_extract_springboot(self):
# a self executable springboot Jar is a zip with a shell script prefix
test_file = self.get_test_loc('archive/shar/demo-spring-boot.jar')
test_dir = self.get_temp_dir()
result = archive.extract_springboot(test_file, test_dir)
assert [] == result
expected = ['META-INF/MANIFEST.MF', 'application.properties']
check_files(test_dir, expected)
def test_springboot_is_not_recognized_without_jar_extension(self):
test_file = self.get_test_loc('archive/shar/demo-spring-boot.sh')
handler = archive.get_best_handler(test_file)
assert None == handler
def test_springboot_is_recognized_with_jar_extension(self):
test_file = self.get_test_loc('archive/shar/demo-spring-boot.jar')
handler = archive.get_best_handler(test_file)
assert handler.name == 'Springboot Java Jar package'
class TestZip(BaseArchiveTestCase):
def test_extract_zip_basic(self):
test_file = self.get_test_loc('archive/zip/basic.zip')
test_dir = self.get_temp_dir()
result = archive.extract_zip(test_file, test_dir)
assert [] == result
expected = ['c/a/a.txt', 'c/b/a.txt', 'c/c/a.txt']
check_files(test_dir, expected)
def test_extract_zip_broken(self):
test_file = self.get_test_loc('archive/zip/zip_broken.zip')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
# note: broken zip opens and extracts with 7z sometimes
assert [] == os.listdir(test_dir)
def test_extract_zip_with_invalid_path(self):
test_file = self.get_test_loc('archive/zip/zip_invalidpath.zip')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
result = os.path.join(test_dir, 'this/that')
assert os.path.exists(result)
def test_extract_zip_with_trailing_data(self):
test_file = self.get_test_loc('archive/zip/zip_trailing_data.zip')
test_dir = self.get_temp_dir()
try:
archive.extract_zip(test_file, test_dir)
except libarchive2.ArchiveError as ae:
assert 'Invalid central directory signature' in str(ae)
# fails because of https://github.com/libarchive/libarchive/issues/545
result = os.path.join(test_dir, 'a.txt')
assert os.path.exists(result)
def test_extract_zip_with_trailing_data2(self):
# test archive created on cygwin with:
# $ echo "test content" > f1
# $ zip test f1
# $ echo "some junk" >> test.zip
test_file = self.get_test_loc('archive/zip/zip_trailing_data2.zip')
test_dir = self.get_temp_dir()
try:
archive.extract_zip(test_file, test_dir)
except libarchive2.ArchiveError as ae:
assert 'Invalid central directory signature' in str(ae)
# fails because of https://github.com/libarchive/libarchive/issues/545
result = os.path.join(test_dir, 'f1')
assert os.path.exists(result)
def test_extract_zip_with_relative_path_using_default_function(self):
# The test files for this test and the next one were created with:
# from zipfile import ZipFile
# f = open('/tmp/a.txt', 'w')
# f.write('some data')
# f.close()
# f = open('/tmp/b.txt', 'w')
# f.write('some data')
# f.close()
# f = ZipFile(os.path.join(self.get_test_loc('archive'), 'relative_parent_folders.zip'), 'w')
# f.write('/tmp/a.txt', '../a_parent_folder.txt')
# f.write('/tmp/b.txt', '../../another_folder/b_two_root.txt')
# f.write('/tmp/b.txt', '../folder/subfolder/b_subfolder.txt')
# f.close()
# f = ZipFile(os.path.join(self.get_test_loc('archive'), 'high_ancest.zip'), 'w')
# f.write('/tmp/a.txt', ('../' * 12) + 'a_parent_folder.txt')
# f.write('/tmp/a.txt', ('../' * 12) + ('sub/' * 6) + 'a_parent_folder_in_sub_1.txt')
# f.write('/tmp/a.txt', ('../' * 6) + ('sub/' * 12) + 'a_parent_folder_in_sub_2.txt')
# f.write('/tmp/a.txt', ('../' * 12) + ('sub/' * 12) + 'a_parent_folder_in_sub_3.txt')
# f.close()
test_file = self.get_test_loc('archive/zip/relative_parent_folders.zip')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
abs_path = os.path.join(test_dir , '../a_parent_folder.txt')
assert not os.path.exists(abs_path)
result = self.collect_extracted_path(test_dir)
expected = [
'/dotdot/',
'/dotdot/a_parent_folder.txt',
'/dotdot/dotdot/',
'/dotdot/dotdot/another_folder/',
'/dotdot/dotdot/another_folder/b_two_root.txt',
'/dotdot/folder/',
'/dotdot/folder/subfolder/',
'/dotdot/folder/subfolder/b_subfolder.txt'
]
assert expected == result
def test_extract_zip_with_relative_path_using_libarchive(self):
test_file = self.get_test_loc('archive/zip/relative_parent_folders.zip')
test_dir = self.get_temp_dir()
result = libarchive2.extract(test_file, test_dir)
assert [] == result
abs_path = os.path.join(test_dir , '../a_parent_folder.txt')
assert not os.path.exists(abs_path)
result = os.path.join(test_dir, 'dotdot/folder/subfolder/b_subfolder.txt')
assert os.path.exists(result)
result = os.path.join(test_dir, 'dotdot/a_parent_folder.txt')
assert os.path.exists(result)
result = os.path.join(test_dir, 'dotdot/dotdot/another_folder/b_two_root.txt')
assert os.path.exists(result)
expected_deeply_nested_relative_path = [
'/dotdot/',
'/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/a_parent_folder.txt',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/a_parent_folder_in_sub_1.txt',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/a_parent_folder_in_sub_3.txt',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/',
'/dotdot/dotdot/dotdot/dotdot/dotdot/dotdot/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/a_parent_folder_in_sub_2.txt'
]
# somehow Windows fails randomly and only on certain windows machines at Appveyor
# so we retest with a skinny expectation
expected_deeply_nested_relative_path_alternative = [
u'/a_parent_folder.txt',
u'/sub/',
u'/sub/sub/',
u'/sub/sub/sub/',
u'/sub/sub/sub/sub/',
u'/sub/sub/sub/sub/sub/',
u'/sub/sub/sub/sub/sub/sub/',
u'/sub/sub/sub/sub/sub/sub/a_parent_folder_in_sub_1.txt',
u'/sub/sub/sub/sub/sub/sub/sub/',
u'/sub/sub/sub/sub/sub/sub/sub/sub/',
u'/sub/sub/sub/sub/sub/sub/sub/sub/sub/',
u'/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/',
u'/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/',
u'/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/',
u'/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/a_parent_folder_in_sub_2.txt',
u'/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/a_parent_folder_in_sub_3.txt']
def test_extract_zip_with_relative_path_deeply_nested(self):
test_file = self.get_test_loc('archive/zip/relative_nested.zip')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
result = self.collect_extracted_path(test_dir)
try:
assert self.expected_deeply_nested_relative_path == result
except:
assert self.expected_deeply_nested_relative_path_alternative == result
@pytest.mark.xfail(reason='Expectations are different on Windows and this may fail on Windows')
def test_extract_zip_with_relative_path_deeply_nested_with_7zip_posix_py3(self):
test_file = self.get_test_loc('archive/zip/relative_nested.zip')
test_dir = self.get_temp_dir()
try:
sevenzip.extract(test_file, test_dir)
self.fail('Should raise an exception')
except ExtractErrorFailedToExtract as e:
assert 'Unknown extraction error' == str(e)
@pytest.mark.skipif(not on_windows, reason='Expectations are different on Windows')
def test_extract_zip_with_relative_path_deeply_nested_with_7zip_windows(self):
test_file = self.get_test_loc('archive/zip/relative_nested.zip')
test_dir = self.get_temp_dir()
sevenzip.extract(test_file, test_dir)
result = self.collect_extracted_path(test_dir)
assert self.expected_deeply_nested_relative_path_alternative == result
def test_list_zip_with_relative_path_deeply_nested_with_7zip(self):
test_file = self.get_test_loc('archive/zip/relative_nested.zip')
result = []
entries, errors = sevenzip.list_entries(test_file)
assert not errors
for entry in entries:
if on_windows:
entry.path = entry.path.replace('\\', '/')
result.append(entry.to_dict(full=False))
expected = [
{'errors': [],
u'is_broken_link': False,
u'is_dir': False,
u'is_file': True,
u'is_hardlink': False,
u'is_special': False,
u'is_symlink': False,
u'link_target': None,
u'path': '../../../../../../../../../../../../a_parent_folder.txt',
u'size': '9'},
{'errors': [],
u'is_broken_link': False,
u'is_dir': False,
u'is_file': True,
u'is_hardlink': False,
u'is_special': False,
u'is_symlink': False,
u'link_target': None,
u'path': '../../../../../../../../../../../../sub/sub/sub/sub/sub/sub/a_parent_folder_in_sub_1.txt',
u'size': '9'},
{'errors': [],
u'is_broken_link': False,
u'is_dir': False,
u'is_file': True,
u'is_hardlink': False,
u'is_special': False,
u'is_symlink': False,
u'link_target': None,
u'path': '../../../../../../sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/a_parent_folder_in_sub_2.txt',
u'size': '9'},
{'errors': [],
u'is_broken_link': False,
u'is_dir': False,
u'is_file': True,
u'is_hardlink': False,
u'is_special': False,
u'is_symlink': False,
u'link_target': None,
u'path': '../../../../../../../../../../../../sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/sub/a_parent_folder_in_sub_3.txt',
u'size': '9'}]
assert expected == result
def test_extract_zip_with_relative_path_deeply_nested_with_libarchive(self):
test_file = self.get_test_loc('archive/zip/relative_nested.zip')
test_dir = self.get_temp_dir()
libarchive2.extract(test_file, test_dir)
result = self.collect_extracted_path(test_dir)
assert self.expected_deeply_nested_relative_path == result
def test_extract_zip_with_password(self):
test_file = self.get_test_loc('archive/zip/zip_password_nexb.zip')
test_dir = self.get_temp_dir()
try:
archive.extract_zip(test_file, test_dir)
except Exception as e:
assert isinstance(e, ExtractErrorFailedToExtract)
assert 'Password protected archive, unable to extract' in str(e)
def test_extract_zip_java_jar(self):
test_file = self.get_test_loc('archive/zip/jar/simple.jar')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
extracted = self.collect_extracted_path(test_dir)
expected = [
'/META-INF/',
'/META-INF/MANIFEST.MF',
'/org/',
'/org/jvnet/',
'/org/jvnet/glassfish/',
'/org/jvnet/glassfish/comms/',
'/org/jvnet/glassfish/comms/sipagent/',
'/org/jvnet/glassfish/comms/sipagent/actions/',
'/org/jvnet/glassfish/comms/sipagent/actions/Bundle.properties',
'/org/jvnet/glassfish/comms/sipagent/actions/SipAgentCookieAction.class',
'/org/jvnet/glassfish/comms/sipagent/actions/bd.png',
'/org/jvnet/glassfish/comms/sipagent/actions/bd24.png',
'/org/jvnet/glassfish/comms/sipagent/org-jvnet-glassfish-comms-sipagent-actions-SipAgentCookieAction.instance',
'/org/jvnet/glassfish/comms/sipagent/org-jvnet-glassfish-comms-sipagent-actions-SipAgentCookieAction_1.instance'
]
assert sorted(expected) == sorted(extracted)
def test_extract_zip_with_duplicated_lowercase_paths(self):
test_file = self.get_test_loc('archive/zip/dup_names.zip')
expected = {'META-INF/license/': None, # a directory
'META-INF/license/LICENSE.base64.txt': 1618,
'META-INF/LICENSE_1': 11366}
self.check_extract(archive.extract_zip, test_file, expected)
def test_extract_zip_with_timezone(self):
test_file = self.get_test_loc('archive/zip/timezone/c.zip')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
expected = [
(os.path.join(test_dir, 'c/a/a.txt'), '2008-07-29'),
(os.path.join(test_dir, 'c/b/a.txt'), '2008-07-29'),
(os.path.join(test_dir, 'c/c/a.txt'), '2008-07-29'),
]
# DST sends a monkey wrench.... so we only test the date, not the time
for loc, expected_date in expected:
result = commoncode_date.get_file_mtime(loc)
assert result.startswith(expected_date)
def test_extract_zip_with_timezone_2(self):
test_file = self.get_test_loc('archive/zip/timezone/projecttest.zip')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
# DST sends a monkey wrench.... so we only test the date, not the time
# and we accept some varation in the date ...
expected = [
(os.path.join(test_dir, 'primes.txt'), ('2009-12-05', '2009-12-06',)),
(os.path.join(test_dir, 'primes2.txt'), ('2009-12-05', '2009-12-06',))
]
for loc, expected_date in expected:
result = commoncode_date.get_file_mtime(loc)
assert result.startswith(expected_date)
def test_extract_zip_with_backslash_in_path_1(self):
test_file = self.get_test_loc('archive/zip/backslash/backslash1.zip')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
# Info-ZIP 'zip' displays:
# warning: booxw-1202-bin.distribution.zip appears to use
# backslashes as path separators (which is the right thing to do)
expected = ['scripts/AutomaticClose.int']
check_files(test_dir, expected)
result = os.path.join(test_dir, 'scripts/AutomaticClose.int')
assert os.path.exists(result)
def test_extract_zip_with_backslash_in_path_2(self):
test_file = self.get_test_loc('archive/zip/backslash/AspectJTest.zip')
test_dir = self.get_temp_dir()
archive.extract_zip(test_file, test_dir)
expected = '''
AspectJTest/.classpath
AspectJTest/.project