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