| 1 |
import lz4.block |
| 2 |
import pytest |
| 3 |
import sys |
| 4 |
import os |
| 5 |
|
| 6 |
|
| 7 |
def test_decompress_ui32_overflow(): |
| 8 |
data = lz4.block.compress(b'A' * 64) |
| 9 |
with pytest.raises(OverflowError): |
| 10 |
lz4.block.decompress(data[4:], uncompressed_size=((1 << 32) + 64)) |
| 11 |
|
| 12 |
|
| 13 |
def test_decompress_without_leak(): |
| 14 |
|
| 15 |
data = lz4.block.compress(b'A' * 64) |
| 16 |
message = r'^Decompressor wrote 64 bytes, but 79 bytes expected from header$' |
| 17 |
with pytest.raises(lz4.block.LZ4BlockError, match=message): |
| 18 |
lz4.block.decompress(b'\x4f' + data[1:]) |
| 19 |
|
| 20 |
|
| 21 |
def test_decompress_with_small_buffer(): |
| 22 |
data = lz4.block.compress(b'A' * 64, store_size=False) |
| 23 |
message = r'^Decompression failed: corrupt input or insufficient space in destination buffer. Error code: \d+$' |
| 24 |
with pytest.raises(lz4.block.LZ4BlockError, match=message): |
| 25 |
lz4.block.decompress(data[4:], uncompressed_size=64) |
| 26 |
with pytest.raises(lz4.block.LZ4BlockError, match=message): |
| 27 |
lz4.block.decompress(data, uncompressed_size=60) |
| 28 |
|
| 29 |
|
| 30 |
def test_decompress_truncated(): |
| 31 |
input_data = b"2099023098234882923049823094823094898239230982349081231290381209380981203981209381238901283098908123109238098123" * 24 |
| 32 |
compressed = lz4.block.compress(input_data) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
with pytest.raises(ValueError, match='Input source data size too small'): |
| 39 |
lz4.block.decompress(compressed[:0]) |
| 40 |
for n in [0, 1]: |
| 41 |
with pytest.raises(ValueError, match='Input source data size too small'): |
| 42 |
lz4.block.decompress(compressed[:n]) |
| 43 |
for n in [24, 25, -2, 27, 67, 85]: |
| 44 |
with pytest.raises(lz4.block.LZ4BlockError): |
| 45 |
lz4.block.decompress(compressed[:n]) |
| 46 |
|
| 47 |
|
| 48 |
def test_decompress_with_trailer(): |
| 49 |
data = b'A' * 64 |
| 50 |
comp = lz4.block.compress(data) |
| 51 |
message = r'^Decompression failed: corrupt input or insufficient space in destination buffer. Error code: \d+$' |
| 52 |
with pytest.raises(lz4.block.LZ4BlockError, match=message): |
| 53 |
lz4.block.decompress(comp + b'A') |
| 54 |
with pytest.raises(lz4.block.LZ4BlockError, match=message): |
| 55 |
lz4.block.decompress(comp + comp) |
| 56 |
with pytest.raises(lz4.block.LZ4BlockError, match=message): |
| 57 |
lz4.block.decompress(comp + comp[4:]) |
| 58 |
|
| 59 |
|
| 60 |
def test_unicode(): |
| 61 |
if sys.version_info < (3,): |
| 62 |
return |
| 63 |
DATA = b'x' |
| 64 |
with pytest.raises(TypeError): |
| 65 |
lz4.block.compress(DATA.decode('latin1')) |
| 66 |
lz4.block.decompress(lz4.block.compress(DATA).decode('latin1')) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
def test_return_bytearray(): |
| 73 |
if sys.version_info < (3,): |
| 74 |
return |
| 75 |
data = os.urandom(128 * 1024) |
| 76 |
compressed = lz4.block.compress(data) |
| 77 |
b = lz4.block.compress(data, return_bytearray=True) |
| 78 |
assert isinstance(b, bytearray) |
| 79 |
assert bytes(b) == compressed |
| 80 |
b = lz4.block.decompress(compressed, return_bytearray=True) |
| 81 |
assert isinstance(b, bytearray) |
| 82 |
assert bytes(b) == data |
| 83 |
|
| 84 |
|
| 85 |
def test_memoryview(): |
| 86 |
if sys.version_info < (2, 7): |
| 87 |
return |
| 88 |
data = os.urandom(128 * 1024) |
| 89 |
compressed = lz4.block.compress(data) |
| 90 |
assert lz4.block.compress(memoryview(data)) == compressed |
| 91 |
assert lz4.block.decompress(memoryview(compressed)) == data |
| 92 |
|
| 93 |
|
| 94 |
def test_with_dict_none(): |
| 95 |
input_data = b"2099023098234882923049823094823094898239230982349081231290381209380981203981209381238901283098908123109238098123" * 24 |
| 96 |
for mode in ['default', 'high_compression']: |
| 97 |
assert lz4.block.decompress(lz4.block.compress( |
| 98 |
input_data, mode=mode, dict=None)) == input_data |
| 99 |
assert lz4.block.decompress(lz4.block.compress( |
| 100 |
input_data, mode=mode), dict=None) == input_data |
| 101 |
assert lz4.block.decompress(lz4.block.compress( |
| 102 |
input_data, mode=mode, dict=b'')) == input_data |
| 103 |
assert lz4.block.decompress(lz4.block.compress( |
| 104 |
input_data, mode=mode), dict=b'') == input_data |
| 105 |
assert lz4.block.decompress(lz4.block.compress( |
| 106 |
input_data, mode=mode, dict='')) == input_data |
| 107 |
assert lz4.block.decompress(lz4.block.compress( |
| 108 |
input_data, mode=mode), dict='') == input_data |
| 109 |
|
| 110 |
|
| 111 |
def test_with_dict(): |
| 112 |
input_data = b"2099023098234882923049823094823094898239230982349081231290381209380981203981209381238901283098908123109238098123" * 24 |
| 113 |
dict1 = input_data[10:30] |
| 114 |
dict2 = input_data[20:40] |
| 115 |
message = r'^Decompression failed: corrupt input or insufficient space in destination buffer. Error code: \d+$' |
| 116 |
for mode in ['default', 'high_compression']: |
| 117 |
compressed = lz4.block.compress(input_data, mode=mode, dict=dict1) |
| 118 |
with pytest.raises(lz4.block.LZ4BlockError, match=message): |
| 119 |
lz4.block.decompress(compressed) |
| 120 |
with pytest.raises(lz4.block.LZ4BlockError, match=message): |
| 121 |
lz4.block.decompress(compressed, dict=dict1[:2]) |
| 122 |
assert lz4.block.decompress(compressed, dict=dict2) != input_data |
| 123 |
assert lz4.block.decompress(compressed, dict=dict1) == input_data |
| 124 |
assert lz4.block.decompress(lz4.block.compress( |
| 125 |
input_data), dict=dict1) == input_data |
| 126 |
|
| 127 |
|
| 128 |
def test_known_decompress_1(): |
| 129 |
input = b'\x00\x00\x00\x00\x00' |
| 130 |
output = b'' |
| 131 |
assert lz4.block.decompress(input) == output |
| 132 |
|
| 133 |
|
| 134 |
def test_known_decompress_2(): |
| 135 |
input = b'\x01\x00\x00\x00\x10 ' |
| 136 |
output = b' ' |
| 137 |
assert lz4.block.decompress(input) == output |
| 138 |
|
| 139 |
|
| 140 |
def test_known_decompress_3(): |
| 141 |
input = b'h\x00\x00\x00\xff\x0bLorem ipsum dolor sit amet\x1a\x006P amet' |
| 142 |
output = b'Lorem ipsum dolor sit amet' * 4 |
| 143 |
assert lz4.block.decompress(input) == output |
| 144 |
|
| 145 |
|
| 146 |
def test_known_decompress_4(): |
| 147 |
input = b'\xb0\xb3\x00\x00\xff\x1fExcepteur sint occaecat cupidatat non proident.\x00' + (b'\xff' * 180) + b'\x1ePident' |
| 148 |
output = b'Excepteur sint occaecat cupidatat non proident' * 1000 |
| 149 |
assert lz4.block.decompress(input) == output |