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