1*f4ee7fbaSAndroid Build Coastguard Worker# Copyright 2016 The Brotli Authors. All rights reserved. 2*f4ee7fbaSAndroid Build Coastguard Worker# 3*f4ee7fbaSAndroid Build Coastguard Worker# Distributed under MIT license. 4*f4ee7fbaSAndroid Build Coastguard Worker# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 5*f4ee7fbaSAndroid Build Coastguard Worker 6*f4ee7fbaSAndroid Build Coastguard Workerimport subprocess 7*f4ee7fbaSAndroid Build Coastguard Workerimport unittest 8*f4ee7fbaSAndroid Build Coastguard Worker 9*f4ee7fbaSAndroid Build Coastguard Workerfrom . import _test_utils 10*f4ee7fbaSAndroid Build Coastguard Workerimport brotli 11*f4ee7fbaSAndroid Build Coastguard Worker 12*f4ee7fbaSAndroid Build Coastguard WorkerBRO_ARGS = _test_utils.BRO_ARGS 13*f4ee7fbaSAndroid Build Coastguard WorkerTEST_ENV = _test_utils.TEST_ENV 14*f4ee7fbaSAndroid Build Coastguard Worker 15*f4ee7fbaSAndroid Build Coastguard Worker 16*f4ee7fbaSAndroid Build Coastguard Workerdef _get_original_name(test_data): 17*f4ee7fbaSAndroid Build Coastguard Worker return test_data.split('.compressed')[0] 18*f4ee7fbaSAndroid Build Coastguard Worker 19*f4ee7fbaSAndroid Build Coastguard Worker 20*f4ee7fbaSAndroid Build Coastguard Workerclass TestBroDecompress(_test_utils.TestCase): 21*f4ee7fbaSAndroid Build Coastguard Worker 22*f4ee7fbaSAndroid Build Coastguard Worker def _check_decompression(self, test_data): 23*f4ee7fbaSAndroid Build Coastguard Worker # Verify decompression matches the original. 24*f4ee7fbaSAndroid Build Coastguard Worker temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) 25*f4ee7fbaSAndroid Build Coastguard Worker original = _get_original_name(test_data) 26*f4ee7fbaSAndroid Build Coastguard Worker self.assertFilesMatch(temp_uncompressed, original) 27*f4ee7fbaSAndroid Build Coastguard Worker 28*f4ee7fbaSAndroid Build Coastguard Worker def _decompress_file(self, test_data): 29*f4ee7fbaSAndroid Build Coastguard Worker temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) 30*f4ee7fbaSAndroid Build Coastguard Worker args = BRO_ARGS + ['-f', '-d', '-i', test_data, '-o', temp_uncompressed] 31*f4ee7fbaSAndroid Build Coastguard Worker subprocess.check_call(args, env=TEST_ENV) 32*f4ee7fbaSAndroid Build Coastguard Worker 33*f4ee7fbaSAndroid Build Coastguard Worker def _decompress_pipe(self, test_data): 34*f4ee7fbaSAndroid Build Coastguard Worker temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) 35*f4ee7fbaSAndroid Build Coastguard Worker args = BRO_ARGS + ['-d'] 36*f4ee7fbaSAndroid Build Coastguard Worker with open(temp_uncompressed, 'wb') as out_file: 37*f4ee7fbaSAndroid Build Coastguard Worker with open(test_data, 'rb') as in_file: 38*f4ee7fbaSAndroid Build Coastguard Worker subprocess.check_call( 39*f4ee7fbaSAndroid Build Coastguard Worker args, stdin=in_file, stdout=out_file, env=TEST_ENV) 40*f4ee7fbaSAndroid Build Coastguard Worker 41*f4ee7fbaSAndroid Build Coastguard Worker def _test_decompress_file(self, test_data): 42*f4ee7fbaSAndroid Build Coastguard Worker self._decompress_file(test_data) 43*f4ee7fbaSAndroid Build Coastguard Worker self._check_decompression(test_data) 44*f4ee7fbaSAndroid Build Coastguard Worker 45*f4ee7fbaSAndroid Build Coastguard Worker def _test_decompress_pipe(self, test_data): 46*f4ee7fbaSAndroid Build Coastguard Worker self._decompress_pipe(test_data) 47*f4ee7fbaSAndroid Build Coastguard Worker self._check_decompression(test_data) 48*f4ee7fbaSAndroid Build Coastguard Worker 49*f4ee7fbaSAndroid Build Coastguard Worker 50*f4ee7fbaSAndroid Build Coastguard Worker_test_utils.generate_test_methods(TestBroDecompress, for_decompression=True) 51*f4ee7fbaSAndroid Build Coastguard Worker 52*f4ee7fbaSAndroid Build Coastguard Worker 53*f4ee7fbaSAndroid Build Coastguard Workerclass TestBroCompress(_test_utils.TestCase): 54*f4ee7fbaSAndroid Build Coastguard Worker 55*f4ee7fbaSAndroid Build Coastguard Worker VARIANTS = {'quality': (1, 6, 9, 11), 'lgwin': (10, 15, 20, 24)} 56*f4ee7fbaSAndroid Build Coastguard Worker 57*f4ee7fbaSAndroid Build Coastguard Worker def _check_decompression(self, test_data, **kwargs): 58*f4ee7fbaSAndroid Build Coastguard Worker # Write decompression to temp file and verify it matches the original. 59*f4ee7fbaSAndroid Build Coastguard Worker temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) 60*f4ee7fbaSAndroid Build Coastguard Worker temp_compressed = _test_utils.get_temp_compressed_name(test_data) 61*f4ee7fbaSAndroid Build Coastguard Worker original = test_data 62*f4ee7fbaSAndroid Build Coastguard Worker args = BRO_ARGS + ['-f', '-d'] 63*f4ee7fbaSAndroid Build Coastguard Worker args.extend(['-i', temp_compressed, '-o', temp_uncompressed]) 64*f4ee7fbaSAndroid Build Coastguard Worker subprocess.check_call(args, env=TEST_ENV) 65*f4ee7fbaSAndroid Build Coastguard Worker self.assertFilesMatch(temp_uncompressed, original) 66*f4ee7fbaSAndroid Build Coastguard Worker 67*f4ee7fbaSAndroid Build Coastguard Worker def _compress_file(self, test_data, **kwargs): 68*f4ee7fbaSAndroid Build Coastguard Worker temp_compressed = _test_utils.get_temp_compressed_name(test_data) 69*f4ee7fbaSAndroid Build Coastguard Worker args = BRO_ARGS + ['-f'] 70*f4ee7fbaSAndroid Build Coastguard Worker if 'quality' in kwargs: 71*f4ee7fbaSAndroid Build Coastguard Worker args.extend(['-q', str(kwargs['quality'])]) 72*f4ee7fbaSAndroid Build Coastguard Worker if 'lgwin' in kwargs: 73*f4ee7fbaSAndroid Build Coastguard Worker args.extend(['--lgwin', str(kwargs['lgwin'])]) 74*f4ee7fbaSAndroid Build Coastguard Worker args.extend(['-i', test_data, '-o', temp_compressed]) 75*f4ee7fbaSAndroid Build Coastguard Worker subprocess.check_call(args, env=TEST_ENV) 76*f4ee7fbaSAndroid Build Coastguard Worker 77*f4ee7fbaSAndroid Build Coastguard Worker def _compress_pipe(self, test_data, **kwargs): 78*f4ee7fbaSAndroid Build Coastguard Worker temp_compressed = _test_utils.get_temp_compressed_name(test_data) 79*f4ee7fbaSAndroid Build Coastguard Worker args = BRO_ARGS 80*f4ee7fbaSAndroid Build Coastguard Worker if 'quality' in kwargs: 81*f4ee7fbaSAndroid Build Coastguard Worker args.extend(['-q', str(kwargs['quality'])]) 82*f4ee7fbaSAndroid Build Coastguard Worker if 'lgwin' in kwargs: 83*f4ee7fbaSAndroid Build Coastguard Worker args.extend(['--lgwin', str(kwargs['lgwin'])]) 84*f4ee7fbaSAndroid Build Coastguard Worker with open(temp_compressed, 'wb') as out_file: 85*f4ee7fbaSAndroid Build Coastguard Worker with open(test_data, 'rb') as in_file: 86*f4ee7fbaSAndroid Build Coastguard Worker subprocess.check_call( 87*f4ee7fbaSAndroid Build Coastguard Worker args, stdin=in_file, stdout=out_file, env=TEST_ENV) 88*f4ee7fbaSAndroid Build Coastguard Worker 89*f4ee7fbaSAndroid Build Coastguard Worker def _test_compress_file(self, test_data, **kwargs): 90*f4ee7fbaSAndroid Build Coastguard Worker self._compress_file(test_data, **kwargs) 91*f4ee7fbaSAndroid Build Coastguard Worker self._check_decompression(test_data) 92*f4ee7fbaSAndroid Build Coastguard Worker 93*f4ee7fbaSAndroid Build Coastguard Worker def _test_compress_pipe(self, test_data, **kwargs): 94*f4ee7fbaSAndroid Build Coastguard Worker self._compress_pipe(test_data, **kwargs) 95*f4ee7fbaSAndroid Build Coastguard Worker self._check_decompression(test_data) 96*f4ee7fbaSAndroid Build Coastguard Worker 97*f4ee7fbaSAndroid Build Coastguard Worker 98*f4ee7fbaSAndroid Build Coastguard Worker_test_utils.generate_test_methods( 99*f4ee7fbaSAndroid Build Coastguard Worker TestBroCompress, variants=TestBroCompress.VARIANTS) 100*f4ee7fbaSAndroid Build Coastguard Worker 101*f4ee7fbaSAndroid Build Coastguard Workerif __name__ == '__main__': 102*f4ee7fbaSAndroid Build Coastguard Worker unittest.main() 103