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