1 # Copyright 2016 The Brotli Authors. All rights reserved. 2 # 3 # Distributed under MIT license. 4 # See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 5 6 import functools 7 import unittest 8 9 from . import _test_utils 10 import brotli 11 12 13 def _get_original_name(test_data): 14 return test_data.split('.compressed')[0] 15 16 17 class TestDecompressor(_test_utils.TestCase): 18 19 CHUNK_SIZE = 1 20 21 def setUp(self): 22 self.decompressor = brotli.Decompressor() 23 24 def _check_decompression(self, test_data): 25 # Verify decompression matches the original. 26 temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) 27 original = _get_original_name(test_data) 28 self.assertFilesMatch(temp_uncompressed, original) 29 30 def _decompress(self, test_data): 31 temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) 32 with open(temp_uncompressed, 'wb') as out_file: 33 with open(test_data, 'rb') as in_file: 34 read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE) 35 for data in iter(read_chunk, b''): 36 out_file.write(self.decompressor.process(data)) 37 self.assertTrue(self.decompressor.is_finished()) 38 39 def _test_decompress(self, test_data): 40 self._decompress(test_data) 41 self._check_decompression(test_data) 42 43 44 _test_utils.generate_test_methods(TestDecompressor, for_decompression=True) 45 46 if __name__ == '__main__': 47 unittest.main() 48