114 lines
5.1 KiB
Python
114 lines
5.1 KiB
Python
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""Host tests for the target-free Phase-1.0AV canary contract."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
from dataclasses import replace
|
|
from pathlib import Path
|
|
import sys
|
|
import unittest
|
|
|
|
|
|
PARSER = argparse.ArgumentParser()
|
|
PARSER.add_argument("--root", type=Path, required=True)
|
|
ROOT = PARSER.parse_args().root
|
|
sys.path.insert(0, str(ROOT / "tools"))
|
|
|
|
from phase10av_launch_context_canary import * # noqa: E402,F403
|
|
|
|
|
|
def plan() -> CanaryPairPlan:
|
|
return CanaryPairPlan(
|
|
PHASE, FIRMWARE, PROTOCOL_MAGIC, "a" * 64,
|
|
(CanaryArm(RAW_ELFLDR, "b" * 64, "CHIMERA_AV_RAW_0001", "c" * 64),
|
|
CanaryArm(BIGAPP_CANDIDATE, "d" * 64,
|
|
"CHIMERA_AV_BIGAPP_01", "e" * 64)),
|
|
True, False, False, False, False, False, False, False, False, False)
|
|
|
|
|
|
def observation(arm: CanaryArm, submit_result: int) -> CanaryObservation:
|
|
return CanaryObservation(
|
|
arm.kind, arm.launcher_sha256, "a" * 64, arm.run_id, PROTOCOL_MAGIC,
|
|
True, -1, 20, True, submit_result, 0, 10, True, 30, True, False, 0, 0)
|
|
|
|
|
|
class LaunchContextCanaryTests(unittest.TestCase):
|
|
def test_complete_plan_and_equal_results(self) -> None:
|
|
value = plan()
|
|
validate_plan(value)
|
|
result = classify_pair(
|
|
value, tuple(observation(arm, -1) for arm in value.arms))
|
|
self.assertTrue(result.pair_comparable)
|
|
self.assertEqual(result.status, "NO_SUBMIT_RETURN_DIFFERENCE")
|
|
self.assertFalse(result.root_cause_proven or result.visible_output_proven)
|
|
|
|
def test_changed_return_is_candidate_only(self) -> None:
|
|
value = plan()
|
|
result = classify_pair(
|
|
value, (observation(value.arms[0], -1),
|
|
observation(value.arms[1], 0)))
|
|
self.assertTrue(result.launch_context_candidate)
|
|
self.assertFalse(result.root_cause_proven)
|
|
self.assertFalse(result.visible_output_proven)
|
|
self.assertFalse(result.firmware_behavior_proven)
|
|
self.assertFalse(result.device_action_authorized)
|
|
|
|
def test_plan_requires_distinct_arms_and_approvals(self) -> None:
|
|
value = plan()
|
|
for field, replacement in (
|
|
("launcher_sha256", value.arms[0].launcher_sha256),
|
|
("run_id", value.arms[0].run_id),
|
|
("approval_sha256", value.arms[0].approval_sha256)):
|
|
arms = (value.arms[0], replace(value.arms[1], **{field: replacement}))
|
|
with self.subTest(field=field), self.assertRaises(CanaryContractError):
|
|
validate_plan(replace(value, arms=arms))
|
|
|
|
def test_plan_rejects_every_authority_and_retry(self) -> None:
|
|
for field in ("automatic_retry", "reconnect", "resume", "installation",
|
|
"autoload", "device_write_authorized",
|
|
"app_termination_authorized", "result_reception_authorized",
|
|
"activation_authorized"):
|
|
with self.subTest(field=field), self.assertRaises(CanaryContractError):
|
|
validate_plan(replace(plan(), **{field: True}))
|
|
|
|
def test_missing_or_early_terminal_is_incomplete(self) -> None:
|
|
value = plan()
|
|
baseline = observation(value.arms[0], -1)
|
|
for candidate in (
|
|
replace(observation(value.arms[1], 0), terminal_seen=False),
|
|
replace(observation(value.arms[1], 0), cleanup_complete=False),
|
|
replace(observation(value.arms[1], 0), terminal_sequence=20),
|
|
replace(observation(value.arms[1], 0), submit_sequence=20),
|
|
replace(observation(value.arms[1], 0), d04_seen=False),
|
|
replace(observation(value.arms[1], 0), submit_seen=False)):
|
|
with self.subTest(candidate=candidate):
|
|
result = classify_pair(value, (baseline, candidate))
|
|
self.assertEqual(result.status, "INCOMPLETE_NO_CAUSAL_COMPARISON")
|
|
|
|
def test_identity_drift_is_hard_failure(self) -> None:
|
|
value = plan()
|
|
baseline = observation(value.arms[0], -1)
|
|
candidate = observation(value.arms[1], 0)
|
|
for field, replacement in (("payload_sha256", "f" * 64),
|
|
("run_id", "CHIMERA_AV_WRONG_001"),
|
|
("protocol_magic", "CHD10OLD"),
|
|
("launcher_sha256", "f" * 64)):
|
|
with self.subTest(field=field), self.assertRaises(CanaryContractError):
|
|
classify_pair(value, (baseline, replace(candidate, **{field: replacement})))
|
|
|
|
def test_forbidden_result_claims_are_hard_failure(self) -> None:
|
|
value = plan()
|
|
baseline = observation(value.arms[0], -1)
|
|
candidate = observation(value.arms[1], 0)
|
|
for field, replacement in (("retry_count", 1),
|
|
("persistent_write_count", 1),
|
|
("visible_output_observed", True)):
|
|
with self.subTest(field=field), self.assertRaises(CanaryContractError):
|
|
classify_pair(value, (baseline, replace(candidate, **{field: replacement})))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(argv=[__file__])
|