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