blob: 9f7bfd2ed557a0fb99347fd5c0212f4bd72950f1 [file] [log] [blame]
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +00001#! /usr/bin/env python
2"""Test script for the gzip module.
3"""
4
5import unittest
6from test import test_support
Christian Heimesc5f05e42008-02-23 17:40:11 +00007import os
Antoine Pitrou673ddf92010-01-03 22:29:56 +00008import io
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +00009import struct
Ezio Melotti1036a7f2009-09-12 14:43:43 +000010gzip = test_support.import_module('gzip')
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000011
12data1 = """ int length=DEFAULTALLOC, err = Z_OK;
13 PyObject *RetVal;
14 int flushmode = Z_FINISH;
15 unsigned long start_total_out;
16
17"""
18
19data2 = """/* 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
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000025class TestGzip(unittest.TestCase):
26 filename = test_support.TESTFN
Tim Peters5cfb05e2004-07-27 21:02:02 +000027
Georg Brandle08e3d02008-05-25 08:07:37 +000028 def setUp(self):
Neal Norwitz36a59b42008-04-10 05:46:39 +000029 test_support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000030
Georg Brandle08e3d02008-05-25 08:07:37 +000031 def tearDown(self):
Neal Norwitz36a59b42008-04-10 05:46:39 +000032 test_support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000033
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000034
Georg Brandle08e3d02008-05-25 08:07:37 +000035 def test_write(self):
Brian Curtin31cf8d02010-10-13 23:51:19 +000036 with gzip.GzipFile(self.filename, 'wb') as f:
37 f.write(data1 * 50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000038
Brian Curtin31cf8d02010-10-13 23:51:19 +000039 # Try flush and fileno.
40 f.flush()
41 f.fileno()
42 if hasattr(os, 'fsync'):
43 os.fsync(f.fileno())
44 f.close()
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000045
Georg Brandle08e3d02008-05-25 08:07:37 +000046 # Test multiple close() calls.
47 f.close()
48
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000049 def test_read(self):
50 self.test_write()
51 # Try reading.
Brian Curtin31cf8d02010-10-13 23:51:19 +000052 with gzip.GzipFile(self.filename, 'r') as f:
53 d = f.read()
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000054 self.assertEqual(d, data1*50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000055
Nadeem Vawdadd72b3f2012-10-21 18:15:05 +020056 def test_read_universal_newlines(self):
57 # Issue #5148: Reading breaks when mode contains 'U'.
58 self.test_write()
59 with gzip.GzipFile(self.filename, 'rU') as f:
60 d = f.read()
61 self.assertEqual(d, data1*50)
62
Antoine Pitrou76a66aa2010-10-06 21:26:52 +000063 def test_io_on_closed_object(self):
64 # Test that I/O operations on closed GzipFile objects raise a
65 # ValueError, just like the corresponding functions on file objects.
66
67 # Write to a file, open it for reading, then close it.
68 self.test_write()
69 f = gzip.GzipFile(self.filename, 'r')
70 f.close()
71 with self.assertRaises(ValueError):
72 f.read(1)
73 with self.assertRaises(ValueError):
74 f.seek(0)
75 with self.assertRaises(ValueError):
76 f.tell()
77 # Open the file for writing, then close it.
78 f = gzip.GzipFile(self.filename, 'w')
79 f.close()
80 with self.assertRaises(ValueError):
Brian Curtin31cf8d02010-10-13 23:51:19 +000081 f.write('')
Antoine Pitrou76a66aa2010-10-06 21:26:52 +000082 with self.assertRaises(ValueError):
83 f.flush()
84
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000085 def test_append(self):
86 self.test_write()
87 # Append to the previous file
Brian Curtin31cf8d02010-10-13 23:51:19 +000088 with gzip.GzipFile(self.filename, 'ab') as f:
89 f.write(data2 * 15)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000090
Brian Curtin31cf8d02010-10-13 23:51:19 +000091 with gzip.GzipFile(self.filename, 'rb') as f:
92 d = f.read()
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000093 self.assertEqual(d, (data1*50) + (data2*15))
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000094
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000095 def test_many_append(self):
96 # Bug #1074261 was triggered when reading a file that contained
97 # many, many members. Create such a file and verify that reading it
98 # works.
Brian Curtin31cf8d02010-10-13 23:51:19 +000099 with gzip.open(self.filename, 'wb', 9) as f:
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000100 f.write('a')
Brian Curtin31cf8d02010-10-13 23:51:19 +0000101 for i in range(0, 200):
102 with gzip.open(self.filename, "ab", 9) as f: # append
103 f.write('a')
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000104
105 # Try reading the file
Brian Curtin31cf8d02010-10-13 23:51:19 +0000106 with gzip.open(self.filename, "rb") as zgfile:
107 contents = ""
108 while 1:
109 ztxt = zgfile.read(8192)
110 contents += ztxt
111 if not ztxt: break
Ezio Melotti2623a372010-11-21 13:34:58 +0000112 self.assertEqual(contents, 'a'*201)
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000113
Antoine Pitrou673ddf92010-01-03 22:29:56 +0000114 def test_buffered_reader(self):
115 # Issue #7471: a GzipFile can be wrapped in a BufferedReader for
116 # performance.
117 self.test_write()
118
Brian Curtin31cf8d02010-10-13 23:51:19 +0000119 with gzip.GzipFile(self.filename, 'rb') as f:
120 with io.BufferedReader(f) as r:
121 lines = [line for line in r]
Antoine Pitrou673ddf92010-01-03 22:29:56 +0000122
123 self.assertEqual(lines, 50 * data1.splitlines(True))
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000124
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000125 def test_readline(self):
126 self.test_write()
127 # Try .readline() with varying line lengths
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000128
Brian Curtin31cf8d02010-10-13 23:51:19 +0000129 with gzip.GzipFile(self.filename, 'rb') as f:
130 line_length = 0
131 while 1:
132 L = f.readline(line_length)
133 if not L and line_length != 0: break
134 self.assertTrue(len(L) <= line_length)
135 line_length = (line_length + 1) % 50
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000136
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000137 def test_readlines(self):
138 self.test_write()
139 # Try .readlines()
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +0000140
Brian Curtin31cf8d02010-10-13 23:51:19 +0000141 with gzip.GzipFile(self.filename, 'rb') as f:
142 L = f.readlines()
Skip Montanaro12424bc2002-05-23 01:43:05 +0000143
Brian Curtin31cf8d02010-10-13 23:51:19 +0000144 with gzip.GzipFile(self.filename, 'rb') as f:
145 while 1:
146 L = f.readlines(150)
147 if L == []: break
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000148
149 def test_seek_read(self):
150 self.test_write()
151 # Try seek, read test
152
Brian Curtin31cf8d02010-10-13 23:51:19 +0000153 with gzip.GzipFile(self.filename) as f:
154 while 1:
155 oldpos = f.tell()
156 line1 = f.readline()
157 if not line1: break
158 newpos = f.tell()
159 f.seek(oldpos) # negative seek
160 if len(line1)>10:
161 amount = 10
162 else:
163 amount = len(line1)
164 line2 = f.read(amount)
165 self.assertEqual(line1[:amount], line2)
166 f.seek(newpos) # positive seek
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000167
Martin v. Löwis065f0c82006-11-12 10:41:39 +0000168 def test_seek_whence(self):
169 self.test_write()
170 # Try seek(whence=1), read test
171
Brian Curtin31cf8d02010-10-13 23:51:19 +0000172 with gzip.GzipFile(self.filename) as f:
173 f.read(10)
174 f.seek(10, whence=1)
175 y = f.read(10)
Ezio Melotti2623a372010-11-21 13:34:58 +0000176 self.assertEqual(y, data1[20:30])
Tim Petersf733abb2007-01-30 03:03:46 +0000177
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000178 def test_seek_write(self):
179 # Try seek, write test
Brian Curtin31cf8d02010-10-13 23:51:19 +0000180 with gzip.GzipFile(self.filename, 'w') as f:
181 for pos in range(0, 256, 16):
182 f.seek(pos)
183 f.write('GZ\n')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000184
185 def test_mode(self):
186 self.test_write()
Brian Curtin31cf8d02010-10-13 23:51:19 +0000187 with gzip.GzipFile(self.filename, 'r') as f:
188 self.assertEqual(f.myfileobj.mode, 'rb')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000189
Lars Gustäbel5b1a7852007-02-13 16:09:24 +0000190 def test_1647484(self):
191 for mode in ('wb', 'rb'):
Brian Curtin31cf8d02010-10-13 23:51:19 +0000192 with gzip.GzipFile(self.filename, mode) as f:
193 self.assertTrue(hasattr(f, "name"))
194 self.assertEqual(f.name, self.filename)
Lars Gustäbel5b1a7852007-02-13 16:09:24 +0000195
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000196 def test_mtime(self):
197 mtime = 123456789
Brian Curtin31cf8d02010-10-13 23:51:19 +0000198 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
199 fWrite.write(data1)
200 with gzip.GzipFile(self.filename) as fRead:
201 dataRead = fRead.read()
202 self.assertEqual(dataRead, data1)
203 self.assertTrue(hasattr(fRead, 'mtime'))
204 self.assertEqual(fRead.mtime, mtime)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000205
206 def test_metadata(self):
207 mtime = 123456789
208
Brian Curtin31cf8d02010-10-13 23:51:19 +0000209 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
210 fWrite.write(data1)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000211
Brian Curtin31cf8d02010-10-13 23:51:19 +0000212 with open(self.filename, 'rb') as fRead:
213 # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000214
Brian Curtin31cf8d02010-10-13 23:51:19 +0000215 idBytes = fRead.read(2)
216 self.assertEqual(idBytes, '\x1f\x8b') # gzip ID
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000217
Brian Curtin31cf8d02010-10-13 23:51:19 +0000218 cmByte = fRead.read(1)
219 self.assertEqual(cmByte, '\x08') # deflate
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000220
Brian Curtin31cf8d02010-10-13 23:51:19 +0000221 flagsByte = fRead.read(1)
222 self.assertEqual(flagsByte, '\x08') # only the FNAME flag is set
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000223
Brian Curtin31cf8d02010-10-13 23:51:19 +0000224 mtimeBytes = fRead.read(4)
225 self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000226
Brian Curtin31cf8d02010-10-13 23:51:19 +0000227 xflByte = fRead.read(1)
228 self.assertEqual(xflByte, '\x02') # maximum compression
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000229
Brian Curtin31cf8d02010-10-13 23:51:19 +0000230 osByte = fRead.read(1)
231 self.assertEqual(osByte, '\xff') # OS "unknown" (OS-independent)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000232
Brian Curtin31cf8d02010-10-13 23:51:19 +0000233 # Since the FNAME flag is set, the zero-terminated filename follows.
234 # RFC 1952 specifies that this is the name of the input file, if any.
235 # However, the gzip module defaults to storing the name of the output
236 # file in this field.
237 expected = self.filename.encode('Latin-1') + '\x00'
238 nameBytes = fRead.read(len(expected))
239 self.assertEqual(nameBytes, expected)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000240
Brian Curtin31cf8d02010-10-13 23:51:19 +0000241 # Since no other flags were set, the header ends here.
242 # Rather than process the compressed data, let's seek to the trailer.
243 fRead.seek(os.stat(self.filename).st_size - 8)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000244
Brian Curtin31cf8d02010-10-13 23:51:19 +0000245 crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
246 self.assertEqual(crc32Bytes, '\xaf\xd7d\x83')
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000247
Brian Curtin31cf8d02010-10-13 23:51:19 +0000248 isizeBytes = fRead.read(4)
249 self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000250
Antoine Pitroub74fc2b2009-01-10 16:13:45 +0000251 def test_with_open(self):
252 # GzipFile supports the context management protocol
253 with gzip.GzipFile(self.filename, "wb") as f:
254 f.write(b"xxx")
255 f = gzip.GzipFile(self.filename, "rb")
256 f.close()
257 try:
258 with f:
259 pass
260 except ValueError:
261 pass
262 else:
263 self.fail("__enter__ on a closed file didn't raise an exception")
264 try:
265 with gzip.GzipFile(self.filename, "wb") as f:
Ezio Melottidde5b942010-02-03 05:37:26 +0000266 1 // 0
Antoine Pitroub74fc2b2009-01-10 16:13:45 +0000267 except ZeroDivisionError:
268 pass
269 else:
Ezio Melottidde5b942010-02-03 05:37:26 +0000270 self.fail("1 // 0 didn't raise an exception")
Antoine Pitroub74fc2b2009-01-10 16:13:45 +0000271
Antoine Pitrou5a9112c2010-01-13 14:32:10 +0000272 def test_zero_padded_file(self):
273 with gzip.GzipFile(self.filename, "wb") as f:
274 f.write(data1 * 50)
275
276 # Pad the file with zeroes
277 with open(self.filename, "ab") as f:
278 f.write("\x00" * 50)
279
280 with gzip.GzipFile(self.filename, "rb") as f:
281 d = f.read()
282 self.assertEqual(d, data1 * 50, "Incorrect data in file")
283
Nadeem Vawdad7664de2012-01-19 00:40:46 +0200284 def test_fileobj_from_fdopen(self):
285 # Issue #13781: Creating a GzipFile using a fileobj from os.fdopen()
286 # should not embed the fake filename "<fdopen>" in the output file.
287 fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT)
288 with os.fdopen(fd, "wb") as f:
289 with gzip.GzipFile(fileobj=f, mode="w") as g:
290 self.assertEqual(g.name, "")
291
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000292def test_main(verbose=None):
293 test_support.run_unittest(TestGzip)
294
295if __name__ == "__main__":
296 test_main(verbose=True)