blob: 2ddebaf95682659cd7f13dd02382a0f714ec6cde [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):
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000036 f = gzip.GzipFile(self.filename, 'wb') ; f.write(data1 * 50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000037
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000038 # Try flush and fileno.
39 f.flush()
40 f.fileno()
41 if hasattr(os, 'fsync'):
42 os.fsync(f.fileno())
43 f.close()
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000044
Georg Brandle08e3d02008-05-25 08:07:37 +000045 # Test multiple close() calls.
46 f.close()
47
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000048 def test_read(self):
49 self.test_write()
50 # Try reading.
51 f = gzip.GzipFile(self.filename, 'r') ; d = f.read() ; f.close()
52 self.assertEqual(d, data1*50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000053
Antoine Pitrou76a66aa2010-10-06 21:26:52 +000054 def test_io_on_closed_object(self):
55 # Test that I/O operations on closed GzipFile objects raise a
56 # ValueError, just like the corresponding functions on file objects.
57
58 # Write to a file, open it for reading, then close it.
59 self.test_write()
60 f = gzip.GzipFile(self.filename, 'r')
61 f.close()
62 with self.assertRaises(ValueError):
63 f.read(1)
64 with self.assertRaises(ValueError):
65 f.seek(0)
66 with self.assertRaises(ValueError):
67 f.tell()
68 # Open the file for writing, then close it.
69 f = gzip.GzipFile(self.filename, 'w')
70 f.close()
71 with self.assertRaises(ValueError):
72 f.write(b'')
73 with self.assertRaises(ValueError):
74 f.flush()
75
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000076 def test_append(self):
77 self.test_write()
78 # Append to the previous file
79 f = gzip.GzipFile(self.filename, 'ab') ; f.write(data2 * 15) ; f.close()
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000080
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000081 f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close()
82 self.assertEqual(d, (data1*50) + (data2*15))
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000083
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000084 def test_many_append(self):
85 # Bug #1074261 was triggered when reading a file that contained
86 # many, many members. Create such a file and verify that reading it
87 # works.
88 f = gzip.open(self.filename, 'wb', 9)
89 f.write('a')
90 f.close()
91 for i in range(0,200):
92 f = gzip.open(self.filename, "ab", 9) # append
93 f.write('a')
94 f.close()
95
96 # Try reading the file
97 zgfile = gzip.open(self.filename, "rb")
98 contents = ""
99 while 1:
100 ztxt = zgfile.read(8192)
101 contents += ztxt
102 if not ztxt: break
103 zgfile.close()
104 self.assertEquals(contents, 'a'*201)
105
Antoine Pitrou673ddf92010-01-03 22:29:56 +0000106 def test_buffered_reader(self):
107 # Issue #7471: a GzipFile can be wrapped in a BufferedReader for
108 # performance.
109 self.test_write()
110
111 f = gzip.GzipFile(self.filename, 'rb')
112 with io.BufferedReader(f) as r:
113 lines = [line for line in r]
114
115 self.assertEqual(lines, 50 * data1.splitlines(True))
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000116
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000117 def test_readline(self):
118 self.test_write()
119 # Try .readline() with varying line lengths
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000120
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000121 f = gzip.GzipFile(self.filename, 'rb')
122 line_length = 0
123 while 1:
124 L = f.readline(line_length)
125 if L == "" and line_length != 0: break
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000126 self.assertTrue(len(L) <= line_length)
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000127 line_length = (line_length + 1) % 50
128 f.close()
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
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000134 f = gzip.GzipFile(self.filename, 'rb')
135 L = f.readlines()
136 f.close()
Skip Montanaro12424bc2002-05-23 01:43:05 +0000137
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000138 f = gzip.GzipFile(self.filename, 'rb')
139 while 1:
140 L = f.readlines(150)
141 if L == []: break
142 f.close()
143
144 def test_seek_read(self):
145 self.test_write()
146 # Try seek, read test
147
148 f = gzip.GzipFile(self.filename)
149 while 1:
150 oldpos = f.tell()
151 line1 = f.readline()
152 if not line1: break
153 newpos = f.tell()
154 f.seek(oldpos) # negative seek
155 if len(line1)>10:
156 amount = 10
157 else:
158 amount = len(line1)
159 line2 = f.read(amount)
160 self.assertEqual(line1[:amount], line2)
161 f.seek(newpos) # positive seek
162 f.close()
163
Martin v. Löwis065f0c82006-11-12 10:41:39 +0000164 def test_seek_whence(self):
165 self.test_write()
166 # Try seek(whence=1), read test
167
168 f = gzip.GzipFile(self.filename)
169 f.read(10)
170 f.seek(10, whence=1)
171 y = f.read(10)
172 f.close()
173 self.assertEquals(y, data1[20:30])
Tim Petersf733abb2007-01-30 03:03:46 +0000174
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000175 def test_seek_write(self):
176 # Try seek, write test
177 f = gzip.GzipFile(self.filename, 'w')
178 for pos in range(0, 256, 16):
179 f.seek(pos)
180 f.write('GZ\n')
181 f.close()
182
183 def test_mode(self):
184 self.test_write()
185 f = gzip.GzipFile(self.filename, 'r')
186 self.assertEqual(f.myfileobj.mode, 'rb')
187 f.close()
188
Lars Gustäbel5b1a7852007-02-13 16:09:24 +0000189 def test_1647484(self):
190 for mode in ('wb', 'rb'):
191 f = gzip.GzipFile(self.filename, mode)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000192 self.assertTrue(hasattr(f, "name"))
Lars Gustäbel5b1a7852007-02-13 16:09:24 +0000193 self.assertEqual(f.name, self.filename)
194 f.close()
195
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000196 def test_mtime(self):
197 mtime = 123456789
198 fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
199 fWrite.write(data1)
200 fWrite.close()
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000201 fRead = gzip.GzipFile(self.filename)
202 dataRead = fRead.read()
203 self.assertEqual(dataRead, data1)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000204 self.assertTrue(hasattr(fRead, 'mtime'))
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +0000205 self.assertEqual(fRead.mtime, mtime)
206 fRead.close()
207
208 def test_metadata(self):
209 mtime = 123456789
210
211 fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
212 fWrite.write(data1)
213 fWrite.close()
214
215 fRead = open(self.filename, 'rb')
216
217 # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
218
219 idBytes = fRead.read(2)
220 self.assertEqual(idBytes, '\x1f\x8b') # gzip ID
221
222 cmByte = fRead.read(1)
223 self.assertEqual(cmByte, '\x08') # deflate
224
225 flagsByte = fRead.read(1)
226 self.assertEqual(flagsByte, '\x08') # only the FNAME flag is set
227
228 mtimeBytes = fRead.read(4)
229 self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
230
231 xflByte = fRead.read(1)
232 self.assertEqual(xflByte, '\x02') # maximum compression
233
234 osByte = fRead.read(1)
235 self.assertEqual(osByte, '\xff') # OS "unknown" (OS-independent)
236
237 # Since the FNAME flag is set, the zero-terminated filename follows.
238 # RFC 1952 specifies that this is the name of the input file, if any.
239 # However, the gzip module defaults to storing the name of the output
240 # file in this field.
241 nameBytes = fRead.read(len(self.filename) + 1)
242 self.assertEqual(nameBytes, self.filename + '\x00')
243
244 # Since no other flags were set, the header ends here.
245 # Rather than process the compressed data, let's seek to the trailer.
246 fRead.seek(os.stat(self.filename).st_size - 8)
247
248 crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
249 self.assertEqual(crc32Bytes, '\xaf\xd7d\x83')
250
251 isizeBytes = fRead.read(4)
252 self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
253
254 fRead.close()
255
Antoine Pitroub74fc2b2009-01-10 16:13:45 +0000256 def test_with_open(self):
257 # GzipFile supports the context management protocol
258 with gzip.GzipFile(self.filename, "wb") as f:
259 f.write(b"xxx")
260 f = gzip.GzipFile(self.filename, "rb")
261 f.close()
262 try:
263 with f:
264 pass
265 except ValueError:
266 pass
267 else:
268 self.fail("__enter__ on a closed file didn't raise an exception")
269 try:
270 with gzip.GzipFile(self.filename, "wb") as f:
Ezio Melottidde5b942010-02-03 05:37:26 +0000271 1 // 0
Antoine Pitroub74fc2b2009-01-10 16:13:45 +0000272 except ZeroDivisionError:
273 pass
274 else:
Ezio Melottidde5b942010-02-03 05:37:26 +0000275 self.fail("1 // 0 didn't raise an exception")
Antoine Pitroub74fc2b2009-01-10 16:13:45 +0000276
Antoine Pitrou5a9112c2010-01-13 14:32:10 +0000277 def test_zero_padded_file(self):
278 with gzip.GzipFile(self.filename, "wb") as f:
279 f.write(data1 * 50)
280
281 # Pad the file with zeroes
282 with open(self.filename, "ab") as f:
283 f.write("\x00" * 50)
284
285 with gzip.GzipFile(self.filename, "rb") as f:
286 d = f.read()
287 self.assertEqual(d, data1 * 50, "Incorrect data in file")
288
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000289def test_main(verbose=None):
290 test_support.run_unittest(TestGzip)
291
292if __name__ == "__main__":
293 test_main(verbose=True)