| 1 |
import lz4.stream |
| 2 |
import pytest |
| 3 |
import sys |
| 4 |
|
| 5 |
|
| 6 |
if sys.version_info < (3, ): |
| 7 |
from struct import pack, unpack |
| 8 |
|
| 9 |
def _get_format(length, byteorder, signed): |
| 10 |
_order = {'l': '<', 'b': '>'} |
| 11 |
_fmt = {1: 'b', 2: 'h', 4: 'i', 8: 'q'} |
| 12 |
_sign = {True: lambda x: x.lower(), False: lambda x: x.upper()} |
| 13 |
return _sign[signed](_order[byteorder[0].lower()] + _fmt[length]) |
| 14 |
|
| 15 |
def int_to_bytes(value, length=4, byteorder='little', signed=False): |
| 16 |
return bytearray(pack(_get_format(length, byteorder, signed), value)) |
| 17 |
|
| 18 |
def int_from_bytes(bytes, byteorder='little', signed=False): |
| 19 |
return unpack(_get_format(len(bytes), byteorder, signed), bytes)[0] |
| 20 |
|
| 21 |
else: |
| 22 |
def int_to_bytes(value, length=4, byteorder='little', signed=False): |
| 23 |
return value.to_bytes(length, byteorder, signed=signed) |
| 24 |
|
| 25 |
def int_from_bytes(bytes, byteorder='little', signed=False): |
| 26 |
return int.from_bytes(bytes, byteorder, signed=signed) |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
def test_round_trip(): |
| 32 |
data = b"2099023098234882923049823094823094898239230982349081231290381209380981203981209381238901283098908123109238098123" * 24 |
| 33 |
kwargs = {'strategy': "double_buffer", 'buffer_size': 256, 'store_comp_size': 4} |
| 34 |
|
| 35 |
oob_kwargs = {} |
| 36 |
oob_kwargs.update(kwargs) |
| 37 |
oob_kwargs['store_comp_size'] = 0 |
| 38 |
|
| 39 |
ib_cstream = bytearray() |
| 40 |
oob_cstream = bytearray() |
| 41 |
oob_sizes = [] |
| 42 |
|
| 43 |
with lz4.stream.LZ4StreamCompressor(**kwargs) as ib_proc, \ |
| 44 |
lz4.stream.LZ4StreamCompressor(**oob_kwargs) as oob_proc: |
| 45 |
for start in range(0, len(data), kwargs['buffer_size']): |
| 46 |
chunk = data[start:start + kwargs['buffer_size']] |
| 47 |
ib_block = ib_proc.compress(chunk) |
| 48 |
oob_block = oob_proc.compress(chunk) |
| 49 |
|
| 50 |
assert (len(ib_block) == (len(oob_block) + kwargs['store_comp_size'])), \ |
| 51 |
"Blocks size mismatch: " \ |
| 52 |
"{}/{}".format(len(ib_block), len(oob_block) + kwargs['store_comp_size']) |
| 53 |
|
| 54 |
assert (int_from_bytes(ib_block[:kwargs['store_comp_size']]) == len(oob_block)), \ |
| 55 |
"Blocks size record mismatch: got {}, expected {}".format( |
| 56 |
int_from_bytes(ib_block[:kwargs['store_comp_size']]), |
| 57 |
len(oob_block)) |
| 58 |
|
| 59 |
assert (ib_block[kwargs['store_comp_size']:] == oob_block), "Blocks data mismatch" |
| 60 |
|
| 61 |
ib_cstream += ib_block |
| 62 |
oob_cstream += oob_block |
| 63 |
oob_sizes.append(len(oob_block)) |
| 64 |
|
| 65 |
ib_dstream = bytearray() |
| 66 |
oob_dstream = bytearray() |
| 67 |
|
| 68 |
with lz4.stream.LZ4StreamDecompressor(**kwargs) as ib_proc, \ |
| 69 |
lz4.stream.LZ4StreamDecompressor(**oob_kwargs) as oob_proc: |
| 70 |
ib_offset = 0 |
| 71 |
oob_index = 0 |
| 72 |
oob_offset = 0 |
| 73 |
while ib_offset < len(ib_cstream) and oob_index < len(oob_sizes): |
| 74 |
ib_block = ib_proc.get_block(ib_cstream[ib_offset:]) |
| 75 |
oob_block = oob_cstream[oob_offset:oob_offset + oob_sizes[oob_index]] |
| 76 |
|
| 77 |
assert (len(ib_block) == len(oob_block)), \ |
| 78 |
"Blocks size mismatch: {}/{}".format(len(ib_block), len(oob_block)) |
| 79 |
|
| 80 |
assert (ib_block == oob_block), "Blocks data mismatch" |
| 81 |
|
| 82 |
ib_chunk = ib_proc.decompress(ib_block) |
| 83 |
oob_chunk = oob_proc.decompress(oob_block) |
| 84 |
|
| 85 |
assert (len(ib_chunk) == len(oob_chunk)), \ |
| 86 |
"Chunks size mismatch: {}/{}".format(len(ib_chunk), len(oob_chunk)) |
| 87 |
|
| 88 |
assert (ib_chunk == oob_chunk), "Chunks data mismatch" |
| 89 |
|
| 90 |
ib_dstream += ib_chunk |
| 91 |
oob_dstream += oob_chunk |
| 92 |
|
| 93 |
ib_offset += kwargs['store_comp_size'] + len(ib_block) |
| 94 |
oob_offset += oob_sizes[oob_index] |
| 95 |
oob_index += 1 |
| 96 |
|
| 97 |
assert (len(ib_dstream) == len(oob_dstream)), "Decompressed streams length mismatch" |
| 98 |
|
| 99 |
assert (len(data) == len(ib_dstream)), "Decompressed streams length mismatch" |
| 100 |
|
| 101 |
assert (len(data) == len(oob_dstream)), "Decompressed streams length mismatch" |
| 102 |
|
| 103 |
assert (ib_dstream == oob_dstream), "Decompressed streams mismatch" |
| 104 |
|
| 105 |
assert (data == ib_dstream), "Decompressed streams mismatch" |
| 106 |
|
| 107 |
assert (data == oob_dstream), "Decompressed streams mismatch" |
| 108 |
|
| 109 |
|
| 110 |
def test_invalid_usage(): |
| 111 |
data = b"2099023098234882923049823094823094898239230982349081231290381209380981203981209381238901283098908123109238098123" * 24 |
| 112 |
kwargs = {'strategy': "double_buffer", 'buffer_size': 256, 'store_comp_size': 0} |
| 113 |
|
| 114 |
cstream = bytearray() |
| 115 |
oob_sizes = [] |
| 116 |
|
| 117 |
with lz4.stream.LZ4StreamCompressor(**kwargs) as proc: |
| 118 |
for start in range(0, len(data), kwargs['buffer_size']): |
| 119 |
chunk = data[start:start + kwargs['buffer_size']] |
| 120 |
block = proc.compress(chunk) |
| 121 |
cstream += block |
| 122 |
oob_sizes.append(len(block)) |
| 123 |
|
| 124 |
message = r"^LZ4 context is configured for storing block size out-of-band$" |
| 125 |
|
| 126 |
with pytest.raises(lz4.stream.LZ4StreamError, match=message): |
| 127 |
dstream = bytearray() |
| 128 |
|
| 129 |
with lz4.stream.LZ4StreamDecompressor(**kwargs) as proc: |
| 130 |
offset = 0 |
| 131 |
index = 0 |
| 132 |
while offset < len(cstream): |
| 133 |
block = proc.get_block(cstream[offset:]) |
| 134 |
chunk = proc.decompress(block) |
| 135 |
|
| 136 |
dstream += chunk |
| 137 |
|
| 138 |
offset += kwargs['store_comp_size'] + len(block) |
| 139 |
index += 1 |