2
"""Test script for the gzip module.
6
from test import support
12
data1 = b""" int length=DEFAULTALLOC, err = Z_OK;
14
int flushmode = Z_FINISH;
15
unsigned long start_total_out;
19
data2 = b"""/* zlibmodule.c -- gzip-compatible data compression */
20
/* See http://www.gzip.org/zlib/
21
/* See http://www.winimage.com/zLibDll for Windows */
25
class TestGzip(unittest.TestCase):
26
filename = support.TESTFN
29
support.unlink(self.filename)
32
support.unlink(self.filename)
36
f = gzip.GzipFile(self.filename, 'wb') ; f.write(data1 * 50)
38
# Try flush and fileno.
41
if hasattr(os, 'fsync'):
45
# Test multiple close() calls.
51
f = gzip.GzipFile(self.filename, 'r') ; d = f.read() ; f.close()
52
self.assertEqual(d, data1*50)
54
def test_append(self):
56
# Append to the previous file
57
f = gzip.GzipFile(self.filename, 'ab') ; f.write(data2 * 15) ; f.close()
59
f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close()
60
self.assertEqual(d, (data1*50) + (data2*15))
62
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
66
f = gzip.open(self.filename, 'wb', 9)
69
for i in range(0, 200):
70
f = gzip.open(self.filename, "ab", 9) # append
74
# Try reading the file
75
zgfile = gzip.open(self.filename, "rb")
78
ztxt = zgfile.read(8192)
82
self.assertEquals(contents, b'a'*201)
85
def test_readline(self):
87
# Try .readline() with varying line lengths
89
f = gzip.GzipFile(self.filename, 'rb')
92
L = f.readline(line_length)
93
if not L and line_length != 0: break
94
self.assert_(len(L) <= line_length)
95
line_length = (line_length + 1) % 50
98
def test_readlines(self):
102
f = gzip.GzipFile(self.filename, 'rb')
106
f = gzip.GzipFile(self.filename, 'rb')
112
def test_seek_read(self):
114
# Try seek, read test
116
f = gzip.GzipFile(self.filename)
122
f.seek(oldpos) # negative seek
127
line2 = f.read(amount)
128
self.assertEqual(line1[:amount], line2)
129
f.seek(newpos) # positive seek
132
def test_seek_whence(self):
134
# Try seek(whence=1), read test
136
f = gzip.GzipFile(self.filename)
141
self.assertEquals(y, data1[20:30])
143
def test_seek_write(self):
144
# Try seek, write test
145
f = gzip.GzipFile(self.filename, 'w')
146
for pos in range(0, 256, 16):
153
f = gzip.GzipFile(self.filename, 'r')
154
self.assertEqual(f.myfileobj.mode, 'rb')
157
def test_1647484(self):
158
for mode in ('wb', 'rb'):
159
f = gzip.GzipFile(self.filename, mode)
160
self.assert_(hasattr(f, "name"))
161
self.assertEqual(f.name, self.filename)
164
def test_mtime(self):
166
fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
169
fRead = gzip.GzipFile(self.filename)
170
dataRead = fRead.read()
171
self.assertEqual(dataRead, data1)
172
self.assert_(hasattr(fRead, 'mtime'))
173
self.assertEqual(fRead.mtime, mtime)
176
def test_metadata(self):
179
fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
183
fRead = open(self.filename, 'rb')
185
# see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
187
idBytes = fRead.read(2)
188
self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID
190
cmByte = fRead.read(1)
191
self.assertEqual(cmByte, b'\x08') # deflate
193
flagsByte = fRead.read(1)
194
self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set
196
mtimeBytes = fRead.read(4)
197
self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
199
xflByte = fRead.read(1)
200
self.assertEqual(xflByte, b'\x02') # maximum compression
202
osByte = fRead.read(1)
203
self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent)
205
# Since the FNAME flag is set, the zero-terminated filename follows.
206
# RFC 1952 specifies that this is the name of the input file, if any.
207
# However, the gzip module defaults to storing the name of the output
208
# file in this field.
209
expected = self.filename.encode('Latin-1') + b'\x00'
210
nameBytes = fRead.read(len(expected))
211
self.assertEqual(nameBytes, expected)
213
# Since no other flags were set, the header ends here.
214
# Rather than process the compressed data, let's seek to the trailer.
215
fRead.seek(os.stat(self.filename).st_size - 8)
217
crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
218
self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83')
220
isizeBytes = fRead.read(4)
221
self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
225
def test_with_open(self):
226
# GzipFile supports the context management protocol
227
with gzip.GzipFile(self.filename, "wb") as f:
229
f = gzip.GzipFile(self.filename, "rb")
237
self.fail("__enter__ on a closed file didn't raise an exception")
239
with gzip.GzipFile(self.filename, "wb") as f:
241
except ZeroDivisionError:
244
self.fail("1/0 didn't raise an exception")
246
def test_main(verbose=None):
247
support.run_unittest(TestGzip)
249
if __name__ == "__main__":
250
test_main(verbose=True)