blob: 2b0ac36c23c1c443e8ffaad3c4e88c9635b0ad78 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +00002"""Test script for the gzip module.
3"""
4
5import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Christian Heimes05e8be12008-02-23 18:30:17 +00007import os
Antoine Pitroub1f88352010-01-03 22:37:40 +00008import io
Antoine Pitrou42db3ef2009-01-04 21:37:59 +00009import struct
Ezio Melotti78ea2022009-09-12 18:41:20 +000010gzip = support.import_module('gzip')
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000011
Walter Dörwald5b1284d2007-06-06 16:43:59 +000012data1 = b""" int length=DEFAULTALLOC, err = Z_OK;
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000013 PyObject *RetVal;
14 int flushmode = Z_FINISH;
15 unsigned long start_total_out;
16
17"""
18
Walter Dörwald5b1284d2007-06-06 16:43:59 +000019data2 = b"""/* zlibmodule.c -- gzip-compatible data compression */
Neal Norwitz014f1032004-07-29 03:55:56 +000020/* See http://www.gzip.org/zlib/
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000021/* See http://www.winimage.com/zLibDll for Windows */
22"""
23
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000024
Antoine Pitrou7b969842010-09-23 16:22:51 +000025class UnseekableIO(io.BytesIO):
26 def seekable(self):
27 return False
28
29 def tell(self):
30 raise io.UnsupportedOperation
31
32 def seek(self, *args):
33 raise io.UnsupportedOperation
34
35
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000036class TestGzip(unittest.TestCase):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000037 filename = support.TESTFN
Tim Peters5cfb05e2004-07-27 21:02:02 +000038
Georg Brandlb533e262008-05-25 18:19:30 +000039 def setUp(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000040 support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000041
Georg Brandlb533e262008-05-25 18:19:30 +000042 def tearDown(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000043 support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000044
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000045
Georg Brandlb533e262008-05-25 18:19:30 +000046 def test_write(self):
Brian Curtin28f96b52010-10-13 02:21:42 +000047 with gzip.GzipFile(self.filename, 'wb') as f:
48 f.write(data1 * 50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000049
Brian Curtin28f96b52010-10-13 02:21:42 +000050 # Try flush and fileno.
51 f.flush()
52 f.fileno()
53 if hasattr(os, 'fsync'):
54 os.fsync(f.fileno())
55 f.close()
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000056
Georg Brandlb533e262008-05-25 18:19:30 +000057 # Test multiple close() calls.
58 f.close()
59
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000060 def test_read(self):
61 self.test_write()
62 # Try reading.
Brian Curtin28f96b52010-10-13 02:21:42 +000063 with gzip.GzipFile(self.filename, 'r') as f:
64 d = f.read()
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000065 self.assertEqual(d, data1*50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000066
Antoine Pitrou7980eaa2010-10-06 21:21:18 +000067 def test_io_on_closed_object(self):
68 # Test that I/O operations on closed GzipFile objects raise a
69 # ValueError, just like the corresponding functions on file objects.
70
71 # Write to a file, open it for reading, then close it.
72 self.test_write()
73 f = gzip.GzipFile(self.filename, 'r')
74 f.close()
75 with self.assertRaises(ValueError):
76 f.read(1)
77 with self.assertRaises(ValueError):
78 f.seek(0)
79 with self.assertRaises(ValueError):
80 f.tell()
81 # Open the file for writing, then close it.
82 f = gzip.GzipFile(self.filename, 'w')
83 f.close()
84 with self.assertRaises(ValueError):
85 f.write(b'')
86 with self.assertRaises(ValueError):
87 f.flush()
88
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000089 def test_append(self):
90 self.test_write()
91 # Append to the previous file
Brian Curtin28f96b52010-10-13 02:21:42 +000092 with gzip.GzipFile(self.filename, 'ab') as f:
93 f.write(data2 * 15)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000094
Brian Curtin28f96b52010-10-13 02:21:42 +000095 with gzip.GzipFile(self.filename, 'rb') as f:
96 d = f.read()
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000097 self.assertEqual(d, (data1*50) + (data2*15))
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000098
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000099 def test_many_append(self):
100 # Bug #1074261 was triggered when reading a file that contained
101 # many, many members. Create such a file and verify that reading it
102 # works.
Brian Curtin28f96b52010-10-13 02:21:42 +0000103 with gzip.open(self.filename, 'wb', 9) as f:
Walter Dörwald5b1284d2007-06-06 16:43:59 +0000104 f.write(b'a')
Brian Curtin28f96b52010-10-13 02:21:42 +0000105 for i in range(0, 200):
106 with gzip.open(self.filename, "ab", 9) as f: # append
107 f.write(b'a')
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000108
109 # Try reading the file
Brian Curtin28f96b52010-10-13 02:21:42 +0000110 with gzip.open(self.filename, "rb") as zgfile:
111 contents = b""
112 while 1:
113 ztxt = zgfile.read(8192)
114 contents += ztxt
115 if not ztxt: break
Ezio Melottib3aedd42010-11-20 19:04:17 +0000116 self.assertEqual(contents, b'a'*201)
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000117
Antoine Pitroub1f88352010-01-03 22:37:40 +0000118 def test_buffered_reader(self):
119 # Issue #7471: a GzipFile can be wrapped in a BufferedReader for
120 # performance.
121 self.test_write()
122
Brian Curtin28f96b52010-10-13 02:21:42 +0000123 with gzip.GzipFile(self.filename, 'rb') as f:
124 with io.BufferedReader(f) as r:
125 lines = [line for line in r]
Antoine Pitroub1f88352010-01-03 22:37:40 +0000126
127 self.assertEqual(lines, 50 * data1.splitlines(True))
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000128
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000129 def test_readline(self):
130 self.test_write()
131 # Try .readline() with varying line lengths
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000132
Brian Curtin28f96b52010-10-13 02:21:42 +0000133 with gzip.GzipFile(self.filename, 'rb') as f:
134 line_length = 0
135 while 1:
136 L = f.readline(line_length)
137 if not L and line_length != 0: break
138 self.assertTrue(len(L) <= line_length)
139 line_length = (line_length + 1) % 50
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000140
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000141 def test_readlines(self):
142 self.test_write()
143 # Try .readlines()
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +0000144
Brian Curtin28f96b52010-10-13 02:21:42 +0000145 with gzip.GzipFile(self.filename, 'rb') as f:
146 L = f.readlines()
Skip Montanaro12424bc2002-05-23 01:43:05 +0000147
Brian Curtin28f96b52010-10-13 02:21:42 +0000148 with gzip.GzipFile(self.filename, 'rb') as f:
149 while 1:
150 L = f.readlines(150)
151 if L == []: break
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000152
153 def test_seek_read(self):
154 self.test_write()
155 # Try seek, read test
156
Brian Curtin28f96b52010-10-13 02:21:42 +0000157 with gzip.GzipFile(self.filename) as f:
158 while 1:
159 oldpos = f.tell()
160 line1 = f.readline()
161 if not line1: break
162 newpos = f.tell()
163 f.seek(oldpos) # negative seek
164 if len(line1)>10:
165 amount = 10
166 else:
167 amount = len(line1)
168 line2 = f.read(amount)
169 self.assertEqual(line1[:amount], line2)
170 f.seek(newpos) # positive seek
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000171
Thomas Wouters89f507f2006-12-13 04:49:30 +0000172 def test_seek_whence(self):
173 self.test_write()
174 # Try seek(whence=1), read test
175
Brian Curtin28f96b52010-10-13 02:21:42 +0000176 with gzip.GzipFile(self.filename) as f:
177 f.read(10)
178 f.seek(10, whence=1)
179 y = f.read(10)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000180 self.assertEqual(y, data1[20:30])
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000181
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000182 def test_seek_write(self):
183 # Try seek, write test
Brian Curtin28f96b52010-10-13 02:21:42 +0000184 with gzip.GzipFile(self.filename, 'w') as f:
185 for pos in range(0, 256, 16):
186 f.seek(pos)
187 f.write(b'GZ\n')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000188
189 def test_mode(self):
190 self.test_write()
Brian Curtin28f96b52010-10-13 02:21:42 +0000191 with gzip.GzipFile(self.filename, 'r') as f:
192 self.assertEqual(f.myfileobj.mode, 'rb')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000193
Thomas Wouterscf297e42007-02-23 15:07:44 +0000194 def test_1647484(self):
195 for mode in ('wb', 'rb'):
Brian Curtin28f96b52010-10-13 02:21:42 +0000196 with gzip.GzipFile(self.filename, mode) as f:
197 self.assertTrue(hasattr(f, "name"))
198 self.assertEqual(f.name, self.filename)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000199
Georg Brandl9f1c1dc2010-11-20 11:25:01 +0000200 def test_paddedfile_getattr(self):
201 self.test_write()
202 with gzip.GzipFile(self.filename, 'rb') as f:
203 self.assertTrue(hasattr(f.fileobj, "name"))
204 self.assertEqual(f.fileobj.name, self.filename)
205
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000206 def test_mtime(self):
207 mtime = 123456789
Brian Curtin28f96b52010-10-13 02:21:42 +0000208 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
209 fWrite.write(data1)
210 with gzip.GzipFile(self.filename) as fRead:
211 dataRead = fRead.read()
212 self.assertEqual(dataRead, data1)
213 self.assertTrue(hasattr(fRead, 'mtime'))
214 self.assertEqual(fRead.mtime, mtime)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000215
216 def test_metadata(self):
217 mtime = 123456789
218
Brian Curtin28f96b52010-10-13 02:21:42 +0000219 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
220 fWrite.write(data1)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000221
Brian Curtin28f96b52010-10-13 02:21:42 +0000222 with open(self.filename, 'rb') as fRead:
223 # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000224
Brian Curtin28f96b52010-10-13 02:21:42 +0000225 idBytes = fRead.read(2)
226 self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000227
Brian Curtin28f96b52010-10-13 02:21:42 +0000228 cmByte = fRead.read(1)
229 self.assertEqual(cmByte, b'\x08') # deflate
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000230
Brian Curtin28f96b52010-10-13 02:21:42 +0000231 flagsByte = fRead.read(1)
232 self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000233
Brian Curtin28f96b52010-10-13 02:21:42 +0000234 mtimeBytes = fRead.read(4)
235 self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000236
Brian Curtin28f96b52010-10-13 02:21:42 +0000237 xflByte = fRead.read(1)
238 self.assertEqual(xflByte, b'\x02') # maximum compression
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000239
Brian Curtin28f96b52010-10-13 02:21:42 +0000240 osByte = fRead.read(1)
241 self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000242
Brian Curtin28f96b52010-10-13 02:21:42 +0000243 # Since the FNAME flag is set, the zero-terminated filename follows.
244 # RFC 1952 specifies that this is the name of the input file, if any.
245 # However, the gzip module defaults to storing the name of the output
246 # file in this field.
247 expected = self.filename.encode('Latin-1') + b'\x00'
248 nameBytes = fRead.read(len(expected))
249 self.assertEqual(nameBytes, expected)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000250
Brian Curtin28f96b52010-10-13 02:21:42 +0000251 # Since no other flags were set, the header ends here.
252 # Rather than process the compressed data, let's seek to the trailer.
253 fRead.seek(os.stat(self.filename).st_size - 8)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000254
Brian Curtin28f96b52010-10-13 02:21:42 +0000255 crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
256 self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83')
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000257
Brian Curtin28f96b52010-10-13 02:21:42 +0000258 isizeBytes = fRead.read(4)
259 self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000260
Antoine Pitrou308705e2009-01-10 16:22:51 +0000261 def test_with_open(self):
262 # GzipFile supports the context management protocol
263 with gzip.GzipFile(self.filename, "wb") as f:
264 f.write(b"xxx")
265 f = gzip.GzipFile(self.filename, "rb")
266 f.close()
267 try:
268 with f:
269 pass
270 except ValueError:
271 pass
272 else:
273 self.fail("__enter__ on a closed file didn't raise an exception")
274 try:
275 with gzip.GzipFile(self.filename, "wb") as f:
276 1/0
277 except ZeroDivisionError:
278 pass
279 else:
280 self.fail("1/0 didn't raise an exception")
281
Antoine Pitrou8e33fd72010-01-13 14:37:26 +0000282 def test_zero_padded_file(self):
283 with gzip.GzipFile(self.filename, "wb") as f:
284 f.write(data1 * 50)
285
286 # Pad the file with zeroes
287 with open(self.filename, "ab") as f:
288 f.write(b"\x00" * 50)
289
290 with gzip.GzipFile(self.filename, "rb") as f:
291 d = f.read()
292 self.assertEqual(d, data1 * 50, "Incorrect data in file")
293
Antoine Pitrou7b969842010-09-23 16:22:51 +0000294 def test_non_seekable_file(self):
295 uncompressed = data1 * 50
296 buf = UnseekableIO()
297 with gzip.GzipFile(fileobj=buf, mode="wb") as f:
298 f.write(uncompressed)
299 compressed = buf.getvalue()
300 buf = UnseekableIO(compressed)
301 with gzip.GzipFile(fileobj=buf, mode="rb") as f:
302 self.assertEqual(f.read(), uncompressed)
303
Antoine Pitrouc3ed2e72010-09-29 10:49:46 +0000304 def test_peek(self):
305 uncompressed = data1 * 200
306 with gzip.GzipFile(self.filename, "wb") as f:
307 f.write(uncompressed)
308
309 def sizes():
310 while True:
311 for n in range(5, 50, 10):
312 yield n
313
314 with gzip.GzipFile(self.filename, "rb") as f:
315 f.max_read_chunk = 33
316 nread = 0
317 for n in sizes():
318 s = f.peek(n)
319 if s == b'':
320 break
321 self.assertEqual(f.read(len(s)), s)
322 nread += len(s)
323 self.assertEqual(f.read(100), b'')
324 self.assertEqual(nread, len(uncompressed))
325
Antoine Pitrou79c5ef12010-08-17 21:10:05 +0000326 # Testing compress/decompress shortcut functions
327
328 def test_compress(self):
329 for data in [data1, data2]:
330 for args in [(), (1,), (6,), (9,)]:
331 datac = gzip.compress(data, *args)
332 self.assertEqual(type(datac), bytes)
333 with gzip.GzipFile(fileobj=io.BytesIO(datac), mode="rb") as f:
334 self.assertEqual(f.read(), data)
335
336 def test_decompress(self):
337 for data in (data1, data2):
338 buf = io.BytesIO()
339 with gzip.GzipFile(fileobj=buf, mode="wb") as f:
340 f.write(data)
341 self.assertEqual(gzip.decompress(buf.getvalue()), data)
342 # Roundtrip with compress
343 datac = gzip.compress(data)
344 self.assertEqual(gzip.decompress(datac), data)
345
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000346def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000347 support.run_unittest(TestGzip)
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000348
349if __name__ == "__main__":
350 test_main(verbose=True)