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) |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 312 | correctfile = os.path.normpath(correctfile) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 313 | |
| 314 | self.assertEqual(writtenfile, correctfile) |
| 315 | |
| 316 | # make sure correct data is in correct file |
| 317 | self.assertEqual(fdata.encode(), open(writtenfile, "rb").read()) |
| 318 | |
| 319 | os.remove(writtenfile) |
| 320 | |
| 321 | zipfp.close() |
| 322 | |
| 323 | # remove the test file subdirectories |
| 324 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 325 | |
| 326 | def testExtractAll(self): |
| 327 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
| 328 | for fpath, fdata in SMALL_TEST_DATA: |
| 329 | zipfp.writestr(fpath, fdata) |
| 330 | zipfp.close() |
| 331 | |
| 332 | zipfp = zipfile.ZipFile(TESTFN2, "r") |
| 333 | zipfp.extractall() |
| 334 | for fpath, fdata in SMALL_TEST_DATA: |
| 335 | if os.path.isabs(fpath): |
| 336 | outfile = os.path.join(os.getcwd(), fpath[1:]) |
| 337 | else: |
| 338 | outfile = os.path.join(os.getcwd(), fpath) |
| 339 | |
| 340 | self.assertEqual(fdata.encode(), open(outfile, "rb").read()) |
| 341 | |
| 342 | os.remove(outfile) |
| 343 | |
| 344 | zipfp.close() |
| 345 | |
| 346 | # remove the test file subdirectories |
| 347 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 348 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 349 | def tearDown(self): |
| 350 | os.remove(TESTFN) |
| 351 | os.remove(TESTFN2) |
| 352 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 353 | class TestZip64InSmallFiles(unittest.TestCase): |
| 354 | # These tests test the ZIP64 functionality without using large files, |
| 355 | # see test_zipfile64 for proper tests. |
| 356 | |
| 357 | def setUp(self): |
| 358 | self._limit = zipfile.ZIP64_LIMIT |
| 359 | zipfile.ZIP64_LIMIT = 5 |
| 360 | |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 361 | line_gen = (bytes("Test of zipfile line %d." % i, "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 362 | for i in range(0, FIXEDTEST_SIZE)) |
| 363 | self.data = b'\n'.join(line_gen) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 364 | |
| 365 | # Make a source file with some lines |
| 366 | fp = open(TESTFN, "wb") |
| 367 | fp.write(self.data) |
| 368 | fp.close() |
| 369 | |
| 370 | def largeFileExceptionTest(self, f, compression): |
| 371 | zipfp = zipfile.ZipFile(f, "w", compression) |
| 372 | self.assertRaises(zipfile.LargeZipFile, |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 373 | zipfp.write, TESTFN, "another.name") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 374 | zipfp.close() |
| 375 | |
| 376 | def largeFileExceptionTest2(self, f, compression): |
| 377 | zipfp = zipfile.ZipFile(f, "w", compression) |
| 378 | self.assertRaises(zipfile.LargeZipFile, |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 379 | zipfp.writestr, "another.name", self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 380 | zipfp.close() |
| 381 | |
| 382 | def testLargeFileException(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 383 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 384 | self.largeFileExceptionTest(f, zipfile.ZIP_STORED) |
| 385 | self.largeFileExceptionTest2(f, zipfile.ZIP_STORED) |
| 386 | |
| 387 | def zipTest(self, f, compression): |
| 388 | # Create the ZIP archive |
| 389 | zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 390 | zipfp.write(TESTFN, "another.name") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 391 | zipfp.write(TESTFN, TESTFN) |
| 392 | zipfp.writestr("strfile", self.data) |
| 393 | zipfp.close() |
| 394 | |
| 395 | # Read the ZIP archive |
| 396 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 397 | self.assertEqual(zipfp.read(TESTFN), self.data) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 398 | self.assertEqual(zipfp.read("another.name"), self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 399 | self.assertEqual(zipfp.read("strfile"), self.data) |
| 400 | |
| 401 | # Print the ZIP directory |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 402 | fp = io.StringIO() |
| 403 | zipfp.printdir(fp) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 404 | |
| 405 | directory = fp.getvalue() |
| 406 | lines = directory.splitlines() |
| 407 | self.assertEquals(len(lines), 4) # Number of files + header |
| 408 | |
| 409 | self.assert_('File Name' in lines[0]) |
| 410 | self.assert_('Modified' in lines[0]) |
| 411 | self.assert_('Size' in lines[0]) |
| 412 | |
| 413 | fn, date, time, size = lines[1].split() |
| 414 | self.assertEquals(fn, 'another.name') |
| 415 | # XXX: timestamp is not tested |
| 416 | self.assertEquals(size, str(len(self.data))) |
| 417 | |
| 418 | # Check the namelist |
| 419 | names = zipfp.namelist() |
| 420 | self.assertEquals(len(names), 3) |
| 421 | self.assert_(TESTFN in names) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 422 | self.assert_("another.name" in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 423 | self.assert_("strfile" in names) |
| 424 | |
| 425 | # Check infolist |
| 426 | infos = zipfp.infolist() |
| 427 | names = [ i.filename for i in infos ] |
| 428 | self.assertEquals(len(names), 3) |
| 429 | self.assert_(TESTFN in names) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 430 | self.assert_("another.name" in names) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 431 | self.assert_("strfile" in names) |
| 432 | for i in infos: |
| 433 | self.assertEquals(i.file_size, len(self.data)) |
| 434 | |
| 435 | # check getinfo |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 436 | for nm in (TESTFN, "another.name", "strfile"): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 437 | info = zipfp.getinfo(nm) |
| 438 | self.assertEquals(info.filename, nm) |
| 439 | self.assertEquals(info.file_size, len(self.data)) |
| 440 | |
| 441 | # Check that testzip doesn't raise an exception |
| 442 | zipfp.testzip() |
| 443 | |
| 444 | |
| 445 | zipfp.close() |
| 446 | |
| 447 | def testStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 448 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 449 | self.zipTest(f, zipfile.ZIP_STORED) |
| 450 | |
| 451 | |
| 452 | if zlib: |
| 453 | def testDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 454 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 455 | self.zipTest(f, zipfile.ZIP_DEFLATED) |
| 456 | |
| 457 | def testAbsoluteArcnames(self): |
| 458 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True) |
| 459 | zipfp.write(TESTFN, "/absolute") |
| 460 | zipfp.close() |
| 461 | |
| 462 | zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) |
| 463 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
| 464 | zipfp.close() |
| 465 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 466 | def tearDown(self): |
| 467 | zipfile.ZIP64_LIMIT = self._limit |
| 468 | os.remove(TESTFN) |
| 469 | os.remove(TESTFN2) |
| 470 | |
| 471 | class PyZipFileTests(unittest.TestCase): |
| 472 | def testWritePyfile(self): |
| 473 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 474 | fn = __file__ |
| 475 | if fn.endswith('.pyc') or fn.endswith('.pyo'): |
| 476 | fn = fn[:-1] |
| 477 | |
| 478 | zipfp.writepy(fn) |
| 479 | |
| 480 | bn = os.path.basename(fn) |
| 481 | self.assert_(bn not in zipfp.namelist()) |
| 482 | self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist()) |
| 483 | zipfp.close() |
| 484 | |
| 485 | |
| 486 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 487 | fn = __file__ |
| 488 | if fn.endswith('.pyc') or fn.endswith('.pyo'): |
| 489 | fn = fn[:-1] |
| 490 | |
| 491 | zipfp.writepy(fn, "testpackage") |
| 492 | |
| 493 | bn = "%s/%s"%("testpackage", os.path.basename(fn)) |
| 494 | self.assert_(bn not in zipfp.namelist()) |
| 495 | self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist()) |
| 496 | zipfp.close() |
| 497 | |
| 498 | def testWritePythonPackage(self): |
| 499 | import email |
| 500 | packagedir = os.path.dirname(email.__file__) |
| 501 | |
| 502 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 503 | zipfp.writepy(packagedir) |
| 504 | |
| 505 | # Check for a couple of modules at different levels of the hieararchy |
| 506 | names = zipfp.namelist() |
| 507 | self.assert_('email/__init__.pyo' in names or 'email/__init__.pyc' in names) |
| 508 | self.assert_('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names) |
| 509 | |
| 510 | def testWritePythonDirectory(self): |
| 511 | os.mkdir(TESTFN2) |
| 512 | try: |
| 513 | fp = open(os.path.join(TESTFN2, "mod1.py"), "w") |
Guido van Rossum | 43fc78d | 2007-02-09 22:18:41 +0000 | [diff] [blame] | 514 | fp.write("print(42)\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 515 | fp.close() |
| 516 | |
| 517 | fp = open(os.path.join(TESTFN2, "mod2.py"), "w") |
Guido van Rossum | 43fc78d | 2007-02-09 22:18:41 +0000 | [diff] [blame] | 518 | fp.write("print(42 * 42)\n") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 519 | fp.close() |
| 520 | |
| 521 | fp = open(os.path.join(TESTFN2, "mod2.txt"), "w") |
| 522 | fp.write("bla bla bla\n") |
| 523 | fp.close() |
| 524 | |
| 525 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 526 | zipfp.writepy(TESTFN2) |
| 527 | |
| 528 | names = zipfp.namelist() |
| 529 | self.assert_('mod1.pyc' in names or 'mod1.pyo' in names) |
| 530 | self.assert_('mod2.pyc' in names or 'mod2.pyo' in names) |
| 531 | self.assert_('mod2.txt' not in names) |
| 532 | |
| 533 | finally: |
| 534 | shutil.rmtree(TESTFN2) |
| 535 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 536 | def testWriteNonPyfile(self): |
| 537 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 538 | open(TESTFN, 'w').write('most definitely not a python file') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 539 | self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) |
| 540 | os.remove(TESTFN) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 541 | |
| 542 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 543 | class OtherTests(unittest.TestCase): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 544 | def testCreateNonExistentFileForAppend(self): |
| 545 | if os.path.exists(TESTFN): |
| 546 | os.unlink(TESTFN) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 547 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 548 | filename = 'testfile.txt' |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 549 | content = b'hello, world. this is some content.' |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 550 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 551 | try: |
| 552 | zf = zipfile.ZipFile(TESTFN, 'a') |
| 553 | zf.writestr(filename, content) |
| 554 | zf.close() |
| 555 | except IOError: |
| 556 | self.fail('Could not append data to a non-existent zip file.') |
| 557 | |
| 558 | self.assert_(os.path.exists(TESTFN)) |
| 559 | |
| 560 | zf = zipfile.ZipFile(TESTFN, 'r') |
| 561 | self.assertEqual(zf.read(filename), content) |
| 562 | zf.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 563 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 564 | def testCloseErroneousFile(self): |
| 565 | # This test checks that the ZipFile constructor closes the file object |
| 566 | # it opens if there's an error in the file. If it doesn't, the traceback |
| 567 | # holds a reference to the ZipFile object and, indirectly, the file object. |
| 568 | # On Windows, this causes the os.unlink() call to fail because the |
| 569 | # underlying file is still open. This is SF bug #412214. |
| 570 | # |
| 571 | fp = open(TESTFN, "w") |
| 572 | fp.write("this is not a legal zip file\n") |
| 573 | fp.close() |
| 574 | try: |
| 575 | zf = zipfile.ZipFile(TESTFN) |
| 576 | except zipfile.BadZipfile: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 577 | pass |
| 578 | |
| 579 | def testIsZipErroneousFile(self): |
| 580 | # This test checks that the is_zipfile function correctly identifies |
| 581 | # a file that is not a zip file |
| 582 | fp = open(TESTFN, "w") |
| 583 | fp.write("this is not a legal zip file\n") |
| 584 | fp.close() |
| 585 | chk = zipfile.is_zipfile(TESTFN) |
| 586 | self.assert_(chk is False) |
| 587 | |
| 588 | def testIsZipValidFile(self): |
| 589 | # This test checks that the is_zipfile function correctly identifies |
| 590 | # a file that is a zip file |
| 591 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 592 | zipf.writestr("foo.txt", b"O, for a Muse of Fire!") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 593 | zipf.close() |
| 594 | chk = zipfile.is_zipfile(TESTFN) |
| 595 | self.assert_(chk is True) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 596 | |
| 597 | def testNonExistentFileRaisesIOError(self): |
| 598 | # make sure we don't raise an AttributeError when a partially-constructed |
| 599 | # ZipFile instance is finalized; this tests for regression on SF tracker |
| 600 | # bug #403871. |
| 601 | |
| 602 | # The bug we're testing for caused an AttributeError to be raised |
| 603 | # when a ZipFile instance was created for a file that did not |
| 604 | # exist; the .fp member was not initialized but was needed by the |
| 605 | # __del__() method. Since the AttributeError is in the __del__(), |
| 606 | # it is ignored, but the user should be sufficiently annoyed by |
| 607 | # the message on the output that regression will be noticed |
| 608 | # quickly. |
| 609 | self.assertRaises(IOError, zipfile.ZipFile, TESTFN) |
| 610 | |
| 611 | def testClosedZipRaisesRuntimeError(self): |
| 612 | # Verify that testzip() doesn't swallow inappropriate exceptions. |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 613 | data = io.BytesIO() |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 614 | zipf = zipfile.ZipFile(data, mode="w") |
| 615 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 616 | zipf.close() |
| 617 | |
| 618 | # This is correct; calling .read on a closed ZipFile should throw |
| 619 | # a RuntimeError, and so should calling .testzip. An earlier |
| 620 | # version of .testzip would swallow this exception (and any other) |
| 621 | # and report that the first file in the archive was corrupt. |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 622 | self.assertRaises(RuntimeError, zipf.read, "foo.txt") |
| 623 | self.assertRaises(RuntimeError, zipf.open, "foo.txt") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 624 | self.assertRaises(RuntimeError, zipf.testzip) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 625 | self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus") |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 626 | open(TESTFN, 'w').write('zipfile test data') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 627 | self.assertRaises(RuntimeError, zipf.write, TESTFN) |
| 628 | |
| 629 | def test_BadConstructorMode(self): |
| 630 | # Check that bad modes passed to ZipFile constructor are caught |
| 631 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q") |
| 632 | |
| 633 | def test_BadOpenMode(self): |
| 634 | # Check that bad modes passed to ZipFile.open are caught |
| 635 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 636 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 637 | zipf.close() |
| 638 | zipf = zipfile.ZipFile(TESTFN, mode="r") |
| 639 | # read the data to make sure the file is there |
| 640 | zipf.read("foo.txt") |
| 641 | self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q") |
| 642 | zipf.close() |
| 643 | |
| 644 | def test_Read0(self): |
| 645 | # Check that calling read(0) on a ZipExtFile object returns an empty |
| 646 | # string and doesn't advance file pointer |
| 647 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 648 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 649 | # read the data to make sure the file is there |
| 650 | f = zipf.open("foo.txt") |
| 651 | for i in range(FIXEDTEST_SIZE): |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 652 | self.assertEqual(f.read(0), b'') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 653 | |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 654 | self.assertEqual(f.read(), b"O, for a Muse of Fire!") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 655 | zipf.close() |
| 656 | |
| 657 | def test_OpenNonexistentItem(self): |
| 658 | # Check that attempting to call open() for an item that doesn't |
| 659 | # exist in the archive raises a RuntimeError |
| 660 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 661 | self.assertRaises(KeyError, zipf.open, "foo.txt", "r") |
| 662 | |
| 663 | def test_BadCompressionMode(self): |
| 664 | # Check that bad compression methods passed to ZipFile.open are caught |
| 665 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1) |
| 666 | |
| 667 | def test_NullByteInFilename(self): |
| 668 | # Check that a filename containing a null byte is properly terminated |
| 669 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
Guido van Rossum | 814661e | 2007-07-18 22:07:29 +0000 | [diff] [blame] | 670 | 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] | 671 | self.assertEqual(zipf.namelist(), ['foo.txt']) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 672 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 673 | def tearDown(self): |
| 674 | support.unlink(TESTFN) |
| 675 | support.unlink(TESTFN2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 676 | |
| 677 | class DecryptionTests(unittest.TestCase): |
| 678 | # This test checks that ZIP decryption works. Since the library does not |
| 679 | # support encryption at the moment, we use a pre-generated encrypted |
| 680 | # ZIP file |
| 681 | |
| 682 | data = ( |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 683 | b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00' |
| 684 | b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y' |
| 685 | b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl' |
| 686 | b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00' |
| 687 | b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81' |
| 688 | b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00' |
| 689 | b'\x00\x00L\x00\x00\x00\x00\x00' ) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 690 | data2 = ( |
| 691 | b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02' |
| 692 | b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04' |
| 693 | b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0' |
| 694 | b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03' |
| 695 | b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00' |
| 696 | b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze' |
| 697 | b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01' |
| 698 | b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' ) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 699 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 700 | plain = b'zipfile.py encryption test' |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 701 | plain2 = b'\x00'*512 |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 702 | |
| 703 | def setUp(self): |
| 704 | fp = open(TESTFN, "wb") |
| 705 | fp.write(self.data) |
| 706 | fp.close() |
| 707 | self.zip = zipfile.ZipFile(TESTFN, "r") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 708 | fp = open(TESTFN2, "wb") |
| 709 | fp.write(self.data2) |
| 710 | fp.close() |
| 711 | self.zip2 = zipfile.ZipFile(TESTFN2, "r") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 712 | |
| 713 | def tearDown(self): |
| 714 | self.zip.close() |
| 715 | os.unlink(TESTFN) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 716 | self.zip2.close() |
| 717 | os.unlink(TESTFN2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 718 | |
| 719 | def testNoPassword(self): |
| 720 | # Reading the encrypted file without password |
| 721 | # must generate a RunTime exception |
| 722 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 723 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 724 | |
| 725 | def testBadPassword(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 726 | self.zip.setpassword(b"perl") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 727 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 728 | self.zip2.setpassword(b"perl") |
| 729 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 730 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 731 | def testGoodPassword(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 732 | self.zip.setpassword(b"python") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 733 | self.assertEquals(self.zip.read("test.txt"), self.plain) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 734 | self.zip2.setpassword(b"12345") |
| 735 | self.assertEquals(self.zip2.read("zero"), self.plain2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 736 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 737 | |
| 738 | class TestsWithRandomBinaryFiles(unittest.TestCase): |
| 739 | def setUp(self): |
| 740 | datacount = randint(16, 64)*1024 + randint(1, 1024) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 741 | self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000)) |
| 742 | for i in range(datacount)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 743 | |
| 744 | # Make a source file with some lines |
| 745 | fp = open(TESTFN, "wb") |
| 746 | fp.write(self.data) |
| 747 | fp.close() |
| 748 | |
| 749 | def tearDown(self): |
| 750 | support.unlink(TESTFN) |
| 751 | support.unlink(TESTFN2) |
| 752 | |
| 753 | def makeTestArchive(self, f, compression): |
| 754 | # Create the ZIP archive |
| 755 | zipfp = zipfile.ZipFile(f, "w", compression) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 756 | zipfp.write(TESTFN, "another.name") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 757 | zipfp.write(TESTFN, TESTFN) |
| 758 | zipfp.close() |
| 759 | |
| 760 | def zipTest(self, f, compression): |
| 761 | self.makeTestArchive(f, compression) |
| 762 | |
| 763 | # Read the ZIP archive |
| 764 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 765 | testdata = zipfp.read(TESTFN) |
| 766 | self.assertEqual(len(testdata), len(self.data)) |
| 767 | self.assertEqual(testdata, self.data) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 768 | self.assertEqual(zipfp.read("another.name"), self.data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 769 | zipfp.close() |
| 770 | |
| 771 | def testStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 772 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 773 | self.zipTest(f, zipfile.ZIP_STORED) |
| 774 | |
| 775 | def zipOpenTest(self, f, compression): |
| 776 | self.makeTestArchive(f, compression) |
| 777 | |
| 778 | # Read the ZIP archive |
| 779 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 780 | zipdata1 = [] |
| 781 | zipopen1 = zipfp.open(TESTFN) |
| 782 | while 1: |
| 783 | read_data = zipopen1.read(256) |
| 784 | if not read_data: |
| 785 | break |
| 786 | zipdata1.append(read_data) |
| 787 | |
| 788 | zipdata2 = [] |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 789 | zipopen2 = zipfp.open("another.name") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 790 | while 1: |
| 791 | read_data = zipopen2.read(256) |
| 792 | if not read_data: |
| 793 | break |
| 794 | zipdata2.append(read_data) |
| 795 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 796 | testdata1 = b''.join(zipdata1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 797 | self.assertEqual(len(testdata1), len(self.data)) |
| 798 | self.assertEqual(testdata1, self.data) |
| 799 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 800 | testdata2 = b''.join(zipdata2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 801 | self.assertEqual(len(testdata1), len(self.data)) |
| 802 | self.assertEqual(testdata1, self.data) |
| 803 | zipfp.close() |
| 804 | |
| 805 | def testOpenStored(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.zipOpenTest(f, zipfile.ZIP_STORED) |
| 808 | |
| 809 | def zipRandomOpenTest(self, f, compression): |
| 810 | self.makeTestArchive(f, compression) |
| 811 | |
| 812 | # Read the ZIP archive |
| 813 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 814 | zipdata1 = [] |
| 815 | zipopen1 = zipfp.open(TESTFN) |
| 816 | while 1: |
| 817 | read_data = zipopen1.read(randint(1, 1024)) |
| 818 | if not read_data: |
| 819 | break |
| 820 | zipdata1.append(read_data) |
| 821 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 822 | testdata = b''.join(zipdata1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 823 | self.assertEqual(len(testdata), len(self.data)) |
| 824 | self.assertEqual(testdata, self.data) |
| 825 | zipfp.close() |
| 826 | |
| 827 | def testRandomOpenStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 828 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 829 | self.zipRandomOpenTest(f, zipfile.ZIP_STORED) |
| 830 | |
| 831 | class TestsWithMultipleOpens(unittest.TestCase): |
| 832 | def setUp(self): |
| 833 | # Create the ZIP archive |
| 834 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) |
| 835 | zipfp.writestr('ones', '1'*FIXEDTEST_SIZE) |
| 836 | zipfp.writestr('twos', '2'*FIXEDTEST_SIZE) |
| 837 | zipfp.close() |
| 838 | |
| 839 | def testSameFile(self): |
| 840 | # Verify that (when the ZipFile is in control of creating file objects) |
| 841 | # multiple open() calls can be made without interfering with each other. |
| 842 | zipf = zipfile.ZipFile(TESTFN2, mode="r") |
| 843 | zopen1 = zipf.open('ones') |
| 844 | zopen2 = zipf.open('ones') |
| 845 | data1 = zopen1.read(500) |
| 846 | data2 = zopen2.read(500) |
| 847 | data1 += zopen1.read(500) |
| 848 | data2 += zopen2.read(500) |
| 849 | self.assertEqual(data1, data2) |
| 850 | zipf.close() |
| 851 | |
| 852 | def testDifferentFile(self): |
| 853 | # Verify that (when the ZipFile is in control of creating file objects) |
| 854 | # multiple open() calls can be made without interfering with each other. |
| 855 | zipf = zipfile.ZipFile(TESTFN2, mode="r") |
| 856 | zopen1 = zipf.open('ones') |
| 857 | zopen2 = zipf.open('twos') |
| 858 | data1 = zopen1.read(500) |
| 859 | data2 = zopen2.read(500) |
| 860 | data1 += zopen1.read(500) |
| 861 | data2 += zopen2.read(500) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 862 | self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) |
| 863 | self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 864 | zipf.close() |
| 865 | |
| 866 | def testInterleaved(self): |
| 867 | # Verify that (when the ZipFile is in control of creating file objects) |
| 868 | # multiple open() calls can be made without interfering with each other. |
| 869 | zipf = zipfile.ZipFile(TESTFN2, mode="r") |
| 870 | zopen1 = zipf.open('ones') |
| 871 | data1 = zopen1.read(500) |
| 872 | zopen2 = zipf.open('twos') |
| 873 | data2 = zopen2.read(500) |
| 874 | data1 += zopen1.read(500) |
| 875 | data2 += zopen2.read(500) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 876 | self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) |
| 877 | self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 878 | zipf.close() |
| 879 | |
| 880 | def tearDown(self): |
| 881 | os.remove(TESTFN2) |
| 882 | |
| 883 | |
| 884 | class UniversalNewlineTests(unittest.TestCase): |
| 885 | def setUp(self): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 886 | self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii") |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 887 | for i in range(FIXEDTEST_SIZE)] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 888 | self.seps = ('\r', '\r\n', '\n') |
| 889 | self.arcdata, self.arcfiles = {}, {} |
| 890 | for n, s in enumerate(self.seps): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 891 | b = s.encode("ascii") |
| 892 | self.arcdata[s] = b.join(self.line_gen) + b |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 893 | self.arcfiles[s] = '%s-%d' % (TESTFN, n) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 894 | f = open(self.arcfiles[s], "wb") |
| 895 | try: |
| 896 | f.write(self.arcdata[s]) |
| 897 | finally: |
| 898 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 899 | |
| 900 | def makeTestArchive(self, f, compression): |
| 901 | # Create the ZIP archive |
| 902 | zipfp = zipfile.ZipFile(f, "w", compression) |
| 903 | for fn in self.arcfiles.values(): |
| 904 | zipfp.write(fn, fn) |
| 905 | zipfp.close() |
| 906 | |
| 907 | def readTest(self, f, compression): |
| 908 | self.makeTestArchive(f, compression) |
| 909 | |
| 910 | # Read the ZIP archive |
| 911 | zipfp = zipfile.ZipFile(f, "r") |
| 912 | for sep, fn in self.arcfiles.items(): |
| 913 | zipdata = zipfp.open(fn, "rU").read() |
| 914 | self.assertEqual(self.arcdata[sep], zipdata) |
| 915 | |
| 916 | zipfp.close() |
| 917 | |
| 918 | def readlineTest(self, f, compression): |
| 919 | self.makeTestArchive(f, compression) |
| 920 | |
| 921 | # Read the ZIP archive |
| 922 | zipfp = zipfile.ZipFile(f, "r") |
| 923 | for sep, fn in self.arcfiles.items(): |
| 924 | zipopen = zipfp.open(fn, "rU") |
| 925 | for line in self.line_gen: |
| 926 | linedata = zipopen.readline() |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 927 | self.assertEqual(linedata, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 928 | |
| 929 | zipfp.close() |
| 930 | |
| 931 | def readlinesTest(self, f, compression): |
| 932 | self.makeTestArchive(f, compression) |
| 933 | |
| 934 | # Read the ZIP archive |
| 935 | zipfp = zipfile.ZipFile(f, "r") |
| 936 | for sep, fn in self.arcfiles.items(): |
| 937 | ziplines = zipfp.open(fn, "rU").readlines() |
| 938 | for line, zipline in zip(self.line_gen, ziplines): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 939 | self.assertEqual(zipline, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 940 | |
| 941 | zipfp.close() |
| 942 | |
| 943 | def iterlinesTest(self, f, compression): |
| 944 | self.makeTestArchive(f, compression) |
| 945 | |
| 946 | # Read the ZIP archive |
| 947 | zipfp = zipfile.ZipFile(f, "r") |
| 948 | for sep, fn in self.arcfiles.items(): |
| 949 | 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] | 950 | self.assertEqual(zipline, line + b'\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 951 | |
| 952 | zipfp.close() |
| 953 | |
| 954 | def testReadStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 955 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 956 | self.readTest(f, zipfile.ZIP_STORED) |
| 957 | |
| 958 | def testReadlineStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 959 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 960 | self.readlineTest(f, zipfile.ZIP_STORED) |
| 961 | |
| 962 | def testReadlinesStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 963 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 964 | self.readlinesTest(f, zipfile.ZIP_STORED) |
| 965 | |
| 966 | def testIterlinesStored(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 967 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 968 | self.iterlinesTest(f, zipfile.ZIP_STORED) |
| 969 | |
| 970 | if zlib: |
| 971 | def testReadDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 972 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 973 | self.readTest(f, zipfile.ZIP_DEFLATED) |
| 974 | |
| 975 | def testReadlineDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 976 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 977 | self.readlineTest(f, zipfile.ZIP_DEFLATED) |
| 978 | |
| 979 | def testReadlinesDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 980 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 981 | self.readlinesTest(f, zipfile.ZIP_DEFLATED) |
| 982 | |
| 983 | def testIterlinesDeflated(self): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 984 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 985 | self.iterlinesTest(f, zipfile.ZIP_DEFLATED) |
| 986 | |
| 987 | def tearDown(self): |
| 988 | for sep, fn in self.arcfiles.items(): |
| 989 | os.remove(fn) |
| 990 | support.unlink(TESTFN) |
| 991 | support.unlink(TESTFN2) |
| 992 | |
| 993 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 994 | def test_main(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 995 | run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests, |
| 996 | PyZipFileTests, DecryptionTests, TestsWithMultipleOpens, |
| 997 | UniversalNewlineTests, TestsWithRandomBinaryFiles) |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 998 | |
| 999 | if __name__ == "__main__": |
| 1000 | test_main() |