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