bro_test.py 3.7 KB

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