Files
mctf/towercore/test_guard.py
T
TakuyaYagam1 3cd120414f fix(towercore): use native peer-read authorization
- Extend the existing native 0x203 ownership gate from opcode 0x21 to 0x23.
- Keep peer handover and context transfer semantics unchanged; block duplicate selector ambiguity in the proxy.
2026-11-29 16:02:13 +03:00

60 lines
2.1 KiB
Python

import importlib
import importlib.util
import unittest
def frame(opcode: int, fields: list[tuple[int, bytes]]) -> bytes:
payload = b"".join(
tag.to_bytes(2, "little") + len(value).to_bytes(2, "little") + value
for tag, value in fields
)
return b"LT" + bytes((1, opcode)) + b"\0" * 10 + len(payload).to_bytes(2, "little") + payload
class GuardTests(unittest.TestCase):
def test_allows_single_peer_read_for_native_authorization(self) -> None:
module = importlib.import_module("guard")
request = frame(0x21, [(0x0B, bytes.fromhex("87481bd9") + b"\0" * 4)])
self.assertFalse(module.should_block(request))
def test_rejects_duplicate_selector_override(self) -> None:
module = importlib.import_module("guard")
request = frame(
0x21,
[
(0x0B, bytes.fromhex("05138e91") + b"\0" * 4),
(0x0B, bytes.fromhex("87481bd9") + b"\0" * 4),
],
)
self.assertTrue(module.should_block(request))
def test_rejects_duplicate_inventory_selector(self) -> None:
module = importlib.import_module("guard")
request = frame(
0x23,
[
(0x0B, bytes.fromhex("05138e91") + b"\0" * 4),
(0x0B, bytes.fromhex("87481bd9") + b"\0" * 4),
],
)
self.assertTrue(module.should_block(request))
def test_allows_single_peer_inventory_for_native_authorization(self) -> None:
module = importlib.import_module("guard")
request = frame(0x23, [(0x0B, bytes.fromhex("87481bd9") + b"\0" * 4)])
self.assertFalse(module.should_block(request))
def test_allows_inventory_without_selector(self) -> None:
module = importlib.import_module("guard")
request = frame(0x23, [(0x0D, b"\x05\0\0\0")])
self.assertFalse(module.should_block(request))
def test_keeps_peer_handover_prepare(self) -> None:
module = importlib.import_module("guard")
request = frame(0x40, [(0x0B, bytes.fromhex("87481bd9") + b"\0" * 4)])
self.assertFalse(module.should_block(request))
if __name__ == "__main__":
unittest.main()