decompress_test.py 1.3 KB

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