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