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