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