Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1 | # We can test part of the module without zlib. |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 2 | try: |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 3 | import zlib |
| 4 | except ImportError: |
| 5 | zlib = None |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 6 | |
| 7 | import io |
| 8 | import os |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 9 | import imp |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 10 | import time |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 11 | import shutil |
| 12 | import struct |
| 13 | import zipfile |
| 14 | import unittest |
| 15 | |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 16 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 17 | from tempfile import TemporaryFile |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 18 | from random import randint, random |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 19 | from unittest import skipUnless |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 20 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 21 | from test.support import TESTFN, run_unittest, findfile, unlink |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 22 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 23 | TESTFN2 = TESTFN + "2" |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 24 | TESTFNDIR = TESTFN + "d" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 25 | FIXEDTEST_SIZE = 1000 |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 26 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 27 | SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'), |
| 28 | ('ziptest2dir/_ziptest2', 'qawsedrftg'), |
| 29 | ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'), |
| 30 | ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')] |
| 31 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 32 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 33 | class TestsWithSourceFile(unittest.TestCase): |
| 34 | def setUp(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 35 | self.line_gen = (bytes("Zipfile test line %d. random float: %f" % |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 36 | (i, random()), "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 37 | for i in range(FIXEDTEST_SIZE)) |
| 38 | self.data = b'\n'.join(self.line_gen) + b'\n' |
Fred Drake | 6e7e485 | 2001-02-28 05:34:16 +0000 | [diff] [blame] | 39 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 40 | # Make a source file with some lines |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 41 | with open(TESTFN, "wb") as fp: |
| 42 | fp.write(self.data) |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 43 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 44 | def make_test_archive(self, f, compression): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 45 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 46 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 47 | zipfp.write(TESTFN, "another.name") |
| 48 | zipfp.write(TESTFN, TESTFN) |
| 49 | zipfp.writestr("strfile", 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 zip_test(self, f, compression): |
| 52 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 53 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 54 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 55 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 56 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 57 | self.assertEqual(zipfp.read("another.name"), self.data) |
| 58 | self.assertEqual(zipfp.read("strfile"), self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 59 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 60 | # Print the ZIP directory |
| 61 | fp = io.StringIO() |
| 62 | zipfp.printdir(file=fp) |
| 63 | directory = fp.getvalue() |
| 64 | lines = directory.splitlines() |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 65 | self.assertEqual(len(lines), 4) # Number of files + header |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 67 | self.assertIn('File Name', lines[0]) |
| 68 | self.assertIn('Modified', lines[0]) |
| 69 | self.assertIn('Size', lines[0]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 70 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 71 | fn, date, time_, size = lines[1].split() |
| 72 | self.assertEqual(fn, 'another.name') |
| 73 | self.assertTrue(time.strptime(date, '%Y-%m-%d')) |
| 74 | self.assertTrue(time.strptime(time_, '%H:%M:%S')) |
| 75 | self.assertEqual(size, str(len(self.data))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 77 | # Check the namelist |
| 78 | names = zipfp.namelist() |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 79 | self.assertEqual(len(names), 3) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 80 | self.assertIn(TESTFN, names) |
| 81 | self.assertIn("another.name", names) |
| 82 | self.assertIn("strfile", names) |
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 infolist |
| 85 | infos = zipfp.infolist() |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 86 | names = [i.filename for i in infos] |
| 87 | self.assertEqual(len(names), 3) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 88 | self.assertIn(TESTFN, names) |
| 89 | self.assertIn("another.name", names) |
| 90 | self.assertIn("strfile", names) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 91 | for i in infos: |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 92 | self.assertEqual(i.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 93 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 94 | # check getinfo |
| 95 | for nm in (TESTFN, "another.name", "strfile"): |
| 96 | info = zipfp.getinfo(nm) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 97 | self.assertEqual(info.filename, nm) |
| 98 | self.assertEqual(info.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 99 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 100 | # Check that testzip doesn't raise an exception |
| 101 | zipfp.testzip() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 102 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 103 | def test_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 104 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 105 | self.zip_test(f, zipfile.ZIP_STORED) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 106 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 107 | def zip_open_test(self, f, compression): |
| 108 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 109 | |
| 110 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 111 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 112 | zipdata1 = [] |
| 113 | zipopen1 = zipfp.open(TESTFN) |
| 114 | while True: |
| 115 | read_data = zipopen1.read(256) |
| 116 | if not read_data: |
| 117 | break |
| 118 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 119 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 120 | zipdata2 = [] |
| 121 | zipopen2 = zipfp.open("another.name") |
| 122 | while True: |
| 123 | read_data = zipopen2.read(256) |
| 124 | if not read_data: |
| 125 | break |
| 126 | zipdata2.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 127 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 128 | self.assertEqual(b''.join(zipdata1), self.data) |
| 129 | self.assertEqual(b''.join(zipdata2), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 130 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 131 | def test_open_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 132 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 133 | self.zip_open_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 134 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 135 | def test_open_via_zip_info(self): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 136 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 137 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 138 | zipfp.writestr("name", "foo") |
| 139 | zipfp.writestr("name", "bar") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 140 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 141 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 142 | infos = zipfp.infolist() |
| 143 | data = b"" |
| 144 | for info in infos: |
| 145 | data += zipfp.open(info).read() |
| 146 | self.assertTrue(data == b"foobar" or data == b"barfoo") |
| 147 | data = b"" |
| 148 | for info in infos: |
| 149 | data += zipfp.read(info) |
| 150 | self.assertTrue(data == b"foobar" or data == b"barfoo") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 151 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 152 | def zip_random_open_test(self, f, compression): |
| 153 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 154 | |
| 155 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 156 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 157 | zipdata1 = [] |
| 158 | zipopen1 = zipfp.open(TESTFN) |
| 159 | while True: |
| 160 | read_data = zipopen1.read(randint(1, 1024)) |
| 161 | if not read_data: |
| 162 | break |
| 163 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 164 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 165 | self.assertEqual(b''.join(zipdata1), self.data) |
| 166 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 167 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 168 | def test_random_open_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 169 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 170 | self.zip_random_open_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 171 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 172 | def test_univeral_readaheads(self): |
| 173 | f = io.BytesIO() |
| 174 | |
| 175 | data = b'a\r\n' * 16 * 1024 |
| 176 | zipfp = zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) |
| 177 | zipfp.writestr(TESTFN, data) |
| 178 | zipfp.close() |
| 179 | |
| 180 | data2 = b'' |
| 181 | zipfp = zipfile.ZipFile(f, 'r') |
| 182 | zipopen = zipfp.open(TESTFN, 'rU') |
| 183 | for line in zipopen: |
| 184 | data2 += line |
| 185 | zipfp.close() |
| 186 | |
| 187 | self.assertEqual(data, data2.replace(b'\n', b'\r\n')) |
| 188 | |
| 189 | def zip_readline_read_test(self, f, compression): |
| 190 | self.make_test_archive(f, compression) |
| 191 | |
| 192 | # Read the ZIP archive |
| 193 | zipfp = zipfile.ZipFile(f, "r") |
| 194 | zipopen = zipfp.open(TESTFN) |
| 195 | |
| 196 | data = b'' |
| 197 | while True: |
| 198 | read = zipopen.readline() |
| 199 | if not read: |
| 200 | break |
| 201 | data += read |
| 202 | |
| 203 | read = zipopen.read(100) |
| 204 | if not read: |
| 205 | break |
| 206 | data += read |
| 207 | |
| 208 | self.assertEqual(data, self.data) |
| 209 | zipfp.close() |
| 210 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 211 | def zip_readline_test(self, f, compression): |
| 212 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 213 | |
| 214 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 215 | with zipfile.ZipFile(f, "r") as zipfp: |
| 216 | zipopen = zipfp.open(TESTFN) |
| 217 | for line in self.line_gen: |
| 218 | linedata = zipopen.readline() |
| 219 | self.assertEqual(linedata, line + '\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 220 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 221 | def zip_readlines_test(self, f, compression): |
| 222 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 223 | |
| 224 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 225 | with zipfile.ZipFile(f, "r") as zipfp: |
| 226 | ziplines = zipfp.open(TESTFN).readlines() |
| 227 | for line, zipline in zip(self.line_gen, ziplines): |
| 228 | self.assertEqual(zipline, line + '\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 229 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 230 | def zip_iterlines_test(self, f, compression): |
| 231 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 232 | |
| 233 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 234 | with zipfile.ZipFile(f, "r") as zipfp: |
| 235 | for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)): |
| 236 | self.assertEqual(zipline, line + '\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 237 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 238 | def test_readline_read_stored(self): |
| 239 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 240 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 241 | self.zip_readline_read_test(f, zipfile.ZIP_STORED) |
| 242 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 243 | def test_readline_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 244 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 245 | self.zip_readline_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 246 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 247 | def test_readlines_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 248 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 249 | self.zip_readlines_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 250 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 251 | def test_iterlines_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 252 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 253 | self.zip_iterlines_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 254 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 255 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 256 | def test_deflated(self): |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 257 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 258 | self.zip_test(f, zipfile.ZIP_DEFLATED) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 259 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 260 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 261 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 262 | def test_open_deflated(self): |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 263 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 264 | self.zip_open_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 265 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 266 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 267 | def test_random_open_deflated(self): |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 268 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 269 | self.zip_random_open_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 270 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 271 | @skipUnless(zlib, "requires zlib") |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 272 | def test_readline_read_deflated(self): |
| 273 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 274 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 275 | self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED) |
| 276 | |
| 277 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 278 | def test_readline_deflated(self): |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 279 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 280 | self.zip_readline_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 281 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 282 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 283 | def test_readlines_deflated(self): |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 284 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 285 | self.zip_readlines_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 286 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 287 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 288 | def test_iterlines_deflated(self): |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 289 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 290 | self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 291 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 292 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 293 | def test_low_compression(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 294 | """Check for cases where compressed data is larger than original.""" |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 295 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 296 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp: |
| 297 | zipfp.writestr("strfile", '12') |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 298 | |
| 299 | # Get an open object for strfile |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 300 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp: |
| 301 | openobj = zipfp.open("strfile") |
| 302 | self.assertEqual(openobj.read(1), b'1') |
| 303 | self.assertEqual(openobj.read(1), b'2') |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 304 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 305 | def test_absolute_arcnames(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 306 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 307 | zipfp.write(TESTFN, "/absolute") |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame] | 308 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 309 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 310 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
Tim Peters | 32cbc96 | 2006-02-20 21:42:18 +0000 | [diff] [blame] | 311 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 312 | def test_append_to_zip_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 313 | """Test appending to an existing zipfile.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 314 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 315 | zipfp.write(TESTFN, TESTFN) |
| 316 | |
| 317 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 318 | zipfp.writestr("strfile", self.data) |
| 319 | self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"]) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 320 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 321 | def test_append_to_non_zip_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 322 | """Test appending to an existing file that is not a zipfile.""" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 323 | # NOTE: this test fails if len(d) < 22 because of the first |
| 324 | # line "fpin.seek(-22, 2)" in _EndRecData |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 325 | data = b'I am not a ZipFile!'*10 |
| 326 | with open(TESTFN2, 'wb') as f: |
| 327 | f.write(data) |
| 328 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 329 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 330 | zipfp.write(TESTFN, TESTFN) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 331 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 332 | with open(TESTFN2, 'rb') as f: |
| 333 | f.seek(len(data)) |
| 334 | with zipfile.ZipFile(f, "r") as zipfp: |
| 335 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 336 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 337 | def test_write_default_name(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 338 | """Check that calling ZipFile.write without arcname specified |
| 339 | produces the expected result.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 340 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 341 | zipfp.write(TESTFN) |
| 342 | self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read()) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 343 | |
Ezio Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 344 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 345 | def test_per_file_compression(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 346 | """Check that files within a Zip archive can have different |
| 347 | compression options.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 348 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 349 | zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED) |
| 350 | zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED) |
| 351 | sinfo = zipfp.getinfo('storeme') |
| 352 | dinfo = zipfp.getinfo('deflateme') |
| 353 | self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED) |
| 354 | self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 355 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 356 | def test_write_to_readonly(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 357 | """Check that trying to call write() on a readonly ZipFile object |
| 358 | raises a RuntimeError.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 359 | with zipfile.ZipFile(TESTFN2, mode="w") as zipfp: |
| 360 | zipfp.writestr("somefile.txt", "bogus") |
| 361 | |
| 362 | with zipfile.ZipFile(TESTFN2, mode="r") as zipfp: |
| 363 | self.assertRaises(RuntimeError, zipfp.write, TESTFN) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 364 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 365 | def test_extract(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 366 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 367 | for fpath, fdata in SMALL_TEST_DATA: |
| 368 | zipfp.writestr(fpath, fdata) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 369 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 370 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 371 | for fpath, fdata in SMALL_TEST_DATA: |
| 372 | writtenfile = zipfp.extract(fpath) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 373 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 374 | # make sure it was written to the right place |
| 375 | if os.path.isabs(fpath): |
| 376 | correctfile = os.path.join(os.getcwd(), fpath[1:]) |
| 377 | else: |
| 378 | correctfile = os.path.join(os.getcwd(), fpath) |
| 379 | correctfile = os.path.normpath(correctfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 380 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 381 | self.assertEqual(writtenfile, correctfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 382 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 383 | # make sure correct data is in correct file |
| 384 | self.assertEqual(fdata.encode(), open(writtenfile, "rb").read()) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 385 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 386 | os.remove(writtenfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 387 | |
| 388 | # remove the test file subdirectories |
| 389 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 390 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 391 | def test_extract_all(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 392 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 393 | for fpath, fdata in SMALL_TEST_DATA: |
| 394 | zipfp.writestr(fpath, fdata) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 395 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 396 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 397 | zipfp.extractall() |
| 398 | for fpath, fdata in SMALL_TEST_DATA: |
| 399 | if os.path.isabs(fpath): |
| 400 | outfile = os.path.join(os.getcwd(), fpath[1:]) |
| 401 | else: |
| 402 | outfile = os.path.join(os.getcwd(), fpath) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 403 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 404 | self.assertEqual(fdata.encode(), open(outfile, "rb").read()) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 405 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 406 | os.remove(outfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 407 | |
| 408 | # remove the test file subdirectories |
| 409 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 410 | |
Ronald Oussoren | ee5c885 | 2010-02-07 20:24:02 +0000 | [diff] [blame] | 411 | def test_writestr_compression(self): |
| 412 | zipfp = zipfile.ZipFile(TESTFN2, "w") |
| 413 | zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED) |
| 414 | if zlib: |
| 415 | zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED) |
| 416 | |
| 417 | info = zipfp.getinfo('a.txt') |
| 418 | self.assertEqual(info.compress_type, zipfile.ZIP_STORED) |
| 419 | |
| 420 | if zlib: |
| 421 | info = zipfp.getinfo('b.txt') |
| 422 | self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED) |
| 423 | |
| 424 | |
Antoine Pitrou | 6e1df8d | 2008-07-25 19:58:18 +0000 | [diff] [blame] | 425 | def zip_test_writestr_permissions(self, f, compression): |
| 426 | # Make sure that writestr creates files with mode 0600, |
| 427 | # when it is passed a name rather than a ZipInfo instance. |
| 428 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 429 | self.make_test_archive(f, compression) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 430 | with zipfile.ZipFile(f, "r") as zipfp: |
| 431 | zinfo = zipfp.getinfo('strfile') |
| 432 | self.assertEqual(zinfo.external_attr, 0o600 << 16) |
Antoine Pitrou | 6e1df8d | 2008-07-25 19:58:18 +0000 | [diff] [blame] | 433 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 434 | def test_writestr_permissions(self): |
Antoine Pitrou | 6e1df8d | 2008-07-25 19:58:18 +0000 | [diff] [blame] | 435 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 436 | self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED) |
| 437 | |
Gregory P. Smith | b0d9ca9 | 2009-07-07 05:06:04 +0000 | [diff] [blame] | 438 | def test_writestr_extended_local_header_issue1202(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 439 | with zipfile.ZipFile(TESTFN2, 'w') as orig_zip: |
| 440 | for data in 'abcdefghijklmnop': |
| 441 | zinfo = zipfile.ZipInfo(data) |
| 442 | zinfo.flag_bits |= 0x08 # Include an extended local header. |
| 443 | orig_zip.writestr(zinfo, data) |
| 444 | |
| 445 | def test_close(self): |
| 446 | """Check that the zipfile is closed after the 'with' block.""" |
| 447 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 448 | for fpath, fdata in SMALL_TEST_DATA: |
| 449 | zipfp.writestr(fpath, fdata) |
| 450 | self.assertTrue(zipfp.fp is not None, 'zipfp is not open') |
| 451 | self.assertTrue(zipfp.fp is None, 'zipfp is not closed') |
| 452 | |
| 453 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 454 | self.assertTrue(zipfp.fp is not None, 'zipfp is not open') |
| 455 | self.assertTrue(zipfp.fp is None, 'zipfp is not closed') |
| 456 | |
| 457 | def test_close_on_exception(self): |
| 458 | """Check that the zipfile is closed if an exception is raised in the |
| 459 | 'with' block.""" |
| 460 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 461 | for fpath, fdata in SMALL_TEST_DATA: |
| 462 | zipfp.writestr(fpath, fdata) |
| 463 | |
| 464 | try: |
| 465 | with zipfile.ZipFile(TESTFN2, "r") as zipfp2: |
| 466 | raise zipfile.BadZipfile() |
| 467 | except zipfile.BadZipfile: |
| 468 | self.assertTrue(zipfp2.fp is None, 'zipfp is not closed') |
Gregory P. Smith | b0d9ca9 | 2009-07-07 05:06:04 +0000 | [diff] [blame] | 469 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 470 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 471 | unlink(TESTFN) |
| 472 | unlink(TESTFN2) |
| 473 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 474 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 475 | class TestZip64InSmallFiles(unittest.TestCase): |
| 476 | # These tests test the ZIP64 functionality without using large files, |
| 477 | # see test_zipfile64 for proper tests. |
| 478 | |
| 479 | def setUp(self): |
| 480 | self._limit = zipfile.ZIP64_LIMIT |
| 481 | zipfile.ZIP64_LIMIT = 5 |
| 482 | |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 483 | line_gen = (bytes("Test of zipfile line %d." % i, "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 484 | for i in range(0, FIXEDTEST_SIZE)) |
| 485 | self.data = b'\n'.join(line_gen) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 486 | |
| 487 | # Make a source file with some lines |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 488 | with open(TESTFN, "wb") as fp: |
| 489 | fp.write(self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 490 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 491 | def large_file_exception_test(self, f, compression): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 492 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 493 | self.assertRaises(zipfile.LargeZipFile, |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 494 | zipfp.write, TESTFN, "another.name") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 495 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 496 | def large_file_exception_test2(self, f, compression): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 497 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 498 | self.assertRaises(zipfile.LargeZipFile, |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 499 | zipfp.writestr, "another.name", self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 500 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 501 | def test_large_file_exception(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 502 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 503 | self.large_file_exception_test(f, zipfile.ZIP_STORED) |
| 504 | self.large_file_exception_test2(f, zipfile.ZIP_STORED) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 505 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 506 | def zip_test(self, f, compression): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 507 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 508 | with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp: |
| 509 | zipfp.write(TESTFN, "another.name") |
| 510 | zipfp.write(TESTFN, TESTFN) |
| 511 | zipfp.writestr("strfile", self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 512 | |
| 513 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 514 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 515 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 516 | self.assertEqual(zipfp.read("another.name"), self.data) |
| 517 | self.assertEqual(zipfp.read("strfile"), self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 518 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 519 | # Print the ZIP directory |
| 520 | fp = io.StringIO() |
| 521 | zipfp.printdir(fp) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 522 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 523 | directory = fp.getvalue() |
| 524 | lines = directory.splitlines() |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 525 | self.assertEqual(len(lines), 4) # Number of files + header |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 526 | |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 527 | self.assertIn('File Name', lines[0]) |
| 528 | self.assertIn('Modified', lines[0]) |
| 529 | self.assertIn('Size', lines[0]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 530 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 531 | fn, date, time_, size = lines[1].split() |
| 532 | self.assertEqual(fn, 'another.name') |
| 533 | self.assertTrue(time.strptime(date, '%Y-%m-%d')) |
| 534 | self.assertTrue(time.strptime(time_, '%H:%M:%S')) |
| 535 | self.assertEqual(size, str(len(self.data))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 536 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 537 | # Check the namelist |
| 538 | names = zipfp.namelist() |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 539 | self.assertEqual(len(names), 3) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 540 | self.assertIn(TESTFN, names) |
| 541 | self.assertIn("another.name", names) |
| 542 | self.assertIn("strfile", names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 543 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 544 | # Check infolist |
| 545 | infos = zipfp.infolist() |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 546 | names = [i.filename for i in infos] |
| 547 | self.assertEqual(len(names), 3) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 548 | self.assertIn(TESTFN, names) |
| 549 | self.assertIn("another.name", names) |
| 550 | self.assertIn("strfile", names) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 551 | for i in infos: |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 552 | self.assertEqual(i.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 553 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 554 | # check getinfo |
| 555 | for nm in (TESTFN, "another.name", "strfile"): |
| 556 | info = zipfp.getinfo(nm) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 557 | self.assertEqual(info.filename, nm) |
| 558 | self.assertEqual(info.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 559 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 560 | # Check that testzip doesn't raise an exception |
| 561 | zipfp.testzip() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 562 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 563 | def test_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 564 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 565 | self.zip_test(f, zipfile.ZIP_STORED) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 566 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 567 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 568 | def test_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 569 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 570 | self.zip_test(f, zipfile.ZIP_DEFLATED) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 571 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 572 | def test_absolute_arcnames(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 573 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, |
| 574 | allowZip64=True) as zipfp: |
| 575 | zipfp.write(TESTFN, "/absolute") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 576 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 577 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 578 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 579 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 580 | def tearDown(self): |
| 581 | zipfile.ZIP64_LIMIT = self._limit |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 582 | unlink(TESTFN) |
| 583 | unlink(TESTFN2) |
| 584 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 585 | |
| 586 | class PyZipFileTests(unittest.TestCase): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 587 | def test_write_pyfile(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 588 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 589 | fn = __file__ |
| 590 | if fn.endswith('.pyc') or fn.endswith('.pyo'): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 591 | path_split = fn.split(os.sep) |
| 592 | if os.altsep is not None: |
| 593 | path_split.extend(fn.split(os.altsep)) |
| 594 | if '__pycache__' in path_split: |
| 595 | fn = imp.source_from_cache(fn) |
| 596 | else: |
| 597 | fn = fn[:-1] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 598 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 599 | zipfp.writepy(fn) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 600 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 601 | bn = os.path.basename(fn) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 602 | self.assertNotIn(bn, zipfp.namelist()) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 603 | self.assertTrue(bn + 'o' in zipfp.namelist() or |
| 604 | bn + 'c' in zipfp.namelist()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 605 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 606 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 607 | fn = __file__ |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 608 | if fn.endswith(('.pyc', '.pyo')): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 609 | fn = fn[:-1] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 610 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 611 | zipfp.writepy(fn, "testpackage") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 612 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 613 | bn = "%s/%s" % ("testpackage", os.path.basename(fn)) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 614 | self.assertNotIn(bn, zipfp.namelist()) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 615 | self.assertTrue(bn + 'o' in zipfp.namelist() or |
| 616 | bn + 'c' in zipfp.namelist()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 617 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 618 | def test_write_python_package(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 619 | import email |
| 620 | packagedir = os.path.dirname(email.__file__) |
| 621 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 622 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 623 | zipfp.writepy(packagedir) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 624 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 625 | # Check for a couple of modules at different levels of the |
| 626 | # hierarchy |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 627 | names = zipfp.namelist() |
| 628 | self.assertTrue('email/__init__.pyo' in names or |
| 629 | 'email/__init__.pyc' in names) |
| 630 | self.assertTrue('email/mime/text.pyo' in names or |
| 631 | 'email/mime/text.pyc' in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 632 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 633 | def test_write_python_directory(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 634 | os.mkdir(TESTFN2) |
| 635 | try: |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 636 | with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: |
| 637 | fp.write("print(42)\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 638 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 639 | with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: |
| 640 | fp.write("print(42 * 42)\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 641 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 642 | with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: |
| 643 | fp.write("bla bla bla\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 644 | |
| 645 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 646 | zipfp.writepy(TESTFN2) |
| 647 | |
| 648 | names = zipfp.namelist() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 649 | self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names) |
| 650 | self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 651 | self.assertNotIn('mod2.txt', names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 652 | |
| 653 | finally: |
| 654 | shutil.rmtree(TESTFN2) |
| 655 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 656 | def test_write_non_pyfile(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 657 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 658 | open(TESTFN, 'w').write('most definitely not a python file') |
| 659 | self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) |
| 660 | os.remove(TESTFN) |
| 661 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 662 | |
| 663 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 664 | class OtherTests(unittest.TestCase): |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 665 | zips_with_bad_crc = { |
| 666 | zipfile.ZIP_STORED: ( |
| 667 | b'PK\003\004\024\0\0\0\0\0 \213\212;:r' |
| 668 | b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af' |
| 669 | b'ilehello,AworldP' |
| 670 | b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:' |
| 671 | b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0' |
| 672 | b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi' |
| 673 | b'lePK\005\006\0\0\0\0\001\0\001\0003\000' |
| 674 | b'\0\0/\0\0\0\0\0'), |
| 675 | zipfile.ZIP_DEFLATED: ( |
| 676 | b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA' |
| 677 | b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' |
| 678 | b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0' |
| 679 | b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n' |
| 680 | b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05' |
| 681 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00' |
| 682 | b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00' |
| 683 | b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'), |
| 684 | } |
| 685 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 686 | def test_unicode_filenames(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 687 | with zipfile.ZipFile(TESTFN, "w") as zf: |
| 688 | zf.writestr("foo.txt", "Test for unicode filename") |
| 689 | zf.writestr("\xf6.txt", "Test for unicode filename") |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 690 | self.assertIsInstance(zf.infolist()[0].filename, str) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 691 | |
| 692 | with zipfile.ZipFile(TESTFN, "r") as zf: |
| 693 | self.assertEqual(zf.filelist[0].filename, "foo.txt") |
| 694 | self.assertEqual(zf.filelist[1].filename, "\xf6.txt") |
Martin v. Löwis | 8570f6a | 2008-05-05 17:44:38 +0000 | [diff] [blame] | 695 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 696 | def test_create_non_existent_file_for_append(self): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 697 | if os.path.exists(TESTFN): |
| 698 | os.unlink(TESTFN) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 699 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 700 | filename = 'testfile.txt' |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 701 | content = b'hello, world. this is some content.' |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 702 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 703 | try: |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 704 | with zipfile.ZipFile(TESTFN, 'a') as zf: |
| 705 | zf.writestr(filename, content) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 706 | except IOError: |
| 707 | self.fail('Could not append data to a non-existent zip file.') |
| 708 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 709 | self.assertTrue(os.path.exists(TESTFN)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 710 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 711 | with zipfile.ZipFile(TESTFN, 'r') as zf: |
| 712 | self.assertEqual(zf.read(filename), content) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 713 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 714 | def test_close_erroneous_file(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 715 | # This test checks that the ZipFile constructor closes the file object |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 716 | # it opens if there's an error in the file. If it doesn't, the |
| 717 | # traceback holds a reference to the ZipFile object and, indirectly, |
| 718 | # the file object. |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 719 | # On Windows, this causes the os.unlink() call to fail because the |
| 720 | # underlying file is still open. This is SF bug #412214. |
| 721 | # |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 722 | with open(TESTFN, "w") as fp: |
| 723 | fp.write("this is not a legal zip file\n") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 724 | try: |
| 725 | zf = zipfile.ZipFile(TESTFN) |
| 726 | except zipfile.BadZipfile: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 727 | pass |
| 728 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 729 | def test_is_zip_erroneous_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 730 | """Check that is_zipfile() correctly identifies non-zip files.""" |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 731 | # - passing a filename |
| 732 | with open(TESTFN, "w") as fp: |
| 733 | fp.write("this is not a legal zip file\n") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 734 | chk = zipfile.is_zipfile(TESTFN) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 735 | self.assertFalse(chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 736 | # - passing a file object |
| 737 | with open(TESTFN, "rb") as fp: |
| 738 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 739 | self.assertTrue(not chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 740 | # - passing a file-like object |
| 741 | fp = io.BytesIO() |
| 742 | fp.write(b"this is not a legal zip file\n") |
| 743 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 744 | self.assertTrue(not chk) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 745 | fp.seek(0, 0) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 746 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 747 | self.assertTrue(not chk) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 748 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 749 | def test_is_zip_valid_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 750 | """Check that is_zipfile() correctly identifies zip files.""" |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 751 | # - passing a filename |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 752 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 753 | zipf.writestr("foo.txt", b"O, for a Muse of Fire!") |
| 754 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 755 | chk = zipfile.is_zipfile(TESTFN) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 756 | self.assertTrue(chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 757 | # - passing a file object |
| 758 | with open(TESTFN, "rb") as fp: |
| 759 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 760 | self.assertTrue(chk) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 761 | fp.seek(0, 0) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 762 | zip_contents = fp.read() |
| 763 | # - passing a file-like object |
| 764 | fp = io.BytesIO() |
| 765 | fp.write(zip_contents) |
| 766 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 767 | self.assertTrue(chk) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 768 | fp.seek(0, 0) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 769 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 770 | self.assertTrue(chk) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 771 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 772 | def test_non_existent_file_raises_IOError(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 773 | # make sure we don't raise an AttributeError when a partially-constructed |
| 774 | # ZipFile instance is finalized; this tests for regression on SF tracker |
| 775 | # bug #403871. |
| 776 | |
| 777 | # The bug we're testing for caused an AttributeError to be raised |
| 778 | # when a ZipFile instance was created for a file that did not |
| 779 | # exist; the .fp member was not initialized but was needed by the |
| 780 | # __del__() method. Since the AttributeError is in the __del__(), |
| 781 | # it is ignored, but the user should be sufficiently annoyed by |
| 782 | # the message on the output that regression will be noticed |
| 783 | # quickly. |
| 784 | self.assertRaises(IOError, zipfile.ZipFile, TESTFN) |
| 785 | |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 786 | def test_empty_file_raises_BadZipFile(self): |
| 787 | f = open(TESTFN, 'w') |
| 788 | f.close() |
| 789 | self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN) |
| 790 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 791 | with open(TESTFN, 'w') as fp: |
| 792 | fp.write("short file") |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 793 | self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN) |
| 794 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 795 | def test_closed_zip_raises_RuntimeError(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 796 | """Verify that testzip() doesn't swallow inappropriate exceptions.""" |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 797 | data = io.BytesIO() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 798 | with zipfile.ZipFile(data, mode="w") as zipf: |
| 799 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 800 | |
| 801 | # This is correct; calling .read on a closed ZipFile should throw |
| 802 | # a RuntimeError, and so should calling .testzip. An earlier |
| 803 | # version of .testzip would swallow this exception (and any other) |
| 804 | # and report that the first file in the archive was corrupt. |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 805 | self.assertRaises(RuntimeError, zipf.read, "foo.txt") |
| 806 | self.assertRaises(RuntimeError, zipf.open, "foo.txt") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 807 | self.assertRaises(RuntimeError, zipf.testzip) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 808 | self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus") |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 809 | open(TESTFN, 'w').write('zipfile test data') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 810 | self.assertRaises(RuntimeError, zipf.write, TESTFN) |
| 811 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 812 | def test_bad_constructor_mode(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 813 | """Check that bad modes passed to ZipFile constructor are caught.""" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 814 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q") |
| 815 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 816 | def test_bad_open_mode(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 817 | """Check that bad modes passed to ZipFile.open are caught.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 818 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 819 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 820 | |
| 821 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 822 | # read the data to make sure the file is there |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 823 | zipf.read("foo.txt") |
| 824 | self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 825 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 826 | def test_read0(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 827 | """Check that calling read(0) on a ZipExtFile object returns an empty |
| 828 | string and doesn't advance file pointer.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 829 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 830 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 831 | # read the data to make sure the file is there |
| 832 | f = zipf.open("foo.txt") |
| 833 | for i in range(FIXEDTEST_SIZE): |
| 834 | self.assertEqual(f.read(0), b'') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 835 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 836 | self.assertEqual(f.read(), b"O, for a Muse of Fire!") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 837 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 838 | def test_open_non_existent_item(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 839 | """Check that attempting to call open() for an item that doesn't |
| 840 | exist in the archive raises a RuntimeError.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 841 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 842 | self.assertRaises(KeyError, zipf.open, "foo.txt", "r") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 843 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 844 | def test_bad_compression_mode(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 845 | """Check that bad compression methods passed to ZipFile.open are |
| 846 | caught.""" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 847 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1) |
| 848 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 849 | def test_null_byte_in_filename(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 850 | """Check that a filename containing a null byte is properly |
| 851 | terminated.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 852 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 853 | zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!") |
| 854 | self.assertEqual(zipf.namelist(), ['foo.txt']) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 855 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 856 | def test_struct_sizes(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 857 | """Check that ZIP internal structure sizes are calculated correctly.""" |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 858 | self.assertEqual(zipfile.sizeEndCentDir, 22) |
| 859 | self.assertEqual(zipfile.sizeCentralDir, 46) |
| 860 | self.assertEqual(zipfile.sizeEndCentDir64, 56) |
| 861 | self.assertEqual(zipfile.sizeEndCentDir64Locator, 20) |
| 862 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 863 | def test_comments(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 864 | """Check that comments on the archive are handled properly.""" |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 865 | |
| 866 | # check default comment is empty |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 867 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 868 | self.assertEqual(zipf.comment, b'') |
| 869 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 870 | |
| 871 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 872 | self.assertEqual(zipfr.comment, b'') |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 873 | |
| 874 | # check a simple short comment |
| 875 | 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] | 876 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 877 | zipf.comment = comment |
| 878 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 879 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 880 | self.assertEqual(zipf.comment, comment) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 881 | |
| 882 | # check a comment of max length |
| 883 | comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)]) |
| 884 | comment2 = comment2.encode("ascii") |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 885 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 886 | zipf.comment = comment2 |
| 887 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 888 | |
| 889 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 890 | self.assertEqual(zipfr.comment, comment2) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 891 | |
| 892 | # check a comment that is too long is truncated |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 893 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 894 | zipf.comment = comment2 + b'oops' |
| 895 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 896 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 897 | self.assertEqual(zipfr.comment, comment2) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 898 | |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 899 | def check_testzip_with_bad_crc(self, compression): |
| 900 | """Tests that files with bad CRCs return their name from testzip.""" |
| 901 | zipdata = self.zips_with_bad_crc[compression] |
| 902 | |
| 903 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 904 | # testzip returns the name of the first corrupt file, or None |
| 905 | self.assertEqual('afile', zipf.testzip()) |
| 906 | |
| 907 | def test_testzip_with_bad_crc_stored(self): |
| 908 | self.check_testzip_with_bad_crc(zipfile.ZIP_STORED) |
| 909 | |
| 910 | @skipUnless(zlib, "requires zlib") |
| 911 | def test_testzip_with_bad_crc_deflated(self): |
| 912 | self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED) |
| 913 | |
| 914 | def check_read_with_bad_crc(self, compression): |
| 915 | """Tests that files with bad CRCs raise a BadZipfile exception when read.""" |
| 916 | zipdata = self.zips_with_bad_crc[compression] |
| 917 | |
| 918 | # Using ZipFile.read() |
| 919 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 920 | self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile') |
| 921 | |
| 922 | # Using ZipExtFile.read() |
| 923 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 924 | with zipf.open('afile', 'r') as corrupt_file: |
| 925 | self.assertRaises(zipfile.BadZipfile, corrupt_file.read) |
| 926 | |
| 927 | # Same with small reads (in order to exercise the buffering logic) |
| 928 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 929 | with zipf.open('afile', 'r') as corrupt_file: |
| 930 | corrupt_file.MIN_READ_SIZE = 2 |
| 931 | with self.assertRaises(zipfile.BadZipfile): |
| 932 | while corrupt_file.read(2): |
| 933 | pass |
| 934 | |
| 935 | def test_read_with_bad_crc_stored(self): |
| 936 | self.check_read_with_bad_crc(zipfile.ZIP_STORED) |
| 937 | |
| 938 | @skipUnless(zlib, "requires zlib") |
| 939 | def test_read_with_bad_crc_deflated(self): |
| 940 | self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED) |
| 941 | |
Antoine Pitrou | 6464d5f | 2010-09-12 14:51:20 +0000 | [diff] [blame] | 942 | def check_read_return_size(self, compression): |
| 943 | # Issue #9837: ZipExtFile.read() shouldn't return more bytes |
| 944 | # than requested. |
| 945 | for test_size in (1, 4095, 4096, 4097, 16384): |
| 946 | file_size = test_size + 1 |
| 947 | junk = b''.join(struct.pack('B', randint(0, 255)) |
| 948 | for x in range(file_size)) |
| 949 | with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf: |
| 950 | zipf.writestr('foo', junk) |
| 951 | with zipf.open('foo', 'r') as fp: |
| 952 | buf = fp.read(test_size) |
| 953 | self.assertEqual(len(buf), test_size) |
| 954 | |
| 955 | def test_read_return_size_stored(self): |
| 956 | self.check_read_return_size(zipfile.ZIP_STORED) |
| 957 | |
| 958 | @skipUnless(zlib, "requires zlib") |
| 959 | def test_read_return_size_deflated(self): |
| 960 | self.check_read_return_size(zipfile.ZIP_DEFLATED) |
| 961 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 962 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 963 | unlink(TESTFN) |
| 964 | unlink(TESTFN2) |
| 965 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 966 | |
| 967 | class DecryptionTests(unittest.TestCase): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 968 | """Check that ZIP decryption works. Since the library does not |
| 969 | support encryption at the moment, we use a pre-generated encrypted |
| 970 | ZIP file.""" |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 971 | |
| 972 | data = ( |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 973 | b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00' |
| 974 | b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y' |
| 975 | b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl' |
| 976 | b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00' |
| 977 | b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81' |
| 978 | b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00' |
| 979 | b'\x00\x00L\x00\x00\x00\x00\x00' ) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 980 | data2 = ( |
| 981 | b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02' |
| 982 | b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04' |
| 983 | b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0' |
| 984 | b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03' |
| 985 | b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00' |
| 986 | b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze' |
| 987 | b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01' |
| 988 | b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' ) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 989 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 990 | plain = b'zipfile.py encryption test' |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 991 | plain2 = b'\x00'*512 |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 992 | |
| 993 | def setUp(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 994 | with open(TESTFN, "wb") as fp: |
| 995 | fp.write(self.data) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 996 | self.zip = zipfile.ZipFile(TESTFN, "r") |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 997 | with open(TESTFN2, "wb") as fp: |
| 998 | fp.write(self.data2) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 999 | self.zip2 = zipfile.ZipFile(TESTFN2, "r") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1000 | |
| 1001 | def tearDown(self): |
| 1002 | self.zip.close() |
| 1003 | os.unlink(TESTFN) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1004 | self.zip2.close() |
| 1005 | os.unlink(TESTFN2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1006 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1007 | def test_no_password(self): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1008 | # Reading the encrypted file without password |
| 1009 | # must generate a RunTime exception |
| 1010 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1011 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1012 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1013 | def test_bad_password(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1014 | self.zip.setpassword(b"perl") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1015 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1016 | self.zip2.setpassword(b"perl") |
| 1017 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1018 | |
Ezio Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 1019 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1020 | def test_good_password(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1021 | self.zip.setpassword(b"python") |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1022 | self.assertEqual(self.zip.read("test.txt"), self.plain) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1023 | self.zip2.setpassword(b"12345") |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1024 | self.assertEqual(self.zip2.read("zero"), self.plain2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1025 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1026 | |
| 1027 | class TestsWithRandomBinaryFiles(unittest.TestCase): |
| 1028 | def setUp(self): |
| 1029 | datacount = randint(16, 64)*1024 + randint(1, 1024) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1030 | self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000)) |
| 1031 | for i in range(datacount)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1032 | |
| 1033 | # Make a source file with some lines |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1034 | with open(TESTFN, "wb") as fp: |
| 1035 | fp.write(self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1036 | |
| 1037 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1038 | unlink(TESTFN) |
| 1039 | unlink(TESTFN2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1040 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1041 | def make_test_archive(self, f, compression): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1042 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1043 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 1044 | zipfp.write(TESTFN, "another.name") |
| 1045 | zipfp.write(TESTFN, TESTFN) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1046 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1047 | def zip_test(self, f, compression): |
| 1048 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1049 | |
| 1050 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1051 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1052 | testdata = zipfp.read(TESTFN) |
| 1053 | self.assertEqual(len(testdata), len(self.data)) |
| 1054 | self.assertEqual(testdata, self.data) |
| 1055 | self.assertEqual(zipfp.read("another.name"), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1056 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1057 | def test_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1058 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1059 | self.zip_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1060 | |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1061 | @skipUnless(zlib, "requires zlib") |
| 1062 | def test_deflated(self): |
| 1063 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1064 | self.zip_test(f, zipfile.ZIP_DEFLATED) |
| 1065 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1066 | def zip_open_test(self, f, compression): |
| 1067 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1068 | |
| 1069 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1070 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1071 | zipdata1 = [] |
| 1072 | zipopen1 = zipfp.open(TESTFN) |
| 1073 | while True: |
| 1074 | read_data = zipopen1.read(256) |
| 1075 | if not read_data: |
| 1076 | break |
| 1077 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1078 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1079 | zipdata2 = [] |
| 1080 | zipopen2 = zipfp.open("another.name") |
| 1081 | while True: |
| 1082 | read_data = zipopen2.read(256) |
| 1083 | if not read_data: |
| 1084 | break |
| 1085 | zipdata2.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1086 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1087 | testdata1 = b''.join(zipdata1) |
| 1088 | self.assertEqual(len(testdata1), len(self.data)) |
| 1089 | self.assertEqual(testdata1, self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1090 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1091 | testdata2 = b''.join(zipdata2) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1092 | self.assertEqual(len(testdata2), len(self.data)) |
| 1093 | self.assertEqual(testdata2, self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1094 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1095 | def test_open_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1096 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1097 | self.zip_open_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1098 | |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1099 | @skipUnless(zlib, "requires zlib") |
| 1100 | def test_open_deflated(self): |
| 1101 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1102 | self.zip_open_test(f, zipfile.ZIP_DEFLATED) |
| 1103 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1104 | def zip_random_open_test(self, f, compression): |
| 1105 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1106 | |
| 1107 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1108 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1109 | zipdata1 = [] |
| 1110 | zipopen1 = zipfp.open(TESTFN) |
| 1111 | while True: |
| 1112 | read_data = zipopen1.read(randint(1, 1024)) |
| 1113 | if not read_data: |
| 1114 | break |
| 1115 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1116 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1117 | testdata = b''.join(zipdata1) |
| 1118 | self.assertEqual(len(testdata), len(self.data)) |
| 1119 | self.assertEqual(testdata, self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1120 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1121 | def test_random_open_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1122 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1123 | self.zip_random_open_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1124 | |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1125 | @skipUnless(zlib, "requires zlib") |
| 1126 | def test_random_open_deflated(self): |
| 1127 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1128 | self.zip_random_open_test(f, zipfile.ZIP_DEFLATED) |
| 1129 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1130 | |
Ezio Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 1131 | @skipUnless(zlib, "requires zlib") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1132 | class TestsWithMultipleOpens(unittest.TestCase): |
| 1133 | def setUp(self): |
| 1134 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1135 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp: |
| 1136 | zipfp.writestr('ones', '1'*FIXEDTEST_SIZE) |
| 1137 | zipfp.writestr('twos', '2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1138 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1139 | def test_same_file(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1140 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1141 | # multiple open() calls can be made without interfering with each other. |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1142 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 1143 | zopen1 = zipf.open('ones') |
| 1144 | zopen2 = zipf.open('ones') |
| 1145 | data1 = zopen1.read(500) |
| 1146 | data2 = zopen2.read(500) |
| 1147 | data1 += zopen1.read(500) |
| 1148 | data2 += zopen2.read(500) |
| 1149 | self.assertEqual(data1, data2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1150 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1151 | def test_different_file(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1152 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1153 | # multiple open() calls can be made without interfering with each other. |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1154 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 1155 | zopen1 = zipf.open('ones') |
| 1156 | zopen2 = zipf.open('twos') |
| 1157 | data1 = zopen1.read(500) |
| 1158 | data2 = zopen2.read(500) |
| 1159 | data1 += zopen1.read(500) |
| 1160 | data2 += zopen2.read(500) |
| 1161 | self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) |
| 1162 | self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1163 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1164 | def test_interleaved(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1165 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1166 | # multiple open() calls can be made without interfering with each other. |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1167 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 1168 | zopen1 = zipf.open('ones') |
| 1169 | data1 = zopen1.read(500) |
| 1170 | zopen2 = zipf.open('twos') |
| 1171 | data2 = zopen2.read(500) |
| 1172 | data1 += zopen1.read(500) |
| 1173 | data2 += zopen2.read(500) |
| 1174 | self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) |
| 1175 | self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1176 | |
| 1177 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1178 | unlink(TESTFN2) |
| 1179 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1180 | |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1181 | class TestWithDirectory(unittest.TestCase): |
| 1182 | def setUp(self): |
| 1183 | os.mkdir(TESTFN2) |
| 1184 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1185 | def test_extract_dir(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1186 | with zipfile.ZipFile(findfile("zipdir.zip")) as zipf: |
| 1187 | zipf.extractall(TESTFN2) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1188 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a"))) |
| 1189 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b"))) |
| 1190 | self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c"))) |
| 1191 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1192 | def test_bug_6050(self): |
Martin v. Löwis | 70ccd16 | 2009-05-24 19:47:22 +0000 | [diff] [blame] | 1193 | # Extraction should succeed if directories already exist |
| 1194 | os.mkdir(os.path.join(TESTFN2, "a")) |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1195 | self.test_extract_dir() |
Martin v. Löwis | 70ccd16 | 2009-05-24 19:47:22 +0000 | [diff] [blame] | 1196 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1197 | def test_store_dir(self): |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1198 | os.mkdir(os.path.join(TESTFN2, "x")) |
| 1199 | zipf = zipfile.ZipFile(TESTFN, "w") |
| 1200 | zipf.write(os.path.join(TESTFN2, "x"), "x") |
| 1201 | self.assertTrue(zipf.filelist[0].filename.endswith("x/")) |
| 1202 | |
| 1203 | def tearDown(self): |
| 1204 | shutil.rmtree(TESTFN2) |
| 1205 | if os.path.exists(TESTFN): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1206 | unlink(TESTFN) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1207 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1208 | |
| 1209 | class UniversalNewlineTests(unittest.TestCase): |
| 1210 | def setUp(self): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 1211 | self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1212 | for i in range(FIXEDTEST_SIZE)] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1213 | self.seps = ('\r', '\r\n', '\n') |
| 1214 | self.arcdata, self.arcfiles = {}, {} |
| 1215 | for n, s in enumerate(self.seps): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1216 | b = s.encode("ascii") |
| 1217 | self.arcdata[s] = b.join(self.line_gen) + b |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1218 | self.arcfiles[s] = '%s-%d' % (TESTFN, n) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1219 | f = open(self.arcfiles[s], "wb") |
| 1220 | try: |
| 1221 | f.write(self.arcdata[s]) |
| 1222 | finally: |
| 1223 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1224 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1225 | def make_test_archive(self, f, compression): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1226 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1227 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 1228 | for fn in self.arcfiles.values(): |
| 1229 | zipfp.write(fn, fn) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1230 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1231 | def read_test(self, f, compression): |
| 1232 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1233 | |
| 1234 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1235 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1236 | for sep, fn in self.arcfiles.items(): |
| 1237 | zipdata = zipfp.open(fn, "rU").read() |
| 1238 | self.assertEqual(self.arcdata[sep], zipdata) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1239 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 1240 | def readline_read_test(self, f, compression): |
| 1241 | self.make_test_archive(f, compression) |
| 1242 | |
| 1243 | # Read the ZIP archive |
| 1244 | zipfp = zipfile.ZipFile(f, "r") |
| 1245 | for sep, fn in self.arcfiles.items(): |
| 1246 | zipopen = zipfp.open(fn, "rU") |
| 1247 | data = b'' |
| 1248 | while True: |
| 1249 | read = zipopen.readline() |
| 1250 | if not read: |
| 1251 | break |
| 1252 | data += read |
| 1253 | |
| 1254 | read = zipopen.read(5) |
| 1255 | if not read: |
| 1256 | break |
| 1257 | data += read |
| 1258 | |
| 1259 | self.assertEqual(data, self.arcdata['\n']) |
| 1260 | |
| 1261 | zipfp.close() |
| 1262 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1263 | def readline_test(self, f, compression): |
| 1264 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1265 | |
| 1266 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1267 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1268 | for sep, fn in self.arcfiles.items(): |
| 1269 | zipopen = zipfp.open(fn, "rU") |
| 1270 | for line in self.line_gen: |
| 1271 | linedata = zipopen.readline() |
| 1272 | self.assertEqual(linedata, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1273 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1274 | def readlines_test(self, f, compression): |
| 1275 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1276 | |
| 1277 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1278 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1279 | for sep, fn in self.arcfiles.items(): |
| 1280 | ziplines = zipfp.open(fn, "rU").readlines() |
| 1281 | for line, zipline in zip(self.line_gen, ziplines): |
| 1282 | self.assertEqual(zipline, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1283 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1284 | def iterlines_test(self, f, compression): |
| 1285 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1286 | |
| 1287 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1288 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1289 | for sep, fn in self.arcfiles.items(): |
| 1290 | for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")): |
| 1291 | self.assertEqual(zipline, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1292 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1293 | def test_read_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1294 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1295 | self.read_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1296 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 1297 | def test_readline_read_stored(self): |
| 1298 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 1299 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1300 | self.readline_read_test(f, zipfile.ZIP_STORED) |
| 1301 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1302 | def test_readline_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1303 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1304 | self.readline_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1305 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1306 | def test_readlines_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1307 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1308 | self.readlines_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1309 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1310 | def test_iterlines_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1311 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1312 | self.iterlines_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1313 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1314 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1315 | def test_read_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1316 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1317 | self.read_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1318 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1319 | @skipUnless(zlib, "requires zlib") |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 1320 | def test_readline_read_deflated(self): |
| 1321 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 1322 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1323 | self.readline_read_test(f, zipfile.ZIP_DEFLATED) |
| 1324 | |
| 1325 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1326 | def test_readline_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1327 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1328 | self.readline_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1329 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1330 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1331 | def test_readlines_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1332 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1333 | self.readlines_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1334 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1335 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1336 | def test_iterlines_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1337 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1338 | self.iterlines_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1339 | |
| 1340 | def tearDown(self): |
| 1341 | for sep, fn in self.arcfiles.items(): |
| 1342 | os.remove(fn) |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1343 | unlink(TESTFN) |
| 1344 | unlink(TESTFN2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1345 | |
| 1346 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1347 | def test_main(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1348 | run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests, |
| 1349 | PyZipFileTests, DecryptionTests, TestsWithMultipleOpens, |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1350 | TestWithDirectory, UniversalNewlineTests, |
| 1351 | TestsWithRandomBinaryFiles) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1352 | |
| 1353 | if __name__ == "__main__": |
| 1354 | test_main() |