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