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 6import subprocess 7import unittest 8 9from . import _test_utils 10 11BRO_ARGS = _test_utils.BRO_ARGS 12TEST_ENV = _test_utils.TEST_ENV 13 14 15def _get_original_name(test_data): 16 return test_data.split('.compressed')[0] 17 18 19class TestBroDecompress(_test_utils.TestCase): 20 21 def _check_decompression(self, test_data): 22 # Verify decompression matches the original. 23 temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) 24 original = _get_original_name(test_data) 25 self.assertFilesMatch(temp_uncompressed, original) 26 27 def _decompress_file(self, test_data): 28 temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) 29 args = BRO_ARGS + ['-f', '-d', '-i', test_data, '-o', temp_uncompressed] 30 subprocess.check_call(args, env=TEST_ENV) 31 32 def _decompress_pipe(self, test_data): 33 temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) 34 args = BRO_ARGS + ['-d'] 35 with open(temp_uncompressed, 'wb') as out_file: 36 with open(test_data, 'rb') as in_file: 37 subprocess.check_call( 38 args, stdin=in_file, stdout=out_file, env=TEST_ENV) 39 40 def _test_decompress_file(self, test_data): 41 self._decompress_file(test_data) 42 self._check_decompression(test_data) 43 44 def _test_decompress_pipe(self, test_data): 45 self._decompress_pipe(test_data) 46 self._check_decompression(test_data) 47 48 49_test_utils.generate_test_methods(TestBroDecompress, for_decompression=True) 50 51 52class TestBroCompress(_test_utils.TestCase): 53 54 VARIANTS = {'quality': (1, 6, 9, 11), 'lgwin': (10, 15, 20, 24)} 55 56 def _check_decompression(self, test_data, **kwargs): 57 # Write decompression to temp file and verify it matches the original. 58 temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) 59 temp_compressed = _test_utils.get_temp_compressed_name(test_data) 60 original = test_data 61 args = BRO_ARGS + ['-f', '-d'] 62 args.extend(['-i', temp_compressed, '-o', temp_uncompressed]) 63 subprocess.check_call(args, env=TEST_ENV) 64 self.assertFilesMatch(temp_uncompressed, original) 65 66 def _compress_file(self, test_data, **kwargs): 67 temp_compressed = _test_utils.get_temp_compressed_name(test_data) 68 args = BRO_ARGS + ['-f'] 69 if 'quality' in kwargs: 70 args.extend(['-q', str(kwargs['quality'])]) 71 if 'lgwin' in kwargs: 72 args.extend(['--lgwin', str(kwargs['lgwin'])]) 73 args.extend(['-i', test_data, '-o', temp_compressed]) 74 subprocess.check_call(args, env=TEST_ENV) 75 76 def _compress_pipe(self, test_data, **kwargs): 77 temp_compressed = _test_utils.get_temp_compressed_name(test_data) 78 args = BRO_ARGS 79 if 'quality' in kwargs: 80 args.extend(['-q', str(kwargs['quality'])]) 81 if 'lgwin' in kwargs: 82 args.extend(['--lgwin', str(kwargs['lgwin'])]) 83 with open(temp_compressed, 'wb') as out_file: 84 with open(test_data, 'rb') as in_file: 85 subprocess.check_call( 86 args, stdin=in_file, stdout=out_file, env=TEST_ENV) 87 88 def _test_compress_file(self, test_data, **kwargs): 89 self._compress_file(test_data, **kwargs) 90 self._check_decompression(test_data) 91 92 def _test_compress_pipe(self, test_data, **kwargs): 93 self._compress_pipe(test_data, **kwargs) 94 self._check_decompression(test_data) 95 96 97_test_utils.generate_test_methods( 98 TestBroCompress, variants=TestBroCompress.VARIANTS) 99 100if __name__ == '__main__': 101 unittest.main() 102