Files
mctf/towercore/test_guard.py
T
TakuyaYagam1 903d8872c2 fix(towercore): isolate subscriber reads
- track the authenticated session TMSI from server responses
- return empty frames for every cross-subscriber read variant
2026-11-29 16:42:23 +03:00

106 lines
4.6 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))
def test_blocks_inventory_during_uncommitted_handover(self) -> None:
module = importlib.import_module("guard")
guard = module.ConnectionGuard()
selector = bytes.fromhex("87481bd9") + b"\0" * 4
self.assertFalse(guard.should_block(frame(0x40, [(0x0B, selector), (0x0C, b"\xe9\x03\0\0")])))
self.assertTrue(guard.should_block(frame(0x23, [(0x0B, selector)])))
def test_allows_inventory_after_handover_abort(self) -> None:
module = importlib.import_module("guard")
guard = module.ConnectionGuard()
selector = bytes.fromhex("87481bd9") + b"\0" * 4
self.assertFalse(guard.should_block(frame(0x40, [(0x0B, selector), (0x0C, b"\xe9\x03\0\0")])))
self.assertFalse(guard.should_block(frame(0x42, [(0x15, b"\x01\0\0\0")])))
self.assertFalse(guard.should_block(frame(0x23, [(0x0B, selector)])))
def test_allows_inventory_after_handover_commit(self) -> None:
module = importlib.import_module("guard")
guard = module.ConnectionGuard()
selector = bytes.fromhex("87481bd9") + b"\0" * 4
self.assertFalse(guard.should_block(frame(0x40, [(0x0B, selector), (0x0C, b"\xea\x03\0\0")])))
self.assertFalse(guard.should_block(frame(0x41, [(0x15, b"\x01\0\0\0")])))
self.assertFalse(guard.should_block(frame(0x23, [(0x0B, selector)])))
def test_returns_empty_success_for_blocked_inventory(self) -> None:
module = importlib.import_module("guard")
request = frame(0x23, [(0x0B, bytes.fromhex("87481bd9") + b"\0" * 4)])
response = module.empty_response(request)
self.assertEqual(response, request[:14] + b"\0\0")
def test_blocks_cross_subscriber_reads_without_disconnect(self) -> None:
module = importlib.import_module("guard")
guard = module.ConnectionGuard()
own_tmsi = b"\x11\x22\x33\x44"
guard.observe_server(frame(0x02, [(0x09, own_tmsi)]))
peer_request = frame(0x21, [(0x0B, b"\x55\x66\x77\x88" + b"\0" * 4)])
self.assertTrue(guard.should_block(peer_request))
self.assertEqual(module.empty_response(peer_request), peer_request[:14] + b"\0\0")
def test_allows_attached_subscriber_reads(self) -> None:
module = importlib.import_module("guard")
guard = module.ConnectionGuard()
own_tmsi = b"\x11\x22\x33\x44"
guard.observe_server(frame(0x04, [(0x09, own_tmsi)]))
self.assertFalse(guard.should_block(frame(0x21, [(0x0B, own_tmsi + b"\0" * 4)])))
if __name__ == "__main__":
unittest.main()