blob: a28cd34b0ca9a06b467c7f807466b13e6db87e94 [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
Antoine Pitrou76a66aa2010-10-06 21:26:52 +000056 def test_io_on_closed_object(self):
57 # Test that I/O operations on closed GzipFile objects raise a
58 # ValueError, just like the corresponding functions on file objects.
59
60 # Write to a file, open it for reading, then close it.
61 self.test_write()
62 f = gzip.GzipFile(self.filename, 'r')
63 f.close()
64 with self.assertRaises(ValueError):
65 f.read(1)
66 with self.assertRaises(ValueError):
67 f.seek(0)
68 with self.assertRaises(ValueError):
69 f.tell()
70 # Open the file for writing, then close it.
71 f = gzip.GzipFile(self.filename, 'w')
72 f.close()
73 with self.assertRaises(ValueError):
Brian Curtin31cf8d02010-10-13 23:51:19 +000074 f.write('')
Antoine Pitrou76a66aa2010-10-06 21:26:52 +000075 with self.assertRaises(ValueError):
76 f.flush()
77
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000078 def test_append(self):
79 self.test_write()
80 # Append to the previous file
Brian Curtin31cf8d02010-10-13 23:51:19 +000081 with gzip.GzipFile(self.filename, 'ab') as f:
82 f.write(data2 * 15)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000083
Brian Curtin31cf8d02010-10-13 23:51:19 +000084 with gzip.GzipFile(self.filename, 'rb') as f:
85 d = f.read()
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000086 self.assertEqual(d, (data1*50) + (data2*15))
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000087
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000088 def test_many_append(self):
89 # Bug #1074261 was triggered when reading a file that contained
90 # many, many members. Create such a file and verify that reading it
91 # works.
Brian Curtin31cf8d02010-10-13 23:51:19 +000092 with gzip.open(self.filename, 'wb', 9) as f:
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000093 f.write('a')
Brian Curtin31cf8d02010-10-13 23:51:19 +000094 for i in range(0, 200):
95 with gzip.open(self.filename, "ab", 9) as f: # append
96 f.write('a')
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000097
98 # Try reading the file
Brian Curtin31cf8d02010-10-13 23:51:19 +000099 with gzip.open(self.filename, "rb") as zgfile:
100 contents = ""
101 while 1:
102 ztxt = zgfile.read(8192)
103 contents += ztxt
104 if not ztxt: break
Ezio Melotti2623a372010-11-21 13:34:58 +0000105 self.assertEqual(contents, 'a'*201)
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000106
Antoine Pitrou673ddf92010-01-03 22:29:56 +0000107 def test_buffered_reader(self):
108 # Issue #7471: a GzipFile can be wrapped in a BufferedReader for
109 # performance.
110 self.test_write()
111
Brian Curtin31cf8d02010-10-13 23:51:19 +0000112 with gzip.GzipFile(self.filename, 'rb') as f:
113 with io.BufferedReader(f) as r:
114 lines = [line for line in r]
Antoine Pitrou673ddf92010-01-03 22:29:56 +0000115
116 self.assertEqual(lines, 50 * data1.splitlines(True))
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000117
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000118 def test_readline(self):
119 self.test_write()
120 # Try .readline() with varying line lengths
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000121
Brian Curtin31cf8d02010-10-13 23:51:19 +0000122 with gzip.GzipFile(self.filename, 'rb') as f:
123 line_length = 0
124 while 1:
125 L = f.readline(line_length)
126 if not L and line_length != 0: break
127 self.assertTrue(len(L) <= line_length)
128 line_length = (line_length + 1) % 50
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000129
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000130 def test_readlines(self):
131 self.test_write()
132 # Try .readlines()
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +0000133
Brian Curtin31cf8d02010-10-13 23:51:19 +0000134 with gzip.GzipFile(self.filename, 'rb') as f:
135 L = f.readlines()
Skip Montanaro12424bc2002-05-23 01:43:05 +0000136
Brian Curtin31cf8d02010-10-13 23:51:19 +0000137 with gzip.GzipFile(self.filename, 'rb') as f:
138 while 1:
139 L = f.readlines(150)
140 if L == []: break
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000141
142 def test_seek_read(self):
143 self.test_write()
144 # Try seek, read test
145
Brian Curtin31cf8d02010-10-13 23:51:19 +0000146 with gzip.GzipFile(self.filename) as f:
147 while 1:
148 oldpos = f.tell()
149 line1 = f.readline()
150 if not line1: break
151 newpos = f.tell()
152 f.seek(oldpos) # negative seek
153 if len(line1)>10:
154 amount = 10
155 else:
156 amount = len(line1)
157 line2 = f.read(amount)
158 self.assertEqual(line1[:amount], line2)
159 f.seek(newpos) # positive seek
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000160
Martin v. Löwis065f0c82006-11-12 10:41:39 +0000161 def test_seek_whence(self):
162 self.test_write()
163 # Try seek(whence=1), read test
164
Brian Curtin31cf8d02010-10-13 23:51:19 +0000165 with gzip.GzipFile(self.filename) as f:
166 f.read(10)
167 f.seek(10, whence=1)
168 y = f.read(10)
Ezio Melotti2623a372010-11-21 13:34:58 +0000169 self.assertEqual(y, data1[20:30])
Tim Petersf733abb2007-01-30 03:03:46 +0000170
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000171 def test_seek_write(self):
172 # Try seek, write test
Brian Curtin31cf8d02010-10-13 23:51:19 +0000173 with gzip.GzipFile(self.filename, 'w') as f:
174 for pos in range(0, 256, 16):
175 f.seek(pos)
176 f.write('GZ\n')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000177
178 def test_mode(self):
179 self.test_write()
Brian Curtin31cf8d02010-10-13 23:51:19 +0000180 with gzip.GzipFile(self.filename, 'r') as f:
181 self.assertEqual(f.myfileobj.mode, 'rb')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000182
Lars Gustäbel5b1a7852007-02-13 16:09:24 +0000183 def test_1647484(self):
184 for mode in ('wb', 'rb'):
Brian Curtin31cf8d02010-10-13 23:51:19 +0000185 with gzip.GzipFile(self.filename, mode) as f:
186 self.assertTrue(hasattr(f, "name"))
187 self.assertEqual(f.name, self.filename)
Lars Gustäbel5b1a7852007-02-13 16:09:24 +0000188
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000189 def test_mtime(self):
190 mtime = 123456789
Brian Curtin31cf8d02010-10-13 23:51:19 +0000191 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
192 fWrite.write(data1)
193 with gzip.GzipFile(self.filename) as fRead:
194 dataRead = fRead.read()
195 self.assertEqual(dataRead, data1)
196 self.assertTrue(hasattr(fRead, 'mtime'))
197 self.assertEqual(fRead.mtime, mtime)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000198
199 def test_metadata(self):
200 mtime = 123456789
201
Brian Curtin31cf8d02010-10-13 23:51:19 +0000202 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
203 fWrite.write(data1)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000204
Brian Curtin31cf8d02010-10-13 23:51:19 +0000205 with open(self.filename, 'rb') as fRead:
206 # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000207
Brian Curtin31cf8d02010-10-13 23:51:19 +0000208 idBytes = fRead.read(2)
209 self.assertEqual(idBytes, '\x1f\x8b') # gzip ID
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000210
Brian Curtin31cf8d02010-10-13 23:51:19 +0000211 cmByte = fRead.read(1)
212 self.assertEqual(cmByte, '\x08') # deflate
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000213
Brian Curtin31cf8d02010-10-13 23:51:19 +0000214 flagsByte = fRead.read(1)
215 self.assertEqual(flagsByte, '\x08') # only the FNAME flag is set
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000216
Brian Curtin31cf8d02010-10-13 23:51:19 +0000217 mtimeBytes = fRead.read(4)
218 self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000219
Brian Curtin31cf8d02010-10-13 23:51:19 +0000220 xflByte = fRead.read(1)
221 self.assertEqual(xflByte, '\x02') # maximum compression
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000222
Brian Curtin31cf8d02010-10-13 23:51:19 +0000223 osByte = fRead.read(1)
224 self.assertEqual(osByte, '\xff') # OS "unknown" (OS-independent)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000225
Brian Curtin31cf8d02010-10-13 23:51:19 +0000226 # Since the FNAME flag is set, the zero-terminated filename follows.
227 # RFC 1952 specifies that this is the name of the input file, if any.
228 # However, the gzip module defaults to storing the name of the output
229 # file in this field.
230 expected = self.filename.encode('Latin-1') + '\x00'
231 nameBytes = fRead.read(len(expected))
232 self.assertEqual(nameBytes, expected)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000233
Brian Curtin31cf8d02010-10-13 23:51:19 +0000234 # Since no other flags were set, the header ends here.
235 # Rather than process the compressed data, let's seek to the trailer.
236 fRead.seek(os.stat(self.filename).st_size - 8)
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000237
Brian Curtin31cf8d02010-10-13 23:51:19 +0000238 crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
239 self.assertEqual(crc32Bytes, '\xaf\xd7d\x83')
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000240
Brian Curtin31cf8d02010-10-13 23:51:19 +0000241 isizeBytes = fRead.read(4)
242 self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000243
Antoine Pitroub74fc2b2009-01-10 16:13:45 +0000244 def test_with_open(self):
245 # GzipFile supports the context management protocol
246 with gzip.GzipFile(self.filename, "wb") as f:
247 f.write(b"xxx")
248 f = gzip.GzipFile(self.filename, "rb")
249 f.close()
250 try:
251 with f:
252 pass
253 except ValueError:
254 pass
255 else:
256 self.fail("__enter__ on a closed file didn't raise an exception")
257 try:
258 with gzip.GzipFile(self.filename, "wb") as f:
Ezio Melottidde5b942010-02-03 05:37:26 +0000259 1 // 0
Antoine Pitroub74fc2b2009-01-10 16:13:45 +0000260 except ZeroDivisionError:
261 pass
262 else:
Ezio Melottidde5b942010-02-03 05:37:26 +0000263 self.fail("1 // 0 didn't raise an exception")
Antoine Pitroub74fc2b2009-01-10 16:13:45 +0000264
Antoine Pitrou5a9112c2010-01-13 14:32:10 +0000265 def test_zero_padded_file(self):
266 with gzip.GzipFile(self.filename, "wb") as f:
267 f.write(data1 * 50)
268
269 # Pad the file with zeroes
270 with open(self.filename, "ab") as f:
271 f.write("\x00" * 50)
272
273 with gzip.GzipFile(self.filename, "rb") as f:
274 d = f.read()
275 self.assertEqual(d, data1 * 50, "Incorrect data in file")
276
Nadeem Vawdad7664de2012-01-19 00:40:46 +0200277 def test_fileobj_from_fdopen(self):
278 # Issue #13781: Creating a GzipFile using a fileobj from os.fdopen()
279 # should not embed the fake filename "<fdopen>" in the output file.
280 fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT)
281 with os.fdopen(fd, "wb") as f:
282 with gzip.GzipFile(fileobj=f, mode="w") as g:
283 self.assertEqual(g.name, "")
284
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000285def test_main(verbose=None):
286 test_support.run_unittest(TestGzip)
287
288if __name__ == "__main__":
289 test_main(verbose=True)