| 1 | import lz4.frame as lz4frame |
| 2 | import pytest |
| 3 | import os |
| 4 | import copy |
| 5 | import sys |
| 6 | from . helpers import ( |
| 7 | get_chunked, |
| 8 | get_frame_info_check, |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | test_data = [ |
| 13 | (b'', 1, 1), |
| 14 | (os.urandom(8 * 1024), 8, 1), |
| 15 | (os.urandom(8 * 1024), 1, 8), |
| 16 | (b'0' * 8 * 1024, 8, 1), |
| 17 | (b'0' * 8 * 1024, 8, 1), |
| 18 | (bytearray(b''), 1, 1), |
| 19 | (bytearray(os.urandom(8 * 1024)), 8, 1), |
| 20 | ] |
| 21 | if sys.version_info > (2, 7): |
| 22 | test_data += [ |
| 23 | (memoryview(b''), 1, 1), |
| 24 | (memoryview(os.urandom(8 * 1024)), 8, 1) |
| 25 | ] |
| 26 | |
| 27 | |
| 28 | @pytest.fixture( |
| 29 | params=test_data, |
| 30 | ids=[ |
| 31 | 'data' + str(i) for i in range(len(test_data)) |
| 32 | ] |
| 33 | ) |
| 34 | def data(request): |
| 35 | return request.param |
| 36 | |
| 37 | |
| 38 | def test_roundtrip_chunked(data, block_size, block_linked, |
| 39 | content_checksum, block_checksum, |
| 40 | compression_level, |
| 41 | auto_flush, store_size): |
| 42 | |
| 43 | data, c_chunks, d_chunks = data |
| 44 | |
| 45 | if isinstance(data, memoryview): |
| 46 | data = memoryview(copy.deepcopy(data.obj)) |
| 47 | elif isinstance(data, bytearray): |
| 48 | data_2 = bytearray() |
| 49 | data_2[:] = data |
| 50 | data = data_2 |
| 51 | |
| 52 | c_context = lz4frame.create_compression_context() |
| 53 | |
| 54 | kwargs = {} |
| 55 | kwargs['compression_level'] = compression_level |
| 56 | kwargs['block_size'] = block_size |
| 57 | kwargs['block_linked'] = block_linked |
| 58 | kwargs['content_checksum'] = content_checksum |
| 59 | kwargs['block_checksum'] = block_checksum |
| 60 | kwargs['auto_flush'] = auto_flush |
| 61 | if store_size is True: |
| 62 | kwargs['source_size'] = len(data) |
| 63 | |
| 64 | compressed = lz4frame.compress_begin( |
| 65 | c_context, |
| 66 | **kwargs |
| 67 | ) |
| 68 | data_in = get_chunked(data, c_chunks) |
| 69 | try: |
| 70 | while True: |
| 71 | compressed += lz4frame.compress_chunk( |
| 72 | c_context, |
| 73 | next(data_in) |
| 74 | ) |
| 75 | except StopIteration: |
| 76 | pass |
| 77 | finally: |
| 78 | del data_in |
| 79 | |
| 80 | compressed += lz4frame.compress_flush(c_context) |
| 81 | |
| 82 | get_frame_info_check( |
| 83 | compressed, |
| 84 | len(data), |
| 85 | store_size, |
| 86 | block_size, |
| 87 | block_linked, |
| 88 | content_checksum, |
| 89 | block_checksum, |
| 90 | ) |
| 91 | |
| 92 | d_context = lz4frame.create_decompression_context() |
| 93 | compressed_in = get_chunked(compressed, d_chunks) |
| 94 | decompressed = b'' |
| 95 | bytes_read = 0 |
| 96 | eofs = [] |
| 97 | try: |
| 98 | while True: |
| 99 | d, b, e = lz4frame.decompress_chunk( |
| 100 | d_context, |
| 101 | next(compressed_in), |
| 102 | ) |
| 103 | decompressed += d |
| 104 | bytes_read += b |
| 105 | eofs.append(e) |
| 106 | |
| 107 | except StopIteration: |
| 108 | pass |
| 109 | finally: |
| 110 | del compressed_in |
| 111 | |
| 112 | assert bytes_read == len(compressed) |
| 113 | assert decompressed == data |
| 114 | assert eofs[-1] is True |
| 115 | assert (True in eofs[:-2]) is False |