196 lines
7.3 KiB
Python
196 lines
7.3 KiB
Python
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""Host-only policy mutation tests for Phase-1.0G."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import sys
|
|
from typing import Callable
|
|
|
|
|
|
def load_validator(path: Path):
|
|
spec = importlib.util.spec_from_file_location("phase10g_validator", path)
|
|
assert spec and spec.loader
|
|
module = importlib.util.module_from_spec(spec)
|
|
sys.modules[spec.name] = module
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
def require(condition: bool, message: str) -> None:
|
|
if not condition:
|
|
raise RuntimeError(message)
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--root", type=Path, required=True)
|
|
args = parser.parse_args()
|
|
root = args.root.resolve()
|
|
validator = load_validator(root / "tools/validate_retroarch_phase10g.py")
|
|
record = validator.load_json(root / "manifests/retroarch/phase-1.0g-one-shot-runner.json")
|
|
approval = validator.load_json(root / "manifests/retroarch/phase-1.0g-one-shot-approval-template.json")
|
|
phase10f = validator.load_json(root / "manifests/retroarch/phase-1.0f-startup-interval.json")
|
|
cases: list[tuple[str, Callable[[], None]]] = []
|
|
|
|
def case(name: str):
|
|
def register(function: Callable[[], None]) -> Callable[[], None]:
|
|
cases.append((name, function))
|
|
return function
|
|
return register
|
|
|
|
@case("01 all authorizations remain false")
|
|
def _() -> None:
|
|
require(validator.all_false(record["authorizations"], validator.AUTHORIZATION_FIELDS), "authorization active")
|
|
|
|
@case("02 connection authorization fails inactive policy")
|
|
def _() -> None:
|
|
value = dict(record["authorizations"])
|
|
value["ps5_connection_authorized"] = True
|
|
require(not validator.all_false(value, validator.AUTHORIZATION_FIELDS), "connection accepted")
|
|
|
|
@case("03 no device actions occurred")
|
|
def _() -> None:
|
|
require(validator.all_false(record["phase_actions"], validator.ACTION_FIELDS), "device action recorded")
|
|
|
|
@case("04 unchanged inactive artifact passes")
|
|
def _() -> None:
|
|
require(validator.artifact_is_inactive(record["artifact"], phase10f), "artifact rejected")
|
|
|
|
@case("05 artifact hash mismatch fails")
|
|
def _() -> None:
|
|
value = dict(record["artifact"])
|
|
value["sha256"] = "0" * 64
|
|
require(not validator.artifact_is_inactive(value, phase10f), "hash mismatch accepted")
|
|
|
|
@case("06 execution eligibility fails")
|
|
def _() -> None:
|
|
value = dict(record["artifact"])
|
|
value["execution_eligible"] = True
|
|
require(not validator.artifact_is_inactive(value, phase10f), "eligible artifact accepted")
|
|
|
|
@case("07 exact F protocol passes")
|
|
def _() -> None:
|
|
require(validator.protocol_is_exact(record["result_protocol"]), "protocol rejected")
|
|
|
|
@case("08 E protocol fails")
|
|
def _() -> None:
|
|
value = dict(record["result_protocol"])
|
|
value["magic"] = "CHD10E01"
|
|
require(not validator.protocol_is_exact(value), "old protocol accepted")
|
|
|
|
@case("09 inactive bounded runner passes")
|
|
def _() -> None:
|
|
require(validator.runner_is_inactive_and_bounded(record["runner"]), "runner rejected")
|
|
|
|
@case("10 protocol activation fails")
|
|
def _() -> None:
|
|
value = dict(record["runner"])
|
|
value["protocol_activation_authorized"] = True
|
|
require(not validator.runner_is_inactive_and_bounded(value), "activation accepted")
|
|
|
|
@case("11 tracked run id fails")
|
|
def _() -> None:
|
|
value = dict(record["runner"])
|
|
value["run_id"] = "run-1"
|
|
require(not validator.runner_is_inactive_and_bounded(value), "run id accepted")
|
|
|
|
@case("12 tracked target fails")
|
|
def _() -> None:
|
|
value = dict(record["runner"])
|
|
value["tracked_target"] = "device"
|
|
require(not validator.runner_is_inactive_and_bounded(value), "target accepted")
|
|
|
|
@case("13 missing attempt receipt fails")
|
|
def _() -> None:
|
|
value = dict(record["runner"])
|
|
value["attempt_receipt_required"] = False
|
|
require(not validator.runner_is_inactive_and_bounded(value), "missing receipt accepted")
|
|
|
|
@case("14 receipt after connect fails")
|
|
def _() -> None:
|
|
value = dict(record["runner"])
|
|
value["attempt_receipt_written_before_connect"] = False
|
|
require(not validator.runner_is_inactive_and_bounded(value), "late receipt accepted")
|
|
|
|
@case("15 nondurable receipt fails")
|
|
def _() -> None:
|
|
value = dict(record["runner"])
|
|
value["attempt_receipt_durable_fsync"] = False
|
|
require(not validator.runner_is_inactive_and_bounded(value), "nondurable receipt accepted")
|
|
|
|
@case("16 retry fails")
|
|
def _() -> None:
|
|
value = dict(record["runner"])
|
|
value["retry"] = True
|
|
require(not validator.runner_is_inactive_and_bounded(value), "retry accepted")
|
|
|
|
@case("17 reconnect fails")
|
|
def _() -> None:
|
|
value = dict(record["runner"])
|
|
value["reconnect"] = True
|
|
require(not validator.runner_is_inactive_and_bounded(value), "reconnect accepted")
|
|
|
|
@case("18 approval template is inactive")
|
|
def _() -> None:
|
|
require(validator.approval_template_is_inactive(approval), "approval rejected")
|
|
|
|
@case("19 authorized tracked approval fails")
|
|
def _() -> None:
|
|
value = dict(approval)
|
|
value["authorized"] = True
|
|
require(not validator.approval_template_is_inactive(value), "active approval accepted")
|
|
|
|
@case("20 target in tracked approval fails")
|
|
def _() -> None:
|
|
value = dict(approval)
|
|
value["target"] = "device"
|
|
require(not validator.approval_template_is_inactive(value), "approval target accepted")
|
|
|
|
@case("21 approval run id fails")
|
|
def _() -> None:
|
|
value = dict(approval)
|
|
value["run_id"] = "run-1"
|
|
require(not validator.approval_template_is_inactive(value), "approval run id accepted")
|
|
|
|
@case("22 approval retry fails")
|
|
def _() -> None:
|
|
value = dict(approval)
|
|
value["automatic_retry"] = True
|
|
require(not validator.approval_template_is_inactive(value), "approval retry accepted")
|
|
|
|
@case("23 all four future maxima are exactly one")
|
|
def _() -> None:
|
|
require(all(record["runner"][field] == 1 for field in (
|
|
"maximum_connections", "maximum_transfers", "maximum_executions", "maximum_result_receives"
|
|
)), "one-shot maximum changed")
|
|
|
|
@case("24 host tests are not hardware evidence")
|
|
def _() -> None:
|
|
require(record["tests"]["hardware_evidence_from_phase10g"] is False, "host result promoted")
|
|
|
|
@case("25 no target artifact is tracked")
|
|
def _() -> None:
|
|
tracked = validator.git(root, "ls-files").splitlines()
|
|
require(not any(path.lower().endswith((".elf", ".self", ".sprx", ".pkg", ".map")) for path in tracked), "artifact tracked")
|
|
|
|
failures: list[str] = []
|
|
for name, function in cases:
|
|
try:
|
|
function()
|
|
print(f"PASS {name}")
|
|
except Exception as error: # noqa: BLE001 - mutation harness
|
|
failures.append(f"{name}: {error}")
|
|
print(f"FAIL {name}: {error}")
|
|
if failures:
|
|
return 1
|
|
print(f"Phase-1.0G policy tests passed: {len(cases)}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|