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 expected_utc = (2025, 1, 1, 7, 19, 58)
1890 self.assertEqual(zip_info.date_time, expected_utc)
1891
1892 def test_write_without_source_date_epoch(self):
1893 with os_helper.EnvironmentVarGuard() as env:
1894 del env['SOURCE_DATE_EPOCH']
1895
1896 with zipfile.ZipFile(TESTFN, "w") as zf:
1897 zf.writestr("test_no_source_date_epoch.txt", "Testing without SOURCE_DATE_EPOCH")
1898
1899 with zipfile.ZipFile(TESTFN, "r") as zf:
1900 zip_info = zf.getinfo("test_no_source_date_epoch.txt")
1901 self.assertTimestampAlmostEqual(time.localtime(), zip_info.date_time, tolerance=2)
1902
1903 def assertTimestampAlmostEqual(self, time1, time2, tolerance):
1904 import datetime
1905 dt1 = datetime.datetime(*time1[:6])
1906 dt2 = datetime.datetime(*time2[:6])
1907 self.assertLessEqual((dt1 - dt2).total_seconds(), tolerance)
1908
1909 def test_close(self):
1910 """Check that the zipfile is closed after the 'with' block."""
1911 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
1912 for fpath, fdata in SMALL_TEST_DATA:
1913 zipfp.writestr(fpath, fdata)
1914 self.assertIsNotNone(zipfp.fp, 'zipfp is not open')
1915 self.assertIsNone(zipfp.fp, 'zipfp is not closed')
1916
1917 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
1918 self.assertIsNotNone(zipfp.fp, 'zipfp is not open')
1919 self.assertIsNone(zipfp.fp, 'zipfp is not closed')
1920
1921 def test_close_on_exception(self):
1922 """Check that the zipfile is closed if an exception is raised in the
1923 'with' block."""
1924 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
1925 for fpath, fdata in SMALL_TEST_DATA:
1926 zipfp.writestr(fpath, fdata)
1927
1928 try:
1929 with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
1930 raise zipfile.BadZipFile()
1931 except zipfile.BadZipFile:
1932 self.assertIsNone(zipfp2.fp, 'zipfp is not closed')
1933
1934 def test_unsupported_version(self):
1935 # File has an extract_version of 120
1936 data = (b'PK\x03\x04x\x00\x00\x00\x00\x00!p\xa1@\x00\x00\x00\x00\x00\x00'
1937 b'\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00xPK\x01\x02x\x03x\x00\x00\x00\x00'
1938 b'\x00!p\xa1@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00'
1939 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00\x00xPK\x05\x06'
1940 b'\x00\x00\x00\x00\x01\x00\x01\x00/\x00\x00\x00\x1f\x00\x00\x00\x00\x00')
1941
1942 self.assertRaises(NotImplementedError, zipfile.ZipFile,
1943 io.BytesIO(data), 'r')
1944
1945 @requires_zlib()
1946 def test_read_unicode_filenames(self):
1947 # bug #10801
1948 fname = findfile('zip_cp437_header.zip', subdir='archivetestdata')
1949 with zipfile.ZipFile(fname) as zipfp:
1950 for name in zipfp.namelist():
1951 zipfp.open(name).close()
1952
1953 def test_write_unicode_filenames(self):
1954 with zipfile.ZipFile(TESTFN, "w") as zf:
1955 zf.writestr("foo.txt", "Test for unicode filename")
1956 zf.writestr("\xf6.txt", "Test for unicode filename")
1957 self.assertIsInstance(zf.infolist()[0].filename, str)
1958
1959 with zipfile.ZipFile(TESTFN, "r") as zf:
1960 self.assertEqual(zf.filelist[0].filename, "foo.txt")
1961 self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
1962
1963 def create_zipfile_with_extra_data(self, filename, extra_data_name):
1964 with zipfile.ZipFile(TESTFN, mode='w') as zf:
1965 filename_encoded = filename.encode("utf-8")
1966 # create a ZipInfo object with Unicode path extra field
1967 zip_info = zipfile.ZipInfo(filename)
1968
1969 tag_for_unicode_path = b'\x75\x70'
1970 version_of_unicode_path = b'\x01'
1971
1972 import zlib
1973 filename_crc = struct.pack('<L', zlib.crc32(filename_encoded))
1974
1975 extra_data = version_of_unicode_path + filename_crc + extra_data_name
1976 tsize = len(extra_data).to_bytes(2, 'little')
1977
1978 zip_info.extra = tag_for_unicode_path + tsize + extra_data
1979
1980 # add the file to the ZIP archive
1981 zf.writestr(zip_info, b'Hello World!')
1982
1983 @requires_zlib()
1984 def test_read_zipfile_containing_unicode_path_extra_field(self):
1985 self.create_zipfile_with_extra_data("이름.txt", "이름.txt".encode("utf-8"))
1986 with zipfile.ZipFile(TESTFN, "r") as zf:
1987 self.assertEqual(zf.filelist[0].filename, "이름.txt")
1988
1989 @requires_zlib()
1990 def test_read_zipfile_warning(self):
1991 self.create_zipfile_with_extra_data("이름.txt", b"")
1992 with self.assertWarns(UserWarning):
1993 zipfile.ZipFile(TESTFN, "r").close()
1994
1995 @requires_zlib()
1996 def test_read_zipfile_error(self):
1997 self.create_zipfile_with_extra_data("이름.txt", b"\xff")
1998 with self.assertRaises(zipfile.BadZipfile):
1999 zipfile.ZipFile(TESTFN, "r").close()
2000
2001 def test_read_after_write_unicode_filenames(self):
2002 with zipfile.ZipFile(TESTFN2, 'w') as zipfp:
2003 zipfp.writestr('приклад', b'sample')
2004 self.assertEqual(zipfp.read('приклад'), b'sample')
2005
2006 def test_exclusive_create_zip_file(self):
2007 """Test exclusive creating a new zipfile."""
2008 unlink(TESTFN2)
2009 filename = 'testfile.txt'
2010 content = b'hello, world. this is some content.'
2011 with zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED) as zipfp:
2012 zipfp.writestr(filename, content)
2013 with self.assertRaises(FileExistsError):
2014 zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED)
2015 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
2016 self.assertEqual(zipfp.namelist(), [filename])
2017 self.assertEqual(zipfp.read(filename), content)
2018
2019 def test_create_non_existent_file_for_append(self):
2020 if os.path.exists(TESTFN):
2021 os.unlink(TESTFN)
2022
2023 filename = 'testfile.txt'
2024 content = b'hello, world. this is some content.'
2025
2026 try:
2027 with zipfile.ZipFile(TESTFN, 'a') as zf:
2028 zf.writestr(filename, content)
2029 except OSError:
2030 self.fail('Could not append data to a non-existent zip file.')
2031
2032 self.assertTrue(os.path.exists(TESTFN))
2033
2034 with zipfile.ZipFile(TESTFN, 'r') as zf:
2035 self.assertEqual(zf.read(filename), content)
2036
2037 def test_close_erroneous_file(self):
2038 # This test checks that the ZipFile constructor closes the file object
2039 # it opens if there's an error in the file. If it doesn't, the
2040 # traceback holds a reference to the ZipFile object and, indirectly,
2041 # the file object.
2042 # On Windows, this causes the os.unlink() call to fail because the
2043 # underlying file is still open. This is SF bug #412214.
2044 #
2045 with open(TESTFN, "w", encoding="utf-8") as fp:
2046 fp.write("this is not a legal zip file\n")
2047 try:
2048 zf = zipfile.ZipFile(TESTFN)
2049 except zipfile.BadZipFile:
2050 pass
2051
2052 def test_is_zip_erroneous_file(self):
2053 """Check that is_zipfile() correctly identifies non-zip files."""
2054 # - passing a filename
2055 with open(TESTFN, "w", encoding='utf-8') as fp:
2056 fp.write("this is not a legal zip file\n")
2057 self.assertFalse(zipfile.is_zipfile(TESTFN))
2058 # - passing a path-like object
2059 self.assertFalse(zipfile.is_zipfile(FakePath(TESTFN)))
2060 # - passing a file object
2061 with open(TESTFN, "rb") as fp:
2062 self.assertFalse(zipfile.is_zipfile(fp))
2063 # - passing a file-like object
2064 fp = io.BytesIO()
2065 fp.write(b"this is not a legal zip file\n")
2066 self.assertFalse(zipfile.is_zipfile(fp))
2067 fp.seek(0, 0)
2068 self.assertFalse(zipfile.is_zipfile(fp))
2069 # - passing non-zipfile with ZIP header elements
2070 # data created using pyPNG like so:
2071 # d = [(ord('P'), ord('K'), 5, 6), (ord('P'), ord('K'), 6, 6)]
2072 # w = png.Writer(1,2,alpha=True,compression=0)
2073 # f = open('onepix.png', 'wb')
2074 # w.write(f, d)
2075 # w.close()
2076 data = (b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00"
2077 b"\x00\x02\x08\x06\x00\x00\x00\x99\x81\xb6'\x00\x00\x00\x15I"
2078 b"DATx\x01\x01\n\x00\xf5\xff\x00PK\x05\x06\x00PK\x06\x06\x07"
2079 b"\xac\x01N\xc6|a\r\x00\x00\x00\x00IEND\xaeB`\x82")
2080 # - passing a filename
2081 with open(TESTFN, "wb") as fp:
2082 fp.write(data)
2083 self.assertFalse(zipfile.is_zipfile(TESTFN))
2084 # - passing a file-like object
2085 fp = io.BytesIO()
2086 fp.write(data)
2087 self.assertFalse(zipfile.is_zipfile(fp))
2088
2089 def test_damaged_zipfile(self):
2090 """Check that zipfiles with missing bytes at the end raise BadZipFile."""
2091 # - Create a valid zip file
2092 fp = io.BytesIO()
2093 with zipfile.ZipFile(fp, mode="w") as zipf:
2094 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
2095 zipfiledata = fp.getvalue()
2096
2097 # - Now create copies of it missing the last N bytes and make sure
2098 # a BadZipFile exception is raised when we try to open it
2099 for N in range(len(zipfiledata)):
2100 fp = io.BytesIO(zipfiledata[:N])
2101 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, fp)
2102
2103 def test_is_zip_valid_file(self):
2104 """Check that is_zipfile() correctly identifies zip files."""
2105 # - passing a filename
2106 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2107 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
2108
2109 self.assertTrue(zipfile.is_zipfile(TESTFN))
2110 # - passing a file object
2111 with open(TESTFN, "rb") as fp:
2112 self.assertTrue(zipfile.is_zipfile(fp))
2113 fp.seek(0, 0)
2114 zip_contents = fp.read()
2115 # - passing a file-like object
2116 fp = io.BytesIO()
2117 end = fp.write(zip_contents)
2118 self.assertEqual(fp.tell(), end)
2119 mid = end // 2
2120 fp.seek(mid, 0)
2121 self.assertTrue(zipfile.is_zipfile(fp))
2122 # check that the position is left unchanged after the call
2123 # see: https://github.com/python/cpython/issues/122356
2124 self.assertEqual(fp.tell(), mid)
2125 self.assertTrue(zipfile.is_zipfile(fp))
2126 self.assertEqual(fp.tell(), mid)
2127
2128 def test_non_existent_file_raises_OSError(self):
2129 # make sure we don't raise an AttributeError when a partially-constructed
2130 # ZipFile instance is finalized; this tests for regression on SF tracker
2131 # bug #403871.
2132
2133 # The bug we're testing for caused an AttributeError to be raised
2134 # when a ZipFile instance was created for a file that did not
2135 # exist; the .fp member was not initialized but was needed by the
2136 # __del__() method. Since the AttributeError is in the __del__(),
2137 # it is ignored, but the user should be sufficiently annoyed by
2138 # the message on the output that regression will be noticed
2139 # quickly.
2140 self.assertRaises(OSError, zipfile.ZipFile, TESTFN)
2141
2142 def test_empty_file_raises_BadZipFile(self):
2143 f = open(TESTFN, 'w', encoding='utf-8')
2144 f.close()
2145 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
2146
2147 with open(TESTFN, 'w', encoding='utf-8') as fp:
2148 fp.write("short file")
2149 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
2150
2151 def test_negative_central_directory_offset_raises_BadZipFile(self):
2152 # Zip file containing an empty EOCD record
2153 buffer = bytearray(b'PK\x05\x06' + b'\0'*18)
2154
2155 # Set the size of the central directory bytes to become 1,
2156 # causing the central directory offset to become negative
2157 for dirsize in 1, 2**32-1:
2158 buffer[12:16] = struct.pack('<L', dirsize)
2159 f = io.BytesIO(buffer)
2160 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, f)
2161
2162 def test_closed_zip_raises_ValueError(self):
2163 """Verify that testzip() doesn't swallow inappropriate exceptions."""
2164 data = io.BytesIO()
2165 with zipfile.ZipFile(data, mode="w") as zipf:
2166 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2167
2168 # This is correct; calling .read on a closed ZipFile should raise
2169 # a ValueError, and so should calling .testzip. An earlier
2170 # version of .testzip would swallow this exception (and any other)
2171 # and report that the first file in the archive was corrupt.
2172 self.assertRaises(ValueError, zipf.read, "foo.txt")
2173 self.assertRaises(ValueError, zipf.open, "foo.txt")
2174 self.assertRaises(ValueError, zipf.testzip)
2175 self.assertRaises(ValueError, zipf.writestr, "bogus.txt", "bogus")
2176 with open(TESTFN, 'w', encoding='utf-8') as f:
2177 f.write('zipfile test data')
2178 self.assertRaises(ValueError, zipf.write, TESTFN)
2179
2180 def test_bad_constructor_mode(self):
2181 """Check that bad modes passed to ZipFile constructor are caught."""
2182 self.assertRaises(ValueError, zipfile.ZipFile, TESTFN, "q")
2183
2184 def test_bad_open_mode(self):
2185 """Check that bad modes passed to ZipFile.open are caught."""
2186 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2187 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2188
2189 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
2190 # read the data to make sure the file is there
2191 zipf.read("foo.txt")
2192 self.assertRaises(ValueError, zipf.open, "foo.txt", "q")
2193 # universal newlines support is removed
2194 self.assertRaises(ValueError, zipf.open, "foo.txt", "U")
2195 self.assertRaises(ValueError, zipf.open, "foo.txt", "rU")
2196
2197 def test_read0(self):
2198 """Check that calling read(0) on a ZipExtFile object returns an empty
2199 string and doesn't advance file pointer."""
2200 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2201 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2202 # read the data to make sure the file is there
2203 with zipf.open("foo.txt") as f:
2204 for i in range(FIXEDTEST_SIZE):
2205 self.assertEqual(f.read(0), b'')
2206
2207 self.assertEqual(f.read(), b"O, for a Muse of Fire!")
2208
2209 def test_open_non_existent_item(self):
2210 """Check that attempting to call open() for an item that doesn't
2211 exist in the archive raises a RuntimeError."""
2212 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2213 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
2214
2215 def test_bad_compression_mode(self):
2216 """Check that bad compression methods passed to ZipFile.open are
2217 caught."""
2218 self.assertRaises(NotImplementedError, zipfile.ZipFile, TESTFN, "w", -1)
2219
2220 def test_unsupported_compression(self):
2221 # data is declared as shrunk, but actually deflated
2222 data = (b'PK\x03\x04.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00'
2223 b'\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00x\x03\x00PK\x01'
2224 b'\x02.\x03.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00\x00\x02\x00\x00'
2225 b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
2226 b'\x80\x01\x00\x00\x00\x00xPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00'
2227 b'/\x00\x00\x00!\x00\x00\x00\x00\x00')
2228 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2229 self.assertRaises(NotImplementedError, zipf.open, 'x')
2230
2231 def test_null_byte_in_filename(self):
2232 """Check that a filename containing a null byte is properly
2233 terminated."""
2234 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2235 zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
2236 self.assertEqual(zipf.namelist(), ['foo.txt'])
2237
2238 def test_struct_sizes(self):
2239 """Check that ZIP internal structure sizes are calculated correctly."""
2240 self.assertEqual(zipfile.sizeEndCentDir, 22)
2241 self.assertEqual(zipfile.sizeCentralDir, 46)
2242 self.assertEqual(zipfile.sizeEndCentDir64, 56)
2243 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
2244
2245 def test_comments(self):
2246 """Check that comments on the archive are handled properly."""
2247
2248 # check default comment is empty
2249 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2250 self.assertEqual(zipf.comment, b'')
2251 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2252
2253 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
2254 self.assertEqual(zipfr.comment, b'')
2255
2256 # check a simple short comment
2257 comment = b'Bravely taking to his feet, he beat a very brave retreat.'
2258 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2259 zipf.comment = comment
2260 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2261 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
2262 self.assertEqual(zipf.comment, comment)
2263
2264 # check a comment of max length
2265 comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)])
2266 comment2 = comment2.encode("ascii")
2267 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2268 zipf.comment = comment2
2269 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2270
2271 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
2272 self.assertEqual(zipfr.comment, comment2)
2273
2274 # check a comment that is too long is truncated
2275 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
2276 with self.assertWarns(UserWarning):
2277 zipf.comment = comment2 + b'oops'
2278 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2279 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
2280 self.assertEqual(zipfr.comment, comment2)
2281
2282 # check that comments are correctly modified in append mode
2283 with zipfile.ZipFile(TESTFN,mode="w") as zipf:
2284 zipf.comment = b"original comment"
2285 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2286 with zipfile.ZipFile(TESTFN,mode="a") as zipf:
2287 zipf.comment = b"an updated comment"
2288 with zipfile.ZipFile(TESTFN,mode="r") as zipf:
2289 self.assertEqual(zipf.comment, b"an updated comment")
2290
2291 # check that comments are correctly shortened in append mode
2292 # and the file is indeed truncated
2293 with zipfile.ZipFile(TESTFN,mode="w") as zipf:
2294 zipf.comment = b"original comment that's longer"
2295 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2296 original_zip_size = os.path.getsize(TESTFN)
2297 with zipfile.ZipFile(TESTFN,mode="a") as zipf:
2298 zipf.comment = b"shorter comment"
2299 self.assertTrue(original_zip_size > os.path.getsize(TESTFN))
2300 with zipfile.ZipFile(TESTFN,mode="r") as zipf:
2301 self.assertEqual(zipf.comment, b"shorter comment")
2302
2303 def test_unicode_comment(self):
2304 with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
2305 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2306 with self.assertRaises(TypeError):
2307 zipf.comment = "this is an error"
2308
2309 def test_change_comment_in_empty_archive(self):
2310 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
2311 self.assertFalse(zipf.filelist)
2312 zipf.comment = b"this is a comment"
2313 with zipfile.ZipFile(TESTFN, "r") as zipf:
2314 self.assertEqual(zipf.comment, b"this is a comment")
2315
2316 def test_change_comment_in_nonempty_archive(self):
2317 with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
2318 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
2319 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
2320 self.assertTrue(zipf.filelist)
2321 zipf.comment = b"this is a comment"
2322 with zipfile.ZipFile(TESTFN, "r") as zipf:
2323 self.assertEqual(zipf.comment, b"this is a comment")
2324
2325 def test_empty_zipfile(self):
2326 # Check that creating a file in 'w' or 'a' mode and closing without
2327 # adding any files to the archives creates a valid empty ZIP file
2328 zipf = zipfile.ZipFile(TESTFN, mode="w")
2329 zipf.close()
2330 try:
2331 zipf = zipfile.ZipFile(TESTFN, mode="r")
2332 except zipfile.BadZipFile:
2333 self.fail("Unable to create empty ZIP file in 'w' mode")
2334 zipf.close()
2335
2336 zipf = zipfile.ZipFile(TESTFN, mode="a")
2337 zipf.close()
2338 try:
2339 zipf = zipfile.ZipFile(TESTFN, mode="r")
2340 except:
2341 self.fail("Unable to create empty ZIP file in 'a' mode")
2342 zipf.close()
2343
2344 def test_open_empty_file(self):
2345 # Issue 1710703: Check that opening a file with less than 22 bytes
2346 # raises a BadZipFile exception (rather than the previously unhelpful
2347 # OSError)
2348 f = open(TESTFN, 'w', encoding='utf-8')
2349 f.close()
2350 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r')
2351
2352 def test_create_zipinfo_before_1980(self):
2353 self.assertRaises(ValueError,
2354 zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0))
2355
2356 def test_create_empty_zipinfo_repr(self):
2357 """Before bpo-26185, repr() on empty ZipInfo object was failing."""
2358 zi = zipfile.ZipInfo(filename="empty")
2359 self.assertEqual(repr(zi), "<ZipInfo filename='empty' file_size=0>")
2360
2361 def test_for_archive(self):
2362 base_filename = TESTFN2.rstrip('/')
2363
2364 with zipfile.ZipFile(TESTFN, mode="w", compresslevel=1,
2365 compression=zipfile.ZIP_STORED) as zf:
2366 # no trailing forward slash
2367 zi = zipfile.ZipInfo(base_filename)._for_archive(zf)
2368 self.assertEqual(zi.compress_level, 1)
2369 self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
2370 # ?rw- --- ---
2371 filemode = stat.S_IRUSR | stat.S_IWUSR
2372 # filemode is stored as the highest 16 bits of external_attr
2373 self.assertEqual(zi.external_attr >> 16, filemode)
2374 self.assertEqual(zi.external_attr & 0xFF, 0) # no MS-DOS flag
2375
2376 with zipfile.ZipFile(TESTFN, mode="w", compresslevel=1,
2377 compression=zipfile.ZIP_STORED) as zf:
2378 # with a trailing slash
2379 zi = zipfile.ZipInfo(f'{base_filename}/')._for_archive(zf)
2380 self.assertEqual(zi.compress_level, 1)
2381 self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
2382 # d rwx rwx r-x
2383 filemode = stat.S_IFDIR
2384 filemode |= stat.S_IRWXU | stat.S_IRWXG
2385 filemode |= stat.S_IROTH | stat.S_IXOTH
2386 self.assertEqual(zi.external_attr >> 16, filemode)
2387 self.assertEqual(zi.external_attr & 0xFF, 0x10) # MS-DOS flag
2388
2389 def test_create_empty_zipinfo_default_attributes(self):
2390 """Ensure all required attributes are set."""
2391 zi = zipfile.ZipInfo()
2392 self.assertEqual(zi.orig_filename, "NoName")
2393 self.assertEqual(zi.filename, "NoName")
2394 self.assertEqual(zi.date_time, (1980, 1, 1, 0, 0, 0))
2395 self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
2396 self.assertEqual(zi.comment, b"")
2397 self.assertEqual(zi.extra, b"")
2398 self.assertIn(zi.create_system, (0, 3))
2399 self.assertEqual(zi.create_version, zipfile.DEFAULT_VERSION)
2400 self.assertEqual(zi.extract_version, zipfile.DEFAULT_VERSION)
2401 self.assertEqual(zi.reserved, 0)
2402 self.assertEqual(zi.flag_bits, 0)
2403 self.assertEqual(zi.volume, 0)
2404 self.assertEqual(zi.internal_attr, 0)
2405 self.assertEqual(zi.external_attr, 0)
2406
2407 # Before bpo-26185, both were missing
2408 self.assertEqual(zi.file_size, 0)
2409 self.assertEqual(zi.compress_size, 0)
2410
2411 def test_zipfile_with_short_extra_field(self):
2412 """If an extra field in the header is less than 4 bytes, skip it."""
2413 zipdata = (
2414 b'PK\x03\x04\x14\x00\x00\x00\x00\x00\x93\x9b\xad@\x8b\x9e'
2415 b'\xd9\xd3\x01\x00\x00\x00\x01\x00\x00\x00\x03\x00\x03\x00ab'
2416 b'c\x00\x00\x00APK\x01\x02\x14\x03\x14\x00\x00\x00\x00'
2417 b'\x00\x93\x9b\xad@\x8b\x9e\xd9\xd3\x01\x00\x00\x00\x01\x00\x00'
2418 b'\x00\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00'
2419 b'\x00\x00\x00abc\x00\x00PK\x05\x06\x00\x00\x00\x00'
2420 b'\x01\x00\x01\x003\x00\x00\x00%\x00\x00\x00\x00\x00'
2421 )
2422 with zipfile.ZipFile(io.BytesIO(zipdata), 'r') as zipf:
2423 # testzip returns the name of the first corrupt file, or None
2424 self.assertIsNone(zipf.testzip())
2425
2426 def test_open_conflicting_handles(self):
2427 # It's only possible to open one writable file handle at a time
2428 msg1 = b"It's fun to charter an accountant!"
2429 msg2 = b"And sail the wide accountant sea"
2430 msg3 = b"To find, explore the funds offshore"
2431 with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipf:
2432 with zipf.open('foo', mode='w') as w2:
2433 w2.write(msg1)
2434 with zipf.open('bar', mode='w') as w1:
2435 with self.assertRaises(ValueError):
2436 zipf.open('handle', mode='w')
2437 with self.assertRaises(ValueError):
2438 zipf.open('foo', mode='r')
2439 with self.assertRaises(ValueError):
2440 zipf.writestr('str', 'abcde')
2441 with self.assertRaises(ValueError):
2442 zipf.write(__file__, 'file')
2443 with self.assertRaises(ValueError):
2444 zipf.close()
2445 w1.write(msg2)
2446 with zipf.open('baz', mode='w') as w2:
2447 w2.write(msg3)
2448
2449 with zipfile.ZipFile(TESTFN2, 'r') as zipf:
2450 self.assertEqual(zipf.read('foo'), msg1)
2451 self.assertEqual(zipf.read('bar'), msg2)
2452 self.assertEqual(zipf.read('baz'), msg3)
2453 self.assertEqual(zipf.namelist(), ['foo', 'bar', 'baz'])
2454
2455 def test_seek_tell(self):
2456 # Test seek functionality
2457 txt = b"Where's Bruce?"
2458 bloc = txt.find(b"Bruce")
2459 # Check seek on a file
2460 with zipfile.ZipFile(TESTFN, "w") as zipf:
2461 zipf.writestr("foo.txt", txt)
2462 with zipfile.ZipFile(TESTFN, "r") as zipf:
2463 with zipf.open("foo.txt", "r") as fp:
2464 fp.seek(bloc, os.SEEK_SET)
2465 self.assertEqual(fp.tell(), bloc)
2466 fp.seek(-bloc, os.SEEK_CUR)
2467 self.assertEqual(fp.tell(), 0)
2468 fp.seek(bloc, os.SEEK_CUR)
2469 self.assertEqual(fp.tell(), bloc)
2470 self.assertEqual(fp.read(5), txt[bloc:bloc+5])
2471 self.assertEqual(fp.tell(), bloc + 5)
2472 fp.seek(0, os.SEEK_END)
2473 self.assertEqual(fp.tell(), len(txt))
2474 fp.seek(0, os.SEEK_SET)
2475 self.assertEqual(fp.tell(), 0)
2476 # Check seek on memory file
2477 data = io.BytesIO()
2478 with zipfile.ZipFile(data, mode="w") as zipf:
2479 zipf.writestr("foo.txt", txt)
2480 with zipfile.ZipFile(data, mode="r") as zipf:
2481 with zipf.open("foo.txt", "r") as fp:
2482 fp.seek(bloc, os.SEEK_SET)
2483 self.assertEqual(fp.tell(), bloc)
2484 fp.seek(-bloc, os.SEEK_CUR)
2485 self.assertEqual(fp.tell(), 0)
2486 fp.seek(bloc, os.SEEK_CUR)
2487 self.assertEqual(fp.tell(), bloc)
2488 self.assertEqual(fp.read(5), txt[bloc:bloc+5])
2489 self.assertEqual(fp.tell(), bloc + 5)
2490 fp.seek(0, os.SEEK_END)
2491 self.assertEqual(fp.tell(), len(txt))
2492 fp.seek(0, os.SEEK_SET)
2493 self.assertEqual(fp.tell(), 0)
2494
2495 def test_read_after_seek(self):
2496 # Issue 102956: Make sure seek(x, os.SEEK_CUR) doesn't break read()
2497 txt = b"Charge men!"
2498 bloc = txt.find(b"men")
2499 with zipfile.ZipFile(TESTFN, "w") as zipf:
2500 zipf.writestr("foo.txt", txt)
2501 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
2502 with zipf.open("foo.txt", "r") as fp:
2503 fp.seek(bloc, os.SEEK_CUR)
2504 self.assertEqual(fp.read(-1), b'men!')
2505 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
2506 with zipf.open("foo.txt", "r") as fp:
2507 fp.read(6)
2508 fp.seek(1, os.SEEK_CUR)
2509 self.assertEqual(fp.read(-1), b'men!')
2510
2511 def test_uncompressed_interleaved_seek_read(self):
2512 # gh-127847: Make sure the position in the archive is correct
2513 # in the special case of seeking in a ZIP_STORED entry.
2514 with zipfile.ZipFile(TESTFN, "w") as zipf:
2515 zipf.writestr("a.txt", "123")
2516 zipf.writestr("b.txt", "456")
2517 with zipfile.ZipFile(TESTFN, "r") as zipf:
2518 with zipf.open("a.txt", "r") as a, zipf.open("b.txt", "r") as b:
2519 self.assertEqual(a.read(1), b"1")
2520 self.assertEqual(b.seek(1), 1)
2521 self.assertEqual(b.read(1), b"5")
2522
2523 @requires_bz2()
2524 def test_decompress_without_3rd_party_library(self):
2525 data = b'PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
2526 zip_file = io.BytesIO(data)
2527 with zipfile.ZipFile(zip_file, 'w', compression=zipfile.ZIP_BZIP2) as zf:
2528 zf.writestr('a.txt', b'a')
2529 with mock.patch('zipfile.bz2', None):
2530 with zipfile.ZipFile(zip_file) as zf:
2531 self.assertRaises(RuntimeError, zf.extract, 'a.txt')
2532
2533 @requires_zlib()
2534 def test_full_overlap_different_names(self):
2535 # The ZIP file contains two central directory entries with
2536 # different names which refer to the same local header.
2537 # The name of the local header matches the name of the first
2538 # central directory entry.
2539 data = (
2540 b'PK\x03\x04\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2541 b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00b\xed'
2542 b'\xc0\x81\x08\x00\x00\x00\xc00\xd6\xfbK\\d\x0b`P'
2543 b'K\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2'
2544 b'\x1e8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00'
2545 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00aPK'
2546 b'\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2547 b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00\x00'
2548 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00bPK\x05'
2549 b'\x06\x00\x00\x00\x00\x02\x00\x02\x00^\x00\x00\x00/\x00\x00'
2550 b'\x00\x00\x00'
2551 )
2552 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2553 self.assertEqual(zipf.namelist(), ['a', 'b'])
2554 zi = zipf.getinfo('a')
2555 self.assertEqual(zi.header_offset, 0)
2556 self.assertEqual(zi.compress_size, 16)
2557 self.assertEqual(zi.file_size, 1033)
2558 zi = zipf.getinfo('b')
2559 self.assertEqual(zi.header_offset, 0)
2560 self.assertEqual(zi.compress_size, 16)
2561 self.assertEqual(zi.file_size, 1033)
2562 self.assertEqual(len(zipf.read('b')), 1033)
2563 with self.assertRaisesRegex(zipfile.BadZipFile, 'File name.*differ'):
2564 zipf.read('a')
2565
2566 @requires_zlib()
2567 def test_full_overlap_different_names2(self):
2568 # The ZIP file contains two central directory entries with
2569 # different names which refer to the same local header.
2570 # The name of the local header matches the name of the second
2571 # central directory entry.
2572 data = (
2573 b'PK\x03\x04\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2574 b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00a\xed'
2575 b'\xc0\x81\x08\x00\x00\x00\xc00\xd6\xfbK\\d\x0b`P'
2576 b'K\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2'
2577 b'\x1e8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00'
2578 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00aPK'
2579 b'\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2580 b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00\x00'
2581 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00bPK\x05'
2582 b'\x06\x00\x00\x00\x00\x02\x00\x02\x00^\x00\x00\x00/\x00\x00'
2583 b'\x00\x00\x00'
2584 )
2585 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2586 self.assertEqual(zipf.namelist(), ['a', 'b'])
2587 zi = zipf.getinfo('a')
2588 self.assertEqual(zi.header_offset, 0)
2589 self.assertEqual(zi.compress_size, 16)
2590 self.assertEqual(zi.file_size, 1033)
2591 zi = zipf.getinfo('b')
2592 self.assertEqual(zi.header_offset, 0)
2593 self.assertEqual(zi.compress_size, 16)
2594 self.assertEqual(zi.file_size, 1033)
2595 with self.assertRaisesRegex(zipfile.BadZipFile, 'File name.*differ'):
2596 zipf.read('b')
2597 with self.assertWarnsRegex(UserWarning, 'Overlapped entries') as cm:
2598 self.assertEqual(len(zipf.read('a')), 1033)
2599 self.assertEqual(cm.filename, __file__)
2600
2601 @requires_zlib()
2602 def test_full_overlap_same_name(self):
2603 # The ZIP file contains two central directory entries with
2604 # the same name which refer to the same local header.
2605 data = (
2606 b'PK\x03\x04\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2607 b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00a\xed'
2608 b'\xc0\x81\x08\x00\x00\x00\xc00\xd6\xfbK\\d\x0b`P'
2609 b'K\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2'
2610 b'\x1e8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00'
2611 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00aPK'
2612 b'\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2613 b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00\x00'
2614 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00aPK\x05'
2615 b'\x06\x00\x00\x00\x00\x02\x00\x02\x00^\x00\x00\x00/\x00\x00'
2616 b'\x00\x00\x00'
2617 )
2618 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2619 self.assertEqual(zipf.namelist(), ['a', 'a'])
2620 self.assertEqual(len(zipf.infolist()), 2)
2621 zi = zipf.getinfo('a')
2622 self.assertEqual(zi.header_offset, 0)
2623 self.assertEqual(zi.compress_size, 16)
2624 self.assertEqual(zi.file_size, 1033)
2625 self.assertEqual(len(zipf.read('a')), 1033)
2626 self.assertEqual(len(zipf.read(zi)), 1033)
2627 self.assertEqual(len(zipf.read(zipf.infolist()[1])), 1033)
2628 with self.assertWarnsRegex(UserWarning, 'Overlapped entries') as cm:
2629 self.assertEqual(len(zipf.read(zipf.infolist()[0])), 1033)
2630 self.assertEqual(cm.filename, __file__)
2631 with self.assertWarnsRegex(UserWarning, 'Overlapped entries') as cm:
2632 zipf.open(zipf.infolist()[0]).close()
2633 self.assertEqual(cm.filename, __file__)
2634
2635 @requires_zlib()
2636 def test_quoted_overlap(self):
2637 # The ZIP file contains two files. The second local header
2638 # is contained in the range of the first file.
2639 data = (
2640 b'PK\x03\x04\x14\x00\x00\x00\x08\x00\xa0lH\x05Y\xfc'
2641 b'8\x044\x00\x00\x00(\x04\x00\x00\x01\x00\x00\x00a\x00'
2642 b'\x1f\x00\xe0\xffPK\x03\x04\x14\x00\x00\x00\x08\x00\xa0l'
2643 b'H\x05\xe2\x1e8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00'
2644 b'\x00\x00b\xed\xc0\x81\x08\x00\x00\x00\xc00\xd6\xfbK\\'
2645 b'd\x0b`PK\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0'
2646 b'lH\x05Y\xfc8\x044\x00\x00\x00(\x04\x00\x00\x01'
2647 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
2648 b'\x00aPK\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0l'
2649 b'H\x05\xe2\x1e8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00'
2650 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$\x00\x00\x00'
2651 b'bPK\x05\x06\x00\x00\x00\x00\x02\x00\x02\x00^\x00\x00'
2652 b'\x00S\x00\x00\x00\x00\x00'
2653 )
2654 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2655 self.assertEqual(zipf.namelist(), ['a', 'b'])
2656 zi = zipf.getinfo('a')
2657 self.assertEqual(zi.header_offset, 0)
2658 self.assertEqual(zi.compress_size, 52)
2659 self.assertEqual(zi.file_size, 1064)
2660 zi = zipf.getinfo('b')
2661 self.assertEqual(zi.header_offset, 36)
2662 self.assertEqual(zi.compress_size, 16)
2663 self.assertEqual(zi.file_size, 1033)
2664 with self.assertRaisesRegex(zipfile.BadZipFile, 'Overlapped entries'):
2665 zipf.read('a')
2666 self.assertEqual(len(zipf.read('b')), 1033)
2667
2668 @requires_zlib()
2669 def test_overlap_with_central_dir(self):
2670 # The local header offset is equal to the central directory offset.
2671 data = (
2672 b'PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00G_|Z'
2673 b'\xe2\x1e8\xbb\x0b\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00'
2674 b'\x00\x00\x00\x00\x00\x00\x00\x00\xb4\x81\x00\x00\x00\x00aP'
2675 b'K\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00/\x00\x00\x00\x00'
2676 b'\x00\x00\x00\x00\x00'
2677 )
2678 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2679 self.assertEqual(zipf.namelist(), ['a'])
2680 self.assertEqual(len(zipf.infolist()), 1)
2681 zi = zipf.getinfo('a')
2682 self.assertEqual(zi.header_offset, 0)
2683 self.assertEqual(zi.compress_size, 11)
2684 self.assertEqual(zi.file_size, 1033)
2685 # Found central directory signature PK\x01\x02 instead of
2686 # local header signature PK\x03\x04.
2687 with self.assertRaisesRegex(zipfile.BadZipFile, 'Bad magic number'):
2688 zipf.read('a')
2689
2690 @requires_zlib()
2691 def test_overlap_with_archive_comment(self):
2692 # The local header is written after the central directory,
2693 # in the archive comment.
2694 data = (
2695 b'PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00G_|Z'
2696 b'\xe2\x1e8\xbb\x0b\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00'
2697 b'\x00\x00\x00\x00\x00\x00\x00\x00\xb4\x81E\x00\x00\x00aP'
2698 b'K\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00/\x00\x00\x00\x00'
2699 b'\x00\x00\x00*\x00'
2700 b'PK\x03\x04\x14\x00\x00\x00\x08\x00G_|Z\xe2\x1e'
2701 b'8\xbb\x0b\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00aK'
2702 b'L\x1c\x05\xa3`\x14\x8cx\x00\x00'
2703 )
2704 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2705 self.assertEqual(zipf.namelist(), ['a'])
2706 self.assertEqual(len(zipf.infolist()), 1)
2707 zi = zipf.getinfo('a')
2708 self.assertEqual(zi.header_offset, 69)
2709 self.assertEqual(zi.compress_size, 11)
2710 self.assertEqual(zi.file_size, 1033)
2711 with self.assertRaisesRegex(zipfile.BadZipFile, 'Overlapped entries'):
2712 zipf.read('a')
2713
2714 def tearDown(self):
2715 unlink(TESTFN)
2716 unlink(TESTFN2)
2717
2718
2719 class AbstractBadCrcTests:
2720 def test_testzip_with_bad_crc(self):
2721 """Tests that files with bad CRCs return their name from testzip."""
2722 zipdata = self.zip_with_bad_crc
2723
2724 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
2725 # testzip returns the name of the first corrupt file, or None
2726 self.assertEqual('afile', zipf.testzip())
2727
2728 def test_read_with_bad_crc(self):
2729 """Tests that files with bad CRCs raise a BadZipFile exception when read."""
2730 zipdata = self.zip_with_bad_crc
2731
2732 # Using ZipFile.read()
2733 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
2734 self.assertRaises(zipfile.BadZipFile, zipf.read, 'afile')
2735
2736 # Using ZipExtFile.read()
2737 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
2738 with zipf.open('afile', 'r') as corrupt_file:
2739 self.assertRaises(zipfile.BadZipFile, corrupt_file.read)
2740
2741 # Same with small reads (in order to exercise the buffering logic)
2742 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
2743 with zipf.open('afile', 'r') as corrupt_file:
2744 corrupt_file.MIN_READ_SIZE = 2
2745 with self.assertRaises(zipfile.BadZipFile):
2746 while corrupt_file.read(2):
2747 pass
2748
2749
2750 class StoredBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
2751 compression = zipfile.ZIP_STORED
2752 zip_with_bad_crc = (
2753 b'PK\003\004\024\0\0\0\0\0 \213\212;:r'
2754 b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af'
2755 b'ilehello,AworldP'
2756 b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:'
2757 b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0'
2758 b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi'
2759 b'lePK\005\006\0\0\0\0\001\0\001\0003\000'
2760 b'\0\0/\0\0\0\0\0')
2761
2762 @requires_zlib()
2763 class DeflateBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
2764 compression = zipfile.ZIP_DEFLATED
2765 zip_with_bad_crc = (
2766 b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA'
2767 b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
2768 b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0'
2769 b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n'
2770 b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05'
2771 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00'
2772 b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00'
2773 b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00')
2774
2775 @requires_bz2()
2776 class Bzip2BadCrcTests(AbstractBadCrcTests, unittest.TestCase):
2777 compression = zipfile.ZIP_BZIP2
2778 zip_with_bad_crc = (
2779 b'PK\x03\x04\x14\x03\x00\x00\x0c\x00nu\x0c=FA'
2780 b'KE8\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
2781 b'ileBZh91AY&SY\xd4\xa8\xca'
2782 b'\x7f\x00\x00\x0f\x11\x80@\x00\x06D\x90\x80 \x00 \xa5'
2783 b'P\xd9!\x03\x03\x13\x13\x13\x89\xa9\xa9\xc2u5:\x9f'
2784 b'\x8b\xb9"\x9c(HjTe?\x80PK\x01\x02\x14'
2785 b'\x03\x14\x03\x00\x00\x0c\x00nu\x0c=FAKE8'
2786 b'\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00'
2787 b'\x00 \x80\x80\x81\x00\x00\x00\x00afilePK'
2788 b'\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00\x00[\x00'
2789 b'\x00\x00\x00\x00')
2790
2791 @requires_lzma()
2792 class LzmaBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
2793 compression = zipfile.ZIP_LZMA
2794 zip_with_bad_crc = (
2795 b'PK\x03\x04\x14\x03\x00\x00\x0e\x00nu\x0c=FA'
2796 b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
2797 b'ile\t\x04\x05\x00]\x00\x00\x00\x04\x004\x19I'
2798 b'\xee\x8d\xe9\x17\x89:3`\tq!.8\x00PK'
2799 b'\x01\x02\x14\x03\x14\x03\x00\x00\x0e\x00nu\x0c=FA'
2800 b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00'
2801 b'\x00\x00\x00\x00 \x80\x80\x81\x00\x00\x00\x00afil'
2802 b'ePK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00'
2803 b'\x00>\x00\x00\x00\x00\x00')
2804
2805 @requires_zstd()
2806 class ZstdBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
2807 compression = zipfile.ZIP_ZSTANDARD
2808 zip_with_bad_crc = (
2809 b'PK\x03\x04?\x00\x00\x00]\x00\x00\x00!\x00V\xb1\x17J\x14\x00'
2810 b'\x00\x00\x0b\x00\x00\x00\x05\x00\x00\x00afile(\xb5/\xfd\x00'
2811 b'XY\x00\x00Hello WorldPK\x01\x02?\x03?\x00\x00\x00]\x00\x00\x00'
2812 b'!\x00V\xb0\x17J\x14\x00\x00\x00\x0b\x00\x00\x00\x05\x00\x00\x00'
2813 b'\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00\x00afilePK'
2814 b'\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00\x007\x00\x00\x00'
2815 b'\x00\x00')
2816
2817 class DecryptionTests(unittest.TestCase):
2818 """Check that ZIP decryption works. Since the library does not
2819 support encryption at the moment, we use a pre-generated encrypted
2820 ZIP file."""
2821
2822 data = (
2823 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
2824 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
2825 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
2826 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
2827 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
2828 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
2829 b'\x00\x00L\x00\x00\x00\x00\x00' )
2830 data2 = (
2831 b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
2832 b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
2833 b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
2834 b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
2835 b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
2836 b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
2837 b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
2838 b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
2839
2840 plain = b'zipfile.py encryption test'
2841 plain2 = b'\x00'*512
2842
2843 def setUp(self):
2844 with open(TESTFN, "wb") as fp:
2845 fp.write(self.data)
2846 self.zip = zipfile.ZipFile(TESTFN, "r")
2847 with open(TESTFN2, "wb") as fp:
2848 fp.write(self.data2)
2849 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
2850
2851 def tearDown(self):
2852 self.zip.close()
2853 os.unlink(TESTFN)
2854 self.zip2.close()
2855 os.unlink(TESTFN2)
2856
2857 def test_no_password(self):
2858 # Reading the encrypted file without password
2859 # must generate a RunTime exception
2860 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
2861 self.assertRaises(RuntimeError, self.zip2.read, "zero")
2862
2863 def test_bad_password(self):
2864 self.zip.setpassword(b"perl")
2865 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
2866 self.zip2.setpassword(b"perl")
2867 self.assertRaises(RuntimeError, self.zip2.read, "zero")
2868
2869 @requires_zlib()
2870 def test_good_password(self):
2871 self.zip.setpassword(b"python")
2872 self.assertEqual(self.zip.read("test.txt"), self.plain)
2873 self.zip2.setpassword(b"12345")
2874 self.assertEqual(self.zip2.read("zero"), self.plain2)
2875
2876 def test_unicode_password(self):
2877 expected_msg = "pwd: expected bytes, got str"
2878
2879 with self.assertRaisesRegex(TypeError, expected_msg):
2880 self.zip.setpassword("unicode")
2881
2882 with self.assertRaisesRegex(TypeError, expected_msg):
2883 self.zip.read("test.txt", "python")
2884
2885 with self.assertRaisesRegex(TypeError, expected_msg):
2886 self.zip.open("test.txt", pwd="python")
2887
2888 with self.assertRaisesRegex(TypeError, expected_msg):
2889 self.zip.extract("test.txt", pwd="python")
2890
2891 with self.assertRaisesRegex(TypeError, expected_msg):
2892 self.zip.pwd = "python"
2893 self.zip.open("test.txt")
2894
2895 def test_seek_tell(self):
2896 self.zip.setpassword(b"python")
2897 txt = self.plain
2898 test_word = b'encryption'
2899 bloc = txt.find(test_word)
2900 bloc_len = len(test_word)
2901 with self.zip.open("test.txt", "r") as fp:
2902 fp.seek(bloc, os.SEEK_SET)
2903 self.assertEqual(fp.tell(), bloc)
2904 fp.seek(-bloc, os.SEEK_CUR)
2905 self.assertEqual(fp.tell(), 0)
2906 fp.seek(bloc, os.SEEK_CUR)
2907 self.assertEqual(fp.tell(), bloc)
2908 self.assertEqual(fp.read(bloc_len), txt[bloc:bloc+bloc_len])
2909
2910 # Make sure that the second read after seeking back beyond
2911 # _readbuffer returns the same content (ie. rewind to the start of
2912 # the file to read forward to the required position).
2913 old_read_size = fp.MIN_READ_SIZE
2914 fp.MIN_READ_SIZE = 1
2915 fp._readbuffer = b''
2916 fp._offset = 0
2917 fp.seek(0, os.SEEK_SET)
2918 self.assertEqual(fp.tell(), 0)
2919 fp.seek(bloc, os.SEEK_CUR)
2920 self.assertEqual(fp.read(bloc_len), txt[bloc:bloc+bloc_len])
2921 fp.MIN_READ_SIZE = old_read_size
2922
2923 fp.seek(0, os.SEEK_END)
2924 self.assertEqual(fp.tell(), len(txt))
2925 fp.seek(0, os.SEEK_SET)
2926 self.assertEqual(fp.tell(), 0)
2927
2928 # Read the file completely to definitely call any eof integrity
2929 # checks (crc) and make sure they still pass.
2930 fp.read()
2931
2932
2933 class AbstractTestsWithRandomBinaryFiles:
2934 @classmethod
2935 def setUpClass(cls):
2936 datacount = randint(16, 64)*1024 + randint(1, 1024)
2937 cls.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
2938 for i in range(datacount))
2939
2940 def setUp(self):
2941 # Make a source file with some lines
2942 with open(TESTFN, "wb") as fp:
2943 fp.write(self.data)
2944
2945 def tearDown(self):
2946 unlink(TESTFN)
2947 unlink(TESTFN2)
2948
2949 def make_test_archive(self, f, compression):
2950 # Create the ZIP archive
2951 with zipfile.ZipFile(f, "w", compression) as zipfp:
2952 zipfp.write(TESTFN, "another.name")
2953 zipfp.write(TESTFN, TESTFN)
2954
2955 def zip_test(self, f, compression):
2956 self.make_test_archive(f, compression)
2957
2958 # Read the ZIP archive
2959 with zipfile.ZipFile(f, "r", compression) as zipfp:
2960 testdata = zipfp.read(TESTFN)
2961 self.assertEqual(len(testdata), len(self.data))
2962 self.assertEqual(testdata, self.data)
2963 self.assertEqual(zipfp.read("another.name"), self.data)
2964
2965 def test_read(self):
2966 for f in get_files(self):
2967 self.zip_test(f, self.compression)
2968
2969 def zip_open_test(self, f, compression):
2970 self.make_test_archive(f, compression)
2971
2972 # Read the ZIP archive
2973 with zipfile.ZipFile(f, "r", compression) as zipfp:
2974 zipdata1 = []
2975 with zipfp.open(TESTFN) as zipopen1:
2976 while True:
2977 read_data = zipopen1.read(256)
2978 if not read_data:
2979 break
2980 zipdata1.append(read_data)
2981
2982 zipdata2 = []
2983 with zipfp.open("another.name") as zipopen2:
2984 while True:
2985 read_data = zipopen2.read(256)
2986 if not read_data:
2987 break
2988 zipdata2.append(read_data)
2989
2990 testdata1 = b''.join(zipdata1)
2991 self.assertEqual(len(testdata1), len(self.data))
2992 self.assertEqual(testdata1, self.data)
2993
2994 testdata2 = b''.join(zipdata2)
2995 self.assertEqual(len(testdata2), len(self.data))
2996 self.assertEqual(testdata2, self.data)
2997
2998 def test_open(self):
2999 for f in get_files(self):
3000 self.zip_open_test(f, self.compression)
3001
3002 def zip_random_open_test(self, f, compression):
3003 self.make_test_archive(f, compression)
3004
3005 # Read the ZIP archive
3006 with zipfile.ZipFile(f, "r", compression) as zipfp:
3007 zipdata1 = []
3008 with zipfp.open(TESTFN) as zipopen1:
3009 while True:
3010 read_data = zipopen1.read(randint(1, 1024))
3011 if not read_data:
3012 break
3013 zipdata1.append(read_data)
3014
3015 testdata = b''.join(zipdata1)
3016 self.assertEqual(len(testdata), len(self.data))
3017 self.assertEqual(testdata, self.data)
3018
3019 def test_random_open(self):
3020 for f in get_files(self):
3021 self.zip_random_open_test(f, self.compression)
3022
3023
3024 class StoredTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
3025 unittest.TestCase):
3026 compression = zipfile.ZIP_STORED
3027
3028 @requires_zlib()
3029 class DeflateTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
3030 unittest.TestCase):
3031 compression = zipfile.ZIP_DEFLATED
3032
3033 @requires_bz2()
3034 class Bzip2TestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
3035 unittest.TestCase):
3036 compression = zipfile.ZIP_BZIP2
3037
3038 @requires_lzma()
3039 class LzmaTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
3040 unittest.TestCase):
3041 compression = zipfile.ZIP_LZMA
3042
3043 @requires_zstd()
3044 class ZstdTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
3045 unittest.TestCase):
3046 compression = zipfile.ZIP_ZSTANDARD
3047
3048 # Provide the tell() method but not seek()
3049 class Tellable:
3050 def __init__(self, fp):
3051 self.fp = fp
3052 self.offset = 0
3053
3054 def write(self, data):
3055 n = self.fp.write(data)
3056 self.offset += n
3057 return n
3058
3059 def tell(self):
3060 return self.offset
3061
3062 def flush(self):
3063 self.fp.flush()
3064
3065 class Unseekable:
3066 def __init__(self, fp):
3067 self.fp = fp
3068
3069 def write(self, data):
3070 return self.fp.write(data)
3071
3072 def flush(self):
3073 self.fp.flush()
3074
3075 class UnseekableTests(unittest.TestCase):
3076 def test_writestr(self):
3077 for wrapper in (lambda f: f), Tellable, Unseekable:
3078 with self.subTest(wrapper=wrapper):
3079 f = io.BytesIO()
3080 f.write(b'abc')
3081 bf = io.BufferedWriter(f)
3082 with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipfp:
3083 zipfp.writestr('ones', b'111')
3084 zipfp.writestr('twos', b'222')
3085 self.assertEqual(f.getvalue()[:5], b'abcPK')
3086 with zipfile.ZipFile(f, mode='r') as zipf:
3087 with zipf.open('ones') as zopen:
3088 self.assertEqual(zopen.read(), b'111')
3089 with zipf.open('twos') as zopen:
3090 self.assertEqual(zopen.read(), b'222')
3091
3092 def test_write(self):
3093 for wrapper in (lambda f: f), Tellable, Unseekable:
3094 with self.subTest(wrapper=wrapper):
3095 f = io.BytesIO()
3096 f.write(b'abc')
3097 bf = io.BufferedWriter(f)
3098 with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipfp:
3099 self.addCleanup(unlink, TESTFN)
3100 with open(TESTFN, 'wb') as f2:
3101 f2.write(b'111')
3102 zipfp.write(TESTFN, 'ones')
3103 with open(TESTFN, 'wb') as f2:
3104 f2.write(b'222')
3105 zipfp.write(TESTFN, 'twos')
3106 self.assertEqual(f.getvalue()[:5], b'abcPK')
3107 with zipfile.ZipFile(f, mode='r') as zipf:
3108 with zipf.open('ones') as zopen:
3109 self.assertEqual(zopen.read(), b'111')
3110 with zipf.open('twos') as zopen:
3111 self.assertEqual(zopen.read(), b'222')
3112
3113 def test_open_write(self):
3114 for wrapper in (lambda f: f), Tellable, Unseekable:
3115 with self.subTest(wrapper=wrapper):
3116 f = io.BytesIO()
3117 f.write(b'abc')
3118 bf = io.BufferedWriter(f)
3119 with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipf:
3120 with zipf.open('ones', 'w') as zopen:
3121 zopen.write(b'111')
3122 with zipf.open('twos', 'w') as zopen:
3123 zopen.write(b'222')
3124 self.assertEqual(f.getvalue()[:5], b'abcPK')
3125 with zipfile.ZipFile(f) as zipf:
3126 self.assertEqual(zipf.read('ones'), b'111')
3127 self.assertEqual(zipf.read('twos'), b'222')
3128
3129
3130 @requires_zlib()
3131 class TestsWithMultipleOpens(unittest.TestCase):
3132 @classmethod
3133 def setUpClass(cls):
3134 cls.data1 = b'111' + randbytes(10000)
3135 cls.data2 = b'222' + randbytes(10000)
3136
3137 def make_test_archive(self, f):
3138 # Create the ZIP archive
3139 with zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED) as zipfp:
3140 zipfp.writestr('ones', self.data1)
3141 zipfp.writestr('twos', self.data2)
3142
3143 def test_same_file(self):
3144 # Verify that (when the ZipFile is in control of creating file objects)
3145 # multiple open() calls can be made without interfering with each other.
3146 for f in get_files(self):
3147 self.make_test_archive(f)
3148 with zipfile.ZipFile(f, mode="r") as zipf:
3149 with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2:
3150 data1 = zopen1.read(500)
3151 data2 = zopen2.read(500)
3152 data1 += zopen1.read()
3153 data2 += zopen2.read()
3154 self.assertEqual(data1, data2)
3155 self.assertEqual(data1, self.data1)
3156
3157 def test_different_file(self):
3158 # Verify that (when the ZipFile is in control of creating file objects)
3159 # multiple open() calls can be made without interfering with each other.
3160 for f in get_files(self):
3161 self.make_test_archive(f)
3162 with zipfile.ZipFile(f, mode="r") as zipf:
3163 with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
3164 data1 = zopen1.read(500)
3165 data2 = zopen2.read(500)
3166 data1 += zopen1.read()
3167 data2 += zopen2.read()
3168 self.assertEqual(data1, self.data1)
3169 self.assertEqual(data2, self.data2)
3170
3171 def test_interleaved(self):
3172 # Verify that (when the ZipFile is in control of creating file objects)
3173 # multiple open() calls can be made without interfering with each other.
3174 for f in get_files(self):
3175 self.make_test_archive(f)
3176 with zipfile.ZipFile(f, mode="r") as zipf:
3177 with zipf.open('ones') as zopen1:
3178 data1 = zopen1.read(500)
3179 with zipf.open('twos') as zopen2:
3180 data2 = zopen2.read(500)
3181 data1 += zopen1.read()
3182 data2 += zopen2.read()
3183 self.assertEqual(data1, self.data1)
3184 self.assertEqual(data2, self.data2)
3185
3186 def test_read_after_close(self):
3187 for f in get_files(self):
3188 self.make_test_archive(f)
3189 with contextlib.ExitStack() as stack:
3190 with zipfile.ZipFile(f, 'r') as zipf:
3191 zopen1 = stack.enter_context(zipf.open('ones'))
3192 zopen2 = stack.enter_context(zipf.open('twos'))
3193 data1 = zopen1.read(500)
3194 data2 = zopen2.read(500)
3195 data1 += zopen1.read()
3196 data2 += zopen2.read()
3197 self.assertEqual(data1, self.data1)
3198 self.assertEqual(data2, self.data2)
3199
3200 def test_read_after_write(self):
3201 for f in get_files(self):
3202 with zipfile.ZipFile(f, 'w', zipfile.ZIP_DEFLATED) as zipf:
3203 zipf.writestr('ones', self.data1)
3204 zipf.writestr('twos', self.data2)
3205 with zipf.open('ones') as zopen1:
3206 data1 = zopen1.read(500)
3207 self.assertEqual(data1, self.data1[:500])
3208 with zipfile.ZipFile(f, 'r') as zipf:
3209 data1 = zipf.read('ones')
3210 data2 = zipf.read('twos')
3211 self.assertEqual(data1, self.data1)
3212 self.assertEqual(data2, self.data2)
3213
3214 def test_write_after_read(self):
3215 for f in get_files(self):
3216 with zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED) as zipf:
3217 zipf.writestr('ones', self.data1)
3218 with zipf.open('ones') as zopen1:
3219 zopen1.read(500)
3220 zipf.writestr('twos', self.data2)
3221 with zipfile.ZipFile(f, 'r') as zipf:
3222 data1 = zipf.read('ones')
3223 data2 = zipf.read('twos')
3224 self.assertEqual(data1, self.data1)
3225 self.assertEqual(data2, self.data2)
3226
3227 def test_many_opens(self):
3228 # Verify that read() and open() promptly close the file descriptor,
3229 # and don't rely on the garbage collector to free resources.
3230 startcount = fd_count()
3231 self.make_test_archive(TESTFN2)
3232 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
3233 for x in range(100):
3234 zipf.read('ones')
3235 with zipf.open('ones') as zopen1:
3236 pass
3237 self.assertEqual(startcount, fd_count())
3238
3239 def test_write_while_reading(self):
3240 with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_DEFLATED) as zipf:
3241 zipf.writestr('ones', self.data1)
3242 with zipfile.ZipFile(TESTFN2, 'a', zipfile.ZIP_DEFLATED) as zipf:
3243 with zipf.open('ones', 'r') as r1:
3244 data1 = r1.read(500)
3245 with zipf.open('twos', 'w') as w1:
3246 w1.write(self.data2)
3247 data1 += r1.read()
3248 self.assertEqual(data1, self.data1)
3249 with zipfile.ZipFile(TESTFN2) as zipf:
3250 self.assertEqual(zipf.read('twos'), self.data2)
3251
3252 def tearDown(self):
3253 unlink(TESTFN2)
3254
3255
3256 class TestWithDirectory(unittest.TestCase):
3257 def setUp(self):
3258 os.mkdir(TESTFN2)
3259
3260 def test_extract_dir(self):
3261 with zipfile.ZipFile(findfile("zipdir.zip", subdir="archivetestdata")) as zipf:
3262 zipf.extractall(TESTFN2)
3263 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
3264 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
3265 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
3266
3267 def test_bug_6050(self):
3268 # Extraction should succeed if directories already exist
3269 os.mkdir(os.path.join(TESTFN2, "a"))
3270 self.test_extract_dir()
3271
3272 def test_extract_dir_backslash(self):
3273 zfname = findfile("zipdir_backslash.zip", subdir="archivetestdata")
3274 with zipfile.ZipFile(zfname) as zipf:
3275 zipf.extractall(TESTFN2)
3276 if os.name == 'nt':
3277 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
3278 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
3279 self.assertTrue(os.path.isfile(os.path.join(TESTFN2, "a", "b", "c")))
3280 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "d")))
3281 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "d", "e")))
3282 else:
3283 self.assertTrue(os.path.isfile(os.path.join(TESTFN2, "a\\b\\c")))
3284 self.assertTrue(os.path.isfile(os.path.join(TESTFN2, "d\\e\\")))
3285 self.assertFalse(os.path.exists(os.path.join(TESTFN2, "a")))
3286 self.assertFalse(os.path.exists(os.path.join(TESTFN2, "d")))
3287
3288 def test_write_dir(self):
3289 dirpath = os.path.join(TESTFN2, "x")
3290 os.mkdir(dirpath)
3291 mode = os.stat(dirpath).st_mode & 0xFFFF
3292 with zipfile.ZipFile(TESTFN, "w") as zipf:
3293 zipf.write(dirpath)
3294 zinfo = zipf.filelist[0]
3295 self.assertEndsWith(zinfo.filename, "/x/")
3296 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
3297 zipf.write(dirpath, "y")
3298 zinfo = zipf.filelist[1]
3299 self.assertTrue(zinfo.filename, "y/")
3300 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
3301 with zipfile.ZipFile(TESTFN, "r") as zipf:
3302 zinfo = zipf.filelist[0]
3303 self.assertEndsWith(zinfo.filename, "/x/")
3304 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
3305 zinfo = zipf.filelist[1]
3306 self.assertTrue(zinfo.filename, "y/")
3307 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
3308 target = os.path.join(TESTFN2, "target")
3309 os.mkdir(target)
3310 zipf.extractall(target)
3311 self.assertTrue(os.path.isdir(os.path.join(target, "y")))
3312 self.assertEqual(len(os.listdir(target)), 2)
3313
3314 def test_writestr_dir(self):
3315 os.mkdir(os.path.join(TESTFN2, "x"))
3316 with zipfile.ZipFile(TESTFN, "w") as zipf:
3317 zipf.writestr("x/", b'')
3318 zinfo = zipf.filelist[0]
3319 self.assertEqual(zinfo.filename, "x/")
3320 self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10)
3321 with zipfile.ZipFile(TESTFN, "r") as zipf:
3322 zinfo = zipf.filelist[0]
3323 self.assertEndsWith(zinfo.filename, "x/")
3324 self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10)
3325 target = os.path.join(TESTFN2, "target")
3326 os.mkdir(target)
3327 zipf.extractall(target)
3328 self.assertTrue(os.path.isdir(os.path.join(target, "x")))
3329 self.assertEqual(os.listdir(target), ["x"])
3330
3331 def test_mkdir(self):
3332 with zipfile.ZipFile(TESTFN, "w") as zf:
3333 zf.mkdir("directory")
3334 zinfo = zf.filelist[0]
3335 self.assertEqual(zinfo.filename, "directory/")
3336 self.assertEqual(zinfo.external_attr, (0o40777 << 16) | 0x10)
3337
3338 zf.mkdir("directory2/")
3339 zinfo = zf.filelist[1]
3340 self.assertEqual(zinfo.filename, "directory2/")
3341 self.assertEqual(zinfo.external_attr, (0o40777 << 16) | 0x10)
3342
3343 zf.mkdir("directory3", mode=0o777)
3344 zinfo = zf.filelist[2]
3345 self.assertEqual(zinfo.filename, "directory3/")
3346 self.assertEqual(zinfo.external_attr, (0o40777 << 16) | 0x10)
3347
3348 old_zinfo = zipfile.ZipInfo("directory4/")
3349 old_zinfo.external_attr = (0o40777 << 16) | 0x10
3350 old_zinfo.CRC = 0
3351 old_zinfo.file_size = 0
3352 old_zinfo.compress_size = 0
3353 zf.mkdir(old_zinfo)
3354 new_zinfo = zf.filelist[3]
3355 self.assertEqual(old_zinfo.filename, "directory4/")
3356 self.assertEqual(old_zinfo.external_attr, new_zinfo.external_attr)
3357
3358 target = os.path.join(TESTFN2, "target")
3359 os.mkdir(target)
3360 zf.extractall(target)
3361 self.assertEqual(set(os.listdir(target)), {"directory", "directory2", "directory3", "directory4"})
3362
3363 def test_create_directory_with_write(self):
3364 with zipfile.ZipFile(TESTFN, "w") as zf:
3365 zf.writestr(zipfile.ZipInfo('directory/'), '')
3366
3367 zinfo = zf.filelist[0]
3368 self.assertEqual(zinfo.filename, "directory/")
3369
3370 directory = os.path.join(TESTFN2, "directory2")
3371 os.mkdir(directory)
3372 mode = os.stat(directory).st_mode & 0xFFFF
3373 zf.write(directory, arcname="directory2/")
3374 zinfo = zf.filelist[1]
3375 self.assertEqual(zinfo.filename, "directory2/")
3376 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
3377
3378 target = os.path.join(TESTFN2, "target")
3379 os.mkdir(target)
3380 zf.extractall(target)
3381
3382 self.assertEqual(set(os.listdir(target)), {"directory", "directory2"})
3383
3384 def test_root_folder_in_zipfile(self):
3385 """
3386 gh-112795: Some tools or self constructed codes will add '/' folder to
3387 the zip file, this is a strange behavior, but we should support it.
3388 """
3389 in_memory_file = io.BytesIO()
3390 zf = zipfile.ZipFile(in_memory_file, "w")
3391 zf.mkdir('/')
3392 zf.writestr('./a.txt', 'aaa')
3393 zf.extractall(TESTFN2)
3394
3395 def tearDown(self):
3396 rmtree(TESTFN2)
3397 if os.path.exists(TESTFN):
3398 unlink(TESTFN)
3399
3400
3401 class ZipInfoTests(unittest.TestCase):
3402 def test_from_file(self):
3403 zi = zipfile.ZipInfo.from_file(__file__)
3404 self.assertEqual(posixpath.basename(zi.filename), 'test_core.py')
3405 self.assertFalse(zi.is_dir())
3406 self.assertEqual(zi.file_size, os.path.getsize(__file__))
3407
3408 def test_from_file_pathlike(self):
3409 zi = zipfile.ZipInfo.from_file(FakePath(__file__))
3410 self.assertEqual(posixpath.basename(zi.filename), 'test_core.py')
3411 self.assertFalse(zi.is_dir())
3412 self.assertEqual(zi.file_size, os.path.getsize(__file__))
3413
3414 def test_from_file_bytes(self):
3415 zi = zipfile.ZipInfo.from_file(os.fsencode(__file__), 'test')
3416 self.assertEqual(posixpath.basename(zi.filename), 'test')
3417 self.assertFalse(zi.is_dir())
3418 self.assertEqual(zi.file_size, os.path.getsize(__file__))
3419
3420 def test_from_file_fileno(self):
3421 with open(__file__, 'rb') as f:
3422 zi = zipfile.ZipInfo.from_file(f.fileno(), 'test')
3423 self.assertEqual(posixpath.basename(zi.filename), 'test')
3424 self.assertFalse(zi.is_dir())
3425 self.assertEqual(zi.file_size, os.path.getsize(__file__))
3426
3427 def test_from_dir(self):
3428 dirpath = os.path.dirname(os.path.abspath(__file__))
3429 zi = zipfile.ZipInfo.from_file(dirpath, 'stdlib_tests')
3430 self.assertEqual(zi.filename, 'stdlib_tests/')
3431 self.assertTrue(zi.is_dir())
3432 self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
3433 self.assertEqual(zi.file_size, 0)
3434
3435 def test_compresslevel_property(self):
3436 zinfo = zipfile.ZipInfo("xxx")
3437 self.assertFalse(zinfo._compresslevel)
3438 self.assertFalse(zinfo.compress_level)
3439 zinfo._compresslevel = 99 # test the legacy @property.setter
3440 self.assertEqual(zinfo.compress_level, 99)
3441 self.assertEqual(zinfo._compresslevel, 99)
3442 zinfo.compress_level = 8
3443 self.assertEqual(zinfo.compress_level, 8)
3444 self.assertEqual(zinfo._compresslevel, 8)
3445
3446
3447 class CommandLineTest(unittest.TestCase):
3448
3449 def zipfilecmd(self, *args, **kwargs):
3450 rc, out, err = script_helper.assert_python_ok('-m', 'zipfile', *args,
3451 **kwargs)
3452 return out.replace(os.linesep.encode(), b'\n')
3453
3454 def zipfilecmd_failure(self, *args):
3455 return script_helper.assert_python_failure('-m', 'zipfile', *args)
3456
3457 def test_bad_use(self):
3458 rc, out, err = self.zipfilecmd_failure()
3459 self.assertEqual(out, b'')
3460 self.assertIn(b'usage', err.lower())
3461 self.assertIn(b'error', err.lower())
3462 self.assertIn(b'required', err.lower())
3463 rc, out, err = self.zipfilecmd_failure('-l', '')
3464 self.assertEqual(out, b'')
3465 self.assertNotEqual(err.strip(), b'')
3466
3467 def test_test_command(self):
3468 zip_name = findfile('zipdir.zip', subdir='archivetestdata')
3469 for opt in '-t', '--test':
3470 out = self.zipfilecmd(opt, zip_name)
3471 self.assertEqual(out.rstrip(), b'Done testing')
3472 zip_name = findfile('testtar.tar')
3473 rc, out, err = self.zipfilecmd_failure('-t', zip_name)
3474 self.assertEqual(out, b'')
3475
3476 def test_list_command(self):
3477 zip_name = findfile('zipdir.zip', subdir='archivetestdata')
3478 t = io.StringIO()
3479 with zipfile.ZipFile(zip_name, 'r') as tf:
3480 tf.printdir(t)
3481 expected = t.getvalue().encode('ascii', 'backslashreplace')
3482 for opt in '-l', '--list':
3483 out = self.zipfilecmd(opt, zip_name,
3484 PYTHONIOENCODING='ascii:backslashreplace')
3485 self.assertEqual(out, expected)
3486
3487 @requires_zlib()
3488 def test_create_command(self):
3489 self.addCleanup(unlink, TESTFN)
3490 with open(TESTFN, 'w', encoding='utf-8') as f:
3491 f.write('test 1')
3492 os.mkdir(TESTFNDIR)
3493 self.addCleanup(rmtree, TESTFNDIR)
3494 with open(os.path.join(TESTFNDIR, 'file.txt'), 'w', encoding='utf-8') as f:
3495 f.write('test 2')
3496 files = [TESTFN, TESTFNDIR]
3497 namelist = [TESTFN, TESTFNDIR + '/', TESTFNDIR + '/file.txt']
3498 for opt in '-c', '--create':
3499 try:
3500 out = self.zipfilecmd(opt, TESTFN2, *files)
3501 self.assertEqual(out, b'')
3502 with zipfile.ZipFile(TESTFN2) as zf:
3503 self.assertEqual(zf.namelist(), namelist)
3504 self.assertEqual(zf.read(namelist[0]), b'test 1')
3505 self.assertEqual(zf.read(namelist[2]), b'test 2')
3506 finally:
3507 unlink(TESTFN2)
3508
3509 def test_extract_command(self):
3510 zip_name = findfile('zipdir.zip', subdir='archivetestdata')
3511 for opt in '-e', '--extract':
3512 with temp_dir() as extdir:
3513 out = self.zipfilecmd(opt, zip_name, extdir)
3514 self.assertEqual(out, b'')
3515 with zipfile.ZipFile(zip_name) as zf:
3516 for zi in zf.infolist():
3517 path = os.path.join(extdir,
3518 zi.filename.replace('/', os.sep))
3519 if zi.is_dir():
3520 self.assertTrue(os.path.isdir(path))
3521 else:
3522 self.assertTrue(os.path.isfile(path))
3523 with open(path, 'rb') as f:
3524 self.assertEqual(f.read(), zf.read(zi))
3525
3526
3527 class TestExecutablePrependedZip(unittest.TestCase):
3528 """Test our ability to open zip files with an executable prepended."""
3529
3530 def setUp(self):
3531 self.exe_zip = findfile('exe_with_zip', subdir='archivetestdata')
3532 self.exe_zip64 = findfile('exe_with_z64', subdir='archivetestdata')
3533
3534 def _test_zip_works(self, name):
3535 # bpo28494 sanity check: ensure is_zipfile works on these.
3536 self.assertTrue(zipfile.is_zipfile(name),
3537 f'is_zipfile failed on {name}')
3538 # Ensure we can operate on these via ZipFile.
3539 with zipfile.ZipFile(name) as zipfp:
3540 for n in zipfp.namelist():
3541 data = zipfp.read(n)
3542 self.assertIn(b'FAVORITE_NUMBER', data)
3543
3544 def test_read_zip_with_exe_prepended(self):
3545 self._test_zip_works(self.exe_zip)
3546
3547 def test_read_zip64_with_exe_prepended(self):
3548 self._test_zip_works(self.exe_zip64)
3549
3550 @unittest.skipUnless(sys.executable, 'sys.executable required.')
3551 @unittest.skipUnless(os.access('/bin/bash', os.X_OK),
3552 'Test relies on #!/bin/bash working.')
3553 @requires_subprocess()
3554 def test_execute_zip2(self):
3555 output = subprocess.check_output([self.exe_zip, sys.executable])
3556 self.assertIn(b'number in executable: 5', output)
3557
3558 @unittest.skipUnless(sys.executable, 'sys.executable required.')
3559 @unittest.skipUnless(os.access('/bin/bash', os.X_OK),
3560 'Test relies on #!/bin/bash working.')
3561 @requires_subprocess()
3562 def test_execute_zip64(self):
3563 output = subprocess.check_output([self.exe_zip64, sys.executable])
3564 self.assertIn(b'number in executable: 5', output)
3565
3566
3567 class EncodedMetadataTests(unittest.TestCase):
3568 file_names = ['\u4e00', '\u4e8c', '\u4e09'] # Han 'one', 'two', 'three'
3569 file_content = [
3570 "This is pure ASCII.\n".encode('ascii'),
3571 # This is modern Japanese. (UTF-8)
3572 "\u3053\u308c\u306f\u73fe\u4ee3\u7684\u65e5\u672c\u8a9e\u3067\u3059\u3002\n".encode('utf-8'),
3573 # This is obsolete Japanese. (Shift JIS)
3574 "\u3053\u308c\u306f\u53e4\u3044\u65e5\u672c\u8a9e\u3067\u3059\u3002\n".encode('shift_jis'),
3575 ]
3576
3577 def setUp(self):
3578 self.addCleanup(unlink, TESTFN)
3579 # Create .zip of 3 members with Han names encoded in Shift JIS.
3580 # Each name is 1 Han character encoding to 2 bytes in Shift JIS.
3581 # The ASCII names are arbitrary as long as they are length 2 and
3582 # not otherwise contained in the zip file.
3583 # Data elements are encoded bytes (ascii, utf-8, shift_jis).
3584 placeholders = ["n1", "n2"] + self.file_names[2:]
3585 with zipfile.ZipFile(TESTFN, mode="w") as tf:
3586 for temp, content in zip(placeholders, self.file_content):
3587 tf.writestr(temp, content, zipfile.ZIP_STORED)
3588 # Hack in the Shift JIS names with flag bit 11 (UTF-8) unset.
3589 with open(TESTFN, "rb") as tf:
3590 data = tf.read()
3591 for name, temp in zip(self.file_names, placeholders[:2]):
3592 data = data.replace(temp.encode('ascii'),
3593 name.encode('shift_jis'))
3594 with open(TESTFN, "wb") as tf:
3595 tf.write(data)
3596
3597 def _test_read(self, zipfp, expected_names, expected_content):
3598 # Check the namelist
3599 names = zipfp.namelist()
3600 self.assertEqual(sorted(names), sorted(expected_names))
3601
3602 # Check infolist
3603 infos = zipfp.infolist()
3604 names = [zi.filename for zi in infos]
3605 self.assertEqual(sorted(names), sorted(expected_names))
3606
3607 # check getinfo
3608 for name, content in zip(expected_names, expected_content):
3609 info = zipfp.getinfo(name)
3610 self.assertEqual(info.filename, name)
3611 self.assertEqual(info.file_size, len(content))
3612 self.assertEqual(zipfp.read(name), content)
3613
3614 def test_read_with_metadata_encoding(self):
3615 # Read the ZIP archive with correct metadata_encoding
3616 with zipfile.ZipFile(TESTFN, "r", metadata_encoding='shift_jis') as zipfp:
3617 self._test_read(zipfp, self.file_names, self.file_content)
3618
3619 def test_read_without_metadata_encoding(self):
3620 # Read the ZIP archive without metadata_encoding
3621 expected_names = [name.encode('shift_jis').decode('cp437')
3622 for name in self.file_names[:2]] + self.file_names[2:]
3623 with zipfile.ZipFile(TESTFN, "r") as zipfp:
3624 self._test_read(zipfp, expected_names, self.file_content)
3625
3626 def test_read_with_incorrect_metadata_encoding(self):
3627 # Read the ZIP archive with incorrect metadata_encoding
3628 expected_names = [name.encode('shift_jis').decode('koi8-u')
3629 for name in self.file_names[:2]] + self.file_names[2:]
3630 with zipfile.ZipFile(TESTFN, "r", metadata_encoding='koi8-u') as zipfp:
3631 self._test_read(zipfp, expected_names, self.file_content)
3632
3633 def test_read_with_unsuitable_metadata_encoding(self):
3634 # Read the ZIP archive with metadata_encoding unsuitable for
3635 # decoding metadata
3636 with self.assertRaises(UnicodeDecodeError):
3637 zipfile.ZipFile(TESTFN, "r", metadata_encoding='ascii')
3638 with self.assertRaises(UnicodeDecodeError):
3639 zipfile.ZipFile(TESTFN, "r", metadata_encoding='utf-8')
3640
3641 def test_read_after_append(self):
3642 newname = '\u56db' # Han 'four'
3643 newname2 = 'fünf' # representable in cp437, but still stored as UTF-8
3644 expected_names = [*self.file_names, newname, newname2]
3645 mojibake_expected_names = [name.encode('shift_jis').decode('cp437')
3646 if i < 2 else name
3647 for i, name in enumerate(expected_names)]
3648 expected_content = (*self.file_content, b"newcontent", b"newcontent2")
3649
3650 with zipfile.ZipFile(TESTFN, "a") as zipfp:
3651 zipfp.writestr(newname, "newcontent")
3652 zipfp.writestr(newname2, "newcontent2")
3653 self.assertEqual(sorted(zipfp.namelist()), sorted(mojibake_expected_names))
3654
3655 with zipfile.ZipFile(TESTFN, "r") as zipfp:
3656 self._test_read(zipfp, mojibake_expected_names, expected_content)
3657
3658 with zipfile.ZipFile(TESTFN, "r", metadata_encoding='shift_jis') as zipfp:
3659 self._test_read(zipfp, expected_names, expected_content)
3660
3661 def test_write_with_metadata_encoding(self):
3662 ZF = zipfile.ZipFile
3663 for mode in ("w", "x", "a"):
3664 with self.assertRaisesRegex(ValueError,
3665 "^metadata_encoding is only"):
3666 ZF("nonesuch.zip", mode, metadata_encoding="shift_jis")
3667
3668 def test_add_comment(self):
3669 with zipfile.ZipFile(TESTFN, "r") as zipfp:
3670 mojibake_expected_names = zipfp.namelist()
3671
3672 with zipfile.ZipFile(TESTFN, "a") as zipfp:
3673 zipfp.comment = b'comment'
3674 self.assertEqual(zipfp.namelist(), mojibake_expected_names)
3675
3676 with zipfile.ZipFile(TESTFN, "r") as zipfp:
3677 self._test_read(zipfp, mojibake_expected_names, self.file_content)
3678
3679 with zipfile.ZipFile(TESTFN, "r", metadata_encoding='shift_jis') as zipfp:
3680 self._test_read(zipfp, self.file_names, self.file_content)
3681
3682 def test_cli_with_metadata_encoding(self):
3683 errmsg = "Non-conforming encodings not supported with -c."
3684 args = ["--metadata-encoding=shift_jis", "-c", "nonesuch", "nonesuch"]
3685 with captured_stdout() as stdout:
3686 with captured_stderr() as stderr:
3687 self.assertRaises(SystemExit, zipfile.main, args)
3688 self.assertEqual(stdout.getvalue(), "")
3689 self.assertIn(errmsg, stderr.getvalue())
3690
3691 with captured_stdout() as stdout:
3692 zipfile.main(["--metadata-encoding=shift_jis", "-t", TESTFN])
3693 listing = stdout.getvalue()
3694
3695 with captured_stdout() as stdout:
3696 zipfile.main(["--metadata-encoding=shift_jis", "-l", TESTFN])
3697 listing = stdout.getvalue()
3698 for name in self.file_names:
3699 self.assertIn(name, listing)
3700
3701 def test_cli_with_metadata_encoding_extract(self):
3702 os.mkdir(TESTFN2)
3703 self.addCleanup(rmtree, TESTFN2)
3704 # Depending on locale, extracted file names can be not encodable
3705 # with the filesystem encoding.
3706 for fn in self.file_names:
3707 try:
3708 os.stat(os.path.join(TESTFN2, fn))
3709 except OSError:
3710 pass
3711 except UnicodeEncodeError:
3712 self.skipTest(f'cannot encode file name {fn!a}')
3713
3714 zipfile.main(["--metadata-encoding=shift_jis", "-e", TESTFN, TESTFN2])
3715 listing = os.listdir(TESTFN2)
3716 for name in self.file_names:
3717 self.assertIn(name, listing)
3718
3719
3720 class StripExtraTests(unittest.TestCase):
3721 # Note: all of the "z" characters are technically invalid, but up
3722 # to 3 bytes at the end of the extra will be passed through as they
3723 # are too short to encode a valid extra.
3724
3725 ZIP64_EXTRA = 1
3726
3727 def test_no_data(self):
3728 s = struct.Struct("<HH")
3729 a = s.pack(self.ZIP64_EXTRA, 0)
3730 b = s.pack(2, 0)
3731 c = s.pack(3, 0)
3732
3733 self.assertEqual(b'', zipfile._Extra.strip(a, (self.ZIP64_EXTRA,)))
3734 self.assertEqual(b, zipfile._Extra.strip(b, (self.ZIP64_EXTRA,)))
3735 self.assertEqual(
3736 b+b"z", zipfile._Extra.strip(b+b"z", (self.ZIP64_EXTRA,)))
3737
3738 self.assertEqual(b+c, zipfile._Extra.strip(a+b+c, (self.ZIP64_EXTRA,)))
3739 self.assertEqual(b+c, zipfile._Extra.strip(b+a+c, (self.ZIP64_EXTRA,)))
3740 self.assertEqual(b+c, zipfile._Extra.strip(b+c+a, (self.ZIP64_EXTRA,)))
3741
3742 def test_with_data(self):
3743 s = struct.Struct("<HH")
3744 a = s.pack(self.ZIP64_EXTRA, 1) + b"a"
3745 b = s.pack(2, 2) + b"bb"
3746 c = s.pack(3, 3) + b"ccc"
3747
3748 self.assertEqual(b"", zipfile._Extra.strip(a, (self.ZIP64_EXTRA,)))
3749 self.assertEqual(b, zipfile._Extra.strip(b, (self.ZIP64_EXTRA,)))
3750 self.assertEqual(
3751 b+b"z", zipfile._Extra.strip(b+b"z", (self.ZIP64_EXTRA,)))
3752
3753 self.assertEqual(b+c, zipfile._Extra.strip(a+b+c, (self.ZIP64_EXTRA,)))
3754 self.assertEqual(b+c, zipfile._Extra.strip(b+a+c, (self.ZIP64_EXTRA,)))
3755 self.assertEqual(b+c, zipfile._Extra.strip(b+c+a, (self.ZIP64_EXTRA,)))
3756
3757 def test_multiples(self):
3758 s = struct.Struct("<HH")
3759 a = s.pack(self.ZIP64_EXTRA, 1) + b"a"
3760 b = s.pack(2, 2) + b"bb"
3761
3762 self.assertEqual(b"", zipfile._Extra.strip(a+a, (self.ZIP64_EXTRA,)))
3763 self.assertEqual(b"", zipfile._Extra.strip(a+a+a, (self.ZIP64_EXTRA,)))
3764 self.assertEqual(
3765 b"z", zipfile._Extra.strip(a+a+b"z", (self.ZIP64_EXTRA,)))
3766 self.assertEqual(
3767 b+b"z", zipfile._Extra.strip(a+a+b+b"z", (self.ZIP64_EXTRA,)))
3768
3769 self.assertEqual(b, zipfile._Extra.strip(a+a+b, (self.ZIP64_EXTRA,)))
3770 self.assertEqual(b, zipfile._Extra.strip(a+b+a, (self.ZIP64_EXTRA,)))
3771 self.assertEqual(b, zipfile._Extra.strip(b+a+a, (self.ZIP64_EXTRA,)))
3772
3773 def test_too_short(self):
3774 self.assertEqual(b"", zipfile._Extra.strip(b"", (self.ZIP64_EXTRA,)))
3775 self.assertEqual(b"z", zipfile._Extra.strip(b"z", (self.ZIP64_EXTRA,)))
3776 self.assertEqual(
3777 b"zz", zipfile._Extra.strip(b"zz", (self.ZIP64_EXTRA,)))
3778 self.assertEqual(
3779 b"zzz", zipfile._Extra.strip(b"zzz", (self.ZIP64_EXTRA,)))
3780
3781
3782 class StatIO(_pyio.BytesIO):
3783 """Buffer which remembers the number of bytes that were read."""
3784
3785 def __init__(self):
3786 super().__init__()
3787 self.bytes_read = 0
3788
3789 def read(self, size=-1):
3790 bs = super().read(size)
3791 self.bytes_read += len(bs)
3792 return bs
3793
3794
3795 class StoredZipExtFileRandomReadTest(unittest.TestCase):
3796 """Tests whether an uncompressed, unencrypted zip entry can be randomly
3797 seek and read without reading redundant bytes."""
3798 def test_stored_seek_and_read(self):
3799
3800 sio = StatIO()
3801 # 20000 bytes
3802 txt = b'0123456789' * 2000
3803
3804 # The seek length must be greater than ZipExtFile.MIN_READ_SIZE
3805 # as `ZipExtFile._read2()` reads in blocks of this size and we
3806 # need to seek out of the buffered data
3807 read_buffer_size = zipfile.ZipExtFile.MIN_READ_SIZE
3808 self.assertGreaterEqual(10002, read_buffer_size) # for forward seek test
3809 self.assertGreaterEqual(5003, read_buffer_size) # for backward seek test
3810 # The read length must be less than MIN_READ_SIZE, since we assume that
3811 # only 1 block is read in the test.
3812 read_length = 100
3813 self.assertGreaterEqual(read_buffer_size, read_length) # for read() calls
3814
3815 with zipfile.ZipFile(sio, "w", compression=zipfile.ZIP_STORED) as zipf:
3816 zipf.writestr("foo.txt", txt)
3817
3818 # check random seek and read on a file
3819 with zipfile.ZipFile(sio, "r") as zipf:
3820 with zipf.open("foo.txt", "r") as fp:
3821 # Test this optimized read hasn't rewound and read from the
3822 # start of the file (as in the case of the unoptimized path)
3823
3824 # forward seek
3825 old_count = sio.bytes_read
3826 forward_seek_len = 10002
3827 current_pos = 0
3828 fp.seek(forward_seek_len, os.SEEK_CUR)
3829 current_pos += forward_seek_len
3830 self.assertEqual(fp.tell(), current_pos)
3831 self.assertEqual(fp._left, fp._compress_left)
3832 arr = fp.read(read_length)
3833 current_pos += read_length
3834 self.assertEqual(fp.tell(), current_pos)
3835 self.assertEqual(arr, txt[current_pos - read_length:current_pos])
3836 self.assertEqual(fp._left, fp._compress_left)
3837 read_count = sio.bytes_read - old_count
3838 self.assertLessEqual(read_count, read_buffer_size)
3839
3840 # backward seek
3841 old_count = sio.bytes_read
3842 backward_seek_len = 5003
3843 fp.seek(-backward_seek_len, os.SEEK_CUR)
3844 current_pos -= backward_seek_len
3845 self.assertEqual(fp.tell(), current_pos)
3846 self.assertEqual(fp._left, fp._compress_left)
3847 arr = fp.read(read_length)
3848 current_pos += read_length
3849 self.assertEqual(fp.tell(), current_pos)
3850 self.assertEqual(arr, txt[current_pos - read_length:current_pos])
3851 self.assertEqual(fp._left, fp._compress_left)
3852 read_count = sio.bytes_read - old_count
3853 self.assertLessEqual(read_count, read_buffer_size)
3854
3855 # eof flags test
3856 fp.seek(0, os.SEEK_END)
3857 fp.seek(12345, os.SEEK_SET)
3858 current_pos = 12345
3859 arr = fp.read(read_length)
3860 current_pos += read_length
3861 self.assertEqual(arr, txt[current_pos - read_length:current_pos])
3862
3863
3864 if __name__ == "__main__":
3865 unittest.main()