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_udp_hbp_mesh_registry.py

118 lines
3.7 KiB

# ADN DMR Peer Server - tests infrastructure udp hbp mesh registry
#
# 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
###############################################################################
"""MeshCodecRegistry wired into HBPProtocol OPENBRIDGE ingress/egress."""
from __future__ import annotations
import logging
from adn_server.domain import bytes_4
from adn_server.infrastructure.hbp_constants import DMRD
from adn_server.infrastructure.mesh.obp_v1 import build_dmrd_v1
from adn_server.infrastructure.twisted_adapters.udp_hbp import HBPProtocol
_PASS = b"test-passphrase\x00\x00\x00\x00\x00\x00"
_SERVER = bytes_4(73010)
_OBP_ADDR = ("127.0.0.1", 62030)
class _RecordingTransport:
def __init__(self) -> None:
self.sent: list[tuple[bytes, tuple[str, int]]] = []
def write(self, data: bytes, addr: tuple[str, int]) -> None:
self.sent.append((data, addr))
def _sample_dmr_voice() -> bytes:
return b"".join(
[
DMRD,
bytes([1]),
bytes_4(1001)[1:4],
bytes_4(52090)[1:4],
bytes_4(1),
bytes([0x10]),
bytes_4(0xAABBCCDD),
b"\x00" * 33,
]
)
def _obp_protocol() -> HBPProtocol:
config = {
"GLOBAL": {"SERVER_ID": _SERVER, "PASSPHRASE": _PASS},
"SYSTEMS": {
"OBP-A": {
"MODE": "OPENBRIDGE",
"PASSPHRASE": _PASS,
"VER": 5,
"TARGET_IP": "127.0.0.1",
"TARGET_PORT": 62030,
"NETWORK_ID": _SERVER,
},
},
}
return HBPProtocol("OBP-A", config)
def test_hbp_decode_mesh_ingress_obp_v1() -> None:
proto = _obp_protocol()
inner = _sample_dmr_voice()
wire = build_dmrd_v1(inner, _SERVER, _PASS)
ingress = proto._try_decode_mesh_ingress(wire)
assert ingress is not None
assert ingress.codec == "obp_v1"
assert ingress.voice_frame[:4] == DMRD
def test_hbp_encode_mesh_egress_dmre_when_ver_ge_4() -> None:
proto = _obp_protocol()
inner = _sample_dmr_voice()
wire = proto._encode_mesh_egress(
inner,
hops=b"\x01",
ber=b"\x00",
rssi=b"\x00",
source_server=_SERVER,
source_rptr=bytes_4(100),
)
assert wire is not None
assert len(wire) == 89
assert wire[:4] == b"DMRE"
def test_obp_hmac_reject_discards_and_warns(caplog) -> None:
proto = _obp_protocol()
proto._config["VER"] = 1
transport = _RecordingTransport()
proto.transport = transport # type: ignore[assignment]
proto._config["TARGET_SOCK"] = _OBP_ADDR
inner = _sample_dmr_voice()
wire = bytearray(build_dmrd_v1(inner, _SERVER, _PASS))
wire[-1] ^= 0xFF
with caplog.at_level(logging.WARNING):
proto.datagramReceived(bytes(wire), _OBP_ADDR)
assert transport.sent == []
assert any("HMAC failed" in record.message for record in caplog.records)

Powered by TurnKey Linux.