bro_test.py revision 26fa459c
126fa459cSmrg# Copyright 2016 The Brotli Authors. All rights reserved.
226fa459cSmrg#
326fa459cSmrg# Distributed under MIT license.
426fa459cSmrg# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
526fa459cSmrg
626fa459cSmrgimport subprocess
726fa459cSmrgimport unittest
826fa459cSmrg
926fa459cSmrgfrom . import _test_utils
1026fa459cSmrgimport brotli
1126fa459cSmrg
1226fa459cSmrgBRO_ARGS = _test_utils.BRO_ARGS
1326fa459cSmrgTEST_ENV = _test_utils.TEST_ENV
1426fa459cSmrg
1526fa459cSmrg
1626fa459cSmrgdef _get_original_name(test_data):
1726fa459cSmrg    return test_data.split('.compressed')[0]
1826fa459cSmrg
1926fa459cSmrg
2026fa459cSmrgclass TestBroDecompress(_test_utils.TestCase):
2126fa459cSmrg
2226fa459cSmrg    def _check_decompression(self, test_data):
2326fa459cSmrg        # Verify decompression matches the original.
2426fa459cSmrg        temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data)
2526fa459cSmrg        original = _get_original_name(test_data)
2626fa459cSmrg        self.assertFilesMatch(temp_uncompressed, original)
2726fa459cSmrg
2826fa459cSmrg    def _decompress_file(self, test_data):
2926fa459cSmrg        temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data)
3026fa459cSmrg        args = BRO_ARGS + ['-f', '-d', '-i', test_data, '-o', temp_uncompressed]
3126fa459cSmrg        subprocess.check_call(args, env=TEST_ENV)
3226fa459cSmrg
3326fa459cSmrg    def _decompress_pipe(self, test_data):
3426fa459cSmrg        temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data)
3526fa459cSmrg        args = BRO_ARGS + ['-d']
3626fa459cSmrg        with open(temp_uncompressed, 'wb') as out_file:
3726fa459cSmrg            with open(test_data, 'rb') as in_file:
3826fa459cSmrg                subprocess.check_call(
3926fa459cSmrg                    args, stdin=in_file, stdout=out_file, env=TEST_ENV)
4026fa459cSmrg
4126fa459cSmrg    def _test_decompress_file(self, test_data):
4226fa459cSmrg        self._decompress_file(test_data)
4326fa459cSmrg        self._check_decompression(test_data)
4426fa459cSmrg
4526fa459cSmrg    def _test_decompress_pipe(self, test_data):
4626fa459cSmrg        self._decompress_pipe(test_data)
4726fa459cSmrg        self._check_decompression(test_data)
4826fa459cSmrg
4926fa459cSmrg
5026fa459cSmrg_test_utils.generate_test_methods(TestBroDecompress, for_decompression=True)
5126fa459cSmrg
5226fa459cSmrg
5326fa459cSmrgclass TestBroCompress(_test_utils.TestCase):
5426fa459cSmrg
5526fa459cSmrg    VARIANTS = {'quality': (1, 6, 9, 11), 'lgwin': (10, 15, 20, 24)}
5626fa459cSmrg
5726fa459cSmrg    def _check_decompression(self, test_data, **kwargs):
5826fa459cSmrg        # Write decompression to temp file and verify it matches the original.
5926fa459cSmrg        temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data)
6026fa459cSmrg        temp_compressed = _test_utils.get_temp_compressed_name(test_data)
6126fa459cSmrg        original = test_data
6226fa459cSmrg        args = BRO_ARGS + ['-f', '-d']
6326fa459cSmrg        args.extend(['-i', temp_compressed, '-o', temp_uncompressed])
6426fa459cSmrg        subprocess.check_call(args, env=TEST_ENV)
6526fa459cSmrg        self.assertFilesMatch(temp_uncompressed, original)
6626fa459cSmrg
6726fa459cSmrg    def _compress_file(self, test_data, **kwargs):
6826fa459cSmrg        temp_compressed = _test_utils.get_temp_compressed_name(test_data)
6926fa459cSmrg        args = BRO_ARGS + ['-f']
7026fa459cSmrg        if 'quality' in kwargs:
7126fa459cSmrg            args.extend(['-q', str(kwargs['quality'])])
7226fa459cSmrg        if 'lgwin' in kwargs:
7326fa459cSmrg            args.extend(['--lgwin', str(kwargs['lgwin'])])
7426fa459cSmrg        args.extend(['-i', test_data, '-o', temp_compressed])
7526fa459cSmrg        subprocess.check_call(args, env=TEST_ENV)
7626fa459cSmrg
7726fa459cSmrg    def _compress_pipe(self, test_data, **kwargs):
7826fa459cSmrg        temp_compressed = _test_utils.get_temp_compressed_name(test_data)
7926fa459cSmrg        args = BRO_ARGS
8026fa459cSmrg        if 'quality' in kwargs:
8126fa459cSmrg            args.extend(['-q', str(kwargs['quality'])])
8226fa459cSmrg        if 'lgwin' in kwargs:
8326fa459cSmrg            args.extend(['--lgwin', str(kwargs['lgwin'])])
8426fa459cSmrg        with open(temp_compressed, 'wb') as out_file:
8526fa459cSmrg            with open(test_data, 'rb') as in_file:
8626fa459cSmrg                subprocess.check_call(
8726fa459cSmrg                    args, stdin=in_file, stdout=out_file, env=TEST_ENV)
8826fa459cSmrg
8926fa459cSmrg    def _test_compress_file(self, test_data, **kwargs):
9026fa459cSmrg        self._compress_file(test_data, **kwargs)
9126fa459cSmrg        self._check_decompression(test_data)
9226fa459cSmrg
9326fa459cSmrg    def _test_compress_pipe(self, test_data, **kwargs):
9426fa459cSmrg        self._compress_pipe(test_data, **kwargs)
9526fa459cSmrg        self._check_decompression(test_data)
9626fa459cSmrg
9726fa459cSmrg
9826fa459cSmrg_test_utils.generate_test_methods(
9926fa459cSmrg    TestBroCompress, variants=TestBroCompress.VARIANTS)
10026fa459cSmrg
10126fa459cSmrgif __name__ == '__main__':
10226fa459cSmrg    unittest.main()
103