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 |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 3 | import zlib |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 4 | import binascii |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 5 | import random |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 6 | |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 8 | class ChecksumTestCase(unittest.TestCase): |
| 9 | # checksum test cases |
| 10 | def test_crc32start(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 11 | self.assertEqual(zlib.crc32(b""), zlib.crc32(b"", 0)) |
| 12 | self.assert_(zlib.crc32(b"abc", 0xffffffff)) |
Andrew M. Kuchling | fcfc8d5 | 2001-08-10 15:50:11 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 14 | def test_crc32empty(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 15 | self.assertEqual(zlib.crc32(b"", 0), 0) |
| 16 | self.assertEqual(zlib.crc32(b"", 1), 1) |
| 17 | self.assertEqual(zlib.crc32(b"", 432), 432) |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 19 | def test_adler32start(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 20 | self.assertEqual(zlib.adler32(b""), zlib.adler32(b"", 1)) |
| 21 | self.assert_(zlib.adler32(b"abc", 0xffffffff)) |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 23 | def test_adler32empty(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 24 | self.assertEqual(zlib.adler32(b"", 0), 0) |
| 25 | self.assertEqual(zlib.adler32(b"", 1), 1) |
| 26 | self.assertEqual(zlib.adler32(b"", 432), 432) |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 28 | def assertEqual32(self, seen, expected): |
| 29 | # 32-bit values masked -- checksums on 32- vs 64- bit machines |
| 30 | # This is important if bit 31 (0x08000000L) is set. |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 31 | self.assertEqual(seen & 0x0FFFFFFFF, expected & 0x0FFFFFFFF) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 32 | |
| 33 | def test_penguins(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 34 | self.assertEqual32(zlib.crc32(b"penguin", 0), 0x0e5c1a120) |
| 35 | self.assertEqual32(zlib.crc32(b"penguin", 1), 0x43b6aa94) |
| 36 | self.assertEqual32(zlib.adler32(b"penguin", 0), 0x0bcf02f6) |
| 37 | self.assertEqual32(zlib.adler32(b"penguin", 1), 0x0bd602f7) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 39 | self.assertEqual(zlib.crc32(b"penguin"), zlib.crc32(b"penguin", 0)) |
| 40 | self.assertEqual(zlib.adler32(b"penguin"),zlib.adler32(b"penguin",1)) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 41 | |
Gregory P. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 42 | def test_crc32_adler32_unsigned(self): |
| 43 | foo = 'abcdefghijklmnop' |
| 44 | # explicitly test signed behavior |
Gregory P. Smith | 2727503 | 2008-03-20 06:20:09 +0000 | [diff] [blame] | 45 | self.assertEqual(zlib.crc32(foo), 2486878355) |
Gregory P. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 46 | self.assertEqual(zlib.crc32('spam'), 1138425661) |
| 47 | self.assertEqual(zlib.adler32(foo+foo), 3573550353) |
| 48 | self.assertEqual(zlib.adler32('spam'), 72286642) |
| 49 | |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 50 | def test_same_as_binascii_crc32(self): |
| 51 | foo = 'abcdefghijklmnop' |
Gregory P. Smith | 2727503 | 2008-03-20 06:20:09 +0000 | [diff] [blame] | 52 | crc = 2486878355 |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 53 | self.assertEqual(binascii.crc32(foo), crc) |
| 54 | self.assertEqual(zlib.crc32(foo), crc) |
| 55 | self.assertEqual(binascii.crc32('spam'), zlib.crc32('spam')) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 56 | |
| 57 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 59 | class ExceptionTestCase(unittest.TestCase): |
| 60 | # make sure we generate some expected errors |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 61 | def test_badlevel(self): |
| 62 | # specifying compression level out of range causes an error |
| 63 | # (but -1 is Z_DEFAULT_COMPRESSION and apparently the zlib |
| 64 | # accepts 0 too) |
| 65 | self.assertRaises(zlib.error, zlib.compress, 'ERROR', 10) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 66 | |
| 67 | def test_badcompressobj(self): |
| 68 | # verify failure on building compress object with bad params |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 69 | self.assertRaises(ValueError, zlib.compressobj, 1, zlib.DEFLATED, 0) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 70 | # specifying total bits too large causes an error |
| 71 | self.assertRaises(ValueError, |
| 72 | zlib.compressobj, 1, zlib.DEFLATED, zlib.MAX_WBITS + 1) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 73 | |
| 74 | def test_baddecompressobj(self): |
| 75 | # verify failure on building decompress object with bad params |
| 76 | self.assertRaises(ValueError, zlib.decompressobj, 0) |
| 77 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 78 | def test_decompressobj_badflush(self): |
| 79 | # verify failure on calling decompressobj.flush with bad params |
| 80 | self.assertRaises(ValueError, zlib.decompressobj().flush, 0) |
| 81 | self.assertRaises(ValueError, zlib.decompressobj().flush, -1) |
| 82 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | class CompressTestCase(unittest.TestCase): |
| 86 | # Test compression in one go (whole message compression) |
| 87 | def test_speech(self): |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 88 | x = zlib.compress(HAMLET_SCENE) |
| 89 | self.assertEqual(zlib.decompress(x), HAMLET_SCENE) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 90 | |
| 91 | def test_speech128(self): |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 92 | # compress more data |
| 93 | data = HAMLET_SCENE * 128 |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 94 | x = zlib.compress(data) |
| 95 | self.assertEqual(zlib.decompress(x), data) |
| 96 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 97 | |
| 98 | |
| 99 | |
| 100 | class CompressObjectTestCase(unittest.TestCase): |
| 101 | # Test compression object |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 102 | def test_pair(self): |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 103 | # straightforward compress/decompress objects |
| 104 | data = HAMLET_SCENE * 128 |
| 105 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 106 | x1 = co.compress(data) |
| 107 | x2 = co.flush() |
| 108 | self.assertRaises(zlib.error, co.flush) # second flush should not work |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 109 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 110 | y1 = dco.decompress(x1 + x2) |
| 111 | y2 = dco.flush() |
| 112 | self.assertEqual(data, y1 + y2) |
| 113 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 114 | def test_compressoptions(self): |
| 115 | # specify lots of options to compressobj() |
| 116 | level = 2 |
| 117 | method = zlib.DEFLATED |
| 118 | wbits = -12 |
| 119 | memlevel = 9 |
| 120 | strategy = zlib.Z_FILTERED |
| 121 | co = zlib.compressobj(level, method, wbits, memlevel, strategy) |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 122 | x1 = co.compress(HAMLET_SCENE) |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 123 | x2 = co.flush() |
| 124 | dco = zlib.decompressobj(wbits) |
| 125 | y1 = dco.decompress(x1 + x2) |
| 126 | y2 = dco.flush() |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 127 | self.assertEqual(HAMLET_SCENE, y1 + y2) |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 129 | def test_compressincremental(self): |
| 130 | # compress object in steps, decompress object as one-shot |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 131 | data = HAMLET_SCENE * 128 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 132 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 133 | bufs = [] |
| 134 | for i in range(0, len(data), 256): |
| 135 | bufs.append(co.compress(data[i:i+256])) |
| 136 | bufs.append(co.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 137 | combuf = b''.join(bufs) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 138 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 139 | dco = zlib.decompressobj() |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 140 | y1 = dco.decompress(b''.join(bufs)) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 141 | y2 = dco.flush() |
| 142 | self.assertEqual(data, y1 + y2) |
| 143 | |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 144 | 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] | 145 | # compress object in steps, decompress object in steps |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 146 | source = source or HAMLET_SCENE |
| 147 | data = source * 128 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 148 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 149 | bufs = [] |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 150 | for i in range(0, len(data), cx): |
| 151 | bufs.append(co.compress(data[i:i+cx])) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 152 | bufs.append(co.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 153 | combuf = b''.join(bufs) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 154 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 155 | self.assertEqual(data, zlib.decompress(combuf)) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 156 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 157 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 158 | bufs = [] |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 159 | for i in range(0, len(combuf), dcx): |
| 160 | bufs.append(dco.decompress(combuf[i:i+dcx])) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 161 | self.assertEqual(b'', dco.unconsumed_tail, ######## |
| 162 | "(A) uct should be b'': not %d long" % |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 163 | len(dco.unconsumed_tail)) |
| 164 | if flush: |
| 165 | bufs.append(dco.flush()) |
| 166 | else: |
| 167 | while True: |
| 168 | chunk = dco.decompress('') |
| 169 | if chunk: |
| 170 | bufs.append(chunk) |
| 171 | else: |
| 172 | break |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 173 | self.assertEqual(b'', dco.unconsumed_tail, ######## |
| 174 | "(B) uct should be b'': not %d long" % |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 175 | len(dco.unconsumed_tail)) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 176 | self.assertEqual(data, b''.join(bufs)) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 177 | # Failure means: "decompressobj with init options failed" |
| 178 | |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 179 | def test_decompincflush(self): |
| 180 | self.test_decompinc(flush=True) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 181 | |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 182 | def test_decompimax(self, source=None, cx=256, dcx=64): |
| 183 | # compress in steps, decompress in length-restricted steps |
| 184 | source = source or HAMLET_SCENE |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 185 | # Check a decompression object with max_length specified |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 186 | data = source * 128 |
| 187 | co = zlib.compressobj() |
| 188 | bufs = [] |
| 189 | for i in range(0, len(data), cx): |
| 190 | bufs.append(co.compress(data[i:i+cx])) |
| 191 | bufs.append(co.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 192 | combuf = b''.join(bufs) |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 193 | self.assertEqual(data, zlib.decompress(combuf), |
| 194 | 'compressed data failure') |
| 195 | |
| 196 | dco = zlib.decompressobj() |
| 197 | bufs = [] |
| 198 | cb = combuf |
| 199 | while cb: |
| 200 | #max_length = 1 + len(cb)//10 |
| 201 | chunk = dco.decompress(cb, dcx) |
| 202 | self.failIf(len(chunk) > dcx, |
| 203 | 'chunk too big (%d>%d)' % (len(chunk), dcx)) |
| 204 | bufs.append(chunk) |
| 205 | cb = dco.unconsumed_tail |
| 206 | bufs.append(dco.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 207 | self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved') |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 208 | |
| 209 | def test_decompressmaxlen(self, flush=False): |
| 210 | # Check a decompression object with max_length specified |
| 211 | data = HAMLET_SCENE * 128 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 212 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 213 | bufs = [] |
| 214 | for i in range(0, len(data), 256): |
| 215 | bufs.append(co.compress(data[i:i+256])) |
| 216 | bufs.append(co.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 217 | combuf = b''.join(bufs) |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 218 | self.assertEqual(data, zlib.decompress(combuf), |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 219 | 'compressed data failure') |
| 220 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 221 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 222 | bufs = [] |
| 223 | cb = combuf |
| 224 | while cb: |
Guido van Rossum | f359410 | 2003-02-27 18:39:18 +0000 | [diff] [blame] | 225 | max_length = 1 + len(cb)//10 |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 226 | chunk = dco.decompress(cb, max_length) |
| 227 | self.failIf(len(chunk) > max_length, |
| 228 | 'chunk too big (%d>%d)' % (len(chunk),max_length)) |
| 229 | bufs.append(chunk) |
| 230 | cb = dco.unconsumed_tail |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 231 | if flush: |
| 232 | bufs.append(dco.flush()) |
| 233 | else: |
| 234 | while chunk: |
| 235 | chunk = dco.decompress('', max_length) |
| 236 | self.failIf(len(chunk) > max_length, |
| 237 | 'chunk too big (%d>%d)' % (len(chunk),max_length)) |
| 238 | bufs.append(chunk) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 239 | self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved') |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 240 | |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 241 | def test_decompressmaxlenflush(self): |
| 242 | self.test_decompressmaxlen(flush=True) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 243 | |
| 244 | def test_maxlenmisc(self): |
| 245 | # Misc tests of max_length |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 246 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 247 | self.assertRaises(ValueError, dco.decompress, "", -1) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 248 | self.assertEqual(b'', dco.unconsumed_tail) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 249 | |
| 250 | def test_flushes(self): |
| 251 | # Test flush() with the various options, using all the |
| 252 | # different levels in order to provide more variations. |
| 253 | sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH'] |
| 254 | sync_opt = [getattr(zlib, opt) for opt in sync_opt |
| 255 | if hasattr(zlib, opt)] |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 256 | data = HAMLET_SCENE * 8 |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 257 | |
| 258 | for sync in sync_opt: |
| 259 | for level in range(10): |
| 260 | obj = zlib.compressobj( level ) |
| 261 | a = obj.compress( data[:3000] ) |
| 262 | b = obj.flush( sync ) |
| 263 | c = obj.compress( data[3000:] ) |
| 264 | d = obj.flush() |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 265 | self.assertEqual(zlib.decompress(b''.join([a,b,c,d])), |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 266 | data, ("Decompress failed: flush " |
| 267 | "mode=%i, level=%i") % (sync, level)) |
| 268 | del obj |
| 269 | |
| 270 | def test_odd_flush(self): |
| 271 | # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1 |
| 272 | import random |
| 273 | |
| 274 | if hasattr(zlib, 'Z_SYNC_FLUSH'): |
| 275 | # Testing on 17K of "random" data |
| 276 | |
| 277 | # Create compressor and decompressor objects |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 278 | co = zlib.compressobj(zlib.Z_BEST_COMPRESSION) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 279 | dco = zlib.decompressobj() |
| 280 | |
| 281 | # Try 17K of data |
| 282 | # generate random data stream |
| 283 | try: |
| 284 | # In 2.3 and later, WichmannHill is the RNG of the bug report |
| 285 | gen = random.WichmannHill() |
| 286 | except AttributeError: |
| 287 | try: |
| 288 | # 2.2 called it Random |
| 289 | gen = random.Random() |
| 290 | except AttributeError: |
| 291 | # others might simply have a single RNG |
| 292 | gen = random |
| 293 | gen.seed(1) |
| 294 | data = genblock(1, 17 * 1024, generator=gen) |
| 295 | |
| 296 | # compress, sync-flush, and decompress |
| 297 | first = co.compress(data) |
| 298 | second = co.flush(zlib.Z_SYNC_FLUSH) |
| 299 | expanded = dco.decompress(first + second) |
| 300 | |
| 301 | # if decompressed data is different from the input data, choke. |
| 302 | self.assertEqual(expanded, data, "17K random source doesn't match") |
| 303 | |
Andrew M. Kuchling | 3b585b3 | 2004-12-28 20:10:48 +0000 | [diff] [blame] | 304 | def test_empty_flush(self): |
| 305 | # Test that calling .flush() on unused objects works. |
| 306 | # (Bug #1083110 -- calling .flush() on decompress objects |
| 307 | # caused a core dump.) |
| 308 | |
| 309 | co = zlib.compressobj(zlib.Z_BEST_COMPRESSION) |
| 310 | self.failUnless(co.flush()) # Returns a zlib header |
| 311 | dco = zlib.decompressobj() |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 312 | self.assertEqual(dco.flush(), b"") # Returns nothing |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 313 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 314 | if hasattr(zlib.compressobj(), "copy"): |
| 315 | def test_compresscopy(self): |
| 316 | # Test copying a compression object |
| 317 | data0 = HAMLET_SCENE |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 318 | data1 = bytes(str(HAMLET_SCENE, "ascii").swapcase(), "ascii") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 319 | c0 = zlib.compressobj(zlib.Z_BEST_COMPRESSION) |
| 320 | bufs0 = [] |
| 321 | bufs0.append(c0.compress(data0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 322 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 323 | c1 = c0.copy() |
| 324 | bufs1 = bufs0[:] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 325 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 326 | bufs0.append(c0.compress(data0)) |
| 327 | bufs0.append(c0.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 328 | s0 = b''.join(bufs0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 329 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 330 | bufs1.append(c1.compress(data1)) |
| 331 | bufs1.append(c1.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 332 | s1 = b''.join(bufs1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 333 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 334 | self.assertEqual(zlib.decompress(s0),data0+data0) |
| 335 | self.assertEqual(zlib.decompress(s1),data0+data1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 336 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 337 | def test_badcompresscopy(self): |
| 338 | # Test copying a compression object in an inconsistent state |
| 339 | c = zlib.compressobj() |
| 340 | c.compress(HAMLET_SCENE) |
| 341 | c.flush() |
| 342 | self.assertRaises(ValueError, c.copy) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 343 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 344 | if hasattr(zlib.decompressobj(), "copy"): |
| 345 | def test_decompresscopy(self): |
| 346 | # Test copying a decompression object |
| 347 | data = HAMLET_SCENE |
| 348 | comp = zlib.compress(data) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 349 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 350 | d0 = zlib.decompressobj() |
| 351 | bufs0 = [] |
| 352 | bufs0.append(d0.decompress(comp[:32])) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 353 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 354 | d1 = d0.copy() |
| 355 | bufs1 = bufs0[:] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 356 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 357 | bufs0.append(d0.decompress(comp[32:])) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 358 | s0 = b''.join(bufs0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 359 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 360 | bufs1.append(d1.decompress(comp[32:])) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 361 | s1 = b''.join(bufs1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 362 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 363 | self.assertEqual(s0,s1) |
| 364 | self.assertEqual(s0,data) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 365 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 366 | def test_baddecompresscopy(self): |
| 367 | # Test copying a compression object in an inconsistent state |
| 368 | data = zlib.compress(HAMLET_SCENE) |
| 369 | d = zlib.decompressobj() |
| 370 | d.decompress(data) |
| 371 | d.flush() |
| 372 | self.assertRaises(ValueError, d.copy) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 373 | |
| 374 | def genblock(seed, length, step=1024, generator=random): |
| 375 | """length-byte stream of random data from a seed (in step-byte blocks).""" |
| 376 | if seed is not None: |
| 377 | generator.seed(seed) |
| 378 | randint = generator.randint |
| 379 | if length < step or step < 2: |
| 380 | step = length |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 381 | blocks = bytes() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 382 | for i in range(0, length, step): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 383 | blocks += bytes(randint(0, 255) for x in range(step)) |
| 384 | return blocks |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 385 | |
| 386 | |
| 387 | |
| 388 | def choose_lines(source, number, seed=None, generator=random): |
| 389 | """Return a list of number lines randomly chosen from the source""" |
| 390 | if seed is not None: |
| 391 | generator.seed(seed) |
| 392 | sources = source.split('\n') |
| 393 | return [generator.choice(sources) for n in range(number)] |
| 394 | |
| 395 | |
| 396 | |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 397 | HAMLET_SCENE = b""" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 398 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 399 | |
| 400 | O, fear me not. |
| 401 | I stay too long: but here my father comes. |
| 402 | |
| 403 | Enter POLONIUS |
| 404 | |
| 405 | A double blessing is a double grace, |
| 406 | Occasion smiles upon a second leave. |
| 407 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 408 | LORD POLONIUS |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 409 | |
| 410 | Yet here, Laertes! aboard, aboard, for shame! |
| 411 | The wind sits in the shoulder of your sail, |
| 412 | And you are stay'd for. There; my blessing with thee! |
| 413 | And these few precepts in thy memory |
| 414 | See thou character. Give thy thoughts no tongue, |
| 415 | Nor any unproportioned thought his act. |
| 416 | Be thou familiar, but by no means vulgar. |
| 417 | Those friends thou hast, and their adoption tried, |
| 418 | Grapple them to thy soul with hoops of steel; |
| 419 | But do not dull thy palm with entertainment |
| 420 | Of each new-hatch'd, unfledged comrade. Beware |
| 421 | Of entrance to a quarrel, but being in, |
| 422 | Bear't that the opposed may beware of thee. |
| 423 | Give every man thy ear, but few thy voice; |
| 424 | Take each man's censure, but reserve thy judgment. |
| 425 | Costly thy habit as thy purse can buy, |
| 426 | But not express'd in fancy; rich, not gaudy; |
| 427 | For the apparel oft proclaims the man, |
| 428 | And they in France of the best rank and station |
| 429 | Are of a most select and generous chief in that. |
| 430 | Neither a borrower nor a lender be; |
| 431 | For loan oft loses both itself and friend, |
| 432 | And borrowing dulls the edge of husbandry. |
| 433 | This above all: to thine ownself be true, |
| 434 | And it must follow, as the night the day, |
| 435 | Thou canst not then be false to any man. |
| 436 | Farewell: my blessing season this in thee! |
| 437 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 438 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 439 | |
| 440 | Most humbly do I take my leave, my lord. |
| 441 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 442 | LORD POLONIUS |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 443 | |
| 444 | The time invites you; go; your servants tend. |
| 445 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 446 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 447 | |
| 448 | Farewell, Ophelia; and remember well |
| 449 | What I have said to you. |
| 450 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 451 | OPHELIA |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 452 | |
| 453 | 'Tis in my memory lock'd, |
| 454 | And you yourself shall keep the key of it. |
| 455 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 456 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 457 | |
| 458 | Farewell. |
| 459 | """ |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 460 | |
| 461 | |
| 462 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 463 | support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 464 | ChecksumTestCase, |
| 465 | ExceptionTestCase, |
| 466 | CompressTestCase, |
| 467 | CompressObjectTestCase |
| 468 | ) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 469 | |
| 470 | if __name__ == "__main__": |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 471 | unittest.main() # XXX |
| 472 | ###test_main() |