Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 2 | """Test script for the gzip module. |
| 3 | """ |
| 4 | |
| 5 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 6 | from test import support |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 7 | import os |
Antoine Pitrou | b1f8835 | 2010-01-03 22:37:40 +0000 | [diff] [blame] | 8 | import io |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 9 | import struct |
Ezio Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 10 | gzip = support.import_module('gzip') |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 11 | |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 12 | data1 = b""" int length=DEFAULTALLOC, err = Z_OK; |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 13 | PyObject *RetVal; |
| 14 | int flushmode = Z_FINISH; |
| 15 | unsigned long start_total_out; |
| 16 | |
| 17 | """ |
| 18 | |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 19 | data2 = b"""/* zlibmodule.c -- gzip-compatible data compression */ |
Neal Norwitz | 014f103 | 2004-07-29 03:55:56 +0000 | [diff] [blame] | 20 | /* See http://www.gzip.org/zlib/ |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 21 | /* See http://www.winimage.com/zLibDll for Windows */ |
| 22 | """ |
| 23 | |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 24 | |
Antoine Pitrou | 7b96984 | 2010-09-23 16:22:51 +0000 | [diff] [blame] | 25 | class UnseekableIO(io.BytesIO): |
| 26 | def seekable(self): |
| 27 | return False |
| 28 | |
| 29 | def tell(self): |
| 30 | raise io.UnsupportedOperation |
| 31 | |
| 32 | def seek(self, *args): |
| 33 | raise io.UnsupportedOperation |
| 34 | |
| 35 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 36 | class TestGzip(unittest.TestCase): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 37 | filename = support.TESTFN |
Tim Peters | 5cfb05e | 2004-07-27 21:02:02 +0000 | [diff] [blame] | 38 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 39 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 40 | support.unlink(self.filename) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 41 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 42 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 43 | support.unlink(self.filename) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 44 | |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 45 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 46 | def test_write(self): |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 47 | f = gzip.GzipFile(self.filename, 'wb') ; f.write(data1 * 50) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 48 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 49 | # Try flush and fileno. |
| 50 | f.flush() |
| 51 | f.fileno() |
| 52 | if hasattr(os, 'fsync'): |
| 53 | os.fsync(f.fileno()) |
| 54 | f.close() |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 55 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 56 | # Test multiple close() calls. |
| 57 | f.close() |
| 58 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 59 | def test_read(self): |
| 60 | self.test_write() |
| 61 | # Try reading. |
| 62 | f = gzip.GzipFile(self.filename, 'r') ; d = f.read() ; f.close() |
| 63 | self.assertEqual(d, data1*50) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 64 | |
Antoine Pitrou | 7980eaa | 2010-10-06 21:21:18 +0000 | [diff] [blame^] | 65 | def test_io_on_closed_object(self): |
| 66 | # Test that I/O operations on closed GzipFile objects raise a |
| 67 | # ValueError, just like the corresponding functions on file objects. |
| 68 | |
| 69 | # Write to a file, open it for reading, then close it. |
| 70 | self.test_write() |
| 71 | f = gzip.GzipFile(self.filename, 'r') |
| 72 | f.close() |
| 73 | with self.assertRaises(ValueError): |
| 74 | f.read(1) |
| 75 | with self.assertRaises(ValueError): |
| 76 | f.seek(0) |
| 77 | with self.assertRaises(ValueError): |
| 78 | f.tell() |
| 79 | # Open the file for writing, then close it. |
| 80 | f = gzip.GzipFile(self.filename, 'w') |
| 81 | f.close() |
| 82 | with self.assertRaises(ValueError): |
| 83 | f.write(b'') |
| 84 | with self.assertRaises(ValueError): |
| 85 | f.flush() |
| 86 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 87 | def test_append(self): |
| 88 | self.test_write() |
| 89 | # Append to the previous file |
| 90 | f = gzip.GzipFile(self.filename, 'ab') ; f.write(data2 * 15) ; f.close() |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 91 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 92 | f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close() |
| 93 | self.assertEqual(d, (data1*50) + (data2*15)) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 94 | |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 95 | def test_many_append(self): |
| 96 | # Bug #1074261 was triggered when reading a file that contained |
| 97 | # many, many members. Create such a file and verify that reading it |
| 98 | # works. |
| 99 | f = gzip.open(self.filename, 'wb', 9) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 100 | f.write(b'a') |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 101 | f.close() |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 102 | for i in range(0, 200): |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 103 | f = gzip.open(self.filename, "ab", 9) # append |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 104 | f.write(b'a') |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 105 | f.close() |
| 106 | |
| 107 | # Try reading the file |
| 108 | zgfile = gzip.open(self.filename, "rb") |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 109 | contents = b"" |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 110 | while 1: |
| 111 | ztxt = zgfile.read(8192) |
| 112 | contents += ztxt |
| 113 | if not ztxt: break |
| 114 | zgfile.close() |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 115 | self.assertEquals(contents, b'a'*201) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | b1f8835 | 2010-01-03 22:37:40 +0000 | [diff] [blame] | 117 | def test_buffered_reader(self): |
| 118 | # Issue #7471: a GzipFile can be wrapped in a BufferedReader for |
| 119 | # performance. |
| 120 | self.test_write() |
| 121 | |
| 122 | f = gzip.GzipFile(self.filename, 'rb') |
| 123 | with io.BufferedReader(f) as r: |
| 124 | lines = [line for line in r] |
| 125 | |
| 126 | self.assertEqual(lines, 50 * data1.splitlines(True)) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 127 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 128 | def test_readline(self): |
| 129 | self.test_write() |
| 130 | # Try .readline() with varying line lengths |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 131 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 132 | f = gzip.GzipFile(self.filename, 'rb') |
| 133 | line_length = 0 |
| 134 | while 1: |
| 135 | L = f.readline(line_length) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 136 | if not L and line_length != 0: break |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 137 | self.assertTrue(len(L) <= line_length) |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 138 | line_length = (line_length + 1) % 50 |
| 139 | f.close() |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 140 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 141 | def test_readlines(self): |
| 142 | self.test_write() |
| 143 | # Try .readlines() |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 144 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 145 | f = gzip.GzipFile(self.filename, 'rb') |
| 146 | L = f.readlines() |
| 147 | f.close() |
Skip Montanaro | 12424bc | 2002-05-23 01:43:05 +0000 | [diff] [blame] | 148 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 149 | f = gzip.GzipFile(self.filename, 'rb') |
| 150 | while 1: |
| 151 | L = f.readlines(150) |
| 152 | if L == []: break |
| 153 | f.close() |
| 154 | |
| 155 | def test_seek_read(self): |
| 156 | self.test_write() |
| 157 | # Try seek, read test |
| 158 | |
| 159 | f = gzip.GzipFile(self.filename) |
| 160 | while 1: |
| 161 | oldpos = f.tell() |
| 162 | line1 = f.readline() |
| 163 | if not line1: break |
| 164 | newpos = f.tell() |
| 165 | f.seek(oldpos) # negative seek |
| 166 | if len(line1)>10: |
| 167 | amount = 10 |
| 168 | else: |
| 169 | amount = len(line1) |
| 170 | line2 = f.read(amount) |
| 171 | self.assertEqual(line1[:amount], line2) |
| 172 | f.seek(newpos) # positive seek |
| 173 | f.close() |
| 174 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 175 | def test_seek_whence(self): |
| 176 | self.test_write() |
| 177 | # Try seek(whence=1), read test |
| 178 | |
| 179 | f = gzip.GzipFile(self.filename) |
| 180 | f.read(10) |
| 181 | f.seek(10, whence=1) |
| 182 | y = f.read(10) |
| 183 | f.close() |
| 184 | self.assertEquals(y, data1[20:30]) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 185 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 186 | def test_seek_write(self): |
| 187 | # Try seek, write test |
| 188 | f = gzip.GzipFile(self.filename, 'w') |
| 189 | for pos in range(0, 256, 16): |
| 190 | f.seek(pos) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 191 | f.write(b'GZ\n') |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 192 | f.close() |
| 193 | |
| 194 | def test_mode(self): |
| 195 | self.test_write() |
| 196 | f = gzip.GzipFile(self.filename, 'r') |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 197 | self.assertEqual(f.myfileobj.mode, 'rb') |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 198 | f.close() |
| 199 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 200 | def test_1647484(self): |
| 201 | for mode in ('wb', 'rb'): |
| 202 | f = gzip.GzipFile(self.filename, mode) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 203 | self.assertTrue(hasattr(f, "name")) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 204 | self.assertEqual(f.name, self.filename) |
| 205 | f.close() |
| 206 | |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 207 | def test_mtime(self): |
| 208 | mtime = 123456789 |
| 209 | fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime) |
| 210 | fWrite.write(data1) |
| 211 | fWrite.close() |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 212 | fRead = gzip.GzipFile(self.filename) |
| 213 | dataRead = fRead.read() |
| 214 | self.assertEqual(dataRead, data1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 215 | self.assertTrue(hasattr(fRead, 'mtime')) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 216 | self.assertEqual(fRead.mtime, mtime) |
| 217 | fRead.close() |
| 218 | |
| 219 | def test_metadata(self): |
| 220 | mtime = 123456789 |
| 221 | |
| 222 | fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime) |
| 223 | fWrite.write(data1) |
| 224 | fWrite.close() |
| 225 | |
| 226 | fRead = open(self.filename, 'rb') |
| 227 | |
| 228 | # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html |
| 229 | |
| 230 | idBytes = fRead.read(2) |
| 231 | self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID |
| 232 | |
| 233 | cmByte = fRead.read(1) |
| 234 | self.assertEqual(cmByte, b'\x08') # deflate |
| 235 | |
| 236 | flagsByte = fRead.read(1) |
| 237 | self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set |
| 238 | |
| 239 | mtimeBytes = fRead.read(4) |
| 240 | self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian |
| 241 | |
| 242 | xflByte = fRead.read(1) |
| 243 | self.assertEqual(xflByte, b'\x02') # maximum compression |
| 244 | |
| 245 | osByte = fRead.read(1) |
| 246 | self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent) |
| 247 | |
| 248 | # Since the FNAME flag is set, the zero-terminated filename follows. |
| 249 | # RFC 1952 specifies that this is the name of the input file, if any. |
| 250 | # However, the gzip module defaults to storing the name of the output |
| 251 | # file in this field. |
| 252 | expected = self.filename.encode('Latin-1') + b'\x00' |
| 253 | nameBytes = fRead.read(len(expected)) |
| 254 | self.assertEqual(nameBytes, expected) |
| 255 | |
| 256 | # Since no other flags were set, the header ends here. |
| 257 | # Rather than process the compressed data, let's seek to the trailer. |
| 258 | fRead.seek(os.stat(self.filename).st_size - 8) |
| 259 | |
| 260 | crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1] |
| 261 | self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83') |
| 262 | |
| 263 | isizeBytes = fRead.read(4) |
| 264 | self.assertEqual(isizeBytes, struct.pack('<i', len(data1))) |
| 265 | |
| 266 | fRead.close() |
| 267 | |
Antoine Pitrou | 308705e | 2009-01-10 16:22:51 +0000 | [diff] [blame] | 268 | def test_with_open(self): |
| 269 | # GzipFile supports the context management protocol |
| 270 | with gzip.GzipFile(self.filename, "wb") as f: |
| 271 | f.write(b"xxx") |
| 272 | f = gzip.GzipFile(self.filename, "rb") |
| 273 | f.close() |
| 274 | try: |
| 275 | with f: |
| 276 | pass |
| 277 | except ValueError: |
| 278 | pass |
| 279 | else: |
| 280 | self.fail("__enter__ on a closed file didn't raise an exception") |
| 281 | try: |
| 282 | with gzip.GzipFile(self.filename, "wb") as f: |
| 283 | 1/0 |
| 284 | except ZeroDivisionError: |
| 285 | pass |
| 286 | else: |
| 287 | self.fail("1/0 didn't raise an exception") |
| 288 | |
Antoine Pitrou | 8e33fd7 | 2010-01-13 14:37:26 +0000 | [diff] [blame] | 289 | def test_zero_padded_file(self): |
| 290 | with gzip.GzipFile(self.filename, "wb") as f: |
| 291 | f.write(data1 * 50) |
| 292 | |
| 293 | # Pad the file with zeroes |
| 294 | with open(self.filename, "ab") as f: |
| 295 | f.write(b"\x00" * 50) |
| 296 | |
| 297 | with gzip.GzipFile(self.filename, "rb") as f: |
| 298 | d = f.read() |
| 299 | self.assertEqual(d, data1 * 50, "Incorrect data in file") |
| 300 | |
Antoine Pitrou | 7b96984 | 2010-09-23 16:22:51 +0000 | [diff] [blame] | 301 | def test_non_seekable_file(self): |
| 302 | uncompressed = data1 * 50 |
| 303 | buf = UnseekableIO() |
| 304 | with gzip.GzipFile(fileobj=buf, mode="wb") as f: |
| 305 | f.write(uncompressed) |
| 306 | compressed = buf.getvalue() |
| 307 | buf = UnseekableIO(compressed) |
| 308 | with gzip.GzipFile(fileobj=buf, mode="rb") as f: |
| 309 | self.assertEqual(f.read(), uncompressed) |
| 310 | |
Antoine Pitrou | c3ed2e7 | 2010-09-29 10:49:46 +0000 | [diff] [blame] | 311 | def test_peek(self): |
| 312 | uncompressed = data1 * 200 |
| 313 | with gzip.GzipFile(self.filename, "wb") as f: |
| 314 | f.write(uncompressed) |
| 315 | |
| 316 | def sizes(): |
| 317 | while True: |
| 318 | for n in range(5, 50, 10): |
| 319 | yield n |
| 320 | |
| 321 | with gzip.GzipFile(self.filename, "rb") as f: |
| 322 | f.max_read_chunk = 33 |
| 323 | nread = 0 |
| 324 | for n in sizes(): |
| 325 | s = f.peek(n) |
| 326 | if s == b'': |
| 327 | break |
| 328 | self.assertEqual(f.read(len(s)), s) |
| 329 | nread += len(s) |
| 330 | self.assertEqual(f.read(100), b'') |
| 331 | self.assertEqual(nread, len(uncompressed)) |
| 332 | |
Antoine Pitrou | 79c5ef1 | 2010-08-17 21:10:05 +0000 | [diff] [blame] | 333 | # Testing compress/decompress shortcut functions |
| 334 | |
| 335 | def test_compress(self): |
| 336 | for data in [data1, data2]: |
| 337 | for args in [(), (1,), (6,), (9,)]: |
| 338 | datac = gzip.compress(data, *args) |
| 339 | self.assertEqual(type(datac), bytes) |
| 340 | with gzip.GzipFile(fileobj=io.BytesIO(datac), mode="rb") as f: |
| 341 | self.assertEqual(f.read(), data) |
| 342 | |
| 343 | def test_decompress(self): |
| 344 | for data in (data1, data2): |
| 345 | buf = io.BytesIO() |
| 346 | with gzip.GzipFile(fileobj=buf, mode="wb") as f: |
| 347 | f.write(data) |
| 348 | self.assertEqual(gzip.decompress(buf.getvalue()), data) |
| 349 | # Roundtrip with compress |
| 350 | datac = gzip.compress(data) |
| 351 | self.assertEqual(gzip.decompress(datac), data) |
| 352 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 353 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 354 | support.run_unittest(TestGzip) |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 355 | |
| 356 | if __name__ == "__main__": |
| 357 | test_main(verbose=True) |