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