2 """Test script for the gzip module.
6 from test
import test_support
10 gzip
= test_support
.import_module('gzip')
12 data1
= """ int length=DEFAULTALLOC, err = Z_OK;
14 int flushmode = Z_FINISH;
15 unsigned long start_total_out;
19 data2
= """/* 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
= test_support
.TESTFN
29 test_support
.unlink(self
.filename
)
32 test_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
, 'a'*201)
84 def test_buffered_reader(self
):
85 # Issue #7471: a GzipFile can be wrapped in a BufferedReader for
89 f
= gzip
.GzipFile(self
.filename
, 'rb')
90 with io
.BufferedReader(f
) as r
:
91 lines
= [line
for line
in r
]
93 self
.assertEqual(lines
, 50 * data1
.splitlines(True))
95 def test_readline(self
):
97 # Try .readline() with varying line lengths
99 f
= gzip
.GzipFile(self
.filename
, 'rb')
102 L
= f
.readline(line_length
)
103 if L
== "" and line_length
!= 0: break
104 self
.assertTrue(len(L
) <= line_length
)
105 line_length
= (line_length
+ 1) % 50
108 def test_readlines(self
):
112 f
= gzip
.GzipFile(self
.filename
, 'rb')
116 f
= gzip
.GzipFile(self
.filename
, 'rb')
122 def test_seek_read(self
):
124 # Try seek, read test
126 f
= gzip
.GzipFile(self
.filename
)
132 f
.seek(oldpos
) # negative seek
137 line2
= f
.read(amount
)
138 self
.assertEqual(line1
[:amount
], line2
)
139 f
.seek(newpos
) # positive seek
142 def test_seek_whence(self
):
144 # Try seek(whence=1), read test
146 f
= gzip
.GzipFile(self
.filename
)
151 self
.assertEquals(y
, data1
[20:30])
153 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):
163 f
= gzip
.GzipFile(self
.filename
, 'r')
164 self
.assertEqual(f
.myfileobj
.mode
, 'rb')
167 def test_1647484(self
):
168 for mode
in ('wb', 'rb'):
169 f
= gzip
.GzipFile(self
.filename
, mode
)
170 self
.assertTrue(hasattr(f
, "name"))
171 self
.assertEqual(f
.name
, self
.filename
)
174 def test_mtime(self
):
176 fWrite
= gzip
.GzipFile(self
.filename
, 'w', mtime
= mtime
)
179 fRead
= gzip
.GzipFile(self
.filename
)
180 dataRead
= fRead
.read()
181 self
.assertEqual(dataRead
, data1
)
182 self
.assertTrue(hasattr(fRead
, 'mtime'))
183 self
.assertEqual(fRead
.mtime
, mtime
)
186 def test_metadata(self
):
189 fWrite
= gzip
.GzipFile(self
.filename
, 'w', mtime
= mtime
)
193 fRead
= open(self
.filename
, 'rb')
195 # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
197 idBytes
= fRead
.read(2)
198 self
.assertEqual(idBytes
, '\x1f\x8b') # gzip ID
200 cmByte
= fRead
.read(1)
201 self
.assertEqual(cmByte
, '\x08') # deflate
203 flagsByte
= fRead
.read(1)
204 self
.assertEqual(flagsByte
, '\x08') # only the FNAME flag is set
206 mtimeBytes
= fRead
.read(4)
207 self
.assertEqual(mtimeBytes
, struct
.pack('<i', mtime
)) # little-endian
209 xflByte
= fRead
.read(1)
210 self
.assertEqual(xflByte
, '\x02') # maximum compression
212 osByte
= fRead
.read(1)
213 self
.assertEqual(osByte
, '\xff') # OS "unknown" (OS-independent)
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 nameBytes
= fRead
.read(len(self
.filename
) + 1)
220 self
.assertEqual(nameBytes
, self
.filename
+ '\x00')
222 # Since no other flags were set, the header ends here.
223 # Rather than process the compressed data, let's seek to the trailer.
224 fRead
.seek(os
.stat(self
.filename
).st_size
- 8)
226 crc32Bytes
= fRead
.read(4) # CRC32 of uncompressed data [data1]
227 self
.assertEqual(crc32Bytes
, '\xaf\xd7d\x83')
229 isizeBytes
= fRead
.read(4)
230 self
.assertEqual(isizeBytes
, struct
.pack('<i', len(data1
)))
234 def test_with_open(self
):
235 # GzipFile supports the context management protocol
236 with gzip
.GzipFile(self
.filename
, "wb") as f
:
238 f
= gzip
.GzipFile(self
.filename
, "rb")
246 self
.fail("__enter__ on a closed file didn't raise an exception")
248 with gzip
.GzipFile(self
.filename
, "wb") as f
:
250 except ZeroDivisionError:
253 self
.fail("1 // 0 didn't raise an exception")
255 def test_zero_padded_file(self
):
256 with gzip
.GzipFile(self
.filename
, "wb") as f
:
259 # Pad the file with zeroes
260 with
open(self
.filename
, "ab") as f
:
263 with gzip
.GzipFile(self
.filename
, "rb") as f
:
265 self
.assertEqual(d
, data1
* 50, "Incorrect data in file")
267 def test_main(verbose
=None):
268 test_support
.run_unittest(TestGzip
)
270 if __name__
== "__main__":
271 test_main(verbose
=True)