#!/usr/bin/env python3 """Offline tests for _elf64_reader and the 6 binary/workload extractor scripts.""" import contextlib import importlib.util import io import json import os from pathlib import Path import struct import tempfile import unittest from unittest import mock ROOT = Path(__file__).resolve().parents[2] def _load(name: str, rel: str): spec = importlib.util.spec_from_file_location(name, ROOT / rel) mod = importlib.util.module_from_spec(spec) assert spec.loader is not None spec.loader.exec_module(mod) return mod class BinaryExtractorsTest(unittest.TestCase): def test_elf64_reader_discovery_and_sha256_check_under_O(self): reader = _load("_elf64_reader", "tools/_elf64_reader.py") with tempfile.TemporaryDirectory() as tmp: fake = Path(tmp) / "nv-kernel.o_binary" fake.write_bytes(b"not-a-real-rm-object") with mock.patch.dict(os.environ, {"NV_RS_RM_OBJECT": str(fake)}, clear=True): self.assertEqual(reader.find_rm_object(), fake) with self.assertRaisesRegex(SystemExit, "sha256 mismatch"): reader.verify_rm_object_sha256(fake) with self.assertRaisesRegex(SystemExit, "not a valid 64-bit"): reader.Elf64Relocatable(b"bad") def test_all_binary_extractors_are_import_safe(self): for rel in ( "tools/extract_gv100_gr_netlist.py", "tools/extract_mmu_callback_table.py", "tools/gpu_extract_acr_packed.py", "tools/gpu_extract_sec2_packed.py", "tools/gpu_extract_lsf_signatures.py", "tools/extract_rmapi_memory_workload.py", ): mod = _load(Path(rel).stem, rel) self.assertTrue(callable(mod.main)) def test_rmapi_memory_workload_cli_and_check_under_O(self): mod = _load("extract_rmapi_memory_workload", "tools/extract_rmapi_memory_workload.py") with contextlib.redirect_stderr(io.StringIO()): with self.assertRaises(SystemExit) as ctx: mod.main(["--bogus-flag"]) self.assertEqual(ctx.exception.code, 2) with tempfile.TemporaryDirectory() as tmp: tmp_path = Path(tmp) # Empty capture fails with clean SystemExit instead of IndexError. empty_cap = tmp_path / "empty.jsonl" empty_cap.write_text("", encoding="utf-8") with self.assertRaisesRegex(SystemExit, "no RM device allocation"): mod.main(["--input", str(empty_cap), "--check"]) # Valid minimal capture: alloc device (0x80) + subdevice (0x2080). dev_after = struct.pack("<4I", 0x1111, 0x1111, 0x2222, 0x80).hex() sub_after = struct.pack("<4I", 0x1111, 0x2222, 0x3333, 0x2080).hex() cap = tmp_path / "cap.jsonl" cap.write_text( "\n".join([ json.dumps({"device": "/dev/nvidiactl", "request": "0xc030462b", "before": dev_after, "after": dev_after, "params_before": "", "params_after": ""}), json.dumps({"device": "/dev/nvidiactl", "request": "0xc030462b", "before": sub_after, "after": sub_after, "params_before": "", "params_after": ""}), ]) + "\n", encoding="utf-8", ) out_target = tmp_path / "workload.json" out_target.write_text('{"stale": true}\n', encoding="utf-8") with mock.patch.object(mod, "TARGET", out_target): with self.assertRaisesRegex(SystemExit, "differs from the checked copy"): mod.main(["--input", str(cap), "--check"]) with contextlib.redirect_stdout(io.StringIO()): mod.main(["--input", str(cap)]) with contextlib.redirect_stdout(io.StringIO()): mod.main(["--input", str(cap), "--check"]) if __name__ == "__main__": unittest.main()