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