Files
ZephCore/tests/unit/forwarder_policy_tests.cpp
Rastislav VysokyandClaude Opus 5.5 f0bce0b335 forwarder: two-radio role that forwards between networks, never repeats
CONFIG_ZEPHCORE_ROLE_FORWARDER, gated on the lora1 alias. One node
identity on two radios: a packet heard on one side may go out on the
other, never back on its own, if the fwd rules allow it.

- RepeaterMesh takes its node state from RepeaterShared: ACL, regions,
  transport keys, flood filter, twin guard, failed-login limiter, time
  sync, neighbours. Side b uses side a's failed-datagram budget.
- Crossing: the repeater's allowPacketForward() checks, then, through
  roleAllowsForward() and before anything is recorded, the direct
  hash-collision guard and the fwd rules (ForwarderRules, default deny).
- Per side: name, radio, TX power, timing, LBT and cad.ceiling, RX gain
  and duty cycle, adverts, send queue, contention. Side b's prefs in
  prefs_b.json; side a's are authoritative for shared fields at boot.
- CLI: per-radio .a/.b/.peer suffixes, `fwd` for the rules.
- RAK4631 + RAK13300 conf and overlay; compile-only CI build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 11:16:17 +01:00

136 lines
7.8 KiB
C++

#include "test.h"
#include <ForwarderPolicy.h>
#include <string.h>
#include <string>
using namespace mesh;
using forwarder::CliRoute;
namespace {
struct Routed { CliRoute::Kind kind; uint8_t side; std::string line, err; };
Routed route(const char* in, bool remote, uint8_t local = forwarder::SIDE_A) {
char line[128]; strcpy(line, in);
char err[96];
CliRoute r = forwarder::routeCliLine(line, remote, local, err, sizeof(err));
return { r.kind, r.side, line, r.kind == CliRoute::ERROR ? r.error : "" };
}
}
TEST(fwd_cli_remote, "UNIT-FWDP-001", "Over LoRa: unsuffixed is the local side, .peer the other, .a/.b absolute") {
auto r = route("set freq 869.618", true, forwarder::SIDE_B);
CHECK(r.kind == CliRoute::LOCAL && r.side == forwarder::SIDE_B && r.line == "set freq 869.618");
r = route("set freq.peer 869.432", true, forwarder::SIDE_B);
CHECK(r.kind == CliRoute::SIDE && r.side == forwarder::SIDE_A && r.line == "set freq 869.432");
r = route("get name.b", true, forwarder::SIDE_A);
CHECK(r.kind == CliRoute::SIDE && r.side == forwarder::SIDE_B && r.line == "get name");
r = route("set name.a My Node A", true, forwarder::SIDE_B);
CHECK(r.kind == CliRoute::SIDE && r.side == forwarder::SIDE_A && r.line == "set name My Node A");
r = route("tempradio.peer 869.4 62.5 7 5 10", true, forwarder::SIDE_A);
CHECK(r.kind == CliRoute::SIDE && r.side == forwarder::SIDE_B && r.line == "tempradio 869.4 62.5 7 5 10");
r = route("advert.zerohop.peer", true, forwarder::SIDE_A);
CHECK(r.kind == CliRoute::SIDE && r.side == forwarder::SIDE_B && r.line == "advert.zerohop");
}
TEST(fwd_cli_serial, "UNIT-FWDP-002", "Over serial: per-radio keys need .a/.b; adverts go out on both") {
auto r = route("get freq", false);
CHECK(r.kind == CliRoute::ERROR && r.err == "Err - freq is per radio, use freq.a or freq.b");
r = route("set radio.b 869.618,62.5,8,5", false);
CHECK(r.kind == CliRoute::SIDE && r.side == forwarder::SIDE_B && r.line == "set radio 869.618,62.5,8,5");
r = route("get name.peer", false);
CHECK(r.kind == CliRoute::ERROR && r.err == "Err - .peer is for remote admin; use name.a or name.b");
r = route("tempradio 869 62.5 7 5 10", false);
CHECK(r.kind == CliRoute::ERROR);
CHECK(route("advert", false).kind == CliRoute::BOTH);
CHECK(route("advert.zerohop", false).kind == CliRoute::BOTH);
r = route("advert.a", false);
CHECK(r.kind == CliRoute::SIDE && r.side == forwarder::SIDE_A && r.line == "advert");
CHECK(route("advert", true).kind == CliRoute::LOCAL);
}
TEST(fwd_cli_shared, "UNIT-FWDP-003", "Shared keys run where they arrive and refuse a suffix") {
auto r = route("set password hunter2", false);
CHECK(r.kind == CliRoute::LOCAL && r.line == "set password hunter2");
CHECK(route("neighbors", true).kind == CliRoute::LOCAL);
CHECK(route("fwd add both allow route=direct", false).kind == CliRoute::LOCAL);
r = route("set password.b x", true);
CHECK(r.kind == CliRoute::ERROR && r.err == "Err - password is shared by both radios, no suffix");
r = route("get radio.rxgain", false); // a per-radio key that starts like another
CHECK(r.kind == CliRoute::ERROR && r.err == "Err - radio.rxgain is per radio, use radio.rxgain.a or radio.rxgain.b");
CHECK(route("clear stats", false).kind == CliRoute::BOTH);
CHECK(route("clear stats", true).kind == CliRoute::BOTH);
CHECK(route("", false).kind == CliRoute::LOCAL);
}
TEST(fwd_prefs_split, "UNIT-FWDP-004", "copySharedPrefs keeps the radio's own fields and takes the node's") {
NodePrefs a, b;
memset(&a, 0, sizeof(a)); memset(&b, 0, sizeof(b));
strcpy(a.node_name, "SK side"); a.freq = 869.618f; a.sf = 8; a.bw = 62.5f; a.cr = 5;
a.tx_power_dbm = 22; a.advert_interval = 60; a.path_hash_mode = 1; a.cad_offset = 3; a.cad_ceiling = 15;
strcpy(a.password, "secret"); strcpy(a.owner_info, "OM3KFF"); a.loop_detect = 2; a.flood_max = 9;
a.node_lat = 48.1; a.node_lon = 17.1;
strcpy(b.node_name, "CZ side"); b.freq = 869.432f; b.sf = 7; b.bw = 62.5f; b.cr = 5;
b.tx_power_dbm = 14; b.advert_interval = 0; b.path_hash_mode = 0; b.cad_offset = -2; b.cad_ceiling = 30;
strcpy(b.password, "old");
forwarder::copySharedPrefs(b, a);
CHECK(strcmp(b.node_name, "CZ side") == 0 && b.freq == 869.432f && b.sf == 7);
CHECK(b.tx_power_dbm == 14 && b.advert_interval == 0 && b.path_hash_mode == 0 && b.cad_offset == -2 && b.cad_ceiling == 30);
CHECK(strcmp(b.password, "secret") == 0 && strcmp(b.owner_info, "OM3KFF") == 0);
CHECK(b.loop_detect == 2 && b.flood_max == 9 && b.node_lat == 48.1 && b.node_lon == 17.1);
}
TEST(fwd_next_hop, "UNIT-FWDP-005", "The hop after this node, for plain direct and TRACE packets") {
Packet p;
p.header = ROUTE_TYPE_DIRECT | (PAYLOAD_TYPE_TXT_MSG << 2);
p.setPathHashSizeAndCount(2, 3);
const uint8_t path[] = {0xAA, 0x01, 0xBB, 0x02, 0xCC, 0x03};
memcpy(p.path, path, sizeof(path)); p.payload_len = 10;
const uint8_t* h; uint8_t len;
CHECK(forwarder::nextHopAfterSelf(&p, &h, &len) && len == 2 && h[0] == 0xBB && h[1] == 0x02);
p.setPathHashSizeAndCount(2, 1);
CHECK(!forwarder::nextHopAfterSelf(&p, &h, &len)); // we are the last hop
p.header = ROUTE_TYPE_FLOOD | (PAYLOAD_TYPE_TXT_MSG << 2);
p.setPathHashSizeAndCount(2, 3);
CHECK(!forwarder::nextHopAfterSelf(&p, &h, &len));
Packet t;
t.header = ROUTE_TYPE_DIRECT | (PAYLOAD_TYPE_TRACE << 2);
t.path_len = 1; // one SNR gathered: ours is entry 1
const uint8_t trace[] = {1, 2, 3, 4, 5, 6, 7, 8, 0x00, 0x11, 0x22, 0x33};
memcpy(t.payload, trace, sizeof(trace)); t.payload_len = sizeof(trace);
CHECK(forwarder::nextHopAfterSelf(&t, &h, &len) && len == 1 && h[0] == 0x33);
t.path_len = 2; // ours is the last entry
CHECK(!forwarder::nextHopAfterSelf(&t, &h, &len));
t.path_len = 0; t.payload[8] = 0x01; // 2-byte entries: ours 0x1122, next 0x3344
t.payload[12] = 0x44; t.payload_len = 13;
CHECK(forwarder::nextHopAfterSelf(&t, &h, &len) && len == 2 && h[0] == 0x33 && h[1] == 0x44);
t.payload_len = 12; // next entry cut short
CHECK(!forwarder::nextHopAfterSelf(&t, &h, &len));
}
TEST(fwd_direct_collision, "UNIT-FWDP-006", "A direct next hop heard only on the arrival side is a hash collision") {
const uint8_t A = 1u << forwarder::SIDE_A, B = 1u << forwarder::SIDE_B;
CHECK(forwarder::isDirectCollision(A, forwarder::SIDE_A));
CHECK(forwarder::isDirectCollision(B, forwarder::SIDE_B));
CHECK(!forwarder::isDirectCollision(A | B, forwarder::SIDE_A)); // the other side has one too: may be ours
CHECK(!forwarder::isDirectCollision(A | B, forwarder::SIDE_B));
CHECK(!forwarder::isDirectCollision(B, forwarder::SIDE_A)); // only beyond us: carry it
CHECK(!forwarder::isDirectCollision(0, forwarder::SIDE_A)); // unknown next hop: the rules decide
}
TEST(fwd_tx_hold, "UNIT-FWDP-007", "TX holds for the other radio's reception, at most max_ms per reception") {
uint32_t start = 0;
CHECK(!forwarder::holdForOtherRadio(false, 1000, 300, &start) && start == 0);
CHECK(forwarder::holdForOtherRadio(true, 1000, 300, &start) && start == 1000);
CHECK(forwarder::holdForOtherRadio(true, 1299, 300, &start));
CHECK(!forwarder::holdForOtherRadio(true, 1300, 300, &start)); // bound reached
CHECK(!forwarder::holdForOtherRadio(true, 5000, 300, &start)); // no new hold while the same one lasts
CHECK(!forwarder::holdForOtherRadio(false, 5001, 300, &start) && start == 0);
CHECK(forwarder::holdForOtherRadio(true, 5002, 300, &start)); // the next reception holds again
start = 0; // the clock at 0 and across its wrap
CHECK(forwarder::holdForOtherRadio(true, 0, 300, &start));
start = 0;
CHECK(forwarder::holdForOtherRadio(true, 0xFFFFFF00u, 300, &start));
CHECK(forwarder::holdForOtherRadio(true, 0x00000020u, 300, &start));
CHECK(!forwarder::holdForOtherRadio(true, 0x00000030u, 300, &start));
}