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