"""Offline checks for bounded stress fixtures and format identification.""" import importlib.util from pathlib import Path import struct import tempfile import unittest import zlib class DecoderMemoryTests(unittest.TestCase): @classmethod def setUpClass(cls): path = Path(__file__).with_name('decoder-memory.py') cls.module = None if path.exists(): spec = importlib.util.spec_from_file_location('decoder_memory', path) cls.module = importlib.util.module_from_spec(spec) spec.loader.exec_module(cls.module) def setUp(self): self.assertIsNotNone(self.module, 'decoder-memory.py is not implemented') def test_streamed_rgba64_png_has_valid_crc_and_zero_pixels(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / 'wide.png' self.module.write_rgba64_png(path, width=19, height=3, chunk_bytes=7) payload = path.read_bytes() self.assertEqual(payload[:8], b'\x89PNG\r\n\x1a\n') pos, compressed = 8, bytearray() while pos < len(payload): size = struct.unpack_from('>I', payload, pos)[0] kind, body = payload[pos + 4:pos + 8], payload[pos + 8:pos + 8 + size] self.assertEqual(struct.unpack_from('>I', payload, pos + 8 + size)[0], zlib.crc32(kind + body) & 0xffffffff) if kind == b'IDAT': compressed.extend(body) pos += size + 12 self.assertEqual(zlib.decompress(compressed), bytes(3 * (19 * 8 + 1))) self.assertEqual(self.module.fixture_info(payload, 'image/png'), {'width': 19, 'height': 3, 'bit_depth': 16, 'color_type': 6}) def test_progressive_jpeg_444_components_are_identified(self): sof = b'\x08' + struct.pack('>HHB', 5000, 8000, 3) + bytes([1, 0x11, 0, 2, 0x11, 0, 3, 0x11, 0]) payload = b'\xff\xd8\xff\xc2' + struct.pack('>H', len(sof) + 2) + sof info = self.module.fixture_info(payload, 'image/jpeg') self.assertEqual((info['width'], info['height'], info['components']), (8000, 5000, 3)) self.assertTrue(info['progressive']) self.assertEqual(info['sampling'], [0x11, 0x11, 0x11]) def test_fixture_byte_cap_is_checked_before_reading(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / 'oversized.png' with path.open('wb') as out: out.truncate(10 * 1024 * 1024 + 1) with self.assertRaises(ValueError): self.module.read_fixture(path, 'image/png')