| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import functools |
| 7 |
import os |
| 8 |
import unittest |
| 9 |
|
| 10 |
import brotli |
| 11 |
|
| 12 |
from . import _test_utils |
| 13 |
|
| 14 |
|
| 15 |
def _get_original_name(test_data): |
| 16 |
return test_data.split('.compressed')[0] |
| 17 |
|
| 18 |
|
| 19 |
class TestDecompressor(_test_utils.TestCase): |
| 20 |
|
| 21 |
CHUNK_SIZE = 1 |
| 22 |
MIN_OUTPUT_BUFFER_SIZE = 32768 |
| 23 |
|
| 24 |
def setUp(self): |
| 25 |
|
| 26 |
self.decompressor = brotli.Decompressor() |
| 27 |
|
| 28 |
def tearDown(self): |
| 29 |
self.decompressor = None |
| 30 |
|
| 31 |
|
| 32 |
def _check_decompression(self, test_data): |
| 33 |
|
| 34 |
temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) |
| 35 |
original = _get_original_name(test_data) |
| 36 |
self.assert_files_match(temp_uncompressed, original) |
| 37 |
|
| 38 |
def _decompress(self, test_data): |
| 39 |
temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) |
| 40 |
with open(temp_uncompressed, 'wb') as out_file: |
| 41 |
with open(test_data, 'rb') as in_file: |
| 42 |
read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE) |
| 43 |
for data in iter(read_chunk, b''): |
| 44 |
out_file.write(self.decompressor.process(data)) |
| 45 |
self.assertTrue(self.decompressor.is_finished()) |
| 46 |
|
| 47 |
def _decompress_with_limit(self, test_data): |
| 48 |
output_buffer_limit = 10922 |
| 49 |
temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) |
| 50 |
with open(temp_uncompressed, 'wb') as out_file: |
| 51 |
with open(test_data, 'rb') as in_file: |
| 52 |
chunk_iter = iter(functools.partial(in_file.read, 10 * 1024), b'') |
| 53 |
while not self.decompressor.is_finished(): |
| 54 |
data = b'' |
| 55 |
if self.decompressor.can_accept_more_data(): |
| 56 |
data = next(chunk_iter, b'') |
| 57 |
decompressed_data = self.decompressor.process( |
| 58 |
data, output_buffer_limit=output_buffer_limit |
| 59 |
) |
| 60 |
self.assertLessEqual( |
| 61 |
len(decompressed_data), self.MIN_OUTPUT_BUFFER_SIZE |
| 62 |
) |
| 63 |
out_file.write(decompressed_data) |
| 64 |
self.assertIsNone(next(chunk_iter, None)) |
| 65 |
|
| 66 |
def _test_decompress(self, test_data): |
| 67 |
self._decompress(test_data) |
| 68 |
self._check_decompression(test_data) |
| 69 |
|
| 70 |
def _test_decompress_with_limit(self, test_data): |
| 71 |
self._decompress_with_limit(test_data) |
| 72 |
self._check_decompression(test_data) |
| 73 |
|
| 74 |
def test_too_much_input(self): |
| 75 |
with open( |
| 76 |
os.path.join(_test_utils.TESTDATA_DIR, 'zerosukkanooa.compressed'), 'rb' |
| 77 |
) as in_file: |
| 78 |
compressed = in_file.read() |
| 79 |
self.decompressor.process(compressed[:-1], output_buffer_limit=10240) |
| 80 |
|
| 81 |
self.assertFalse(self.decompressor.can_accept_more_data()) |
| 82 |
with self.assertRaises(brotli.error): |
| 83 |
self.decompressor.process(compressed[-1:]) |
| 84 |
|
| 85 |
def test_changing_limit(self): |
| 86 |
test_data = os.path.join( |
| 87 |
_test_utils.TESTDATA_DIR, 'zerosukkanooa.compressed' |
| 88 |
) |
| 89 |
check_output = os.path.exists(test_data.replace('.compressed', '')) |
| 90 |
temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) |
| 91 |
with open(temp_uncompressed, 'wb') as out_file: |
| 92 |
with open(test_data, 'rb') as in_file: |
| 93 |
compressed = in_file.read() |
| 94 |
uncompressed = self.decompressor.process( |
| 95 |
compressed[:-1], output_buffer_limit=10240 |
| 96 |
) |
| 97 |
self.assertLessEqual(len(uncompressed), self.MIN_OUTPUT_BUFFER_SIZE) |
| 98 |
out_file.write(uncompressed) |
| 99 |
while not self.decompressor.can_accept_more_data(): |
| 100 |
out_file.write(self.decompressor.process(b'')) |
| 101 |
out_file.write(self.decompressor.process(compressed[-1:])) |
| 102 |
if check_output: |
| 103 |
self._check_decompression(test_data) |
| 104 |
|
| 105 |
def test_garbage_appended(self): |
| 106 |
with self.assertRaises(brotli.error): |
| 107 |
self.decompressor.process(brotli.compress(b'a') + b'a') |
| 108 |
|
| 109 |
def test_already_finished(self): |
| 110 |
self.decompressor.process(brotli.compress(b'a')) |
| 111 |
with self.assertRaises(brotli.error): |
| 112 |
self.decompressor.process(b'a') |
| 113 |
|
| 114 |
|
| 115 |
_test_utils.generate_test_methods(TestDecompressor, for_decompression=True) |
| 116 |
|
| 117 |
if __name__ == '__main__': |
| 118 |
unittest.main() |