Matches original script APIs against MethodSpec registrations in Rex-EMoolator and keeps review evidence separate from registration. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
175 lines
8.0 KiB
Python
175 lines
8.0 KiB
Python
from copy import deepcopy
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from ams.snapshot import Snapshot
|
|
from ams.tracker import build_tracker, load_reviews, scan_emulator
|
|
|
|
SHA = "a" * 64
|
|
|
|
|
|
def emulator(tmp_path):
|
|
directory = tmp_path / "core/src/pl/genschu/bloomooemulator/interpreter/variable"
|
|
directory.mkdir(parents=True)
|
|
(directory / "Variable.java").write_text('''interface Variable {
|
|
Map.entry("CLONE", MethodSpec.of((s,a,c) -> clone(s)));
|
|
}''')
|
|
(directory / "BoolVariable.java").write_text('''record BoolVariable() {
|
|
VariableType type() { return VariableType.BOOLEAN; }
|
|
// Map.entry("FAKE", MethodSpec.of((s,a,c) -> null));
|
|
String misleading = "Map.entry(\\"FAKE2\\", MethodSpec.of(";
|
|
Map.entry("SET", MethodSpec.of((s,a,c) -> {
|
|
String x = ") /*"; // )
|
|
return new Value(a.get(0));
|
|
}));
|
|
Map.entry("TODO", MethodSpec.of((s,a,c) -> {
|
|
throw new UnsupportedOperationException("todo");
|
|
}));
|
|
Map.entry("NOOP", MethodSpec.of((s,a,c) -> MethodResult.noReturn()));
|
|
}''')
|
|
result = scan_emulator(tmp_path)
|
|
result["commit"] = "f" * 40
|
|
return result
|
|
|
|
|
|
def native(sha=SHA):
|
|
return Snapshot({"binary": {"name": "test.dll", "sha256": sha},
|
|
"types": [{"script_name": "BOOL", "cpp_class": "CMC_Bool"},
|
|
{"script_name": "UNKNOWN", "cpp_class": None}],
|
|
"methods": [{"owner": "CMC_Bool", "name": "set", "id": 1},
|
|
{"owner": "CMC", "name": "CLONE", "id": 1},
|
|
{"owner": "CMC", "name": "SET", "id": 2}],
|
|
"method_inheritance": [{"runner": "CMC_Bool_Runner", "base_runner": "CMC_Runner"}],
|
|
"method_dispatch": [{"owner": "CMC_Bool", "id": 1, "impl_addr": "0x10001000"}]})
|
|
|
|
|
|
def row(result, typ="BOOLEAN", method="SET"):
|
|
return next(m for t in result["types"] if t["name"] == typ for m in t["methods"] if m["name"] == method)
|
|
|
|
|
|
def review(emu, **changes):
|
|
return {"type": "BOOLEAN", "method": "SET", "binary_sha256": SHA, "rva": "0x1000",
|
|
"emulator_commit": emu["commit"], "body_sha256": emu["types"]["BOOLEAN"]["methods"]["SET"]["source"]["body_sha256"],
|
|
"source_tree_sha256": emu["source_tree_sha256"], "reviewer": "test", "date": "2026-10-10",
|
|
"notes": "Checked integer input", "evidence": "evidence/example.json#method", "kind": "asm",
|
|
"verdict": "matches", **changes}
|
|
|
|
|
|
def test_java_adapter_ignores_comments_literals_and_balances(tmp_path):
|
|
emu = emulator(tmp_path)
|
|
methods = emu["types"]["BOOLEAN"]["methods"]
|
|
assert set(methods) == {"SET", "TODO", "NOOP"}
|
|
assert methods["TODO"]["status"] == "stub_candidate"
|
|
assert methods["NOOP"]["status"] == "registered"
|
|
assert methods["SET"]["source"]["line"] == 5
|
|
|
|
|
|
def test_inheritance_aliases_and_globals_are_not_implementation_proof(tmp_path):
|
|
result = build_tracker([native()], emulator(tmp_path))
|
|
assert row(result)["status"] == "registered"
|
|
assert row(result)["verification"] == "unverified"
|
|
assert row(result)["original"][0]["owner"] == "CMC_Bool"
|
|
assert len(row(result)["original"]) == 1 # child wins over SET on base
|
|
assert row(result, method="CLONE")["source"]["owner"] == "*"
|
|
assert row(result, method="CLONE")["original"][0]["inherited"]
|
|
assert next(t for t in result["types"] if t["name"] == "UNKNOWN")["methods"] == []
|
|
|
|
|
|
def test_unknown_hash_not_fabricated_and_method_ids_are_binary_local(tmp_path):
|
|
a, b = native(), native(None)
|
|
b.raw["method_dispatch"][0]["impl_addr"] = "0x10009999"
|
|
result = build_tracker([a,b], emulator(tmp_path))
|
|
assert {o["impl_addr"] for o in row(result)["original"]} == {"0x10001000", "0x10009999"}
|
|
assert any(b["sha256"] is None and b["id"].startswith("snapshot:") for b in result["binaries"])
|
|
|
|
|
|
def test_review_stales_on_source_or_commit_change_and_covers_only_one_binary(tmp_path):
|
|
emu = emulator(tmp_path)
|
|
r = review(emu)
|
|
assert row(build_tracker([native()], emu, [r]))["verification"] == "asm_reviewed"
|
|
changed = deepcopy(emu)
|
|
changed["source_tree_sha256"] = "b" * 64 # includes helper changes, not just lambda
|
|
assert row(build_tracker([native()], changed, [r]))["verification"] == "stale"
|
|
changed = deepcopy(emu)
|
|
changed["commit"] = "d" * 40
|
|
assert row(build_tracker([native()], changed, [r]))["verification"] == "stale"
|
|
multiple = build_tracker([native(),native("c"*64)], emu, [r])
|
|
assert row(multiple)["verification"] == "unverified"
|
|
assert row(multiple)["verified_variants"] == 1
|
|
|
|
|
|
def test_review_without_required_anchors_rejected(tmp_path):
|
|
path = tmp_path / "reviews.json"
|
|
path.write_text(json.dumps({"schema":"tracker-reviews/1", "reviews":[{"type":"BOOLEAN"}]}))
|
|
with pytest.raises(ValueError): load_reviews(path)
|
|
emu = emulator(tmp_path / "repo")
|
|
path.write_text(json.dumps({"schema":"tracker-reviews/1", "reviews":[review(emu),review(emu)]}))
|
|
with pytest.raises(ValueError, match="Duplicate"): load_reviews(path)
|
|
|
|
|
|
def test_kolorowanka_registration_is_not_behavior_verification(tmp_path):
|
|
snap = Snapshot.load(str(Path(__file__).parents[1] / "snapshots/kolorowanka.snapshot.json"))
|
|
result = build_tracker([snap], emulator(tmp_path))
|
|
t = next(t for t in result["types"] if t["name"] == "KOLOROWANKA")
|
|
assert len(t["methods"]) == 23
|
|
assert all(m["verification"] == "unverified" for m in t["methods"])
|
|
assert row(result,"KOLOROWANKA","GETENDCOLOR")["original"][0]["evidence_ids"]
|
|
|
|
|
|
def test_duplicate_native_name_and_id_preserved(tmp_path):
|
|
snap = native()
|
|
snap.raw["methods"].extend([
|
|
{"owner": "CMC_Bool", "name": "SET", "id": 3},
|
|
{"owner": "CMC_Bool", "name": "OTHER", "id": 3},
|
|
])
|
|
result = build_tracker([snap], emulator(tmp_path))
|
|
assert {o["method_id"] for o in row(result)["original"]} == {1, 3}
|
|
assert len(row(result)["notes"]) == 2
|
|
assert len(row(result, method="OTHER")["notes"]) == 1
|
|
|
|
|
|
def test_world_snapshot_joins_and_keeps_registration_ambiguity(tmp_path):
|
|
snap = Snapshot.load(str(Path(__file__).parents[1] / "snapshots/world.snapshot.json"))
|
|
result = build_tracker([snap], emulator(tmp_path))
|
|
world = next(t for t in result["types"] if t["name"] == "WORLD")
|
|
assert len(world["methods"]) == 66
|
|
position = row(result, "WORLD", "SETPOSITION")
|
|
assert {o["method_id"] for o in position["original"]} == {24, 43}
|
|
assert len(position["notes"]) == 2
|
|
assert result["binaries"][0]["limitations"]
|
|
|
|
|
|
def test_known_class_aliases_do_not_create_false_missing_types(tmp_path):
|
|
snap = Snapshot({"binary": {"sha256": SHA}, "types": [
|
|
{"script_name": "CANVAS_OBSERVER", "cpp_class": None},
|
|
{"script_name": "GROUP", "cpp_class": None}], "methods": [
|
|
{"owner": "CMC_CanvasObserver", "name": "REFRESH", "id": None},
|
|
{"owner": "CMC_Groupped", "name": "ADD", "id": None}]})
|
|
result = build_tracker([snap], emulator(tmp_path))
|
|
assert row(result, "CANVAS_OBSERVER", "REFRESH")["original"][0]["mapping"] == "inferred"
|
|
assert row(result, "GROUP", "ADD")["original"]
|
|
assert not {"CANVASOBSERVER", "GROUPPED"} & {t["name"] for t in result["types"]}
|
|
|
|
|
|
def test_api_configuration_and_download(tmp_path, monkeypatch):
|
|
from fastapi.testclient import TestClient
|
|
from ams.api.app import create_app
|
|
monkeypatch.delenv("AMS_EMULATOR_ROOT", raising=False)
|
|
monkeypatch.delenv("AMS_TRACKER_REVIEWS", raising=False)
|
|
monkeypatch.delenv("AMS_TRACKER_SNAPSHOTS", raising=False)
|
|
client = TestClient(create_app("sqlite:///" + str(tmp_path / "test.db")))
|
|
assert client.get("/tracker").status_code == 503
|
|
emulator(tmp_path / "repo")
|
|
monkeypatch.setenv("AMS_EMULATOR_ROOT", str(tmp_path / "repo"))
|
|
snaps = tmp_path / "snapshots"
|
|
snaps.mkdir()
|
|
(snaps / "test.snapshot.json").write_text(json.dumps(native().raw))
|
|
monkeypatch.setenv("AMS_TRACKER_SNAPSHOTS", str(snaps))
|
|
response = client.get("/tracker?download=1")
|
|
assert response.status_code == 200
|
|
assert "attachment" in response.headers["content-disposition"]
|
|
assert response.json()["schema"] == "tracker/1"
|
|
assert client.get("/ui/tracker.html").status_code == 200
|