mirror of
https://github.com/liquidraver/ZephCore.git
synced 2026-12-06 08:44:22 +00:00
139 lines
5.9 KiB
C++
139 lines
5.9 KiB
C++
#include "test.h"
|
|
#include <TwinGuard.h>
|
|
#include <string.h>
|
|
using namespace mesh;
|
|
|
|
/* A real pair from the station logs (2026-11-14 11:21 UTC, TXT_MSG): the
|
|
* twin's last byte went 77 -> 47, 2 bits. */
|
|
static const uint8_t ORIG[20] = {0x71, 0x49, 0x68, 0x83, 0x8D, 0x84, 0x0E, 0x13, 0x71, 0x67,
|
|
0x90, 0xE4, 0x0C, 0x61, 0xEB, 0x77, 0x32, 0xB9, 0xA3, 0x77};
|
|
|
|
static Packet pkt(const uint8_t* payload, int len, uint8_t type = PAYLOAD_TYPE_TXT_MSG,
|
|
uint8_t route = ROUTE_TYPE_FLOOD) {
|
|
Packet p;
|
|
p.header = route | (type << 2);
|
|
p.setPathHashSizeAndCount(2, 0);
|
|
memcpy(p.payload, payload, len);
|
|
p.payload_len = len;
|
|
return p;
|
|
}
|
|
static Packet twinOf(const uint8_t* payload, int len, int flip_bits, uint8_t type = PAYLOAD_TYPE_TXT_MSG) {
|
|
Packet p = pkt(payload, len, type);
|
|
for (int i = 0; i < flip_bits; i++) p.payload[len - 1 - i / 8] ^= (uint8_t)(1u << (i % 8));
|
|
return p;
|
|
}
|
|
static int bitsOf(const TwinGuard& g, Packet p, uint32_t t) { return g.twinBits(&p, t); }
|
|
/* Forwarded once, then heard again from a neighbour: support 2. */
|
|
static void forwardHeardTwice(TwinGuard& g, const Packet& a, uint32_t t) {
|
|
g.noteReception(&a, false, t);
|
|
g.noteForward(&a, false, t);
|
|
g.noteReception(&a, false, t + 100);
|
|
}
|
|
|
|
TEST(twin_real_pair, "UNIT-TWIN-001", "A last-byte twin is refused once the forwarded copy was heard twice") {
|
|
TwinGuard g;
|
|
Packet a = pkt(ORIG, 20);
|
|
Packet b = pkt(ORIG, 20);
|
|
b.payload[19] = 0x47;
|
|
g.noteReception(&a, false, 0);
|
|
g.noteForward(&a, false, 0);
|
|
CHECK(g.twinBits(&b, 50) == 0); /* heard once: could be our own damaged copy */
|
|
g.noteReception(&a, false, 100);
|
|
CHECK(g.twinBits(&b, 200) == 2);
|
|
}
|
|
|
|
TEST(twin_echo_no_support, "UNIT-TWIN-002", "Copies that came back through this node add no support") {
|
|
TwinGuard g;
|
|
Packet a = pkt(ORIG, 20);
|
|
g.noteForward(&a, false, 0);
|
|
for (int i = 0; i < 3; i++) g.noteReception(&a, true, 100 + i);
|
|
CHECK(bitsOf(g, twinOf(ORIG, 20, 1), 500) == 0);
|
|
TwinGuard h; /* forwarded a copy that already held our hash */
|
|
h.noteForward(&a, true, 0);
|
|
h.noteReception(&a, false, 100);
|
|
CHECK(bitsOf(h, twinOf(ORIG, 20, 1), 200) == 0);
|
|
h.noteReception(&a, false, 150);
|
|
CHECK(bitsOf(h, twinOf(ORIG, 20, 1), 200) == 1);
|
|
}
|
|
|
|
TEST(twin_window, "UNIT-TWIN-003", "Only forwards from the last 60 s count, across an uptime wrap") {
|
|
TwinGuard g;
|
|
Packet a = pkt(ORIG, 20);
|
|
forwardHeardTwice(g, a, 1000);
|
|
CHECK(bitsOf(g, twinOf(ORIG, 20, 1), 1000 + TwinGuard::WINDOW_MS - 1) == 1);
|
|
CHECK(bitsOf(g, twinOf(ORIG, 20, 1), 1000 + TwinGuard::WINDOW_MS) == 0);
|
|
TwinGuard w;
|
|
forwardHeardTwice(w, a, 0xFFFFFF00u);
|
|
CHECK(bitsOf(w, twinOf(ORIG, 20, 1), 0x00000100u) == 1);
|
|
}
|
|
|
|
TEST(twin_distance, "UNIT-TWIN-004", "1 to 24 bits apart is a twin; 0 and 25 are not") {
|
|
TwinGuard g;
|
|
Packet a = pkt(ORIG, 20);
|
|
forwardHeardTwice(g, a, 0);
|
|
CHECK(g.twinBits(&a, 200) == 0);
|
|
CHECK(bitsOf(g, twinOf(ORIG, 20, 24), 200) == 24);
|
|
CHECK(bitsOf(g, twinOf(ORIG, 20, 25), 200) == 0);
|
|
Packet body = pkt(ORIG, 20); /* damage in the body counts too */
|
|
body.payload[4] ^= 0x11; body.payload[9] ^= 0x80;
|
|
CHECK(g.twinBits(&body, 200) == 3);
|
|
}
|
|
|
|
TEST(twin_scope, "UNIT-TWIN-005", "Other type, other length, direct route and uncovered types are never twins") {
|
|
TwinGuard g;
|
|
Packet a = pkt(ORIG, 20);
|
|
forwardHeardTwice(g, a, 0);
|
|
CHECK(bitsOf(g, twinOf(ORIG, 20, 1, PAYLOAD_TYPE_REQ), 200) == 0);
|
|
CHECK(bitsOf(g, twinOf(ORIG, 19, 1), 200) == 0);
|
|
Packet direct = twinOf(ORIG, 20, 1);
|
|
direct.header = ROUTE_TYPE_DIRECT | (PAYLOAD_TYPE_TXT_MSG << 2);
|
|
CHECK(g.twinBits(&direct, 200) == 0);
|
|
const uint8_t covered[] = {PAYLOAD_TYPE_REQ, PAYLOAD_TYPE_RESPONSE, PAYLOAD_TYPE_TXT_MSG, PAYLOAD_TYPE_GRP_TXT,
|
|
PAYLOAD_TYPE_GRP_DATA, PAYLOAD_TYPE_ANON_REQ, PAYLOAD_TYPE_PATH};
|
|
for (uint8_t t : covered) { Packet c = pkt(ORIG, 20, t); CHECK(TwinGuard::appliesTo(&c)); }
|
|
const uint8_t other[] = {PAYLOAD_TYPE_ACK, PAYLOAD_TYPE_ADVERT, PAYLOAD_TYPE_TRACE, PAYLOAD_TYPE_MULTIPART,
|
|
PAYLOAD_TYPE_CONTROL, PAYLOAD_TYPE_RAW_CUSTOM};
|
|
for (uint8_t t : other) {
|
|
Packet o = pkt(ORIG, 20, t);
|
|
CHECK(!TwinGuard::appliesTo(&o));
|
|
TwinGuard h;
|
|
forwardHeardTwice(h, o, 0);
|
|
CHECK(bitsOf(h, twinOf(ORIG, 20, 1, t), 200) == 0);
|
|
}
|
|
}
|
|
|
|
TEST(twin_ring, "UNIT-TWIN-006", "The newest SLOTS forwards are kept; the oldest is overwritten") {
|
|
TwinGuard g;
|
|
Packet a = pkt(ORIG, 20);
|
|
forwardHeardTwice(g, a, 0);
|
|
uint32_t seed = 7;
|
|
for (int i = 0; i < TwinGuard::SLOTS - 1; i++) {
|
|
uint8_t other[20];
|
|
for (uint8_t& b : other) b = (uint8_t)nextRandom(seed);
|
|
Packet o = pkt(other, 20);
|
|
g.noteForward(&o, false, 10 + i);
|
|
}
|
|
CHECK(bitsOf(g, twinOf(ORIG, 20, 1), 500) == 1);
|
|
Packet last = pkt(ORIG, 20, PAYLOAD_TYPE_GRP_TXT);
|
|
g.noteForward(&last, false, 400);
|
|
CHECK(bitsOf(g, twinOf(ORIG, 20, 1), 500) == 0);
|
|
}
|
|
|
|
TEST(twin_retry_shape, "UNIT-TWIN-007", "Genuine retries (new MAC and AES block) are far apart and not twins") {
|
|
uint32_t seed = 12345;
|
|
int nearest = 1000;
|
|
for (int trial = 0; trial < 2000; trial++) {
|
|
uint8_t first[20], retry[20];
|
|
first[0] = retry[0] = 0x9C; first[1] = retry[1] = 0x42; /* same destination and source */
|
|
for (int i = 2; i < 20; i++) { first[i] = (uint8_t)nextRandom(seed); retry[i] = (uint8_t)nextRandom(seed); }
|
|
int bits = 0;
|
|
for (int i = 0; i < 20; i++) bits += __builtin_popcount((unsigned)(first[i] ^ retry[i]));
|
|
if (bits < nearest) nearest = bits;
|
|
TwinGuard g;
|
|
Packet a = pkt(first, 20), b = pkt(retry, 20);
|
|
forwardHeardTwice(g, a, 0);
|
|
CHECK(g.twinBits(&b, 200) == (bits <= TwinGuard::MAX_BITS ? bits : 0));
|
|
}
|
|
CHECK(nearest > TwinGuard::MAX_BITS);
|
|
}
|