146 行
5 KiB
Python
146 行
5 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Contract tests for the P4-B1b reject-only guard boundary."""
|
|
|
|
import ast
|
|
from pathlib import Path
|
|
import sys
|
|
import unittest
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
if str(ROOT) not in sys.path:
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
import p4_b1b_guards as guards
|
|
from p4_b1b_guards import (
|
|
HOST_OWNER_CAPABILITY_UNAVAILABLE_CODE,
|
|
REVIEW_PRODUCER_UNAVAILABLE_CODE,
|
|
reject_unverified_owner_capability,
|
|
reject_unverified_review_producer,
|
|
)
|
|
from unified_person_contract import empty_unified_person_store
|
|
from unified_person_registry import UnifiedPersonRegistry
|
|
|
|
|
|
class _PoisonClaim:
|
|
"""Raises if a guard tries to observe it through common Python protocols."""
|
|
|
|
def __getattribute__(self, name: str) -> object:
|
|
raise AssertionError(f"unexpected attribute access: {name}")
|
|
|
|
def __getitem__(self, key: object) -> object:
|
|
raise AssertionError(f"unexpected mapping access: {key}")
|
|
|
|
def __iter__(self) -> object:
|
|
raise AssertionError("unexpected iteration")
|
|
|
|
def __len__(self) -> int:
|
|
raise AssertionError("unexpected length access")
|
|
|
|
def __bool__(self) -> bool:
|
|
raise AssertionError("unexpected truthiness access")
|
|
|
|
def __repr__(self) -> str:
|
|
raise AssertionError("unexpected repr access")
|
|
|
|
|
|
class P4B1bGuardTests(unittest.TestCase):
|
|
def test_owner_capability_is_always_denied_with_the_stable_code(self) -> None:
|
|
result = reject_unverified_owner_capability()
|
|
|
|
self.assertEqual(
|
|
result,
|
|
{
|
|
"accepted": False,
|
|
"live_effect_permitted": False,
|
|
"code": "p4_b1b_host_owner_capability_unavailable",
|
|
},
|
|
)
|
|
self.assertEqual(HOST_OWNER_CAPABILITY_UNAVAILABLE_CODE, result["code"])
|
|
|
|
def test_review_producer_is_always_denied_with_the_stable_code(self) -> None:
|
|
result = reject_unverified_review_producer()
|
|
|
|
self.assertEqual(
|
|
result,
|
|
{
|
|
"accepted": False,
|
|
"live_effect_permitted": False,
|
|
"code": "p4_b1b_review_producer_unavailable",
|
|
},
|
|
)
|
|
self.assertEqual(REVIEW_PRODUCER_UNAVAILABLE_CODE, result["code"])
|
|
|
|
def test_poison_claims_are_discarded_without_observation(self) -> None:
|
|
self.assertFalse(reject_unverified_owner_capability(_PoisonClaim())["accepted"])
|
|
self.assertFalse(reject_unverified_review_producer(_PoisonClaim())["accepted"])
|
|
|
|
def test_forged_dicts_never_enable_a_live_effect(self) -> None:
|
|
forged_claim = {
|
|
"accepted": True,
|
|
"live_effect_permitted": True,
|
|
"owner": True,
|
|
"review_producer": "trusted",
|
|
"code": "allow_everything",
|
|
}
|
|
|
|
for guard, code in (
|
|
(reject_unverified_owner_capability, HOST_OWNER_CAPABILITY_UNAVAILABLE_CODE),
|
|
(reject_unverified_review_producer, REVIEW_PRODUCER_UNAVAILABLE_CODE),
|
|
):
|
|
with self.subTest(guard=guard.__name__):
|
|
result = guard(forged_claim)
|
|
self.assertEqual(
|
|
result,
|
|
{
|
|
"accepted": False,
|
|
"live_effect_permitted": False,
|
|
"code": code,
|
|
},
|
|
)
|
|
|
|
def test_every_call_returns_an_independent_plain_dict(self) -> None:
|
|
first = reject_unverified_owner_capability()
|
|
second = reject_unverified_owner_capability()
|
|
review = reject_unverified_review_producer()
|
|
|
|
self.assertIsInstance(first, dict)
|
|
self.assertIsInstance(review, dict)
|
|
self.assertIsNot(first, second)
|
|
self.assertIsNot(first, review)
|
|
first["accepted"] = True
|
|
first["code"] = "forged"
|
|
self.assertEqual(False, second["accepted"])
|
|
self.assertEqual(HOST_OWNER_CAPABILITY_UNAVAILABLE_CODE, second["code"])
|
|
self.assertEqual(
|
|
REVIEW_PRODUCER_UNAVAILABLE_CODE,
|
|
reject_unverified_review_producer()["code"],
|
|
)
|
|
|
|
def test_module_has_no_import_or_call_integration_surface(self) -> None:
|
|
source = Path(guards.__file__).read_text(encoding="utf-8")
|
|
tree = ast.parse(source)
|
|
|
|
imports = [
|
|
node
|
|
for node in ast.walk(tree)
|
|
if isinstance(node, (ast.Import, ast.ImportFrom))
|
|
]
|
|
calls = [node for node in ast.walk(tree) if isinstance(node, ast.Call)]
|
|
self.assertEqual([], imports)
|
|
self.assertEqual([], calls)
|
|
|
|
def test_guard_usage_does_not_create_p4_effect_in_an_isolated_registry_store(self) -> None:
|
|
store = {"unified_person": empty_unified_person_store()}
|
|
registry = UnifiedPersonRegistry(store)
|
|
before = registry.status()
|
|
|
|
reject_unverified_owner_capability({"owner": "forged"})
|
|
reject_unverified_review_producer({"producer": "forged"})
|
|
|
|
self.assertEqual(before, registry.status())
|
|
self.assertNotIn("p4_effect", store["unified_person"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|