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 |
Victor Stinner | ff1d2f4 | 2011-05-18 13:43:23 +0200 | [diff] [blame] | 6 | import io |
| 7 | import os |
| 8 | import shutil |
| 9 | import struct |
| 10 | import sys |
| 11 | import unittest |
| 12 | import zipfile |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 13 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 14 | from tempfile import TemporaryFile |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 15 | from random import randint, random |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 16 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 17 | import test.support as support |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 18 | from test.support import TESTFN, run_unittest, findfile |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 19 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 20 | TESTFN2 = TESTFN + "2" |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 21 | TESTFNDIR = TESTFN + "d" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 22 | FIXEDTEST_SIZE = 1000 |
Victor Stinner | ff1d2f4 | 2011-05-18 13:43:23 +0200 | [diff] [blame] | 23 | DATAFILES_DIR = 'zipfile_datafiles' |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 24 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 25 | SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'), |
| 26 | ('ziptest2dir/_ziptest2', 'qawsedrftg'), |
| 27 | ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'), |
| 28 | ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')] |
| 29 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 30 | class TestsWithSourceFile(unittest.TestCase): |
| 31 | def setUp(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 32 | self.line_gen = (bytes("Zipfile test line %d. random float: %f" % |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 33 | (i, random()), "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 34 | for i in range(FIXEDTEST_SIZE)) |
| 35 | self.data = b'\n'.join(self.line_gen) + b'\n' |
Fred Drake | 6e7e485 | 2001-02-28 05:34:16 +0000 | [diff] [blame] | 36 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 37 | # Make a source file with some lines |
| 38 | fp = open(TESTFN, "wb") |
| 39 | fp.write(self.data) |
| 40 | fp.close() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 42 | def makeTestArchive(self, f, compression): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 43 | # Create the ZIP archive |
| 44 | zipfp = zipfile.ZipFile(f, "w", compression) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 45 | zipfp.write(TESTFN, "another.name") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 46 | zipfp.write(TESTFN, TESTFN) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 47 | zipfp.writestr("strfile", self.data) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 48 | zipfp.close() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 50 | def zipTest(self, f, compression): |
| 51 | self.makeTestArchive(f, compression) |
| 52 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 53 | # Read the ZIP archive |
| 54 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 55 | self.assertEqual(zipfp.read(TESTFN), self.data) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 56 | self.assertEqual(zipfp.read("another.name"), self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 57 | self.assertEqual(zipfp.read("strfile"), self.data) |
| 58 | |
| 59 | # Print the ZIP directory |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 60 | fp = io.StringIO() |
| 61 | zipfp.printdir(file=fp) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 62 | |
| 63 | directory = fp.getvalue() |
| 64 | lines = directory.splitlines() |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 65 | self.assertEqual(len(lines), 4) # Number of files + header |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +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() |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 72 | self.assertEqual(fn, 'another.name') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 73 | # XXX: timestamp is not tested |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 74 | self.assertEqual(size, str(len(self.data))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | |
| 76 | # Check the namelist |
| 77 | names = zipfp.namelist() |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 78 | self.assertEqual(len(names), 3) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +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 ] |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 86 | self.assertEqual(len(names), 3) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +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: |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 91 | self.assertEqual(i.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 92 | |
| 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) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 96 | self.assertEqual(info.filename, nm) |
| 97 | self.assertEqual(info.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 98 | |
| 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 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 103 | def testStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 104 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 105 | self.zipTest(f, zipfile.ZIP_STORED) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 107 | def zipOpenTest(self, f, compression): |
| 108 | self.makeTestArchive(f, compression) |
| 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 | |
| 132 | def testOpenStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 133 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 134 | self.zipOpenTest(f, zipfile.ZIP_STORED) |
| 135 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 136 | def testOpenViaZipInfo(self): |
| 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() |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +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) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +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 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 155 | def zipRandomOpenTest(self, f, compression): |
| 156 | self.makeTestArchive(f, compression) |
| 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 | |
| 171 | def testRandomOpenStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 172 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 173 | self.zipRandomOpenTest(f, zipfile.ZIP_STORED) |
| 174 | |
| 175 | def zipReadlineTest(self, f, compression): |
| 176 | self.makeTestArchive(f, compression) |
| 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 | |
| 187 | def zipReadlinesTest(self, f, compression): |
| 188 | self.makeTestArchive(f, compression) |
| 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 | |
| 198 | def zipIterlinesTest(self, f, compression): |
| 199 | self.makeTestArchive(f, compression) |
| 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 | |
| 208 | def testReadlineStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 209 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 210 | self.zipReadlineTest(f, zipfile.ZIP_STORED) |
| 211 | |
| 212 | def testReadlinesStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 213 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 214 | self.zipReadlinesTest(f, zipfile.ZIP_STORED) |
| 215 | |
| 216 | def testIterlinesStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 217 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 218 | self.zipIterlinesTest(f, zipfile.ZIP_STORED) |
| 219 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 220 | if zlib: |
| 221 | def testDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 222 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 223 | self.zipTest(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 | def testOpenDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 226 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 227 | self.zipOpenTest(f, zipfile.ZIP_DEFLATED) |
| 228 | |
| 229 | def testRandomOpenDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 230 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 231 | self.zipRandomOpenTest(f, zipfile.ZIP_DEFLATED) |
| 232 | |
| 233 | def testReadlineDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 234 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 235 | self.zipReadlineTest(f, zipfile.ZIP_DEFLATED) |
| 236 | |
| 237 | def testReadlinesDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 238 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 239 | self.zipReadlinesTest(f, zipfile.ZIP_DEFLATED) |
| 240 | |
| 241 | def testIterlinesDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 242 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 243 | self.zipIterlinesTest(f, zipfile.ZIP_DEFLATED) |
| 244 | |
| 245 | def testLowCompression(self): |
| 246 | # Checks for cases where compressed data is larger than original |
| 247 | # Create the ZIP archive |
| 248 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) |
| 249 | zipfp.writestr("strfile", '12') |
| 250 | zipfp.close() |
| 251 | |
| 252 | # Get an open object for strfile |
| 253 | zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) |
| 254 | openobj = zipfp.open("strfile") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 255 | self.assertEqual(openobj.read(1), b'1') |
| 256 | self.assertEqual(openobj.read(1), b'2') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 257 | |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame] | 258 | def testAbsoluteArcnames(self): |
| 259 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
| 260 | zipfp.write(TESTFN, "/absolute") |
| 261 | zipfp.close() |
| 262 | |
| 263 | zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) |
| 264 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
| 265 | zipfp.close() |
Tim Peters | 32cbc96 | 2006-02-20 21:42:18 +0000 | [diff] [blame] | 266 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 267 | def testAppendToZipFile(self): |
| 268 | # Test appending to an existing zipfile |
| 269 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
| 270 | zipfp.write(TESTFN, TESTFN) |
| 271 | zipfp.close() |
| 272 | zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) |
| 273 | zipfp.writestr("strfile", self.data) |
| 274 | self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"]) |
| 275 | zipfp.close() |
| 276 | |
| 277 | def testAppendToNonZipFile(self): |
| 278 | # Test appending to an existing file that is not a zipfile |
| 279 | # NOTE: this test fails if len(d) < 22 because of the first |
| 280 | # line "fpin.seek(-22, 2)" in _EndRecData |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 281 | d = b'I am not a ZipFile!'*10 |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 282 | f = open(TESTFN2, 'wb') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 283 | f.write(d) |
| 284 | f.close() |
| 285 | zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) |
| 286 | zipfp.write(TESTFN, TESTFN) |
| 287 | zipfp.close() |
| 288 | |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 289 | f = open(TESTFN2, 'rb') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 290 | f.seek(len(d)) |
| 291 | zipfp = zipfile.ZipFile(f, "r") |
| 292 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
| 293 | zipfp.close() |
| 294 | f.close() |
| 295 | |
| 296 | def test_WriteDefaultName(self): |
| 297 | # Check that calling ZipFile.write without arcname specified produces the expected result |
| 298 | zipfp = zipfile.ZipFile(TESTFN2, "w") |
| 299 | zipfp.write(TESTFN) |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 300 | self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read()) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 301 | zipfp.close() |
| 302 | |
| 303 | def test_PerFileCompression(self): |
| 304 | # Check that files within a Zip archive can have different compression options |
| 305 | zipfp = zipfile.ZipFile(TESTFN2, "w") |
| 306 | zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED) |
| 307 | zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED) |
| 308 | sinfo = zipfp.getinfo('storeme') |
| 309 | dinfo = zipfp.getinfo('deflateme') |
| 310 | self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED) |
| 311 | self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED) |
| 312 | zipfp.close() |
| 313 | |
| 314 | def test_WriteToReadonly(self): |
| 315 | # Check that trying to call write() on a readonly ZipFile object |
| 316 | # raises a RuntimeError |
| 317 | zipf = zipfile.ZipFile(TESTFN2, mode="w") |
| 318 | zipf.writestr("somefile.txt", "bogus") |
| 319 | zipf.close() |
| 320 | zipf = zipfile.ZipFile(TESTFN2, mode="r") |
| 321 | self.assertRaises(RuntimeError, zipf.write, TESTFN) |
| 322 | zipf.close() |
| 323 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 324 | def testExtract(self): |
| 325 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
| 326 | for fpath, fdata in SMALL_TEST_DATA: |
| 327 | zipfp.writestr(fpath, fdata) |
| 328 | zipfp.close() |
| 329 | |
| 330 | zipfp = zipfile.ZipFile(TESTFN2, "r") |
| 331 | for fpath, fdata in SMALL_TEST_DATA: |
| 332 | writtenfile = zipfp.extract(fpath) |
| 333 | |
| 334 | # make sure it was written to the right place |
| 335 | if os.path.isabs(fpath): |
| 336 | correctfile = os.path.join(os.getcwd(), fpath[1:]) |
| 337 | else: |
| 338 | correctfile = os.path.join(os.getcwd(), fpath) |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 339 | correctfile = os.path.normpath(correctfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 340 | |
| 341 | self.assertEqual(writtenfile, correctfile) |
| 342 | |
| 343 | # make sure correct data is in correct file |
| 344 | self.assertEqual(fdata.encode(), open(writtenfile, "rb").read()) |
| 345 | |
| 346 | os.remove(writtenfile) |
| 347 | |
| 348 | zipfp.close() |
| 349 | |
| 350 | # remove the test file subdirectories |
| 351 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 352 | |
| 353 | def testExtractAll(self): |
| 354 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
| 355 | for fpath, fdata in SMALL_TEST_DATA: |
| 356 | zipfp.writestr(fpath, fdata) |
| 357 | zipfp.close() |
| 358 | |
| 359 | zipfp = zipfile.ZipFile(TESTFN2, "r") |
| 360 | zipfp.extractall() |
| 361 | for fpath, fdata in SMALL_TEST_DATA: |
| 362 | if os.path.isabs(fpath): |
| 363 | outfile = os.path.join(os.getcwd(), fpath[1:]) |
| 364 | else: |
| 365 | outfile = os.path.join(os.getcwd(), fpath) |
| 366 | |
| 367 | self.assertEqual(fdata.encode(), open(outfile, "rb").read()) |
| 368 | |
| 369 | os.remove(outfile) |
| 370 | |
| 371 | zipfp.close() |
| 372 | |
| 373 | # remove the test file subdirectories |
| 374 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 375 | |
Antoine Pitrou | 6e1df8d | 2008-07-25 19:58:18 +0000 | [diff] [blame] | 376 | def zip_test_writestr_permissions(self, f, compression): |
| 377 | # Make sure that writestr creates files with mode 0600, |
| 378 | # when it is passed a name rather than a ZipInfo instance. |
| 379 | |
| 380 | self.makeTestArchive(f, compression) |
| 381 | zipfp = zipfile.ZipFile(f, "r") |
| 382 | zinfo = zipfp.getinfo('strfile') |
| 383 | self.assertEqual(zinfo.external_attr, 0o600 << 16) |
| 384 | |
| 385 | def test_writestr_permissions(self): |
| 386 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 387 | self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED) |
| 388 | |
Georg Brandl | df47515 | 2009-08-13 09:05:38 +0000 | [diff] [blame] | 389 | def test_writestr_extended_local_header_issue1202(self): |
| 390 | orig_zip = zipfile.ZipFile(TESTFN2, 'w') |
| 391 | for data in 'abcdefghijklmnop': |
| 392 | zinfo = zipfile.ZipInfo(data) |
| 393 | zinfo.flag_bits |= 0x08 # Include an extended local header. |
| 394 | orig_zip.writestr(zinfo, data) |
| 395 | orig_zip.close() |
| 396 | |
Victor Stinner | ff1d2f4 | 2011-05-18 13:43:23 +0200 | [diff] [blame] | 397 | def test_unicode_filenames(self): |
| 398 | if __name__ == '__main__': |
| 399 | myfile = sys.argv[0] |
| 400 | else: |
| 401 | myfile = __file__ |
| 402 | |
| 403 | mydir = os.path.dirname(myfile) or os.curdir |
| 404 | fname = os.path.join(mydir, 'zip_cp437_header.zip') |
| 405 | |
Victor Stinner | ff1d2f4 | 2011-05-18 13:43:23 +0200 | [diff] [blame] | 406 | zipfp = zipfile.ZipFile(fname) |
| 407 | try: |
| 408 | zipfp.extractall() |
| 409 | finally: |
| 410 | zipfp.close() |
| 411 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 412 | def tearDown(self): |
Victor Stinner | ff1d2f4 | 2011-05-18 13:43:23 +0200 | [diff] [blame] | 413 | support.unlink(TESTFN) |
| 414 | support.unlink(TESTFN2) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 415 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 416 | class TestZip64InSmallFiles(unittest.TestCase): |
| 417 | # These tests test the ZIP64 functionality without using large files, |
| 418 | # see test_zipfile64 for proper tests. |
| 419 | |
| 420 | def setUp(self): |
| 421 | self._limit = zipfile.ZIP64_LIMIT |
| 422 | zipfile.ZIP64_LIMIT = 5 |
| 423 | |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 424 | line_gen = (bytes("Test of zipfile line %d." % i, "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 425 | for i in range(0, FIXEDTEST_SIZE)) |
| 426 | self.data = b'\n'.join(line_gen) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 427 | |
| 428 | # Make a source file with some lines |
| 429 | fp = open(TESTFN, "wb") |
| 430 | fp.write(self.data) |
| 431 | fp.close() |
| 432 | |
| 433 | def largeFileExceptionTest(self, f, compression): |
| 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.write, TESTFN, "another.name") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 437 | zipfp.close() |
| 438 | |
| 439 | def largeFileExceptionTest2(self, f, compression): |
| 440 | zipfp = zipfile.ZipFile(f, "w", compression) |
| 441 | self.assertRaises(zipfile.LargeZipFile, |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 442 | zipfp.writestr, "another.name", self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 443 | zipfp.close() |
| 444 | |
| 445 | def testLargeFileException(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 446 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 447 | self.largeFileExceptionTest(f, zipfile.ZIP_STORED) |
| 448 | self.largeFileExceptionTest2(f, zipfile.ZIP_STORED) |
| 449 | |
| 450 | def zipTest(self, f, compression): |
| 451 | # Create the ZIP archive |
| 452 | zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 453 | zipfp.write(TESTFN, "another.name") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 454 | zipfp.write(TESTFN, TESTFN) |
| 455 | zipfp.writestr("strfile", self.data) |
| 456 | zipfp.close() |
| 457 | |
| 458 | # Read the ZIP archive |
| 459 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 460 | self.assertEqual(zipfp.read(TESTFN), self.data) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 461 | self.assertEqual(zipfp.read("another.name"), self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 462 | self.assertEqual(zipfp.read("strfile"), self.data) |
| 463 | |
| 464 | # Print the ZIP directory |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 465 | fp = io.StringIO() |
| 466 | zipfp.printdir(fp) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 467 | |
| 468 | directory = fp.getvalue() |
| 469 | lines = directory.splitlines() |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 470 | self.assertEqual(len(lines), 4) # Number of files + header |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 471 | |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 472 | self.assertTrue('File Name' in lines[0]) |
| 473 | self.assertTrue('Modified' in lines[0]) |
| 474 | self.assertTrue('Size' in lines[0]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 475 | |
| 476 | fn, date, time, size = lines[1].split() |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 477 | self.assertEqual(fn, 'another.name') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 478 | # XXX: timestamp is not tested |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 479 | self.assertEqual(size, str(len(self.data))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 480 | |
| 481 | # Check the namelist |
| 482 | names = zipfp.namelist() |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 483 | self.assertEqual(len(names), 3) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 484 | self.assertTrue(TESTFN in names) |
| 485 | self.assertTrue("another.name" in names) |
| 486 | self.assertTrue("strfile" in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 487 | |
| 488 | # Check infolist |
| 489 | infos = zipfp.infolist() |
| 490 | names = [ i.filename for i in infos ] |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 491 | self.assertEqual(len(names), 3) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 492 | self.assertTrue(TESTFN in names) |
| 493 | self.assertTrue("another.name" in names) |
| 494 | self.assertTrue("strfile" in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 495 | for i in infos: |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 496 | self.assertEqual(i.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 497 | |
| 498 | # check getinfo |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 499 | for nm in (TESTFN, "another.name", "strfile"): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 500 | info = zipfp.getinfo(nm) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 501 | self.assertEqual(info.filename, nm) |
| 502 | self.assertEqual(info.file_size, len(self.data)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 503 | |
| 504 | # Check that testzip doesn't raise an exception |
| 505 | zipfp.testzip() |
| 506 | |
| 507 | |
| 508 | zipfp.close() |
| 509 | |
| 510 | def testStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 511 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 512 | self.zipTest(f, zipfile.ZIP_STORED) |
| 513 | |
| 514 | |
| 515 | if zlib: |
| 516 | def testDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 517 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 518 | self.zipTest(f, zipfile.ZIP_DEFLATED) |
| 519 | |
| 520 | def testAbsoluteArcnames(self): |
| 521 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True) |
| 522 | zipfp.write(TESTFN, "/absolute") |
| 523 | zipfp.close() |
| 524 | |
| 525 | zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) |
| 526 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
| 527 | zipfp.close() |
| 528 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 529 | def tearDown(self): |
| 530 | zipfile.ZIP64_LIMIT = self._limit |
| 531 | os.remove(TESTFN) |
| 532 | os.remove(TESTFN2) |
| 533 | |
| 534 | class PyZipFileTests(unittest.TestCase): |
| 535 | def testWritePyfile(self): |
| 536 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 537 | fn = __file__ |
| 538 | if fn.endswith('.pyc') or fn.endswith('.pyo'): |
| 539 | fn = fn[:-1] |
| 540 | |
| 541 | zipfp.writepy(fn) |
| 542 | |
| 543 | bn = os.path.basename(fn) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 544 | self.assertTrue(bn not in zipfp.namelist()) |
| 545 | 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] | 546 | zipfp.close() |
| 547 | |
| 548 | |
| 549 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 550 | fn = __file__ |
| 551 | if fn.endswith('.pyc') or fn.endswith('.pyo'): |
| 552 | fn = fn[:-1] |
| 553 | |
| 554 | zipfp.writepy(fn, "testpackage") |
| 555 | |
| 556 | bn = "%s/%s"%("testpackage", os.path.basename(fn)) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 557 | self.assertTrue(bn not in zipfp.namelist()) |
| 558 | 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] | 559 | zipfp.close() |
| 560 | |
| 561 | def testWritePythonPackage(self): |
| 562 | import email |
| 563 | packagedir = os.path.dirname(email.__file__) |
| 564 | |
| 565 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 566 | zipfp.writepy(packagedir) |
| 567 | |
| 568 | # Check for a couple of modules at different levels of the hieararchy |
| 569 | names = zipfp.namelist() |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 570 | self.assertTrue('email/__init__.pyo' in names or 'email/__init__.pyc' in names) |
| 571 | 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] | 572 | |
| 573 | def testWritePythonDirectory(self): |
| 574 | os.mkdir(TESTFN2) |
| 575 | try: |
| 576 | fp = open(os.path.join(TESTFN2, "mod1.py"), "w") |
Guido van Rossum | 43fc78d | 2007-02-09 22:18:41 +0000 | [diff] [blame] | 577 | fp.write("print(42)\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 578 | fp.close() |
| 579 | |
| 580 | fp = open(os.path.join(TESTFN2, "mod2.py"), "w") |
Guido van Rossum | 43fc78d | 2007-02-09 22:18:41 +0000 | [diff] [blame] | 581 | fp.write("print(42 * 42)\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 582 | fp.close() |
| 583 | |
| 584 | fp = open(os.path.join(TESTFN2, "mod2.txt"), "w") |
| 585 | fp.write("bla bla bla\n") |
| 586 | fp.close() |
| 587 | |
| 588 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 589 | zipfp.writepy(TESTFN2) |
| 590 | |
| 591 | names = zipfp.namelist() |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 592 | self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names) |
| 593 | self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names) |
| 594 | self.assertTrue('mod2.txt' not in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 595 | |
| 596 | finally: |
| 597 | shutil.rmtree(TESTFN2) |
| 598 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 599 | def testWriteNonPyfile(self): |
| 600 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 601 | open(TESTFN, 'w').write('most definitely not a python file') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 602 | self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) |
| 603 | os.remove(TESTFN) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 604 | |
| 605 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 606 | class OtherTests(unittest.TestCase): |
Antoine Pitrou | 5f2a7bc | 2010-08-12 15:30:13 +0000 | [diff] [blame] | 607 | zips_with_bad_crc = { |
| 608 | zipfile.ZIP_STORED: ( |
| 609 | b'PK\003\004\024\0\0\0\0\0 \213\212;:r' |
| 610 | b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af' |
| 611 | b'ilehello,AworldP' |
| 612 | b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:' |
| 613 | b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0' |
| 614 | b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi' |
| 615 | b'lePK\005\006\0\0\0\0\001\0\001\0003\000' |
| 616 | b'\0\0/\0\0\0\0\0'), |
| 617 | zipfile.ZIP_DEFLATED: ( |
| 618 | b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA' |
| 619 | b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' |
| 620 | b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0' |
| 621 | b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n' |
| 622 | b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05' |
| 623 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00' |
| 624 | b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00' |
| 625 | b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'), |
| 626 | } |
| 627 | |
Martin v. Löwis | 8570f6a | 2008-05-05 17:44:38 +0000 | [diff] [blame] | 628 | def testUnicodeFilenames(self): |
| 629 | zf = zipfile.ZipFile(TESTFN, "w") |
| 630 | zf.writestr("foo.txt", "Test for unicode filename") |
Martin v. Löwis | 1a9f900 | 2008-05-05 17:50:05 +0000 | [diff] [blame] | 631 | zf.writestr("\xf6.txt", "Test for unicode filename") |
Martin v. Löwis | 8570f6a | 2008-05-05 17:44:38 +0000 | [diff] [blame] | 632 | zf.close() |
Martin v. Löwis | 1a9f900 | 2008-05-05 17:50:05 +0000 | [diff] [blame] | 633 | zf = zipfile.ZipFile(TESTFN, "r") |
| 634 | self.assertEqual(zf.filelist[0].filename, "foo.txt") |
| 635 | self.assertEqual(zf.filelist[1].filename, "\xf6.txt") |
| 636 | zf.close() |
Martin v. Löwis | 8570f6a | 2008-05-05 17:44:38 +0000 | [diff] [blame] | 637 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 638 | def testCreateNonExistentFileForAppend(self): |
| 639 | if os.path.exists(TESTFN): |
| 640 | os.unlink(TESTFN) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 641 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 642 | filename = 'testfile.txt' |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 643 | content = b'hello, world. this is some content.' |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 644 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 645 | try: |
| 646 | zf = zipfile.ZipFile(TESTFN, 'a') |
| 647 | zf.writestr(filename, content) |
| 648 | zf.close() |
| 649 | except IOError: |
| 650 | self.fail('Could not append data to a non-existent zip file.') |
| 651 | |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 652 | self.assertTrue(os.path.exists(TESTFN)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 653 | |
| 654 | zf = zipfile.ZipFile(TESTFN, 'r') |
| 655 | self.assertEqual(zf.read(filename), content) |
| 656 | zf.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 657 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 658 | def testCloseErroneousFile(self): |
| 659 | # This test checks that the ZipFile constructor closes the file object |
| 660 | # it opens if there's an error in the file. If it doesn't, the traceback |
| 661 | # holds a reference to the ZipFile object and, indirectly, the file object. |
| 662 | # On Windows, this causes the os.unlink() call to fail because the |
| 663 | # underlying file is still open. This is SF bug #412214. |
| 664 | # |
| 665 | fp = open(TESTFN, "w") |
| 666 | fp.write("this is not a legal zip file\n") |
| 667 | fp.close() |
| 668 | try: |
| 669 | zf = zipfile.ZipFile(TESTFN) |
| 670 | except zipfile.BadZipfile: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 671 | pass |
| 672 | |
| 673 | def testIsZipErroneousFile(self): |
| 674 | # This test checks that the is_zipfile function correctly identifies |
| 675 | # a file that is not a zip file |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 676 | |
| 677 | # - passing a filename |
| 678 | with open(TESTFN, "w") as fp: |
| 679 | fp.write("this is not a legal zip file\n") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 680 | chk = zipfile.is_zipfile(TESTFN) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 681 | self.assertTrue(not chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 682 | # - passing a file object |
| 683 | with open(TESTFN, "rb") as fp: |
| 684 | chk = zipfile.is_zipfile(fp) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 685 | self.assertTrue(not chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 686 | # - passing a file-like object |
| 687 | fp = io.BytesIO() |
| 688 | fp.write(b"this is not a legal zip file\n") |
| 689 | chk = zipfile.is_zipfile(fp) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 690 | self.assertTrue(not chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 691 | fp.seek(0,0) |
| 692 | chk = zipfile.is_zipfile(fp) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 693 | self.assertTrue(not chk) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 694 | |
| 695 | def testIsZipValidFile(self): |
| 696 | # This test checks that the is_zipfile function correctly identifies |
| 697 | # a file that is a zip file |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 698 | |
| 699 | # - passing a filename |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 700 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 701 | zipf.writestr("foo.txt", b"O, for a Muse of Fire!") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 702 | zipf.close() |
| 703 | chk = zipfile.is_zipfile(TESTFN) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 704 | self.assertTrue(chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 705 | # - passing a file object |
| 706 | with open(TESTFN, "rb") as fp: |
| 707 | chk = zipfile.is_zipfile(fp) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 708 | self.assertTrue(chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 709 | fp.seek(0,0) |
| 710 | zip_contents = fp.read() |
| 711 | # - passing a file-like object |
| 712 | fp = io.BytesIO() |
| 713 | fp.write(zip_contents) |
| 714 | chk = zipfile.is_zipfile(fp) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 715 | self.assertTrue(chk) |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 716 | fp.seek(0,0) |
| 717 | chk = zipfile.is_zipfile(fp) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 718 | self.assertTrue(chk) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 719 | |
| 720 | def testNonExistentFileRaisesIOError(self): |
| 721 | # make sure we don't raise an AttributeError when a partially-constructed |
| 722 | # ZipFile instance is finalized; this tests for regression on SF tracker |
| 723 | # bug #403871. |
| 724 | |
| 725 | # The bug we're testing for caused an AttributeError to be raised |
| 726 | # when a ZipFile instance was created for a file that did not |
| 727 | # exist; the .fp member was not initialized but was needed by the |
| 728 | # __del__() method. Since the AttributeError is in the __del__(), |
| 729 | # it is ignored, but the user should be sufficiently annoyed by |
| 730 | # the message on the output that regression will be noticed |
| 731 | # quickly. |
| 732 | self.assertRaises(IOError, zipfile.ZipFile, TESTFN) |
| 733 | |
R. David Murray | 93a5965 | 2010-01-06 20:12:07 +0000 | [diff] [blame] | 734 | def test_empty_file_raises_BadZipFile(self): |
| 735 | f = open(TESTFN, 'w') |
| 736 | f.close() |
| 737 | self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN) |
| 738 | |
| 739 | f = open(TESTFN, 'w') |
| 740 | f.write("short file") |
| 741 | f.close() |
| 742 | self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN) |
| 743 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 744 | def testClosedZipRaisesRuntimeError(self): |
| 745 | # Verify that testzip() doesn't swallow inappropriate exceptions. |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 746 | data = io.BytesIO() |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 747 | zipf = zipfile.ZipFile(data, mode="w") |
| 748 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 749 | zipf.close() |
| 750 | |
| 751 | # This is correct; calling .read on a closed ZipFile should throw |
| 752 | # a RuntimeError, and so should calling .testzip. An earlier |
| 753 | # version of .testzip would swallow this exception (and any other) |
| 754 | # and report that the first file in the archive was corrupt. |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 755 | self.assertRaises(RuntimeError, zipf.read, "foo.txt") |
| 756 | self.assertRaises(RuntimeError, zipf.open, "foo.txt") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 757 | self.assertRaises(RuntimeError, zipf.testzip) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 758 | self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus") |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 759 | open(TESTFN, 'w').write('zipfile test data') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 760 | self.assertRaises(RuntimeError, zipf.write, TESTFN) |
| 761 | |
| 762 | def test_BadConstructorMode(self): |
| 763 | # Check that bad modes passed to ZipFile constructor are caught |
| 764 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q") |
| 765 | |
| 766 | def test_BadOpenMode(self): |
| 767 | # Check that bad modes passed to ZipFile.open are caught |
| 768 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 769 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 770 | zipf.close() |
| 771 | zipf = zipfile.ZipFile(TESTFN, mode="r") |
| 772 | # read the data to make sure the file is there |
| 773 | zipf.read("foo.txt") |
| 774 | self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q") |
| 775 | zipf.close() |
| 776 | |
| 777 | def test_Read0(self): |
| 778 | # Check that calling read(0) on a ZipExtFile object returns an empty |
| 779 | # string and doesn't advance file pointer |
| 780 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 781 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 782 | # read the data to make sure the file is there |
| 783 | f = zipf.open("foo.txt") |
| 784 | for i in range(FIXEDTEST_SIZE): |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 785 | self.assertEqual(f.read(0), b'') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 786 | |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 787 | self.assertEqual(f.read(), b"O, for a Muse of Fire!") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 788 | zipf.close() |
| 789 | |
| 790 | def test_OpenNonexistentItem(self): |
| 791 | # Check that attempting to call open() for an item that doesn't |
| 792 | # exist in the archive raises a RuntimeError |
| 793 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 794 | self.assertRaises(KeyError, zipf.open, "foo.txt", "r") |
| 795 | |
| 796 | def test_BadCompressionMode(self): |
| 797 | # Check that bad compression methods passed to ZipFile.open are caught |
| 798 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1) |
| 799 | |
| 800 | def test_NullByteInFilename(self): |
| 801 | # Check that a filename containing a null byte is properly terminated |
| 802 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 803 | 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] | 804 | self.assertEqual(zipf.namelist(), ['foo.txt']) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 805 | |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 806 | def test_StructSizes(self): |
| 807 | # check that ZIP internal structure sizes are calculated correctly |
| 808 | self.assertEqual(zipfile.sizeEndCentDir, 22) |
| 809 | self.assertEqual(zipfile.sizeCentralDir, 46) |
| 810 | self.assertEqual(zipfile.sizeEndCentDir64, 56) |
| 811 | self.assertEqual(zipfile.sizeEndCentDir64Locator, 20) |
| 812 | |
| 813 | def testComments(self): |
| 814 | # This test checks that comments on the archive are handled properly |
| 815 | |
| 816 | # check default comment is empty |
| 817 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 818 | self.assertEqual(zipf.comment, b'') |
| 819 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 820 | zipf.close() |
| 821 | zipfr = zipfile.ZipFile(TESTFN, mode="r") |
| 822 | self.assertEqual(zipfr.comment, b'') |
| 823 | zipfr.close() |
| 824 | |
| 825 | # check a simple short comment |
| 826 | comment = b'Bravely taking to his feet, he beat a very brave retreat.' |
| 827 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 828 | zipf.comment = comment |
| 829 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 830 | zipf.close() |
| 831 | zipfr = zipfile.ZipFile(TESTFN, mode="r") |
| 832 | self.assertEqual(zipfr.comment, comment) |
| 833 | zipfr.close() |
| 834 | |
| 835 | # check a comment of max length |
| 836 | comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)]) |
| 837 | comment2 = comment2.encode("ascii") |
| 838 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 839 | zipf.comment = comment2 |
| 840 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 841 | zipf.close() |
| 842 | zipfr = zipfile.ZipFile(TESTFN, mode="r") |
| 843 | self.assertEqual(zipfr.comment, comment2) |
| 844 | zipfr.close() |
| 845 | |
| 846 | # check a comment that is too long is truncated |
| 847 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 848 | zipf.comment = comment2 + b'oops' |
| 849 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 850 | zipf.close() |
| 851 | zipfr = zipfile.ZipFile(TESTFN, mode="r") |
| 852 | self.assertEqual(zipfr.comment, comment2) |
| 853 | zipfr.close() |
| 854 | |
Antoine Pitrou | 5f2a7bc | 2010-08-12 15:30:13 +0000 | [diff] [blame] | 855 | def check_testzip_with_bad_crc(self, compression): |
| 856 | """Tests that files with bad CRCs return their name from testzip.""" |
| 857 | zipdata = self.zips_with_bad_crc[compression] |
| 858 | |
| 859 | zipf = zipfile.ZipFile(io.BytesIO(zipdata), mode="r") |
| 860 | # testzip returns the name of the first corrupt file, or None |
| 861 | self.assertEqual('afile', zipf.testzip()) |
| 862 | zipf.close() |
| 863 | |
| 864 | def test_testzip_with_bad_crc_stored(self): |
| 865 | self.check_testzip_with_bad_crc(zipfile.ZIP_STORED) |
| 866 | |
| 867 | if zlib: |
| 868 | def test_testzip_with_bad_crc_deflated(self): |
| 869 | self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED) |
| 870 | |
| 871 | def check_read_with_bad_crc(self, compression): |
| 872 | """Tests that files with bad CRCs raise a BadZipfile exception when read.""" |
| 873 | zipdata = self.zips_with_bad_crc[compression] |
| 874 | |
| 875 | # Using ZipFile.read() |
| 876 | zipf = zipfile.ZipFile(io.BytesIO(zipdata), mode="r") |
| 877 | self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile') |
| 878 | zipf.close() |
| 879 | |
| 880 | # Using ZipExtFile.read() |
| 881 | zipf = zipfile.ZipFile(io.BytesIO(zipdata), mode="r") |
| 882 | corrupt_file = zipf.open('afile', 'r') |
| 883 | self.assertRaises(zipfile.BadZipfile, corrupt_file.read) |
| 884 | corrupt_file.close() |
| 885 | zipf.close() |
| 886 | |
| 887 | # Same with small reads (in order to exercise the buffering logic) |
| 888 | zipf = zipfile.ZipFile(io.BytesIO(zipdata), mode="r") |
| 889 | corrupt_file = zipf.open('afile', 'r') |
| 890 | corrupt_file.MIN_READ_SIZE = 2 |
| 891 | with self.assertRaises(zipfile.BadZipfile): |
| 892 | while corrupt_file.read(2): |
| 893 | pass |
| 894 | corrupt_file.close() |
| 895 | zipf.close() |
| 896 | |
| 897 | def test_read_with_bad_crc_stored(self): |
| 898 | self.check_read_with_bad_crc(zipfile.ZIP_STORED) |
| 899 | |
| 900 | if zlib: |
| 901 | def test_read_with_bad_crc_deflated(self): |
| 902 | self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED) |
| 903 | |
Antoine Pitrou | 1052e39 | 2010-09-12 14:55:22 +0000 | [diff] [blame] | 904 | def check_read_return_size(self, compression): |
| 905 | # Issue #9837: ZipExtFile.read() shouldn't return more bytes |
| 906 | # than requested. |
| 907 | for test_size in (1, 4095, 4096, 4097, 16384): |
| 908 | file_size = test_size + 1 |
| 909 | junk = b''.join(struct.pack('B', randint(0, 255)) |
| 910 | for x in range(file_size)) |
| 911 | zipf = zipfile.ZipFile(io.BytesIO(), "w", compression) |
| 912 | try: |
| 913 | zipf.writestr('foo', junk) |
| 914 | fp = zipf.open('foo', 'r') |
| 915 | buf = fp.read(test_size) |
| 916 | self.assertEqual(len(buf), test_size) |
| 917 | finally: |
| 918 | zipf.close() |
| 919 | |
| 920 | def test_read_return_size_stored(self): |
| 921 | self.check_read_return_size(zipfile.ZIP_STORED) |
| 922 | |
| 923 | if zlib: |
| 924 | def test_read_return_size_deflated(self): |
| 925 | self.check_read_return_size(zipfile.ZIP_DEFLATED) |
| 926 | |
Georg Brandl | aba9796 | 2010-11-26 08:37:46 +0000 | [diff] [blame] | 927 | def test_empty_zipfile(self): |
| 928 | # Check that creating a file in 'w' or 'a' mode and closing without |
| 929 | # adding any files to the archives creates a valid empty ZIP file |
| 930 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 931 | zipf.close() |
| 932 | try: |
| 933 | zipf = zipfile.ZipFile(TESTFN, mode="r") |
Éric Araujo | a172e25 | 2011-02-02 16:58:43 +0000 | [diff] [blame] | 934 | except zipfile.BadZipfile: |
Georg Brandl | aba9796 | 2010-11-26 08:37:46 +0000 | [diff] [blame] | 935 | self.fail("Unable to create empty ZIP file in 'w' mode") |
| 936 | |
| 937 | zipf = zipfile.ZipFile(TESTFN, mode="a") |
| 938 | zipf.close() |
| 939 | try: |
| 940 | zipf = zipfile.ZipFile(TESTFN, mode="r") |
| 941 | except: |
| 942 | self.fail("Unable to create empty ZIP file in 'a' mode") |
| 943 | |
| 944 | def test_open_empty_file(self): |
| 945 | # Issue 1710703: Check that opening a file with less than 22 bytes |
| 946 | # raises a BadZipfile exception (rather than the previously unhelpful |
| 947 | # IOError) |
| 948 | f = open(TESTFN, 'w') |
| 949 | f.close() |
| 950 | self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r') |
| 951 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 952 | def tearDown(self): |
| 953 | support.unlink(TESTFN) |
| 954 | support.unlink(TESTFN2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 955 | |
| 956 | class DecryptionTests(unittest.TestCase): |
| 957 | # This test checks that ZIP decryption works. Since the library does not |
| 958 | # support encryption at the moment, we use a pre-generated encrypted |
| 959 | # ZIP file |
| 960 | |
| 961 | data = ( |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 962 | b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00' |
| 963 | b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y' |
| 964 | b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl' |
| 965 | b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00' |
| 966 | b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81' |
| 967 | b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00' |
| 968 | b'\x00\x00L\x00\x00\x00\x00\x00' ) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 969 | data2 = ( |
| 970 | b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02' |
| 971 | b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04' |
| 972 | b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0' |
| 973 | b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03' |
| 974 | b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00' |
| 975 | b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze' |
| 976 | b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01' |
| 977 | b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' ) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 978 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 979 | plain = b'zipfile.py encryption test' |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 980 | plain2 = b'\x00'*512 |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 981 | |
| 982 | def setUp(self): |
| 983 | fp = open(TESTFN, "wb") |
| 984 | fp.write(self.data) |
| 985 | fp.close() |
| 986 | self.zip = zipfile.ZipFile(TESTFN, "r") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 987 | fp = open(TESTFN2, "wb") |
| 988 | fp.write(self.data2) |
| 989 | fp.close() |
| 990 | self.zip2 = zipfile.ZipFile(TESTFN2, "r") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 991 | |
| 992 | def tearDown(self): |
| 993 | self.zip.close() |
| 994 | os.unlink(TESTFN) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 995 | self.zip2.close() |
| 996 | os.unlink(TESTFN2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 997 | |
| 998 | def testNoPassword(self): |
| 999 | # Reading the encrypted file without password |
| 1000 | # must generate a RunTime exception |
| 1001 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1002 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1003 | |
| 1004 | def testBadPassword(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1005 | self.zip.setpassword(b"perl") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1006 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1007 | self.zip2.setpassword(b"perl") |
| 1008 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1009 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1010 | def testGoodPassword(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1011 | self.zip.setpassword(b"python") |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 1012 | self.assertEqual(self.zip.read("test.txt"), self.plain) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1013 | self.zip2.setpassword(b"12345") |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 1014 | self.assertEqual(self.zip2.read("zero"), self.plain2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1015 | |
R. David Murray | 30f9c8c | 2010-12-21 21:57:54 +0000 | [diff] [blame] | 1016 | def test_unicode_password(self): |
| 1017 | self.assertRaises(TypeError, self.zip.setpassword, "unicode") |
| 1018 | self.assertRaises(TypeError, self.zip.read, "test.txt", "python") |
| 1019 | self.assertRaises(TypeError, self.zip.open, "test.txt", pwd="python") |
| 1020 | self.assertRaises(TypeError, self.zip.extract, "test.txt", pwd="python") |
| 1021 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1022 | |
| 1023 | class TestsWithRandomBinaryFiles(unittest.TestCase): |
| 1024 | def setUp(self): |
| 1025 | datacount = randint(16, 64)*1024 + randint(1, 1024) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1026 | self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000)) |
| 1027 | for i in range(datacount)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1028 | |
| 1029 | # Make a source file with some lines |
| 1030 | fp = open(TESTFN, "wb") |
| 1031 | fp.write(self.data) |
| 1032 | fp.close() |
| 1033 | |
| 1034 | def tearDown(self): |
| 1035 | support.unlink(TESTFN) |
| 1036 | support.unlink(TESTFN2) |
| 1037 | |
| 1038 | def makeTestArchive(self, f, compression): |
| 1039 | # Create the ZIP archive |
| 1040 | zipfp = zipfile.ZipFile(f, "w", compression) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 1041 | zipfp.write(TESTFN, "another.name") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1042 | zipfp.write(TESTFN, TESTFN) |
| 1043 | zipfp.close() |
| 1044 | |
| 1045 | def zipTest(self, f, compression): |
| 1046 | self.makeTestArchive(f, compression) |
| 1047 | |
| 1048 | # Read the ZIP archive |
| 1049 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 1050 | testdata = zipfp.read(TESTFN) |
| 1051 | self.assertEqual(len(testdata), len(self.data)) |
| 1052 | self.assertEqual(testdata, self.data) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 1053 | self.assertEqual(zipfp.read("another.name"), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1054 | zipfp.close() |
| 1055 | |
| 1056 | def testStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1057 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1058 | self.zipTest(f, zipfile.ZIP_STORED) |
| 1059 | |
| 1060 | def zipOpenTest(self, f, compression): |
| 1061 | self.makeTestArchive(f, compression) |
| 1062 | |
| 1063 | # Read the ZIP archive |
| 1064 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 1065 | zipdata1 = [] |
| 1066 | zipopen1 = zipfp.open(TESTFN) |
| 1067 | while 1: |
| 1068 | read_data = zipopen1.read(256) |
| 1069 | if not read_data: |
| 1070 | break |
| 1071 | zipdata1.append(read_data) |
| 1072 | |
| 1073 | zipdata2 = [] |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 1074 | zipopen2 = zipfp.open("another.name") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1075 | while 1: |
| 1076 | read_data = zipopen2.read(256) |
| 1077 | if not read_data: |
| 1078 | break |
| 1079 | zipdata2.append(read_data) |
| 1080 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1081 | testdata1 = b''.join(zipdata1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1082 | self.assertEqual(len(testdata1), len(self.data)) |
| 1083 | self.assertEqual(testdata1, self.data) |
| 1084 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1085 | testdata2 = b''.join(zipdata2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1086 | self.assertEqual(len(testdata1), len(self.data)) |
| 1087 | self.assertEqual(testdata1, self.data) |
| 1088 | zipfp.close() |
| 1089 | |
| 1090 | def testOpenStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1091 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1092 | self.zipOpenTest(f, zipfile.ZIP_STORED) |
| 1093 | |
| 1094 | def zipRandomOpenTest(self, f, compression): |
| 1095 | self.makeTestArchive(f, compression) |
| 1096 | |
| 1097 | # Read the ZIP archive |
| 1098 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 1099 | zipdata1 = [] |
| 1100 | zipopen1 = zipfp.open(TESTFN) |
| 1101 | while 1: |
| 1102 | read_data = zipopen1.read(randint(1, 1024)) |
| 1103 | if not read_data: |
| 1104 | break |
| 1105 | zipdata1.append(read_data) |
| 1106 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1107 | testdata = b''.join(zipdata1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1108 | self.assertEqual(len(testdata), len(self.data)) |
| 1109 | self.assertEqual(testdata, self.data) |
| 1110 | zipfp.close() |
| 1111 | |
| 1112 | def testRandomOpenStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1113 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1114 | self.zipRandomOpenTest(f, zipfile.ZIP_STORED) |
| 1115 | |
| 1116 | class TestsWithMultipleOpens(unittest.TestCase): |
| 1117 | def setUp(self): |
| 1118 | # Create the ZIP archive |
| 1119 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) |
| 1120 | zipfp.writestr('ones', '1'*FIXEDTEST_SIZE) |
| 1121 | zipfp.writestr('twos', '2'*FIXEDTEST_SIZE) |
| 1122 | zipfp.close() |
| 1123 | |
| 1124 | def testSameFile(self): |
| 1125 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1126 | # multiple open() calls can be made without interfering with each other. |
| 1127 | zipf = zipfile.ZipFile(TESTFN2, mode="r") |
| 1128 | zopen1 = zipf.open('ones') |
| 1129 | zopen2 = zipf.open('ones') |
| 1130 | data1 = zopen1.read(500) |
| 1131 | data2 = zopen2.read(500) |
| 1132 | data1 += zopen1.read(500) |
| 1133 | data2 += zopen2.read(500) |
| 1134 | self.assertEqual(data1, data2) |
| 1135 | zipf.close() |
| 1136 | |
| 1137 | def testDifferentFile(self): |
| 1138 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1139 | # multiple open() calls can be made without interfering with each other. |
| 1140 | zipf = zipfile.ZipFile(TESTFN2, mode="r") |
| 1141 | zopen1 = zipf.open('ones') |
| 1142 | zopen2 = zipf.open('twos') |
| 1143 | data1 = zopen1.read(500) |
| 1144 | data2 = zopen2.read(500) |
| 1145 | data1 += zopen1.read(500) |
| 1146 | data2 += zopen2.read(500) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1147 | self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) |
| 1148 | self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1149 | zipf.close() |
| 1150 | |
| 1151 | def testInterleaved(self): |
| 1152 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1153 | # multiple open() calls can be made without interfering with each other. |
| 1154 | zipf = zipfile.ZipFile(TESTFN2, mode="r") |
| 1155 | zopen1 = zipf.open('ones') |
| 1156 | data1 = zopen1.read(500) |
| 1157 | zopen2 = zipf.open('twos') |
| 1158 | data2 = zopen2.read(500) |
| 1159 | data1 += zopen1.read(500) |
| 1160 | data2 += zopen2.read(500) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1161 | self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) |
| 1162 | self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1163 | zipf.close() |
| 1164 | |
| 1165 | def tearDown(self): |
| 1166 | os.remove(TESTFN2) |
| 1167 | |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1168 | class TestWithDirectory(unittest.TestCase): |
| 1169 | def setUp(self): |
| 1170 | os.mkdir(TESTFN2) |
| 1171 | |
| 1172 | def testExtractDir(self): |
| 1173 | zipf = zipfile.ZipFile(findfile("zipdir.zip")) |
| 1174 | zipf.extractall(TESTFN2) |
| 1175 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a"))) |
| 1176 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b"))) |
| 1177 | self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c"))) |
| 1178 | |
Martin v. Löwis | 70ccd16 | 2009-05-24 19:47:22 +0000 | [diff] [blame] | 1179 | def test_bug_6050(self): |
| 1180 | # Extraction should succeed if directories already exist |
| 1181 | os.mkdir(os.path.join(TESTFN2, "a")) |
| 1182 | self.testExtractDir() |
| 1183 | |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1184 | def testStoreDir(self): |
| 1185 | os.mkdir(os.path.join(TESTFN2, "x")) |
| 1186 | zipf = zipfile.ZipFile(TESTFN, "w") |
| 1187 | zipf.write(os.path.join(TESTFN2, "x"), "x") |
| 1188 | self.assertTrue(zipf.filelist[0].filename.endswith("x/")) |
| 1189 | |
| 1190 | def tearDown(self): |
| 1191 | shutil.rmtree(TESTFN2) |
| 1192 | if os.path.exists(TESTFN): |
| 1193 | os.remove(TESTFN) |
| 1194 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1195 | |
| 1196 | class UniversalNewlineTests(unittest.TestCase): |
| 1197 | def setUp(self): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 1198 | self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1199 | for i in range(FIXEDTEST_SIZE)] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1200 | self.seps = ('\r', '\r\n', '\n') |
| 1201 | self.arcdata, self.arcfiles = {}, {} |
| 1202 | for n, s in enumerate(self.seps): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1203 | b = s.encode("ascii") |
| 1204 | self.arcdata[s] = b.join(self.line_gen) + b |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1205 | self.arcfiles[s] = '%s-%d' % (TESTFN, n) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1206 | f = open(self.arcfiles[s], "wb") |
| 1207 | try: |
| 1208 | f.write(self.arcdata[s]) |
| 1209 | finally: |
| 1210 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1211 | |
| 1212 | def makeTestArchive(self, f, compression): |
| 1213 | # Create the ZIP archive |
| 1214 | zipfp = zipfile.ZipFile(f, "w", compression) |
| 1215 | for fn in self.arcfiles.values(): |
| 1216 | zipfp.write(fn, fn) |
| 1217 | zipfp.close() |
| 1218 | |
| 1219 | def readTest(self, f, compression): |
| 1220 | self.makeTestArchive(f, compression) |
| 1221 | |
| 1222 | # Read the ZIP archive |
| 1223 | zipfp = zipfile.ZipFile(f, "r") |
| 1224 | for sep, fn in self.arcfiles.items(): |
| 1225 | zipdata = zipfp.open(fn, "rU").read() |
| 1226 | self.assertEqual(self.arcdata[sep], zipdata) |
| 1227 | |
| 1228 | zipfp.close() |
| 1229 | |
| 1230 | def readlineTest(self, f, compression): |
| 1231 | self.makeTestArchive(f, compression) |
| 1232 | |
| 1233 | # Read the ZIP archive |
| 1234 | zipfp = zipfile.ZipFile(f, "r") |
| 1235 | for sep, fn in self.arcfiles.items(): |
| 1236 | zipopen = zipfp.open(fn, "rU") |
| 1237 | for line in self.line_gen: |
| 1238 | linedata = zipopen.readline() |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1239 | self.assertEqual(linedata, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1240 | |
| 1241 | zipfp.close() |
| 1242 | |
| 1243 | def readlinesTest(self, f, compression): |
| 1244 | self.makeTestArchive(f, compression) |
| 1245 | |
| 1246 | # Read the ZIP archive |
| 1247 | zipfp = zipfile.ZipFile(f, "r") |
| 1248 | for sep, fn in self.arcfiles.items(): |
| 1249 | ziplines = zipfp.open(fn, "rU").readlines() |
| 1250 | for line, zipline in zip(self.line_gen, ziplines): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1251 | self.assertEqual(zipline, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1252 | |
| 1253 | zipfp.close() |
| 1254 | |
| 1255 | def iterlinesTest(self, f, compression): |
| 1256 | self.makeTestArchive(f, compression) |
| 1257 | |
| 1258 | # Read the ZIP archive |
| 1259 | zipfp = zipfile.ZipFile(f, "r") |
| 1260 | for sep, fn in self.arcfiles.items(): |
| 1261 | 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] | 1262 | self.assertEqual(zipline, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1263 | |
| 1264 | zipfp.close() |
| 1265 | |
| 1266 | def testReadStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1267 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1268 | self.readTest(f, zipfile.ZIP_STORED) |
| 1269 | |
| 1270 | def testReadlineStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1271 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1272 | self.readlineTest(f, zipfile.ZIP_STORED) |
| 1273 | |
| 1274 | def testReadlinesStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1275 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1276 | self.readlinesTest(f, zipfile.ZIP_STORED) |
| 1277 | |
| 1278 | def testIterlinesStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1279 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1280 | self.iterlinesTest(f, zipfile.ZIP_STORED) |
| 1281 | |
| 1282 | if zlib: |
| 1283 | def testReadDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1284 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1285 | self.readTest(f, zipfile.ZIP_DEFLATED) |
| 1286 | |
| 1287 | def testReadlineDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1288 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1289 | self.readlineTest(f, zipfile.ZIP_DEFLATED) |
| 1290 | |
| 1291 | def testReadlinesDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1292 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1293 | self.readlinesTest(f, zipfile.ZIP_DEFLATED) |
| 1294 | |
| 1295 | def testIterlinesDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1296 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1297 | self.iterlinesTest(f, zipfile.ZIP_DEFLATED) |
| 1298 | |
| 1299 | def tearDown(self): |
| 1300 | for sep, fn in self.arcfiles.items(): |
| 1301 | os.remove(fn) |
| 1302 | support.unlink(TESTFN) |
| 1303 | support.unlink(TESTFN2) |
| 1304 | |
| 1305 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1306 | def test_main(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1307 | run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests, |
| 1308 | PyZipFileTests, DecryptionTests, TestsWithMultipleOpens, |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1309 | TestWithDirectory, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1310 | UniversalNewlineTests, TestsWithRandomBinaryFiles) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1311 | |
| 1312 | if __name__ == "__main__": |
| 1313 | test_main() |