blob: ed800c577dd12b110d651426568e564df6fba077 [file] [log] [blame]
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001# We can test part of the module without zlib.
Guido van Rossum368f04a2000-04-10 13:23:04 +00002try:
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00003 import zlib
4except ImportError:
5 zlib = None
Guido van Rossumd6ca5462007-05-22 01:29:33 +00006import zipfile, os, unittest, sys, shutil, struct, io
Tim Petersa45cacf2004-08-20 03:47:14 +00007
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00008from tempfile import TemporaryFile
Guido van Rossumd8faa362007-04-27 19:54:29 +00009from random import randint, random
Tim Petersa19a1682001-03-29 04:36:09 +000010
Benjamin Petersonee8712c2008-05-20 21:35:26 +000011import test.support as support
Martin v. Löwis59e47792009-01-24 14:10:07 +000012from test.support import TESTFN, run_unittest, findfile
Guido van Rossum368f04a2000-04-10 13:23:04 +000013
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000014TESTFN2 = TESTFN + "2"
Martin v. Löwis59e47792009-01-24 14:10:07 +000015TESTFNDIR = TESTFN + "d"
Guido van Rossumb5a755e2007-07-18 18:15:48 +000016FIXEDTEST_SIZE = 1000
Guido van Rossum368f04a2000-04-10 13:23:04 +000017
Christian Heimes790c8232008-01-07 21:14:23 +000018SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
19 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
20 ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
21 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
22
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000023class TestsWithSourceFile(unittest.TestCase):
24 def setUp(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +000025 self.line_gen = (bytes("Zipfile test line %d. random float: %f" %
Guido van Rossum9c627722007-08-27 18:31:48 +000026 (i, random()), "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +000027 for i in range(FIXEDTEST_SIZE))
28 self.data = b'\n'.join(self.line_gen) + b'\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000029
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000030 # Make a source file with some lines
31 fp = open(TESTFN, "wb")
32 fp.write(self.data)
33 fp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000034
Guido van Rossumd8faa362007-04-27 19:54:29 +000035 def makeTestArchive(self, f, compression):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000036 # Create the ZIP archive
37 zipfp = zipfile.ZipFile(f, "w", compression)
Skip Montanaro7a98be22007-08-16 14:35:24 +000038 zipfp.write(TESTFN, "another.name")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000039 zipfp.write(TESTFN, TESTFN)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000040 zipfp.writestr("strfile", self.data)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000041 zipfp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000042
Guido van Rossumd8faa362007-04-27 19:54:29 +000043 def zipTest(self, f, compression):
44 self.makeTestArchive(f, compression)
45
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000046 # Read the ZIP archive
47 zipfp = zipfile.ZipFile(f, "r", compression)
48 self.assertEqual(zipfp.read(TESTFN), self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +000049 self.assertEqual(zipfp.read("another.name"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000050 self.assertEqual(zipfp.read("strfile"), self.data)
51
52 # Print the ZIP directory
Guido van Rossumd6ca5462007-05-22 01:29:33 +000053 fp = io.StringIO()
54 zipfp.printdir(file=fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000055
56 directory = fp.getvalue()
57 lines = directory.splitlines()
58 self.assertEquals(len(lines), 4) # Number of files + header
59
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000060 self.assertTrue('File Name' in lines[0])
61 self.assertTrue('Modified' in lines[0])
62 self.assertTrue('Size' in lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063
64 fn, date, time, size = lines[1].split()
65 self.assertEquals(fn, 'another.name')
66 # XXX: timestamp is not tested
67 self.assertEquals(size, str(len(self.data)))
68
69 # Check the namelist
70 names = zipfp.namelist()
71 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000072 self.assertTrue(TESTFN in names)
73 self.assertTrue("another.name" in names)
74 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075
76 # Check infolist
77 infos = zipfp.infolist()
78 names = [ i.filename for i in infos ]
79 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000080 self.assertTrue(TESTFN in names)
81 self.assertTrue("another.name" in names)
82 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083 for i in infos:
84 self.assertEquals(i.file_size, len(self.data))
85
86 # check getinfo
Skip Montanaro7a98be22007-08-16 14:35:24 +000087 for nm in (TESTFN, "another.name", "strfile"):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000088 info = zipfp.getinfo(nm)
89 self.assertEquals(info.filename, nm)
90 self.assertEquals(info.file_size, len(self.data))
91
92 # Check that testzip doesn't raise an exception
93 zipfp.testzip()
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000094 zipfp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000095
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000096 def testStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +000097 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000098 self.zipTest(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +000099
Guido van Rossumd8faa362007-04-27 19:54:29 +0000100 def zipOpenTest(self, f, compression):
101 self.makeTestArchive(f, compression)
102
103 # Read the ZIP archive
104 zipfp = zipfile.ZipFile(f, "r", compression)
105 zipdata1 = []
106 zipopen1 = zipfp.open(TESTFN)
107 while 1:
108 read_data = zipopen1.read(256)
109 if not read_data:
110 break
111 zipdata1.append(read_data)
112
113 zipdata2 = []
Skip Montanaro7a98be22007-08-16 14:35:24 +0000114 zipopen2 = zipfp.open("another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000115 while 1:
116 read_data = zipopen2.read(256)
117 if not read_data:
118 break
119 zipdata2.append(read_data)
120
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000121 self.assertEqual(b''.join(zipdata1), self.data)
122 self.assertEqual(b''.join(zipdata2), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000123 zipfp.close()
124
125 def testOpenStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000126 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000127 self.zipOpenTest(f, zipfile.ZIP_STORED)
128
Georg Brandlb533e262008-05-25 18:19:30 +0000129 def testOpenViaZipInfo(self):
130 # Create the ZIP archive
131 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
132 zipfp.writestr("name", "foo")
133 zipfp.writestr("name", "bar")
134 zipfp.close()
135
136 zipfp = zipfile.ZipFile(TESTFN2, "r")
137 infos = zipfp.infolist()
138 data = b""
139 for info in infos:
140 data += zipfp.open(info).read()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000141 self.assertTrue(data == b"foobar" or data == b"barfoo")
Georg Brandlb533e262008-05-25 18:19:30 +0000142 data = b""
143 for info in infos:
144 data += zipfp.read(info)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000145 self.assertTrue(data == b"foobar" or data == b"barfoo")
Georg Brandlb533e262008-05-25 18:19:30 +0000146 zipfp.close()
147
Guido van Rossumd8faa362007-04-27 19:54:29 +0000148 def zipRandomOpenTest(self, f, compression):
149 self.makeTestArchive(f, compression)
150
151 # Read the ZIP archive
152 zipfp = zipfile.ZipFile(f, "r", compression)
153 zipdata1 = []
154 zipopen1 = zipfp.open(TESTFN)
155 while 1:
156 read_data = zipopen1.read(randint(1, 1024))
157 if not read_data:
158 break
159 zipdata1.append(read_data)
160
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000161 self.assertEqual(b''.join(zipdata1), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000162 zipfp.close()
163
164 def testRandomOpenStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000165 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000166 self.zipRandomOpenTest(f, zipfile.ZIP_STORED)
167
168 def zipReadlineTest(self, f, compression):
169 self.makeTestArchive(f, compression)
170
171 # Read the ZIP archive
172 zipfp = zipfile.ZipFile(f, "r")
173 zipopen = zipfp.open(TESTFN)
174 for line in self.line_gen:
175 linedata = zipopen.readline()
176 self.assertEqual(linedata, line + '\n')
177
178 zipfp.close()
179
180 def zipReadlinesTest(self, f, compression):
181 self.makeTestArchive(f, compression)
182
183 # Read the ZIP archive
184 zipfp = zipfile.ZipFile(f, "r")
185 ziplines = zipfp.open(TESTFN).readlines()
186 for line, zipline in zip(self.line_gen, ziplines):
187 self.assertEqual(zipline, line + '\n')
188
189 zipfp.close()
190
191 def zipIterlinesTest(self, f, compression):
192 self.makeTestArchive(f, compression)
193
194 # Read the ZIP archive
195 zipfp = zipfile.ZipFile(f, "r")
196 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
197 self.assertEqual(zipline, line + '\n')
198
199 zipfp.close()
200
201 def testReadlineStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000202 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000203 self.zipReadlineTest(f, zipfile.ZIP_STORED)
204
205 def testReadlinesStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000206 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000207 self.zipReadlinesTest(f, zipfile.ZIP_STORED)
208
209 def testIterlinesStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000210 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000211 self.zipIterlinesTest(f, zipfile.ZIP_STORED)
212
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000213 if zlib:
214 def testDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000215 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000216 self.zipTest(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000217
Guido van Rossumd8faa362007-04-27 19:54:29 +0000218 def testOpenDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000219 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000220 self.zipOpenTest(f, zipfile.ZIP_DEFLATED)
221
222 def testRandomOpenDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000223 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000224 self.zipRandomOpenTest(f, zipfile.ZIP_DEFLATED)
225
226 def testReadlineDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000227 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000228 self.zipReadlineTest(f, zipfile.ZIP_DEFLATED)
229
230 def testReadlinesDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000231 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000232 self.zipReadlinesTest(f, zipfile.ZIP_DEFLATED)
233
234 def testIterlinesDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000235 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000236 self.zipIterlinesTest(f, zipfile.ZIP_DEFLATED)
237
238 def testLowCompression(self):
239 # Checks for cases where compressed data is larger than original
240 # Create the ZIP archive
241 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
242 zipfp.writestr("strfile", '12')
243 zipfp.close()
244
245 # Get an open object for strfile
246 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED)
247 openobj = zipfp.open("strfile")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000248 self.assertEqual(openobj.read(1), b'1')
249 self.assertEqual(openobj.read(1), b'2')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000250
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000251 def testAbsoluteArcnames(self):
252 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
253 zipfp.write(TESTFN, "/absolute")
254 zipfp.close()
255
256 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
257 self.assertEqual(zipfp.namelist(), ["absolute"])
258 zipfp.close()
Tim Peters32cbc962006-02-20 21:42:18 +0000259
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000260 def testAppendToZipFile(self):
261 # Test appending to an existing zipfile
262 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
263 zipfp.write(TESTFN, TESTFN)
264 zipfp.close()
265 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
266 zipfp.writestr("strfile", self.data)
267 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
268 zipfp.close()
269
270 def testAppendToNonZipFile(self):
271 # Test appending to an existing file that is not a zipfile
272 # NOTE: this test fails if len(d) < 22 because of the first
273 # line "fpin.seek(-22, 2)" in _EndRecData
Guido van Rossum9c627722007-08-27 18:31:48 +0000274 d = b'I am not a ZipFile!'*10
Guido van Rossum814661e2007-07-18 22:07:29 +0000275 f = open(TESTFN2, 'wb')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000276 f.write(d)
277 f.close()
278 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
279 zipfp.write(TESTFN, TESTFN)
280 zipfp.close()
281
Guido van Rossum814661e2007-07-18 22:07:29 +0000282 f = open(TESTFN2, 'rb')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000283 f.seek(len(d))
284 zipfp = zipfile.ZipFile(f, "r")
285 self.assertEqual(zipfp.namelist(), [TESTFN])
286 zipfp.close()
287 f.close()
288
289 def test_WriteDefaultName(self):
290 # Check that calling ZipFile.write without arcname specified produces the expected result
291 zipfp = zipfile.ZipFile(TESTFN2, "w")
292 zipfp.write(TESTFN)
Guido van Rossum814661e2007-07-18 22:07:29 +0000293 self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read())
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000294 zipfp.close()
295
296 def test_PerFileCompression(self):
297 # Check that files within a Zip archive can have different compression options
298 zipfp = zipfile.ZipFile(TESTFN2, "w")
299 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
300 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
301 sinfo = zipfp.getinfo('storeme')
302 dinfo = zipfp.getinfo('deflateme')
303 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
304 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
305 zipfp.close()
306
307 def test_WriteToReadonly(self):
308 # Check that trying to call write() on a readonly ZipFile object
309 # raises a RuntimeError
310 zipf = zipfile.ZipFile(TESTFN2, mode="w")
311 zipf.writestr("somefile.txt", "bogus")
312 zipf.close()
313 zipf = zipfile.ZipFile(TESTFN2, mode="r")
314 self.assertRaises(RuntimeError, zipf.write, TESTFN)
315 zipf.close()
316
Christian Heimes790c8232008-01-07 21:14:23 +0000317 def testExtract(self):
318 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
319 for fpath, fdata in SMALL_TEST_DATA:
320 zipfp.writestr(fpath, fdata)
321 zipfp.close()
322
323 zipfp = zipfile.ZipFile(TESTFN2, "r")
324 for fpath, fdata in SMALL_TEST_DATA:
325 writtenfile = zipfp.extract(fpath)
326
327 # make sure it was written to the right place
328 if os.path.isabs(fpath):
329 correctfile = os.path.join(os.getcwd(), fpath[1:])
330 else:
331 correctfile = os.path.join(os.getcwd(), fpath)
Christian Heimesaf98da12008-01-27 15:18:18 +0000332 correctfile = os.path.normpath(correctfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000333
334 self.assertEqual(writtenfile, correctfile)
335
336 # make sure correct data is in correct file
337 self.assertEqual(fdata.encode(), open(writtenfile, "rb").read())
338
339 os.remove(writtenfile)
340
341 zipfp.close()
342
343 # remove the test file subdirectories
344 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
345
346 def testExtractAll(self):
347 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
348 for fpath, fdata in SMALL_TEST_DATA:
349 zipfp.writestr(fpath, fdata)
350 zipfp.close()
351
352 zipfp = zipfile.ZipFile(TESTFN2, "r")
353 zipfp.extractall()
354 for fpath, fdata in SMALL_TEST_DATA:
355 if os.path.isabs(fpath):
356 outfile = os.path.join(os.getcwd(), fpath[1:])
357 else:
358 outfile = os.path.join(os.getcwd(), fpath)
359
360 self.assertEqual(fdata.encode(), open(outfile, "rb").read())
361
362 os.remove(outfile)
363
364 zipfp.close()
365
366 # remove the test file subdirectories
367 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
368
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000369 def zip_test_writestr_permissions(self, f, compression):
370 # Make sure that writestr creates files with mode 0600,
371 # when it is passed a name rather than a ZipInfo instance.
372
373 self.makeTestArchive(f, compression)
374 zipfp = zipfile.ZipFile(f, "r")
375 zinfo = zipfp.getinfo('strfile')
376 self.assertEqual(zinfo.external_attr, 0o600 << 16)
377
378 def test_writestr_permissions(self):
379 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
380 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
381
Gregory P. Smithb0d9ca92009-07-07 05:06:04 +0000382 def test_writestr_extended_local_header_issue1202(self):
383 orig_zip = zipfile.ZipFile(TESTFN2, 'w')
384 for data in 'abcdefghijklmnop':
385 zinfo = zipfile.ZipInfo(data)
386 zinfo.flag_bits |= 0x08 # Include an extended local header.
387 orig_zip.writestr(zinfo, data)
388 orig_zip.close()
389
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000390 def tearDown(self):
391 os.remove(TESTFN)
392 os.remove(TESTFN2)
393
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000394class TestZip64InSmallFiles(unittest.TestCase):
395 # These tests test the ZIP64 functionality without using large files,
396 # see test_zipfile64 for proper tests.
397
398 def setUp(self):
399 self._limit = zipfile.ZIP64_LIMIT
400 zipfile.ZIP64_LIMIT = 5
401
Guido van Rossum9c627722007-08-27 18:31:48 +0000402 line_gen = (bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000403 for i in range(0, FIXEDTEST_SIZE))
404 self.data = b'\n'.join(line_gen)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000405
406 # Make a source file with some lines
407 fp = open(TESTFN, "wb")
408 fp.write(self.data)
409 fp.close()
410
411 def largeFileExceptionTest(self, f, compression):
412 zipfp = zipfile.ZipFile(f, "w", compression)
413 self.assertRaises(zipfile.LargeZipFile,
Skip Montanaro7a98be22007-08-16 14:35:24 +0000414 zipfp.write, TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000415 zipfp.close()
416
417 def largeFileExceptionTest2(self, f, compression):
418 zipfp = zipfile.ZipFile(f, "w", compression)
419 self.assertRaises(zipfile.LargeZipFile,
Skip Montanaro7a98be22007-08-16 14:35:24 +0000420 zipfp.writestr, "another.name", self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000421 zipfp.close()
422
423 def testLargeFileException(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000424 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000425 self.largeFileExceptionTest(f, zipfile.ZIP_STORED)
426 self.largeFileExceptionTest2(f, zipfile.ZIP_STORED)
427
428 def zipTest(self, f, compression):
429 # Create the ZIP archive
430 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000431 zipfp.write(TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000432 zipfp.write(TESTFN, TESTFN)
433 zipfp.writestr("strfile", self.data)
434 zipfp.close()
435
436 # Read the ZIP archive
437 zipfp = zipfile.ZipFile(f, "r", compression)
438 self.assertEqual(zipfp.read(TESTFN), self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000439 self.assertEqual(zipfp.read("another.name"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000440 self.assertEqual(zipfp.read("strfile"), self.data)
441
442 # Print the ZIP directory
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000443 fp = io.StringIO()
444 zipfp.printdir(fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000445
446 directory = fp.getvalue()
447 lines = directory.splitlines()
448 self.assertEquals(len(lines), 4) # Number of files + header
449
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000450 self.assertTrue('File Name' in lines[0])
451 self.assertTrue('Modified' in lines[0])
452 self.assertTrue('Size' in lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000453
454 fn, date, time, size = lines[1].split()
455 self.assertEquals(fn, 'another.name')
456 # XXX: timestamp is not tested
457 self.assertEquals(size, str(len(self.data)))
458
459 # Check the namelist
460 names = zipfp.namelist()
461 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000462 self.assertTrue(TESTFN in names)
463 self.assertTrue("another.name" in names)
464 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000465
466 # Check infolist
467 infos = zipfp.infolist()
468 names = [ i.filename for i in infos ]
469 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000470 self.assertTrue(TESTFN in names)
471 self.assertTrue("another.name" in names)
472 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000473 for i in infos:
474 self.assertEquals(i.file_size, len(self.data))
475
476 # check getinfo
Skip Montanaro7a98be22007-08-16 14:35:24 +0000477 for nm in (TESTFN, "another.name", "strfile"):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000478 info = zipfp.getinfo(nm)
479 self.assertEquals(info.filename, nm)
480 self.assertEquals(info.file_size, len(self.data))
481
482 # Check that testzip doesn't raise an exception
483 zipfp.testzip()
484
485
486 zipfp.close()
487
488 def testStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000489 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000490 self.zipTest(f, zipfile.ZIP_STORED)
491
492
493 if zlib:
494 def testDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000495 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000496 self.zipTest(f, zipfile.ZIP_DEFLATED)
497
498 def testAbsoluteArcnames(self):
499 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True)
500 zipfp.write(TESTFN, "/absolute")
501 zipfp.close()
502
503 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
504 self.assertEqual(zipfp.namelist(), ["absolute"])
505 zipfp.close()
506
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000507 def tearDown(self):
508 zipfile.ZIP64_LIMIT = self._limit
509 os.remove(TESTFN)
510 os.remove(TESTFN2)
511
512class PyZipFileTests(unittest.TestCase):
513 def testWritePyfile(self):
514 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
515 fn = __file__
516 if fn.endswith('.pyc') or fn.endswith('.pyo'):
517 fn = fn[:-1]
518
519 zipfp.writepy(fn)
520
521 bn = os.path.basename(fn)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000522 self.assertTrue(bn not in zipfp.namelist())
523 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000524 zipfp.close()
525
526
527 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
528 fn = __file__
529 if fn.endswith('.pyc') or fn.endswith('.pyo'):
530 fn = fn[:-1]
531
532 zipfp.writepy(fn, "testpackage")
533
534 bn = "%s/%s"%("testpackage", os.path.basename(fn))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000535 self.assertTrue(bn not in zipfp.namelist())
536 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000537 zipfp.close()
538
539 def testWritePythonPackage(self):
540 import email
541 packagedir = os.path.dirname(email.__file__)
542
543 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
544 zipfp.writepy(packagedir)
545
546 # Check for a couple of modules at different levels of the hieararchy
547 names = zipfp.namelist()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000548 self.assertTrue('email/__init__.pyo' in names or 'email/__init__.pyc' in names)
549 self.assertTrue('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000550
551 def testWritePythonDirectory(self):
552 os.mkdir(TESTFN2)
553 try:
554 fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000555 fp.write("print(42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000556 fp.close()
557
558 fp = open(os.path.join(TESTFN2, "mod2.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000559 fp.write("print(42 * 42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000560 fp.close()
561
562 fp = open(os.path.join(TESTFN2, "mod2.txt"), "w")
563 fp.write("bla bla bla\n")
564 fp.close()
565
566 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
567 zipfp.writepy(TESTFN2)
568
569 names = zipfp.namelist()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000570 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
571 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
572 self.assertTrue('mod2.txt' not in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000573
574 finally:
575 shutil.rmtree(TESTFN2)
576
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000577 def testWriteNonPyfile(self):
578 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
Guido van Rossum814661e2007-07-18 22:07:29 +0000579 open(TESTFN, 'w').write('most definitely not a python file')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000580 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
581 os.remove(TESTFN)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000582
583
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000584class OtherTests(unittest.TestCase):
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000585 def testUnicodeFilenames(self):
586 zf = zipfile.ZipFile(TESTFN, "w")
587 zf.writestr("foo.txt", "Test for unicode filename")
Martin v. Löwis1a9f9002008-05-05 17:50:05 +0000588 zf.writestr("\xf6.txt", "Test for unicode filename")
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000589 zf.close()
Martin v. Löwis1a9f9002008-05-05 17:50:05 +0000590 zf = zipfile.ZipFile(TESTFN, "r")
591 self.assertEqual(zf.filelist[0].filename, "foo.txt")
592 self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
593 zf.close()
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000594
Thomas Wouterscf297e42007-02-23 15:07:44 +0000595 def testCreateNonExistentFileForAppend(self):
596 if os.path.exists(TESTFN):
597 os.unlink(TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000598
Thomas Wouterscf297e42007-02-23 15:07:44 +0000599 filename = 'testfile.txt'
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000600 content = b'hello, world. this is some content.'
Guido van Rossumd8faa362007-04-27 19:54:29 +0000601
Thomas Wouterscf297e42007-02-23 15:07:44 +0000602 try:
603 zf = zipfile.ZipFile(TESTFN, 'a')
604 zf.writestr(filename, content)
605 zf.close()
606 except IOError:
607 self.fail('Could not append data to a non-existent zip file.')
608
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000609 self.assertTrue(os.path.exists(TESTFN))
Thomas Wouterscf297e42007-02-23 15:07:44 +0000610
611 zf = zipfile.ZipFile(TESTFN, 'r')
612 self.assertEqual(zf.read(filename), content)
613 zf.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000614
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000615 def testCloseErroneousFile(self):
616 # This test checks that the ZipFile constructor closes the file object
617 # it opens if there's an error in the file. If it doesn't, the traceback
618 # holds a reference to the ZipFile object and, indirectly, the file object.
619 # On Windows, this causes the os.unlink() call to fail because the
620 # underlying file is still open. This is SF bug #412214.
621 #
622 fp = open(TESTFN, "w")
623 fp.write("this is not a legal zip file\n")
624 fp.close()
625 try:
626 zf = zipfile.ZipFile(TESTFN)
627 except zipfile.BadZipfile:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000628 pass
629
630 def testIsZipErroneousFile(self):
631 # This test checks that the is_zipfile function correctly identifies
632 # a file that is not a zip file
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000633
634 # - passing a filename
635 with open(TESTFN, "w") as fp:
636 fp.write("this is not a legal zip file\n")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000637 chk = zipfile.is_zipfile(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000638 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000639 # - passing a file object
640 with open(TESTFN, "rb") as fp:
641 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000642 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000643 # - passing a file-like object
644 fp = io.BytesIO()
645 fp.write(b"this is not a legal zip file\n")
646 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000647 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000648 fp.seek(0,0)
649 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000650 self.assertTrue(not chk)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000651
652 def testIsZipValidFile(self):
653 # This test checks that the is_zipfile function correctly identifies
654 # a file that is a zip file
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000655
656 # - passing a filename
Guido van Rossumd8faa362007-04-27 19:54:29 +0000657 zipf = zipfile.ZipFile(TESTFN, mode="w")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000658 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000659 zipf.close()
660 chk = zipfile.is_zipfile(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000661 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000662 # - passing a file object
663 with open(TESTFN, "rb") as fp:
664 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000665 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000666 fp.seek(0,0)
667 zip_contents = fp.read()
668 # - passing a file-like object
669 fp = io.BytesIO()
670 fp.write(zip_contents)
671 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000672 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000673 fp.seek(0,0)
674 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000675 self.assertTrue(chk)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000676
677 def testNonExistentFileRaisesIOError(self):
678 # make sure we don't raise an AttributeError when a partially-constructed
679 # ZipFile instance is finalized; this tests for regression on SF tracker
680 # bug #403871.
681
682 # The bug we're testing for caused an AttributeError to be raised
683 # when a ZipFile instance was created for a file that did not
684 # exist; the .fp member was not initialized but was needed by the
685 # __del__() method. Since the AttributeError is in the __del__(),
686 # it is ignored, but the user should be sufficiently annoyed by
687 # the message on the output that regression will be noticed
688 # quickly.
689 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
690
691 def testClosedZipRaisesRuntimeError(self):
692 # Verify that testzip() doesn't swallow inappropriate exceptions.
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000693 data = io.BytesIO()
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000694 zipf = zipfile.ZipFile(data, mode="w")
695 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
696 zipf.close()
697
698 # This is correct; calling .read on a closed ZipFile should throw
699 # a RuntimeError, and so should calling .testzip. An earlier
700 # version of .testzip would swallow this exception (and any other)
701 # and report that the first file in the archive was corrupt.
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000702 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
703 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000704 self.assertRaises(RuntimeError, zipf.testzip)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000705 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Guido van Rossum814661e2007-07-18 22:07:29 +0000706 open(TESTFN, 'w').write('zipfile test data')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000707 self.assertRaises(RuntimeError, zipf.write, TESTFN)
708
709 def test_BadConstructorMode(self):
710 # Check that bad modes passed to ZipFile constructor are caught
711 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
712
713 def test_BadOpenMode(self):
714 # Check that bad modes passed to ZipFile.open are caught
715 zipf = zipfile.ZipFile(TESTFN, mode="w")
716 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
717 zipf.close()
718 zipf = zipfile.ZipFile(TESTFN, mode="r")
719 # read the data to make sure the file is there
720 zipf.read("foo.txt")
721 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
722 zipf.close()
723
724 def test_Read0(self):
725 # Check that calling read(0) on a ZipExtFile object returns an empty
726 # string and doesn't advance file pointer
727 zipf = zipfile.ZipFile(TESTFN, mode="w")
728 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
729 # read the data to make sure the file is there
730 f = zipf.open("foo.txt")
731 for i in range(FIXEDTEST_SIZE):
Guido van Rossum814661e2007-07-18 22:07:29 +0000732 self.assertEqual(f.read(0), b'')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000733
Guido van Rossum814661e2007-07-18 22:07:29 +0000734 self.assertEqual(f.read(), b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000735 zipf.close()
736
737 def test_OpenNonexistentItem(self):
738 # Check that attempting to call open() for an item that doesn't
739 # exist in the archive raises a RuntimeError
740 zipf = zipfile.ZipFile(TESTFN, mode="w")
741 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
742
743 def test_BadCompressionMode(self):
744 # Check that bad compression methods passed to ZipFile.open are caught
745 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
746
747 def test_NullByteInFilename(self):
748 # Check that a filename containing a null byte is properly terminated
749 zipf = zipfile.ZipFile(TESTFN, mode="w")
Guido van Rossum814661e2007-07-18 22:07:29 +0000750 zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000751 self.assertEqual(zipf.namelist(), ['foo.txt'])
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000752
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000753 def test_StructSizes(self):
754 # check that ZIP internal structure sizes are calculated correctly
755 self.assertEqual(zipfile.sizeEndCentDir, 22)
756 self.assertEqual(zipfile.sizeCentralDir, 46)
757 self.assertEqual(zipfile.sizeEndCentDir64, 56)
758 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
759
760 def testComments(self):
761 # This test checks that comments on the archive are handled properly
762
763 # check default comment is empty
764 zipf = zipfile.ZipFile(TESTFN, mode="w")
765 self.assertEqual(zipf.comment, b'')
766 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
767 zipf.close()
768 zipfr = zipfile.ZipFile(TESTFN, mode="r")
769 self.assertEqual(zipfr.comment, b'')
770 zipfr.close()
771
772 # check a simple short comment
773 comment = b'Bravely taking to his feet, he beat a very brave retreat.'
774 zipf = zipfile.ZipFile(TESTFN, mode="w")
775 zipf.comment = comment
776 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
777 zipf.close()
778 zipfr = zipfile.ZipFile(TESTFN, mode="r")
779 self.assertEqual(zipfr.comment, comment)
780 zipfr.close()
781
782 # check a comment of max length
783 comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)])
784 comment2 = comment2.encode("ascii")
785 zipf = zipfile.ZipFile(TESTFN, mode="w")
786 zipf.comment = comment2
787 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
788 zipf.close()
789 zipfr = zipfile.ZipFile(TESTFN, mode="r")
790 self.assertEqual(zipfr.comment, comment2)
791 zipfr.close()
792
793 # check a comment that is too long is truncated
794 zipf = zipfile.ZipFile(TESTFN, mode="w")
795 zipf.comment = comment2 + b'oops'
796 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
797 zipf.close()
798 zipfr = zipfile.ZipFile(TESTFN, mode="r")
799 self.assertEqual(zipfr.comment, comment2)
800 zipfr.close()
801
Guido van Rossumd8faa362007-04-27 19:54:29 +0000802 def tearDown(self):
803 support.unlink(TESTFN)
804 support.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000805
806class DecryptionTests(unittest.TestCase):
807 # This test checks that ZIP decryption works. Since the library does not
808 # support encryption at the moment, we use a pre-generated encrypted
809 # ZIP file
810
811 data = (
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000812 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
813 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
814 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
815 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
816 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
817 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
818 b'\x00\x00L\x00\x00\x00\x00\x00' )
Christian Heimesfdab48e2008-01-20 09:06:41 +0000819 data2 = (
820 b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
821 b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
822 b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
823 b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
824 b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
825 b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
826 b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
827 b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Thomas Wouterscf297e42007-02-23 15:07:44 +0000828
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000829 plain = b'zipfile.py encryption test'
Christian Heimesfdab48e2008-01-20 09:06:41 +0000830 plain2 = b'\x00'*512
Thomas Wouterscf297e42007-02-23 15:07:44 +0000831
832 def setUp(self):
833 fp = open(TESTFN, "wb")
834 fp.write(self.data)
835 fp.close()
836 self.zip = zipfile.ZipFile(TESTFN, "r")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000837 fp = open(TESTFN2, "wb")
838 fp.write(self.data2)
839 fp.close()
840 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000841
842 def tearDown(self):
843 self.zip.close()
844 os.unlink(TESTFN)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000845 self.zip2.close()
846 os.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000847
848 def testNoPassword(self):
849 # Reading the encrypted file without password
850 # must generate a RunTime exception
851 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000852 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000853
854 def testBadPassword(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000855 self.zip.setpassword(b"perl")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000856 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000857 self.zip2.setpassword(b"perl")
858 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000859
Thomas Wouterscf297e42007-02-23 15:07:44 +0000860 def testGoodPassword(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000861 self.zip.setpassword(b"python")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000862 self.assertEquals(self.zip.read("test.txt"), self.plain)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000863 self.zip2.setpassword(b"12345")
864 self.assertEquals(self.zip2.read("zero"), self.plain2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000865
Guido van Rossumd8faa362007-04-27 19:54:29 +0000866
867class TestsWithRandomBinaryFiles(unittest.TestCase):
868 def setUp(self):
869 datacount = randint(16, 64)*1024 + randint(1, 1024)
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000870 self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
871 for i in range(datacount))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000872
873 # Make a source file with some lines
874 fp = open(TESTFN, "wb")
875 fp.write(self.data)
876 fp.close()
877
878 def tearDown(self):
879 support.unlink(TESTFN)
880 support.unlink(TESTFN2)
881
882 def makeTestArchive(self, f, compression):
883 # Create the ZIP archive
884 zipfp = zipfile.ZipFile(f, "w", compression)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000885 zipfp.write(TESTFN, "another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000886 zipfp.write(TESTFN, TESTFN)
887 zipfp.close()
888
889 def zipTest(self, f, compression):
890 self.makeTestArchive(f, compression)
891
892 # Read the ZIP archive
893 zipfp = zipfile.ZipFile(f, "r", compression)
894 testdata = zipfp.read(TESTFN)
895 self.assertEqual(len(testdata), len(self.data))
896 self.assertEqual(testdata, self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000897 self.assertEqual(zipfp.read("another.name"), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000898 zipfp.close()
899
900 def testStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000901 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000902 self.zipTest(f, zipfile.ZIP_STORED)
903
904 def zipOpenTest(self, f, compression):
905 self.makeTestArchive(f, compression)
906
907 # Read the ZIP archive
908 zipfp = zipfile.ZipFile(f, "r", compression)
909 zipdata1 = []
910 zipopen1 = zipfp.open(TESTFN)
911 while 1:
912 read_data = zipopen1.read(256)
913 if not read_data:
914 break
915 zipdata1.append(read_data)
916
917 zipdata2 = []
Skip Montanaro7a98be22007-08-16 14:35:24 +0000918 zipopen2 = zipfp.open("another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000919 while 1:
920 read_data = zipopen2.read(256)
921 if not read_data:
922 break
923 zipdata2.append(read_data)
924
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000925 testdata1 = b''.join(zipdata1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000926 self.assertEqual(len(testdata1), len(self.data))
927 self.assertEqual(testdata1, self.data)
928
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000929 testdata2 = b''.join(zipdata2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000930 self.assertEqual(len(testdata1), len(self.data))
931 self.assertEqual(testdata1, self.data)
932 zipfp.close()
933
934 def testOpenStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000935 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000936 self.zipOpenTest(f, zipfile.ZIP_STORED)
937
938 def zipRandomOpenTest(self, f, compression):
939 self.makeTestArchive(f, compression)
940
941 # Read the ZIP archive
942 zipfp = zipfile.ZipFile(f, "r", compression)
943 zipdata1 = []
944 zipopen1 = zipfp.open(TESTFN)
945 while 1:
946 read_data = zipopen1.read(randint(1, 1024))
947 if not read_data:
948 break
949 zipdata1.append(read_data)
950
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000951 testdata = b''.join(zipdata1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000952 self.assertEqual(len(testdata), len(self.data))
953 self.assertEqual(testdata, self.data)
954 zipfp.close()
955
956 def testRandomOpenStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000957 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000958 self.zipRandomOpenTest(f, zipfile.ZIP_STORED)
959
960class TestsWithMultipleOpens(unittest.TestCase):
961 def setUp(self):
962 # Create the ZIP archive
963 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
964 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
965 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
966 zipfp.close()
967
968 def testSameFile(self):
969 # Verify that (when the ZipFile is in control of creating file objects)
970 # multiple open() calls can be made without interfering with each other.
971 zipf = zipfile.ZipFile(TESTFN2, mode="r")
972 zopen1 = zipf.open('ones')
973 zopen2 = zipf.open('ones')
974 data1 = zopen1.read(500)
975 data2 = zopen2.read(500)
976 data1 += zopen1.read(500)
977 data2 += zopen2.read(500)
978 self.assertEqual(data1, data2)
979 zipf.close()
980
981 def testDifferentFile(self):
982 # Verify that (when the ZipFile is in control of creating file objects)
983 # multiple open() calls can be made without interfering with each other.
984 zipf = zipfile.ZipFile(TESTFN2, mode="r")
985 zopen1 = zipf.open('ones')
986 zopen2 = zipf.open('twos')
987 data1 = zopen1.read(500)
988 data2 = zopen2.read(500)
989 data1 += zopen1.read(500)
990 data2 += zopen2.read(500)
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000991 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
992 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000993 zipf.close()
994
995 def testInterleaved(self):
996 # Verify that (when the ZipFile is in control of creating file objects)
997 # multiple open() calls can be made without interfering with each other.
998 zipf = zipfile.ZipFile(TESTFN2, mode="r")
999 zopen1 = zipf.open('ones')
1000 data1 = zopen1.read(500)
1001 zopen2 = zipf.open('twos')
1002 data2 = zopen2.read(500)
1003 data1 += zopen1.read(500)
1004 data2 += zopen2.read(500)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001005 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1006 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001007 zipf.close()
1008
1009 def tearDown(self):
1010 os.remove(TESTFN2)
1011
Martin v. Löwis59e47792009-01-24 14:10:07 +00001012class TestWithDirectory(unittest.TestCase):
1013 def setUp(self):
1014 os.mkdir(TESTFN2)
1015
1016 def testExtractDir(self):
1017 zipf = zipfile.ZipFile(findfile("zipdir.zip"))
1018 zipf.extractall(TESTFN2)
1019 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1020 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1021 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1022
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001023 def test_bug_6050(self):
1024 # Extraction should succeed if directories already exist
1025 os.mkdir(os.path.join(TESTFN2, "a"))
1026 self.testExtractDir()
1027
Martin v. Löwis59e47792009-01-24 14:10:07 +00001028 def testStoreDir(self):
1029 os.mkdir(os.path.join(TESTFN2, "x"))
1030 zipf = zipfile.ZipFile(TESTFN, "w")
1031 zipf.write(os.path.join(TESTFN2, "x"), "x")
1032 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1033
1034 def tearDown(self):
1035 shutil.rmtree(TESTFN2)
1036 if os.path.exists(TESTFN):
1037 os.remove(TESTFN)
1038
Guido van Rossumd8faa362007-04-27 19:54:29 +00001039
1040class UniversalNewlineTests(unittest.TestCase):
1041 def setUp(self):
Guido van Rossum9c627722007-08-27 18:31:48 +00001042 self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001043 for i in range(FIXEDTEST_SIZE)]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001044 self.seps = ('\r', '\r\n', '\n')
1045 self.arcdata, self.arcfiles = {}, {}
1046 for n, s in enumerate(self.seps):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001047 b = s.encode("ascii")
1048 self.arcdata[s] = b.join(self.line_gen) + b
Guido van Rossumd8faa362007-04-27 19:54:29 +00001049 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001050 f = open(self.arcfiles[s], "wb")
1051 try:
1052 f.write(self.arcdata[s])
1053 finally:
1054 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001055
1056 def makeTestArchive(self, f, compression):
1057 # Create the ZIP archive
1058 zipfp = zipfile.ZipFile(f, "w", compression)
1059 for fn in self.arcfiles.values():
1060 zipfp.write(fn, fn)
1061 zipfp.close()
1062
1063 def readTest(self, f, compression):
1064 self.makeTestArchive(f, compression)
1065
1066 # Read the ZIP archive
1067 zipfp = zipfile.ZipFile(f, "r")
1068 for sep, fn in self.arcfiles.items():
1069 zipdata = zipfp.open(fn, "rU").read()
1070 self.assertEqual(self.arcdata[sep], zipdata)
1071
1072 zipfp.close()
1073
1074 def readlineTest(self, f, compression):
1075 self.makeTestArchive(f, compression)
1076
1077 # Read the ZIP archive
1078 zipfp = zipfile.ZipFile(f, "r")
1079 for sep, fn in self.arcfiles.items():
1080 zipopen = zipfp.open(fn, "rU")
1081 for line in self.line_gen:
1082 linedata = zipopen.readline()
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001083 self.assertEqual(linedata, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001084
1085 zipfp.close()
1086
1087 def readlinesTest(self, f, compression):
1088 self.makeTestArchive(f, compression)
1089
1090 # Read the ZIP archive
1091 zipfp = zipfile.ZipFile(f, "r")
1092 for sep, fn in self.arcfiles.items():
1093 ziplines = zipfp.open(fn, "rU").readlines()
1094 for line, zipline in zip(self.line_gen, ziplines):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001095 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001096
1097 zipfp.close()
1098
1099 def iterlinesTest(self, f, compression):
1100 self.makeTestArchive(f, compression)
1101
1102 # Read the ZIP archive
1103 zipfp = zipfile.ZipFile(f, "r")
1104 for sep, fn in self.arcfiles.items():
1105 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001106 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001107
1108 zipfp.close()
1109
1110 def testReadStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001111 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001112 self.readTest(f, zipfile.ZIP_STORED)
1113
1114 def testReadlineStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001115 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001116 self.readlineTest(f, zipfile.ZIP_STORED)
1117
1118 def testReadlinesStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001119 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001120 self.readlinesTest(f, zipfile.ZIP_STORED)
1121
1122 def testIterlinesStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001123 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001124 self.iterlinesTest(f, zipfile.ZIP_STORED)
1125
1126 if zlib:
1127 def testReadDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001128 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001129 self.readTest(f, zipfile.ZIP_DEFLATED)
1130
1131 def testReadlineDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001132 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001133 self.readlineTest(f, zipfile.ZIP_DEFLATED)
1134
1135 def testReadlinesDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001136 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001137 self.readlinesTest(f, zipfile.ZIP_DEFLATED)
1138
1139 def testIterlinesDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001140 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001141 self.iterlinesTest(f, zipfile.ZIP_DEFLATED)
1142
1143 def tearDown(self):
1144 for sep, fn in self.arcfiles.items():
1145 os.remove(fn)
1146 support.unlink(TESTFN)
1147 support.unlink(TESTFN2)
1148
1149
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001150def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +00001151 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1152 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Martin v. Löwis59e47792009-01-24 14:10:07 +00001153 TestWithDirectory,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001154 UniversalNewlineTests, TestsWithRandomBinaryFiles)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001155
1156if __name__ == "__main__":
1157 test_main()