You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ADN-DMR-Peer-Server/tests/infrastructure/test_report_server_wire.py

191 lines
6.5 KiB

# ADN DMR Peer Server - tests infrastructure report server wire
#
# Copyright (C) 2026 Rodrigo Pérez, CE5RPY <ce5rpy@qmd.cl>
#
###############################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
###############################################################################
"""Report server wire encoding."""
from __future__ import annotations
import json
from adn_server.domain.value_objects import bytes_4
from adn_server.infrastructure.twisted_adapters.report_server import (
REPORT_OPCODES,
ReportServerFactory,
)
class _CapturingClient:
def __init__(self) -> None:
self.messages: list[bytes] = []
def sendString(self, data: bytes) -> None:
self.messages.append(data)
def _report_config() -> dict:
return {"REPORTS": {"REPORT": True, "REPORT_CLIENTS": ["*"]}}
def test_connect_sends_hello_topology_and_routing() -> None:
factory = ReportServerFactory(_report_config())
factory.set_systems(
{
"MASTER-A": {"MODE": "MASTER", "ENABLED": True, "IP": "10.0.0.1", "PORT": 62030},
}
)
factory.set_routing_table(
{
"52090": [
{"SYSTEM": "MASTER-A", "TS": 2, "TGID": 52090, "ACTIVE": True, "TO_TYPE": "ON"},
],
}
)
client = _CapturingClient()
factory.clients.append(client)
factory._send_hello_to(client)
factory._send_state_to(client, force=True)
assert len(client.messages) == 2
hello = json.loads(client.messages[0][1:].decode())
assert hello["report_protocol"] == 2
assert "REPORT_V2" in hello["features"]
assert client.messages[1][:1] == REPORT_OPCODES["STATE_SND"]
assert json.loads(client.messages[1][1:])["type"] == "dashboard_state"
def test_bridge_event_emits_voice_event_json() -> None:
factory = ReportServerFactory(_report_config())
client = _CapturingClient()
factory.clients.append(client)
factory.send_routing_event("GROUP VOICE,START,RX,MASTER-A,2155905152,1001,3120001,2,52090")
assert len(client.messages) == 1
assert client.messages[0][:1] == REPORT_OPCODES["VOICE_EVENT_SND"]
voice = json.loads(client.messages[0][1:].decode())
assert voice["type"] == "voice_event"
assert voice["phase"] == "START"
assert voice["dst_id"] == 52090
def test_incremental_bridge_update_sends_delta() -> None:
factory = ReportServerFactory(_report_config())
factory.set_routing_table(
{
"52090": [
{"SYSTEM": "MASTER-A", "TS": 2, "TGID": 52090, "ACTIVE": True, "TO_TYPE": "ON"},
],
}
)
client = _CapturingClient()
factory.clients.append(client)
factory.send_routing_table(incremental=False)
factory.set_routing_table(
{
"52090": [
{"SYSTEM": "MASTER-A", "TS": 2, "TGID": 52090, "ACTIVE": False, "TO_TYPE": "ON"},
],
}
)
factory.send_routing_table(incremental=True)
assert len(client.messages) == 2
assert client.messages[0][:1] == REPORT_OPCODES["ROUTING_TABLE_SND"]
assert client.messages[1][:1] == REPORT_OPCODES["DELTA_SND"]
delta = json.loads(client.messages[1][1:].decode())
assert delta["type"] == "delta"
assert delta["patch"]["type"] == "routing_table"
def test_inject_proxy_topology_expanded_for_monitor() -> None:
peer = bytes_4(730039101)
config = {
"REPORTS": {"REPORT": True, "REPORT_CLIENTS": ["*"]},
"PROXY": {"TARGET_SYSTEM": "SYSTEM"},
}
factory = ReportServerFactory(config)
factory.set_systems(
{
"SYSTEM": {
"MODE": "MASTER",
"ENABLED": True,
"MAX_PEERS": 102,
"_REPORT_BASE_PORT": 56400,
"PEERS": {
peer: {
"CONNECTION": "YES",
"CONNECTED": 1_700_000_000,
"IP": "203.0.113.10",
"PORT": 62031,
"CALLSIGN": b"CE5RPY ",
}
},
}
}
)
factory.set_peer_slot_map(lambda: {peer: 2})
client = _CapturingClient()
factory.clients.append(client)
factory._send_state_to(client, force=True)
assert client.messages[0][:1] == REPORT_OPCODES["STATE_SND"]
state_doc = json.loads(client.messages[0][1:].decode())
names = set((state_doc.get("ctable") or {}).get("MASTERS", {}))
names |= set((state_doc.get("ctable") or {}).get("OPENBRIDGES", {}))
assert "SYSTEM" not in names
assert "SYSTEM-2" in names
system = state_doc["ctable"]["MASTERS"]["SYSTEM-2"]
assert system["port"] == 56402
peer_keys = {int(k) for k in system["peers"]}
assert 730039101 in peer_keys
def test_send_routing_event_remaps_inject_proxy_system_name() -> None:
peer = bytes_4(730039101)
config = {
"REPORTS": {"REPORT": True, "REPORT_CLIENTS": ["*"]},
"PROXY": {"TARGET_SYSTEM": "SYSTEM"},
}
factory = ReportServerFactory(config)
factory.set_systems(
{
"SYSTEM": {
"MODE": "MASTER",
"ENABLED": True,
"MAX_PEERS": 102,
"PEERS": {
peer: {
"CONNECTION": "YES",
"CONNECTED": 1_700_000_000,
"IP": "203.0.113.10",
"PORT": 62031,
}
},
}
}
)
factory.set_peer_slot_map(lambda: {peer: 4})
client = _CapturingClient()
factory.clients.append(client)
factory.send_routing_event(
"GROUP VOICE,START,TX,SYSTEM,2693411696,9990,7300392,2,9990"
)
assert len(client.messages) == 1
payload = json.loads(client.messages[0][1:].decode())
assert payload["type"] == "voice_event"
assert payload["system"] == "SYSTEM-4"
assert payload["peer_id"] == 9990

Powered by TurnKey Linux.