xref: /aosp_15_r20/external/brotli/python/tests/decompress_test.py (revision f4ee7fba7774faf2a30f13154332c0a06550dbc4)
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 unittest
7*f4ee7fbaSAndroid Build Coastguard Worker
8*f4ee7fbaSAndroid Build Coastguard Workerfrom . import _test_utils
9*f4ee7fbaSAndroid Build Coastguard Workerimport brotli
10*f4ee7fbaSAndroid Build Coastguard Worker
11*f4ee7fbaSAndroid Build Coastguard Worker
12*f4ee7fbaSAndroid Build Coastguard Workerdef _get_original_name(test_data):
13*f4ee7fbaSAndroid Build Coastguard Worker    return test_data.split('.compressed')[0]
14*f4ee7fbaSAndroid Build Coastguard Worker
15*f4ee7fbaSAndroid Build Coastguard Worker
16*f4ee7fbaSAndroid Build Coastguard Workerclass TestDecompress(_test_utils.TestCase):
17*f4ee7fbaSAndroid Build Coastguard Worker
18*f4ee7fbaSAndroid Build Coastguard Worker    def _check_decompression(self, test_data):
19*f4ee7fbaSAndroid Build Coastguard Worker        # Verify decompression matches the original.
20*f4ee7fbaSAndroid Build Coastguard Worker        temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data)
21*f4ee7fbaSAndroid Build Coastguard Worker        original = _get_original_name(test_data)
22*f4ee7fbaSAndroid Build Coastguard Worker        self.assertFilesMatch(temp_uncompressed, original)
23*f4ee7fbaSAndroid Build Coastguard Worker
24*f4ee7fbaSAndroid Build Coastguard Worker    def _decompress(self, test_data):
25*f4ee7fbaSAndroid Build Coastguard Worker        temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data)
26*f4ee7fbaSAndroid Build Coastguard Worker        with open(temp_uncompressed, 'wb') as out_file:
27*f4ee7fbaSAndroid Build Coastguard Worker            with open(test_data, 'rb') as in_file:
28*f4ee7fbaSAndroid Build Coastguard Worker                out_file.write(brotli.decompress(in_file.read()))
29*f4ee7fbaSAndroid Build Coastguard Worker
30*f4ee7fbaSAndroid Build Coastguard Worker    def _test_decompress(self, test_data):
31*f4ee7fbaSAndroid Build Coastguard Worker        self._decompress(test_data)
32*f4ee7fbaSAndroid Build Coastguard Worker        self._check_decompression(test_data)
33*f4ee7fbaSAndroid Build Coastguard Worker
34*f4ee7fbaSAndroid Build Coastguard Worker    def test_garbage_appended(self):
35*f4ee7fbaSAndroid Build Coastguard Worker        with self.assertRaises(brotli.error):
36*f4ee7fbaSAndroid Build Coastguard Worker            brotli.decompress(brotli.compress(b'a') + b'a')
37*f4ee7fbaSAndroid Build Coastguard Worker
38*f4ee7fbaSAndroid Build Coastguard Worker
39*f4ee7fbaSAndroid Build Coastguard Worker_test_utils.generate_test_methods(TestDecompress, for_decompression=True)
40*f4ee7fbaSAndroid Build Coastguard Worker
41*f4ee7fbaSAndroid Build Coastguard Workerif __name__ == '__main__':
42*f4ee7fbaSAndroid Build Coastguard Worker    unittest.main()
43