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