Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1 | # We can test part of the module without zlib. |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 2 | try: |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 3 | import zlib |
| 4 | except ImportError: |
| 5 | zlib = None |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 6 | |
| 7 | import io |
| 8 | import os |
| 9 | import shutil |
| 10 | import struct |
| 11 | import zipfile |
| 12 | import unittest |
| 13 | |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 14 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 15 | from tempfile import TemporaryFile |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 16 | from random import randint, random |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 17 | from unittest import skipUnless |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 18 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 19 | from test.support import TESTFN, run_unittest, findfile, unlink |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 20 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 21 | TESTFN2 = TESTFN + "2" |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 22 | TESTFNDIR = TESTFN + "d" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 23 | FIXEDTEST_SIZE = 1000 |
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'), |
| 27 | ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'), |
| 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 |
| 39 | fp = open(TESTFN, "wb") |
| 40 | fp.write(self.data) |
| 41 | fp.close() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 42 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 43 | def make_test_archive(self, f, compression): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 44 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 45 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 46 | zipfp.write(TESTFN, "another.name") |
| 47 | zipfp.write(TESTFN, TESTFN) |
| 48 | zipfp.writestr("strfile", self.data) |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 49 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 50 | def zip_test(self, f, compression): |
| 51 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 52 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 53 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 54 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 55 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 56 | self.assertEqual(zipfp.read("another.name"), self.data) |
| 57 | self.assertEqual(zipfp.read("strfile"), self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 58 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 59 | # Print the ZIP directory |
| 60 | fp = io.StringIO() |
| 61 | zipfp.printdir(file=fp) |
| 62 | directory = fp.getvalue() |
| 63 | lines = directory.splitlines() |
| 64 | self.assertEquals(len(lines), 4) # Number of files + header |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 65 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 66 | self.assertTrue('File Name' in lines[0]) |
| 67 | self.assertTrue('Modified' in lines[0]) |
| 68 | self.assertTrue('Size' in lines[0]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 69 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 70 | fn, date, time, size = lines[1].split() |
| 71 | self.assertEquals(fn, 'another.name') |
| 72 | # XXX: timestamp is not tested |
| 73 | self.assertEquals(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() |
| 77 | self.assertEquals(len(names), 3) |
| 78 | self.assertTrue(TESTFN in names) |
| 79 | self.assertTrue("another.name" in names) |
| 80 | self.assertTrue("strfile" in 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() |
| 84 | names = [ i.filename for i in infos ] |
| 85 | self.assertEquals(len(names), 3) |
| 86 | self.assertTrue(TESTFN in names) |
| 87 | self.assertTrue("another.name" in names) |
| 88 | self.assertTrue("strfile" in names) |
| 89 | for i in infos: |
| 90 | self.assertEquals(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) |
| 95 | self.assertEquals(info.filename, nm) |
| 96 | self.assertEquals(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() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 100 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 101 | def test_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 102 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 103 | self.zip_test(f, zipfile.ZIP_STORED) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 104 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 105 | def zip_open_test(self, f, compression): |
| 106 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 107 | |
| 108 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 109 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 110 | zipdata1 = [] |
| 111 | zipopen1 = zipfp.open(TESTFN) |
| 112 | while True: |
| 113 | read_data = zipopen1.read(256) |
| 114 | if not read_data: |
| 115 | break |
| 116 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 117 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 118 | zipdata2 = [] |
| 119 | zipopen2 = zipfp.open("another.name") |
| 120 | while True: |
| 121 | read_data = zipopen2.read(256) |
| 122 | if not read_data: |
| 123 | break |
| 124 | zipdata2.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 125 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 126 | self.assertEqual(b''.join(zipdata1), self.data) |
| 127 | self.assertEqual(b''.join(zipdata2), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 128 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 129 | def test_open_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 130 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 131 | self.zip_open_test(f, zipfile.ZIP_STORED) |
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_via_zip_info(self): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 134 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 135 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 136 | zipfp.writestr("name", "foo") |
| 137 | zipfp.writestr("name", "bar") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 138 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 139 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 140 | infos = zipfp.infolist() |
| 141 | data = b"" |
| 142 | for info in infos: |
| 143 | data += zipfp.open(info).read() |
| 144 | self.assertTrue(data == b"foobar" or data == b"barfoo") |
| 145 | data = b"" |
| 146 | for info in infos: |
| 147 | data += zipfp.read(info) |
| 148 | self.assertTrue(data == b"foobar" or data == b"barfoo") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 149 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 150 | def zip_random_open_test(self, f, compression): |
| 151 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 152 | |
| 153 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 154 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 155 | zipdata1 = [] |
| 156 | zipopen1 = zipfp.open(TESTFN) |
| 157 | while True: |
| 158 | read_data = zipopen1.read(randint(1, 1024)) |
| 159 | if not read_data: |
| 160 | break |
| 161 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 162 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 163 | self.assertEqual(b''.join(zipdata1), self.data) |
| 164 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 165 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 166 | def test_random_open_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 167 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 168 | self.zip_random_open_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 169 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 170 | def zip_readline_test(self, f, compression): |
| 171 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 172 | |
| 173 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 174 | with zipfile.ZipFile(f, "r") as zipfp: |
| 175 | zipopen = zipfp.open(TESTFN) |
| 176 | for line in self.line_gen: |
| 177 | linedata = zipopen.readline() |
| 178 | self.assertEqual(linedata, line + '\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 179 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 180 | def zip_readlines_test(self, f, compression): |
| 181 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 182 | |
| 183 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 184 | with zipfile.ZipFile(f, "r") as zipfp: |
| 185 | ziplines = zipfp.open(TESTFN).readlines() |
| 186 | for line, zipline in zip(self.line_gen, ziplines): |
| 187 | self.assertEqual(zipline, line + '\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 188 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 189 | def zip_iterlines_test(self, f, compression): |
| 190 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 191 | |
| 192 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 193 | with zipfile.ZipFile(f, "r") as zipfp: |
| 194 | for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)): |
| 195 | self.assertEqual(zipline, line + '\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 196 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 197 | def test_readline_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 198 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 199 | self.zip_readline_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 200 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 201 | def test_readlines_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 202 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 203 | self.zip_readlines_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 204 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 205 | def test_iterlines_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 206 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 207 | self.zip_iterlines_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 208 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 209 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 210 | def test_deflated(self): |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 211 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 212 | self.zip_test(f, zipfile.ZIP_DEFLATED) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 214 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 215 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 216 | def test_open_deflated(self): |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 217 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 218 | self.zip_open_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 219 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 220 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 221 | def test_random_open_deflated(self): |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 222 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 223 | self.zip_random_open_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 224 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 225 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 226 | def test_readline_deflated(self): |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 227 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 228 | self.zip_readline_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 229 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 230 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 231 | def test_readlines_deflated(self): |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 232 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 233 | self.zip_readlines_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 234 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 235 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 236 | def test_iterlines_deflated(self): |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 237 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 238 | self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 239 | |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 240 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 241 | def test_low_compression(self): |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 242 | # Checks for cases where compressed data is larger than original |
| 243 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 244 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp: |
| 245 | zipfp.writestr("strfile", '12') |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 246 | |
| 247 | # Get an open object for strfile |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 248 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp: |
| 249 | openobj = zipfp.open("strfile") |
| 250 | self.assertEqual(openobj.read(1), b'1') |
| 251 | self.assertEqual(openobj.read(1), b'2') |
Ezio Melotti | 74c96ec | 2009-07-08 22:24:06 +0000 | [diff] [blame] | 252 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 253 | def test_absolute_arcnames(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 254 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 255 | zipfp.write(TESTFN, "/absolute") |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame] | 256 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 257 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 258 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
Tim Peters | 32cbc96 | 2006-02-20 21:42:18 +0000 | [diff] [blame] | 259 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 260 | def test_append_to_zip_file(self): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 261 | # Test appending to an existing zipfile |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 262 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 263 | zipfp.write(TESTFN, TESTFN) |
| 264 | |
| 265 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 266 | zipfp.writestr("strfile", self.data) |
| 267 | self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"]) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 268 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 269 | def test_append_to_non_zip_file(self): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 270 | # Test appending to an existing file that is not a zipfile |
| 271 | # NOTE: this test fails if len(d) < 22 because of the first |
| 272 | # line "fpin.seek(-22, 2)" in _EndRecData |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 273 | d = b'I am not a ZipFile!'*10 |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 274 | f = open(TESTFN2, 'wb') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 275 | f.write(d) |
| 276 | f.close() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 277 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 278 | zipfp.write(TESTFN, TESTFN) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 280 | f = open(TESTFN2, 'rb') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 281 | f.seek(len(d)) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 282 | with zipfile.ZipFile(f, "r") as zipfp: |
| 283 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 284 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 285 | def test_write_default_name(self): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 286 | # Check that calling ZipFile.write without arcname specified produces the expected result |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 287 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 288 | zipfp.write(TESTFN) |
| 289 | self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read()) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 290 | |
Ezio Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 291 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 292 | def test_per_file_compression(self): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 293 | # Check that files within a Zip archive can have different compression options |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 294 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 295 | zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED) |
| 296 | zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED) |
| 297 | sinfo = zipfp.getinfo('storeme') |
| 298 | dinfo = zipfp.getinfo('deflateme') |
| 299 | self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED) |
| 300 | self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 301 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 302 | def test_write_to_readonly(self): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 303 | # Check that trying to call write() on a readonly ZipFile object |
| 304 | # raises a RuntimeError |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 305 | with zipfile.ZipFile(TESTFN2, mode="w") as zipfp: |
| 306 | zipfp.writestr("somefile.txt", "bogus") |
| 307 | |
| 308 | with zipfile.ZipFile(TESTFN2, mode="r") as zipfp: |
| 309 | self.assertRaises(RuntimeError, zipfp.write, TESTFN) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 310 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 311 | def test_extract(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 312 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 313 | for fpath, fdata in SMALL_TEST_DATA: |
| 314 | zipfp.writestr(fpath, fdata) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 315 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 316 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 317 | for fpath, fdata in SMALL_TEST_DATA: |
| 318 | writtenfile = zipfp.extract(fpath) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 319 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 320 | # make sure it was written to the right place |
| 321 | if os.path.isabs(fpath): |
| 322 | correctfile = os.path.join(os.getcwd(), fpath[1:]) |
| 323 | else: |
| 324 | correctfile = os.path.join(os.getcwd(), fpath) |
| 325 | correctfile = os.path.normpath(correctfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 326 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 327 | self.assertEqual(writtenfile, correctfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 328 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 329 | # make sure correct data is in correct file |
| 330 | self.assertEqual(fdata.encode(), open(writtenfile, "rb").read()) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 331 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 332 | os.remove(writtenfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 333 | |
| 334 | # remove the test file subdirectories |
| 335 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 336 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 337 | def test_extract_all(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 338 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 339 | for fpath, fdata in SMALL_TEST_DATA: |
| 340 | zipfp.writestr(fpath, fdata) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 341 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 342 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 343 | zipfp.extractall() |
| 344 | for fpath, fdata in SMALL_TEST_DATA: |
| 345 | if os.path.isabs(fpath): |
| 346 | outfile = os.path.join(os.getcwd(), fpath[1:]) |
| 347 | else: |
| 348 | outfile = os.path.join(os.getcwd(), fpath) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 349 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 350 | self.assertEqual(fdata.encode(), open(outfile, "rb").read()) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 351 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 352 | os.remove(outfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 353 | |
| 354 | # remove the test file subdirectories |
| 355 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 356 | |
Antoine Pitrou | 6e1df8d | 2008-07-25 19:58:18 +0000 | [diff] [blame] | 357 | def zip_test_writestr_permissions(self, f, compression): |
| 358 | # Make sure that writestr creates files with mode 0600, |
| 359 | # when it is passed a name rather than a ZipInfo instance. |
| 360 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 361 | self.make_test_archive(f, compression) |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 362 | with zipfile.ZipFile(f, "r") as zipfp: |
| 363 | zinfo = zipfp.getinfo('strfile') |
| 364 | self.assertEqual(zinfo.external_attr, 0o600 << 16) |
Antoine Pitrou | 6e1df8d | 2008-07-25 19:58:18 +0000 | [diff] [blame] | 365 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 366 | def test_writestr_permissions(self): |
Antoine Pitrou | 6e1df8d | 2008-07-25 19:58:18 +0000 | [diff] [blame] | 367 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 368 | self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED) |
| 369 | |
Gregory P. Smith | b0d9ca9 | 2009-07-07 05:06:04 +0000 | [diff] [blame] | 370 | def test_writestr_extended_local_header_issue1202(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 371 | with zipfile.ZipFile(TESTFN2, 'w') as orig_zip: |
| 372 | for data in 'abcdefghijklmnop': |
| 373 | zinfo = zipfile.ZipInfo(data) |
| 374 | zinfo.flag_bits |= 0x08 # Include an extended local header. |
| 375 | orig_zip.writestr(zinfo, data) |
| 376 | |
| 377 | def test_close(self): |
| 378 | """Check that the zipfile is closed after the 'with' block.""" |
| 379 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 380 | for fpath, fdata in SMALL_TEST_DATA: |
| 381 | zipfp.writestr(fpath, fdata) |
| 382 | self.assertTrue(zipfp.fp is not None, 'zipfp is not open') |
| 383 | self.assertTrue(zipfp.fp is None, 'zipfp is not closed') |
| 384 | |
| 385 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 386 | self.assertTrue(zipfp.fp is not None, 'zipfp is not open') |
| 387 | self.assertTrue(zipfp.fp is None, 'zipfp is not closed') |
| 388 | |
| 389 | def test_close_on_exception(self): |
| 390 | """Check that the zipfile is closed if an exception is raised in the |
| 391 | 'with' block.""" |
| 392 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 393 | for fpath, fdata in SMALL_TEST_DATA: |
| 394 | zipfp.writestr(fpath, fdata) |
| 395 | |
| 396 | try: |
| 397 | with zipfile.ZipFile(TESTFN2, "r") as zipfp2: |
| 398 | raise zipfile.BadZipfile() |
| 399 | except zipfile.BadZipfile: |
| 400 | self.assertTrue(zipfp2.fp is None, 'zipfp is not closed') |
Gregory P. Smith | b0d9ca9 | 2009-07-07 05:06:04 +0000 | [diff] [blame] | 401 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 402 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 403 | unlink(TESTFN) |
| 404 | unlink(TESTFN2) |
| 405 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 406 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 407 | class TestZip64InSmallFiles(unittest.TestCase): |
| 408 | # These tests test the ZIP64 functionality without using large files, |
| 409 | # see test_zipfile64 for proper tests. |
| 410 | |
| 411 | def setUp(self): |
| 412 | self._limit = zipfile.ZIP64_LIMIT |
| 413 | zipfile.ZIP64_LIMIT = 5 |
| 414 | |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 415 | line_gen = (bytes("Test of zipfile line %d." % i, "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 416 | for i in range(0, FIXEDTEST_SIZE)) |
| 417 | self.data = b'\n'.join(line_gen) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 418 | |
| 419 | # Make a source file with some lines |
| 420 | fp = open(TESTFN, "wb") |
| 421 | fp.write(self.data) |
| 422 | fp.close() |
| 423 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 424 | def large_file_exception_test(self, f, compression): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 425 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 426 | self.assertRaises(zipfile.LargeZipFile, |
| 427 | zipfp.write, TESTFN, "another.name") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 428 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 429 | def large_file_exception_test2(self, f, compression): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 430 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 431 | self.assertRaises(zipfile.LargeZipFile, |
| 432 | zipfp.writestr, "another.name", self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 433 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 434 | def test_large_file_exception(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 435 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 436 | self.large_file_exception_test(f, zipfile.ZIP_STORED) |
| 437 | self.large_file_exception_test2(f, zipfile.ZIP_STORED) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 438 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 439 | def zip_test(self, f, compression): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 440 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 441 | with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp: |
| 442 | zipfp.write(TESTFN, "another.name") |
| 443 | zipfp.write(TESTFN, TESTFN) |
| 444 | zipfp.writestr("strfile", self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 445 | |
| 446 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 447 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 448 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 449 | self.assertEqual(zipfp.read("another.name"), self.data) |
| 450 | self.assertEqual(zipfp.read("strfile"), self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 451 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 452 | # Print the ZIP directory |
| 453 | fp = io.StringIO() |
| 454 | zipfp.printdir(fp) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 455 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 456 | directory = fp.getvalue() |
| 457 | lines = directory.splitlines() |
| 458 | self.assertEquals(len(lines), 4) # Number of files + header |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 459 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 460 | self.assertTrue('File Name' in lines[0]) |
| 461 | self.assertTrue('Modified' in lines[0]) |
| 462 | self.assertTrue('Size' in lines[0]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 463 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 464 | fn, date, time, size = lines[1].split() |
| 465 | self.assertEquals(fn, 'another.name') |
| 466 | # XXX: timestamp is not tested |
| 467 | self.assertEquals(size, str(len(self.data))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 468 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 469 | # Check the namelist |
| 470 | names = zipfp.namelist() |
| 471 | self.assertEquals(len(names), 3) |
| 472 | self.assertTrue(TESTFN in names) |
| 473 | self.assertTrue("another.name" in names) |
| 474 | self.assertTrue("strfile" in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 475 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 476 | # Check infolist |
| 477 | infos = zipfp.infolist() |
| 478 | names = [ i.filename for i in infos ] |
| 479 | self.assertEquals(len(names), 3) |
| 480 | self.assertTrue(TESTFN in names) |
| 481 | self.assertTrue("another.name" in names) |
| 482 | self.assertTrue("strfile" in names) |
| 483 | for i in infos: |
| 484 | self.assertEquals(i.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 485 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 486 | # check getinfo |
| 487 | for nm in (TESTFN, "another.name", "strfile"): |
| 488 | info = zipfp.getinfo(nm) |
| 489 | self.assertEquals(info.filename, nm) |
| 490 | self.assertEquals(info.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 491 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 492 | # Check that testzip doesn't raise an exception |
| 493 | zipfp.testzip() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 494 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 495 | def test_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 496 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 497 | self.zip_test(f, zipfile.ZIP_STORED) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 498 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 499 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 500 | def test_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 501 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 502 | self.zip_test(f, zipfile.ZIP_DEFLATED) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 503 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 504 | def test_absolute_arcnames(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 505 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, |
| 506 | allowZip64=True) as zipfp: |
| 507 | zipfp.write(TESTFN, "/absolute") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 508 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 509 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 510 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 511 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 512 | def tearDown(self): |
| 513 | zipfile.ZIP64_LIMIT = self._limit |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 514 | unlink(TESTFN) |
| 515 | unlink(TESTFN2) |
| 516 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 517 | |
| 518 | class PyZipFileTests(unittest.TestCase): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 519 | def test_write_pyfile(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 520 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 521 | fn = __file__ |
| 522 | if fn.endswith('.pyc') or fn.endswith('.pyo'): |
| 523 | fn = fn[:-1] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 524 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 525 | zipfp.writepy(fn) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 526 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 527 | bn = os.path.basename(fn) |
| 528 | self.assertTrue(bn not in zipfp.namelist()) |
| 529 | self.assertTrue(bn + 'o' in zipfp.namelist() or |
| 530 | bn + 'c' in zipfp.namelist()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 531 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 532 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 533 | fn = __file__ |
| 534 | if fn.endswith('.pyc') or fn.endswith('.pyo'): |
| 535 | fn = fn[:-1] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 536 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 537 | zipfp.writepy(fn, "testpackage") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 538 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 539 | bn = "%s/%s"%("testpackage", os.path.basename(fn)) |
| 540 | self.assertTrue(bn not in zipfp.namelist()) |
| 541 | self.assertTrue(bn + 'o' in zipfp.namelist() or |
| 542 | bn + 'c' in zipfp.namelist()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 543 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 544 | def test_write_python_package(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 545 | import email |
| 546 | packagedir = os.path.dirname(email.__file__) |
| 547 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 548 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 549 | zipfp.writepy(packagedir) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 550 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 551 | # Check for a couple of modules at different levels of the hieararchy |
| 552 | names = zipfp.namelist() |
| 553 | self.assertTrue('email/__init__.pyo' in names or |
| 554 | 'email/__init__.pyc' in names) |
| 555 | self.assertTrue('email/mime/text.pyo' in names or |
| 556 | 'email/mime/text.pyc' in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 557 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 558 | def test_write_python_directory(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 559 | os.mkdir(TESTFN2) |
| 560 | try: |
| 561 | fp = open(os.path.join(TESTFN2, "mod1.py"), "w") |
Guido van Rossum | 43fc78d | 2007-02-09 22:18:41 +0000 | [diff] [blame] | 562 | fp.write("print(42)\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 563 | fp.close() |
| 564 | |
| 565 | fp = open(os.path.join(TESTFN2, "mod2.py"), "w") |
Guido van Rossum | 43fc78d | 2007-02-09 22:18:41 +0000 | [diff] [blame] | 566 | fp.write("print(42 * 42)\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 567 | fp.close() |
| 568 | |
| 569 | fp = open(os.path.join(TESTFN2, "mod2.txt"), "w") |
| 570 | fp.write("bla bla bla\n") |
| 571 | fp.close() |
| 572 | |
| 573 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 574 | zipfp.writepy(TESTFN2) |
| 575 | |
| 576 | names = zipfp.namelist() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 577 | self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names) |
| 578 | self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names) |
| 579 | self.assertTrue('mod2.txt' not in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 580 | |
| 581 | finally: |
| 582 | shutil.rmtree(TESTFN2) |
| 583 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 584 | def test_write_non_pyfile(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 585 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 586 | open(TESTFN, 'w').write('most definitely not a python file') |
| 587 | self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) |
| 588 | os.remove(TESTFN) |
| 589 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 590 | |
| 591 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 592 | class OtherTests(unittest.TestCase): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 593 | def test_unicode_filenames(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 594 | with zipfile.ZipFile(TESTFN, "w") as zf: |
| 595 | zf.writestr("foo.txt", "Test for unicode filename") |
| 596 | zf.writestr("\xf6.txt", "Test for unicode filename") |
| 597 | |
| 598 | with zipfile.ZipFile(TESTFN, "r") as zf: |
| 599 | self.assertEqual(zf.filelist[0].filename, "foo.txt") |
| 600 | self.assertEqual(zf.filelist[1].filename, "\xf6.txt") |
Martin v. Löwis | 8570f6a | 2008-05-05 17:44:38 +0000 | [diff] [blame] | 601 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 602 | def test_create_non_existent_file_for_append(self): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 603 | if os.path.exists(TESTFN): |
| 604 | os.unlink(TESTFN) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 605 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 606 | filename = 'testfile.txt' |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 607 | content = b'hello, world. this is some content.' |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 608 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 609 | try: |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 610 | with zipfile.ZipFile(TESTFN, 'a') as zf: |
| 611 | zf.writestr(filename, content) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 612 | except IOError: |
| 613 | self.fail('Could not append data to a non-existent zip file.') |
| 614 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 615 | self.assertTrue(os.path.exists(TESTFN)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 616 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 617 | with zipfile.ZipFile(TESTFN, 'r') as zf: |
| 618 | self.assertEqual(zf.read(filename), content) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 619 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 620 | def test_close_erroneous_file(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 621 | # This test checks that the ZipFile constructor closes the file object |
| 622 | # it opens if there's an error in the file. If it doesn't, the traceback |
| 623 | # holds a reference to the ZipFile object and, indirectly, the file object. |
| 624 | # On Windows, this causes the os.unlink() call to fail because the |
| 625 | # underlying file is still open. This is SF bug #412214. |
| 626 | # |
| 627 | fp = open(TESTFN, "w") |
| 628 | fp.write("this is not a legal zip file\n") |
| 629 | fp.close() |
| 630 | try: |
| 631 | zf = zipfile.ZipFile(TESTFN) |
| 632 | except zipfile.BadZipfile: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 633 | pass |
| 634 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 635 | def test_is_zip_erroneous_file(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 636 | # This test checks that the is_zipfile function correctly identifies |
| 637 | # a file that is not a zip file |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 638 | |
| 639 | # - passing a filename |
| 640 | with open(TESTFN, "w") as fp: |
| 641 | fp.write("this is not a legal zip file\n") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 642 | chk = zipfile.is_zipfile(TESTFN) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 643 | self.assertTrue(not chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 644 | # - passing a file object |
| 645 | with open(TESTFN, "rb") as fp: |
| 646 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 647 | self.assertTrue(not chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 648 | # - passing a file-like object |
| 649 | fp = io.BytesIO() |
| 650 | fp.write(b"this is not a legal zip file\n") |
| 651 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 652 | self.assertTrue(not chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 653 | fp.seek(0,0) |
| 654 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 655 | self.assertTrue(not chk) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 656 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 657 | def test_is_zip_valid_file(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 658 | # This test checks that the is_zipfile function correctly identifies |
| 659 | # a file that is a zip file |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 660 | |
| 661 | # - passing a filename |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 662 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 663 | zipf.writestr("foo.txt", b"O, for a Muse of Fire!") |
| 664 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 665 | chk = zipfile.is_zipfile(TESTFN) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 666 | self.assertTrue(chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 667 | # - passing a file object |
| 668 | with open(TESTFN, "rb") as fp: |
| 669 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 670 | self.assertTrue(chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 671 | fp.seek(0,0) |
| 672 | zip_contents = fp.read() |
| 673 | # - passing a file-like object |
| 674 | fp = io.BytesIO() |
| 675 | fp.write(zip_contents) |
| 676 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 677 | self.assertTrue(chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 678 | fp.seek(0,0) |
| 679 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 680 | self.assertTrue(chk) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 681 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 682 | def test_non_existent_file_raises_IOError(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 683 | # make sure we don't raise an AttributeError when a partially-constructed |
| 684 | # ZipFile instance is finalized; this tests for regression on SF tracker |
| 685 | # bug #403871. |
| 686 | |
| 687 | # The bug we're testing for caused an AttributeError to be raised |
| 688 | # when a ZipFile instance was created for a file that did not |
| 689 | # exist; the .fp member was not initialized but was needed by the |
| 690 | # __del__() method. Since the AttributeError is in the __del__(), |
| 691 | # it is ignored, but the user should be sufficiently annoyed by |
| 692 | # the message on the output that regression will be noticed |
| 693 | # quickly. |
| 694 | self.assertRaises(IOError, zipfile.ZipFile, TESTFN) |
| 695 | |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 696 | def test_empty_file_raises_BadZipFile(self): |
| 697 | f = open(TESTFN, 'w') |
| 698 | f.close() |
| 699 | self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN) |
| 700 | |
| 701 | f = open(TESTFN, 'w') |
| 702 | f.write("short file") |
| 703 | f.close() |
| 704 | self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN) |
| 705 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 706 | def test_closed_zip_raises_RuntimeError(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 707 | # Verify that testzip() doesn't swallow inappropriate exceptions. |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 708 | data = io.BytesIO() |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 709 | with zipfile.ZipFile(data, mode="w") as zipf: |
| 710 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 711 | |
| 712 | # This is correct; calling .read on a closed ZipFile should throw |
| 713 | # a RuntimeError, and so should calling .testzip. An earlier |
| 714 | # version of .testzip would swallow this exception (and any other) |
| 715 | # and report that the first file in the archive was corrupt. |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 716 | self.assertRaises(RuntimeError, zipf.read, "foo.txt") |
| 717 | self.assertRaises(RuntimeError, zipf.open, "foo.txt") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 718 | self.assertRaises(RuntimeError, zipf.testzip) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 719 | self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus") |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 720 | open(TESTFN, 'w').write('zipfile test data') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 721 | self.assertRaises(RuntimeError, zipf.write, TESTFN) |
| 722 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 723 | def test_bad_constructor_mode(self): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 724 | # Check that bad modes passed to ZipFile constructor are caught |
| 725 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q") |
| 726 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 727 | def test_bad_open_mode(self): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 728 | # Check that bad modes passed to ZipFile.open are caught |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 729 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 730 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 731 | |
| 732 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 733 | # read the data to make sure the file is there |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 734 | zipf.read("foo.txt") |
| 735 | self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 736 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 737 | def test_read0(self): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 738 | # Check that calling read(0) on a ZipExtFile object returns an empty |
| 739 | # string and doesn't advance file pointer |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 740 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 741 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 742 | # read the data to make sure the file is there |
| 743 | f = zipf.open("foo.txt") |
| 744 | for i in range(FIXEDTEST_SIZE): |
| 745 | self.assertEqual(f.read(0), b'') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 746 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 747 | self.assertEqual(f.read(), b"O, for a Muse of Fire!") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 748 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 749 | def test_open_non_existent_item(self): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 750 | # Check that attempting to call open() for an item that doesn't |
| 751 | # exist in the archive raises a RuntimeError |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 752 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 753 | self.assertRaises(KeyError, zipf.open, "foo.txt", "r") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 754 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 755 | def test_bad_compression_mode(self): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 756 | # Check that bad compression methods passed to ZipFile.open are caught |
| 757 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1) |
| 758 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 759 | def test_null_byte_in_filename(self): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 760 | # Check that a filename containing a null byte is properly terminated |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 761 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 762 | zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!") |
| 763 | self.assertEqual(zipf.namelist(), ['foo.txt']) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 764 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 765 | def test_struct_sizes(self): |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 766 | # check that ZIP internal structure sizes are calculated correctly |
| 767 | self.assertEqual(zipfile.sizeEndCentDir, 22) |
| 768 | self.assertEqual(zipfile.sizeCentralDir, 46) |
| 769 | self.assertEqual(zipfile.sizeEndCentDir64, 56) |
| 770 | self.assertEqual(zipfile.sizeEndCentDir64Locator, 20) |
| 771 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 772 | def test_comments(self): |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 773 | # This test checks that comments on the archive are handled properly |
| 774 | |
| 775 | # check default comment is empty |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 776 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 777 | self.assertEqual(zipf.comment, b'') |
| 778 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 779 | |
| 780 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 781 | self.assertEqual(zipfr.comment, b'') |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 782 | |
| 783 | # check a simple short comment |
| 784 | 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^] | 785 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 786 | zipf.comment = comment |
| 787 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 788 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 789 | self.assertEqual(zipf.comment, comment) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 790 | |
| 791 | # check a comment of max length |
| 792 | comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)]) |
| 793 | comment2 = comment2.encode("ascii") |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 794 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 795 | zipf.comment = comment2 |
| 796 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 797 | |
| 798 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 799 | self.assertEqual(zipfr.comment, comment2) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 800 | |
| 801 | # check a comment that is too long is truncated |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 802 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 803 | zipf.comment = comment2 + b'oops' |
| 804 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 805 | with zipfile.ZipFile(TESTFN, mode="r") as zipfr: |
| 806 | self.assertEqual(zipfr.comment, comment2) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 807 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 808 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 809 | unlink(TESTFN) |
| 810 | unlink(TESTFN2) |
| 811 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 812 | |
| 813 | class DecryptionTests(unittest.TestCase): |
| 814 | # This test checks that ZIP decryption works. Since the library does not |
| 815 | # support encryption at the moment, we use a pre-generated encrypted |
| 816 | # ZIP file |
| 817 | |
| 818 | data = ( |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 819 | b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00' |
| 820 | b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y' |
| 821 | b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl' |
| 822 | b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00' |
| 823 | b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81' |
| 824 | b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00' |
| 825 | b'\x00\x00L\x00\x00\x00\x00\x00' ) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 826 | data2 = ( |
| 827 | b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02' |
| 828 | b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04' |
| 829 | b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0' |
| 830 | b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03' |
| 831 | b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00' |
| 832 | b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze' |
| 833 | b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01' |
| 834 | b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' ) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 835 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 836 | plain = b'zipfile.py encryption test' |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 837 | plain2 = b'\x00'*512 |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 838 | |
| 839 | def setUp(self): |
| 840 | fp = open(TESTFN, "wb") |
| 841 | fp.write(self.data) |
| 842 | fp.close() |
| 843 | self.zip = zipfile.ZipFile(TESTFN, "r") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 844 | fp = open(TESTFN2, "wb") |
| 845 | fp.write(self.data2) |
| 846 | fp.close() |
| 847 | self.zip2 = zipfile.ZipFile(TESTFN2, "r") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 848 | |
| 849 | def tearDown(self): |
| 850 | self.zip.close() |
| 851 | os.unlink(TESTFN) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 852 | self.zip2.close() |
| 853 | os.unlink(TESTFN2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 854 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 855 | def test_no_password(self): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 856 | # Reading the encrypted file without password |
| 857 | # must generate a RunTime exception |
| 858 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 859 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 860 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 861 | def test_bad_password(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 862 | self.zip.setpassword(b"perl") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 863 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 864 | self.zip2.setpassword(b"perl") |
| 865 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 866 | |
Ezio Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 867 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 868 | def test_good_password(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 869 | self.zip.setpassword(b"python") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 870 | self.assertEquals(self.zip.read("test.txt"), self.plain) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 871 | self.zip2.setpassword(b"12345") |
| 872 | self.assertEquals(self.zip2.read("zero"), self.plain2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 873 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 874 | |
| 875 | class TestsWithRandomBinaryFiles(unittest.TestCase): |
| 876 | def setUp(self): |
| 877 | datacount = randint(16, 64)*1024 + randint(1, 1024) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 878 | self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000)) |
| 879 | for i in range(datacount)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 880 | |
| 881 | # Make a source file with some lines |
| 882 | fp = open(TESTFN, "wb") |
| 883 | fp.write(self.data) |
| 884 | fp.close() |
| 885 | |
| 886 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 887 | unlink(TESTFN) |
| 888 | unlink(TESTFN2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 889 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 890 | def make_test_archive(self, f, compression): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 891 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 892 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 893 | zipfp.write(TESTFN, "another.name") |
| 894 | zipfp.write(TESTFN, TESTFN) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 895 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 896 | def zip_test(self, f, compression): |
| 897 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 898 | |
| 899 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 900 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 901 | testdata = zipfp.read(TESTFN) |
| 902 | self.assertEqual(len(testdata), len(self.data)) |
| 903 | self.assertEqual(testdata, self.data) |
| 904 | self.assertEqual(zipfp.read("another.name"), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 905 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 906 | def test_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 907 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 908 | self.zip_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 909 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 910 | def zip_open_test(self, f, compression): |
| 911 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 912 | |
| 913 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 914 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 915 | zipdata1 = [] |
| 916 | zipopen1 = zipfp.open(TESTFN) |
| 917 | while True: |
| 918 | read_data = zipopen1.read(256) |
| 919 | if not read_data: |
| 920 | break |
| 921 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 922 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 923 | zipdata2 = [] |
| 924 | zipopen2 = zipfp.open("another.name") |
| 925 | while True: |
| 926 | read_data = zipopen2.read(256) |
| 927 | if not read_data: |
| 928 | break |
| 929 | zipdata2.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 930 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 931 | testdata1 = b''.join(zipdata1) |
| 932 | self.assertEqual(len(testdata1), len(self.data)) |
| 933 | self.assertEqual(testdata1, self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 934 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 935 | testdata2 = b''.join(zipdata2) |
| 936 | self.assertEqual(len(testdata1), len(self.data)) |
| 937 | self.assertEqual(testdata1, self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 938 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 939 | def test_open_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 940 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 941 | self.zip_open_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 942 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 943 | def zip_random_open_test(self, f, compression): |
| 944 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 945 | |
| 946 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 947 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 948 | zipdata1 = [] |
| 949 | zipopen1 = zipfp.open(TESTFN) |
| 950 | while True: |
| 951 | read_data = zipopen1.read(randint(1, 1024)) |
| 952 | if not read_data: |
| 953 | break |
| 954 | zipdata1.append(read_data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 955 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 956 | testdata = b''.join(zipdata1) |
| 957 | self.assertEqual(len(testdata), len(self.data)) |
| 958 | self.assertEqual(testdata, self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 959 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 960 | def test_random_open_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 961 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 962 | self.zip_random_open_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 963 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 964 | |
Ezio Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 965 | @skipUnless(zlib, "requires zlib") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 966 | class TestsWithMultipleOpens(unittest.TestCase): |
| 967 | def setUp(self): |
| 968 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 969 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp: |
| 970 | zipfp.writestr('ones', '1'*FIXEDTEST_SIZE) |
| 971 | zipfp.writestr('twos', '2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 972 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 973 | def test_same_file(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 974 | # Verify that (when the ZipFile is in control of creating file objects) |
| 975 | # multiple open() calls can be made without interfering with each other. |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 976 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 977 | zopen1 = zipf.open('ones') |
| 978 | zopen2 = zipf.open('ones') |
| 979 | data1 = zopen1.read(500) |
| 980 | data2 = zopen2.read(500) |
| 981 | data1 += zopen1.read(500) |
| 982 | data2 += zopen2.read(500) |
| 983 | self.assertEqual(data1, data2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 984 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 985 | def test_different_file(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 986 | # Verify that (when the ZipFile is in control of creating file objects) |
| 987 | # multiple open() calls can be made without interfering with each other. |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 988 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 989 | zopen1 = zipf.open('ones') |
| 990 | zopen2 = zipf.open('twos') |
| 991 | data1 = zopen1.read(500) |
| 992 | data2 = zopen2.read(500) |
| 993 | data1 += zopen1.read(500) |
| 994 | data2 += zopen2.read(500) |
| 995 | self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) |
| 996 | self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 997 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 998 | def test_interleaved(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 999 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1000 | # multiple open() calls can be made without interfering with each other. |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 1001 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 1002 | zopen1 = zipf.open('ones') |
| 1003 | data1 = zopen1.read(500) |
| 1004 | zopen2 = zipf.open('twos') |
| 1005 | data2 = zopen2.read(500) |
| 1006 | data1 += zopen1.read(500) |
| 1007 | data2 += zopen2.read(500) |
| 1008 | self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) |
| 1009 | self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1010 | |
| 1011 | def tearDown(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1012 | unlink(TESTFN2) |
| 1013 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1014 | |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1015 | class TestWithDirectory(unittest.TestCase): |
| 1016 | def setUp(self): |
| 1017 | os.mkdir(TESTFN2) |
| 1018 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1019 | def test_extract_dir(self): |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 1020 | with zipfile.ZipFile(findfile("zipdir.zip")) as zipf: |
| 1021 | zipf.extractall(TESTFN2) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1022 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a"))) |
| 1023 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b"))) |
| 1024 | self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c"))) |
| 1025 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1026 | def test_bug_6050(self): |
Martin v. Löwis | 70ccd16 | 2009-05-24 19:47:22 +0000 | [diff] [blame] | 1027 | # Extraction should succeed if directories already exist |
| 1028 | os.mkdir(os.path.join(TESTFN2, "a")) |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1029 | self.test_extract_dir() |
Martin v. Löwis | 70ccd16 | 2009-05-24 19:47:22 +0000 | [diff] [blame] | 1030 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1031 | def test_store_dir(self): |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1032 | os.mkdir(os.path.join(TESTFN2, "x")) |
| 1033 | zipf = zipfile.ZipFile(TESTFN, "w") |
| 1034 | zipf.write(os.path.join(TESTFN2, "x"), "x") |
| 1035 | self.assertTrue(zipf.filelist[0].filename.endswith("x/")) |
| 1036 | |
| 1037 | def tearDown(self): |
| 1038 | shutil.rmtree(TESTFN2) |
| 1039 | if os.path.exists(TESTFN): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1040 | unlink(TESTFN) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1041 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1042 | |
| 1043 | class UniversalNewlineTests(unittest.TestCase): |
| 1044 | def setUp(self): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 1045 | self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1046 | for i in range(FIXEDTEST_SIZE)] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1047 | self.seps = ('\r', '\r\n', '\n') |
| 1048 | self.arcdata, self.arcfiles = {}, {} |
| 1049 | for n, s in enumerate(self.seps): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1050 | b = s.encode("ascii") |
| 1051 | self.arcdata[s] = b.join(self.line_gen) + b |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1052 | self.arcfiles[s] = '%s-%d' % (TESTFN, n) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1053 | f = open(self.arcfiles[s], "wb") |
| 1054 | try: |
| 1055 | f.write(self.arcdata[s]) |
| 1056 | finally: |
| 1057 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1058 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1059 | def make_test_archive(self, f, compression): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1060 | # Create the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 1061 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 1062 | for fn in self.arcfiles.values(): |
| 1063 | zipfp.write(fn, fn) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1064 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1065 | def read_test(self, f, compression): |
| 1066 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1067 | |
| 1068 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 1069 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1070 | for sep, fn in self.arcfiles.items(): |
| 1071 | zipdata = zipfp.open(fn, "rU").read() |
| 1072 | self.assertEqual(self.arcdata[sep], zipdata) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1073 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1074 | def readline_test(self, f, compression): |
| 1075 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1076 | |
| 1077 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 1078 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1079 | for sep, fn in self.arcfiles.items(): |
| 1080 | zipopen = zipfp.open(fn, "rU") |
| 1081 | for line in self.line_gen: |
| 1082 | linedata = zipopen.readline() |
| 1083 | self.assertEqual(linedata, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1084 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1085 | def readlines_test(self, f, compression): |
| 1086 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1087 | |
| 1088 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 1089 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1090 | for sep, fn in self.arcfiles.items(): |
| 1091 | ziplines = zipfp.open(fn, "rU").readlines() |
| 1092 | for line, zipline in zip(self.line_gen, ziplines): |
| 1093 | self.assertEqual(zipline, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1094 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1095 | def iterlines_test(self, f, compression): |
| 1096 | self.make_test_archive(f, compression) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1097 | |
| 1098 | # Read the ZIP archive |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame^] | 1099 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1100 | for sep, fn in self.arcfiles.items(): |
| 1101 | for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")): |
| 1102 | self.assertEqual(zipline, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1103 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1104 | def test_read_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1105 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1106 | self.read_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1107 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1108 | def test_readline_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1109 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1110 | self.readline_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1111 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1112 | def test_readlines_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1113 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1114 | self.readlines_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1115 | |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1116 | def test_iterlines_stored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1117 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1118 | self.iterlines_test(f, zipfile.ZIP_STORED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1119 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1120 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1121 | def test_read_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1122 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1123 | self.read_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1124 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1125 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1126 | def test_readline_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1127 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1128 | self.readline_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1129 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1130 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1131 | def test_readlines_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1132 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1133 | self.readlines_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1134 | |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1135 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1136 | def test_iterlines_deflated(self): |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1137 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Ezio Melotti | afd0d11 | 2009-07-15 17:17:17 +0000 | [diff] [blame] | 1138 | self.iterlines_test(f, zipfile.ZIP_DEFLATED) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1139 | |
| 1140 | def tearDown(self): |
| 1141 | for sep, fn in self.arcfiles.items(): |
| 1142 | os.remove(fn) |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1143 | unlink(TESTFN) |
| 1144 | unlink(TESTFN2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1145 | |
| 1146 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1147 | def test_main(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1148 | run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests, |
| 1149 | PyZipFileTests, DecryptionTests, TestsWithMultipleOpens, |
Ezio Melotti | 7643024 | 2009-07-11 18:28:48 +0000 | [diff] [blame] | 1150 | TestWithDirectory, UniversalNewlineTests, |
| 1151 | TestsWithRandomBinaryFiles) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1152 | |
| 1153 | if __name__ == "__main__": |
| 1154 | test_main() |