test_block_1.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import lz4.block
  2. import pytest
  3. import sys
  4. import os
  5. def test_decompress_ui32_overflow():
  6. data = lz4.block.compress(b'A' * 64)
  7. with pytest.raises(OverflowError):
  8. lz4.block.decompress(data[4:], uncompressed_size=((1 << 32) + 64))
  9. def test_decompress_without_leak():
  10. # Verify that hand-crafted packet does not leak uninitialized(?) memory.
  11. data = lz4.block.compress(b'A' * 64)
  12. message = r'^Decompressor wrote 64 bytes, but 79 bytes expected from header$'
  13. with pytest.raises(lz4.block.LZ4BlockError, match=message):
  14. lz4.block.decompress(b'\x4f' + data[1:])
  15. def test_decompress_with_small_buffer():
  16. data = lz4.block.compress(b'A' * 64, store_size=False)
  17. message = r'^Decompression failed: corrupt input or insufficient space in destination buffer. Error code: \d+$'
  18. with pytest.raises(lz4.block.LZ4BlockError, match=message):
  19. lz4.block.decompress(data[4:], uncompressed_size=64)
  20. with pytest.raises(lz4.block.LZ4BlockError, match=message):
  21. lz4.block.decompress(data, uncompressed_size=60)
  22. def test_decompress_truncated():
  23. input_data = b"2099023098234882923049823094823094898239230982349081231290381209380981203981209381238901283098908123109238098123" * 24
  24. compressed = lz4.block.compress(input_data)
  25. # for i in range(len(compressed)):
  26. # try:
  27. # lz4.block.decompress(compressed[:i])
  28. # except:
  29. # print(i, sys.exc_info()[0], sys.exc_info()[1])
  30. with pytest.raises(ValueError, match='Input source data size too small'):
  31. lz4.block.decompress(compressed[:0])
  32. for n in [0, 1]:
  33. with pytest.raises(ValueError, match='Input source data size too small'):
  34. lz4.block.decompress(compressed[:n])
  35. for n in [24, 25, -2, 27, 67, 85]:
  36. with pytest.raises(lz4.block.LZ4BlockError):
  37. lz4.block.decompress(compressed[:n])
  38. def test_decompress_with_trailer():
  39. data = b'A' * 64
  40. comp = lz4.block.compress(data)
  41. message = r'^Decompression failed: corrupt input or insufficient space in destination buffer. Error code: \d+$'
  42. with pytest.raises(lz4.block.LZ4BlockError, match=message):
  43. lz4.block.decompress(comp + b'A')
  44. with pytest.raises(lz4.block.LZ4BlockError, match=message):
  45. lz4.block.decompress(comp + comp)
  46. with pytest.raises(lz4.block.LZ4BlockError, match=message):
  47. lz4.block.decompress(comp + comp[4:])
  48. def test_unicode():
  49. if sys.version_info < (3,):
  50. return # skip
  51. DATA = b'x'
  52. with pytest.raises(TypeError):
  53. lz4.block.compress(DATA.decode('latin1'))
  54. lz4.block.decompress(lz4.block.compress(DATA).decode('latin1'))
  55. # These next two are probably redundant given test_1 above but we'll keep them
  56. # for now
  57. def test_return_bytearray():
  58. if sys.version_info < (3,):
  59. return # skip
  60. data = os.urandom(128 * 1024) # Read 128kb
  61. compressed = lz4.block.compress(data)
  62. b = lz4.block.compress(data, return_bytearray=True)
  63. assert isinstance(b, bytearray)
  64. assert bytes(b) == compressed
  65. b = lz4.block.decompress(compressed, return_bytearray=True)
  66. assert isinstance(b, bytearray)
  67. assert bytes(b) == data
  68. def test_memoryview():
  69. if sys.version_info < (2, 7):
  70. return # skip
  71. data = os.urandom(128 * 1024) # Read 128kb
  72. compressed = lz4.block.compress(data)
  73. assert lz4.block.compress(memoryview(data)) == compressed
  74. assert lz4.block.decompress(memoryview(compressed)) == data
  75. def test_with_dict_none():
  76. input_data = b"2099023098234882923049823094823094898239230982349081231290381209380981203981209381238901283098908123109238098123" * 24
  77. for mode in ['default', 'high_compression']:
  78. assert lz4.block.decompress(lz4.block.compress(
  79. input_data, mode=mode, dict=None)) == input_data
  80. assert lz4.block.decompress(lz4.block.compress(
  81. input_data, mode=mode), dict=None) == input_data
  82. assert lz4.block.decompress(lz4.block.compress(
  83. input_data, mode=mode, dict=b'')) == input_data
  84. assert lz4.block.decompress(lz4.block.compress(
  85. input_data, mode=mode), dict=b'') == input_data
  86. assert lz4.block.decompress(lz4.block.compress(
  87. input_data, mode=mode, dict='')) == input_data
  88. assert lz4.block.decompress(lz4.block.compress(
  89. input_data, mode=mode), dict='') == input_data
  90. def test_with_dict():
  91. input_data = b"2099023098234882923049823094823094898239230982349081231290381209380981203981209381238901283098908123109238098123" * 24
  92. dict1 = input_data[10:30]
  93. dict2 = input_data[20:40]
  94. message = r'^Decompression failed: corrupt input or insufficient space in destination buffer. Error code: \d+$'
  95. for mode in ['default', 'high_compression']:
  96. compressed = lz4.block.compress(input_data, mode=mode, dict=dict1)
  97. with pytest.raises(lz4.block.LZ4BlockError, match=message):
  98. lz4.block.decompress(compressed)
  99. with pytest.raises(lz4.block.LZ4BlockError, match=message):
  100. lz4.block.decompress(compressed, dict=dict1[:2])
  101. assert lz4.block.decompress(compressed, dict=dict2) != input_data
  102. assert lz4.block.decompress(compressed, dict=dict1) == input_data
  103. assert lz4.block.decompress(lz4.block.compress(
  104. input_data), dict=dict1) == input_data
  105. def test_known_decompress_1():
  106. input = b'\x00\x00\x00\x00\x00'
  107. output = b''
  108. assert lz4.block.decompress(input) == output
  109. def test_known_decompress_2():
  110. input = b'\x01\x00\x00\x00\x10 '
  111. output = b' '
  112. assert lz4.block.decompress(input) == output
  113. def test_known_decompress_3():
  114. input = b'h\x00\x00\x00\xff\x0bLorem ipsum dolor sit amet\x1a\x006P amet'
  115. output = b'Lorem ipsum dolor sit amet' * 4
  116. assert lz4.block.decompress(input) == output
  117. def test_known_decompress_4():
  118. input = b'\xb0\xb3\x00\x00\xff\x1fExcepteur sint occaecat cupidatat non proident.\x00' + (b'\xff' * 180) + b'\x1ePident'
  119. output = b'Excepteur sint occaecat cupidatat non proident' * 1000
  120. assert lz4.block.decompress(input) == output