"""ELF emitter tests: structural validity + segment fidelity. Validates with readelf where available and always with a programmatic re-parse against the container's own region table. """ import os import struct import subprocess import sys import unittest from pathlib import Path REPO = Path(__file__).resolve().parent.parent sys.path.insert(0, str(REPO / 'tools')) import mtk_fw_elf as E # noqa: E402 FW = Path(os.environ.get('MTK_FW_DIR', '/lib/firmware/mediatek')) def blobs(): return sorted(FW.glob('mt79*_wm.bin')) + sorted(FW.glob('mt79*_wa.bin')) \ + sorted(FW.glob('mt79*_rom_patch.bin')) class Emitter(unittest.TestCase): def test_structure_and_fidelity(self): found = blobs() if not found: self.skipTest('no mediatek blobs') for p in found: with self.subTest(blob=p.name): d = p.read_bytes() if 'patch' in p.name: regions = list(E.patch_regions(d)) else: regions = list(E.ram_regions(d)) regions = [(a, dat) for a, dat in regions if dat] elf = E.build_elf(regions) # ehdr sanity self.assertEqual(elf[:4], b'\x7fELF') etype, mach, _, entry, phoff = struct.unpack_from('