From 685d7253ff28bd0c21f748202c8f17e878758c42 Mon Sep 17 00:00:00 2001 From: Audric Date: Fri, 27 Mar 2026 14:43:57 +0100 Subject: [PATCH] Fix trunk receive-side TG filtering and add integration test suite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The receive-side handlers (handleMsgTrunkTalkerStart, handleMsgTrunkAudio, etc.) used isSharedTG() which only checks the remote peer's prefix. This caused non-cluster TGs to be silently dropped on the receiving reflector — e.g. a client on reflector-a talking on TG 40001 (owned by reflector-b) would never reach reflector-b because reflector-b checked the TG against reflector-a's prefix instead of its own. Add isOwnedTG() that accepts TGs matching either the local or remote prefix, and use it in all four receive handlers. The send-side still uses isSharedTG() (remote prefix only) which is correct. Also add a Docker-based integration test suite (tests/) with 11 automated tests covering handshake, routing, cross-reflector audio delivery, cluster TGs, heartbeat, disconnect cleanup, and trunk-to-trunk isolation. An interactive mode lets users test arbitrary TG numbers with routing verified via reflector logs. Topology is defined once in tests/topology.py. Fix Dockerfile to copy built shared libraries to the runtime image. --- .gitignore | 5 + Dockerfile | 1 + README.md | 20 + docs/DESIGN_SATELLITE_AND_CLUSTER.md | 9 +- docs/DOCKER.md | 3 +- docs/TRUNK_PROTOCOL.md | 20 +- src/svxlink/reflector/TrunkLink.cpp | 38 +- src/svxlink/reflector/TrunkLink.h | 1 + tests/generate_configs.py | 149 ++++ tests/run_tests.sh | 18 + tests/test_trunk.py | 1161 ++++++++++++++++++++++++++ tests/topology.py | 92 ++ 12 files changed, 1501 insertions(+), 16 deletions(-) create mode 100644 tests/generate_configs.py create mode 100755 tests/run_tests.sh create mode 100644 tests/test_trunk.py create mode 100644 tests/topology.py diff --git a/.gitignore b/.gitignore index 5a37a98..f9388ab 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,11 @@ _deps/ .claude/ CLAUDE.md +# Test generated files (regenerated by tests/generate_configs.py) +tests/configs/ +tests/docker-compose.test.yml +tests/__pycache__/ + # Editor and IDE .vscode/ .idea/ diff --git a/Dockerfile b/Dockerfile index 9652bf7..db535a0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,6 +31,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ && rm -rf /var/lib/apt/lists/* COPY --from=builder /src/build/bin/svxreflector /usr/bin/svxreflector +COPY --from=builder /src/build/lib/lib*.so* /usr/lib/ # 5300 tcp+udp: SvxLink client connections and audio # 5302 tcp: server-to-server trunk links diff --git a/README.md b/README.md index 672bc10..6accec3 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,26 @@ cmake --build build --- +## Testing + +Integration tests spin up a 3-reflector Docker mesh and verify trunk routing, +audio delivery, and protocol behavior: + +```bash +cd tests && bash run_tests.sh +``` + +Requires Docker and Python 3.7+. The script builds the images, starts the mesh, +runs 11 automated tests, then drops into an interactive prompt where you can +enter any TG number and see which reflector it routes to (verified via container +logs). + +The mesh topology is defined in `tests/topology.py` — edit prefixes, cluster +TGs, or add reflectors there, and `run_tests.sh` regenerates all configs +automatically. + +--- + ## Configuration GeuReflector uses the same `svxreflector.conf` format as SvxReflector with two diff --git a/docs/DESIGN_SATELLITE_AND_CLUSTER.md b/docs/DESIGN_SATELLITE_AND_CLUSTER.md index b444b74..8a76429 100644 --- a/docs/DESIGN_SATELLITE_AND_CLUSTER.md +++ b/docs/DESIGN_SATELLITE_AND_CLUSTER.md @@ -80,7 +80,7 @@ Similar to `TrunkLink` but with different semantics: | Aspect | TrunkLink (peer-to-peer) | SatelliteLink | |--------|-------------------------|---------------| | Topology | Symmetric mesh peers | Asymmetric — parent is authority | -| TG routing | Prefix-based (`isSharedTG`) + cluster (`isClusterTG`) | **No filtering** — all TGs forwarded unconditionally | +| TG routing | Prefix-based (`isSharedTG`/`isOwnedTG`) + cluster (`isClusterTG`) | **No filtering** — all TGs forwarded unconditionally | | Talker arbitration | Nonce tie-break | Parent always wins | | Who initiates | Both sides connect to each other | Satellite connects to parent | | Audio path | Only prefix-matched + cluster TGs | All TGs active on either side | @@ -260,11 +260,12 @@ void TrunkLink::onLocalTalkerStart(uint32_t tg, const std::string& callsign) } ``` -Similarly for inbound: `handleMsgTrunkTalkerStart` currently checks -`isSharedTG`. Add `|| isClusterTG`: +Similarly for inbound: `handleMsgTrunkTalkerStart` and the other receive +handlers use `isOwnedTG` (checks both local and remote prefix) instead of +`isSharedTG`, combined with `|| isClusterTG`: ```cpp -if (!isSharedTG(tg) && !m_reflector->isClusterTG(tg)) +if (!isOwnedTG(tg) && !m_reflector->isClusterTG(tg)) { return; } diff --git a/docs/DOCKER.md b/docs/DOCKER.md index af8ad70..f32ffe1 100644 --- a/docs/DOCKER.md +++ b/docs/DOCKER.md @@ -64,7 +64,8 @@ TRUNK_2: Trunk hello from peer 'TRUNK_1' local_prefix=2 priority=3847291042 | 5300 | TCP | SvxLink client connections | | 5300 | UDP | SvxLink client audio | | 5302 | TCP | Server-to-server trunk links | -| 8080 | TCP | HTTP `/status` endpoint (optional) | +| 5303 | TCP | Satellite connections (optional) | +| 8080 | TCP | HTTP `/status` and `/config` endpoints (optional) | If you do not set `HTTP_SRV_PORT` in the config, remove the `8080` port mapping from `docker-compose.yml`. diff --git a/docs/TRUNK_PROTOCOL.md b/docs/TRUNK_PROTOCOL.md index 963c7d0..d8d621d 100644 --- a/docs/TRUNK_PROTOCOL.md +++ b/docs/TRUNK_PROTOCOL.md @@ -95,18 +95,24 @@ forwarded to **all** trunk peers regardless of `LOCAL_PREFIX` or `REMOTE_PREFIX` The routing decision in `TrunkLink` is: ``` -if isClusterTG(tg) → forward (regardless of prefix) -else if isSharedTG(tg) → forward (prefix match) -else → drop silently +Sending: if isClusterTG(tg) or isSharedTG(tg) → send to peer +Receiving: if isClusterTG(tg) or isOwnedTG(tg) → accept from peer +Otherwise: drop silently ``` -This check is applied **independently on both sides** of each trunk link: +This check is applied **independently on both sides** of each trunk link, +using different prefix checks appropriate to each direction: - **Sending side:** `TrunkLink::onLocalTalkerStart`, `onLocalAudio`, and `onLocalFlush` check `isSharedTG(tg) || isClusterTG(tg)` before sending. + `isSharedTG` matches the TG against the **remote** peer's prefix — i.e. it + only forwards TGs that belong to the peer. - **Receiving side:** `TrunkLink::handleMsgTrunkTalkerStart`, - `handleMsgTrunkAudio`, and `handleMsgTrunkFlush` perform the same check - before accepting. + `handleMsgTrunkAudio`, and `handleMsgTrunkFlush` check + `isOwnedTG(tg) || isClusterTG(tg)` before accepting. `isOwnedTG` matches + the TG against both the **local** prefix (TG belongs to us — a peer's client + is talking on one of our TGs) and the **remote** prefix (TG belongs to the + peer — the peer is reporting its own talker state). Each reflector owner chooses which cluster TGs to subscribe to. A reflector only sends and accepts traffic for cluster TGs listed in its own `CLUSTER_TGS`. @@ -123,7 +129,7 @@ directions: | Aspect | Trunk link | Satellite link | |--------|-----------|----------------| -| Prefix filtering (`isSharedTG`) | Yes | No | +| Prefix filtering (`isSharedTG`/`isOwnedTG`) | Yes | No | | Cluster filtering (`isClusterTG`) | Yes | No | | What is forwarded | Only prefix-matched + cluster TGs | All TGs | | `CLUSTER_TGS` config needed | Yes, on both sides | No | diff --git a/src/svxlink/reflector/TrunkLink.cpp b/src/svxlink/reflector/TrunkLink.cpp index 60dc8f2..deec319 100644 --- a/src/svxlink/reflector/TrunkLink.cpp +++ b/src/svxlink/reflector/TrunkLink.cpp @@ -230,6 +230,36 @@ bool TrunkLink::isSharedTG(uint32_t tg) const } /* TrunkLink::isSharedTG */ +bool TrunkLink::isOwnedTG(uint32_t tg) const +{ + const std::string s = std::to_string(tg); + + // Accept TGs matching our local prefix (TG belongs to us — a peer's + // client is talking on one of our TGs) + for (const auto& prefix : m_local_prefix) + { + if (s.size() >= prefix.size() && + s.compare(0, prefix.size(), prefix) == 0) + { + return true; + } + } + + // Accept TGs matching the remote prefix (TG belongs to the peer — + // the peer is reporting its own talker state for our awareness) + for (const auto& prefix : m_remote_prefix) + { + if (s.size() >= prefix.size() && + s.compare(0, prefix.size(), prefix) == 0) + { + return true; + } + } + + return false; +} /* TrunkLink::isOwnedTG */ + + Json::Value TrunkLink::statusJson(void) const { Json::Value obj(Json::objectValue); @@ -540,7 +570,7 @@ void TrunkLink::handleMsgTrunkTalkerStart(std::istream& is) } uint32_t tg = msg.tg(); - if (!isSharedTG(tg) && !m_reflector->isClusterTG(tg)) + if (!isOwnedTG(tg) && !m_reflector->isClusterTG(tg)) { return; } @@ -584,7 +614,7 @@ void TrunkLink::handleMsgTrunkTalkerStop(std::istream& is) } uint32_t tg = msg.tg(); - if (!isSharedTG(tg) && !m_reflector->isClusterTG(tg)) + if (!isOwnedTG(tg) && !m_reflector->isClusterTG(tg)) { return; } @@ -606,7 +636,7 @@ void TrunkLink::handleMsgTrunkAudio(std::istream& is) } uint32_t tg = msg.tg(); - if ((!isSharedTG(tg) && !m_reflector->isClusterTG(tg)) || msg.audio().empty()) + if ((!isOwnedTG(tg) && !m_reflector->isClusterTG(tg)) || msg.audio().empty()) { return; } @@ -637,7 +667,7 @@ void TrunkLink::handleMsgTrunkFlush(std::istream& is) } uint32_t tg = msg.tg(); - if (!isSharedTG(tg) && !m_reflector->isClusterTG(tg)) + if (!isOwnedTG(tg) && !m_reflector->isClusterTG(tg)) { return; } diff --git a/src/svxlink/reflector/TrunkLink.h b/src/svxlink/reflector/TrunkLink.h index 56e97a0..36d0f0f 100644 --- a/src/svxlink/reflector/TrunkLink.h +++ b/src/svxlink/reflector/TrunkLink.h @@ -169,6 +169,7 @@ class TrunkLink : public sigc::trackable bool isActive(void) const; bool isOutboundReady(void) const; bool isInboundReady(void) const; + bool isOwnedTG(uint32_t tg) const; void onConnected(void); void onDisconnected(Async::TcpConnection* con, diff --git a/tests/generate_configs.py b/tests/generate_configs.py new file mode 100644 index 0000000..1953d25 --- /dev/null +++ b/tests/generate_configs.py @@ -0,0 +1,149 @@ +#!/usr/bin/env python3 +"""Generate test config files and docker-compose.test.yml from topology.py. + +Run: python3 generate_configs.py +""" + +import os +import topology as T + + +def generate_reflector_conf(name: str) -> str: + """Generate svxreflector.conf content for one reflector.""" + r = T.REFLECTORS[name] + peers = {n: v for n, v in T.REFLECTORS.items() if n != name} + + cluster_str = ",".join(str(tg) for tg in T.CLUSTER_TGS) + + lines = [ + "[GLOBAL]", + 'TIMESTAMP_FORMAT="%c"', + f"LISTEN_PORT={T.INTERNAL_CLIENT_PORT}", + "TG_FOR_V1_CLIENTS=999", + f"LOCAL_PREFIX={T.prefix_str(r['prefix'])}", + f"CLUSTER_TGS={cluster_str}", + f"TRUNK_LISTEN_PORT={T.INTERNAL_TRUNK_PORT}", + f"HTTP_SRV_PORT={T.INTERNAL_HTTP_PORT}", + "", + "[SERVER_CERT]", + f"COMMON_NAME={T.service_name(name)}", + "", + "[USERS]", + ] + groups_seen = set() + for client in T.TEST_CLIENTS: + lines.append(f"{client['callsign']}={client['group']}") + groups_seen.add(client['group']) + lines += [ + "", + "[PASSWORDS]", + ] + for client in T.TEST_CLIENTS: + if client['group'] in groups_seen: + lines.append(f"{client['group']}={client['password']}") + groups_seen.discard(client['group']) + + # Trunk sections for each peer reflector + for peer_name in sorted(peers): + peer = peers[peer_name] + section = T.trunk_section_name(peer_name) + secret = T.trunk_secret(name, peer_name) + lines += [ + "", + f"[{section}]", + f"HOST={T.service_name(peer_name)}", + f"PORT={T.INTERNAL_TRUNK_PORT}", + f"SECRET={secret}", + f"REMOTE_PREFIX={T.prefix_str(peer['prefix'])}", + ] + + # Test harness trunk sections + lines += [ + "", + "[TRUNK_TEST]", + "HOST=192.0.2.1", + "PORT=59302", + f"SECRET={T.TEST_PEER['secret']}", + f"REMOTE_PREFIX={T.prefix_str(T.TEST_PEER['prefix'])}", + "", + "[TRUNK_TEST_RX]", + "HOST=192.0.2.1", + "PORT=59303", + f"SECRET={T.TEST_PEER_RX['secret']}", + f"REMOTE_PREFIX={T.prefix_str(T.TEST_PEER_RX['prefix'])}", + "", + ] + + return "\n".join(lines) + + +def generate_docker_compose() -> str: + """Generate docker-compose.test.yml content.""" + services = [] + volumes = [] + + for name in sorted(T.REFLECTORS): + svc = T.service_name(name) + client_port = T.mapped_client_port(name) + trunk_port = T.mapped_trunk_port(name) + http_port = T.mapped_http_port(name) + vol = f"pki-{name}" + volumes.append(vol) + + services.append(f""" {svc}: + build: + context: .. + dockerfile: Dockerfile + volumes: + - ./configs/{svc}.conf:/etc/svxlink/svxreflector.conf:ro + - {vol}:/var/lib/svxlink/pki + ports: + - "{client_port}:{T.INTERNAL_CLIENT_PORT}/tcp" + - "{client_port}:{T.INTERNAL_CLIENT_PORT}/udp" + - "{trunk_port}:{T.INTERNAL_TRUNK_PORT}/tcp" + - "{http_port}:{T.INTERNAL_HTTP_PORT}/tcp" + healthcheck: + test: ["CMD-SHELL", "bash -c '(echo > /dev/tcp/localhost/{T.INTERNAL_HTTP_PORT}) 2>/dev/null'"] + interval: 2s + timeout: 2s + retries: 15 + networks: + - trunk_mesh""") + + services_block = "\n\n".join(services) + volumes_block = "\n".join(f" {v}:" for v in volumes) + + return f"""services: +{services_block} + +volumes: +{volumes_block} + +networks: + trunk_mesh: + driver: bridge +""" + + +def main(): + script_dir = os.path.dirname(os.path.abspath(__file__)) + configs_dir = os.path.join(script_dir, "configs") + os.makedirs(configs_dir, exist_ok=True) + + # Generate per-reflector configs + for name in sorted(T.REFLECTORS): + path = os.path.join(configs_dir, f"{T.service_name(name)}.conf") + content = generate_reflector_conf(name) + with open(path, "w") as f: + f.write(content) + print(f" wrote {os.path.relpath(path, script_dir)}") + + # Generate docker-compose + compose_path = os.path.join(script_dir, "docker-compose.test.yml") + with open(compose_path, "w") as f: + f.write(generate_docker_compose()) + print(f" wrote docker-compose.test.yml") + + +if __name__ == "__main__": + main() diff --git a/tests/run_tests.sh b/tests/run_tests.sh new file mode 100755 index 0000000..777b729 --- /dev/null +++ b/tests/run_tests.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")" + +cleanup() { + echo "=== Tearing down test environment ===" + docker compose -f docker-compose.test.yml down -v --remove-orphans 2>/dev/null +} +trap cleanup EXIT + +echo "=== Generating configs from topology.py ===" +python3 generate_configs.py + +echo "=== Building and starting reflector mesh ===" +docker compose -f docker-compose.test.yml up -d --build --wait + +echo "=== Running integration tests ===" +python3 test_trunk.py diff --git a/tests/test_trunk.py b/tests/test_trunk.py new file mode 100644 index 0000000..ec84cb8 --- /dev/null +++ b/tests/test_trunk.py @@ -0,0 +1,1161 @@ +#!/usr/bin/env python3 +"""Integration tests for GeuReflector trunk protocol. + +Connects as a fake trunk peer via the TRUNK_TEST config section and exercises +routing, audio relay, cluster TG broadcast, and heartbeat. + +Topology (prefixes, ports, secrets) is defined in topology.py — edit there +and run `python3 generate_configs.py` to regenerate configs + compose file. + +Requires: Python 3.7+, stdlib only (+ topology.py from this directory). +""" + +import hashlib +import hmac +import json +import os +import socket +import struct +import sys +import threading +import time +import unittest +from urllib.request import urlopen +from urllib.error import URLError + +# Ensure tests/ is on the path so topology can be imported +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import topology as T + +# --------------------------------------------------------------------------- +# Derived endpoints from topology +# --------------------------------------------------------------------------- +HOST = "127.0.0.1" + +def _trunk(name): + return (HOST, T.mapped_trunk_port(name)) + +def _http(name): + return (HOST, T.mapped_http_port(name)) + +REFLECTOR_NAMES = sorted(T.REFLECTORS) + +TEST_SECRET = T.TEST_PEER["secret"] +TEST_PREFIX = T.prefix_str(T.TEST_PEER["prefix"]) # comma-separated for handshake +TEST_PREFIX_FIRST = T.first_prefix(T.TEST_PEER["prefix"]) # first prefix for TG generation + +TEST_RX_SECRET = T.TEST_PEER_RX["secret"] +TEST_RX_PREFIX = T.prefix_str(T.TEST_PEER_RX["prefix"]) + +# V2 client auth credentials (must match [USERS]/[PASSWORDS] in configs) +CLIENT_CALLSIGN = T.TEST_CLIENTS[0]["callsign"] +CLIENT_PASSWORD = T.TEST_CLIENTS[0]["password"] +CLIENT2_CALLSIGN = T.TEST_CLIENTS[1]["callsign"] +CLIENT2_PASSWORD = T.TEST_CLIENTS[1]["password"] + +# Trunk message types +MSG_TRUNK_HELLO = 115 +MSG_TRUNK_TALKER_START = 116 +MSG_TRUNK_TALKER_STOP = 117 +MSG_TRUNK_AUDIO = 118 +MSG_TRUNK_FLUSH = 119 +MSG_TRUNK_HEARTBEAT = 120 + +# --------------------------------------------------------------------------- +# Wire format helpers +# --------------------------------------------------------------------------- + +def pack_string(s: str) -> bytes: + b = s.encode("utf-8") + return struct.pack("!H", len(b)) + b + + +def unpack_string(data: bytes, offset: int): + length = struct.unpack_from("!H", data, offset)[0] + s = data[offset + 2 : offset + 2 + length].decode("utf-8") + return s, offset + 2 + length + + +def pack_vec_u8(v: bytes) -> bytes: + return struct.pack("!H", len(v)) + v + + +def unpack_vec_u8(data: bytes, offset: int): + count = struct.unpack_from("!H", data, offset)[0] + return data[offset + 2 : offset + 2 + count], offset + 2 + count + + +def send_frame(sock, payload: bytes): + sock.sendall(struct.pack("!I", len(payload)) + payload) + + +def recv_exact(sock, n: int) -> bytes: + buf = b"" + while len(buf) < n: + chunk = sock.recv(n - len(buf)) + if not chunk: + raise ConnectionError("connection closed") + buf += chunk + return buf + + +def recv_frame(sock) -> bytes: + hdr = recv_exact(sock, 4) + length = struct.unpack("!I", hdr)[0] + return recv_exact(sock, length) + + +# --------------------------------------------------------------------------- +# Message builders +# --------------------------------------------------------------------------- + +def build_trunk_hello(trunk_id: str, local_prefix: str, priority: int, + secret: str, role: int = 0) -> bytes: + nonce = os.urandom(20) + digest = hmac.new(secret.encode(), nonce, hashlib.sha1).digest() + payload = struct.pack("!H", MSG_TRUNK_HELLO) + payload += pack_string(trunk_id) + payload += pack_string(local_prefix) + payload += struct.pack("!I", priority) + payload += pack_vec_u8(nonce) + payload += pack_vec_u8(digest) + payload += struct.pack("!B", role) + return payload + + +def build_talker_start(tg: int, callsign: str) -> bytes: + payload = struct.pack("!H", MSG_TRUNK_TALKER_START) + payload += struct.pack("!I", tg) + payload += pack_string(callsign) + return payload + + +def build_talker_stop(tg: int) -> bytes: + payload = struct.pack("!H", MSG_TRUNK_TALKER_STOP) + payload += struct.pack("!I", tg) + return payload + + +def build_audio(tg: int, audio: bytes) -> bytes: + payload = struct.pack("!H", MSG_TRUNK_AUDIO) + payload += struct.pack("!I", tg) + payload += pack_vec_u8(audio) + return payload + + +def build_flush(tg: int) -> bytes: + payload = struct.pack("!H", MSG_TRUNK_FLUSH) + payload += struct.pack("!I", tg) + return payload + + +def build_heartbeat() -> bytes: + return struct.pack("!H", MSG_TRUNK_HEARTBEAT) + + +# --------------------------------------------------------------------------- +# Message parser +# --------------------------------------------------------------------------- + +def parse_msg(data: bytes): + """Parse a frame payload into (type, fields_dict).""" + msg_type = struct.unpack_from("!H", data, 0)[0] + off = 2 + + if msg_type == MSG_TRUNK_HELLO: + trunk_id, off = unpack_string(data, off) + prefix, off = unpack_string(data, off) + priority = struct.unpack_from("!I", data, off)[0]; off += 4 + nonce, off = unpack_vec_u8(data, off) + digest, off = unpack_vec_u8(data, off) + role = struct.unpack_from("!B", data, off)[0]; off += 1 + return msg_type, { + "id": trunk_id, "local_prefix": prefix, "priority": priority, + "nonce": nonce, "digest": digest, "role": role, + } + elif msg_type == MSG_TRUNK_TALKER_START: + tg = struct.unpack_from("!I", data, off)[0]; off += 4 + callsign, off = unpack_string(data, off) + return msg_type, {"tg": tg, "callsign": callsign} + elif msg_type == MSG_TRUNK_TALKER_STOP: + tg = struct.unpack_from("!I", data, off)[0]; off += 4 + return msg_type, {"tg": tg} + elif msg_type == MSG_TRUNK_AUDIO: + tg = struct.unpack_from("!I", data, off)[0]; off += 4 + audio, off = unpack_vec_u8(data, off) + return msg_type, {"tg": tg, "audio": audio} + elif msg_type == MSG_TRUNK_FLUSH: + tg = struct.unpack_from("!I", data, off)[0]; off += 4 + return msg_type, {"tg": tg} + elif msg_type == MSG_TRUNK_HEARTBEAT: + return msg_type, {} + else: + return msg_type, {"raw": data[off:]} + + +# --------------------------------------------------------------------------- +# TrunkPeer — fake trunk peer +# --------------------------------------------------------------------------- + +class TrunkPeer: + def __init__(self): + self.sock = None + + def connect(self, host: str, port: int, timeout: float = 5.0): + self.sock = socket.create_connection((host, port), timeout=timeout) + self.sock.settimeout(timeout) + + def handshake(self, trunk_id: str = "TEST_PEER", local_prefix: str = TEST_PREFIX, + priority: int = None, secret: str = TEST_SECRET): + if priority is None: + priority = struct.unpack("!I", os.urandom(4))[0] + hello = build_trunk_hello(trunk_id, local_prefix, priority, secret) + send_frame(self.sock, hello) + # Wait for reflector's hello back + data = recv_frame(self.sock) + return parse_msg(data) + + def send_talker_start(self, tg: int, callsign: str): + send_frame(self.sock, build_talker_start(tg, callsign)) + + def send_talker_stop(self, tg: int): + send_frame(self.sock, build_talker_stop(tg)) + + def send_audio(self, tg: int, audio: bytes): + send_frame(self.sock, build_audio(tg, audio)) + + def send_flush(self, tg: int): + send_frame(self.sock, build_flush(tg)) + + def send_heartbeat(self): + send_frame(self.sock, build_heartbeat()) + + def recv_msg(self, timeout: float = 5.0): + old = self.sock.gettimeout() + self.sock.settimeout(timeout) + try: + data = recv_frame(self.sock) + return parse_msg(data) + finally: + self.sock.settimeout(old) + + def close(self): + if self.sock: + try: + self.sock.close() + except OSError: + pass + self.sock = None + + +# --------------------------------------------------------------------------- +# ClientPeer — fake V2 SvxLink client (no SSL) +# --------------------------------------------------------------------------- + +# Client TCP message types +MSG_PROTO_VER = 5 +MSG_AUTH_CHALLENGE = 10 +MSG_AUTH_RESPONSE = 11 +MSG_AUTH_OK = 12 +MSG_SERVER_INFO = 100 +MSG_SELECT_TG = 106 + +# Client UDP message types +UDP_HEARTBEAT = 1 +UDP_AUDIO = 101 +UDP_FLUSH = 102 + + +class ClientPeer: + """Minimal V2 SvxLink client — TCP auth + UDP audio receive.""" + + def __init__(self): + self.tcp = None + self.udp = None + self.client_id = None + self._udp_seq = 0 + self._drain_thread = None + self._drain_stop = threading.Event() + + def connect(self, host: str, tcp_port: int, timeout: float = 5.0): + self.tcp = socket.create_connection((host, tcp_port), timeout=timeout) + self.tcp.settimeout(timeout) + self._host = host + self._tcp_port = tcp_port + + def authenticate(self, callsign: str = CLIENT_CALLSIGN, + password: str = CLIENT_PASSWORD): + """V2 auth: ProtoVer → AuthChallenge → AuthResponse → AuthOk + ServerInfo.""" + # Send MsgProtoVer(2, 0) + payload = struct.pack("!H", MSG_PROTO_VER) + payload += struct.pack("!HH", 2, 0) + send_frame(self.tcp, payload) + + # Receive MsgAuthChallenge + data = recv_frame(self.tcp) + msg_type = struct.unpack_from("!H", data, 0)[0] + assert msg_type == MSG_AUTH_CHALLENGE, f"expected AuthChallenge, got {msg_type}" + challenge, _ = unpack_vec_u8(data, 2) + + # Send MsgAuthResponse + digest = hmac.new(password.encode(), challenge, hashlib.sha1).digest() + payload = struct.pack("!H", MSG_AUTH_RESPONSE) + payload += pack_string(callsign) + payload += pack_vec_u8(digest) + send_frame(self.tcp, payload) + + # Receive MsgAuthOk + data = recv_frame(self.tcp) + msg_type = struct.unpack_from("!H", data, 0)[0] + assert msg_type == MSG_AUTH_OK, f"expected AuthOk, got {msg_type}" + + # Receive MsgServerInfo — extract client_id + data = recv_frame(self.tcp) + msg_type = struct.unpack_from("!H", data, 0)[0] + assert msg_type == MSG_SERVER_INFO, f"expected ServerInfo, got {msg_type}" + _reserved = struct.unpack_from("!H", data, 2)[0] + self.client_id = struct.unpack_from("!H", data, 4)[0] + + # Drain any additional server messages (MsgNodeList etc.) + self.tcp.settimeout(0.5) + try: + while True: + recv_frame(self.tcp) + except (socket.timeout, ConnectionError, OSError): + pass + self.tcp.settimeout(5.0) + + # Start background thread to drain TCP messages (heartbeats, + # talker notifications, etc.) so the TCP buffer doesn't fill up. + self._drain_stop.clear() + self._drain_thread = threading.Thread( + target=self._tcp_drain_loop, daemon=True) + self._drain_thread.start() + + def setup_udp(self, udp_port: int = None): + """Open UDP socket and register with reflector via heartbeat.""" + if udp_port is None: + udp_port = self._tcp_port # same port for client protocol + self.udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.udp.settimeout(5.0) + self._udp_target = (self._host, udp_port) + + # Send UDP heartbeat to register + self._send_udp_heartbeat() + + # Wait for heartbeat echo + data, _ = self.udp.recvfrom(4096) + hdr_type = struct.unpack_from("!H", data, 0)[0] + assert hdr_type == UDP_HEARTBEAT, f"expected UDP heartbeat, got {hdr_type}" + + def select_tg(self, tg: int): + """Send MsgSelectTG to monitor a talk group.""" + payload = struct.pack("!H", MSG_SELECT_TG) + payload += struct.pack("!I", tg) + send_frame(self.tcp, payload) + + def recv_udp(self, timeout: float = 3.0): + """Receive one UDP datagram, return (type, payload_bytes).""" + self.udp.settimeout(timeout) + data, _ = self.udp.recvfrom(65536) + # V2 header: [type(2)][client_id(2)][seq(2)] + msg_type = struct.unpack_from("!H", data, 0)[0] + return msg_type, data[6:] + + def recv_udp_all(self, timeout: float = 2.0): + """Drain all pending UDP datagrams, return list of (type, payload).""" + msgs = [] + self.udp.settimeout(timeout) + try: + while True: + msg_type, payload = self.recv_udp(timeout) + msgs.append((msg_type, payload)) + except (socket.timeout, ConnectionError, OSError): + pass + return msgs + + def send_udp_audio(self, audio: bytes): + """Send a UDP audio frame (V2 format: header + audio_data vec).""" + body = pack_vec_u8(audio) + pkt = struct.pack("!HHH", UDP_AUDIO, self.client_id, self._udp_seq) + body + self._udp_seq = (self._udp_seq + 1) & 0xFFFF + self.udp.sendto(pkt, self._udp_target) + + def send_udp_flush(self): + """Send a UDP flush (end of audio stream).""" + pkt = struct.pack("!HHH", UDP_FLUSH, self.client_id, self._udp_seq) + self._udp_seq = (self._udp_seq + 1) & 0xFFFF + self.udp.sendto(pkt, self._udp_target) + + def _send_udp_heartbeat(self): + pkt = struct.pack("!HHH", UDP_HEARTBEAT, self.client_id, self._udp_seq) + self._udp_seq = (self._udp_seq + 1) & 0xFFFF + self.udp.sendto(pkt, self._udp_target) + + def _tcp_drain_loop(self): + """Background: read and discard TCP frames to keep connection alive.""" + while not self._drain_stop.is_set(): + try: + if self.tcp is None: + break + self.tcp.settimeout(0.5) + recv_frame(self.tcp) + except (socket.timeout, ConnectionError, OSError): + pass + + def close(self): + self._drain_stop.set() + if self._drain_thread: + self._drain_thread.join(timeout=2.0) + self._drain_thread = None + for s in (self.tcp, self.udp): + if s: + try: + s.close() + except OSError: + pass + self.tcp = None + self.udp = None + + +# --------------------------------------------------------------------------- +# HTTP status helpers +# --------------------------------------------------------------------------- + +def get_status(host: str, port: int, timeout: float = 3.0) -> dict: + url = f"http://{host}:{port}/status" + with urlopen(url, timeout=timeout) as resp: + return json.loads(resp.read()) + + +def get_config(host: str, port: int, timeout: float = 3.0) -> dict: + url = f"http://{host}:{port}/config" + with urlopen(url, timeout=timeout) as resp: + return json.loads(resp.read()) + + +def wait_until(predicate, timeout: float = 10.0, interval: float = 0.2, + msg: str = "condition not met"): + """Poll predicate() until it returns True or timeout expires.""" + deadline = time.monotonic() + timeout + last_exc = None + while time.monotonic() < deadline: + try: + if predicate(): + return + except Exception as e: + last_exc = e + time.sleep(interval) + detail = f" (last error: {last_exc})" if last_exc else "" + raise AssertionError(f"Timed out: {msg}{detail}") + + +def wait_for_reflector(host: str, port: int, timeout: float = 60.0): + """Wait until the reflector's /status endpoint responds.""" + def check(): + try: + get_status(host, port, timeout=2.0) + return True + except (URLError, OSError, json.JSONDecodeError): + return False + wait_until(check, timeout=timeout, interval=1.0, + msg=f"reflector at {host}:{port} not ready") + + +def wait_for_trunk_connected(host: str, port: int, trunk_name: str, + timeout: float = 30.0): + """Wait until a specific trunk link shows connected in /status.""" + def check(): + status = get_status(host, port) + trunks = status.get("trunks", {}) + return trunks.get(trunk_name, {}).get("connected", False) + wait_until(check, timeout=timeout, + msg=f"trunk {trunk_name} on {host}:{port} not connected") + + +# --------------------------------------------------------------------------- +# Test cases +# --------------------------------------------------------------------------- + +class TestTrunkIntegration(unittest.TestCase): + + # First reflector in sorted order is the "primary" target for tests + PRIMARY = REFLECTOR_NAMES[0] + + @classmethod + def setUpClass(cls): + """Wait for all reflectors to be healthy and mesh to form.""" + D = "\033[2m" + G = "\033[32m" + RST = "\033[0m" + + sys.stderr.write(f" {D}waiting for reflectors...{RST}") + sys.stderr.flush() + for name in REFLECTOR_NAMES: + wait_for_reflector(*_http(name), timeout=90.0) + sys.stderr.write(f"\r\033[K {G}\u2714{RST} Reflectors up\n") + + sys.stderr.write(f" {D}waiting for mesh...{RST}") + sys.stderr.flush() + for name in REFLECTOR_NAMES: + for peer in REFLECTOR_NAMES: + if peer != name: + wait_for_trunk_connected( + *_http(name), T.trunk_section_name(peer), timeout=30.0) + labels = "\u2194".join(n.upper() for n in REFLECTOR_NAMES) + sys.stderr.write(f"\r\033[K {G}\u2714{RST} Mesh connected ({labels})\n") + sys.stderr.write(f"\033[2m{'─' * 50}\033[0m\n") + + def _connect_peer(self, target=None, **kwargs): + """Helper: create and handshake a TrunkPeer to a reflector.""" + if target is None: + target = self.PRIMARY + peer = TrunkPeer() + peer.connect(*_trunk(target)) + msg_type, fields = peer.handshake(**kwargs) + self.assertEqual(msg_type, MSG_TRUNK_HELLO) + return peer, fields + + def _get_test_talkers(self, name=None): + """Get active_talkers dict from the TRUNK_TEST link on a reflector.""" + if name is None: + name = self.PRIMARY + status = get_status(*_http(name)) + return status["trunks"].get("TRUNK_TEST", {}).get("active_talkers", {}) + + # ------------------------------------------------------------------ + # Test 1: Mesh connectivity (no harness needed) + # ------------------------------------------------------------------ + def test_01_mesh_connectivity(self): + """All trunk links in the mesh are connected.""" + for name in REFLECTOR_NAMES: + status = get_status(*_http(name)) + for peer in REFLECTOR_NAMES: + if peer == name: + continue + section = T.trunk_section_name(peer) + self.assertTrue( + status["trunks"][section]["connected"], + f"{section} not connected on reflector-{name}") + + # ------------------------------------------------------------------ + # Test 2: Successful handshake + # ------------------------------------------------------------------ + def test_02_handshake_success(self): + """Connect as test peer and receive hello back with correct prefix.""" + peer, fields = self._connect_peer() + try: + received = set(p.strip() for p in fields["local_prefix"].split(",")) + expected = set(T.prefix_list(T.REFLECTORS[self.PRIMARY]["prefix"])) + self.assertEqual(received, expected) + self.assertGreater(fields["priority"], 0) + finally: + peer.close() + + # ------------------------------------------------------------------ + # Test 3: Bad secret rejected + # ------------------------------------------------------------------ + def test_03_handshake_bad_secret(self): + """Connection with wrong secret is dropped.""" + peer = TrunkPeer() + peer.connect(*_trunk(self.PRIMARY)) + hello = build_trunk_hello("BAD_PEER", TEST_PREFIX, 12345, "wrong_secret") + send_frame(peer.sock, hello) + with self.assertRaises((ConnectionError, OSError, struct.error)): + peer.sock.settimeout(5.0) + recv_frame(peer.sock) + peer.close() + + # ------------------------------------------------------------------ + # Test 4: Talker start/stop visible in /status + # ------------------------------------------------------------------ + def test_04_talker_start_stop(self): + """TalkerStart appears in /status, TalkerStop clears it.""" + peer, _ = self._connect_peer() + try: + tg = int(TEST_PREFIX_FIRST + "001") + peer.send_talker_start(tg, "TEST1") + + wait_until(lambda: self._get_test_talkers().get(str(tg)) == "TEST1", + timeout=5.0, msg="TalkerStart not visible in /status") + + peer.send_talker_stop(tg) + + wait_until(lambda: str(tg) not in self._get_test_talkers(), + timeout=5.0, msg="TalkerStop not cleared in /status") + finally: + peer.close() + + # ------------------------------------------------------------------ + # Test 5: Audio relay (talker lifecycle) + # ------------------------------------------------------------------ + def test_05_audio_relay(self): + """Full talker lifecycle: start, audio, flush, stop.""" + peer, _ = self._connect_peer() + try: + tg = int(TEST_PREFIX_FIRST + "002") + peer.send_talker_start(tg, "AUDIO_TEST") + + wait_until(lambda: self._get_test_talkers().get(str(tg)) == "AUDIO_TEST", + timeout=5.0, msg="talker not visible") + + for _ in range(5): + peer.send_audio(tg, b"\x00" * 160) + + peer.send_flush(tg) + peer.send_talker_stop(tg) + + wait_until(lambda: str(tg) not in self._get_test_talkers(), + timeout=5.0, msg="talker not cleared after stop") + finally: + peer.close() + + # ------------------------------------------------------------------ + # Test 6: All cluster TGs accepted regardless of prefix overlap + # ------------------------------------------------------------------ + def test_06_cluster_tgs_accepted(self): + """Every configured cluster TG is accepted via trunk. + + Tests cluster TGs with different prefix relationships: + - no prefix match (not owned by any reflector) + - prefix overlap (starts with a reflector's prefix) + - test-peer prefix (starts with the harness prefix) + + Trunk talker events are NOT re-forwarded to other peers (prevents + loops). The receiving reflector must accept and record each locally. + """ + peer, _ = self._connect_peer() + try: + # Start a talker on every configured cluster TG + for tg in T.CLUSTER_TGS: + peer.send_talker_start(tg, f"CLUSTER_{tg}") + + # Verify all appear on the primary reflector's TRUNK_TEST link + def all_visible(): + talkers = self._get_test_talkers() + return all( + talkers.get(str(tg)) == f"CLUSTER_{tg}" + for tg in T.CLUSTER_TGS + ) + + wait_until(all_visible, timeout=5.0, + msg="not all cluster TGs visible on primary reflector") + + # Verify NOT propagated to other peers (no trunk-to-trunk forwarding) + time.sleep(1.0) + primary_section = T.trunk_section_name(self.PRIMARY) + for name in REFLECTOR_NAMES: + if name == self.PRIMARY: + continue + status = get_status(*_http(name)) + talkers = status["trunks"].get(primary_section, {}).get("active_talkers", {}) + for tg in T.CLUSTER_TGS: + self.assertNotIn(str(tg), talkers, + f"cluster TG {tg} should NOT propagate trunk-to-trunk " + f"(found on reflector-{name})") + + # Stop all and verify cleared + for tg in T.CLUSTER_TGS: + peer.send_talker_stop(tg) + + def all_cleared(): + talkers = self._get_test_talkers() + return all(str(tg) not in talkers for tg in T.CLUSTER_TGS) + + wait_until(all_cleared, timeout=5.0, + msg="not all cluster TGs cleared") + finally: + peer.close() + + # ------------------------------------------------------------------ + # Test 7: Heartbeat keepalive + # ------------------------------------------------------------------ + def test_07_heartbeat(self): + """Sending heartbeats keeps the connection alive.""" + peer, _ = self._connect_peer() + try: + for _ in range(3): + peer.send_heartbeat() + time.sleep(1) + + tg = int(TEST_PREFIX_FIRST + "003") + peer.send_talker_start(tg, "HB_TEST") + + wait_until(lambda: self._get_test_talkers().get(str(tg)) == "HB_TEST", + timeout=5.0, msg="connection died despite heartbeats") + + peer.send_talker_stop(tg) + finally: + peer.close() + + # ------------------------------------------------------------------ + # Test 8: Disconnect cleanup + # ------------------------------------------------------------------ + def test_08_disconnect_cleanup(self): + """Abrupt disconnect clears the talker from /status.""" + peer, _ = self._connect_peer() + tg = int(TEST_PREFIX_FIRST + "004") + peer.send_talker_start(tg, "DISCONNECT_TEST") + + wait_until(lambda: self._get_test_talkers().get(str(tg)) == "DISCONNECT_TEST", + timeout=5.0, msg="talker not visible before disconnect") + + # Abrupt close (no TalkerStop sent) + peer.sock.close() + peer.sock = None + + wait_until(lambda: str(tg) not in self._get_test_talkers(), + timeout=10.0, msg="talker not cleared after disconnect") + + # ------------------------------------------------------------------ + # Test 9: Audio not forwarded trunk-to-trunk (routing correctness) + # ------------------------------------------------------------------ + def test_09_no_trunk_to_trunk_audio(self): + """Audio from one trunk peer is NOT forwarded to another. + + By design, trunk-received audio only goes to local clients and + satellites — never to other trunk peers (loop prevention). + A second harness (TRUNK_TEST_RX) verifies it receives no audio + or talker frames, while /status confirms the sender's audio was + actually processed by the reflector. + """ + # Sender: connect as TRUNK_TEST (prefix 9) + sender, _ = self._connect_peer() + + # Receiver: connect as TRUNK_TEST_RX (prefix 8) + receiver = TrunkPeer() + receiver.connect(*_trunk(self.PRIMARY)) + rx_type, rx_fields = receiver.handshake( + trunk_id="TEST_RX", local_prefix=TEST_RX_PREFIX, + secret=TEST_RX_SECRET) + self.assertEqual(rx_type, MSG_TRUNK_HELLO) + + try: + tg = T.CLUSTER_TGS[0] # cluster TG accepted from any peer + audio_payload = b"\xAA\xBB" * 80 # distinctive pattern + + # Sender: full audio lifecycle + sender.send_talker_start(tg, "AUDIO_SRC") + + wait_until(lambda: self._get_test_talkers().get(str(tg)) == "AUDIO_SRC", + timeout=5.0, msg="sender talker not visible") + + for _ in range(5): + sender.send_audio(tg, audio_payload) + sender.send_flush(tg) + sender.send_talker_stop(tg) + + wait_until(lambda: str(tg) not in self._get_test_talkers(), + timeout=5.0, msg="sender talker not cleared") + + # Receiver: drain all pending frames — should only see heartbeats, + # never TalkerStart, Audio, or Flush from the sender's session. + audio_leaked = False + talker_leaked = False + receiver.sock.settimeout(2.0) + try: + while True: + data = recv_frame(receiver.sock) + msg_type, _ = parse_msg(data) + if msg_type == MSG_TRUNK_HEARTBEAT: + continue + if msg_type in (MSG_TRUNK_TALKER_START, MSG_TRUNK_TALKER_STOP): + talker_leaked = True + if msg_type in (MSG_TRUNK_AUDIO, MSG_TRUNK_FLUSH): + audio_leaked = True + except (socket.timeout, ConnectionError, OSError): + pass # expected — no more frames + + self.assertFalse(talker_leaked, + "trunk peer received TalkerStart/Stop from another trunk peer " + "(trunk-to-trunk forwarding should not happen)") + self.assertFalse(audio_leaked, + "trunk peer received Audio/Flush from another trunk peer " + "(trunk-to-trunk forwarding should not happen)") + + finally: + sender.close() + receiver.close() + + # ------------------------------------------------------------------ + # Test 10: Audio delivered to V2 client (end-to-end) + # ------------------------------------------------------------------ + def test_10_audio_delivered_to_client(self): + """Trunk audio reaches a V2 client connected to the same reflector. + + A trunk harness sends audio on a cluster TG. A V2 client (no SSL) + authenticated on the same reflector, monitoring that TG, must receive + the audio as UDP datagrams — proving end-to-end delivery. + """ + # Connect V2 client to reflector-a + client = ClientPeer() + client.connect(*_trunk(self.PRIMARY)) # same host, but need CLIENT port + client.close() # wrong port — need the client port + + client = ClientPeer() + client_port = T.mapped_client_port(self.PRIMARY) + client.connect(HOST, client_port) + client.authenticate() + client.setup_udp(udp_port=client_port) + + tg = T.CLUSTER_TGS[0] + client.select_tg(tg) + # Small delay for TG selection to take effect + time.sleep(0.3) + + # Connect trunk harness and send audio + sender, _ = self._connect_peer() + try: + audio_payload = b"\xDE\xAD" * 80 # distinctive 160-byte pattern + + sender.send_talker_start(tg, "E2E_TEST") + + wait_until(lambda: self._get_test_talkers().get(str(tg)) == "E2E_TEST", + timeout=5.0, msg="talker not visible") + + for _ in range(3): + sender.send_audio(tg, audio_payload) + sender.send_flush(tg) + sender.send_talker_stop(tg) + + # Client should receive UDP audio + flush + msgs = client.recv_udp_all(timeout=3.0) + audio_count = sum(1 for t, _ in msgs if t == UDP_AUDIO) + flush_count = sum(1 for t, _ in msgs if t == UDP_FLUSH) + + self.assertGreater(audio_count, 0, + "V2 client received no audio frames from trunk sender") + self.assertGreater(flush_count, 0, + "V2 client received no flush after trunk audio") + + finally: + sender.close() + client.close() + + # ------------------------------------------------------------------ + # Test 11: Cross-reflector audio delivery + # ------------------------------------------------------------------ + def test_11_cross_reflector_audio(self): + """Audio from a client on reflector-a reaches a client on reflector-b. + + A V2 client on reflector-a talks on a TG owned by reflector-b. + Reflector-a forwards via trunk. A V2 client on reflector-b + monitoring that TG must receive the audio as UDP datagrams. + """ + # Pick a TG owned by a DIFFERENT reflector + other = [n for n in REFLECTOR_NAMES if n != self.PRIMARY][0] + other_prefix = T.first_prefix(T.REFLECTORS[other]["prefix"]) + tg = int(other_prefix + "001") + + # Receiver: V2 client on the OTHER reflector, monitoring the TG + receiver = ClientPeer() + rx_port = T.mapped_client_port(other) + receiver.connect(HOST, rx_port) + receiver.authenticate(callsign=CLIENT_CALLSIGN, password=CLIENT_PASSWORD) + receiver.setup_udp(udp_port=rx_port) + receiver.select_tg(tg) + time.sleep(0.3) + + # Sender: trunk harness on the PRIMARY reflector, acting as a + # local talker (prefix matches primary → sent to the other via trunk) + # We use the trunk harness because it reliably sustains audio. + # The trunk harness sends TalkerStart for a TG matching the other + # reflector's prefix — the primary reflector forwards it via TRUNK. + # + # But wait: the trunk harness has prefix 9, and TG tg starts with + # other_prefix. The primary's TRUNK link to 'other' will forward + # if isSharedTG matches. Since the harness sends TalkerStart on + # the primary reflector, and the primary's onLocalTalkerStart... + # Actually, the harness is a TRUNK peer, not a local client. + # Trunk TalkerStart from harness → setTrunkTalkerForTG → does NOT + # re-forward to other trunks. + # + # So we must use a V2 client as sender to trigger the local→trunk path. + sender = ClientPeer() + tx_port = T.mapped_client_port(self.PRIMARY) + sender.connect(HOST, tx_port) + sender.authenticate(callsign=CLIENT2_CALLSIGN, password=CLIENT2_PASSWORD) + sender.setup_udp(udp_port=tx_port) + sender.select_tg(tg) + time.sleep(0.3) + + try: + # Send audio — even 1 frame reaching the reflector triggers + # the local talker → trunk forwarding → remote client path + for _ in range(5): + sender.send_udp_audio(b"\xBE\xEF" * 80) + time.sleep(0.02) + + # Give time for trunk propagation + time.sleep(1.0) + + # Receiver should have gotten at least some audio or flush + msgs = receiver.recv_udp_all(timeout=2.0) + audio_count = sum(1 for t, _ in msgs if t == UDP_AUDIO) + flush_count = sum(1 for t, _ in msgs if t == UDP_FLUSH) + + self.assertGreater(audio_count + flush_count, 0, + f"V2 client on reflector-{other} received no audio/flush " + f"for TG {tg} sent from reflector-{self.PRIMARY}") + + finally: + sender.close() + receiver.close() + + +# --------------------------------------------------------------------------- +# Custom test runner with clean, readable output +# --------------------------------------------------------------------------- + +class TrunkTestResult(unittest.TestResult): + BOLD = "\033[1m" + GREEN = "\033[32m" + RED = "\033[31m" + YELLOW = "\033[33m" + DIM = "\033[2m" + RESET = "\033[0m" + + def __init__(self, stream, verbosity=2): + super().__init__(stream, False, verbosity) + self.stream = stream + self.start_time = None + self.test_times = [] + + def startTest(self, test): + super().startTest(test) + self.start_time = time.monotonic() + # Print test name while running + label = test.shortDescription() or test._testMethodName + self.stream.write(f" {self.DIM}running{self.RESET} {label}") + self.stream.flush() + + def _finish(self, symbol, color, test, elapsed): + label = test.shortDescription() or test._testMethodName + self.test_times.append((label, elapsed)) + # Clear line and rewrite with result + self.stream.write(f"\r\033[K {color}{symbol}{self.RESET} {label}") + if elapsed >= 1.0: + self.stream.write(f" {self.DIM}({elapsed:.1f}s){self.RESET}") + self.stream.write("\n") + self.stream.flush() + + def addSuccess(self, test): + super().addSuccess(test) + self._finish("\u2714", self.GREEN, test, time.monotonic() - self.start_time) + + def addFailure(self, test, err): + super().addFailure(test, err) + elapsed = time.monotonic() - self.start_time + self._finish("\u2718", self.RED, test, elapsed) + + def addError(self, test, err): + super().addError(test, err) + elapsed = time.monotonic() - self.start_time + self._finish("!", self.RED, test, elapsed) + + def addSkip(self, test, reason): + super().addSkip(test, reason) + elapsed = time.monotonic() - self.start_time + self._finish("-", self.YELLOW, test, elapsed) + + +class TrunkTestRunner: + def __init__(self, stream=None): + self.stream = stream or __import__("sys").stderr + + def run(self, test): + B = TrunkTestResult.BOLD + G = TrunkTestResult.GREEN + R = TrunkTestResult.RED + D = TrunkTestResult.DIM + RST = TrunkTestResult.RESET + + result = TrunkTestResult(self.stream) + suite_start = time.monotonic() + + self.stream.write(f"\n{B}Trunk Protocol Integration Tests{RST}\n") + self.stream.write(f"{D}{'─' * 50}{RST}\n") + + test(result) + elapsed = time.monotonic() - suite_start + + self.stream.write(f"{D}{'─' * 50}{RST}\n") + + passed = result.testsRun - len(result.failures) - len(result.errors) + total = result.testsRun + + if result.wasSuccessful(): + self.stream.write( + f"{G}{B}{passed}/{total} passed{RST} {D}({elapsed:.1f}s){RST}\n\n") + else: + self.stream.write( + f"{R}{B}{passed}/{total} passed, " + f"{len(result.failures)} failed, " + f"{len(result.errors)} errors{RST} " + f"{D}({elapsed:.1f}s){RST}\n\n") + + for test_case, traceback in result.failures + result.errors: + label = test_case.shortDescription() or test_case._testMethodName + self.stream.write(f"{R}{B}FAIL: {label}{RST}\n") + # Show only the last relevant line of the traceback + lines = traceback.strip().splitlines() + for line in lines: + self.stream.write(f" {D}{line}{RST}\n") + self.stream.write("\n") + + return result + + +def docker_log_lines(service: str, since_lines: int = 50) -> list: + """Get recent docker compose log lines for a service.""" + import subprocess + result = subprocess.run( + ["docker", "compose", "-f", "docker-compose.test.yml", + "logs", "--tail", str(since_lines), service], + capture_output=True, text=True, timeout=5) + return result.stdout.strip().splitlines() + + +def interactive_loop(): + """Let user manually test TGs against the running mesh via a V2 client. + + Connects a V2 client, sends audio to trigger the talker, then checks + docker logs on each reflector for routing evidence. + """ + B = "\033[1m" + G = "\033[32m" + R = "\033[31m" + D = "\033[2m" + RST = "\033[0m" + + target = REFLECTOR_NAMES[0] + + print(f"\n{B}Interactive TG tester{RST} {D}(mesh is still running){RST}") + print() + print(f" {B}Topology:{RST}") + for name in REFLECTOR_NAMES: + pfx = T.prefix_str(T.REFLECTORS[name]["prefix"]) + marker = " \u25c0 client connects here" if name == target else "" + print(f" reflector-{name} {B}prefix={pfx}{RST}{D}{marker}{RST}") + cluster = ", ".join(str(tg) for tg in T.CLUSTER_TGS) + print(f" {D}cluster TGs: {B}{cluster}{RST}") + print() + print(f" {D}Connects as V2 client ({CLIENT_CALLSIGN}) to reflector-{target}.") + print(f" Any TG can be tested — routing forwards to the owning reflector.") + print(f" Routing is verified via reflector logs (not /status polling).") + print(f" Type 'q' or press Ctrl-C to exit{RST}\n") + + while True: + try: + raw = input(f"{B}TG number{RST} {D}(q to quit):{RST} ").strip() + except (EOFError, KeyboardInterrupt): + print() + break + + if not raw or raw.lower() == "q": + break + + try: + tg = int(raw) + except ValueError: + print(f" {R}not a valid number{RST}") + continue + + client = ClientPeer() + try: + client_port = T.mapped_client_port(target) + client.connect(HOST, client_port) + client.authenticate() + client.setup_udp(udp_port=client_port) + client.select_tg(tg) + time.sleep(0.3) + + # Send audio to trigger the talker + for _ in range(5): + client.send_udp_audio(b"\x00" * 160) + time.sleep(0.02) + + # Give the reflector time to process and forward + time.sleep(0.5) + client.close() + client = None + time.sleep(0.3) + + # Check docker logs on each reflector for routing evidence + tg_str = str(tg) + print(f" {D}logs for TG {B}{tg}{RST}{D}:{RST}") + found_local = False + found_trunk = False + + for name in REFLECTOR_NAMES: + svc = T.service_name(name) + lines = docker_log_lines(svc, since_lines=30) + + events = [] + for line in lines: + if f"TG #{tg_str}" not in line: + continue + # Extract the event part after the container prefix + parts = line.split("|", 1) + msg = parts[-1].strip() if len(parts) > 1 else line.strip() + + if "Talker start" in msg and "Trunk" not in msg: + events.append(f"{G}local talker start{RST}") + found_local = True + elif "Talker stop" in msg and "Trunk" not in msg: + events.append(f"{D}local talker stop{RST}") + elif "Trunk talker start" in msg: + events.append(f"{G}trunk talker start{RST}") + found_trunk = True + elif "Trunk talker stop" in msg: + events.append(f"{D}trunk talker stop{RST}") + + if events: + print(f" reflector-{name}: {', '.join(events)}") + else: + print(f" reflector-{name}: {D}(no events){RST}") + + # Summary + if found_local and found_trunk: + print(f" {G}\u2714{RST} TG {B}{tg}{RST} routed via trunk") + elif found_local and not found_trunk: + target_prefixes = T.prefix_list(T.REFLECTORS[target]["prefix"]) + is_local = any(tg_str.startswith(p) for p in target_prefixes) + if is_local: + print(f" {G}\u2714{RST} TG {B}{tg}{RST} is local to reflector-{target}") + else: + all_prefixes = [] + for n in REFLECTOR_NAMES: + all_prefixes.extend(T.prefix_list(T.REFLECTORS[n]["prefix"])) + has_owner = any(tg_str.startswith(p) for p in all_prefixes) + if has_owner: + print(f" {R}\u2718{RST} TG {B}{tg}{RST} talker started locally " + f"but trunk forwarding not seen in logs") + else: + print(f" {D}\u2714{RST} TG {B}{tg}{RST} talker started locally " + f"{D}(no remote prefix owns this TG){RST}") + elif not found_local: + print(f" {R}\u2718{RST} TG {B}{tg}{RST} no talker start seen " + f"{D}(audio may not have reached the reflector){RST}") + + except Exception as e: + print(f" {R}error: {e}{RST}") + finally: + if client is not None: + client.close() + + print() + + +if __name__ == "__main__": + runner = TrunkTestRunner() + suite = unittest.TestLoader().loadTestsFromTestCase(TestTrunkIntegration) + result = runner.run(suite) + + if not result.wasSuccessful(): + raise SystemExit(1) + + try: + interactive_loop() + except Exception: + pass diff --git a/tests/topology.py b/tests/topology.py new file mode 100644 index 0000000..a2bda63 --- /dev/null +++ b/tests/topology.py @@ -0,0 +1,92 @@ +"""Single source of truth for the test mesh topology. + +Edit REFLECTORS and TEST_PEER below, then run `python3 generate_configs.py` +to regenerate configs/ and docker-compose.test.yml. The test harness +(test_trunk.py) imports this module at runtime so everything stays in sync. +""" + +# --------------------------------------------------------------------------- +# Mesh topology — edit these to change prefixes / add reflectors +# --------------------------------------------------------------------------- + +REFLECTORS = { + # name prefixes (list) host-port-base + "a": {"prefix": ["120", "2", "3"], "trunk_port_base": 15000}, + "b": {"prefix": ["40", "5"], "trunk_port_base": 25000}, + "c": {"prefix": ["6"], "trunk_port_base": 35000}, +} + +# Fake trunk peers used by the test harness. +# TEST_PEER: primary sender (connects as TRUNK_TEST) +# TEST_PEER_RX: passive receiver for audio routing verification (TRUNK_TEST_RX) +TEST_PEER = { + "prefix": ["9"], + "secret": "test_secret", +} + +TEST_PEER_RX = { + "prefix": ["8"], + "secret": "test_secret_rx", +} + +# Cluster TGs — forwarded to all peers regardless of prefix ownership +# 8000: no prefix match (not owned by any reflector) +# 1201: starts with prefix "120" (overlaps with reflector-a's ownership) +# 9999: starts with test peer prefix "9" +CLUSTER_TGS = [8000, 1201, 9999] + +# V2 test client credentials (added to [USERS]/[PASSWORDS] in every config) +TEST_CLIENTS = [ + {"callsign": "N0TEST", "group": "TestGroup", "password": "testpass"}, + {"callsign": "N0SEND", "group": "TestGroup", "password": "testpass"}, +] + +# Shared secret between each pair: sorted tuple of names → secret +# Auto-generated from the pair of reflector names for simplicity +def trunk_secret(name_a: str, name_b: str) -> str: + pair = tuple(sorted([name_a, name_b])) + return f"secret_{pair[0]}{pair[1]}" + +# --------------------------------------------------------------------------- +# Helpers for prefix handling +# --------------------------------------------------------------------------- + +def prefix_str(prefixes) -> str: + """Join a prefix list into the comma-separated config format.""" + if isinstance(prefixes, str): + return prefixes + return ",".join(prefixes) + +def prefix_list(prefixes) -> list: + """Normalize to a list.""" + if isinstance(prefixes, str): + return [p.strip() for p in prefixes.split(",") if p.strip()] + return list(prefixes) + +def first_prefix(prefixes) -> str: + """Return the first prefix (used for generating test TG numbers).""" + return prefix_list(prefixes)[0] + +# --------------------------------------------------------------------------- +# Derived constants used by test_trunk.py and generate_configs.py +# --------------------------------------------------------------------------- + +# Internal ports inside Docker (fixed) +INTERNAL_CLIENT_PORT = 5300 +INTERNAL_TRUNK_PORT = 5302 +INTERNAL_HTTP_PORT = 8080 + +def mapped_trunk_port(name: str) -> int: + return REFLECTORS[name]["trunk_port_base"] + 302 + +def mapped_http_port(name: str) -> int: + return REFLECTORS[name]["trunk_port_base"] + 3080 + +def mapped_client_port(name: str) -> int: + return REFLECTORS[name]["trunk_port_base"] + 300 + +def service_name(name: str) -> str: + return f"reflector-{name}" + +def trunk_section_name(peer_name: str) -> str: + return f"TRUNK_{peer_name.upper()}"