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