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