Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 1 | import contextlib |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 2 | import io |
| 3 | import os |
Brett Cannon | b57a085 | 2013-06-15 17:32:30 -0400 | [diff] [blame] | 4 | import importlib.util |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 5 | import pathlib |
Serhiy Storchaka | 503f908 | 2016-02-08 00:02:25 +0200 | [diff] [blame] | 6 | import posixpath |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 7 | import time |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 8 | import struct |
| 9 | import zipfile |
| 10 | import unittest |
| 11 | |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 12 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 13 | from tempfile import TemporaryFile |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 14 | from random import randint, random, getrandbits |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 15 | |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 16 | from test.support import script_helper |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 17 | from test.support import (TESTFN, findfile, unlink, rmtree, temp_dir, temp_cwd, |
Serhiy Storchaka | c5b75db | 2013-01-29 20:14:08 +0200 | [diff] [blame] | 18 | requires_zlib, requires_bz2, requires_lzma, |
Victor Stinner | d6debb2 | 2017-03-27 16:05:26 +0200 | [diff] [blame] | 19 | captured_stdout) |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 20 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 21 | TESTFN2 = TESTFN + "2" |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 22 | TESTFNDIR = TESTFN + "d" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 23 | FIXEDTEST_SIZE = 1000 |
Georg Brandl | 5ba11de | 2011-01-01 10:09:32 +0000 | [diff] [blame] | 24 | DATAFILES_DIR = 'zipfile_datafiles' |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 25 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 26 | SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'), |
| 27 | ('ziptest2dir/_ziptest2', 'qawsedrftg'), |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 28 | ('ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'), |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 29 | ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')] |
| 30 | |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 31 | def getrandbytes(size): |
| 32 | return getrandbits(8 * size).to_bytes(size, 'little') |
| 33 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 34 | def get_files(test): |
| 35 | yield TESTFN2 |
| 36 | with TemporaryFile() as f: |
| 37 | yield f |
| 38 | test.assertFalse(f.closed) |
| 39 | with io.BytesIO() as f: |
| 40 | yield f |
| 41 | test.assertFalse(f.closed) |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 42 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 43 | class AbstractTestsWithSourceFile: |
| 44 | @classmethod |
| 45 | def setUpClass(cls): |
| 46 | cls.line_gen = [bytes("Zipfile test line %d. random float: %f\n" % |
| 47 | (i, random()), "ascii") |
| 48 | for i in range(FIXEDTEST_SIZE)] |
| 49 | cls.data = b''.join(cls.line_gen) |
| 50 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 51 | def setUp(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 52 | # Make a source file with some lines |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 53 | with open(TESTFN, "wb") as fp: |
| 54 | fp.write(self.data) |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 55 | |
Bo Bayles | ce237c7 | 2018-01-29 23:54:07 -0600 | [diff] [blame] | 56 | def make_test_archive(self, f, compression, compresslevel=None): |
| 57 | kwargs = {'compression': compression, 'compresslevel': compresslevel} |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 58 | # Create the ZIP archive |
Bo Bayles | ce237c7 | 2018-01-29 23:54:07 -0600 | [diff] [blame] | 59 | with zipfile.ZipFile(f, "w", **kwargs) as zipfp: |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 60 | zipfp.write(TESTFN, "another.name") |
| 61 | zipfp.write(TESTFN, TESTFN) |
| 62 | zipfp.writestr("strfile", self.data) |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 63 | with zipfp.open('written-open-w', mode='w') as f: |
| 64 | for line in self.line_gen: |
| 65 | f.write(line) |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 66 | |
Bo Bayles | ce237c7 | 2018-01-29 23:54:07 -0600 | [diff] [blame] | 67 | def zip_test(self, f, compression, compresslevel=None): |
| 68 | self.make_test_archive(f, compression, compresslevel) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 69 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 70 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 71 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 72 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 73 | self.assertEqual(zipfp.read("another.name"), self.data) |
| 74 | self.assertEqual(zipfp.read("strfile"), self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 76 | # Print the ZIP directory |
| 77 | fp = io.StringIO() |
| 78 | zipfp.printdir(file=fp) |
| 79 | directory = fp.getvalue() |
| 80 | lines = directory.splitlines() |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 81 | self.assertEqual(len(lines), 5) # Number of files + header |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 82 | |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 83 | self.assertIn('File Name', lines[0]) |
| 84 | self.assertIn('Modified', lines[0]) |
| 85 | self.assertIn('Size', lines[0]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 86 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 87 | fn, date, time_, size = lines[1].split() |
| 88 | self.assertEqual(fn, 'another.name') |
| 89 | self.assertTrue(time.strptime(date, '%Y-%m-%d')) |
| 90 | self.assertTrue(time.strptime(time_, '%H:%M:%S')) |
| 91 | self.assertEqual(size, str(len(self.data))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 92 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 93 | # Check the namelist |
| 94 | names = zipfp.namelist() |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 95 | self.assertEqual(len(names), 4) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 96 | self.assertIn(TESTFN, names) |
| 97 | self.assertIn("another.name", names) |
| 98 | self.assertIn("strfile", names) |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 99 | self.assertIn("written-open-w", names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 100 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 101 | # Check infolist |
| 102 | infos = zipfp.infolist() |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 103 | names = [i.filename for i in infos] |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 104 | self.assertEqual(len(names), 4) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 105 | self.assertIn(TESTFN, names) |
| 106 | self.assertIn("another.name", names) |
| 107 | self.assertIn("strfile", names) |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 108 | self.assertIn("written-open-w", names) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 109 | for i in infos: |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 110 | self.assertEqual(i.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 111 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 112 | # check getinfo |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 113 | for nm in (TESTFN, "another.name", "strfile", "written-open-w"): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 114 | info = zipfp.getinfo(nm) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 115 | self.assertEqual(info.filename, nm) |
| 116 | self.assertEqual(info.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 117 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 118 | # Check that testzip doesn't raise an exception |
| 119 | zipfp.testzip() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 120 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 121 | def test_basic(self): |
| 122 | for f in get_files(self): |
| 123 | self.zip_test(f, self.compression) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 124 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 125 | def zip_open_test(self, f, compression): |
| 126 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 127 | |
| 128 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 129 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 130 | zipdata1 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 131 | with zipfp.open(TESTFN) as zipopen1: |
| 132 | while True: |
| 133 | read_data = zipopen1.read(256) |
| 134 | if not read_data: |
| 135 | break |
| 136 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 137 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 138 | zipdata2 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 139 | with zipfp.open("another.name") as zipopen2: |
| 140 | while True: |
| 141 | read_data = zipopen2.read(256) |
| 142 | if not read_data: |
| 143 | break |
| 144 | zipdata2.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 145 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 146 | self.assertEqual(b''.join(zipdata1), self.data) |
| 147 | self.assertEqual(b''.join(zipdata2), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 148 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 149 | def test_open(self): |
| 150 | for f in get_files(self): |
| 151 | self.zip_open_test(f, self.compression) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 152 | |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 153 | def test_open_with_pathlike(self): |
| 154 | path = pathlib.Path(TESTFN2) |
| 155 | self.zip_open_test(path, self.compression) |
| 156 | with zipfile.ZipFile(path, "r", self.compression) as zipfp: |
| 157 | self.assertIsInstance(zipfp.filename, str) |
| 158 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 159 | def zip_random_open_test(self, f, compression): |
| 160 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 161 | |
| 162 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 163 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 164 | zipdata1 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 165 | with zipfp.open(TESTFN) as zipopen1: |
| 166 | while True: |
| 167 | read_data = zipopen1.read(randint(1, 1024)) |
| 168 | if not read_data: |
| 169 | break |
| 170 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 171 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 172 | self.assertEqual(b''.join(zipdata1), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 173 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 174 | def test_random_open(self): |
| 175 | for f in get_files(self): |
| 176 | self.zip_random_open_test(f, self.compression) |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 177 | |
Serhiy Storchaka | d2c07a5 | 2013-09-27 22:11:57 +0300 | [diff] [blame] | 178 | def zip_read1_test(self, f, compression): |
| 179 | self.make_test_archive(f, compression) |
| 180 | |
| 181 | # Read the ZIP archive |
| 182 | with zipfile.ZipFile(f, "r") as zipfp, \ |
| 183 | zipfp.open(TESTFN) as zipopen: |
| 184 | zipdata = [] |
| 185 | while True: |
| 186 | read_data = zipopen.read1(-1) |
| 187 | if not read_data: |
| 188 | break |
| 189 | zipdata.append(read_data) |
| 190 | |
| 191 | self.assertEqual(b''.join(zipdata), self.data) |
| 192 | |
| 193 | def test_read1(self): |
| 194 | for f in get_files(self): |
| 195 | self.zip_read1_test(f, self.compression) |
| 196 | |
| 197 | def zip_read1_10_test(self, f, compression): |
| 198 | self.make_test_archive(f, compression) |
| 199 | |
| 200 | # Read the ZIP archive |
| 201 | with zipfile.ZipFile(f, "r") as zipfp, \ |
| 202 | zipfp.open(TESTFN) as zipopen: |
| 203 | zipdata = [] |
| 204 | while True: |
| 205 | read_data = zipopen.read1(10) |
| 206 | self.assertLessEqual(len(read_data), 10) |
| 207 | if not read_data: |
| 208 | break |
| 209 | zipdata.append(read_data) |
| 210 | |
| 211 | self.assertEqual(b''.join(zipdata), self.data) |
| 212 | |
| 213 | def test_read1_10(self): |
| 214 | for f in get_files(self): |
| 215 | self.zip_read1_10_test(f, self.compression) |
| 216 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 217 | def zip_readline_read_test(self, f, compression): |
| 218 | self.make_test_archive(f, compression) |
| 219 | |
| 220 | # Read the ZIP archive |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 221 | with zipfile.ZipFile(f, "r") as zipfp, \ |
| 222 | zipfp.open(TESTFN) as zipopen: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 223 | data = b'' |
| 224 | while True: |
| 225 | read = zipopen.readline() |
| 226 | if not read: |
| 227 | break |
| 228 | data += read |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 229 | |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 230 | read = zipopen.read(100) |
| 231 | if not read: |
| 232 | break |
| 233 | data += read |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 234 | |
| 235 | self.assertEqual(data, self.data) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 236 | |
| 237 | def test_readline_read(self): |
| 238 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 239 | for f in get_files(self): |
| 240 | self.zip_readline_read_test(f, self.compression) |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 241 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 242 | def zip_readline_test(self, f, compression): |
| 243 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 244 | |
| 245 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 246 | with zipfile.ZipFile(f, "r") as zipfp: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 247 | with zipfp.open(TESTFN) as zipopen: |
| 248 | for line in self.line_gen: |
| 249 | linedata = zipopen.readline() |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 250 | self.assertEqual(linedata, line) |
| 251 | |
| 252 | def test_readline(self): |
| 253 | for f in get_files(self): |
| 254 | self.zip_readline_test(f, self.compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 255 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 256 | def zip_readlines_test(self, f, compression): |
| 257 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 258 | |
| 259 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 260 | with zipfile.ZipFile(f, "r") as zipfp: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 261 | with zipfp.open(TESTFN) as zipopen: |
| 262 | ziplines = zipopen.readlines() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 263 | for line, zipline in zip(self.line_gen, ziplines): |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 264 | self.assertEqual(zipline, line) |
| 265 | |
| 266 | def test_readlines(self): |
| 267 | for f in get_files(self): |
| 268 | self.zip_readlines_test(f, self.compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 269 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 270 | def zip_iterlines_test(self, f, compression): |
| 271 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 272 | |
| 273 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 274 | with zipfile.ZipFile(f, "r") as zipfp: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 275 | with zipfp.open(TESTFN) as zipopen: |
| 276 | for line, zipline in zip(self.line_gen, zipopen): |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 277 | self.assertEqual(zipline, line) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 278 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 279 | def test_iterlines(self): |
| 280 | for f in get_files(self): |
| 281 | self.zip_iterlines_test(f, self.compression) |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 282 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 283 | def test_low_compression(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 284 | """Check for cases where compressed data is larger than original.""" |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 285 | # Create the ZIP archive |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 286 | with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipfp: |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 287 | zipfp.writestr("strfile", '12') |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 288 | |
| 289 | # Get an open object for strfile |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 290 | with zipfile.ZipFile(TESTFN2, "r", self.compression) as zipfp: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 291 | with zipfp.open("strfile") as openobj: |
| 292 | self.assertEqual(openobj.read(1), b'1') |
| 293 | self.assertEqual(openobj.read(1), b'2') |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 294 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 295 | def test_writestr_compression(self): |
| 296 | zipfp = zipfile.ZipFile(TESTFN2, "w") |
| 297 | zipfp.writestr("b.txt", "hello world", compress_type=self.compression) |
| 298 | info = zipfp.getinfo('b.txt') |
| 299 | self.assertEqual(info.compress_type, self.compression) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 300 | |
Bo Bayles | ce237c7 | 2018-01-29 23:54:07 -0600 | [diff] [blame] | 301 | def test_writestr_compresslevel(self): |
| 302 | zipfp = zipfile.ZipFile(TESTFN2, "w", compresslevel=1) |
| 303 | zipfp.writestr("a.txt", "hello world", compress_type=self.compression) |
| 304 | zipfp.writestr("b.txt", "hello world", compress_type=self.compression, |
| 305 | compresslevel=2) |
| 306 | |
| 307 | # Compression level follows the constructor. |
| 308 | a_info = zipfp.getinfo('a.txt') |
| 309 | self.assertEqual(a_info.compress_type, self.compression) |
| 310 | self.assertEqual(a_info._compresslevel, 1) |
| 311 | |
| 312 | # Compression level is overridden. |
| 313 | b_info = zipfp.getinfo('b.txt') |
| 314 | self.assertEqual(b_info.compress_type, self.compression) |
| 315 | self.assertEqual(b_info._compresslevel, 2) |
| 316 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 317 | def test_read_return_size(self): |
| 318 | # Issue #9837: ZipExtFile.read() shouldn't return more bytes |
| 319 | # than requested. |
| 320 | for test_size in (1, 4095, 4096, 4097, 16384): |
| 321 | file_size = test_size + 1 |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 322 | junk = getrandbytes(file_size) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 323 | with zipfile.ZipFile(io.BytesIO(), "w", self.compression) as zipf: |
| 324 | zipf.writestr('foo', junk) |
| 325 | with zipf.open('foo', 'r') as fp: |
| 326 | buf = fp.read(test_size) |
| 327 | self.assertEqual(len(buf), test_size) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 328 | |
Serhiy Storchaka | 5ce3f10 | 2014-01-09 14:50:20 +0200 | [diff] [blame] | 329 | def test_truncated_zipfile(self): |
| 330 | fp = io.BytesIO() |
| 331 | with zipfile.ZipFile(fp, mode='w') as zipf: |
| 332 | zipf.writestr('strfile', self.data, compress_type=self.compression) |
| 333 | end_offset = fp.tell() |
| 334 | zipfiledata = fp.getvalue() |
| 335 | |
| 336 | fp = io.BytesIO(zipfiledata) |
| 337 | with zipfile.ZipFile(fp) as zipf: |
| 338 | with zipf.open('strfile') as zipopen: |
| 339 | fp.truncate(end_offset - 20) |
| 340 | with self.assertRaises(EOFError): |
| 341 | zipopen.read() |
| 342 | |
| 343 | fp = io.BytesIO(zipfiledata) |
| 344 | with zipfile.ZipFile(fp) as zipf: |
| 345 | with zipf.open('strfile') as zipopen: |
| 346 | fp.truncate(end_offset - 20) |
| 347 | with self.assertRaises(EOFError): |
| 348 | while zipopen.read(100): |
| 349 | pass |
| 350 | |
| 351 | fp = io.BytesIO(zipfiledata) |
| 352 | with zipfile.ZipFile(fp) as zipf: |
| 353 | with zipf.open('strfile') as zipopen: |
| 354 | fp.truncate(end_offset - 20) |
| 355 | with self.assertRaises(EOFError): |
| 356 | while zipopen.read1(100): |
| 357 | pass |
| 358 | |
Serhiy Storchaka | 51a4370 | 2014-10-29 22:42:06 +0200 | [diff] [blame] | 359 | def test_repr(self): |
| 360 | fname = 'file.name' |
| 361 | for f in get_files(self): |
| 362 | with zipfile.ZipFile(f, 'w', self.compression) as zipfp: |
| 363 | zipfp.write(TESTFN, fname) |
| 364 | r = repr(zipfp) |
| 365 | self.assertIn("mode='w'", r) |
| 366 | |
| 367 | with zipfile.ZipFile(f, 'r') as zipfp: |
| 368 | r = repr(zipfp) |
| 369 | if isinstance(f, str): |
| 370 | self.assertIn('filename=%r' % f, r) |
| 371 | else: |
| 372 | self.assertIn('file=%r' % f, r) |
| 373 | self.assertIn("mode='r'", r) |
| 374 | r = repr(zipfp.getinfo(fname)) |
| 375 | self.assertIn('filename=%r' % fname, r) |
| 376 | self.assertIn('filemode=', r) |
| 377 | self.assertIn('file_size=', r) |
| 378 | if self.compression != zipfile.ZIP_STORED: |
| 379 | self.assertIn('compress_type=', r) |
| 380 | self.assertIn('compress_size=', r) |
| 381 | with zipfp.open(fname) as zipopen: |
| 382 | r = repr(zipopen) |
| 383 | self.assertIn('name=%r' % fname, r) |
| 384 | self.assertIn("mode='r'", r) |
| 385 | if self.compression != zipfile.ZIP_STORED: |
| 386 | self.assertIn('compress_type=', r) |
| 387 | self.assertIn('[closed]', repr(zipopen)) |
| 388 | self.assertIn('[closed]', repr(zipfp)) |
| 389 | |
Bo Bayles | ce237c7 | 2018-01-29 23:54:07 -0600 | [diff] [blame] | 390 | def test_compresslevel_basic(self): |
| 391 | for f in get_files(self): |
| 392 | self.zip_test(f, self.compression, compresslevel=9) |
| 393 | |
| 394 | def test_per_file_compresslevel(self): |
| 395 | """Check that files within a Zip archive can have different |
| 396 | compression levels.""" |
| 397 | with zipfile.ZipFile(TESTFN2, "w", compresslevel=1) as zipfp: |
| 398 | zipfp.write(TESTFN, 'compress_1') |
| 399 | zipfp.write(TESTFN, 'compress_9', compresslevel=9) |
| 400 | one_info = zipfp.getinfo('compress_1') |
| 401 | nine_info = zipfp.getinfo('compress_9') |
| 402 | self.assertEqual(one_info._compresslevel, 1) |
| 403 | self.assertEqual(nine_info._compresslevel, 9) |
| 404 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 405 | def tearDown(self): |
| 406 | unlink(TESTFN) |
| 407 | unlink(TESTFN2) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 408 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 409 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 410 | class StoredTestsWithSourceFile(AbstractTestsWithSourceFile, |
| 411 | unittest.TestCase): |
| 412 | compression = zipfile.ZIP_STORED |
| 413 | test_low_compression = None |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 414 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 415 | def zip_test_writestr_permissions(self, f, compression): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 416 | # Make sure that writestr and open(... mode='w') create files with |
| 417 | # mode 0600, when they are passed a name rather than a ZipInfo |
| 418 | # instance. |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 419 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 420 | self.make_test_archive(f, compression) |
| 421 | with zipfile.ZipFile(f, "r") as zipfp: |
| 422 | zinfo = zipfp.getinfo('strfile') |
| 423 | self.assertEqual(zinfo.external_attr, 0o600 << 16) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 424 | |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 425 | zinfo2 = zipfp.getinfo('written-open-w') |
| 426 | self.assertEqual(zinfo2.external_attr, 0o600 << 16) |
| 427 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 428 | def test_writestr_permissions(self): |
| 429 | for f in get_files(self): |
| 430 | self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 431 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 432 | def test_absolute_arcnames(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 433 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 434 | zipfp.write(TESTFN, "/absolute") |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame] | 435 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 436 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 437 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
Tim Peters | 32cbc96 | 2006-02-20 21:42:18 +0000 | [diff] [blame] | 438 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 439 | def test_append_to_zip_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 440 | """Test appending to an existing zipfile.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 441 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 442 | zipfp.write(TESTFN, TESTFN) |
| 443 | |
| 444 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 445 | zipfp.writestr("strfile", self.data) |
| 446 | self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"]) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 447 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 448 | def test_append_to_non_zip_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 449 | """Test appending to an existing file that is not a zipfile.""" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 450 | # NOTE: this test fails if len(d) < 22 because of the first |
| 451 | # line "fpin.seek(-22, 2)" in _EndRecData |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 452 | data = b'I am not a ZipFile!'*10 |
| 453 | with open(TESTFN2, 'wb') as f: |
| 454 | f.write(data) |
| 455 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 456 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 457 | zipfp.write(TESTFN, TESTFN) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 458 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 459 | with open(TESTFN2, 'rb') as f: |
| 460 | f.seek(len(data)) |
| 461 | with zipfile.ZipFile(f, "r") as zipfp: |
| 462 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
Serhiy Storchaka | 8793b21 | 2016-10-07 22:20:50 +0300 | [diff] [blame] | 463 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 464 | with open(TESTFN2, 'rb') as f: |
| 465 | self.assertEqual(f.read(len(data)), data) |
| 466 | zipfiledata = f.read() |
| 467 | with io.BytesIO(zipfiledata) as bio, zipfile.ZipFile(bio) as zipfp: |
| 468 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
| 469 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 470 | |
| 471 | def test_read_concatenated_zip_file(self): |
| 472 | with io.BytesIO() as bio: |
| 473 | with zipfile.ZipFile(bio, 'w', zipfile.ZIP_STORED) as zipfp: |
| 474 | zipfp.write(TESTFN, TESTFN) |
| 475 | zipfiledata = bio.getvalue() |
| 476 | data = b'I am not a ZipFile!'*10 |
| 477 | with open(TESTFN2, 'wb') as f: |
| 478 | f.write(data) |
| 479 | f.write(zipfiledata) |
| 480 | |
| 481 | with zipfile.ZipFile(TESTFN2) as zipfp: |
| 482 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
| 483 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 484 | |
| 485 | def test_append_to_concatenated_zip_file(self): |
| 486 | with io.BytesIO() as bio: |
| 487 | with zipfile.ZipFile(bio, 'w', zipfile.ZIP_STORED) as zipfp: |
| 488 | zipfp.write(TESTFN, TESTFN) |
| 489 | zipfiledata = bio.getvalue() |
| 490 | data = b'I am not a ZipFile!'*1000000 |
| 491 | with open(TESTFN2, 'wb') as f: |
| 492 | f.write(data) |
| 493 | f.write(zipfiledata) |
| 494 | |
| 495 | with zipfile.ZipFile(TESTFN2, 'a') as zipfp: |
| 496 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
| 497 | zipfp.writestr('strfile', self.data) |
| 498 | |
| 499 | with open(TESTFN2, 'rb') as f: |
| 500 | self.assertEqual(f.read(len(data)), data) |
| 501 | zipfiledata = f.read() |
| 502 | with io.BytesIO(zipfiledata) as bio, zipfile.ZipFile(bio) as zipfp: |
| 503 | self.assertEqual(zipfp.namelist(), [TESTFN, 'strfile']) |
| 504 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 505 | self.assertEqual(zipfp.read('strfile'), self.data) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 506 | |
R David Murray | 4fbb9db | 2011-06-09 15:50:51 -0400 | [diff] [blame] | 507 | def test_ignores_newline_at_end(self): |
| 508 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 509 | zipfp.write(TESTFN, TESTFN) |
| 510 | with open(TESTFN2, 'a') as f: |
| 511 | f.write("\r\n\00\00\00") |
| 512 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 513 | self.assertIsInstance(zipfp, zipfile.ZipFile) |
| 514 | |
| 515 | def test_ignores_stuff_appended_past_comments(self): |
| 516 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 517 | zipfp.comment = b"this is a comment" |
| 518 | zipfp.write(TESTFN, TESTFN) |
| 519 | with open(TESTFN2, 'a') as f: |
| 520 | f.write("abcdef\r\n") |
| 521 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 522 | self.assertIsInstance(zipfp, zipfile.ZipFile) |
| 523 | self.assertEqual(zipfp.comment, b"this is a comment") |
| 524 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 525 | def test_write_default_name(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 526 | """Check that calling ZipFile.write without arcname specified |
| 527 | produces the expected result.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 528 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 529 | zipfp.write(TESTFN) |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 530 | with open(TESTFN, "rb") as f: |
| 531 | self.assertEqual(zipfp.read(TESTFN), f.read()) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 532 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 533 | def test_write_to_readonly(self): |
| 534 | """Check that trying to call write() on a readonly ZipFile object |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 535 | raises a ValueError.""" |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 536 | with zipfile.ZipFile(TESTFN2, mode="w") as zipfp: |
| 537 | zipfp.writestr("somefile.txt", "bogus") |
| 538 | |
| 539 | with zipfile.ZipFile(TESTFN2, mode="r") as zipfp: |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 540 | self.assertRaises(ValueError, zipfp.write, TESTFN) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 541 | |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 542 | with zipfile.ZipFile(TESTFN2, mode="r") as zipfp: |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 543 | with self.assertRaises(ValueError): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 544 | zipfp.open(TESTFN, mode='w') |
| 545 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 546 | def test_add_file_before_1980(self): |
| 547 | # Set atime and mtime to 1970-01-01 |
| 548 | os.utime(TESTFN, (0, 0)) |
| 549 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 550 | self.assertRaises(ValueError, zipfp.write, TESTFN) |
| 551 | |
Serhiy Storchaka | 5ce3f10 | 2014-01-09 14:50:20 +0200 | [diff] [blame] | 552 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 553 | @requires_zlib |
| 554 | class DeflateTestsWithSourceFile(AbstractTestsWithSourceFile, |
| 555 | unittest.TestCase): |
| 556 | compression = zipfile.ZIP_DEFLATED |
| 557 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 558 | def test_per_file_compression(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 559 | """Check that files within a Zip archive can have different |
| 560 | compression options.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 561 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 562 | zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED) |
| 563 | zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED) |
| 564 | sinfo = zipfp.getinfo('storeme') |
| 565 | dinfo = zipfp.getinfo('deflateme') |
| 566 | self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED) |
| 567 | self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 568 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 569 | @requires_bz2 |
| 570 | class Bzip2TestsWithSourceFile(AbstractTestsWithSourceFile, |
| 571 | unittest.TestCase): |
| 572 | compression = zipfile.ZIP_BZIP2 |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 573 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 574 | @requires_lzma |
| 575 | class LzmaTestsWithSourceFile(AbstractTestsWithSourceFile, |
| 576 | unittest.TestCase): |
| 577 | compression = zipfile.ZIP_LZMA |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 578 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 579 | |
| 580 | class AbstractTestZip64InSmallFiles: |
| 581 | # These tests test the ZIP64 functionality without using large files, |
| 582 | # see test_zipfile64 for proper tests. |
| 583 | |
| 584 | @classmethod |
| 585 | def setUpClass(cls): |
| 586 | line_gen = (bytes("Test of zipfile line %d." % i, "ascii") |
| 587 | for i in range(0, FIXEDTEST_SIZE)) |
| 588 | cls.data = b'\n'.join(line_gen) |
| 589 | |
| 590 | def setUp(self): |
| 591 | self._limit = zipfile.ZIP64_LIMIT |
Serhiy Storchaka | 026a399 | 2014-09-23 22:27:34 +0300 | [diff] [blame] | 592 | self._filecount_limit = zipfile.ZIP_FILECOUNT_LIMIT |
| 593 | zipfile.ZIP64_LIMIT = 1000 |
| 594 | zipfile.ZIP_FILECOUNT_LIMIT = 9 |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 595 | |
| 596 | # Make a source file with some lines |
| 597 | with open(TESTFN, "wb") as fp: |
| 598 | fp.write(self.data) |
| 599 | |
| 600 | def zip_test(self, f, compression): |
| 601 | # Create the ZIP archive |
| 602 | with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp: |
| 603 | zipfp.write(TESTFN, "another.name") |
| 604 | zipfp.write(TESTFN, TESTFN) |
| 605 | zipfp.writestr("strfile", self.data) |
| 606 | |
| 607 | # Read the ZIP archive |
| 608 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 609 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 610 | self.assertEqual(zipfp.read("another.name"), self.data) |
| 611 | self.assertEqual(zipfp.read("strfile"), self.data) |
| 612 | |
| 613 | # Print the ZIP directory |
| 614 | fp = io.StringIO() |
| 615 | zipfp.printdir(fp) |
| 616 | |
| 617 | directory = fp.getvalue() |
| 618 | lines = directory.splitlines() |
| 619 | self.assertEqual(len(lines), 4) # Number of files + header |
| 620 | |
| 621 | self.assertIn('File Name', lines[0]) |
| 622 | self.assertIn('Modified', lines[0]) |
| 623 | self.assertIn('Size', lines[0]) |
| 624 | |
| 625 | fn, date, time_, size = lines[1].split() |
| 626 | self.assertEqual(fn, 'another.name') |
| 627 | self.assertTrue(time.strptime(date, '%Y-%m-%d')) |
| 628 | self.assertTrue(time.strptime(time_, '%H:%M:%S')) |
| 629 | self.assertEqual(size, str(len(self.data))) |
| 630 | |
| 631 | # Check the namelist |
| 632 | names = zipfp.namelist() |
| 633 | self.assertEqual(len(names), 3) |
| 634 | self.assertIn(TESTFN, names) |
| 635 | self.assertIn("another.name", names) |
| 636 | self.assertIn("strfile", names) |
| 637 | |
| 638 | # Check infolist |
| 639 | infos = zipfp.infolist() |
| 640 | names = [i.filename for i in infos] |
| 641 | self.assertEqual(len(names), 3) |
| 642 | self.assertIn(TESTFN, names) |
| 643 | self.assertIn("another.name", names) |
| 644 | self.assertIn("strfile", names) |
| 645 | for i in infos: |
| 646 | self.assertEqual(i.file_size, len(self.data)) |
| 647 | |
| 648 | # check getinfo |
| 649 | for nm in (TESTFN, "another.name", "strfile"): |
| 650 | info = zipfp.getinfo(nm) |
| 651 | self.assertEqual(info.filename, nm) |
| 652 | self.assertEqual(info.file_size, len(self.data)) |
| 653 | |
| 654 | # Check that testzip doesn't raise an exception |
| 655 | zipfp.testzip() |
| 656 | |
| 657 | def test_basic(self): |
| 658 | for f in get_files(self): |
| 659 | self.zip_test(f, self.compression) |
| 660 | |
Serhiy Storchaka | 026a399 | 2014-09-23 22:27:34 +0300 | [diff] [blame] | 661 | def test_too_many_files(self): |
| 662 | # This test checks that more than 64k files can be added to an archive, |
| 663 | # and that the resulting archive can be read properly by ZipFile |
| 664 | zipf = zipfile.ZipFile(TESTFN, "w", self.compression, |
| 665 | allowZip64=True) |
| 666 | zipf.debug = 100 |
| 667 | numfiles = 15 |
| 668 | for i in range(numfiles): |
| 669 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 670 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 671 | zipf.close() |
| 672 | |
| 673 | zipf2 = zipfile.ZipFile(TESTFN, "r", self.compression) |
| 674 | self.assertEqual(len(zipf2.namelist()), numfiles) |
| 675 | for i in range(numfiles): |
| 676 | content = zipf2.read("foo%08d" % i).decode('ascii') |
| 677 | self.assertEqual(content, "%d" % (i**3 % 57)) |
| 678 | zipf2.close() |
| 679 | |
| 680 | def test_too_many_files_append(self): |
| 681 | zipf = zipfile.ZipFile(TESTFN, "w", self.compression, |
| 682 | allowZip64=False) |
| 683 | zipf.debug = 100 |
| 684 | numfiles = 9 |
| 685 | for i in range(numfiles): |
| 686 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 687 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 688 | with self.assertRaises(zipfile.LargeZipFile): |
| 689 | zipf.writestr("foo%08d" % numfiles, b'') |
| 690 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 691 | zipf.close() |
| 692 | |
| 693 | zipf = zipfile.ZipFile(TESTFN, "a", self.compression, |
| 694 | allowZip64=False) |
| 695 | zipf.debug = 100 |
| 696 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 697 | with self.assertRaises(zipfile.LargeZipFile): |
| 698 | zipf.writestr("foo%08d" % numfiles, b'') |
| 699 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 700 | zipf.close() |
| 701 | |
| 702 | zipf = zipfile.ZipFile(TESTFN, "a", self.compression, |
| 703 | allowZip64=True) |
| 704 | zipf.debug = 100 |
| 705 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 706 | numfiles2 = 15 |
| 707 | for i in range(numfiles, numfiles2): |
| 708 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 709 | self.assertEqual(len(zipf.namelist()), numfiles2) |
| 710 | zipf.close() |
| 711 | |
| 712 | zipf2 = zipfile.ZipFile(TESTFN, "r", self.compression) |
| 713 | self.assertEqual(len(zipf2.namelist()), numfiles2) |
| 714 | for i in range(numfiles2): |
| 715 | content = zipf2.read("foo%08d" % i).decode('ascii') |
| 716 | self.assertEqual(content, "%d" % (i**3 % 57)) |
| 717 | zipf2.close() |
| 718 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 719 | def tearDown(self): |
| 720 | zipfile.ZIP64_LIMIT = self._limit |
Serhiy Storchaka | 026a399 | 2014-09-23 22:27:34 +0300 | [diff] [blame] | 721 | zipfile.ZIP_FILECOUNT_LIMIT = self._filecount_limit |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 722 | unlink(TESTFN) |
| 723 | unlink(TESTFN2) |
| 724 | |
| 725 | |
| 726 | class StoredTestZip64InSmallFiles(AbstractTestZip64InSmallFiles, |
| 727 | unittest.TestCase): |
| 728 | compression = zipfile.ZIP_STORED |
| 729 | |
| 730 | def large_file_exception_test(self, f, compression): |
Serhiy Storchaka | 235c5e0 | 2013-11-23 15:55:38 +0200 | [diff] [blame] | 731 | with zipfile.ZipFile(f, "w", compression, allowZip64=False) as zipfp: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 732 | self.assertRaises(zipfile.LargeZipFile, |
| 733 | zipfp.write, TESTFN, "another.name") |
| 734 | |
| 735 | def large_file_exception_test2(self, f, compression): |
Serhiy Storchaka | 235c5e0 | 2013-11-23 15:55:38 +0200 | [diff] [blame] | 736 | with zipfile.ZipFile(f, "w", compression, allowZip64=False) as zipfp: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 737 | self.assertRaises(zipfile.LargeZipFile, |
| 738 | zipfp.writestr, "another.name", self.data) |
| 739 | |
| 740 | def test_large_file_exception(self): |
| 741 | for f in get_files(self): |
| 742 | self.large_file_exception_test(f, zipfile.ZIP_STORED) |
| 743 | self.large_file_exception_test2(f, zipfile.ZIP_STORED) |
| 744 | |
| 745 | def test_absolute_arcnames(self): |
| 746 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, |
| 747 | allowZip64=True) as zipfp: |
| 748 | zipfp.write(TESTFN, "/absolute") |
| 749 | |
| 750 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 751 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
| 752 | |
| 753 | @requires_zlib |
| 754 | class DeflateTestZip64InSmallFiles(AbstractTestZip64InSmallFiles, |
| 755 | unittest.TestCase): |
| 756 | compression = zipfile.ZIP_DEFLATED |
| 757 | |
| 758 | @requires_bz2 |
| 759 | class Bzip2TestZip64InSmallFiles(AbstractTestZip64InSmallFiles, |
| 760 | unittest.TestCase): |
| 761 | compression = zipfile.ZIP_BZIP2 |
| 762 | |
| 763 | @requires_lzma |
| 764 | class LzmaTestZip64InSmallFiles(AbstractTestZip64InSmallFiles, |
| 765 | unittest.TestCase): |
| 766 | compression = zipfile.ZIP_LZMA |
| 767 | |
| 768 | |
Serhiy Storchaka | 4c0d9ea | 2017-04-12 16:03:23 +0300 | [diff] [blame] | 769 | class AbstractWriterTests: |
| 770 | |
| 771 | def tearDown(self): |
| 772 | unlink(TESTFN2) |
| 773 | |
| 774 | def test_close_after_close(self): |
| 775 | data = b'content' |
| 776 | with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipf: |
| 777 | w = zipf.open('test', 'w') |
| 778 | w.write(data) |
| 779 | w.close() |
| 780 | self.assertTrue(w.closed) |
| 781 | w.close() |
| 782 | self.assertTrue(w.closed) |
| 783 | self.assertEqual(zipf.read('test'), data) |
| 784 | |
| 785 | def test_write_after_close(self): |
| 786 | data = b'content' |
| 787 | with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipf: |
| 788 | w = zipf.open('test', 'w') |
| 789 | w.write(data) |
| 790 | w.close() |
| 791 | self.assertTrue(w.closed) |
| 792 | self.assertRaises(ValueError, w.write, b'') |
| 793 | self.assertEqual(zipf.read('test'), data) |
| 794 | |
| 795 | class StoredWriterTests(AbstractWriterTests, unittest.TestCase): |
| 796 | compression = zipfile.ZIP_STORED |
| 797 | |
| 798 | @requires_zlib |
| 799 | class DeflateWriterTests(AbstractWriterTests, unittest.TestCase): |
| 800 | compression = zipfile.ZIP_DEFLATED |
| 801 | |
| 802 | @requires_bz2 |
| 803 | class Bzip2WriterTests(AbstractWriterTests, unittest.TestCase): |
| 804 | compression = zipfile.ZIP_BZIP2 |
| 805 | |
| 806 | @requires_lzma |
| 807 | class LzmaWriterTests(AbstractWriterTests, unittest.TestCase): |
| 808 | compression = zipfile.ZIP_LZMA |
| 809 | |
| 810 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 811 | class PyZipFileTests(unittest.TestCase): |
| 812 | def assertCompiledIn(self, name, namelist): |
| 813 | if name + 'o' not in namelist: |
| 814 | self.assertIn(name + 'c', namelist) |
| 815 | |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 816 | def requiresWriteAccess(self, path): |
Berker Peksag | e1efc07 | 2015-02-16 04:36:18 +0200 | [diff] [blame] | 817 | # effective_ids unavailable on windows |
| 818 | if not os.access(path, os.W_OK, |
| 819 | effective_ids=os.access in os.supports_effective_ids): |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 820 | self.skipTest('requires write access to the installed location') |
Serhiy Storchaka | d86a6ef | 2015-09-19 10:55:20 +0300 | [diff] [blame] | 821 | filename = os.path.join(path, 'test_zipfile.try') |
| 822 | try: |
| 823 | fd = os.open(filename, os.O_WRONLY | os.O_CREAT) |
| 824 | os.close(fd) |
| 825 | except Exception: |
| 826 | self.skipTest('requires write access to the installed location') |
| 827 | unlink(filename) |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 828 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 829 | def test_write_pyfile(self): |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 830 | self.requiresWriteAccess(os.path.dirname(__file__)) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 831 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 832 | fn = __file__ |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 833 | if fn.endswith('.pyc'): |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 834 | path_split = fn.split(os.sep) |
| 835 | if os.altsep is not None: |
| 836 | path_split.extend(fn.split(os.altsep)) |
| 837 | if '__pycache__' in path_split: |
Serhiy Storchaka | 9068e4d | 2013-07-22 21:02:14 +0300 | [diff] [blame] | 838 | fn = importlib.util.source_from_cache(fn) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 839 | else: |
| 840 | fn = fn[:-1] |
| 841 | |
| 842 | zipfp.writepy(fn) |
| 843 | |
| 844 | bn = os.path.basename(fn) |
| 845 | self.assertNotIn(bn, zipfp.namelist()) |
| 846 | self.assertCompiledIn(bn, zipfp.namelist()) |
| 847 | |
| 848 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 849 | fn = __file__ |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 850 | if fn.endswith('.pyc'): |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 851 | fn = fn[:-1] |
| 852 | |
| 853 | zipfp.writepy(fn, "testpackage") |
| 854 | |
| 855 | bn = "%s/%s" % ("testpackage", os.path.basename(fn)) |
| 856 | self.assertNotIn(bn, zipfp.namelist()) |
| 857 | self.assertCompiledIn(bn, zipfp.namelist()) |
| 858 | |
| 859 | def test_write_python_package(self): |
| 860 | import email |
| 861 | packagedir = os.path.dirname(email.__file__) |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 862 | self.requiresWriteAccess(packagedir) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 863 | |
| 864 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 865 | zipfp.writepy(packagedir) |
| 866 | |
| 867 | # Check for a couple of modules at different levels of the |
| 868 | # hierarchy |
| 869 | names = zipfp.namelist() |
| 870 | self.assertCompiledIn('email/__init__.py', names) |
| 871 | self.assertCompiledIn('email/mime/text.py', names) |
| 872 | |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 873 | def test_write_filtered_python_package(self): |
| 874 | import test |
| 875 | packagedir = os.path.dirname(test.__file__) |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 876 | self.requiresWriteAccess(packagedir) |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 877 | |
| 878 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 879 | |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 880 | # first make sure that the test folder gives error messages |
Georg Brandl | a606542 | 2013-10-21 08:29:29 +0200 | [diff] [blame] | 881 | # (on the badsyntax_... files) |
| 882 | with captured_stdout() as reportSIO: |
| 883 | zipfp.writepy(packagedir) |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 884 | reportStr = reportSIO.getvalue() |
| 885 | self.assertTrue('SyntaxError' in reportStr) |
| 886 | |
Christian Tismer | 410d931 | 2013-10-22 04:09:28 +0200 | [diff] [blame] | 887 | # then check that the filter works on the whole package |
Georg Brandl | a606542 | 2013-10-21 08:29:29 +0200 | [diff] [blame] | 888 | with captured_stdout() as reportSIO: |
| 889 | zipfp.writepy(packagedir, filterfunc=lambda whatever: False) |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 890 | reportStr = reportSIO.getvalue() |
| 891 | self.assertTrue('SyntaxError' not in reportStr) |
| 892 | |
Christian Tismer | 410d931 | 2013-10-22 04:09:28 +0200 | [diff] [blame] | 893 | # then check that the filter works on individual files |
Larry Hastings | 7e63b36 | 2015-05-08 06:54:58 -0700 | [diff] [blame] | 894 | def filter(path): |
| 895 | return not os.path.basename(path).startswith("bad") |
Serhiy Storchaka | c46d1fa | 2014-01-20 21:59:33 +0200 | [diff] [blame] | 896 | with captured_stdout() as reportSIO, self.assertWarns(UserWarning): |
Larry Hastings | 7e63b36 | 2015-05-08 06:54:58 -0700 | [diff] [blame] | 897 | zipfp.writepy(packagedir, filterfunc=filter) |
Christian Tismer | 410d931 | 2013-10-22 04:09:28 +0200 | [diff] [blame] | 898 | reportStr = reportSIO.getvalue() |
| 899 | if reportStr: |
| 900 | print(reportStr) |
| 901 | self.assertTrue('SyntaxError' not in reportStr) |
| 902 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 903 | def test_write_with_optimization(self): |
| 904 | import email |
| 905 | packagedir = os.path.dirname(email.__file__) |
Serhiy Storchaka | db724fe | 2015-02-14 23:04:35 +0200 | [diff] [blame] | 906 | self.requiresWriteAccess(packagedir) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 907 | optlevel = 1 if __debug__ else 0 |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 908 | ext = '.pyc' |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 909 | |
| 910 | with TemporaryFile() as t, \ |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 911 | zipfile.PyZipFile(t, "w", optimize=optlevel) as zipfp: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 912 | zipfp.writepy(packagedir) |
| 913 | |
| 914 | names = zipfp.namelist() |
| 915 | self.assertIn('email/__init__' + ext, names) |
| 916 | self.assertIn('email/mime/text' + ext, names) |
| 917 | |
| 918 | def test_write_python_directory(self): |
| 919 | os.mkdir(TESTFN2) |
| 920 | try: |
| 921 | with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: |
| 922 | fp.write("print(42)\n") |
| 923 | |
| 924 | with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: |
| 925 | fp.write("print(42 * 42)\n") |
| 926 | |
| 927 | with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: |
| 928 | fp.write("bla bla bla\n") |
| 929 | |
| 930 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 931 | zipfp.writepy(TESTFN2) |
| 932 | |
| 933 | names = zipfp.namelist() |
| 934 | self.assertCompiledIn('mod1.py', names) |
| 935 | self.assertCompiledIn('mod2.py', names) |
| 936 | self.assertNotIn('mod2.txt', names) |
| 937 | |
| 938 | finally: |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 939 | rmtree(TESTFN2) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 940 | |
Christian Tismer | 410d931 | 2013-10-22 04:09:28 +0200 | [diff] [blame] | 941 | def test_write_python_directory_filtered(self): |
| 942 | os.mkdir(TESTFN2) |
| 943 | try: |
| 944 | with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: |
| 945 | fp.write("print(42)\n") |
| 946 | |
| 947 | with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: |
| 948 | fp.write("print(42 * 42)\n") |
| 949 | |
| 950 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 951 | zipfp.writepy(TESTFN2, filterfunc=lambda fn: |
| 952 | not fn.endswith('mod2.py')) |
| 953 | |
| 954 | names = zipfp.namelist() |
| 955 | self.assertCompiledIn('mod1.py', names) |
| 956 | self.assertNotIn('mod2.py', names) |
| 957 | |
| 958 | finally: |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 959 | rmtree(TESTFN2) |
Christian Tismer | 410d931 | 2013-10-22 04:09:28 +0200 | [diff] [blame] | 960 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 961 | def test_write_non_pyfile(self): |
| 962 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 963 | with open(TESTFN, 'w') as f: |
| 964 | f.write('most definitely not a python file') |
| 965 | self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) |
Victor Stinner | 88b215e | 2014-09-04 00:51:09 +0200 | [diff] [blame] | 966 | unlink(TESTFN) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 967 | |
| 968 | def test_write_pyfile_bad_syntax(self): |
| 969 | os.mkdir(TESTFN2) |
| 970 | try: |
| 971 | with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: |
| 972 | fp.write("Bad syntax in python file\n") |
| 973 | |
| 974 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 975 | # syntax errors are printed to stdout |
| 976 | with captured_stdout() as s: |
| 977 | zipfp.writepy(os.path.join(TESTFN2, "mod1.py")) |
| 978 | |
| 979 | self.assertIn("SyntaxError", s.getvalue()) |
| 980 | |
| 981 | # as it will not have compiled the python file, it will |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 982 | # include the .py file not .pyc |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 983 | names = zipfp.namelist() |
| 984 | self.assertIn('mod1.py', names) |
| 985 | self.assertNotIn('mod1.pyc', names) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 986 | |
| 987 | finally: |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 988 | rmtree(TESTFN2) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 989 | |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 990 | def test_write_pathlike(self): |
| 991 | os.mkdir(TESTFN2) |
| 992 | try: |
| 993 | with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: |
| 994 | fp.write("print(42)\n") |
| 995 | |
| 996 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 997 | zipfp.writepy(pathlib.Path(TESTFN2) / "mod1.py") |
| 998 | names = zipfp.namelist() |
| 999 | self.assertCompiledIn('mod1.py', names) |
| 1000 | finally: |
| 1001 | rmtree(TESTFN2) |
| 1002 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1003 | |
| 1004 | class ExtractTests(unittest.TestCase): |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1005 | |
| 1006 | def make_test_file(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1007 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 1008 | for fpath, fdata in SMALL_TEST_DATA: |
| 1009 | zipfp.writestr(fpath, fdata) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1010 | |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1011 | def test_extract(self): |
| 1012 | with temp_cwd(): |
| 1013 | self.make_test_file() |
| 1014 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 1015 | for fpath, fdata in SMALL_TEST_DATA: |
| 1016 | writtenfile = zipfp.extract(fpath) |
| 1017 | |
| 1018 | # make sure it was written to the right place |
| 1019 | correctfile = os.path.join(os.getcwd(), fpath) |
| 1020 | correctfile = os.path.normpath(correctfile) |
| 1021 | |
| 1022 | self.assertEqual(writtenfile, correctfile) |
| 1023 | |
| 1024 | # make sure correct data is in correct file |
| 1025 | with open(writtenfile, "rb") as f: |
| 1026 | self.assertEqual(fdata.encode(), f.read()) |
| 1027 | |
| 1028 | unlink(writtenfile) |
| 1029 | |
| 1030 | def _test_extract_with_target(self, target): |
| 1031 | self.make_test_file() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1032 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 1033 | for fpath, fdata in SMALL_TEST_DATA: |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1034 | writtenfile = zipfp.extract(fpath, target) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1035 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1036 | # make sure it was written to the right place |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1037 | correctfile = os.path.join(target, fpath) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1038 | correctfile = os.path.normpath(correctfile) |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1039 | self.assertTrue(os.path.samefile(writtenfile, correctfile), (writtenfile, target)) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1040 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1041 | # make sure correct data is in correct file |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1042 | with open(writtenfile, "rb") as f: |
| 1043 | self.assertEqual(fdata.encode(), f.read()) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1044 | |
Victor Stinner | 88b215e | 2014-09-04 00:51:09 +0200 | [diff] [blame] | 1045 | unlink(writtenfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1046 | |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1047 | unlink(TESTFN2) |
| 1048 | |
| 1049 | def test_extract_with_target(self): |
| 1050 | with temp_dir() as extdir: |
| 1051 | self._test_extract_with_target(extdir) |
| 1052 | |
| 1053 | def test_extract_with_target_pathlike(self): |
| 1054 | with temp_dir() as extdir: |
| 1055 | self._test_extract_with_target(pathlib.Path(extdir)) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1056 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1057 | def test_extract_all(self): |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1058 | with temp_cwd(): |
| 1059 | self.make_test_file() |
| 1060 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 1061 | zipfp.extractall() |
| 1062 | for fpath, fdata in SMALL_TEST_DATA: |
| 1063 | outfile = os.path.join(os.getcwd(), fpath) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1064 | |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1065 | with open(outfile, "rb") as f: |
| 1066 | self.assertEqual(fdata.encode(), f.read()) |
| 1067 | |
| 1068 | unlink(outfile) |
| 1069 | |
| 1070 | def _test_extract_all_with_target(self, target): |
| 1071 | self.make_test_file() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1072 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1073 | zipfp.extractall(target) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1074 | for fpath, fdata in SMALL_TEST_DATA: |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1075 | outfile = os.path.join(target, fpath) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1076 | |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1077 | with open(outfile, "rb") as f: |
| 1078 | self.assertEqual(fdata.encode(), f.read()) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1079 | |
Victor Stinner | 88b215e | 2014-09-04 00:51:09 +0200 | [diff] [blame] | 1080 | unlink(outfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1081 | |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1082 | unlink(TESTFN2) |
| 1083 | |
| 1084 | def test_extract_all_with_target(self): |
| 1085 | with temp_dir() as extdir: |
| 1086 | self._test_extract_all_with_target(extdir) |
| 1087 | |
| 1088 | def test_extract_all_with_target_pathlike(self): |
| 1089 | with temp_dir() as extdir: |
| 1090 | self._test_extract_all_with_target(pathlib.Path(extdir)) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1091 | |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1092 | def check_file(self, filename, content): |
| 1093 | self.assertTrue(os.path.isfile(filename)) |
| 1094 | with open(filename, 'rb') as f: |
| 1095 | self.assertEqual(f.read(), content) |
| 1096 | |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1097 | def test_sanitize_windows_name(self): |
| 1098 | san = zipfile.ZipFile._sanitize_windows_name |
| 1099 | # Passing pathsep in allows this test to work regardless of platform. |
| 1100 | self.assertEqual(san(r',,?,C:,foo,bar/z', ','), r'_,C_,foo,bar/z') |
| 1101 | self.assertEqual(san(r'a\b,c<d>e|f"g?h*i', ','), r'a\b,c_d_e_f_g_h_i') |
| 1102 | self.assertEqual(san('../../foo../../ba..r', '/'), r'foo/ba..r') |
| 1103 | |
| 1104 | def test_extract_hackers_arcnames_common_cases(self): |
| 1105 | common_hacknames = [ |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1106 | ('../foo/bar', 'foo/bar'), |
| 1107 | ('foo/../bar', 'foo/bar'), |
| 1108 | ('foo/../../bar', 'foo/bar'), |
| 1109 | ('foo/bar/..', 'foo/bar'), |
| 1110 | ('./../foo/bar', 'foo/bar'), |
| 1111 | ('/foo/bar', 'foo/bar'), |
| 1112 | ('/foo/../bar', 'foo/bar'), |
| 1113 | ('/foo/../../bar', 'foo/bar'), |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1114 | ] |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1115 | self._test_extract_hackers_arcnames(common_hacknames) |
| 1116 | |
| 1117 | @unittest.skipIf(os.path.sep != '\\', 'Requires \\ as path separator.') |
| 1118 | def test_extract_hackers_arcnames_windows_only(self): |
| 1119 | """Test combination of path fixing and windows name sanitization.""" |
| 1120 | windows_hacknames = [ |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1121 | (r'..\foo\bar', 'foo/bar'), |
| 1122 | (r'..\/foo\/bar', 'foo/bar'), |
| 1123 | (r'foo/\..\/bar', 'foo/bar'), |
| 1124 | (r'foo\/../\bar', 'foo/bar'), |
| 1125 | (r'C:foo/bar', 'foo/bar'), |
| 1126 | (r'C:/foo/bar', 'foo/bar'), |
| 1127 | (r'C://foo/bar', 'foo/bar'), |
| 1128 | (r'C:\foo\bar', 'foo/bar'), |
| 1129 | (r'//conky/mountpoint/foo/bar', 'foo/bar'), |
| 1130 | (r'\\conky\mountpoint\foo\bar', 'foo/bar'), |
| 1131 | (r'///conky/mountpoint/foo/bar', 'conky/mountpoint/foo/bar'), |
| 1132 | (r'\\\conky\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'), |
| 1133 | (r'//conky//mountpoint/foo/bar', 'conky/mountpoint/foo/bar'), |
| 1134 | (r'\\conky\\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'), |
| 1135 | (r'//?/C:/foo/bar', 'foo/bar'), |
| 1136 | (r'\\?\C:\foo\bar', 'foo/bar'), |
| 1137 | (r'C:/../C:/foo/bar', 'C_/foo/bar'), |
| 1138 | (r'a:b\c<d>e|f"g?h*i', 'b/c_d_e_f_g_h_i'), |
| 1139 | ('../../foo../../ba..r', 'foo/ba..r'), |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1140 | ] |
| 1141 | self._test_extract_hackers_arcnames(windows_hacknames) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1142 | |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1143 | @unittest.skipIf(os.path.sep != '/', r'Requires / as path separator.') |
| 1144 | def test_extract_hackers_arcnames_posix_only(self): |
| 1145 | posix_hacknames = [ |
| 1146 | ('//foo/bar', 'foo/bar'), |
| 1147 | ('../../foo../../ba..r', 'foo../ba..r'), |
| 1148 | (r'foo/..\bar', r'foo/..\bar'), |
| 1149 | ] |
| 1150 | self._test_extract_hackers_arcnames(posix_hacknames) |
| 1151 | |
| 1152 | def _test_extract_hackers_arcnames(self, hacknames): |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1153 | for arcname, fixedname in hacknames: |
| 1154 | content = b'foobar' + arcname.encode() |
| 1155 | with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipfp: |
Serhiy Storchaka | e5e6444 | 2013-02-02 19:50:59 +0200 | [diff] [blame] | 1156 | zinfo = zipfile.ZipInfo() |
| 1157 | # preserve backslashes |
| 1158 | zinfo.filename = arcname |
| 1159 | zinfo.external_attr = 0o600 << 16 |
| 1160 | zipfp.writestr(zinfo, content) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1161 | |
Serhiy Storchaka | e5e6444 | 2013-02-02 19:50:59 +0200 | [diff] [blame] | 1162 | arcname = arcname.replace(os.sep, "/") |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1163 | targetpath = os.path.join('target', 'subdir', 'subsub') |
| 1164 | correctfile = os.path.join(targetpath, *fixedname.split('/')) |
| 1165 | |
| 1166 | with zipfile.ZipFile(TESTFN2, 'r') as zipfp: |
| 1167 | writtenfile = zipfp.extract(arcname, targetpath) |
Serhiy Storchaka | e5e6444 | 2013-02-02 19:50:59 +0200 | [diff] [blame] | 1168 | self.assertEqual(writtenfile, correctfile, |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1169 | msg='extract %r: %r != %r' % |
| 1170 | (arcname, writtenfile, correctfile)) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1171 | self.check_file(correctfile, content) |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 1172 | rmtree('target') |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1173 | |
| 1174 | with zipfile.ZipFile(TESTFN2, 'r') as zipfp: |
| 1175 | zipfp.extractall(targetpath) |
| 1176 | self.check_file(correctfile, content) |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 1177 | rmtree('target') |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1178 | |
| 1179 | correctfile = os.path.join(os.getcwd(), *fixedname.split('/')) |
| 1180 | |
| 1181 | with zipfile.ZipFile(TESTFN2, 'r') as zipfp: |
| 1182 | writtenfile = zipfp.extract(arcname) |
Serhiy Storchaka | e5e6444 | 2013-02-02 19:50:59 +0200 | [diff] [blame] | 1183 | self.assertEqual(writtenfile, correctfile, |
| 1184 | msg="extract %r" % arcname) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1185 | self.check_file(correctfile, content) |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 1186 | rmtree(fixedname.split('/')[0]) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1187 | |
| 1188 | with zipfile.ZipFile(TESTFN2, 'r') as zipfp: |
| 1189 | zipfp.extractall() |
| 1190 | self.check_file(correctfile, content) |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 1191 | rmtree(fixedname.split('/')[0]) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1192 | |
Victor Stinner | 88b215e | 2014-09-04 00:51:09 +0200 | [diff] [blame] | 1193 | unlink(TESTFN2) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1194 | |
Ronald Oussoren | ee5c885 | 2010-02-07 20:24:02 +0000 | [diff] [blame] | 1195 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1196 | class OtherTests(unittest.TestCase): |
| 1197 | def test_open_via_zip_info(self): |
| 1198 | # Create the ZIP archive |
| 1199 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 1200 | zipfp.writestr("name", "foo") |
Serhiy Storchaka | 9b7a1a1 | 2014-01-20 21:57:40 +0200 | [diff] [blame] | 1201 | with self.assertWarns(UserWarning): |
| 1202 | zipfp.writestr("name", "bar") |
| 1203 | self.assertEqual(zipfp.namelist(), ["name"] * 2) |
Ronald Oussoren | ee5c885 | 2010-02-07 20:24:02 +0000 | [diff] [blame] | 1204 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1205 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 1206 | infos = zipfp.infolist() |
| 1207 | data = b"" |
| 1208 | for info in infos: |
| 1209 | with zipfp.open(info) as zipopen: |
| 1210 | data += zipopen.read() |
| 1211 | self.assertIn(data, {b"foobar", b"barfoo"}) |
| 1212 | data = b"" |
| 1213 | for info in infos: |
| 1214 | data += zipfp.read(info) |
| 1215 | self.assertIn(data, {b"foobar", b"barfoo"}) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1216 | |
Gregory P. Smith | b0d9ca92 | 2009-07-07 05:06:04 +0000 | [diff] [blame] | 1217 | def test_writestr_extended_local_header_issue1202(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1218 | with zipfile.ZipFile(TESTFN2, 'w') as orig_zip: |
| 1219 | for data in 'abcdefghijklmnop': |
| 1220 | zinfo = zipfile.ZipInfo(data) |
| 1221 | zinfo.flag_bits |= 0x08 # Include an extended local header. |
| 1222 | orig_zip.writestr(zinfo, data) |
| 1223 | |
| 1224 | def test_close(self): |
| 1225 | """Check that the zipfile is closed after the 'with' block.""" |
| 1226 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 1227 | for fpath, fdata in SMALL_TEST_DATA: |
| 1228 | zipfp.writestr(fpath, fdata) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1229 | self.assertIsNotNone(zipfp.fp, 'zipfp is not open') |
| 1230 | self.assertIsNone(zipfp.fp, 'zipfp is not closed') |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1231 | |
| 1232 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1233 | self.assertIsNotNone(zipfp.fp, 'zipfp is not open') |
| 1234 | self.assertIsNone(zipfp.fp, 'zipfp is not closed') |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1235 | |
| 1236 | def test_close_on_exception(self): |
| 1237 | """Check that the zipfile is closed if an exception is raised in the |
| 1238 | 'with' block.""" |
| 1239 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 1240 | for fpath, fdata in SMALL_TEST_DATA: |
| 1241 | zipfp.writestr(fpath, fdata) |
| 1242 | |
| 1243 | try: |
| 1244 | with zipfile.ZipFile(TESTFN2, "r") as zipfp2: |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1245 | raise zipfile.BadZipFile() |
| 1246 | except zipfile.BadZipFile: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1247 | self.assertIsNone(zipfp2.fp, 'zipfp is not closed') |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1248 | |
Martin v. Löwis | d099b56 | 2012-05-01 14:08:22 +0200 | [diff] [blame] | 1249 | def test_unsupported_version(self): |
| 1250 | # File has an extract_version of 120 |
| 1251 | 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] | 1252 | b'\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00xPK\x01\x02x\x03x\x00\x00\x00\x00' |
| 1253 | b'\x00!p\xa1@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00' |
| 1254 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00\x00xPK\x05\x06' |
| 1255 | 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] | 1256 | |
Martin v. Löwis | d099b56 | 2012-05-01 14:08:22 +0200 | [diff] [blame] | 1257 | self.assertRaises(NotImplementedError, zipfile.ZipFile, |
| 1258 | io.BytesIO(data), 'r') |
| 1259 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1260 | @requires_zlib |
| 1261 | def test_read_unicode_filenames(self): |
| 1262 | # bug #10801 |
| 1263 | fname = findfile('zip_cp437_header.zip') |
| 1264 | with zipfile.ZipFile(fname) as zipfp: |
| 1265 | for name in zipfp.namelist(): |
| 1266 | zipfp.open(name).close() |
| 1267 | |
| 1268 | def test_write_unicode_filenames(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1269 | with zipfile.ZipFile(TESTFN, "w") as zf: |
| 1270 | zf.writestr("foo.txt", "Test for unicode filename") |
| 1271 | zf.writestr("\xf6.txt", "Test for unicode filename") |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 1272 | self.assertIsInstance(zf.infolist()[0].filename, str) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1273 | |
| 1274 | with zipfile.ZipFile(TESTFN, "r") as zf: |
| 1275 | self.assertEqual(zf.filelist[0].filename, "foo.txt") |
| 1276 | self.assertEqual(zf.filelist[1].filename, "\xf6.txt") |
Martin v. Löwis | 8570f6a | 2008-05-05 17:44:38 +0000 | [diff] [blame] | 1277 | |
Serhiy Storchaka | 764fc9b | 2015-03-25 10:09:41 +0200 | [diff] [blame] | 1278 | def test_exclusive_create_zip_file(self): |
| 1279 | """Test exclusive creating a new zipfile.""" |
| 1280 | unlink(TESTFN2) |
| 1281 | filename = 'testfile.txt' |
| 1282 | content = b'hello, world. this is some content.' |
| 1283 | with zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED) as zipfp: |
| 1284 | zipfp.writestr(filename, content) |
| 1285 | with self.assertRaises(FileExistsError): |
| 1286 | zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED) |
| 1287 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 1288 | self.assertEqual(zipfp.namelist(), [filename]) |
| 1289 | self.assertEqual(zipfp.read(filename), content) |
| 1290 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1291 | def test_create_non_existent_file_for_append(self): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1292 | if os.path.exists(TESTFN): |
| 1293 | os.unlink(TESTFN) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1294 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1295 | filename = 'testfile.txt' |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1296 | content = b'hello, world. this is some content.' |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1297 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1298 | try: |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1299 | with zipfile.ZipFile(TESTFN, 'a') as zf: |
| 1300 | zf.writestr(filename, content) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1301 | except OSError: |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1302 | self.fail('Could not append data to a non-existent zip file.') |
| 1303 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1304 | self.assertTrue(os.path.exists(TESTFN)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1305 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1306 | with zipfile.ZipFile(TESTFN, 'r') as zf: |
| 1307 | self.assertEqual(zf.read(filename), content) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1308 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1309 | def test_close_erroneous_file(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1310 | # This test checks that the ZipFile constructor closes the file object |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1311 | # it opens if there's an error in the file. If it doesn't, the |
| 1312 | # traceback holds a reference to the ZipFile object and, indirectly, |
| 1313 | # the file object. |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1314 | # On Windows, this causes the os.unlink() call to fail because the |
| 1315 | # underlying file is still open. This is SF bug #412214. |
| 1316 | # |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1317 | with open(TESTFN, "w") as fp: |
| 1318 | fp.write("this is not a legal zip file\n") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1319 | try: |
| 1320 | zf = zipfile.ZipFile(TESTFN) |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1321 | except zipfile.BadZipFile: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1322 | pass |
| 1323 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1324 | def test_is_zip_erroneous_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1325 | """Check that is_zipfile() correctly identifies non-zip files.""" |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 1326 | # - passing a filename |
| 1327 | with open(TESTFN, "w") as fp: |
| 1328 | fp.write("this is not a legal zip file\n") |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1329 | self.assertFalse(zipfile.is_zipfile(TESTFN)) |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 1330 | # - passing a path-like object |
| 1331 | self.assertFalse(zipfile.is_zipfile(pathlib.Path(TESTFN))) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 1332 | # - passing a file object |
| 1333 | with open(TESTFN, "rb") as fp: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1334 | self.assertFalse(zipfile.is_zipfile(fp)) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 1335 | # - passing a file-like object |
| 1336 | fp = io.BytesIO() |
| 1337 | fp.write(b"this is not a legal zip file\n") |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1338 | self.assertFalse(zipfile.is_zipfile(fp)) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1339 | fp.seek(0, 0) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1340 | self.assertFalse(zipfile.is_zipfile(fp)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1341 | |
Serhiy Storchaka | d2b1527 | 2013-01-31 15:27:07 +0200 | [diff] [blame] | 1342 | def test_damaged_zipfile(self): |
| 1343 | """Check that zipfiles with missing bytes at the end raise BadZipFile.""" |
| 1344 | # - Create a valid zip file |
| 1345 | fp = io.BytesIO() |
| 1346 | with zipfile.ZipFile(fp, mode="w") as zipf: |
| 1347 | zipf.writestr("foo.txt", b"O, for a Muse of Fire!") |
| 1348 | zipfiledata = fp.getvalue() |
| 1349 | |
| 1350 | # - Now create copies of it missing the last N bytes and make sure |
| 1351 | # a BadZipFile exception is raised when we try to open it |
| 1352 | for N in range(len(zipfiledata)): |
| 1353 | fp = io.BytesIO(zipfiledata[:N]) |
| 1354 | self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, fp) |
| 1355 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1356 | def test_is_zip_valid_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1357 | """Check that is_zipfile() correctly identifies zip files.""" |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 1358 | # - passing a filename |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1359 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1360 | zipf.writestr("foo.txt", b"O, for a Muse of Fire!") |
| 1361 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1362 | self.assertTrue(zipfile.is_zipfile(TESTFN)) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 1363 | # - passing a file object |
| 1364 | with open(TESTFN, "rb") as fp: |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1365 | self.assertTrue(zipfile.is_zipfile(fp)) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1366 | fp.seek(0, 0) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 1367 | zip_contents = fp.read() |
| 1368 | # - passing a file-like object |
| 1369 | fp = io.BytesIO() |
| 1370 | fp.write(zip_contents) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1371 | self.assertTrue(zipfile.is_zipfile(fp)) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1372 | fp.seek(0, 0) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1373 | self.assertTrue(zipfile.is_zipfile(fp)) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1374 | |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1375 | def test_non_existent_file_raises_OSError(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1376 | # make sure we don't raise an AttributeError when a partially-constructed |
| 1377 | # ZipFile instance is finalized; this tests for regression on SF tracker |
| 1378 | # bug #403871. |
| 1379 | |
| 1380 | # The bug we're testing for caused an AttributeError to be raised |
| 1381 | # when a ZipFile instance was created for a file that did not |
| 1382 | # exist; the .fp member was not initialized but was needed by the |
| 1383 | # __del__() method. Since the AttributeError is in the __del__(), |
| 1384 | # it is ignored, but the user should be sufficiently annoyed by |
| 1385 | # the message on the output that regression will be noticed |
| 1386 | # quickly. |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1387 | self.assertRaises(OSError, zipfile.ZipFile, TESTFN) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1388 | |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 1389 | def test_empty_file_raises_BadZipFile(self): |
| 1390 | f = open(TESTFN, 'w') |
| 1391 | f.close() |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1392 | self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN) |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 1393 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1394 | with open(TESTFN, 'w') as fp: |
| 1395 | fp.write("short file") |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1396 | self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN) |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 1397 | |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1398 | def test_closed_zip_raises_ValueError(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1399 | """Verify that testzip() doesn't swallow inappropriate exceptions.""" |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1400 | data = io.BytesIO() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1401 | with zipfile.ZipFile(data, mode="w") as zipf: |
| 1402 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1403 | |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 1404 | # This is correct; calling .read on a closed ZipFile should raise |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1405 | # a ValueError, and so should calling .testzip. An earlier |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1406 | # version of .testzip would swallow this exception (and any other) |
| 1407 | # and report that the first file in the archive was corrupt. |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1408 | self.assertRaises(ValueError, zipf.read, "foo.txt") |
| 1409 | self.assertRaises(ValueError, zipf.open, "foo.txt") |
| 1410 | self.assertRaises(ValueError, zipf.testzip) |
| 1411 | self.assertRaises(ValueError, zipf.writestr, "bogus.txt", "bogus") |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1412 | with open(TESTFN, 'w') as f: |
| 1413 | f.write('zipfile test data') |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1414 | self.assertRaises(ValueError, zipf.write, TESTFN) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1415 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1416 | def test_bad_constructor_mode(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1417 | """Check that bad modes passed to ZipFile constructor are caught.""" |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1418 | self.assertRaises(ValueError, zipfile.ZipFile, TESTFN, "q") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1419 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1420 | def test_bad_open_mode(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1421 | """Check that bad modes passed to ZipFile.open are caught.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1422 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1423 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1424 | |
| 1425 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
Serhiy Storchaka | e670be2 | 2016-06-11 19:32:44 +0300 | [diff] [blame] | 1426 | # read the data to make sure the file is there |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1427 | zipf.read("foo.txt") |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1428 | self.assertRaises(ValueError, zipf.open, "foo.txt", "q") |
Serhiy Storchaka | e670be2 | 2016-06-11 19:32:44 +0300 | [diff] [blame] | 1429 | # universal newlines support is removed |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1430 | self.assertRaises(ValueError, zipf.open, "foo.txt", "U") |
| 1431 | self.assertRaises(ValueError, zipf.open, "foo.txt", "rU") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1432 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1433 | def test_read0(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1434 | """Check that calling read(0) on a ZipExtFile object returns an empty |
| 1435 | string and doesn't advance file pointer.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1436 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1437 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1438 | # read the data to make sure the file is there |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1439 | with zipf.open("foo.txt") as f: |
| 1440 | for i in range(FIXEDTEST_SIZE): |
| 1441 | self.assertEqual(f.read(0), b'') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1442 | |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1443 | self.assertEqual(f.read(), b"O, for a Muse of Fire!") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1444 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1445 | def test_open_non_existent_item(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1446 | """Check that attempting to call open() for an item that doesn't |
| 1447 | exist in the archive raises a RuntimeError.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1448 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1449 | self.assertRaises(KeyError, zipf.open, "foo.txt", "r") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1450 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1451 | def test_bad_compression_mode(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1452 | """Check that bad compression methods passed to ZipFile.open are |
| 1453 | caught.""" |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1454 | self.assertRaises(NotImplementedError, zipfile.ZipFile, TESTFN, "w", -1) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1455 | |
Martin v. Löwis | b3260f0 | 2012-05-01 08:38:01 +0200 | [diff] [blame] | 1456 | def test_unsupported_compression(self): |
| 1457 | # data is declared as shrunk, but actually deflated |
| 1458 | 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] | 1459 | b'\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00x\x03\x00PK\x01' |
| 1460 | b'\x02.\x03.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00\x00\x02\x00\x00' |
| 1461 | b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 1462 | b'\x80\x01\x00\x00\x00\x00xPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00' |
| 1463 | b'/\x00\x00\x00!\x00\x00\x00\x00\x00') |
Martin v. Löwis | b3260f0 | 2012-05-01 08:38:01 +0200 | [diff] [blame] | 1464 | with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf: |
| 1465 | self.assertRaises(NotImplementedError, zipf.open, 'x') |
| 1466 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1467 | def test_null_byte_in_filename(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1468 | """Check that a filename containing a null byte is properly |
| 1469 | terminated.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1470 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1471 | zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!") |
| 1472 | self.assertEqual(zipf.namelist(), ['foo.txt']) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1473 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1474 | def test_struct_sizes(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1475 | """Check that ZIP internal structure sizes are calculated correctly.""" |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1476 | self.assertEqual(zipfile.sizeEndCentDir, 22) |
| 1477 | self.assertEqual(zipfile.sizeCentralDir, 46) |
| 1478 | self.assertEqual(zipfile.sizeEndCentDir64, 56) |
| 1479 | self.assertEqual(zipfile.sizeEndCentDir64Locator, 20) |
| 1480 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1481 | def test_comments(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1482 | """Check that comments on the archive are handled properly.""" |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1483 | |
| 1484 | # check default comment is empty |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1485 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1486 | self.assertEqual(zipf.comment, b'') |
| 1487 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1488 | |
| 1489 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 1490 | self.assertEqual(zipfr.comment, b'') |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1491 | |
| 1492 | # check a simple short comment |
| 1493 | 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] | 1494 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1495 | zipf.comment = comment |
| 1496 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1497 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 1498 | self.assertEqual(zipf.comment, comment) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1499 | |
| 1500 | # check a comment of max length |
| 1501 | comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)]) |
| 1502 | comment2 = comment2.encode("ascii") |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1503 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1504 | zipf.comment = comment2 |
| 1505 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1506 | |
| 1507 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 1508 | self.assertEqual(zipfr.comment, comment2) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1509 | |
| 1510 | # check a comment that is too long is truncated |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1511 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
Serhiy Storchaka | 9b7a1a1 | 2014-01-20 21:57:40 +0200 | [diff] [blame] | 1512 | with self.assertWarns(UserWarning): |
| 1513 | zipf.comment = comment2 + b'oops' |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1514 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1515 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 1516 | self.assertEqual(zipfr.comment, comment2) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1517 | |
Antoine Pitrou | c399185 | 2012-06-30 17:31:37 +0200 | [diff] [blame] | 1518 | # check that comments are correctly modified in append mode |
| 1519 | with zipfile.ZipFile(TESTFN,mode="w") as zipf: |
| 1520 | zipf.comment = b"original comment" |
| 1521 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1522 | with zipfile.ZipFile(TESTFN,mode="a") as zipf: |
| 1523 | zipf.comment = b"an updated comment" |
| 1524 | with zipfile.ZipFile(TESTFN,mode="r") as zipf: |
| 1525 | self.assertEqual(zipf.comment, b"an updated comment") |
| 1526 | |
| 1527 | # check that comments are correctly shortened in append mode |
| 1528 | with zipfile.ZipFile(TESTFN,mode="w") as zipf: |
| 1529 | zipf.comment = b"original comment that's longer" |
| 1530 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1531 | with zipfile.ZipFile(TESTFN,mode="a") as zipf: |
| 1532 | zipf.comment = b"shorter comment" |
| 1533 | with zipfile.ZipFile(TESTFN,mode="r") as zipf: |
| 1534 | self.assertEqual(zipf.comment, b"shorter comment") |
| 1535 | |
R David Murray | f50b38a | 2012-04-12 18:44:58 -0400 | [diff] [blame] | 1536 | def test_unicode_comment(self): |
| 1537 | with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf: |
| 1538 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1539 | with self.assertRaises(TypeError): |
| 1540 | zipf.comment = "this is an error" |
| 1541 | |
| 1542 | def test_change_comment_in_empty_archive(self): |
| 1543 | with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf: |
| 1544 | self.assertFalse(zipf.filelist) |
| 1545 | zipf.comment = b"this is a comment" |
| 1546 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 1547 | self.assertEqual(zipf.comment, b"this is a comment") |
| 1548 | |
| 1549 | def test_change_comment_in_nonempty_archive(self): |
| 1550 | with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf: |
| 1551 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1552 | with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf: |
| 1553 | self.assertTrue(zipf.filelist) |
| 1554 | zipf.comment = b"this is a comment" |
| 1555 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 1556 | self.assertEqual(zipf.comment, b"this is a comment") |
| 1557 | |
Georg Brandl | 268e4d4 | 2010-10-14 06:59:45 +0000 | [diff] [blame] | 1558 | def test_empty_zipfile(self): |
| 1559 | # Check that creating a file in 'w' or 'a' mode and closing without |
| 1560 | # adding any files to the archives creates a valid empty ZIP file |
| 1561 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 1562 | zipf.close() |
| 1563 | try: |
| 1564 | zipf = zipfile.ZipFile(TESTFN, mode="r") |
| 1565 | except zipfile.BadZipFile: |
| 1566 | self.fail("Unable to create empty ZIP file in 'w' mode") |
| 1567 | |
| 1568 | zipf = zipfile.ZipFile(TESTFN, mode="a") |
| 1569 | zipf.close() |
| 1570 | try: |
| 1571 | zipf = zipfile.ZipFile(TESTFN, mode="r") |
| 1572 | except: |
| 1573 | self.fail("Unable to create empty ZIP file in 'a' mode") |
| 1574 | |
| 1575 | def test_open_empty_file(self): |
| 1576 | # Issue 1710703: Check that opening a file with less than 22 bytes |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1577 | # raises a BadZipFile exception (rather than the previously unhelpful |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1578 | # OSError) |
Georg Brandl | 268e4d4 | 2010-10-14 06:59:45 +0000 | [diff] [blame] | 1579 | f = open(TESTFN, 'w') |
| 1580 | f.close() |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1581 | self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r') |
Georg Brandl | 268e4d4 | 2010-10-14 06:59:45 +0000 | [diff] [blame] | 1582 | |
Senthil Kumaran | 29fa9d4 | 2011-10-20 01:46:00 +0800 | [diff] [blame] | 1583 | def test_create_zipinfo_before_1980(self): |
| 1584 | self.assertRaises(ValueError, |
| 1585 | zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0)) |
| 1586 | |
Gregory P. Smith | 0af8a86 | 2014-05-29 23:42:14 -0700 | [diff] [blame] | 1587 | def test_zipfile_with_short_extra_field(self): |
| 1588 | """If an extra field in the header is less than 4 bytes, skip it.""" |
| 1589 | zipdata = ( |
| 1590 | b'PK\x03\x04\x14\x00\x00\x00\x00\x00\x93\x9b\xad@\x8b\x9e' |
| 1591 | b'\xd9\xd3\x01\x00\x00\x00\x01\x00\x00\x00\x03\x00\x03\x00ab' |
| 1592 | b'c\x00\x00\x00APK\x01\x02\x14\x03\x14\x00\x00\x00\x00' |
| 1593 | b'\x00\x93\x9b\xad@\x8b\x9e\xd9\xd3\x01\x00\x00\x00\x01\x00\x00' |
| 1594 | b'\x00\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00' |
| 1595 | b'\x00\x00\x00abc\x00\x00PK\x05\x06\x00\x00\x00\x00' |
| 1596 | b'\x01\x00\x01\x003\x00\x00\x00%\x00\x00\x00\x00\x00' |
| 1597 | ) |
| 1598 | with zipfile.ZipFile(io.BytesIO(zipdata), 'r') as zipf: |
| 1599 | # testzip returns the name of the first corrupt file, or None |
| 1600 | self.assertIsNone(zipf.testzip()) |
| 1601 | |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 1602 | def test_open_conflicting_handles(self): |
| 1603 | # It's only possible to open one writable file handle at a time |
| 1604 | msg1 = b"It's fun to charter an accountant!" |
| 1605 | msg2 = b"And sail the wide accountant sea" |
| 1606 | msg3 = b"To find, explore the funds offshore" |
| 1607 | with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipf: |
| 1608 | with zipf.open('foo', mode='w') as w2: |
| 1609 | w2.write(msg1) |
| 1610 | with zipf.open('bar', mode='w') as w1: |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1611 | with self.assertRaises(ValueError): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 1612 | zipf.open('handle', mode='w') |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1613 | with self.assertRaises(ValueError): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 1614 | zipf.open('foo', mode='r') |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1615 | with self.assertRaises(ValueError): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 1616 | zipf.writestr('str', 'abcde') |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1617 | with self.assertRaises(ValueError): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 1618 | zipf.write(__file__, 'file') |
Serhiy Storchaka | b0d497c | 2016-09-10 21:28:07 +0300 | [diff] [blame] | 1619 | with self.assertRaises(ValueError): |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 1620 | zipf.close() |
| 1621 | w1.write(msg2) |
| 1622 | with zipf.open('baz', mode='w') as w2: |
| 1623 | w2.write(msg3) |
| 1624 | |
| 1625 | with zipfile.ZipFile(TESTFN2, 'r') as zipf: |
| 1626 | self.assertEqual(zipf.read('foo'), msg1) |
| 1627 | self.assertEqual(zipf.read('bar'), msg2) |
| 1628 | self.assertEqual(zipf.read('baz'), msg3) |
| 1629 | self.assertEqual(zipf.namelist(), ['foo', 'bar', 'baz']) |
| 1630 | |
John Jolly | 066df4f | 2018-01-30 01:51:35 -0700 | [diff] [blame] | 1631 | def test_seek_tell(self): |
| 1632 | # Test seek functionality |
| 1633 | txt = b"Where's Bruce?" |
| 1634 | bloc = txt.find(b"Bruce") |
| 1635 | # Check seek on a file |
| 1636 | with zipfile.ZipFile(TESTFN, "w") as zipf: |
| 1637 | zipf.writestr("foo.txt", txt) |
| 1638 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 1639 | with zipf.open("foo.txt", "r") as fp: |
| 1640 | fp.seek(bloc, os.SEEK_SET) |
| 1641 | self.assertEqual(fp.tell(), bloc) |
| 1642 | fp.seek(-bloc, os.SEEK_CUR) |
| 1643 | self.assertEqual(fp.tell(), 0) |
| 1644 | fp.seek(bloc, os.SEEK_CUR) |
| 1645 | self.assertEqual(fp.tell(), bloc) |
| 1646 | self.assertEqual(fp.read(5), txt[bloc:bloc+5]) |
| 1647 | fp.seek(0, os.SEEK_END) |
| 1648 | self.assertEqual(fp.tell(), len(txt)) |
| 1649 | # Check seek on memory file |
| 1650 | data = io.BytesIO() |
| 1651 | with zipfile.ZipFile(data, mode="w") as zipf: |
| 1652 | zipf.writestr("foo.txt", txt) |
| 1653 | with zipfile.ZipFile(data, mode="r") as zipf: |
| 1654 | with zipf.open("foo.txt", "r") as fp: |
| 1655 | fp.seek(bloc, os.SEEK_SET) |
| 1656 | self.assertEqual(fp.tell(), bloc) |
| 1657 | fp.seek(-bloc, os.SEEK_CUR) |
| 1658 | self.assertEqual(fp.tell(), 0) |
| 1659 | fp.seek(bloc, os.SEEK_CUR) |
| 1660 | self.assertEqual(fp.tell(), bloc) |
| 1661 | self.assertEqual(fp.read(5), txt[bloc:bloc+5]) |
| 1662 | fp.seek(0, os.SEEK_END) |
| 1663 | self.assertEqual(fp.tell(), len(txt)) |
| 1664 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1665 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1666 | unlink(TESTFN) |
| 1667 | unlink(TESTFN2) |
| 1668 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1669 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1670 | class AbstractBadCrcTests: |
| 1671 | def test_testzip_with_bad_crc(self): |
| 1672 | """Tests that files with bad CRCs return their name from testzip.""" |
| 1673 | zipdata = self.zip_with_bad_crc |
| 1674 | |
| 1675 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 1676 | # testzip returns the name of the first corrupt file, or None |
| 1677 | self.assertEqual('afile', zipf.testzip()) |
| 1678 | |
| 1679 | def test_read_with_bad_crc(self): |
| 1680 | """Tests that files with bad CRCs raise a BadZipFile exception when read.""" |
| 1681 | zipdata = self.zip_with_bad_crc |
| 1682 | |
| 1683 | # Using ZipFile.read() |
| 1684 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 1685 | self.assertRaises(zipfile.BadZipFile, zipf.read, 'afile') |
| 1686 | |
| 1687 | # Using ZipExtFile.read() |
| 1688 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 1689 | with zipf.open('afile', 'r') as corrupt_file: |
| 1690 | self.assertRaises(zipfile.BadZipFile, corrupt_file.read) |
| 1691 | |
| 1692 | # Same with small reads (in order to exercise the buffering logic) |
| 1693 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 1694 | with zipf.open('afile', 'r') as corrupt_file: |
| 1695 | corrupt_file.MIN_READ_SIZE = 2 |
| 1696 | with self.assertRaises(zipfile.BadZipFile): |
| 1697 | while corrupt_file.read(2): |
| 1698 | pass |
| 1699 | |
| 1700 | |
| 1701 | class StoredBadCrcTests(AbstractBadCrcTests, unittest.TestCase): |
| 1702 | compression = zipfile.ZIP_STORED |
| 1703 | zip_with_bad_crc = ( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1704 | b'PK\003\004\024\0\0\0\0\0 \213\212;:r' |
| 1705 | b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af' |
| 1706 | b'ilehello,AworldP' |
| 1707 | b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:' |
| 1708 | b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0' |
| 1709 | b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi' |
| 1710 | b'lePK\005\006\0\0\0\0\001\0\001\0003\000' |
| 1711 | b'\0\0/\0\0\0\0\0') |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1712 | |
| 1713 | @requires_zlib |
| 1714 | class DeflateBadCrcTests(AbstractBadCrcTests, unittest.TestCase): |
| 1715 | compression = zipfile.ZIP_DEFLATED |
| 1716 | zip_with_bad_crc = ( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1717 | b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA' |
| 1718 | b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' |
| 1719 | b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0' |
| 1720 | b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n' |
| 1721 | b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05' |
| 1722 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00' |
| 1723 | b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00' |
| 1724 | b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00') |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1725 | |
| 1726 | @requires_bz2 |
| 1727 | class Bzip2BadCrcTests(AbstractBadCrcTests, unittest.TestCase): |
| 1728 | compression = zipfile.ZIP_BZIP2 |
| 1729 | zip_with_bad_crc = ( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1730 | b'PK\x03\x04\x14\x03\x00\x00\x0c\x00nu\x0c=FA' |
| 1731 | b'KE8\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' |
| 1732 | b'ileBZh91AY&SY\xd4\xa8\xca' |
| 1733 | b'\x7f\x00\x00\x0f\x11\x80@\x00\x06D\x90\x80 \x00 \xa5' |
| 1734 | b'P\xd9!\x03\x03\x13\x13\x13\x89\xa9\xa9\xc2u5:\x9f' |
| 1735 | b'\x8b\xb9"\x9c(HjTe?\x80PK\x01\x02\x14' |
| 1736 | b'\x03\x14\x03\x00\x00\x0c\x00nu\x0c=FAKE8' |
| 1737 | b'\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00' |
| 1738 | b'\x00 \x80\x80\x81\x00\x00\x00\x00afilePK' |
| 1739 | b'\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00\x00[\x00' |
| 1740 | b'\x00\x00\x00\x00') |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1741 | |
| 1742 | @requires_lzma |
| 1743 | class LzmaBadCrcTests(AbstractBadCrcTests, unittest.TestCase): |
| 1744 | compression = zipfile.ZIP_LZMA |
| 1745 | zip_with_bad_crc = ( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1746 | b'PK\x03\x04\x14\x03\x00\x00\x0e\x00nu\x0c=FA' |
| 1747 | b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' |
| 1748 | b'ile\t\x04\x05\x00]\x00\x00\x00\x04\x004\x19I' |
| 1749 | b'\xee\x8d\xe9\x17\x89:3`\tq!.8\x00PK' |
| 1750 | b'\x01\x02\x14\x03\x14\x03\x00\x00\x0e\x00nu\x0c=FA' |
| 1751 | b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00' |
| 1752 | b'\x00\x00\x00\x00 \x80\x80\x81\x00\x00\x00\x00afil' |
| 1753 | b'ePK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00' |
| 1754 | b'\x00>\x00\x00\x00\x00\x00') |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1755 | |
| 1756 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1757 | class DecryptionTests(unittest.TestCase): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1758 | """Check that ZIP decryption works. Since the library does not |
| 1759 | support encryption at the moment, we use a pre-generated encrypted |
| 1760 | ZIP file.""" |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1761 | |
| 1762 | data = ( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1763 | b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00' |
| 1764 | b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y' |
| 1765 | b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl' |
| 1766 | b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00' |
| 1767 | b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81' |
| 1768 | b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00' |
| 1769 | b'\x00\x00L\x00\x00\x00\x00\x00' ) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1770 | data2 = ( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1771 | b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02' |
| 1772 | b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04' |
| 1773 | b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0' |
| 1774 | b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03' |
| 1775 | b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00' |
| 1776 | b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze' |
| 1777 | b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01' |
| 1778 | b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' ) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1779 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1780 | plain = b'zipfile.py encryption test' |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1781 | plain2 = b'\x00'*512 |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1782 | |
| 1783 | def setUp(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1784 | with open(TESTFN, "wb") as fp: |
| 1785 | fp.write(self.data) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1786 | self.zip = zipfile.ZipFile(TESTFN, "r") |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1787 | with open(TESTFN2, "wb") as fp: |
| 1788 | fp.write(self.data2) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1789 | self.zip2 = zipfile.ZipFile(TESTFN2, "r") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1790 | |
| 1791 | def tearDown(self): |
| 1792 | self.zip.close() |
| 1793 | os.unlink(TESTFN) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1794 | self.zip2.close() |
| 1795 | os.unlink(TESTFN2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1796 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1797 | def test_no_password(self): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1798 | # Reading the encrypted file without password |
| 1799 | # must generate a RunTime exception |
| 1800 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1801 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1802 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1803 | def test_bad_password(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1804 | self.zip.setpassword(b"perl") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1805 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1806 | self.zip2.setpassword(b"perl") |
| 1807 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1808 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1809 | @requires_zlib |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1810 | def test_good_password(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1811 | self.zip.setpassword(b"python") |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1812 | self.assertEqual(self.zip.read("test.txt"), self.plain) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1813 | self.zip2.setpassword(b"12345") |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1814 | self.assertEqual(self.zip2.read("zero"), self.plain2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1815 | |
R. David Murray | 8d855d8 | 2010-12-21 21:53:37 +0000 | [diff] [blame] | 1816 | def test_unicode_password(self): |
| 1817 | self.assertRaises(TypeError, self.zip.setpassword, "unicode") |
| 1818 | self.assertRaises(TypeError, self.zip.read, "test.txt", "python") |
| 1819 | self.assertRaises(TypeError, self.zip.open, "test.txt", pwd="python") |
| 1820 | self.assertRaises(TypeError, self.zip.extract, "test.txt", pwd="python") |
| 1821 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1822 | class AbstractTestsWithRandomBinaryFiles: |
| 1823 | @classmethod |
| 1824 | def setUpClass(cls): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1825 | datacount = randint(16, 64)*1024 + randint(1, 1024) |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1826 | cls.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000)) |
| 1827 | for i in range(datacount)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1828 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1829 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1830 | # Make a source file with some lines |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1831 | with open(TESTFN, "wb") as fp: |
| 1832 | fp.write(self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1833 | |
| 1834 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1835 | unlink(TESTFN) |
| 1836 | unlink(TESTFN2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1837 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1838 | def make_test_archive(self, f, compression): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1839 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1840 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 1841 | zipfp.write(TESTFN, "another.name") |
| 1842 | zipfp.write(TESTFN, TESTFN) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1843 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1844 | def zip_test(self, f, compression): |
| 1845 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1846 | |
| 1847 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1848 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1849 | testdata = zipfp.read(TESTFN) |
| 1850 | self.assertEqual(len(testdata), len(self.data)) |
| 1851 | self.assertEqual(testdata, self.data) |
| 1852 | self.assertEqual(zipfp.read("another.name"), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1853 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1854 | def test_read(self): |
| 1855 | for f in get_files(self): |
| 1856 | self.zip_test(f, self.compression) |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 1857 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1858 | def zip_open_test(self, f, compression): |
| 1859 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1860 | |
| 1861 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1862 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1863 | zipdata1 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1864 | with zipfp.open(TESTFN) as zipopen1: |
| 1865 | while True: |
| 1866 | read_data = zipopen1.read(256) |
| 1867 | if not read_data: |
| 1868 | break |
| 1869 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1870 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1871 | zipdata2 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1872 | with zipfp.open("another.name") as zipopen2: |
| 1873 | while True: |
| 1874 | read_data = zipopen2.read(256) |
| 1875 | if not read_data: |
| 1876 | break |
| 1877 | zipdata2.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1878 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1879 | testdata1 = b''.join(zipdata1) |
| 1880 | self.assertEqual(len(testdata1), len(self.data)) |
| 1881 | self.assertEqual(testdata1, self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1882 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1883 | testdata2 = b''.join(zipdata2) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1884 | self.assertEqual(len(testdata2), len(self.data)) |
| 1885 | self.assertEqual(testdata2, self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1886 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1887 | def test_open(self): |
| 1888 | for f in get_files(self): |
| 1889 | self.zip_open_test(f, self.compression) |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 1890 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1891 | def zip_random_open_test(self, f, compression): |
| 1892 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1893 | |
| 1894 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1895 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1896 | zipdata1 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1897 | with zipfp.open(TESTFN) as zipopen1: |
| 1898 | while True: |
| 1899 | read_data = zipopen1.read(randint(1, 1024)) |
| 1900 | if not read_data: |
| 1901 | break |
| 1902 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1903 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1904 | testdata = b''.join(zipdata1) |
| 1905 | self.assertEqual(len(testdata), len(self.data)) |
| 1906 | self.assertEqual(testdata, self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1907 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1908 | def test_random_open(self): |
| 1909 | for f in get_files(self): |
| 1910 | self.zip_random_open_test(f, self.compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1911 | |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1912 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1913 | class StoredTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles, |
| 1914 | unittest.TestCase): |
| 1915 | compression = zipfile.ZIP_STORED |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1916 | |
Serhiy Storchaka | fa6bc29 | 2013-07-22 21:00:11 +0300 | [diff] [blame] | 1917 | @requires_zlib |
| 1918 | class DeflateTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles, |
| 1919 | unittest.TestCase): |
| 1920 | compression = zipfile.ZIP_DEFLATED |
| 1921 | |
| 1922 | @requires_bz2 |
| 1923 | class Bzip2TestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles, |
| 1924 | unittest.TestCase): |
| 1925 | compression = zipfile.ZIP_BZIP2 |
| 1926 | |
| 1927 | @requires_lzma |
| 1928 | class LzmaTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles, |
| 1929 | unittest.TestCase): |
| 1930 | compression = zipfile.ZIP_LZMA |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 1931 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1932 | |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 1933 | # Provide the tell() method but not seek() |
Serhiy Storchaka | a14f7d2 | 2015-01-26 14:01:27 +0200 | [diff] [blame] | 1934 | class Tellable: |
| 1935 | def __init__(self, fp): |
| 1936 | self.fp = fp |
| 1937 | self.offset = 0 |
| 1938 | |
| 1939 | def write(self, data): |
Serhiy Storchaka | 77d8997 | 2015-03-23 01:09:35 +0200 | [diff] [blame] | 1940 | n = self.fp.write(data) |
| 1941 | self.offset += n |
| 1942 | return n |
Serhiy Storchaka | a14f7d2 | 2015-01-26 14:01:27 +0200 | [diff] [blame] | 1943 | |
| 1944 | def tell(self): |
| 1945 | return self.offset |
| 1946 | |
| 1947 | def flush(self): |
Serhiy Storchaka | 77d8997 | 2015-03-23 01:09:35 +0200 | [diff] [blame] | 1948 | self.fp.flush() |
| 1949 | |
| 1950 | class Unseekable: |
| 1951 | def __init__(self, fp): |
| 1952 | self.fp = fp |
| 1953 | |
| 1954 | def write(self, data): |
| 1955 | return self.fp.write(data) |
| 1956 | |
| 1957 | def flush(self): |
| 1958 | self.fp.flush() |
Serhiy Storchaka | a14f7d2 | 2015-01-26 14:01:27 +0200 | [diff] [blame] | 1959 | |
| 1960 | class UnseekableTests(unittest.TestCase): |
Serhiy Storchaka | 77d8997 | 2015-03-23 01:09:35 +0200 | [diff] [blame] | 1961 | def test_writestr(self): |
| 1962 | for wrapper in (lambda f: f), Tellable, Unseekable: |
| 1963 | with self.subTest(wrapper=wrapper): |
| 1964 | f = io.BytesIO() |
| 1965 | f.write(b'abc') |
| 1966 | bf = io.BufferedWriter(f) |
| 1967 | with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipfp: |
| 1968 | zipfp.writestr('ones', b'111') |
| 1969 | zipfp.writestr('twos', b'222') |
| 1970 | self.assertEqual(f.getvalue()[:5], b'abcPK') |
| 1971 | with zipfile.ZipFile(f, mode='r') as zipf: |
| 1972 | with zipf.open('ones') as zopen: |
| 1973 | self.assertEqual(zopen.read(), b'111') |
| 1974 | with zipf.open('twos') as zopen: |
| 1975 | self.assertEqual(zopen.read(), b'222') |
| 1976 | |
| 1977 | def test_write(self): |
| 1978 | for wrapper in (lambda f: f), Tellable, Unseekable: |
| 1979 | with self.subTest(wrapper=wrapper): |
| 1980 | f = io.BytesIO() |
| 1981 | f.write(b'abc') |
| 1982 | bf = io.BufferedWriter(f) |
| 1983 | with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipfp: |
| 1984 | self.addCleanup(unlink, TESTFN) |
| 1985 | with open(TESTFN, 'wb') as f2: |
| 1986 | f2.write(b'111') |
| 1987 | zipfp.write(TESTFN, 'ones') |
| 1988 | with open(TESTFN, 'wb') as f2: |
| 1989 | f2.write(b'222') |
| 1990 | zipfp.write(TESTFN, 'twos') |
| 1991 | self.assertEqual(f.getvalue()[:5], b'abcPK') |
| 1992 | with zipfile.ZipFile(f, mode='r') as zipf: |
| 1993 | with zipf.open('ones') as zopen: |
| 1994 | self.assertEqual(zopen.read(), b'111') |
| 1995 | with zipf.open('twos') as zopen: |
| 1996 | self.assertEqual(zopen.read(), b'222') |
Serhiy Storchaka | a14f7d2 | 2015-01-26 14:01:27 +0200 | [diff] [blame] | 1997 | |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 1998 | def test_open_write(self): |
| 1999 | for wrapper in (lambda f: f), Tellable, Unseekable: |
| 2000 | with self.subTest(wrapper=wrapper): |
| 2001 | f = io.BytesIO() |
| 2002 | f.write(b'abc') |
| 2003 | bf = io.BufferedWriter(f) |
| 2004 | with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipf: |
| 2005 | with zipf.open('ones', 'w') as zopen: |
| 2006 | zopen.write(b'111') |
| 2007 | with zipf.open('twos', 'w') as zopen: |
| 2008 | zopen.write(b'222') |
| 2009 | self.assertEqual(f.getvalue()[:5], b'abcPK') |
| 2010 | with zipfile.ZipFile(f) as zipf: |
| 2011 | self.assertEqual(zipf.read('ones'), b'111') |
| 2012 | self.assertEqual(zipf.read('twos'), b'222') |
| 2013 | |
Serhiy Storchaka | a14f7d2 | 2015-01-26 14:01:27 +0200 | [diff] [blame] | 2014 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 2015 | @requires_zlib |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2016 | class TestsWithMultipleOpens(unittest.TestCase): |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2017 | @classmethod |
| 2018 | def setUpClass(cls): |
| 2019 | cls.data1 = b'111' + getrandbytes(10000) |
| 2020 | cls.data2 = b'222' + getrandbytes(10000) |
| 2021 | |
| 2022 | def make_test_archive(self, f): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2023 | # Create the ZIP archive |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2024 | with zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED) as zipfp: |
| 2025 | zipfp.writestr('ones', self.data1) |
| 2026 | zipfp.writestr('twos', self.data2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2027 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2028 | def test_same_file(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2029 | # Verify that (when the ZipFile is in control of creating file objects) |
| 2030 | # multiple open() calls can be made without interfering with each other. |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2031 | for f in get_files(self): |
| 2032 | self.make_test_archive(f) |
| 2033 | with zipfile.ZipFile(f, mode="r") as zipf: |
| 2034 | with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2: |
| 2035 | data1 = zopen1.read(500) |
| 2036 | data2 = zopen2.read(500) |
| 2037 | data1 += zopen1.read() |
| 2038 | data2 += zopen2.read() |
| 2039 | self.assertEqual(data1, data2) |
| 2040 | self.assertEqual(data1, self.data1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2041 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2042 | def test_different_file(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2043 | # Verify that (when the ZipFile is in control of creating file objects) |
| 2044 | # multiple open() calls can be made without interfering with each other. |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2045 | for f in get_files(self): |
| 2046 | self.make_test_archive(f) |
| 2047 | with zipfile.ZipFile(f, mode="r") as zipf: |
| 2048 | with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2: |
| 2049 | data1 = zopen1.read(500) |
| 2050 | data2 = zopen2.read(500) |
| 2051 | data1 += zopen1.read() |
| 2052 | data2 += zopen2.read() |
| 2053 | self.assertEqual(data1, self.data1) |
| 2054 | self.assertEqual(data2, self.data2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2055 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2056 | def test_interleaved(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2057 | # Verify that (when the ZipFile is in control of creating file objects) |
| 2058 | # multiple open() calls can be made without interfering with each other. |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2059 | for f in get_files(self): |
| 2060 | self.make_test_archive(f) |
| 2061 | with zipfile.ZipFile(f, mode="r") as zipf: |
Serhiy Storchaka | d76c7c2 | 2016-05-13 21:18:58 +0300 | [diff] [blame] | 2062 | with zipf.open('ones') as zopen1: |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2063 | data1 = zopen1.read(500) |
Serhiy Storchaka | d76c7c2 | 2016-05-13 21:18:58 +0300 | [diff] [blame] | 2064 | with zipf.open('twos') as zopen2: |
| 2065 | data2 = zopen2.read(500) |
| 2066 | data1 += zopen1.read() |
| 2067 | data2 += zopen2.read() |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2068 | self.assertEqual(data1, self.data1) |
| 2069 | self.assertEqual(data2, self.data2) |
| 2070 | |
| 2071 | def test_read_after_close(self): |
| 2072 | for f in get_files(self): |
| 2073 | self.make_test_archive(f) |
| 2074 | with contextlib.ExitStack() as stack: |
| 2075 | with zipfile.ZipFile(f, 'r') as zipf: |
| 2076 | zopen1 = stack.enter_context(zipf.open('ones')) |
| 2077 | zopen2 = stack.enter_context(zipf.open('twos')) |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 2078 | data1 = zopen1.read(500) |
| 2079 | data2 = zopen2.read(500) |
Serhiy Storchaka | 1ad088f | 2014-12-03 09:11:57 +0200 | [diff] [blame] | 2080 | data1 += zopen1.read() |
| 2081 | data2 += zopen2.read() |
| 2082 | self.assertEqual(data1, self.data1) |
| 2083 | self.assertEqual(data2, self.data2) |
| 2084 | |
| 2085 | def test_read_after_write(self): |
| 2086 | for f in get_files(self): |
| 2087 | with zipfile.ZipFile(f, 'w', zipfile.ZIP_DEFLATED) as zipf: |
| 2088 | zipf.writestr('ones', self.data1) |
| 2089 | zipf.writestr('twos', self.data2) |
| 2090 | with zipf.open('ones') as zopen1: |
| 2091 | data1 = zopen1.read(500) |
| 2092 | self.assertEqual(data1, self.data1[:500]) |
| 2093 | with zipfile.ZipFile(f, 'r') as zipf: |
| 2094 | data1 = zipf.read('ones') |
| 2095 | data2 = zipf.read('twos') |
| 2096 | self.assertEqual(data1, self.data1) |
| 2097 | self.assertEqual(data2, self.data2) |
| 2098 | |
| 2099 | def test_write_after_read(self): |
| 2100 | for f in get_files(self): |
| 2101 | with zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED) as zipf: |
| 2102 | zipf.writestr('ones', self.data1) |
| 2103 | with zipf.open('ones') as zopen1: |
| 2104 | zopen1.read(500) |
| 2105 | zipf.writestr('twos', self.data2) |
| 2106 | with zipfile.ZipFile(f, 'r') as zipf: |
| 2107 | data1 = zipf.read('ones') |
| 2108 | data2 = zipf.read('twos') |
| 2109 | self.assertEqual(data1, self.data1) |
| 2110 | self.assertEqual(data2, self.data2) |
| 2111 | |
| 2112 | def test_many_opens(self): |
| 2113 | # Verify that read() and open() promptly close the file descriptor, |
| 2114 | # and don't rely on the garbage collector to free resources. |
| 2115 | self.make_test_archive(TESTFN2) |
| 2116 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 2117 | for x in range(100): |
| 2118 | zipf.read('ones') |
| 2119 | with zipf.open('ones') as zopen1: |
| 2120 | pass |
| 2121 | with open(os.devnull) as f: |
| 2122 | self.assertLess(f.fileno(), 100) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2123 | |
Serhiy Storchaka | 18ee29d | 2016-05-13 13:52:49 +0300 | [diff] [blame] | 2124 | def test_write_while_reading(self): |
| 2125 | with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_DEFLATED) as zipf: |
| 2126 | zipf.writestr('ones', self.data1) |
| 2127 | with zipfile.ZipFile(TESTFN2, 'a', zipfile.ZIP_DEFLATED) as zipf: |
| 2128 | with zipf.open('ones', 'r') as r1: |
| 2129 | data1 = r1.read(500) |
| 2130 | with zipf.open('twos', 'w') as w1: |
| 2131 | w1.write(self.data2) |
| 2132 | data1 += r1.read() |
| 2133 | self.assertEqual(data1, self.data1) |
| 2134 | with zipfile.ZipFile(TESTFN2) as zipf: |
| 2135 | self.assertEqual(zipf.read('twos'), self.data2) |
| 2136 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2137 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 2138 | unlink(TESTFN2) |
| 2139 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2140 | |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 2141 | class TestWithDirectory(unittest.TestCase): |
| 2142 | def setUp(self): |
| 2143 | os.mkdir(TESTFN2) |
| 2144 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2145 | def test_extract_dir(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 2146 | with zipfile.ZipFile(findfile("zipdir.zip")) as zipf: |
| 2147 | zipf.extractall(TESTFN2) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 2148 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a"))) |
| 2149 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b"))) |
| 2150 | self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c"))) |
| 2151 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2152 | def test_bug_6050(self): |
Martin v. Löwis | 70ccd16 | 2009-05-24 19:47:22 +0000 | [diff] [blame] | 2153 | # Extraction should succeed if directories already exist |
| 2154 | os.mkdir(os.path.join(TESTFN2, "a")) |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 2155 | self.test_extract_dir() |
Martin v. Löwis | 70ccd16 | 2009-05-24 19:47:22 +0000 | [diff] [blame] | 2156 | |
Serhiy Storchaka | 46a3492 | 2014-09-23 22:40:23 +0300 | [diff] [blame] | 2157 | def test_write_dir(self): |
| 2158 | dirpath = os.path.join(TESTFN2, "x") |
| 2159 | os.mkdir(dirpath) |
| 2160 | mode = os.stat(dirpath).st_mode & 0xFFFF |
| 2161 | with zipfile.ZipFile(TESTFN, "w") as zipf: |
| 2162 | zipf.write(dirpath) |
| 2163 | zinfo = zipf.filelist[0] |
| 2164 | self.assertTrue(zinfo.filename.endswith("/x/")) |
| 2165 | self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10) |
| 2166 | zipf.write(dirpath, "y") |
| 2167 | zinfo = zipf.filelist[1] |
| 2168 | self.assertTrue(zinfo.filename, "y/") |
| 2169 | self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10) |
| 2170 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 2171 | zinfo = zipf.filelist[0] |
| 2172 | self.assertTrue(zinfo.filename.endswith("/x/")) |
| 2173 | self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10) |
| 2174 | zinfo = zipf.filelist[1] |
| 2175 | self.assertTrue(zinfo.filename, "y/") |
| 2176 | self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10) |
| 2177 | target = os.path.join(TESTFN2, "target") |
| 2178 | os.mkdir(target) |
| 2179 | zipf.extractall(target) |
| 2180 | self.assertTrue(os.path.isdir(os.path.join(target, "y"))) |
| 2181 | self.assertEqual(len(os.listdir(target)), 2) |
| 2182 | |
| 2183 | def test_writestr_dir(self): |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 2184 | os.mkdir(os.path.join(TESTFN2, "x")) |
Serhiy Storchaka | 46a3492 | 2014-09-23 22:40:23 +0300 | [diff] [blame] | 2185 | with zipfile.ZipFile(TESTFN, "w") as zipf: |
| 2186 | zipf.writestr("x/", b'') |
| 2187 | zinfo = zipf.filelist[0] |
| 2188 | self.assertEqual(zinfo.filename, "x/") |
| 2189 | self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10) |
| 2190 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 2191 | zinfo = zipf.filelist[0] |
| 2192 | self.assertTrue(zinfo.filename.endswith("x/")) |
| 2193 | self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10) |
| 2194 | target = os.path.join(TESTFN2, "target") |
| 2195 | os.mkdir(target) |
| 2196 | zipf.extractall(target) |
| 2197 | self.assertTrue(os.path.isdir(os.path.join(target, "x"))) |
| 2198 | self.assertEqual(os.listdir(target), ["x"]) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 2199 | |
| 2200 | def tearDown(self): |
Victor Stinner | 57004c6 | 2014-09-04 00:49:01 +0200 | [diff] [blame] | 2201 | rmtree(TESTFN2) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 2202 | if os.path.exists(TESTFN): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 2203 | unlink(TESTFN) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 2204 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2205 | |
Serhiy Storchaka | 503f908 | 2016-02-08 00:02:25 +0200 | [diff] [blame] | 2206 | class ZipInfoTests(unittest.TestCase): |
| 2207 | def test_from_file(self): |
| 2208 | zi = zipfile.ZipInfo.from_file(__file__) |
| 2209 | self.assertEqual(posixpath.basename(zi.filename), 'test_zipfile.py') |
| 2210 | self.assertFalse(zi.is_dir()) |
Serhiy Storchaka | 8606e95 | 2017-03-08 14:37:51 +0200 | [diff] [blame] | 2211 | self.assertEqual(zi.file_size, os.path.getsize(__file__)) |
| 2212 | |
| 2213 | def test_from_file_pathlike(self): |
| 2214 | zi = zipfile.ZipInfo.from_file(pathlib.Path(__file__)) |
| 2215 | self.assertEqual(posixpath.basename(zi.filename), 'test_zipfile.py') |
| 2216 | self.assertFalse(zi.is_dir()) |
| 2217 | self.assertEqual(zi.file_size, os.path.getsize(__file__)) |
| 2218 | |
| 2219 | def test_from_file_bytes(self): |
| 2220 | zi = zipfile.ZipInfo.from_file(os.fsencode(__file__), 'test') |
| 2221 | self.assertEqual(posixpath.basename(zi.filename), 'test') |
| 2222 | self.assertFalse(zi.is_dir()) |
| 2223 | self.assertEqual(zi.file_size, os.path.getsize(__file__)) |
| 2224 | |
| 2225 | def test_from_file_fileno(self): |
| 2226 | with open(__file__, 'rb') as f: |
| 2227 | zi = zipfile.ZipInfo.from_file(f.fileno(), 'test') |
| 2228 | self.assertEqual(posixpath.basename(zi.filename), 'test') |
| 2229 | self.assertFalse(zi.is_dir()) |
| 2230 | self.assertEqual(zi.file_size, os.path.getsize(__file__)) |
Serhiy Storchaka | 503f908 | 2016-02-08 00:02:25 +0200 | [diff] [blame] | 2231 | |
| 2232 | def test_from_dir(self): |
| 2233 | dirpath = os.path.dirname(os.path.abspath(__file__)) |
| 2234 | zi = zipfile.ZipInfo.from_file(dirpath, 'stdlib_tests') |
| 2235 | self.assertEqual(zi.filename, 'stdlib_tests/') |
| 2236 | self.assertTrue(zi.is_dir()) |
| 2237 | self.assertEqual(zi.compress_type, zipfile.ZIP_STORED) |
| 2238 | self.assertEqual(zi.file_size, 0) |
| 2239 | |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2240 | |
| 2241 | class CommandLineTest(unittest.TestCase): |
| 2242 | |
| 2243 | def zipfilecmd(self, *args, **kwargs): |
| 2244 | rc, out, err = script_helper.assert_python_ok('-m', 'zipfile', *args, |
| 2245 | **kwargs) |
| 2246 | return out.replace(os.linesep.encode(), b'\n') |
| 2247 | |
| 2248 | def zipfilecmd_failure(self, *args): |
| 2249 | return script_helper.assert_python_failure('-m', 'zipfile', *args) |
| 2250 | |
Serhiy Storchaka | 150cd19 | 2017-04-07 18:56:12 +0300 | [diff] [blame] | 2251 | def test_bad_use(self): |
| 2252 | rc, out, err = self.zipfilecmd_failure() |
| 2253 | self.assertEqual(out, b'') |
| 2254 | self.assertIn(b'usage', err.lower()) |
| 2255 | self.assertIn(b'error', err.lower()) |
| 2256 | self.assertIn(b'required', err.lower()) |
| 2257 | rc, out, err = self.zipfilecmd_failure('-l', '') |
| 2258 | self.assertEqual(out, b'') |
| 2259 | self.assertNotEqual(err.strip(), b'') |
| 2260 | |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2261 | def test_test_command(self): |
| 2262 | zip_name = findfile('zipdir.zip') |
Serhiy Storchaka | 8c93310 | 2016-10-23 13:32:12 +0300 | [diff] [blame] | 2263 | for opt in '-t', '--test': |
| 2264 | out = self.zipfilecmd(opt, zip_name) |
| 2265 | self.assertEqual(out.rstrip(), b'Done testing') |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2266 | zip_name = findfile('testtar.tar') |
| 2267 | rc, out, err = self.zipfilecmd_failure('-t', zip_name) |
| 2268 | self.assertEqual(out, b'') |
| 2269 | |
| 2270 | def test_list_command(self): |
| 2271 | zip_name = findfile('zipdir.zip') |
| 2272 | t = io.StringIO() |
| 2273 | with zipfile.ZipFile(zip_name, 'r') as tf: |
| 2274 | tf.printdir(t) |
| 2275 | expected = t.getvalue().encode('ascii', 'backslashreplace') |
Serhiy Storchaka | 8c93310 | 2016-10-23 13:32:12 +0300 | [diff] [blame] | 2276 | for opt in '-l', '--list': |
| 2277 | out = self.zipfilecmd(opt, zip_name, |
| 2278 | PYTHONIOENCODING='ascii:backslashreplace') |
| 2279 | self.assertEqual(out, expected) |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2280 | |
Serhiy Storchaka | b4293ef | 2016-10-23 22:32:30 +0300 | [diff] [blame] | 2281 | @requires_zlib |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2282 | def test_create_command(self): |
| 2283 | self.addCleanup(unlink, TESTFN) |
| 2284 | with open(TESTFN, 'w') as f: |
| 2285 | f.write('test 1') |
| 2286 | os.mkdir(TESTFNDIR) |
| 2287 | self.addCleanup(rmtree, TESTFNDIR) |
| 2288 | with open(os.path.join(TESTFNDIR, 'file.txt'), 'w') as f: |
| 2289 | f.write('test 2') |
| 2290 | files = [TESTFN, TESTFNDIR] |
| 2291 | namelist = [TESTFN, TESTFNDIR + '/', TESTFNDIR + '/file.txt'] |
Serhiy Storchaka | 8c93310 | 2016-10-23 13:32:12 +0300 | [diff] [blame] | 2292 | for opt in '-c', '--create': |
| 2293 | try: |
| 2294 | out = self.zipfilecmd(opt, TESTFN2, *files) |
| 2295 | self.assertEqual(out, b'') |
| 2296 | with zipfile.ZipFile(TESTFN2) as zf: |
| 2297 | self.assertEqual(zf.namelist(), namelist) |
| 2298 | self.assertEqual(zf.read(namelist[0]), b'test 1') |
| 2299 | self.assertEqual(zf.read(namelist[2]), b'test 2') |
| 2300 | finally: |
| 2301 | unlink(TESTFN2) |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2302 | |
| 2303 | def test_extract_command(self): |
| 2304 | zip_name = findfile('zipdir.zip') |
Serhiy Storchaka | 8c93310 | 2016-10-23 13:32:12 +0300 | [diff] [blame] | 2305 | for opt in '-e', '--extract': |
| 2306 | with temp_dir() as extdir: |
| 2307 | out = self.zipfilecmd(opt, zip_name, extdir) |
| 2308 | self.assertEqual(out, b'') |
| 2309 | with zipfile.ZipFile(zip_name) as zf: |
| 2310 | for zi in zf.infolist(): |
| 2311 | path = os.path.join(extdir, |
| 2312 | zi.filename.replace('/', os.sep)) |
| 2313 | if zi.is_dir(): |
| 2314 | self.assertTrue(os.path.isdir(path)) |
| 2315 | else: |
| 2316 | self.assertTrue(os.path.isfile(path)) |
| 2317 | with open(path, 'rb') as f: |
| 2318 | self.assertEqual(f.read(), zf.read(zi)) |
Serhiy Storchaka | 61c4c44 | 2016-10-23 13:07:59 +0300 | [diff] [blame] | 2319 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 2320 | if __name__ == "__main__": |
Brett Cannon | d5b4e1d | 2013-06-12 19:57:19 -0400 | [diff] [blame] | 2321 | unittest.main() |