Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 1 | import contextlib |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2 | import importlib.util |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 3 | import io |
Daniel Hillier | da6ce58 | 2019-10-29 18:24:18 +1100 | [diff] [blame] | 4 | import itertools |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 5 | import os |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 6 | import pathlib |
Serhiy Storchaka | 503f908 | 2016-02-08 00:02:25 +0200 | [diff] [blame] | 7 | import posixpath |
Jason R. Coombs | 0aeab5c | 2020-02-29 10:34:11 -0600 | [diff] [blame] | 8 | import string |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 9 | import struct |
Gregory P. Smith | 3f4db4a | 2019-09-10 17:14:11 +0100 | [diff] [blame] | 10 | import subprocess |
| 11 | import sys |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 12 | import time |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 13 | import unittest |
Berker Peksag | 2f1b857 | 2019-09-12 17:13:44 +0300 | [diff] [blame] | 14 | import unittest.mock as mock |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 15 | import zipfile |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 16 | |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 17 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 18 | from tempfile import TemporaryFile |
Victor Stinner | 87502dd | 2020-04-17 22:54:38 +0200 | [diff] [blame] | 19 | from random import randint, random, randbytes |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 20 | |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 21 | from test.support import script_helper |
Hai Shi | deb0162 | 2020-07-06 20:29:49 +0800 | [diff] [blame] | 22 | from test.support import (findfile, requires_zlib, requires_bz2, |
| 23 | requires_lzma, captured_stdout) |
| 24 | from test.support.os_helper import TESTFN, unlink, rmtree, temp_dir, temp_cwd |
| 25 | |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 26 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 27 | TESTFN2 = TESTFN + "2" |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 28 | TESTFNDIR = TESTFN + "d" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 29 | FIXEDTEST_SIZE = 1000 |
Georg Brandl | 5ba11de | 2011-01-01 10:09:32 +0000 | [diff] [blame] | 30 | DATAFILES_DIR = 'zipfile_datafiles' |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 31 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 32 | SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'), |
| 33 | ('ziptest2dir/_ziptest2', 'qawsedrftg'), |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 34 | ('ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'), |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 35 | ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')] |
| 36 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 37 | def get_files(test): |
| 38 | yield TESTFN2 |
| 39 | with TemporaryFile() as f: |
| 40 | yield f |
| 41 | test.assertFalse(f.closed) |
| 42 | with io.BytesIO() as f: |
| 43 | yield f |
| 44 | test.assertFalse(f.closed) |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 45 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 46 | class AbstractTestsWithSourceFile: |
| 47 | @classmethod |
| 48 | def setUpClass(cls): |
| 49 | cls.line_gen = [bytes("Zipfile test line %d. random float: %f\n" % |
| 50 | (i, random()), "ascii") |
| 51 | for i in range(FIXEDTEST_SIZE)] |
| 52 | cls.data = b''.join(cls.line_gen) |
| 53 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 54 | def setUp(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 55 | # Make a source file with some lines |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 56 | with open(TESTFN, "wb") as fp: |
| 57 | fp.write(self.data) |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 58 | |
Bo Bayles | ce237c7 | 2018-01-29 23:54:07 -0600 | [diff] [blame] | 59 | def make_test_archive(self, f, compression, compresslevel=None): |
| 60 | kwargs = {'compression': compression, 'compresslevel': compresslevel} |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 61 | # Create the ZIP archive |
Bo Bayles | ce237c7 | 2018-01-29 23:54:07 -0600 | [diff] [blame] | 62 | with zipfile.ZipFile(f, "w", **kwargs) as zipfp: |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 63 | zipfp.write(TESTFN, "another.name") |
| 64 | zipfp.write(TESTFN, TESTFN) |
| 65 | zipfp.writestr("strfile", self.data) |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 66 | with zipfp.open('written-open-w', mode='w') as f: |
| 67 | for line in self.line_gen: |
| 68 | f.write(line) |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 69 | |
Bo Bayles | ce237c7 | 2018-01-29 23:54:07 -0600 | [diff] [blame] | 70 | def zip_test(self, f, compression, compresslevel=None): |
| 71 | self.make_test_archive(f, compression, compresslevel) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 72 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 73 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 74 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 75 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 76 | self.assertEqual(zipfp.read("another.name"), self.data) |
| 77 | self.assertEqual(zipfp.read("strfile"), self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 79 | # Print the ZIP directory |
| 80 | fp = io.StringIO() |
| 81 | zipfp.printdir(file=fp) |
| 82 | directory = fp.getvalue() |
| 83 | lines = directory.splitlines() |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 84 | self.assertEqual(len(lines), 5) # Number of files + header |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 85 | |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 86 | self.assertIn('File Name', lines[0]) |
| 87 | self.assertIn('Modified', lines[0]) |
| 88 | self.assertIn('Size', lines[0]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 89 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 90 | fn, date, time_, size = lines[1].split() |
| 91 | self.assertEqual(fn, 'another.name') |
| 92 | self.assertTrue(time.strptime(date, '%Y-%m-%d')) |
| 93 | self.assertTrue(time.strptime(time_, '%H:%M:%S')) |
| 94 | self.assertEqual(size, str(len(self.data))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 95 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 96 | # Check the namelist |
| 97 | names = zipfp.namelist() |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 98 | self.assertEqual(len(names), 4) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 99 | self.assertIn(TESTFN, names) |
| 100 | self.assertIn("another.name", names) |
| 101 | self.assertIn("strfile", names) |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 102 | self.assertIn("written-open-w", names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 103 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 104 | # Check infolist |
| 105 | infos = zipfp.infolist() |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 106 | names = [i.filename for i in infos] |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 107 | self.assertEqual(len(names), 4) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 108 | self.assertIn(TESTFN, names) |
| 109 | self.assertIn("another.name", names) |
| 110 | self.assertIn("strfile", names) |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 111 | self.assertIn("written-open-w", names) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 112 | for i in infos: |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 113 | self.assertEqual(i.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 114 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 115 | # check getinfo |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 116 | for nm in (TESTFN, "another.name", "strfile", "written-open-w"): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 117 | info = zipfp.getinfo(nm) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 118 | self.assertEqual(info.filename, nm) |
| 119 | self.assertEqual(info.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 120 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 121 | # Check that testzip doesn't raise an exception |
| 122 | zipfp.testzip() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 123 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 124 | def test_basic(self): |
| 125 | for f in get_files(self): |
| 126 | self.zip_test(f, self.compression) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 127 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 128 | def zip_open_test(self, f, compression): |
| 129 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 130 | |
| 131 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 132 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 133 | zipdata1 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 134 | with zipfp.open(TESTFN) as zipopen1: |
| 135 | while True: |
| 136 | read_data = zipopen1.read(256) |
| 137 | if not read_data: |
| 138 | break |
| 139 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 140 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 141 | zipdata2 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 142 | with zipfp.open("another.name") as zipopen2: |
| 143 | while True: |
| 144 | read_data = zipopen2.read(256) |
| 145 | if not read_data: |
| 146 | break |
| 147 | zipdata2.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 148 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 149 | self.assertEqual(b''.join(zipdata1), self.data) |
| 150 | self.assertEqual(b''.join(zipdata2), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 151 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 152 | def test_open(self): |
| 153 | for f in get_files(self): |
| 154 | self.zip_open_test(f, self.compression) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 155 | |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 156 | def test_open_with_pathlike(self): |
| 157 | path = pathlib.Path(TESTFN2) |
| 158 | self.zip_open_test(path, self.compression) |
| 159 | with zipfile.ZipFile(path, "r", self.compression) as zipfp: |
| 160 | self.assertIsInstance(zipfp.filename, str) |
| 161 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 162 | def zip_random_open_test(self, f, compression): |
| 163 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 164 | |
| 165 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 166 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 167 | zipdata1 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 168 | with zipfp.open(TESTFN) as zipopen1: |
| 169 | while True: |
| 170 | read_data = zipopen1.read(randint(1, 1024)) |
| 171 | if not read_data: |
| 172 | break |
| 173 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 174 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 175 | self.assertEqual(b''.join(zipdata1), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 176 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 177 | def test_random_open(self): |
| 178 | for f in get_files(self): |
| 179 | self.zip_random_open_test(f, self.compression) |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 180 | |
Serhiy Storchaka | d2c07a5 | 2013-09-27 22:11:57 +0300 | [diff] [blame] | 181 | def zip_read1_test(self, f, compression): |
| 182 | self.make_test_archive(f, compression) |
| 183 | |
| 184 | # Read the ZIP archive |
| 185 | with zipfile.ZipFile(f, "r") as zipfp, \ |
| 186 | zipfp.open(TESTFN) as zipopen: |
| 187 | zipdata = [] |
| 188 | while True: |
| 189 | read_data = zipopen.read1(-1) |
| 190 | if not read_data: |
| 191 | break |
| 192 | zipdata.append(read_data) |
| 193 | |
| 194 | self.assertEqual(b''.join(zipdata), self.data) |
| 195 | |
| 196 | def test_read1(self): |
| 197 | for f in get_files(self): |
| 198 | self.zip_read1_test(f, self.compression) |
| 199 | |
| 200 | def zip_read1_10_test(self, f, compression): |
| 201 | self.make_test_archive(f, compression) |
| 202 | |
| 203 | # Read the ZIP archive |
| 204 | with zipfile.ZipFile(f, "r") as zipfp, \ |
| 205 | zipfp.open(TESTFN) as zipopen: |
| 206 | zipdata = [] |
| 207 | while True: |
| 208 | read_data = zipopen.read1(10) |
| 209 | self.assertLessEqual(len(read_data), 10) |
| 210 | if not read_data: |
| 211 | break |
| 212 | zipdata.append(read_data) |
| 213 | |
| 214 | self.assertEqual(b''.join(zipdata), self.data) |
| 215 | |
| 216 | def test_read1_10(self): |
| 217 | for f in get_files(self): |
| 218 | self.zip_read1_10_test(f, self.compression) |
| 219 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 220 | def zip_readline_read_test(self, f, compression): |
| 221 | self.make_test_archive(f, compression) |
| 222 | |
| 223 | # Read the ZIP archive |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 224 | with zipfile.ZipFile(f, "r") as zipfp, \ |
| 225 | zipfp.open(TESTFN) as zipopen: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 226 | data = b'' |
| 227 | while True: |
| 228 | read = zipopen.readline() |
| 229 | if not read: |
| 230 | break |
| 231 | data += read |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 232 | |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 233 | read = zipopen.read(100) |
| 234 | if not read: |
| 235 | break |
| 236 | data += read |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 237 | |
| 238 | self.assertEqual(data, self.data) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 239 | |
| 240 | def test_readline_read(self): |
| 241 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 242 | for f in get_files(self): |
| 243 | self.zip_readline_read_test(f, self.compression) |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 244 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 245 | def zip_readline_test(self, f, compression): |
| 246 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 247 | |
| 248 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 249 | with zipfile.ZipFile(f, "r") as zipfp: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 250 | with zipfp.open(TESTFN) as zipopen: |
| 251 | for line in self.line_gen: |
| 252 | linedata = zipopen.readline() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 253 | self.assertEqual(linedata, line) |
| 254 | |
| 255 | def test_readline(self): |
| 256 | for f in get_files(self): |
| 257 | self.zip_readline_test(f, self.compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 258 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 259 | def zip_readlines_test(self, f, compression): |
| 260 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 261 | |
| 262 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 263 | with zipfile.ZipFile(f, "r") as zipfp: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 264 | with zipfp.open(TESTFN) as zipopen: |
| 265 | ziplines = zipopen.readlines() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 266 | for line, zipline in zip(self.line_gen, ziplines): |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 267 | self.assertEqual(zipline, line) |
| 268 | |
| 269 | def test_readlines(self): |
| 270 | for f in get_files(self): |
| 271 | self.zip_readlines_test(f, self.compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 272 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 273 | def zip_iterlines_test(self, f, compression): |
| 274 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 275 | |
| 276 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 277 | with zipfile.ZipFile(f, "r") as zipfp: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 278 | with zipfp.open(TESTFN) as zipopen: |
| 279 | for line, zipline in zip(self.line_gen, zipopen): |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 280 | self.assertEqual(zipline, line) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 281 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 282 | def test_iterlines(self): |
| 283 | for f in get_files(self): |
| 284 | self.zip_iterlines_test(f, self.compression) |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 285 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 286 | def test_low_compression(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 287 | """Check for cases where compressed data is larger than original.""" |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 288 | # Create the ZIP archive |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 289 | with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipfp: |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 290 | zipfp.writestr("strfile", '12') |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 291 | |
| 292 | # Get an open object for strfile |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 293 | with zipfile.ZipFile(TESTFN2, "r", self.compression) as zipfp: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 294 | with zipfp.open("strfile") as openobj: |
| 295 | self.assertEqual(openobj.read(1), b'1') |
| 296 | self.assertEqual(openobj.read(1), b'2') |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 297 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 298 | def test_writestr_compression(self): |
| 299 | zipfp = zipfile.ZipFile(TESTFN2, "w") |
| 300 | zipfp.writestr("b.txt", "hello world", compress_type=self.compression) |
| 301 | info = zipfp.getinfo('b.txt') |
| 302 | self.assertEqual(info.compress_type, self.compression) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 303 | |
Bo Bayles | ce237c7 | 2018-01-29 23:54:07 -0600 | [diff] [blame] | 304 | def test_writestr_compresslevel(self): |
| 305 | zipfp = zipfile.ZipFile(TESTFN2, "w", compresslevel=1) |
| 306 | zipfp.writestr("a.txt", "hello world", compress_type=self.compression) |
| 307 | zipfp.writestr("b.txt", "hello world", compress_type=self.compression, |
| 308 | compresslevel=2) |
| 309 | |
| 310 | # Compression level follows the constructor. |
| 311 | a_info = zipfp.getinfo('a.txt') |
| 312 | self.assertEqual(a_info.compress_type, self.compression) |
| 313 | self.assertEqual(a_info._compresslevel, 1) |
| 314 | |
| 315 | # Compression level is overridden. |
| 316 | b_info = zipfp.getinfo('b.txt') |
| 317 | self.assertEqual(b_info.compress_type, self.compression) |
| 318 | self.assertEqual(b_info._compresslevel, 2) |
| 319 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 320 | def test_read_return_size(self): |
| 321 | # Issue #9837: ZipExtFile.read() shouldn't return more bytes |
| 322 | # than requested. |
| 323 | for test_size in (1, 4095, 4096, 4097, 16384): |
| 324 | file_size = test_size + 1 |
Victor Stinner | 87502dd | 2020-04-17 22:54:38 +0200 | [diff] [blame] | 325 | junk = randbytes(file_size) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 326 | with zipfile.ZipFile(io.BytesIO(), "w", self.compression) as zipf: |
| 327 | zipf.writestr('foo', junk) |
| 328 | with zipf.open('foo', 'r') as fp: |
| 329 | buf = fp.read(test_size) |
| 330 | self.assertEqual(len(buf), test_size) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 331 | |
Serhiy Storchaka | 5ce3f10 | 2014-01-09 14:50:20 +0200 | [diff] [blame] | 332 | def test_truncated_zipfile(self): |
| 333 | fp = io.BytesIO() |
| 334 | with zipfile.ZipFile(fp, mode='w') as zipf: |
| 335 | zipf.writestr('strfile', self.data, compress_type=self.compression) |
| 336 | end_offset = fp.tell() |
| 337 | zipfiledata = fp.getvalue() |
| 338 | |
| 339 | fp = io.BytesIO(zipfiledata) |
| 340 | with zipfile.ZipFile(fp) as zipf: |
| 341 | with zipf.open('strfile') as zipopen: |
| 342 | fp.truncate(end_offset - 20) |
| 343 | with self.assertRaises(EOFError): |
| 344 | zipopen.read() |
| 345 | |
| 346 | fp = io.BytesIO(zipfiledata) |
| 347 | with zipfile.ZipFile(fp) as zipf: |
| 348 | with zipf.open('strfile') as zipopen: |
| 349 | fp.truncate(end_offset - 20) |
| 350 | with self.assertRaises(EOFError): |
| 351 | while zipopen.read(100): |
| 352 | pass |
| 353 | |
| 354 | fp = io.BytesIO(zipfiledata) |
| 355 | with zipfile.ZipFile(fp) as zipf: |
| 356 | with zipf.open('strfile') as zipopen: |
| 357 | fp.truncate(end_offset - 20) |
| 358 | with self.assertRaises(EOFError): |
| 359 | while zipopen.read1(100): |
| 360 | pass |
| 361 | |
Serhiy Storchaka | 51a4370 | 2014-10-29 22:42:06 +0200 | [diff] [blame] | 362 | def test_repr(self): |
| 363 | fname = 'file.name' |
| 364 | for f in get_files(self): |
| 365 | with zipfile.ZipFile(f, 'w', self.compression) as zipfp: |
| 366 | zipfp.write(TESTFN, fname) |
| 367 | r = repr(zipfp) |
| 368 | self.assertIn("mode='w'", r) |
| 369 | |
| 370 | with zipfile.ZipFile(f, 'r') as zipfp: |
| 371 | r = repr(zipfp) |
| 372 | if isinstance(f, str): |
| 373 | self.assertIn('filename=%r' % f, r) |
| 374 | else: |
| 375 | self.assertIn('file=%r' % f, r) |
| 376 | self.assertIn("mode='r'", r) |
| 377 | r = repr(zipfp.getinfo(fname)) |
| 378 | self.assertIn('filename=%r' % fname, r) |
| 379 | self.assertIn('filemode=', r) |
| 380 | self.assertIn('file_size=', r) |
| 381 | if self.compression != zipfile.ZIP_STORED: |
| 382 | self.assertIn('compress_type=', r) |
| 383 | self.assertIn('compress_size=', r) |
| 384 | with zipfp.open(fname) as zipopen: |
| 385 | r = repr(zipopen) |
| 386 | self.assertIn('name=%r' % fname, r) |
| 387 | self.assertIn("mode='r'", r) |
| 388 | if self.compression != zipfile.ZIP_STORED: |
| 389 | self.assertIn('compress_type=', r) |
| 390 | self.assertIn('[closed]', repr(zipopen)) |
| 391 | self.assertIn('[closed]', repr(zipfp)) |
| 392 | |
Bo Bayles | ce237c7 | 2018-01-29 23:54:07 -0600 | [diff] [blame] | 393 | def test_compresslevel_basic(self): |
| 394 | for f in get_files(self): |
| 395 | self.zip_test(f, self.compression, compresslevel=9) |
| 396 | |
| 397 | def test_per_file_compresslevel(self): |
| 398 | """Check that files within a Zip archive can have different |
| 399 | compression levels.""" |
| 400 | with zipfile.ZipFile(TESTFN2, "w", compresslevel=1) as zipfp: |
| 401 | zipfp.write(TESTFN, 'compress_1') |
| 402 | zipfp.write(TESTFN, 'compress_9', compresslevel=9) |
| 403 | one_info = zipfp.getinfo('compress_1') |
| 404 | nine_info = zipfp.getinfo('compress_9') |
| 405 | self.assertEqual(one_info._compresslevel, 1) |
| 406 | self.assertEqual(nine_info._compresslevel, 9) |
| 407 | |
Serhiy Storchaka | 2524fde | 2019-03-30 08:25:19 +0200 | [diff] [blame] | 408 | def test_writing_errors(self): |
| 409 | class BrokenFile(io.BytesIO): |
| 410 | def write(self, data): |
| 411 | nonlocal count |
| 412 | if count is not None: |
| 413 | if count == stop: |
| 414 | raise OSError |
| 415 | count += 1 |
| 416 | super().write(data) |
| 417 | |
| 418 | stop = 0 |
| 419 | while True: |
| 420 | testfile = BrokenFile() |
| 421 | count = None |
| 422 | with zipfile.ZipFile(testfile, 'w', self.compression) as zipfp: |
| 423 | with zipfp.open('file1', 'w') as f: |
| 424 | f.write(b'data1') |
| 425 | count = 0 |
| 426 | try: |
| 427 | with zipfp.open('file2', 'w') as f: |
| 428 | f.write(b'data2') |
| 429 | except OSError: |
| 430 | stop += 1 |
| 431 | else: |
| 432 | break |
| 433 | finally: |
| 434 | count = None |
| 435 | with zipfile.ZipFile(io.BytesIO(testfile.getvalue())) as zipfp: |
| 436 | self.assertEqual(zipfp.namelist(), ['file1']) |
| 437 | self.assertEqual(zipfp.read('file1'), b'data1') |
| 438 | |
| 439 | with zipfile.ZipFile(io.BytesIO(testfile.getvalue())) as zipfp: |
| 440 | self.assertEqual(zipfp.namelist(), ['file1', 'file2']) |
| 441 | self.assertEqual(zipfp.read('file1'), b'data1') |
| 442 | self.assertEqual(zipfp.read('file2'), b'data2') |
| 443 | |
| 444 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 445 | def tearDown(self): |
| 446 | unlink(TESTFN) |
| 447 | unlink(TESTFN2) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 448 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 449 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 450 | class StoredTestsWithSourceFile(AbstractTestsWithSourceFile, |
| 451 | unittest.TestCase): |
| 452 | compression = zipfile.ZIP_STORED |
| 453 | test_low_compression = None |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 454 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 455 | def zip_test_writestr_permissions(self, f, compression): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 456 | # Make sure that writestr and open(... mode='w') create files with |
| 457 | # mode 0600, when they are passed a name rather than a ZipInfo |
| 458 | # instance. |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 459 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 460 | self.make_test_archive(f, compression) |
| 461 | with zipfile.ZipFile(f, "r") as zipfp: |
| 462 | zinfo = zipfp.getinfo('strfile') |
| 463 | self.assertEqual(zinfo.external_attr, 0o600 << 16) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 464 | |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 465 | zinfo2 = zipfp.getinfo('written-open-w') |
| 466 | self.assertEqual(zinfo2.external_attr, 0o600 << 16) |
| 467 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 468 | def test_writestr_permissions(self): |
| 469 | for f in get_files(self): |
| 470 | self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 471 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 472 | def test_absolute_arcnames(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 473 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 474 | zipfp.write(TESTFN, "/absolute") |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame] | 475 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 476 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 477 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
Tim Peters | 32cbc96 | 2006-02-20 21:42:18 +0000 | [diff] [blame] | 478 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 479 | def test_append_to_zip_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 480 | """Test appending to an existing zipfile.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 481 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 482 | zipfp.write(TESTFN, TESTFN) |
| 483 | |
| 484 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 485 | zipfp.writestr("strfile", self.data) |
| 486 | self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"]) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 487 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 488 | def test_append_to_non_zip_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 489 | """Test appending to an existing file that is not a zipfile.""" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 490 | # NOTE: this test fails if len(d) < 22 because of the first |
| 491 | # line "fpin.seek(-22, 2)" in _EndRecData |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 492 | data = b'I am not a ZipFile!'*10 |
| 493 | with open(TESTFN2, 'wb') as f: |
| 494 | f.write(data) |
| 495 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 496 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 497 | zipfp.write(TESTFN, TESTFN) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 498 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 499 | with open(TESTFN2, 'rb') as f: |
| 500 | f.seek(len(data)) |
| 501 | with zipfile.ZipFile(f, "r") as zipfp: |
| 502 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
Serhiy Storchaka | 8793b21 | 2016-10-07 22:20:50 +0300 | [diff] [blame] | 503 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 504 | with open(TESTFN2, 'rb') as f: |
| 505 | self.assertEqual(f.read(len(data)), data) |
| 506 | zipfiledata = f.read() |
| 507 | with io.BytesIO(zipfiledata) as bio, zipfile.ZipFile(bio) as zipfp: |
| 508 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
| 509 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 510 | |
| 511 | def test_read_concatenated_zip_file(self): |
| 512 | with io.BytesIO() as bio: |
| 513 | with zipfile.ZipFile(bio, 'w', zipfile.ZIP_STORED) as zipfp: |
| 514 | zipfp.write(TESTFN, TESTFN) |
| 515 | zipfiledata = bio.getvalue() |
| 516 | data = b'I am not a ZipFile!'*10 |
| 517 | with open(TESTFN2, 'wb') as f: |
| 518 | f.write(data) |
| 519 | f.write(zipfiledata) |
| 520 | |
| 521 | with zipfile.ZipFile(TESTFN2) as zipfp: |
| 522 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
| 523 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 524 | |
| 525 | def test_append_to_concatenated_zip_file(self): |
| 526 | with io.BytesIO() as bio: |
| 527 | with zipfile.ZipFile(bio, 'w', zipfile.ZIP_STORED) as zipfp: |
| 528 | zipfp.write(TESTFN, TESTFN) |
| 529 | zipfiledata = bio.getvalue() |
| 530 | data = b'I am not a ZipFile!'*1000000 |
| 531 | with open(TESTFN2, 'wb') as f: |
| 532 | f.write(data) |
| 533 | f.write(zipfiledata) |
| 534 | |
| 535 | with zipfile.ZipFile(TESTFN2, 'a') as zipfp: |
| 536 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
| 537 | zipfp.writestr('strfile', self.data) |
| 538 | |
| 539 | with open(TESTFN2, 'rb') as f: |
| 540 | self.assertEqual(f.read(len(data)), data) |
| 541 | zipfiledata = f.read() |
| 542 | with io.BytesIO(zipfiledata) as bio, zipfile.ZipFile(bio) as zipfp: |
| 543 | self.assertEqual(zipfp.namelist(), [TESTFN, 'strfile']) |
| 544 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 545 | self.assertEqual(zipfp.read('strfile'), self.data) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 546 | |
R David Murray | 4fbb9db | 2011-06-09 15:50:51 -0400 | [diff] [blame] | 547 | def test_ignores_newline_at_end(self): |
| 548 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 549 | zipfp.write(TESTFN, TESTFN) |
| 550 | with open(TESTFN2, 'a') as f: |
| 551 | f.write("\r\n\00\00\00") |
| 552 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 553 | self.assertIsInstance(zipfp, zipfile.ZipFile) |
| 554 | |
| 555 | def test_ignores_stuff_appended_past_comments(self): |
| 556 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 557 | zipfp.comment = b"this is a comment" |
| 558 | zipfp.write(TESTFN, TESTFN) |
| 559 | with open(TESTFN2, 'a') as f: |
| 560 | f.write("abcdef\r\n") |
| 561 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 562 | self.assertIsInstance(zipfp, zipfile.ZipFile) |
| 563 | self.assertEqual(zipfp.comment, b"this is a comment") |
| 564 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 565 | def test_write_default_name(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 566 | """Check that calling ZipFile.write without arcname specified |
| 567 | produces the expected result.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 568 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 569 | zipfp.write(TESTFN) |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 570 | with open(TESTFN, "rb") as f: |
| 571 | self.assertEqual(zipfp.read(TESTFN), f.read()) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 572 | |
Daniel Hillier | 8d62df6 | 2019-11-30 19:30:47 +1100 | [diff] [blame] | 573 | def test_io_on_closed_zipextfile(self): |
| 574 | fname = "somefile.txt" |
| 575 | with zipfile.ZipFile(TESTFN2, mode="w") as zipfp: |
| 576 | zipfp.writestr(fname, "bogus") |
| 577 | |
| 578 | with zipfile.ZipFile(TESTFN2, mode="r") as zipfp: |
| 579 | with zipfp.open(fname) as fid: |
| 580 | fid.close() |
| 581 | self.assertRaises(ValueError, fid.read) |
| 582 | self.assertRaises(ValueError, fid.seek, 0) |
| 583 | self.assertRaises(ValueError, fid.tell) |
| 584 | self.assertRaises(ValueError, fid.readable) |
| 585 | self.assertRaises(ValueError, fid.seekable) |
| 586 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 587 | def test_write_to_readonly(self): |
| 588 | """Check that trying to call write() on a readonly ZipFile object |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 589 | raises a ValueError.""" |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 590 | with zipfile.ZipFile(TESTFN2, mode="w") as zipfp: |
| 591 | zipfp.writestr("somefile.txt", "bogus") |
| 592 | |
| 593 | with zipfile.ZipFile(TESTFN2, mode="r") as zipfp: |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 594 | self.assertRaises(ValueError, zipfp.write, TESTFN) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 595 | |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 596 | with zipfile.ZipFile(TESTFN2, mode="r") as zipfp: |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 597 | with self.assertRaises(ValueError): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 598 | zipfp.open(TESTFN, mode='w') |
| 599 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 600 | def test_add_file_before_1980(self): |
| 601 | # Set atime and mtime to 1970-01-01 |
| 602 | os.utime(TESTFN, (0, 0)) |
| 603 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 604 | self.assertRaises(ValueError, zipfp.write, TESTFN) |
| 605 | |
Marcel Plch | 77b112c | 2018-08-31 16:43:31 +0200 | [diff] [blame] | 606 | with zipfile.ZipFile(TESTFN2, "w", strict_timestamps=False) as zipfp: |
| 607 | zipfp.write(TESTFN) |
Marcel Plch | a2fe1e5 | 2018-08-02 15:04:52 +0200 | [diff] [blame] | 608 | zinfo = zipfp.getinfo(TESTFN) |
| 609 | self.assertEqual(zinfo.date_time, (1980, 1, 1, 0, 0, 0)) |
| 610 | |
| 611 | def test_add_file_after_2107(self): |
| 612 | # Set atime and mtime to 2108-12-30 |
Victor Stinner | c232c91 | 2020-01-30 15:47:53 +0100 | [diff] [blame] | 613 | ts = 4386268800 |
Marcel Plch | 7b41dba | 2018-08-03 17:59:19 +0200 | [diff] [blame] | 614 | try: |
Victor Stinner | c232c91 | 2020-01-30 15:47:53 +0100 | [diff] [blame] | 615 | time.localtime(ts) |
| 616 | except OverflowError: |
| 617 | self.skipTest(f'time.localtime({ts}) raises OverflowError') |
| 618 | try: |
| 619 | os.utime(TESTFN, (ts, ts)) |
Marcel Plch | 7b41dba | 2018-08-03 17:59:19 +0200 | [diff] [blame] | 620 | except OverflowError: |
| 621 | self.skipTest('Host fs cannot set timestamp to required value.') |
| 622 | |
Victor Stinner | 3cb49b6 | 2020-01-29 15:23:29 +0100 | [diff] [blame] | 623 | mtime_ns = os.stat(TESTFN).st_mtime_ns |
| 624 | if mtime_ns != (4386268800 * 10**9): |
| 625 | # XFS filesystem is limited to 32-bit timestamp, but the syscall |
| 626 | # didn't fail. Moreover, there is a VFS bug which returns |
| 627 | # a cached timestamp which is different than the value on disk. |
| 628 | # |
| 629 | # Test st_mtime_ns rather than st_mtime to avoid rounding issues. |
| 630 | # |
| 631 | # https://bugzilla.redhat.com/show_bug.cgi?id=1795576 |
| 632 | # https://bugs.python.org/issue39460#msg360952 |
| 633 | self.skipTest(f"Linux VFS/XFS kernel bug detected: {mtime_ns=}") |
| 634 | |
Marcel Plch | a2fe1e5 | 2018-08-02 15:04:52 +0200 | [diff] [blame] | 635 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 636 | self.assertRaises(struct.error, zipfp.write, TESTFN) |
| 637 | |
Marcel Plch | 77b112c | 2018-08-31 16:43:31 +0200 | [diff] [blame] | 638 | with zipfile.ZipFile(TESTFN2, "w", strict_timestamps=False) as zipfp: |
| 639 | zipfp.write(TESTFN) |
Marcel Plch | a2fe1e5 | 2018-08-02 15:04:52 +0200 | [diff] [blame] | 640 | zinfo = zipfp.getinfo(TESTFN) |
| 641 | self.assertEqual(zinfo.date_time, (2107, 12, 31, 23, 59, 59)) |
| 642 | |
Serhiy Storchaka | 5ce3f10 | 2014-01-09 14:50:20 +0200 | [diff] [blame] | 643 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 644 | @requires_zlib() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 645 | class DeflateTestsWithSourceFile(AbstractTestsWithSourceFile, |
| 646 | unittest.TestCase): |
| 647 | compression = zipfile.ZIP_DEFLATED |
| 648 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 649 | def test_per_file_compression(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 650 | """Check that files within a Zip archive can have different |
| 651 | compression options.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 652 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 653 | zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED) |
| 654 | zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED) |
| 655 | sinfo = zipfp.getinfo('storeme') |
| 656 | dinfo = zipfp.getinfo('deflateme') |
| 657 | self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED) |
| 658 | self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 659 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 660 | @requires_bz2() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 661 | class Bzip2TestsWithSourceFile(AbstractTestsWithSourceFile, |
| 662 | unittest.TestCase): |
| 663 | compression = zipfile.ZIP_BZIP2 |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 664 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 665 | @requires_lzma() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 666 | class LzmaTestsWithSourceFile(AbstractTestsWithSourceFile, |
| 667 | unittest.TestCase): |
| 668 | compression = zipfile.ZIP_LZMA |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 669 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 670 | |
| 671 | class AbstractTestZip64InSmallFiles: |
| 672 | # These tests test the ZIP64 functionality without using large files, |
| 673 | # see test_zipfile64 for proper tests. |
| 674 | |
| 675 | @classmethod |
| 676 | def setUpClass(cls): |
| 677 | line_gen = (bytes("Test of zipfile line %d." % i, "ascii") |
| 678 | for i in range(0, FIXEDTEST_SIZE)) |
| 679 | cls.data = b'\n'.join(line_gen) |
| 680 | |
| 681 | def setUp(self): |
| 682 | self._limit = zipfile.ZIP64_LIMIT |
Serhiy Storchaka | 026a399 | 2014-09-23 22:27:34 +0300 | [diff] [blame] | 683 | self._filecount_limit = zipfile.ZIP_FILECOUNT_LIMIT |
| 684 | zipfile.ZIP64_LIMIT = 1000 |
| 685 | zipfile.ZIP_FILECOUNT_LIMIT = 9 |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 686 | |
| 687 | # Make a source file with some lines |
| 688 | with open(TESTFN, "wb") as fp: |
| 689 | fp.write(self.data) |
| 690 | |
| 691 | def zip_test(self, f, compression): |
| 692 | # Create the ZIP archive |
| 693 | with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp: |
| 694 | zipfp.write(TESTFN, "another.name") |
| 695 | zipfp.write(TESTFN, TESTFN) |
| 696 | zipfp.writestr("strfile", self.data) |
| 697 | |
| 698 | # Read the ZIP archive |
| 699 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 700 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 701 | self.assertEqual(zipfp.read("another.name"), self.data) |
| 702 | self.assertEqual(zipfp.read("strfile"), self.data) |
| 703 | |
| 704 | # Print the ZIP directory |
| 705 | fp = io.StringIO() |
| 706 | zipfp.printdir(fp) |
| 707 | |
| 708 | directory = fp.getvalue() |
| 709 | lines = directory.splitlines() |
| 710 | self.assertEqual(len(lines), 4) # Number of files + header |
| 711 | |
| 712 | self.assertIn('File Name', lines[0]) |
| 713 | self.assertIn('Modified', lines[0]) |
| 714 | self.assertIn('Size', lines[0]) |
| 715 | |
| 716 | fn, date, time_, size = lines[1].split() |
| 717 | self.assertEqual(fn, 'another.name') |
| 718 | self.assertTrue(time.strptime(date, '%Y-%m-%d')) |
| 719 | self.assertTrue(time.strptime(time_, '%H:%M:%S')) |
| 720 | self.assertEqual(size, str(len(self.data))) |
| 721 | |
| 722 | # Check the namelist |
| 723 | names = zipfp.namelist() |
| 724 | self.assertEqual(len(names), 3) |
| 725 | self.assertIn(TESTFN, names) |
| 726 | self.assertIn("another.name", names) |
| 727 | self.assertIn("strfile", names) |
| 728 | |
| 729 | # Check infolist |
| 730 | infos = zipfp.infolist() |
| 731 | names = [i.filename for i in infos] |
| 732 | self.assertEqual(len(names), 3) |
| 733 | self.assertIn(TESTFN, names) |
| 734 | self.assertIn("another.name", names) |
| 735 | self.assertIn("strfile", names) |
| 736 | for i in infos: |
| 737 | self.assertEqual(i.file_size, len(self.data)) |
| 738 | |
| 739 | # check getinfo |
| 740 | for nm in (TESTFN, "another.name", "strfile"): |
| 741 | info = zipfp.getinfo(nm) |
| 742 | self.assertEqual(info.filename, nm) |
| 743 | self.assertEqual(info.file_size, len(self.data)) |
| 744 | |
| 745 | # Check that testzip doesn't raise an exception |
| 746 | zipfp.testzip() |
| 747 | |
| 748 | def test_basic(self): |
| 749 | for f in get_files(self): |
| 750 | self.zip_test(f, self.compression) |
| 751 | |
Serhiy Storchaka | 026a399 | 2014-09-23 22:27:34 +0300 | [diff] [blame] | 752 | def test_too_many_files(self): |
| 753 | # This test checks that more than 64k files can be added to an archive, |
| 754 | # and that the resulting archive can be read properly by ZipFile |
| 755 | zipf = zipfile.ZipFile(TESTFN, "w", self.compression, |
| 756 | allowZip64=True) |
| 757 | zipf.debug = 100 |
| 758 | numfiles = 15 |
| 759 | for i in range(numfiles): |
| 760 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 761 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 762 | zipf.close() |
| 763 | |
| 764 | zipf2 = zipfile.ZipFile(TESTFN, "r", self.compression) |
| 765 | self.assertEqual(len(zipf2.namelist()), numfiles) |
| 766 | for i in range(numfiles): |
| 767 | content = zipf2.read("foo%08d" % i).decode('ascii') |
| 768 | self.assertEqual(content, "%d" % (i**3 % 57)) |
| 769 | zipf2.close() |
| 770 | |
| 771 | def test_too_many_files_append(self): |
| 772 | zipf = zipfile.ZipFile(TESTFN, "w", self.compression, |
| 773 | allowZip64=False) |
| 774 | zipf.debug = 100 |
| 775 | numfiles = 9 |
| 776 | for i in range(numfiles): |
| 777 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 778 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 779 | with self.assertRaises(zipfile.LargeZipFile): |
| 780 | zipf.writestr("foo%08d" % numfiles, b'') |
| 781 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 782 | zipf.close() |
| 783 | |
| 784 | zipf = zipfile.ZipFile(TESTFN, "a", self.compression, |
| 785 | allowZip64=False) |
| 786 | zipf.debug = 100 |
| 787 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 788 | with self.assertRaises(zipfile.LargeZipFile): |
| 789 | zipf.writestr("foo%08d" % numfiles, b'') |
| 790 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 791 | zipf.close() |
| 792 | |
| 793 | zipf = zipfile.ZipFile(TESTFN, "a", self.compression, |
| 794 | allowZip64=True) |
| 795 | zipf.debug = 100 |
| 796 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 797 | numfiles2 = 15 |
| 798 | for i in range(numfiles, numfiles2): |
| 799 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 800 | self.assertEqual(len(zipf.namelist()), numfiles2) |
| 801 | zipf.close() |
| 802 | |
| 803 | zipf2 = zipfile.ZipFile(TESTFN, "r", self.compression) |
| 804 | self.assertEqual(len(zipf2.namelist()), numfiles2) |
| 805 | for i in range(numfiles2): |
| 806 | content = zipf2.read("foo%08d" % i).decode('ascii') |
| 807 | self.assertEqual(content, "%d" % (i**3 % 57)) |
| 808 | zipf2.close() |
| 809 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 810 | def tearDown(self): |
| 811 | zipfile.ZIP64_LIMIT = self._limit |
Serhiy Storchaka | 026a399 | 2014-09-23 22:27:34 +0300 | [diff] [blame] | 812 | zipfile.ZIP_FILECOUNT_LIMIT = self._filecount_limit |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 813 | unlink(TESTFN) |
| 814 | unlink(TESTFN2) |
| 815 | |
| 816 | |
| 817 | class StoredTestZip64InSmallFiles(AbstractTestZip64InSmallFiles, |
| 818 | unittest.TestCase): |
| 819 | compression = zipfile.ZIP_STORED |
| 820 | |
| 821 | def large_file_exception_test(self, f, compression): |
Serhiy Storchaka | 235c5e0 | 2013-11-23 15:55:38 +0200 | [diff] [blame] | 822 | with zipfile.ZipFile(f, "w", compression, allowZip64=False) as zipfp: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 823 | self.assertRaises(zipfile.LargeZipFile, |
| 824 | zipfp.write, TESTFN, "another.name") |
| 825 | |
| 826 | def large_file_exception_test2(self, f, compression): |
Serhiy Storchaka | 235c5e0 | 2013-11-23 15:55:38 +0200 | [diff] [blame] | 827 | with zipfile.ZipFile(f, "w", compression, allowZip64=False) as zipfp: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 828 | self.assertRaises(zipfile.LargeZipFile, |
| 829 | zipfp.writestr, "another.name", self.data) |
| 830 | |
| 831 | def test_large_file_exception(self): |
| 832 | for f in get_files(self): |
| 833 | self.large_file_exception_test(f, zipfile.ZIP_STORED) |
| 834 | self.large_file_exception_test2(f, zipfile.ZIP_STORED) |
| 835 | |
| 836 | def test_absolute_arcnames(self): |
| 837 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, |
| 838 | allowZip64=True) as zipfp: |
| 839 | zipfp.write(TESTFN, "/absolute") |
| 840 | |
| 841 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 842 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
| 843 | |
Serhiy Storchaka | 9bdb7be | 2018-09-17 15:36:40 +0300 | [diff] [blame] | 844 | def test_append(self): |
| 845 | # Test that appending to the Zip64 archive doesn't change |
| 846 | # extra fields of existing entries. |
| 847 | with zipfile.ZipFile(TESTFN2, "w", allowZip64=True) as zipfp: |
| 848 | zipfp.writestr("strfile", self.data) |
| 849 | with zipfile.ZipFile(TESTFN2, "r", allowZip64=True) as zipfp: |
| 850 | zinfo = zipfp.getinfo("strfile") |
| 851 | extra = zinfo.extra |
| 852 | with zipfile.ZipFile(TESTFN2, "a", allowZip64=True) as zipfp: |
| 853 | zipfp.writestr("strfile2", self.data) |
| 854 | with zipfile.ZipFile(TESTFN2, "r", allowZip64=True) as zipfp: |
| 855 | zinfo = zipfp.getinfo("strfile") |
| 856 | self.assertEqual(zinfo.extra, extra) |
| 857 | |
Daniel Hillier | da6ce58 | 2019-10-29 18:24:18 +1100 | [diff] [blame] | 858 | def make_zip64_file( |
| 859 | self, file_size_64_set=False, file_size_extra=False, |
| 860 | compress_size_64_set=False, compress_size_extra=False, |
| 861 | header_offset_64_set=False, header_offset_extra=False, |
| 862 | ): |
| 863 | """Generate bytes sequence for a zip with (incomplete) zip64 data. |
| 864 | |
| 865 | The actual values (not the zip 64 0xffffffff values) stored in the file |
| 866 | are: |
| 867 | file_size: 8 |
| 868 | compress_size: 8 |
| 869 | header_offset: 0 |
| 870 | """ |
| 871 | actual_size = 8 |
| 872 | actual_header_offset = 0 |
| 873 | local_zip64_fields = [] |
| 874 | central_zip64_fields = [] |
| 875 | |
| 876 | file_size = actual_size |
| 877 | if file_size_64_set: |
| 878 | file_size = 0xffffffff |
| 879 | if file_size_extra: |
| 880 | local_zip64_fields.append(actual_size) |
| 881 | central_zip64_fields.append(actual_size) |
| 882 | file_size = struct.pack("<L", file_size) |
| 883 | |
| 884 | compress_size = actual_size |
| 885 | if compress_size_64_set: |
| 886 | compress_size = 0xffffffff |
| 887 | if compress_size_extra: |
| 888 | local_zip64_fields.append(actual_size) |
| 889 | central_zip64_fields.append(actual_size) |
| 890 | compress_size = struct.pack("<L", compress_size) |
| 891 | |
| 892 | header_offset = actual_header_offset |
| 893 | if header_offset_64_set: |
| 894 | header_offset = 0xffffffff |
| 895 | if header_offset_extra: |
| 896 | central_zip64_fields.append(actual_header_offset) |
| 897 | header_offset = struct.pack("<L", header_offset) |
| 898 | |
| 899 | local_extra = struct.pack( |
| 900 | '<HH' + 'Q'*len(local_zip64_fields), |
| 901 | 0x0001, |
| 902 | 8*len(local_zip64_fields), |
| 903 | *local_zip64_fields |
| 904 | ) |
| 905 | |
| 906 | central_extra = struct.pack( |
| 907 | '<HH' + 'Q'*len(central_zip64_fields), |
| 908 | 0x0001, |
| 909 | 8*len(central_zip64_fields), |
| 910 | *central_zip64_fields |
| 911 | ) |
| 912 | |
| 913 | central_dir_size = struct.pack('<Q', 58 + 8 * len(central_zip64_fields)) |
| 914 | offset_to_central_dir = struct.pack('<Q', 50 + 8 * len(local_zip64_fields)) |
| 915 | |
| 916 | local_extra_length = struct.pack("<H", 4 + 8 * len(local_zip64_fields)) |
| 917 | central_extra_length = struct.pack("<H", 4 + 8 * len(central_zip64_fields)) |
| 918 | |
| 919 | filename = b"test.txt" |
| 920 | content = b"test1234" |
| 921 | filename_length = struct.pack("<H", len(filename)) |
| 922 | zip64_contents = ( |
| 923 | # Local file header |
| 924 | b"PK\x03\x04\x14\x00\x00\x00\x00\x00\x00\x00!\x00\x9e%\xf5\xaf" |
| 925 | + compress_size |
| 926 | + file_size |
| 927 | + filename_length |
| 928 | + local_extra_length |
| 929 | + filename |
| 930 | + local_extra |
| 931 | + content |
| 932 | # Central directory: |
| 933 | + b"PK\x01\x02-\x03-\x00\x00\x00\x00\x00\x00\x00!\x00\x9e%\xf5\xaf" |
| 934 | + compress_size |
| 935 | + file_size |
| 936 | + filename_length |
| 937 | + central_extra_length |
| 938 | + b"\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01" |
| 939 | + header_offset |
| 940 | + filename |
| 941 | + central_extra |
| 942 | # Zip64 end of central directory |
| 943 | + b"PK\x06\x06,\x00\x00\x00\x00\x00\x00\x00-\x00-" |
| 944 | + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00" |
| 945 | + b"\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00" |
| 946 | + central_dir_size |
| 947 | + offset_to_central_dir |
| 948 | # Zip64 end of central directory locator |
| 949 | + b"PK\x06\x07\x00\x00\x00\x00l\x00\x00\x00\x00\x00\x00\x00\x01" |
| 950 | + b"\x00\x00\x00" |
| 951 | # end of central directory |
| 952 | + b"PK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00:\x00\x00\x002\x00" |
| 953 | + b"\x00\x00\x00\x00" |
| 954 | ) |
| 955 | return zip64_contents |
| 956 | |
| 957 | def test_bad_zip64_extra(self): |
| 958 | """Missing zip64 extra records raises an exception. |
| 959 | |
| 960 | There are 4 fields that the zip64 format handles (the disk number is |
| 961 | not used in this module and so is ignored here). According to the zip |
| 962 | spec: |
| 963 | The order of the fields in the zip64 extended |
| 964 | information record is fixed, but the fields MUST |
| 965 | only appear if the corresponding Local or Central |
| 966 | directory record field is set to 0xFFFF or 0xFFFFFFFF. |
| 967 | |
| 968 | If the zip64 extra content doesn't contain enough entries for the |
| 969 | number of fields marked with 0xFFFF or 0xFFFFFFFF, we raise an error. |
| 970 | This test mismatches the length of the zip64 extra field and the number |
| 971 | of fields set to indicate the presence of zip64 data. |
| 972 | """ |
| 973 | # zip64 file size present, no fields in extra, expecting one, equals |
| 974 | # missing file size. |
| 975 | missing_file_size_extra = self.make_zip64_file( |
| 976 | file_size_64_set=True, |
| 977 | ) |
| 978 | with self.assertRaises(zipfile.BadZipFile) as e: |
| 979 | zipfile.ZipFile(io.BytesIO(missing_file_size_extra)) |
| 980 | self.assertIn('file size', str(e.exception).lower()) |
| 981 | |
| 982 | # zip64 file size present, zip64 compress size present, one field in |
| 983 | # extra, expecting two, equals missing compress size. |
| 984 | missing_compress_size_extra = self.make_zip64_file( |
| 985 | file_size_64_set=True, |
| 986 | file_size_extra=True, |
| 987 | compress_size_64_set=True, |
| 988 | ) |
| 989 | with self.assertRaises(zipfile.BadZipFile) as e: |
| 990 | zipfile.ZipFile(io.BytesIO(missing_compress_size_extra)) |
| 991 | self.assertIn('compress size', str(e.exception).lower()) |
| 992 | |
| 993 | # zip64 compress size present, no fields in extra, expecting one, |
| 994 | # equals missing compress size. |
| 995 | missing_compress_size_extra = self.make_zip64_file( |
| 996 | compress_size_64_set=True, |
| 997 | ) |
| 998 | with self.assertRaises(zipfile.BadZipFile) as e: |
| 999 | zipfile.ZipFile(io.BytesIO(missing_compress_size_extra)) |
| 1000 | self.assertIn('compress size', str(e.exception).lower()) |
| 1001 | |
| 1002 | # zip64 file size present, zip64 compress size present, zip64 header |
| 1003 | # offset present, two fields in extra, expecting three, equals missing |
| 1004 | # header offset |
| 1005 | missing_header_offset_extra = self.make_zip64_file( |
| 1006 | file_size_64_set=True, |
| 1007 | file_size_extra=True, |
| 1008 | compress_size_64_set=True, |
| 1009 | compress_size_extra=True, |
| 1010 | header_offset_64_set=True, |
| 1011 | ) |
| 1012 | with self.assertRaises(zipfile.BadZipFile) as e: |
| 1013 | zipfile.ZipFile(io.BytesIO(missing_header_offset_extra)) |
| 1014 | self.assertIn('header offset', str(e.exception).lower()) |
| 1015 | |
| 1016 | # zip64 compress size present, zip64 header offset present, one field |
| 1017 | # in extra, expecting two, equals missing header offset |
| 1018 | missing_header_offset_extra = self.make_zip64_file( |
| 1019 | file_size_64_set=False, |
| 1020 | compress_size_64_set=True, |
| 1021 | compress_size_extra=True, |
| 1022 | header_offset_64_set=True, |
| 1023 | ) |
| 1024 | with self.assertRaises(zipfile.BadZipFile) as e: |
| 1025 | zipfile.ZipFile(io.BytesIO(missing_header_offset_extra)) |
| 1026 | self.assertIn('header offset', str(e.exception).lower()) |
| 1027 | |
| 1028 | # zip64 file size present, zip64 header offset present, one field in |
| 1029 | # extra, expecting two, equals missing header offset |
| 1030 | missing_header_offset_extra = self.make_zip64_file( |
| 1031 | file_size_64_set=True, |
| 1032 | file_size_extra=True, |
| 1033 | compress_size_64_set=False, |
| 1034 | header_offset_64_set=True, |
| 1035 | ) |
| 1036 | with self.assertRaises(zipfile.BadZipFile) as e: |
| 1037 | zipfile.ZipFile(io.BytesIO(missing_header_offset_extra)) |
| 1038 | self.assertIn('header offset', str(e.exception).lower()) |
| 1039 | |
| 1040 | # zip64 header offset present, no fields in extra, expecting one, |
| 1041 | # equals missing header offset |
| 1042 | missing_header_offset_extra = self.make_zip64_file( |
| 1043 | file_size_64_set=False, |
| 1044 | compress_size_64_set=False, |
| 1045 | header_offset_64_set=True, |
| 1046 | ) |
| 1047 | with self.assertRaises(zipfile.BadZipFile) as e: |
| 1048 | zipfile.ZipFile(io.BytesIO(missing_header_offset_extra)) |
| 1049 | self.assertIn('header offset', str(e.exception).lower()) |
| 1050 | |
| 1051 | def test_generated_valid_zip64_extra(self): |
| 1052 | # These values are what is set in the make_zip64_file method. |
| 1053 | expected_file_size = 8 |
| 1054 | expected_compress_size = 8 |
| 1055 | expected_header_offset = 0 |
| 1056 | expected_content = b"test1234" |
| 1057 | |
| 1058 | # Loop through the various valid combinations of zip64 masks |
| 1059 | # present and extra fields present. |
| 1060 | params = ( |
| 1061 | {"file_size_64_set": True, "file_size_extra": True}, |
| 1062 | {"compress_size_64_set": True, "compress_size_extra": True}, |
| 1063 | {"header_offset_64_set": True, "header_offset_extra": True}, |
| 1064 | ) |
| 1065 | |
| 1066 | for r in range(1, len(params) + 1): |
| 1067 | for combo in itertools.combinations(params, r): |
| 1068 | kwargs = {} |
| 1069 | for c in combo: |
| 1070 | kwargs.update(c) |
| 1071 | with zipfile.ZipFile(io.BytesIO(self.make_zip64_file(**kwargs))) as zf: |
| 1072 | zinfo = zf.infolist()[0] |
| 1073 | self.assertEqual(zinfo.file_size, expected_file_size) |
| 1074 | self.assertEqual(zinfo.compress_size, expected_compress_size) |
| 1075 | self.assertEqual(zinfo.header_offset, expected_header_offset) |
| 1076 | self.assertEqual(zf.read(zinfo), expected_content) |
| 1077 | |
| 1078 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 1079 | @requires_zlib() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1080 | class DeflateTestZip64InSmallFiles(AbstractTestZip64InSmallFiles, |
| 1081 | unittest.TestCase): |
| 1082 | compression = zipfile.ZIP_DEFLATED |
| 1083 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 1084 | @requires_bz2() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1085 | class Bzip2TestZip64InSmallFiles(AbstractTestZip64InSmallFiles, |
| 1086 | unittest.TestCase): |
| 1087 | compression = zipfile.ZIP_BZIP2 |
| 1088 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 1089 | @requires_lzma() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1090 | class LzmaTestZip64InSmallFiles(AbstractTestZip64InSmallFiles, |
| 1091 | unittest.TestCase): |
| 1092 | compression = zipfile.ZIP_LZMA |
| 1093 | |
| 1094 | |
Serhiy Storchaka | 4c0d9ea | 2017-04-12 16:03:23 +0300 | [diff] [blame] | 1095 | class AbstractWriterTests: |
| 1096 | |
| 1097 | def tearDown(self): |
| 1098 | unlink(TESTFN2) |
| 1099 | |
| 1100 | def test_close_after_close(self): |
| 1101 | data = b'content' |
| 1102 | with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipf: |
| 1103 | w = zipf.open('test', 'w') |
| 1104 | w.write(data) |
| 1105 | w.close() |
| 1106 | self.assertTrue(w.closed) |
| 1107 | w.close() |
| 1108 | self.assertTrue(w.closed) |
| 1109 | self.assertEqual(zipf.read('test'), data) |
| 1110 | |
| 1111 | def test_write_after_close(self): |
| 1112 | data = b'content' |
| 1113 | with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipf: |
| 1114 | w = zipf.open('test', 'w') |
| 1115 | w.write(data) |
| 1116 | w.close() |
| 1117 | self.assertTrue(w.closed) |
| 1118 | self.assertRaises(ValueError, w.write, b'') |
| 1119 | self.assertEqual(zipf.read('test'), data) |
| 1120 | |
| 1121 | class StoredWriterTests(AbstractWriterTests, unittest.TestCase): |
| 1122 | compression = zipfile.ZIP_STORED |
| 1123 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 1124 | @requires_zlib() |
Serhiy Storchaka | 4c0d9ea | 2017-04-12 16:03:23 +0300 | [diff] [blame] | 1125 | class DeflateWriterTests(AbstractWriterTests, unittest.TestCase): |
| 1126 | compression = zipfile.ZIP_DEFLATED |
| 1127 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 1128 | @requires_bz2() |
Serhiy Storchaka | 4c0d9ea | 2017-04-12 16:03:23 +0300 | [diff] [blame] | 1129 | class Bzip2WriterTests(AbstractWriterTests, unittest.TestCase): |
| 1130 | compression = zipfile.ZIP_BZIP2 |
| 1131 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 1132 | @requires_lzma() |
Serhiy Storchaka | 4c0d9ea | 2017-04-12 16:03:23 +0300 | [diff] [blame] | 1133 | class LzmaWriterTests(AbstractWriterTests, unittest.TestCase): |
| 1134 | compression = zipfile.ZIP_LZMA |
| 1135 | |
| 1136 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1137 | class PyZipFileTests(unittest.TestCase): |
| 1138 | def assertCompiledIn(self, name, namelist): |
| 1139 | if name + 'o' not in namelist: |
| 1140 | self.assertIn(name + 'c', namelist) |
| 1141 | |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 1142 | def requiresWriteAccess(self, path): |
Berker Peksag | e1efc07 | 2015-02-16 04:36:18 +0200 | [diff] [blame] | 1143 | # effective_ids unavailable on windows |
| 1144 | if not os.access(path, os.W_OK, |
| 1145 | effective_ids=os.access in os.supports_effective_ids): |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 1146 | self.skipTest('requires write access to the installed location') |
Serhiy Storchaka | d86a6ef | 2015-09-19 10:55:20 +0300 | [diff] [blame] | 1147 | filename = os.path.join(path, 'test_zipfile.try') |
| 1148 | try: |
| 1149 | fd = os.open(filename, os.O_WRONLY | os.O_CREAT) |
| 1150 | os.close(fd) |
| 1151 | except Exception: |
| 1152 | self.skipTest('requires write access to the installed location') |
| 1153 | unlink(filename) |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 1154 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1155 | def test_write_pyfile(self): |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 1156 | self.requiresWriteAccess(os.path.dirname(__file__)) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1157 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 1158 | fn = __file__ |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 1159 | if fn.endswith('.pyc'): |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1160 | path_split = fn.split(os.sep) |
| 1161 | if os.altsep is not None: |
| 1162 | path_split.extend(fn.split(os.altsep)) |
| 1163 | if '__pycache__' in path_split: |
Serhiy Storchaka | 9068e4d | 2013-07-22 21:02:14 +0300 | [diff] [blame] | 1164 | fn = importlib.util.source_from_cache(fn) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1165 | else: |
| 1166 | fn = fn[:-1] |
| 1167 | |
| 1168 | zipfp.writepy(fn) |
| 1169 | |
| 1170 | bn = os.path.basename(fn) |
| 1171 | self.assertNotIn(bn, zipfp.namelist()) |
| 1172 | self.assertCompiledIn(bn, zipfp.namelist()) |
| 1173 | |
| 1174 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 1175 | fn = __file__ |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 1176 | if fn.endswith('.pyc'): |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1177 | fn = fn[:-1] |
| 1178 | |
| 1179 | zipfp.writepy(fn, "testpackage") |
| 1180 | |
| 1181 | bn = "%s/%s" % ("testpackage", os.path.basename(fn)) |
| 1182 | self.assertNotIn(bn, zipfp.namelist()) |
| 1183 | self.assertCompiledIn(bn, zipfp.namelist()) |
| 1184 | |
| 1185 | def test_write_python_package(self): |
| 1186 | import email |
| 1187 | packagedir = os.path.dirname(email.__file__) |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 1188 | self.requiresWriteAccess(packagedir) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1189 | |
| 1190 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 1191 | zipfp.writepy(packagedir) |
| 1192 | |
| 1193 | # Check for a couple of modules at different levels of the |
| 1194 | # hierarchy |
| 1195 | names = zipfp.namelist() |
| 1196 | self.assertCompiledIn('email/__init__.py', names) |
| 1197 | self.assertCompiledIn('email/mime/text.py', names) |
| 1198 | |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1199 | def test_write_filtered_python_package(self): |
| 1200 | import test |
| 1201 | packagedir = os.path.dirname(test.__file__) |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 1202 | self.requiresWriteAccess(packagedir) |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1203 | |
| 1204 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 1205 | |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1206 | # first make sure that the test folder gives error messages |
Georg Brandl | a606542 | 2013-10-21 08:29:29 +0200 | [diff] [blame] | 1207 | # (on the badsyntax_... files) |
| 1208 | with captured_stdout() as reportSIO: |
| 1209 | zipfp.writepy(packagedir) |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1210 | reportStr = reportSIO.getvalue() |
| 1211 | self.assertTrue('SyntaxError' in reportStr) |
| 1212 | |
Christian Tismer | 410d931 | 2013-10-22 04:09:28 +0200 | [diff] [blame] | 1213 | # then check that the filter works on the whole package |
Georg Brandl | a606542 | 2013-10-21 08:29:29 +0200 | [diff] [blame] | 1214 | with captured_stdout() as reportSIO: |
| 1215 | zipfp.writepy(packagedir, filterfunc=lambda whatever: False) |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1216 | reportStr = reportSIO.getvalue() |
| 1217 | self.assertTrue('SyntaxError' not in reportStr) |
| 1218 | |
Christian Tismer | 410d931 | 2013-10-22 04:09:28 +0200 | [diff] [blame] | 1219 | # then check that the filter works on individual files |
Larry Hastings | 7e63b36 | 2015-05-08 06:54:58 -0700 | [diff] [blame] | 1220 | def filter(path): |
| 1221 | return not os.path.basename(path).startswith("bad") |
Serhiy Storchaka | c46d1fa | 2014-01-20 21:59:33 +0200 | [diff] [blame] | 1222 | with captured_stdout() as reportSIO, self.assertWarns(UserWarning): |
Larry Hastings | 7e63b36 | 2015-05-08 06:54:58 -0700 | [diff] [blame] | 1223 | zipfp.writepy(packagedir, filterfunc=filter) |
Christian Tismer | 410d931 | 2013-10-22 04:09:28 +0200 | [diff] [blame] | 1224 | reportStr = reportSIO.getvalue() |
| 1225 | if reportStr: |
| 1226 | print(reportStr) |
| 1227 | self.assertTrue('SyntaxError' not in reportStr) |
| 1228 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1229 | def test_write_with_optimization(self): |
| 1230 | import email |
| 1231 | packagedir = os.path.dirname(email.__file__) |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 1232 | self.requiresWriteAccess(packagedir) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1233 | optlevel = 1 if __debug__ else 0 |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 1234 | ext = '.pyc' |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1235 | |
| 1236 | with TemporaryFile() as t, \ |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1237 | zipfile.PyZipFile(t, "w", optimize=optlevel) as zipfp: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1238 | zipfp.writepy(packagedir) |
| 1239 | |
| 1240 | names = zipfp.namelist() |
| 1241 | self.assertIn('email/__init__' + ext, names) |
| 1242 | self.assertIn('email/mime/text' + ext, names) |
| 1243 | |
| 1244 | def test_write_python_directory(self): |
| 1245 | os.mkdir(TESTFN2) |
| 1246 | try: |
| 1247 | with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: |
| 1248 | fp.write("print(42)\n") |
| 1249 | |
| 1250 | with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: |
| 1251 | fp.write("print(42 * 42)\n") |
| 1252 | |
| 1253 | with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: |
| 1254 | fp.write("bla bla bla\n") |
| 1255 | |
| 1256 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 1257 | zipfp.writepy(TESTFN2) |
| 1258 | |
| 1259 | names = zipfp.namelist() |
| 1260 | self.assertCompiledIn('mod1.py', names) |
| 1261 | self.assertCompiledIn('mod2.py', names) |
| 1262 | self.assertNotIn('mod2.txt', names) |
| 1263 | |
| 1264 | finally: |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 1265 | rmtree(TESTFN2) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1266 | |
Christian Tismer | 410d931 | 2013-10-22 04:09:28 +0200 | [diff] [blame] | 1267 | def test_write_python_directory_filtered(self): |
| 1268 | os.mkdir(TESTFN2) |
| 1269 | try: |
| 1270 | with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: |
| 1271 | fp.write("print(42)\n") |
| 1272 | |
| 1273 | with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: |
| 1274 | fp.write("print(42 * 42)\n") |
| 1275 | |
| 1276 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 1277 | zipfp.writepy(TESTFN2, filterfunc=lambda fn: |
| 1278 | not fn.endswith('mod2.py')) |
| 1279 | |
| 1280 | names = zipfp.namelist() |
| 1281 | self.assertCompiledIn('mod1.py', names) |
| 1282 | self.assertNotIn('mod2.py', names) |
| 1283 | |
| 1284 | finally: |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 1285 | rmtree(TESTFN2) |
Christian Tismer | 410d931 | 2013-10-22 04:09:28 +0200 | [diff] [blame] | 1286 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1287 | def test_write_non_pyfile(self): |
| 1288 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 1289 | with open(TESTFN, 'w') as f: |
| 1290 | f.write('most definitely not a python file') |
| 1291 | self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) |
Victor Stinner | 88b215e | 2014-09-04 00:51:09 +0200 | [diff] [blame] | 1292 | unlink(TESTFN) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1293 | |
| 1294 | def test_write_pyfile_bad_syntax(self): |
| 1295 | os.mkdir(TESTFN2) |
| 1296 | try: |
| 1297 | with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: |
| 1298 | fp.write("Bad syntax in python file\n") |
| 1299 | |
| 1300 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 1301 | # syntax errors are printed to stdout |
| 1302 | with captured_stdout() as s: |
| 1303 | zipfp.writepy(os.path.join(TESTFN2, "mod1.py")) |
| 1304 | |
| 1305 | self.assertIn("SyntaxError", s.getvalue()) |
| 1306 | |
| 1307 | # as it will not have compiled the python file, it will |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 1308 | # include the .py file not .pyc |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1309 | names = zipfp.namelist() |
| 1310 | self.assertIn('mod1.py', names) |
| 1311 | self.assertNotIn('mod1.pyc', names) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1312 | |
| 1313 | finally: |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 1314 | rmtree(TESTFN2) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1315 | |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1316 | def test_write_pathlike(self): |
| 1317 | os.mkdir(TESTFN2) |
| 1318 | try: |
| 1319 | with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: |
| 1320 | fp.write("print(42)\n") |
| 1321 | |
| 1322 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 1323 | zipfp.writepy(pathlib.Path(TESTFN2) / "mod1.py") |
| 1324 | names = zipfp.namelist() |
| 1325 | self.assertCompiledIn('mod1.py', names) |
| 1326 | finally: |
| 1327 | rmtree(TESTFN2) |
| 1328 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1329 | |
| 1330 | class ExtractTests(unittest.TestCase): |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1331 | |
| 1332 | def make_test_file(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1333 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 1334 | for fpath, fdata in SMALL_TEST_DATA: |
| 1335 | zipfp.writestr(fpath, fdata) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1336 | |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1337 | def test_extract(self): |
| 1338 | with temp_cwd(): |
| 1339 | self.make_test_file() |
| 1340 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 1341 | for fpath, fdata in SMALL_TEST_DATA: |
| 1342 | writtenfile = zipfp.extract(fpath) |
| 1343 | |
| 1344 | # make sure it was written to the right place |
| 1345 | correctfile = os.path.join(os.getcwd(), fpath) |
| 1346 | correctfile = os.path.normpath(correctfile) |
| 1347 | |
| 1348 | self.assertEqual(writtenfile, correctfile) |
| 1349 | |
| 1350 | # make sure correct data is in correct file |
| 1351 | with open(writtenfile, "rb") as f: |
| 1352 | self.assertEqual(fdata.encode(), f.read()) |
| 1353 | |
| 1354 | unlink(writtenfile) |
| 1355 | |
| 1356 | def _test_extract_with_target(self, target): |
| 1357 | self.make_test_file() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1358 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 1359 | for fpath, fdata in SMALL_TEST_DATA: |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1360 | writtenfile = zipfp.extract(fpath, target) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1361 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1362 | # make sure it was written to the right place |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1363 | correctfile = os.path.join(target, fpath) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1364 | correctfile = os.path.normpath(correctfile) |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1365 | self.assertTrue(os.path.samefile(writtenfile, correctfile), (writtenfile, target)) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1366 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1367 | # make sure correct data is in correct file |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1368 | with open(writtenfile, "rb") as f: |
| 1369 | self.assertEqual(fdata.encode(), f.read()) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1370 | |
Victor Stinner | 88b215e | 2014-09-04 00:51:09 +0200 | [diff] [blame] | 1371 | unlink(writtenfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1372 | |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1373 | unlink(TESTFN2) |
| 1374 | |
| 1375 | def test_extract_with_target(self): |
| 1376 | with temp_dir() as extdir: |
| 1377 | self._test_extract_with_target(extdir) |
| 1378 | |
| 1379 | def test_extract_with_target_pathlike(self): |
| 1380 | with temp_dir() as extdir: |
| 1381 | self._test_extract_with_target(pathlib.Path(extdir)) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1382 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1383 | def test_extract_all(self): |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1384 | with temp_cwd(): |
| 1385 | self.make_test_file() |
| 1386 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 1387 | zipfp.extractall() |
| 1388 | for fpath, fdata in SMALL_TEST_DATA: |
| 1389 | outfile = os.path.join(os.getcwd(), fpath) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1390 | |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1391 | with open(outfile, "rb") as f: |
| 1392 | self.assertEqual(fdata.encode(), f.read()) |
| 1393 | |
| 1394 | unlink(outfile) |
| 1395 | |
| 1396 | def _test_extract_all_with_target(self, target): |
| 1397 | self.make_test_file() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1398 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1399 | zipfp.extractall(target) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1400 | for fpath, fdata in SMALL_TEST_DATA: |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1401 | outfile = os.path.join(target, fpath) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1402 | |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1403 | with open(outfile, "rb") as f: |
| 1404 | self.assertEqual(fdata.encode(), f.read()) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1405 | |
Victor Stinner | 88b215e | 2014-09-04 00:51:09 +0200 | [diff] [blame] | 1406 | unlink(outfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1407 | |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1408 | unlink(TESTFN2) |
| 1409 | |
| 1410 | def test_extract_all_with_target(self): |
| 1411 | with temp_dir() as extdir: |
| 1412 | self._test_extract_all_with_target(extdir) |
| 1413 | |
| 1414 | def test_extract_all_with_target_pathlike(self): |
| 1415 | with temp_dir() as extdir: |
| 1416 | self._test_extract_all_with_target(pathlib.Path(extdir)) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1417 | |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1418 | def check_file(self, filename, content): |
| 1419 | self.assertTrue(os.path.isfile(filename)) |
| 1420 | with open(filename, 'rb') as f: |
| 1421 | self.assertEqual(f.read(), content) |
| 1422 | |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1423 | def test_sanitize_windows_name(self): |
| 1424 | san = zipfile.ZipFile._sanitize_windows_name |
| 1425 | # Passing pathsep in allows this test to work regardless of platform. |
| 1426 | self.assertEqual(san(r',,?,C:,foo,bar/z', ','), r'_,C_,foo,bar/z') |
| 1427 | self.assertEqual(san(r'a\b,c<d>e|f"g?h*i', ','), r'a\b,c_d_e_f_g_h_i') |
| 1428 | self.assertEqual(san('../../foo../../ba..r', '/'), r'foo/ba..r') |
| 1429 | |
| 1430 | def test_extract_hackers_arcnames_common_cases(self): |
| 1431 | common_hacknames = [ |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1432 | ('../foo/bar', 'foo/bar'), |
| 1433 | ('foo/../bar', 'foo/bar'), |
| 1434 | ('foo/../../bar', 'foo/bar'), |
| 1435 | ('foo/bar/..', 'foo/bar'), |
| 1436 | ('./../foo/bar', 'foo/bar'), |
| 1437 | ('/foo/bar', 'foo/bar'), |
| 1438 | ('/foo/../bar', 'foo/bar'), |
| 1439 | ('/foo/../../bar', 'foo/bar'), |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1440 | ] |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1441 | self._test_extract_hackers_arcnames(common_hacknames) |
| 1442 | |
| 1443 | @unittest.skipIf(os.path.sep != '\\', 'Requires \\ as path separator.') |
| 1444 | def test_extract_hackers_arcnames_windows_only(self): |
| 1445 | """Test combination of path fixing and windows name sanitization.""" |
| 1446 | windows_hacknames = [ |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1447 | (r'..\foo\bar', 'foo/bar'), |
| 1448 | (r'..\/foo\/bar', 'foo/bar'), |
| 1449 | (r'foo/\..\/bar', 'foo/bar'), |
| 1450 | (r'foo\/../\bar', 'foo/bar'), |
| 1451 | (r'C:foo/bar', 'foo/bar'), |
| 1452 | (r'C:/foo/bar', 'foo/bar'), |
| 1453 | (r'C://foo/bar', 'foo/bar'), |
| 1454 | (r'C:\foo\bar', 'foo/bar'), |
| 1455 | (r'//conky/mountpoint/foo/bar', 'foo/bar'), |
| 1456 | (r'\\conky\mountpoint\foo\bar', 'foo/bar'), |
| 1457 | (r'///conky/mountpoint/foo/bar', 'conky/mountpoint/foo/bar'), |
| 1458 | (r'\\\conky\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'), |
| 1459 | (r'//conky//mountpoint/foo/bar', 'conky/mountpoint/foo/bar'), |
| 1460 | (r'\\conky\\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'), |
| 1461 | (r'//?/C:/foo/bar', 'foo/bar'), |
| 1462 | (r'\\?\C:\foo\bar', 'foo/bar'), |
| 1463 | (r'C:/../C:/foo/bar', 'C_/foo/bar'), |
| 1464 | (r'a:b\c<d>e|f"g?h*i', 'b/c_d_e_f_g_h_i'), |
| 1465 | ('../../foo../../ba..r', 'foo/ba..r'), |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1466 | ] |
| 1467 | self._test_extract_hackers_arcnames(windows_hacknames) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1468 | |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1469 | @unittest.skipIf(os.path.sep != '/', r'Requires / as path separator.') |
| 1470 | def test_extract_hackers_arcnames_posix_only(self): |
| 1471 | posix_hacknames = [ |
| 1472 | ('//foo/bar', 'foo/bar'), |
| 1473 | ('../../foo../../ba..r', 'foo../ba..r'), |
| 1474 | (r'foo/..\bar', r'foo/..\bar'), |
| 1475 | ] |
| 1476 | self._test_extract_hackers_arcnames(posix_hacknames) |
| 1477 | |
| 1478 | def _test_extract_hackers_arcnames(self, hacknames): |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1479 | for arcname, fixedname in hacknames: |
| 1480 | content = b'foobar' + arcname.encode() |
| 1481 | with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipfp: |
Serhiy Storchaka | e5e6444 | 2013-02-02 19:50:59 +0200 | [diff] [blame] | 1482 | zinfo = zipfile.ZipInfo() |
| 1483 | # preserve backslashes |
| 1484 | zinfo.filename = arcname |
| 1485 | zinfo.external_attr = 0o600 << 16 |
| 1486 | zipfp.writestr(zinfo, content) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1487 | |
Serhiy Storchaka | e5e6444 | 2013-02-02 19:50:59 +0200 | [diff] [blame] | 1488 | arcname = arcname.replace(os.sep, "/") |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1489 | targetpath = os.path.join('target', 'subdir', 'subsub') |
| 1490 | correctfile = os.path.join(targetpath, *fixedname.split('/')) |
| 1491 | |
| 1492 | with zipfile.ZipFile(TESTFN2, 'r') as zipfp: |
| 1493 | writtenfile = zipfp.extract(arcname, targetpath) |
Serhiy Storchaka | e5e6444 | 2013-02-02 19:50:59 +0200 | [diff] [blame] | 1494 | self.assertEqual(writtenfile, correctfile, |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1495 | msg='extract %r: %r != %r' % |
| 1496 | (arcname, writtenfile, correctfile)) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1497 | self.check_file(correctfile, content) |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 1498 | rmtree('target') |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1499 | |
| 1500 | with zipfile.ZipFile(TESTFN2, 'r') as zipfp: |
| 1501 | zipfp.extractall(targetpath) |
| 1502 | self.check_file(correctfile, content) |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 1503 | rmtree('target') |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1504 | |
| 1505 | correctfile = os.path.join(os.getcwd(), *fixedname.split('/')) |
| 1506 | |
| 1507 | with zipfile.ZipFile(TESTFN2, 'r') as zipfp: |
| 1508 | writtenfile = zipfp.extract(arcname) |
Serhiy Storchaka | e5e6444 | 2013-02-02 19:50:59 +0200 | [diff] [blame] | 1509 | self.assertEqual(writtenfile, correctfile, |
| 1510 | msg="extract %r" % arcname) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1511 | self.check_file(correctfile, content) |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 1512 | rmtree(fixedname.split('/')[0]) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1513 | |
| 1514 | with zipfile.ZipFile(TESTFN2, 'r') as zipfp: |
| 1515 | zipfp.extractall() |
| 1516 | self.check_file(correctfile, content) |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 1517 | rmtree(fixedname.split('/')[0]) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1518 | |
Victor Stinner | 88b215e | 2014-09-04 00:51:09 +0200 | [diff] [blame] | 1519 | unlink(TESTFN2) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1520 | |
Ronald Oussoren | ee5c885 | 2010-02-07 20:24:02 +0000 | [diff] [blame] | 1521 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1522 | class OtherTests(unittest.TestCase): |
| 1523 | def test_open_via_zip_info(self): |
| 1524 | # Create the ZIP archive |
| 1525 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 1526 | zipfp.writestr("name", "foo") |
Serhiy Storchaka | 9b7a1a1 | 2014-01-20 21:57:40 +0200 | [diff] [blame] | 1527 | with self.assertWarns(UserWarning): |
| 1528 | zipfp.writestr("name", "bar") |
| 1529 | self.assertEqual(zipfp.namelist(), ["name"] * 2) |
Ronald Oussoren | ee5c885 | 2010-02-07 20:24:02 +0000 | [diff] [blame] | 1530 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1531 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 1532 | infos = zipfp.infolist() |
| 1533 | data = b"" |
| 1534 | for info in infos: |
| 1535 | with zipfp.open(info) as zipopen: |
| 1536 | data += zipopen.read() |
| 1537 | self.assertIn(data, {b"foobar", b"barfoo"}) |
| 1538 | data = b"" |
| 1539 | for info in infos: |
| 1540 | data += zipfp.read(info) |
| 1541 | self.assertIn(data, {b"foobar", b"barfoo"}) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1542 | |
Gregory P. Smith | b0d9ca9 | 2009-07-07 05:06:04 +0000 | [diff] [blame] | 1543 | def test_writestr_extended_local_header_issue1202(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1544 | with zipfile.ZipFile(TESTFN2, 'w') as orig_zip: |
| 1545 | for data in 'abcdefghijklmnop': |
| 1546 | zinfo = zipfile.ZipInfo(data) |
| 1547 | zinfo.flag_bits |= 0x08 # Include an extended local header. |
| 1548 | orig_zip.writestr(zinfo, data) |
| 1549 | |
| 1550 | def test_close(self): |
| 1551 | """Check that the zipfile is closed after the 'with' block.""" |
| 1552 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 1553 | for fpath, fdata in SMALL_TEST_DATA: |
| 1554 | zipfp.writestr(fpath, fdata) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1555 | self.assertIsNotNone(zipfp.fp, 'zipfp is not open') |
| 1556 | self.assertIsNone(zipfp.fp, 'zipfp is not closed') |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1557 | |
| 1558 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1559 | self.assertIsNotNone(zipfp.fp, 'zipfp is not open') |
| 1560 | self.assertIsNone(zipfp.fp, 'zipfp is not closed') |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1561 | |
| 1562 | def test_close_on_exception(self): |
| 1563 | """Check that the zipfile is closed if an exception is raised in the |
| 1564 | 'with' block.""" |
| 1565 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 1566 | for fpath, fdata in SMALL_TEST_DATA: |
| 1567 | zipfp.writestr(fpath, fdata) |
| 1568 | |
| 1569 | try: |
| 1570 | with zipfile.ZipFile(TESTFN2, "r") as zipfp2: |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1571 | raise zipfile.BadZipFile() |
| 1572 | except zipfile.BadZipFile: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1573 | self.assertIsNone(zipfp2.fp, 'zipfp is not closed') |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1574 | |
Martin v. Löwis | d099b56 | 2012-05-01 14:08:22 +0200 | [diff] [blame] | 1575 | def test_unsupported_version(self): |
| 1576 | # File has an extract_version of 120 |
| 1577 | data = (b'PK\x03\x04x\x00\x00\x00\x00\x00!p\xa1@\x00\x00\x00\x00\x00\x00' |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1578 | b'\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00xPK\x01\x02x\x03x\x00\x00\x00\x00' |
| 1579 | b'\x00!p\xa1@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00' |
| 1580 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00\x00xPK\x05\x06' |
| 1581 | b'\x00\x00\x00\x00\x01\x00\x01\x00/\x00\x00\x00\x1f\x00\x00\x00\x00\x00') |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1582 | |
Martin v. Löwis | d099b56 | 2012-05-01 14:08:22 +0200 | [diff] [blame] | 1583 | self.assertRaises(NotImplementedError, zipfile.ZipFile, |
| 1584 | io.BytesIO(data), 'r') |
| 1585 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 1586 | @requires_zlib() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1587 | def test_read_unicode_filenames(self): |
| 1588 | # bug #10801 |
| 1589 | fname = findfile('zip_cp437_header.zip') |
| 1590 | with zipfile.ZipFile(fname) as zipfp: |
| 1591 | for name in zipfp.namelist(): |
| 1592 | zipfp.open(name).close() |
| 1593 | |
| 1594 | def test_write_unicode_filenames(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1595 | with zipfile.ZipFile(TESTFN, "w") as zf: |
| 1596 | zf.writestr("foo.txt", "Test for unicode filename") |
| 1597 | zf.writestr("\xf6.txt", "Test for unicode filename") |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 1598 | self.assertIsInstance(zf.infolist()[0].filename, str) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1599 | |
| 1600 | with zipfile.ZipFile(TESTFN, "r") as zf: |
| 1601 | self.assertEqual(zf.filelist[0].filename, "foo.txt") |
| 1602 | self.assertEqual(zf.filelist[1].filename, "\xf6.txt") |
Martin v. Löwis | 8570f6a | 2008-05-05 17:44:38 +0000 | [diff] [blame] | 1603 | |
Serhiy Storchaka | 36ff513 | 2020-06-22 11:24:11 +0300 | [diff] [blame] | 1604 | def test_read_after_write_unicode_filenames(self): |
| 1605 | with zipfile.ZipFile(TESTFN2, 'w') as zipfp: |
| 1606 | zipfp.writestr('приклад', b'sample') |
| 1607 | self.assertEqual(zipfp.read('приклад'), b'sample') |
| 1608 | |
Serhiy Storchaka | 764fc9b | 2015-03-25 10:09:41 +0200 | [diff] [blame] | 1609 | def test_exclusive_create_zip_file(self): |
| 1610 | """Test exclusive creating a new zipfile.""" |
| 1611 | unlink(TESTFN2) |
| 1612 | filename = 'testfile.txt' |
| 1613 | content = b'hello, world. this is some content.' |
| 1614 | with zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED) as zipfp: |
| 1615 | zipfp.writestr(filename, content) |
| 1616 | with self.assertRaises(FileExistsError): |
| 1617 | zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED) |
| 1618 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 1619 | self.assertEqual(zipfp.namelist(), [filename]) |
| 1620 | self.assertEqual(zipfp.read(filename), content) |
| 1621 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1622 | def test_create_non_existent_file_for_append(self): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1623 | if os.path.exists(TESTFN): |
| 1624 | os.unlink(TESTFN) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1625 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1626 | filename = 'testfile.txt' |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1627 | content = b'hello, world. this is some content.' |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1628 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1629 | try: |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1630 | with zipfile.ZipFile(TESTFN, 'a') as zf: |
| 1631 | zf.writestr(filename, content) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1632 | except OSError: |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1633 | self.fail('Could not append data to a non-existent zip file.') |
| 1634 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1635 | self.assertTrue(os.path.exists(TESTFN)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1636 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1637 | with zipfile.ZipFile(TESTFN, 'r') as zf: |
| 1638 | self.assertEqual(zf.read(filename), content) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1639 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1640 | def test_close_erroneous_file(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1641 | # This test checks that the ZipFile constructor closes the file object |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1642 | # it opens if there's an error in the file. If it doesn't, the |
| 1643 | # traceback holds a reference to the ZipFile object and, indirectly, |
| 1644 | # the file object. |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1645 | # On Windows, this causes the os.unlink() call to fail because the |
| 1646 | # underlying file is still open. This is SF bug #412214. |
| 1647 | # |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1648 | with open(TESTFN, "w") as fp: |
| 1649 | fp.write("this is not a legal zip file\n") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1650 | try: |
| 1651 | zf = zipfile.ZipFile(TESTFN) |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1652 | except zipfile.BadZipFile: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1653 | pass |
| 1654 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1655 | def test_is_zip_erroneous_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1656 | """Check that is_zipfile() correctly identifies non-zip files.""" |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 1657 | # - passing a filename |
| 1658 | with open(TESTFN, "w") as fp: |
| 1659 | fp.write("this is not a legal zip file\n") |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1660 | self.assertFalse(zipfile.is_zipfile(TESTFN)) |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1661 | # - passing a path-like object |
| 1662 | self.assertFalse(zipfile.is_zipfile(pathlib.Path(TESTFN))) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 1663 | # - passing a file object |
| 1664 | with open(TESTFN, "rb") as fp: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1665 | self.assertFalse(zipfile.is_zipfile(fp)) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 1666 | # - passing a file-like object |
| 1667 | fp = io.BytesIO() |
| 1668 | fp.write(b"this is not a legal zip file\n") |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1669 | self.assertFalse(zipfile.is_zipfile(fp)) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1670 | fp.seek(0, 0) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1671 | self.assertFalse(zipfile.is_zipfile(fp)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1672 | |
Serhiy Storchaka | d2b1527 | 2013-01-31 15:27:07 +0200 | [diff] [blame] | 1673 | def test_damaged_zipfile(self): |
| 1674 | """Check that zipfiles with missing bytes at the end raise BadZipFile.""" |
| 1675 | # - Create a valid zip file |
| 1676 | fp = io.BytesIO() |
| 1677 | with zipfile.ZipFile(fp, mode="w") as zipf: |
| 1678 | zipf.writestr("foo.txt", b"O, for a Muse of Fire!") |
| 1679 | zipfiledata = fp.getvalue() |
| 1680 | |
| 1681 | # - Now create copies of it missing the last N bytes and make sure |
| 1682 | # a BadZipFile exception is raised when we try to open it |
| 1683 | for N in range(len(zipfiledata)): |
| 1684 | fp = io.BytesIO(zipfiledata[:N]) |
| 1685 | self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, fp) |
| 1686 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1687 | def test_is_zip_valid_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1688 | """Check that is_zipfile() correctly identifies zip files.""" |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 1689 | # - passing a filename |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1690 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1691 | zipf.writestr("foo.txt", b"O, for a Muse of Fire!") |
| 1692 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1693 | self.assertTrue(zipfile.is_zipfile(TESTFN)) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 1694 | # - passing a file object |
| 1695 | with open(TESTFN, "rb") as fp: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1696 | self.assertTrue(zipfile.is_zipfile(fp)) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1697 | fp.seek(0, 0) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 1698 | zip_contents = fp.read() |
| 1699 | # - passing a file-like object |
| 1700 | fp = io.BytesIO() |
| 1701 | fp.write(zip_contents) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1702 | self.assertTrue(zipfile.is_zipfile(fp)) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1703 | fp.seek(0, 0) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1704 | self.assertTrue(zipfile.is_zipfile(fp)) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1705 | |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1706 | def test_non_existent_file_raises_OSError(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1707 | # make sure we don't raise an AttributeError when a partially-constructed |
| 1708 | # ZipFile instance is finalized; this tests for regression on SF tracker |
| 1709 | # bug #403871. |
| 1710 | |
| 1711 | # The bug we're testing for caused an AttributeError to be raised |
| 1712 | # when a ZipFile instance was created for a file that did not |
| 1713 | # exist; the .fp member was not initialized but was needed by the |
| 1714 | # __del__() method. Since the AttributeError is in the __del__(), |
| 1715 | # it is ignored, but the user should be sufficiently annoyed by |
| 1716 | # the message on the output that regression will be noticed |
| 1717 | # quickly. |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1718 | self.assertRaises(OSError, zipfile.ZipFile, TESTFN) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1719 | |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 1720 | def test_empty_file_raises_BadZipFile(self): |
| 1721 | f = open(TESTFN, 'w') |
| 1722 | f.close() |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1723 | self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN) |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 1724 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1725 | with open(TESTFN, 'w') as fp: |
| 1726 | fp.write("short file") |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1727 | self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN) |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 1728 | |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1729 | def test_closed_zip_raises_ValueError(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1730 | """Verify that testzip() doesn't swallow inappropriate exceptions.""" |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1731 | data = io.BytesIO() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1732 | with zipfile.ZipFile(data, mode="w") as zipf: |
| 1733 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1734 | |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 1735 | # This is correct; calling .read on a closed ZipFile should raise |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1736 | # a ValueError, and so should calling .testzip. An earlier |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1737 | # version of .testzip would swallow this exception (and any other) |
| 1738 | # and report that the first file in the archive was corrupt. |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1739 | self.assertRaises(ValueError, zipf.read, "foo.txt") |
| 1740 | self.assertRaises(ValueError, zipf.open, "foo.txt") |
| 1741 | self.assertRaises(ValueError, zipf.testzip) |
| 1742 | self.assertRaises(ValueError, zipf.writestr, "bogus.txt", "bogus") |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1743 | with open(TESTFN, 'w') as f: |
| 1744 | f.write('zipfile test data') |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1745 | self.assertRaises(ValueError, zipf.write, TESTFN) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1746 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1747 | def test_bad_constructor_mode(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1748 | """Check that bad modes passed to ZipFile constructor are caught.""" |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1749 | self.assertRaises(ValueError, zipfile.ZipFile, TESTFN, "q") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1750 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1751 | def test_bad_open_mode(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1752 | """Check that bad modes passed to ZipFile.open are caught.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1753 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1754 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1755 | |
| 1756 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
Serhiy Storchaka | e670be2 | 2016-06-11 19:32:44 +0300 | [diff] [blame] | 1757 | # read the data to make sure the file is there |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1758 | zipf.read("foo.txt") |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1759 | self.assertRaises(ValueError, zipf.open, "foo.txt", "q") |
Serhiy Storchaka | e670be2 | 2016-06-11 19:32:44 +0300 | [diff] [blame] | 1760 | # universal newlines support is removed |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1761 | self.assertRaises(ValueError, zipf.open, "foo.txt", "U") |
| 1762 | self.assertRaises(ValueError, zipf.open, "foo.txt", "rU") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1763 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1764 | def test_read0(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1765 | """Check that calling read(0) on a ZipExtFile object returns an empty |
| 1766 | string and doesn't advance file pointer.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1767 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1768 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1769 | # read the data to make sure the file is there |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1770 | with zipf.open("foo.txt") as f: |
| 1771 | for i in range(FIXEDTEST_SIZE): |
| 1772 | self.assertEqual(f.read(0), b'') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1773 | |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1774 | self.assertEqual(f.read(), b"O, for a Muse of Fire!") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1775 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1776 | def test_open_non_existent_item(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1777 | """Check that attempting to call open() for an item that doesn't |
| 1778 | exist in the archive raises a RuntimeError.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1779 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1780 | self.assertRaises(KeyError, zipf.open, "foo.txt", "r") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1781 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1782 | def test_bad_compression_mode(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1783 | """Check that bad compression methods passed to ZipFile.open are |
| 1784 | caught.""" |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1785 | self.assertRaises(NotImplementedError, zipfile.ZipFile, TESTFN, "w", -1) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1786 | |
Martin v. Löwis | b3260f0 | 2012-05-01 08:38:01 +0200 | [diff] [blame] | 1787 | def test_unsupported_compression(self): |
| 1788 | # data is declared as shrunk, but actually deflated |
| 1789 | data = (b'PK\x03\x04.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00' |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1790 | b'\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00x\x03\x00PK\x01' |
| 1791 | b'\x02.\x03.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00\x00\x02\x00\x00' |
| 1792 | b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 1793 | b'\x80\x01\x00\x00\x00\x00xPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00' |
| 1794 | b'/\x00\x00\x00!\x00\x00\x00\x00\x00') |
Martin v. Löwis | b3260f0 | 2012-05-01 08:38:01 +0200 | [diff] [blame] | 1795 | with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf: |
| 1796 | self.assertRaises(NotImplementedError, zipf.open, 'x') |
| 1797 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1798 | def test_null_byte_in_filename(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1799 | """Check that a filename containing a null byte is properly |
| 1800 | terminated.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1801 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1802 | zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!") |
| 1803 | self.assertEqual(zipf.namelist(), ['foo.txt']) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1804 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1805 | def test_struct_sizes(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1806 | """Check that ZIP internal structure sizes are calculated correctly.""" |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1807 | self.assertEqual(zipfile.sizeEndCentDir, 22) |
| 1808 | self.assertEqual(zipfile.sizeCentralDir, 46) |
| 1809 | self.assertEqual(zipfile.sizeEndCentDir64, 56) |
| 1810 | self.assertEqual(zipfile.sizeEndCentDir64Locator, 20) |
| 1811 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1812 | def test_comments(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1813 | """Check that comments on the archive are handled properly.""" |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1814 | |
| 1815 | # check default comment is empty |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1816 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1817 | self.assertEqual(zipf.comment, b'') |
| 1818 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1819 | |
| 1820 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 1821 | self.assertEqual(zipfr.comment, b'') |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1822 | |
| 1823 | # check a simple short comment |
| 1824 | comment = b'Bravely taking to his feet, he beat a very brave retreat.' |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1825 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1826 | zipf.comment = comment |
| 1827 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1828 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 1829 | self.assertEqual(zipf.comment, comment) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1830 | |
| 1831 | # check a comment of max length |
| 1832 | comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)]) |
| 1833 | comment2 = comment2.encode("ascii") |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1834 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1835 | zipf.comment = comment2 |
| 1836 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1837 | |
| 1838 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 1839 | self.assertEqual(zipfr.comment, comment2) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1840 | |
| 1841 | # check a comment that is too long is truncated |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1842 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
Serhiy Storchaka | 9b7a1a1 | 2014-01-20 21:57:40 +0200 | [diff] [blame] | 1843 | with self.assertWarns(UserWarning): |
| 1844 | zipf.comment = comment2 + b'oops' |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1845 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1846 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 1847 | self.assertEqual(zipfr.comment, comment2) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1848 | |
Antoine Pitrou | c399185 | 2012-06-30 17:31:37 +0200 | [diff] [blame] | 1849 | # check that comments are correctly modified in append mode |
| 1850 | with zipfile.ZipFile(TESTFN,mode="w") as zipf: |
| 1851 | zipf.comment = b"original comment" |
| 1852 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1853 | with zipfile.ZipFile(TESTFN,mode="a") as zipf: |
| 1854 | zipf.comment = b"an updated comment" |
| 1855 | with zipfile.ZipFile(TESTFN,mode="r") as zipf: |
| 1856 | self.assertEqual(zipf.comment, b"an updated comment") |
| 1857 | |
| 1858 | # check that comments are correctly shortened in append mode |
Jan Mazur | ff9147d | 2020-09-28 20:53:33 +0200 | [diff] [blame^] | 1859 | # and the file is indeed truncated |
Antoine Pitrou | c399185 | 2012-06-30 17:31:37 +0200 | [diff] [blame] | 1860 | with zipfile.ZipFile(TESTFN,mode="w") as zipf: |
| 1861 | zipf.comment = b"original comment that's longer" |
| 1862 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
Jan Mazur | ff9147d | 2020-09-28 20:53:33 +0200 | [diff] [blame^] | 1863 | original_zip_size = os.path.getsize(TESTFN) |
Antoine Pitrou | c399185 | 2012-06-30 17:31:37 +0200 | [diff] [blame] | 1864 | with zipfile.ZipFile(TESTFN,mode="a") as zipf: |
| 1865 | zipf.comment = b"shorter comment" |
Jan Mazur | ff9147d | 2020-09-28 20:53:33 +0200 | [diff] [blame^] | 1866 | self.assertTrue(original_zip_size > os.path.getsize(TESTFN)) |
Antoine Pitrou | c399185 | 2012-06-30 17:31:37 +0200 | [diff] [blame] | 1867 | with zipfile.ZipFile(TESTFN,mode="r") as zipf: |
| 1868 | self.assertEqual(zipf.comment, b"shorter comment") |
| 1869 | |
R David Murray | f50b38a | 2012-04-12 18:44:58 -0400 | [diff] [blame] | 1870 | def test_unicode_comment(self): |
| 1871 | with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf: |
| 1872 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1873 | with self.assertRaises(TypeError): |
| 1874 | zipf.comment = "this is an error" |
| 1875 | |
| 1876 | def test_change_comment_in_empty_archive(self): |
| 1877 | with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf: |
| 1878 | self.assertFalse(zipf.filelist) |
| 1879 | zipf.comment = b"this is a comment" |
| 1880 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 1881 | self.assertEqual(zipf.comment, b"this is a comment") |
| 1882 | |
| 1883 | def test_change_comment_in_nonempty_archive(self): |
| 1884 | with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf: |
| 1885 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1886 | with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf: |
| 1887 | self.assertTrue(zipf.filelist) |
| 1888 | zipf.comment = b"this is a comment" |
| 1889 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 1890 | self.assertEqual(zipf.comment, b"this is a comment") |
| 1891 | |
Georg Brandl | 268e4d4 | 2010-10-14 06:59:45 +0000 | [diff] [blame] | 1892 | def test_empty_zipfile(self): |
| 1893 | # Check that creating a file in 'w' or 'a' mode and closing without |
| 1894 | # adding any files to the archives creates a valid empty ZIP file |
| 1895 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 1896 | zipf.close() |
| 1897 | try: |
| 1898 | zipf = zipfile.ZipFile(TESTFN, mode="r") |
| 1899 | except zipfile.BadZipFile: |
| 1900 | self.fail("Unable to create empty ZIP file in 'w' mode") |
| 1901 | |
| 1902 | zipf = zipfile.ZipFile(TESTFN, mode="a") |
| 1903 | zipf.close() |
| 1904 | try: |
| 1905 | zipf = zipfile.ZipFile(TESTFN, mode="r") |
| 1906 | except: |
| 1907 | self.fail("Unable to create empty ZIP file in 'a' mode") |
| 1908 | |
| 1909 | def test_open_empty_file(self): |
| 1910 | # Issue 1710703: Check that opening a file with less than 22 bytes |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1911 | # raises a BadZipFile exception (rather than the previously unhelpful |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1912 | # OSError) |
Georg Brandl | 268e4d4 | 2010-10-14 06:59:45 +0000 | [diff] [blame] | 1913 | f = open(TESTFN, 'w') |
| 1914 | f.close() |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1915 | self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r') |
Georg Brandl | 268e4d4 | 2010-10-14 06:59:45 +0000 | [diff] [blame] | 1916 | |
Senthil Kumaran | 29fa9d4 | 2011-10-20 01:46:00 +0800 | [diff] [blame] | 1917 | def test_create_zipinfo_before_1980(self): |
| 1918 | self.assertRaises(ValueError, |
| 1919 | zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0)) |
| 1920 | |
Mickaël Schoentgen | 992347d | 2019-09-09 15:08:54 +0200 | [diff] [blame] | 1921 | def test_create_empty_zipinfo_repr(self): |
| 1922 | """Before bpo-26185, repr() on empty ZipInfo object was failing.""" |
| 1923 | zi = zipfile.ZipInfo(filename="empty") |
| 1924 | self.assertEqual(repr(zi), "<ZipInfo filename='empty' file_size=0>") |
| 1925 | |
| 1926 | def test_create_empty_zipinfo_default_attributes(self): |
| 1927 | """Ensure all required attributes are set.""" |
| 1928 | zi = zipfile.ZipInfo() |
| 1929 | self.assertEqual(zi.orig_filename, "NoName") |
| 1930 | self.assertEqual(zi.filename, "NoName") |
| 1931 | self.assertEqual(zi.date_time, (1980, 1, 1, 0, 0, 0)) |
| 1932 | self.assertEqual(zi.compress_type, zipfile.ZIP_STORED) |
| 1933 | self.assertEqual(zi.comment, b"") |
| 1934 | self.assertEqual(zi.extra, b"") |
| 1935 | self.assertIn(zi.create_system, (0, 3)) |
| 1936 | self.assertEqual(zi.create_version, zipfile.DEFAULT_VERSION) |
| 1937 | self.assertEqual(zi.extract_version, zipfile.DEFAULT_VERSION) |
| 1938 | self.assertEqual(zi.reserved, 0) |
| 1939 | self.assertEqual(zi.flag_bits, 0) |
| 1940 | self.assertEqual(zi.volume, 0) |
| 1941 | self.assertEqual(zi.internal_attr, 0) |
| 1942 | self.assertEqual(zi.external_attr, 0) |
| 1943 | |
| 1944 | # Before bpo-26185, both were missing |
| 1945 | self.assertEqual(zi.file_size, 0) |
| 1946 | self.assertEqual(zi.compress_size, 0) |
| 1947 | |
Gregory P. Smith | 0af8a86 | 2014-05-29 23:42:14 -0700 | [diff] [blame] | 1948 | def test_zipfile_with_short_extra_field(self): |
| 1949 | """If an extra field in the header is less than 4 bytes, skip it.""" |
| 1950 | zipdata = ( |
| 1951 | b'PK\x03\x04\x14\x00\x00\x00\x00\x00\x93\x9b\xad@\x8b\x9e' |
| 1952 | b'\xd9\xd3\x01\x00\x00\x00\x01\x00\x00\x00\x03\x00\x03\x00ab' |
| 1953 | b'c\x00\x00\x00APK\x01\x02\x14\x03\x14\x00\x00\x00\x00' |
| 1954 | b'\x00\x93\x9b\xad@\x8b\x9e\xd9\xd3\x01\x00\x00\x00\x01\x00\x00' |
| 1955 | b'\x00\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00' |
| 1956 | b'\x00\x00\x00abc\x00\x00PK\x05\x06\x00\x00\x00\x00' |
| 1957 | b'\x01\x00\x01\x003\x00\x00\x00%\x00\x00\x00\x00\x00' |
| 1958 | ) |
| 1959 | with zipfile.ZipFile(io.BytesIO(zipdata), 'r') as zipf: |
| 1960 | # testzip returns the name of the first corrupt file, or None |
| 1961 | self.assertIsNone(zipf.testzip()) |
| 1962 | |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 1963 | def test_open_conflicting_handles(self): |
| 1964 | # It's only possible to open one writable file handle at a time |
| 1965 | msg1 = b"It's fun to charter an accountant!" |
| 1966 | msg2 = b"And sail the wide accountant sea" |
| 1967 | msg3 = b"To find, explore the funds offshore" |
| 1968 | with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipf: |
| 1969 | with zipf.open('foo', mode='w') as w2: |
| 1970 | w2.write(msg1) |
| 1971 | with zipf.open('bar', mode='w') as w1: |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1972 | with self.assertRaises(ValueError): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 1973 | zipf.open('handle', mode='w') |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1974 | with self.assertRaises(ValueError): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 1975 | zipf.open('foo', mode='r') |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1976 | with self.assertRaises(ValueError): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 1977 | zipf.writestr('str', 'abcde') |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1978 | with self.assertRaises(ValueError): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 1979 | zipf.write(__file__, 'file') |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1980 | with self.assertRaises(ValueError): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 1981 | zipf.close() |
| 1982 | w1.write(msg2) |
| 1983 | with zipf.open('baz', mode='w') as w2: |
| 1984 | w2.write(msg3) |
| 1985 | |
| 1986 | with zipfile.ZipFile(TESTFN2, 'r') as zipf: |
| 1987 | self.assertEqual(zipf.read('foo'), msg1) |
| 1988 | self.assertEqual(zipf.read('bar'), msg2) |
| 1989 | self.assertEqual(zipf.read('baz'), msg3) |
| 1990 | self.assertEqual(zipf.namelist(), ['foo', 'bar', 'baz']) |
| 1991 | |
John Jolly | 066df4f | 2018-01-30 01:51:35 -0700 | [diff] [blame] | 1992 | def test_seek_tell(self): |
| 1993 | # Test seek functionality |
| 1994 | txt = b"Where's Bruce?" |
| 1995 | bloc = txt.find(b"Bruce") |
| 1996 | # Check seek on a file |
| 1997 | with zipfile.ZipFile(TESTFN, "w") as zipf: |
| 1998 | zipf.writestr("foo.txt", txt) |
| 1999 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 2000 | with zipf.open("foo.txt", "r") as fp: |
| 2001 | fp.seek(bloc, os.SEEK_SET) |
| 2002 | self.assertEqual(fp.tell(), bloc) |
| 2003 | fp.seek(-bloc, os.SEEK_CUR) |
| 2004 | self.assertEqual(fp.tell(), 0) |
| 2005 | fp.seek(bloc, os.SEEK_CUR) |
| 2006 | self.assertEqual(fp.tell(), bloc) |
| 2007 | self.assertEqual(fp.read(5), txt[bloc:bloc+5]) |
| 2008 | fp.seek(0, os.SEEK_END) |
| 2009 | self.assertEqual(fp.tell(), len(txt)) |
Mickaël Schoentgen | 3f8c691 | 2018-07-29 20:26:52 +0200 | [diff] [blame] | 2010 | fp.seek(0, os.SEEK_SET) |
| 2011 | self.assertEqual(fp.tell(), 0) |
John Jolly | 066df4f | 2018-01-30 01:51:35 -0700 | [diff] [blame] | 2012 | # Check seek on memory file |
| 2013 | data = io.BytesIO() |
| 2014 | with zipfile.ZipFile(data, mode="w") as zipf: |
| 2015 | zipf.writestr("foo.txt", txt) |
| 2016 | with zipfile.ZipFile(data, mode="r") as zipf: |
| 2017 | with zipf.open("foo.txt", "r") as fp: |
| 2018 | fp.seek(bloc, os.SEEK_SET) |
| 2019 | self.assertEqual(fp.tell(), bloc) |
| 2020 | fp.seek(-bloc, os.SEEK_CUR) |
| 2021 | self.assertEqual(fp.tell(), 0) |
| 2022 | fp.seek(bloc, os.SEEK_CUR) |
| 2023 | self.assertEqual(fp.tell(), bloc) |
| 2024 | self.assertEqual(fp.read(5), txt[bloc:bloc+5]) |
| 2025 | fp.seek(0, os.SEEK_END) |
| 2026 | self.assertEqual(fp.tell(), len(txt)) |
Mickaël Schoentgen | 3f8c691 | 2018-07-29 20:26:52 +0200 | [diff] [blame] | 2027 | fp.seek(0, os.SEEK_SET) |
| 2028 | self.assertEqual(fp.tell(), 0) |
John Jolly | 066df4f | 2018-01-30 01:51:35 -0700 | [diff] [blame] | 2029 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 2030 | @requires_bz2() |
Berker Peksag | 2f1b857 | 2019-09-12 17:13:44 +0300 | [diff] [blame] | 2031 | def test_decompress_without_3rd_party_library(self): |
| 2032 | data = b'PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 2033 | zip_file = io.BytesIO(data) |
| 2034 | with zipfile.ZipFile(zip_file, 'w', compression=zipfile.ZIP_BZIP2) as zf: |
| 2035 | zf.writestr('a.txt', b'a') |
| 2036 | with mock.patch('zipfile.bz2', None): |
| 2037 | with zipfile.ZipFile(zip_file) as zf: |
| 2038 | self.assertRaises(RuntimeError, zf.extract, 'a.txt') |
| 2039 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2040 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 2041 | unlink(TESTFN) |
| 2042 | unlink(TESTFN2) |
| 2043 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2044 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2045 | class AbstractBadCrcTests: |
| 2046 | def test_testzip_with_bad_crc(self): |
| 2047 | """Tests that files with bad CRCs return their name from testzip.""" |
| 2048 | zipdata = self.zip_with_bad_crc |
| 2049 | |
| 2050 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 2051 | # testzip returns the name of the first corrupt file, or None |
| 2052 | self.assertEqual('afile', zipf.testzip()) |
| 2053 | |
| 2054 | def test_read_with_bad_crc(self): |
| 2055 | """Tests that files with bad CRCs raise a BadZipFile exception when read.""" |
| 2056 | zipdata = self.zip_with_bad_crc |
| 2057 | |
| 2058 | # Using ZipFile.read() |
| 2059 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 2060 | self.assertRaises(zipfile.BadZipFile, zipf.read, 'afile') |
| 2061 | |
| 2062 | # Using ZipExtFile.read() |
| 2063 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 2064 | with zipf.open('afile', 'r') as corrupt_file: |
| 2065 | self.assertRaises(zipfile.BadZipFile, corrupt_file.read) |
| 2066 | |
| 2067 | # Same with small reads (in order to exercise the buffering logic) |
| 2068 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 2069 | with zipf.open('afile', 'r') as corrupt_file: |
| 2070 | corrupt_file.MIN_READ_SIZE = 2 |
| 2071 | with self.assertRaises(zipfile.BadZipFile): |
| 2072 | while corrupt_file.read(2): |
| 2073 | pass |
| 2074 | |
| 2075 | |
| 2076 | class StoredBadCrcTests(AbstractBadCrcTests, unittest.TestCase): |
| 2077 | compression = zipfile.ZIP_STORED |
| 2078 | zip_with_bad_crc = ( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 2079 | b'PK\003\004\024\0\0\0\0\0 \213\212;:r' |
| 2080 | b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af' |
| 2081 | b'ilehello,AworldP' |
| 2082 | b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:' |
| 2083 | b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0' |
| 2084 | b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi' |
| 2085 | b'lePK\005\006\0\0\0\0\001\0\001\0003\000' |
| 2086 | b'\0\0/\0\0\0\0\0') |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2087 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 2088 | @requires_zlib() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2089 | class DeflateBadCrcTests(AbstractBadCrcTests, unittest.TestCase): |
| 2090 | compression = zipfile.ZIP_DEFLATED |
| 2091 | zip_with_bad_crc = ( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 2092 | b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA' |
| 2093 | b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' |
| 2094 | b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0' |
| 2095 | b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n' |
| 2096 | b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05' |
| 2097 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00' |
| 2098 | b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00' |
| 2099 | b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00') |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2100 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 2101 | @requires_bz2() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2102 | class Bzip2BadCrcTests(AbstractBadCrcTests, unittest.TestCase): |
| 2103 | compression = zipfile.ZIP_BZIP2 |
| 2104 | zip_with_bad_crc = ( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 2105 | b'PK\x03\x04\x14\x03\x00\x00\x0c\x00nu\x0c=FA' |
| 2106 | b'KE8\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' |
| 2107 | b'ileBZh91AY&SY\xd4\xa8\xca' |
| 2108 | b'\x7f\x00\x00\x0f\x11\x80@\x00\x06D\x90\x80 \x00 \xa5' |
| 2109 | b'P\xd9!\x03\x03\x13\x13\x13\x89\xa9\xa9\xc2u5:\x9f' |
| 2110 | b'\x8b\xb9"\x9c(HjTe?\x80PK\x01\x02\x14' |
| 2111 | b'\x03\x14\x03\x00\x00\x0c\x00nu\x0c=FAKE8' |
| 2112 | b'\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00' |
| 2113 | b'\x00 \x80\x80\x81\x00\x00\x00\x00afilePK' |
| 2114 | b'\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00\x00[\x00' |
| 2115 | b'\x00\x00\x00\x00') |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2116 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 2117 | @requires_lzma() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2118 | class LzmaBadCrcTests(AbstractBadCrcTests, unittest.TestCase): |
| 2119 | compression = zipfile.ZIP_LZMA |
| 2120 | zip_with_bad_crc = ( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 2121 | b'PK\x03\x04\x14\x03\x00\x00\x0e\x00nu\x0c=FA' |
| 2122 | b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' |
| 2123 | b'ile\t\x04\x05\x00]\x00\x00\x00\x04\x004\x19I' |
| 2124 | b'\xee\x8d\xe9\x17\x89:3`\tq!.8\x00PK' |
| 2125 | b'\x01\x02\x14\x03\x14\x03\x00\x00\x0e\x00nu\x0c=FA' |
| 2126 | b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00' |
| 2127 | b'\x00\x00\x00\x00 \x80\x80\x81\x00\x00\x00\x00afil' |
| 2128 | b'ePK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00' |
| 2129 | b'\x00>\x00\x00\x00\x00\x00') |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2130 | |
| 2131 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2132 | class DecryptionTests(unittest.TestCase): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 2133 | """Check that ZIP decryption works. Since the library does not |
| 2134 | support encryption at the moment, we use a pre-generated encrypted |
| 2135 | ZIP file.""" |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2136 | |
| 2137 | data = ( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 2138 | b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00' |
| 2139 | b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y' |
| 2140 | b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl' |
| 2141 | b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00' |
| 2142 | b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81' |
| 2143 | b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00' |
| 2144 | b'\x00\x00L\x00\x00\x00\x00\x00' ) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 2145 | data2 = ( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 2146 | b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02' |
| 2147 | b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04' |
| 2148 | b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0' |
| 2149 | b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03' |
| 2150 | b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00' |
| 2151 | b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze' |
| 2152 | b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01' |
| 2153 | b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' ) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2154 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 2155 | plain = b'zipfile.py encryption test' |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 2156 | plain2 = b'\x00'*512 |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2157 | |
| 2158 | def setUp(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 2159 | with open(TESTFN, "wb") as fp: |
| 2160 | fp.write(self.data) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2161 | self.zip = zipfile.ZipFile(TESTFN, "r") |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 2162 | with open(TESTFN2, "wb") as fp: |
| 2163 | fp.write(self.data2) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 2164 | self.zip2 = zipfile.ZipFile(TESTFN2, "r") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2165 | |
| 2166 | def tearDown(self): |
| 2167 | self.zip.close() |
| 2168 | os.unlink(TESTFN) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 2169 | self.zip2.close() |
| 2170 | os.unlink(TESTFN2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2171 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2172 | def test_no_password(self): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2173 | # Reading the encrypted file without password |
| 2174 | # must generate a RunTime exception |
| 2175 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 2176 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2177 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2178 | def test_bad_password(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 2179 | self.zip.setpassword(b"perl") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2180 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 2181 | self.zip2.setpassword(b"perl") |
| 2182 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2183 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 2184 | @requires_zlib() |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2185 | def test_good_password(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 2186 | self.zip.setpassword(b"python") |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 2187 | self.assertEqual(self.zip.read("test.txt"), self.plain) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 2188 | self.zip2.setpassword(b"12345") |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 2189 | self.assertEqual(self.zip2.read("zero"), self.plain2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2190 | |
R. David Murray | 8d855d8 | 2010-12-21 21:53:37 +0000 | [diff] [blame] | 2191 | def test_unicode_password(self): |
| 2192 | self.assertRaises(TypeError, self.zip.setpassword, "unicode") |
| 2193 | self.assertRaises(TypeError, self.zip.read, "test.txt", "python") |
| 2194 | self.assertRaises(TypeError, self.zip.open, "test.txt", pwd="python") |
| 2195 | self.assertRaises(TypeError, self.zip.extract, "test.txt", pwd="python") |
| 2196 | |
Serhiy Storchaka | 5c32af7 | 2019-10-27 10:22:14 +0200 | [diff] [blame] | 2197 | def test_seek_tell(self): |
| 2198 | self.zip.setpassword(b"python") |
| 2199 | txt = self.plain |
| 2200 | test_word = b'encryption' |
| 2201 | bloc = txt.find(test_word) |
| 2202 | bloc_len = len(test_word) |
| 2203 | with self.zip.open("test.txt", "r") as fp: |
| 2204 | fp.seek(bloc, os.SEEK_SET) |
| 2205 | self.assertEqual(fp.tell(), bloc) |
| 2206 | fp.seek(-bloc, os.SEEK_CUR) |
| 2207 | self.assertEqual(fp.tell(), 0) |
| 2208 | fp.seek(bloc, os.SEEK_CUR) |
| 2209 | self.assertEqual(fp.tell(), bloc) |
| 2210 | self.assertEqual(fp.read(bloc_len), txt[bloc:bloc+bloc_len]) |
| 2211 | |
| 2212 | # Make sure that the second read after seeking back beyond |
| 2213 | # _readbuffer returns the same content (ie. rewind to the start of |
| 2214 | # the file to read forward to the required position). |
| 2215 | old_read_size = fp.MIN_READ_SIZE |
| 2216 | fp.MIN_READ_SIZE = 1 |
| 2217 | fp._readbuffer = b'' |
| 2218 | fp._offset = 0 |
| 2219 | fp.seek(0, os.SEEK_SET) |
| 2220 | self.assertEqual(fp.tell(), 0) |
| 2221 | fp.seek(bloc, os.SEEK_CUR) |
| 2222 | self.assertEqual(fp.read(bloc_len), txt[bloc:bloc+bloc_len]) |
| 2223 | fp.MIN_READ_SIZE = old_read_size |
| 2224 | |
| 2225 | fp.seek(0, os.SEEK_END) |
| 2226 | self.assertEqual(fp.tell(), len(txt)) |
| 2227 | fp.seek(0, os.SEEK_SET) |
| 2228 | self.assertEqual(fp.tell(), 0) |
| 2229 | |
| 2230 | # Read the file completely to definitely call any eof integrity |
| 2231 | # checks (crc) and make sure they still pass. |
| 2232 | fp.read() |
| 2233 | |
| 2234 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2235 | class AbstractTestsWithRandomBinaryFiles: |
| 2236 | @classmethod |
| 2237 | def setUpClass(cls): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2238 | datacount = randint(16, 64)*1024 + randint(1, 1024) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2239 | cls.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000)) |
| 2240 | for i in range(datacount)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2241 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2242 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2243 | # Make a source file with some lines |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 2244 | with open(TESTFN, "wb") as fp: |
| 2245 | fp.write(self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2246 | |
| 2247 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 2248 | unlink(TESTFN) |
| 2249 | unlink(TESTFN2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2250 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2251 | def make_test_archive(self, f, compression): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2252 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 2253 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 2254 | zipfp.write(TESTFN, "another.name") |
| 2255 | zipfp.write(TESTFN, TESTFN) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2256 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2257 | def zip_test(self, f, compression): |
| 2258 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2259 | |
| 2260 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 2261 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 2262 | testdata = zipfp.read(TESTFN) |
| 2263 | self.assertEqual(len(testdata), len(self.data)) |
| 2264 | self.assertEqual(testdata, self.data) |
| 2265 | self.assertEqual(zipfp.read("another.name"), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2266 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2267 | def test_read(self): |
| 2268 | for f in get_files(self): |
| 2269 | self.zip_test(f, self.compression) |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 2270 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2271 | def zip_open_test(self, f, compression): |
| 2272 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2273 | |
| 2274 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 2275 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 2276 | zipdata1 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 2277 | with zipfp.open(TESTFN) as zipopen1: |
| 2278 | while True: |
| 2279 | read_data = zipopen1.read(256) |
| 2280 | if not read_data: |
| 2281 | break |
| 2282 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2283 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 2284 | zipdata2 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 2285 | with zipfp.open("another.name") as zipopen2: |
| 2286 | while True: |
| 2287 | read_data = zipopen2.read(256) |
| 2288 | if not read_data: |
| 2289 | break |
| 2290 | zipdata2.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2291 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 2292 | testdata1 = b''.join(zipdata1) |
| 2293 | self.assertEqual(len(testdata1), len(self.data)) |
| 2294 | self.assertEqual(testdata1, self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2295 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 2296 | testdata2 = b''.join(zipdata2) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 2297 | self.assertEqual(len(testdata2), len(self.data)) |
| 2298 | self.assertEqual(testdata2, self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2299 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2300 | def test_open(self): |
| 2301 | for f in get_files(self): |
| 2302 | self.zip_open_test(f, self.compression) |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 2303 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2304 | def zip_random_open_test(self, f, compression): |
| 2305 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2306 | |
| 2307 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 2308 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 2309 | zipdata1 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 2310 | with zipfp.open(TESTFN) as zipopen1: |
| 2311 | while True: |
| 2312 | read_data = zipopen1.read(randint(1, 1024)) |
| 2313 | if not read_data: |
| 2314 | break |
| 2315 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2316 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 2317 | testdata = b''.join(zipdata1) |
| 2318 | self.assertEqual(len(testdata), len(self.data)) |
| 2319 | self.assertEqual(testdata, self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2320 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2321 | def test_random_open(self): |
| 2322 | for f in get_files(self): |
| 2323 | self.zip_random_open_test(f, self.compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2324 | |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 2325 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2326 | class StoredTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles, |
| 2327 | unittest.TestCase): |
| 2328 | compression = zipfile.ZIP_STORED |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 2329 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 2330 | @requires_zlib() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2331 | class DeflateTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles, |
| 2332 | unittest.TestCase): |
| 2333 | compression = zipfile.ZIP_DEFLATED |
| 2334 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 2335 | @requires_bz2() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2336 | class Bzip2TestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles, |
| 2337 | unittest.TestCase): |
| 2338 | compression = zipfile.ZIP_BZIP2 |
| 2339 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 2340 | @requires_lzma() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 2341 | class LzmaTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles, |
| 2342 | unittest.TestCase): |
| 2343 | compression = zipfile.ZIP_LZMA |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 2344 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 2345 | |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 2346 | # Provide the tell() method but not seek() |
Serhiy Storchaka | a14f7d2 | 2015-01-26 14:01:27 +0200 | [diff] [blame] | 2347 | class Tellable: |
| 2348 | def __init__(self, fp): |
| 2349 | self.fp = fp |
| 2350 | self.offset = 0 |
| 2351 | |
| 2352 | def write(self, data): |
Serhiy Storchaka | 77d8997 | 2015-03-23 01:09:35 +0200 | [diff] [blame] | 2353 | n = self.fp.write(data) |
| 2354 | self.offset += n |
| 2355 | return n |
Serhiy Storchaka | a14f7d2 | 2015-01-26 14:01:27 +0200 | [diff] [blame] | 2356 | |
| 2357 | def tell(self): |
| 2358 | return self.offset |
| 2359 | |
| 2360 | def flush(self): |
Serhiy Storchaka | 77d8997 | 2015-03-23 01:09:35 +0200 | [diff] [blame] | 2361 | self.fp.flush() |
| 2362 | |
| 2363 | class Unseekable: |
| 2364 | def __init__(self, fp): |
| 2365 | self.fp = fp |
| 2366 | |
| 2367 | def write(self, data): |
| 2368 | return self.fp.write(data) |
| 2369 | |
| 2370 | def flush(self): |
| 2371 | self.fp.flush() |
Serhiy Storchaka | a14f7d2 | 2015-01-26 14:01:27 +0200 | [diff] [blame] | 2372 | |
| 2373 | class UnseekableTests(unittest.TestCase): |
Serhiy Storchaka | 77d8997 | 2015-03-23 01:09:35 +0200 | [diff] [blame] | 2374 | def test_writestr(self): |
| 2375 | for wrapper in (lambda f: f), Tellable, Unseekable: |
| 2376 | with self.subTest(wrapper=wrapper): |
| 2377 | f = io.BytesIO() |
| 2378 | f.write(b'abc') |
| 2379 | bf = io.BufferedWriter(f) |
| 2380 | with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipfp: |
| 2381 | zipfp.writestr('ones', b'111') |
| 2382 | zipfp.writestr('twos', b'222') |
| 2383 | self.assertEqual(f.getvalue()[:5], b'abcPK') |
| 2384 | with zipfile.ZipFile(f, mode='r') as zipf: |
| 2385 | with zipf.open('ones') as zopen: |
| 2386 | self.assertEqual(zopen.read(), b'111') |
| 2387 | with zipf.open('twos') as zopen: |
| 2388 | self.assertEqual(zopen.read(), b'222') |
| 2389 | |
| 2390 | def test_write(self): |
| 2391 | for wrapper in (lambda f: f), Tellable, Unseekable: |
| 2392 | with self.subTest(wrapper=wrapper): |
| 2393 | f = io.BytesIO() |
| 2394 | f.write(b'abc') |
| 2395 | bf = io.BufferedWriter(f) |
| 2396 | with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipfp: |
| 2397 | self.addCleanup(unlink, TESTFN) |
| 2398 | with open(TESTFN, 'wb') as f2: |
| 2399 | f2.write(b'111') |
| 2400 | zipfp.write(TESTFN, 'ones') |
| 2401 | with open(TESTFN, 'wb') as f2: |
| 2402 | f2.write(b'222') |
| 2403 | zipfp.write(TESTFN, 'twos') |
| 2404 | self.assertEqual(f.getvalue()[:5], b'abcPK') |
| 2405 | with zipfile.ZipFile(f, mode='r') as zipf: |
| 2406 | with zipf.open('ones') as zopen: |
| 2407 | self.assertEqual(zopen.read(), b'111') |
| 2408 | with zipf.open('twos') as zopen: |
| 2409 | self.assertEqual(zopen.read(), b'222') |
Serhiy Storchaka | a14f7d2 | 2015-01-26 14:01:27 +0200 | [diff] [blame] | 2410 | |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 2411 | def test_open_write(self): |
| 2412 | for wrapper in (lambda f: f), Tellable, Unseekable: |
| 2413 | with self.subTest(wrapper=wrapper): |
| 2414 | f = io.BytesIO() |
| 2415 | f.write(b'abc') |
| 2416 | bf = io.BufferedWriter(f) |
| 2417 | with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipf: |
| 2418 | with zipf.open('ones', 'w') as zopen: |
| 2419 | zopen.write(b'111') |
| 2420 | with zipf.open('twos', 'w') as zopen: |
| 2421 | zopen.write(b'222') |
| 2422 | self.assertEqual(f.getvalue()[:5], b'abcPK') |
| 2423 | with zipfile.ZipFile(f) as zipf: |
| 2424 | self.assertEqual(zipf.read('ones'), b'111') |
| 2425 | self.assertEqual(zipf.read('twos'), b'222') |
| 2426 | |
Serhiy Storchaka | a14f7d2 | 2015-01-26 14:01:27 +0200 | [diff] [blame] | 2427 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 2428 | @requires_zlib() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2429 | class TestsWithMultipleOpens(unittest.TestCase): |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2430 | @classmethod |
| 2431 | def setUpClass(cls): |
Victor Stinner | 87502dd | 2020-04-17 22:54:38 +0200 | [diff] [blame] | 2432 | cls.data1 = b'111' + randbytes(10000) |
| 2433 | cls.data2 = b'222' + randbytes(10000) |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2434 | |
| 2435 | def make_test_archive(self, f): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2436 | # Create the ZIP archive |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2437 | with zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED) as zipfp: |
| 2438 | zipfp.writestr('ones', self.data1) |
| 2439 | zipfp.writestr('twos', self.data2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2440 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2441 | def test_same_file(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2442 | # Verify that (when the ZipFile is in control of creating file objects) |
| 2443 | # multiple open() calls can be made without interfering with each other. |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2444 | for f in get_files(self): |
| 2445 | self.make_test_archive(f) |
| 2446 | with zipfile.ZipFile(f, mode="r") as zipf: |
| 2447 | with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2: |
| 2448 | data1 = zopen1.read(500) |
| 2449 | data2 = zopen2.read(500) |
| 2450 | data1 += zopen1.read() |
| 2451 | data2 += zopen2.read() |
| 2452 | self.assertEqual(data1, data2) |
| 2453 | self.assertEqual(data1, self.data1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2454 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2455 | def test_different_file(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2456 | # Verify that (when the ZipFile is in control of creating file objects) |
| 2457 | # multiple open() calls can be made without interfering with each other. |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2458 | for f in get_files(self): |
| 2459 | self.make_test_archive(f) |
| 2460 | with zipfile.ZipFile(f, mode="r") as zipf: |
| 2461 | with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2: |
| 2462 | data1 = zopen1.read(500) |
| 2463 | data2 = zopen2.read(500) |
| 2464 | data1 += zopen1.read() |
| 2465 | data2 += zopen2.read() |
| 2466 | self.assertEqual(data1, self.data1) |
| 2467 | self.assertEqual(data2, self.data2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2468 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2469 | def test_interleaved(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2470 | # Verify that (when the ZipFile is in control of creating file objects) |
| 2471 | # multiple open() calls can be made without interfering with each other. |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2472 | for f in get_files(self): |
| 2473 | self.make_test_archive(f) |
| 2474 | with zipfile.ZipFile(f, mode="r") as zipf: |
Serhiy Storchaka | d76c7c2 | 2016-05-13 21:18:58 +0300 | [diff] [blame] | 2475 | with zipf.open('ones') as zopen1: |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2476 | data1 = zopen1.read(500) |
Serhiy Storchaka | d76c7c2 | 2016-05-13 21:18:58 +0300 | [diff] [blame] | 2477 | with zipf.open('twos') as zopen2: |
| 2478 | data2 = zopen2.read(500) |
| 2479 | data1 += zopen1.read() |
| 2480 | data2 += zopen2.read() |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2481 | self.assertEqual(data1, self.data1) |
| 2482 | self.assertEqual(data2, self.data2) |
| 2483 | |
| 2484 | def test_read_after_close(self): |
| 2485 | for f in get_files(self): |
| 2486 | self.make_test_archive(f) |
| 2487 | with contextlib.ExitStack() as stack: |
| 2488 | with zipfile.ZipFile(f, 'r') as zipf: |
| 2489 | zopen1 = stack.enter_context(zipf.open('ones')) |
| 2490 | zopen2 = stack.enter_context(zipf.open('twos')) |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 2491 | data1 = zopen1.read(500) |
| 2492 | data2 = zopen2.read(500) |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2493 | data1 += zopen1.read() |
| 2494 | data2 += zopen2.read() |
| 2495 | self.assertEqual(data1, self.data1) |
| 2496 | self.assertEqual(data2, self.data2) |
| 2497 | |
| 2498 | def test_read_after_write(self): |
| 2499 | for f in get_files(self): |
| 2500 | with zipfile.ZipFile(f, 'w', zipfile.ZIP_DEFLATED) as zipf: |
| 2501 | zipf.writestr('ones', self.data1) |
| 2502 | zipf.writestr('twos', self.data2) |
| 2503 | with zipf.open('ones') as zopen1: |
| 2504 | data1 = zopen1.read(500) |
| 2505 | self.assertEqual(data1, self.data1[:500]) |
| 2506 | with zipfile.ZipFile(f, 'r') as zipf: |
| 2507 | data1 = zipf.read('ones') |
| 2508 | data2 = zipf.read('twos') |
| 2509 | self.assertEqual(data1, self.data1) |
| 2510 | self.assertEqual(data2, self.data2) |
| 2511 | |
| 2512 | def test_write_after_read(self): |
| 2513 | for f in get_files(self): |
| 2514 | with zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED) as zipf: |
| 2515 | zipf.writestr('ones', self.data1) |
| 2516 | with zipf.open('ones') as zopen1: |
| 2517 | zopen1.read(500) |
| 2518 | zipf.writestr('twos', self.data2) |
| 2519 | with zipfile.ZipFile(f, 'r') as zipf: |
| 2520 | data1 = zipf.read('ones') |
| 2521 | data2 = zipf.read('twos') |
| 2522 | self.assertEqual(data1, self.data1) |
| 2523 | self.assertEqual(data2, self.data2) |
| 2524 | |
| 2525 | def test_many_opens(self): |
| 2526 | # Verify that read() and open() promptly close the file descriptor, |
| 2527 | # and don't rely on the garbage collector to free resources. |
| 2528 | self.make_test_archive(TESTFN2) |
| 2529 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 2530 | for x in range(100): |
| 2531 | zipf.read('ones') |
| 2532 | with zipf.open('ones') as zopen1: |
| 2533 | pass |
| 2534 | with open(os.devnull) as f: |
| 2535 | self.assertLess(f.fileno(), 100) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2536 | |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 2537 | def test_write_while_reading(self): |
| 2538 | with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_DEFLATED) as zipf: |
| 2539 | zipf.writestr('ones', self.data1) |
| 2540 | with zipfile.ZipFile(TESTFN2, 'a', zipfile.ZIP_DEFLATED) as zipf: |
| 2541 | with zipf.open('ones', 'r') as r1: |
| 2542 | data1 = r1.read(500) |
| 2543 | with zipf.open('twos', 'w') as w1: |
| 2544 | w1.write(self.data2) |
| 2545 | data1 += r1.read() |
| 2546 | self.assertEqual(data1, self.data1) |
| 2547 | with zipfile.ZipFile(TESTFN2) as zipf: |
| 2548 | self.assertEqual(zipf.read('twos'), self.data2) |
| 2549 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2550 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 2551 | unlink(TESTFN2) |
| 2552 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2553 | |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 2554 | class TestWithDirectory(unittest.TestCase): |
| 2555 | def setUp(self): |
| 2556 | os.mkdir(TESTFN2) |
| 2557 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2558 | def test_extract_dir(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 2559 | with zipfile.ZipFile(findfile("zipdir.zip")) as zipf: |
| 2560 | zipf.extractall(TESTFN2) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 2561 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a"))) |
| 2562 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b"))) |
| 2563 | self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c"))) |
| 2564 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2565 | def test_bug_6050(self): |
Martin v. Löwis | 70ccd16 | 2009-05-24 19:47:22 +0000 | [diff] [blame] | 2566 | # Extraction should succeed if directories already exist |
| 2567 | os.mkdir(os.path.join(TESTFN2, "a")) |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2568 | self.test_extract_dir() |
Martin v. Löwis | 70ccd16 | 2009-05-24 19:47:22 +0000 | [diff] [blame] | 2569 | |
Serhiy Storchaka | 46a3492 | 2014-09-23 22:40:23 +0300 | [diff] [blame] | 2570 | def test_write_dir(self): |
| 2571 | dirpath = os.path.join(TESTFN2, "x") |
| 2572 | os.mkdir(dirpath) |
| 2573 | mode = os.stat(dirpath).st_mode & 0xFFFF |
| 2574 | with zipfile.ZipFile(TESTFN, "w") as zipf: |
| 2575 | zipf.write(dirpath) |
| 2576 | zinfo = zipf.filelist[0] |
| 2577 | self.assertTrue(zinfo.filename.endswith("/x/")) |
| 2578 | self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10) |
| 2579 | zipf.write(dirpath, "y") |
| 2580 | zinfo = zipf.filelist[1] |
| 2581 | self.assertTrue(zinfo.filename, "y/") |
| 2582 | self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10) |
| 2583 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 2584 | zinfo = zipf.filelist[0] |
| 2585 | self.assertTrue(zinfo.filename.endswith("/x/")) |
| 2586 | self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10) |
| 2587 | zinfo = zipf.filelist[1] |
| 2588 | self.assertTrue(zinfo.filename, "y/") |
| 2589 | self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10) |
| 2590 | target = os.path.join(TESTFN2, "target") |
| 2591 | os.mkdir(target) |
| 2592 | zipf.extractall(target) |
| 2593 | self.assertTrue(os.path.isdir(os.path.join(target, "y"))) |
| 2594 | self.assertEqual(len(os.listdir(target)), 2) |
| 2595 | |
| 2596 | def test_writestr_dir(self): |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 2597 | os.mkdir(os.path.join(TESTFN2, "x")) |
Serhiy Storchaka | 46a3492 | 2014-09-23 22:40:23 +0300 | [diff] [blame] | 2598 | with zipfile.ZipFile(TESTFN, "w") as zipf: |
| 2599 | zipf.writestr("x/", b'') |
| 2600 | zinfo = zipf.filelist[0] |
| 2601 | self.assertEqual(zinfo.filename, "x/") |
| 2602 | self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10) |
| 2603 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 2604 | zinfo = zipf.filelist[0] |
| 2605 | self.assertTrue(zinfo.filename.endswith("x/")) |
| 2606 | self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10) |
| 2607 | target = os.path.join(TESTFN2, "target") |
| 2608 | os.mkdir(target) |
| 2609 | zipf.extractall(target) |
| 2610 | self.assertTrue(os.path.isdir(os.path.join(target, "x"))) |
| 2611 | self.assertEqual(os.listdir(target), ["x"]) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 2612 | |
| 2613 | def tearDown(self): |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 2614 | rmtree(TESTFN2) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 2615 | if os.path.exists(TESTFN): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 2616 | unlink(TESTFN) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 2617 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2618 | |
Serhiy Storchaka | 503f908 | 2016-02-08 00:02:25 +0200 | [diff] [blame] | 2619 | class ZipInfoTests(unittest.TestCase): |
| 2620 | def test_from_file(self): |
| 2621 | zi = zipfile.ZipInfo.from_file(__file__) |
| 2622 | self.assertEqual(posixpath.basename(zi.filename), 'test_zipfile.py') |
| 2623 | self.assertFalse(zi.is_dir()) |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 2624 | self.assertEqual(zi.file_size, os.path.getsize(__file__)) |
| 2625 | |
| 2626 | def test_from_file_pathlike(self): |
| 2627 | zi = zipfile.ZipInfo.from_file(pathlib.Path(__file__)) |
| 2628 | self.assertEqual(posixpath.basename(zi.filename), 'test_zipfile.py') |
| 2629 | self.assertFalse(zi.is_dir()) |
| 2630 | self.assertEqual(zi.file_size, os.path.getsize(__file__)) |
| 2631 | |
| 2632 | def test_from_file_bytes(self): |
| 2633 | zi = zipfile.ZipInfo.from_file(os.fsencode(__file__), 'test') |
| 2634 | self.assertEqual(posixpath.basename(zi.filename), 'test') |
| 2635 | self.assertFalse(zi.is_dir()) |
| 2636 | self.assertEqual(zi.file_size, os.path.getsize(__file__)) |
| 2637 | |
| 2638 | def test_from_file_fileno(self): |
| 2639 | with open(__file__, 'rb') as f: |
| 2640 | zi = zipfile.ZipInfo.from_file(f.fileno(), 'test') |
| 2641 | self.assertEqual(posixpath.basename(zi.filename), 'test') |
| 2642 | self.assertFalse(zi.is_dir()) |
| 2643 | self.assertEqual(zi.file_size, os.path.getsize(__file__)) |
Serhiy Storchaka | 503f908 | 2016-02-08 00:02:25 +0200 | [diff] [blame] | 2644 | |
| 2645 | def test_from_dir(self): |
| 2646 | dirpath = os.path.dirname(os.path.abspath(__file__)) |
| 2647 | zi = zipfile.ZipInfo.from_file(dirpath, 'stdlib_tests') |
| 2648 | self.assertEqual(zi.filename, 'stdlib_tests/') |
| 2649 | self.assertTrue(zi.is_dir()) |
| 2650 | self.assertEqual(zi.compress_type, zipfile.ZIP_STORED) |
| 2651 | self.assertEqual(zi.file_size, 0) |
| 2652 | |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2653 | |
| 2654 | class CommandLineTest(unittest.TestCase): |
| 2655 | |
| 2656 | def zipfilecmd(self, *args, **kwargs): |
| 2657 | rc, out, err = script_helper.assert_python_ok('-m', 'zipfile', *args, |
| 2658 | **kwargs) |
| 2659 | return out.replace(os.linesep.encode(), b'\n') |
| 2660 | |
| 2661 | def zipfilecmd_failure(self, *args): |
| 2662 | return script_helper.assert_python_failure('-m', 'zipfile', *args) |
| 2663 | |
Serhiy Storchaka | 150cd19 | 2017-04-07 18:56:12 +0300 | [diff] [blame] | 2664 | def test_bad_use(self): |
| 2665 | rc, out, err = self.zipfilecmd_failure() |
| 2666 | self.assertEqual(out, b'') |
| 2667 | self.assertIn(b'usage', err.lower()) |
| 2668 | self.assertIn(b'error', err.lower()) |
| 2669 | self.assertIn(b'required', err.lower()) |
| 2670 | rc, out, err = self.zipfilecmd_failure('-l', '') |
| 2671 | self.assertEqual(out, b'') |
| 2672 | self.assertNotEqual(err.strip(), b'') |
| 2673 | |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2674 | def test_test_command(self): |
| 2675 | zip_name = findfile('zipdir.zip') |
Serhiy Storchaka | 8c93310 | 2016-10-23 13:32:12 +0300 | [diff] [blame] | 2676 | for opt in '-t', '--test': |
| 2677 | out = self.zipfilecmd(opt, zip_name) |
| 2678 | self.assertEqual(out.rstrip(), b'Done testing') |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2679 | zip_name = findfile('testtar.tar') |
| 2680 | rc, out, err = self.zipfilecmd_failure('-t', zip_name) |
| 2681 | self.assertEqual(out, b'') |
| 2682 | |
| 2683 | def test_list_command(self): |
| 2684 | zip_name = findfile('zipdir.zip') |
| 2685 | t = io.StringIO() |
| 2686 | with zipfile.ZipFile(zip_name, 'r') as tf: |
| 2687 | tf.printdir(t) |
| 2688 | expected = t.getvalue().encode('ascii', 'backslashreplace') |
Serhiy Storchaka | 8c93310 | 2016-10-23 13:32:12 +0300 | [diff] [blame] | 2689 | for opt in '-l', '--list': |
| 2690 | out = self.zipfilecmd(opt, zip_name, |
| 2691 | PYTHONIOENCODING='ascii:backslashreplace') |
| 2692 | self.assertEqual(out, expected) |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2693 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 2694 | @requires_zlib() |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2695 | def test_create_command(self): |
| 2696 | self.addCleanup(unlink, TESTFN) |
| 2697 | with open(TESTFN, 'w') as f: |
| 2698 | f.write('test 1') |
| 2699 | os.mkdir(TESTFNDIR) |
| 2700 | self.addCleanup(rmtree, TESTFNDIR) |
| 2701 | with open(os.path.join(TESTFNDIR, 'file.txt'), 'w') as f: |
| 2702 | f.write('test 2') |
| 2703 | files = [TESTFN, TESTFNDIR] |
| 2704 | namelist = [TESTFN, TESTFNDIR + '/', TESTFNDIR + '/file.txt'] |
Serhiy Storchaka | 8c93310 | 2016-10-23 13:32:12 +0300 | [diff] [blame] | 2705 | for opt in '-c', '--create': |
| 2706 | try: |
| 2707 | out = self.zipfilecmd(opt, TESTFN2, *files) |
| 2708 | self.assertEqual(out, b'') |
| 2709 | with zipfile.ZipFile(TESTFN2) as zf: |
| 2710 | self.assertEqual(zf.namelist(), namelist) |
| 2711 | self.assertEqual(zf.read(namelist[0]), b'test 1') |
| 2712 | self.assertEqual(zf.read(namelist[2]), b'test 2') |
| 2713 | finally: |
| 2714 | unlink(TESTFN2) |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2715 | |
| 2716 | def test_extract_command(self): |
| 2717 | zip_name = findfile('zipdir.zip') |
Serhiy Storchaka | 8c93310 | 2016-10-23 13:32:12 +0300 | [diff] [blame] | 2718 | for opt in '-e', '--extract': |
| 2719 | with temp_dir() as extdir: |
| 2720 | out = self.zipfilecmd(opt, zip_name, extdir) |
| 2721 | self.assertEqual(out, b'') |
| 2722 | with zipfile.ZipFile(zip_name) as zf: |
| 2723 | for zi in zf.infolist(): |
| 2724 | path = os.path.join(extdir, |
| 2725 | zi.filename.replace('/', os.sep)) |
| 2726 | if zi.is_dir(): |
| 2727 | self.assertTrue(os.path.isdir(path)) |
| 2728 | else: |
| 2729 | self.assertTrue(os.path.isfile(path)) |
| 2730 | with open(path, 'rb') as f: |
| 2731 | self.assertEqual(f.read(), zf.read(zi)) |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2732 | |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2733 | |
Jason R. Coombs | e5bd736 | 2020-02-11 21:58:47 -0500 | [diff] [blame] | 2734 | class TestExecutablePrependedZip(unittest.TestCase): |
| 2735 | """Test our ability to open zip files with an executable prepended.""" |
| 2736 | |
| 2737 | def setUp(self): |
| 2738 | self.exe_zip = findfile('exe_with_zip', subdir='ziptestdata') |
| 2739 | self.exe_zip64 = findfile('exe_with_z64', subdir='ziptestdata') |
| 2740 | |
| 2741 | def _test_zip_works(self, name): |
| 2742 | # bpo28494 sanity check: ensure is_zipfile works on these. |
| 2743 | self.assertTrue(zipfile.is_zipfile(name), |
| 2744 | f'is_zipfile failed on {name}') |
| 2745 | # Ensure we can operate on these via ZipFile. |
| 2746 | with zipfile.ZipFile(name) as zipfp: |
| 2747 | for n in zipfp.namelist(): |
| 2748 | data = zipfp.read(n) |
| 2749 | self.assertIn(b'FAVORITE_NUMBER', data) |
| 2750 | |
| 2751 | def test_read_zip_with_exe_prepended(self): |
| 2752 | self._test_zip_works(self.exe_zip) |
| 2753 | |
| 2754 | def test_read_zip64_with_exe_prepended(self): |
| 2755 | self._test_zip_works(self.exe_zip64) |
| 2756 | |
| 2757 | @unittest.skipUnless(sys.executable, 'sys.executable required.') |
| 2758 | @unittest.skipUnless(os.access('/bin/bash', os.X_OK), |
| 2759 | 'Test relies on #!/bin/bash working.') |
| 2760 | def test_execute_zip2(self): |
| 2761 | output = subprocess.check_output([self.exe_zip, sys.executable]) |
| 2762 | self.assertIn(b'number in executable: 5', output) |
| 2763 | |
| 2764 | @unittest.skipUnless(sys.executable, 'sys.executable required.') |
| 2765 | @unittest.skipUnless(os.access('/bin/bash', os.X_OK), |
| 2766 | 'Test relies on #!/bin/bash working.') |
| 2767 | def test_execute_zip64(self): |
| 2768 | output = subprocess.check_output([self.exe_zip64, sys.executable]) |
| 2769 | self.assertIn(b'number in executable: 5', output) |
| 2770 | |
| 2771 | |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2772 | # Poor man's technique to consume a (smallish) iterable. |
| 2773 | consume = tuple |
| 2774 | |
| 2775 | |
Jason R. Coombs | e5bd736 | 2020-02-11 21:58:47 -0500 | [diff] [blame] | 2776 | # from jaraco.itertools 5.0 |
| 2777 | class jaraco: |
| 2778 | class itertools: |
| 2779 | class Counter: |
| 2780 | def __init__(self, i): |
| 2781 | self.count = 0 |
| 2782 | self._orig_iter = iter(i) |
| 2783 | |
| 2784 | def __iter__(self): |
| 2785 | return self |
| 2786 | |
| 2787 | def __next__(self): |
| 2788 | result = next(self._orig_iter) |
| 2789 | self.count += 1 |
| 2790 | return result |
| 2791 | |
| 2792 | |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2793 | def add_dirs(zf): |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2794 | """ |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2795 | Given a writable zip file zf, inject directory entries for |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2796 | any directories implied by the presence of children. |
| 2797 | """ |
Jason R. Coombs | e5bd736 | 2020-02-11 21:58:47 -0500 | [diff] [blame] | 2798 | for name in zipfile.CompleteDirs._implied_dirs(zf.namelist()): |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2799 | zf.writestr(name, b"") |
| 2800 | return zf |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2801 | |
| 2802 | |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2803 | def build_alpharep_fixture(): |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2804 | """ |
| 2805 | Create a zip file with this structure: |
| 2806 | |
| 2807 | . |
| 2808 | ├── a.txt |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2809 | ├── b |
| 2810 | │ ├── c.txt |
| 2811 | │ ├── d |
| 2812 | │ │ └── e.txt |
| 2813 | │ └── f.txt |
| 2814 | └── g |
| 2815 | └── h |
| 2816 | └── i.txt |
| 2817 | |
| 2818 | This fixture has the following key characteristics: |
| 2819 | |
| 2820 | - a file at the root (a) |
| 2821 | - a file two levels deep (b/d/e) |
| 2822 | - multiple files in a directory (b/c, b/f) |
| 2823 | - a directory containing only a directory (g/h) |
| 2824 | |
| 2825 | "alpha" because it uses alphabet |
| 2826 | "rep" because it's a representative example |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2827 | """ |
| 2828 | data = io.BytesIO() |
| 2829 | zf = zipfile.ZipFile(data, "w") |
| 2830 | zf.writestr("a.txt", b"content of a") |
| 2831 | zf.writestr("b/c.txt", b"content of c") |
| 2832 | zf.writestr("b/d/e.txt", b"content of e") |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2833 | zf.writestr("b/f.txt", b"content of f") |
| 2834 | zf.writestr("g/h/i.txt", b"content of i") |
| 2835 | zf.filename = "alpharep.zip" |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2836 | return zf |
| 2837 | |
| 2838 | |
| 2839 | class TestPath(unittest.TestCase): |
| 2840 | def setUp(self): |
| 2841 | self.fixtures = contextlib.ExitStack() |
| 2842 | self.addCleanup(self.fixtures.close) |
| 2843 | |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2844 | def zipfile_alpharep(self): |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2845 | with self.subTest(): |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2846 | yield build_alpharep_fixture() |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2847 | with self.subTest(): |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2848 | yield add_dirs(build_alpharep_fixture()) |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2849 | |
| 2850 | def zipfile_ondisk(self): |
| 2851 | tmpdir = pathlib.Path(self.fixtures.enter_context(temp_dir())) |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2852 | for alpharep in self.zipfile_alpharep(): |
| 2853 | buffer = alpharep.fp |
| 2854 | alpharep.close() |
| 2855 | path = tmpdir / alpharep.filename |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2856 | with path.open("wb") as strm: |
| 2857 | strm.write(buffer.getvalue()) |
| 2858 | yield path |
| 2859 | |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2860 | def test_iterdir_and_types(self): |
| 2861 | for alpharep in self.zipfile_alpharep(): |
| 2862 | root = zipfile.Path(alpharep) |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2863 | assert root.is_dir() |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2864 | a, b, g = root.iterdir() |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2865 | assert a.is_file() |
| 2866 | assert b.is_dir() |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2867 | assert g.is_dir() |
| 2868 | c, f, d = b.iterdir() |
| 2869 | assert c.is_file() and f.is_file() |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2870 | e, = d.iterdir() |
| 2871 | assert e.is_file() |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2872 | h, = g.iterdir() |
| 2873 | i, = h.iterdir() |
| 2874 | assert i.is_file() |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2875 | |
Jason R. Coombs | e5bd736 | 2020-02-11 21:58:47 -0500 | [diff] [blame] | 2876 | def test_subdir_is_dir(self): |
| 2877 | for alpharep in self.zipfile_alpharep(): |
| 2878 | root = zipfile.Path(alpharep) |
| 2879 | assert (root / 'b').is_dir() |
| 2880 | assert (root / 'b/').is_dir() |
| 2881 | assert (root / 'g').is_dir() |
| 2882 | assert (root / 'g/').is_dir() |
| 2883 | |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2884 | def test_open(self): |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2885 | for alpharep in self.zipfile_alpharep(): |
| 2886 | root = zipfile.Path(alpharep) |
| 2887 | a, b, g = root.iterdir() |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2888 | with a.open() as strm: |
| 2889 | data = strm.read() |
Jason R. Coombs | 0aeab5c | 2020-02-29 10:34:11 -0600 | [diff] [blame] | 2890 | assert data == "content of a" |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2891 | |
| 2892 | def test_read(self): |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2893 | for alpharep in self.zipfile_alpharep(): |
| 2894 | root = zipfile.Path(alpharep) |
| 2895 | a, b, g = root.iterdir() |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2896 | assert a.read_text() == "content of a" |
| 2897 | assert a.read_bytes() == b"content of a" |
| 2898 | |
Jason R. Coombs | 33e067d | 2019-05-09 11:34:36 -0400 | [diff] [blame] | 2899 | def test_joinpath(self): |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2900 | for alpharep in self.zipfile_alpharep(): |
| 2901 | root = zipfile.Path(alpharep) |
Jason R. Coombs | 33e067d | 2019-05-09 11:34:36 -0400 | [diff] [blame] | 2902 | a = root.joinpath("a") |
| 2903 | assert a.is_file() |
| 2904 | e = root.joinpath("b").joinpath("d").joinpath("e.txt") |
| 2905 | assert e.read_text() == "content of e" |
| 2906 | |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2907 | def test_traverse_truediv(self): |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2908 | for alpharep in self.zipfile_alpharep(): |
| 2909 | root = zipfile.Path(alpharep) |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2910 | a = root / "a" |
| 2911 | assert a.is_file() |
| 2912 | e = root / "b" / "d" / "e.txt" |
| 2913 | assert e.read_text() == "content of e" |
| 2914 | |
| 2915 | def test_pathlike_construction(self): |
| 2916 | """ |
| 2917 | zipfile.Path should be constructable from a path-like object |
| 2918 | """ |
| 2919 | for zipfile_ondisk in self.zipfile_ondisk(): |
| 2920 | pathlike = pathlib.Path(str(zipfile_ondisk)) |
| 2921 | zipfile.Path(pathlike) |
| 2922 | |
| 2923 | def test_traverse_pathlike(self): |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2924 | for alpharep in self.zipfile_alpharep(): |
| 2925 | root = zipfile.Path(alpharep) |
Jason R. Coombs | b2758ff | 2019-05-08 09:45:06 -0400 | [diff] [blame] | 2926 | root / pathlib.Path("a") |
| 2927 | |
Jason R. Coombs | 33e067d | 2019-05-09 11:34:36 -0400 | [diff] [blame] | 2928 | def test_parent(self): |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2929 | for alpharep in self.zipfile_alpharep(): |
| 2930 | root = zipfile.Path(alpharep) |
Jason R. Coombs | 33e067d | 2019-05-09 11:34:36 -0400 | [diff] [blame] | 2931 | assert (root / 'a').parent.at == '' |
| 2932 | assert (root / 'a' / 'b').parent.at == 'a/' |
| 2933 | |
Jason R. Coombs | 38f44b4 | 2019-07-07 17:37:50 -0400 | [diff] [blame] | 2934 | def test_dir_parent(self): |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2935 | for alpharep in self.zipfile_alpharep(): |
| 2936 | root = zipfile.Path(alpharep) |
Jason R. Coombs | 38f44b4 | 2019-07-07 17:37:50 -0400 | [diff] [blame] | 2937 | assert (root / 'b').parent.at == '' |
| 2938 | assert (root / 'b/').parent.at == '' |
| 2939 | |
| 2940 | def test_missing_dir_parent(self): |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2941 | for alpharep in self.zipfile_alpharep(): |
| 2942 | root = zipfile.Path(alpharep) |
Jason R. Coombs | 38f44b4 | 2019-07-07 17:37:50 -0400 | [diff] [blame] | 2943 | assert (root / 'missing dir/').parent.at == '' |
| 2944 | |
Jason R. Coombs | e5bd736 | 2020-02-11 21:58:47 -0500 | [diff] [blame] | 2945 | def test_mutability(self): |
| 2946 | """ |
| 2947 | If the underlying zipfile is changed, the Path object should |
| 2948 | reflect that change. |
| 2949 | """ |
| 2950 | for alpharep in self.zipfile_alpharep(): |
| 2951 | root = zipfile.Path(alpharep) |
| 2952 | a, b, g = root.iterdir() |
| 2953 | alpharep.writestr('foo.txt', 'foo') |
| 2954 | alpharep.writestr('bar/baz.txt', 'baz') |
| 2955 | assert any( |
| 2956 | child.name == 'foo.txt' |
| 2957 | for child in root.iterdir()) |
| 2958 | assert (root / 'foo.txt').read_text() == 'foo' |
| 2959 | baz, = (root / 'bar').iterdir() |
| 2960 | assert baz.read_text() == 'baz' |
| 2961 | |
| 2962 | HUGE_ZIPFILE_NUM_ENTRIES = 2 ** 13 |
| 2963 | |
| 2964 | def huge_zipfile(self): |
| 2965 | """Create a read-only zipfile with a huge number of entries entries.""" |
| 2966 | strm = io.BytesIO() |
| 2967 | zf = zipfile.ZipFile(strm, "w") |
| 2968 | for entry in map(str, range(self.HUGE_ZIPFILE_NUM_ENTRIES)): |
| 2969 | zf.writestr(entry, entry) |
| 2970 | zf.mode = 'r' |
| 2971 | return zf |
| 2972 | |
| 2973 | def test_joinpath_constant_time(self): |
| 2974 | """ |
| 2975 | Ensure joinpath on items in zipfile is linear time. |
| 2976 | """ |
| 2977 | root = zipfile.Path(self.huge_zipfile()) |
| 2978 | entries = jaraco.itertools.Counter(root.iterdir()) |
| 2979 | for entry in entries: |
| 2980 | entry.joinpath('suffix') |
| 2981 | # Check the file iterated all items |
| 2982 | assert entries.count == self.HUGE_ZIPFILE_NUM_ENTRIES |
| 2983 | |
Jason R. Coombs | 0aeab5c | 2020-02-29 10:34:11 -0600 | [diff] [blame] | 2984 | # @func_timeout.func_set_timeout(3) |
| 2985 | def test_implied_dirs_performance(self): |
| 2986 | data = ['/'.join(string.ascii_lowercase + str(n)) for n in range(10000)] |
| 2987 | zipfile.CompleteDirs._implied_dirs(data) |
| 2988 | |
shireenrao | a4e2991 | 2019-08-24 11:26:41 -0400 | [diff] [blame] | 2989 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 2990 | if __name__ == "__main__": |
Brett Cannon | d5b4e1d | 2013-06-12 19:57:19 -0400 | [diff] [blame] | 2991 | unittest.main() |