Fix trunk receive-side TG filtering and add integration test suite
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.
This commit is contained in:
1 parent
3b7831c307
commit
685d7253ff
12 files changed
+1501
-16
No files matched your search
@@ -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/
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+2
-1
@@ -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`.
|
||||
|
||||
+13
-7
@@ -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 |
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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()
|
||||
Executable
+18
@@ -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
|
||||
+1161
File diff suppressed because it is too large.
Load diff
@@ -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()}"
|
||||
Reference in new issue
Block a user