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 | |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 16 | from test.support import TESTFN, run_unittest, findfile, unlink, requires_zlib, requires_bz2, requires_lzma |
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 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 316 | @requires_bz2 |
| 317 | def test_bzip2(self): |
| 318 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 319 | self.zip_test(f, zipfile.ZIP_BZIP2) |
| 320 | |
| 321 | @requires_bz2 |
| 322 | def test_open_bzip2(self): |
| 323 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 324 | self.zip_open_test(f, zipfile.ZIP_BZIP2) |
| 325 | |
| 326 | @requires_bz2 |
| 327 | def test_random_open_bzip2(self): |
| 328 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 329 | self.zip_random_open_test(f, zipfile.ZIP_BZIP2) |
| 330 | |
| 331 | @requires_bz2 |
| 332 | def test_readline_read_bzip2(self): |
| 333 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 334 | self.zip_readline_read_test(f, zipfile.ZIP_BZIP2) |
| 335 | |
| 336 | @requires_bz2 |
| 337 | def test_readline_bzip2(self): |
| 338 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 339 | self.zip_readline_test(f, zipfile.ZIP_BZIP2) |
| 340 | |
| 341 | @requires_bz2 |
| 342 | def test_readlines_bzip2(self): |
| 343 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 344 | self.zip_readlines_test(f, zipfile.ZIP_BZIP2) |
| 345 | |
| 346 | @requires_bz2 |
| 347 | def test_iterlines_bzip2(self): |
| 348 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 349 | self.zip_iterlines_test(f, zipfile.ZIP_BZIP2) |
| 350 | |
| 351 | @requires_bz2 |
| 352 | def test_low_compression_bzip2(self): |
| 353 | """Check for cases where compressed data is larger than original.""" |
| 354 | # Create the ZIP archive |
| 355 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_BZIP2) as zipfp: |
| 356 | zipfp.writestr("strfile", '12') |
| 357 | |
| 358 | # Get an open object for strfile |
| 359 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_BZIP2) as zipfp: |
| 360 | with zipfp.open("strfile") as openobj: |
| 361 | self.assertEqual(openobj.read(1), b'1') |
| 362 | self.assertEqual(openobj.read(1), b'2') |
| 363 | |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 364 | @requires_lzma |
| 365 | def test_lzma(self): |
| 366 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 367 | self.zip_test(f, zipfile.ZIP_LZMA) |
| 368 | |
| 369 | @requires_lzma |
| 370 | def test_open_lzma(self): |
| 371 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 372 | self.zip_open_test(f, zipfile.ZIP_LZMA) |
| 373 | |
| 374 | @requires_lzma |
| 375 | def test_random_open_lzma(self): |
| 376 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 377 | self.zip_random_open_test(f, zipfile.ZIP_LZMA) |
| 378 | |
| 379 | @requires_lzma |
| 380 | def test_readline_read_lzma(self): |
| 381 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 382 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 383 | self.zip_readline_read_test(f, zipfile.ZIP_LZMA) |
| 384 | |
| 385 | @requires_lzma |
| 386 | def test_readline_lzma(self): |
| 387 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 388 | self.zip_readline_test(f, zipfile.ZIP_LZMA) |
| 389 | |
| 390 | @requires_lzma |
| 391 | def test_readlines_lzma(self): |
| 392 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 393 | self.zip_readlines_test(f, zipfile.ZIP_LZMA) |
| 394 | |
| 395 | @requires_lzma |
| 396 | def test_iterlines_lzma(self): |
| 397 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 398 | self.zip_iterlines_test(f, zipfile.ZIP_LZMA) |
| 399 | |
| 400 | @requires_lzma |
| 401 | def test_low_compression_lzma(self): |
| 402 | """Check for cases where compressed data is larger than original.""" |
| 403 | # Create the ZIP archive |
| 404 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_LZMA) as zipfp: |
| 405 | zipfp.writestr("strfile", '12') |
| 406 | |
| 407 | # Get an open object for strfile |
| 408 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_LZMA) as zipfp: |
| 409 | with zipfp.open("strfile") as openobj: |
| 410 | self.assertEqual(openobj.read(1), b'1') |
| 411 | self.assertEqual(openobj.read(1), b'2') |
| 412 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 413 | def test_absolute_arcnames(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 414 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 415 | zipfp.write(TESTFN, "/absolute") |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame] | 416 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 417 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 418 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
Tim Peters | 32cbc96 | 2006-02-20 21:42:18 +0000 | [diff] [blame] | 419 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 420 | def test_append_to_zip_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 421 | """Test appending to an existing zipfile.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 422 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 423 | zipfp.write(TESTFN, TESTFN) |
| 424 | |
| 425 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 426 | zipfp.writestr("strfile", self.data) |
| 427 | self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"]) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 428 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 429 | def test_append_to_non_zip_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 430 | """Test appending to an existing file that is not a zipfile.""" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 431 | # NOTE: this test fails if len(d) < 22 because of the first |
| 432 | # line "fpin.seek(-22, 2)" in _EndRecData |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 433 | data = b'I am not a ZipFile!'*10 |
| 434 | with open(TESTFN2, 'wb') as f: |
| 435 | f.write(data) |
| 436 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 437 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 438 | zipfp.write(TESTFN, TESTFN) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 439 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 440 | with open(TESTFN2, 'rb') as f: |
| 441 | f.seek(len(data)) |
| 442 | with zipfile.ZipFile(f, "r") as zipfp: |
| 443 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 444 | |
R David Murray | 4fbb9db | 2011-06-09 15:50:51 -0400 | [diff] [blame] | 445 | def test_ignores_newline_at_end(self): |
| 446 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 447 | zipfp.write(TESTFN, TESTFN) |
| 448 | with open(TESTFN2, 'a') as f: |
| 449 | f.write("\r\n\00\00\00") |
| 450 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 451 | self.assertIsInstance(zipfp, zipfile.ZipFile) |
| 452 | |
| 453 | def test_ignores_stuff_appended_past_comments(self): |
| 454 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 455 | zipfp.comment = b"this is a comment" |
| 456 | zipfp.write(TESTFN, TESTFN) |
| 457 | with open(TESTFN2, 'a') as f: |
| 458 | f.write("abcdef\r\n") |
| 459 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 460 | self.assertIsInstance(zipfp, zipfile.ZipFile) |
| 461 | self.assertEqual(zipfp.comment, b"this is a comment") |
| 462 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 463 | def test_write_default_name(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 464 | """Check that calling ZipFile.write without arcname specified |
| 465 | produces the expected result.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 466 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 467 | zipfp.write(TESTFN) |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 468 | with open(TESTFN, "rb") as f: |
| 469 | self.assertEqual(zipfp.read(TESTFN), f.read()) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 470 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 471 | @requires_zlib |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 472 | def test_per_file_compression(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 473 | """Check that files within a Zip archive can have different |
| 474 | compression options.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 475 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 476 | zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED) |
| 477 | zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED) |
| 478 | sinfo = zipfp.getinfo('storeme') |
| 479 | dinfo = zipfp.getinfo('deflateme') |
| 480 | self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED) |
| 481 | self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 482 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 483 | def test_write_to_readonly(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 484 | """Check that trying to call write() on a readonly ZipFile object |
| 485 | raises a RuntimeError.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 486 | with zipfile.ZipFile(TESTFN2, mode="w") as zipfp: |
| 487 | zipfp.writestr("somefile.txt", "bogus") |
| 488 | |
| 489 | with zipfile.ZipFile(TESTFN2, mode="r") as zipfp: |
| 490 | self.assertRaises(RuntimeError, zipfp.write, TESTFN) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 491 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 492 | def test_extract(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 493 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 494 | for fpath, fdata in SMALL_TEST_DATA: |
| 495 | zipfp.writestr(fpath, fdata) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 496 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 497 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 498 | for fpath, fdata in SMALL_TEST_DATA: |
| 499 | writtenfile = zipfp.extract(fpath) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 500 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 501 | # make sure it was written to the right place |
| 502 | if os.path.isabs(fpath): |
| 503 | correctfile = os.path.join(os.getcwd(), fpath[1:]) |
| 504 | else: |
| 505 | correctfile = os.path.join(os.getcwd(), fpath) |
| 506 | correctfile = os.path.normpath(correctfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 507 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 508 | self.assertEqual(writtenfile, correctfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 509 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 510 | # make sure correct data is in correct file |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 511 | with open(writtenfile, "rb") as f: |
| 512 | self.assertEqual(fdata.encode(), f.read()) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 513 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 514 | os.remove(writtenfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 515 | |
| 516 | # remove the test file subdirectories |
| 517 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 518 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 519 | def test_extract_all(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 520 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 521 | for fpath, fdata in SMALL_TEST_DATA: |
| 522 | zipfp.writestr(fpath, fdata) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 523 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 524 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 525 | zipfp.extractall() |
| 526 | for fpath, fdata in SMALL_TEST_DATA: |
| 527 | if os.path.isabs(fpath): |
| 528 | outfile = os.path.join(os.getcwd(), fpath[1:]) |
| 529 | else: |
| 530 | outfile = os.path.join(os.getcwd(), fpath) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 531 | |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 532 | with open(outfile, "rb") as f: |
| 533 | self.assertEqual(fdata.encode(), f.read()) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 534 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 535 | os.remove(outfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 536 | |
| 537 | # remove the test file subdirectories |
| 538 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 539 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 540 | def test_writestr_compression_stored(self): |
Ronald Oussoren | ee5c885 | 2010-02-07 20:24:02 +0000 | [diff] [blame] | 541 | zipfp = zipfile.ZipFile(TESTFN2, "w") |
| 542 | zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED) |
Ronald Oussoren | ee5c885 | 2010-02-07 20:24:02 +0000 | [diff] [blame] | 543 | info = zipfp.getinfo('a.txt') |
| 544 | self.assertEqual(info.compress_type, zipfile.ZIP_STORED) |
| 545 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 546 | @requires_zlib |
| 547 | def test_writestr_compression_deflated(self): |
| 548 | zipfp = zipfile.ZipFile(TESTFN2, "w") |
| 549 | zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED) |
| 550 | info = zipfp.getinfo('b.txt') |
| 551 | self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED) |
Ronald Oussoren | ee5c885 | 2010-02-07 20:24:02 +0000 | [diff] [blame] | 552 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 553 | @requires_bz2 |
| 554 | def test_writestr_compression_bzip2(self): |
| 555 | zipfp = zipfile.ZipFile(TESTFN2, "w") |
| 556 | zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_BZIP2) |
| 557 | info = zipfp.getinfo('b.txt') |
| 558 | self.assertEqual(info.compress_type, zipfile.ZIP_BZIP2) |
| 559 | |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 560 | @requires_lzma |
| 561 | def test_writestr_compression_lzma(self): |
| 562 | zipfp = zipfile.ZipFile(TESTFN2, "w") |
| 563 | zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_LZMA) |
| 564 | info = zipfp.getinfo('b.txt') |
| 565 | self.assertEqual(info.compress_type, zipfile.ZIP_LZMA) |
| 566 | |
Antoine Pitrou | 6e1df8d | 2008-07-25 19:58:18 +0000 | [diff] [blame] | 567 | def zip_test_writestr_permissions(self, f, compression): |
| 568 | # Make sure that writestr creates files with mode 0600, |
| 569 | # when it is passed a name rather than a ZipInfo instance. |
| 570 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 571 | self.make_test_archive(f, compression) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 572 | with zipfile.ZipFile(f, "r") as zipfp: |
| 573 | zinfo = zipfp.getinfo('strfile') |
| 574 | self.assertEqual(zinfo.external_attr, 0o600 << 16) |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 575 | if not isinstance(f, str): |
| 576 | f.close() |
Antoine Pitrou | 6e1df8d | 2008-07-25 19:58:18 +0000 | [diff] [blame] | 577 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 578 | def test_writestr_permissions(self): |
Antoine Pitrou | 6e1df8d | 2008-07-25 19:58:18 +0000 | [diff] [blame] | 579 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 580 | self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED) |
| 581 | |
Gregory P. Smith | b0d9ca9 | 2009-07-07 05:06:04 +0000 | [diff] [blame] | 582 | def test_writestr_extended_local_header_issue1202(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 583 | with zipfile.ZipFile(TESTFN2, 'w') as orig_zip: |
| 584 | for data in 'abcdefghijklmnop': |
| 585 | zinfo = zipfile.ZipInfo(data) |
| 586 | zinfo.flag_bits |= 0x08 # Include an extended local header. |
| 587 | orig_zip.writestr(zinfo, data) |
| 588 | |
| 589 | def test_close(self): |
| 590 | """Check that the zipfile is closed after the 'with' block.""" |
| 591 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 592 | for fpath, fdata in SMALL_TEST_DATA: |
| 593 | zipfp.writestr(fpath, fdata) |
| 594 | self.assertTrue(zipfp.fp is not None, 'zipfp is not open') |
| 595 | self.assertTrue(zipfp.fp is None, 'zipfp is not closed') |
| 596 | |
| 597 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 598 | self.assertTrue(zipfp.fp is not None, 'zipfp is not open') |
| 599 | self.assertTrue(zipfp.fp is None, 'zipfp is not closed') |
| 600 | |
| 601 | def test_close_on_exception(self): |
| 602 | """Check that the zipfile is closed if an exception is raised in the |
| 603 | 'with' block.""" |
| 604 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 605 | for fpath, fdata in SMALL_TEST_DATA: |
| 606 | zipfp.writestr(fpath, fdata) |
| 607 | |
| 608 | try: |
| 609 | with zipfile.ZipFile(TESTFN2, "r") as zipfp2: |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 610 | raise zipfile.BadZipFile() |
| 611 | except zipfile.BadZipFile: |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 612 | self.assertTrue(zipfp2.fp is None, 'zipfp is not closed') |
Gregory P. Smith | b0d9ca9 | 2009-07-07 05:06:04 +0000 | [diff] [blame] | 613 | |
Senthil Kumaran | 29fa9d4 | 2011-10-20 01:46:00 +0800 | [diff] [blame] | 614 | def test_add_file_before_1980(self): |
| 615 | # Set atime and mtime to 1970-01-01 |
| 616 | os.utime(TESTFN, (0, 0)) |
| 617 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 618 | self.assertRaises(ValueError, zipfp.write, TESTFN) |
| 619 | |
| 620 | |
Senthil Kumaran | 7e3062b | 2011-10-20 01:52:41 +0800 | [diff] [blame] | 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 626 | @requires_zlib |
Georg Brandl | 5ba11de | 2011-01-01 10:09:32 +0000 | [diff] [blame] | 627 | def test_unicode_filenames(self): |
Georg Brandl | 04480a8 | 2011-01-01 10:42:31 +0000 | [diff] [blame] | 628 | # bug #10801 |
| 629 | fname = findfile('zip_cp437_header.zip') |
Georg Brandl | 5ba11de | 2011-01-01 10:09:32 +0000 | [diff] [blame] | 630 | with zipfile.ZipFile(fname) as zipfp: |
Georg Brandl | 04480a8 | 2011-01-01 10:42:31 +0000 | [diff] [blame] | 631 | for name in zipfp.namelist(): |
| 632 | zipfp.open(name).close() |
Georg Brandl | 5ba11de | 2011-01-01 10:09:32 +0000 | [diff] [blame] | 633 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 634 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 635 | unlink(TESTFN) |
| 636 | unlink(TESTFN2) |
| 637 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 638 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 639 | class TestZip64InSmallFiles(unittest.TestCase): |
| 640 | # These tests test the ZIP64 functionality without using large files, |
| 641 | # see test_zipfile64 for proper tests. |
| 642 | |
| 643 | def setUp(self): |
| 644 | self._limit = zipfile.ZIP64_LIMIT |
| 645 | zipfile.ZIP64_LIMIT = 5 |
| 646 | |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 647 | line_gen = (bytes("Test of zipfile line %d." % i, "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 648 | for i in range(0, FIXEDTEST_SIZE)) |
| 649 | self.data = b'\n'.join(line_gen) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 650 | |
| 651 | # Make a source file with some lines |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 652 | with open(TESTFN, "wb") as fp: |
| 653 | fp.write(self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 654 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 655 | def large_file_exception_test(self, f, compression): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 656 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 657 | self.assertRaises(zipfile.LargeZipFile, |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 658 | zipfp.write, TESTFN, "another.name") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 659 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 660 | def large_file_exception_test2(self, f, compression): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 661 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 662 | self.assertRaises(zipfile.LargeZipFile, |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 663 | zipfp.writestr, "another.name", self.data) |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 664 | if not isinstance(f, str): |
| 665 | f.close() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 666 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 667 | def test_large_file_exception(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 668 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 669 | self.large_file_exception_test(f, zipfile.ZIP_STORED) |
| 670 | self.large_file_exception_test2(f, zipfile.ZIP_STORED) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 671 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 672 | def zip_test(self, f, compression): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 673 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 674 | with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp: |
| 675 | zipfp.write(TESTFN, "another.name") |
| 676 | zipfp.write(TESTFN, TESTFN) |
| 677 | zipfp.writestr("strfile", self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 678 | |
| 679 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 680 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 681 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 682 | self.assertEqual(zipfp.read("another.name"), self.data) |
| 683 | self.assertEqual(zipfp.read("strfile"), self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 684 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 685 | # Print the ZIP directory |
| 686 | fp = io.StringIO() |
| 687 | zipfp.printdir(fp) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 688 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 689 | directory = fp.getvalue() |
| 690 | lines = directory.splitlines() |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 691 | self.assertEqual(len(lines), 4) # Number of files + header |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 692 | |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 693 | self.assertIn('File Name', lines[0]) |
| 694 | self.assertIn('Modified', lines[0]) |
| 695 | self.assertIn('Size', lines[0]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 696 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 697 | fn, date, time_, size = lines[1].split() |
| 698 | self.assertEqual(fn, 'another.name') |
| 699 | self.assertTrue(time.strptime(date, '%Y-%m-%d')) |
| 700 | self.assertTrue(time.strptime(time_, '%H:%M:%S')) |
| 701 | self.assertEqual(size, str(len(self.data))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 702 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 703 | # Check the namelist |
| 704 | names = zipfp.namelist() |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 705 | self.assertEqual(len(names), 3) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 706 | self.assertIn(TESTFN, names) |
| 707 | self.assertIn("another.name", names) |
| 708 | self.assertIn("strfile", names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 709 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 710 | # Check infolist |
| 711 | infos = zipfp.infolist() |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 712 | names = [i.filename for i in infos] |
| 713 | self.assertEqual(len(names), 3) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 714 | self.assertIn(TESTFN, names) |
| 715 | self.assertIn("another.name", names) |
| 716 | self.assertIn("strfile", names) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 717 | for i in infos: |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 718 | self.assertEqual(i.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 719 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 720 | # check getinfo |
| 721 | for nm in (TESTFN, "another.name", "strfile"): |
| 722 | info = zipfp.getinfo(nm) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 723 | self.assertEqual(info.filename, nm) |
| 724 | self.assertEqual(info.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 725 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 726 | # Check that testzip doesn't raise an exception |
| 727 | zipfp.testzip() |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 728 | if not isinstance(f, str): |
| 729 | f.close() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 730 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 731 | def test_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 732 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 733 | self.zip_test(f, zipfile.ZIP_STORED) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 734 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 735 | @requires_zlib |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 736 | def test_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 737 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 738 | self.zip_test(f, zipfile.ZIP_DEFLATED) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 739 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 740 | @requires_bz2 |
| 741 | def test_bzip2(self): |
| 742 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 743 | self.zip_test(f, zipfile.ZIP_BZIP2) |
| 744 | |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 745 | @requires_lzma |
| 746 | def test_lzma(self): |
| 747 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 748 | self.zip_test(f, zipfile.ZIP_LZMA) |
| 749 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 750 | def test_absolute_arcnames(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 751 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, |
| 752 | allowZip64=True) as zipfp: |
| 753 | zipfp.write(TESTFN, "/absolute") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 754 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 755 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 756 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 757 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 758 | def tearDown(self): |
| 759 | zipfile.ZIP64_LIMIT = self._limit |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 760 | unlink(TESTFN) |
| 761 | unlink(TESTFN2) |
| 762 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 763 | |
| 764 | class PyZipFileTests(unittest.TestCase): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 765 | def test_write_pyfile(self): |
Łukasz Langa | a9f054b | 2010-11-23 00:15:02 +0000 | [diff] [blame] | 766 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 767 | fn = __file__ |
| 768 | if fn.endswith('.pyc') or fn.endswith('.pyo'): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 769 | path_split = fn.split(os.sep) |
| 770 | if os.altsep is not None: |
| 771 | path_split.extend(fn.split(os.altsep)) |
| 772 | if '__pycache__' in path_split: |
| 773 | fn = imp.source_from_cache(fn) |
| 774 | else: |
| 775 | fn = fn[:-1] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 776 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 777 | zipfp.writepy(fn) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 778 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 779 | bn = os.path.basename(fn) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 780 | self.assertNotIn(bn, zipfp.namelist()) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 781 | self.assertTrue(bn + 'o' in zipfp.namelist() or |
| 782 | bn + 'c' in zipfp.namelist()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 783 | |
Łukasz Langa | a9f054b | 2010-11-23 00:15:02 +0000 | [diff] [blame] | 784 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 785 | fn = __file__ |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 786 | if fn.endswith(('.pyc', '.pyo')): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 787 | fn = fn[:-1] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 788 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 789 | zipfp.writepy(fn, "testpackage") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 790 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 791 | bn = "%s/%s" % ("testpackage", os.path.basename(fn)) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 792 | self.assertNotIn(bn, zipfp.namelist()) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 793 | self.assertTrue(bn + 'o' in zipfp.namelist() or |
| 794 | bn + 'c' in zipfp.namelist()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 795 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 796 | def test_write_python_package(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 797 | import email |
| 798 | packagedir = os.path.dirname(email.__file__) |
| 799 | |
Łukasz Langa | a9f054b | 2010-11-23 00:15:02 +0000 | [diff] [blame] | 800 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 801 | zipfp.writepy(packagedir) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 802 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 803 | # Check for a couple of modules at different levels of the |
| 804 | # hierarchy |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 805 | names = zipfp.namelist() |
| 806 | self.assertTrue('email/__init__.pyo' in names or |
| 807 | 'email/__init__.pyc' in names) |
| 808 | self.assertTrue('email/mime/text.pyo' in names or |
| 809 | 'email/mime/text.pyc' in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 810 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 811 | def test_write_with_optimization(self): |
| 812 | import email |
| 813 | packagedir = os.path.dirname(email.__file__) |
| 814 | # use .pyc if running test in optimization mode, |
| 815 | # use .pyo if running test in debug mode |
| 816 | optlevel = 1 if __debug__ else 0 |
| 817 | ext = '.pyo' if optlevel == 1 else '.pyc' |
| 818 | |
| 819 | with TemporaryFile() as t, \ |
| 820 | zipfile.PyZipFile(t, "w", optimize=optlevel) as zipfp: |
| 821 | zipfp.writepy(packagedir) |
| 822 | |
| 823 | names = zipfp.namelist() |
| 824 | self.assertIn('email/__init__' + ext, names) |
| 825 | self.assertIn('email/mime/text' + ext, names) |
| 826 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 827 | def test_write_python_directory(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 828 | os.mkdir(TESTFN2) |
| 829 | try: |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 830 | with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: |
| 831 | fp.write("print(42)\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 832 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 833 | with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: |
| 834 | fp.write("print(42 * 42)\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 835 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 836 | with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: |
| 837 | fp.write("bla bla bla\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 838 | |
Łukasz Langa | a9f054b | 2010-11-23 00:15:02 +0000 | [diff] [blame] | 839 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 840 | zipfp.writepy(TESTFN2) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 841 | |
Łukasz Langa | a9f054b | 2010-11-23 00:15:02 +0000 | [diff] [blame] | 842 | names = zipfp.namelist() |
| 843 | self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names) |
| 844 | self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names) |
| 845 | self.assertNotIn('mod2.txt', names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 846 | |
| 847 | finally: |
| 848 | shutil.rmtree(TESTFN2) |
| 849 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 850 | def test_write_non_pyfile(self): |
Łukasz Langa | a9f054b | 2010-11-23 00:15:02 +0000 | [diff] [blame] | 851 | with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 852 | with open(TESTFN, 'w') as f: |
| 853 | f.write('most definitely not a python file') |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 854 | self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) |
| 855 | os.remove(TESTFN) |
| 856 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 857 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 858 | class OtherTests(unittest.TestCase): |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 859 | zips_with_bad_crc = { |
| 860 | zipfile.ZIP_STORED: ( |
| 861 | b'PK\003\004\024\0\0\0\0\0 \213\212;:r' |
| 862 | b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af' |
| 863 | b'ilehello,AworldP' |
| 864 | b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:' |
| 865 | b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0' |
| 866 | b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi' |
| 867 | b'lePK\005\006\0\0\0\0\001\0\001\0003\000' |
| 868 | b'\0\0/\0\0\0\0\0'), |
| 869 | zipfile.ZIP_DEFLATED: ( |
| 870 | b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA' |
| 871 | b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' |
| 872 | b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0' |
| 873 | b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n' |
| 874 | b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05' |
| 875 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00' |
| 876 | b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00' |
| 877 | b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'), |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 878 | zipfile.ZIP_BZIP2: ( |
| 879 | b'PK\x03\x04\x14\x03\x00\x00\x0c\x00nu\x0c=FA' |
| 880 | b'KE8\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' |
| 881 | b'ileBZh91AY&SY\xd4\xa8\xca' |
| 882 | b'\x7f\x00\x00\x0f\x11\x80@\x00\x06D\x90\x80 \x00 \xa5' |
| 883 | b'P\xd9!\x03\x03\x13\x13\x13\x89\xa9\xa9\xc2u5:\x9f' |
| 884 | b'\x8b\xb9"\x9c(HjTe?\x80PK\x01\x02\x14' |
| 885 | b'\x03\x14\x03\x00\x00\x0c\x00nu\x0c=FAKE8' |
| 886 | b'\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00' |
| 887 | b'\x00 \x80\x80\x81\x00\x00\x00\x00afilePK' |
| 888 | b'\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00\x00[\x00' |
| 889 | b'\x00\x00\x00\x00'), |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 890 | zipfile.ZIP_LZMA: ( |
| 891 | b'PK\x03\x04\x14\x03\x00\x00\x0e\x00nu\x0c=FA' |
| 892 | b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' |
| 893 | b'ile\t\x04\x05\x00]\x00\x00\x00\x04\x004\x19I' |
| 894 | b'\xee\x8d\xe9\x17\x89:3`\tq!.8\x00PK' |
| 895 | b'\x01\x02\x14\x03\x14\x03\x00\x00\x0e\x00nu\x0c=FA' |
| 896 | b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00' |
| 897 | b'\x00\x00\x00\x00 \x80\x80\x81\x00\x00\x00\x00afil' |
| 898 | b'ePK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00' |
| 899 | b'\x00>\x00\x00\x00\x00\x00'), |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Martin v. Löwis | d099b56 | 2012-05-01 14:08:22 +0200 | [diff] [blame] | 902 | def test_unsupported_version(self): |
| 903 | # File has an extract_version of 120 |
| 904 | data = (b'PK\x03\x04x\x00\x00\x00\x00\x00!p\xa1@\x00\x00\x00\x00\x00\x00' |
| 905 | b'\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00xPK\x01\x02x\x03x\x00\x00\x00\x00' |
| 906 | b'\x00!p\xa1@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00' |
| 907 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00\x00xPK\x05\x06' |
| 908 | b'\x00\x00\x00\x00\x01\x00\x01\x00/\x00\x00\x00\x1f\x00\x00\x00\x00\x00') |
| 909 | self.assertRaises(NotImplementedError, zipfile.ZipFile, |
| 910 | io.BytesIO(data), 'r') |
| 911 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 912 | def test_unicode_filenames(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 913 | with zipfile.ZipFile(TESTFN, "w") as zf: |
| 914 | zf.writestr("foo.txt", "Test for unicode filename") |
| 915 | zf.writestr("\xf6.txt", "Test for unicode filename") |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 916 | self.assertIsInstance(zf.infolist()[0].filename, str) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 917 | |
| 918 | with zipfile.ZipFile(TESTFN, "r") as zf: |
| 919 | self.assertEqual(zf.filelist[0].filename, "foo.txt") |
| 920 | self.assertEqual(zf.filelist[1].filename, "\xf6.txt") |
Martin v. Löwis | 8570f6a | 2008-05-05 17:44:38 +0000 | [diff] [blame] | 921 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 922 | def test_create_non_existent_file_for_append(self): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 923 | if os.path.exists(TESTFN): |
| 924 | os.unlink(TESTFN) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 925 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 926 | filename = 'testfile.txt' |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 927 | content = b'hello, world. this is some content.' |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 928 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 929 | try: |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 930 | with zipfile.ZipFile(TESTFN, 'a') as zf: |
| 931 | zf.writestr(filename, content) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 932 | except IOError: |
| 933 | self.fail('Could not append data to a non-existent zip file.') |
| 934 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 935 | self.assertTrue(os.path.exists(TESTFN)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 936 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 937 | with zipfile.ZipFile(TESTFN, 'r') as zf: |
| 938 | self.assertEqual(zf.read(filename), content) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 939 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 940 | def test_close_erroneous_file(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 941 | # This test checks that the ZipFile constructor closes the file object |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 942 | # it opens if there's an error in the file. If it doesn't, the |
| 943 | # traceback holds a reference to the ZipFile object and, indirectly, |
| 944 | # the file object. |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 945 | # On Windows, this causes the os.unlink() call to fail because the |
| 946 | # underlying file is still open. This is SF bug #412214. |
| 947 | # |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 948 | with open(TESTFN, "w") as fp: |
| 949 | fp.write("this is not a legal zip file\n") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 950 | try: |
| 951 | zf = zipfile.ZipFile(TESTFN) |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 952 | except zipfile.BadZipFile: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 953 | pass |
| 954 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 955 | def test_is_zip_erroneous_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 956 | """Check that is_zipfile() correctly identifies non-zip files.""" |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 957 | # - passing a filename |
| 958 | with open(TESTFN, "w") as fp: |
| 959 | fp.write("this is not a legal zip file\n") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 960 | chk = zipfile.is_zipfile(TESTFN) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 961 | self.assertFalse(chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 962 | # - passing a file object |
| 963 | with open(TESTFN, "rb") as fp: |
| 964 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 965 | self.assertTrue(not chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 966 | # - passing a file-like object |
| 967 | fp = io.BytesIO() |
| 968 | fp.write(b"this is not a legal zip file\n") |
| 969 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 970 | self.assertTrue(not chk) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 971 | fp.seek(0, 0) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 972 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 973 | self.assertTrue(not chk) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 974 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 975 | def test_is_zip_valid_file(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 976 | """Check that is_zipfile() correctly identifies zip files.""" |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 977 | # - passing a filename |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 978 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 979 | zipf.writestr("foo.txt", b"O, for a Muse of Fire!") |
| 980 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 981 | chk = zipfile.is_zipfile(TESTFN) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 982 | self.assertTrue(chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 983 | # - passing a file object |
| 984 | with open(TESTFN, "rb") as fp: |
| 985 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 986 | self.assertTrue(chk) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 987 | fp.seek(0, 0) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 988 | zip_contents = fp.read() |
| 989 | # - passing a file-like object |
| 990 | fp = io.BytesIO() |
| 991 | fp.write(zip_contents) |
| 992 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 993 | self.assertTrue(chk) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 994 | fp.seek(0, 0) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 995 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 996 | self.assertTrue(chk) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 997 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 998 | def test_non_existent_file_raises_IOError(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 999 | # make sure we don't raise an AttributeError when a partially-constructed |
| 1000 | # ZipFile instance is finalized; this tests for regression on SF tracker |
| 1001 | # bug #403871. |
| 1002 | |
| 1003 | # The bug we're testing for caused an AttributeError to be raised |
| 1004 | # when a ZipFile instance was created for a file that did not |
| 1005 | # exist; the .fp member was not initialized but was needed by the |
| 1006 | # __del__() method. Since the AttributeError is in the __del__(), |
| 1007 | # it is ignored, but the user should be sufficiently annoyed by |
| 1008 | # the message on the output that regression will be noticed |
| 1009 | # quickly. |
| 1010 | self.assertRaises(IOError, zipfile.ZipFile, TESTFN) |
| 1011 | |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 1012 | def test_empty_file_raises_BadZipFile(self): |
| 1013 | f = open(TESTFN, 'w') |
| 1014 | f.close() |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1015 | self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN) |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 1016 | |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1017 | with open(TESTFN, 'w') as fp: |
| 1018 | fp.write("short file") |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1019 | self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN) |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 1020 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1021 | def test_closed_zip_raises_RuntimeError(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1022 | """Verify that testzip() doesn't swallow inappropriate exceptions.""" |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1023 | data = io.BytesIO() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1024 | with zipfile.ZipFile(data, mode="w") as zipf: |
| 1025 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1026 | |
| 1027 | # This is correct; calling .read on a closed ZipFile should throw |
| 1028 | # a RuntimeError, and so should calling .testzip. An earlier |
| 1029 | # version of .testzip would swallow this exception (and any other) |
| 1030 | # and report that the first file in the archive was corrupt. |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1031 | self.assertRaises(RuntimeError, zipf.read, "foo.txt") |
| 1032 | self.assertRaises(RuntimeError, zipf.open, "foo.txt") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1033 | self.assertRaises(RuntimeError, zipf.testzip) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1034 | self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus") |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1035 | with open(TESTFN, 'w') as f: |
| 1036 | f.write('zipfile test data') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1037 | self.assertRaises(RuntimeError, zipf.write, TESTFN) |
| 1038 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1039 | def test_bad_constructor_mode(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1040 | """Check that bad modes passed to ZipFile constructor are caught.""" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1041 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q") |
| 1042 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1043 | def test_bad_open_mode(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1044 | """Check that bad modes passed to ZipFile.open are caught.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1045 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1046 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1047 | |
| 1048 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1049 | # read the data to make sure the file is there |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1050 | zipf.read("foo.txt") |
| 1051 | self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1052 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1053 | def test_read0(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1054 | """Check that calling read(0) on a ZipExtFile object returns an empty |
| 1055 | string and doesn't advance file pointer.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1056 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1057 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1058 | # read the data to make sure the file is there |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1059 | with zipf.open("foo.txt") as f: |
| 1060 | for i in range(FIXEDTEST_SIZE): |
| 1061 | self.assertEqual(f.read(0), b'') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1062 | |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1063 | self.assertEqual(f.read(), b"O, for a Muse of Fire!") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1064 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1065 | def test_open_non_existent_item(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1066 | """Check that attempting to call open() for an item that doesn't |
| 1067 | exist in the archive raises a RuntimeError.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1068 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1069 | self.assertRaises(KeyError, zipf.open, "foo.txt", "r") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1070 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1071 | def test_bad_compression_mode(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1072 | """Check that bad compression methods passed to ZipFile.open are |
| 1073 | caught.""" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1074 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1) |
| 1075 | |
Martin v. Löwis | b3260f0 | 2012-05-01 08:38:01 +0200 | [diff] [blame] | 1076 | def test_unsupported_compression(self): |
| 1077 | # data is declared as shrunk, but actually deflated |
| 1078 | data = (b'PK\x03\x04.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00' |
| 1079 | b'\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00x\x03\x00PK\x01' |
| 1080 | b'\x02.\x03.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00\x00\x02\x00\x00' |
| 1081 | b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 1082 | b'\x80\x01\x00\x00\x00\x00xPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00' |
| 1083 | b'/\x00\x00\x00!\x00\x00\x00\x00\x00') |
| 1084 | with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf: |
| 1085 | self.assertRaises(NotImplementedError, zipf.open, 'x') |
| 1086 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1087 | def test_null_byte_in_filename(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1088 | """Check that a filename containing a null byte is properly |
| 1089 | terminated.""" |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1090 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1091 | zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!") |
| 1092 | self.assertEqual(zipf.namelist(), ['foo.txt']) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1093 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1094 | def test_struct_sizes(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1095 | """Check that ZIP internal structure sizes are calculated correctly.""" |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1096 | self.assertEqual(zipfile.sizeEndCentDir, 22) |
| 1097 | self.assertEqual(zipfile.sizeCentralDir, 46) |
| 1098 | self.assertEqual(zipfile.sizeEndCentDir64, 56) |
| 1099 | self.assertEqual(zipfile.sizeEndCentDir64Locator, 20) |
| 1100 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1101 | def test_comments(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1102 | """Check that comments on the archive are handled properly.""" |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1103 | |
| 1104 | # check default comment is empty |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1105 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1106 | self.assertEqual(zipf.comment, b'') |
| 1107 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1108 | |
| 1109 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 1110 | self.assertEqual(zipfr.comment, b'') |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1111 | |
| 1112 | # check a simple short comment |
| 1113 | 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] | 1114 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1115 | zipf.comment = comment |
| 1116 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1117 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 1118 | self.assertEqual(zipf.comment, comment) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1119 | |
| 1120 | # check a comment of max length |
| 1121 | comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)]) |
| 1122 | comment2 = comment2.encode("ascii") |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1123 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1124 | zipf.comment = comment2 |
| 1125 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1126 | |
| 1127 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 1128 | self.assertEqual(zipfr.comment, comment2) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1129 | |
| 1130 | # check a comment that is too long is truncated |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1131 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1132 | zipf.comment = comment2 + b'oops' |
| 1133 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1134 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 1135 | self.assertEqual(zipfr.comment, comment2) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1136 | |
Antoine Pitrou | c399185 | 2012-06-30 17:31:37 +0200 | [diff] [blame] | 1137 | # check that comments are correctly modified in append mode |
| 1138 | with zipfile.ZipFile(TESTFN,mode="w") as zipf: |
| 1139 | zipf.comment = b"original comment" |
| 1140 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1141 | with zipfile.ZipFile(TESTFN,mode="a") as zipf: |
| 1142 | zipf.comment = b"an updated comment" |
| 1143 | with zipfile.ZipFile(TESTFN,mode="r") as zipf: |
| 1144 | self.assertEqual(zipf.comment, b"an updated comment") |
| 1145 | |
| 1146 | # check that comments are correctly shortened in append mode |
| 1147 | with zipfile.ZipFile(TESTFN,mode="w") as zipf: |
| 1148 | zipf.comment = b"original comment that's longer" |
| 1149 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1150 | with zipfile.ZipFile(TESTFN,mode="a") as zipf: |
| 1151 | zipf.comment = b"shorter comment" |
| 1152 | with zipfile.ZipFile(TESTFN,mode="r") as zipf: |
| 1153 | self.assertEqual(zipf.comment, b"shorter comment") |
| 1154 | |
R David Murray | f50b38a | 2012-04-12 18:44:58 -0400 | [diff] [blame] | 1155 | def test_unicode_comment(self): |
| 1156 | with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf: |
| 1157 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1158 | with self.assertRaises(TypeError): |
| 1159 | zipf.comment = "this is an error" |
| 1160 | |
| 1161 | def test_change_comment_in_empty_archive(self): |
| 1162 | with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf: |
| 1163 | self.assertFalse(zipf.filelist) |
| 1164 | zipf.comment = b"this is a comment" |
| 1165 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 1166 | self.assertEqual(zipf.comment, b"this is a comment") |
| 1167 | |
| 1168 | def test_change_comment_in_nonempty_archive(self): |
| 1169 | with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf: |
| 1170 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1171 | with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf: |
| 1172 | self.assertTrue(zipf.filelist) |
| 1173 | zipf.comment = b"this is a comment" |
| 1174 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 1175 | self.assertEqual(zipf.comment, b"this is a comment") |
| 1176 | |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1177 | def check_testzip_with_bad_crc(self, compression): |
| 1178 | """Tests that files with bad CRCs return their name from testzip.""" |
| 1179 | zipdata = self.zips_with_bad_crc[compression] |
| 1180 | |
| 1181 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 1182 | # testzip returns the name of the first corrupt file, or None |
| 1183 | self.assertEqual('afile', zipf.testzip()) |
| 1184 | |
| 1185 | def test_testzip_with_bad_crc_stored(self): |
| 1186 | self.check_testzip_with_bad_crc(zipfile.ZIP_STORED) |
| 1187 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1188 | @requires_zlib |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1189 | def test_testzip_with_bad_crc_deflated(self): |
| 1190 | self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED) |
| 1191 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1192 | @requires_bz2 |
| 1193 | def test_testzip_with_bad_crc_bzip2(self): |
| 1194 | self.check_testzip_with_bad_crc(zipfile.ZIP_BZIP2) |
| 1195 | |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 1196 | @requires_lzma |
| 1197 | def test_testzip_with_bad_crc_lzma(self): |
| 1198 | self.check_testzip_with_bad_crc(zipfile.ZIP_LZMA) |
| 1199 | |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1200 | def check_read_with_bad_crc(self, compression): |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1201 | """Tests that files with bad CRCs raise a BadZipFile exception when read.""" |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1202 | zipdata = self.zips_with_bad_crc[compression] |
| 1203 | |
| 1204 | # Using ZipFile.read() |
| 1205 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1206 | self.assertRaises(zipfile.BadZipFile, zipf.read, 'afile') |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1207 | |
| 1208 | # Using ZipExtFile.read() |
| 1209 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 1210 | with zipf.open('afile', 'r') as corrupt_file: |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1211 | self.assertRaises(zipfile.BadZipFile, corrupt_file.read) |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1212 | |
| 1213 | # Same with small reads (in order to exercise the buffering logic) |
| 1214 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 1215 | with zipf.open('afile', 'r') as corrupt_file: |
| 1216 | corrupt_file.MIN_READ_SIZE = 2 |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1217 | with self.assertRaises(zipfile.BadZipFile): |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1218 | while corrupt_file.read(2): |
| 1219 | pass |
| 1220 | |
| 1221 | def test_read_with_bad_crc_stored(self): |
| 1222 | self.check_read_with_bad_crc(zipfile.ZIP_STORED) |
| 1223 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1224 | @requires_zlib |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1225 | def test_read_with_bad_crc_deflated(self): |
| 1226 | self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED) |
| 1227 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1228 | @requires_bz2 |
| 1229 | def test_read_with_bad_crc_bzip2(self): |
| 1230 | self.check_read_with_bad_crc(zipfile.ZIP_BZIP2) |
| 1231 | |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 1232 | @requires_lzma |
| 1233 | def test_read_with_bad_crc_lzma(self): |
| 1234 | self.check_read_with_bad_crc(zipfile.ZIP_LZMA) |
| 1235 | |
Antoine Pitrou | 6464d5f | 2010-09-12 14:51:20 +0000 | [diff] [blame] | 1236 | def check_read_return_size(self, compression): |
| 1237 | # Issue #9837: ZipExtFile.read() shouldn't return more bytes |
| 1238 | # than requested. |
| 1239 | for test_size in (1, 4095, 4096, 4097, 16384): |
| 1240 | file_size = test_size + 1 |
| 1241 | junk = b''.join(struct.pack('B', randint(0, 255)) |
| 1242 | for x in range(file_size)) |
| 1243 | with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf: |
| 1244 | zipf.writestr('foo', junk) |
| 1245 | with zipf.open('foo', 'r') as fp: |
| 1246 | buf = fp.read(test_size) |
| 1247 | self.assertEqual(len(buf), test_size) |
| 1248 | |
| 1249 | def test_read_return_size_stored(self): |
| 1250 | self.check_read_return_size(zipfile.ZIP_STORED) |
| 1251 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1252 | @requires_zlib |
Antoine Pitrou | 6464d5f | 2010-09-12 14:51:20 +0000 | [diff] [blame] | 1253 | def test_read_return_size_deflated(self): |
| 1254 | self.check_read_return_size(zipfile.ZIP_DEFLATED) |
| 1255 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1256 | @requires_bz2 |
| 1257 | def test_read_return_size_bzip2(self): |
| 1258 | self.check_read_return_size(zipfile.ZIP_BZIP2) |
| 1259 | |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 1260 | @requires_lzma |
| 1261 | def test_read_return_size_lzma(self): |
| 1262 | self.check_read_return_size(zipfile.ZIP_LZMA) |
| 1263 | |
Georg Brandl | 268e4d4 | 2010-10-14 06:59:45 +0000 | [diff] [blame] | 1264 | def test_empty_zipfile(self): |
| 1265 | # Check that creating a file in 'w' or 'a' mode and closing without |
| 1266 | # adding any files to the archives creates a valid empty ZIP file |
| 1267 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 1268 | zipf.close() |
| 1269 | try: |
| 1270 | zipf = zipfile.ZipFile(TESTFN, mode="r") |
| 1271 | except zipfile.BadZipFile: |
| 1272 | self.fail("Unable to create empty ZIP file in 'w' mode") |
| 1273 | |
| 1274 | zipf = zipfile.ZipFile(TESTFN, mode="a") |
| 1275 | zipf.close() |
| 1276 | try: |
| 1277 | zipf = zipfile.ZipFile(TESTFN, mode="r") |
| 1278 | except: |
| 1279 | self.fail("Unable to create empty ZIP file in 'a' mode") |
| 1280 | |
| 1281 | def test_open_empty_file(self): |
| 1282 | # Issue 1710703: Check that opening a file with less than 22 bytes |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1283 | # raises a BadZipFile exception (rather than the previously unhelpful |
Georg Brandl | 268e4d4 | 2010-10-14 06:59:45 +0000 | [diff] [blame] | 1284 | # IOError) |
| 1285 | f = open(TESTFN, 'w') |
| 1286 | f.close() |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1287 | self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r') |
Georg Brandl | 268e4d4 | 2010-10-14 06:59:45 +0000 | [diff] [blame] | 1288 | |
Senthil Kumaran | 29fa9d4 | 2011-10-20 01:46:00 +0800 | [diff] [blame] | 1289 | def test_create_zipinfo_before_1980(self): |
| 1290 | self.assertRaises(ValueError, |
| 1291 | zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0)) |
| 1292 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1293 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1294 | unlink(TESTFN) |
| 1295 | unlink(TESTFN2) |
| 1296 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1297 | |
| 1298 | class DecryptionTests(unittest.TestCase): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1299 | """Check that ZIP decryption works. Since the library does not |
| 1300 | support encryption at the moment, we use a pre-generated encrypted |
| 1301 | ZIP file.""" |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1302 | |
| 1303 | data = ( |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1304 | b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00' |
| 1305 | b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y' |
| 1306 | b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl' |
| 1307 | b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00' |
| 1308 | b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81' |
| 1309 | b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00' |
| 1310 | b'\x00\x00L\x00\x00\x00\x00\x00' ) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1311 | data2 = ( |
| 1312 | b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02' |
| 1313 | b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04' |
| 1314 | b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0' |
| 1315 | b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03' |
| 1316 | b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00' |
| 1317 | b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze' |
| 1318 | b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01' |
| 1319 | b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' ) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1320 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1321 | plain = b'zipfile.py encryption test' |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1322 | plain2 = b'\x00'*512 |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1323 | |
| 1324 | def setUp(self): |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1325 | with open(TESTFN, "wb") as fp: |
| 1326 | fp.write(self.data) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1327 | self.zip = zipfile.ZipFile(TESTFN, "r") |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1328 | with open(TESTFN2, "wb") as fp: |
| 1329 | fp.write(self.data2) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1330 | self.zip2 = zipfile.ZipFile(TESTFN2, "r") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1331 | |
| 1332 | def tearDown(self): |
| 1333 | self.zip.close() |
| 1334 | os.unlink(TESTFN) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1335 | self.zip2.close() |
| 1336 | os.unlink(TESTFN2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1337 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1338 | def test_no_password(self): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1339 | # Reading the encrypted file without password |
| 1340 | # must generate a RunTime exception |
| 1341 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1342 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1343 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1344 | def test_bad_password(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1345 | self.zip.setpassword(b"perl") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1346 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1347 | self.zip2.setpassword(b"perl") |
| 1348 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1349 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1350 | @requires_zlib |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1351 | def test_good_password(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1352 | self.zip.setpassword(b"python") |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1353 | self.assertEqual(self.zip.read("test.txt"), self.plain) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1354 | self.zip2.setpassword(b"12345") |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1355 | self.assertEqual(self.zip2.read("zero"), self.plain2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1356 | |
R. David Murray | 8d855d8 | 2010-12-21 21:53:37 +0000 | [diff] [blame] | 1357 | def test_unicode_password(self): |
| 1358 | self.assertRaises(TypeError, self.zip.setpassword, "unicode") |
| 1359 | self.assertRaises(TypeError, self.zip.read, "test.txt", "python") |
| 1360 | self.assertRaises(TypeError, self.zip.open, "test.txt", pwd="python") |
| 1361 | self.assertRaises(TypeError, self.zip.extract, "test.txt", pwd="python") |
| 1362 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1363 | |
| 1364 | class TestsWithRandomBinaryFiles(unittest.TestCase): |
| 1365 | def setUp(self): |
| 1366 | datacount = randint(16, 64)*1024 + randint(1, 1024) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1367 | self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000)) |
| 1368 | for i in range(datacount)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1369 | |
| 1370 | # Make a source file with some lines |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1371 | with open(TESTFN, "wb") as fp: |
| 1372 | fp.write(self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1373 | |
| 1374 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1375 | unlink(TESTFN) |
| 1376 | unlink(TESTFN2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1377 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1378 | def make_test_archive(self, f, compression): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1379 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1380 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 1381 | zipfp.write(TESTFN, "another.name") |
| 1382 | zipfp.write(TESTFN, TESTFN) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1383 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1384 | def zip_test(self, f, compression): |
| 1385 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1386 | |
| 1387 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1388 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1389 | testdata = zipfp.read(TESTFN) |
| 1390 | self.assertEqual(len(testdata), len(self.data)) |
| 1391 | self.assertEqual(testdata, self.data) |
| 1392 | self.assertEqual(zipfp.read("another.name"), self.data) |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 1393 | if not isinstance(f, str): |
| 1394 | f.close() |
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_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.zip_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1399 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1400 | @requires_zlib |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1401 | def test_deflated(self): |
| 1402 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1403 | self.zip_test(f, zipfile.ZIP_DEFLATED) |
| 1404 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1405 | @requires_bz2 |
| 1406 | def test_bzip2(self): |
| 1407 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1408 | self.zip_test(f, zipfile.ZIP_BZIP2) |
| 1409 | |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 1410 | @requires_lzma |
| 1411 | def test_lzma(self): |
| 1412 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1413 | self.zip_test(f, zipfile.ZIP_LZMA) |
| 1414 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1415 | def zip_open_test(self, f, compression): |
| 1416 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1417 | |
| 1418 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1419 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1420 | zipdata1 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1421 | with zipfp.open(TESTFN) as zipopen1: |
| 1422 | while True: |
| 1423 | read_data = zipopen1.read(256) |
| 1424 | if not read_data: |
| 1425 | break |
| 1426 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1427 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1428 | zipdata2 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1429 | with zipfp.open("another.name") as zipopen2: |
| 1430 | while True: |
| 1431 | read_data = zipopen2.read(256) |
| 1432 | if not read_data: |
| 1433 | break |
| 1434 | zipdata2.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1435 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1436 | testdata1 = b''.join(zipdata1) |
| 1437 | self.assertEqual(len(testdata1), len(self.data)) |
| 1438 | self.assertEqual(testdata1, self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1439 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1440 | testdata2 = b''.join(zipdata2) |
Ezio Melotti | 3538671 | 2009-12-31 13:22:41 +0000 | [diff] [blame] | 1441 | self.assertEqual(len(testdata2), len(self.data)) |
| 1442 | self.assertEqual(testdata2, self.data) |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 1443 | if not isinstance(f, str): |
| 1444 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1445 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1446 | def test_open_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1447 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1448 | self.zip_open_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1449 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1450 | @requires_zlib |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1451 | def test_open_deflated(self): |
| 1452 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1453 | self.zip_open_test(f, zipfile.ZIP_DEFLATED) |
| 1454 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1455 | @requires_bz2 |
| 1456 | def test_open_bzip2(self): |
| 1457 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1458 | self.zip_open_test(f, zipfile.ZIP_BZIP2) |
| 1459 | |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 1460 | @requires_lzma |
| 1461 | def test_open_lzma(self): |
| 1462 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1463 | self.zip_open_test(f, zipfile.ZIP_LZMA) |
| 1464 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1465 | def zip_random_open_test(self, f, compression): |
| 1466 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1467 | |
| 1468 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1469 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1470 | zipdata1 = [] |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1471 | with zipfp.open(TESTFN) as zipopen1: |
| 1472 | while True: |
| 1473 | read_data = zipopen1.read(randint(1, 1024)) |
| 1474 | if not read_data: |
| 1475 | break |
| 1476 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1477 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1478 | testdata = b''.join(zipdata1) |
| 1479 | self.assertEqual(len(testdata), len(self.data)) |
| 1480 | self.assertEqual(testdata, self.data) |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 1481 | if not isinstance(f, str): |
| 1482 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1483 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1484 | def test_random_open_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1485 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1486 | self.zip_random_open_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1487 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1488 | @requires_zlib |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1489 | def test_random_open_deflated(self): |
| 1490 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1491 | self.zip_random_open_test(f, zipfile.ZIP_DEFLATED) |
| 1492 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1493 | @requires_bz2 |
| 1494 | def test_random_open_bzip2(self): |
| 1495 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1496 | self.zip_random_open_test(f, zipfile.ZIP_BZIP2) |
| 1497 | |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 1498 | @requires_lzma |
| 1499 | def test_random_open_lzma(self): |
| 1500 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1501 | self.zip_random_open_test(f, zipfile.ZIP_LZMA) |
| 1502 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1503 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1504 | @requires_zlib |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1505 | class TestsWithMultipleOpens(unittest.TestCase): |
| 1506 | def setUp(self): |
| 1507 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1508 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp: |
| 1509 | zipfp.writestr('ones', '1'*FIXEDTEST_SIZE) |
| 1510 | zipfp.writestr('twos', '2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1511 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1512 | def test_same_file(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1513 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1514 | # multiple open() calls can be made without interfering with each other. |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1515 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1516 | with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2: |
| 1517 | data1 = zopen1.read(500) |
| 1518 | data2 = zopen2.read(500) |
| 1519 | data1 += zopen1.read(500) |
| 1520 | data2 += zopen2.read(500) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1521 | self.assertEqual(data1, data2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1522 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1523 | def test_different_file(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1524 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1525 | # multiple open() calls can be made without interfering with each other. |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1526 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1527 | with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2: |
| 1528 | data1 = zopen1.read(500) |
| 1529 | data2 = zopen2.read(500) |
| 1530 | data1 += zopen1.read(500) |
| 1531 | data2 += zopen2.read(500) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1532 | self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) |
| 1533 | self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1534 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1535 | def test_interleaved(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1536 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1537 | # multiple open() calls can be made without interfering with each other. |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1538 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1539 | with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2: |
| 1540 | data1 = zopen1.read(500) |
| 1541 | data2 = zopen2.read(500) |
| 1542 | data1 += zopen1.read(500) |
| 1543 | data2 += zopen2.read(500) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1544 | self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) |
| 1545 | self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1546 | |
| 1547 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1548 | unlink(TESTFN2) |
| 1549 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1550 | |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1551 | class TestWithDirectory(unittest.TestCase): |
| 1552 | def setUp(self): |
| 1553 | os.mkdir(TESTFN2) |
| 1554 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1555 | def test_extract_dir(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1556 | with zipfile.ZipFile(findfile("zipdir.zip")) as zipf: |
| 1557 | zipf.extractall(TESTFN2) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1558 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a"))) |
| 1559 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b"))) |
| 1560 | self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c"))) |
| 1561 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1562 | def test_bug_6050(self): |
Martin v. Löwis | 70ccd16 | 2009-05-24 19:47:22 +0000 | [diff] [blame] | 1563 | # Extraction should succeed if directories already exist |
| 1564 | os.mkdir(os.path.join(TESTFN2, "a")) |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1565 | self.test_extract_dir() |
Martin v. Löwis | 70ccd16 | 2009-05-24 19:47:22 +0000 | [diff] [blame] | 1566 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1567 | def test_store_dir(self): |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1568 | os.mkdir(os.path.join(TESTFN2, "x")) |
| 1569 | zipf = zipfile.ZipFile(TESTFN, "w") |
| 1570 | zipf.write(os.path.join(TESTFN2, "x"), "x") |
| 1571 | self.assertTrue(zipf.filelist[0].filename.endswith("x/")) |
| 1572 | |
| 1573 | def tearDown(self): |
| 1574 | shutil.rmtree(TESTFN2) |
| 1575 | if os.path.exists(TESTFN): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1576 | unlink(TESTFN) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1577 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1578 | |
| 1579 | class UniversalNewlineTests(unittest.TestCase): |
| 1580 | def setUp(self): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 1581 | self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1582 | for i in range(FIXEDTEST_SIZE)] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1583 | self.seps = ('\r', '\r\n', '\n') |
| 1584 | self.arcdata, self.arcfiles = {}, {} |
| 1585 | for n, s in enumerate(self.seps): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1586 | b = s.encode("ascii") |
| 1587 | self.arcdata[s] = b.join(self.line_gen) + b |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1588 | self.arcfiles[s] = '%s-%d' % (TESTFN, n) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1589 | f = open(self.arcfiles[s], "wb") |
| 1590 | try: |
| 1591 | f.write(self.arcdata[s]) |
| 1592 | finally: |
| 1593 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1594 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1595 | def make_test_archive(self, f, compression): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1596 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1597 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 1598 | for fn in self.arcfiles.values(): |
| 1599 | zipfp.write(fn, fn) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1600 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1601 | def read_test(self, f, compression): |
| 1602 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1603 | |
| 1604 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1605 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1606 | for sep, fn in self.arcfiles.items(): |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 1607 | with zipfp.open(fn, "rU") as fp: |
| 1608 | zipdata = fp.read() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1609 | self.assertEqual(self.arcdata[sep], zipdata) |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 1610 | if not isinstance(f, str): |
| 1611 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1612 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 1613 | def readline_read_test(self, f, compression): |
| 1614 | self.make_test_archive(f, compression) |
| 1615 | |
| 1616 | # Read the ZIP archive |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1617 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1618 | for sep, fn in self.arcfiles.items(): |
| 1619 | with zipfp.open(fn, "rU") as zipopen: |
| 1620 | data = b'' |
| 1621 | while True: |
| 1622 | read = zipopen.readline() |
| 1623 | if not read: |
| 1624 | break |
| 1625 | data += read |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 1626 | |
Brian Curtin | 8fb9b86 | 2010-11-18 02:15:28 +0000 | [diff] [blame] | 1627 | read = zipopen.read(5) |
| 1628 | if not read: |
| 1629 | break |
| 1630 | data += read |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 1631 | |
| 1632 | self.assertEqual(data, self.arcdata['\n']) |
| 1633 | |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 1634 | if not isinstance(f, str): |
| 1635 | f.close() |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 1636 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1637 | def readline_test(self, f, compression): |
| 1638 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1639 | |
| 1640 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1641 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1642 | for sep, fn in self.arcfiles.items(): |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 1643 | with zipfp.open(fn, "rU") as zipopen: |
| 1644 | for line in self.line_gen: |
| 1645 | linedata = zipopen.readline() |
| 1646 | self.assertEqual(linedata, line + b'\n') |
| 1647 | if not isinstance(f, str): |
| 1648 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1649 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1650 | def readlines_test(self, f, compression): |
| 1651 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1652 | |
| 1653 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1654 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1655 | for sep, fn in self.arcfiles.items(): |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 1656 | with zipfp.open(fn, "rU") as fp: |
| 1657 | ziplines = fp.readlines() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1658 | for line, zipline in zip(self.line_gen, ziplines): |
| 1659 | self.assertEqual(zipline, line + b'\n') |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 1660 | if not isinstance(f, str): |
| 1661 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1662 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1663 | def iterlines_test(self, f, compression): |
| 1664 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1665 | |
| 1666 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 1667 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1668 | for sep, fn in self.arcfiles.items(): |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 1669 | with zipfp.open(fn, "rU") as fp: |
| 1670 | for line, zipline in zip(self.line_gen, fp): |
| 1671 | self.assertEqual(zipline, line + b'\n') |
| 1672 | if not isinstance(f, str): |
| 1673 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1674 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1675 | def test_read_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1676 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1677 | self.read_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 1679 | def test_readline_read_stored(self): |
| 1680 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 1681 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1682 | self.readline_read_test(f, zipfile.ZIP_STORED) |
| 1683 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1684 | def test_readline_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1685 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1686 | self.readline_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1687 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1688 | def test_readlines_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1689 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1690 | self.readlines_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1691 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1692 | def test_iterlines_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1693 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1694 | self.iterlines_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1695 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1696 | @requires_zlib |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1697 | def test_read_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1698 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1699 | self.read_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1700 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1701 | @requires_zlib |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 1702 | def test_readline_read_deflated(self): |
| 1703 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 1704 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1705 | self.readline_read_test(f, zipfile.ZIP_DEFLATED) |
| 1706 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1707 | @requires_zlib |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1708 | def test_readline_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1709 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1710 | self.readline_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1711 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1712 | @requires_zlib |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1713 | def test_readlines_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1714 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1715 | self.readlines_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1716 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 1717 | @requires_zlib |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1718 | def test_iterlines_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1719 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1720 | self.iterlines_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1721 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1722 | @requires_bz2 |
| 1723 | def test_read_bzip2(self): |
| 1724 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1725 | self.read_test(f, zipfile.ZIP_BZIP2) |
| 1726 | |
| 1727 | @requires_bz2 |
| 1728 | def test_readline_read_bzip2(self): |
| 1729 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1730 | self.readline_read_test(f, zipfile.ZIP_BZIP2) |
| 1731 | |
| 1732 | @requires_bz2 |
| 1733 | def test_readline_bzip2(self): |
| 1734 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1735 | self.readline_test(f, zipfile.ZIP_BZIP2) |
| 1736 | |
| 1737 | @requires_bz2 |
| 1738 | def test_readlines_bzip2(self): |
| 1739 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1740 | self.readlines_test(f, zipfile.ZIP_BZIP2) |
| 1741 | |
| 1742 | @requires_bz2 |
| 1743 | def test_iterlines_bzip2(self): |
| 1744 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1745 | self.iterlines_test(f, zipfile.ZIP_BZIP2) |
| 1746 | |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 1747 | @requires_lzma |
| 1748 | def test_read_lzma(self): |
| 1749 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1750 | self.read_test(f, zipfile.ZIP_LZMA) |
| 1751 | |
| 1752 | @requires_lzma |
| 1753 | def test_readline_read_lzma(self): |
| 1754 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1755 | self.readline_read_test(f, zipfile.ZIP_LZMA) |
| 1756 | |
| 1757 | @requires_lzma |
| 1758 | def test_readline_lzma(self): |
| 1759 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1760 | self.readline_test(f, zipfile.ZIP_LZMA) |
| 1761 | |
| 1762 | @requires_lzma |
| 1763 | def test_readlines_lzma(self): |
| 1764 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1765 | self.readlines_test(f, zipfile.ZIP_LZMA) |
| 1766 | |
| 1767 | @requires_lzma |
| 1768 | def test_iterlines_lzma(self): |
| 1769 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1770 | self.iterlines_test(f, zipfile.ZIP_LZMA) |
| 1771 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1772 | def tearDown(self): |
| 1773 | for sep, fn in self.arcfiles.items(): |
| 1774 | os.remove(fn) |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1775 | unlink(TESTFN) |
| 1776 | unlink(TESTFN2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1777 | |
| 1778 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1779 | def test_main(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1780 | run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests, |
| 1781 | PyZipFileTests, DecryptionTests, TestsWithMultipleOpens, |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1782 | TestWithDirectory, UniversalNewlineTests, |
| 1783 | TestsWithRandomBinaryFiles) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1784 | |
| 1785 | if __name__ == "__main__": |
| 1786 | test_main() |