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 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 25 | class TestGzip(unittest.TestCase): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 26 | filename = support.TESTFN |
Tim Peters | 5cfb05e | 2004-07-27 21:02:02 +0000 | [diff] [blame] | 27 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 28 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 29 | support.unlink(self.filename) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 30 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 31 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 32 | support.unlink(self.filename) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 33 | |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 34 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 35 | def test_write(self): |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 36 | f = gzip.GzipFile(self.filename, 'wb') ; f.write(data1 * 50) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 37 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 38 | # 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. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 44 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 45 | # Test multiple close() calls. |
| 46 | f.close() |
| 47 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 48 | 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. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 53 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 54 | 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. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 58 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 59 | f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close() |
| 60 | self.assertEqual(d, (data1*50) + (data2*15)) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 61 | |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 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 |
| 65 | # works. |
| 66 | f = gzip.open(self.filename, 'wb', 9) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 67 | f.write(b'a') |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 68 | f.close() |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 69 | for i in range(0, 200): |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 70 | f = gzip.open(self.filename, "ab", 9) # append |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 71 | f.write(b'a') |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 72 | f.close() |
| 73 | |
| 74 | # Try reading the file |
| 75 | zgfile = gzip.open(self.filename, "rb") |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 76 | contents = b"" |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 77 | while 1: |
| 78 | ztxt = zgfile.read(8192) |
| 79 | contents += ztxt |
| 80 | if not ztxt: break |
| 81 | zgfile.close() |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 82 | self.assertEquals(contents, b'a'*201) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 83 | |
Antoine Pitrou | b1f8835 | 2010-01-03 22:37:40 +0000 | [diff] [blame] | 84 | 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. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 94 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 95 | def test_readline(self): |
| 96 | self.test_write() |
| 97 | # Try .readline() with varying line lengths |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 98 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 99 | f = gzip.GzipFile(self.filename, 'rb') |
| 100 | line_length = 0 |
| 101 | while 1: |
| 102 | L = f.readline(line_length) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 103 | if not L and line_length != 0: break |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 104 | self.assertTrue(len(L) <= line_length) |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 105 | line_length = (line_length + 1) % 50 |
| 106 | f.close() |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 107 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 108 | def test_readlines(self): |
| 109 | self.test_write() |
| 110 | # Try .readlines() |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 111 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 112 | f = gzip.GzipFile(self.filename, 'rb') |
| 113 | L = f.readlines() |
| 114 | f.close() |
Skip Montanaro | 12424bc | 2002-05-23 01:43:05 +0000 | [diff] [blame] | 115 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 116 | 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 Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 142 | 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 Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 152 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 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): |
| 157 | f.seek(pos) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 158 | f.write(b'GZ\n') |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 159 | f.close() |
| 160 | |
| 161 | def test_mode(self): |
| 162 | self.test_write() |
| 163 | f = gzip.GzipFile(self.filename, 'r') |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 164 | self.assertEqual(f.myfileobj.mode, 'rb') |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 165 | f.close() |
| 166 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 167 | def test_1647484(self): |
| 168 | for mode in ('wb', 'rb'): |
| 169 | f = gzip.GzipFile(self.filename, mode) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 170 | self.assertTrue(hasattr(f, "name")) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 171 | self.assertEqual(f.name, self.filename) |
| 172 | f.close() |
| 173 | |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 174 | def test_mtime(self): |
| 175 | mtime = 123456789 |
| 176 | fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime) |
| 177 | fWrite.write(data1) |
| 178 | fWrite.close() |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 179 | fRead = gzip.GzipFile(self.filename) |
| 180 | dataRead = fRead.read() |
| 181 | self.assertEqual(dataRead, data1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 182 | self.assertTrue(hasattr(fRead, 'mtime')) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 183 | 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 Pitrou | 308705e | 2009-01-10 16:22:51 +0000 | [diff] [blame] | 235 | 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 Pitrou | 8e33fd7 | 2010-01-13 14:37:26 +0000 | [diff] [blame] | 256 | 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. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 268 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 269 | support.run_unittest(TestGzip) |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 270 | |
| 271 | if __name__ == "__main__": |
| 272 | test_main(verbose=True) |