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 |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 6 | import zipfile, os, unittest, sys, shutil, struct, io |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 7 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 8 | from tempfile import TemporaryFile |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 9 | from random import randint, random |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 11 | import test.test_support as support |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 12 | from test.test_support import TESTFN, run_unittest |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 13 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 14 | TESTFN2 = TESTFN + "2" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 15 | FIXEDTEST_SIZE = 1000 |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 16 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 17 | SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'), |
| 18 | ('ziptest2dir/_ziptest2', 'qawsedrftg'), |
| 19 | ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'), |
| 20 | ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')] |
| 21 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 22 | class TestsWithSourceFile(unittest.TestCase): |
| 23 | def setUp(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 24 | self.line_gen = (bytes("Zipfile test line %d. random float: %f" % |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 25 | (i, random()), "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 26 | for i in range(FIXEDTEST_SIZE)) |
| 27 | self.data = b'\n'.join(self.line_gen) + b'\n' |
Fred Drake | 6e7e485 | 2001-02-28 05:34:16 +0000 | [diff] [blame] | 28 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 29 | # Make a source file with some lines |
| 30 | fp = open(TESTFN, "wb") |
| 31 | fp.write(self.data) |
| 32 | fp.close() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 34 | def makeTestArchive(self, f, compression): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 35 | # Create the ZIP archive |
| 36 | zipfp = zipfile.ZipFile(f, "w", compression) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 37 | zipfp.write(TESTFN, "another.name") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 38 | zipfp.write(TESTFN, TESTFN) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 39 | zipfp.writestr("strfile", self.data) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 40 | zipfp.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 zipTest(self, f, compression): |
| 43 | self.makeTestArchive(f, compression) |
| 44 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 45 | # Read the ZIP archive |
| 46 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 47 | self.assertEqual(zipfp.read(TESTFN), self.data) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 48 | self.assertEqual(zipfp.read("another.name"), self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 49 | self.assertEqual(zipfp.read("strfile"), self.data) |
| 50 | |
| 51 | # Print the ZIP directory |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 52 | fp = io.StringIO() |
| 53 | zipfp.printdir(file=fp) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 54 | |
| 55 | directory = fp.getvalue() |
| 56 | lines = directory.splitlines() |
| 57 | self.assertEquals(len(lines), 4) # Number of files + header |
| 58 | |
| 59 | self.assert_('File Name' in lines[0]) |
| 60 | self.assert_('Modified' in lines[0]) |
| 61 | self.assert_('Size' in lines[0]) |
| 62 | |
| 63 | fn, date, time, size = lines[1].split() |
| 64 | self.assertEquals(fn, 'another.name') |
| 65 | # XXX: timestamp is not tested |
| 66 | self.assertEquals(size, str(len(self.data))) |
| 67 | |
| 68 | # Check the namelist |
| 69 | names = zipfp.namelist() |
| 70 | self.assertEquals(len(names), 3) |
| 71 | self.assert_(TESTFN in names) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 72 | self.assert_("another.name" in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 73 | self.assert_("strfile" in names) |
| 74 | |
| 75 | # Check infolist |
| 76 | infos = zipfp.infolist() |
| 77 | names = [ i.filename for i in infos ] |
| 78 | self.assertEquals(len(names), 3) |
| 79 | self.assert_(TESTFN in names) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 80 | self.assert_("another.name" in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 81 | self.assert_("strfile" in names) |
| 82 | for i in infos: |
| 83 | self.assertEquals(i.file_size, len(self.data)) |
| 84 | |
| 85 | # check getinfo |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 86 | for nm in (TESTFN, "another.name", "strfile"): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 87 | info = zipfp.getinfo(nm) |
| 88 | self.assertEquals(info.filename, nm) |
| 89 | self.assertEquals(info.file_size, len(self.data)) |
| 90 | |
| 91 | # Check that testzip doesn't raise an exception |
| 92 | zipfp.testzip() |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 93 | zipfp.close() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 94 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 95 | def testStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 96 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 97 | self.zipTest(f, zipfile.ZIP_STORED) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 99 | def zipOpenTest(self, f, compression): |
| 100 | self.makeTestArchive(f, compression) |
| 101 | |
| 102 | # Read the ZIP archive |
| 103 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 104 | zipdata1 = [] |
| 105 | zipopen1 = zipfp.open(TESTFN) |
| 106 | while 1: |
| 107 | read_data = zipopen1.read(256) |
| 108 | if not read_data: |
| 109 | break |
| 110 | zipdata1.append(read_data) |
| 111 | |
| 112 | zipdata2 = [] |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 113 | zipopen2 = zipfp.open("another.name") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 114 | while 1: |
| 115 | read_data = zipopen2.read(256) |
| 116 | if not read_data: |
| 117 | break |
| 118 | zipdata2.append(read_data) |
| 119 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 120 | self.assertEqual(b''.join(zipdata1), self.data) |
| 121 | self.assertEqual(b''.join(zipdata2), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 122 | zipfp.close() |
| 123 | |
| 124 | def testOpenStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 125 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 126 | self.zipOpenTest(f, zipfile.ZIP_STORED) |
| 127 | |
| 128 | def zipRandomOpenTest(self, f, compression): |
| 129 | self.makeTestArchive(f, compression) |
| 130 | |
| 131 | # Read the ZIP archive |
| 132 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 133 | zipdata1 = [] |
| 134 | zipopen1 = zipfp.open(TESTFN) |
| 135 | while 1: |
| 136 | read_data = zipopen1.read(randint(1, 1024)) |
| 137 | if not read_data: |
| 138 | break |
| 139 | zipdata1.append(read_data) |
| 140 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 141 | self.assertEqual(b''.join(zipdata1), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 142 | zipfp.close() |
| 143 | |
| 144 | def testRandomOpenStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 145 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 146 | self.zipRandomOpenTest(f, zipfile.ZIP_STORED) |
| 147 | |
| 148 | def zipReadlineTest(self, f, compression): |
| 149 | self.makeTestArchive(f, compression) |
| 150 | |
| 151 | # Read the ZIP archive |
| 152 | zipfp = zipfile.ZipFile(f, "r") |
| 153 | zipopen = zipfp.open(TESTFN) |
| 154 | for line in self.line_gen: |
| 155 | linedata = zipopen.readline() |
| 156 | self.assertEqual(linedata, line + '\n') |
| 157 | |
| 158 | zipfp.close() |
| 159 | |
| 160 | def zipReadlinesTest(self, f, compression): |
| 161 | self.makeTestArchive(f, compression) |
| 162 | |
| 163 | # Read the ZIP archive |
| 164 | zipfp = zipfile.ZipFile(f, "r") |
| 165 | ziplines = zipfp.open(TESTFN).readlines() |
| 166 | for line, zipline in zip(self.line_gen, ziplines): |
| 167 | self.assertEqual(zipline, line + '\n') |
| 168 | |
| 169 | zipfp.close() |
| 170 | |
| 171 | def zipIterlinesTest(self, f, compression): |
| 172 | self.makeTestArchive(f, compression) |
| 173 | |
| 174 | # Read the ZIP archive |
| 175 | zipfp = zipfile.ZipFile(f, "r") |
| 176 | for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)): |
| 177 | self.assertEqual(zipline, line + '\n') |
| 178 | |
| 179 | zipfp.close() |
| 180 | |
| 181 | def testReadlineStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 182 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 183 | self.zipReadlineTest(f, zipfile.ZIP_STORED) |
| 184 | |
| 185 | def testReadlinesStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 186 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 187 | self.zipReadlinesTest(f, zipfile.ZIP_STORED) |
| 188 | |
| 189 | def testIterlinesStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 190 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 191 | self.zipIterlinesTest(f, zipfile.ZIP_STORED) |
| 192 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 193 | if zlib: |
| 194 | def testDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 195 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 196 | self.zipTest(f, zipfile.ZIP_DEFLATED) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 197 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 198 | def testOpenDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 199 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 200 | self.zipOpenTest(f, zipfile.ZIP_DEFLATED) |
| 201 | |
| 202 | def testRandomOpenDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 203 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 204 | self.zipRandomOpenTest(f, zipfile.ZIP_DEFLATED) |
| 205 | |
| 206 | def testReadlineDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 207 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 208 | self.zipReadlineTest(f, zipfile.ZIP_DEFLATED) |
| 209 | |
| 210 | def testReadlinesDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 211 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 212 | self.zipReadlinesTest(f, zipfile.ZIP_DEFLATED) |
| 213 | |
| 214 | def testIterlinesDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 215 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 216 | self.zipIterlinesTest(f, zipfile.ZIP_DEFLATED) |
| 217 | |
| 218 | def testLowCompression(self): |
| 219 | # Checks for cases where compressed data is larger than original |
| 220 | # Create the ZIP archive |
| 221 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) |
| 222 | zipfp.writestr("strfile", '12') |
| 223 | zipfp.close() |
| 224 | |
| 225 | # Get an open object for strfile |
| 226 | zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) |
| 227 | openobj = zipfp.open("strfile") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 228 | self.assertEqual(openobj.read(1), b'1') |
| 229 | self.assertEqual(openobj.read(1), b'2') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 230 | |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame] | 231 | def testAbsoluteArcnames(self): |
| 232 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
| 233 | zipfp.write(TESTFN, "/absolute") |
| 234 | zipfp.close() |
| 235 | |
| 236 | zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) |
| 237 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
| 238 | zipfp.close() |
Tim Peters | 32cbc96 | 2006-02-20 21:42:18 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 240 | def testAppendToZipFile(self): |
| 241 | # Test appending to an existing zipfile |
| 242 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
| 243 | zipfp.write(TESTFN, TESTFN) |
| 244 | zipfp.close() |
| 245 | zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) |
| 246 | zipfp.writestr("strfile", self.data) |
| 247 | self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"]) |
| 248 | zipfp.close() |
| 249 | |
| 250 | def testAppendToNonZipFile(self): |
| 251 | # Test appending to an existing file that is not a zipfile |
| 252 | # NOTE: this test fails if len(d) < 22 because of the first |
| 253 | # line "fpin.seek(-22, 2)" in _EndRecData |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 254 | d = b'I am not a ZipFile!'*10 |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 255 | f = open(TESTFN2, 'wb') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 256 | f.write(d) |
| 257 | f.close() |
| 258 | zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) |
| 259 | zipfp.write(TESTFN, TESTFN) |
| 260 | zipfp.close() |
| 261 | |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 262 | f = open(TESTFN2, 'rb') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 263 | f.seek(len(d)) |
| 264 | zipfp = zipfile.ZipFile(f, "r") |
| 265 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
| 266 | zipfp.close() |
| 267 | f.close() |
| 268 | |
| 269 | def test_WriteDefaultName(self): |
| 270 | # Check that calling ZipFile.write without arcname specified produces the expected result |
| 271 | zipfp = zipfile.ZipFile(TESTFN2, "w") |
| 272 | zipfp.write(TESTFN) |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 273 | self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read()) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 274 | zipfp.close() |
| 275 | |
| 276 | def test_PerFileCompression(self): |
| 277 | # Check that files within a Zip archive can have different compression options |
| 278 | zipfp = zipfile.ZipFile(TESTFN2, "w") |
| 279 | zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED) |
| 280 | zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED) |
| 281 | sinfo = zipfp.getinfo('storeme') |
| 282 | dinfo = zipfp.getinfo('deflateme') |
| 283 | self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED) |
| 284 | self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED) |
| 285 | zipfp.close() |
| 286 | |
| 287 | def test_WriteToReadonly(self): |
| 288 | # Check that trying to call write() on a readonly ZipFile object |
| 289 | # raises a RuntimeError |
| 290 | zipf = zipfile.ZipFile(TESTFN2, mode="w") |
| 291 | zipf.writestr("somefile.txt", "bogus") |
| 292 | zipf.close() |
| 293 | zipf = zipfile.ZipFile(TESTFN2, mode="r") |
| 294 | self.assertRaises(RuntimeError, zipf.write, TESTFN) |
| 295 | zipf.close() |
| 296 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 297 | def testExtract(self): |
| 298 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
| 299 | for fpath, fdata in SMALL_TEST_DATA: |
| 300 | zipfp.writestr(fpath, fdata) |
| 301 | zipfp.close() |
| 302 | |
| 303 | zipfp = zipfile.ZipFile(TESTFN2, "r") |
| 304 | for fpath, fdata in SMALL_TEST_DATA: |
| 305 | writtenfile = zipfp.extract(fpath) |
| 306 | |
| 307 | # make sure it was written to the right place |
| 308 | if os.path.isabs(fpath): |
| 309 | correctfile = os.path.join(os.getcwd(), fpath[1:]) |
| 310 | else: |
| 311 | correctfile = os.path.join(os.getcwd(), fpath) |
| 312 | |
| 313 | self.assertEqual(writtenfile, correctfile) |
| 314 | |
| 315 | # make sure correct data is in correct file |
| 316 | self.assertEqual(fdata.encode(), open(writtenfile, "rb").read()) |
| 317 | |
| 318 | os.remove(writtenfile) |
| 319 | |
| 320 | zipfp.close() |
| 321 | |
| 322 | # remove the test file subdirectories |
| 323 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 324 | |
| 325 | def testExtractAll(self): |
| 326 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
| 327 | for fpath, fdata in SMALL_TEST_DATA: |
| 328 | zipfp.writestr(fpath, fdata) |
| 329 | zipfp.close() |
| 330 | |
| 331 | zipfp = zipfile.ZipFile(TESTFN2, "r") |
| 332 | zipfp.extractall() |
| 333 | for fpath, fdata in SMALL_TEST_DATA: |
| 334 | if os.path.isabs(fpath): |
| 335 | outfile = os.path.join(os.getcwd(), fpath[1:]) |
| 336 | else: |
| 337 | outfile = os.path.join(os.getcwd(), fpath) |
| 338 | |
| 339 | self.assertEqual(fdata.encode(), open(outfile, "rb").read()) |
| 340 | |
| 341 | os.remove(outfile) |
| 342 | |
| 343 | zipfp.close() |
| 344 | |
| 345 | # remove the test file subdirectories |
| 346 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 347 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 348 | def tearDown(self): |
| 349 | os.remove(TESTFN) |
| 350 | os.remove(TESTFN2) |
| 351 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 352 | class TestZip64InSmallFiles(unittest.TestCase): |
| 353 | # These tests test the ZIP64 functionality without using large files, |
| 354 | # see test_zipfile64 for proper tests. |
| 355 | |
| 356 | def setUp(self): |
| 357 | self._limit = zipfile.ZIP64_LIMIT |
| 358 | zipfile.ZIP64_LIMIT = 5 |
| 359 | |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 360 | line_gen = (bytes("Test of zipfile line %d." % i, "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 361 | for i in range(0, FIXEDTEST_SIZE)) |
| 362 | self.data = b'\n'.join(line_gen) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 363 | |
| 364 | # Make a source file with some lines |
| 365 | fp = open(TESTFN, "wb") |
| 366 | fp.write(self.data) |
| 367 | fp.close() |
| 368 | |
| 369 | def largeFileExceptionTest(self, f, compression): |
| 370 | zipfp = zipfile.ZipFile(f, "w", compression) |
| 371 | self.assertRaises(zipfile.LargeZipFile, |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 372 | zipfp.write, TESTFN, "another.name") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 373 | zipfp.close() |
| 374 | |
| 375 | def largeFileExceptionTest2(self, f, compression): |
| 376 | zipfp = zipfile.ZipFile(f, "w", compression) |
| 377 | self.assertRaises(zipfile.LargeZipFile, |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 378 | zipfp.writestr, "another.name", self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 379 | zipfp.close() |
| 380 | |
| 381 | def testLargeFileException(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 382 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 383 | self.largeFileExceptionTest(f, zipfile.ZIP_STORED) |
| 384 | self.largeFileExceptionTest2(f, zipfile.ZIP_STORED) |
| 385 | |
| 386 | def zipTest(self, f, compression): |
| 387 | # Create the ZIP archive |
| 388 | zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 389 | zipfp.write(TESTFN, "another.name") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 390 | zipfp.write(TESTFN, TESTFN) |
| 391 | zipfp.writestr("strfile", self.data) |
| 392 | zipfp.close() |
| 393 | |
| 394 | # Read the ZIP archive |
| 395 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 396 | self.assertEqual(zipfp.read(TESTFN), self.data) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 397 | self.assertEqual(zipfp.read("another.name"), self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 398 | self.assertEqual(zipfp.read("strfile"), self.data) |
| 399 | |
| 400 | # Print the ZIP directory |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 401 | fp = io.StringIO() |
| 402 | zipfp.printdir(fp) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 403 | |
| 404 | directory = fp.getvalue() |
| 405 | lines = directory.splitlines() |
| 406 | self.assertEquals(len(lines), 4) # Number of files + header |
| 407 | |
| 408 | self.assert_('File Name' in lines[0]) |
| 409 | self.assert_('Modified' in lines[0]) |
| 410 | self.assert_('Size' in lines[0]) |
| 411 | |
| 412 | fn, date, time, size = lines[1].split() |
| 413 | self.assertEquals(fn, 'another.name') |
| 414 | # XXX: timestamp is not tested |
| 415 | self.assertEquals(size, str(len(self.data))) |
| 416 | |
| 417 | # Check the namelist |
| 418 | names = zipfp.namelist() |
| 419 | self.assertEquals(len(names), 3) |
| 420 | self.assert_(TESTFN in names) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 421 | self.assert_("another.name" in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 422 | self.assert_("strfile" in names) |
| 423 | |
| 424 | # Check infolist |
| 425 | infos = zipfp.infolist() |
| 426 | names = [ i.filename for i in infos ] |
| 427 | self.assertEquals(len(names), 3) |
| 428 | self.assert_(TESTFN in names) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 429 | self.assert_("another.name" in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 430 | self.assert_("strfile" in names) |
| 431 | for i in infos: |
| 432 | self.assertEquals(i.file_size, len(self.data)) |
| 433 | |
| 434 | # check getinfo |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 435 | for nm in (TESTFN, "another.name", "strfile"): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 436 | info = zipfp.getinfo(nm) |
| 437 | self.assertEquals(info.filename, nm) |
| 438 | self.assertEquals(info.file_size, len(self.data)) |
| 439 | |
| 440 | # Check that testzip doesn't raise an exception |
| 441 | zipfp.testzip() |
| 442 | |
| 443 | |
| 444 | zipfp.close() |
| 445 | |
| 446 | def testStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 447 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 448 | self.zipTest(f, zipfile.ZIP_STORED) |
| 449 | |
| 450 | |
| 451 | if zlib: |
| 452 | def testDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 453 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 454 | self.zipTest(f, zipfile.ZIP_DEFLATED) |
| 455 | |
| 456 | def testAbsoluteArcnames(self): |
| 457 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True) |
| 458 | zipfp.write(TESTFN, "/absolute") |
| 459 | zipfp.close() |
| 460 | |
| 461 | zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) |
| 462 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
| 463 | zipfp.close() |
| 464 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 465 | def tearDown(self): |
| 466 | zipfile.ZIP64_LIMIT = self._limit |
| 467 | os.remove(TESTFN) |
| 468 | os.remove(TESTFN2) |
| 469 | |
| 470 | class PyZipFileTests(unittest.TestCase): |
| 471 | def testWritePyfile(self): |
| 472 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 473 | fn = __file__ |
| 474 | if fn.endswith('.pyc') or fn.endswith('.pyo'): |
| 475 | fn = fn[:-1] |
| 476 | |
| 477 | zipfp.writepy(fn) |
| 478 | |
| 479 | bn = os.path.basename(fn) |
| 480 | self.assert_(bn not in zipfp.namelist()) |
| 481 | self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist()) |
| 482 | zipfp.close() |
| 483 | |
| 484 | |
| 485 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 486 | fn = __file__ |
| 487 | if fn.endswith('.pyc') or fn.endswith('.pyo'): |
| 488 | fn = fn[:-1] |
| 489 | |
| 490 | zipfp.writepy(fn, "testpackage") |
| 491 | |
| 492 | bn = "%s/%s"%("testpackage", os.path.basename(fn)) |
| 493 | self.assert_(bn not in zipfp.namelist()) |
| 494 | self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist()) |
| 495 | zipfp.close() |
| 496 | |
| 497 | def testWritePythonPackage(self): |
| 498 | import email |
| 499 | packagedir = os.path.dirname(email.__file__) |
| 500 | |
| 501 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 502 | zipfp.writepy(packagedir) |
| 503 | |
| 504 | # Check for a couple of modules at different levels of the hieararchy |
| 505 | names = zipfp.namelist() |
| 506 | self.assert_('email/__init__.pyo' in names or 'email/__init__.pyc' in names) |
| 507 | self.assert_('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names) |
| 508 | |
| 509 | def testWritePythonDirectory(self): |
| 510 | os.mkdir(TESTFN2) |
| 511 | try: |
| 512 | fp = open(os.path.join(TESTFN2, "mod1.py"), "w") |
Guido van Rossum | 43fc78d | 2007-02-09 22:18:41 +0000 | [diff] [blame] | 513 | fp.write("print(42)\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 514 | fp.close() |
| 515 | |
| 516 | fp = open(os.path.join(TESTFN2, "mod2.py"), "w") |
Guido van Rossum | 43fc78d | 2007-02-09 22:18:41 +0000 | [diff] [blame] | 517 | fp.write("print(42 * 42)\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 518 | fp.close() |
| 519 | |
| 520 | fp = open(os.path.join(TESTFN2, "mod2.txt"), "w") |
| 521 | fp.write("bla bla bla\n") |
| 522 | fp.close() |
| 523 | |
| 524 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 525 | zipfp.writepy(TESTFN2) |
| 526 | |
| 527 | names = zipfp.namelist() |
| 528 | self.assert_('mod1.pyc' in names or 'mod1.pyo' in names) |
| 529 | self.assert_('mod2.pyc' in names or 'mod2.pyo' in names) |
| 530 | self.assert_('mod2.txt' not in names) |
| 531 | |
| 532 | finally: |
| 533 | shutil.rmtree(TESTFN2) |
| 534 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 535 | def testWriteNonPyfile(self): |
| 536 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 537 | open(TESTFN, 'w').write('most definitely not a python file') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 538 | self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) |
| 539 | os.remove(TESTFN) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 540 | |
| 541 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 542 | class OtherTests(unittest.TestCase): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 543 | def testCreateNonExistentFileForAppend(self): |
| 544 | if os.path.exists(TESTFN): |
| 545 | os.unlink(TESTFN) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 546 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 547 | filename = 'testfile.txt' |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 548 | content = b'hello, world. this is some content.' |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 549 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 550 | try: |
| 551 | zf = zipfile.ZipFile(TESTFN, 'a') |
| 552 | zf.writestr(filename, content) |
| 553 | zf.close() |
| 554 | except IOError: |
| 555 | self.fail('Could not append data to a non-existent zip file.') |
| 556 | |
| 557 | self.assert_(os.path.exists(TESTFN)) |
| 558 | |
| 559 | zf = zipfile.ZipFile(TESTFN, 'r') |
| 560 | self.assertEqual(zf.read(filename), content) |
| 561 | zf.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 562 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 563 | def testCloseErroneousFile(self): |
| 564 | # This test checks that the ZipFile constructor closes the file object |
| 565 | # it opens if there's an error in the file. If it doesn't, the traceback |
| 566 | # holds a reference to the ZipFile object and, indirectly, the file object. |
| 567 | # On Windows, this causes the os.unlink() call to fail because the |
| 568 | # underlying file is still open. This is SF bug #412214. |
| 569 | # |
| 570 | fp = open(TESTFN, "w") |
| 571 | fp.write("this is not a legal zip file\n") |
| 572 | fp.close() |
| 573 | try: |
| 574 | zf = zipfile.ZipFile(TESTFN) |
| 575 | except zipfile.BadZipfile: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 576 | pass |
| 577 | |
| 578 | def testIsZipErroneousFile(self): |
| 579 | # This test checks that the is_zipfile function correctly identifies |
| 580 | # a file that is not a zip file |
| 581 | fp = open(TESTFN, "w") |
| 582 | fp.write("this is not a legal zip file\n") |
| 583 | fp.close() |
| 584 | chk = zipfile.is_zipfile(TESTFN) |
| 585 | self.assert_(chk is False) |
| 586 | |
| 587 | def testIsZipValidFile(self): |
| 588 | # This test checks that the is_zipfile function correctly identifies |
| 589 | # a file that is a zip file |
| 590 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 591 | zipf.writestr("foo.txt", b"O, for a Muse of Fire!") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 592 | zipf.close() |
| 593 | chk = zipfile.is_zipfile(TESTFN) |
| 594 | self.assert_(chk is True) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 595 | |
| 596 | def testNonExistentFileRaisesIOError(self): |
| 597 | # make sure we don't raise an AttributeError when a partially-constructed |
| 598 | # ZipFile instance is finalized; this tests for regression on SF tracker |
| 599 | # bug #403871. |
| 600 | |
| 601 | # The bug we're testing for caused an AttributeError to be raised |
| 602 | # when a ZipFile instance was created for a file that did not |
| 603 | # exist; the .fp member was not initialized but was needed by the |
| 604 | # __del__() method. Since the AttributeError is in the __del__(), |
| 605 | # it is ignored, but the user should be sufficiently annoyed by |
| 606 | # the message on the output that regression will be noticed |
| 607 | # quickly. |
| 608 | self.assertRaises(IOError, zipfile.ZipFile, TESTFN) |
| 609 | |
| 610 | def testClosedZipRaisesRuntimeError(self): |
| 611 | # Verify that testzip() doesn't swallow inappropriate exceptions. |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 612 | data = io.BytesIO() |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 613 | zipf = zipfile.ZipFile(data, mode="w") |
| 614 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 615 | zipf.close() |
| 616 | |
| 617 | # This is correct; calling .read on a closed ZipFile should throw |
| 618 | # a RuntimeError, and so should calling .testzip. An earlier |
| 619 | # version of .testzip would swallow this exception (and any other) |
| 620 | # and report that the first file in the archive was corrupt. |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 621 | self.assertRaises(RuntimeError, zipf.read, "foo.txt") |
| 622 | self.assertRaises(RuntimeError, zipf.open, "foo.txt") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 623 | self.assertRaises(RuntimeError, zipf.testzip) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 624 | self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus") |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 625 | open(TESTFN, 'w').write('zipfile test data') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 626 | self.assertRaises(RuntimeError, zipf.write, TESTFN) |
| 627 | |
| 628 | def test_BadConstructorMode(self): |
| 629 | # Check that bad modes passed to ZipFile constructor are caught |
| 630 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q") |
| 631 | |
| 632 | def test_BadOpenMode(self): |
| 633 | # Check that bad modes passed to ZipFile.open are caught |
| 634 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 635 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 636 | zipf.close() |
| 637 | zipf = zipfile.ZipFile(TESTFN, mode="r") |
| 638 | # read the data to make sure the file is there |
| 639 | zipf.read("foo.txt") |
| 640 | self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q") |
| 641 | zipf.close() |
| 642 | |
| 643 | def test_Read0(self): |
| 644 | # Check that calling read(0) on a ZipExtFile object returns an empty |
| 645 | # string and doesn't advance file pointer |
| 646 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 647 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 648 | # read the data to make sure the file is there |
| 649 | f = zipf.open("foo.txt") |
| 650 | for i in range(FIXEDTEST_SIZE): |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 651 | self.assertEqual(f.read(0), b'') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 652 | |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 653 | self.assertEqual(f.read(), b"O, for a Muse of Fire!") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 654 | zipf.close() |
| 655 | |
| 656 | def test_OpenNonexistentItem(self): |
| 657 | # Check that attempting to call open() for an item that doesn't |
| 658 | # exist in the archive raises a RuntimeError |
| 659 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 660 | self.assertRaises(KeyError, zipf.open, "foo.txt", "r") |
| 661 | |
| 662 | def test_BadCompressionMode(self): |
| 663 | # Check that bad compression methods passed to ZipFile.open are caught |
| 664 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1) |
| 665 | |
| 666 | def test_NullByteInFilename(self): |
| 667 | # Check that a filename containing a null byte is properly terminated |
| 668 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 669 | 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] | 670 | self.assertEqual(zipf.namelist(), ['foo.txt']) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 671 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 672 | def tearDown(self): |
| 673 | support.unlink(TESTFN) |
| 674 | support.unlink(TESTFN2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 675 | |
| 676 | class DecryptionTests(unittest.TestCase): |
| 677 | # This test checks that ZIP decryption works. Since the library does not |
| 678 | # support encryption at the moment, we use a pre-generated encrypted |
| 679 | # ZIP file |
| 680 | |
| 681 | data = ( |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 682 | b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00' |
| 683 | b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y' |
| 684 | b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl' |
| 685 | b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00' |
| 686 | b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81' |
| 687 | b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00' |
| 688 | b'\x00\x00L\x00\x00\x00\x00\x00' ) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 689 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 690 | plain = b'zipfile.py encryption test' |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 691 | |
| 692 | def setUp(self): |
| 693 | fp = open(TESTFN, "wb") |
| 694 | fp.write(self.data) |
| 695 | fp.close() |
| 696 | self.zip = zipfile.ZipFile(TESTFN, "r") |
| 697 | |
| 698 | def tearDown(self): |
| 699 | self.zip.close() |
| 700 | os.unlink(TESTFN) |
| 701 | |
| 702 | def testNoPassword(self): |
| 703 | # Reading the encrypted file without password |
| 704 | # must generate a RunTime exception |
| 705 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
| 706 | |
| 707 | def testBadPassword(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 708 | self.zip.setpassword(b"perl") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 709 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 710 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 711 | def testGoodPassword(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 712 | self.zip.setpassword(b"python") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 713 | self.assertEquals(self.zip.read("test.txt"), self.plain) |
| 714 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 715 | |
| 716 | class TestsWithRandomBinaryFiles(unittest.TestCase): |
| 717 | def setUp(self): |
| 718 | datacount = randint(16, 64)*1024 + randint(1, 1024) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 719 | self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000)) |
| 720 | for i in range(datacount)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 721 | |
| 722 | # Make a source file with some lines |
| 723 | fp = open(TESTFN, "wb") |
| 724 | fp.write(self.data) |
| 725 | fp.close() |
| 726 | |
| 727 | def tearDown(self): |
| 728 | support.unlink(TESTFN) |
| 729 | support.unlink(TESTFN2) |
| 730 | |
| 731 | def makeTestArchive(self, f, compression): |
| 732 | # Create the ZIP archive |
| 733 | zipfp = zipfile.ZipFile(f, "w", compression) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 734 | zipfp.write(TESTFN, "another.name") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 735 | zipfp.write(TESTFN, TESTFN) |
| 736 | zipfp.close() |
| 737 | |
| 738 | def zipTest(self, f, compression): |
| 739 | self.makeTestArchive(f, compression) |
| 740 | |
| 741 | # Read the ZIP archive |
| 742 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 743 | testdata = zipfp.read(TESTFN) |
| 744 | self.assertEqual(len(testdata), len(self.data)) |
| 745 | self.assertEqual(testdata, self.data) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 746 | self.assertEqual(zipfp.read("another.name"), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 747 | zipfp.close() |
| 748 | |
| 749 | def testStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 750 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 751 | self.zipTest(f, zipfile.ZIP_STORED) |
| 752 | |
| 753 | def zipOpenTest(self, f, compression): |
| 754 | self.makeTestArchive(f, compression) |
| 755 | |
| 756 | # Read the ZIP archive |
| 757 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 758 | zipdata1 = [] |
| 759 | zipopen1 = zipfp.open(TESTFN) |
| 760 | while 1: |
| 761 | read_data = zipopen1.read(256) |
| 762 | if not read_data: |
| 763 | break |
| 764 | zipdata1.append(read_data) |
| 765 | |
| 766 | zipdata2 = [] |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 767 | zipopen2 = zipfp.open("another.name") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 768 | while 1: |
| 769 | read_data = zipopen2.read(256) |
| 770 | if not read_data: |
| 771 | break |
| 772 | zipdata2.append(read_data) |
| 773 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 774 | testdata1 = b''.join(zipdata1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 775 | self.assertEqual(len(testdata1), len(self.data)) |
| 776 | self.assertEqual(testdata1, self.data) |
| 777 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 778 | testdata2 = b''.join(zipdata2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 779 | self.assertEqual(len(testdata1), len(self.data)) |
| 780 | self.assertEqual(testdata1, self.data) |
| 781 | zipfp.close() |
| 782 | |
| 783 | def testOpenStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 784 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 785 | self.zipOpenTest(f, zipfile.ZIP_STORED) |
| 786 | |
| 787 | def zipRandomOpenTest(self, f, compression): |
| 788 | self.makeTestArchive(f, compression) |
| 789 | |
| 790 | # Read the ZIP archive |
| 791 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 792 | zipdata1 = [] |
| 793 | zipopen1 = zipfp.open(TESTFN) |
| 794 | while 1: |
| 795 | read_data = zipopen1.read(randint(1, 1024)) |
| 796 | if not read_data: |
| 797 | break |
| 798 | zipdata1.append(read_data) |
| 799 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 800 | testdata = b''.join(zipdata1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 801 | self.assertEqual(len(testdata), len(self.data)) |
| 802 | self.assertEqual(testdata, self.data) |
| 803 | zipfp.close() |
| 804 | |
| 805 | def testRandomOpenStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 806 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 807 | self.zipRandomOpenTest(f, zipfile.ZIP_STORED) |
| 808 | |
| 809 | class TestsWithMultipleOpens(unittest.TestCase): |
| 810 | def setUp(self): |
| 811 | # Create the ZIP archive |
| 812 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) |
| 813 | zipfp.writestr('ones', '1'*FIXEDTEST_SIZE) |
| 814 | zipfp.writestr('twos', '2'*FIXEDTEST_SIZE) |
| 815 | zipfp.close() |
| 816 | |
| 817 | def testSameFile(self): |
| 818 | # Verify that (when the ZipFile is in control of creating file objects) |
| 819 | # multiple open() calls can be made without interfering with each other. |
| 820 | zipf = zipfile.ZipFile(TESTFN2, mode="r") |
| 821 | zopen1 = zipf.open('ones') |
| 822 | zopen2 = zipf.open('ones') |
| 823 | data1 = zopen1.read(500) |
| 824 | data2 = zopen2.read(500) |
| 825 | data1 += zopen1.read(500) |
| 826 | data2 += zopen2.read(500) |
| 827 | self.assertEqual(data1, data2) |
| 828 | zipf.close() |
| 829 | |
| 830 | def testDifferentFile(self): |
| 831 | # Verify that (when the ZipFile is in control of creating file objects) |
| 832 | # multiple open() calls can be made without interfering with each other. |
| 833 | zipf = zipfile.ZipFile(TESTFN2, mode="r") |
| 834 | zopen1 = zipf.open('ones') |
| 835 | zopen2 = zipf.open('twos') |
| 836 | data1 = zopen1.read(500) |
| 837 | data2 = zopen2.read(500) |
| 838 | data1 += zopen1.read(500) |
| 839 | data2 += zopen2.read(500) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 840 | self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) |
| 841 | self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 842 | zipf.close() |
| 843 | |
| 844 | def testInterleaved(self): |
| 845 | # Verify that (when the ZipFile is in control of creating file objects) |
| 846 | # multiple open() calls can be made without interfering with each other. |
| 847 | zipf = zipfile.ZipFile(TESTFN2, mode="r") |
| 848 | zopen1 = zipf.open('ones') |
| 849 | data1 = zopen1.read(500) |
| 850 | zopen2 = zipf.open('twos') |
| 851 | data2 = zopen2.read(500) |
| 852 | data1 += zopen1.read(500) |
| 853 | data2 += zopen2.read(500) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 854 | self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) |
| 855 | self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 856 | zipf.close() |
| 857 | |
| 858 | def tearDown(self): |
| 859 | os.remove(TESTFN2) |
| 860 | |
| 861 | |
| 862 | class UniversalNewlineTests(unittest.TestCase): |
| 863 | def setUp(self): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 864 | self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 865 | for i in range(FIXEDTEST_SIZE)] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 866 | self.seps = ('\r', '\r\n', '\n') |
| 867 | self.arcdata, self.arcfiles = {}, {} |
| 868 | for n, s in enumerate(self.seps): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 869 | b = s.encode("ascii") |
| 870 | self.arcdata[s] = b.join(self.line_gen) + b |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 871 | self.arcfiles[s] = '%s-%d' % (TESTFN, n) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 872 | f = open(self.arcfiles[s], "wb") |
| 873 | try: |
| 874 | f.write(self.arcdata[s]) |
| 875 | finally: |
| 876 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 877 | |
| 878 | def makeTestArchive(self, f, compression): |
| 879 | # Create the ZIP archive |
| 880 | zipfp = zipfile.ZipFile(f, "w", compression) |
| 881 | for fn in self.arcfiles.values(): |
| 882 | zipfp.write(fn, fn) |
| 883 | zipfp.close() |
| 884 | |
| 885 | def readTest(self, f, compression): |
| 886 | self.makeTestArchive(f, compression) |
| 887 | |
| 888 | # Read the ZIP archive |
| 889 | zipfp = zipfile.ZipFile(f, "r") |
| 890 | for sep, fn in self.arcfiles.items(): |
| 891 | zipdata = zipfp.open(fn, "rU").read() |
| 892 | self.assertEqual(self.arcdata[sep], zipdata) |
| 893 | |
| 894 | zipfp.close() |
| 895 | |
| 896 | def readlineTest(self, f, compression): |
| 897 | self.makeTestArchive(f, compression) |
| 898 | |
| 899 | # Read the ZIP archive |
| 900 | zipfp = zipfile.ZipFile(f, "r") |
| 901 | for sep, fn in self.arcfiles.items(): |
| 902 | zipopen = zipfp.open(fn, "rU") |
| 903 | for line in self.line_gen: |
| 904 | linedata = zipopen.readline() |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 905 | self.assertEqual(linedata, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 906 | |
| 907 | zipfp.close() |
| 908 | |
| 909 | def readlinesTest(self, f, compression): |
| 910 | self.makeTestArchive(f, compression) |
| 911 | |
| 912 | # Read the ZIP archive |
| 913 | zipfp = zipfile.ZipFile(f, "r") |
| 914 | for sep, fn in self.arcfiles.items(): |
| 915 | ziplines = zipfp.open(fn, "rU").readlines() |
| 916 | for line, zipline in zip(self.line_gen, ziplines): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 917 | self.assertEqual(zipline, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 918 | |
| 919 | zipfp.close() |
| 920 | |
| 921 | def iterlinesTest(self, f, compression): |
| 922 | self.makeTestArchive(f, compression) |
| 923 | |
| 924 | # Read the ZIP archive |
| 925 | zipfp = zipfile.ZipFile(f, "r") |
| 926 | for sep, fn in self.arcfiles.items(): |
| 927 | 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] | 928 | self.assertEqual(zipline, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 929 | |
| 930 | zipfp.close() |
| 931 | |
| 932 | def testReadStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 933 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 934 | self.readTest(f, zipfile.ZIP_STORED) |
| 935 | |
| 936 | def testReadlineStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 937 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 938 | self.readlineTest(f, zipfile.ZIP_STORED) |
| 939 | |
| 940 | def testReadlinesStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 941 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 942 | self.readlinesTest(f, zipfile.ZIP_STORED) |
| 943 | |
| 944 | def testIterlinesStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 945 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 946 | self.iterlinesTest(f, zipfile.ZIP_STORED) |
| 947 | |
| 948 | if zlib: |
| 949 | def testReadDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 950 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 951 | self.readTest(f, zipfile.ZIP_DEFLATED) |
| 952 | |
| 953 | def testReadlineDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 954 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 955 | self.readlineTest(f, zipfile.ZIP_DEFLATED) |
| 956 | |
| 957 | def testReadlinesDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 958 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 959 | self.readlinesTest(f, zipfile.ZIP_DEFLATED) |
| 960 | |
| 961 | def testIterlinesDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 962 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 963 | self.iterlinesTest(f, zipfile.ZIP_DEFLATED) |
| 964 | |
| 965 | def tearDown(self): |
| 966 | for sep, fn in self.arcfiles.items(): |
| 967 | os.remove(fn) |
| 968 | support.unlink(TESTFN) |
| 969 | support.unlink(TESTFN2) |
| 970 | |
| 971 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 972 | def test_main(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 973 | run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests, |
| 974 | PyZipFileTests, DecryptionTests, TestsWithMultipleOpens, |
| 975 | UniversalNewlineTests, TestsWithRandomBinaryFiles) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 976 | |
| 977 | if __name__ == "__main__": |
| 978 | test_main() |