blob: d28c024d07ffcffe85f57519b2c80b69e5da9075 [file] [log] [blame]
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +00001#! /usr/bin/env python
2"""Test script for the gzip module.
3"""
4
5import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Christian Heimes05e8be12008-02-23 18:30:17 +00007import os
Guido van Rossum3b0a3292002-08-09 16:38:32 +00008import gzip
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +00009
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000010
Walter Dörwald5b1284d2007-06-06 16:43:59 +000011data1 = b""" int length=DEFAULTALLOC, err = Z_OK;
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000012 PyObject *RetVal;
13 int flushmode = Z_FINISH;
14 unsigned long start_total_out;
15
16"""
17
Walter Dörwald5b1284d2007-06-06 16:43:59 +000018data2 = b"""/* zlibmodule.c -- gzip-compatible data compression */
Neal Norwitz014f1032004-07-29 03:55:56 +000019/* See http://www.gzip.org/zlib/
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000020/* See http://www.winimage.com/zLibDll for Windows */
21"""
22
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000023
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000024class TestGzip(unittest.TestCase):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000025 filename = support.TESTFN
Tim Peters5cfb05e2004-07-27 21:02:02 +000026
Georg Brandlb533e262008-05-25 18:19:30 +000027 def setUp(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000028 support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000029
Georg Brandlb533e262008-05-25 18:19:30 +000030 def tearDown(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000031 support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000032
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000033
Georg Brandlb533e262008-05-25 18:19:30 +000034 def test_write(self):
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000035 f = gzip.GzipFile(self.filename, 'wb') ; f.write(data1 * 50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000036
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000037 # 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. Kuchling85ab7382000-07-29 20:18:34 +000043
Georg Brandlb533e262008-05-25 18:19:30 +000044 # Test multiple close() calls.
45 f.close()
46
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000047 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. Kuchling85ab7382000-07-29 20:18:34 +000052
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000053 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. Kuchling85ab7382000-07-29 20:18:34 +000057
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000058 f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close()
59 self.assertEqual(d, (data1*50) + (data2*15))
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000060
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000061 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örwald5b1284d2007-06-06 16:43:59 +000066 f.write(b'a')
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000067 f.close()
Walter Dörwald5b1284d2007-06-06 16:43:59 +000068 for i in range(0, 200):
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000069 f = gzip.open(self.filename, "ab", 9) # append
Walter Dörwald5b1284d2007-06-06 16:43:59 +000070 f.write(b'a')
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000071 f.close()
72
73 # Try reading the file
74 zgfile = gzip.open(self.filename, "rb")
Walter Dörwald5b1284d2007-06-06 16:43:59 +000075 contents = b""
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000076 while 1:
77 ztxt = zgfile.read(8192)
78 contents += ztxt
79 if not ztxt: break
80 zgfile.close()
Walter Dörwald5b1284d2007-06-06 16:43:59 +000081 self.assertEquals(contents, b'a'*201)
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +000082
83
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000084 def test_readline(self):
85 self.test_write()
86 # Try .readline() with varying line lengths
Martin v. Löwis8cc965c2001-08-09 07:21:56 +000087
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000088 f = gzip.GzipFile(self.filename, 'rb')
89 line_length = 0
90 while 1:
91 L = f.readline(line_length)
Walter Dörwald5b1284d2007-06-06 16:43:59 +000092 if not L and line_length != 0: break
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000093 self.assert_(len(L) <= line_length)
94 line_length = (line_length + 1) % 50
95 f.close()
Martin v. Löwis8cc965c2001-08-09 07:21:56 +000096
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000097 def test_readlines(self):
98 self.test_write()
99 # Try .readlines()
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +0000100
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000101 f = gzip.GzipFile(self.filename, 'rb')
102 L = f.readlines()
103 f.close()
Skip Montanaro12424bc2002-05-23 01:43:05 +0000104
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000105 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 Wouters89f507f2006-12-13 04:49:30 +0000131 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 Wouters9fe394c2007-02-05 01:24:16 +0000141
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000142 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örwald5b1284d2007-06-06 16:43:59 +0000147 f.write(b'GZ\n')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000148 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 Wouterscf297e42007-02-23 15:07:44 +0000156 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. Kuchlinga6f68e12005-06-09 14:12:36 +0000163def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000164 support.run_unittest(TestGzip)
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000165
166if __name__ == "__main__":
167 test_main(verbose=True)