blob: 6fea893993b559cd4abcce35cef729f47ed98d39 [file] [log] [blame]
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00003import binascii
Serhiy Storchakad7a44152015-11-12 11:23:04 +02004import pickle
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00005import random
Antoine Pitrouf3d22752011-02-21 18:09:00 +00006import sys
Antoine Pitrou94190bb2011-10-04 10:22:36 +02007from test.support import bigmemtest, _1G, _4G
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00008
R. David Murraya21e4ca2009-03-31 23:16:50 +00009zlib = support.import_module('zlib')
10
Serhiy Storchaka43767632013-11-03 21:31:38 +020011requires_Compress_copy = unittest.skipUnless(
12 hasattr(zlib.compressobj(), "copy"),
13 'requires Compress.copy()')
14requires_Decompress_copy = unittest.skipUnless(
15 hasattr(zlib.decompressobj(), "copy"),
16 'requires Decompress.copy()')
17
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +000018
Nadeem Vawda64d25dd2011-09-12 00:04:13 +020019class VersionTestCase(unittest.TestCase):
20
21 def test_library_version(self):
Nadeem Vawda131c7072012-01-25 23:16:50 +020022 # Test that the major version of the actual library in use matches the
23 # major version that we were compiled against. We can't guarantee that
24 # the minor versions will match (even on the machine on which the module
25 # was compiled), and the API is stable between minor versions, so
Nadeem Vawdad770fe42012-01-28 17:32:47 +020026 # testing only the major versions avoids spurious failures.
Nadeem Vawda131c7072012-01-25 23:16:50 +020027 self.assertEqual(zlib.ZLIB_RUNTIME_VERSION[0], zlib.ZLIB_VERSION[0])
Nadeem Vawda64d25dd2011-09-12 00:04:13 +020028
29
Guido van Rossum7d9ea502003-02-03 20:45:52 +000030class ChecksumTestCase(unittest.TestCase):
31 # checksum test cases
32 def test_crc32start(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000033 self.assertEqual(zlib.crc32(b""), zlib.crc32(b"", 0))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000034 self.assertTrue(zlib.crc32(b"abc", 0xffffffff))
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +000035
Guido van Rossum7d9ea502003-02-03 20:45:52 +000036 def test_crc32empty(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000037 self.assertEqual(zlib.crc32(b"", 0), 0)
38 self.assertEqual(zlib.crc32(b"", 1), 1)
39 self.assertEqual(zlib.crc32(b"", 432), 432)
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +000040
Guido van Rossum7d9ea502003-02-03 20:45:52 +000041 def test_adler32start(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000042 self.assertEqual(zlib.adler32(b""), zlib.adler32(b"", 1))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000043 self.assertTrue(zlib.adler32(b"abc", 0xffffffff))
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000044
Guido van Rossum7d9ea502003-02-03 20:45:52 +000045 def test_adler32empty(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000046 self.assertEqual(zlib.adler32(b"", 0), 0)
47 self.assertEqual(zlib.adler32(b"", 1), 1)
48 self.assertEqual(zlib.adler32(b"", 432), 432)
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000049
Guido van Rossum7d9ea502003-02-03 20:45:52 +000050 def test_penguins(self):
Martin Panterb82032f2015-12-11 05:19:29 +000051 self.assertEqual(zlib.crc32(b"penguin", 0), 0x0e5c1a120)
52 self.assertEqual(zlib.crc32(b"penguin", 1), 0x43b6aa94)
53 self.assertEqual(zlib.adler32(b"penguin", 0), 0x0bcf02f6)
54 self.assertEqual(zlib.adler32(b"penguin", 1), 0x0bd602f7)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000055
Guido van Rossum776152b2007-05-22 22:44:07 +000056 self.assertEqual(zlib.crc32(b"penguin"), zlib.crc32(b"penguin", 0))
57 self.assertEqual(zlib.adler32(b"penguin"),zlib.adler32(b"penguin",1))
Guido van Rossum7d9ea502003-02-03 20:45:52 +000058
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000059 def test_crc32_adler32_unsigned(self):
Antoine Pitrou77b338b2009-12-14 18:00:06 +000060 foo = b'abcdefghijklmnop'
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000061 # explicitly test signed behavior
Gregory P. Smith27275032008-03-20 06:20:09 +000062 self.assertEqual(zlib.crc32(foo), 2486878355)
Antoine Pitrou77b338b2009-12-14 18:00:06 +000063 self.assertEqual(zlib.crc32(b'spam'), 1138425661)
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000064 self.assertEqual(zlib.adler32(foo+foo), 3573550353)
Antoine Pitrou77b338b2009-12-14 18:00:06 +000065 self.assertEqual(zlib.adler32(b'spam'), 72286642)
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000066
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000067 def test_same_as_binascii_crc32(self):
Martin v. Löwis15b16a32008-12-02 06:00:15 +000068 foo = b'abcdefghijklmnop'
Gregory P. Smith27275032008-03-20 06:20:09 +000069 crc = 2486878355
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000070 self.assertEqual(binascii.crc32(foo), crc)
71 self.assertEqual(zlib.crc32(foo), crc)
Martin v. Löwis15b16a32008-12-02 06:00:15 +000072 self.assertEqual(binascii.crc32(b'spam'), zlib.crc32(b'spam'))
Guido van Rossum7d9ea502003-02-03 20:45:52 +000073
74
Antoine Pitrouf3d22752011-02-21 18:09:00 +000075# Issue #10276 - check that inputs >=4GB are handled correctly.
76class ChecksumBigBufferTestCase(unittest.TestCase):
77
Nadeem Vawdabc8c8172012-02-23 14:16:15 +020078 @bigmemtest(size=_4G + 4, memuse=1, dry_run=False)
79 def test_big_buffer(self, size):
Nadeem Vawdab063a482012-02-23 13:36:25 +020080 data = b"nyan" * (_1G + 1)
81 self.assertEqual(zlib.crc32(data), 1044521549)
82 self.assertEqual(zlib.adler32(data), 2256789997)
Antoine Pitrouf3d22752011-02-21 18:09:00 +000083
Christian Heimesb186d002008-03-18 15:15:01 +000084
Guido van Rossum7d9ea502003-02-03 20:45:52 +000085class ExceptionTestCase(unittest.TestCase):
86 # make sure we generate some expected errors
Guido van Rossum8ce8a782007-11-01 19:42:39 +000087 def test_badlevel(self):
88 # specifying compression level out of range causes an error
89 # (but -1 is Z_DEFAULT_COMPRESSION and apparently the zlib
90 # accepts 0 too)
Antoine Pitrou77b338b2009-12-14 18:00:06 +000091 self.assertRaises(zlib.error, zlib.compress, b'ERROR', 10)
92
93 def test_badargs(self):
94 self.assertRaises(TypeError, zlib.adler32)
95 self.assertRaises(TypeError, zlib.crc32)
96 self.assertRaises(TypeError, zlib.compress)
97 self.assertRaises(TypeError, zlib.decompress)
98 for arg in (42, None, '', 'abc', (), []):
99 self.assertRaises(TypeError, zlib.adler32, arg)
100 self.assertRaises(TypeError, zlib.crc32, arg)
101 self.assertRaises(TypeError, zlib.compress, arg)
102 self.assertRaises(TypeError, zlib.decompress, arg)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000103
104 def test_badcompressobj(self):
105 # verify failure on building compress object with bad params
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000106 self.assertRaises(ValueError, zlib.compressobj, 1, zlib.DEFLATED, 0)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000107 # specifying total bits too large causes an error
108 self.assertRaises(ValueError,
109 zlib.compressobj, 1, zlib.DEFLATED, zlib.MAX_WBITS + 1)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000110
111 def test_baddecompressobj(self):
112 # verify failure on building decompress object with bad params
Antoine Pitrou90ee4df2010-04-06 17:23:13 +0000113 self.assertRaises(ValueError, zlib.decompressobj, -1)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000114
Christian Heimes5e696852008-04-09 08:37:03 +0000115 def test_decompressobj_badflush(self):
116 # verify failure on calling decompressobj.flush with bad params
117 self.assertRaises(ValueError, zlib.decompressobj().flush, 0)
118 self.assertRaises(ValueError, zlib.decompressobj().flush, -1)
119
Martin Pantere99e9772015-11-20 08:13:35 +0000120 @support.cpython_only
121 def test_overflow(self):
122 with self.assertRaisesRegex(OverflowError, 'int too large'):
123 zlib.decompress(b'', 15, sys.maxsize + 1)
124 with self.assertRaisesRegex(OverflowError, 'int too large'):
Martin Panter84544c12016-07-23 03:02:07 +0000125 zlib.decompressobj().decompress(b'', sys.maxsize + 1)
126 with self.assertRaisesRegex(OverflowError, 'int too large'):
Martin Pantere99e9772015-11-20 08:13:35 +0000127 zlib.decompressobj().flush(sys.maxsize + 1)
128
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000129
Antoine Pitrou89562712010-05-07 17:04:02 +0000130class BaseCompressTestCase(object):
131 def check_big_compress_buffer(self, size, compress_func):
132 _1M = 1024 * 1024
Antoine Pitrou89562712010-05-07 17:04:02 +0000133 # Generate 10MB worth of random, and expand it by repeating it.
134 # The assumption is that zlib's memory is not big enough to exploit
135 # such spread out redundancy.
136 data = b''.join([random.getrandbits(8 * _1M).to_bytes(_1M, 'little')
137 for i in range(10)])
138 data = data * (size // len(data) + 1)
139 try:
140 compress_func(data)
141 finally:
142 # Release memory
143 data = None
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000144
Antoine Pitrou89562712010-05-07 17:04:02 +0000145 def check_big_decompress_buffer(self, size, decompress_func):
146 data = b'x' * size
147 try:
148 compressed = zlib.compress(data, 1)
149 finally:
150 # Release memory
151 data = None
152 data = decompress_func(compressed)
153 # Sanity check
154 try:
155 self.assertEqual(len(data), size)
156 self.assertEqual(len(data.strip(b'x')), 0)
157 finally:
158 data = None
159
160
161class CompressTestCase(BaseCompressTestCase, unittest.TestCase):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000162 # Test compression in one go (whole message compression)
163 def test_speech(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000164 x = zlib.compress(HAMLET_SCENE)
165 self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000166
167 def test_speech128(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000168 # compress more data
169 data = HAMLET_SCENE * 128
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000170 x = zlib.compress(data)
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000171 self.assertEqual(zlib.compress(bytearray(data)), x)
172 for ob in x, bytearray(x):
173 self.assertEqual(zlib.decompress(ob), data)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000174
Antoine Pitrou53b21662010-05-11 23:46:02 +0000175 def test_incomplete_stream(self):
Martin Panter6245cb32016-04-15 02:14:19 +0000176 # A useful error message is given
Antoine Pitrou53b21662010-05-11 23:46:02 +0000177 x = zlib.compress(HAMLET_SCENE)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000178 self.assertRaisesRegex(zlib.error,
Antoine Pitrou53b21662010-05-11 23:46:02 +0000179 "Error -5 while decompressing data: incomplete or truncated stream",
180 zlib.decompress, x[:-1])
181
Antoine Pitrou89562712010-05-07 17:04:02 +0000182 # Memory use of the following functions takes into account overallocation
183
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200184 @bigmemtest(size=_1G + 1024 * 1024, memuse=3)
Antoine Pitrou89562712010-05-07 17:04:02 +0000185 def test_big_compress_buffer(self, size):
186 compress = lambda s: zlib.compress(s, 1)
187 self.check_big_compress_buffer(size, compress)
188
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200189 @bigmemtest(size=_1G + 1024 * 1024, memuse=2)
Antoine Pitrou89562712010-05-07 17:04:02 +0000190 def test_big_decompress_buffer(self, size):
191 self.check_big_decompress_buffer(size, zlib.decompress)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000192
Martin Pantere99e9772015-11-20 08:13:35 +0000193 @bigmemtest(size=_4G, memuse=1)
194 def test_large_bufsize(self, size):
195 # Test decompress(bufsize) parameter greater than the internal limit
196 data = HAMLET_SCENE * 10
197 compressed = zlib.compress(data, 1)
198 self.assertEqual(zlib.decompress(compressed, 15, size), data)
199
200 def test_custom_bufsize(self):
201 data = HAMLET_SCENE * 10
202 compressed = zlib.compress(data, 1)
203 self.assertEqual(zlib.decompress(compressed, 15, CustomInt()), data)
204
Martin Panter84544c12016-07-23 03:02:07 +0000205 @unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform')
206 @bigmemtest(size=_4G + 100, memuse=4)
207 def test_64bit_compress(self, size):
208 data = b'x' * size
209 try:
210 comp = zlib.compress(data, 0)
211 self.assertEqual(zlib.decompress(comp), data)
212 finally:
213 comp = data = None
214
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000215
Antoine Pitrou89562712010-05-07 17:04:02 +0000216class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000217 # Test compression object
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000218 def test_pair(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000219 # straightforward compress/decompress objects
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000220 datasrc = HAMLET_SCENE * 128
221 datazip = zlib.compress(datasrc)
222 # should compress both bytes and bytearray data
223 for data in (datasrc, bytearray(datasrc)):
224 co = zlib.compressobj()
225 x1 = co.compress(data)
226 x2 = co.flush()
227 self.assertRaises(zlib.error, co.flush) # second flush should not work
228 self.assertEqual(x1 + x2, datazip)
229 for v1, v2 in ((x1, x2), (bytearray(x1), bytearray(x2))):
230 dco = zlib.decompressobj()
231 y1 = dco.decompress(v1 + v2)
232 y2 = dco.flush()
233 self.assertEqual(data, y1 + y2)
234 self.assertIsInstance(dco.unconsumed_tail, bytes)
235 self.assertIsInstance(dco.unused_data, bytes)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000236
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000237 def test_compressoptions(self):
238 # specify lots of options to compressobj()
239 level = 2
240 method = zlib.DEFLATED
241 wbits = -12
Martin Panterbf19d162015-09-09 01:01:13 +0000242 memLevel = 9
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000243 strategy = zlib.Z_FILTERED
Martin Panterbf19d162015-09-09 01:01:13 +0000244 co = zlib.compressobj(level, method, wbits, memLevel, strategy)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000245 x1 = co.compress(HAMLET_SCENE)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000246 x2 = co.flush()
247 dco = zlib.decompressobj(wbits)
248 y1 = dco.decompress(x1 + x2)
249 y2 = dco.flush()
Neil Schemenauer6412b122004-06-05 19:34:28 +0000250 self.assertEqual(HAMLET_SCENE, y1 + y2)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000251
Martin Panterbf19d162015-09-09 01:01:13 +0000252 # keyword arguments should also be supported
253 zlib.compressobj(level=level, method=method, wbits=wbits,
254 memLevel=memLevel, strategy=strategy, zdict=b"")
255
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000256 def test_compressincremental(self):
257 # compress object in steps, decompress object as one-shot
Neil Schemenauer6412b122004-06-05 19:34:28 +0000258 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000259 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000260 bufs = []
261 for i in range(0, len(data), 256):
262 bufs.append(co.compress(data[i:i+256]))
263 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000264 combuf = b''.join(bufs)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000265
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000266 dco = zlib.decompressobj()
Guido van Rossum776152b2007-05-22 22:44:07 +0000267 y1 = dco.decompress(b''.join(bufs))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000268 y2 = dco.flush()
269 self.assertEqual(data, y1 + y2)
270
Neil Schemenauer6412b122004-06-05 19:34:28 +0000271 def test_decompinc(self, flush=False, source=None, cx=256, dcx=64):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000272 # compress object in steps, decompress object in steps
Neil Schemenauer6412b122004-06-05 19:34:28 +0000273 source = source or HAMLET_SCENE
274 data = source * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000275 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000276 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000277 for i in range(0, len(data), cx):
278 bufs.append(co.compress(data[i:i+cx]))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000279 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000280 combuf = b''.join(bufs)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000281
Gregory P. Smith693fc462008-09-06 20:13:06 +0000282 decombuf = zlib.decompress(combuf)
283 # Test type of return value
Ezio Melottie9615932010-01-24 19:26:24 +0000284 self.assertIsInstance(decombuf, bytes)
Gregory P. Smith693fc462008-09-06 20:13:06 +0000285
286 self.assertEqual(data, decombuf)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000287
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000288 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000289 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000290 for i in range(0, len(combuf), dcx):
291 bufs.append(dco.decompress(combuf[i:i+dcx]))
Guido van Rossum776152b2007-05-22 22:44:07 +0000292 self.assertEqual(b'', dco.unconsumed_tail, ########
293 "(A) uct should be b'': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000294 len(dco.unconsumed_tail))
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000295 self.assertEqual(b'', dco.unused_data)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000296 if flush:
297 bufs.append(dco.flush())
298 else:
299 while True:
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000300 chunk = dco.decompress(b'')
Neil Schemenauer6412b122004-06-05 19:34:28 +0000301 if chunk:
302 bufs.append(chunk)
303 else:
304 break
Guido van Rossum776152b2007-05-22 22:44:07 +0000305 self.assertEqual(b'', dco.unconsumed_tail, ########
306 "(B) uct should be b'': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000307 len(dco.unconsumed_tail))
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000308 self.assertEqual(b'', dco.unused_data)
Guido van Rossum776152b2007-05-22 22:44:07 +0000309 self.assertEqual(data, b''.join(bufs))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000310 # Failure means: "decompressobj with init options failed"
311
Neil Schemenauer6412b122004-06-05 19:34:28 +0000312 def test_decompincflush(self):
313 self.test_decompinc(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000314
Neil Schemenauer6412b122004-06-05 19:34:28 +0000315 def test_decompimax(self, source=None, cx=256, dcx=64):
316 # compress in steps, decompress in length-restricted steps
317 source = source or HAMLET_SCENE
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000318 # Check a decompression object with max_length specified
Neil Schemenauer6412b122004-06-05 19:34:28 +0000319 data = source * 128
320 co = zlib.compressobj()
321 bufs = []
322 for i in range(0, len(data), cx):
323 bufs.append(co.compress(data[i:i+cx]))
324 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000325 combuf = b''.join(bufs)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000326 self.assertEqual(data, zlib.decompress(combuf),
327 'compressed data failure')
328
329 dco = zlib.decompressobj()
330 bufs = []
331 cb = combuf
332 while cb:
333 #max_length = 1 + len(cb)//10
334 chunk = dco.decompress(cb, dcx)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000335 self.assertFalse(len(chunk) > dcx,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000336 'chunk too big (%d>%d)' % (len(chunk), dcx))
337 bufs.append(chunk)
338 cb = dco.unconsumed_tail
339 bufs.append(dco.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000340 self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved')
Neil Schemenauer6412b122004-06-05 19:34:28 +0000341
342 def test_decompressmaxlen(self, flush=False):
343 # Check a decompression object with max_length specified
344 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000345 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000346 bufs = []
347 for i in range(0, len(data), 256):
348 bufs.append(co.compress(data[i:i+256]))
349 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000350 combuf = b''.join(bufs)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000351 self.assertEqual(data, zlib.decompress(combuf),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000352 'compressed data failure')
353
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000354 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000355 bufs = []
356 cb = combuf
357 while cb:
Guido van Rossumf3594102003-02-27 18:39:18 +0000358 max_length = 1 + len(cb)//10
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000359 chunk = dco.decompress(cb, max_length)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000360 self.assertFalse(len(chunk) > max_length,
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000361 'chunk too big (%d>%d)' % (len(chunk),max_length))
362 bufs.append(chunk)
363 cb = dco.unconsumed_tail
Neil Schemenauer6412b122004-06-05 19:34:28 +0000364 if flush:
365 bufs.append(dco.flush())
366 else:
367 while chunk:
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000368 chunk = dco.decompress(b'', max_length)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000369 self.assertFalse(len(chunk) > max_length,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000370 'chunk too big (%d>%d)' % (len(chunk),max_length))
371 bufs.append(chunk)
Guido van Rossum776152b2007-05-22 22:44:07 +0000372 self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved')
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000373
Neil Schemenauer6412b122004-06-05 19:34:28 +0000374 def test_decompressmaxlenflush(self):
375 self.test_decompressmaxlen(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000376
377 def test_maxlenmisc(self):
378 # Misc tests of max_length
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000379 dco = zlib.decompressobj()
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000380 self.assertRaises(ValueError, dco.decompress, b"", -1)
Guido van Rossum776152b2007-05-22 22:44:07 +0000381 self.assertEqual(b'', dco.unconsumed_tail)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000382
Martin Pantere99e9772015-11-20 08:13:35 +0000383 def test_maxlen_large(self):
384 # Sizes up to sys.maxsize should be accepted, although zlib is
385 # internally limited to expressing sizes with unsigned int
386 data = HAMLET_SCENE * 10
387 self.assertGreater(len(data), zlib.DEF_BUF_SIZE)
388 compressed = zlib.compress(data, 1)
389 dco = zlib.decompressobj()
390 self.assertEqual(dco.decompress(compressed, sys.maxsize), data)
391
392 def test_maxlen_custom(self):
393 data = HAMLET_SCENE * 10
394 compressed = zlib.compress(data, 1)
395 dco = zlib.decompressobj()
396 self.assertEqual(dco.decompress(compressed, CustomInt()), data[:100])
397
Nadeem Vawda7619e882011-05-14 14:05:20 +0200398 def test_clear_unconsumed_tail(self):
399 # Issue #12050: calling decompress() without providing max_length
400 # should clear the unconsumed_tail attribute.
401 cdata = b"x\x9cKLJ\x06\x00\x02M\x01" # "abc"
402 dco = zlib.decompressobj()
403 ddata = dco.decompress(cdata, 1)
404 ddata += dco.decompress(dco.unconsumed_tail)
405 self.assertEqual(dco.unconsumed_tail, b"")
406
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000407 def test_flushes(self):
408 # Test flush() with the various options, using all the
409 # different levels in order to provide more variations.
410 sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
411 sync_opt = [getattr(zlib, opt) for opt in sync_opt
412 if hasattr(zlib, opt)]
Neil Schemenauer6412b122004-06-05 19:34:28 +0000413 data = HAMLET_SCENE * 8
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000414
415 for sync in sync_opt:
416 for level in range(10):
417 obj = zlib.compressobj( level )
418 a = obj.compress( data[:3000] )
419 b = obj.flush( sync )
420 c = obj.compress( data[3000:] )
421 d = obj.flush()
Guido van Rossum776152b2007-05-22 22:44:07 +0000422 self.assertEqual(zlib.decompress(b''.join([a,b,c,d])),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000423 data, ("Decompress failed: flush "
424 "mode=%i, level=%i") % (sync, level))
425 del obj
426
Serhiy Storchaka43767632013-11-03 21:31:38 +0200427 @unittest.skipUnless(hasattr(zlib, 'Z_SYNC_FLUSH'),
428 'requires zlib.Z_SYNC_FLUSH')
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000429 def test_odd_flush(self):
430 # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
431 import random
Serhiy Storchaka43767632013-11-03 21:31:38 +0200432 # Testing on 17K of "random" data
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000433
Serhiy Storchaka43767632013-11-03 21:31:38 +0200434 # Create compressor and decompressor objects
435 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
436 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000437
Serhiy Storchaka43767632013-11-03 21:31:38 +0200438 # Try 17K of data
439 # generate random data stream
440 try:
441 # In 2.3 and later, WichmannHill is the RNG of the bug report
442 gen = random.WichmannHill()
443 except AttributeError:
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000444 try:
Serhiy Storchaka43767632013-11-03 21:31:38 +0200445 # 2.2 called it Random
446 gen = random.Random()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000447 except AttributeError:
Serhiy Storchaka43767632013-11-03 21:31:38 +0200448 # others might simply have a single RNG
449 gen = random
450 gen.seed(1)
451 data = genblock(1, 17 * 1024, generator=gen)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000452
Serhiy Storchaka43767632013-11-03 21:31:38 +0200453 # compress, sync-flush, and decompress
454 first = co.compress(data)
455 second = co.flush(zlib.Z_SYNC_FLUSH)
456 expanded = dco.decompress(first + second)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000457
Serhiy Storchaka43767632013-11-03 21:31:38 +0200458 # if decompressed data is different from the input data, choke.
459 self.assertEqual(expanded, data, "17K random source doesn't match")
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000460
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000461 def test_empty_flush(self):
462 # Test that calling .flush() on unused objects works.
463 # (Bug #1083110 -- calling .flush() on decompress objects
464 # caused a core dump.)
465
466 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000467 self.assertTrue(co.flush()) # Returns a zlib header
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000468 dco = zlib.decompressobj()
Guido van Rossum776152b2007-05-22 22:44:07 +0000469 self.assertEqual(dco.flush(), b"") # Returns nothing
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000470
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200471 def test_dictionary(self):
472 h = HAMLET_SCENE
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200473 # Build a simulated dictionary out of the words in HAMLET.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200474 words = h.split()
475 random.shuffle(words)
476 zdict = b''.join(words)
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200477 # Use it to compress HAMLET.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200478 co = zlib.compressobj(zdict=zdict)
479 cd = co.compress(h) + co.flush()
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200480 # Verify that it will decompress with the dictionary.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200481 dco = zlib.decompressobj(zdict=zdict)
482 self.assertEqual(dco.decompress(cd) + dco.flush(), h)
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200483 # Verify that it fails when not given the dictionary.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200484 dco = zlib.decompressobj()
485 self.assertRaises(zlib.error, dco.decompress, cd)
486
487 def test_dictionary_streaming(self):
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200488 # This simulates the reuse of a compressor object for compressing
489 # several separate data streams.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200490 co = zlib.compressobj(zdict=HAMLET_SCENE)
491 do = zlib.decompressobj(zdict=HAMLET_SCENE)
492 piece = HAMLET_SCENE[1000:1500]
493 d0 = co.compress(piece) + co.flush(zlib.Z_SYNC_FLUSH)
494 d1 = co.compress(piece[100:]) + co.flush(zlib.Z_SYNC_FLUSH)
495 d2 = co.compress(piece[:-100]) + co.flush(zlib.Z_SYNC_FLUSH)
496 self.assertEqual(do.decompress(d0), piece)
497 self.assertEqual(do.decompress(d1), piece[100:])
498 self.assertEqual(do.decompress(d2), piece[:-100])
499
Antoine Pitrouc09c92f2010-05-11 23:36:40 +0000500 def test_decompress_incomplete_stream(self):
501 # This is 'foo', deflated
502 x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E'
503 # For the record
504 self.assertEqual(zlib.decompress(x), b'foo')
505 self.assertRaises(zlib.error, zlib.decompress, x[:-5])
506 # Omitting the stream end works with decompressor objects
507 # (see issue #8672).
508 dco = zlib.decompressobj()
509 y = dco.decompress(x[:-5])
510 y += dco.flush()
511 self.assertEqual(y, b'foo')
512
Nadeem Vawda1c385462011-08-13 15:22:40 +0200513 def test_decompress_eof(self):
514 x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' # 'foo'
515 dco = zlib.decompressobj()
516 self.assertFalse(dco.eof)
517 dco.decompress(x[:-5])
518 self.assertFalse(dco.eof)
519 dco.decompress(x[-5:])
520 self.assertTrue(dco.eof)
521 dco.flush()
522 self.assertTrue(dco.eof)
523
524 def test_decompress_eof_incomplete_stream(self):
525 x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' # 'foo'
526 dco = zlib.decompressobj()
527 self.assertFalse(dco.eof)
528 dco.decompress(x[:-5])
529 self.assertFalse(dco.eof)
530 dco.flush()
531 self.assertFalse(dco.eof)
532
Nadeem Vawda39079942012-11-05 00:37:42 +0100533 def test_decompress_unused_data(self):
534 # Repeated calls to decompress() after EOF should accumulate data in
535 # dco.unused_data, instead of just storing the arg to the last call.
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100536 source = b'abcdefghijklmnopqrstuvwxyz'
537 remainder = b'0123456789'
538 y = zlib.compress(source)
539 x = y + remainder
540 for maxlen in 0, 1000:
541 for step in 1, 2, len(y), len(x):
542 dco = zlib.decompressobj()
543 data = b''
544 for i in range(0, len(x), step):
545 if i < len(y):
546 self.assertEqual(dco.unused_data, b'')
547 if maxlen == 0:
548 data += dco.decompress(x[i : i + step])
549 self.assertEqual(dco.unconsumed_tail, b'')
550 else:
551 data += dco.decompress(
552 dco.unconsumed_tail + x[i : i + step], maxlen)
553 data += dco.flush()
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100554 self.assertTrue(dco.eof)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100555 self.assertEqual(data, source)
556 self.assertEqual(dco.unconsumed_tail, b'')
557 self.assertEqual(dco.unused_data, remainder)
Nadeem Vawda39079942012-11-05 00:37:42 +0100558
Martin Panter3f0ee832016-06-05 10:48:34 +0000559 # issue27164
560 def test_decompress_raw_with_dictionary(self):
561 zdict = b'abcdefghijklmnopqrstuvwxyz'
562 co = zlib.compressobj(wbits=-zlib.MAX_WBITS, zdict=zdict)
563 comp = co.compress(zdict) + co.flush()
564 dco = zlib.decompressobj(wbits=-zlib.MAX_WBITS, zdict=zdict)
565 uncomp = dco.decompress(comp) + dco.flush()
566 self.assertEqual(zdict, uncomp)
567
Nadeem Vawda7ee95552012-11-11 03:15:32 +0100568 def test_flush_with_freed_input(self):
569 # Issue #16411: decompressor accesses input to last decompress() call
570 # in flush(), even if this object has been freed in the meanwhile.
571 input1 = b'abcdefghijklmnopqrstuvwxyz'
572 input2 = b'QWERTYUIOPASDFGHJKLZXCVBNM'
573 data = zlib.compress(input1)
574 dco = zlib.decompressobj()
575 dco.decompress(data, 1)
576 del data
577 data = zlib.compress(input2)
578 self.assertEqual(dco.flush(), input1[1:])
579
Martin Pantere99e9772015-11-20 08:13:35 +0000580 @bigmemtest(size=_4G, memuse=1)
581 def test_flush_large_length(self, size):
582 # Test flush(length) parameter greater than internal limit UINT_MAX
583 input = HAMLET_SCENE * 10
584 data = zlib.compress(input, 1)
585 dco = zlib.decompressobj()
586 dco.decompress(data, 1)
587 self.assertEqual(dco.flush(size), input[1:])
588
589 def test_flush_custom_length(self):
590 input = HAMLET_SCENE * 10
591 data = zlib.compress(input, 1)
592 dco = zlib.decompressobj()
593 dco.decompress(data, 1)
594 self.assertEqual(dco.flush(CustomInt()), input[1:])
595
Serhiy Storchaka43767632013-11-03 21:31:38 +0200596 @requires_Compress_copy
597 def test_compresscopy(self):
598 # Test copying a compression object
599 data0 = HAMLET_SCENE
600 data1 = bytes(str(HAMLET_SCENE, "ascii").swapcase(), "ascii")
601 c0 = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
602 bufs0 = []
603 bufs0.append(c0.compress(data0))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604
Serhiy Storchaka43767632013-11-03 21:31:38 +0200605 c1 = c0.copy()
606 bufs1 = bufs0[:]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000607
Serhiy Storchaka43767632013-11-03 21:31:38 +0200608 bufs0.append(c0.compress(data0))
609 bufs0.append(c0.flush())
610 s0 = b''.join(bufs0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000611
Serhiy Storchaka43767632013-11-03 21:31:38 +0200612 bufs1.append(c1.compress(data1))
613 bufs1.append(c1.flush())
614 s1 = b''.join(bufs1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000615
Serhiy Storchaka43767632013-11-03 21:31:38 +0200616 self.assertEqual(zlib.decompress(s0),data0+data0)
617 self.assertEqual(zlib.decompress(s1),data0+data1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000618
Serhiy Storchaka43767632013-11-03 21:31:38 +0200619 @requires_Compress_copy
620 def test_badcompresscopy(self):
621 # Test copying a compression object in an inconsistent state
622 c = zlib.compressobj()
623 c.compress(HAMLET_SCENE)
624 c.flush()
625 self.assertRaises(ValueError, c.copy)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000626
Serhiy Storchaka43767632013-11-03 21:31:38 +0200627 @requires_Decompress_copy
628 def test_decompresscopy(self):
629 # Test copying a decompression object
630 data = HAMLET_SCENE
631 comp = zlib.compress(data)
632 # Test type of return value
633 self.assertIsInstance(comp, bytes)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000634
Serhiy Storchaka43767632013-11-03 21:31:38 +0200635 d0 = zlib.decompressobj()
636 bufs0 = []
637 bufs0.append(d0.decompress(comp[:32]))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000638
Serhiy Storchaka43767632013-11-03 21:31:38 +0200639 d1 = d0.copy()
640 bufs1 = bufs0[:]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000641
Serhiy Storchaka43767632013-11-03 21:31:38 +0200642 bufs0.append(d0.decompress(comp[32:]))
643 s0 = b''.join(bufs0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644
Serhiy Storchaka43767632013-11-03 21:31:38 +0200645 bufs1.append(d1.decompress(comp[32:]))
646 s1 = b''.join(bufs1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000647
Serhiy Storchaka43767632013-11-03 21:31:38 +0200648 self.assertEqual(s0,s1)
649 self.assertEqual(s0,data)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000650
Serhiy Storchaka43767632013-11-03 21:31:38 +0200651 @requires_Decompress_copy
652 def test_baddecompresscopy(self):
653 # Test copying a compression object in an inconsistent state
654 data = zlib.compress(HAMLET_SCENE)
655 d = zlib.decompressobj()
656 d.decompress(data)
657 d.flush()
658 self.assertRaises(ValueError, d.copy)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000659
Serhiy Storchakad7a44152015-11-12 11:23:04 +0200660 def test_compresspickle(self):
661 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
662 with self.assertRaises((TypeError, pickle.PicklingError)):
663 pickle.dumps(zlib.compressobj(zlib.Z_BEST_COMPRESSION), proto)
664
665 def test_decompresspickle(self):
666 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
667 with self.assertRaises((TypeError, pickle.PicklingError)):
668 pickle.dumps(zlib.decompressobj(), proto)
669
Antoine Pitrou89562712010-05-07 17:04:02 +0000670 # Memory use of the following functions takes into account overallocation
671
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200672 @bigmemtest(size=_1G + 1024 * 1024, memuse=3)
Antoine Pitrou89562712010-05-07 17:04:02 +0000673 def test_big_compress_buffer(self, size):
674 c = zlib.compressobj(1)
675 compress = lambda s: c.compress(s) + c.flush()
676 self.check_big_compress_buffer(size, compress)
677
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200678 @bigmemtest(size=_1G + 1024 * 1024, memuse=2)
Antoine Pitrou89562712010-05-07 17:04:02 +0000679 def test_big_decompress_buffer(self, size):
680 d = zlib.decompressobj()
681 decompress = lambda s: d.decompress(s) + d.flush()
682 self.check_big_decompress_buffer(size, decompress)
683
Martin Panter84544c12016-07-23 03:02:07 +0000684 @unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform')
685 @bigmemtest(size=_4G + 100, memuse=4)
686 def test_64bit_compress(self, size):
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200687 data = b'x' * size
Martin Panter84544c12016-07-23 03:02:07 +0000688 co = zlib.compressobj(0)
689 do = zlib.decompressobj()
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200690 try:
Martin Panter84544c12016-07-23 03:02:07 +0000691 comp = co.compress(data) + co.flush()
692 uncomp = do.decompress(comp) + do.flush()
693 self.assertEqual(uncomp, data)
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200694 finally:
Martin Panter84544c12016-07-23 03:02:07 +0000695 comp = uncomp = data = None
696
697 @unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform')
698 @bigmemtest(size=_4G + 100, memuse=3)
699 def test_large_unused_data(self, size):
700 data = b'abcdefghijklmnop'
701 unused = b'x' * size
702 comp = zlib.compress(data) + unused
703 do = zlib.decompressobj()
704 try:
705 uncomp = do.decompress(comp) + do.flush()
706 self.assertEqual(unused, do.unused_data)
707 self.assertEqual(uncomp, data)
708 finally:
709 unused = comp = do = None
710
711 @unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform')
712 @bigmemtest(size=_4G + 100, memuse=5)
713 def test_large_unconsumed_tail(self, size):
714 data = b'x' * size
715 do = zlib.decompressobj()
716 try:
717 comp = zlib.compress(data, 0)
718 uncomp = do.decompress(comp, 1) + do.flush()
719 self.assertEqual(uncomp, data)
720 self.assertEqual(do.unconsumed_tail, b'')
721 finally:
722 comp = uncomp = data = None
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200723
Martin Panter0fdf41d2016-05-27 07:32:11 +0000724 def test_wbits(self):
Martin Panterc618ae82016-05-27 11:20:21 +0000725 # wbits=0 only supported since zlib v1.2.3.5
726 # Register "1.2.3" as "1.2.3.0"
727 v = (zlib.ZLIB_RUNTIME_VERSION + ".0").split(".", 4)
728 supports_wbits_0 = int(v[0]) > 1 or int(v[0]) == 1 \
729 and (int(v[1]) > 2 or int(v[1]) == 2
730 and (int(v[2]) > 3 or int(v[2]) == 3 and int(v[3]) >= 5))
731
Martin Panter0fdf41d2016-05-27 07:32:11 +0000732 co = zlib.compressobj(level=1, wbits=15)
733 zlib15 = co.compress(HAMLET_SCENE) + co.flush()
734 self.assertEqual(zlib.decompress(zlib15, 15), HAMLET_SCENE)
Martin Panterc618ae82016-05-27 11:20:21 +0000735 if supports_wbits_0:
736 self.assertEqual(zlib.decompress(zlib15, 0), HAMLET_SCENE)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000737 self.assertEqual(zlib.decompress(zlib15, 32 + 15), HAMLET_SCENE)
738 with self.assertRaisesRegex(zlib.error, 'invalid window size'):
739 zlib.decompress(zlib15, 14)
740 dco = zlib.decompressobj(wbits=32 + 15)
741 self.assertEqual(dco.decompress(zlib15), HAMLET_SCENE)
742 dco = zlib.decompressobj(wbits=14)
743 with self.assertRaisesRegex(zlib.error, 'invalid window size'):
744 dco.decompress(zlib15)
745
746 co = zlib.compressobj(level=1, wbits=9)
747 zlib9 = co.compress(HAMLET_SCENE) + co.flush()
748 self.assertEqual(zlib.decompress(zlib9, 9), HAMLET_SCENE)
749 self.assertEqual(zlib.decompress(zlib9, 15), HAMLET_SCENE)
Martin Panterc618ae82016-05-27 11:20:21 +0000750 if supports_wbits_0:
751 self.assertEqual(zlib.decompress(zlib9, 0), HAMLET_SCENE)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000752 self.assertEqual(zlib.decompress(zlib9, 32 + 9), HAMLET_SCENE)
753 dco = zlib.decompressobj(wbits=32 + 9)
754 self.assertEqual(dco.decompress(zlib9), HAMLET_SCENE)
755
756 co = zlib.compressobj(level=1, wbits=-15)
757 deflate15 = co.compress(HAMLET_SCENE) + co.flush()
758 self.assertEqual(zlib.decompress(deflate15, -15), HAMLET_SCENE)
759 dco = zlib.decompressobj(wbits=-15)
760 self.assertEqual(dco.decompress(deflate15), HAMLET_SCENE)
761
762 co = zlib.compressobj(level=1, wbits=-9)
763 deflate9 = co.compress(HAMLET_SCENE) + co.flush()
764 self.assertEqual(zlib.decompress(deflate9, -9), HAMLET_SCENE)
765 self.assertEqual(zlib.decompress(deflate9, -15), HAMLET_SCENE)
766 dco = zlib.decompressobj(wbits=-9)
767 self.assertEqual(dco.decompress(deflate9), HAMLET_SCENE)
768
769 co = zlib.compressobj(level=1, wbits=16 + 15)
770 gzip = co.compress(HAMLET_SCENE) + co.flush()
771 self.assertEqual(zlib.decompress(gzip, 16 + 15), HAMLET_SCENE)
772 self.assertEqual(zlib.decompress(gzip, 32 + 15), HAMLET_SCENE)
773 dco = zlib.decompressobj(32 + 15)
774 self.assertEqual(dco.decompress(gzip), HAMLET_SCENE)
775
Antoine Pitrou89562712010-05-07 17:04:02 +0000776
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000777def genblock(seed, length, step=1024, generator=random):
778 """length-byte stream of random data from a seed (in step-byte blocks)."""
779 if seed is not None:
780 generator.seed(seed)
781 randint = generator.randint
782 if length < step or step < 2:
783 step = length
Guido van Rossum776152b2007-05-22 22:44:07 +0000784 blocks = bytes()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000785 for i in range(0, length, step):
Guido van Rossum776152b2007-05-22 22:44:07 +0000786 blocks += bytes(randint(0, 255) for x in range(step))
787 return blocks
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000788
789
790
791def choose_lines(source, number, seed=None, generator=random):
792 """Return a list of number lines randomly chosen from the source"""
793 if seed is not None:
794 generator.seed(seed)
795 sources = source.split('\n')
796 return [generator.choice(sources) for n in range(number)]
797
798
799
Guido van Rossum776152b2007-05-22 22:44:07 +0000800HAMLET_SCENE = b"""
Fred Drake004d5e62000-10-23 17:22:08 +0000801LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000802
803 O, fear me not.
804 I stay too long: but here my father comes.
805
806 Enter POLONIUS
807
808 A double blessing is a double grace,
809 Occasion smiles upon a second leave.
810
Fred Drake004d5e62000-10-23 17:22:08 +0000811LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000812
813 Yet here, Laertes! aboard, aboard, for shame!
814 The wind sits in the shoulder of your sail,
815 And you are stay'd for. There; my blessing with thee!
816 And these few precepts in thy memory
817 See thou character. Give thy thoughts no tongue,
818 Nor any unproportioned thought his act.
819 Be thou familiar, but by no means vulgar.
820 Those friends thou hast, and their adoption tried,
821 Grapple them to thy soul with hoops of steel;
822 But do not dull thy palm with entertainment
823 Of each new-hatch'd, unfledged comrade. Beware
824 Of entrance to a quarrel, but being in,
825 Bear't that the opposed may beware of thee.
826 Give every man thy ear, but few thy voice;
827 Take each man's censure, but reserve thy judgment.
828 Costly thy habit as thy purse can buy,
829 But not express'd in fancy; rich, not gaudy;
830 For the apparel oft proclaims the man,
831 And they in France of the best rank and station
832 Are of a most select and generous chief in that.
833 Neither a borrower nor a lender be;
834 For loan oft loses both itself and friend,
835 And borrowing dulls the edge of husbandry.
836 This above all: to thine ownself be true,
837 And it must follow, as the night the day,
838 Thou canst not then be false to any man.
839 Farewell: my blessing season this in thee!
840
Fred Drake004d5e62000-10-23 17:22:08 +0000841LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000842
843 Most humbly do I take my leave, my lord.
844
Fred Drake004d5e62000-10-23 17:22:08 +0000845LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000846
847 The time invites you; go; your servants tend.
848
Fred Drake004d5e62000-10-23 17:22:08 +0000849LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000850
851 Farewell, Ophelia; and remember well
852 What I have said to you.
853
Fred Drake004d5e62000-10-23 17:22:08 +0000854OPHELIA
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000855
856 'Tis in my memory lock'd,
857 And you yourself shall keep the key of it.
858
Fred Drake004d5e62000-10-23 17:22:08 +0000859LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000860
861 Farewell.
862"""
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000863
864
Martin Pantere99e9772015-11-20 08:13:35 +0000865class CustomInt:
866 def __int__(self):
867 return 100
868
869
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000870if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500871 unittest.main()