Debian Codegraph @

Lib/test/test_zipfile/test_core.py

raw
1 import _pyio
2 import array
3 import contextlib
4 import importlib.util
5 import io
6 import itertools
7 import os
8 import posixpath
9 import stat
10 import struct
11 import subprocess
12 import sys
13 import time
14 import unittest
15 import unittest.mock as mock
16 import zipfile
17
18
19 from tempfile import TemporaryFile
20 from random import randint, random, randbytes
21
22 from test import archiver_tests
23 from test.support import script_helper, os_helper
24 from test.support import (
25 findfile, requires_zlib, requires_bz2, requires_lzma,
26 requires_zstd, captured_stdout, captured_stderr, requires_subprocess,
27 cpython_only
28 )
29 from test.support.os_helper import (
30 TESTFN, unlink, rmtree, temp_dir, temp_cwd, fd_count, FakePath
31 )
32 from test.support.import_helper import ensure_lazy_imports
33
34
35 TESTFN2 = TESTFN + "2"
36 TESTFNDIR = TESTFN + "d"
37 FIXEDTEST_SIZE = 1000
38 DATAFILES_DIR = 'zipfile_datafiles'
39
40 SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
41 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
42 ('ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
43 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
44
45 def get_files(test):
46 yield TESTFN2
47 with TemporaryFile() as f:
48 yield f
49 test.assertFalse(f.closed)
50 with io.BytesIO() as f:
51 yield f
52 test.assertFalse(f.closed)
53
54
55 class LazyImportTest(unittest.TestCase):
56 @cpython_only
57 def test_lazy_import(self):
58 ensure_lazy_imports("zipfile", {"typing"})
59
60
61 class AbstractTestsWithSourceFile:
62 @classmethod
63 def setUpClass(cls):
64 cls.line_gen = [bytes("Zipfile test line %d. random float: %f\n" %
65 (i, random()), "ascii")
66 for i in range(FIXEDTEST_SIZE)]
67 cls.data = b''.join(cls.line_gen)
68
69 def setUp(self):
70 # Make a source file with some lines
71 with open(TESTFN, "wb") as fp:
72 fp.write(self.data)
73
74 def make_test_archive(self, f, compression, compresslevel=None):
75 kwargs = {'compression': compression, 'compresslevel': compresslevel}
76 # Create the ZIP archive
77 with zipfile.ZipFile(f, "w", **kwargs) as zipfp:
78 zipfp.write(TESTFN, "another.name")
79 zipfp.write(TESTFN, TESTFN)
80 zipfp.writestr("strfile", self.data)
81 with zipfp.open('written-open-w', mode='w') as f:
82 for line in self.line_gen:
83 f.write(line)
84
85 def zip_test(self, f, compression, compresslevel=None):
86 self.make_test_archive(f, compression, compresslevel)
87
88 # Read the ZIP archive
89 with zipfile.ZipFile(f, "r", compression) as zipfp:
90 self.assertEqual(zipfp.read(TESTFN), self.data)
91 self.assertEqual(zipfp.read("another.name"), self.data)
92 self.assertEqual(zipfp.read("strfile"), self.data)
93
94 # Print the ZIP directory
95 fp = io.StringIO()
96 zipfp.printdir(file=fp)
97 directory = fp.getvalue()
98 lines = directory.splitlines()
99 self.assertEqual(len(lines), 5) # Number of files + header
100
101 self.assertIn('File Name', lines[0])
102 self.assertIn('Modified', lines[0])
103 self.assertIn('Size', lines[0])
104
105 fn, date, time_, size = lines[1].split()
106 self.assertEqual(fn, 'another.name')
107 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
108 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
109 self.assertEqual(size, str(len(self.data)))
110
111 # Check the namelist
112 names = zipfp.namelist()
113 self.assertEqual(len(names), 4)
114 self.assertIn(TESTFN, names)
115 self.assertIn("another.name", names)
116 self.assertIn("strfile", names)
117 self.assertIn("written-open-w", names)
118
119 # Check infolist
120 infos = zipfp.infolist()
121 names = [i.filename for i in infos]
122 self.assertEqual(len(names), 4)
123 self.assertIn(TESTFN, names)
124 self.assertIn("another.name", names)
125 self.assertIn("strfile", names)
126 self.assertIn("written-open-w", names)
127 for i in infos:
128 self.assertEqual(i.file_size, len(self.data))
129
130 # check getinfo
131 for nm in (TESTFN, "another.name", "strfile", "written-open-w"):
132 info = zipfp.getinfo(nm)
133 self.assertEqual(info.filename, nm)
134 self.assertEqual(info.file_size, len(self.data))
135
136 # Check that testzip thinks the archive is ok
137 # (it returns None if all contents could be read properly)
138 self.assertIsNone(zipfp.testzip())
139
140 def test_basic(self):
141 for f in get_files(self):
142 self.zip_test(f, self.compression)
143
144 def zip_open_test(self, f, compression):
145 self.make_test_archive(f, compression)
146
147 # Read the ZIP archive
148 with zipfile.ZipFile(f, "r", compression) as zipfp:
149 zipdata1 = []
150 with zipfp.open(TESTFN) as zipopen1:
151 while True:
152 read_data = zipopen1.read(256)
153 if not read_data:
154 break
155 zipdata1.append(read_data)
156
157 zipdata2 = []
158 with zipfp.open("another.name") as zipopen2:
159 while True:
160 read_data = zipopen2.read(256)
161 if not read_data:
162 break
163 zipdata2.append(read_data)
164
165 self.assertEqual(b''.join(zipdata1), self.data)
166 self.assertEqual(b''.join(zipdata2), self.data)
167
168 def test_open(self):
169 for f in get_files(self):
170 self.zip_open_test(f, self.compression)
171
172 def test_open_with_pathlike(self):
173 path = FakePath(TESTFN2)
174 self.zip_open_test(path, self.compression)
175 with zipfile.ZipFile(path, "r", self.compression) as zipfp:
176 self.assertIsInstance(zipfp.filename, str)
177
178 def zip_random_open_test(self, f, compression):
179 self.make_test_archive(f, compression)
180
181 # Read the ZIP archive
182 with zipfile.ZipFile(f, "r", compression) as zipfp:
183 zipdata1 = []
184 with zipfp.open(TESTFN) as zipopen1:
185 while True:
186 read_data = zipopen1.read(randint(1, 1024))
187 if not read_data:
188 break
189 zipdata1.append(read_data)
190
191 self.assertEqual(b''.join(zipdata1), self.data)
192
193 def test_random_open(self):
194 for f in get_files(self):
195 self.zip_random_open_test(f, self.compression)
196
197 def zip_read1_test(self, f, compression):
198 self.make_test_archive(f, compression)
199
200 # Read the ZIP archive
201 with zipfile.ZipFile(f, "r") as zipfp, \
202 zipfp.open(TESTFN) as zipopen:
203 zipdata = []
204 while True:
205 read_data = zipopen.read1(-1)
206 if not read_data:
207 break
208 zipdata.append(read_data)
209
210 self.assertEqual(b''.join(zipdata), self.data)
211
212 def test_read1(self):
213 for f in get_files(self):
214 self.zip_read1_test(f, self.compression)
215
216 def zip_read1_10_test(self, f, compression):
217 self.make_test_archive(f, compression)
218
219 # Read the ZIP archive
220 with zipfile.ZipFile(f, "r") as zipfp, \
221 zipfp.open(TESTFN) as zipopen:
222 zipdata = []
223 while True:
224 read_data = zipopen.read1(10)
225 self.assertLessEqual(len(read_data), 10)
226 if not read_data:
227 break
228 zipdata.append(read_data)
229
230 self.assertEqual(b''.join(zipdata), self.data)
231
232 def test_read1_10(self):
233 for f in get_files(self):
234 self.zip_read1_10_test(f, self.compression)
235
236 def zip_readline_read_test(self, f, compression):
237 self.make_test_archive(f, compression)
238
239 # Read the ZIP archive
240 with zipfile.ZipFile(f, "r") as zipfp, \
241 zipfp.open(TESTFN) as zipopen:
242 data = b''
243 while True:
244 read = zipopen.readline()
245 if not read:
246 break
247 data += read
248
249 read = zipopen.read(100)
250 if not read:
251 break
252 data += read
253
254 self.assertEqual(data, self.data)
255
256 def test_readline_read(self):
257 # Issue #7610: calls to readline() interleaved with calls to read().
258 for f in get_files(self):
259 self.zip_readline_read_test(f, self.compression)
260
261 def zip_readline_test(self, f, compression):
262 self.make_test_archive(f, compression)
263
264 # Read the ZIP archive
265 with zipfile.ZipFile(f, "r") as zipfp:
266 with zipfp.open(TESTFN) as zipopen:
267 for line in self.line_gen:
268 linedata = zipopen.readline()
269 self.assertEqual(linedata, line)
270
271 def test_readline(self):
272 for f in get_files(self):
273 self.zip_readline_test(f, self.compression)
274
275 def zip_readlines_test(self, f, compression):
276 self.make_test_archive(f, compression)
277
278 # Read the ZIP archive
279 with zipfile.ZipFile(f, "r") as zipfp:
280 with zipfp.open(TESTFN) as zipopen:
281 ziplines = zipopen.readlines()
282 for line, zipline in zip(self.line_gen, ziplines):
283 self.assertEqual(zipline, line)
284
285 def test_readlines(self):
286 for f in get_files(self):
287 self.zip_readlines_test(f, self.compression)
288
289 def zip_iterlines_test(self, f, compression):
290 self.make_test_archive(f, compression)
291
292 # Read the ZIP archive
293 with zipfile.ZipFile(f, "r") as zipfp:
294 with zipfp.open(TESTFN) as zipopen:
295 for line, zipline in zip(self.line_gen, zipopen):
296 self.assertEqual(zipline, line)
297
298 def test_iterlines(self):
299 for f in get_files(self):
300 self.zip_iterlines_test(f, self.compression)
301
302 def test_low_compression(self):
303 """Check for cases where compressed data is larger than original."""
304 # Create the ZIP archive
305 with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipfp:
306 zipfp.writestr("strfile", '12')
307
308 # Get an open object for strfile
309 with zipfile.ZipFile(TESTFN2, "r", self.compression) as zipfp:
310 with zipfp.open("strfile") as openobj:
311 self.assertEqual(openobj.read(1), b'1')
312 self.assertEqual(openobj.read(1), b'2')
313
314 def test_writestr_compression(self):
315 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
316 zipfp.writestr("b.txt", "hello world", compress_type=self.compression)
317 info = zipfp.getinfo('b.txt')
318 self.assertEqual(info.compress_type, self.compression)
319
320 def test_writestr_compresslevel(self):
321 with zipfile.ZipFile(TESTFN2, "w", compresslevel=1) as zipfp:
322 zipfp.writestr("a.txt", "hello world", compress_type=self.compression)
323 zipfp.writestr("b.txt", "hello world", compress_type=self.compression,
324 compresslevel=2)
325
326 # Compression level follows the constructor.
327 a_info = zipfp.getinfo('a.txt')
328 self.assertEqual(a_info.compress_type, self.compression)
329 self.assertEqual(a_info.compress_level, 1)
330
331 # Compression level is overridden.
332 b_info = zipfp.getinfo('b.txt')
333 self.assertEqual(b_info.compress_type, self.compression)
334 self.assertEqual(b_info._compresslevel, 2)
335
336 def test_read_return_size(self):
337 # Issue #9837: ZipExtFile.read() shouldn't return more bytes
338 # than requested.
339 for test_size in (1, 4095, 4096, 4097, 16384):
340 file_size = test_size + 1
341 junk = randbytes(file_size)
342 with zipfile.ZipFile(io.BytesIO(), "w", self.compression) as zipf:
343 zipf.writestr('foo', junk)
344 with zipf.open('foo', 'r') as fp:
345 buf = fp.read(test_size)
346 self.assertEqual(len(buf), test_size)
347
348 def test_truncated_zipfile(self):
349 fp = io.BytesIO()
350 with zipfile.ZipFile(fp, mode='w') as zipf:
351 zipf.writestr('strfile', self.data, compress_type=self.compression)
352 end_offset = fp.tell()
353 zipfiledata = fp.getvalue()
354
355 fp = io.BytesIO(zipfiledata)
356 with zipfile.ZipFile(fp) as zipf:
357 with zipf.open('strfile') as zipopen:
358 fp.truncate(end_offset - 20)
359 with self.assertRaises(EOFError):
360 zipopen.read()
361
362 fp = io.BytesIO(zipfiledata)
363 with zipfile.ZipFile(fp) as zipf:
364 with zipf.open('strfile') as zipopen:
365 fp.truncate(end_offset - 20)
366 with self.assertRaises(EOFError):
367 while zipopen.read(100):
368 pass
369
370 fp = io.BytesIO(zipfiledata)
371 with zipfile.ZipFile(fp) as zipf:
372 with zipf.open('strfile') as zipopen:
373 fp.truncate(end_offset - 20)
374 with self.assertRaises(EOFError):
375 while zipopen.read1(100):
376 pass
377
378 def test_repr(self):
379 fname = 'file.name'
380 for f in get_files(self):
381 with zipfile.ZipFile(f, 'w', self.compression) as zipfp:
382 zipfp.write(TESTFN, fname)
383 r = repr(zipfp)
384 self.assertIn("mode='w'", r)
385
386 with zipfile.ZipFile(f, 'r') as zipfp:
387 r = repr(zipfp)
388 if isinstance(f, str):
389 self.assertIn('filename=%r' % f, r)
390 else:
391 self.assertIn('file=%r' % f, r)
392 self.assertIn("mode='r'", r)
393 r = repr(zipfp.getinfo(fname))
394 self.assertIn('filename=%r' % fname, r)
395 self.assertIn('filemode=', r)
396 self.assertIn('file_size=', r)
397 if self.compression != zipfile.ZIP_STORED:
398 self.assertIn('compress_type=', r)
399 self.assertIn('compress_size=', r)
400 with zipfp.open(fname) as zipopen:
401 r = repr(zipopen)
402 self.assertIn('name=%r' % fname, r)
403 if self.compression != zipfile.ZIP_STORED:
404 self.assertIn('compress_type=', r)
405 self.assertIn('[closed]', repr(zipopen))
406 self.assertIn('[closed]', repr(zipfp))
407
408 def test_compresslevel_basic(self):
409 for f in get_files(self):
410 self.zip_test(f, self.compression, compresslevel=9)
411
412 def test_per_file_compresslevel(self):
413 """Check that files within a Zip archive can have different
414 compression levels."""
415 with zipfile.ZipFile(TESTFN2, "w", compresslevel=1) as zipfp:
416 zipfp.write(TESTFN, 'compress_1')
417 zipfp.write(TESTFN, 'compress_9', compresslevel=9)
418 one_info = zipfp.getinfo('compress_1')
419 nine_info = zipfp.getinfo('compress_9')
420 self.assertEqual(one_info._compresslevel, 1)
421 self.assertEqual(nine_info.compress_level, 9)
422
423 def test_writing_errors(self):
424 class BrokenFile(io.BytesIO):
425 def write(self, data):
426 nonlocal count
427 if count is not None:
428 if count == stop:
429 raise OSError
430 count += 1
431 super().write(data)
432
433 stop = 0
434 while True:
435 testfile = BrokenFile()
436 count = None
437 with zipfile.ZipFile(testfile, 'w', self.compression) as zipfp:
438 with zipfp.open('file1', 'w') as f:
439 f.write(b'data1')
440 count = 0
441 try:
442 with zipfp.open('file2', 'w') as f:
443 f.write(b'data2')
444 except OSError:
445 stop += 1
446 else:
447 break
448 finally:
449 count = None
450 with zipfile.ZipFile(io.BytesIO(testfile.getvalue())) as zipfp:
451 self.assertEqual(zipfp.namelist(), ['file1'])
452 self.assertEqual(zipfp.read('file1'), b'data1')
453
454 with zipfile.ZipFile(io.BytesIO(testfile.getvalue())) as zipfp:
455 self.assertEqual(zipfp.namelist(), ['file1', 'file2'])
456 self.assertEqual(zipfp.read('file1'), b'data1')
457 self.assertEqual(zipfp.read('file2'), b'data2')
458
459 def test_zipextfile_attrs(self):
460 fname = "somefile.txt"
461 with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
462 zipfp.writestr(fname, "bogus")
463
464 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
465 with zipfp.open(fname) as fid:
466 self.assertEqual(fid.name, fname)
467 self.assertRaises(io.UnsupportedOperation, fid.fileno)
468 self.assertEqual(fid.mode, 'rb')
469 self.assertIs(fid.readable(), True)
470 self.assertIs(fid.writable(), False)
471 self.assertIs(fid.seekable(), True)
472 self.assertIs(fid.closed, False)
473 self.assertIs(fid.closed, True)
474 self.assertEqual(fid.name, fname)
475 self.assertEqual(fid.mode, 'rb')
476 self.assertRaises(io.UnsupportedOperation, fid.fileno)
477 self.assertRaises(ValueError, fid.readable)
478 self.assertIs(fid.writable(), False)
479 self.assertRaises(ValueError, fid.seekable)
480
481 def tearDown(self):
482 unlink(TESTFN)
483 unlink(TESTFN2)
484
485
486 class StoredTestsWithSourceFile(AbstractTestsWithSourceFile,
487 unittest.TestCase):
488 compression = zipfile.ZIP_STORED
489 test_low_compression = None
490
491 def zip_test_writestr_permissions(self, f, compression):
492 # Make sure that writestr and open(... mode='w') create files with
493 # mode 0600, when they are passed a name rather than a ZipInfo
494 # instance.
495
496 self.make_test_archive(f, compression)
497 with zipfile.ZipFile(f, "r") as zipfp:
498 zinfo = zipfp.getinfo('strfile')
499 self.assertEqual(zinfo.external_attr, 0o600 << 16)
500
501 zinfo2 = zipfp.getinfo('written-open-w')
502 self.assertEqual(zinfo2.external_attr, 0o600 << 16)
503
504 def test_writestr_permissions(self):
505 for f in get_files(self):
506 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
507
508 def test_absolute_arcnames(self):
509 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
510 zipfp.write(TESTFN, "/absolute")
511
512 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
513 self.assertEqual(zipfp.namelist(), ["absolute"])
514
515 def test_append_to_zip_file(self):
516 """Test appending to an existing zipfile."""
517 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
518 zipfp.write(TESTFN, TESTFN)
519
520 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
521 zipfp.writestr("strfile", self.data)
522 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
523
524 def test_append_to_non_zip_file(self):
525 """Test appending to an existing file that is not a zipfile."""
526 # NOTE: this test fails if len(d) < 22 because of the first
527 # line "fpin.seek(-22, 2)" in _EndRecData
528 data = b'I am not a ZipFile!'*10
529 with open(TESTFN2, 'wb') as f:
530 f.write(data)
531
532 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
533 zipfp.write(TESTFN, TESTFN)
534
535 with open(TESTFN2, 'rb') as f:
536 f.seek(len(data))
537 with zipfile.ZipFile(f, "r") as zipfp:
538 self.assertEqual(zipfp.namelist(), [TESTFN])
539 self.assertEqual(zipfp.read(TESTFN), self.data)
540 with open(TESTFN2, 'rb') as f:
541 self.assertEqual(f.read(len(data)), data)
542 zipfiledata = f.read()
543 with io.BytesIO(zipfiledata) as bio, zipfile.ZipFile(bio) as zipfp:
544 self.assertEqual(zipfp.namelist(), [TESTFN])
545 self.assertEqual(zipfp.read(TESTFN), self.data)
546
547 def test_read_concatenated_zip_file(self):
548 with io.BytesIO() as bio:
549 with zipfile.ZipFile(bio, 'w', zipfile.ZIP_STORED) as zipfp:
550 zipfp.write(TESTFN, TESTFN)
551 zipfiledata = bio.getvalue()
552 data = b'I am not a ZipFile!'*10
553 with open(TESTFN2, 'wb') as f:
554 f.write(data)
555 f.write(zipfiledata)
556
557 with zipfile.ZipFile(TESTFN2) as zipfp:
558 self.assertEqual(zipfp.namelist(), [TESTFN])
559 self.assertEqual(zipfp.read(TESTFN), self.data)
560
561 def test_append_to_concatenated_zip_file(self):
562 with io.BytesIO() as bio:
563 with zipfile.ZipFile(bio, 'w', zipfile.ZIP_STORED) as zipfp:
564 zipfp.write(TESTFN, TESTFN)
565 zipfiledata = bio.getvalue()
566 data = b'I am not a ZipFile!'*1000000
567 with open(TESTFN2, 'wb') as f:
568 f.write(data)
569 f.write(zipfiledata)
570
571 with zipfile.ZipFile(TESTFN2, 'a') as zipfp:
572 self.assertEqual(zipfp.namelist(), [TESTFN])
573 zipfp.writestr('strfile', self.data)
574
575 with open(TESTFN2, 'rb') as f:
576 self.assertEqual(f.read(len(data)), data)
577 zipfiledata = f.read()
578 with io.BytesIO(zipfiledata) as bio, zipfile.ZipFile(bio) as zipfp:
579 self.assertEqual(zipfp.namelist(), [TESTFN, 'strfile'])
580 self.assertEqual(zipfp.read(TESTFN), self.data)
581 self.assertEqual(zipfp.read('strfile'), self.data)
582
583 def test_ignores_newline_at_end(self):
584 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
585 zipfp.write(TESTFN, TESTFN)
586 with open(TESTFN2, 'a', encoding='utf-8') as f:
587 f.write("\r\n\00\00\00")
588 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
589 self.assertIsInstance(zipfp, zipfile.ZipFile)
590
591 def test_ignores_stuff_appended_past_comments(self):
592 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
593 zipfp.comment = b"this is a comment"
594 zipfp.write(TESTFN, TESTFN)
595 with open(TESTFN2, 'a', encoding='utf-8') as f:
596 f.write("abcdef\r\n")
597 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
598 self.assertIsInstance(zipfp, zipfile.ZipFile)
599 self.assertEqual(zipfp.comment, b"this is a comment")
600
601 def test_write_default_name(self):
602 """Check that calling ZipFile.write without arcname specified
603 produces the expected result."""
604 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
605 zipfp.write(TESTFN)
606 with open(TESTFN, "rb") as f:
607 self.assertEqual(zipfp.read(TESTFN), f.read())
608
609 def test_io_on_closed_zipextfile(self):
610 fname = "somefile.txt"
611 with zipfile.ZipFile(TESTFN2, mode="w", compression=self.compression) as zipfp:
612 zipfp.writestr(fname, "bogus")
613
614 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
615 with zipfp.open(fname) as fid:
616 fid.close()
617 self.assertIs(fid.closed, True)
618 self.assertRaises(ValueError, fid.read)
619 self.assertRaises(ValueError, fid.seek, 0)
620 self.assertRaises(ValueError, fid.tell)
621
622 def test_write_to_readonly(self):
623 """Check that trying to call write() on a readonly ZipFile object
624 raises a ValueError."""
625 with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
626 zipfp.writestr("somefile.txt", "bogus")
627
628 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
629 self.assertRaises(ValueError, zipfp.write, TESTFN)
630
631 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
632 with self.assertRaises(ValueError):
633 zipfp.open(TESTFN, mode='w')
634
635 def test_add_file_before_1980(self):
636 # Set atime and mtime to 1970-01-01
637 os.utime(TESTFN, (0, 0))
638 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
639 self.assertRaises(ValueError, zipfp.write, TESTFN)
640
641 with zipfile.ZipFile(TESTFN2, "w", strict_timestamps=False) as zipfp:
642 zipfp.write(TESTFN)
643 zinfo = zipfp.getinfo(TESTFN)
644 self.assertEqual(zinfo.date_time, (1980, 1, 1, 0, 0, 0))
645
646 def test_add_file_after_2107(self):
647 # Set atime and mtime to 2108-12-30
648 ts = 4386268800
649 try:
650 time.localtime(ts)
651 except OverflowError:
652 self.skipTest(f'time.localtime({ts}) raises OverflowError')
653 try:
654 os.utime(TESTFN, (ts, ts))
655 except OverflowError:
656 self.skipTest('Host fs cannot set timestamp to required value.')
657
658 mtime_ns = os.stat(TESTFN).st_mtime_ns
659 if mtime_ns != (4386268800 * 10**9):
660 # XFS filesystem is limited to 32-bit timestamp, but the syscall
661 # didn't fail. Moreover, there is a VFS bug which returns
662 # a cached timestamp which is different than the value on disk.
663 #
664 # Test st_mtime_ns rather than st_mtime to avoid rounding issues.
665 #
666 # https://bugzilla.redhat.com/show_bug.cgi?id=1795576
667 # https://bugs.python.org/issue39460#msg360952
668 self.skipTest(f"Linux VFS/XFS kernel bug detected: {mtime_ns=}")
669
670 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
671 self.assertRaises(struct.error, zipfp.write, TESTFN)
672
673 with zipfile.ZipFile(TESTFN2, "w", strict_timestamps=False) as zipfp:
674 zipfp.write(TESTFN)
675 zinfo = zipfp.getinfo(TESTFN)
676 self.assertEqual(zinfo.date_time, (2107, 12, 31, 23, 59, 59))
677
678
679 @requires_zlib()
680 class DeflateTestsWithSourceFile(AbstractTestsWithSourceFile,
681 unittest.TestCase):
682 compression = zipfile.ZIP_DEFLATED
683
684 def test_per_file_compression(self):
685 """Check that files within a Zip archive can have different
686 compression options."""
687 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
688 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
689 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
690 sinfo = zipfp.getinfo('storeme')
691 dinfo = zipfp.getinfo('deflateme')
692 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
693 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
694
695 @requires_bz2()
696 class Bzip2TestsWithSourceFile(AbstractTestsWithSourceFile,
697 unittest.TestCase):
698 compression = zipfile.ZIP_BZIP2
699
700 @requires_lzma()
701 class LzmaTestsWithSourceFile(AbstractTestsWithSourceFile,
702 unittest.TestCase):
703 compression = zipfile.ZIP_LZMA
704
705 @requires_zstd()
706 class ZstdTestsWithSourceFile(AbstractTestsWithSourceFile,
707 unittest.TestCase):
708 compression = zipfile.ZIP_ZSTANDARD
709
710 class AbstractTestZip64InSmallFiles:
711 # These tests test the ZIP64 functionality without using large files,
712 # see test_zipfile64 for proper tests.
713
714 @classmethod
715 def setUpClass(cls):
716 line_gen = (bytes("Test of zipfile line %d." % i, "ascii")
717 for i in range(0, FIXEDTEST_SIZE))
718 cls.data = b'\n'.join(line_gen)
719
720 def setUp(self):
721 self._limit = zipfile.ZIP64_LIMIT
722 self._filecount_limit = zipfile.ZIP_FILECOUNT_LIMIT
723 zipfile.ZIP64_LIMIT = 1000
724 zipfile.ZIP_FILECOUNT_LIMIT = 9
725
726 # Make a source file with some lines
727 with open(TESTFN, "wb") as fp:
728 fp.write(self.data)
729
730 def zip_test(self, f, compression):
731 # Create the ZIP archive
732 with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
733 zipfp.write(TESTFN, "another.name")
734 zipfp.write(TESTFN, TESTFN)
735 zipfp.writestr("strfile", self.data)
736
737 # Read the ZIP archive
738 with zipfile.ZipFile(f, "r", compression) as zipfp:
739 self.assertEqual(zipfp.read(TESTFN), self.data)
740 self.assertEqual(zipfp.read("another.name"), self.data)
741 self.assertEqual(zipfp.read("strfile"), self.data)
742
743 # Print the ZIP directory
744 fp = io.StringIO()
745 zipfp.printdir(fp)
746
747 directory = fp.getvalue()
748 lines = directory.splitlines()
749 self.assertEqual(len(lines), 4) # Number of files + header
750
751 self.assertIn('File Name', lines[0])
752 self.assertIn('Modified', lines[0])
753 self.assertIn('Size', lines[0])
754
755 fn, date, time_, size = lines[1].split()
756 self.assertEqual(fn, 'another.name')
757 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
758 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
759 self.assertEqual(size, str(len(self.data)))
760
761 # Check the namelist
762 names = zipfp.namelist()
763 self.assertEqual(len(names), 3)
764 self.assertIn(TESTFN, names)
765 self.assertIn("another.name", names)
766 self.assertIn("strfile", names)
767
768 # Check infolist
769 infos = zipfp.infolist()
770 names = [i.filename for i in infos]
771 self.assertEqual(len(names), 3)
772 self.assertIn(TESTFN, names)
773 self.assertIn("another.name", names)
774 self.assertIn("strfile", names)
775 for i in infos:
776 self.assertEqual(i.file_size, len(self.data))
777
778 # check getinfo
779 for nm in (TESTFN, "another.name", "strfile"):
780 info = zipfp.getinfo(nm)
781 self.assertEqual(info.filename, nm)
782 self.assertEqual(info.file_size, len(self.data))
783
784 # Check that testzip thinks the archive is valid
785 self.assertIsNone(zipfp.testzip())
786
787 def test_basic(self):
788 for f in get_files(self):
789 self.zip_test(f, self.compression)
790
791 def test_too_many_files(self):
792 # This test checks that more than 64k files can be added to an archive,
793 # and that the resulting archive can be read properly by ZipFile
794 zipf = zipfile.ZipFile(TESTFN, "w", self.compression,
795 allowZip64=True)
796 zipf.debug = 100
797 numfiles = 15
798 for i in range(numfiles):
799 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
800 self.assertEqual(len(zipf.namelist()), numfiles)
801 zipf.close()
802
803 zipf2 = zipfile.ZipFile(TESTFN, "r", self.compression)
804 self.assertEqual(len(zipf2.namelist()), numfiles)
805 for i in range(numfiles):
806 content = zipf2.read("foo%08d" % i).decode('ascii')
807 self.assertEqual(content, "%d" % (i**3 % 57))
808 zipf2.close()
809
810 def test_too_many_files_append(self):
811 zipf = zipfile.ZipFile(TESTFN, "w", self.compression,
812 allowZip64=False)
813 zipf.debug = 100
814 numfiles = 9
815 for i in range(numfiles):
816 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
817 self.assertEqual(len(zipf.namelist()), numfiles)
818 with self.assertRaises(zipfile.LargeZipFile):
819 zipf.writestr("foo%08d" % numfiles, b'')
820 self.assertEqual(len(zipf.namelist()), numfiles)
821 zipf.close()
822
823 zipf = zipfile.ZipFile(TESTFN, "a", self.compression,
824 allowZip64=False)
825 zipf.debug = 100
826 self.assertEqual(len(zipf.namelist()), numfiles)
827 with self.assertRaises(zipfile.LargeZipFile):
828 zipf.writestr("foo%08d" % numfiles, b'')
829 self.assertEqual(len(zipf.namelist()), numfiles)
830 zipf.close()
831
832 zipf = zipfile.ZipFile(TESTFN, "a", self.compression,
833 allowZip64=True)
834 zipf.debug = 100
835 self.assertEqual(len(zipf.namelist()), numfiles)
836 numfiles2 = 15
837 for i in range(numfiles, numfiles2):
838 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
839 self.assertEqual(len(zipf.namelist()), numfiles2)
840 zipf.close()
841
842 zipf2 = zipfile.ZipFile(TESTFN, "r", self.compression)
843 self.assertEqual(len(zipf2.namelist()), numfiles2)
844 for i in range(numfiles2):
845 content = zipf2.read("foo%08d" % i).decode('ascii')
846 self.assertEqual(content, "%d" % (i**3 % 57))
847 zipf2.close()
848
849 def tearDown(self):
850 zipfile.ZIP64_LIMIT = self._limit
851 zipfile.ZIP_FILECOUNT_LIMIT = self._filecount_limit
852 unlink(TESTFN)
853 unlink(TESTFN2)
854
855
856 class StoredTestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
857 unittest.TestCase):
858 compression = zipfile.ZIP_STORED
859
860 def large_file_exception_test(self, f, compression):
861 with zipfile.ZipFile(f, "w", compression, allowZip64=False) as zipfp:
862 self.assertRaises(zipfile.LargeZipFile,
863 zipfp.write, TESTFN, "another.name")
864
865 def large_file_exception_test2(self, f, compression):
866 with zipfile.ZipFile(f, "w", compression, allowZip64=False) as zipfp:
867 self.assertRaises(zipfile.LargeZipFile,
868 zipfp.writestr, "another.name", self.data)
869
870 def test_large_file_exception(self):
871 for f in get_files(self):
872 self.large_file_exception_test(f, zipfile.ZIP_STORED)
873 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
874
875 def test_absolute_arcnames(self):
876 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED,
877 allowZip64=True) as zipfp:
878 zipfp.write(TESTFN, "/absolute")
879
880 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
881 self.assertEqual(zipfp.namelist(), ["absolute"])
882
883 def test_append(self):
884 # Test that appending to the Zip64 archive doesn't change
885 # extra fields of existing entries.
886 with zipfile.ZipFile(TESTFN2, "w", allowZip64=True) as zipfp:
887 zipfp.writestr("strfile", self.data)
888 with zipfile.ZipFile(TESTFN2, "r", allowZip64=True) as zipfp:
889 zinfo = zipfp.getinfo("strfile")
890 extra = zinfo.extra
891 with zipfile.ZipFile(TESTFN2, "a", allowZip64=True) as zipfp:
892 zipfp.writestr("strfile2", self.data)
893 with zipfile.ZipFile(TESTFN2, "r", allowZip64=True) as zipfp:
894 zinfo = zipfp.getinfo("strfile")
895 self.assertEqual(zinfo.extra, extra)
896
897 def make_zip64_file(
898 self, file_size_64_set=False, file_size_extra=False,
899 compress_size_64_set=False, compress_size_extra=False,
900 header_offset_64_set=False, header_offset_extra=False,
901 extensible_data=b'',
902 end_of_central_dir_size=None, offset_to_end_of_central_dir=None,
903 ):
904 """Generate bytes sequence for a zip with (incomplete) zip64 data.
905
906 The actual values (not the zip 64 0xffffffff values) stored in the file
907 are:
908 file_size: 8
909 compress_size: 8
910 header_offset: 0
911 """
912 actual_size = 8
913 actual_header_offset = 0
914 local_zip64_fields = []
915 central_zip64_fields = []
916
917 file_size = actual_size
918 if file_size_64_set:
919 file_size = 0xffffffff
920 if file_size_extra:
921 local_zip64_fields.append(actual_size)
922 central_zip64_fields.append(actual_size)
923 file_size = struct.pack("<L", file_size)
924
925 compress_size = actual_size
926 if compress_size_64_set:
927 compress_size = 0xffffffff
928 if compress_size_extra:
929 local_zip64_fields.append(actual_size)
930 central_zip64_fields.append(actual_size)
931 compress_size = struct.pack("<L", compress_size)
932
933 header_offset = actual_header_offset
934 if header_offset_64_set:
935 header_offset = 0xffffffff
936 if header_offset_extra:
937 central_zip64_fields.append(actual_header_offset)
938 header_offset = struct.pack("<L", header_offset)
939
940 local_extra = struct.pack(
941 '<HH' + 'Q'*len(local_zip64_fields),
942 0x0001,
943 8*len(local_zip64_fields),
944 *local_zip64_fields
945 )
946
947 central_extra = struct.pack(
948 '<HH' + 'Q'*len(central_zip64_fields),
949 0x0001,
950 8*len(central_zip64_fields),
951 *central_zip64_fields
952 )
953
954 central_dir_size = struct.pack('<Q', 58 + 8 * len(central_zip64_fields))
955 offset_to_central_dir = struct.pack('<Q', 50 + 8 * len(local_zip64_fields))
956 if end_of_central_dir_size is None:
957 end_of_central_dir_size = 44 + len(extensible_data)
958 if offset_to_end_of_central_dir is None:
959 offset_to_end_of_central_dir = (108
960 + 8 * len(local_zip64_fields)
961 + 8 * len(central_zip64_fields))
962
963 local_extra_length = struct.pack("<H", 4 + 8 * len(local_zip64_fields))
964 central_extra_length = struct.pack("<H", 4 + 8 * len(central_zip64_fields))
965
966 filename = b"test.txt"
967 content = b"test1234"
968 filename_length = struct.pack("<H", len(filename))
969 zip64_contents = (
970 # Local file header
971 b"PK\x03\x04\x14\x00\x00\x00\x00\x00\x00\x00!\x00\x9e%\xf5\xaf"
972 + compress_size
973 + file_size
974 + filename_length
975 + local_extra_length
976 + filename
977 + local_extra
978 + content
979 # Central directory:
980 + b"PK\x01\x02-\x03-\x00\x00\x00\x00\x00\x00\x00!\x00\x9e%\xf5\xaf"
981 + compress_size
982 + file_size
983 + filename_length
984 + central_extra_length
985 + b"\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01"
986 + header_offset
987 + filename
988 + central_extra
989 # Zip64 end of central directory
990 + b"PK\x06\x06"
991 + struct.pack('<Q', end_of_central_dir_size)
992 + b"-\x00-\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00"
993 + b"\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00"
994 + central_dir_size
995 + offset_to_central_dir
996 + extensible_data
997 # Zip64 end of central directory locator
998 + b"PK\x06\x07\x00\x00\x00\x00"
999 + struct.pack('<Q', offset_to_end_of_central_dir)
1000 + b"\x01\x00\x00\x00"
1001 # end of central directory
1002 + b"PK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00:\x00\x00\x002\x00"
1003 + b"\x00\x00\x00\x00"
1004 )
1005 return zip64_contents
1006
1007 def test_bad_zip64_extra(self):
1008 """Missing zip64 extra records raises an exception.
1009
1010 There are 4 fields that the zip64 format handles (the disk number is
1011 not used in this module and so is ignored here). According to the zip
1012 spec:
1013 The order of the fields in the zip64 extended
1014 information record is fixed, but the fields MUST
1015 only appear if the corresponding Local or Central
1016 directory record field is set to 0xFFFF or 0xFFFFFFFF.
1017
1018 If the zip64 extra content doesn't contain enough entries for the
1019 number of fields marked with 0xFFFF or 0xFFFFFFFF, we raise an error.
1020 This test mismatches the length of the zip64 extra field and the number
1021 of fields set to indicate the presence of zip64 data.
1022 """
1023 # zip64 file size present, no fields in extra, expecting one, equals
1024 # missing file size.
1025 missing_file_size_extra = self.make_zip64_file(
1026 file_size_64_set=True,
1027 )
1028 with self.assertRaises(zipfile.BadZipFile) as e:
1029 zipfile.ZipFile(io.BytesIO(missing_file_size_extra))
1030 self.assertIn('file size', str(e.exception).lower())
1031 self.assertTrue(zipfile.is_zipfile(io.BytesIO(missing_file_size_extra)))
1032
1033 # zip64 file size present, zip64 compress size present, one field in
1034 # extra, expecting two, equals missing compress size.
1035 missing_compress_size_extra = self.make_zip64_file(
1036 file_size_64_set=True,
1037 file_size_extra=True,
1038 compress_size_64_set=True,
1039 )
1040 with self.assertRaises(zipfile.BadZipFile) as e:
1041 zipfile.ZipFile(io.BytesIO(missing_compress_size_extra))
1042 self.assertIn('compress size', str(e.exception).lower())
1043 self.assertTrue(zipfile.is_zipfile(io.BytesIO(missing_compress_size_extra)))
1044
1045 # zip64 compress size present, no fields in extra, expecting one,
1046 # equals missing compress size.
1047 missing_compress_size_extra = self.make_zip64_file(
1048 compress_size_64_set=True,
1049 )
1050 with self.assertRaises(zipfile.BadZipFile) as e:
1051 zipfile.ZipFile(io.BytesIO(missing_compress_size_extra))
1052 self.assertIn('compress size', str(e.exception).lower())
1053 self.assertTrue(zipfile.is_zipfile(io.BytesIO(missing_compress_size_extra)))
1054
1055 # zip64 file size present, zip64 compress size present, zip64 header
1056 # offset present, two fields in extra, expecting three, equals missing
1057 # header offset
1058 missing_header_offset_extra = self.make_zip64_file(
1059 file_size_64_set=True,
1060 file_size_extra=True,
1061 compress_size_64_set=True,
1062 compress_size_extra=True,
1063 header_offset_64_set=True,
1064 )
1065 with self.assertRaises(zipfile.BadZipFile) as e:
1066 zipfile.ZipFile(io.BytesIO(missing_header_offset_extra))
1067 self.assertIn('header offset', str(e.exception).lower())
1068 self.assertTrue(zipfile.is_zipfile(io.BytesIO(missing_header_offset_extra)))
1069
1070 # zip64 compress size present, zip64 header offset present, one field
1071 # in extra, expecting two, equals missing header offset
1072 missing_header_offset_extra = self.make_zip64_file(
1073 file_size_64_set=False,
1074 compress_size_64_set=True,
1075 compress_size_extra=True,
1076 header_offset_64_set=True,
1077 )
1078 with self.assertRaises(zipfile.BadZipFile) as e:
1079 zipfile.ZipFile(io.BytesIO(missing_header_offset_extra))
1080 self.assertIn('header offset', str(e.exception).lower())
1081 self.assertTrue(zipfile.is_zipfile(io.BytesIO(missing_header_offset_extra)))
1082
1083 # zip64 file size present, zip64 header offset present, one field in
1084 # extra, expecting two, equals missing header offset
1085 missing_header_offset_extra = self.make_zip64_file(
1086 file_size_64_set=True,
1087 file_size_extra=True,
1088 compress_size_64_set=False,
1089 header_offset_64_set=True,
1090 )
1091 with self.assertRaises(zipfile.BadZipFile) as e:
1092 zipfile.ZipFile(io.BytesIO(missing_header_offset_extra))
1093 self.assertIn('header offset', str(e.exception).lower())
1094 self.assertTrue(zipfile.is_zipfile(io.BytesIO(missing_header_offset_extra)))
1095
1096 # zip64 header offset present, no fields in extra, expecting one,
1097 # equals missing header offset
1098 missing_header_offset_extra = self.make_zip64_file(
1099 file_size_64_set=False,
1100 compress_size_64_set=False,
1101 header_offset_64_set=True,
1102 )
1103 with self.assertRaises(zipfile.BadZipFile) as e:
1104 zipfile.ZipFile(io.BytesIO(missing_header_offset_extra))
1105 self.assertIn('header offset', str(e.exception).lower())
1106 self.assertTrue(zipfile.is_zipfile(io.BytesIO(missing_header_offset_extra)))
1107
1108 def test_bad_zip64_end_of_central_dir(self):
1109 zipdata = self.make_zip64_file(end_of_central_dir_size=0)
1110 with self.assertRaisesRegex(zipfile.BadZipFile, 'Corrupt.*record'):
1111 zipfile.ZipFile(io.BytesIO(zipdata))
1112 self.assertFalse(zipfile.is_zipfile(io.BytesIO(zipdata)))
1113
1114 zipdata = self.make_zip64_file(end_of_central_dir_size=100)
1115 with self.assertRaisesRegex(zipfile.BadZipFile, 'Corrupt.*record'):
1116 zipfile.ZipFile(io.BytesIO(zipdata))
1117 self.assertFalse(zipfile.is_zipfile(io.BytesIO(zipdata)))
1118
1119 zipdata = self.make_zip64_file(offset_to_end_of_central_dir=0)
1120 with self.assertRaisesRegex(zipfile.BadZipFile, 'Corrupt.*record'):
1121 zipfile.ZipFile(io.BytesIO(zipdata))
1122 self.assertFalse(zipfile.is_zipfile(io.BytesIO(zipdata)))
1123
1124 zipdata = self.make_zip64_file(offset_to_end_of_central_dir=1000)
1125 with self.assertRaisesRegex(zipfile.BadZipFile, 'Corrupt.*locator'):
1126 zipfile.ZipFile(io.BytesIO(zipdata))
1127 self.assertFalse(zipfile.is_zipfile(io.BytesIO(zipdata)))
1128
1129 def test_zip64_end_of_central_dir_record_not_found(self):
1130 zipdata = self.make_zip64_file()
1131 zipdata = zipdata.replace(b"PK\x06\x06", b'\x00'*4)
1132 with self.assertRaisesRegex(zipfile.BadZipFile, 'record not found'):
1133 zipfile.ZipFile(io.BytesIO(zipdata))
1134 self.assertFalse(zipfile.is_zipfile(io.BytesIO(zipdata)))
1135
1136 zipdata = self.make_zip64_file(
1137 extensible_data=b'\xca\xfe\x04\x00\x00\x00data')
1138 zipdata = zipdata.replace(b"PK\x06\x06", b'\x00'*4)
1139 with self.assertRaisesRegex(zipfile.BadZipFile, 'record not found'):
1140 zipfile.ZipFile(io.BytesIO(zipdata))
1141 self.assertFalse(zipfile.is_zipfile(io.BytesIO(zipdata)))
1142
1143 def test_zip64_extensible_data(self):
1144 # These values are what is set in the make_zip64_file method.
1145 expected_file_size = 8
1146 expected_compress_size = 8
1147 expected_header_offset = 0
1148 expected_content = b"test1234"
1149
1150 zipdata = self.make_zip64_file(
1151 extensible_data=b'\xca\xfe\x04\x00\x00\x00data')
1152 with zipfile.ZipFile(io.BytesIO(zipdata)) as zf:
1153 zinfo = zf.infolist()[0]
1154 self.assertEqual(zinfo.file_size, expected_file_size)
1155 self.assertEqual(zinfo.compress_size, expected_compress_size)
1156 self.assertEqual(zinfo.header_offset, expected_header_offset)
1157 self.assertEqual(zf.read(zinfo), expected_content)
1158 self.assertTrue(zipfile.is_zipfile(io.BytesIO(zipdata)))
1159
1160 with self.assertRaisesRegex(zipfile.BadZipFile, 'record not found'):
1161 zipfile.ZipFile(io.BytesIO(b'prepended' + zipdata))
1162 self.assertFalse(zipfile.is_zipfile(io.BytesIO(b'prepended' + zipdata)))
1163
1164 def test_generated_valid_zip64_extra(self):
1165 # These values are what is set in the make_zip64_file method.
1166 expected_file_size = 8
1167 expected_compress_size = 8
1168 expected_header_offset = 0
1169 expected_content = b"test1234"
1170
1171 # Loop through the various valid combinations of zip64 masks
1172 # present and extra fields present.
1173 params = (
1174 {"file_size_64_set": True, "file_size_extra": True},
1175 {"compress_size_64_set": True, "compress_size_extra": True},
1176 {"header_offset_64_set": True, "header_offset_extra": True},
1177 )
1178
1179 for r in range(1, len(params) + 1):
1180 for combo in itertools.combinations(params, r):
1181 kwargs = {}
1182 for c in combo:
1183 kwargs.update(c)
1184 with zipfile.ZipFile(io.BytesIO(self.make_zip64_file(**kwargs))) as zf:
1185 zinfo = zf.infolist()[0]
1186 self.assertEqual(zinfo.file_size, expected_file_size)
1187 self.assertEqual(zinfo.compress_size, expected_compress_size)
1188 self.assertEqual(zinfo.header_offset, expected_header_offset)
1189 self.assertEqual(zf.read(zinfo), expected_content)
1190
1191 def test_force_zip64(self):
1192 """Test that forcing zip64 extensions correctly notes this in the zip file"""
1193
1194 # GH-103861 describes an issue where forcing a small file to use zip64
1195 # extensions would add a zip64 extra record, but not change the data
1196 # sizes to 0xFFFFFFFF to indicate to the extractor that the zip64
1197 # record should be read. Additionally, it would not set the required
1198 # version to indicate that zip64 extensions are required to extract it.
1199 # This test replicates the situation and reads the raw data to specifically ensure:
1200 # - The required extract version is always >= ZIP64_VERSION
1201 # - The compressed and uncompressed size in the file headers are both
1202 # 0xFFFFFFFF (ie. point to zip64 record)
1203 # - The zip64 record is provided and has the correct sizes in it
1204 # Other aspects of the zip are checked as well, but verifying the above is the main goal.
1205 # Because this is hard to verify by parsing the data as a zip, the raw
1206 # bytes are checked to ensure that they line up with the zip spec.
1207 # The spec for this can be found at: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
1208 # The relevant sections for this test are:
1209 # - 4.3.7 for local file header
1210 # - 4.5.3 for zip64 extra field
1211
1212 data = io.BytesIO()
1213 with zipfile.ZipFile(data, mode="w", allowZip64=True) as zf:
1214 with zf.open("text.txt", mode="w", force_zip64=True) as zi:
1215 zi.write(b"_")
1216
1217 zipdata = data.getvalue()
1218
1219 # pull out and check zip information
1220 (
1221 header, vers, os, flags, comp, csize, usize, fn_len,
1222 ex_total_len, filename, ex_id, ex_len, ex_usize, ex_csize, cd_sig
1223 ) = struct.unpack("<4sBBHH8xIIHH8shhQQx4s", zipdata[:63])
1224
1225 self.assertEqual(header, b"PK\x03\x04") # local file header
1226 self.assertGreaterEqual(vers, zipfile.ZIP64_VERSION) # requires zip64 to extract
1227 self.assertEqual(os, 0) # compatible with MS-DOS
1228 self.assertEqual(flags, 0) # no flags
1229 self.assertEqual(comp, 0) # compression method = stored
1230 self.assertEqual(csize, 0xFFFFFFFF) # sizes are in zip64 extra
1231 self.assertEqual(usize, 0xFFFFFFFF)
1232 self.assertEqual(fn_len, 8) # filename len
1233 self.assertEqual(ex_total_len, 20) # size of extra records
1234 self.assertEqual(ex_id, 1) # Zip64 extra record
1235 self.assertEqual(ex_len, 16) # 16 bytes of data
1236 self.assertEqual(ex_usize, 1) # uncompressed size
1237 self.assertEqual(ex_csize, 1) # compressed size
1238 self.assertEqual(cd_sig, b"PK\x01\x02") # ensure the central directory header is next
1239
1240 z = zipfile.ZipFile(io.BytesIO(zipdata))
1241 zinfos = z.infolist()
1242 self.assertEqual(len(zinfos), 1)
1243 self.assertGreaterEqual(zinfos[0].extract_version, zipfile.ZIP64_VERSION) # requires zip64 to extract
1244
1245 def test_unseekable_zip_unknown_filesize(self):
1246 """Test that creating a zip with/without seeking will raise a RuntimeError if zip64 was required but not used"""
1247
1248 def make_zip(fp):
1249 with zipfile.ZipFile(fp, mode="w", allowZip64=True) as zf:
1250 with zf.open("text.txt", mode="w", force_zip64=False) as zi:
1251 zi.write(b"_" * (zipfile.ZIP64_LIMIT + 1))
1252
1253 self.assertRaises(RuntimeError, make_zip, io.BytesIO())
1254 self.assertRaises(RuntimeError, make_zip, Unseekable(io.BytesIO()))
1255
1256 def test_zip64_required_not_allowed_fail(self):
1257 """Test that trying to add a large file to a zip that doesn't allow zip64 extensions fails on add"""
1258 def make_zip(fp):
1259 with zipfile.ZipFile(fp, mode="w", allowZip64=False) as zf:
1260 # pretend zipfile.ZipInfo.from_file was used to get the name and filesize
1261 info = zipfile.ZipInfo("text.txt")
1262 info.file_size = zipfile.ZIP64_LIMIT + 1
1263 zf.open(info, mode="w")
1264
1265 self.assertRaises(zipfile.LargeZipFile, make_zip, io.BytesIO())
1266 self.assertRaises(zipfile.LargeZipFile, make_zip, Unseekable(io.BytesIO()))
1267
1268 def test_unseekable_zip_known_filesize(self):
1269 """Test that creating a zip without seeking will use zip64 extensions if the file size is provided up-front"""
1270
1271 # This test ensures that the zip will use a zip64 data descriptor (same
1272 # as a regular data descriptor except the sizes are 8 bytes instead of
1273 # 4) record to communicate the size of a file if the zip is being
1274 # written to an unseekable stream.
1275 # Because this sort of thing is hard to verify by parsing the data back
1276 # in as a zip, this test looks at the raw bytes created to ensure that
1277 # the correct data has been generated.
1278 # The spec for this can be found at: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
1279 # The relevant sections for this test are:
1280 # - 4.3.7 for local file header
1281 # - 4.3.9 for the data descriptor
1282 # - 4.5.3 for zip64 extra field
1283
1284 file_size = zipfile.ZIP64_LIMIT + 1
1285
1286 def make_zip(fp):
1287 with zipfile.ZipFile(fp, mode="w", allowZip64=True) as zf:
1288 # pretend zipfile.ZipInfo.from_file was used to get the name and filesize
1289 info = zipfile.ZipInfo("text.txt")
1290 info.file_size = file_size
1291 with zf.open(info, mode="w", force_zip64=False) as zi:
1292 zi.write(b"_" * file_size)
1293 return fp
1294
1295 # check seekable file information
1296 seekable_data = make_zip(io.BytesIO()).getvalue()
1297 (
1298 header, vers, os, flags, comp, csize, usize, fn_len,
1299 ex_total_len, filename, ex_id, ex_len, ex_usize, ex_csize,
1300 cd_sig
1301 ) = struct.unpack("<4sBBHH8xIIHH8shhQQ{}x4s".format(file_size), seekable_data[:62 + file_size])
1302
1303 self.assertEqual(header, b"PK\x03\x04") # local file header
1304 self.assertGreaterEqual(vers, zipfile.ZIP64_VERSION) # requires zip64 to extract
1305 self.assertEqual(os, 0) # compatible with MS-DOS
1306 self.assertEqual(flags, 0) # no flags set
1307 self.assertEqual(comp, 0) # compression method = stored
1308 self.assertEqual(csize, 0xFFFFFFFF) # sizes are in zip64 extra
1309 self.assertEqual(usize, 0xFFFFFFFF)
1310 self.assertEqual(fn_len, 8) # filename len
1311 self.assertEqual(ex_total_len, 20) # size of extra records
1312 self.assertEqual(ex_id, 1) # Zip64 extra record
1313 self.assertEqual(ex_len, 16) # 16 bytes of data
1314 self.assertEqual(ex_usize, file_size) # uncompressed size
1315 self.assertEqual(ex_csize, file_size) # compressed size
1316 self.assertEqual(cd_sig, b"PK\x01\x02") # ensure the central directory header is next
1317
1318 # check unseekable file information
1319 unseekable_data = make_zip(Unseekable(io.BytesIO())).fp.getvalue()
1320 (
1321 header, vers, os, flags, comp, csize, usize, fn_len,
1322 ex_total_len, filename, ex_id, ex_len, ex_usize, ex_csize,
1323 dd_header, dd_usize, dd_csize, cd_sig
1324 ) = struct.unpack("<4sBBHH8xIIHH8shhQQ{}x4s4xQQ4s".format(file_size), unseekable_data[:86 + file_size])
1325
1326 self.assertEqual(header, b"PK\x03\x04") # local file header
1327 self.assertGreaterEqual(vers, zipfile.ZIP64_VERSION) # requires zip64 to extract
1328 self.assertEqual(os, 0) # compatible with MS-DOS
1329 self.assertEqual("{:b}".format(flags), "1000") # streaming flag set
1330 self.assertEqual(comp, 0) # compression method = stored
1331 self.assertEqual(csize, 0xFFFFFFFF) # sizes are in zip64 extra
1332 self.assertEqual(usize, 0xFFFFFFFF)
1333 self.assertEqual(fn_len, 8) # filename len
1334 self.assertEqual(ex_total_len, 20) # size of extra records
1335 self.assertEqual(ex_id, 1) # Zip64 extra record
1336 self.assertEqual(ex_len, 16) # 16 bytes of data
1337 self.assertEqual(ex_usize, 0) # uncompressed size - 0 to defer to data descriptor
1338 self.assertEqual(ex_csize, 0) # compressed size - 0 to defer to data descriptor
1339 self.assertEqual(dd_header, b"PK\07\x08") # data descriptor
1340 self.assertEqual(dd_usize, file_size) # file size (8 bytes because zip64)
1341 self.assertEqual(dd_csize, file_size) # compressed size (8 bytes because zip64)
1342 self.assertEqual(cd_sig, b"PK\x01\x02") # ensure the central directory header is next
1343
1344
1345 @requires_zlib()
1346 class DeflateTestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
1347 unittest.TestCase):
1348 compression = zipfile.ZIP_DEFLATED
1349
1350 @requires_bz2()
1351 class Bzip2TestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
1352 unittest.TestCase):
1353 compression = zipfile.ZIP_BZIP2
1354
1355 @requires_lzma()
1356 class LzmaTestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
1357 unittest.TestCase):
1358 compression = zipfile.ZIP_LZMA
1359
1360 @requires_zstd()
1361 class ZstdTestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
1362 unittest.TestCase):
1363 compression = zipfile.ZIP_ZSTANDARD
1364
1365 class AbstractWriterTests:
1366
1367 def tearDown(self):
1368 unlink(TESTFN2)
1369
1370 def test_close_after_close(self):
1371 data = b'content'
1372 with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipf:
1373 w = zipf.open('test', 'w')
1374 w.write(data)
1375 w.close()
1376 self.assertTrue(w.closed)
1377 w.close()
1378 self.assertTrue(w.closed)
1379 self.assertEqual(zipf.read('test'), data)
1380
1381 def test_write_after_close(self):
1382 data = b'content'
1383 with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipf:
1384 w = zipf.open('test', 'w')
1385 w.write(data)
1386 w.close()
1387 self.assertTrue(w.closed)
1388 self.assertRaises(ValueError, w.write, b'')
1389 self.assertEqual(zipf.read('test'), data)
1390
1391 def test_issue44439(self):
1392 q = array.array('Q', [1, 2, 3, 4, 5])
1393 LENGTH = len(q) * q.itemsize
1394 with zipfile.ZipFile(io.BytesIO(), 'w', self.compression) as zip:
1395 with zip.open('data', 'w') as data:
1396 self.assertEqual(data.write(q), LENGTH)
1397 self.assertEqual(zip.getinfo('data').file_size, LENGTH)
1398
1399 def test_zipwritefile_attrs(self):
1400 fname = "somefile.txt"
1401 with zipfile.ZipFile(TESTFN2, mode="w", compression=self.compression) as zipfp:
1402 with zipfp.open(fname, 'w') as fid:
1403 self.assertEqual(fid.name, fname)
1404 self.assertRaises(io.UnsupportedOperation, fid.fileno)
1405 self.assertEqual(fid.mode, 'wb')
1406 self.assertIs(fid.readable(), False)
1407 self.assertIs(fid.writable(), True)
1408 self.assertIs(fid.seekable(), False)
1409 self.assertIs(fid.closed, False)
1410 self.assertIs(fid.closed, True)
1411 self.assertEqual(fid.name, fname)
1412 self.assertEqual(fid.mode, 'wb')
1413 self.assertRaises(io.UnsupportedOperation, fid.fileno)
1414 self.assertIs(fid.readable(), False)
1415 self.assertIs(fid.writable(), True)
1416 self.assertIs(fid.seekable(), False)
1417
1418 class StoredWriterTests(AbstractWriterTests, unittest.TestCase):
1419 compression = zipfile.ZIP_STORED
1420
1421 @requires_zlib()
1422 class DeflateWriterTests(AbstractWriterTests, unittest.TestCase):
1423 compression = zipfile.ZIP_DEFLATED
1424
1425 @requires_bz2()
1426 class Bzip2WriterTests(AbstractWriterTests, unittest.TestCase):
1427 compression = zipfile.ZIP_BZIP2
1428
1429 @requires_lzma()
1430 class LzmaWriterTests(AbstractWriterTests, unittest.TestCase):
1431 compression = zipfile.ZIP_LZMA
1432
1433 @requires_zstd()
1434 class ZstdWriterTests(AbstractWriterTests, unittest.TestCase):
1435 compression = zipfile.ZIP_ZSTANDARD
1436
1437 class PyZipFileTests(unittest.TestCase):
1438 def assertCompiledIn(self, name, namelist):
1439 if name + 'o' not in namelist:
1440 self.assertIn(name + 'c', namelist)
1441
1442 def requiresWriteAccess(self, path):
1443 # effective_ids unavailable on windows
1444 if not os.access(path, os.W_OK,
1445 effective_ids=os.access in os.supports_effective_ids):
1446 self.skipTest('requires write access to the installed location')
1447 filename = os.path.join(path, 'test_zipfile.try')
1448 try:
1449 fd = os.open(filename, os.O_WRONLY | os.O_CREAT)
1450 os.close(fd)
1451 except Exception:
1452 self.skipTest('requires write access to the installed location')
1453 unlink(filename)
1454
1455 def test_write_pyfile(self):
1456 self.requiresWriteAccess(os.path.dirname(__file__))
1457 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
1458 fn = __file__
1459 if fn.endswith('.pyc'):
1460 path_split = fn.split(os.sep)
1461 if os.altsep is not None:
1462 path_split.extend(fn.split(os.altsep))
1463 if '__pycache__' in path_split:
1464 fn = importlib.util.source_from_cache(fn)
1465 else:
1466 fn = fn[:-1]
1467
1468 zipfp.writepy(fn)
1469
1470 bn = os.path.basename(fn)
1471 self.assertNotIn(bn, zipfp.namelist())
1472 self.assertCompiledIn(bn, zipfp.namelist())
1473
1474 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
1475 fn = __file__
1476 if fn.endswith('.pyc'):
1477 fn = fn[:-1]
1478
1479 zipfp.writepy(fn, "testpackage")
1480
1481 bn = "%s/%s" % ("testpackage", os.path.basename(fn))
1482 self.assertNotIn(bn, zipfp.namelist())
1483 self.assertCompiledIn(bn, zipfp.namelist())
1484
1485 def test_write_python_package(self):
1486 import email
1487 packagedir = os.path.dirname(email.__file__)
1488 self.requiresWriteAccess(packagedir)
1489
1490 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
1491 zipfp.writepy(packagedir)
1492
1493 # Check for a couple of modules at different levels of the
1494 # hierarchy
1495 names = zipfp.namelist()
1496 self.assertCompiledIn('email/__init__.py', names)
1497 self.assertCompiledIn('email/mime/text.py', names)
1498
1499 def test_write_filtered_python_package(self):
1500 import test
1501 packagedir = os.path.dirname(test.__file__)
1502 self.requiresWriteAccess(packagedir)
1503
1504 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
1505
1506 # first make sure that the test folder gives error messages
1507 # (on the badsyntax_... files)
1508 with captured_stdout() as reportSIO:
1509 zipfp.writepy(packagedir)
1510 reportStr = reportSIO.getvalue()
1511 self.assertTrue('SyntaxError' in reportStr)
1512
1513 # then check that the filter works on the whole package
1514 with captured_stdout() as reportSIO:
1515 zipfp.writepy(packagedir, filterfunc=lambda whatever: False)
1516 reportStr = reportSIO.getvalue()
1517 self.assertTrue('SyntaxError' not in reportStr)
1518
1519 # then check that the filter works on individual files
1520 def filter(path):
1521 return not os.path.basename(path).startswith("bad")
1522 with captured_stdout() as reportSIO, self.assertWarns(UserWarning):
1523 zipfp.writepy(packagedir, filterfunc=filter)
1524 reportStr = reportSIO.getvalue()
1525 if reportStr:
1526 print(reportStr)
1527 self.assertTrue('SyntaxError' not in reportStr)
1528
1529 def test_write_with_optimization(self):
1530 import email
1531 packagedir = os.path.dirname(email.__file__)
1532 self.requiresWriteAccess(packagedir)
1533 optlevel = 1 if __debug__ else 0
1534 ext = '.pyc'
1535
1536 with TemporaryFile() as t, \
1537 zipfile.PyZipFile(t, "w", optimize=optlevel) as zipfp:
1538 zipfp.writepy(packagedir)
1539
1540 names = zipfp.namelist()
1541 self.assertIn('email/__init__' + ext, names)
1542 self.assertIn('email/mime/text' + ext, names)
1543
1544 def test_write_python_directory(self):
1545 os.mkdir(TESTFN2)
1546 try:
1547 with open(os.path.join(TESTFN2, "mod1.py"), "w", encoding='utf-8') as fp:
1548 fp.write("print(42)\n")
1549
1550 with open(os.path.join(TESTFN2, "mod2.py"), "w", encoding='utf-8') as fp:
1551 fp.write("print(42 * 42)\n")
1552
1553 with open(os.path.join(TESTFN2, "mod2.txt"), "w", encoding='utf-8') as fp:
1554 fp.write("bla bla bla\n")
1555
1556 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
1557 zipfp.writepy(TESTFN2)
1558
1559 names = zipfp.namelist()
1560 self.assertCompiledIn('mod1.py', names)
1561 self.assertCompiledIn('mod2.py', names)
1562 self.assertNotIn('mod2.txt', names)
1563
1564 finally:
1565 rmtree(TESTFN2)
1566
1567 def test_write_python_directory_filtered(self):
1568 os.mkdir(TESTFN2)
1569 try:
1570 with open(os.path.join(TESTFN2, "mod1.py"), "w", encoding='utf-8') as fp:
1571 fp.write("print(42)\n")
1572
1573 with open(os.path.join(TESTFN2, "mod2.py"), "w", encoding='utf-8') as fp:
1574 fp.write("print(42 * 42)\n")
1575
1576 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
1577 zipfp.writepy(TESTFN2, filterfunc=lambda fn:
1578 not fn.endswith('mod2.py'))
1579
1580 names = zipfp.namelist()
1581 self.assertCompiledIn('mod1.py', names)
1582 self.assertNotIn('mod2.py', names)
1583
1584 finally:
1585 rmtree(TESTFN2)
1586
1587 def test_write_non_pyfile(self):
1588 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
1589 with open(TESTFN, 'w', encoding='utf-8') as f:
1590 f.write('most definitely not a python file')
1591 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
1592 unlink(TESTFN)
1593
1594 def test_write_pyfile_bad_syntax(self):
1595 os.mkdir(TESTFN2)
1596 try:
1597 with open(os.path.join(TESTFN2, "mod1.py"), "w", encoding='utf-8') as fp:
1598 fp.write("Bad syntax in python file\n")
1599
1600 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
1601 # syntax errors are printed to stdout
1602 with captured_stdout() as s:
1603 zipfp.writepy(os.path.join(TESTFN2, "mod1.py"))
1604
1605 self.assertIn("SyntaxError", s.getvalue())
1606
1607 # as it will not have compiled the python file, it will
1608 # include the .py file not .pyc
1609 names = zipfp.namelist()
1610 self.assertIn('mod1.py', names)
1611 self.assertNotIn('mod1.pyc', names)
1612
1613 finally:
1614 rmtree(TESTFN2)
1615
1616 def test_write_pathlike(self):
1617 os.mkdir(TESTFN2)
1618 try:
1619 with open(os.path.join(TESTFN2, "mod1.py"), "w", encoding='utf-8') as fp:
1620 fp.write("print(42)\n")
1621
1622 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
1623 zipfp.writepy(FakePath(os.path.join(TESTFN2, "mod1.py")))
1624 names = zipfp.namelist()
1625 self.assertCompiledIn('mod1.py', names)
1626 finally:
1627 rmtree(TESTFN2)
1628
1629
1630 class ExtractTests(unittest.TestCase):
1631
1632 def make_test_file(self):
1633 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
1634 for fpath, fdata in SMALL_TEST_DATA:
1635 zipfp.writestr(fpath, fdata)
1636
1637 def test_extract(self):
1638 with temp_cwd():
1639 self.make_test_file()
1640 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
1641 for fpath, fdata in SMALL_TEST_DATA:
1642 writtenfile = zipfp.extract(fpath)
1643
1644 # make sure it was written to the right place
1645 correctfile = os.path.join(os.getcwd(), fpath)
1646 correctfile = os.path.normpath(correctfile)
1647
1648 self.assertEqual(writtenfile, correctfile)
1649
1650 # make sure correct data is in correct file
1651 with open(writtenfile, "rb") as f:
1652 self.assertEqual(fdata.encode(), f.read())
1653
1654 unlink(writtenfile)
1655
1656 def _test_extract_with_target(self, target):
1657 self.make_test_file()
1658 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
1659 for fpath, fdata in SMALL_TEST_DATA:
1660 writtenfile = zipfp.extract(fpath, target)
1661
1662 # make sure it was written to the right place
1663 correctfile = os.path.join(target, fpath)
1664 correctfile = os.path.normpath(correctfile)
1665 self.assertTrue(os.path.samefile(writtenfile, correctfile), (writtenfile, target))
1666
1667 # make sure correct data is in correct file
1668 with open(writtenfile, "rb") as f:
1669 self.assertEqual(fdata.encode(), f.read())
1670
1671 unlink(writtenfile)
1672
1673 unlink(TESTFN2)
1674
1675 def test_extract_with_target(self):
1676 with temp_dir() as extdir:
1677 self._test_extract_with_target(extdir)
1678
1679 def test_extract_with_target_pathlike(self):
1680 with temp_dir() as extdir:
1681 self._test_extract_with_target(FakePath(extdir))
1682
1683 def test_extract_all(self):
1684 with temp_cwd():
1685 self.make_test_file()
1686 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
1687 zipfp.extractall()
1688 for fpath, fdata in SMALL_TEST_DATA:
1689 outfile = os.path.join(os.getcwd(), fpath)
1690
1691 with open(outfile, "rb") as f:
1692 self.assertEqual(fdata.encode(), f.read())
1693
1694 unlink(outfile)
1695
1696 def _test_extract_all_with_target(self, target):
1697 self.make_test_file()
1698 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
1699 zipfp.extractall(target)
1700 for fpath, fdata in SMALL_TEST_DATA:
1701 outfile = os.path.join(target, fpath)
1702
1703 with open(outfile, "rb") as f:
1704 self.assertEqual(fdata.encode(), f.read())
1705
1706 unlink(outfile)
1707
1708 unlink(TESTFN2)
1709
1710 def test_extract_all_with_target(self):
1711 with temp_dir() as extdir:
1712 self._test_extract_all_with_target(extdir)
1713
1714 def test_extract_all_with_target_pathlike(self):
1715 with temp_dir() as extdir:
1716 self._test_extract_all_with_target(FakePath(extdir))
1717
1718 def check_file(self, filename, content):
1719 self.assertTrue(os.path.isfile(filename))
1720 with open(filename, 'rb') as f:
1721 self.assertEqual(f.read(), content)
1722
1723 def test_sanitize_windows_name(self):
1724 san = zipfile.ZipFile._sanitize_windows_name
1725 # Passing pathsep in allows this test to work regardless of platform.
1726 self.assertEqual(san(r',,?,C:,foo,bar/z', ','), r'_,C_,foo,bar/z')
1727 self.assertEqual(san(r'a\b,c<d>e|f"g?h*i', ','), r'a\b,c_d_e_f_g_h_i')
1728 self.assertEqual(san('../../foo../../ba..r', '/'), r'foo/ba..r')
1729 self.assertEqual(san(' / /foo / /ba r', '/'), r'foo/ba r')
1730 self.assertEqual(san(' . /. /foo ./ . /. ./ba .r', '/'), r'foo/ba .r')
1731
1732 def test_extract_hackers_arcnames_common_cases(self):
1733 common_hacknames = [
1734 ('../foo/bar', 'foo/bar'),
1735 ('foo/../bar', 'foo/bar'),
1736 ('foo/../../bar', 'foo/bar'),
1737 ('foo/bar/..', 'foo/bar'),
1738 ('./../foo/bar', 'foo/bar'),
1739 ('/foo/bar', 'foo/bar'),
1740 ('/foo/../bar', 'foo/bar'),
1741 ('/foo/../../bar', 'foo/bar'),
1742 ]
1743 self._test_extract_hackers_arcnames(common_hacknames)
1744
1745 @unittest.skipIf(os.path.sep != '\\', 'Requires \\ as path separator.')
1746 def test_extract_hackers_arcnames_windows_only(self):
1747 """Test combination of path fixing and windows name sanitization."""
1748 windows_hacknames = [
1749 (r'..\foo\bar', 'foo/bar'),
1750 (r'..\/foo\/bar', 'foo/bar'),
1751 (r'foo/\..\/bar', 'foo/bar'),
1752 (r'foo\/../\bar', 'foo/bar'),
1753 (r'C:foo/bar', 'foo/bar'),
1754 (r'C:/foo/bar', 'foo/bar'),
1755 (r'C://foo/bar', 'foo/bar'),
1756 (r'C:\foo\bar', 'foo/bar'),
1757 (r'//conky/mountpoint/foo/bar', 'foo/bar'),
1758 (r'\\conky\mountpoint\foo\bar', 'foo/bar'),
1759 (r'///conky/mountpoint/foo/bar', 'mountpoint/foo/bar'),
1760 (r'\\\conky\mountpoint\foo\bar', 'mountpoint/foo/bar'),
1761 (r'//conky//mountpoint/foo/bar', 'mountpoint/foo/bar'),
1762 (r'\\conky\\mountpoint\foo\bar', 'mountpoint/foo/bar'),
1763 (r'//?/C:/foo/bar', 'foo/bar'),
1764 (r'\\?\C:\foo\bar', 'foo/bar'),
1765 (r'C:/../C:/foo/bar', 'C_/foo/bar'),
1766 (r'a:b\c<d>e|f"g?h*i', 'b/c_d_e_f_g_h_i'),
1767 ('../../foo../../ba..r', 'foo/ba..r'),
1768 ]
1769 self._test_extract_hackers_arcnames(windows_hacknames)
1770
1771 @unittest.skipIf(os.path.sep != '/', r'Requires / as path separator.')
1772 def test_extract_hackers_arcnames_posix_only(self):
1773 posix_hacknames = [
1774 ('//foo/bar', 'foo/bar'),
1775 ('../../foo../../ba..r', 'foo../ba..r'),
1776 (r'foo/..\bar', r'foo/..\bar'),
1777 ]
1778 self._test_extract_hackers_arcnames(posix_hacknames)
1779
1780 def _test_extract_hackers_arcnames(self, hacknames):
1781 for arcname, fixedname in hacknames:
1782 content = b'foobar' + arcname.encode()
1783 with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipfp:
1784 zinfo = zipfile.ZipInfo()
1785 # preserve backslashes
1786 zinfo.filename = arcname
1787 zinfo.external_attr = 0o600 << 16
1788 zipfp.writestr(zinfo, content)
1789
1790 arcname = arcname.replace(os.sep, "/")
1791 targetpath = os.path.join('target', 'subdir', 'subsub')
1792 correctfile = os.path.join(targetpath, *fixedname.split('/'))
1793
1794 with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
1795 writtenfile = zipfp.extract(arcname, targetpath)
1796 self.assertEqual(writtenfile, correctfile,
1797 msg='extract %r: %r != %r' %
1798 (arcname, writtenfile, correctfile))
1799 self.check_file(correctfile, content)
1800 rmtree('target')
1801
1802 with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
1803 zipfp.extractall(targetpath)
1804 self.check_file(correctfile, content)
1805 rmtree('target')
1806
1807 correctfile = os.path.join(os.getcwd(), *fixedname.split('/'))
1808
1809 with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
1810 writtenfile = zipfp.extract(arcname)
1811 self.assertEqual(writtenfile, correctfile,
1812 msg="extract %r" % arcname)
1813 self.check_file(correctfile, content)
1814 rmtree(fixedname.split('/')[0])
1815
1816 with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
1817 zipfp.extractall()
1818 self.check_file(correctfile, content)
1819 rmtree(fixedname.split('/')[0])
1820
1821 unlink(TESTFN2)
1822
1823
1824 class OverwriteTests(archiver_tests.OverwriteTests, unittest.TestCase):
1825 testdir = TESTFN
1826
1827 @classmethod
1828 def setUpClass(cls):
1829 p = cls.ar_with_file = TESTFN + '-with-file.zip'
1830 cls.addClassCleanup(unlink, p)
1831 with zipfile.ZipFile(p, 'w') as zipfp:
1832 zipfp.writestr('test', b'newcontent')
1833
1834 p = cls.ar_with_dir = TESTFN + '-with-dir.zip'
1835 cls.addClassCleanup(unlink, p)
1836 with zipfile.ZipFile(p, 'w') as zipfp:
1837 zipfp.mkdir('test')
1838
1839 p = cls.ar_with_implicit_dir = TESTFN + '-with-implicit-dir.zip'
1840 cls.addClassCleanup(unlink, p)
1841 with zipfile.ZipFile(p, 'w') as zipfp:
1842 zipfp.writestr('test/file', b'newcontent')
1843
1844 def open(self, path):
1845 return zipfile.ZipFile(path, 'r')
1846
1847 def extractall(self, ar):
1848 ar.extractall(self.testdir)
1849
1850
1851 class OtherTests(unittest.TestCase):
1852 def test_open_via_zip_info(self):
1853 # Create the ZIP archive
1854 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
1855 zipfp.writestr("name", "foo")
1856 with self.assertWarns(UserWarning):
1857 zipfp.writestr("name", "bar")
1858 self.assertEqual(zipfp.namelist(), ["name"] * 2)
1859
1860 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
1861 infos = zipfp.infolist()
1862 data = b""
1863 for info in infos:
1864 with zipfp.open(info) as zipopen:
1865 data += zipopen.read()
1866 self.assertIn(data, {b"foobar", b"barfoo"})
1867 data = b""
1868 for info in infos:
1869 data += zipfp.read(info)
1870 self.assertIn(data, {b"foobar", b"barfoo"})
1871
1872 def test_writestr_extended_local_header_issue1202(self):
1873 with zipfile.ZipFile(TESTFN2, 'w') as orig_zip:
1874 for data in 'abcdefghijklmnop':
1875 zinfo = zipfile.ZipInfo(data)
1876 zinfo.flag_bits |= zipfile._MASK_USE_DATA_DESCRIPTOR # Include an extended local header.
1877 orig_zip.writestr(zinfo, data)
1878
1879 def test_write_with_source_date_epoch(self):
1880 with os_helper.EnvironmentVarGuard() as env:
1881 # Set the SOURCE_DATE_EPOCH environment variable to a specific timestamp
1882 env['SOURCE_DATE_EPOCH'] = "1735715999"
1883
1884 with zipfile.ZipFile(TESTFN, "w") as zf:
1885 zf.writestr("test_source_date_epoch.txt", "Testing SOURCE_DATE_EPOCH")
1886
1887 with zipfile.ZipFile(TESTFN, "r") as zf:
1888 zip_info = zf.getinfo("test_source_date_epoch.txt")
1889 get_time = time.localtime(int(os.environ['SOURCE_DATE_EPOCH']))[:6]
1890 # Compare each element of the date_time tuple
1891 # Allow for a 1-second difference
1892 for z_time, g_time in zip(zip_info.date_time, get_time):
1893 self.assertAlmostEqual(z_time, g_time, delta=1)
1894
1895 def test_write_without_source_date_epoch(self):
1896 with os_helper.EnvironmentVarGuard() as env:
1897 del env['SOURCE_DATE_EPOCH']
1898
1899 with zipfile.ZipFile(TESTFN, "w") as zf:
1900 zf.writestr("test_no_source_date_epoch.txt", "Testing without SOURCE_DATE_EPOCH")
1901
1902 with zipfile.ZipFile(TESTFN, "r") as zf:
1903 zip_info = zf.getinfo("test_no_source_date_epoch.txt")
1904 current_time = time.localtime()[:6]
1905 for z_time, c_time in zip(zip_info.date_time, current_time):
1906 self.assertAlmostEqual(z_time, c_time, delta=2)
1907
1908 def test_close(self):
1909 """Check that the zipfile is closed after the 'with' block."""
1910 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
1911 for fpath, fdata in SMALL_TEST_DATA:
1912 zipfp.writestr(fpath, fdata)
1913 self.assertIsNotNone(zipfp.fp, 'zipfp is not open')
1914 self.assertIsNone(zipfp.fp, 'zipfp is not closed')
1915
1916 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
1917 self.assertIsNotNone(zipfp.fp, 'zipfp is not open')
1918 self.assertIsNone(zipfp.fp, 'zipfp is not closed')
1919
1920 def test_close_on_exception(self):
1921 """Check that the zipfile is closed if an exception is raised in the
1922 'with' block."""
1923 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
1924 for fpath, fdata in SMALL_TEST_DATA:
1925 zipfp.writestr(fpath, fdata)
1926
1927 try:
1928 with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
1929 raise zipfile.BadZipFile()
1930 except zipfile.BadZipFile:
1931 self.assertIsNone(zipfp2.fp, 'zipfp is not closed')
1932
1933 def test_unsupported_version(self):
1934 # File has an extract_version of 120
1935 data = (b'PK\x03\x04x\x00\x00\x00\x00\x00!p\xa1@\x00\x00\x00\x00\x00\x00'
1936 b'\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00xPK\x01\x02x\x03x\x00\x00\x00\x00'
1937 b'\x00!p\xa1@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00'
1938 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00\x00xPK\x05\x06'
1939 b'\x00\x00\x00\x00\x01\x00\x01\x00/\x00\x00\x00\x1f\x00\x00\x00\x00\x00')
1940
1941 self.assertRaises(NotImplementedError, zipfile.ZipFile,
1942 io.BytesIO(data), 'r')
1943
1944 @requires_zlib()
1945 def test_read_unicode_filenames(self):
1946 # bug #10801
1947 fname = findfile('zip_cp437_header.zip', subdir='archivetestdata')
1948 with zipfile.ZipFile(fname) as zipfp:
1949 for name in zipfp.namelist():
1950 zipfp.open(name).close()
1951
1952 def test_write_unicode_filenames(self):
1953 with zipfile.ZipFile(TESTFN, "w") as zf:
1954 zf.writestr("foo.txt", "Test for unicode filename")
1955 zf.writestr("\xf6.txt", "Test for unicode filename")
1956 self.assertIsInstance(zf.infolist()[0].filename, str)
1957
1958 with zipfile.ZipFile(TESTFN, "r") as zf:
1959 self.assertEqual(zf.filelist[0].filename, "foo.txt")
1960 self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
1961
1962 def create_zipfile_with_extra_data(self, filename, extra_data_name):
1963 with zipfile.ZipFile(TESTFN, mode='w') as zf:
1964 filename_encoded = filename.encode("utf-8")
1965 # create a ZipInfo object with Unicode path extra field
1966 zip_info = zipfile.ZipInfo(filename)
1967
1968 tag_for_unicode_path = b'\x75\x70'
1969 version_of_unicode_path = b'\x01'
1970
1971 import zlib
1972 filename_crc = struct.pack('<L', zlib.crc32(filename_encoded))
1973
1974 extra_data = version_of_unicode_path + filename_crc + extra_data_name
1975 tsize = len(extra_data).to_bytes(2, 'little')
1976
1977 zip_info.extra = tag_for_unicode_path + tsize + extra_data
1978
1979 # add the file to the ZIP archive
1980 zf.writestr(zip_info, b'Hello World!')
1981
1982 @requires_zlib()
1983 def test_read_zipfile_containing_unicode_path_extra_field(self):
1984 self.create_zipfile_with_extra_data("이름.txt", "이름.txt".encode("utf-8"))
1985 with zipfile.ZipFile(TESTFN, "r") as zf:
1986 self.assertEqual(zf.filelist[0].filename, "이름.txt")
1987
1988 @requires_zlib()
1989 def test_read_zipfile_warning(self):
1990 self.create_zipfile_with_extra_data("이름.txt", b"")
1991 with self.assertWarns(UserWarning):
1992 zipfile.ZipFile(TESTFN, "r").close()
1993
1994 @requires_zlib()
1995 def test_read_zipfile_error(self):
1996 self.create_zipfile_with_extra_data("이름.txt", b"\xff")
1997 with self.assertRaises(zipfile.BadZipfile):
1998 zipfile.ZipFile(TESTFN, "r").close()
1999
2000 def test_read_after_write_unicode_filenames(self):
2001 with zipfile.ZipFile(TESTFN2, 'w') as zipfp:
2002 zipfp.writestr('приклад', b'sample')
2003 self.assertEqual(zipfp.read('приклад'), b'sample')
2004
2005 def test_exclusive_create_zip_file(self):
2006 """Test exclusive creating a new zipfile."""
2007 unlink(TESTFN2)
2008 filename = 'testfile.txt'
2009 content = b'hello, world. this is some content.'
2010 with zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED) as zipfp:
2011 zipfp.writestr(filename, content)
2012 with self.assertRaises(FileExistsError):
2013 zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED)
2014 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
2015 self.assertEqual(zipfp.namelist(), [filename])
2016 self.assertEqual(zipfp.read(filename), content)
2017
2018 def test_create_non_existent_file_for_append(self):
2019 if os.path.exists(TESTFN):
2020 os.unlink(TESTFN)
2021
2022 filename = 'testfile.txt'
2023 content = b'hello, world. this is some content.'
2024
2025 try:
2026 with zipfile.ZipFile(TESTFN, 'a') as zf:
2027 zf.writestr(filename, content)
2028 except OSError:
2029 self.fail('Could not append data to a non-existent zip file.')
2030
2031 self.assertTrue(os.path.exists(TESTFN))
2032
2033 with zipfile.ZipFile(TESTFN, 'r') as zf:
2034 self.assertEqual(zf.read(filename), content)
2035
2036 def test_close_erroneous_file(self):
2037 # This test checks that the ZipFile constructor closes the file object
2038 # it opens if there's an error in the file. If it doesn't, the
2039 # traceback holds a reference to the ZipFile object and, indirectly,
2040 # the file object.
2041 # On Windows, this causes the os.unlink() call to fail because the
2042 # underlying file is still open. This is SF bug #412214.
2043 #
2044 with open(TESTFN, "w", encoding="utf-8") as fp:
2045 fp.write("this is not a legal zip file\n")
2046 try:
2047 zf = zipfile.ZipFile(TESTFN)
2048 except zipfile.BadZipFile:
2049 pass
2050
2051 def test_is_zip_erroneous_file(self):
2052 """Check that is_zipfile() correctly identifies non-zip files."""
2053 # - passing a filename
2054 with open(TESTFN, "w", encoding='utf-8') as fp:
2055 fp.write("this is not a legal zip file\n")
2056 self.assertFalse(zipfile.is_zipfile(TESTFN))
2057 # - passing a path-like object
2058 self.assertFalse(zipfile.is_zipfile(FakePath(TESTFN)))
2059 # - passing a file object
2060 with open(TESTFN, "rb") as fp:
2061 self.assertFalse(zipfile.is_zipfile(fp))
2062 # - passing a file-like object
2063 fp = io.BytesIO()
2064 fp.write(b"this is not a legal zip file\n")
2065 self.assertFalse(zipfile.is_zipfile(fp))
2066 fp.seek(0, 0)
2067 self.assertFalse(zipfile.is_zipfile(fp))
2068 # - passing non-zipfile with ZIP header elements
2069 # data created using pyPNG like so:
2070 # d = [(ord('P'), ord('K'), 5, 6), (ord('P'), ord('K'), 6, 6)]
2071 # w = png.Writer(1,2,alpha=True,compression=0)
2072 # f = open('onepix.png', 'wb')
2073 # w.write(f, d)
2074 # w.close()
2075 data = (b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00"
2076 b"\x00\x02\x08\x06\x00\x00\x00\x99\x81\xb6'\x00\x00\x00\x15I"
2077 b"DATx\x01\x01\n\x00\xf5\xff\x00PK\x05\x06\x00PK\x06\x06\x07"
2078 b"\xac\x01N\xc6|a\r\x00\x00\x00\x00IEND\xaeB`\x82")
2079 # - passing a filename
2080 with open(TESTFN, "wb") as fp:
2081 fp.write(data)
2082 self.assertFalse(zipfile.is_zipfile(TESTFN))
2083 # - passing a file-like object
2084 fp = io.BytesIO()
2085 fp.write(data)
2086 self.assertFalse(zipfile.is_zipfile(fp))
2087
2088 def test_damaged_zipfile(self):
2089 """Check that zipfiles with missing bytes at the end raise BadZipFile."""
2090 # - Create a valid zip file
2091 fp = io.BytesIO()
2092 with zipfile.ZipFile(fp, mode="w") as zipf:
2093 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
2094 zipfiledata = fp.getvalue()
2095
2096 # - Now create copies of it missing the last N bytes and make sure
2097 # a BadZipFile exception is raised when we try to open it
2098 for N in range(len(zipfiledata)):
2099 fp = io.BytesIO(zipfiledata[:N])
2100 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, fp)
2101
2102 def test_is_zip_valid_file(self):
2103 """Check that is_zipfile() correctly identifies zip files."""
2104 # - passing a filename
2105 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2106 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
2107
2108 self.assertTrue(zipfile.is_zipfile(TESTFN))
2109 # - passing a file object
2110 with open(TESTFN, "rb") as fp:
2111 self.assertTrue(zipfile.is_zipfile(fp))
2112 fp.seek(0, 0)
2113 zip_contents = fp.read()
2114 # - passing a file-like object
2115 fp = io.BytesIO()
2116 end = fp.write(zip_contents)
2117 self.assertEqual(fp.tell(), end)
2118 mid = end // 2
2119 fp.seek(mid, 0)
2120 self.assertTrue(zipfile.is_zipfile(fp))
2121 # check that the position is left unchanged after the call
2122 # see: https://github.com/python/cpython/issues/122356
2123 self.assertEqual(fp.tell(), mid)
2124 self.assertTrue(zipfile.is_zipfile(fp))
2125 self.assertEqual(fp.tell(), mid)
2126
2127 def test_non_existent_file_raises_OSError(self):
2128 # make sure we don't raise an AttributeError when a partially-constructed
2129 # ZipFile instance is finalized; this tests for regression on SF tracker
2130 # bug #403871.
2131
2132 # The bug we're testing for caused an AttributeError to be raised
2133 # when a ZipFile instance was created for a file that did not
2134 # exist; the .fp member was not initialized but was needed by the
2135 # __del__() method. Since the AttributeError is in the __del__(),
2136 # it is ignored, but the user should be sufficiently annoyed by
2137 # the message on the output that regression will be noticed
2138 # quickly.
2139 self.assertRaises(OSError, zipfile.ZipFile, TESTFN)
2140
2141 def test_empty_file_raises_BadZipFile(self):
2142 f = open(TESTFN, 'w', encoding='utf-8')
2143 f.close()
2144 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
2145
2146 with open(TESTFN, 'w', encoding='utf-8') as fp:
2147 fp.write("short file")
2148 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
2149
2150 def test_negative_central_directory_offset_raises_BadZipFile(self):
2151 # Zip file containing an empty EOCD record
2152 buffer = bytearray(b'PK\x05\x06' + b'\0'*18)
2153
2154 # Set the size of the central directory bytes to become 1,
2155 # causing the central directory offset to become negative
2156 for dirsize in 1, 2**32-1:
2157 buffer[12:16] = struct.pack('<L', dirsize)
2158 f = io.BytesIO(buffer)
2159 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, f)
2160
2161 def test_closed_zip_raises_ValueError(self):
2162 """Verify that testzip() doesn't swallow inappropriate exceptions."""
2163 data = io.BytesIO()
2164 with zipfile.ZipFile(data, mode="w") as zipf:
2165 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2166
2167 # This is correct; calling .read on a closed ZipFile should raise
2168 # a ValueError, and so should calling .testzip. An earlier
2169 # version of .testzip would swallow this exception (and any other)
2170 # and report that the first file in the archive was corrupt.
2171 self.assertRaises(ValueError, zipf.read, "foo.txt")
2172 self.assertRaises(ValueError, zipf.open, "foo.txt")
2173 self.assertRaises(ValueError, zipf.testzip)
2174 self.assertRaises(ValueError, zipf.writestr, "bogus.txt", "bogus")
2175 with open(TESTFN, 'w', encoding='utf-8') as f:
2176 f.write('zipfile test data')
2177 self.assertRaises(ValueError, zipf.write, TESTFN)
2178
2179 def test_bad_constructor_mode(self):
2180 """Check that bad modes passed to ZipFile constructor are caught."""
2181 self.assertRaises(ValueError, zipfile.ZipFile, TESTFN, "q")
2182
2183 def test_bad_open_mode(self):
2184 """Check that bad modes passed to ZipFile.open are caught."""
2185 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2186 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2187
2188 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
2189 # read the data to make sure the file is there
2190 zipf.read("foo.txt")
2191 self.assertRaises(ValueError, zipf.open, "foo.txt", "q")
2192 # universal newlines support is removed
2193 self.assertRaises(ValueError, zipf.open, "foo.txt", "U")
2194 self.assertRaises(ValueError, zipf.open, "foo.txt", "rU")
2195
2196 def test_read0(self):
2197 """Check that calling read(0) on a ZipExtFile object returns an empty
2198 string and doesn't advance file pointer."""
2199 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2200 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2201 # read the data to make sure the file is there
2202 with zipf.open("foo.txt") as f:
2203 for i in range(FIXEDTEST_SIZE):
2204 self.assertEqual(f.read(0), b'')
2205
2206 self.assertEqual(f.read(), b"O, for a Muse of Fire!")
2207
2208 def test_open_non_existent_item(self):
2209 """Check that attempting to call open() for an item that doesn't
2210 exist in the archive raises a RuntimeError."""
2211 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2212 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
2213
2214 def test_bad_compression_mode(self):
2215 """Check that bad compression methods passed to ZipFile.open are
2216 caught."""
2217 self.assertRaises(NotImplementedError, zipfile.ZipFile, TESTFN, "w", -1)
2218
2219 def test_unsupported_compression(self):
2220 # data is declared as shrunk, but actually deflated
2221 data = (b'PK\x03\x04.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00'
2222 b'\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00x\x03\x00PK\x01'
2223 b'\x02.\x03.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00\x00\x02\x00\x00'
2224 b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
2225 b'\x80\x01\x00\x00\x00\x00xPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00'
2226 b'/\x00\x00\x00!\x00\x00\x00\x00\x00')
2227 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2228 self.assertRaises(NotImplementedError, zipf.open, 'x')
2229
2230 def test_null_byte_in_filename(self):
2231 """Check that a filename containing a null byte is properly
2232 terminated."""
2233 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2234 zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
2235 self.assertEqual(zipf.namelist(), ['foo.txt'])
2236
2237 def test_struct_sizes(self):
2238 """Check that ZIP internal structure sizes are calculated correctly."""
2239 self.assertEqual(zipfile.sizeEndCentDir, 22)
2240 self.assertEqual(zipfile.sizeCentralDir, 46)
2241 self.assertEqual(zipfile.sizeEndCentDir64, 56)
2242 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
2243
2244 def test_comments(self):
2245 """Check that comments on the archive are handled properly."""
2246
2247 # check default comment is empty
2248 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2249 self.assertEqual(zipf.comment, b'')
2250 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2251
2252 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
2253 self.assertEqual(zipfr.comment, b'')
2254
2255 # check a simple short comment
2256 comment = b'Bravely taking to his feet, he beat a very brave retreat.'
2257 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2258 zipf.comment = comment
2259 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2260 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
2261 self.assertEqual(zipf.comment, comment)
2262
2263 # check a comment of max length
2264 comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)])
2265 comment2 = comment2.encode("ascii")
2266 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2267 zipf.comment = comment2
2268 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2269
2270 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
2271 self.assertEqual(zipfr.comment, comment2)
2272
2273 # check a comment that is too long is truncated
2274 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2275 with self.assertWarns(UserWarning):
2276 zipf.comment = comment2 + b'oops'
2277 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2278 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
2279 self.assertEqual(zipfr.comment, comment2)
2280
2281 # check that comments are correctly modified in append mode
2282 with zipfile.ZipFile(TESTFN,mode="w") as zipf:
2283 zipf.comment = b"original comment"
2284 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2285 with zipfile.ZipFile(TESTFN,mode="a") as zipf:
2286 zipf.comment = b"an updated comment"
2287 with zipfile.ZipFile(TESTFN,mode="r") as zipf:
2288 self.assertEqual(zipf.comment, b"an updated comment")
2289
2290 # check that comments are correctly shortened in append mode
2291 # and the file is indeed truncated
2292 with zipfile.ZipFile(TESTFN,mode="w") as zipf:
2293 zipf.comment = b"original comment that's longer"
2294 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2295 original_zip_size = os.path.getsize(TESTFN)
2296 with zipfile.ZipFile(TESTFN,mode="a") as zipf:
2297 zipf.comment = b"shorter comment"
2298 self.assertTrue(original_zip_size > os.path.getsize(TESTFN))
2299 with zipfile.ZipFile(TESTFN,mode="r") as zipf:
2300 self.assertEqual(zipf.comment, b"shorter comment")
2301
2302 def test_unicode_comment(self):
2303 with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
2304 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2305 with self.assertRaises(TypeError):
2306 zipf.comment = "this is an error"
2307
2308 def test_change_comment_in_empty_archive(self):
2309 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
2310 self.assertFalse(zipf.filelist)
2311 zipf.comment = b"this is a comment"
2312 with zipfile.ZipFile(TESTFN, "r") as zipf:
2313 self.assertEqual(zipf.comment, b"this is a comment")
2314
2315 def test_change_comment_in_nonempty_archive(self):
2316 with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
2317 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2318 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
2319 self.assertTrue(zipf.filelist)
2320 zipf.comment = b"this is a comment"
2321 with zipfile.ZipFile(TESTFN, "r") as zipf:
2322 self.assertEqual(zipf.comment, b"this is a comment")
2323
2324 def test_empty_zipfile(self):
2325 # Check that creating a file in 'w' or 'a' mode and closing without
2326 # adding any files to the archives creates a valid empty ZIP file
2327 zipf = zipfile.ZipFile(TESTFN, mode="w")
2328 zipf.close()
2329 try:
2330 zipf = zipfile.ZipFile(TESTFN, mode="r")
2331 except zipfile.BadZipFile:
2332 self.fail("Unable to create empty ZIP file in 'w' mode")
2333 zipf.close()
2334
2335 zipf = zipfile.ZipFile(TESTFN, mode="a")
2336 zipf.close()
2337 try:
2338 zipf = zipfile.ZipFile(TESTFN, mode="r")
2339 except:
2340 self.fail("Unable to create empty ZIP file in 'a' mode")
2341 zipf.close()
2342
2343 def test_open_empty_file(self):
2344 # Issue 1710703: Check that opening a file with less than 22 bytes
2345 # raises a BadZipFile exception (rather than the previously unhelpful
2346 # OSError)
2347 f = open(TESTFN, 'w', encoding='utf-8')
2348 f.close()
2349 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r')
2350
2351 def test_create_zipinfo_before_1980(self):
2352 self.assertRaises(ValueError,
2353 zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0))
2354
2355 def test_create_empty_zipinfo_repr(self):
2356 """Before bpo-26185, repr() on empty ZipInfo object was failing."""
2357 zi = zipfile.ZipInfo(filename="empty")
2358 self.assertEqual(repr(zi), "<ZipInfo filename='empty' file_size=0>")
2359
2360 def test_for_archive(self):
2361 base_filename = TESTFN2.rstrip('/')
2362
2363 with zipfile.ZipFile(TESTFN, mode="w", compresslevel=1,
2364 compression=zipfile.ZIP_STORED) as zf:
2365 # no trailing forward slash
2366 zi = zipfile.ZipInfo(base_filename)._for_archive(zf)
2367 self.assertEqual(zi.compress_level, 1)
2368 self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
2369 # ?rw- --- ---
2370 filemode = stat.S_IRUSR | stat.S_IWUSR
2371 # filemode is stored as the highest 16 bits of external_attr
2372 self.assertEqual(zi.external_attr >> 16, filemode)
2373 self.assertEqual(zi.external_attr & 0xFF, 0) # no MS-DOS flag
2374
2375 with zipfile.ZipFile(TESTFN, mode="w", compresslevel=1,
2376 compression=zipfile.ZIP_STORED) as zf:
2377 # with a trailing slash
2378 zi = zipfile.ZipInfo(f'{base_filename}/')._for_archive(zf)
2379 self.assertEqual(zi.compress_level, 1)
2380 self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
2381 # d rwx rwx r-x
2382 filemode = stat.S_IFDIR
2383 filemode |= stat.S_IRWXU | stat.S_IRWXG
2384 filemode |= stat.S_IROTH | stat.S_IXOTH
2385 self.assertEqual(zi.external_attr >> 16, filemode)
2386 self.assertEqual(zi.external_attr & 0xFF, 0x10) # MS-DOS flag
2387
2388 def test_create_empty_zipinfo_default_attributes(self):
2389 """Ensure all required attributes are set."""
2390 zi = zipfile.ZipInfo()
2391 self.assertEqual(zi.orig_filename, "NoName")
2392 self.assertEqual(zi.filename, "NoName")
2393 self.assertEqual(zi.date_time, (1980, 1, 1, 0, 0, 0))
2394 self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
2395 self.assertEqual(zi.comment, b"")
2396 self.assertEqual(zi.extra, b"")
2397 self.assertIn(zi.create_system, (0, 3))
2398 self.assertEqual(zi.create_version, zipfile.DEFAULT_VERSION)
2399 self.assertEqual(zi.extract_version, zipfile.DEFAULT_VERSION)
2400 self.assertEqual(zi.reserved, 0)
2401 self.assertEqual(zi.flag_bits, 0)
2402 self.assertEqual(zi.volume, 0)
2403 self.assertEqual(zi.internal_attr, 0)
2404 self.assertEqual(zi.external_attr, 0)
2405
2406 # Before bpo-26185, both were missing
2407 self.assertEqual(zi.file_size, 0)
2408 self.assertEqual(zi.compress_size, 0)
2409
2410 def test_zipfile_with_short_extra_field(self):
2411 """If an extra field in the header is less than 4 bytes, skip it."""
2412 zipdata = (
2413 b'PK\x03\x04\x14\x00\x00\x00\x00\x00\x93\x9b\xad@\x8b\x9e'
2414 b'\xd9\xd3\x01\x00\x00\x00\x01\x00\x00\x00\x03\x00\x03\x00ab'
2415 b'c\x00\x00\x00APK\x01\x02\x14\x03\x14\x00\x00\x00\x00'
2416 b'\x00\x93\x9b\xad@\x8b\x9e\xd9\xd3\x01\x00\x00\x00\x01\x00\x00'
2417 b'\x00\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00'
2418 b'\x00\x00\x00abc\x00\x00PK\x05\x06\x00\x00\x00\x00'
2419 b'\x01\x00\x01\x003\x00\x00\x00%\x00\x00\x00\x00\x00'
2420 )
2421 with zipfile.ZipFile(io.BytesIO(zipdata), 'r') as zipf:
2422 # testzip returns the name of the first corrupt file, or None
2423 self.assertIsNone(zipf.testzip())
2424
2425 def test_open_conflicting_handles(self):
2426 # It's only possible to open one writable file handle at a time
2427 msg1 = b"It's fun to charter an accountant!"
2428 msg2 = b"And sail the wide accountant sea"
2429 msg3 = b"To find, explore the funds offshore"
2430 with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipf:
2431 with zipf.open('foo', mode='w') as w2:
2432 w2.write(msg1)
2433 with zipf.open('bar', mode='w') as w1:
2434 with self.assertRaises(ValueError):
2435 zipf.open('handle', mode='w')
2436 with self.assertRaises(ValueError):
2437 zipf.open('foo', mode='r')
2438 with self.assertRaises(ValueError):
2439 zipf.writestr('str', 'abcde')
2440 with self.assertRaises(ValueError):
2441 zipf.write(__file__, 'file')
2442 with self.assertRaises(ValueError):
2443 zipf.close()
2444 w1.write(msg2)
2445 with zipf.open('baz', mode='w') as w2:
2446 w2.write(msg3)
2447
2448 with zipfile.ZipFile(TESTFN2, 'r') as zipf:
2449 self.assertEqual(zipf.read('foo'), msg1)
2450 self.assertEqual(zipf.read('bar'), msg2)
2451 self.assertEqual(zipf.read('baz'), msg3)
2452 self.assertEqual(zipf.namelist(), ['foo', 'bar', 'baz'])
2453
2454 def test_seek_tell(self):
2455 # Test seek functionality
2456 txt = b"Where's Bruce?"
2457 bloc = txt.find(b"Bruce")
2458 # Check seek on a file
2459 with zipfile.ZipFile(TESTFN, "w") as zipf:
2460 zipf.writestr("foo.txt", txt)
2461 with zipfile.ZipFile(TESTFN, "r") as zipf:
2462 with zipf.open("foo.txt", "r") as fp:
2463 fp.seek(bloc, os.SEEK_SET)
2464 self.assertEqual(fp.tell(), bloc)
2465 fp.seek(-bloc, os.SEEK_CUR)
2466 self.assertEqual(fp.tell(), 0)
2467 fp.seek(bloc, os.SEEK_CUR)
2468 self.assertEqual(fp.tell(), bloc)
2469 self.assertEqual(fp.read(5), txt[bloc:bloc+5])
2470 self.assertEqual(fp.tell(), bloc + 5)
2471 fp.seek(0, os.SEEK_END)
2472 self.assertEqual(fp.tell(), len(txt))
2473 fp.seek(0, os.SEEK_SET)
2474 self.assertEqual(fp.tell(), 0)
2475 # Check seek on memory file
2476 data = io.BytesIO()
2477 with zipfile.ZipFile(data, mode="w") as zipf:
2478 zipf.writestr("foo.txt", txt)
2479 with zipfile.ZipFile(data, mode="r") as zipf:
2480 with zipf.open("foo.txt", "r") as fp:
2481 fp.seek(bloc, os.SEEK_SET)
2482 self.assertEqual(fp.tell(), bloc)
2483 fp.seek(-bloc, os.SEEK_CUR)
2484 self.assertEqual(fp.tell(), 0)
2485 fp.seek(bloc, os.SEEK_CUR)
2486 self.assertEqual(fp.tell(), bloc)
2487 self.assertEqual(fp.read(5), txt[bloc:bloc+5])
2488 self.assertEqual(fp.tell(), bloc + 5)
2489 fp.seek(0, os.SEEK_END)
2490 self.assertEqual(fp.tell(), len(txt))
2491 fp.seek(0, os.SEEK_SET)
2492 self.assertEqual(fp.tell(), 0)
2493
2494 def test_read_after_seek(self):
2495 # Issue 102956: Make sure seek(x, os.SEEK_CUR) doesn't break read()
2496 txt = b"Charge men!"
2497 bloc = txt.find(b"men")
2498 with zipfile.ZipFile(TESTFN, "w") as zipf:
2499 zipf.writestr("foo.txt", txt)
2500 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
2501 with zipf.open("foo.txt", "r") as fp:
2502 fp.seek(bloc, os.SEEK_CUR)
2503 self.assertEqual(fp.read(-1), b'men!')
2504 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
2505 with zipf.open("foo.txt", "r") as fp:
2506 fp.read(6)
2507 fp.seek(1, os.SEEK_CUR)
2508 self.assertEqual(fp.read(-1), b'men!')
2509
2510 def test_uncompressed_interleaved_seek_read(self):
2511 # gh-127847: Make sure the position in the archive is correct
2512 # in the special case of seeking in a ZIP_STORED entry.
2513 with zipfile.ZipFile(TESTFN, "w") as zipf:
2514 zipf.writestr("a.txt", "123")
2515 zipf.writestr("b.txt", "456")
2516 with zipfile.ZipFile(TESTFN, "r") as zipf:
2517 with zipf.open("a.txt", "r") as a, zipf.open("b.txt", "r") as b:
2518 self.assertEqual(a.read(1), b"1")
2519 self.assertEqual(b.seek(1), 1)
2520 self.assertEqual(b.read(1), b"5")
2521
2522 @requires_bz2()
2523 def test_decompress_without_3rd_party_library(self):
2524 data = b'PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
2525 zip_file = io.BytesIO(data)
2526 with zipfile.ZipFile(zip_file, 'w', compression=zipfile.ZIP_BZIP2) as zf:
2527 zf.writestr('a.txt', b'a')
2528 with mock.patch('zipfile.bz2', None):
2529 with zipfile.ZipFile(zip_file) as zf:
2530 self.assertRaises(RuntimeError, zf.extract, 'a.txt')
2531
2532 @requires_zlib()
2533 def test_full_overlap_different_names(self):
2534 # The ZIP file contains two central directory entries with
2535 # different names which refer to the same local header.
2536 # The name of the local header matches the name of the first
2537 # central directory entry.
2538 data = (
2539 b'PK\x03\x04\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2540 b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00b\xed'
2541 b'\xc0\x81\x08\x00\x00\x00\xc00\xd6\xfbK\\d\x0b`P'
2542 b'K\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2'
2543 b'\x1e8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00'
2544 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00aPK'
2545 b'\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2546 b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00\x00'
2547 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00bPK\x05'
2548 b'\x06\x00\x00\x00\x00\x02\x00\x02\x00^\x00\x00\x00/\x00\x00'
2549 b'\x00\x00\x00'
2550 )
2551 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2552 self.assertEqual(zipf.namelist(), ['a', 'b'])
2553 zi = zipf.getinfo('a')
2554 self.assertEqual(zi.header_offset, 0)
2555 self.assertEqual(zi.compress_size, 16)
2556 self.assertEqual(zi.file_size, 1033)
2557 zi = zipf.getinfo('b')
2558 self.assertEqual(zi.header_offset, 0)
2559 self.assertEqual(zi.compress_size, 16)
2560 self.assertEqual(zi.file_size, 1033)
2561 self.assertEqual(len(zipf.read('b')), 1033)
2562 with self.assertRaisesRegex(zipfile.BadZipFile, 'File name.*differ'):
2563 zipf.read('a')
2564
2565 @requires_zlib()
2566 def test_full_overlap_different_names2(self):
2567 # The ZIP file contains two central directory entries with
2568 # different names which refer to the same local header.
2569 # The name of the local header matches the name of the second
2570 # central directory entry.
2571 data = (
2572 b'PK\x03\x04\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2573 b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00a\xed'
2574 b'\xc0\x81\x08\x00\x00\x00\xc00\xd6\xfbK\\d\x0b`P'
2575 b'K\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2'
2576 b'\x1e8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00'
2577 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00aPK'
2578 b'\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2579 b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00\x00'
2580 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00bPK\x05'
2581 b'\x06\x00\x00\x00\x00\x02\x00\x02\x00^\x00\x00\x00/\x00\x00'
2582 b'\x00\x00\x00'
2583 )
2584 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2585 self.assertEqual(zipf.namelist(), ['a', 'b'])
2586 zi = zipf.getinfo('a')
2587 self.assertEqual(zi.header_offset, 0)
2588 self.assertEqual(zi.compress_size, 16)
2589 self.assertEqual(zi.file_size, 1033)
2590 zi = zipf.getinfo('b')
2591 self.assertEqual(zi.header_offset, 0)
2592 self.assertEqual(zi.compress_size, 16)
2593 self.assertEqual(zi.file_size, 1033)
2594 with self.assertRaisesRegex(zipfile.BadZipFile, 'File name.*differ'):
2595 zipf.read('b')
2596 with self.assertWarnsRegex(UserWarning, 'Overlapped entries') as cm:
2597 self.assertEqual(len(zipf.read('a')), 1033)
2598 self.assertEqual(cm.filename, __file__)
2599
2600 @requires_zlib()
2601 def test_full_overlap_same_name(self):
2602 # The ZIP file contains two central directory entries with
2603 # the same name which refer to the same local header.
2604 data = (
2605 b'PK\x03\x04\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2606 b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00a\xed'
2607 b'\xc0\x81\x08\x00\x00\x00\xc00\xd6\xfbK\\d\x0b`P'
2608 b'K\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2'
2609 b'\x1e8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00'
2610 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00aPK'
2611 b'\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2612 b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00\x00'
2613 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00aPK\x05'
2614 b'\x06\x00\x00\x00\x00\x02\x00\x02\x00^\x00\x00\x00/\x00\x00'
2615 b'\x00\x00\x00'
2616 )
2617 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2618 self.assertEqual(zipf.namelist(), ['a', 'a'])
2619 self.assertEqual(len(zipf.infolist()), 2)
2620 zi = zipf.getinfo('a')
2621 self.assertEqual(zi.header_offset, 0)
2622 self.assertEqual(zi.compress_size, 16)
2623 self.assertEqual(zi.file_size, 1033)
2624 self.assertEqual(len(zipf.read('a')), 1033)
2625 self.assertEqual(len(zipf.read(zi)), 1033)
2626 self.assertEqual(len(zipf.read(zipf.infolist()[1])), 1033)
2627 with self.assertWarnsRegex(UserWarning, 'Overlapped entries') as cm:
2628 self.assertEqual(len(zipf.read(zipf.infolist()[0])), 1033)
2629 self.assertEqual(cm.filename, __file__)
2630 with self.assertWarnsRegex(UserWarning, 'Overlapped entries') as cm:
2631 zipf.open(zipf.infolist()[0]).close()
2632 self.assertEqual(cm.filename, __file__)
2633
2634 @requires_zlib()
2635 def test_quoted_overlap(self):
2636 # The ZIP file contains two files. The second local header
2637 # is contained in the range of the first file.
2638 data = (
2639 b'PK\x03\x04\x14\x00\x00\x00\x08\x00\xa0lH\x05Y\xfc'
2640 b'8\x044\x00\x00\x00(\x04\x00\x00\x01\x00\x00\x00a\x00'
2641 b'\x1f\x00\xe0\xffPK\x03\x04\x14\x00\x00\x00\x08\x00\xa0l'
2642 b'H\x05\xe2\x1e8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00'
2643 b'\x00\x00b\xed\xc0\x81\x08\x00\x00\x00\xc00\xd6\xfbK\\'
2644 b'd\x0b`PK\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0'
2645 b'lH\x05Y\xfc8\x044\x00\x00\x00(\x04\x00\x00\x01'
2646 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
2647 b'\x00aPK\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0l'
2648 b'H\x05\xe2\x1e8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00'
2649 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$\x00\x00\x00'
2650 b'bPK\x05\x06\x00\x00\x00\x00\x02\x00\x02\x00^\x00\x00'
2651 b'\x00S\x00\x00\x00\x00\x00'
2652 )
2653 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2654 self.assertEqual(zipf.namelist(), ['a', 'b'])
2655 zi = zipf.getinfo('a')
2656 self.assertEqual(zi.header_offset, 0)
2657 self.assertEqual(zi.compress_size, 52)
2658 self.assertEqual(zi.file_size, 1064)
2659 zi = zipf.getinfo('b')
2660 self.assertEqual(zi.header_offset, 36)
2661 self.assertEqual(zi.compress_size, 16)
2662 self.assertEqual(zi.file_size, 1033)
2663 with self.assertRaisesRegex(zipfile.BadZipFile, 'Overlapped entries'):
2664 zipf.read('a')
2665 self.assertEqual(len(zipf.read('b')), 1033)
2666
2667 @requires_zlib()
2668 def test_overlap_with_central_dir(self):
2669 # The local header offset is equal to the central directory offset.
2670 data = (
2671 b'PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00G_|Z'
2672 b'\xe2\x1e8\xbb\x0b\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00'
2673 b'\x00\x00\x00\x00\x00\x00\x00\x00\xb4\x81\x00\x00\x00\x00aP'
2674 b'K\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00/\x00\x00\x00\x00'
2675 b'\x00\x00\x00\x00\x00'
2676 )
2677 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2678 self.assertEqual(zipf.namelist(), ['a'])
2679 self.assertEqual(len(zipf.infolist()), 1)
2680 zi = zipf.getinfo('a')
2681 self.assertEqual(zi.header_offset, 0)
2682 self.assertEqual(zi.compress_size, 11)
2683 self.assertEqual(zi.file_size, 1033)
2684 # Found central directory signature PK\x01\x02 instead of
2685 # local header signature PK\x03\x04.
2686 with self.assertRaisesRegex(zipfile.BadZipFile, 'Bad magic number'):
2687 zipf.read('a')
2688
2689 @requires_zlib()
2690 def test_overlap_with_archive_comment(self):
2691 # The local header is written after the central directory,
2692 # in the archive comment.
2693 data = (
2694 b'PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00G_|Z'
2695 b'\xe2\x1e8\xbb\x0b\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00'
2696 b'\x00\x00\x00\x00\x00\x00\x00\x00\xb4\x81E\x00\x00\x00aP'
2697 b'K\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00/\x00\x00\x00\x00'
2698 b'\x00\x00\x00*\x00'
2699 b'PK\x03\x04\x14\x00\x00\x00\x08\x00G_|Z\xe2\x1e'
2700 b'8\xbb\x0b\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00aK'
2701 b'L\x1c\x05\xa3`\x14\x8cx\x00\x00'
2702 )
2703 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2704 self.assertEqual(zipf.namelist(), ['a'])
2705 self.assertEqual(len(zipf.infolist()), 1)
2706 zi = zipf.getinfo('a')
2707 self.assertEqual(zi.header_offset, 69)
2708 self.assertEqual(zi.compress_size, 11)
2709 self.assertEqual(zi.file_size, 1033)
2710 with self.assertRaisesRegex(zipfile.BadZipFile, 'Overlapped entries'):
2711 zipf.read('a')
2712
2713 def tearDown(self):
2714 unlink(TESTFN)
2715 unlink(TESTFN2)
2716
2717
2718 class AbstractBadCrcTests:
2719 def test_testzip_with_bad_crc(self):
2720 """Tests that files with bad CRCs return their name from testzip."""
2721 zipdata = self.zip_with_bad_crc
2722
2723 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
2724 # testzip returns the name of the first corrupt file, or None
2725 self.assertEqual('afile', zipf.testzip())
2726
2727 def test_read_with_bad_crc(self):
2728 """Tests that files with bad CRCs raise a BadZipFile exception when read."""
2729 zipdata = self.zip_with_bad_crc
2730
2731 # Using ZipFile.read()
2732 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
2733 self.assertRaises(zipfile.BadZipFile, zipf.read, 'afile')
2734
2735 # Using ZipExtFile.read()
2736 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
2737 with zipf.open('afile', 'r') as corrupt_file:
2738 self.assertRaises(zipfile.BadZipFile, corrupt_file.read)
2739
2740 # Same with small reads (in order to exercise the buffering logic)
2741 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
2742 with zipf.open('afile', 'r') as corrupt_file:
2743 corrupt_file.MIN_READ_SIZE = 2
2744 with self.assertRaises(zipfile.BadZipFile):
2745 while corrupt_file.read(2):
2746 pass
2747
2748
2749 class StoredBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
2750 compression = zipfile.ZIP_STORED
2751 zip_with_bad_crc = (
2752 b'PK\003\004\024\0\0\0\0\0 \213\212;:r'
2753 b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af'
2754 b'ilehello,AworldP'
2755 b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:'
2756 b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0'
2757 b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi'
2758 b'lePK\005\006\0\0\0\0\001\0\001\0003\000'
2759 b'\0\0/\0\0\0\0\0')
2760
2761 @requires_zlib()
2762 class DeflateBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
2763 compression = zipfile.ZIP_DEFLATED
2764 zip_with_bad_crc = (
2765 b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA'
2766 b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
2767 b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0'
2768 b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n'
2769 b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05'
2770 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00'
2771 b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00'
2772 b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00')
2773
2774 @requires_bz2()
2775 class Bzip2BadCrcTests(AbstractBadCrcTests, unittest.TestCase):
2776 compression = zipfile.ZIP_BZIP2
2777 zip_with_bad_crc = (
2778 b'PK\x03\x04\x14\x03\x00\x00\x0c\x00nu\x0c=FA'
2779 b'KE8\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
2780 b'ileBZh91AY&SY\xd4\xa8\xca'
2781 b'\x7f\x00\x00\x0f\x11\x80@\x00\x06D\x90\x80 \x00 \xa5'
2782 b'P\xd9!\x03\x03\x13\x13\x13\x89\xa9\xa9\xc2u5:\x9f'
2783 b'\x8b\xb9"\x9c(HjTe?\x80PK\x01\x02\x14'
2784 b'\x03\x14\x03\x00\x00\x0c\x00nu\x0c=FAKE8'
2785 b'\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00'
2786 b'\x00 \x80\x80\x81\x00\x00\x00\x00afilePK'
2787 b'\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00\x00[\x00'
2788 b'\x00\x00\x00\x00')
2789
2790 @requires_lzma()
2791 class LzmaBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
2792 compression = zipfile.ZIP_LZMA
2793 zip_with_bad_crc = (
2794 b'PK\x03\x04\x14\x03\x00\x00\x0e\x00nu\x0c=FA'
2795 b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
2796 b'ile\t\x04\x05\x00]\x00\x00\x00\x04\x004\x19I'
2797 b'\xee\x8d\xe9\x17\x89:3`\tq!.8\x00PK'
2798 b'\x01\x02\x14\x03\x14\x03\x00\x00\x0e\x00nu\x0c=FA'
2799 b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00'
2800 b'\x00\x00\x00\x00 \x80\x80\x81\x00\x00\x00\x00afil'
2801 b'ePK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00'
2802 b'\x00>\x00\x00\x00\x00\x00')
2803
2804 @requires_zstd()
2805 class ZstdBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
2806 compression = zipfile.ZIP_ZSTANDARD
2807 zip_with_bad_crc = (
2808 b'PK\x03\x04?\x00\x00\x00]\x00\x00\x00!\x00V\xb1\x17J\x14\x00'
2809 b'\x00\x00\x0b\x00\x00\x00\x05\x00\x00\x00afile(\xb5/\xfd\x00'
2810 b'XY\x00\x00Hello WorldPK\x01\x02?\x03?\x00\x00\x00]\x00\x00\x00'
2811 b'!\x00V\xb0\x17J\x14\x00\x00\x00\x0b\x00\x00\x00\x05\x00\x00\x00'
2812 b'\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00\x00afilePK'
2813 b'\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00\x007\x00\x00\x00'
2814 b'\x00\x00')
2815
2816 class DecryptionTests(unittest.TestCase):
2817 """Check that ZIP decryption works. Since the library does not
2818 support encryption at the moment, we use a pre-generated encrypted
2819 ZIP file."""
2820
2821 data = (
2822 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
2823 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
2824 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
2825 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
2826 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
2827 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
2828 b'\x00\x00L\x00\x00\x00\x00\x00' )
2829 data2 = (
2830 b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
2831 b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
2832 b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
2833 b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
2834 b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
2835 b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
2836 b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
2837 b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
2838
2839 plain = b'zipfile.py encryption test'
2840 plain2 = b'\x00'*512
2841
2842 def setUp(self):
2843 with open(TESTFN, "wb") as fp:
2844 fp.write(self.data)
2845 self.zip = zipfile.ZipFile(TESTFN, "r")
2846 with open(TESTFN2, "wb") as fp:
2847 fp.write(self.data2)
2848 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
2849
2850 def tearDown(self):
2851 self.zip.close()
2852 os.unlink(TESTFN)
2853 self.zip2.close()
2854 os.unlink(TESTFN2)
2855
2856 def test_no_password(self):
2857 # Reading the encrypted file without password
2858 # must generate a RunTime exception
2859 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
2860 self.assertRaises(RuntimeError, self.zip2.read, "zero")
2861
2862 def test_bad_password(self):
2863 self.zip.setpassword(b"perl")
2864 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
2865 self.zip2.setpassword(b"perl")
2866 self.assertRaises(RuntimeError, self.zip2.read, "zero")
2867
2868 @requires_zlib()
2869 def test_good_password(self):
2870 self.zip.setpassword(b"python")
2871 self.assertEqual(self.zip.read("test.txt"), self.plain)
2872 self.zip2.setpassword(b"12345")
2873 self.assertEqual(self.zip2.read("zero"), self.plain2)
2874
2875 def test_unicode_password(self):
2876 expected_msg = "pwd: expected bytes, got str"
2877
2878 with self.assertRaisesRegex(TypeError, expected_msg):
2879 self.zip.setpassword("unicode")
2880
2881 with self.assertRaisesRegex(TypeError, expected_msg):
2882 self.zip.read("test.txt", "python")
2883
2884 with self.assertRaisesRegex(TypeError, expected_msg):
2885 self.zip.open("test.txt", pwd="python")
2886
2887 with self.assertRaisesRegex(TypeError, expected_msg):
2888 self.zip.extract("test.txt", pwd="python")
2889
2890 with self.assertRaisesRegex(TypeError, expected_msg):
2891 self.zip.pwd = "python"
2892 self.zip.open("test.txt")
2893
2894 def test_seek_tell(self):
2895 self.zip.setpassword(b"python")
2896 txt = self.plain
2897 test_word = b'encryption'
2898 bloc = txt.find(test_word)
2899 bloc_len = len(test_word)
2900 with self.zip.open("test.txt", "r") as fp:
2901 fp.seek(bloc, os.SEEK_SET)
2902 self.assertEqual(fp.tell(), bloc)
2903 fp.seek(-bloc, os.SEEK_CUR)
2904 self.assertEqual(fp.tell(), 0)
2905 fp.seek(bloc, os.SEEK_CUR)
2906 self.assertEqual(fp.tell(), bloc)
2907 self.assertEqual(fp.read(bloc_len), txt[bloc:bloc+bloc_len])
2908
2909 # Make sure that the second read after seeking back beyond
2910 # _readbuffer returns the same content (ie. rewind to the start of
2911 # the file to read forward to the required position).
2912 old_read_size = fp.MIN_READ_SIZE
2913 fp.MIN_READ_SIZE = 1
2914 fp._readbuffer = b''
2915 fp._offset = 0
2916 fp.seek(0, os.SEEK_SET)
2917 self.assertEqual(fp.tell(), 0)
2918 fp.seek(bloc, os.SEEK_CUR)
2919 self.assertEqual(fp.read(bloc_len), txt[bloc:bloc+bloc_len])
2920 fp.MIN_READ_SIZE = old_read_size
2921
2922 fp.seek(0, os.SEEK_END)
2923 self.assertEqual(fp.tell(), len(txt))
2924 fp.seek(0, os.SEEK_SET)
2925 self.assertEqual(fp.tell(), 0)
2926
2927 # Read the file completely to definitely call any eof integrity
2928 # checks (crc) and make sure they still pass.
2929 fp.read()
2930
2931
2932 class AbstractTestsWithRandomBinaryFiles:
2933 @classmethod
2934 def setUpClass(cls):
2935 datacount = randint(16, 64)*1024 + randint(1, 1024)
2936 cls.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
2937 for i in range(datacount))
2938
2939 def setUp(self):
2940 # Make a source file with some lines
2941 with open(TESTFN, "wb") as fp:
2942 fp.write(self.data)
2943
2944 def tearDown(self):
2945 unlink(TESTFN)
2946 unlink(TESTFN2)
2947
2948 def make_test_archive(self, f, compression):
2949 # Create the ZIP archive
2950 with zipfile.ZipFile(f, "w", compression) as zipfp:
2951 zipfp.write(TESTFN, "another.name")
2952 zipfp.write(TESTFN, TESTFN)
2953
2954 def zip_test(self, f, compression):
2955 self.make_test_archive(f, compression)
2956
2957 # Read the ZIP archive
2958 with zipfile.ZipFile(f, "r", compression) as zipfp:
2959 testdata = zipfp.read(TESTFN)
2960 self.assertEqual(len(testdata), len(self.data))
2961 self.assertEqual(testdata, self.data)
2962 self.assertEqual(zipfp.read("another.name"), self.data)
2963
2964 def test_read(self):
2965 for f in get_files(self):
2966 self.zip_test(f, self.compression)
2967
2968 def zip_open_test(self, f, compression):
2969 self.make_test_archive(f, compression)
2970
2971 # Read the ZIP archive
2972 with zipfile.ZipFile(f, "r", compression) as zipfp:
2973 zipdata1 = []
2974 with zipfp.open(TESTFN) as zipopen1:
2975 while True:
2976 read_data = zipopen1.read(256)
2977 if not read_data:
2978 break
2979 zipdata1.append(read_data)
2980
2981 zipdata2 = []
2982 with zipfp.open("another.name") as zipopen2:
2983 while True:
2984 read_data = zipopen2.read(256)
2985 if not read_data:
2986 break
2987 zipdata2.append(read_data)
2988
2989 testdata1 = b''.join(zipdata1)
2990 self.assertEqual(len(testdata1), len(self.data))
2991 self.assertEqual(testdata1, self.data)
2992
2993 testdata2 = b''.join(zipdata2)
2994 self.assertEqual(len(testdata2), len(self.data))
2995 self.assertEqual(testdata2, self.data)
2996
2997 def test_open(self):
2998 for f in get_files(self):
2999 self.zip_open_test(f, self.compression)
3000
3001 def zip_random_open_test(self, f, compression):
3002 self.make_test_archive(f, compression)
3003
3004 # Read the ZIP archive
3005 with zipfile.ZipFile(f, "r", compression) as zipfp:
3006 zipdata1 = []
3007 with zipfp.open(TESTFN) as zipopen1:
3008 while True:
3009 read_data = zipopen1.read(randint(1, 1024))
3010 if not read_data:
3011 break
3012 zipdata1.append(read_data)
3013
3014 testdata = b''.join(zipdata1)
3015 self.assertEqual(len(testdata), len(self.data))
3016 self.assertEqual(testdata, self.data)
3017
3018 def test_random_open(self):
3019 for f in get_files(self):
3020 self.zip_random_open_test(f, self.compression)
3021
3022
3023 class StoredTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
3024 unittest.TestCase):
3025 compression = zipfile.ZIP_STORED
3026
3027 @requires_zlib()
3028 class DeflateTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
3029 unittest.TestCase):
3030 compression = zipfile.ZIP_DEFLATED
3031
3032 @requires_bz2()
3033 class Bzip2TestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
3034 unittest.TestCase):
3035 compression = zipfile.ZIP_BZIP2
3036
3037 @requires_lzma()
3038 class LzmaTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
3039 unittest.TestCase):
3040 compression = zipfile.ZIP_LZMA
3041
3042 @requires_zstd()
3043 class ZstdTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
3044 unittest.TestCase):
3045 compression = zipfile.ZIP_ZSTANDARD
3046
3047 # Provide the tell() method but not seek()
3048 class Tellable:
3049 def __init__(self, fp):
3050 self.fp = fp
3051 self.offset = 0
3052
3053 def write(self, data):
3054 n = self.fp.write(data)
3055 self.offset += n
3056 return n
3057
3058 def tell(self):
3059 return self.offset
3060
3061 def flush(self):
3062 self.fp.flush()
3063
3064 class Unseekable:
3065 def __init__(self, fp):
3066 self.fp = fp
3067
3068 def write(self, data):
3069 return self.fp.write(data)
3070
3071 def flush(self):
3072 self.fp.flush()
3073
3074 class UnseekableTests(unittest.TestCase):
3075 def test_writestr(self):
3076 for wrapper in (lambda f: f), Tellable, Unseekable:
3077 with self.subTest(wrapper=wrapper):
3078 f = io.BytesIO()
3079 f.write(b'abc')
3080 bf = io.BufferedWriter(f)
3081 with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipfp:
3082 zipfp.writestr('ones', b'111')
3083 zipfp.writestr('twos', b'222')
3084 self.assertEqual(f.getvalue()[:5], b'abcPK')
3085 with zipfile.ZipFile(f, mode='r') as zipf:
3086 with zipf.open('ones') as zopen:
3087 self.assertEqual(zopen.read(), b'111')
3088 with zipf.open('twos') as zopen:
3089 self.assertEqual(zopen.read(), b'222')
3090
3091 def test_write(self):
3092 for wrapper in (lambda f: f), Tellable, Unseekable:
3093 with self.subTest(wrapper=wrapper):
3094 f = io.BytesIO()
3095 f.write(b'abc')
3096 bf = io.BufferedWriter(f)
3097 with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipfp:
3098 self.addCleanup(unlink, TESTFN)
3099 with open(TESTFN, 'wb') as f2:
3100 f2.write(b'111')
3101 zipfp.write(TESTFN, 'ones')
3102 with open(TESTFN, 'wb') as f2:
3103 f2.write(b'222')
3104 zipfp.write(TESTFN, 'twos')
3105 self.assertEqual(f.getvalue()[:5], b'abcPK')
3106 with zipfile.ZipFile(f, mode='r') as zipf:
3107 with zipf.open('ones') as zopen:
3108 self.assertEqual(zopen.read(), b'111')
3109 with zipf.open('twos') as zopen:
3110 self.assertEqual(zopen.read(), b'222')
3111
3112 def test_open_write(self):
3113 for wrapper in (lambda f: f), Tellable, Unseekable:
3114 with self.subTest(wrapper=wrapper):
3115 f = io.BytesIO()
3116 f.write(b'abc')
3117 bf = io.BufferedWriter(f)
3118 with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipf:
3119 with zipf.open('ones', 'w') as zopen:
3120 zopen.write(b'111')
3121 with zipf.open('twos', 'w') as zopen:
3122 zopen.write(b'222')
3123 self.assertEqual(f.getvalue()[:5], b'abcPK')
3124 with zipfile.ZipFile(f) as zipf:
3125 self.assertEqual(zipf.read('ones'), b'111')
3126 self.assertEqual(zipf.read('twos'), b'222')
3127
3128
3129 @requires_zlib()
3130 class TestsWithMultipleOpens(unittest.TestCase):
3131 @classmethod
3132 def setUpClass(cls):
3133 cls.data1 = b'111' + randbytes(10000)
3134 cls.data2 = b'222' + randbytes(10000)
3135
3136 def make_test_archive(self, f):
3137 # Create the ZIP archive
3138 with zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED) as zipfp:
3139 zipfp.writestr('ones', self.data1)
3140 zipfp.writestr('twos', self.data2)
3141
3142 def test_same_file(self):
3143 # Verify that (when the ZipFile is in control of creating file objects)
3144 # multiple open() calls can be made without interfering with each other.
3145 for f in get_files(self):
3146 self.make_test_archive(f)
3147 with zipfile.ZipFile(f, mode="r") as zipf:
3148 with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2:
3149 data1 = zopen1.read(500)
3150 data2 = zopen2.read(500)
3151 data1 += zopen1.read()
3152 data2 += zopen2.read()
3153 self.assertEqual(data1, data2)
3154 self.assertEqual(data1, self.data1)
3155
3156 def test_different_file(self):
3157 # Verify that (when the ZipFile is in control of creating file objects)
3158 # multiple open() calls can be made without interfering with each other.
3159 for f in get_files(self):
3160 self.make_test_archive(f)
3161 with zipfile.ZipFile(f, mode="r") as zipf:
3162 with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
3163 data1 = zopen1.read(500)
3164 data2 = zopen2.read(500)
3165 data1 += zopen1.read()
3166 data2 += zopen2.read()
3167 self.assertEqual(data1, self.data1)
3168 self.assertEqual(data2, self.data2)
3169
3170 def test_interleaved(self):
3171 # Verify that (when the ZipFile is in control of creating file objects)
3172 # multiple open() calls can be made without interfering with each other.
3173 for f in get_files(self):
3174 self.make_test_archive(f)
3175 with zipfile.ZipFile(f, mode="r") as zipf:
3176 with zipf.open('ones') as zopen1:
3177 data1 = zopen1.read(500)
3178 with zipf.open('twos') as zopen2:
3179 data2 = zopen2.read(500)
3180 data1 += zopen1.read()
3181 data2 += zopen2.read()
3182 self.assertEqual(data1, self.data1)
3183 self.assertEqual(data2, self.data2)
3184
3185 def test_read_after_close(self):
3186 for f in get_files(self):
3187 self.make_test_archive(f)
3188 with contextlib.ExitStack() as stack:
3189 with zipfile.ZipFile(f, 'r') as zipf:
3190 zopen1 = stack.enter_context(zipf.open('ones'))
3191 zopen2 = stack.enter_context(zipf.open('twos'))
3192 data1 = zopen1.read(500)
3193 data2 = zopen2.read(500)
3194 data1 += zopen1.read()
3195 data2 += zopen2.read()
3196 self.assertEqual(data1, self.data1)
3197 self.assertEqual(data2, self.data2)
3198
3199 def test_read_after_write(self):
3200 for f in get_files(self):
3201 with zipfile.ZipFile(f, 'w', zipfile.ZIP_DEFLATED) as zipf:
3202 zipf.writestr('ones', self.data1)
3203 zipf.writestr('twos', self.data2)
3204 with zipf.open('ones') as zopen1:
3205 data1 = zopen1.read(500)
3206 self.assertEqual(data1, self.data1[:500])
3207 with zipfile.ZipFile(f, 'r') as zipf:
3208 data1 = zipf.read('ones')
3209 data2 = zipf.read('twos')
3210 self.assertEqual(data1, self.data1)
3211 self.assertEqual(data2, self.data2)
3212
3213 def test_write_after_read(self):
3214 for f in get_files(self):
3215 with zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED) as zipf:
3216 zipf.writestr('ones', self.data1)
3217 with zipf.open('ones') as zopen1:
3218 zopen1.read(500)
3219 zipf.writestr('twos', self.data2)
3220 with zipfile.ZipFile(f, 'r') as zipf:
3221 data1 = zipf.read('ones')
3222 data2 = zipf.read('twos')
3223 self.assertEqual(data1, self.data1)
3224 self.assertEqual(data2, self.data2)
3225
3226 def test_many_opens(self):
3227 # Verify that read() and open() promptly close the file descriptor,
3228 # and don't rely on the garbage collector to free resources.
3229 startcount = fd_count()
3230 self.make_test_archive(TESTFN2)
3231 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
3232 for x in range(100):
3233 zipf.read('ones')
3234 with zipf.open('ones') as zopen1:
3235 pass
3236 self.assertEqual(startcount, fd_count())
3237
3238 def test_write_while_reading(self):
3239 with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_DEFLATED) as zipf:
3240 zipf.writestr('ones', self.data1)
3241 with zipfile.ZipFile(TESTFN2, 'a', zipfile.ZIP_DEFLATED) as zipf:
3242 with zipf.open('ones', 'r') as r1:
3243 data1 = r1.read(500)
3244 with zipf.open('twos', 'w') as w1:
3245 w1.write(self.data2)
3246 data1 += r1.read()
3247 self.assertEqual(data1, self.data1)
3248 with zipfile.ZipFile(TESTFN2) as zipf:
3249 self.assertEqual(zipf.read('twos'), self.data2)
3250
3251 def tearDown(self):
3252 unlink(TESTFN2)
3253
3254
3255 class TestWithDirectory(unittest.TestCase):
3256 def setUp(self):
3257 os.mkdir(TESTFN2)
3258
3259 def test_extract_dir(self):
3260 with zipfile.ZipFile(findfile("zipdir.zip", subdir="archivetestdata")) as zipf:
3261 zipf.extractall(TESTFN2)
3262 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
3263 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
3264 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
3265
3266 def test_bug_6050(self):
3267 # Extraction should succeed if directories already exist
3268 os.mkdir(os.path.join(TESTFN2, "a"))
3269 self.test_extract_dir()
3270
3271 def test_extract_dir_backslash(self):
3272 zfname = findfile("zipdir_backslash.zip", subdir="archivetestdata")
3273 with zipfile.ZipFile(zfname) as zipf:
3274 zipf.extractall(TESTFN2)
3275 if os.name == 'nt':
3276 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
3277 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
3278 self.assertTrue(os.path.isfile(os.path.join(TESTFN2, "a", "b", "c")))
3279 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "d")))
3280 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "d", "e")))
3281 else:
3282 self.assertTrue(os.path.isfile(os.path.join(TESTFN2, "a\\b\\c")))
3283 self.assertTrue(os.path.isfile(os.path.join(TESTFN2, "d\\e\\")))
3284 self.assertFalse(os.path.exists(os.path.join(TESTFN2, "a")))
3285 self.assertFalse(os.path.exists(os.path.join(TESTFN2, "d")))
3286
3287 def test_write_dir(self):
3288 dirpath = os.path.join(TESTFN2, "x")
3289 os.mkdir(dirpath)
3290 mode = os.stat(dirpath).st_mode & 0xFFFF
3291 with zipfile.ZipFile(TESTFN, "w") as zipf:
3292 zipf.write(dirpath)
3293 zinfo = zipf.filelist[0]
3294 self.assertEndsWith(zinfo.filename, "/x/")
3295 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
3296 zipf.write(dirpath, "y")
3297 zinfo = zipf.filelist[1]
3298 self.assertTrue(zinfo.filename, "y/")
3299 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
3300 with zipfile.ZipFile(TESTFN, "r") as zipf:
3301 zinfo = zipf.filelist[0]
3302 self.assertEndsWith(zinfo.filename, "/x/")
3303 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
3304 zinfo = zipf.filelist[1]
3305 self.assertTrue(zinfo.filename, "y/")
3306 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
3307 target = os.path.join(TESTFN2, "target")
3308 os.mkdir(target)
3309 zipf.extractall(target)
3310 self.assertTrue(os.path.isdir(os.path.join(target, "y")))
3311 self.assertEqual(len(os.listdir(target)), 2)
3312
3313 def test_writestr_dir(self):
3314 os.mkdir(os.path.join(TESTFN2, "x"))
3315 with zipfile.ZipFile(TESTFN, "w") as zipf:
3316 zipf.writestr("x/", b'')
3317 zinfo = zipf.filelist[0]
3318 self.assertEqual(zinfo.filename, "x/")
3319 self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10)
3320 with zipfile.ZipFile(TESTFN, "r") as zipf:
3321 zinfo = zipf.filelist[0]
3322 self.assertEndsWith(zinfo.filename, "x/")
3323 self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10)
3324 target = os.path.join(TESTFN2, "target")
3325 os.mkdir(target)
3326 zipf.extractall(target)
3327 self.assertTrue(os.path.isdir(os.path.join(target, "x")))
3328 self.assertEqual(os.listdir(target), ["x"])
3329
3330 def test_mkdir(self):
3331 with zipfile.ZipFile(TESTFN, "w") as zf:
3332 zf.mkdir("directory")
3333 zinfo = zf.filelist[0]
3334 self.assertEqual(zinfo.filename, "directory/")
3335 self.assertEqual(zinfo.external_attr, (0o40777 << 16) | 0x10)
3336
3337 zf.mkdir("directory2/")
3338 zinfo = zf.filelist[1]
3339 self.assertEqual(zinfo.filename, "directory2/")
3340 self.assertEqual(zinfo.external_attr, (0o40777 << 16) | 0x10)
3341
3342 zf.mkdir("directory3", mode=0o777)
3343 zinfo = zf.filelist[2]
3344 self.assertEqual(zinfo.filename, "directory3/")
3345 self.assertEqual(zinfo.external_attr, (0o40777 << 16) | 0x10)
3346
3347 old_zinfo = zipfile.ZipInfo("directory4/")
3348 old_zinfo.external_attr = (0o40777 << 16) | 0x10
3349 old_zinfo.CRC = 0
3350 old_zinfo.file_size = 0
3351 old_zinfo.compress_size = 0
3352 zf.mkdir(old_zinfo)
3353 new_zinfo = zf.filelist[3]
3354 self.assertEqual(old_zinfo.filename, "directory4/")
3355 self.assertEqual(old_zinfo.external_attr, new_zinfo.external_attr)
3356
3357 target = os.path.join(TESTFN2, "target")
3358 os.mkdir(target)
3359 zf.extractall(target)
3360 self.assertEqual(set(os.listdir(target)), {"directory", "directory2", "directory3", "directory4"})
3361
3362 def test_create_directory_with_write(self):
3363 with zipfile.ZipFile(TESTFN, "w") as zf:
3364 zf.writestr(zipfile.ZipInfo('directory/'), '')
3365
3366 zinfo = zf.filelist[0]
3367 self.assertEqual(zinfo.filename, "directory/")
3368
3369 directory = os.path.join(TESTFN2, "directory2")
3370 os.mkdir(directory)
3371 mode = os.stat(directory).st_mode & 0xFFFF
3372 zf.write(directory, arcname="directory2/")
3373 zinfo = zf.filelist[1]
3374 self.assertEqual(zinfo.filename, "directory2/")
3375 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
3376
3377 target = os.path.join(TESTFN2, "target")
3378 os.mkdir(target)
3379 zf.extractall(target)
3380
3381 self.assertEqual(set(os.listdir(target)), {"directory", "directory2"})
3382
3383 def test_root_folder_in_zipfile(self):
3384 """
3385 gh-112795: Some tools or self constructed codes will add '/' folder to
3386 the zip file, this is a strange behavior, but we should support it.
3387 """
3388 in_memory_file = io.BytesIO()
3389 zf = zipfile.ZipFile(in_memory_file, "w")
3390 zf.mkdir('/')
3391 zf.writestr('./a.txt', 'aaa')
3392 zf.extractall(TESTFN2)
3393
3394 def tearDown(self):
3395 rmtree(TESTFN2)
3396 if os.path.exists(TESTFN):
3397 unlink(TESTFN)
3398
3399
3400 class ZipInfoTests(unittest.TestCase):
3401 def test_from_file(self):
3402 zi = zipfile.ZipInfo.from_file(__file__)
3403 self.assertEqual(posixpath.basename(zi.filename), 'test_core.py')
3404 self.assertFalse(zi.is_dir())
3405 self.assertEqual(zi.file_size, os.path.getsize(__file__))
3406
3407 def test_from_file_pathlike(self):
3408 zi = zipfile.ZipInfo.from_file(FakePath(__file__))
3409 self.assertEqual(posixpath.basename(zi.filename), 'test_core.py')
3410 self.assertFalse(zi.is_dir())
3411 self.assertEqual(zi.file_size, os.path.getsize(__file__))
3412
3413 def test_from_file_bytes(self):
3414 zi = zipfile.ZipInfo.from_file(os.fsencode(__file__), 'test')
3415 self.assertEqual(posixpath.basename(zi.filename), 'test')
3416 self.assertFalse(zi.is_dir())
3417 self.assertEqual(zi.file_size, os.path.getsize(__file__))
3418
3419 def test_from_file_fileno(self):
3420 with open(__file__, 'rb') as f:
3421 zi = zipfile.ZipInfo.from_file(f.fileno(), 'test')
3422 self.assertEqual(posixpath.basename(zi.filename), 'test')
3423 self.assertFalse(zi.is_dir())
3424 self.assertEqual(zi.file_size, os.path.getsize(__file__))
3425
3426 def test_from_dir(self):
3427 dirpath = os.path.dirname(os.path.abspath(__file__))
3428 zi = zipfile.ZipInfo.from_file(dirpath, 'stdlib_tests')
3429 self.assertEqual(zi.filename, 'stdlib_tests/')
3430 self.assertTrue(zi.is_dir())
3431 self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
3432 self.assertEqual(zi.file_size, 0)
3433
3434 def test_compresslevel_property(self):
3435 zinfo = zipfile.ZipInfo("xxx")
3436 self.assertFalse(zinfo._compresslevel)
3437 self.assertFalse(zinfo.compress_level)
3438 zinfo._compresslevel = 99 # test the legacy @property.setter
3439 self.assertEqual(zinfo.compress_level, 99)
3440 self.assertEqual(zinfo._compresslevel, 99)
3441 zinfo.compress_level = 8
3442 self.assertEqual(zinfo.compress_level, 8)
3443 self.assertEqual(zinfo._compresslevel, 8)
3444
3445
3446 class CommandLineTest(unittest.TestCase):
3447
3448 def zipfilecmd(self, *args, **kwargs):
3449 rc, out, err = script_helper.assert_python_ok('-m', 'zipfile', *args,
3450 **kwargs)
3451 return out.replace(os.linesep.encode(), b'\n')
3452
3453 def zipfilecmd_failure(self, *args):
3454 return script_helper.assert_python_failure('-m', 'zipfile', *args)
3455
3456 def test_bad_use(self):
3457 rc, out, err = self.zipfilecmd_failure()
3458 self.assertEqual(out, b'')
3459 self.assertIn(b'usage', err.lower())
3460 self.assertIn(b'error', err.lower())
3461 self.assertIn(b'required', err.lower())
3462 rc, out, err = self.zipfilecmd_failure('-l', '')
3463 self.assertEqual(out, b'')
3464 self.assertNotEqual(err.strip(), b'')
3465
3466 def test_test_command(self):
3467 zip_name = findfile('zipdir.zip', subdir='archivetestdata')
3468 for opt in '-t', '--test':
3469 out = self.zipfilecmd(opt, zip_name)
3470 self.assertEqual(out.rstrip(), b'Done testing')
3471 zip_name = findfile('testtar.tar')
3472 rc, out, err = self.zipfilecmd_failure('-t', zip_name)
3473 self.assertEqual(out, b'')
3474
3475 def test_list_command(self):
3476 zip_name = findfile('zipdir.zip', subdir='archivetestdata')
3477 t = io.StringIO()
3478 with zipfile.ZipFile(zip_name, 'r') as tf:
3479 tf.printdir(t)
3480 expected = t.getvalue().encode('ascii', 'backslashreplace')
3481 for opt in '-l', '--list':
3482 out = self.zipfilecmd(opt, zip_name,
3483 PYTHONIOENCODING='ascii:backslashreplace')
3484 self.assertEqual(out, expected)
3485
3486 @requires_zlib()
3487 def test_create_command(self):
3488 self.addCleanup(unlink, TESTFN)
3489 with open(TESTFN, 'w', encoding='utf-8') as f:
3490 f.write('test 1')
3491 os.mkdir(TESTFNDIR)
3492 self.addCleanup(rmtree, TESTFNDIR)
3493 with open(os.path.join(TESTFNDIR, 'file.txt'), 'w', encoding='utf-8') as f:
3494 f.write('test 2')
3495 files = [TESTFN, TESTFNDIR]
3496 namelist = [TESTFN, TESTFNDIR + '/', TESTFNDIR + '/file.txt']
3497 for opt in '-c', '--create':
3498 try:
3499 out = self.zipfilecmd(opt, TESTFN2, *files)
3500 self.assertEqual(out, b'')
3501 with zipfile.ZipFile(TESTFN2) as zf:
3502 self.assertEqual(zf.namelist(), namelist)
3503 self.assertEqual(zf.read(namelist[0]), b'test 1')
3504 self.assertEqual(zf.read(namelist[2]), b'test 2')
3505 finally:
3506 unlink(TESTFN2)
3507
3508 def test_extract_command(self):
3509 zip_name = findfile('zipdir.zip', subdir='archivetestdata')
3510 for opt in '-e', '--extract':
3511 with temp_dir() as extdir:
3512 out = self.zipfilecmd(opt, zip_name, extdir)
3513 self.assertEqual(out, b'')
3514 with zipfile.ZipFile(zip_name) as zf:
3515 for zi in zf.infolist():
3516 path = os.path.join(extdir,
3517 zi.filename.replace('/', os.sep))
3518 if zi.is_dir():
3519 self.assertTrue(os.path.isdir(path))
3520 else:
3521 self.assertTrue(os.path.isfile(path))
3522 with open(path, 'rb') as f:
3523 self.assertEqual(f.read(), zf.read(zi))
3524
3525
3526 class TestExecutablePrependedZip(unittest.TestCase):
3527 """Test our ability to open zip files with an executable prepended."""
3528
3529 def setUp(self):
3530 self.exe_zip = findfile('exe_with_zip', subdir='archivetestdata')
3531 self.exe_zip64 = findfile('exe_with_z64', subdir='archivetestdata')
3532
3533 def _test_zip_works(self, name):
3534 # bpo28494 sanity check: ensure is_zipfile works on these.
3535 self.assertTrue(zipfile.is_zipfile(name),
3536 f'is_zipfile failed on {name}')
3537 # Ensure we can operate on these via ZipFile.
3538 with zipfile.ZipFile(name) as zipfp:
3539 for n in zipfp.namelist():
3540 data = zipfp.read(n)
3541 self.assertIn(b'FAVORITE_NUMBER', data)
3542
3543 def test_read_zip_with_exe_prepended(self):
3544 self._test_zip_works(self.exe_zip)
3545
3546 def test_read_zip64_with_exe_prepended(self):
3547 self._test_zip_works(self.exe_zip64)
3548
3549 @unittest.skipUnless(sys.executable, 'sys.executable required.')
3550 @unittest.skipUnless(os.access('/bin/bash', os.X_OK),
3551 'Test relies on #!/bin/bash working.')
3552 @requires_subprocess()
3553 def test_execute_zip2(self):
3554 output = subprocess.check_output([self.exe_zip, sys.executable])
3555 self.assertIn(b'number in executable: 5', output)
3556
3557 @unittest.skipUnless(sys.executable, 'sys.executable required.')
3558 @unittest.skipUnless(os.access('/bin/bash', os.X_OK),
3559 'Test relies on #!/bin/bash working.')
3560 @requires_subprocess()
3561 def test_execute_zip64(self):
3562 output = subprocess.check_output([self.exe_zip64, sys.executable])
3563 self.assertIn(b'number in executable: 5', output)
3564
3565
3566 class EncodedMetadataTests(unittest.TestCase):
3567 file_names = ['\u4e00', '\u4e8c', '\u4e09'] # Han 'one', 'two', 'three'
3568 file_content = [
3569 "This is pure ASCII.\n".encode('ascii'),
3570 # This is modern Japanese. (UTF-8)
3571 "\u3053\u308c\u306f\u73fe\u4ee3\u7684\u65e5\u672c\u8a9e\u3067\u3059\u3002\n".encode('utf-8'),
3572 # This is obsolete Japanese. (Shift JIS)
3573 "\u3053\u308c\u306f\u53e4\u3044\u65e5\u672c\u8a9e\u3067\u3059\u3002\n".encode('shift_jis'),
3574 ]
3575
3576 def setUp(self):
3577 self.addCleanup(unlink, TESTFN)
3578 # Create .zip of 3 members with Han names encoded in Shift JIS.
3579 # Each name is 1 Han character encoding to 2 bytes in Shift JIS.
3580 # The ASCII names are arbitrary as long as they are length 2 and
3581 # not otherwise contained in the zip file.
3582 # Data elements are encoded bytes (ascii, utf-8, shift_jis).
3583 placeholders = ["n1", "n2"] + self.file_names[2:]
3584 with zipfile.ZipFile(TESTFN, mode="w") as tf:
3585 for temp, content in zip(placeholders, self.file_content):
3586 tf.writestr(temp, content, zipfile.ZIP_STORED)
3587 # Hack in the Shift JIS names with flag bit 11 (UTF-8) unset.
3588 with open(TESTFN, "rb") as tf:
3589 data = tf.read()
3590 for name, temp in zip(self.file_names, placeholders[:2]):
3591 data = data.replace(temp.encode('ascii'),
3592 name.encode('shift_jis'))
3593 with open(TESTFN, "wb") as tf:
3594 tf.write(data)
3595
3596 def _test_read(self, zipfp, expected_names, expected_content):
3597 # Check the namelist
3598 names = zipfp.namelist()
3599 self.assertEqual(sorted(names), sorted(expected_names))
3600
3601 # Check infolist
3602 infos = zipfp.infolist()
3603 names = [zi.filename for zi in infos]
3604 self.assertEqual(sorted(names), sorted(expected_names))
3605
3606 # check getinfo
3607 for name, content in zip(expected_names, expected_content):
3608 info = zipfp.getinfo(name)
3609 self.assertEqual(info.filename, name)
3610 self.assertEqual(info.file_size, len(content))
3611 self.assertEqual(zipfp.read(name), content)
3612
3613 def test_read_with_metadata_encoding(self):
3614 # Read the ZIP archive with correct metadata_encoding
3615 with zipfile.ZipFile(TESTFN, "r", metadata_encoding='shift_jis') as zipfp:
3616 self._test_read(zipfp, self.file_names, self.file_content)
3617
3618 def test_read_without_metadata_encoding(self):
3619 # Read the ZIP archive without metadata_encoding
3620 expected_names = [name.encode('shift_jis').decode('cp437')
3621 for name in self.file_names[:2]] + self.file_names[2:]
3622 with zipfile.ZipFile(TESTFN, "r") as zipfp:
3623 self._test_read(zipfp, expected_names, self.file_content)
3624
3625 def test_read_with_incorrect_metadata_encoding(self):
3626 # Read the ZIP archive with incorrect metadata_encoding
3627 expected_names = [name.encode('shift_jis').decode('koi8-u')
3628 for name in self.file_names[:2]] + self.file_names[2:]
3629 with zipfile.ZipFile(TESTFN, "r", metadata_encoding='koi8-u') as zipfp:
3630 self._test_read(zipfp, expected_names, self.file_content)
3631
3632 def test_read_with_unsuitable_metadata_encoding(self):
3633 # Read the ZIP archive with metadata_encoding unsuitable for
3634 # decoding metadata
3635 with self.assertRaises(UnicodeDecodeError):
3636 zipfile.ZipFile(TESTFN, "r", metadata_encoding='ascii')
3637 with self.assertRaises(UnicodeDecodeError):
3638 zipfile.ZipFile(TESTFN, "r", metadata_encoding='utf-8')
3639
3640 def test_read_after_append(self):
3641 newname = '\u56db' # Han 'four'
3642 expected_names = [name.encode('shift_jis').decode('cp437')
3643 for name in self.file_names[:2]] + self.file_names[2:]
3644 expected_names.append(newname)
3645 expected_content = (*self.file_content, b"newcontent")
3646
3647 with zipfile.ZipFile(TESTFN, "a") as zipfp:
3648 zipfp.writestr(newname, "newcontent")
3649 self.assertEqual(sorted(zipfp.namelist()), sorted(expected_names))
3650
3651 with zipfile.ZipFile(TESTFN, "r") as zipfp:
3652 self._test_read(zipfp, expected_names, expected_content)
3653
3654 with zipfile.ZipFile(TESTFN, "r", metadata_encoding='shift_jis') as zipfp:
3655 self.assertEqual(sorted(zipfp.namelist()), sorted(expected_names))
3656 for i, (name, content) in enumerate(zip(expected_names, expected_content)):
3657 info = zipfp.getinfo(name)
3658 self.assertEqual(info.filename, name)
3659 self.assertEqual(info.file_size, len(content))
3660 if i < 2:
3661 with self.assertRaises(zipfile.BadZipFile):
3662 zipfp.read(name)
3663 else:
3664 self.assertEqual(zipfp.read(name), content)
3665
3666 def test_write_with_metadata_encoding(self):
3667 ZF = zipfile.ZipFile
3668 for mode in ("w", "x", "a"):
3669 with self.assertRaisesRegex(ValueError,
3670 "^metadata_encoding is only"):
3671 ZF("nonesuch.zip", mode, metadata_encoding="shift_jis")
3672
3673 def test_cli_with_metadata_encoding(self):
3674 errmsg = "Non-conforming encodings not supported with -c."
3675 args = ["--metadata-encoding=shift_jis", "-c", "nonesuch", "nonesuch"]
3676 with captured_stdout() as stdout:
3677 with captured_stderr() as stderr:
3678 self.assertRaises(SystemExit, zipfile.main, args)
3679 self.assertEqual(stdout.getvalue(), "")
3680 self.assertIn(errmsg, stderr.getvalue())
3681
3682 with captured_stdout() as stdout:
3683 zipfile.main(["--metadata-encoding=shift_jis", "-t", TESTFN])
3684 listing = stdout.getvalue()
3685
3686 with captured_stdout() as stdout:
3687 zipfile.main(["--metadata-encoding=shift_jis", "-l", TESTFN])
3688 listing = stdout.getvalue()
3689 for name in self.file_names:
3690 self.assertIn(name, listing)
3691
3692 def test_cli_with_metadata_encoding_extract(self):
3693 os.mkdir(TESTFN2)
3694 self.addCleanup(rmtree, TESTFN2)
3695 # Depending on locale, extracted file names can be not encodable
3696 # with the filesystem encoding.
3697 for fn in self.file_names:
3698 try:
3699 os.stat(os.path.join(TESTFN2, fn))
3700 except OSError:
3701 pass
3702 except UnicodeEncodeError:
3703 self.skipTest(f'cannot encode file name {fn!a}')
3704
3705 zipfile.main(["--metadata-encoding=shift_jis", "-e", TESTFN, TESTFN2])
3706 listing = os.listdir(TESTFN2)
3707 for name in self.file_names:
3708 self.assertIn(name, listing)
3709
3710
3711 class StripExtraTests(unittest.TestCase):
3712 # Note: all of the "z" characters are technically invalid, but up
3713 # to 3 bytes at the end of the extra will be passed through as they
3714 # are too short to encode a valid extra.
3715
3716 ZIP64_EXTRA = 1
3717
3718 def test_no_data(self):
3719 s = struct.Struct("<HH")
3720 a = s.pack(self.ZIP64_EXTRA, 0)
3721 b = s.pack(2, 0)
3722 c = s.pack(3, 0)
3723
3724 self.assertEqual(b'', zipfile._Extra.strip(a, (self.ZIP64_EXTRA,)))
3725 self.assertEqual(b, zipfile._Extra.strip(b, (self.ZIP64_EXTRA,)))
3726 self.assertEqual(
3727 b+b"z", zipfile._Extra.strip(b+b"z", (self.ZIP64_EXTRA,)))
3728
3729 self.assertEqual(b+c, zipfile._Extra.strip(a+b+c, (self.ZIP64_EXTRA,)))
3730 self.assertEqual(b+c, zipfile._Extra.strip(b+a+c, (self.ZIP64_EXTRA,)))
3731 self.assertEqual(b+c, zipfile._Extra.strip(b+c+a, (self.ZIP64_EXTRA,)))
3732
3733 def test_with_data(self):
3734 s = struct.Struct("<HH")
3735 a = s.pack(self.ZIP64_EXTRA, 1) + b"a"
3736 b = s.pack(2, 2) + b"bb"
3737 c = s.pack(3, 3) + b"ccc"
3738
3739 self.assertEqual(b"", zipfile._Extra.strip(a, (self.ZIP64_EXTRA,)))
3740 self.assertEqual(b, zipfile._Extra.strip(b, (self.ZIP64_EXTRA,)))
3741 self.assertEqual(
3742 b+b"z", zipfile._Extra.strip(b+b"z", (self.ZIP64_EXTRA,)))
3743
3744 self.assertEqual(b+c, zipfile._Extra.strip(a+b+c, (self.ZIP64_EXTRA,)))
3745 self.assertEqual(b+c, zipfile._Extra.strip(b+a+c, (self.ZIP64_EXTRA,)))
3746 self.assertEqual(b+c, zipfile._Extra.strip(b+c+a, (self.ZIP64_EXTRA,)))
3747
3748 def test_multiples(self):
3749 s = struct.Struct("<HH")
3750 a = s.pack(self.ZIP64_EXTRA, 1) + b"a"
3751 b = s.pack(2, 2) + b"bb"
3752
3753 self.assertEqual(b"", zipfile._Extra.strip(a+a, (self.ZIP64_EXTRA,)))
3754 self.assertEqual(b"", zipfile._Extra.strip(a+a+a, (self.ZIP64_EXTRA,)))
3755 self.assertEqual(
3756 b"z", zipfile._Extra.strip(a+a+b"z", (self.ZIP64_EXTRA,)))
3757 self.assertEqual(
3758 b+b"z", zipfile._Extra.strip(a+a+b+b"z", (self.ZIP64_EXTRA,)))
3759
3760 self.assertEqual(b, zipfile._Extra.strip(a+a+b, (self.ZIP64_EXTRA,)))
3761 self.assertEqual(b, zipfile._Extra.strip(a+b+a, (self.ZIP64_EXTRA,)))
3762 self.assertEqual(b, zipfile._Extra.strip(b+a+a, (self.ZIP64_EXTRA,)))
3763
3764 def test_too_short(self):
3765 self.assertEqual(b"", zipfile._Extra.strip(b"", (self.ZIP64_EXTRA,)))
3766 self.assertEqual(b"z", zipfile._Extra.strip(b"z", (self.ZIP64_EXTRA,)))
3767 self.assertEqual(
3768 b"zz", zipfile._Extra.strip(b"zz", (self.ZIP64_EXTRA,)))
3769 self.assertEqual(
3770 b"zzz", zipfile._Extra.strip(b"zzz", (self.ZIP64_EXTRA,)))
3771
3772
3773 class StatIO(_pyio.BytesIO):
3774 """Buffer which remembers the number of bytes that were read."""
3775
3776 def __init__(self):
3777 super().__init__()
3778 self.bytes_read = 0
3779
3780 def read(self, size=-1):
3781 bs = super().read(size)
3782 self.bytes_read += len(bs)
3783 return bs
3784
3785
3786 class StoredZipExtFileRandomReadTest(unittest.TestCase):
3787 """Tests whether an uncompressed, unencrypted zip entry can be randomly
3788 seek and read without reading redundant bytes."""
3789 def test_stored_seek_and_read(self):
3790
3791 sio = StatIO()
3792 # 20000 bytes
3793 txt = b'0123456789' * 2000
3794
3795 # The seek length must be greater than ZipExtFile.MIN_READ_SIZE
3796 # as `ZipExtFile._read2()` reads in blocks of this size and we
3797 # need to seek out of the buffered data
3798 read_buffer_size = zipfile.ZipExtFile.MIN_READ_SIZE
3799 self.assertGreaterEqual(10002, read_buffer_size) # for forward seek test
3800 self.assertGreaterEqual(5003, read_buffer_size) # for backward seek test
3801 # The read length must be less than MIN_READ_SIZE, since we assume that
3802 # only 1 block is read in the test.
3803 read_length = 100
3804 self.assertGreaterEqual(read_buffer_size, read_length) # for read() calls
3805
3806 with zipfile.ZipFile(sio, "w", compression=zipfile.ZIP_STORED) as zipf:
3807 zipf.writestr("foo.txt", txt)
3808
3809 # check random seek and read on a file
3810 with zipfile.ZipFile(sio, "r") as zipf:
3811 with zipf.open("foo.txt", "r") as fp:
3812 # Test this optimized read hasn't rewound and read from the
3813 # start of the file (as in the case of the unoptimized path)
3814
3815 # forward seek
3816 old_count = sio.bytes_read
3817 forward_seek_len = 10002
3818 current_pos = 0
3819 fp.seek(forward_seek_len, os.SEEK_CUR)
3820 current_pos += forward_seek_len
3821 self.assertEqual(fp.tell(), current_pos)
3822 self.assertEqual(fp._left, fp._compress_left)
3823 arr = fp.read(read_length)
3824 current_pos += read_length
3825 self.assertEqual(fp.tell(), current_pos)
3826 self.assertEqual(arr, txt[current_pos - read_length:current_pos])
3827 self.assertEqual(fp._left, fp._compress_left)
3828 read_count = sio.bytes_read - old_count
3829 self.assertLessEqual(read_count, read_buffer_size)
3830
3831 # backward seek
3832 old_count = sio.bytes_read
3833 backward_seek_len = 5003
3834 fp.seek(-backward_seek_len, os.SEEK_CUR)
3835 current_pos -= backward_seek_len
3836 self.assertEqual(fp.tell(), current_pos)
3837 self.assertEqual(fp._left, fp._compress_left)
3838 arr = fp.read(read_length)
3839 current_pos += read_length
3840 self.assertEqual(fp.tell(), current_pos)
3841 self.assertEqual(arr, txt[current_pos - read_length:current_pos])
3842 self.assertEqual(fp._left, fp._compress_left)
3843 read_count = sio.bytes_read - old_count
3844 self.assertLessEqual(read_count, read_buffer_size)
3845
3846 # eof flags test
3847 fp.seek(0, os.SEEK_END)
3848 fp.seek(12345, os.SEEK_SET)
3849 current_pos = 12345
3850 arr = fp.read(read_length)
3851 current_pos += read_length
3852 self.assertEqual(arr, txt[current_pos - read_length:current_pos])
3853
3854
3855 if __name__ == "__main__":
3856 unittest.main()