blob: 2e52c6834ba795a60f62cc5acb531da0780bfb3f [file] [log] [blame]
Ezio Melotti74c96ec2009-07-08 22:24:06 +00001import io
2import os
Georg Brandl5ba11de2011-01-01 10:09:32 +00003import sys
Barry Warsaw28a691b2010-04-17 00:19:56 +00004import imp
Ezio Melotti35386712009-12-31 13:22:41 +00005import time
Ezio Melotti74c96ec2009-07-08 22:24:06 +00006import shutil
7import struct
8import zipfile
9import unittest
10
Tim Petersa45cacf2004-08-20 03:47:14 +000011
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000012from tempfile import TemporaryFile
Guido van Rossumd8faa362007-04-27 19:54:29 +000013from random import randint, random
Ezio Melotti74c96ec2009-07-08 22:24:06 +000014from unittest import skipUnless
Tim Petersa19a1682001-03-29 04:36:09 +000015
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +020016from test.support import TESTFN, run_unittest, findfile, unlink, requires_zlib, requires_bz2, requires_lzma
Guido van Rossum368f04a2000-04-10 13:23:04 +000017
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000018TESTFN2 = TESTFN + "2"
Martin v. Löwis59e47792009-01-24 14:10:07 +000019TESTFNDIR = TESTFN + "d"
Guido van Rossumb5a755e2007-07-18 18:15:48 +000020FIXEDTEST_SIZE = 1000
Georg Brandl5ba11de2011-01-01 10:09:32 +000021DATAFILES_DIR = 'zipfile_datafiles'
Guido van Rossum368f04a2000-04-10 13:23:04 +000022
Christian Heimes790c8232008-01-07 21:14:23 +000023SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
24 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
25 ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
26 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
27
Ezio Melotti76430242009-07-11 18:28:48 +000028
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000029class TestsWithSourceFile(unittest.TestCase):
30 def setUp(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +000031 self.line_gen = (bytes("Zipfile test line %d. random float: %f" %
Guido van Rossum9c627722007-08-27 18:31:48 +000032 (i, random()), "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +000033 for i in range(FIXEDTEST_SIZE))
34 self.data = b'\n'.join(self.line_gen) + b'\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000035
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000036 # Make a source file with some lines
Ezio Melotti35386712009-12-31 13:22:41 +000037 with open(TESTFN, "wb") as fp:
38 fp.write(self.data)
Tim Peters7d3bad62001-04-04 18:56:49 +000039
Ezio Melottiafd0d112009-07-15 17:17:17 +000040 def make_test_archive(self, f, compression):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000041 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000042 with zipfile.ZipFile(f, "w", compression) as zipfp:
43 zipfp.write(TESTFN, "another.name")
44 zipfp.write(TESTFN, TESTFN)
45 zipfp.writestr("strfile", self.data)
Tim Peters7d3bad62001-04-04 18:56:49 +000046
Ezio Melottiafd0d112009-07-15 17:17:17 +000047 def zip_test(self, f, compression):
48 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +000049
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000050 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000051 with zipfile.ZipFile(f, "r", compression) as zipfp:
52 self.assertEqual(zipfp.read(TESTFN), self.data)
53 self.assertEqual(zipfp.read("another.name"), self.data)
54 self.assertEqual(zipfp.read("strfile"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000055
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000056 # Print the ZIP directory
57 fp = io.StringIO()
58 zipfp.printdir(file=fp)
59 directory = fp.getvalue()
60 lines = directory.splitlines()
Ezio Melotti35386712009-12-31 13:22:41 +000061 self.assertEqual(len(lines), 4) # Number of files + header
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062
Benjamin Peterson577473f2010-01-19 00:09:57 +000063 self.assertIn('File Name', lines[0])
64 self.assertIn('Modified', lines[0])
65 self.assertIn('Size', lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066
Ezio Melotti35386712009-12-31 13:22:41 +000067 fn, date, time_, size = lines[1].split()
68 self.assertEqual(fn, 'another.name')
69 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
70 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
71 self.assertEqual(size, str(len(self.data)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000073 # Check the namelist
74 names = zipfp.namelist()
Ezio Melotti35386712009-12-31 13:22:41 +000075 self.assertEqual(len(names), 3)
Benjamin Peterson577473f2010-01-19 00:09:57 +000076 self.assertIn(TESTFN, names)
77 self.assertIn("another.name", names)
78 self.assertIn("strfile", names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000080 # Check infolist
81 infos = zipfp.infolist()
Ezio Melotti35386712009-12-31 13:22:41 +000082 names = [i.filename for i in infos]
83 self.assertEqual(len(names), 3)
Benjamin Peterson577473f2010-01-19 00:09:57 +000084 self.assertIn(TESTFN, names)
85 self.assertIn("another.name", names)
86 self.assertIn("strfile", names)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000087 for i in infos:
Ezio Melotti35386712009-12-31 13:22:41 +000088 self.assertEqual(i.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000089
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000090 # check getinfo
91 for nm in (TESTFN, "another.name", "strfile"):
92 info = zipfp.getinfo(nm)
Ezio Melotti35386712009-12-31 13:22:41 +000093 self.assertEqual(info.filename, nm)
94 self.assertEqual(info.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000096 # Check that testzip doesn't raise an exception
97 zipfp.testzip()
Benjamin Petersond285bdb2010-10-31 17:57:22 +000098 if not isinstance(f, str):
99 f.close()
Tim Peters7d3bad62001-04-04 18:56:49 +0000100
Ezio Melottiafd0d112009-07-15 17:17:17 +0000101 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000102 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000103 self.zip_test(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000104
Ezio Melottiafd0d112009-07-15 17:17:17 +0000105 def zip_open_test(self, f, compression):
106 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000107
108 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000109 with zipfile.ZipFile(f, "r", compression) as zipfp:
110 zipdata1 = []
Brian Curtin8fb9b862010-11-18 02:15:28 +0000111 with zipfp.open(TESTFN) as zipopen1:
112 while True:
113 read_data = zipopen1.read(256)
114 if not read_data:
115 break
116 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000117
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000118 zipdata2 = []
Brian Curtin8fb9b862010-11-18 02:15:28 +0000119 with zipfp.open("another.name") as zipopen2:
120 while True:
121 read_data = zipopen2.read(256)
122 if not read_data:
123 break
124 zipdata2.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000125
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000126 self.assertEqual(b''.join(zipdata1), self.data)
127 self.assertEqual(b''.join(zipdata2), self.data)
Benjamin Petersond285bdb2010-10-31 17:57:22 +0000128 if not isinstance(f, str):
129 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000130
Ezio Melottiafd0d112009-07-15 17:17:17 +0000131 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000132 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000133 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000134
Ezio Melottiafd0d112009-07-15 17:17:17 +0000135 def test_open_via_zip_info(self):
Georg Brandlb533e262008-05-25 18:19:30 +0000136 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000137 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
138 zipfp.writestr("name", "foo")
139 zipfp.writestr("name", "bar")
Georg Brandlb533e262008-05-25 18:19:30 +0000140
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000141 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
142 infos = zipfp.infolist()
143 data = b""
144 for info in infos:
Brian Curtin8fb9b862010-11-18 02:15:28 +0000145 with zipfp.open(info) as zipopen:
146 data += zipopen.read()
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000147 self.assertTrue(data == b"foobar" or data == b"barfoo")
148 data = b""
149 for info in infos:
150 data += zipfp.read(info)
151 self.assertTrue(data == b"foobar" or data == b"barfoo")
Georg Brandlb533e262008-05-25 18:19:30 +0000152
Ezio Melottiafd0d112009-07-15 17:17:17 +0000153 def zip_random_open_test(self, f, compression):
154 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000155
156 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000157 with zipfile.ZipFile(f, "r", compression) as zipfp:
158 zipdata1 = []
Brian Curtin8fb9b862010-11-18 02:15:28 +0000159 with zipfp.open(TESTFN) as zipopen1:
160 while True:
161 read_data = zipopen1.read(randint(1, 1024))
162 if not read_data:
163 break
164 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000165
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000166 self.assertEqual(b''.join(zipdata1), self.data)
Benjamin Petersond285bdb2010-10-31 17:57:22 +0000167 if not isinstance(f, str):
168 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000169
Ezio Melottiafd0d112009-07-15 17:17:17 +0000170 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000171 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000172 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000173
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000174 def test_univeral_readaheads(self):
175 f = io.BytesIO()
176
177 data = b'a\r\n' * 16 * 1024
178 zipfp = zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED)
179 zipfp.writestr(TESTFN, data)
180 zipfp.close()
181
182 data2 = b''
183 zipfp = zipfile.ZipFile(f, 'r')
Brian Curtin8fb9b862010-11-18 02:15:28 +0000184 with zipfp.open(TESTFN, 'rU') as zipopen:
185 for line in zipopen:
186 data2 += line
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000187 zipfp.close()
188
189 self.assertEqual(data, data2.replace(b'\n', b'\r\n'))
190
191 def zip_readline_read_test(self, f, compression):
192 self.make_test_archive(f, compression)
193
194 # Read the ZIP archive
195 zipfp = zipfile.ZipFile(f, "r")
Brian Curtin8fb9b862010-11-18 02:15:28 +0000196 with zipfp.open(TESTFN) as zipopen:
197 data = b''
198 while True:
199 read = zipopen.readline()
200 if not read:
201 break
202 data += read
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000203
Brian Curtin8fb9b862010-11-18 02:15:28 +0000204 read = zipopen.read(100)
205 if not read:
206 break
207 data += read
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000208
209 self.assertEqual(data, self.data)
210 zipfp.close()
Benjamin Petersond285bdb2010-10-31 17:57:22 +0000211 if not isinstance(f, str):
212 f.close()
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000213
Ezio Melottiafd0d112009-07-15 17:17:17 +0000214 def zip_readline_test(self, f, compression):
215 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000216
217 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000218 with zipfile.ZipFile(f, "r") as zipfp:
Brian Curtin8fb9b862010-11-18 02:15:28 +0000219 with zipfp.open(TESTFN) as zipopen:
220 for line in self.line_gen:
221 linedata = zipopen.readline()
222 self.assertEqual(linedata, line + '\n')
Benjamin Petersond285bdb2010-10-31 17:57:22 +0000223 if not isinstance(f, str):
224 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000225
Ezio Melottiafd0d112009-07-15 17:17:17 +0000226 def zip_readlines_test(self, f, compression):
227 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000228
229 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000230 with zipfile.ZipFile(f, "r") as zipfp:
Brian Curtin8fb9b862010-11-18 02:15:28 +0000231 with zipfp.open(TESTFN) as zipopen:
232 ziplines = zipopen.readlines()
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000233 for line, zipline in zip(self.line_gen, ziplines):
234 self.assertEqual(zipline, line + '\n')
Benjamin Petersond285bdb2010-10-31 17:57:22 +0000235 if not isinstance(f, str):
236 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000237
Ezio Melottiafd0d112009-07-15 17:17:17 +0000238 def zip_iterlines_test(self, f, compression):
239 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000240
241 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000242 with zipfile.ZipFile(f, "r") as zipfp:
Brian Curtin8fb9b862010-11-18 02:15:28 +0000243 with zipfp.open(TESTFN) as zipopen:
244 for line, zipline in zip(self.line_gen, zipopen):
245 self.assertEqual(zipline, line + '\n')
Benjamin Petersond285bdb2010-10-31 17:57:22 +0000246 if not isinstance(f, str):
247 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000248
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000249 def test_readline_read_stored(self):
250 # Issue #7610: calls to readline() interleaved with calls to read().
251 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
252 self.zip_readline_read_test(f, zipfile.ZIP_STORED)
253
Ezio Melottiafd0d112009-07-15 17:17:17 +0000254 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000255 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000256 self.zip_readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000257
Ezio Melottiafd0d112009-07-15 17:17:17 +0000258 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000259 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000260 self.zip_readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000261
Ezio Melottiafd0d112009-07-15 17:17:17 +0000262 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000263 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000264 self.zip_iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000265
Ezio Melotti975077a2011-05-19 22:03:22 +0300266 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +0000267 def test_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000268 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000269 self.zip_test(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000270
Guido van Rossumd8faa362007-04-27 19:54:29 +0000271
Ezio Melotti975077a2011-05-19 22:03:22 +0300272 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +0000273 def test_open_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000274 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000275 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000276
Ezio Melotti975077a2011-05-19 22:03:22 +0300277 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +0000278 def test_random_open_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000279 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000280 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000281
Ezio Melotti975077a2011-05-19 22:03:22 +0300282 @requires_zlib
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000283 def test_readline_read_deflated(self):
284 # Issue #7610: calls to readline() interleaved with calls to read().
285 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
286 self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED)
287
Ezio Melotti975077a2011-05-19 22:03:22 +0300288 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +0000289 def test_readline_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000290 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000291 self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000292
Ezio Melotti975077a2011-05-19 22:03:22 +0300293 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +0000294 def test_readlines_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000295 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000296 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000297
Ezio Melotti975077a2011-05-19 22:03:22 +0300298 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +0000299 def test_iterlines_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000300 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000301 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000302
Ezio Melotti975077a2011-05-19 22:03:22 +0300303 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +0000304 def test_low_compression(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000305 """Check for cases where compressed data is larger than original."""
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000306 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000307 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
308 zipfp.writestr("strfile", '12')
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000309
310 # Get an open object for strfile
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000311 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
Brian Curtin8fb9b862010-11-18 02:15:28 +0000312 with zipfp.open("strfile") as openobj:
313 self.assertEqual(openobj.read(1), b'1')
314 self.assertEqual(openobj.read(1), b'2')
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000315
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200316 @requires_bz2
317 def test_bzip2(self):
318 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
319 self.zip_test(f, zipfile.ZIP_BZIP2)
320
321 @requires_bz2
322 def test_open_bzip2(self):
323 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
324 self.zip_open_test(f, zipfile.ZIP_BZIP2)
325
326 @requires_bz2
327 def test_random_open_bzip2(self):
328 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
329 self.zip_random_open_test(f, zipfile.ZIP_BZIP2)
330
331 @requires_bz2
332 def test_readline_read_bzip2(self):
333 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
334 self.zip_readline_read_test(f, zipfile.ZIP_BZIP2)
335
336 @requires_bz2
337 def test_readline_bzip2(self):
338 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
339 self.zip_readline_test(f, zipfile.ZIP_BZIP2)
340
341 @requires_bz2
342 def test_readlines_bzip2(self):
343 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
344 self.zip_readlines_test(f, zipfile.ZIP_BZIP2)
345
346 @requires_bz2
347 def test_iterlines_bzip2(self):
348 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
349 self.zip_iterlines_test(f, zipfile.ZIP_BZIP2)
350
351 @requires_bz2
352 def test_low_compression_bzip2(self):
353 """Check for cases where compressed data is larger than original."""
354 # Create the ZIP archive
355 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_BZIP2) as zipfp:
356 zipfp.writestr("strfile", '12')
357
358 # Get an open object for strfile
359 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_BZIP2) as zipfp:
360 with zipfp.open("strfile") as openobj:
361 self.assertEqual(openobj.read(1), b'1')
362 self.assertEqual(openobj.read(1), b'2')
363
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200364 @requires_lzma
365 def test_lzma(self):
366 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
367 self.zip_test(f, zipfile.ZIP_LZMA)
368
369 @requires_lzma
370 def test_open_lzma(self):
371 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
372 self.zip_open_test(f, zipfile.ZIP_LZMA)
373
374 @requires_lzma
375 def test_random_open_lzma(self):
376 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
377 self.zip_random_open_test(f, zipfile.ZIP_LZMA)
378
379 @requires_lzma
380 def test_readline_read_lzma(self):
381 # Issue #7610: calls to readline() interleaved with calls to read().
382 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
383 self.zip_readline_read_test(f, zipfile.ZIP_LZMA)
384
385 @requires_lzma
386 def test_readline_lzma(self):
387 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
388 self.zip_readline_test(f, zipfile.ZIP_LZMA)
389
390 @requires_lzma
391 def test_readlines_lzma(self):
392 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
393 self.zip_readlines_test(f, zipfile.ZIP_LZMA)
394
395 @requires_lzma
396 def test_iterlines_lzma(self):
397 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
398 self.zip_iterlines_test(f, zipfile.ZIP_LZMA)
399
400 @requires_lzma
401 def test_low_compression_lzma(self):
402 """Check for cases where compressed data is larger than original."""
403 # Create the ZIP archive
404 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_LZMA) as zipfp:
405 zipfp.writestr("strfile", '12')
406
407 # Get an open object for strfile
408 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_LZMA) as zipfp:
409 with zipfp.open("strfile") as openobj:
410 self.assertEqual(openobj.read(1), b'1')
411 self.assertEqual(openobj.read(1), b'2')
412
Ezio Melottiafd0d112009-07-15 17:17:17 +0000413 def test_absolute_arcnames(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000414 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
415 zipfp.write(TESTFN, "/absolute")
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000416
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000417 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
418 self.assertEqual(zipfp.namelist(), ["absolute"])
Tim Peters32cbc962006-02-20 21:42:18 +0000419
Ezio Melottiafd0d112009-07-15 17:17:17 +0000420 def test_append_to_zip_file(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000421 """Test appending to an existing zipfile."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000422 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
423 zipfp.write(TESTFN, TESTFN)
424
425 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
426 zipfp.writestr("strfile", self.data)
427 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000428
Ezio Melottiafd0d112009-07-15 17:17:17 +0000429 def test_append_to_non_zip_file(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000430 """Test appending to an existing file that is not a zipfile."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000431 # NOTE: this test fails if len(d) < 22 because of the first
432 # line "fpin.seek(-22, 2)" in _EndRecData
Ezio Melotti35386712009-12-31 13:22:41 +0000433 data = b'I am not a ZipFile!'*10
434 with open(TESTFN2, 'wb') as f:
435 f.write(data)
436
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000437 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
438 zipfp.write(TESTFN, TESTFN)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000439
Ezio Melotti35386712009-12-31 13:22:41 +0000440 with open(TESTFN2, 'rb') as f:
441 f.seek(len(data))
442 with zipfile.ZipFile(f, "r") as zipfp:
443 self.assertEqual(zipfp.namelist(), [TESTFN])
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000444
R David Murray4fbb9db2011-06-09 15:50:51 -0400445 def test_ignores_newline_at_end(self):
446 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
447 zipfp.write(TESTFN, TESTFN)
448 with open(TESTFN2, 'a') as f:
449 f.write("\r\n\00\00\00")
450 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
451 self.assertIsInstance(zipfp, zipfile.ZipFile)
452
453 def test_ignores_stuff_appended_past_comments(self):
454 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
455 zipfp.comment = b"this is a comment"
456 zipfp.write(TESTFN, TESTFN)
457 with open(TESTFN2, 'a') as f:
458 f.write("abcdef\r\n")
459 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
460 self.assertIsInstance(zipfp, zipfile.ZipFile)
461 self.assertEqual(zipfp.comment, b"this is a comment")
462
Ezio Melottiafd0d112009-07-15 17:17:17 +0000463 def test_write_default_name(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000464 """Check that calling ZipFile.write without arcname specified
465 produces the expected result."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000466 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
467 zipfp.write(TESTFN)
Brian Curtin8fb9b862010-11-18 02:15:28 +0000468 with open(TESTFN, "rb") as f:
469 self.assertEqual(zipfp.read(TESTFN), f.read())
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000470
Ezio Melotti975077a2011-05-19 22:03:22 +0300471 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +0000472 def test_per_file_compression(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000473 """Check that files within a Zip archive can have different
474 compression options."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000475 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
476 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
477 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
478 sinfo = zipfp.getinfo('storeme')
479 dinfo = zipfp.getinfo('deflateme')
480 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
481 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000482
Ezio Melottiafd0d112009-07-15 17:17:17 +0000483 def test_write_to_readonly(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000484 """Check that trying to call write() on a readonly ZipFile object
485 raises a RuntimeError."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000486 with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
487 zipfp.writestr("somefile.txt", "bogus")
488
489 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
490 self.assertRaises(RuntimeError, zipfp.write, TESTFN)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000491
Ezio Melottiafd0d112009-07-15 17:17:17 +0000492 def test_extract(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000493 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
494 for fpath, fdata in SMALL_TEST_DATA:
495 zipfp.writestr(fpath, fdata)
Christian Heimes790c8232008-01-07 21:14:23 +0000496
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000497 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
498 for fpath, fdata in SMALL_TEST_DATA:
499 writtenfile = zipfp.extract(fpath)
Christian Heimes790c8232008-01-07 21:14:23 +0000500
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000501 # make sure it was written to the right place
502 if os.path.isabs(fpath):
503 correctfile = os.path.join(os.getcwd(), fpath[1:])
504 else:
505 correctfile = os.path.join(os.getcwd(), fpath)
506 correctfile = os.path.normpath(correctfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000507
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000508 self.assertEqual(writtenfile, correctfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000509
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000510 # make sure correct data is in correct file
Brian Curtin8fb9b862010-11-18 02:15:28 +0000511 with open(writtenfile, "rb") as f:
512 self.assertEqual(fdata.encode(), f.read())
Christian Heimes790c8232008-01-07 21:14:23 +0000513
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000514 os.remove(writtenfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000515
516 # remove the test file subdirectories
517 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
518
Ezio Melottiafd0d112009-07-15 17:17:17 +0000519 def test_extract_all(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000520 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
521 for fpath, fdata in SMALL_TEST_DATA:
522 zipfp.writestr(fpath, fdata)
Christian Heimes790c8232008-01-07 21:14:23 +0000523
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000524 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
525 zipfp.extractall()
526 for fpath, fdata in SMALL_TEST_DATA:
527 if os.path.isabs(fpath):
528 outfile = os.path.join(os.getcwd(), fpath[1:])
529 else:
530 outfile = os.path.join(os.getcwd(), fpath)
Christian Heimes790c8232008-01-07 21:14:23 +0000531
Brian Curtin8fb9b862010-11-18 02:15:28 +0000532 with open(outfile, "rb") as f:
533 self.assertEqual(fdata.encode(), f.read())
Christian Heimes790c8232008-01-07 21:14:23 +0000534
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000535 os.remove(outfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000536
537 # remove the test file subdirectories
538 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
539
Ezio Melotti975077a2011-05-19 22:03:22 +0300540 def test_writestr_compression_stored(self):
Ronald Oussorenee5c8852010-02-07 20:24:02 +0000541 zipfp = zipfile.ZipFile(TESTFN2, "w")
542 zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED)
Ronald Oussorenee5c8852010-02-07 20:24:02 +0000543 info = zipfp.getinfo('a.txt')
544 self.assertEqual(info.compress_type, zipfile.ZIP_STORED)
545
Ezio Melotti975077a2011-05-19 22:03:22 +0300546 @requires_zlib
547 def test_writestr_compression_deflated(self):
548 zipfp = zipfile.ZipFile(TESTFN2, "w")
549 zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED)
550 info = zipfp.getinfo('b.txt')
551 self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED)
Ronald Oussorenee5c8852010-02-07 20:24:02 +0000552
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200553 @requires_bz2
554 def test_writestr_compression_bzip2(self):
555 zipfp = zipfile.ZipFile(TESTFN2, "w")
556 zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_BZIP2)
557 info = zipfp.getinfo('b.txt')
558 self.assertEqual(info.compress_type, zipfile.ZIP_BZIP2)
559
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200560 @requires_lzma
561 def test_writestr_compression_lzma(self):
562 zipfp = zipfile.ZipFile(TESTFN2, "w")
563 zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_LZMA)
564 info = zipfp.getinfo('b.txt')
565 self.assertEqual(info.compress_type, zipfile.ZIP_LZMA)
566
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000567 def zip_test_writestr_permissions(self, f, compression):
568 # Make sure that writestr creates files with mode 0600,
569 # when it is passed a name rather than a ZipInfo instance.
570
Ezio Melottiafd0d112009-07-15 17:17:17 +0000571 self.make_test_archive(f, compression)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000572 with zipfile.ZipFile(f, "r") as zipfp:
573 zinfo = zipfp.getinfo('strfile')
574 self.assertEqual(zinfo.external_attr, 0o600 << 16)
Benjamin Petersond285bdb2010-10-31 17:57:22 +0000575 if not isinstance(f, str):
576 f.close()
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000577
Ezio Melottiafd0d112009-07-15 17:17:17 +0000578 def test_writestr_permissions(self):
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000579 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
580 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
581
Gregory P. Smithb0d9ca92009-07-07 05:06:04 +0000582 def test_writestr_extended_local_header_issue1202(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000583 with zipfile.ZipFile(TESTFN2, 'w') as orig_zip:
584 for data in 'abcdefghijklmnop':
585 zinfo = zipfile.ZipInfo(data)
586 zinfo.flag_bits |= 0x08 # Include an extended local header.
587 orig_zip.writestr(zinfo, data)
588
589 def test_close(self):
590 """Check that the zipfile is closed after the 'with' block."""
591 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
592 for fpath, fdata in SMALL_TEST_DATA:
593 zipfp.writestr(fpath, fdata)
594 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
595 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
596
597 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
598 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
599 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
600
601 def test_close_on_exception(self):
602 """Check that the zipfile is closed if an exception is raised in the
603 'with' block."""
604 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
605 for fpath, fdata in SMALL_TEST_DATA:
606 zipfp.writestr(fpath, fdata)
607
608 try:
609 with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
Georg Brandl4d540882010-10-28 06:42:33 +0000610 raise zipfile.BadZipFile()
611 except zipfile.BadZipFile:
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000612 self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
Gregory P. Smithb0d9ca92009-07-07 05:06:04 +0000613
Senthil Kumaran29fa9d42011-10-20 01:46:00 +0800614 def test_add_file_before_1980(self):
615 # Set atime and mtime to 1970-01-01
616 os.utime(TESTFN, (0, 0))
617 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
618 self.assertRaises(ValueError, zipfp.write, TESTFN)
619
620
Senthil Kumaran7e3062b2011-10-20 01:52:41 +0800621
622
623
624
625
Ezio Melotti975077a2011-05-19 22:03:22 +0300626 @requires_zlib
Georg Brandl5ba11de2011-01-01 10:09:32 +0000627 def test_unicode_filenames(self):
Georg Brandl04480a82011-01-01 10:42:31 +0000628 # bug #10801
629 fname = findfile('zip_cp437_header.zip')
Georg Brandl5ba11de2011-01-01 10:09:32 +0000630 with zipfile.ZipFile(fname) as zipfp:
Georg Brandl04480a82011-01-01 10:42:31 +0000631 for name in zipfp.namelist():
632 zipfp.open(name).close()
Georg Brandl5ba11de2011-01-01 10:09:32 +0000633
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000634 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000635 unlink(TESTFN)
636 unlink(TESTFN2)
637
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000638
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000639class TestZip64InSmallFiles(unittest.TestCase):
640 # These tests test the ZIP64 functionality without using large files,
641 # see test_zipfile64 for proper tests.
642
643 def setUp(self):
644 self._limit = zipfile.ZIP64_LIMIT
645 zipfile.ZIP64_LIMIT = 5
646
Guido van Rossum9c627722007-08-27 18:31:48 +0000647 line_gen = (bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000648 for i in range(0, FIXEDTEST_SIZE))
649 self.data = b'\n'.join(line_gen)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000650
651 # Make a source file with some lines
Ezio Melotti35386712009-12-31 13:22:41 +0000652 with open(TESTFN, "wb") as fp:
653 fp.write(self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000654
Ezio Melottiafd0d112009-07-15 17:17:17 +0000655 def large_file_exception_test(self, f, compression):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000656 with zipfile.ZipFile(f, "w", compression) as zipfp:
657 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti35386712009-12-31 13:22:41 +0000658 zipfp.write, TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000659
Ezio Melottiafd0d112009-07-15 17:17:17 +0000660 def large_file_exception_test2(self, f, compression):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000661 with zipfile.ZipFile(f, "w", compression) as zipfp:
662 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti35386712009-12-31 13:22:41 +0000663 zipfp.writestr, "another.name", self.data)
Benjamin Petersond285bdb2010-10-31 17:57:22 +0000664 if not isinstance(f, str):
665 f.close()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000666
Ezio Melottiafd0d112009-07-15 17:17:17 +0000667 def test_large_file_exception(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000668 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000669 self.large_file_exception_test(f, zipfile.ZIP_STORED)
670 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000671
Ezio Melottiafd0d112009-07-15 17:17:17 +0000672 def zip_test(self, f, compression):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000673 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000674 with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
675 zipfp.write(TESTFN, "another.name")
676 zipfp.write(TESTFN, TESTFN)
677 zipfp.writestr("strfile", self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000678
679 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000680 with zipfile.ZipFile(f, "r", compression) as zipfp:
681 self.assertEqual(zipfp.read(TESTFN), self.data)
682 self.assertEqual(zipfp.read("another.name"), self.data)
683 self.assertEqual(zipfp.read("strfile"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000684
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000685 # Print the ZIP directory
686 fp = io.StringIO()
687 zipfp.printdir(fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000688
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000689 directory = fp.getvalue()
690 lines = directory.splitlines()
Ezio Melotti35386712009-12-31 13:22:41 +0000691 self.assertEqual(len(lines), 4) # Number of files + header
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000692
Benjamin Peterson577473f2010-01-19 00:09:57 +0000693 self.assertIn('File Name', lines[0])
694 self.assertIn('Modified', lines[0])
695 self.assertIn('Size', lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000696
Ezio Melotti35386712009-12-31 13:22:41 +0000697 fn, date, time_, size = lines[1].split()
698 self.assertEqual(fn, 'another.name')
699 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
700 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
701 self.assertEqual(size, str(len(self.data)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000702
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000703 # Check the namelist
704 names = zipfp.namelist()
Ezio Melotti35386712009-12-31 13:22:41 +0000705 self.assertEqual(len(names), 3)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000706 self.assertIn(TESTFN, names)
707 self.assertIn("another.name", names)
708 self.assertIn("strfile", names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000709
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000710 # Check infolist
711 infos = zipfp.infolist()
Ezio Melotti35386712009-12-31 13:22:41 +0000712 names = [i.filename for i in infos]
713 self.assertEqual(len(names), 3)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000714 self.assertIn(TESTFN, names)
715 self.assertIn("another.name", names)
716 self.assertIn("strfile", names)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000717 for i in infos:
Ezio Melotti35386712009-12-31 13:22:41 +0000718 self.assertEqual(i.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000719
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000720 # check getinfo
721 for nm in (TESTFN, "another.name", "strfile"):
722 info = zipfp.getinfo(nm)
Ezio Melotti35386712009-12-31 13:22:41 +0000723 self.assertEqual(info.filename, nm)
724 self.assertEqual(info.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000725
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000726 # Check that testzip doesn't raise an exception
727 zipfp.testzip()
Benjamin Petersond285bdb2010-10-31 17:57:22 +0000728 if not isinstance(f, str):
729 f.close()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000730
Ezio Melottiafd0d112009-07-15 17:17:17 +0000731 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000732 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000733 self.zip_test(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000734
Ezio Melotti975077a2011-05-19 22:03:22 +0300735 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +0000736 def test_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000737 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000738 self.zip_test(f, zipfile.ZIP_DEFLATED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000739
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200740 @requires_bz2
741 def test_bzip2(self):
742 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
743 self.zip_test(f, zipfile.ZIP_BZIP2)
744
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200745 @requires_lzma
746 def test_lzma(self):
747 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
748 self.zip_test(f, zipfile.ZIP_LZMA)
749
Ezio Melottiafd0d112009-07-15 17:17:17 +0000750 def test_absolute_arcnames(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000751 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED,
752 allowZip64=True) as zipfp:
753 zipfp.write(TESTFN, "/absolute")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000754
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000755 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
756 self.assertEqual(zipfp.namelist(), ["absolute"])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000757
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000758 def tearDown(self):
759 zipfile.ZIP64_LIMIT = self._limit
Ezio Melotti76430242009-07-11 18:28:48 +0000760 unlink(TESTFN)
761 unlink(TESTFN2)
762
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000763
764class PyZipFileTests(unittest.TestCase):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000765 def test_write_pyfile(self):
Łukasz Langaa9f054b2010-11-23 00:15:02 +0000766 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000767 fn = __file__
768 if fn.endswith('.pyc') or fn.endswith('.pyo'):
Barry Warsaw28a691b2010-04-17 00:19:56 +0000769 path_split = fn.split(os.sep)
770 if os.altsep is not None:
771 path_split.extend(fn.split(os.altsep))
772 if '__pycache__' in path_split:
773 fn = imp.source_from_cache(fn)
774 else:
775 fn = fn[:-1]
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000776
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000777 zipfp.writepy(fn)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000778
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000779 bn = os.path.basename(fn)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000780 self.assertNotIn(bn, zipfp.namelist())
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000781 self.assertTrue(bn + 'o' in zipfp.namelist() or
782 bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000783
Łukasz Langaa9f054b2010-11-23 00:15:02 +0000784 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000785 fn = __file__
Ezio Melotti35386712009-12-31 13:22:41 +0000786 if fn.endswith(('.pyc', '.pyo')):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000787 fn = fn[:-1]
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000788
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000789 zipfp.writepy(fn, "testpackage")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000790
Ezio Melotti35386712009-12-31 13:22:41 +0000791 bn = "%s/%s" % ("testpackage", os.path.basename(fn))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000792 self.assertNotIn(bn, zipfp.namelist())
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000793 self.assertTrue(bn + 'o' in zipfp.namelist() or
794 bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000795
Ezio Melottiafd0d112009-07-15 17:17:17 +0000796 def test_write_python_package(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000797 import email
798 packagedir = os.path.dirname(email.__file__)
799
Łukasz Langaa9f054b2010-11-23 00:15:02 +0000800 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000801 zipfp.writepy(packagedir)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000802
Ezio Melotti35386712009-12-31 13:22:41 +0000803 # Check for a couple of modules at different levels of the
804 # hierarchy
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000805 names = zipfp.namelist()
806 self.assertTrue('email/__init__.pyo' in names or
807 'email/__init__.pyc' in names)
808 self.assertTrue('email/mime/text.pyo' in names or
809 'email/mime/text.pyc' in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000810
Georg Brandl8334fd92010-12-04 10:26:46 +0000811 def test_write_with_optimization(self):
812 import email
813 packagedir = os.path.dirname(email.__file__)
814 # use .pyc if running test in optimization mode,
815 # use .pyo if running test in debug mode
816 optlevel = 1 if __debug__ else 0
817 ext = '.pyo' if optlevel == 1 else '.pyc'
818
819 with TemporaryFile() as t, \
820 zipfile.PyZipFile(t, "w", optimize=optlevel) as zipfp:
821 zipfp.writepy(packagedir)
822
823 names = zipfp.namelist()
824 self.assertIn('email/__init__' + ext, names)
825 self.assertIn('email/mime/text' + ext, names)
826
Ezio Melottiafd0d112009-07-15 17:17:17 +0000827 def test_write_python_directory(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000828 os.mkdir(TESTFN2)
829 try:
Ezio Melotti35386712009-12-31 13:22:41 +0000830 with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
831 fp.write("print(42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000832
Ezio Melotti35386712009-12-31 13:22:41 +0000833 with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
834 fp.write("print(42 * 42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000835
Ezio Melotti35386712009-12-31 13:22:41 +0000836 with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp:
837 fp.write("bla bla bla\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000838
Łukasz Langaa9f054b2010-11-23 00:15:02 +0000839 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
840 zipfp.writepy(TESTFN2)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000841
Łukasz Langaa9f054b2010-11-23 00:15:02 +0000842 names = zipfp.namelist()
843 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
844 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
845 self.assertNotIn('mod2.txt', names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000846
847 finally:
848 shutil.rmtree(TESTFN2)
849
Ezio Melottiafd0d112009-07-15 17:17:17 +0000850 def test_write_non_pyfile(self):
Łukasz Langaa9f054b2010-11-23 00:15:02 +0000851 with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
Brian Curtin8fb9b862010-11-18 02:15:28 +0000852 with open(TESTFN, 'w') as f:
853 f.write('most definitely not a python file')
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000854 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
855 os.remove(TESTFN)
856
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000857
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000858class OtherTests(unittest.TestCase):
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +0000859 zips_with_bad_crc = {
860 zipfile.ZIP_STORED: (
861 b'PK\003\004\024\0\0\0\0\0 \213\212;:r'
862 b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af'
863 b'ilehello,AworldP'
864 b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:'
865 b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0'
866 b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi'
867 b'lePK\005\006\0\0\0\0\001\0\001\0003\000'
868 b'\0\0/\0\0\0\0\0'),
869 zipfile.ZIP_DEFLATED: (
870 b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA'
871 b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
872 b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0'
873 b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n'
874 b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05'
875 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00'
876 b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00'
877 b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'),
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200878 zipfile.ZIP_BZIP2: (
879 b'PK\x03\x04\x14\x03\x00\x00\x0c\x00nu\x0c=FA'
880 b'KE8\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
881 b'ileBZh91AY&SY\xd4\xa8\xca'
882 b'\x7f\x00\x00\x0f\x11\x80@\x00\x06D\x90\x80 \x00 \xa5'
883 b'P\xd9!\x03\x03\x13\x13\x13\x89\xa9\xa9\xc2u5:\x9f'
884 b'\x8b\xb9"\x9c(HjTe?\x80PK\x01\x02\x14'
885 b'\x03\x14\x03\x00\x00\x0c\x00nu\x0c=FAKE8'
886 b'\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00'
887 b'\x00 \x80\x80\x81\x00\x00\x00\x00afilePK'
888 b'\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00\x00[\x00'
889 b'\x00\x00\x00\x00'),
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200890 zipfile.ZIP_LZMA: (
891 b'PK\x03\x04\x14\x03\x00\x00\x0e\x00nu\x0c=FA'
892 b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
893 b'ile\t\x04\x05\x00]\x00\x00\x00\x04\x004\x19I'
894 b'\xee\x8d\xe9\x17\x89:3`\tq!.8\x00PK'
895 b'\x01\x02\x14\x03\x14\x03\x00\x00\x0e\x00nu\x0c=FA'
896 b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00'
897 b'\x00\x00\x00\x00 \x80\x80\x81\x00\x00\x00\x00afil'
898 b'ePK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00'
899 b'\x00>\x00\x00\x00\x00\x00'),
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +0000900 }
901
Martin v. Löwisd099b562012-05-01 14:08:22 +0200902 def test_unsupported_version(self):
903 # File has an extract_version of 120
904 data = (b'PK\x03\x04x\x00\x00\x00\x00\x00!p\xa1@\x00\x00\x00\x00\x00\x00'
905 b'\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00xPK\x01\x02x\x03x\x00\x00\x00\x00'
906 b'\x00!p\xa1@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00'
907 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00\x00xPK\x05\x06'
908 b'\x00\x00\x00\x00\x01\x00\x01\x00/\x00\x00\x00\x1f\x00\x00\x00\x00\x00')
909 self.assertRaises(NotImplementedError, zipfile.ZipFile,
910 io.BytesIO(data), 'r')
911
Ezio Melottiafd0d112009-07-15 17:17:17 +0000912 def test_unicode_filenames(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000913 with zipfile.ZipFile(TESTFN, "w") as zf:
914 zf.writestr("foo.txt", "Test for unicode filename")
915 zf.writestr("\xf6.txt", "Test for unicode filename")
Ezio Melottie9615932010-01-24 19:26:24 +0000916 self.assertIsInstance(zf.infolist()[0].filename, str)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000917
918 with zipfile.ZipFile(TESTFN, "r") as zf:
919 self.assertEqual(zf.filelist[0].filename, "foo.txt")
920 self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000921
Ezio Melottiafd0d112009-07-15 17:17:17 +0000922 def test_create_non_existent_file_for_append(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000923 if os.path.exists(TESTFN):
924 os.unlink(TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000925
Thomas Wouterscf297e42007-02-23 15:07:44 +0000926 filename = 'testfile.txt'
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000927 content = b'hello, world. this is some content.'
Guido van Rossumd8faa362007-04-27 19:54:29 +0000928
Thomas Wouterscf297e42007-02-23 15:07:44 +0000929 try:
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000930 with zipfile.ZipFile(TESTFN, 'a') as zf:
931 zf.writestr(filename, content)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000932 except IOError:
933 self.fail('Could not append data to a non-existent zip file.')
934
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000935 self.assertTrue(os.path.exists(TESTFN))
Thomas Wouterscf297e42007-02-23 15:07:44 +0000936
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000937 with zipfile.ZipFile(TESTFN, 'r') as zf:
938 self.assertEqual(zf.read(filename), content)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000939
Ezio Melottiafd0d112009-07-15 17:17:17 +0000940 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000941 # This test checks that the ZipFile constructor closes the file object
Ezio Melotti35386712009-12-31 13:22:41 +0000942 # it opens if there's an error in the file. If it doesn't, the
943 # traceback holds a reference to the ZipFile object and, indirectly,
944 # the file object.
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000945 # On Windows, this causes the os.unlink() call to fail because the
946 # underlying file is still open. This is SF bug #412214.
947 #
Ezio Melotti35386712009-12-31 13:22:41 +0000948 with open(TESTFN, "w") as fp:
949 fp.write("this is not a legal zip file\n")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000950 try:
951 zf = zipfile.ZipFile(TESTFN)
Georg Brandl4d540882010-10-28 06:42:33 +0000952 except zipfile.BadZipFile:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000953 pass
954
Ezio Melottiafd0d112009-07-15 17:17:17 +0000955 def test_is_zip_erroneous_file(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000956 """Check that is_zipfile() correctly identifies non-zip files."""
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000957 # - passing a filename
958 with open(TESTFN, "w") as fp:
959 fp.write("this is not a legal zip file\n")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000960 chk = zipfile.is_zipfile(TESTFN)
Ezio Melotti35386712009-12-31 13:22:41 +0000961 self.assertFalse(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000962 # - passing a file object
963 with open(TESTFN, "rb") as fp:
964 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000965 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000966 # - passing a file-like object
967 fp = io.BytesIO()
968 fp.write(b"this is not a legal zip file\n")
969 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000970 self.assertTrue(not chk)
Ezio Melotti35386712009-12-31 13:22:41 +0000971 fp.seek(0, 0)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000972 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000973 self.assertTrue(not chk)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000974
Ezio Melottiafd0d112009-07-15 17:17:17 +0000975 def test_is_zip_valid_file(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000976 """Check that is_zipfile() correctly identifies zip files."""
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000977 # - passing a filename
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000978 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
979 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
980
Guido van Rossumd8faa362007-04-27 19:54:29 +0000981 chk = zipfile.is_zipfile(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000982 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000983 # - passing a file object
984 with open(TESTFN, "rb") as fp:
985 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000986 self.assertTrue(chk)
Ezio Melotti35386712009-12-31 13:22:41 +0000987 fp.seek(0, 0)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000988 zip_contents = fp.read()
989 # - passing a file-like object
990 fp = io.BytesIO()
991 fp.write(zip_contents)
992 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000993 self.assertTrue(chk)
Ezio Melotti35386712009-12-31 13:22:41 +0000994 fp.seek(0, 0)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000995 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000996 self.assertTrue(chk)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000997
Ezio Melottiafd0d112009-07-15 17:17:17 +0000998 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000999 # make sure we don't raise an AttributeError when a partially-constructed
1000 # ZipFile instance is finalized; this tests for regression on SF tracker
1001 # bug #403871.
1002
1003 # The bug we're testing for caused an AttributeError to be raised
1004 # when a ZipFile instance was created for a file that did not
1005 # exist; the .fp member was not initialized but was needed by the
1006 # __del__() method. Since the AttributeError is in the __del__(),
1007 # it is ignored, but the user should be sufficiently annoyed by
1008 # the message on the output that regression will be noticed
1009 # quickly.
1010 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
1011
Amaury Forgeot d'Arcbc347802009-07-28 22:18:57 +00001012 def test_empty_file_raises_BadZipFile(self):
1013 f = open(TESTFN, 'w')
1014 f.close()
Georg Brandl4d540882010-10-28 06:42:33 +00001015 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
Amaury Forgeot d'Arcbc347802009-07-28 22:18:57 +00001016
Ezio Melotti35386712009-12-31 13:22:41 +00001017 with open(TESTFN, 'w') as fp:
1018 fp.write("short file")
Georg Brandl4d540882010-10-28 06:42:33 +00001019 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
Amaury Forgeot d'Arcbc347802009-07-28 22:18:57 +00001020
Ezio Melottiafd0d112009-07-15 17:17:17 +00001021 def test_closed_zip_raises_RuntimeError(self):
Ezio Melotti35386712009-12-31 13:22:41 +00001022 """Verify that testzip() doesn't swallow inappropriate exceptions."""
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001023 data = io.BytesIO()
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001024 with zipfile.ZipFile(data, mode="w") as zipf:
1025 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001026
1027 # This is correct; calling .read on a closed ZipFile should throw
1028 # a RuntimeError, and so should calling .testzip. An earlier
1029 # version of .testzip would swallow this exception (and any other)
1030 # and report that the first file in the archive was corrupt.
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001031 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
1032 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001033 self.assertRaises(RuntimeError, zipf.testzip)
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001034 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Brian Curtin8fb9b862010-11-18 02:15:28 +00001035 with open(TESTFN, 'w') as f:
1036 f.write('zipfile test data')
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001037 self.assertRaises(RuntimeError, zipf.write, TESTFN)
1038
Ezio Melottiafd0d112009-07-15 17:17:17 +00001039 def test_bad_constructor_mode(self):
Ezio Melotti35386712009-12-31 13:22:41 +00001040 """Check that bad modes passed to ZipFile constructor are caught."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001041 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
1042
Ezio Melottiafd0d112009-07-15 17:17:17 +00001043 def test_bad_open_mode(self):
Ezio Melotti35386712009-12-31 13:22:41 +00001044 """Check that bad modes passed to ZipFile.open are caught."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001045 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1046 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1047
1048 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001049 # read the data to make sure the file is there
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001050 zipf.read("foo.txt")
1051 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001052
Ezio Melottiafd0d112009-07-15 17:17:17 +00001053 def test_read0(self):
Ezio Melotti35386712009-12-31 13:22:41 +00001054 """Check that calling read(0) on a ZipExtFile object returns an empty
1055 string and doesn't advance file pointer."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001056 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1057 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1058 # read the data to make sure the file is there
Brian Curtin8fb9b862010-11-18 02:15:28 +00001059 with zipf.open("foo.txt") as f:
1060 for i in range(FIXEDTEST_SIZE):
1061 self.assertEqual(f.read(0), b'')
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001062
Brian Curtin8fb9b862010-11-18 02:15:28 +00001063 self.assertEqual(f.read(), b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001064
Ezio Melottiafd0d112009-07-15 17:17:17 +00001065 def test_open_non_existent_item(self):
Ezio Melotti35386712009-12-31 13:22:41 +00001066 """Check that attempting to call open() for an item that doesn't
1067 exist in the archive raises a RuntimeError."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001068 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1069 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001070
Ezio Melottiafd0d112009-07-15 17:17:17 +00001071 def test_bad_compression_mode(self):
Ezio Melotti35386712009-12-31 13:22:41 +00001072 """Check that bad compression methods passed to ZipFile.open are
1073 caught."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001074 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
1075
Martin v. Löwisb3260f02012-05-01 08:38:01 +02001076 def test_unsupported_compression(self):
1077 # data is declared as shrunk, but actually deflated
1078 data = (b'PK\x03\x04.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00'
1079 b'\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00x\x03\x00PK\x01'
1080 b'\x02.\x03.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00\x00\x02\x00\x00'
1081 b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
1082 b'\x80\x01\x00\x00\x00\x00xPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00'
1083 b'/\x00\x00\x00!\x00\x00\x00\x00\x00')
1084 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
1085 self.assertRaises(NotImplementedError, zipf.open, 'x')
1086
Ezio Melottiafd0d112009-07-15 17:17:17 +00001087 def test_null_byte_in_filename(self):
Ezio Melotti35386712009-12-31 13:22:41 +00001088 """Check that a filename containing a null byte is properly
1089 terminated."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001090 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1091 zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
1092 self.assertEqual(zipf.namelist(), ['foo.txt'])
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001093
Ezio Melottiafd0d112009-07-15 17:17:17 +00001094 def test_struct_sizes(self):
Ezio Melotti35386712009-12-31 13:22:41 +00001095 """Check that ZIP internal structure sizes are calculated correctly."""
Martin v. Löwisb09b8442008-07-03 14:13:42 +00001096 self.assertEqual(zipfile.sizeEndCentDir, 22)
1097 self.assertEqual(zipfile.sizeCentralDir, 46)
1098 self.assertEqual(zipfile.sizeEndCentDir64, 56)
1099 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
1100
Ezio Melottiafd0d112009-07-15 17:17:17 +00001101 def test_comments(self):
Ezio Melotti35386712009-12-31 13:22:41 +00001102 """Check that comments on the archive are handled properly."""
Martin v. Löwisb09b8442008-07-03 14:13:42 +00001103
1104 # check default comment is empty
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001105 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1106 self.assertEqual(zipf.comment, b'')
1107 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1108
1109 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
1110 self.assertEqual(zipfr.comment, b'')
Martin v. Löwisb09b8442008-07-03 14:13:42 +00001111
1112 # check a simple short comment
1113 comment = b'Bravely taking to his feet, he beat a very brave retreat.'
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001114 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1115 zipf.comment = comment
1116 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1117 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
1118 self.assertEqual(zipf.comment, comment)
Martin v. Löwisb09b8442008-07-03 14:13:42 +00001119
1120 # check a comment of max length
1121 comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)])
1122 comment2 = comment2.encode("ascii")
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001123 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1124 zipf.comment = comment2
1125 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1126
1127 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
1128 self.assertEqual(zipfr.comment, comment2)
Martin v. Löwisb09b8442008-07-03 14:13:42 +00001129
1130 # check a comment that is too long is truncated
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001131 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1132 zipf.comment = comment2 + b'oops'
1133 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1134 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
1135 self.assertEqual(zipfr.comment, comment2)
Martin v. Löwisb09b8442008-07-03 14:13:42 +00001136
Antoine Pitrouc3991852012-06-30 17:31:37 +02001137 # check that comments are correctly modified in append mode
1138 with zipfile.ZipFile(TESTFN,mode="w") as zipf:
1139 zipf.comment = b"original comment"
1140 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1141 with zipfile.ZipFile(TESTFN,mode="a") as zipf:
1142 zipf.comment = b"an updated comment"
1143 with zipfile.ZipFile(TESTFN,mode="r") as zipf:
1144 self.assertEqual(zipf.comment, b"an updated comment")
1145
1146 # check that comments are correctly shortened in append mode
1147 with zipfile.ZipFile(TESTFN,mode="w") as zipf:
1148 zipf.comment = b"original comment that's longer"
1149 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1150 with zipfile.ZipFile(TESTFN,mode="a") as zipf:
1151 zipf.comment = b"shorter comment"
1152 with zipfile.ZipFile(TESTFN,mode="r") as zipf:
1153 self.assertEqual(zipf.comment, b"shorter comment")
1154
R David Murrayf50b38a2012-04-12 18:44:58 -04001155 def test_unicode_comment(self):
1156 with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
1157 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1158 with self.assertRaises(TypeError):
1159 zipf.comment = "this is an error"
1160
1161 def test_change_comment_in_empty_archive(self):
1162 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
1163 self.assertFalse(zipf.filelist)
1164 zipf.comment = b"this is a comment"
1165 with zipfile.ZipFile(TESTFN, "r") as zipf:
1166 self.assertEqual(zipf.comment, b"this is a comment")
1167
1168 def test_change_comment_in_nonempty_archive(self):
1169 with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
1170 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1171 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
1172 self.assertTrue(zipf.filelist)
1173 zipf.comment = b"this is a comment"
1174 with zipfile.ZipFile(TESTFN, "r") as zipf:
1175 self.assertEqual(zipf.comment, b"this is a comment")
1176
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001177 def check_testzip_with_bad_crc(self, compression):
1178 """Tests that files with bad CRCs return their name from testzip."""
1179 zipdata = self.zips_with_bad_crc[compression]
1180
1181 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
1182 # testzip returns the name of the first corrupt file, or None
1183 self.assertEqual('afile', zipf.testzip())
1184
1185 def test_testzip_with_bad_crc_stored(self):
1186 self.check_testzip_with_bad_crc(zipfile.ZIP_STORED)
1187
Ezio Melotti975077a2011-05-19 22:03:22 +03001188 @requires_zlib
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001189 def test_testzip_with_bad_crc_deflated(self):
1190 self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED)
1191
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001192 @requires_bz2
1193 def test_testzip_with_bad_crc_bzip2(self):
1194 self.check_testzip_with_bad_crc(zipfile.ZIP_BZIP2)
1195
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001196 @requires_lzma
1197 def test_testzip_with_bad_crc_lzma(self):
1198 self.check_testzip_with_bad_crc(zipfile.ZIP_LZMA)
1199
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001200 def check_read_with_bad_crc(self, compression):
Georg Brandl4d540882010-10-28 06:42:33 +00001201 """Tests that files with bad CRCs raise a BadZipFile exception when read."""
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001202 zipdata = self.zips_with_bad_crc[compression]
1203
1204 # Using ZipFile.read()
1205 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
Georg Brandl4d540882010-10-28 06:42:33 +00001206 self.assertRaises(zipfile.BadZipFile, zipf.read, 'afile')
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001207
1208 # Using ZipExtFile.read()
1209 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
1210 with zipf.open('afile', 'r') as corrupt_file:
Georg Brandl4d540882010-10-28 06:42:33 +00001211 self.assertRaises(zipfile.BadZipFile, corrupt_file.read)
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001212
1213 # Same with small reads (in order to exercise the buffering logic)
1214 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
1215 with zipf.open('afile', 'r') as corrupt_file:
1216 corrupt_file.MIN_READ_SIZE = 2
Georg Brandl4d540882010-10-28 06:42:33 +00001217 with self.assertRaises(zipfile.BadZipFile):
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001218 while corrupt_file.read(2):
1219 pass
1220
1221 def test_read_with_bad_crc_stored(self):
1222 self.check_read_with_bad_crc(zipfile.ZIP_STORED)
1223
Ezio Melotti975077a2011-05-19 22:03:22 +03001224 @requires_zlib
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001225 def test_read_with_bad_crc_deflated(self):
1226 self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED)
1227
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001228 @requires_bz2
1229 def test_read_with_bad_crc_bzip2(self):
1230 self.check_read_with_bad_crc(zipfile.ZIP_BZIP2)
1231
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001232 @requires_lzma
1233 def test_read_with_bad_crc_lzma(self):
1234 self.check_read_with_bad_crc(zipfile.ZIP_LZMA)
1235
Antoine Pitrou6464d5f2010-09-12 14:51:20 +00001236 def check_read_return_size(self, compression):
1237 # Issue #9837: ZipExtFile.read() shouldn't return more bytes
1238 # than requested.
1239 for test_size in (1, 4095, 4096, 4097, 16384):
1240 file_size = test_size + 1
1241 junk = b''.join(struct.pack('B', randint(0, 255))
1242 for x in range(file_size))
1243 with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf:
1244 zipf.writestr('foo', junk)
1245 with zipf.open('foo', 'r') as fp:
1246 buf = fp.read(test_size)
1247 self.assertEqual(len(buf), test_size)
1248
1249 def test_read_return_size_stored(self):
1250 self.check_read_return_size(zipfile.ZIP_STORED)
1251
Ezio Melotti975077a2011-05-19 22:03:22 +03001252 @requires_zlib
Antoine Pitrou6464d5f2010-09-12 14:51:20 +00001253 def test_read_return_size_deflated(self):
1254 self.check_read_return_size(zipfile.ZIP_DEFLATED)
1255
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001256 @requires_bz2
1257 def test_read_return_size_bzip2(self):
1258 self.check_read_return_size(zipfile.ZIP_BZIP2)
1259
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001260 @requires_lzma
1261 def test_read_return_size_lzma(self):
1262 self.check_read_return_size(zipfile.ZIP_LZMA)
1263
Georg Brandl268e4d42010-10-14 06:59:45 +00001264 def test_empty_zipfile(self):
1265 # Check that creating a file in 'w' or 'a' mode and closing without
1266 # adding any files to the archives creates a valid empty ZIP file
1267 zipf = zipfile.ZipFile(TESTFN, mode="w")
1268 zipf.close()
1269 try:
1270 zipf = zipfile.ZipFile(TESTFN, mode="r")
1271 except zipfile.BadZipFile:
1272 self.fail("Unable to create empty ZIP file in 'w' mode")
1273
1274 zipf = zipfile.ZipFile(TESTFN, mode="a")
1275 zipf.close()
1276 try:
1277 zipf = zipfile.ZipFile(TESTFN, mode="r")
1278 except:
1279 self.fail("Unable to create empty ZIP file in 'a' mode")
1280
1281 def test_open_empty_file(self):
1282 # Issue 1710703: Check that opening a file with less than 22 bytes
Georg Brandl4d540882010-10-28 06:42:33 +00001283 # raises a BadZipFile exception (rather than the previously unhelpful
Georg Brandl268e4d42010-10-14 06:59:45 +00001284 # IOError)
1285 f = open(TESTFN, 'w')
1286 f.close()
Georg Brandl4d540882010-10-28 06:42:33 +00001287 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r')
Georg Brandl268e4d42010-10-14 06:59:45 +00001288
Senthil Kumaran29fa9d42011-10-20 01:46:00 +08001289 def test_create_zipinfo_before_1980(self):
1290 self.assertRaises(ValueError,
1291 zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0))
1292
Guido van Rossumd8faa362007-04-27 19:54:29 +00001293 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001294 unlink(TESTFN)
1295 unlink(TESTFN2)
1296
Thomas Wouterscf297e42007-02-23 15:07:44 +00001297
1298class DecryptionTests(unittest.TestCase):
Ezio Melotti35386712009-12-31 13:22:41 +00001299 """Check that ZIP decryption works. Since the library does not
1300 support encryption at the moment, we use a pre-generated encrypted
1301 ZIP file."""
Thomas Wouterscf297e42007-02-23 15:07:44 +00001302
1303 data = (
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001304 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
1305 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
1306 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
1307 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
1308 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
1309 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
1310 b'\x00\x00L\x00\x00\x00\x00\x00' )
Christian Heimesfdab48e2008-01-20 09:06:41 +00001311 data2 = (
1312 b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
1313 b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
1314 b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
1315 b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
1316 b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
1317 b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
1318 b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
1319 b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Thomas Wouterscf297e42007-02-23 15:07:44 +00001320
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001321 plain = b'zipfile.py encryption test'
Christian Heimesfdab48e2008-01-20 09:06:41 +00001322 plain2 = b'\x00'*512
Thomas Wouterscf297e42007-02-23 15:07:44 +00001323
1324 def setUp(self):
Ezio Melotti35386712009-12-31 13:22:41 +00001325 with open(TESTFN, "wb") as fp:
1326 fp.write(self.data)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001327 self.zip = zipfile.ZipFile(TESTFN, "r")
Ezio Melotti35386712009-12-31 13:22:41 +00001328 with open(TESTFN2, "wb") as fp:
1329 fp.write(self.data2)
Christian Heimesfdab48e2008-01-20 09:06:41 +00001330 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001331
1332 def tearDown(self):
1333 self.zip.close()
1334 os.unlink(TESTFN)
Christian Heimesfdab48e2008-01-20 09:06:41 +00001335 self.zip2.close()
1336 os.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001337
Ezio Melottiafd0d112009-07-15 17:17:17 +00001338 def test_no_password(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +00001339 # Reading the encrypted file without password
1340 # must generate a RunTime exception
1341 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +00001342 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001343
Ezio Melottiafd0d112009-07-15 17:17:17 +00001344 def test_bad_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001345 self.zip.setpassword(b"perl")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001346 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +00001347 self.zip2.setpassword(b"perl")
1348 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001349
Ezio Melotti975077a2011-05-19 22:03:22 +03001350 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +00001351 def test_good_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001352 self.zip.setpassword(b"python")
Ezio Melotti35386712009-12-31 13:22:41 +00001353 self.assertEqual(self.zip.read("test.txt"), self.plain)
Christian Heimesfdab48e2008-01-20 09:06:41 +00001354 self.zip2.setpassword(b"12345")
Ezio Melotti35386712009-12-31 13:22:41 +00001355 self.assertEqual(self.zip2.read("zero"), self.plain2)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001356
R. David Murray8d855d82010-12-21 21:53:37 +00001357 def test_unicode_password(self):
1358 self.assertRaises(TypeError, self.zip.setpassword, "unicode")
1359 self.assertRaises(TypeError, self.zip.read, "test.txt", "python")
1360 self.assertRaises(TypeError, self.zip.open, "test.txt", pwd="python")
1361 self.assertRaises(TypeError, self.zip.extract, "test.txt", pwd="python")
1362
Guido van Rossumd8faa362007-04-27 19:54:29 +00001363
1364class TestsWithRandomBinaryFiles(unittest.TestCase):
1365 def setUp(self):
1366 datacount = randint(16, 64)*1024 + randint(1, 1024)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001367 self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
1368 for i in range(datacount))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001369
1370 # Make a source file with some lines
Ezio Melotti35386712009-12-31 13:22:41 +00001371 with open(TESTFN, "wb") as fp:
1372 fp.write(self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001373
1374 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001375 unlink(TESTFN)
1376 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001377
Ezio Melottiafd0d112009-07-15 17:17:17 +00001378 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001379 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001380 with zipfile.ZipFile(f, "w", compression) as zipfp:
1381 zipfp.write(TESTFN, "another.name")
1382 zipfp.write(TESTFN, TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001383
Ezio Melottiafd0d112009-07-15 17:17:17 +00001384 def zip_test(self, f, compression):
1385 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001386
1387 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001388 with zipfile.ZipFile(f, "r", compression) as zipfp:
1389 testdata = zipfp.read(TESTFN)
1390 self.assertEqual(len(testdata), len(self.data))
1391 self.assertEqual(testdata, self.data)
1392 self.assertEqual(zipfp.read("another.name"), self.data)
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001393 if not isinstance(f, str):
1394 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001395
Ezio Melottiafd0d112009-07-15 17:17:17 +00001396 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001397 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001398 self.zip_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001399
Ezio Melotti975077a2011-05-19 22:03:22 +03001400 @requires_zlib
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001401 def test_deflated(self):
1402 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1403 self.zip_test(f, zipfile.ZIP_DEFLATED)
1404
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001405 @requires_bz2
1406 def test_bzip2(self):
1407 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1408 self.zip_test(f, zipfile.ZIP_BZIP2)
1409
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001410 @requires_lzma
1411 def test_lzma(self):
1412 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1413 self.zip_test(f, zipfile.ZIP_LZMA)
1414
Ezio Melottiafd0d112009-07-15 17:17:17 +00001415 def zip_open_test(self, f, compression):
1416 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001417
1418 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001419 with zipfile.ZipFile(f, "r", compression) as zipfp:
1420 zipdata1 = []
Brian Curtin8fb9b862010-11-18 02:15:28 +00001421 with zipfp.open(TESTFN) as zipopen1:
1422 while True:
1423 read_data = zipopen1.read(256)
1424 if not read_data:
1425 break
1426 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001427
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001428 zipdata2 = []
Brian Curtin8fb9b862010-11-18 02:15:28 +00001429 with zipfp.open("another.name") as zipopen2:
1430 while True:
1431 read_data = zipopen2.read(256)
1432 if not read_data:
1433 break
1434 zipdata2.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001435
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001436 testdata1 = b''.join(zipdata1)
1437 self.assertEqual(len(testdata1), len(self.data))
1438 self.assertEqual(testdata1, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001439
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001440 testdata2 = b''.join(zipdata2)
Ezio Melotti35386712009-12-31 13:22:41 +00001441 self.assertEqual(len(testdata2), len(self.data))
1442 self.assertEqual(testdata2, self.data)
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001443 if not isinstance(f, str):
1444 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001445
Ezio Melottiafd0d112009-07-15 17:17:17 +00001446 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001447 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001448 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001449
Ezio Melotti975077a2011-05-19 22:03:22 +03001450 @requires_zlib
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001451 def test_open_deflated(self):
1452 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1453 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
1454
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001455 @requires_bz2
1456 def test_open_bzip2(self):
1457 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1458 self.zip_open_test(f, zipfile.ZIP_BZIP2)
1459
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001460 @requires_lzma
1461 def test_open_lzma(self):
1462 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1463 self.zip_open_test(f, zipfile.ZIP_LZMA)
1464
Ezio Melottiafd0d112009-07-15 17:17:17 +00001465 def zip_random_open_test(self, f, compression):
1466 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001467
1468 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001469 with zipfile.ZipFile(f, "r", compression) as zipfp:
1470 zipdata1 = []
Brian Curtin8fb9b862010-11-18 02:15:28 +00001471 with zipfp.open(TESTFN) as zipopen1:
1472 while True:
1473 read_data = zipopen1.read(randint(1, 1024))
1474 if not read_data:
1475 break
1476 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001477
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001478 testdata = b''.join(zipdata1)
1479 self.assertEqual(len(testdata), len(self.data))
1480 self.assertEqual(testdata, self.data)
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001481 if not isinstance(f, str):
1482 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001483
Ezio Melottiafd0d112009-07-15 17:17:17 +00001484 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001485 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001486 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001487
Ezio Melotti975077a2011-05-19 22:03:22 +03001488 @requires_zlib
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001489 def test_random_open_deflated(self):
1490 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1491 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
1492
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001493 @requires_bz2
1494 def test_random_open_bzip2(self):
1495 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1496 self.zip_random_open_test(f, zipfile.ZIP_BZIP2)
1497
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001498 @requires_lzma
1499 def test_random_open_lzma(self):
1500 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1501 self.zip_random_open_test(f, zipfile.ZIP_LZMA)
1502
Ezio Melotti76430242009-07-11 18:28:48 +00001503
Ezio Melotti975077a2011-05-19 22:03:22 +03001504@requires_zlib
Guido van Rossumd8faa362007-04-27 19:54:29 +00001505class TestsWithMultipleOpens(unittest.TestCase):
1506 def setUp(self):
1507 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001508 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
1509 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
1510 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001511
Ezio Melottiafd0d112009-07-15 17:17:17 +00001512 def test_same_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001513 # Verify that (when the ZipFile is in control of creating file objects)
1514 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001515 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
Brian Curtin8fb9b862010-11-18 02:15:28 +00001516 with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2:
1517 data1 = zopen1.read(500)
1518 data2 = zopen2.read(500)
1519 data1 += zopen1.read(500)
1520 data2 += zopen2.read(500)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001521 self.assertEqual(data1, data2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001522
Ezio Melottiafd0d112009-07-15 17:17:17 +00001523 def test_different_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001524 # Verify that (when the ZipFile is in control of creating file objects)
1525 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001526 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
Brian Curtin8fb9b862010-11-18 02:15:28 +00001527 with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
1528 data1 = zopen1.read(500)
1529 data2 = zopen2.read(500)
1530 data1 += zopen1.read(500)
1531 data2 += zopen2.read(500)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001532 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1533 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001534
Ezio Melottiafd0d112009-07-15 17:17:17 +00001535 def test_interleaved(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001536 # Verify that (when the ZipFile is in control of creating file objects)
1537 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001538 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
Brian Curtin8fb9b862010-11-18 02:15:28 +00001539 with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
1540 data1 = zopen1.read(500)
1541 data2 = zopen2.read(500)
1542 data1 += zopen1.read(500)
1543 data2 += zopen2.read(500)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001544 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1545 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001546
1547 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001548 unlink(TESTFN2)
1549
Guido van Rossumd8faa362007-04-27 19:54:29 +00001550
Martin v. Löwis59e47792009-01-24 14:10:07 +00001551class TestWithDirectory(unittest.TestCase):
1552 def setUp(self):
1553 os.mkdir(TESTFN2)
1554
Ezio Melottiafd0d112009-07-15 17:17:17 +00001555 def test_extract_dir(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001556 with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
1557 zipf.extractall(TESTFN2)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001558 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1559 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1560 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1561
Ezio Melottiafd0d112009-07-15 17:17:17 +00001562 def test_bug_6050(self):
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001563 # Extraction should succeed if directories already exist
1564 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottiafd0d112009-07-15 17:17:17 +00001565 self.test_extract_dir()
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001566
Ezio Melottiafd0d112009-07-15 17:17:17 +00001567 def test_store_dir(self):
Martin v. Löwis59e47792009-01-24 14:10:07 +00001568 os.mkdir(os.path.join(TESTFN2, "x"))
1569 zipf = zipfile.ZipFile(TESTFN, "w")
1570 zipf.write(os.path.join(TESTFN2, "x"), "x")
1571 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1572
1573 def tearDown(self):
1574 shutil.rmtree(TESTFN2)
1575 if os.path.exists(TESTFN):
Ezio Melotti76430242009-07-11 18:28:48 +00001576 unlink(TESTFN)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001577
Guido van Rossumd8faa362007-04-27 19:54:29 +00001578
1579class UniversalNewlineTests(unittest.TestCase):
1580 def setUp(self):
Guido van Rossum9c627722007-08-27 18:31:48 +00001581 self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001582 for i in range(FIXEDTEST_SIZE)]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001583 self.seps = ('\r', '\r\n', '\n')
1584 self.arcdata, self.arcfiles = {}, {}
1585 for n, s in enumerate(self.seps):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001586 b = s.encode("ascii")
1587 self.arcdata[s] = b.join(self.line_gen) + b
Guido van Rossumd8faa362007-04-27 19:54:29 +00001588 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001589 f = open(self.arcfiles[s], "wb")
1590 try:
1591 f.write(self.arcdata[s])
1592 finally:
1593 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001594
Ezio Melottiafd0d112009-07-15 17:17:17 +00001595 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001596 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001597 with zipfile.ZipFile(f, "w", compression) as zipfp:
1598 for fn in self.arcfiles.values():
1599 zipfp.write(fn, fn)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001600
Ezio Melottiafd0d112009-07-15 17:17:17 +00001601 def read_test(self, f, compression):
1602 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001603
1604 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001605 with zipfile.ZipFile(f, "r") as zipfp:
1606 for sep, fn in self.arcfiles.items():
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001607 with zipfp.open(fn, "rU") as fp:
1608 zipdata = fp.read()
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001609 self.assertEqual(self.arcdata[sep], zipdata)
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001610 if not isinstance(f, str):
1611 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001612
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001613 def readline_read_test(self, f, compression):
1614 self.make_test_archive(f, compression)
1615
1616 # Read the ZIP archive
Brian Curtin8fb9b862010-11-18 02:15:28 +00001617 with zipfile.ZipFile(f, "r") as zipfp:
1618 for sep, fn in self.arcfiles.items():
1619 with zipfp.open(fn, "rU") as zipopen:
1620 data = b''
1621 while True:
1622 read = zipopen.readline()
1623 if not read:
1624 break
1625 data += read
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001626
Brian Curtin8fb9b862010-11-18 02:15:28 +00001627 read = zipopen.read(5)
1628 if not read:
1629 break
1630 data += read
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001631
1632 self.assertEqual(data, self.arcdata['\n'])
1633
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001634 if not isinstance(f, str):
1635 f.close()
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001636
Ezio Melottiafd0d112009-07-15 17:17:17 +00001637 def readline_test(self, f, compression):
1638 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001639
1640 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001641 with zipfile.ZipFile(f, "r") as zipfp:
1642 for sep, fn in self.arcfiles.items():
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001643 with zipfp.open(fn, "rU") as zipopen:
1644 for line in self.line_gen:
1645 linedata = zipopen.readline()
1646 self.assertEqual(linedata, line + b'\n')
1647 if not isinstance(f, str):
1648 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001649
Ezio Melottiafd0d112009-07-15 17:17:17 +00001650 def readlines_test(self, f, compression):
1651 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001652
1653 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001654 with zipfile.ZipFile(f, "r") as zipfp:
1655 for sep, fn in self.arcfiles.items():
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001656 with zipfp.open(fn, "rU") as fp:
1657 ziplines = fp.readlines()
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001658 for line, zipline in zip(self.line_gen, ziplines):
1659 self.assertEqual(zipline, line + b'\n')
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001660 if not isinstance(f, str):
1661 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001662
Ezio Melottiafd0d112009-07-15 17:17:17 +00001663 def iterlines_test(self, f, compression):
1664 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001665
1666 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001667 with zipfile.ZipFile(f, "r") as zipfp:
1668 for sep, fn in self.arcfiles.items():
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001669 with zipfp.open(fn, "rU") as fp:
1670 for line, zipline in zip(self.line_gen, fp):
1671 self.assertEqual(zipline, line + b'\n')
1672 if not isinstance(f, str):
1673 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001674
Ezio Melottiafd0d112009-07-15 17:17:17 +00001675 def test_read_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001676 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001677 self.read_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001678
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001679 def test_readline_read_stored(self):
1680 # Issue #7610: calls to readline() interleaved with calls to read().
1681 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1682 self.readline_read_test(f, zipfile.ZIP_STORED)
1683
Ezio Melottiafd0d112009-07-15 17:17:17 +00001684 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001685 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001686 self.readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001687
Ezio Melottiafd0d112009-07-15 17:17:17 +00001688 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001689 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001690 self.readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001691
Ezio Melottiafd0d112009-07-15 17:17:17 +00001692 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001693 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001694 self.iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001695
Ezio Melotti975077a2011-05-19 22:03:22 +03001696 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +00001697 def test_read_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001698 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001699 self.read_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001700
Ezio Melotti975077a2011-05-19 22:03:22 +03001701 @requires_zlib
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001702 def test_readline_read_deflated(self):
1703 # Issue #7610: calls to readline() interleaved with calls to read().
1704 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1705 self.readline_read_test(f, zipfile.ZIP_DEFLATED)
1706
Ezio Melotti975077a2011-05-19 22:03:22 +03001707 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +00001708 def test_readline_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001709 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001710 self.readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001711
Ezio Melotti975077a2011-05-19 22:03:22 +03001712 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +00001713 def test_readlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001714 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001715 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001716
Ezio Melotti975077a2011-05-19 22:03:22 +03001717 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +00001718 def test_iterlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001719 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001720 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001721
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001722 @requires_bz2
1723 def test_read_bzip2(self):
1724 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1725 self.read_test(f, zipfile.ZIP_BZIP2)
1726
1727 @requires_bz2
1728 def test_readline_read_bzip2(self):
1729 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1730 self.readline_read_test(f, zipfile.ZIP_BZIP2)
1731
1732 @requires_bz2
1733 def test_readline_bzip2(self):
1734 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1735 self.readline_test(f, zipfile.ZIP_BZIP2)
1736
1737 @requires_bz2
1738 def test_readlines_bzip2(self):
1739 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1740 self.readlines_test(f, zipfile.ZIP_BZIP2)
1741
1742 @requires_bz2
1743 def test_iterlines_bzip2(self):
1744 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1745 self.iterlines_test(f, zipfile.ZIP_BZIP2)
1746
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001747 @requires_lzma
1748 def test_read_lzma(self):
1749 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1750 self.read_test(f, zipfile.ZIP_LZMA)
1751
1752 @requires_lzma
1753 def test_readline_read_lzma(self):
1754 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1755 self.readline_read_test(f, zipfile.ZIP_LZMA)
1756
1757 @requires_lzma
1758 def test_readline_lzma(self):
1759 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1760 self.readline_test(f, zipfile.ZIP_LZMA)
1761
1762 @requires_lzma
1763 def test_readlines_lzma(self):
1764 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1765 self.readlines_test(f, zipfile.ZIP_LZMA)
1766
1767 @requires_lzma
1768 def test_iterlines_lzma(self):
1769 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1770 self.iterlines_test(f, zipfile.ZIP_LZMA)
1771
Guido van Rossumd8faa362007-04-27 19:54:29 +00001772 def tearDown(self):
1773 for sep, fn in self.arcfiles.items():
1774 os.remove(fn)
Ezio Melotti76430242009-07-11 18:28:48 +00001775 unlink(TESTFN)
1776 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001777
1778
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001779def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +00001780 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1781 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti76430242009-07-11 18:28:48 +00001782 TestWithDirectory, UniversalNewlineTests,
1783 TestsWithRandomBinaryFiles)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001784
1785if __name__ == "__main__":
1786 test_main()