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 |
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 |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 8 | import gzip |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 9 | |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 10 | |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 11 | data1 = b""" int length=DEFAULTALLOC, err = Z_OK; |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 12 | PyObject *RetVal; |
| 13 | int flushmode = Z_FINISH; |
| 14 | unsigned long start_total_out; |
| 15 | |
| 16 | """ |
| 17 | |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 18 | data2 = b"""/* zlibmodule.c -- gzip-compatible data compression */ |
Neal Norwitz | 014f103 | 2004-07-29 03:55:56 +0000 | [diff] [blame] | 19 | /* See http://www.gzip.org/zlib/ |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 20 | /* See http://www.winimage.com/zLibDll for Windows */ |
| 21 | """ |
| 22 | |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 23 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 24 | class TestGzip(unittest.TestCase): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 25 | filename = support.TESTFN |
Tim Peters | 5cfb05e | 2004-07-27 21:02:02 +0000 | [diff] [blame] | 26 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 27 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 28 | support.unlink(self.filename) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 29 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 30 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 31 | support.unlink(self.filename) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 32 | |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 33 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 34 | def test_write(self): |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 35 | f = gzip.GzipFile(self.filename, 'wb') ; f.write(data1 * 50) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 36 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 37 | # Try flush and fileno. |
| 38 | f.flush() |
| 39 | f.fileno() |
| 40 | if hasattr(os, 'fsync'): |
| 41 | os.fsync(f.fileno()) |
| 42 | f.close() |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 43 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 44 | # Test multiple close() calls. |
| 45 | f.close() |
| 46 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 47 | def test_read(self): |
| 48 | self.test_write() |
| 49 | # Try reading. |
| 50 | f = gzip.GzipFile(self.filename, 'r') ; d = f.read() ; f.close() |
| 51 | self.assertEqual(d, data1*50) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 52 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 53 | def test_append(self): |
| 54 | self.test_write() |
| 55 | # Append to the previous file |
| 56 | 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] | 57 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 58 | f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close() |
| 59 | self.assertEqual(d, (data1*50) + (data2*15)) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 60 | |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 61 | def test_many_append(self): |
| 62 | # Bug #1074261 was triggered when reading a file that contained |
| 63 | # many, many members. Create such a file and verify that reading it |
| 64 | # works. |
| 65 | f = gzip.open(self.filename, 'wb', 9) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 66 | f.write(b'a') |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 67 | f.close() |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 68 | for i in range(0, 200): |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 69 | f = gzip.open(self.filename, "ab", 9) # append |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 70 | f.write(b'a') |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 71 | f.close() |
| 72 | |
| 73 | # Try reading the file |
| 74 | zgfile = gzip.open(self.filename, "rb") |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 75 | contents = b"" |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 76 | while 1: |
| 77 | ztxt = zgfile.read(8192) |
| 78 | contents += ztxt |
| 79 | if not ztxt: break |
| 80 | zgfile.close() |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 81 | self.assertEquals(contents, b'a'*201) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 82 | |
| 83 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 84 | def test_readline(self): |
| 85 | self.test_write() |
| 86 | # Try .readline() with varying line lengths |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 87 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 88 | f = gzip.GzipFile(self.filename, 'rb') |
| 89 | line_length = 0 |
| 90 | while 1: |
| 91 | L = f.readline(line_length) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 92 | if not L and line_length != 0: break |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 93 | self.assert_(len(L) <= line_length) |
| 94 | line_length = (line_length + 1) % 50 |
| 95 | f.close() |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 96 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 97 | def test_readlines(self): |
| 98 | self.test_write() |
| 99 | # Try .readlines() |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 100 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 101 | f = gzip.GzipFile(self.filename, 'rb') |
| 102 | L = f.readlines() |
| 103 | f.close() |
Skip Montanaro | 12424bc | 2002-05-23 01:43:05 +0000 | [diff] [blame] | 104 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 105 | f = gzip.GzipFile(self.filename, 'rb') |
| 106 | while 1: |
| 107 | L = f.readlines(150) |
| 108 | if L == []: break |
| 109 | f.close() |
| 110 | |
| 111 | def test_seek_read(self): |
| 112 | self.test_write() |
| 113 | # Try seek, read test |
| 114 | |
| 115 | f = gzip.GzipFile(self.filename) |
| 116 | while 1: |
| 117 | oldpos = f.tell() |
| 118 | line1 = f.readline() |
| 119 | if not line1: break |
| 120 | newpos = f.tell() |
| 121 | f.seek(oldpos) # negative seek |
| 122 | if len(line1)>10: |
| 123 | amount = 10 |
| 124 | else: |
| 125 | amount = len(line1) |
| 126 | line2 = f.read(amount) |
| 127 | self.assertEqual(line1[:amount], line2) |
| 128 | f.seek(newpos) # positive seek |
| 129 | f.close() |
| 130 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 131 | def test_seek_whence(self): |
| 132 | self.test_write() |
| 133 | # Try seek(whence=1), read test |
| 134 | |
| 135 | f = gzip.GzipFile(self.filename) |
| 136 | f.read(10) |
| 137 | f.seek(10, whence=1) |
| 138 | y = f.read(10) |
| 139 | f.close() |
| 140 | self.assertEquals(y, data1[20:30]) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 141 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 142 | def test_seek_write(self): |
| 143 | # Try seek, write test |
| 144 | f = gzip.GzipFile(self.filename, 'w') |
| 145 | for pos in range(0, 256, 16): |
| 146 | f.seek(pos) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 147 | f.write(b'GZ\n') |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 148 | f.close() |
| 149 | |
| 150 | def test_mode(self): |
| 151 | self.test_write() |
| 152 | f = gzip.GzipFile(self.filename, 'r') |
| 153 | self.assertEqual(f.myfileobj.mode, 'rb') |
| 154 | f.close() |
| 155 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 156 | def test_1647484(self): |
| 157 | for mode in ('wb', 'rb'): |
| 158 | f = gzip.GzipFile(self.filename, mode) |
| 159 | self.assert_(hasattr(f, "name")) |
| 160 | self.assertEqual(f.name, self.filename) |
| 161 | f.close() |
| 162 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 163 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 164 | support.run_unittest(TestGzip) |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 165 | |
| 166 | if __name__ == "__main__": |
| 167 | test_main(verbose=True) |