blob: 4d3611c4a6948fa5da4374ebc2635ca3f3468d60 [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'):
125 zlib.decompressobj().flush(sys.maxsize + 1)
126
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000127
Antoine Pitrou89562712010-05-07 17:04:02 +0000128class BaseCompressTestCase(object):
129 def check_big_compress_buffer(self, size, compress_func):
130 _1M = 1024 * 1024
Antoine Pitrou89562712010-05-07 17:04:02 +0000131 # Generate 10MB worth of random, and expand it by repeating it.
132 # The assumption is that zlib's memory is not big enough to exploit
133 # such spread out redundancy.
134 data = b''.join([random.getrandbits(8 * _1M).to_bytes(_1M, 'little')
135 for i in range(10)])
136 data = data * (size // len(data) + 1)
137 try:
138 compress_func(data)
139 finally:
140 # Release memory
141 data = None
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000142
Antoine Pitrou89562712010-05-07 17:04:02 +0000143 def check_big_decompress_buffer(self, size, decompress_func):
144 data = b'x' * size
145 try:
146 compressed = zlib.compress(data, 1)
147 finally:
148 # Release memory
149 data = None
150 data = decompress_func(compressed)
151 # Sanity check
152 try:
153 self.assertEqual(len(data), size)
154 self.assertEqual(len(data.strip(b'x')), 0)
155 finally:
156 data = None
157
158
159class CompressTestCase(BaseCompressTestCase, unittest.TestCase):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000160 # Test compression in one go (whole message compression)
161 def test_speech(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000162 x = zlib.compress(HAMLET_SCENE)
163 self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000164
Martin Panter1fe0d132016-02-10 10:06:36 +0000165 def test_keywords(self):
166 x = zlib.compress(data=HAMLET_SCENE, level=3)
167 self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
168
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000169 def test_speech128(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000170 # compress more data
171 data = HAMLET_SCENE * 128
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000172 x = zlib.compress(data)
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000173 self.assertEqual(zlib.compress(bytearray(data)), x)
174 for ob in x, bytearray(x):
175 self.assertEqual(zlib.decompress(ob), data)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000176
Antoine Pitrou53b21662010-05-11 23:46:02 +0000177 def test_incomplete_stream(self):
Martin Panter6245cb32016-04-15 02:14:19 +0000178 # A useful error message is given
Antoine Pitrou53b21662010-05-11 23:46:02 +0000179 x = zlib.compress(HAMLET_SCENE)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000180 self.assertRaisesRegex(zlib.error,
Antoine Pitrou53b21662010-05-11 23:46:02 +0000181 "Error -5 while decompressing data: incomplete or truncated stream",
182 zlib.decompress, x[:-1])
183
Antoine Pitrou89562712010-05-07 17:04:02 +0000184 # Memory use of the following functions takes into account overallocation
185
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200186 @bigmemtest(size=_1G + 1024 * 1024, memuse=3)
Antoine Pitrou89562712010-05-07 17:04:02 +0000187 def test_big_compress_buffer(self, size):
188 compress = lambda s: zlib.compress(s, 1)
189 self.check_big_compress_buffer(size, compress)
190
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200191 @bigmemtest(size=_1G + 1024 * 1024, memuse=2)
Antoine Pitrou89562712010-05-07 17:04:02 +0000192 def test_big_decompress_buffer(self, size):
193 self.check_big_decompress_buffer(size, zlib.decompress)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000194
Nadeem Vawda197e22c2012-02-23 14:23:17 +0200195 @bigmemtest(size=_4G + 100, memuse=1, dry_run=False)
Victor Stinner8848c7a2011-01-04 02:07:36 +0000196 def test_length_overflow(self, size):
Victor Stinner8848c7a2011-01-04 02:07:36 +0000197 data = b'x' * size
198 try:
199 self.assertRaises(OverflowError, zlib.compress, data, 1)
Nadeem Vawda154bdf92011-05-14 23:07:36 +0200200 self.assertRaises(OverflowError, zlib.decompress, data)
Victor Stinner8848c7a2011-01-04 02:07:36 +0000201 finally:
202 data = None
203
Martin Pantere99e9772015-11-20 08:13:35 +0000204 @bigmemtest(size=_4G, memuse=1)
205 def test_large_bufsize(self, size):
206 # Test decompress(bufsize) parameter greater than the internal limit
207 data = HAMLET_SCENE * 10
208 compressed = zlib.compress(data, 1)
209 self.assertEqual(zlib.decompress(compressed, 15, size), data)
210
211 def test_custom_bufsize(self):
212 data = HAMLET_SCENE * 10
213 compressed = zlib.compress(data, 1)
214 self.assertEqual(zlib.decompress(compressed, 15, CustomInt()), data)
215
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000216
Antoine Pitrou89562712010-05-07 17:04:02 +0000217class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000218 # Test compression object
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000219 def test_pair(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000220 # straightforward compress/decompress objects
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000221 datasrc = HAMLET_SCENE * 128
222 datazip = zlib.compress(datasrc)
223 # should compress both bytes and bytearray data
224 for data in (datasrc, bytearray(datasrc)):
225 co = zlib.compressobj()
226 x1 = co.compress(data)
227 x2 = co.flush()
228 self.assertRaises(zlib.error, co.flush) # second flush should not work
229 self.assertEqual(x1 + x2, datazip)
230 for v1, v2 in ((x1, x2), (bytearray(x1), bytearray(x2))):
231 dco = zlib.decompressobj()
232 y1 = dco.decompress(v1 + v2)
233 y2 = dco.flush()
234 self.assertEqual(data, y1 + y2)
235 self.assertIsInstance(dco.unconsumed_tail, bytes)
236 self.assertIsInstance(dco.unused_data, bytes)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000237
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000238 def test_compressoptions(self):
239 # specify lots of options to compressobj()
240 level = 2
241 method = zlib.DEFLATED
242 wbits = -12
Martin Panterbf19d162015-09-09 01:01:13 +0000243 memLevel = 9
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000244 strategy = zlib.Z_FILTERED
Martin Panterbf19d162015-09-09 01:01:13 +0000245 co = zlib.compressobj(level, method, wbits, memLevel, strategy)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000246 x1 = co.compress(HAMLET_SCENE)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000247 x2 = co.flush()
248 dco = zlib.decompressobj(wbits)
249 y1 = dco.decompress(x1 + x2)
250 y2 = dco.flush()
Neil Schemenauer6412b122004-06-05 19:34:28 +0000251 self.assertEqual(HAMLET_SCENE, y1 + y2)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000252
Martin Panterbf19d162015-09-09 01:01:13 +0000253 # keyword arguments should also be supported
254 zlib.compressobj(level=level, method=method, wbits=wbits,
255 memLevel=memLevel, strategy=strategy, zdict=b"")
256
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000257 def test_compressincremental(self):
258 # compress object in steps, decompress object as one-shot
Neil Schemenauer6412b122004-06-05 19:34:28 +0000259 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000260 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000261 bufs = []
262 for i in range(0, len(data), 256):
263 bufs.append(co.compress(data[i:i+256]))
264 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000265 combuf = b''.join(bufs)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000266
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000267 dco = zlib.decompressobj()
Guido van Rossum776152b2007-05-22 22:44:07 +0000268 y1 = dco.decompress(b''.join(bufs))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000269 y2 = dco.flush()
270 self.assertEqual(data, y1 + y2)
271
Neil Schemenauer6412b122004-06-05 19:34:28 +0000272 def test_decompinc(self, flush=False, source=None, cx=256, dcx=64):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000273 # compress object in steps, decompress object in steps
Neil Schemenauer6412b122004-06-05 19:34:28 +0000274 source = source or HAMLET_SCENE
275 data = source * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000276 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000277 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000278 for i in range(0, len(data), cx):
279 bufs.append(co.compress(data[i:i+cx]))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000280 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000281 combuf = b''.join(bufs)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000282
Gregory P. Smith693fc462008-09-06 20:13:06 +0000283 decombuf = zlib.decompress(combuf)
284 # Test type of return value
Ezio Melottie9615932010-01-24 19:26:24 +0000285 self.assertIsInstance(decombuf, bytes)
Gregory P. Smith693fc462008-09-06 20:13:06 +0000286
287 self.assertEqual(data, decombuf)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000288
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000289 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000290 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000291 for i in range(0, len(combuf), dcx):
292 bufs.append(dco.decompress(combuf[i:i+dcx]))
Guido van Rossum776152b2007-05-22 22:44:07 +0000293 self.assertEqual(b'', dco.unconsumed_tail, ########
294 "(A) uct should be b'': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000295 len(dco.unconsumed_tail))
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000296 self.assertEqual(b'', dco.unused_data)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000297 if flush:
298 bufs.append(dco.flush())
299 else:
300 while True:
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000301 chunk = dco.decompress(b'')
Neil Schemenauer6412b122004-06-05 19:34:28 +0000302 if chunk:
303 bufs.append(chunk)
304 else:
305 break
Guido van Rossum776152b2007-05-22 22:44:07 +0000306 self.assertEqual(b'', dco.unconsumed_tail, ########
307 "(B) uct should be b'': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000308 len(dco.unconsumed_tail))
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000309 self.assertEqual(b'', dco.unused_data)
Guido van Rossum776152b2007-05-22 22:44:07 +0000310 self.assertEqual(data, b''.join(bufs))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000311 # Failure means: "decompressobj with init options failed"
312
Neil Schemenauer6412b122004-06-05 19:34:28 +0000313 def test_decompincflush(self):
314 self.test_decompinc(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000315
Neil Schemenauer6412b122004-06-05 19:34:28 +0000316 def test_decompimax(self, source=None, cx=256, dcx=64):
317 # compress in steps, decompress in length-restricted steps
318 source = source or HAMLET_SCENE
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000319 # Check a decompression object with max_length specified
Neil Schemenauer6412b122004-06-05 19:34:28 +0000320 data = source * 128
321 co = zlib.compressobj()
322 bufs = []
323 for i in range(0, len(data), cx):
324 bufs.append(co.compress(data[i:i+cx]))
325 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000326 combuf = b''.join(bufs)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000327 self.assertEqual(data, zlib.decompress(combuf),
328 'compressed data failure')
329
330 dco = zlib.decompressobj()
331 bufs = []
332 cb = combuf
333 while cb:
334 #max_length = 1 + len(cb)//10
335 chunk = dco.decompress(cb, dcx)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000336 self.assertFalse(len(chunk) > dcx,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000337 'chunk too big (%d>%d)' % (len(chunk), dcx))
338 bufs.append(chunk)
339 cb = dco.unconsumed_tail
340 bufs.append(dco.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000341 self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved')
Neil Schemenauer6412b122004-06-05 19:34:28 +0000342
343 def test_decompressmaxlen(self, flush=False):
344 # Check a decompression object with max_length specified
345 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000346 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000347 bufs = []
348 for i in range(0, len(data), 256):
349 bufs.append(co.compress(data[i:i+256]))
350 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000351 combuf = b''.join(bufs)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000352 self.assertEqual(data, zlib.decompress(combuf),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000353 'compressed data failure')
354
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000355 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000356 bufs = []
357 cb = combuf
358 while cb:
Guido van Rossumf3594102003-02-27 18:39:18 +0000359 max_length = 1 + len(cb)//10
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000360 chunk = dco.decompress(cb, max_length)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000361 self.assertFalse(len(chunk) > max_length,
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000362 'chunk too big (%d>%d)' % (len(chunk),max_length))
363 bufs.append(chunk)
364 cb = dco.unconsumed_tail
Neil Schemenauer6412b122004-06-05 19:34:28 +0000365 if flush:
366 bufs.append(dco.flush())
367 else:
368 while chunk:
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000369 chunk = dco.decompress(b'', max_length)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000370 self.assertFalse(len(chunk) > max_length,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000371 'chunk too big (%d>%d)' % (len(chunk),max_length))
372 bufs.append(chunk)
Guido van Rossum776152b2007-05-22 22:44:07 +0000373 self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved')
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000374
Neil Schemenauer6412b122004-06-05 19:34:28 +0000375 def test_decompressmaxlenflush(self):
376 self.test_decompressmaxlen(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000377
378 def test_maxlenmisc(self):
379 # Misc tests of max_length
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000380 dco = zlib.decompressobj()
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000381 self.assertRaises(ValueError, dco.decompress, b"", -1)
Guido van Rossum776152b2007-05-22 22:44:07 +0000382 self.assertEqual(b'', dco.unconsumed_tail)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000383
Martin Pantere99e9772015-11-20 08:13:35 +0000384 def test_maxlen_large(self):
385 # Sizes up to sys.maxsize should be accepted, although zlib is
386 # internally limited to expressing sizes with unsigned int
387 data = HAMLET_SCENE * 10
388 self.assertGreater(len(data), zlib.DEF_BUF_SIZE)
389 compressed = zlib.compress(data, 1)
390 dco = zlib.decompressobj()
391 self.assertEqual(dco.decompress(compressed, sys.maxsize), data)
392
393 def test_maxlen_custom(self):
394 data = HAMLET_SCENE * 10
395 compressed = zlib.compress(data, 1)
396 dco = zlib.decompressobj()
397 self.assertEqual(dco.decompress(compressed, CustomInt()), data[:100])
398
Nadeem Vawda7619e882011-05-14 14:05:20 +0200399 def test_clear_unconsumed_tail(self):
400 # Issue #12050: calling decompress() without providing max_length
401 # should clear the unconsumed_tail attribute.
402 cdata = b"x\x9cKLJ\x06\x00\x02M\x01" # "abc"
403 dco = zlib.decompressobj()
404 ddata = dco.decompress(cdata, 1)
405 ddata += dco.decompress(dco.unconsumed_tail)
406 self.assertEqual(dco.unconsumed_tail, b"")
407
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000408 def test_flushes(self):
409 # Test flush() with the various options, using all the
410 # different levels in order to provide more variations.
411 sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
412 sync_opt = [getattr(zlib, opt) for opt in sync_opt
413 if hasattr(zlib, opt)]
Neil Schemenauer6412b122004-06-05 19:34:28 +0000414 data = HAMLET_SCENE * 8
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000415
416 for sync in sync_opt:
417 for level in range(10):
418 obj = zlib.compressobj( level )
419 a = obj.compress( data[:3000] )
420 b = obj.flush( sync )
421 c = obj.compress( data[3000:] )
422 d = obj.flush()
Guido van Rossum776152b2007-05-22 22:44:07 +0000423 self.assertEqual(zlib.decompress(b''.join([a,b,c,d])),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000424 data, ("Decompress failed: flush "
425 "mode=%i, level=%i") % (sync, level))
426 del obj
427
Serhiy Storchaka43767632013-11-03 21:31:38 +0200428 @unittest.skipUnless(hasattr(zlib, 'Z_SYNC_FLUSH'),
429 'requires zlib.Z_SYNC_FLUSH')
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000430 def test_odd_flush(self):
431 # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
432 import random
Serhiy Storchaka43767632013-11-03 21:31:38 +0200433 # Testing on 17K of "random" data
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000434
Serhiy Storchaka43767632013-11-03 21:31:38 +0200435 # Create compressor and decompressor objects
436 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
437 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000438
Serhiy Storchaka43767632013-11-03 21:31:38 +0200439 # Try 17K of data
440 # generate random data stream
441 try:
442 # In 2.3 and later, WichmannHill is the RNG of the bug report
443 gen = random.WichmannHill()
444 except AttributeError:
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000445 try:
Serhiy Storchaka43767632013-11-03 21:31:38 +0200446 # 2.2 called it Random
447 gen = random.Random()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000448 except AttributeError:
Serhiy Storchaka43767632013-11-03 21:31:38 +0200449 # others might simply have a single RNG
450 gen = random
451 gen.seed(1)
452 data = genblock(1, 17 * 1024, generator=gen)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000453
Serhiy Storchaka43767632013-11-03 21:31:38 +0200454 # compress, sync-flush, and decompress
455 first = co.compress(data)
456 second = co.flush(zlib.Z_SYNC_FLUSH)
457 expanded = dco.decompress(first + second)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000458
Serhiy Storchaka43767632013-11-03 21:31:38 +0200459 # if decompressed data is different from the input data, choke.
460 self.assertEqual(expanded, data, "17K random source doesn't match")
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000461
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000462 def test_empty_flush(self):
463 # Test that calling .flush() on unused objects works.
464 # (Bug #1083110 -- calling .flush() on decompress objects
465 # caused a core dump.)
466
467 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000468 self.assertTrue(co.flush()) # Returns a zlib header
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000469 dco = zlib.decompressobj()
Guido van Rossum776152b2007-05-22 22:44:07 +0000470 self.assertEqual(dco.flush(), b"") # Returns nothing
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000471
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200472 def test_dictionary(self):
473 h = HAMLET_SCENE
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200474 # Build a simulated dictionary out of the words in HAMLET.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200475 words = h.split()
476 random.shuffle(words)
477 zdict = b''.join(words)
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200478 # Use it to compress HAMLET.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200479 co = zlib.compressobj(zdict=zdict)
480 cd = co.compress(h) + co.flush()
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200481 # Verify that it will decompress with the dictionary.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200482 dco = zlib.decompressobj(zdict=zdict)
483 self.assertEqual(dco.decompress(cd) + dco.flush(), h)
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200484 # Verify that it fails when not given the dictionary.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200485 dco = zlib.decompressobj()
486 self.assertRaises(zlib.error, dco.decompress, cd)
487
488 def test_dictionary_streaming(self):
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200489 # This simulates the reuse of a compressor object for compressing
490 # several separate data streams.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200491 co = zlib.compressobj(zdict=HAMLET_SCENE)
492 do = zlib.decompressobj(zdict=HAMLET_SCENE)
493 piece = HAMLET_SCENE[1000:1500]
494 d0 = co.compress(piece) + co.flush(zlib.Z_SYNC_FLUSH)
495 d1 = co.compress(piece[100:]) + co.flush(zlib.Z_SYNC_FLUSH)
496 d2 = co.compress(piece[:-100]) + co.flush(zlib.Z_SYNC_FLUSH)
497 self.assertEqual(do.decompress(d0), piece)
498 self.assertEqual(do.decompress(d1), piece[100:])
499 self.assertEqual(do.decompress(d2), piece[:-100])
500
Antoine Pitrouc09c92f2010-05-11 23:36:40 +0000501 def test_decompress_incomplete_stream(self):
502 # This is 'foo', deflated
503 x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E'
504 # For the record
505 self.assertEqual(zlib.decompress(x), b'foo')
506 self.assertRaises(zlib.error, zlib.decompress, x[:-5])
507 # Omitting the stream end works with decompressor objects
508 # (see issue #8672).
509 dco = zlib.decompressobj()
510 y = dco.decompress(x[:-5])
511 y += dco.flush()
512 self.assertEqual(y, b'foo')
513
Nadeem Vawda1c385462011-08-13 15:22:40 +0200514 def test_decompress_eof(self):
515 x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' # 'foo'
516 dco = zlib.decompressobj()
517 self.assertFalse(dco.eof)
518 dco.decompress(x[:-5])
519 self.assertFalse(dco.eof)
520 dco.decompress(x[-5:])
521 self.assertTrue(dco.eof)
522 dco.flush()
523 self.assertTrue(dco.eof)
524
525 def test_decompress_eof_incomplete_stream(self):
526 x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' # 'foo'
527 dco = zlib.decompressobj()
528 self.assertFalse(dco.eof)
529 dco.decompress(x[:-5])
530 self.assertFalse(dco.eof)
531 dco.flush()
532 self.assertFalse(dco.eof)
533
Nadeem Vawda39079942012-11-05 00:37:42 +0100534 def test_decompress_unused_data(self):
535 # Repeated calls to decompress() after EOF should accumulate data in
536 # dco.unused_data, instead of just storing the arg to the last call.
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100537 source = b'abcdefghijklmnopqrstuvwxyz'
538 remainder = b'0123456789'
539 y = zlib.compress(source)
540 x = y + remainder
541 for maxlen in 0, 1000:
542 for step in 1, 2, len(y), len(x):
543 dco = zlib.decompressobj()
544 data = b''
545 for i in range(0, len(x), step):
546 if i < len(y):
547 self.assertEqual(dco.unused_data, b'')
548 if maxlen == 0:
549 data += dco.decompress(x[i : i + step])
550 self.assertEqual(dco.unconsumed_tail, b'')
551 else:
552 data += dco.decompress(
553 dco.unconsumed_tail + x[i : i + step], maxlen)
554 data += dco.flush()
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100555 self.assertTrue(dco.eof)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100556 self.assertEqual(data, source)
557 self.assertEqual(dco.unconsumed_tail, b'')
558 self.assertEqual(dco.unused_data, remainder)
Nadeem Vawda39079942012-11-05 00:37:42 +0100559
Martin Panter3f0ee832016-06-05 10:48:34 +0000560 # issue27164
561 def test_decompress_raw_with_dictionary(self):
562 zdict = b'abcdefghijklmnopqrstuvwxyz'
563 co = zlib.compressobj(wbits=-zlib.MAX_WBITS, zdict=zdict)
564 comp = co.compress(zdict) + co.flush()
565 dco = zlib.decompressobj(wbits=-zlib.MAX_WBITS, zdict=zdict)
566 uncomp = dco.decompress(comp) + dco.flush()
567 self.assertEqual(zdict, uncomp)
568
Nadeem Vawda7ee95552012-11-11 03:15:32 +0100569 def test_flush_with_freed_input(self):
570 # Issue #16411: decompressor accesses input to last decompress() call
571 # in flush(), even if this object has been freed in the meanwhile.
572 input1 = b'abcdefghijklmnopqrstuvwxyz'
573 input2 = b'QWERTYUIOPASDFGHJKLZXCVBNM'
574 data = zlib.compress(input1)
575 dco = zlib.decompressobj()
576 dco.decompress(data, 1)
577 del data
578 data = zlib.compress(input2)
579 self.assertEqual(dco.flush(), input1[1:])
580
Martin Pantere99e9772015-11-20 08:13:35 +0000581 @bigmemtest(size=_4G, memuse=1)
582 def test_flush_large_length(self, size):
583 # Test flush(length) parameter greater than internal limit UINT_MAX
584 input = HAMLET_SCENE * 10
585 data = zlib.compress(input, 1)
586 dco = zlib.decompressobj()
587 dco.decompress(data, 1)
588 self.assertEqual(dco.flush(size), input[1:])
589
590 def test_flush_custom_length(self):
591 input = HAMLET_SCENE * 10
592 data = zlib.compress(input, 1)
593 dco = zlib.decompressobj()
594 dco.decompress(data, 1)
595 self.assertEqual(dco.flush(CustomInt()), input[1:])
596
Serhiy Storchaka43767632013-11-03 21:31:38 +0200597 @requires_Compress_copy
598 def test_compresscopy(self):
599 # Test copying a compression object
600 data0 = HAMLET_SCENE
601 data1 = bytes(str(HAMLET_SCENE, "ascii").swapcase(), "ascii")
602 c0 = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
603 bufs0 = []
604 bufs0.append(c0.compress(data0))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000605
Serhiy Storchaka43767632013-11-03 21:31:38 +0200606 c1 = c0.copy()
607 bufs1 = bufs0[:]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000608
Serhiy Storchaka43767632013-11-03 21:31:38 +0200609 bufs0.append(c0.compress(data0))
610 bufs0.append(c0.flush())
611 s0 = b''.join(bufs0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000612
Serhiy Storchaka43767632013-11-03 21:31:38 +0200613 bufs1.append(c1.compress(data1))
614 bufs1.append(c1.flush())
615 s1 = b''.join(bufs1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000616
Serhiy Storchaka43767632013-11-03 21:31:38 +0200617 self.assertEqual(zlib.decompress(s0),data0+data0)
618 self.assertEqual(zlib.decompress(s1),data0+data1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000619
Serhiy Storchaka43767632013-11-03 21:31:38 +0200620 @requires_Compress_copy
621 def test_badcompresscopy(self):
622 # Test copying a compression object in an inconsistent state
623 c = zlib.compressobj()
624 c.compress(HAMLET_SCENE)
625 c.flush()
626 self.assertRaises(ValueError, c.copy)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000627
Serhiy Storchaka43767632013-11-03 21:31:38 +0200628 @requires_Decompress_copy
629 def test_decompresscopy(self):
630 # Test copying a decompression object
631 data = HAMLET_SCENE
632 comp = zlib.compress(data)
633 # Test type of return value
634 self.assertIsInstance(comp, bytes)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000635
Serhiy Storchaka43767632013-11-03 21:31:38 +0200636 d0 = zlib.decompressobj()
637 bufs0 = []
638 bufs0.append(d0.decompress(comp[:32]))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000639
Serhiy Storchaka43767632013-11-03 21:31:38 +0200640 d1 = d0.copy()
641 bufs1 = bufs0[:]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000642
Serhiy Storchaka43767632013-11-03 21:31:38 +0200643 bufs0.append(d0.decompress(comp[32:]))
644 s0 = b''.join(bufs0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000645
Serhiy Storchaka43767632013-11-03 21:31:38 +0200646 bufs1.append(d1.decompress(comp[32:]))
647 s1 = b''.join(bufs1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000648
Serhiy Storchaka43767632013-11-03 21:31:38 +0200649 self.assertEqual(s0,s1)
650 self.assertEqual(s0,data)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000651
Serhiy Storchaka43767632013-11-03 21:31:38 +0200652 @requires_Decompress_copy
653 def test_baddecompresscopy(self):
654 # Test copying a compression object in an inconsistent state
655 data = zlib.compress(HAMLET_SCENE)
656 d = zlib.decompressobj()
657 d.decompress(data)
658 d.flush()
659 self.assertRaises(ValueError, d.copy)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000660
Serhiy Storchakad7a44152015-11-12 11:23:04 +0200661 def test_compresspickle(self):
662 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
663 with self.assertRaises((TypeError, pickle.PicklingError)):
664 pickle.dumps(zlib.compressobj(zlib.Z_BEST_COMPRESSION), proto)
665
666 def test_decompresspickle(self):
667 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
668 with self.assertRaises((TypeError, pickle.PicklingError)):
669 pickle.dumps(zlib.decompressobj(), proto)
670
Antoine Pitrou89562712010-05-07 17:04:02 +0000671 # Memory use of the following functions takes into account overallocation
672
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200673 @bigmemtest(size=_1G + 1024 * 1024, memuse=3)
Antoine Pitrou89562712010-05-07 17:04:02 +0000674 def test_big_compress_buffer(self, size):
675 c = zlib.compressobj(1)
676 compress = lambda s: c.compress(s) + c.flush()
677 self.check_big_compress_buffer(size, compress)
678
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200679 @bigmemtest(size=_1G + 1024 * 1024, memuse=2)
Antoine Pitrou89562712010-05-07 17:04:02 +0000680 def test_big_decompress_buffer(self, size):
681 d = zlib.decompressobj()
682 decompress = lambda s: d.decompress(s) + d.flush()
683 self.check_big_decompress_buffer(size, decompress)
684
Nadeem Vawda197e22c2012-02-23 14:23:17 +0200685 @bigmemtest(size=_4G + 100, memuse=1, dry_run=False)
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200686 def test_length_overflow(self, size):
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200687 data = b'x' * size
Nadeem Vawda1161a9c2011-05-15 00:48:24 +0200688 c = zlib.compressobj(1)
689 d = zlib.decompressobj()
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200690 try:
Nadeem Vawda1161a9c2011-05-15 00:48:24 +0200691 self.assertRaises(OverflowError, c.compress, data)
692 self.assertRaises(OverflowError, d.decompress, data)
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200693 finally:
694 data = None
695
Martin Panter0fdf41d2016-05-27 07:32:11 +0000696 def test_wbits(self):
Martin Panterc618ae82016-05-27 11:20:21 +0000697 # wbits=0 only supported since zlib v1.2.3.5
698 # Register "1.2.3" as "1.2.3.0"
699 v = (zlib.ZLIB_RUNTIME_VERSION + ".0").split(".", 4)
700 supports_wbits_0 = int(v[0]) > 1 or int(v[0]) == 1 \
701 and (int(v[1]) > 2 or int(v[1]) == 2
702 and (int(v[2]) > 3 or int(v[2]) == 3 and int(v[3]) >= 5))
703
Martin Panter0fdf41d2016-05-27 07:32:11 +0000704 co = zlib.compressobj(level=1, wbits=15)
705 zlib15 = co.compress(HAMLET_SCENE) + co.flush()
706 self.assertEqual(zlib.decompress(zlib15, 15), HAMLET_SCENE)
Martin Panterc618ae82016-05-27 11:20:21 +0000707 if supports_wbits_0:
708 self.assertEqual(zlib.decompress(zlib15, 0), HAMLET_SCENE)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000709 self.assertEqual(zlib.decompress(zlib15, 32 + 15), HAMLET_SCENE)
710 with self.assertRaisesRegex(zlib.error, 'invalid window size'):
711 zlib.decompress(zlib15, 14)
712 dco = zlib.decompressobj(wbits=32 + 15)
713 self.assertEqual(dco.decompress(zlib15), HAMLET_SCENE)
714 dco = zlib.decompressobj(wbits=14)
715 with self.assertRaisesRegex(zlib.error, 'invalid window size'):
716 dco.decompress(zlib15)
717
718 co = zlib.compressobj(level=1, wbits=9)
719 zlib9 = co.compress(HAMLET_SCENE) + co.flush()
720 self.assertEqual(zlib.decompress(zlib9, 9), HAMLET_SCENE)
721 self.assertEqual(zlib.decompress(zlib9, 15), HAMLET_SCENE)
Martin Panterc618ae82016-05-27 11:20:21 +0000722 if supports_wbits_0:
723 self.assertEqual(zlib.decompress(zlib9, 0), HAMLET_SCENE)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000724 self.assertEqual(zlib.decompress(zlib9, 32 + 9), HAMLET_SCENE)
725 dco = zlib.decompressobj(wbits=32 + 9)
726 self.assertEqual(dco.decompress(zlib9), HAMLET_SCENE)
727
728 co = zlib.compressobj(level=1, wbits=-15)
729 deflate15 = co.compress(HAMLET_SCENE) + co.flush()
730 self.assertEqual(zlib.decompress(deflate15, -15), HAMLET_SCENE)
731 dco = zlib.decompressobj(wbits=-15)
732 self.assertEqual(dco.decompress(deflate15), HAMLET_SCENE)
733
734 co = zlib.compressobj(level=1, wbits=-9)
735 deflate9 = co.compress(HAMLET_SCENE) + co.flush()
736 self.assertEqual(zlib.decompress(deflate9, -9), HAMLET_SCENE)
737 self.assertEqual(zlib.decompress(deflate9, -15), HAMLET_SCENE)
738 dco = zlib.decompressobj(wbits=-9)
739 self.assertEqual(dco.decompress(deflate9), HAMLET_SCENE)
740
741 co = zlib.compressobj(level=1, wbits=16 + 15)
742 gzip = co.compress(HAMLET_SCENE) + co.flush()
743 self.assertEqual(zlib.decompress(gzip, 16 + 15), HAMLET_SCENE)
744 self.assertEqual(zlib.decompress(gzip, 32 + 15), HAMLET_SCENE)
745 dco = zlib.decompressobj(32 + 15)
746 self.assertEqual(dco.decompress(gzip), HAMLET_SCENE)
747
Antoine Pitrou89562712010-05-07 17:04:02 +0000748
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000749def genblock(seed, length, step=1024, generator=random):
750 """length-byte stream of random data from a seed (in step-byte blocks)."""
751 if seed is not None:
752 generator.seed(seed)
753 randint = generator.randint
754 if length < step or step < 2:
755 step = length
Guido van Rossum776152b2007-05-22 22:44:07 +0000756 blocks = bytes()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000757 for i in range(0, length, step):
Guido van Rossum776152b2007-05-22 22:44:07 +0000758 blocks += bytes(randint(0, 255) for x in range(step))
759 return blocks
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000760
761
762
763def choose_lines(source, number, seed=None, generator=random):
764 """Return a list of number lines randomly chosen from the source"""
765 if seed is not None:
766 generator.seed(seed)
767 sources = source.split('\n')
768 return [generator.choice(sources) for n in range(number)]
769
770
771
Guido van Rossum776152b2007-05-22 22:44:07 +0000772HAMLET_SCENE = b"""
Fred Drake004d5e62000-10-23 17:22:08 +0000773LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000774
775 O, fear me not.
776 I stay too long: but here my father comes.
777
778 Enter POLONIUS
779
780 A double blessing is a double grace,
781 Occasion smiles upon a second leave.
782
Fred Drake004d5e62000-10-23 17:22:08 +0000783LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000784
785 Yet here, Laertes! aboard, aboard, for shame!
786 The wind sits in the shoulder of your sail,
787 And you are stay'd for. There; my blessing with thee!
788 And these few precepts in thy memory
789 See thou character. Give thy thoughts no tongue,
790 Nor any unproportioned thought his act.
791 Be thou familiar, but by no means vulgar.
792 Those friends thou hast, and their adoption tried,
793 Grapple them to thy soul with hoops of steel;
794 But do not dull thy palm with entertainment
795 Of each new-hatch'd, unfledged comrade. Beware
796 Of entrance to a quarrel, but being in,
797 Bear't that the opposed may beware of thee.
798 Give every man thy ear, but few thy voice;
799 Take each man's censure, but reserve thy judgment.
800 Costly thy habit as thy purse can buy,
801 But not express'd in fancy; rich, not gaudy;
802 For the apparel oft proclaims the man,
803 And they in France of the best rank and station
804 Are of a most select and generous chief in that.
805 Neither a borrower nor a lender be;
806 For loan oft loses both itself and friend,
807 And borrowing dulls the edge of husbandry.
808 This above all: to thine ownself be true,
809 And it must follow, as the night the day,
810 Thou canst not then be false to any man.
811 Farewell: my blessing season this in thee!
812
Fred Drake004d5e62000-10-23 17:22:08 +0000813LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000814
815 Most humbly do I take my leave, my lord.
816
Fred Drake004d5e62000-10-23 17:22:08 +0000817LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000818
819 The time invites you; go; your servants tend.
820
Fred Drake004d5e62000-10-23 17:22:08 +0000821LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000822
823 Farewell, Ophelia; and remember well
824 What I have said to you.
825
Fred Drake004d5e62000-10-23 17:22:08 +0000826OPHELIA
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000827
828 'Tis in my memory lock'd,
829 And you yourself shall keep the key of it.
830
Fred Drake004d5e62000-10-23 17:22:08 +0000831LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000832
833 Farewell.
834"""
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000835
836
Martin Pantere99e9772015-11-20 08:13:35 +0000837class CustomInt:
838 def __int__(self):
839 return 100
840
841
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000842if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500843 unittest.main()