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