blob: 07722360fd0356d07bd3781653e029de564ac8ae [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
R David Murrayf50b38a2012-04-12 18:44:58 -04001137 def test_unicode_comment(self):
1138 with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
1139 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1140 with self.assertRaises(TypeError):
1141 zipf.comment = "this is an error"
1142
1143 def test_change_comment_in_empty_archive(self):
1144 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
1145 self.assertFalse(zipf.filelist)
1146 zipf.comment = b"this is a comment"
1147 with zipfile.ZipFile(TESTFN, "r") as zipf:
1148 self.assertEqual(zipf.comment, b"this is a comment")
1149
1150 def test_change_comment_in_nonempty_archive(self):
1151 with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
1152 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1153 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
1154 self.assertTrue(zipf.filelist)
1155 zipf.comment = b"this is a comment"
1156 with zipfile.ZipFile(TESTFN, "r") as zipf:
1157 self.assertEqual(zipf.comment, b"this is a comment")
1158
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001159 def check_testzip_with_bad_crc(self, compression):
1160 """Tests that files with bad CRCs return their name from testzip."""
1161 zipdata = self.zips_with_bad_crc[compression]
1162
1163 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
1164 # testzip returns the name of the first corrupt file, or None
1165 self.assertEqual('afile', zipf.testzip())
1166
1167 def test_testzip_with_bad_crc_stored(self):
1168 self.check_testzip_with_bad_crc(zipfile.ZIP_STORED)
1169
Ezio Melotti975077a2011-05-19 22:03:22 +03001170 @requires_zlib
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001171 def test_testzip_with_bad_crc_deflated(self):
1172 self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED)
1173
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001174 @requires_bz2
1175 def test_testzip_with_bad_crc_bzip2(self):
1176 self.check_testzip_with_bad_crc(zipfile.ZIP_BZIP2)
1177
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001178 @requires_lzma
1179 def test_testzip_with_bad_crc_lzma(self):
1180 self.check_testzip_with_bad_crc(zipfile.ZIP_LZMA)
1181
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001182 def check_read_with_bad_crc(self, compression):
Georg Brandl4d540882010-10-28 06:42:33 +00001183 """Tests that files with bad CRCs raise a BadZipFile exception when read."""
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001184 zipdata = self.zips_with_bad_crc[compression]
1185
1186 # Using ZipFile.read()
1187 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
Georg Brandl4d540882010-10-28 06:42:33 +00001188 self.assertRaises(zipfile.BadZipFile, zipf.read, 'afile')
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001189
1190 # Using ZipExtFile.read()
1191 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
1192 with zipf.open('afile', 'r') as corrupt_file:
Georg Brandl4d540882010-10-28 06:42:33 +00001193 self.assertRaises(zipfile.BadZipFile, corrupt_file.read)
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001194
1195 # Same with small reads (in order to exercise the buffering logic)
1196 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
1197 with zipf.open('afile', 'r') as corrupt_file:
1198 corrupt_file.MIN_READ_SIZE = 2
Georg Brandl4d540882010-10-28 06:42:33 +00001199 with self.assertRaises(zipfile.BadZipFile):
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001200 while corrupt_file.read(2):
1201 pass
1202
1203 def test_read_with_bad_crc_stored(self):
1204 self.check_read_with_bad_crc(zipfile.ZIP_STORED)
1205
Ezio Melotti975077a2011-05-19 22:03:22 +03001206 @requires_zlib
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001207 def test_read_with_bad_crc_deflated(self):
1208 self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED)
1209
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001210 @requires_bz2
1211 def test_read_with_bad_crc_bzip2(self):
1212 self.check_read_with_bad_crc(zipfile.ZIP_BZIP2)
1213
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001214 @requires_lzma
1215 def test_read_with_bad_crc_lzma(self):
1216 self.check_read_with_bad_crc(zipfile.ZIP_LZMA)
1217
Antoine Pitrou6464d5f2010-09-12 14:51:20 +00001218 def check_read_return_size(self, compression):
1219 # Issue #9837: ZipExtFile.read() shouldn't return more bytes
1220 # than requested.
1221 for test_size in (1, 4095, 4096, 4097, 16384):
1222 file_size = test_size + 1
1223 junk = b''.join(struct.pack('B', randint(0, 255))
1224 for x in range(file_size))
1225 with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf:
1226 zipf.writestr('foo', junk)
1227 with zipf.open('foo', 'r') as fp:
1228 buf = fp.read(test_size)
1229 self.assertEqual(len(buf), test_size)
1230
1231 def test_read_return_size_stored(self):
1232 self.check_read_return_size(zipfile.ZIP_STORED)
1233
Ezio Melotti975077a2011-05-19 22:03:22 +03001234 @requires_zlib
Antoine Pitrou6464d5f2010-09-12 14:51:20 +00001235 def test_read_return_size_deflated(self):
1236 self.check_read_return_size(zipfile.ZIP_DEFLATED)
1237
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001238 @requires_bz2
1239 def test_read_return_size_bzip2(self):
1240 self.check_read_return_size(zipfile.ZIP_BZIP2)
1241
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001242 @requires_lzma
1243 def test_read_return_size_lzma(self):
1244 self.check_read_return_size(zipfile.ZIP_LZMA)
1245
Georg Brandl268e4d42010-10-14 06:59:45 +00001246 def test_empty_zipfile(self):
1247 # Check that creating a file in 'w' or 'a' mode and closing without
1248 # adding any files to the archives creates a valid empty ZIP file
1249 zipf = zipfile.ZipFile(TESTFN, mode="w")
1250 zipf.close()
1251 try:
1252 zipf = zipfile.ZipFile(TESTFN, mode="r")
1253 except zipfile.BadZipFile:
1254 self.fail("Unable to create empty ZIP file in 'w' mode")
1255
1256 zipf = zipfile.ZipFile(TESTFN, mode="a")
1257 zipf.close()
1258 try:
1259 zipf = zipfile.ZipFile(TESTFN, mode="r")
1260 except:
1261 self.fail("Unable to create empty ZIP file in 'a' mode")
1262
1263 def test_open_empty_file(self):
1264 # Issue 1710703: Check that opening a file with less than 22 bytes
Georg Brandl4d540882010-10-28 06:42:33 +00001265 # raises a BadZipFile exception (rather than the previously unhelpful
Georg Brandl268e4d42010-10-14 06:59:45 +00001266 # IOError)
1267 f = open(TESTFN, 'w')
1268 f.close()
Georg Brandl4d540882010-10-28 06:42:33 +00001269 self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r')
Georg Brandl268e4d42010-10-14 06:59:45 +00001270
Senthil Kumaran29fa9d42011-10-20 01:46:00 +08001271 def test_create_zipinfo_before_1980(self):
1272 self.assertRaises(ValueError,
1273 zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0))
1274
Guido van Rossumd8faa362007-04-27 19:54:29 +00001275 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001276 unlink(TESTFN)
1277 unlink(TESTFN2)
1278
Thomas Wouterscf297e42007-02-23 15:07:44 +00001279
1280class DecryptionTests(unittest.TestCase):
Ezio Melotti35386712009-12-31 13:22:41 +00001281 """Check that ZIP decryption works. Since the library does not
1282 support encryption at the moment, we use a pre-generated encrypted
1283 ZIP file."""
Thomas Wouterscf297e42007-02-23 15:07:44 +00001284
1285 data = (
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001286 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
1287 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
1288 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
1289 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
1290 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
1291 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
1292 b'\x00\x00L\x00\x00\x00\x00\x00' )
Christian Heimesfdab48e2008-01-20 09:06:41 +00001293 data2 = (
1294 b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
1295 b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
1296 b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
1297 b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
1298 b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
1299 b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
1300 b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
1301 b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Thomas Wouterscf297e42007-02-23 15:07:44 +00001302
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001303 plain = b'zipfile.py encryption test'
Christian Heimesfdab48e2008-01-20 09:06:41 +00001304 plain2 = b'\x00'*512
Thomas Wouterscf297e42007-02-23 15:07:44 +00001305
1306 def setUp(self):
Ezio Melotti35386712009-12-31 13:22:41 +00001307 with open(TESTFN, "wb") as fp:
1308 fp.write(self.data)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001309 self.zip = zipfile.ZipFile(TESTFN, "r")
Ezio Melotti35386712009-12-31 13:22:41 +00001310 with open(TESTFN2, "wb") as fp:
1311 fp.write(self.data2)
Christian Heimesfdab48e2008-01-20 09:06:41 +00001312 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001313
1314 def tearDown(self):
1315 self.zip.close()
1316 os.unlink(TESTFN)
Christian Heimesfdab48e2008-01-20 09:06:41 +00001317 self.zip2.close()
1318 os.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001319
Ezio Melottiafd0d112009-07-15 17:17:17 +00001320 def test_no_password(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +00001321 # Reading the encrypted file without password
1322 # must generate a RunTime exception
1323 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +00001324 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001325
Ezio Melottiafd0d112009-07-15 17:17:17 +00001326 def test_bad_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001327 self.zip.setpassword(b"perl")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001328 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +00001329 self.zip2.setpassword(b"perl")
1330 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001331
Ezio Melotti975077a2011-05-19 22:03:22 +03001332 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +00001333 def test_good_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001334 self.zip.setpassword(b"python")
Ezio Melotti35386712009-12-31 13:22:41 +00001335 self.assertEqual(self.zip.read("test.txt"), self.plain)
Christian Heimesfdab48e2008-01-20 09:06:41 +00001336 self.zip2.setpassword(b"12345")
Ezio Melotti35386712009-12-31 13:22:41 +00001337 self.assertEqual(self.zip2.read("zero"), self.plain2)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001338
R. David Murray8d855d82010-12-21 21:53:37 +00001339 def test_unicode_password(self):
1340 self.assertRaises(TypeError, self.zip.setpassword, "unicode")
1341 self.assertRaises(TypeError, self.zip.read, "test.txt", "python")
1342 self.assertRaises(TypeError, self.zip.open, "test.txt", pwd="python")
1343 self.assertRaises(TypeError, self.zip.extract, "test.txt", pwd="python")
1344
Guido van Rossumd8faa362007-04-27 19:54:29 +00001345
1346class TestsWithRandomBinaryFiles(unittest.TestCase):
1347 def setUp(self):
1348 datacount = randint(16, 64)*1024 + randint(1, 1024)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001349 self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
1350 for i in range(datacount))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001351
1352 # Make a source file with some lines
Ezio Melotti35386712009-12-31 13:22:41 +00001353 with open(TESTFN, "wb") as fp:
1354 fp.write(self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001355
1356 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001357 unlink(TESTFN)
1358 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001359
Ezio Melottiafd0d112009-07-15 17:17:17 +00001360 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001361 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001362 with zipfile.ZipFile(f, "w", compression) as zipfp:
1363 zipfp.write(TESTFN, "another.name")
1364 zipfp.write(TESTFN, TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001365
Ezio Melottiafd0d112009-07-15 17:17:17 +00001366 def zip_test(self, f, compression):
1367 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001368
1369 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001370 with zipfile.ZipFile(f, "r", compression) as zipfp:
1371 testdata = zipfp.read(TESTFN)
1372 self.assertEqual(len(testdata), len(self.data))
1373 self.assertEqual(testdata, self.data)
1374 self.assertEqual(zipfp.read("another.name"), self.data)
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001375 if not isinstance(f, str):
1376 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001377
Ezio Melottiafd0d112009-07-15 17:17:17 +00001378 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001379 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001380 self.zip_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001381
Ezio Melotti975077a2011-05-19 22:03:22 +03001382 @requires_zlib
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001383 def test_deflated(self):
1384 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1385 self.zip_test(f, zipfile.ZIP_DEFLATED)
1386
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001387 @requires_bz2
1388 def test_bzip2(self):
1389 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1390 self.zip_test(f, zipfile.ZIP_BZIP2)
1391
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001392 @requires_lzma
1393 def test_lzma(self):
1394 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1395 self.zip_test(f, zipfile.ZIP_LZMA)
1396
Ezio Melottiafd0d112009-07-15 17:17:17 +00001397 def zip_open_test(self, f, compression):
1398 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001399
1400 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001401 with zipfile.ZipFile(f, "r", compression) as zipfp:
1402 zipdata1 = []
Brian Curtin8fb9b862010-11-18 02:15:28 +00001403 with zipfp.open(TESTFN) as zipopen1:
1404 while True:
1405 read_data = zipopen1.read(256)
1406 if not read_data:
1407 break
1408 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001409
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001410 zipdata2 = []
Brian Curtin8fb9b862010-11-18 02:15:28 +00001411 with zipfp.open("another.name") as zipopen2:
1412 while True:
1413 read_data = zipopen2.read(256)
1414 if not read_data:
1415 break
1416 zipdata2.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001417
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001418 testdata1 = b''.join(zipdata1)
1419 self.assertEqual(len(testdata1), len(self.data))
1420 self.assertEqual(testdata1, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001421
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001422 testdata2 = b''.join(zipdata2)
Ezio Melotti35386712009-12-31 13:22:41 +00001423 self.assertEqual(len(testdata2), len(self.data))
1424 self.assertEqual(testdata2, self.data)
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001425 if not isinstance(f, str):
1426 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001427
Ezio Melottiafd0d112009-07-15 17:17:17 +00001428 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001429 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001430 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001431
Ezio Melotti975077a2011-05-19 22:03:22 +03001432 @requires_zlib
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001433 def test_open_deflated(self):
1434 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1435 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
1436
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001437 @requires_bz2
1438 def test_open_bzip2(self):
1439 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1440 self.zip_open_test(f, zipfile.ZIP_BZIP2)
1441
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001442 @requires_lzma
1443 def test_open_lzma(self):
1444 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1445 self.zip_open_test(f, zipfile.ZIP_LZMA)
1446
Ezio Melottiafd0d112009-07-15 17:17:17 +00001447 def zip_random_open_test(self, f, compression):
1448 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001449
1450 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001451 with zipfile.ZipFile(f, "r", compression) as zipfp:
1452 zipdata1 = []
Brian Curtin8fb9b862010-11-18 02:15:28 +00001453 with zipfp.open(TESTFN) as zipopen1:
1454 while True:
1455 read_data = zipopen1.read(randint(1, 1024))
1456 if not read_data:
1457 break
1458 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001459
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001460 testdata = b''.join(zipdata1)
1461 self.assertEqual(len(testdata), len(self.data))
1462 self.assertEqual(testdata, self.data)
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001463 if not isinstance(f, str):
1464 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001465
Ezio Melottiafd0d112009-07-15 17:17:17 +00001466 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001467 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001468 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001469
Ezio Melotti975077a2011-05-19 22:03:22 +03001470 @requires_zlib
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001471 def test_random_open_deflated(self):
1472 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1473 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
1474
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001475 @requires_bz2
1476 def test_random_open_bzip2(self):
1477 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1478 self.zip_random_open_test(f, zipfile.ZIP_BZIP2)
1479
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001480 @requires_lzma
1481 def test_random_open_lzma(self):
1482 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1483 self.zip_random_open_test(f, zipfile.ZIP_LZMA)
1484
Ezio Melotti76430242009-07-11 18:28:48 +00001485
Ezio Melotti975077a2011-05-19 22:03:22 +03001486@requires_zlib
Guido van Rossumd8faa362007-04-27 19:54:29 +00001487class TestsWithMultipleOpens(unittest.TestCase):
1488 def setUp(self):
1489 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001490 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
1491 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
1492 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001493
Ezio Melottiafd0d112009-07-15 17:17:17 +00001494 def test_same_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001495 # Verify that (when the ZipFile is in control of creating file objects)
1496 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001497 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
Brian Curtin8fb9b862010-11-18 02:15:28 +00001498 with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2:
1499 data1 = zopen1.read(500)
1500 data2 = zopen2.read(500)
1501 data1 += zopen1.read(500)
1502 data2 += zopen2.read(500)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001503 self.assertEqual(data1, data2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001504
Ezio Melottiafd0d112009-07-15 17:17:17 +00001505 def test_different_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001506 # Verify that (when the ZipFile is in control of creating file objects)
1507 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001508 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
Brian Curtin8fb9b862010-11-18 02:15:28 +00001509 with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
1510 data1 = zopen1.read(500)
1511 data2 = zopen2.read(500)
1512 data1 += zopen1.read(500)
1513 data2 += zopen2.read(500)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001514 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1515 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001516
Ezio Melottiafd0d112009-07-15 17:17:17 +00001517 def test_interleaved(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001518 # Verify that (when the ZipFile is in control of creating file objects)
1519 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001520 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
Brian Curtin8fb9b862010-11-18 02:15:28 +00001521 with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
1522 data1 = zopen1.read(500)
1523 data2 = zopen2.read(500)
1524 data1 += zopen1.read(500)
1525 data2 += zopen2.read(500)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001526 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1527 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001528
1529 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001530 unlink(TESTFN2)
1531
Guido van Rossumd8faa362007-04-27 19:54:29 +00001532
Martin v. Löwis59e47792009-01-24 14:10:07 +00001533class TestWithDirectory(unittest.TestCase):
1534 def setUp(self):
1535 os.mkdir(TESTFN2)
1536
Ezio Melottiafd0d112009-07-15 17:17:17 +00001537 def test_extract_dir(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001538 with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
1539 zipf.extractall(TESTFN2)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001540 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1541 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1542 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1543
Ezio Melottiafd0d112009-07-15 17:17:17 +00001544 def test_bug_6050(self):
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001545 # Extraction should succeed if directories already exist
1546 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottiafd0d112009-07-15 17:17:17 +00001547 self.test_extract_dir()
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001548
Ezio Melottiafd0d112009-07-15 17:17:17 +00001549 def test_store_dir(self):
Martin v. Löwis59e47792009-01-24 14:10:07 +00001550 os.mkdir(os.path.join(TESTFN2, "x"))
1551 zipf = zipfile.ZipFile(TESTFN, "w")
1552 zipf.write(os.path.join(TESTFN2, "x"), "x")
1553 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1554
1555 def tearDown(self):
1556 shutil.rmtree(TESTFN2)
1557 if os.path.exists(TESTFN):
Ezio Melotti76430242009-07-11 18:28:48 +00001558 unlink(TESTFN)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001559
Guido van Rossumd8faa362007-04-27 19:54:29 +00001560
1561class UniversalNewlineTests(unittest.TestCase):
1562 def setUp(self):
Guido van Rossum9c627722007-08-27 18:31:48 +00001563 self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001564 for i in range(FIXEDTEST_SIZE)]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001565 self.seps = ('\r', '\r\n', '\n')
1566 self.arcdata, self.arcfiles = {}, {}
1567 for n, s in enumerate(self.seps):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001568 b = s.encode("ascii")
1569 self.arcdata[s] = b.join(self.line_gen) + b
Guido van Rossumd8faa362007-04-27 19:54:29 +00001570 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001571 f = open(self.arcfiles[s], "wb")
1572 try:
1573 f.write(self.arcdata[s])
1574 finally:
1575 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001576
Ezio Melottiafd0d112009-07-15 17:17:17 +00001577 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001578 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001579 with zipfile.ZipFile(f, "w", compression) as zipfp:
1580 for fn in self.arcfiles.values():
1581 zipfp.write(fn, fn)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001582
Ezio Melottiafd0d112009-07-15 17:17:17 +00001583 def read_test(self, f, compression):
1584 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001585
1586 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001587 with zipfile.ZipFile(f, "r") as zipfp:
1588 for sep, fn in self.arcfiles.items():
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001589 with zipfp.open(fn, "rU") as fp:
1590 zipdata = fp.read()
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001591 self.assertEqual(self.arcdata[sep], zipdata)
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001592 if not isinstance(f, str):
1593 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001594
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001595 def readline_read_test(self, f, compression):
1596 self.make_test_archive(f, compression)
1597
1598 # Read the ZIP archive
Brian Curtin8fb9b862010-11-18 02:15:28 +00001599 with zipfile.ZipFile(f, "r") as zipfp:
1600 for sep, fn in self.arcfiles.items():
1601 with zipfp.open(fn, "rU") as zipopen:
1602 data = b''
1603 while True:
1604 read = zipopen.readline()
1605 if not read:
1606 break
1607 data += read
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001608
Brian Curtin8fb9b862010-11-18 02:15:28 +00001609 read = zipopen.read(5)
1610 if not read:
1611 break
1612 data += read
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001613
1614 self.assertEqual(data, self.arcdata['\n'])
1615
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001616 if not isinstance(f, str):
1617 f.close()
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001618
Ezio Melottiafd0d112009-07-15 17:17:17 +00001619 def readline_test(self, f, compression):
1620 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001621
1622 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001623 with zipfile.ZipFile(f, "r") as zipfp:
1624 for sep, fn in self.arcfiles.items():
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001625 with zipfp.open(fn, "rU") as zipopen:
1626 for line in self.line_gen:
1627 linedata = zipopen.readline()
1628 self.assertEqual(linedata, line + b'\n')
1629 if not isinstance(f, str):
1630 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001631
Ezio Melottiafd0d112009-07-15 17:17:17 +00001632 def readlines_test(self, f, compression):
1633 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001634
1635 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001636 with zipfile.ZipFile(f, "r") as zipfp:
1637 for sep, fn in self.arcfiles.items():
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001638 with zipfp.open(fn, "rU") as fp:
1639 ziplines = fp.readlines()
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001640 for line, zipline in zip(self.line_gen, ziplines):
1641 self.assertEqual(zipline, line + b'\n')
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001642 if not isinstance(f, str):
1643 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001644
Ezio Melottiafd0d112009-07-15 17:17:17 +00001645 def iterlines_test(self, f, compression):
1646 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001647
1648 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001649 with zipfile.ZipFile(f, "r") as zipfp:
1650 for sep, fn in self.arcfiles.items():
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001651 with zipfp.open(fn, "rU") as fp:
1652 for line, zipline in zip(self.line_gen, fp):
1653 self.assertEqual(zipline, line + b'\n')
1654 if not isinstance(f, str):
1655 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001656
Ezio Melottiafd0d112009-07-15 17:17:17 +00001657 def test_read_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001658 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001659 self.read_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001660
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001661 def test_readline_read_stored(self):
1662 # Issue #7610: calls to readline() interleaved with calls to read().
1663 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1664 self.readline_read_test(f, zipfile.ZIP_STORED)
1665
Ezio Melottiafd0d112009-07-15 17:17:17 +00001666 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001667 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001668 self.readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001669
Ezio Melottiafd0d112009-07-15 17:17:17 +00001670 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001671 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001672 self.readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001673
Ezio Melottiafd0d112009-07-15 17:17:17 +00001674 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001675 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001676 self.iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001677
Ezio Melotti975077a2011-05-19 22:03:22 +03001678 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +00001679 def test_read_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001680 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001681 self.read_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001682
Ezio Melotti975077a2011-05-19 22:03:22 +03001683 @requires_zlib
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001684 def test_readline_read_deflated(self):
1685 # Issue #7610: calls to readline() interleaved with calls to read().
1686 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1687 self.readline_read_test(f, zipfile.ZIP_DEFLATED)
1688
Ezio Melotti975077a2011-05-19 22:03:22 +03001689 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +00001690 def test_readline_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001691 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001692 self.readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001693
Ezio Melotti975077a2011-05-19 22:03:22 +03001694 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +00001695 def test_readlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001696 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001697 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001698
Ezio Melotti975077a2011-05-19 22:03:22 +03001699 @requires_zlib
Ezio Melottiafd0d112009-07-15 17:17:17 +00001700 def test_iterlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001701 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001702 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001703
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001704 @requires_bz2
1705 def test_read_bzip2(self):
1706 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1707 self.read_test(f, zipfile.ZIP_BZIP2)
1708
1709 @requires_bz2
1710 def test_readline_read_bzip2(self):
1711 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1712 self.readline_read_test(f, zipfile.ZIP_BZIP2)
1713
1714 @requires_bz2
1715 def test_readline_bzip2(self):
1716 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1717 self.readline_test(f, zipfile.ZIP_BZIP2)
1718
1719 @requires_bz2
1720 def test_readlines_bzip2(self):
1721 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1722 self.readlines_test(f, zipfile.ZIP_BZIP2)
1723
1724 @requires_bz2
1725 def test_iterlines_bzip2(self):
1726 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1727 self.iterlines_test(f, zipfile.ZIP_BZIP2)
1728
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001729 @requires_lzma
1730 def test_read_lzma(self):
1731 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1732 self.read_test(f, zipfile.ZIP_LZMA)
1733
1734 @requires_lzma
1735 def test_readline_read_lzma(self):
1736 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1737 self.readline_read_test(f, zipfile.ZIP_LZMA)
1738
1739 @requires_lzma
1740 def test_readline_lzma(self):
1741 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1742 self.readline_test(f, zipfile.ZIP_LZMA)
1743
1744 @requires_lzma
1745 def test_readlines_lzma(self):
1746 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1747 self.readlines_test(f, zipfile.ZIP_LZMA)
1748
1749 @requires_lzma
1750 def test_iterlines_lzma(self):
1751 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1752 self.iterlines_test(f, zipfile.ZIP_LZMA)
1753
Guido van Rossumd8faa362007-04-27 19:54:29 +00001754 def tearDown(self):
1755 for sep, fn in self.arcfiles.items():
1756 os.remove(fn)
Ezio Melotti76430242009-07-11 18:28:48 +00001757 unlink(TESTFN)
1758 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001759
1760
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001761def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +00001762 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1763 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti76430242009-07-11 18:28:48 +00001764 TestWithDirectory, UniversalNewlineTests,
1765 TestsWithRandomBinaryFiles)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001766
1767if __name__ == "__main__":
1768 test_main()