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