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