test_frame_3.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import lz4.frame as lz4frame
  2. import pytest
  3. import os
  4. import struct
  5. test_data = [
  6. (os.urandom(256 * 1024)),
  7. ]
  8. @pytest.fixture(
  9. params=test_data,
  10. ids=[
  11. 'data' + str(i) for i in range(len(test_data))
  12. ]
  13. )
  14. def data(request):
  15. return request.param
  16. def test_decompress_truncated(data):
  17. compressed = lz4frame.compress(data)
  18. message = r'^LZ4F_getFrameInfo failed with code: ERROR_frameHeader_incomplete'
  19. with pytest.raises(RuntimeError, match=message):
  20. lz4frame.decompress(compressed[:6])
  21. for i in range(16, len(compressed) - 1, 5): # 15 is the max size of the header
  22. message = r'^Frame incomplete. LZ4F_decompress returned:'
  23. try:
  24. lz4frame.decompress(compressed[:i])
  25. except RuntimeError as r:
  26. print(r)
  27. with pytest.raises(RuntimeError, match=message):
  28. lz4frame.decompress(compressed[:i])
  29. def test_content_checksum_failure(data):
  30. compressed = lz4frame.compress(data, content_checksum=True)
  31. message = r'^LZ4F_decompress failed with code: ERROR_contentChecksum_invalid$'
  32. with pytest.raises(RuntimeError, match=message):
  33. last = struct.unpack('B', compressed[-1:])[0]
  34. lz4frame.decompress(compressed[:-1] + struct.pack('B', last ^ 0x42))
  35. def test_block_checksum_failure(data):
  36. compressed = lz4frame.compress(
  37. data,
  38. content_checksum=True,
  39. block_checksum=True,
  40. return_bytearray=True,
  41. )
  42. message = r'^LZ4F_decompress failed with code: ERROR_blockChecksum_invalid$'
  43. if len(compressed) > 32:
  44. with pytest.raises(RuntimeError, match=message):
  45. compressed[22] = compressed[18] ^ 0x42
  46. lz4frame.decompress(compressed)