blob: 5025b91d343aa6cf6ad8e3181b1af60edf1e0460 [file] [log] [blame]
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +00001"""Test script for the gzip module.
2"""
3
4import unittest
5from test import test_support
Christian Heimesc5f05e42008-02-23 17:40:11 +00006import os
Antoine Pitrou673ddf92010-01-03 22:29:56 +00007import io
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +00008import struct
Ezio Melotti1036a7f2009-09-12 14:43:43 +00009gzip = test_support.import_module('gzip')
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000010
11data1 = """ int length=DEFAULTALLOC, err = Z_OK;
12 PyObject *RetVal;
13 int flushmode = Z_FINISH;
14 unsigned long start_total_out;
15
16"""
17
18data2 = """/* zlibmodule.c -- gzip-compatible data compression */
Neal Norwitz014f1032004-07-29 03:55:56 +000019/* See http://www.gzip.org/zlib/
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000020/* See http://www.winimage.com/zLibDll for Windows */
21"""
22
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000023
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000024class TestGzip(unittest.TestCase):
25 filename = test_support.TESTFN
Tim Peters5cfb05e2004-07-27 21:02:02 +000026
Georg Brandle08e3d02008-05-25 08:07:37 +000027 def setUp(self):
Neal Norwitz36a59b42008-04-10 05:46:39 +000028 test_support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000029
Georg Brandle08e3d02008-05-25 08:07:37 +000030 def tearDown(self):
Neal Norwitz36a59b42008-04-10 05:46:39 +000031 test_support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000032
Serhiy Storchakaf689f102015-03-23 15:25:18 +020033 def write_and_read_back(self, data, mode='b'):
34 b_data = memoryview(data).tobytes()
35 with gzip.GzipFile(self.filename, 'w'+mode) as f:
36 l = f.write(data)
37 self.assertEqual(l, len(b_data))
38 with gzip.GzipFile(self.filename, 'r'+mode) as f:
39 self.assertEqual(f.read(), b_data)
40
Serhiy Storchaka54edfb32014-10-12 22:23:28 +030041 @test_support.requires_unicode
42 def test_unicode_filename(self):
43 unicode_filename = test_support.TESTFN_UNICODE
Serhiy Storchakafbddffa2014-10-13 10:33:32 +030044 try:
45 unicode_filename.encode(test_support.TESTFN_ENCODING)
46 except (UnicodeError, TypeError):
47 self.skipTest("Requires unicode filenames support")
Victor Stinner7d490652015-03-30 02:20:37 +020048 self.filename = unicode_filename
Serhiy Storchaka54edfb32014-10-12 22:23:28 +030049 with gzip.GzipFile(unicode_filename, "wb") as f:
50 f.write(data1 * 50)
51 with gzip.GzipFile(unicode_filename, "rb") as f:
52 self.assertEqual(f.read(), data1 * 50)
53 # Sanity check that we are actually operating on the right file.
54 with open(unicode_filename, 'rb') as fobj, \
55 gzip.GzipFile(fileobj=fobj, mode="rb") as f:
56 self.assertEqual(f.read(), data1 * 50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000057
Georg Brandle08e3d02008-05-25 08:07:37 +000058 def test_write(self):
Brian Curtin31cf8d02010-10-13 23:51:19 +000059 with gzip.GzipFile(self.filename, 'wb') as f:
60 f.write(data1 * 50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000061
Brian Curtin31cf8d02010-10-13 23:51:19 +000062 # Try flush and fileno.
63 f.flush()
64 f.fileno()
65 if hasattr(os, 'fsync'):
66 os.fsync(f.fileno())
67 f.close()
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000068
Georg Brandle08e3d02008-05-25 08:07:37 +000069 # Test multiple close() calls.
70 f.close()
71
Serhiy Storchakaf689f102015-03-23 15:25:18 +020072 # The following test_write_xy methods test that write accepts
73 # the corresponding bytes-like object type as input
74 # and that the data written equals bytes(xy) in all cases.
75 def test_write_memoryview(self):
76 self.write_and_read_back(memoryview(data1 * 50))
77
78 def test_write_incompatible_type(self):
79 # Test that non-bytes-like types raise TypeError.
80 # Issue #21560: attempts to write incompatible types
81 # should not affect the state of the fileobject
82 with gzip.GzipFile(self.filename, 'wb') as f:
83 with self.assertRaises(UnicodeEncodeError):
84 f.write(u'\xff')
85 with self.assertRaises(TypeError):
86 f.write([1])
87 f.write(data1)
88 with gzip.GzipFile(self.filename, 'rb') as f:
89 self.assertEqual(f.read(), data1)
90
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000091 def test_read(self):
92 self.test_write()
93 # Try reading.
Brian Curtin31cf8d02010-10-13 23:51:19 +000094 with gzip.GzipFile(self.filename, 'r') as f:
95 d = f.read()
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000096 self.assertEqual(d, data1*50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000097
Nadeem Vawdadd72b3f2012-10-21 18:15:05 +020098 def test_read_universal_newlines(self):
99 # Issue #5148: Reading breaks when mode contains 'U'.
100 self.test_write()
101 with gzip.GzipFile(self.filename, 'rU') as f:
102 d = f.read()
103 self.assertEqual(d, data1*50)
104
Antoine Pitrou76a66aa2010-10-06 21:26:52 +0000105 def test_io_on_closed_object(self):
106 # Test that I/O operations on closed GzipFile objects raise a
107 # ValueError, just like the corresponding functions on file objects.
108
109 # Write to a file, open it for reading, then close it.
110 self.test_write()
111 f = gzip.GzipFile(self.filename, 'r')
112 f.close()
113 with self.assertRaises(ValueError):
114 f.read(1)
115 with self.assertRaises(ValueError):
116 f.seek(0)
117 with self.assertRaises(ValueError):
118 f.tell()
119 # Open the file for writing, then close it.
120 f = gzip.GzipFile(self.filename, 'w')
121 f.close()
122 with self.assertRaises(ValueError):
Brian Curtin31cf8d02010-10-13 23:51:19 +0000123 f.write('')
Antoine Pitrou76a66aa2010-10-06 21:26:52 +0000124 with self.assertRaises(ValueError):
125 f.flush()
126
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000127 def test_append(self):
128 self.test_write()
129 # Append to the previous file
Brian Curtin31cf8d02010-10-13 23:51:19 +0000130 with gzip.GzipFile(self.filename, 'ab') as f:
131 f.write(data2 * 15)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +0000132
Brian Curtin31cf8d02010-10-13 23:51:19 +0000133 with gzip.GzipFile(self.filename, 'rb') as f:
134 d = f.read()
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000135 self.assertEqual(d, (data1*50) + (data2*15))
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +0000136
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000137 def test_many_append(self):
138 # Bug #1074261 was triggered when reading a file that contained
139 # many, many members. Create such a file and verify that reading it
140 # works.
Brian Curtin31cf8d02010-10-13 23:51:19 +0000141 with gzip.open(self.filename, 'wb', 9) as f:
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000142 f.write('a')
Brian Curtin31cf8d02010-10-13 23:51:19 +0000143 for i in range(0, 200):
144 with gzip.open(self.filename, "ab", 9) as f: # append
145 f.write('a')
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000146
147 # Try reading the file
Brian Curtin31cf8d02010-10-13 23:51:19 +0000148 with gzip.open(self.filename, "rb") as zgfile:
149 contents = ""
150 while 1:
151 ztxt = zgfile.read(8192)
152 contents += ztxt
153 if not ztxt: break
Ezio Melotti2623a372010-11-21 13:34:58 +0000154 self.assertEqual(contents, 'a'*201)
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000155
Antoine Pitrou673ddf92010-01-03 22:29:56 +0000156 def test_buffered_reader(self):
157 # Issue #7471: a GzipFile can be wrapped in a BufferedReader for
158 # performance.
159 self.test_write()
160
Brian Curtin31cf8d02010-10-13 23:51:19 +0000161 with gzip.GzipFile(self.filename, 'rb') as f:
162 with io.BufferedReader(f) as r:
163 lines = [line for line in r]
Antoine Pitrou673ddf92010-01-03 22:29:56 +0000164
165 self.assertEqual(lines, 50 * data1.splitlines(True))
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000166
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000167 def test_readline(self):
168 self.test_write()
169 # Try .readline() with varying line lengths
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000170
Brian Curtin31cf8d02010-10-13 23:51:19 +0000171 with gzip.GzipFile(self.filename, 'rb') as f:
172 line_length = 0
173 while 1:
174 L = f.readline(line_length)
175 if not L and line_length != 0: break
176 self.assertTrue(len(L) <= line_length)
177 line_length = (line_length + 1) % 50
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000178
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000179 def test_readlines(self):
180 self.test_write()
181 # Try .readlines()
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +0000182
Brian Curtin31cf8d02010-10-13 23:51:19 +0000183 with gzip.GzipFile(self.filename, 'rb') as f:
184 L = f.readlines()
Skip Montanaro12424bc2002-05-23 01:43:05 +0000185
Brian Curtin31cf8d02010-10-13 23:51:19 +0000186 with gzip.GzipFile(self.filename, 'rb') as f:
187 while 1:
188 L = f.readlines(150)
189 if L == []: break
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000190
191 def test_seek_read(self):
192 self.test_write()
193 # Try seek, read test
194
Brian Curtin31cf8d02010-10-13 23:51:19 +0000195 with gzip.GzipFile(self.filename) as f:
196 while 1:
197 oldpos = f.tell()
198 line1 = f.readline()
199 if not line1: break
200 newpos = f.tell()
201 f.seek(oldpos) # negative seek
202 if len(line1)>10:
203 amount = 10
204 else:
205 amount = len(line1)
206 line2 = f.read(amount)
207 self.assertEqual(line1[:amount], line2)
208 f.seek(newpos) # positive seek
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000209
Martin v. Löwis065f0c82006-11-12 10:41:39 +0000210 def test_seek_whence(self):
211 self.test_write()
212 # Try seek(whence=1), read test
213
Brian Curtin31cf8d02010-10-13 23:51:19 +0000214 with gzip.GzipFile(self.filename) as f:
215 f.read(10)
216 f.seek(10, whence=1)
217 y = f.read(10)
Ezio Melotti2623a372010-11-21 13:34:58 +0000218 self.assertEqual(y, data1[20:30])
Tim Petersf733abb2007-01-30 03:03:46 +0000219
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000220 def test_seek_write(self):
221 # Try seek, write test
Brian Curtin31cf8d02010-10-13 23:51:19 +0000222 with gzip.GzipFile(self.filename, 'w') as f:
223 for pos in range(0, 256, 16):
224 f.seek(pos)
225 f.write('GZ\n')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000226
227 def test_mode(self):
228 self.test_write()
Brian Curtin31cf8d02010-10-13 23:51:19 +0000229 with gzip.GzipFile(self.filename, 'r') as f:
230 self.assertEqual(f.myfileobj.mode, 'rb')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000231
Lars Gustäbel5b1a7852007-02-13 16:09:24 +0000232 def test_1647484(self):
233 for mode in ('wb', 'rb'):
Brian Curtin31cf8d02010-10-13 23:51:19 +0000234 with gzip.GzipFile(self.filename, mode) as f:
235 self.assertTrue(hasattr(f, "name"))
236 self.assertEqual(f.name, self.filename)
Lars Gustäbel5b1a7852007-02-13 16:09:24 +0000237
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000238 def test_mtime(self):
239 mtime = 123456789
Brian Curtin31cf8d02010-10-13 23:51:19 +0000240 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
241 fWrite.write(data1)
242 with gzip.GzipFile(self.filename) as fRead:
243 dataRead = fRead.read()
244 self.assertEqual(dataRead, data1)
245 self.assertTrue(hasattr(fRead, 'mtime'))
246 self.assertEqual(fRead.mtime, mtime)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000247
248 def test_metadata(self):
249 mtime = 123456789
250
Brian Curtin31cf8d02010-10-13 23:51:19 +0000251 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
252 fWrite.write(data1)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000253
Brian Curtin31cf8d02010-10-13 23:51:19 +0000254 with open(self.filename, 'rb') as fRead:
255 # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000256
Brian Curtin31cf8d02010-10-13 23:51:19 +0000257 idBytes = fRead.read(2)
258 self.assertEqual(idBytes, '\x1f\x8b') # gzip ID
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000259
Brian Curtin31cf8d02010-10-13 23:51:19 +0000260 cmByte = fRead.read(1)
261 self.assertEqual(cmByte, '\x08') # deflate
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000262
Brian Curtin31cf8d02010-10-13 23:51:19 +0000263 flagsByte = fRead.read(1)
264 self.assertEqual(flagsByte, '\x08') # only the FNAME flag is set
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000265
Brian Curtin31cf8d02010-10-13 23:51:19 +0000266 mtimeBytes = fRead.read(4)
267 self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000268
Brian Curtin31cf8d02010-10-13 23:51:19 +0000269 xflByte = fRead.read(1)
270 self.assertEqual(xflByte, '\x02') # maximum compression
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000271
Brian Curtin31cf8d02010-10-13 23:51:19 +0000272 osByte = fRead.read(1)
273 self.assertEqual(osByte, '\xff') # OS "unknown" (OS-independent)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000274
Brian Curtin31cf8d02010-10-13 23:51:19 +0000275 # Since the FNAME flag is set, the zero-terminated filename follows.
276 # RFC 1952 specifies that this is the name of the input file, if any.
277 # However, the gzip module defaults to storing the name of the output
278 # file in this field.
279 expected = self.filename.encode('Latin-1') + '\x00'
280 nameBytes = fRead.read(len(expected))
281 self.assertEqual(nameBytes, expected)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000282
Brian Curtin31cf8d02010-10-13 23:51:19 +0000283 # Since no other flags were set, the header ends here.
284 # Rather than process the compressed data, let's seek to the trailer.
285 fRead.seek(os.stat(self.filename).st_size - 8)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000286
Brian Curtin31cf8d02010-10-13 23:51:19 +0000287 crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
288 self.assertEqual(crc32Bytes, '\xaf\xd7d\x83')
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000289
Brian Curtin31cf8d02010-10-13 23:51:19 +0000290 isizeBytes = fRead.read(4)
291 self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000292
Antoine Pitroub74fc2b2009-01-10 16:13:45 +0000293 def test_with_open(self):
294 # GzipFile supports the context management protocol
295 with gzip.GzipFile(self.filename, "wb") as f:
296 f.write(b"xxx")
297 f = gzip.GzipFile(self.filename, "rb")
298 f.close()
299 try:
300 with f:
301 pass
302 except ValueError:
303 pass
304 else:
305 self.fail("__enter__ on a closed file didn't raise an exception")
306 try:
307 with gzip.GzipFile(self.filename, "wb") as f:
Ezio Melottidde5b942010-02-03 05:37:26 +0000308 1 // 0
Antoine Pitroub74fc2b2009-01-10 16:13:45 +0000309 except ZeroDivisionError:
310 pass
311 else:
Ezio Melottidde5b942010-02-03 05:37:26 +0000312 self.fail("1 // 0 didn't raise an exception")
Antoine Pitroub74fc2b2009-01-10 16:13:45 +0000313
Antoine Pitrou5a9112c2010-01-13 14:32:10 +0000314 def test_zero_padded_file(self):
315 with gzip.GzipFile(self.filename, "wb") as f:
316 f.write(data1 * 50)
317
318 # Pad the file with zeroes
319 with open(self.filename, "ab") as f:
320 f.write("\x00" * 50)
321
322 with gzip.GzipFile(self.filename, "rb") as f:
323 d = f.read()
324 self.assertEqual(d, data1 * 50, "Incorrect data in file")
325
Nadeem Vawdad7664de2012-01-19 00:40:46 +0200326 def test_fileobj_from_fdopen(self):
327 # Issue #13781: Creating a GzipFile using a fileobj from os.fdopen()
328 # should not embed the fake filename "<fdopen>" in the output file.
329 fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT)
330 with os.fdopen(fd, "wb") as f:
331 with gzip.GzipFile(fileobj=f, mode="w") as g:
332 self.assertEqual(g.name, "")
333
Serhiy Storchaka371432b2013-04-08 22:33:55 +0300334 def test_read_with_extra(self):
335 # Gzip data with an extra field
336 gzdata = (b'\x1f\x8b\x08\x04\xb2\x17cQ\x02\xff'
337 b'\x05\x00Extra'
338 b'\x0bI-.\x01\x002\xd1Mx\x04\x00\x00\x00')
339 with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f:
340 self.assertEqual(f.read(), b'Test')
Serhiy Storchaka353e54e2013-01-22 17:13:26 +0200341
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000342def test_main(verbose=None):
343 test_support.run_unittest(TestGzip)
344
345if __name__ == "__main__":
346 test_main(verbose=True)