blob: 7eade6f60a68cd4959648d00f86b3e368b8c723e [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
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000025class TestGzip(unittest.TestCase):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000026 filename = support.TESTFN
Tim Peters5cfb05e2004-07-27 21:02:02 +000027
Georg Brandlb533e262008-05-25 18:19:30 +000028 def setUp(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000029 support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000030
Georg Brandlb533e262008-05-25 18:19:30 +000031 def tearDown(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000032 support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000033
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000034
Georg Brandlb533e262008-05-25 18:19:30 +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 Brandlb533e262008-05-25 18:19:30 +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
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000054 def test_append(self):
55 self.test_write()
56 # Append to the previous file
57 f = gzip.GzipFile(self.filename, 'ab') ; f.write(data2 * 15) ; f.close()
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000058
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000059 f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close()
60 self.assertEqual(d, (data1*50) + (data2*15))
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000061
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000062 def test_many_append(self):
63 # Bug #1074261 was triggered when reading a file that contained
64 # many, many members. Create such a file and verify that reading it
65 # works.
66 f = gzip.open(self.filename, 'wb', 9)
Walter Dörwald5b1284d2007-06-06 16:43:59 +000067 f.write(b'a')
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000068 f.close()
Walter Dörwald5b1284d2007-06-06 16:43:59 +000069 for i in range(0, 200):
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000070 f = gzip.open(self.filename, "ab", 9) # append
Walter Dörwald5b1284d2007-06-06 16:43:59 +000071 f.write(b'a')
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000072 f.close()
73
74 # Try reading the file
75 zgfile = gzip.open(self.filename, "rb")
Walter Dörwald5b1284d2007-06-06 16:43:59 +000076 contents = b""
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000077 while 1:
78 ztxt = zgfile.read(8192)
79 contents += ztxt
80 if not ztxt: break
81 zgfile.close()
Walter Dörwald5b1284d2007-06-06 16:43:59 +000082 self.assertEquals(contents, b'a'*201)
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000083
Antoine Pitroub1f88352010-01-03 22:37:40 +000084 def test_buffered_reader(self):
85 # Issue #7471: a GzipFile can be wrapped in a BufferedReader for
86 # performance.
87 self.test_write()
88
89 f = gzip.GzipFile(self.filename, 'rb')
90 with io.BufferedReader(f) as r:
91 lines = [line for line in r]
92
93 self.assertEqual(lines, 50 * data1.splitlines(True))
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000094
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000095 def test_readline(self):
96 self.test_write()
97 # Try .readline() with varying line lengths
Martin v. Löwis8cc965c2001-08-09 07:21:56 +000098
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000099 f = gzip.GzipFile(self.filename, 'rb')
100 line_length = 0
101 while 1:
102 L = f.readline(line_length)
Walter Dörwald5b1284d2007-06-06 16:43:59 +0000103 if not L and line_length != 0: break
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000104 self.assertTrue(len(L) <= line_length)
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000105 line_length = (line_length + 1) % 50
106 f.close()
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000107
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000108 def test_readlines(self):
109 self.test_write()
110 # Try .readlines()
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +0000111
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000112 f = gzip.GzipFile(self.filename, 'rb')
113 L = f.readlines()
114 f.close()
Skip Montanaro12424bc2002-05-23 01:43:05 +0000115
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000116 f = gzip.GzipFile(self.filename, 'rb')
117 while 1:
118 L = f.readlines(150)
119 if L == []: break
120 f.close()
121
122 def test_seek_read(self):
123 self.test_write()
124 # Try seek, read test
125
126 f = gzip.GzipFile(self.filename)
127 while 1:
128 oldpos = f.tell()
129 line1 = f.readline()
130 if not line1: break
131 newpos = f.tell()
132 f.seek(oldpos) # negative seek
133 if len(line1)>10:
134 amount = 10
135 else:
136 amount = len(line1)
137 line2 = f.read(amount)
138 self.assertEqual(line1[:amount], line2)
139 f.seek(newpos) # positive seek
140 f.close()
141
Thomas Wouters89f507f2006-12-13 04:49:30 +0000142 def test_seek_whence(self):
143 self.test_write()
144 # Try seek(whence=1), read test
145
146 f = gzip.GzipFile(self.filename)
147 f.read(10)
148 f.seek(10, whence=1)
149 y = f.read(10)
150 f.close()
151 self.assertEquals(y, data1[20:30])
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000152
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000153 def test_seek_write(self):
154 # Try seek, write test
155 f = gzip.GzipFile(self.filename, 'w')
156 for pos in range(0, 256, 16):
157 f.seek(pos)
Walter Dörwald5b1284d2007-06-06 16:43:59 +0000158 f.write(b'GZ\n')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000159 f.close()
160
161 def test_mode(self):
162 self.test_write()
163 f = gzip.GzipFile(self.filename, 'r')
Benjamin Peterson44309e62008-11-22 00:41:45 +0000164 self.assertEqual(f.myfileobj.mode, 'rb')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000165 f.close()
166
Thomas Wouterscf297e42007-02-23 15:07:44 +0000167 def test_1647484(self):
168 for mode in ('wb', 'rb'):
169 f = gzip.GzipFile(self.filename, mode)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000170 self.assertTrue(hasattr(f, "name"))
Thomas Wouterscf297e42007-02-23 15:07:44 +0000171 self.assertEqual(f.name, self.filename)
172 f.close()
173
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000174 def test_mtime(self):
175 mtime = 123456789
176 fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
177 fWrite.write(data1)
178 fWrite.close()
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000179 fRead = gzip.GzipFile(self.filename)
180 dataRead = fRead.read()
181 self.assertEqual(dataRead, data1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000182 self.assertTrue(hasattr(fRead, 'mtime'))
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000183 self.assertEqual(fRead.mtime, mtime)
184 fRead.close()
185
186 def test_metadata(self):
187 mtime = 123456789
188
189 fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
190 fWrite.write(data1)
191 fWrite.close()
192
193 fRead = open(self.filename, 'rb')
194
195 # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
196
197 idBytes = fRead.read(2)
198 self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID
199
200 cmByte = fRead.read(1)
201 self.assertEqual(cmByte, b'\x08') # deflate
202
203 flagsByte = fRead.read(1)
204 self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set
205
206 mtimeBytes = fRead.read(4)
207 self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
208
209 xflByte = fRead.read(1)
210 self.assertEqual(xflByte, b'\x02') # maximum compression
211
212 osByte = fRead.read(1)
213 self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent)
214
215 # Since the FNAME flag is set, the zero-terminated filename follows.
216 # RFC 1952 specifies that this is the name of the input file, if any.
217 # However, the gzip module defaults to storing the name of the output
218 # file in this field.
219 expected = self.filename.encode('Latin-1') + b'\x00'
220 nameBytes = fRead.read(len(expected))
221 self.assertEqual(nameBytes, expected)
222
223 # Since no other flags were set, the header ends here.
224 # Rather than process the compressed data, let's seek to the trailer.
225 fRead.seek(os.stat(self.filename).st_size - 8)
226
227 crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
228 self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83')
229
230 isizeBytes = fRead.read(4)
231 self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
232
233 fRead.close()
234
Antoine Pitrou308705e2009-01-10 16:22:51 +0000235 def test_with_open(self):
236 # GzipFile supports the context management protocol
237 with gzip.GzipFile(self.filename, "wb") as f:
238 f.write(b"xxx")
239 f = gzip.GzipFile(self.filename, "rb")
240 f.close()
241 try:
242 with f:
243 pass
244 except ValueError:
245 pass
246 else:
247 self.fail("__enter__ on a closed file didn't raise an exception")
248 try:
249 with gzip.GzipFile(self.filename, "wb") as f:
250 1/0
251 except ZeroDivisionError:
252 pass
253 else:
254 self.fail("1/0 didn't raise an exception")
255
Antoine Pitrou8e33fd72010-01-13 14:37:26 +0000256 def test_zero_padded_file(self):
257 with gzip.GzipFile(self.filename, "wb") as f:
258 f.write(data1 * 50)
259
260 # Pad the file with zeroes
261 with open(self.filename, "ab") as f:
262 f.write(b"\x00" * 50)
263
264 with gzip.GzipFile(self.filename, "rb") as f:
265 d = f.read()
266 self.assertEqual(d, data1 * 50, "Incorrect data in file")
267
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000268def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000269 support.run_unittest(TestGzip)
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000270
271if __name__ == "__main__":
272 test_main(verbose=True)