Debian Codegraph @

python/tests/bro_test.py

raw symbols SCIP
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
11 BRO_ARGS = _test_utils.BRO_ARGS
12 TEST_ENV = _test_utils.TEST_ENV
13
14
15 def _get_original_name(test_data):
16 return test_data.split('.compressed')[0]
17
18
19 class 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.assert_files_match(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
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):
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.assert_files_match(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
90 def _test_compress_file(self, test_data, **kwargs):
91 self._compress_file(test_data, **kwargs)
92 self._check_decompression(test_data)
93
94 def _test_compress_pipe(self, test_data, **kwargs):
95 self._compress_pipe(test_data, **kwargs)
96 self._check_decompression(test_data)
97
98
99 _test_utils.generate_test_methods(
100 TestBroCompress, variants=TestBroCompress.VARIANTS
101 )
102
103 if __name__ == '__main__':
104 unittest.main()