| 1 |
""" |
| 2 |
Tests for the zlib module. |
| 3 |
""" |
| 4 |
|
| 5 |
import pypy |
| 6 |
import py |
| 7 |
|
| 8 |
try: |
| 9 |
import zlib |
| 10 |
except ImportError: |
| 11 |
py.test.skip("no zlib module on this host Python") |
| 12 |
|
| 13 |
from pypy.interpreter.gateway import interp2app |
| 14 |
try: |
| 15 |
from pypy.module.zlib import interp_zlib |
| 16 |
from rpython.rlib import rzlib |
| 17 |
except ImportError: |
| 18 |
py.test.skip("no zlib C library on this machine") |
| 19 |
|
| 20 |
|
| 21 |
class AppTestZlib(object): |
| 22 |
spaceconfig = dict(usemodules=['zlib']) |
| 23 |
|
| 24 |
def setup_class(cls): |
| 25 |
""" |
| 26 |
Create a space with the zlib module and import it for use by the tests. |
| 27 |
Also create some compressed data with the bootstrap zlib module so that |
| 28 |
compression and decompression tests have a little real data to assert |
| 29 |
against. |
| 30 |
""" |
| 31 |
cls.w_runappdirect = cls.space.wrap(cls.runappdirect) |
| 32 |
|
| 33 |
cls.w_zlib = cls.space.getbuiltinmodule('zlib') |
| 34 |
expanded = b'some bytes which will be compressed' |
| 35 |
cls.w_expanded = cls.space.newbytes(expanded) |
| 36 |
cls.w_compressed = cls.space.newbytes(zlib.compress(expanded)) |
| 37 |
cls.w_LICENSE = cls.space.newbytes( |
| 38 |
py.path.local(pypy.__file__).dirpath().dirpath() |
| 39 |
.join('LICENSE').read()) |
| 40 |
|
| 41 |
def intentionally_break_a_z_stream(space, w_zobj): |
| 42 |
""" |
| 43 |
Intentionally break the z_stream associated with a |
| 44 |
compressobj or decompressobj in a way that causes their copy |
| 45 |
methods to raise RZlibErrors. |
| 46 |
""" |
| 47 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 48 |
w_zobj.stream.c_zalloc = rffi.cast( |
| 49 |
lltype.typeOf(w_zobj.stream.c_zalloc), |
| 50 |
rzlib.Z_NULL, |
| 51 |
) |
| 52 |
|
| 53 |
cls.w_intentionally_break_a_z_stream = cls.space.wrap( |
| 54 |
interp2app(intentionally_break_a_z_stream), |
| 55 |
) |
| 56 |
|
| 57 |
def test_def_buf_size(self): |
| 58 |
assert self.zlib.DEF_BUF_SIZE >= 0 |
| 59 |
|
| 60 |
def test_error(self): |
| 61 |
""" |
| 62 |
zlib.error should be an exception class. |
| 63 |
""" |
| 64 |
assert issubclass(self.zlib.error, Exception) |
| 65 |
|
| 66 |
def test_crc32(self): |
| 67 |
""" |
| 68 |
When called with a string, zlib.crc32 should compute its CRC32 and |
| 69 |
return it as an unsigned 32 bit integer. |
| 70 |
""" |
| 71 |
assert self.zlib.crc32(b'') == 0 |
| 72 |
assert self.zlib.crc32(b'\0') == 3523407757 |
| 73 |
assert self.zlib.crc32(b'hello, world.') == 3358036098 |
| 74 |
|
| 75 |
def test_crc32_start_value(self): |
| 76 |
""" |
| 77 |
When called with a string and an integer, zlib.crc32 should compute the |
| 78 |
CRC32 of the string using the integer as the starting value. |
| 79 |
""" |
| 80 |
assert self.zlib.crc32(b'', 42) == 42 |
| 81 |
assert self.zlib.crc32(b'\0', 42) == 163128923 |
| 82 |
assert self.zlib.crc32(b'hello, world.', 42) == 1090960721 |
| 83 |
hello = b'hello, ' |
| 84 |
hellocrc = self.zlib.crc32(hello) |
| 85 |
world = b'world.' |
| 86 |
helloworldcrc = self.zlib.crc32(world, hellocrc) |
| 87 |
assert helloworldcrc == self.zlib.crc32(hello + world) |
| 88 |
|
| 89 |
def test_crc32_negative_start(self): |
| 90 |
v = self.zlib.crc32(b'', -1) |
| 91 |
assert v == 4294967295 |
| 92 |
|
| 93 |
def test_crc32_negative_start_bug(self): |
| 94 |
v1 = self.zlib.crc32(b'ham', 0xffffffff) |
| 95 |
v2 = self.zlib.crc32(b'ham', -1) |
| 96 |
assert v1 == v2 |
| 97 |
|
| 98 |
def test_crc32_negative_long_start(self): |
| 99 |
v = self.zlib.crc32(b'', -1) |
| 100 |
assert v == 4294967295 |
| 101 |
assert self.zlib.crc32(b'foo', -99999999999999999999999) == 1611238463 |
| 102 |
|
| 103 |
def test_crc32_long_start(self): |
| 104 |
import sys |
| 105 |
v = self.zlib.crc32(b'', sys.maxsize*2) |
| 106 |
assert v == 4294967294 |
| 107 |
assert self.zlib.crc32(b'foo', 99999999999999999999999) == 1635107045 |
| 108 |
|
| 109 |
def test_adler32(self): |
| 110 |
""" |
| 111 |
When called with a string, zlib.adler32() should compute its adler 32 |
| 112 |
checksum and return it as an unsigned 32 bit integer. |
| 113 |
""" |
| 114 |
assert self.zlib.adler32(b'') == 1 |
| 115 |
assert self.zlib.adler32(b'\0') == 65537 |
| 116 |
assert self.zlib.adler32(b'hello, world.') == 571147447 |
| 117 |
assert self.zlib.adler32(b'x' * 23) == 2172062409 |
| 118 |
|
| 119 |
def test_adler32_start_value(self): |
| 120 |
""" |
| 121 |
When called with a string and an integer, zlib.adler32 should compute |
| 122 |
the adler 32 checksum of the string using the integer as the starting |
| 123 |
value. |
| 124 |
""" |
| 125 |
assert self.zlib.adler32(b'', 42) == 42 |
| 126 |
assert self.zlib.adler32(b'\0', 42) == 2752554 |
| 127 |
assert self.zlib.adler32(b'hello, world.', 42) == 606078176 |
| 128 |
assert self.zlib.adler32(b'x' * 23, 42) == 2233862898 |
| 129 |
hello = b'hello, ' |
| 130 |
hellosum = self.zlib.adler32(hello) |
| 131 |
world = b'world.' |
| 132 |
helloworldsum = self.zlib.adler32(world, hellosum) |
| 133 |
assert helloworldsum == self.zlib.adler32(hello + world) |
| 134 |
|
| 135 |
assert self.zlib.adler32(b'foo', -1) == 45547858 |
| 136 |
assert self.zlib.adler32(b'foo', 99999999999999999999999) == 4180148562 |
| 137 |
|
| 138 |
def test_invalidLevel(self): |
| 139 |
""" |
| 140 |
zlib.compressobj should raise ValueError when an out of bounds level is |
| 141 |
passed to it. |
| 142 |
""" |
| 143 |
raises(ValueError, self.zlib.compressobj, -2) |
| 144 |
raises(ValueError, self.zlib.compressobj, 10) |
| 145 |
|
| 146 |
def test_compression(self): |
| 147 |
""" |
| 148 |
zlib.compressobj should return an object which can be used to compress |
| 149 |
bytes. |
| 150 |
""" |
| 151 |
compressor = self.zlib.compressobj() |
| 152 |
bytes = compressor.compress(self.expanded) |
| 153 |
raises(OverflowError, compressor.flush, 2**31) |
| 154 |
bytes += compressor.flush() |
| 155 |
assert bytes == self.compressed |
| 156 |
|
| 157 |
def test_decompression(self): |
| 158 |
""" |
| 159 |
zlib.decompressobj should return an object which can be used to |
| 160 |
decompress bytes. |
| 161 |
""" |
| 162 |
import sys |
| 163 |
decompressor = self.zlib.decompressobj() |
| 164 |
bytes = decompressor.decompress(self.compressed) |
| 165 |
bytes += decompressor.flush() |
| 166 |
assert bytes == self.expanded |
| 167 |
|
| 168 |
def test_compress(self): |
| 169 |
""" |
| 170 |
Test the zlib.compress() function. |
| 171 |
""" |
| 172 |
bytes = self.zlib.compress(self.expanded) |
| 173 |
assert bytes == self.compressed |
| 174 |
|
| 175 |
def test_decompress(self): |
| 176 |
""" |
| 177 |
Test the zlib.decompress() function. |
| 178 |
""" |
| 179 |
bytes = self.zlib.decompress(self.compressed) |
| 180 |
assert bytes == self.expanded |
| 181 |
|
| 182 |
def test_decompress_invalid_input(self): |
| 183 |
""" |
| 184 |
Try to feed garbage to zlib.decompress(). |
| 185 |
""" |
| 186 |
raises(self.zlib.error, self.zlib.decompress, self.compressed[:-2]) |
| 187 |
raises(self.zlib.error, self.zlib.decompress, b'foobar') |
| 188 |
|
| 189 |
def test_bad_arguments(self): |
| 190 |
import zlib, sys |
| 191 |
BIG = sys.maxsize + 1 |
| 192 |
raises(ValueError, zlib.decompressobj().flush, 0) |
| 193 |
raises(ValueError, zlib.decompressobj().flush, -1) |
| 194 |
raises(TypeError, zlib.decompressobj().flush, None) |
| 195 |
raises(ValueError, zlib.decompressobj().decompress, b'abc', -1) |
| 196 |
raises(TypeError, zlib.decompressobj().decompress, b'abc', None) |
| 197 |
raises(TypeError, self.zlib.decompress, self.compressed, None) |
| 198 |
raises(OverflowError, self.zlib.decompress, self.compressed, BIG) |
| 199 |
|
| 200 |
def test_empty_flush(self): |
| 201 |
import zlib |
| 202 |
co = zlib.compressobj(zlib.Z_BEST_COMPRESSION) |
| 203 |
assert co.flush() |
| 204 |
dco = zlib.decompressobj() |
| 205 |
assert dco.flush() == b"" |
| 206 |
|
| 207 |
def test_decompress_eof(self): |
| 208 |
import zlib |
| 209 |
x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' |
| 210 |
dco = zlib.decompressobj() |
| 211 |
assert dco.eof == False |
| 212 |
dco.decompress(x[:-5]) |
| 213 |
assert dco.eof == False |
| 214 |
dco.decompress(x[-5:]) |
| 215 |
assert dco.eof == True |
| 216 |
dco.flush() |
| 217 |
assert dco.eof == True |
| 218 |
|
| 219 |
def test_decompress_eof_incomplete_stream(self): |
| 220 |
import zlib |
| 221 |
x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' |
| 222 |
dco = zlib.decompressobj() |
| 223 |
assert dco.eof == False |
| 224 |
dco.decompress(x[:-5]) |
| 225 |
assert dco.eof == False |
| 226 |
dco.flush() |
| 227 |
assert dco.eof == False |
| 228 |
|
| 229 |
def test_decompress_incomplete_stream(self): |
| 230 |
import zlib |
| 231 |
|
| 232 |
x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' |
| 233 |
|
| 234 |
assert zlib.decompress(x) == b'foo' |
| 235 |
raises(zlib.error, zlib.decompress, x[:-5]) |
| 236 |
|
| 237 |
|
| 238 |
dco = zlib.decompressobj() |
| 239 |
y = dco.decompress(x[:-5]) |
| 240 |
y += dco.flush() |
| 241 |
assert y == b'foo' |
| 242 |
|
| 243 |
def test_unused_data(self): |
| 244 |
""" |
| 245 |
Try to feed too much data to zlib.decompress(). |
| 246 |
It should show up in the unused_data attribute. |
| 247 |
""" |
| 248 |
d = self.zlib.decompressobj() |
| 249 |
s = d.decompress(self.compressed + b'extrastuff', 0) |
| 250 |
assert s == self.expanded |
| 251 |
assert d.unused_data == b'extrastuff' |
| 252 |
assert d.flush() == b'' |
| 253 |
assert d.unused_data == b'extrastuff' |
| 254 |
|
| 255 |
d = self.zlib.decompressobj() |
| 256 |
s1 = d.decompress(self.compressed[:10]) |
| 257 |
assert d.unused_data == b'' |
| 258 |
s2 = d.decompress(self.compressed[10:-3]) |
| 259 |
assert d.unused_data == b'' |
| 260 |
s3 = d.decompress(self.compressed[-3:] + b'spam' * 100) |
| 261 |
assert d.unused_data == b'spam' * 100 |
| 262 |
assert s1 + s2 + s3 == self.expanded |
| 263 |
s4 = d.decompress(b'egg' * 50) |
| 264 |
assert d.unused_data == (b'spam' * 100) + (b'egg' * 50) |
| 265 |
assert s4 == b'' |
| 266 |
|
| 267 |
def test_max_length(self): |
| 268 |
""" |
| 269 |
Test the max_length argument of the decompress() method |
| 270 |
and the corresponding unconsumed_tail attribute. |
| 271 |
""" |
| 272 |
d = self.zlib.decompressobj() |
| 273 |
data = self.compressed |
| 274 |
for i in range(0, 100, 10): |
| 275 |
s1 = d.decompress(data, 10) |
| 276 |
assert s1 == self.expanded[i:i+10] |
| 277 |
data = d.unconsumed_tail |
| 278 |
assert not data |
| 279 |
|
| 280 |
def test_max_length_large(self): |
| 281 |
import sys |
| 282 |
d = self.zlib.decompressobj() |
| 283 |
assert d.decompress(self.compressed, sys.maxsize) == self.expanded |
| 284 |
|
| 285 |
def test_buffer(self): |
| 286 |
""" |
| 287 |
We should be able to pass buffer objects instead of strings. |
| 288 |
""" |
| 289 |
assert self.zlib.crc32(memoryview(b'hello, world.')) == 3358036098 |
| 290 |
assert self.zlib.adler32(memoryview(b'hello, world.')) == 571147447 |
| 291 |
|
| 292 |
compressor = self.zlib.compressobj() |
| 293 |
bytes = compressor.compress(memoryview(self.expanded)) |
| 294 |
bytes += compressor.flush() |
| 295 |
assert bytes == self.compressed |
| 296 |
|
| 297 |
decompressor = self.zlib.decompressobj() |
| 298 |
bytes = decompressor.decompress(memoryview(self.compressed)) |
| 299 |
bytes += decompressor.flush() |
| 300 |
assert bytes == self.expanded |
| 301 |
|
| 302 |
bytes = self.zlib.compress(memoryview(self.expanded)) |
| 303 |
assert bytes == self.compressed |
| 304 |
|
| 305 |
bytes = self.zlib.decompress(memoryview(self.compressed)) |
| 306 |
assert bytes == self.expanded |
| 307 |
|
| 308 |
def test_flush_with_freed_input(self): |
| 309 |
|
| 310 |
|
| 311 |
input1 = b'abcdefghijklmnopqrstuvwxyz' |
| 312 |
input2 = b'QWERTYUIOPASDFGHJKLZXCVBNM' |
| 313 |
data = self.zlib.compress(input1) |
| 314 |
dco = self.zlib.decompressobj() |
| 315 |
dco.decompress(data, 1) |
| 316 |
del data |
| 317 |
data = self.zlib.compress(input2) |
| 318 |
assert dco.flush(1) == input1[1:] |
| 319 |
assert dco.unused_data == b'' |
| 320 |
assert dco.unconsumed_tail == b'' |
| 321 |
|
| 322 |
def test_dictionary(self): |
| 323 |
l = self.LICENSE |
| 324 |
|
| 325 |
words = l.split() |
| 326 |
zdict = b''.join(set(words)) |
| 327 |
|
| 328 |
co = self.zlib.compressobj(zdict=zdict) |
| 329 |
cd = co.compress(l) + co.flush() |
| 330 |
|
| 331 |
dco = self.zlib.decompressobj(zdict=zdict) |
| 332 |
assert dco.decompress(cd) + dco.flush() == l |
| 333 |
|
| 334 |
dco = self.zlib.decompressobj() |
| 335 |
raises(self.zlib.error, dco.decompress, cd) |
| 336 |
|
| 337 |
def test_dictionary_streaming(self): |
| 338 |
|
| 339 |
|
| 340 |
co = self.zlib.compressobj(zdict=self.LICENSE) |
| 341 |
do = self.zlib.decompressobj(zdict=self.LICENSE) |
| 342 |
piece = self.LICENSE[1000:1500] |
| 343 |
d0 = co.compress(piece) + co.flush(self.zlib.Z_SYNC_FLUSH) |
| 344 |
d1 = co.compress(piece[100:]) + co.flush(self.zlib.Z_SYNC_FLUSH) |
| 345 |
d2 = co.compress(piece[:-100]) + co.flush(self.zlib.Z_SYNC_FLUSH) |
| 346 |
assert do.decompress(d0) == piece |
| 347 |
do.decompress(d1) == piece[100:] |
| 348 |
do.decompress(d2) == piece[:-100] |
| 349 |
|
| 350 |
def test_version(self): |
| 351 |
zlib = self.zlib |
| 352 |
assert zlib.ZLIB_VERSION[0] == zlib.ZLIB_RUNTIME_VERSION[0] |
| 353 |
|
| 354 |
|
| 355 |
def test_decompress_raw_with_dictionary(self): |
| 356 |
zlib = self.zlib |
| 357 |
zdict = b'abcdefghijklmnopqrstuvwxyz' |
| 358 |
co = zlib.compressobj(wbits=-zlib.MAX_WBITS, zdict=zdict) |
| 359 |
comp = co.compress(zdict) + co.flush() |
| 360 |
dco = zlib.decompressobj(wbits=-zlib.MAX_WBITS, zdict=zdict) |
| 361 |
uncomp = dco.decompress(comp) + dco.flush() |
| 362 |
assert zdict == uncomp |
| 363 |
|
| 364 |
def test_decompress_copy(self): |
| 365 |
import copy |
| 366 |
for func in [lambda d: d.copy(), copy.copy, copy.deepcopy]: |
| 367 |
decompressor = self.zlib.decompressobj() |
| 368 |
d1 = decompressor.decompress(self.compressed[:10]) |
| 369 |
assert d1 |
| 370 |
|
| 371 |
copied = func(decompressor) |
| 372 |
|
| 373 |
from_copy = copied.decompress(self.compressed[10:]) |
| 374 |
from_decompressor = decompressor.decompress(self.compressed[10:]) |
| 375 |
|
| 376 |
assert (d1 + from_copy) == (d1 + from_decompressor) |
| 377 |
|
| 378 |
def test_cannot_copy_decompressor_with_stream_in_inconsistent_state(self): |
| 379 |
if self.runappdirect: skip("can't run with -A") |
| 380 |
decompressor = self.zlib.decompressobj() |
| 381 |
self.intentionally_break_a_z_stream(zobj=decompressor) |
| 382 |
raises(self.zlib.error, decompressor.copy) |
| 383 |
|
| 384 |
def test_decompress_copy_carries_along_state(self): |
| 385 |
""" |
| 386 |
Decompressor.unused_data and unconsumed_tail are carried along when a |
| 387 |
copy is done. |
| 388 |
""" |
| 389 |
decompressor = self.zlib.decompressobj() |
| 390 |
decompressor.decompress(self.compressed, max_length=5) |
| 391 |
unconsumed_tail = decompressor.unconsumed_tail |
| 392 |
assert unconsumed_tail |
| 393 |
assert decompressor.copy().unconsumed_tail == unconsumed_tail |
| 394 |
|
| 395 |
decompressor.decompress(decompressor.unconsumed_tail) |
| 396 |
decompressor.decompress(b"xxx") |
| 397 |
unused_data = decompressor.unused_data |
| 398 |
assert unused_data |
| 399 |
assert decompressor.copy().unused_data == unused_data |
| 400 |
|
| 401 |
def test_compress_copy(self): |
| 402 |
import copy |
| 403 |
for func in [lambda c: c.copy(), copy.copy, copy.deepcopy]: |
| 404 |
compressor = self.zlib.compressobj() |
| 405 |
d1 = compressor.compress(self.expanded[:10]) |
| 406 |
assert d1 |
| 407 |
|
| 408 |
copied = func(compressor) |
| 409 |
|
| 410 |
from_copy = copied.compress(self.expanded[10:]) |
| 411 |
from_compressor = compressor.compress(self.expanded[10:]) |
| 412 |
|
| 413 |
assert (d1 + from_copy) == (d1 + from_compressor) |
| 414 |
|
| 415 |
@py.test.mark.skipif(rzlib.ZLIB_VERSION in ('1.2.7', '1.2.8', '1.2.3'), reason='does not error check') |
| 416 |
def test_cannot_copy_compressor_with_stream_in_inconsistent_state(self): |
| 417 |
if self.runappdirect: skip("can't run with -A") |
| 418 |
compressor = self.zlib.compressobj() |
| 419 |
self.intentionally_break_a_z_stream(zobj=compressor) |
| 420 |
raises(self.zlib.error, compressor.copy) |
| 421 |
|
| 422 |
def test_cannot_copy_compressor_with_flushed_stream(self): |
| 423 |
compressor = self.zlib.compressobj() |
| 424 |
compressor.flush() |
| 425 |
raises(ValueError, compressor.copy) |
| 426 |
|
| 427 |
def test_double_flush(self): |
| 428 |
import zlib |
| 429 |
x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' |
| 430 |
dco = zlib.decompressobj() |
| 431 |
dco.decompress(x) |
| 432 |
dco.flush() |
| 433 |
|
| 434 |
dco.flush() |
| 435 |
|
| 436 |
def test_constants(self): |
| 437 |
|
| 438 |
import zlib |
| 439 |
contents = dir(zlib) |
| 440 |
for c in ''' |
| 441 |
Z_NO_COMPRESSION Z_RLE Z_PARTIAL_FLUSH Z_FIXED Z_BLOCK Z_TREES |
| 442 |
'''.split(): |
| 443 |
assert c in contents |