| 1 |
|
| 2 |
""" |
| 3 |
Tests for the rzlib module. |
| 4 |
""" |
| 5 |
|
| 6 |
import py, sys |
| 7 |
from rpython.rlib import rzlib |
| 8 |
from rpython.rlib.rarithmetic import r_uint |
| 9 |
from rpython.rlib import clibffi |
| 10 |
import zlib |
| 11 |
|
| 12 |
expanded = 'some bytes which will be compressed' |
| 13 |
compressed = zlib.compress(expanded) |
| 14 |
|
| 15 |
|
| 16 |
def test_crc32(): |
| 17 |
""" |
| 18 |
When called with a string, rzlib.crc32 should compute its CRC32 and |
| 19 |
return it as a unsigned 32 bit integer. |
| 20 |
""" |
| 21 |
assert rzlib.crc32('') == r_uint(0) |
| 22 |
assert rzlib.crc32('\0') == r_uint(3523407757) |
| 23 |
assert rzlib.crc32('hello, world.') == r_uint(3358036098) |
| 24 |
|
| 25 |
|
| 26 |
def test_crc32_start_value(): |
| 27 |
""" |
| 28 |
When called with a string and an integer, zlib.crc32 should compute the |
| 29 |
CRC32 of the string using the integer as the starting value. |
| 30 |
""" |
| 31 |
assert rzlib.crc32('', 42) == r_uint(42) |
| 32 |
assert rzlib.crc32('\0', 42) == r_uint(163128923) |
| 33 |
assert rzlib.crc32('hello, world.', 42) == r_uint(1090960721) |
| 34 |
hello = 'hello, ' |
| 35 |
hellocrc = rzlib.crc32(hello) |
| 36 |
world = 'world.' |
| 37 |
helloworldcrc = rzlib.crc32(world, hellocrc) |
| 38 |
assert helloworldcrc == rzlib.crc32(hello + world) |
| 39 |
|
| 40 |
|
| 41 |
def test_adler32(): |
| 42 |
""" |
| 43 |
When called with a string, zlib.crc32 should compute its adler 32 |
| 44 |
checksum and return it as an unsigned 32 bit integer. |
| 45 |
""" |
| 46 |
assert rzlib.adler32('') == r_uint(1) |
| 47 |
assert rzlib.adler32('\0') == r_uint(65537) |
| 48 |
assert rzlib.adler32('hello, world.') == r_uint(571147447) |
| 49 |
assert rzlib.adler32('x' * 23) == r_uint(2172062409) |
| 50 |
|
| 51 |
|
| 52 |
def test_adler32_start_value(): |
| 53 |
""" |
| 54 |
When called with a string and an integer, zlib.adler32 should compute |
| 55 |
the adler 32 checksum of the string using the integer as the starting |
| 56 |
value. |
| 57 |
""" |
| 58 |
assert rzlib.adler32('', 42) == r_uint(42) |
| 59 |
assert rzlib.adler32('\0', 42) == r_uint(2752554) |
| 60 |
assert rzlib.adler32('hello, world.', 42) == r_uint(606078176) |
| 61 |
assert rzlib.adler32('x' * 23, 42) == r_uint(2233862898) |
| 62 |
hello = 'hello, ' |
| 63 |
hellosum = rzlib.adler32(hello) |
| 64 |
world = 'world.' |
| 65 |
helloworldsum = rzlib.adler32(world, hellosum) |
| 66 |
assert helloworldsum == rzlib.adler32(hello + world) |
| 67 |
|
| 68 |
|
| 69 |
def test_invalidLevel(): |
| 70 |
""" |
| 71 |
deflateInit() should raise ValueError when an out of bounds level is |
| 72 |
passed to it. |
| 73 |
""" |
| 74 |
py.test.raises(ValueError, rzlib.deflateInit, -2) |
| 75 |
py.test.raises(ValueError, rzlib.deflateInit, 10) |
| 76 |
|
| 77 |
|
| 78 |
def test_deflate_init_end(): |
| 79 |
""" |
| 80 |
deflateInit() followed by deflateEnd() should work and do nothing. |
| 81 |
""" |
| 82 |
stream = rzlib.deflateInit() |
| 83 |
rzlib.deflateEnd(stream) |
| 84 |
|
| 85 |
|
| 86 |
def test_deflate_set_dictionary(): |
| 87 |
text = 'abcabc' |
| 88 |
zdict = 'abc' |
| 89 |
stream = rzlib.deflateInit() |
| 90 |
rzlib.deflateSetDictionary(stream, zdict) |
| 91 |
bytes = rzlib.compress(stream, text, rzlib.Z_FINISH) |
| 92 |
rzlib.deflateEnd(stream) |
| 93 |
|
| 94 |
stream2 = rzlib.inflateInit() |
| 95 |
|
| 96 |
from rpython.rtyper.lltypesystem import lltype, rffi, rstr |
| 97 |
from rpython.rtyper.annlowlevel import llstr |
| 98 |
from rpython.rlib.rstring import StringBuilder |
| 99 |
with lltype.scoped_alloc(rffi.CCHARP.TO, len(bytes)) as inbuf: |
| 100 |
rstr.copy_string_to_raw(llstr(bytes), inbuf, 0, len(bytes)) |
| 101 |
stream2.c_next_in = rffi.cast(rzlib.Bytefp, inbuf) |
| 102 |
rffi.setintfield(stream2, 'c_avail_in', len(bytes)) |
| 103 |
with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as outbuf: |
| 104 |
stream2.c_next_out = rffi.cast(rzlib.Bytefp, outbuf) |
| 105 |
bufsize = 100 |
| 106 |
rffi.setintfield(stream2, 'c_avail_out', bufsize) |
| 107 |
err = rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH) |
| 108 |
assert err == rzlib.Z_NEED_DICT |
| 109 |
rzlib.inflateSetDictionary(stream2, zdict) |
| 110 |
rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH) |
| 111 |
avail_out = rffi.cast(lltype.Signed, stream2.c_avail_out) |
| 112 |
result = StringBuilder() |
| 113 |
result.append_charpsize(outbuf, bufsize - avail_out) |
| 114 |
|
| 115 |
rzlib.inflateEnd(stream2) |
| 116 |
assert result.build() == text |
| 117 |
|
| 118 |
|
| 119 |
def test_compression(): |
| 120 |
""" |
| 121 |
Once we have got a deflate stream, rzlib.compress() |
| 122 |
should allow us to compress bytes. |
| 123 |
""" |
| 124 |
stream = rzlib.deflateInit() |
| 125 |
bytes = rzlib.compress(stream, expanded) |
| 126 |
bytes += rzlib.compress(stream, "", rzlib.Z_FINISH) |
| 127 |
rzlib.deflateEnd(stream) |
| 128 |
assert bytes == compressed |
| 129 |
|
| 130 |
|
| 131 |
def test_compression_lots_of_data(): |
| 132 |
""" |
| 133 |
Test compression of more data that fits in a single internal output buffer. |
| 134 |
""" |
| 135 |
expanded = repr(range(20000)) |
| 136 |
compressed = zlib.compress(expanded) |
| 137 |
print len(expanded), '=>', len(compressed) |
| 138 |
stream = rzlib.deflateInit() |
| 139 |
bytes = rzlib.compress(stream, expanded, rzlib.Z_FINISH) |
| 140 |
rzlib.deflateEnd(stream) |
| 141 |
assert bytes == compressed |
| 142 |
|
| 143 |
|
| 144 |
def test_inflate_init_end(): |
| 145 |
""" |
| 146 |
inflateInit() followed by inflateEnd() should work and do nothing. |
| 147 |
""" |
| 148 |
stream = rzlib.inflateInit() |
| 149 |
rzlib.inflateEnd(stream) |
| 150 |
|
| 151 |
|
| 152 |
def test_decompression(): |
| 153 |
""" |
| 154 |
Once we have got a inflate stream, rzlib.decompress() |
| 155 |
should allow us to decompress bytes. |
| 156 |
""" |
| 157 |
stream = rzlib.inflateInit() |
| 158 |
bytes1, finished1, unused1 = rzlib.decompress(stream, compressed) |
| 159 |
bytes2, finished2, unused2 = rzlib.decompress(stream, "", rzlib.Z_FINISH) |
| 160 |
rzlib.inflateEnd(stream) |
| 161 |
assert bytes1 + bytes2 == expanded |
| 162 |
assert finished1 is True |
| 163 |
assert finished2 is True |
| 164 |
assert unused1 == 0 |
| 165 |
assert unused2 == 0 |
| 166 |
|
| 167 |
|
| 168 |
def test_decompression_lots_of_data(): |
| 169 |
""" |
| 170 |
Test compression of more data that fits in a single internal output buffer. |
| 171 |
""" |
| 172 |
expanded = repr(range(20000)) |
| 173 |
compressed = zlib.compress(expanded) |
| 174 |
print len(compressed), '=>', len(expanded) |
| 175 |
stream = rzlib.inflateInit() |
| 176 |
bytes, finished, unused = rzlib.decompress(stream, compressed, |
| 177 |
rzlib.Z_FINISH) |
| 178 |
rzlib.inflateEnd(stream) |
| 179 |
assert bytes == expanded |
| 180 |
assert finished is True |
| 181 |
assert unused == 0 |
| 182 |
|
| 183 |
|
| 184 |
def test_decompression_truncated_input(): |
| 185 |
""" |
| 186 |
Test that we can accept incomplete input when inflating, but also |
| 187 |
detect this situation when using Z_FINISH. |
| 188 |
""" |
| 189 |
expanded = repr(range(20000)) |
| 190 |
compressed = zlib.compress(expanded) |
| 191 |
print len(compressed), '=>', len(expanded) |
| 192 |
stream = rzlib.inflateInit() |
| 193 |
data, finished1, unused1 = rzlib.decompress(stream, compressed[:1000]) |
| 194 |
assert expanded.startswith(data) |
| 195 |
assert finished1 is False |
| 196 |
assert unused1 == 0 |
| 197 |
data2, finished2, unused2 = rzlib.decompress(stream, compressed[1000:2000]) |
| 198 |
data += data2 |
| 199 |
assert finished2 is False |
| 200 |
assert unused2 == 0 |
| 201 |
assert expanded.startswith(data) |
| 202 |
exc = py.test.raises( |
| 203 |
rzlib.RZlibError, |
| 204 |
rzlib.decompress, stream, compressed[2000:-500], rzlib.Z_FINISH) |
| 205 |
msg = "Error -5 while decompressing data: incomplete or truncated stream" |
| 206 |
assert str(exc.value) == msg |
| 207 |
rzlib.inflateEnd(stream) |
| 208 |
|
| 209 |
|
| 210 |
def test_decompression_too_much_input(): |
| 211 |
""" |
| 212 |
Check the case where we feed extra data to decompress(). |
| 213 |
""" |
| 214 |
stream = rzlib.inflateInit() |
| 215 |
data1, finished1, unused1 = rzlib.decompress(stream, compressed[:-5]) |
| 216 |
assert finished1 is False |
| 217 |
assert unused1 == 0 |
| 218 |
data2, finished2, unused2 = rzlib.decompress(stream, |
| 219 |
compressed[-5:] + 'garbage') |
| 220 |
assert finished2 is True |
| 221 |
assert unused2 == len('garbage') |
| 222 |
assert data1 + data2 == expanded |
| 223 |
data3, finished3, unused3 = rzlib.decompress(stream, 'more_garbage') |
| 224 |
assert finished3 is True |
| 225 |
assert unused3 == len('more_garbage') |
| 226 |
assert data3 == '' |
| 227 |
|
| 228 |
rzlib.deflateEnd(stream) |
| 229 |
|
| 230 |
|
| 231 |
def test_decompress_max_length(): |
| 232 |
""" |
| 233 |
Test the max_length argument of decompress(). |
| 234 |
""" |
| 235 |
stream = rzlib.inflateInit() |
| 236 |
data1, finished1, unused1 = rzlib.decompress(stream, compressed, |
| 237 |
max_length = 17) |
| 238 |
assert data1 == expanded[:17] |
| 239 |
assert finished1 is False |
| 240 |
assert unused1 > 0 |
| 241 |
data2, finished2, unused2 = rzlib.decompress(stream, compressed[-unused1:]) |
| 242 |
assert data2 == expanded[17:] |
| 243 |
assert finished2 is True |
| 244 |
assert unused2 == 0 |
| 245 |
|
| 246 |
rzlib.deflateEnd(stream) |
| 247 |
|
| 248 |
|
| 249 |
def test_compress_copy(): |
| 250 |
""" |
| 251 |
inflateCopy produces an independent copy of a stream. |
| 252 |
""" |
| 253 |
|
| 254 |
stream = rzlib.deflateInit() |
| 255 |
|
| 256 |
bytes1 = rzlib.compress(stream, expanded[:10]) |
| 257 |
assert bytes1 |
| 258 |
|
| 259 |
copied = rzlib.deflateCopy(stream) |
| 260 |
|
| 261 |
bytes_stream = rzlib.compress( |
| 262 |
stream, |
| 263 |
expanded[10:], |
| 264 |
rzlib.Z_FINISH, |
| 265 |
) |
| 266 |
assert bytes1 + bytes_stream == compressed |
| 267 |
rzlib.deflateEnd(stream) |
| 268 |
|
| 269 |
bytes_copy = rzlib.compress( |
| 270 |
copied, |
| 271 |
expanded[10:], |
| 272 |
rzlib.Z_FINISH, |
| 273 |
) |
| 274 |
rzlib.deflateEnd(copied) |
| 275 |
assert bytes1 + bytes_copy == compressed |
| 276 |
|
| 277 |
@py.test.mark.skipif(rzlib.ZLIB_VERSION in ('1.2.3', '1.2.8', '1.2.7'), reason='does not error check') |
| 278 |
def test_unsuccessful_compress_copy(): |
| 279 |
""" |
| 280 |
Errors during unsuccesful deflateCopy operations raise RZlibErrors. |
| 281 |
""" |
| 282 |
stream = rzlib.deflateInit() |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 289 |
stream.c_zalloc = rffi.cast(lltype.typeOf(stream.c_zalloc), rzlib.Z_NULL) |
| 290 |
|
| 291 |
exc = py.test.raises(rzlib.RZlibError, rzlib.deflateCopy, stream) |
| 292 |
msg = "Error -2 while copying compression object: inconsistent stream state" |
| 293 |
assert str(exc.value) == msg |
| 294 |
rzlib.deflateEnd(stream) |
| 295 |
|
| 296 |
|
| 297 |
def test_decompress_copy(): |
| 298 |
""" |
| 299 |
inflateCopy produces an independent copy of a stream. |
| 300 |
""" |
| 301 |
|
| 302 |
stream = rzlib.inflateInit() |
| 303 |
|
| 304 |
bytes1, finished1, unused1 = rzlib.decompress(stream, compressed[:10]) |
| 305 |
assert bytes1 |
| 306 |
assert finished1 is False |
| 307 |
|
| 308 |
copied = rzlib.inflateCopy(stream) |
| 309 |
|
| 310 |
bytes_stream, finished_stream, unused_stream = rzlib.decompress( |
| 311 |
stream, |
| 312 |
compressed[10:], |
| 313 |
rzlib.Z_FINISH, |
| 314 |
) |
| 315 |
assert bytes1 + bytes_stream == expanded |
| 316 |
assert finished_stream is True |
| 317 |
assert unused1 == 0 |
| 318 |
assert unused_stream == 0 |
| 319 |
rzlib.inflateEnd(stream) |
| 320 |
|
| 321 |
bytes_copy, finished_copy, unused_copy = rzlib.decompress( |
| 322 |
copied, |
| 323 |
compressed[10:], |
| 324 |
rzlib.Z_FINISH, |
| 325 |
) |
| 326 |
rzlib.inflateEnd(copied) |
| 327 |
assert bytes1 + bytes_copy == expanded |
| 328 |
assert finished_copy is True |
| 329 |
assert unused_copy == 0 |
| 330 |
|
| 331 |
|
| 332 |
def test_unsuccessful_decompress_copy(): |
| 333 |
""" |
| 334 |
Errors during unsuccesful inflateCopy operations raise RZlibErrors. |
| 335 |
""" |
| 336 |
stream = rzlib.inflateInit() |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 343 |
stream.c_zalloc = rffi.cast(lltype.typeOf(stream.c_zalloc), rzlib.Z_NULL) |
| 344 |
|
| 345 |
exc = py.test.raises(rzlib.RZlibError, rzlib.inflateCopy, stream) |
| 346 |
msg = "Error -2 while copying decompression object: inconsistent stream state" |
| 347 |
assert str(exc.value) == msg |
| 348 |
rzlib.inflateEnd(stream) |
| 349 |
|
| 350 |
|
| 351 |
def test_cornercases(): |
| 352 |
""" |
| 353 |
Test degenerate arguments. |
| 354 |
""" |
| 355 |
stream = rzlib.deflateInit() |
| 356 |
bytes = rzlib.compress(stream, "") |
| 357 |
bytes += rzlib.compress(stream, "") |
| 358 |
bytes += rzlib.compress(stream, "", rzlib.Z_FINISH) |
| 359 |
assert zlib.decompress(bytes) == "" |
| 360 |
rzlib.deflateEnd(stream) |
| 361 |
|
| 362 |
stream = rzlib.inflateInit() |
| 363 |
data, finished, unused = rzlib.decompress(stream, "") |
| 364 |
assert data == "" |
| 365 |
assert finished is False |
| 366 |
assert unused == 0 |
| 367 |
buf = compressed |
| 368 |
for i in range(10): |
| 369 |
data, finished, unused = rzlib.decompress(stream, buf, max_length=0) |
| 370 |
assert data == "" |
| 371 |
assert finished is False |
| 372 |
assert unused > 0 |
| 373 |
buf = buf[-unused:] |
| 374 |
rzlib.deflateEnd(stream) |
| 375 |
|
| 376 |
def test_zlibVersion(): |
| 377 |
runtime_version = rzlib.zlibVersion() |
| 378 |
assert runtime_version[0] == rzlib.ZLIB_VERSION[0] |
| 379 |
|
| 380 |
def test_translate_and_large_input(): |
| 381 |
from rpython.translator.c.test.test_genc import compile |
| 382 |
|
| 383 |
def f(i, check): |
| 384 |
bytes = "s" * i |
| 385 |
if check == 1: |
| 386 |
for j in range(3): |
| 387 |
stream = rzlib.deflateInit() |
| 388 |
bytes = rzlib.compress(stream, bytes, rzlib.Z_FINISH) |
| 389 |
rzlib.deflateEnd(stream) |
| 390 |
return bytes |
| 391 |
if check == 2: |
| 392 |
return str(rzlib.adler32(bytes)) |
| 393 |
if check == 3: |
| 394 |
return str(rzlib.crc32(bytes)) |
| 395 |
return '?' |
| 396 |
|
| 397 |
fc = compile(f, [int, int]) |
| 398 |
|
| 399 |
test_list = [1, 2, 3, 5, 8, 87, 876, 8765, 87654, 876543, 8765432, |
| 400 |
127329129] |
| 401 |
if sys.maxint > 2**32: |
| 402 |
test_list.append(4305704715) |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
for a in test_list: |
| 407 |
print 'Testing compression of "s" * %d' % a |
| 408 |
z = zlib.compressobj() |
| 409 |
count = a |
| 410 |
pieces = [] |
| 411 |
while count > 1024*1024: |
| 412 |
pieces.append(z.compress("s" * (1024*1024))) |
| 413 |
count -= 1024*1024 |
| 414 |
pieces.append(z.compress("s" * count)) |
| 415 |
pieces.append(z.flush(zlib.Z_FINISH)) |
| 416 |
expected = ''.join(pieces) |
| 417 |
del pieces |
| 418 |
expected = zlib.compress(expected) |
| 419 |
expected = zlib.compress(expected) |
| 420 |
assert fc(a, 1) == expected |
| 421 |
|
| 422 |
print 'Testing adler32 and crc32 of "s" * %d' % a |
| 423 |
def compute(function, start): |
| 424 |
count = a |
| 425 |
while count > 0: |
| 426 |
count1 = min(count, 1024*1024) |
| 427 |
start = function("s" * count1, start) |
| 428 |
count -= count1 |
| 429 |
return start |
| 430 |
expected_adler32 = compute(zlib.adler32, 1) & (2**32-1) |
| 431 |
expected_crc32 = compute(zlib.crc32, 0) & (2**32-1) |
| 432 |
assert fc(a, 2) == str(expected_adler32) |
| 433 |
assert fc(a, 3) == str(expected_crc32) |