"""Tests for verified, deterministic Galaxian program-ROM loading.""" from hashlib import sha1 from pathlib import Path from zipfile import ZIP_DEFLATED, ZipFile from zlib import crc32 import pytest from galemu.roms import ( GRAPHICS_ROM_SIZE, GRAPHICS_ROM_SPECS, PALETTE_PROM_SPEC, PROGRAM_ROM_SIZE, PROGRAM_ROM_SPECS, RomSpec, RomValidationError, _load_rom_region, load_graphics_rom, load_palette_prom, load_program_roms, ) ROM_DIRECTORY = Path(__file__).parents[2] / "rom" / "galaxian" @pytest.mark.rom def test_load_program_roms_matches_verified_parent_set() -> None: image = load_program_roms(ROM_DIRECTORY) assert len(image) == PROGRAM_ROM_SIZE for spec in PROGRAM_ROM_SPECS: expected = (ROM_DIRECTORY / spec.filename).read_bytes() assert image[spec.offset : spec.offset + spec.size] == expected def test_load_program_roms_rejects_missing_file(tmp_path: Path) -> None: with pytest.raises(RomValidationError, match=r"missing required program ROM: .*galmidw\.u"): load_program_roms(tmp_path) @pytest.mark.rom def test_load_program_roms_rejects_modified_dump(tmp_path: Path) -> None: for spec in PROGRAM_ROM_SPECS: contents = (ROM_DIRECTORY / spec.filename).read_bytes() if spec.filename == "galmidw.w": contents = bytes((contents[0] ^ 0x01, *contents[1:])) (tmp_path / spec.filename).write_bytes(contents) with pytest.raises(RomValidationError, match=r"galmidw\.w: expected CRC32"): load_program_roms(tmp_path) @pytest.mark.rom def test_load_graphics_and_palette_assets_match_the_verified_parent_set() -> None: graphics = load_graphics_rom(ROM_DIRECTORY) palette = load_palette_prom(ROM_DIRECTORY) assert len(graphics) == GRAPHICS_ROM_SIZE for spec in GRAPHICS_ROM_SPECS: assert ( graphics[spec.offset : spec.offset + spec.size] == (ROM_DIRECTORY / spec.filename).read_bytes() ) assert palette == (ROM_DIRECTORY / PALETTE_PROM_SPEC.filename).read_bytes() def test_asset_loaders_reject_missing_files(tmp_path: Path) -> None: with pytest.raises(RomValidationError, match=r"missing required graphics ROM: .*1h\.bin"): load_graphics_rom(tmp_path) with pytest.raises(RomValidationError, match=r"missing required palette PROM: .*6l\.bpr"): load_palette_prom(tmp_path) def _test_spec(filename: str, contents: bytes) -> RomSpec: return RomSpec(filename, 0, len(contents), crc32(contents), sha1(contents).hexdigest()) def test_zip_loader_reads_nested_mixed_case_member_without_extracting(tmp_path: Path) -> None: contents = b"verified-rom-bytes" spec = _test_spec("chip.bin", contents) archive_path = tmp_path / "galaxian.zip" with ZipFile(archive_path, "w", ZIP_DEFLATED) as archive: archive.writestr("roms/GALAXIAN/CHIP.BIN", contents) image = _load_rom_region(archive_path, (spec,), len(contents), "test ROM") assert image == contents assert list(tmp_path.iterdir()) == [archive_path] def test_zip_loader_uses_expected_identity_to_resolve_duplicate_basename(tmp_path: Path) -> None: contents = b"correct" spec = _test_spec("chip.bin", contents) archive_path = tmp_path / "galaxian.zip" with ZipFile(archive_path, "w") as archive: archive.writestr("wrong/chip.bin", b"incorrect") archive.writestr("right/CHIP.BIN", contents) assert _load_rom_region(archive_path, (spec,), len(contents), "test ROM") == contents def test_zip_loader_reports_missing_member_and_invalid_archive(tmp_path: Path) -> None: spec = _test_spec("chip.bin", b"correct") empty_archive = tmp_path / "empty.zip" with ZipFile(empty_archive, "w"): pass with pytest.raises(RomValidationError, match=r"missing required test ROM: chip\.bin"): _load_rom_region(empty_archive, (spec,), spec.size, "test ROM") invalid_archive = tmp_path / "invalid.zip" invalid_archive.write_bytes(b"not a zip") with pytest.raises(RomValidationError, match=r"invalid ROM ZIP archive"): _load_rom_region(invalid_archive, (spec,), spec.size, "test ROM") @pytest.mark.rom def test_mame_zip_matches_extracted_parent_set(tmp_path: Path) -> None: archive_path = tmp_path / "galaxian.zip" with ZipFile(archive_path, "w", ZIP_DEFLATED) as archive: for spec in (*PROGRAM_ROM_SPECS, *GRAPHICS_ROM_SPECS, PALETTE_PROM_SPEC): archive.write(ROM_DIRECTORY / spec.filename, f"galaxian/{spec.filename.upper()}") assert load_program_roms(archive_path) == load_program_roms(ROM_DIRECTORY) assert load_graphics_rom(archive_path) == load_graphics_rom(ROM_DIRECTORY) assert load_palette_prom(archive_path) == load_palette_prom(ROM_DIRECTORY)