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