Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 3 | import binascii |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 4 | import random |
Antoine Pitrou | f3d2275 | 2011-02-21 18:09:00 +0000 | [diff] [blame] | 5 | import sys |
Antoine Pitrou | 94190bb | 2011-10-04 10:22:36 +0200 | [diff] [blame] | 6 | from test.support import bigmemtest, _1G, _4G |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 7 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 8 | zlib = support.import_module('zlib') |
| 9 | |
Antoine Pitrou | f3d2275 | 2011-02-21 18:09:00 +0000 | [diff] [blame] | 10 | try: |
| 11 | import mmap |
| 12 | except ImportError: |
| 13 | mmap = None |
| 14 | |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 16 | class ChecksumTestCase(unittest.TestCase): |
| 17 | # checksum test cases |
| 18 | def test_crc32start(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 19 | self.assertEqual(zlib.crc32(b""), zlib.crc32(b"", 0)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 20 | self.assertTrue(zlib.crc32(b"abc", 0xffffffff)) |
Andrew M. Kuchling | fcfc8d5 | 2001-08-10 15:50:11 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 22 | def test_crc32empty(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 23 | self.assertEqual(zlib.crc32(b"", 0), 0) |
| 24 | self.assertEqual(zlib.crc32(b"", 1), 1) |
| 25 | self.assertEqual(zlib.crc32(b"", 432), 432) |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 27 | def test_adler32start(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 28 | self.assertEqual(zlib.adler32(b""), zlib.adler32(b"", 1)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 29 | self.assertTrue(zlib.adler32(b"abc", 0xffffffff)) |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 31 | def test_adler32empty(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 32 | self.assertEqual(zlib.adler32(b"", 0), 0) |
| 33 | self.assertEqual(zlib.adler32(b"", 1), 1) |
| 34 | self.assertEqual(zlib.adler32(b"", 432), 432) |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 36 | def assertEqual32(self, seen, expected): |
| 37 | # 32-bit values masked -- checksums on 32- vs 64- bit machines |
| 38 | # This is important if bit 31 (0x08000000L) is set. |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 39 | self.assertEqual(seen & 0x0FFFFFFFF, expected & 0x0FFFFFFFF) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 40 | |
| 41 | def test_penguins(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 42 | self.assertEqual32(zlib.crc32(b"penguin", 0), 0x0e5c1a120) |
| 43 | self.assertEqual32(zlib.crc32(b"penguin", 1), 0x43b6aa94) |
| 44 | self.assertEqual32(zlib.adler32(b"penguin", 0), 0x0bcf02f6) |
| 45 | self.assertEqual32(zlib.adler32(b"penguin", 1), 0x0bd602f7) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 47 | self.assertEqual(zlib.crc32(b"penguin"), zlib.crc32(b"penguin", 0)) |
| 48 | self.assertEqual(zlib.adler32(b"penguin"),zlib.adler32(b"penguin",1)) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 49 | |
Gregory P. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 50 | def test_crc32_adler32_unsigned(self): |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 51 | foo = b'abcdefghijklmnop' |
Gregory P. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 52 | # explicitly test signed behavior |
Gregory P. Smith | 2727503 | 2008-03-20 06:20:09 +0000 | [diff] [blame] | 53 | self.assertEqual(zlib.crc32(foo), 2486878355) |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 54 | self.assertEqual(zlib.crc32(b'spam'), 1138425661) |
Gregory P. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 55 | self.assertEqual(zlib.adler32(foo+foo), 3573550353) |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 56 | self.assertEqual(zlib.adler32(b'spam'), 72286642) |
Gregory P. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 57 | |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 58 | def test_same_as_binascii_crc32(self): |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 59 | foo = b'abcdefghijklmnop' |
Gregory P. Smith | 2727503 | 2008-03-20 06:20:09 +0000 | [diff] [blame] | 60 | crc = 2486878355 |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 61 | self.assertEqual(binascii.crc32(foo), crc) |
| 62 | self.assertEqual(zlib.crc32(foo), crc) |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 63 | self.assertEqual(binascii.crc32(b'spam'), zlib.crc32(b'spam')) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 64 | |
| 65 | |
Antoine Pitrou | f3d2275 | 2011-02-21 18:09:00 +0000 | [diff] [blame] | 66 | # Issue #10276 - check that inputs >=4GB are handled correctly. |
| 67 | class ChecksumBigBufferTestCase(unittest.TestCase): |
| 68 | |
Nadeem Vawda | 05a4dd8 | 2012-02-27 00:42:58 +0200 | [diff] [blame] | 69 | @bigmemtest(size=_4G + 4, memuse=1, dry_run=False) |
| 70 | def test_big_buffer(self, size): |
| 71 | data = b"nyan" * (_1G + 1) |
| 72 | self.assertEqual(zlib.crc32(data), 1044521549) |
| 73 | self.assertEqual(zlib.adler32(data), 2256789997) |
Antoine Pitrou | f3d2275 | 2011-02-21 18:09:00 +0000 | [diff] [blame] | 74 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 76 | class ExceptionTestCase(unittest.TestCase): |
| 77 | # make sure we generate some expected errors |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 78 | def test_badlevel(self): |
| 79 | # specifying compression level out of range causes an error |
| 80 | # (but -1 is Z_DEFAULT_COMPRESSION and apparently the zlib |
| 81 | # accepts 0 too) |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 82 | self.assertRaises(zlib.error, zlib.compress, b'ERROR', 10) |
| 83 | |
| 84 | def test_badargs(self): |
| 85 | self.assertRaises(TypeError, zlib.adler32) |
| 86 | self.assertRaises(TypeError, zlib.crc32) |
| 87 | self.assertRaises(TypeError, zlib.compress) |
| 88 | self.assertRaises(TypeError, zlib.decompress) |
| 89 | for arg in (42, None, '', 'abc', (), []): |
| 90 | self.assertRaises(TypeError, zlib.adler32, arg) |
| 91 | self.assertRaises(TypeError, zlib.crc32, arg) |
| 92 | self.assertRaises(TypeError, zlib.compress, arg) |
| 93 | self.assertRaises(TypeError, zlib.decompress, arg) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 94 | |
| 95 | def test_badcompressobj(self): |
| 96 | # verify failure on building compress object with bad params |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 97 | self.assertRaises(ValueError, zlib.compressobj, 1, zlib.DEFLATED, 0) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 98 | # specifying total bits too large causes an error |
| 99 | self.assertRaises(ValueError, |
| 100 | zlib.compressobj, 1, zlib.DEFLATED, zlib.MAX_WBITS + 1) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 101 | |
| 102 | def test_baddecompressobj(self): |
| 103 | # verify failure on building decompress object with bad params |
Antoine Pitrou | 90ee4df | 2010-04-06 17:23:13 +0000 | [diff] [blame] | 104 | self.assertRaises(ValueError, zlib.decompressobj, -1) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 105 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 106 | def test_decompressobj_badflush(self): |
| 107 | # verify failure on calling decompressobj.flush with bad params |
| 108 | self.assertRaises(ValueError, zlib.decompressobj().flush, 0) |
| 109 | self.assertRaises(ValueError, zlib.decompressobj().flush, -1) |
| 110 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 111 | |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 112 | class BaseCompressTestCase(object): |
| 113 | def check_big_compress_buffer(self, size, compress_func): |
| 114 | _1M = 1024 * 1024 |
| 115 | fmt = "%%0%dx" % (2 * _1M) |
| 116 | # Generate 10MB worth of random, and expand it by repeating it. |
| 117 | # The assumption is that zlib's memory is not big enough to exploit |
| 118 | # such spread out redundancy. |
| 119 | data = b''.join([random.getrandbits(8 * _1M).to_bytes(_1M, 'little') |
| 120 | for i in range(10)]) |
| 121 | data = data * (size // len(data) + 1) |
| 122 | try: |
| 123 | compress_func(data) |
| 124 | finally: |
| 125 | # Release memory |
| 126 | data = None |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 128 | def check_big_decompress_buffer(self, size, decompress_func): |
| 129 | data = b'x' * size |
| 130 | try: |
| 131 | compressed = zlib.compress(data, 1) |
| 132 | finally: |
| 133 | # Release memory |
| 134 | data = None |
| 135 | data = decompress_func(compressed) |
| 136 | # Sanity check |
| 137 | try: |
| 138 | self.assertEqual(len(data), size) |
| 139 | self.assertEqual(len(data.strip(b'x')), 0) |
| 140 | finally: |
| 141 | data = None |
| 142 | |
| 143 | |
| 144 | class CompressTestCase(BaseCompressTestCase, unittest.TestCase): |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 145 | # Test compression in one go (whole message compression) |
| 146 | def test_speech(self): |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 147 | x = zlib.compress(HAMLET_SCENE) |
| 148 | self.assertEqual(zlib.decompress(x), HAMLET_SCENE) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 149 | |
| 150 | def test_speech128(self): |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 151 | # compress more data |
| 152 | data = HAMLET_SCENE * 128 |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 153 | x = zlib.compress(data) |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 154 | self.assertEqual(zlib.compress(bytearray(data)), x) |
| 155 | for ob in x, bytearray(x): |
| 156 | self.assertEqual(zlib.decompress(ob), data) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | 53b2166 | 2010-05-11 23:46:02 +0000 | [diff] [blame] | 158 | def test_incomplete_stream(self): |
| 159 | # An useful error message is given |
| 160 | x = zlib.compress(HAMLET_SCENE) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 161 | self.assertRaisesRegex(zlib.error, |
Antoine Pitrou | 53b2166 | 2010-05-11 23:46:02 +0000 | [diff] [blame] | 162 | "Error -5 while decompressing data: incomplete or truncated stream", |
| 163 | zlib.decompress, x[:-1]) |
| 164 | |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 165 | # Memory use of the following functions takes into account overallocation |
| 166 | |
Antoine Pitrou | 94190bb | 2011-10-04 10:22:36 +0200 | [diff] [blame] | 167 | @bigmemtest(size=_1G + 1024 * 1024, memuse=3) |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 168 | def test_big_compress_buffer(self, size): |
| 169 | compress = lambda s: zlib.compress(s, 1) |
| 170 | self.check_big_compress_buffer(size, compress) |
| 171 | |
Antoine Pitrou | 94190bb | 2011-10-04 10:22:36 +0200 | [diff] [blame] | 172 | @bigmemtest(size=_1G + 1024 * 1024, memuse=2) |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 173 | def test_big_decompress_buffer(self, size): |
| 174 | self.check_big_decompress_buffer(size, zlib.decompress) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 175 | |
Antoine Pitrou | 94190bb | 2011-10-04 10:22:36 +0200 | [diff] [blame] | 176 | @bigmemtest(size=_4G + 100, memuse=1) |
Victor Stinner | 8848c7a | 2011-01-04 02:07:36 +0000 | [diff] [blame] | 177 | def test_length_overflow(self, size): |
| 178 | if size < _4G + 100: |
| 179 | self.skipTest("not enough free memory, need at least 4 GB") |
| 180 | data = b'x' * size |
| 181 | try: |
| 182 | self.assertRaises(OverflowError, zlib.compress, data, 1) |
Nadeem Vawda | 154bdf9 | 2011-05-14 23:07:36 +0200 | [diff] [blame] | 183 | self.assertRaises(OverflowError, zlib.decompress, data) |
Victor Stinner | 8848c7a | 2011-01-04 02:07:36 +0000 | [diff] [blame] | 184 | finally: |
| 185 | data = None |
| 186 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 187 | |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 188 | class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase): |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 189 | # Test compression object |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 190 | def test_pair(self): |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 191 | # straightforward compress/decompress objects |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 192 | datasrc = HAMLET_SCENE * 128 |
| 193 | datazip = zlib.compress(datasrc) |
| 194 | # should compress both bytes and bytearray data |
| 195 | for data in (datasrc, bytearray(datasrc)): |
| 196 | co = zlib.compressobj() |
| 197 | x1 = co.compress(data) |
| 198 | x2 = co.flush() |
| 199 | self.assertRaises(zlib.error, co.flush) # second flush should not work |
| 200 | self.assertEqual(x1 + x2, datazip) |
| 201 | for v1, v2 in ((x1, x2), (bytearray(x1), bytearray(x2))): |
| 202 | dco = zlib.decompressobj() |
| 203 | y1 = dco.decompress(v1 + v2) |
| 204 | y2 = dco.flush() |
| 205 | self.assertEqual(data, y1 + y2) |
| 206 | self.assertIsInstance(dco.unconsumed_tail, bytes) |
| 207 | self.assertIsInstance(dco.unused_data, bytes) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 208 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 209 | def test_compressoptions(self): |
| 210 | # specify lots of options to compressobj() |
| 211 | level = 2 |
| 212 | method = zlib.DEFLATED |
| 213 | wbits = -12 |
| 214 | memlevel = 9 |
| 215 | strategy = zlib.Z_FILTERED |
| 216 | co = zlib.compressobj(level, method, wbits, memlevel, strategy) |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 217 | x1 = co.compress(HAMLET_SCENE) |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 218 | x2 = co.flush() |
| 219 | dco = zlib.decompressobj(wbits) |
| 220 | y1 = dco.decompress(x1 + x2) |
| 221 | y2 = dco.flush() |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 222 | self.assertEqual(HAMLET_SCENE, y1 + y2) |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 223 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 224 | def test_compressincremental(self): |
| 225 | # compress object in steps, decompress object as one-shot |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 226 | data = HAMLET_SCENE * 128 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 227 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 228 | bufs = [] |
| 229 | for i in range(0, len(data), 256): |
| 230 | bufs.append(co.compress(data[i:i+256])) |
| 231 | bufs.append(co.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 232 | combuf = b''.join(bufs) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 233 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 234 | dco = zlib.decompressobj() |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 235 | y1 = dco.decompress(b''.join(bufs)) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 236 | y2 = dco.flush() |
| 237 | self.assertEqual(data, y1 + y2) |
| 238 | |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 239 | def test_decompinc(self, flush=False, source=None, cx=256, dcx=64): |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 240 | # compress object in steps, decompress object in steps |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 241 | source = source or HAMLET_SCENE |
| 242 | data = source * 128 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 243 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 244 | bufs = [] |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 245 | for i in range(0, len(data), cx): |
| 246 | bufs.append(co.compress(data[i:i+cx])) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 247 | bufs.append(co.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 248 | combuf = b''.join(bufs) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 249 | |
Gregory P. Smith | 693fc46 | 2008-09-06 20:13:06 +0000 | [diff] [blame] | 250 | decombuf = zlib.decompress(combuf) |
| 251 | # Test type of return value |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 252 | self.assertIsInstance(decombuf, bytes) |
Gregory P. Smith | 693fc46 | 2008-09-06 20:13:06 +0000 | [diff] [blame] | 253 | |
| 254 | self.assertEqual(data, decombuf) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 255 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 256 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 257 | bufs = [] |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 258 | for i in range(0, len(combuf), dcx): |
| 259 | bufs.append(dco.decompress(combuf[i:i+dcx])) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 260 | self.assertEqual(b'', dco.unconsumed_tail, ######## |
| 261 | "(A) uct should be b'': not %d long" % |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 262 | len(dco.unconsumed_tail)) |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 263 | self.assertEqual(b'', dco.unused_data) |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 264 | if flush: |
| 265 | bufs.append(dco.flush()) |
| 266 | else: |
| 267 | while True: |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 268 | chunk = dco.decompress(b'') |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 269 | if chunk: |
| 270 | bufs.append(chunk) |
| 271 | else: |
| 272 | break |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 273 | self.assertEqual(b'', dco.unconsumed_tail, ######## |
| 274 | "(B) uct should be b'': not %d long" % |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 275 | len(dco.unconsumed_tail)) |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 276 | self.assertEqual(b'', dco.unused_data) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 277 | self.assertEqual(data, b''.join(bufs)) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 278 | # Failure means: "decompressobj with init options failed" |
| 279 | |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 280 | def test_decompincflush(self): |
| 281 | self.test_decompinc(flush=True) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 282 | |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 283 | def test_decompimax(self, source=None, cx=256, dcx=64): |
| 284 | # compress in steps, decompress in length-restricted steps |
| 285 | source = source or HAMLET_SCENE |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 286 | # Check a decompression object with max_length specified |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 287 | data = source * 128 |
| 288 | co = zlib.compressobj() |
| 289 | bufs = [] |
| 290 | for i in range(0, len(data), cx): |
| 291 | bufs.append(co.compress(data[i:i+cx])) |
| 292 | bufs.append(co.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 293 | combuf = b''.join(bufs) |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 294 | self.assertEqual(data, zlib.decompress(combuf), |
| 295 | 'compressed data failure') |
| 296 | |
| 297 | dco = zlib.decompressobj() |
| 298 | bufs = [] |
| 299 | cb = combuf |
| 300 | while cb: |
| 301 | #max_length = 1 + len(cb)//10 |
| 302 | chunk = dco.decompress(cb, dcx) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 303 | self.assertFalse(len(chunk) > dcx, |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 304 | 'chunk too big (%d>%d)' % (len(chunk), dcx)) |
| 305 | bufs.append(chunk) |
| 306 | cb = dco.unconsumed_tail |
| 307 | bufs.append(dco.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 308 | self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved') |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 309 | |
| 310 | def test_decompressmaxlen(self, flush=False): |
| 311 | # Check a decompression object with max_length specified |
| 312 | data = HAMLET_SCENE * 128 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 313 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 314 | bufs = [] |
| 315 | for i in range(0, len(data), 256): |
| 316 | bufs.append(co.compress(data[i:i+256])) |
| 317 | bufs.append(co.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 318 | combuf = b''.join(bufs) |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 319 | self.assertEqual(data, zlib.decompress(combuf), |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 320 | 'compressed data failure') |
| 321 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 322 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 323 | bufs = [] |
| 324 | cb = combuf |
| 325 | while cb: |
Guido van Rossum | f359410 | 2003-02-27 18:39:18 +0000 | [diff] [blame] | 326 | max_length = 1 + len(cb)//10 |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 327 | chunk = dco.decompress(cb, max_length) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 328 | self.assertFalse(len(chunk) > max_length, |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 329 | 'chunk too big (%d>%d)' % (len(chunk),max_length)) |
| 330 | bufs.append(chunk) |
| 331 | cb = dco.unconsumed_tail |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 332 | if flush: |
| 333 | bufs.append(dco.flush()) |
| 334 | else: |
| 335 | while chunk: |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 336 | chunk = dco.decompress(b'', max_length) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 337 | self.assertFalse(len(chunk) > max_length, |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 338 | 'chunk too big (%d>%d)' % (len(chunk),max_length)) |
| 339 | bufs.append(chunk) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 340 | self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved') |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 341 | |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 342 | def test_decompressmaxlenflush(self): |
| 343 | self.test_decompressmaxlen(flush=True) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 344 | |
| 345 | def test_maxlenmisc(self): |
| 346 | # Misc tests of max_length |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 347 | dco = zlib.decompressobj() |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 348 | self.assertRaises(ValueError, dco.decompress, b"", -1) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 349 | self.assertEqual(b'', dco.unconsumed_tail) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 350 | |
Nadeem Vawda | 7619e88 | 2011-05-14 14:05:20 +0200 | [diff] [blame] | 351 | def test_clear_unconsumed_tail(self): |
| 352 | # Issue #12050: calling decompress() without providing max_length |
| 353 | # should clear the unconsumed_tail attribute. |
| 354 | cdata = b"x\x9cKLJ\x06\x00\x02M\x01" # "abc" |
| 355 | dco = zlib.decompressobj() |
| 356 | ddata = dco.decompress(cdata, 1) |
| 357 | ddata += dco.decompress(dco.unconsumed_tail) |
| 358 | self.assertEqual(dco.unconsumed_tail, b"") |
| 359 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 360 | def test_flushes(self): |
| 361 | # Test flush() with the various options, using all the |
| 362 | # different levels in order to provide more variations. |
| 363 | sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH'] |
| 364 | sync_opt = [getattr(zlib, opt) for opt in sync_opt |
| 365 | if hasattr(zlib, opt)] |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 366 | data = HAMLET_SCENE * 8 |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 367 | |
| 368 | for sync in sync_opt: |
| 369 | for level in range(10): |
| 370 | obj = zlib.compressobj( level ) |
| 371 | a = obj.compress( data[:3000] ) |
| 372 | b = obj.flush( sync ) |
| 373 | c = obj.compress( data[3000:] ) |
| 374 | d = obj.flush() |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 375 | self.assertEqual(zlib.decompress(b''.join([a,b,c,d])), |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 376 | data, ("Decompress failed: flush " |
| 377 | "mode=%i, level=%i") % (sync, level)) |
| 378 | del obj |
| 379 | |
| 380 | def test_odd_flush(self): |
| 381 | # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1 |
| 382 | import random |
| 383 | |
| 384 | if hasattr(zlib, 'Z_SYNC_FLUSH'): |
| 385 | # Testing on 17K of "random" data |
| 386 | |
| 387 | # Create compressor and decompressor objects |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 388 | co = zlib.compressobj(zlib.Z_BEST_COMPRESSION) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 389 | dco = zlib.decompressobj() |
| 390 | |
| 391 | # Try 17K of data |
| 392 | # generate random data stream |
| 393 | try: |
| 394 | # In 2.3 and later, WichmannHill is the RNG of the bug report |
| 395 | gen = random.WichmannHill() |
| 396 | except AttributeError: |
| 397 | try: |
| 398 | # 2.2 called it Random |
| 399 | gen = random.Random() |
| 400 | except AttributeError: |
| 401 | # others might simply have a single RNG |
| 402 | gen = random |
| 403 | gen.seed(1) |
| 404 | data = genblock(1, 17 * 1024, generator=gen) |
| 405 | |
| 406 | # compress, sync-flush, and decompress |
| 407 | first = co.compress(data) |
| 408 | second = co.flush(zlib.Z_SYNC_FLUSH) |
| 409 | expanded = dco.decompress(first + second) |
| 410 | |
| 411 | # if decompressed data is different from the input data, choke. |
| 412 | self.assertEqual(expanded, data, "17K random source doesn't match") |
| 413 | |
Andrew M. Kuchling | 3b585b3 | 2004-12-28 20:10:48 +0000 | [diff] [blame] | 414 | def test_empty_flush(self): |
| 415 | # Test that calling .flush() on unused objects works. |
| 416 | # (Bug #1083110 -- calling .flush() on decompress objects |
| 417 | # caused a core dump.) |
| 418 | |
| 419 | co = zlib.compressobj(zlib.Z_BEST_COMPRESSION) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 420 | self.assertTrue(co.flush()) # Returns a zlib header |
Andrew M. Kuchling | 3b585b3 | 2004-12-28 20:10:48 +0000 | [diff] [blame] | 421 | dco = zlib.decompressobj() |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 422 | self.assertEqual(dco.flush(), b"") # Returns nothing |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 423 | |
Antoine Pitrou | c09c92f | 2010-05-11 23:36:40 +0000 | [diff] [blame] | 424 | def test_decompress_incomplete_stream(self): |
| 425 | # This is 'foo', deflated |
| 426 | x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' |
| 427 | # For the record |
| 428 | self.assertEqual(zlib.decompress(x), b'foo') |
| 429 | self.assertRaises(zlib.error, zlib.decompress, x[:-5]) |
| 430 | # Omitting the stream end works with decompressor objects |
| 431 | # (see issue #8672). |
| 432 | dco = zlib.decompressobj() |
| 433 | y = dco.decompress(x[:-5]) |
| 434 | y += dco.flush() |
| 435 | self.assertEqual(y, b'foo') |
| 436 | |
Nadeem Vawda | 3907994 | 2012-11-05 00:37:42 +0100 | [diff] [blame^] | 437 | def test_decompress_unused_data(self): |
| 438 | # Repeated calls to decompress() after EOF should accumulate data in |
| 439 | # dco.unused_data, instead of just storing the arg to the last call. |
| 440 | x = zlib.compress(HAMLET_SCENE) + HAMLET_SCENE |
| 441 | for step in 1, 2, 100: |
| 442 | dco = zlib.decompressobj() |
| 443 | data = b''.join(dco.decompress(x[i : i + step]) |
| 444 | for i in range(0, len(x), step)) |
| 445 | data += dco.flush() |
| 446 | |
| 447 | self.assertEqual(data, HAMLET_SCENE) |
| 448 | self.assertEqual(dco.unused_data, HAMLET_SCENE) |
| 449 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 450 | if hasattr(zlib.compressobj(), "copy"): |
| 451 | def test_compresscopy(self): |
| 452 | # Test copying a compression object |
| 453 | data0 = HAMLET_SCENE |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 454 | data1 = bytes(str(HAMLET_SCENE, "ascii").swapcase(), "ascii") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 455 | c0 = zlib.compressobj(zlib.Z_BEST_COMPRESSION) |
| 456 | bufs0 = [] |
| 457 | bufs0.append(c0.compress(data0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 458 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 459 | c1 = c0.copy() |
| 460 | bufs1 = bufs0[:] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 461 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 462 | bufs0.append(c0.compress(data0)) |
| 463 | bufs0.append(c0.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 464 | s0 = b''.join(bufs0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 465 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 466 | bufs1.append(c1.compress(data1)) |
| 467 | bufs1.append(c1.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 468 | s1 = b''.join(bufs1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 469 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 470 | self.assertEqual(zlib.decompress(s0),data0+data0) |
| 471 | self.assertEqual(zlib.decompress(s1),data0+data1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 472 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 473 | def test_badcompresscopy(self): |
| 474 | # Test copying a compression object in an inconsistent state |
| 475 | c = zlib.compressobj() |
| 476 | c.compress(HAMLET_SCENE) |
| 477 | c.flush() |
| 478 | self.assertRaises(ValueError, c.copy) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 479 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 480 | if hasattr(zlib.decompressobj(), "copy"): |
| 481 | def test_decompresscopy(self): |
| 482 | # Test copying a decompression object |
| 483 | data = HAMLET_SCENE |
| 484 | comp = zlib.compress(data) |
Gregory P. Smith | 693fc46 | 2008-09-06 20:13:06 +0000 | [diff] [blame] | 485 | # Test type of return value |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 486 | self.assertIsInstance(comp, bytes) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 487 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 488 | d0 = zlib.decompressobj() |
| 489 | bufs0 = [] |
| 490 | bufs0.append(d0.decompress(comp[:32])) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 491 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 492 | d1 = d0.copy() |
| 493 | bufs1 = bufs0[:] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 494 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 495 | bufs0.append(d0.decompress(comp[32:])) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 496 | s0 = b''.join(bufs0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 497 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 498 | bufs1.append(d1.decompress(comp[32:])) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 499 | s1 = b''.join(bufs1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 500 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 501 | self.assertEqual(s0,s1) |
| 502 | self.assertEqual(s0,data) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 503 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 504 | def test_baddecompresscopy(self): |
| 505 | # Test copying a compression object in an inconsistent state |
| 506 | data = zlib.compress(HAMLET_SCENE) |
| 507 | d = zlib.decompressobj() |
| 508 | d.decompress(data) |
| 509 | d.flush() |
| 510 | self.assertRaises(ValueError, d.copy) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 511 | |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 512 | # Memory use of the following functions takes into account overallocation |
| 513 | |
Antoine Pitrou | 94190bb | 2011-10-04 10:22:36 +0200 | [diff] [blame] | 514 | @bigmemtest(size=_1G + 1024 * 1024, memuse=3) |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 515 | def test_big_compress_buffer(self, size): |
| 516 | c = zlib.compressobj(1) |
| 517 | compress = lambda s: c.compress(s) + c.flush() |
| 518 | self.check_big_compress_buffer(size, compress) |
| 519 | |
Antoine Pitrou | 94190bb | 2011-10-04 10:22:36 +0200 | [diff] [blame] | 520 | @bigmemtest(size=_1G + 1024 * 1024, memuse=2) |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 521 | def test_big_decompress_buffer(self, size): |
| 522 | d = zlib.decompressobj() |
| 523 | decompress = lambda s: d.decompress(s) + d.flush() |
| 524 | self.check_big_decompress_buffer(size, decompress) |
| 525 | |
Antoine Pitrou | 94190bb | 2011-10-04 10:22:36 +0200 | [diff] [blame] | 526 | @bigmemtest(size=_4G + 100, memuse=1) |
Nadeem Vawda | 0c3d96a | 2011-05-15 00:19:50 +0200 | [diff] [blame] | 527 | def test_length_overflow(self, size): |
| 528 | if size < _4G + 100: |
| 529 | self.skipTest("not enough free memory, need at least 4 GB") |
| 530 | data = b'x' * size |
Nadeem Vawda | 1161a9c | 2011-05-15 00:48:24 +0200 | [diff] [blame] | 531 | c = zlib.compressobj(1) |
| 532 | d = zlib.decompressobj() |
Nadeem Vawda | 0c3d96a | 2011-05-15 00:19:50 +0200 | [diff] [blame] | 533 | try: |
Nadeem Vawda | 1161a9c | 2011-05-15 00:48:24 +0200 | [diff] [blame] | 534 | self.assertRaises(OverflowError, c.compress, data) |
| 535 | self.assertRaises(OverflowError, d.decompress, data) |
Nadeem Vawda | 0c3d96a | 2011-05-15 00:19:50 +0200 | [diff] [blame] | 536 | finally: |
| 537 | data = None |
| 538 | |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 539 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 540 | def genblock(seed, length, step=1024, generator=random): |
| 541 | """length-byte stream of random data from a seed (in step-byte blocks).""" |
| 542 | if seed is not None: |
| 543 | generator.seed(seed) |
| 544 | randint = generator.randint |
| 545 | if length < step or step < 2: |
| 546 | step = length |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 547 | blocks = bytes() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 548 | for i in range(0, length, step): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 549 | blocks += bytes(randint(0, 255) for x in range(step)) |
| 550 | return blocks |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 551 | |
| 552 | |
| 553 | |
| 554 | def choose_lines(source, number, seed=None, generator=random): |
| 555 | """Return a list of number lines randomly chosen from the source""" |
| 556 | if seed is not None: |
| 557 | generator.seed(seed) |
| 558 | sources = source.split('\n') |
| 559 | return [generator.choice(sources) for n in range(number)] |
| 560 | |
| 561 | |
| 562 | |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 563 | HAMLET_SCENE = b""" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 564 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 565 | |
| 566 | O, fear me not. |
| 567 | I stay too long: but here my father comes. |
| 568 | |
| 569 | Enter POLONIUS |
| 570 | |
| 571 | A double blessing is a double grace, |
| 572 | Occasion smiles upon a second leave. |
| 573 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 574 | LORD POLONIUS |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 575 | |
| 576 | Yet here, Laertes! aboard, aboard, for shame! |
| 577 | The wind sits in the shoulder of your sail, |
| 578 | And you are stay'd for. There; my blessing with thee! |
| 579 | And these few precepts in thy memory |
| 580 | See thou character. Give thy thoughts no tongue, |
| 581 | Nor any unproportioned thought his act. |
| 582 | Be thou familiar, but by no means vulgar. |
| 583 | Those friends thou hast, and their adoption tried, |
| 584 | Grapple them to thy soul with hoops of steel; |
| 585 | But do not dull thy palm with entertainment |
| 586 | Of each new-hatch'd, unfledged comrade. Beware |
| 587 | Of entrance to a quarrel, but being in, |
| 588 | Bear't that the opposed may beware of thee. |
| 589 | Give every man thy ear, but few thy voice; |
| 590 | Take each man's censure, but reserve thy judgment. |
| 591 | Costly thy habit as thy purse can buy, |
| 592 | But not express'd in fancy; rich, not gaudy; |
| 593 | For the apparel oft proclaims the man, |
| 594 | And they in France of the best rank and station |
| 595 | Are of a most select and generous chief in that. |
| 596 | Neither a borrower nor a lender be; |
| 597 | For loan oft loses both itself and friend, |
| 598 | And borrowing dulls the edge of husbandry. |
| 599 | This above all: to thine ownself be true, |
| 600 | And it must follow, as the night the day, |
| 601 | Thou canst not then be false to any man. |
| 602 | Farewell: my blessing season this in thee! |
| 603 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 604 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 605 | |
| 606 | Most humbly do I take my leave, my lord. |
| 607 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 608 | LORD POLONIUS |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 609 | |
| 610 | The time invites you; go; your servants tend. |
| 611 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 612 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 613 | |
| 614 | Farewell, Ophelia; and remember well |
| 615 | What I have said to you. |
| 616 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 617 | OPHELIA |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 618 | |
| 619 | 'Tis in my memory lock'd, |
| 620 | And you yourself shall keep the key of it. |
| 621 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 622 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 623 | |
| 624 | Farewell. |
| 625 | """ |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 626 | |
| 627 | |
| 628 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 629 | support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 630 | ChecksumTestCase, |
Antoine Pitrou | f3d2275 | 2011-02-21 18:09:00 +0000 | [diff] [blame] | 631 | ChecksumBigBufferTestCase, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 632 | ExceptionTestCase, |
| 633 | CompressTestCase, |
| 634 | CompressObjectTestCase |
| 635 | ) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 636 | |
| 637 | if __name__ == "__main__": |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 638 | unittest.main() # XXX |
| 639 | ###test_main() |