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