Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Test script for the gzip module. |
| 3 | """ |
| 4 | |
| 5 | import unittest |
| 6 | from test import test_support |
Christian Heimes | c5f05e4 | 2008-02-23 17:40:11 +0000 | [diff] [blame] | 7 | import os |
Antoine Pitrou | 673ddf9 | 2010-01-03 22:29:56 +0000 | [diff] [blame] | 8 | import io |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 9 | import struct |
Ezio Melotti | 1036a7f | 2009-09-12 14:43:43 +0000 | [diff] [blame] | 10 | gzip = test_support.import_module('gzip') |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 11 | |
| 12 | data1 = """ int length=DEFAULTALLOC, err = Z_OK; |
| 13 | PyObject *RetVal; |
| 14 | int flushmode = Z_FINISH; |
| 15 | unsigned long start_total_out; |
| 16 | |
| 17 | """ |
| 18 | |
| 19 | data2 = """/* 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): |
| 26 | filename = test_support.TESTFN |
Tim Peters | 5cfb05e | 2004-07-27 21:02:02 +0000 | [diff] [blame] | 27 | |
Georg Brandl | e08e3d0 | 2008-05-25 08:07:37 +0000 | [diff] [blame] | 28 | def setUp(self): |
Neal Norwitz | 36a59b4 | 2008-04-10 05:46:39 +0000 | [diff] [blame] | 29 | test_support.unlink(self.filename) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 30 | |
Georg Brandl | e08e3d0 | 2008-05-25 08:07:37 +0000 | [diff] [blame] | 31 | def tearDown(self): |
Neal Norwitz | 36a59b4 | 2008-04-10 05:46:39 +0000 | [diff] [blame] | 32 | test_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 | e08e3d0 | 2008-05-25 08:07:37 +0000 | [diff] [blame] | 35 | def test_write(self): |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 36 | with gzip.GzipFile(self.filename, 'wb') as f: |
| 37 | f.write(data1 * 50) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 38 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 39 | # Try flush and fileno. |
| 40 | f.flush() |
| 41 | f.fileno() |
| 42 | if hasattr(os, 'fsync'): |
| 43 | os.fsync(f.fileno()) |
| 44 | f.close() |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 45 | |
Georg Brandl | e08e3d0 | 2008-05-25 08:07:37 +0000 | [diff] [blame] | 46 | # Test multiple close() calls. |
| 47 | f.close() |
| 48 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 49 | def test_read(self): |
| 50 | self.test_write() |
| 51 | # Try reading. |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 52 | with gzip.GzipFile(self.filename, 'r') as f: |
| 53 | d = f.read() |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 54 | self.assertEqual(d, data1*50) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 55 | |
Antoine Pitrou | 76a66aa | 2010-10-06 21:26:52 +0000 | [diff] [blame] | 56 | def test_io_on_closed_object(self): |
| 57 | # Test that I/O operations on closed GzipFile objects raise a |
| 58 | # ValueError, just like the corresponding functions on file objects. |
| 59 | |
| 60 | # Write to a file, open it for reading, then close it. |
| 61 | self.test_write() |
| 62 | f = gzip.GzipFile(self.filename, 'r') |
| 63 | f.close() |
| 64 | with self.assertRaises(ValueError): |
| 65 | f.read(1) |
| 66 | with self.assertRaises(ValueError): |
| 67 | f.seek(0) |
| 68 | with self.assertRaises(ValueError): |
| 69 | f.tell() |
| 70 | # Open the file for writing, then close it. |
| 71 | f = gzip.GzipFile(self.filename, 'w') |
| 72 | f.close() |
| 73 | with self.assertRaises(ValueError): |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 74 | f.write('') |
Antoine Pitrou | 76a66aa | 2010-10-06 21:26:52 +0000 | [diff] [blame] | 75 | with self.assertRaises(ValueError): |
| 76 | f.flush() |
| 77 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 78 | def test_append(self): |
| 79 | self.test_write() |
| 80 | # Append to the previous file |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 81 | with gzip.GzipFile(self.filename, 'ab') as f: |
| 82 | f.write(data2 * 15) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 83 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 84 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 85 | d = f.read() |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 86 | self.assertEqual(d, (data1*50) + (data2*15)) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 87 | |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 88 | def test_many_append(self): |
| 89 | # Bug #1074261 was triggered when reading a file that contained |
| 90 | # many, many members. Create such a file and verify that reading it |
| 91 | # works. |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 92 | with gzip.open(self.filename, 'wb', 9) as f: |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 93 | f.write('a') |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 94 | for i in range(0, 200): |
| 95 | with gzip.open(self.filename, "ab", 9) as f: # append |
| 96 | f.write('a') |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 97 | |
| 98 | # Try reading the file |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 99 | with gzip.open(self.filename, "rb") as zgfile: |
| 100 | contents = "" |
| 101 | while 1: |
| 102 | ztxt = zgfile.read(8192) |
| 103 | contents += ztxt |
| 104 | if not ztxt: break |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 105 | self.assertEqual(contents, 'a'*201) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 106 | |
Antoine Pitrou | 673ddf9 | 2010-01-03 22:29:56 +0000 | [diff] [blame] | 107 | def test_buffered_reader(self): |
| 108 | # Issue #7471: a GzipFile can be wrapped in a BufferedReader for |
| 109 | # performance. |
| 110 | self.test_write() |
| 111 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 112 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 113 | with io.BufferedReader(f) as r: |
| 114 | lines = [line for line in r] |
Antoine Pitrou | 673ddf9 | 2010-01-03 22:29:56 +0000 | [diff] [blame] | 115 | |
| 116 | self.assertEqual(lines, 50 * data1.splitlines(True)) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 117 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 118 | def test_readline(self): |
| 119 | self.test_write() |
| 120 | # Try .readline() with varying line lengths |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 121 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 122 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 123 | line_length = 0 |
| 124 | while 1: |
| 125 | L = f.readline(line_length) |
| 126 | if not L and line_length != 0: break |
| 127 | self.assertTrue(len(L) <= line_length) |
| 128 | line_length = (line_length + 1) % 50 |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 129 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 130 | def test_readlines(self): |
| 131 | self.test_write() |
| 132 | # Try .readlines() |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 133 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 134 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 135 | L = f.readlines() |
Skip Montanaro | 12424bc | 2002-05-23 01:43:05 +0000 | [diff] [blame] | 136 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 137 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 138 | while 1: |
| 139 | L = f.readlines(150) |
| 140 | if L == []: break |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 141 | |
| 142 | def test_seek_read(self): |
| 143 | self.test_write() |
| 144 | # Try seek, read test |
| 145 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 146 | with gzip.GzipFile(self.filename) as f: |
| 147 | while 1: |
| 148 | oldpos = f.tell() |
| 149 | line1 = f.readline() |
| 150 | if not line1: break |
| 151 | newpos = f.tell() |
| 152 | f.seek(oldpos) # negative seek |
| 153 | if len(line1)>10: |
| 154 | amount = 10 |
| 155 | else: |
| 156 | amount = len(line1) |
| 157 | line2 = f.read(amount) |
| 158 | self.assertEqual(line1[:amount], line2) |
| 159 | f.seek(newpos) # positive seek |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 160 | |
Martin v. Löwis | 065f0c8 | 2006-11-12 10:41:39 +0000 | [diff] [blame] | 161 | def test_seek_whence(self): |
| 162 | self.test_write() |
| 163 | # Try seek(whence=1), read test |
| 164 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 165 | with gzip.GzipFile(self.filename) as f: |
| 166 | f.read(10) |
| 167 | f.seek(10, whence=1) |
| 168 | y = f.read(10) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 169 | self.assertEqual(y, data1[20:30]) |
Tim Peters | f733abb | 2007-01-30 03:03:46 +0000 | [diff] [blame] | 170 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 171 | def test_seek_write(self): |
| 172 | # Try seek, write test |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 173 | with gzip.GzipFile(self.filename, 'w') as f: |
| 174 | for pos in range(0, 256, 16): |
| 175 | f.seek(pos) |
| 176 | f.write('GZ\n') |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 177 | |
| 178 | def test_mode(self): |
| 179 | self.test_write() |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 180 | with gzip.GzipFile(self.filename, 'r') as f: |
| 181 | self.assertEqual(f.myfileobj.mode, 'rb') |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 182 | |
Lars Gustäbel | 5b1a785 | 2007-02-13 16:09:24 +0000 | [diff] [blame] | 183 | def test_1647484(self): |
| 184 | for mode in ('wb', 'rb'): |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 185 | with gzip.GzipFile(self.filename, mode) as f: |
| 186 | self.assertTrue(hasattr(f, "name")) |
| 187 | self.assertEqual(f.name, self.filename) |
Lars Gustäbel | 5b1a785 | 2007-02-13 16:09:24 +0000 | [diff] [blame] | 188 | |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 189 | def test_mtime(self): |
| 190 | mtime = 123456789 |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 191 | with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: |
| 192 | fWrite.write(data1) |
| 193 | with gzip.GzipFile(self.filename) as fRead: |
| 194 | dataRead = fRead.read() |
| 195 | self.assertEqual(dataRead, data1) |
| 196 | self.assertTrue(hasattr(fRead, 'mtime')) |
| 197 | self.assertEqual(fRead.mtime, mtime) |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 198 | |
| 199 | def test_metadata(self): |
| 200 | mtime = 123456789 |
| 201 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 202 | with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: |
| 203 | fWrite.write(data1) |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 204 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 205 | with open(self.filename, 'rb') as fRead: |
| 206 | # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 207 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 208 | idBytes = fRead.read(2) |
| 209 | self.assertEqual(idBytes, '\x1f\x8b') # gzip ID |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 210 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 211 | cmByte = fRead.read(1) |
| 212 | self.assertEqual(cmByte, '\x08') # deflate |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 213 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 214 | flagsByte = fRead.read(1) |
| 215 | self.assertEqual(flagsByte, '\x08') # only the FNAME flag is set |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 216 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 217 | mtimeBytes = fRead.read(4) |
| 218 | self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 219 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 220 | xflByte = fRead.read(1) |
| 221 | self.assertEqual(xflByte, '\x02') # maximum compression |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 222 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 223 | osByte = fRead.read(1) |
| 224 | self.assertEqual(osByte, '\xff') # OS "unknown" (OS-independent) |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 225 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 226 | # Since the FNAME flag is set, the zero-terminated filename follows. |
| 227 | # RFC 1952 specifies that this is the name of the input file, if any. |
| 228 | # However, the gzip module defaults to storing the name of the output |
| 229 | # file in this field. |
| 230 | expected = self.filename.encode('Latin-1') + '\x00' |
| 231 | nameBytes = fRead.read(len(expected)) |
| 232 | self.assertEqual(nameBytes, expected) |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 233 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 234 | # Since no other flags were set, the header ends here. |
| 235 | # Rather than process the compressed data, let's seek to the trailer. |
| 236 | fRead.seek(os.stat(self.filename).st_size - 8) |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 237 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 238 | crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1] |
| 239 | self.assertEqual(crc32Bytes, '\xaf\xd7d\x83') |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 240 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 241 | isizeBytes = fRead.read(4) |
| 242 | self.assertEqual(isizeBytes, struct.pack('<i', len(data1))) |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 243 | |
Antoine Pitrou | b74fc2b | 2009-01-10 16:13:45 +0000 | [diff] [blame] | 244 | def test_with_open(self): |
| 245 | # GzipFile supports the context management protocol |
| 246 | with gzip.GzipFile(self.filename, "wb") as f: |
| 247 | f.write(b"xxx") |
| 248 | f = gzip.GzipFile(self.filename, "rb") |
| 249 | f.close() |
| 250 | try: |
| 251 | with f: |
| 252 | pass |
| 253 | except ValueError: |
| 254 | pass |
| 255 | else: |
| 256 | self.fail("__enter__ on a closed file didn't raise an exception") |
| 257 | try: |
| 258 | with gzip.GzipFile(self.filename, "wb") as f: |
Ezio Melotti | dde5b94 | 2010-02-03 05:37:26 +0000 | [diff] [blame] | 259 | 1 // 0 |
Antoine Pitrou | b74fc2b | 2009-01-10 16:13:45 +0000 | [diff] [blame] | 260 | except ZeroDivisionError: |
| 261 | pass |
| 262 | else: |
Ezio Melotti | dde5b94 | 2010-02-03 05:37:26 +0000 | [diff] [blame] | 263 | self.fail("1 // 0 didn't raise an exception") |
Antoine Pitrou | b74fc2b | 2009-01-10 16:13:45 +0000 | [diff] [blame] | 264 | |
Antoine Pitrou | 5a9112c | 2010-01-13 14:32:10 +0000 | [diff] [blame] | 265 | def test_zero_padded_file(self): |
| 266 | with gzip.GzipFile(self.filename, "wb") as f: |
| 267 | f.write(data1 * 50) |
| 268 | |
| 269 | # Pad the file with zeroes |
| 270 | with open(self.filename, "ab") as f: |
| 271 | f.write("\x00" * 50) |
| 272 | |
| 273 | with gzip.GzipFile(self.filename, "rb") as f: |
| 274 | d = f.read() |
| 275 | self.assertEqual(d, data1 * 50, "Incorrect data in file") |
| 276 | |
Nadeem Vawda | d7664de | 2012-01-19 00:40:46 +0200 | [diff] [blame^] | 277 | def test_fileobj_from_fdopen(self): |
| 278 | # Issue #13781: Creating a GzipFile using a fileobj from os.fdopen() |
| 279 | # should not embed the fake filename "<fdopen>" in the output file. |
| 280 | fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT) |
| 281 | with os.fdopen(fd, "wb") as f: |
| 282 | with gzip.GzipFile(fileobj=f, mode="w") as g: |
| 283 | self.assertEqual(g.name, "") |
| 284 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 285 | def test_main(verbose=None): |
| 286 | test_support.run_unittest(TestGzip) |
| 287 | |
| 288 | if __name__ == "__main__": |
| 289 | test_main(verbose=True) |