diff options
Diffstat (limited to 'roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests')
7 files changed, 450 insertions, 0 deletions
diff --git a/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/__init__.py b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/__init__.py diff --git a/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/_test_utils.py b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/_test_utils.py new file mode 100644 index 000000000..104e6548e --- /dev/null +++ b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/_test_utils.py @@ -0,0 +1,112 @@ +from __future__ import print_function +import filecmp +import glob +import itertools +import os +import sys +import sysconfig +import tempfile +import unittest + + +project_dir = os.path.abspath(os.path.join(__file__, '..', '..', '..')) +src_dir = os.path.join(project_dir, 'python') +test_dir = os.path.join(project_dir, 'tests') + +python_exe = sys.executable or 'python' +bro_path = os.path.join(src_dir, 'bro.py') +BRO_ARGS = [python_exe, bro_path] + +# Get the platform/version-specific build folder. +# By default, the distutils build base is in the same location as setup.py. +platform_lib_name = 'lib.{platform}-{version[0]}.{version[1]}'.format( + platform=sysconfig.get_platform(), version=sys.version_info) +build_dir = os.path.join(project_dir, 'bin', platform_lib_name) + +# Prepend the build folder to sys.path and the PYTHONPATH environment variable. +if build_dir not in sys.path: + sys.path.insert(0, build_dir) +TEST_ENV = os.environ.copy() +if 'PYTHONPATH' not in TEST_ENV: + TEST_ENV['PYTHONPATH'] = build_dir +else: + TEST_ENV['PYTHONPATH'] = build_dir + os.pathsep + TEST_ENV['PYTHONPATH'] + +TESTDATA_DIR = os.path.join(test_dir, 'testdata') + +TESTDATA_FILES = [ + 'empty', # Empty file + '10x10y', # Small text + 'alice29.txt', # Large text + 'random_org_10k.bin', # Small data + 'mapsdatazrh', # Large data +] + +TESTDATA_PATHS = [os.path.join(TESTDATA_DIR, f) for f in TESTDATA_FILES] + +TESTDATA_PATHS_FOR_DECOMPRESSION = glob.glob( + os.path.join(TESTDATA_DIR, '*.compressed')) + +TEMP_DIR = tempfile.mkdtemp() + + +def get_temp_compressed_name(filename): + return os.path.join(TEMP_DIR, os.path.basename(filename + '.bro')) + + +def get_temp_uncompressed_name(filename): + return os.path.join(TEMP_DIR, os.path.basename(filename + '.unbro')) + + +def bind_method_args(method, *args, **kwargs): + return lambda self: method(self, *args, **kwargs) + + +def generate_test_methods(test_case_class, + for_decompression=False, + variants=None): + # Add test methods for each test data file. This makes identifying problems + # with specific compression scenarios easier. + if for_decompression: + paths = TESTDATA_PATHS_FOR_DECOMPRESSION + else: + paths = TESTDATA_PATHS + opts = [] + if variants: + opts_list = [] + for k, v in variants.items(): + opts_list.append([r for r in itertools.product([k], v)]) + for o in itertools.product(*opts_list): + opts_name = '_'.join([str(i) for i in itertools.chain(*o)]) + opts_dict = dict(o) + opts.append([opts_name, opts_dict]) + else: + opts.append(['', {}]) + for method in [m for m in dir(test_case_class) if m.startswith('_test')]: + for testdata in paths: + for (opts_name, opts_dict) in opts: + f = os.path.splitext(os.path.basename(testdata))[0] + name = 'test_{method}_{options}_{file}'.format( + method=method, options=opts_name, file=f) + func = bind_method_args( + getattr(test_case_class, method), testdata, **opts_dict) + setattr(test_case_class, name, func) + + +class TestCase(unittest.TestCase): + + def tearDown(self): + for f in TESTDATA_PATHS: + try: + os.unlink(get_temp_compressed_name(f)) + except OSError: + pass + try: + os.unlink(get_temp_uncompressed_name(f)) + except OSError: + pass + + def assertFilesMatch(self, first, second): + self.assertTrue( + filecmp.cmp(first, second, shallow=False), + 'File {} differs from {}'.format(first, second)) diff --git a/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/bro_test.py b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/bro_test.py new file mode 100644 index 000000000..b55129def --- /dev/null +++ b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/bro_test.py @@ -0,0 +1,102 @@ +# Copyright 2016 The Brotli Authors. All rights reserved. +# +# Distributed under MIT license. +# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT + +import subprocess +import unittest + +from . import _test_utils +import brotli + +BRO_ARGS = _test_utils.BRO_ARGS +TEST_ENV = _test_utils.TEST_ENV + + +def _get_original_name(test_data): + return test_data.split('.compressed')[0] + + +class TestBroDecompress(_test_utils.TestCase): + + def _check_decompression(self, test_data): + # Verify decompression matches the original. + temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) + original = _get_original_name(test_data) + self.assertFilesMatch(temp_uncompressed, original) + + def _decompress_file(self, test_data): + temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) + args = BRO_ARGS + ['-f', '-d', '-i', test_data, '-o', temp_uncompressed] + subprocess.check_call(args, env=TEST_ENV) + + def _decompress_pipe(self, test_data): + temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) + args = BRO_ARGS + ['-d'] + with open(temp_uncompressed, 'wb') as out_file: + with open(test_data, 'rb') as in_file: + subprocess.check_call( + args, stdin=in_file, stdout=out_file, env=TEST_ENV) + + def _test_decompress_file(self, test_data): + self._decompress_file(test_data) + self._check_decompression(test_data) + + def _test_decompress_pipe(self, test_data): + self._decompress_pipe(test_data) + self._check_decompression(test_data) + + +_test_utils.generate_test_methods(TestBroDecompress, for_decompression=True) + + +class TestBroCompress(_test_utils.TestCase): + + VARIANTS = {'quality': (1, 6, 9, 11), 'lgwin': (10, 15, 20, 24)} + + def _check_decompression(self, test_data, **kwargs): + # Write decompression to temp file and verify it matches the original. + temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) + temp_compressed = _test_utils.get_temp_compressed_name(test_data) + original = test_data + args = BRO_ARGS + ['-f', '-d'] + args.extend(['-i', temp_compressed, '-o', temp_uncompressed]) + subprocess.check_call(args, env=TEST_ENV) + self.assertFilesMatch(temp_uncompressed, original) + + def _compress_file(self, test_data, **kwargs): + temp_compressed = _test_utils.get_temp_compressed_name(test_data) + args = BRO_ARGS + ['-f'] + if 'quality' in kwargs: + args.extend(['-q', str(kwargs['quality'])]) + if 'lgwin' in kwargs: + args.extend(['--lgwin', str(kwargs['lgwin'])]) + args.extend(['-i', test_data, '-o', temp_compressed]) + subprocess.check_call(args, env=TEST_ENV) + + def _compress_pipe(self, test_data, **kwargs): + temp_compressed = _test_utils.get_temp_compressed_name(test_data) + args = BRO_ARGS + if 'quality' in kwargs: + args.extend(['-q', str(kwargs['quality'])]) + if 'lgwin' in kwargs: + args.extend(['--lgwin', str(kwargs['lgwin'])]) + with open(temp_compressed, 'wb') as out_file: + with open(test_data, 'rb') as in_file: + subprocess.check_call( + args, stdin=in_file, stdout=out_file, env=TEST_ENV) + + def _test_compress_file(self, test_data, **kwargs): + self._compress_file(test_data, **kwargs) + self._check_decompression(test_data) + + def _test_compress_pipe(self, test_data, **kwargs): + self._compress_pipe(test_data, **kwargs) + self._check_decompression(test_data) + + +_test_utils.generate_test_methods( + TestBroCompress, variants=TestBroCompress.VARIANTS) + +if __name__ == '__main__': + unittest.main() diff --git a/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/compress_test.py b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/compress_test.py new file mode 100644 index 000000000..46ff68f50 --- /dev/null +++ b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/compress_test.py @@ -0,0 +1,41 @@ +# Copyright 2016 The Brotli Authors. All rights reserved. +# +# Distributed under MIT license. +# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT + +import unittest + +from . import _test_utils +import brotli + + +class TestCompress(_test_utils.TestCase): + + VARIANTS = {'quality': (1, 6, 9, 11), 'lgwin': (10, 15, 20, 24)} + + def _check_decompression(self, test_data, **kwargs): + kwargs = {} + # Write decompression to temp file and verify it matches the original. + temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) + temp_compressed = _test_utils.get_temp_compressed_name(test_data) + original = test_data + with open(temp_uncompressed, 'wb') as out_file: + with open(temp_compressed, 'rb') as in_file: + out_file.write(brotli.decompress(in_file.read(), **kwargs)) + self.assertFilesMatch(temp_uncompressed, original) + + def _compress(self, test_data, **kwargs): + temp_compressed = _test_utils.get_temp_compressed_name(test_data) + with open(temp_compressed, 'wb') as out_file: + with open(test_data, 'rb') as in_file: + out_file.write(brotli.compress(in_file.read(), **kwargs)) + + def _test_compress(self, test_data, **kwargs): + self._compress(test_data, **kwargs) + self._check_decompression(test_data, **kwargs) + + +_test_utils.generate_test_methods(TestCompress, variants=TestCompress.VARIANTS) + +if __name__ == '__main__': + unittest.main() diff --git a/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/compressor_test.py b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/compressor_test.py new file mode 100644 index 000000000..2d4791996 --- /dev/null +++ b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/compressor_test.py @@ -0,0 +1,94 @@ +# Copyright 2016 The Brotli Authors. All rights reserved. +# +# Distributed under MIT license. +# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT + +import functools +import unittest + +from . import _test_utils +import brotli + + +# Do not inherit from TestCase here to ensure that test methods +# are not run automatically and instead are run as part of a specific +# configuration below. +class _TestCompressor(object): + + CHUNK_SIZE = 2048 + + def tearDown(self): + self.compressor = None + + def _check_decompression(self, test_data): + # Write decompression to temp file and verify it matches the original. + temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) + temp_compressed = _test_utils.get_temp_compressed_name(test_data) + original = test_data + with open(temp_uncompressed, 'wb') as out_file: + with open(temp_compressed, 'rb') as in_file: + out_file.write(brotli.decompress(in_file.read())) + self.assertFilesMatch(temp_uncompressed, original) + + def _test_single_process(self, test_data): + # Write single-shot compression to temp file. + temp_compressed = _test_utils.get_temp_compressed_name(test_data) + with open(temp_compressed, 'wb') as out_file: + with open(test_data, 'rb') as in_file: + out_file.write(self.compressor.process(in_file.read())) + out_file.write(self.compressor.finish()) + self._check_decompression(test_data) + + def _test_multiple_process(self, test_data): + # Write chunked compression to temp file. + temp_compressed = _test_utils.get_temp_compressed_name(test_data) + with open(temp_compressed, 'wb') as out_file: + with open(test_data, 'rb') as in_file: + read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE) + for data in iter(read_chunk, b''): + out_file.write(self.compressor.process(data)) + out_file.write(self.compressor.finish()) + self._check_decompression(test_data) + + def _test_multiple_process_and_flush(self, test_data): + # Write chunked and flushed compression to temp file. + temp_compressed = _test_utils.get_temp_compressed_name(test_data) + with open(temp_compressed, 'wb') as out_file: + with open(test_data, 'rb') as in_file: + read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE) + for data in iter(read_chunk, b''): + out_file.write(self.compressor.process(data)) + out_file.write(self.compressor.flush()) + out_file.write(self.compressor.finish()) + self._check_decompression(test_data) + + +_test_utils.generate_test_methods(_TestCompressor) + + +class TestCompressorQuality1(_TestCompressor, _test_utils.TestCase): + + def setUp(self): + self.compressor = brotli.Compressor(quality=1) + + +class TestCompressorQuality6(_TestCompressor, _test_utils.TestCase): + + def setUp(self): + self.compressor = brotli.Compressor(quality=6) + + +class TestCompressorQuality9(_TestCompressor, _test_utils.TestCase): + + def setUp(self): + self.compressor = brotli.Compressor(quality=9) + + +class TestCompressorQuality11(_TestCompressor, _test_utils.TestCase): + + def setUp(self): + self.compressor = brotli.Compressor(quality=11) + + +if __name__ == '__main__': + unittest.main() diff --git a/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/decompress_test.py b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/decompress_test.py new file mode 100644 index 000000000..814e56332 --- /dev/null +++ b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/decompress_test.py @@ -0,0 +1,42 @@ +# Copyright 2016 The Brotli Authors. All rights reserved. +# +# Distributed under MIT license. +# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT + +import unittest + +from . import _test_utils +import brotli + + +def _get_original_name(test_data): + return test_data.split('.compressed')[0] + + +class TestDecompress(_test_utils.TestCase): + + def _check_decompression(self, test_data): + # Verify decompression matches the original. + temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) + original = _get_original_name(test_data) + self.assertFilesMatch(temp_uncompressed, original) + + def _decompress(self, test_data): + temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) + with open(temp_uncompressed, 'wb') as out_file: + with open(test_data, 'rb') as in_file: + out_file.write(brotli.decompress(in_file.read())) + + def _test_decompress(self, test_data): + self._decompress(test_data) + self._check_decompression(test_data) + + def test_garbage_appended(self): + with self.assertRaises(brotli.error): + brotli.decompress(brotli.compress(b'a') + b'a') + + +_test_utils.generate_test_methods(TestDecompress, for_decompression=True) + +if __name__ == '__main__': + unittest.main() diff --git a/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/decompressor_test.py b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/decompressor_test.py new file mode 100644 index 000000000..05918ada8 --- /dev/null +++ b/roms/edk2/MdeModulePkg/Library/BrotliCustomDecompressLib/brotli/python/tests/decompressor_test.py @@ -0,0 +1,59 @@ +# Copyright 2016 The Brotli Authors. All rights reserved. +# +# Distributed under MIT license. +# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT + +import functools +import unittest + +from . import _test_utils +import brotli + + +def _get_original_name(test_data): + return test_data.split('.compressed')[0] + + +class TestDecompressor(_test_utils.TestCase): + + CHUNK_SIZE = 1 + + def setUp(self): + self.decompressor = brotli.Decompressor() + + def tearDown(self): + self.decompressor = None + + def _check_decompression(self, test_data): + # Verify decompression matches the original. + temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) + original = _get_original_name(test_data) + self.assertFilesMatch(temp_uncompressed, original) + + def _decompress(self, test_data): + temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) + with open(temp_uncompressed, 'wb') as out_file: + with open(test_data, 'rb') as in_file: + read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE) + for data in iter(read_chunk, b''): + out_file.write(self.decompressor.process(data)) + self.assertTrue(self.decompressor.is_finished()) + + def _test_decompress(self, test_data): + self._decompress(test_data) + self._check_decompression(test_data) + + def test_garbage_appended(self): + with self.assertRaises(brotli.error): + self.decompressor.process(brotli.compress(b'a') + b'a') + + def test_already_finished(self): + self.decompressor.process(brotli.compress(b'a')) + with self.assertRaises(brotli.error): + self.decompressor.process(b'a') + + +_test_utils.generate_test_methods(TestDecompressor, for_decompression=True) + +if __name__ == '__main__': + unittest.main() |