#include "test.h" #include #include #include #include using namespace mesh; namespace { // Keys are the name's first bytes; a packet is "in" a region or channel when // its first payload byte equals the key's first byte. struct FakeMatcher : ForwarderRules::Matcher { std::vector hashed; void hashtagKey(const char* name, uint8_t key[ForwarderRules::KEY_SIZE]) override { hashed.push_back(name); memset(key, 0, ForwarderRules::KEY_SIZE); memcpy(key, name, strlen(name) < 16 ? strlen(name) : 16); } bool inRegion(const Packet* pkt, const ForwarderRules::Rule& r) override { return pkt->hasTransportCodes() && r.region_keyed && pkt->transport_codes[0] == r.region_key[1]; } bool fromChannel(const Packet* pkt, const uint8_t key[ForwarderRules::KEY_SIZE]) override { return pkt->payload_len > 0 && pkt->payload[0] == key[1]; } }; Packet pkt(uint8_t route, uint8_t type, uint8_t hops = 0, uint8_t first = 0, uint16_t code = 0) { Packet p; p.header = route | (type << 2); p.setPathHashSizeAndCount(1, hops); p.transport_codes[0] = code; p.transport_codes[1] = 0; p.payload[0] = first; p.payload_len = 20; return p; } bool run(ForwarderRules& f, FakeMatcher& m, const char* line, std::string* reply = nullptr, bool* changed = nullptr) { char r[256] = {0}; bool c = false; bool handled = f.command(line, r, 161, &c, m); if (reply) *reply = r; if (changed) *changed = c; return handled; } const char* const PRODUCTION_FILTER[] = { "fwd add both allow route=direct", "fwd add both allow region=#cs", "fwd add both allow type=grp_txt channel=#cs", "fwd add both allow type=grp_txt channel=#hamradio", }; } TEST(fwd_rules_default, "UNIT-FWD-001", "No rules: default deny, counted on the default") { ForwarderRules f; FakeMatcher m; Packet p = pkt(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_ADVERT); CHECK(!f.allows(ForwarderRules::DIR_A_TO_B, &p, m)); CHECK(f.defaultHits() == 1); CHECK(run(f, m, "fwd default allow")); CHECK(f.allows(ForwarderRules::DIR_B_TO_A, &p, m) && f.defaultHits() == 2); } TEST(fwd_rules_production, "UNIT-FWD-002", "The meshcore-forwarder production filter, expressed as fwd rules") { ForwarderRules f; FakeMatcher m; for (auto line : PRODUCTION_FILTER) CHECK(run(f, m, line)); CHECK(f.count() == 4 && !f.defaultAllow()); CHECK(m.hashed == std::vector({"#cs", "#cs", "#hamradio"})); const uint8_t AB = ForwarderRules::DIR_A_TO_B; Packet direct = pkt(ROUTE_TYPE_DIRECT, PAYLOAD_TYPE_TXT_MSG, 3); Packet in_cs = pkt(ROUTE_TYPE_TRANSPORT_FLOOD, PAYLOAD_TYPE_ADVERT, 2, 0, 'c'); Packet other_region = pkt(ROUTE_TYPE_TRANSPORT_FLOOD, PAYLOAD_TYPE_ADVERT, 2, 0, 'x'); Packet cs_chat = pkt(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_GRP_TXT, 4, 'c'); Packet ham_chat = pkt(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_GRP_TXT, 4, 'h'); Packet public_chat = pkt(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_GRP_TXT, 4, 'p'); Packet cs_data = pkt(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_GRP_DATA, 4, 'c'); Packet advert = pkt(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_ADVERT, 1); CHECK(f.allows(AB, &direct, m) && f.allows(AB, &in_cs, m)); CHECK(f.allows(AB, &cs_chat, m) && f.allows(AB, &ham_chat, m)); CHECK(!f.allows(AB, &other_region, m) && !f.allows(AB, &public_chat, m)); CHECK(!f.allows(AB, &cs_data, m) && !f.allows(AB, &advert, m)); CHECK(f.hits(0) == 1 && f.hits(1) == 1 && f.hits(2) == 1 && f.hits(3) == 1 && f.defaultHits() == 4); } TEST(fwd_rules_order, "UNIT-FWD-003", "First match wins; ins places a rule ahead; directions are separate") { ForwarderRules f; FakeMatcher m; CHECK(run(f, m, "fwd add both allow route=flood")); CHECK(run(f, m, "fwd ins 1 a>b deny type=advert")); Packet advert = pkt(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_ADVERT); CHECK(!f.allows(ForwarderRules::DIR_A_TO_B, &advert, m)); CHECK(f.allows(ForwarderRules::DIR_B_TO_A, &advert, m)); CHECK(f.match(ForwarderRules::DIR_A_TO_B, &advert, m) == 0); CHECK(f.match(ForwarderRules::DIR_B_TO_A, &advert, m) == 1); CHECK(run(f, m, "fwd del 1")); CHECK(f.allows(ForwarderRules::DIR_A_TO_B, &advert, m) && f.count() == 1); } TEST(fwd_rules_route_hops, "UNIT-FWD-004", "route= classes and hops<= use the hop count as received") { ForwarderRules f; FakeMatcher m; CHECK(run(f, m, "fwd add both allow route=uflood hops<=3")); CHECK(run(f, m, "fwd add both allow route=tflood,direct")); const uint8_t AB = ForwarderRules::DIR_A_TO_B; Packet u3 = pkt(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_ADVERT, 3), u4 = pkt(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_ADVERT, 4); Packet t9 = pkt(ROUTE_TYPE_TRANSPORT_FLOOD, PAYLOAD_TYPE_ADVERT, 9); Packet td = pkt(ROUTE_TYPE_TRANSPORT_DIRECT, PAYLOAD_TYPE_REQ, 2); CHECK(f.match(AB, &u3, m) == 0 && f.match(AB, &u4, m) == -1); CHECK(f.match(AB, &t9, m) == 1 && f.match(AB, &td, m) == 1); } TEST(fwd_rules_channel_types, "UNIT-FWD-005", "channel= matches group packets only, by key") { ForwarderRules f; FakeMatcher m; CHECK(run(f, m, "fwd add both allow channel=8b3387e9c5cdea6ac9e5edbaa115cd72")); CHECK(m.hashed.empty()); const uint8_t AB = ForwarderRules::DIR_A_TO_B; Packet grp = pkt(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_GRP_DATA, 1, 0x33); // key[1] = 0x33 Packet txt = pkt(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_TXT_MSG, 1, 0x33); Packet other = pkt(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_GRP_TXT, 1, 0x34); CHECK(f.match(AB, &grp, m) == 0 && f.match(AB, &txt, m) == -1 && f.match(AB, &other, m) == -1); std::string reply; CHECK(run(f, m, "fwd list", &reply)); CHECK(reply == "1:both allow channel=8b3387e9\ndefault deny"); } TEST(fwd_rules_format_roundtrip, "UNIT-FWD-006", "Every rule reads back as the text that makes it") { FakeMatcher m; const char* lines[] = { "both allow route=direct", "a>b deny route=uflood type=advert,grp_data hops<=7", "both allow route=tflood,direct", "b>a allow route=flood region=#cs", "both allow type=grp_txt channel=#hamradio", "a>b allow region=Bratislava", "both deny region=$private", }; for (auto line : lines) { char text[96]; strcpy(text, line); ForwarderRules::Rule r; const char* err = ""; CHECK(ForwarderRules::parseRule(text, &r, m, &err)); char out[96]; ForwarderRules::formatRule(r, out, sizeof(out)); CHECK(std::string(out) == line); } FakeMatcher regions; char bare[] = "both allow region=cs", priv[] = "both allow region=$cs"; ForwarderRules::Rule rb, rp; const char* e = ""; CHECK(ForwarderRules::parseRule(bare, &rb, regions, &e) && rb.region_keyed); CHECK(ForwarderRules::parseRule(priv, &rp, regions, &e) && !rp.region_keyed); CHECK(regions.hashed == std::vector({"#cs"})); // a bare name is its hashtag char both[] = "both allow route=uflood,tflood"; // the two floods are "flood" ForwarderRules::Rule r; const char* err = ""; CHECK(ForwarderRules::parseRule(both, &r, m, &err) && r.routes == ForwarderRules::ROUTE_FLOOD); } TEST(fwd_rules_parse_errors, "UNIT-FWD-007", "Malformed rules are refused and change nothing") { ForwarderRules f; FakeMatcher m; const char* bad[] = { "fwd add", "fwd add sideways allow", "fwd add both maybe", "fwd add both allow type=bogus", "fwd add both allow route=", "fwd add both allow hops<=64", "fwd add both allow channel=abc", "fwd add both allow region=#a-name-that-is-longer-than-31-chars", "fwd add both allow color=red", "fwd ins 3 both allow", "fwd del 1", "fwd default maybe", "fwd lists", }; for (auto line : bad) { std::string reply; bool changed = true; CHECK(run(f, m, line, &reply, &changed)); CHECK(!changed && reply.rfind("Err", 0) == 0); } CHECK(f.count() == 0); CHECK(!run(f, m, "fwdx")); CHECK(!run(f, m, "filter rpt")); } TEST(fwd_rules_full, "UNIT-FWD-008", "The list holds MAX_RULES rules and refuses one more") { ForwarderRules f; FakeMatcher m; for (int i = 0; i < ForwarderRules::MAX_RULES; i++) CHECK(run(f, m, "fwd add both allow route=direct")); std::string reply; bool changed = true; CHECK(run(f, m, "fwd add both allow route=direct", &reply, &changed)); CHECK(!changed && reply.rfind("Err - rule list full", 0) == 0); CHECK(run(f, m, "fwd list 16", &reply)); CHECK(reply == "16:both allow route=direct\ndefault deny"); } TEST(fwd_rules_storage, "UNIT-FWD-009", "save/load round trip; a bad blob loads as no rules") { ForwarderRules f; FakeMatcher m; for (auto line : PRODUCTION_FILTER) CHECK(run(f, m, line)); CHECK(run(f, m, "fwd add a>b deny type=advert hops<=2")); CHECK(run(f, m, "fwd default allow")); uint8_t blob[ForwarderRules::BLOB_MAX]; size_t n = f.save(blob, sizeof(blob)); CHECK(n == 4 + 5 * ForwarderRules::RULE_BLOB); CHECK(f.save(blob, n - 1) == 0); ForwarderRules g; CHECK(g.load(blob, n)); CHECK(g.count() == 5 && g.defaultAllow()); for (int i = 0; i < 5; i++) { char a[96], b[96]; ForwarderRules::formatRule(f.rule(i), a, sizeof(a)); ForwarderRules::formatRule(g.rule(i), b, sizeof(b)); CHECK(std::string(a) == b); CHECK(memcmp(f.rule(i).channel_key, g.rule(i).channel_key, 16) == 0); CHECK(memcmp(f.rule(i).region_key, g.rule(i).region_key, 16) == 0); } CHECK(!g.load(blob, n - 1) && g.count() == 0 && !g.defaultAllow()); blob[1] = 99; CHECK(!g.load(blob, n)); blob[1] = 1; blob[4] = 7; CHECK(!g.load(blob, n)); // first rule: bad direction } TEST(fwd_rules_changed, "UNIT-FWD-010", "changed is set only when the persisted rules change") { ForwarderRules f; FakeMatcher m; bool changed = true; CHECK(run(f, m, "fwd", nullptr, &changed) && !changed); CHECK(run(f, m, "fwd default deny", nullptr, &changed) && !changed); CHECK(run(f, m, "fwd default allow", nullptr, &changed) && changed); CHECK(run(f, m, "fwd add both deny type=advert", nullptr, &changed) && changed); Packet advert = pkt(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_ADVERT); f.allows(ForwarderRules::DIR_A_TO_B, &advert, m); std::string reply; CHECK(run(f, m, "fwd stats", &reply, &changed) && !changed); CHECK(reply == "1:1\ndefault:0"); CHECK(run(f, m, "fwd clear", &reply, &changed) && changed && f.count() == 0 && f.defaultAllow()); CHECK(run(f, m, "fwd clear", nullptr, &changed) && !changed); }