#include "test.h" #include "dispatcher_fixture.h" #include #include #include extern int (*test_mac_then_decrypt)(const uint8_t*, uint8_t*, const uint8_t*, int); namespace { struct Rng : mesh::RNG { void random(uint8_t *dest, size_t len) override { memset(dest, 0x42, len); } }; struct Rtc : mesh::RTCClock { uint32_t now = 1700000000; uint32_t getCurrentTime() override { return now; } void setCurrentTime(uint32_t t) override { now = t; } }; struct Tables : mesh::MeshTables { bool wasSeen(const mesh::Packet*) override { return false; } void markSeen(const mesh::Packet*) override {} void clear(const mesh::Packet*) override {} }; struct ChatMesh : BaseChatMesh { ChatMesh(mesh::Radio& r, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::PacketManager& mgr, mesh::MeshTables& t) : BaseChatMesh(r, ms, rng, rtc, mgr, t) {} void onDiscoveredContact(ContactInfo&, bool, uint8_t, const uint8_t*) override {} ContactInfo* ack_from = nullptr; ContactInfo* processAck(const uint8_t*) override { return ack_from; } int path_retries = 0; void handleReturnPathRetry(const ContactInfo&, const uint8_t*, uint8_t) override { ++path_retries; } using BaseChatMesh::onAckRecv; void onContactPathUpdated(const ContactInfo&) override {} void onMessageRecv(const ContactInfo&, mesh::Packet*, uint32_t, const char*) override {} void onCommandDataRecv(const ContactInfo&, mesh::Packet*, uint32_t, const char*) override {} void onCLICommandRecv(const ContactInfo&, mesh::Packet*, uint32_t, const char*, char*) override {} void onSignedMessageRecv(const ContactInfo&, mesh::Packet*, uint32_t, const uint8_t*, const char*) override {} uint32_t calcFloodTimeoutMillisFor(uint32_t) const override { return 1000; } uint32_t calcDirectTimeoutMillisFor(uint32_t, uint8_t) const override { return 1000; } void onSendTimeout() override {} void onChannelMessageRecv(const mesh::GroupChannel&, mesh::Packet*, uint32_t, const char*) override {} uint8_t onContactRequest(const ContactInfo&, uint32_t, const uint8_t*, uint8_t, uint8_t*) override { return 0; } void onContactResponse(const ContactInfo&, const uint8_t*, uint8_t) override {} using BaseChatMesh::searchPeersByHash; bool forward = false; bool allowPacketForward(const mesh::Packet*) override { return forward; } mesh::MacFailThrottle* shared_fails = nullptr; // another Mesh's budget, as a forwarder side uses mesh::MacFailThrottle& macFails() override { return shared_fails ? *shared_fails : ownFails(); } mesh::MacFailThrottle& ownFails() { return mesh::Mesh::macFails(); } }; struct ChatFixture { test_support::Clock clock; test_support::Radio radio; StaticPoolPacketManager pool; Rng rng; Rtc rtc; Tables tables; ChatMesh mesh{radio, clock, rng, rtc, pool, tables}; }; ContactInfo makeContact(uint8_t first_byte, uint8_t type, const char *name) { ContactInfo c{}; for (int i = 0; i < PUB_KEY_SIZE; ++i) c.id.pub_key[i] = uint8_t(i + 1); c.id.pub_key[0] = first_byte; c.type = type; strncpy(c.name, name, sizeof(c.name) - 1); return c; } } // namespace TEST(empty_slot_skipped, "UNIT-CHAT-001", "An empty contact slot (all-zero key) matches no lookup") { ChatFixture f; ContactInfo empty{}; // what a cleared anon slot holds empty.type = ADV_TYPE_NONE; CHECK(empty.isEmptySlot()); CHECK(f.mesh.addContact(empty)); // occupies a slot inside num_contacts ContactInfo real = makeContact(0x00, ADV_TYPE_CHAT, "alice"); CHECK(!real.isEmptySlot()); CHECK(f.mesh.addContact(real)); const uint8_t zero_hash[1] = {0x00}; CHECK(f.mesh.searchPeersByHash(zero_hash) == 1); // only the real contact with hash 0x00 uint8_t zero_key[PUB_KEY_SIZE] = {0}; CHECK(f.mesh.lookupContactByPubKey(zero_key, PUB_KEY_SIZE) == nullptr); CHECK(f.mesh.lookupContactByPubKey(zero_key, 1) != nullptr); CHECK(f.mesh.lookupContactByPubKey(zero_key, 1)->id.matches(real.id)); ContactInfo *by_name = f.mesh.searchContactsByPrefix(""); CHECK(by_name != nullptr && strcmp(by_name->name, "alice") == 0); CHECK(!f.mesh.removeContact(empty)); // the empty slot is not a removable match CHECK(f.mesh.removeContact(real)); CHECK(f.mesh.searchPeersByHash(zero_hash) == 0); } TEST(group_msg_negative_len, "UNIT-CHAT-002", "sendGroupMessage refuses a negative text length") { ChatFixture f; mesh::GroupChannel ch{}; CHECK(!f.mesh.sendGroupMessage(1700000000, ch, "me", "x", -4)); } TEST(trace_partial_hash, "UNIT-CHAT-003", "A TRACE ending in a partial hash entry is not forwarded") { ChatFixture f; f.mesh.forward = true; uint8_t seed[SEED_SIZE]; memset(seed, 7, sizeof(seed)); f.mesh.self_id.fromSeed(seed); const uint8_t *pub = f.mesh.self_id.pub_key; f.mesh.begin(); // Prime every pool slot so payload[12] holds the second byte of our key. for (int i = 0; i < 32; ++i) { std::vector raw(2 + 13, 0); raw[0] = uint8_t((0x0C << 2) | ROUTE_TYPE_DIRECT); raw[2 + 12] = pub[1]; f.radio.incoming.push_back(raw); } f.mesh.loop(); CHECK(f.pool.getFreeCount() == 32); // 2-byte hashes, one hop done, a 3-byte hash list: one whole entry, then only the first byte of ours. f.radio.incoming.push_back({uint8_t((PAYLOAD_TYPE_TRACE << 2) | ROUTE_TYPE_DIRECT), 1, 0x55, 1, 2, 3, 4, 5, 6, 7, 8, 0x01, 0xEE, 0xEE, pub[0]}); f.mesh.loop(); CHECK(f.radio.attempts == 0 && f.pool.getOutboundTotal() == 0 && f.pool.getFreeCount() == 32); } TEST(zero_key_advert, "UNIT-CHAT-004", "An all-zero-key advert with an all-zero signature is dropped, not stored or relayed") { ChatFixture f; f.mesh.forward = true; uint8_t seed[SEED_SIZE]; memset(seed, 7, sizeof(seed)); f.mesh.self_id.fromSeed(seed); f.mesh.begin(); uint8_t app[32]; AdvertDataBuilder builder(ADV_TYPE_CHAT, "Z"); int app_len = builder.encodeTo(app); for (uint32_t n = 0; n < 8; ++n) { std::vector raw = {uint8_t((PAYLOAD_TYPE_ADVERT << 2) | ROUTE_TYPE_FLOOD), 0}; raw.insert(raw.end(), PUB_KEY_SIZE, 0); // pub key uint32_t ts = 1700000000 + n; raw.insert(raw.end(), (uint8_t*)&ts, (uint8_t*)&ts + 4); raw.insert(raw.end(), SIGNATURE_SIZE, 0); // signature raw.insert(raw.end(), app, app + app_len); f.radio.incoming.push_back(raw); f.mesh.loop(); f.clock.now += 5000; f.radio.completion = true; f.mesh.loop(); f.mesh.loop(); } CHECK(f.mesh.getNumContacts() == 0); CHECK(f.radio.sent.empty()); } TEST(anon_req_small_order_sender, "UNIT-CHAT-005", "An ANON_REQ from a small-order sender key is dropped before key exchange and decryption") { // Utils::MACThenDecrypt throws in the host build, so reaching decryption shows as an exception. auto reachesDecrypt = [](const uint8_t sender[PUB_KEY_SIZE]) { ChatFixture f; uint8_t seed[SEED_SIZE]; memset(seed, 7, sizeof(seed)); f.mesh.self_id.fromSeed(seed); f.mesh.begin(); std::vector raw = {uint8_t((PAYLOAD_TYPE_ANON_REQ << 2) | ROUTE_TYPE_FLOOD), 0, f.mesh.self_id.pub_key[0]}; raw.insert(raw.end(), sender, sender + PUB_KEY_SIZE); raw.insert(raw.end(), 2 + 16, 0x5A); // MAC + one cipher block f.radio.incoming.push_back(raw); try { f.mesh.loop(); } catch (const std::runtime_error&) { return true; } return false; }; uint8_t zero[PUB_KEY_SIZE] = {}; CHECK(!reachesDecrypt(zero)); mesh::LocalIdentity peer; uint8_t peer_seed[SEED_SIZE]; memset(peer_seed, 9, sizeof(peer_seed)); peer.fromSeed(peer_seed); CHECK(reachesDecrypt(peer.pub_key)); // control: a real key is decrypted } TEST(ack_flood_path_retry, "UNIT-CHAT-006", "A flood ACK for us from a contact with a known path re-sends the return path, then is marked do-not-retransmit") { auto ackOnRoute = [](uint8_t route, uint8_t out_path_len) { ChatFixture f; ContactInfo c = makeContact(0x11, ADV_TYPE_CHAT, "bob"); c.out_path_len = out_path_len; f.mesh.ack_from = &c; mesh::Packet pkt; pkt.header = uint8_t((PAYLOAD_TYPE_ACK << 2) | route); pkt.path_len = 0; f.mesh.onAckRecv(&pkt, 0x12345678); CHECK(pkt.isMarkedDoNotRetransmit()); return f.mesh.path_retries; }; CHECK(ackOnRoute(ROUTE_TYPE_FLOOD, 0) == 1); CHECK(ackOnRoute(ROUTE_TYPE_TRANSPORT_FLOOD, 2) == 1); CHECK(ackOnRoute(ROUTE_TYPE_FLOOD, OUT_PATH_UNKNOWN) == 0); // no path of ours to re-send CHECK(ackOnRoute(ROUTE_TYPE_DIRECT, 0) == 0); // the peer already routes directly } TEST(mac_fail_throttle_budget, "UNIT-CHAT-007", "A source hash that spent its failed-datagram budget is blocked until its window ends; other hashes are not") { mesh::MacFailThrottle t; const uint32_t t0 = 0xFFFFF000u; // the window spans the millis wrap for (int n = 0; n < mesh::MacFailThrottle::BUDGET; ++n) { CHECK(!t.blocked(0x11, t0 + n)); t.fail(0x11, t0 + n); } CHECK(t.blocked(0x11, t0 + 10)); CHECK(!t.blocked(0x12, t0 + 10)); CHECK(t.blocked(0x11, t0 + mesh::MacFailThrottle::WINDOW_MS - 1)); CHECK(!t.blocked(0x11, t0 + mesh::MacFailThrottle::WINDOW_MS)); } TEST(mac_fail_throttle_mesh, "UNIT-CHAT-008", "Datagrams for us that no contact accepts stop being decrypted for that source hash once its budget is spent, until the window ends") { static int calls; calls = 0; struct ResetHook { ~ResetHook() { test_mac_then_decrypt = nullptr; } } reset; test_mac_then_decrypt = [](const uint8_t*, uint8_t*, const uint8_t*, int) { ++calls; return 0; }; ChatFixture f; uint8_t seed[SEED_SIZE]; memset(seed, 7, sizeof(seed)); f.mesh.self_id.fromSeed(seed); mesh::LocalIdentity peer; uint8_t peer_seed[SEED_SIZE]; memset(peer_seed, 9, sizeof(peer_seed)); peer.fromSeed(peer_seed); ContactInfo c = makeContact(0, ADV_TYPE_CHAT, "bob"); memcpy(c.id.pub_key, peer.pub_key, PUB_KEY_SIZE); CHECK(f.mesh.addContact(c)); f.mesh.begin(); auto send = [&f, &peer](uint8_t fill) { std::vector raw = {uint8_t((PAYLOAD_TYPE_TXT_MSG << 2) | ROUTE_TYPE_FLOOD), 0, f.mesh.self_id.pub_key[0], peer.pub_key[0]}; raw.insert(raw.end(), 2 + 16, fill); // MAC + one cipher block, distinct per try f.radio.incoming.push_back(raw); f.mesh.loop(); }; const int budget = mesh::MacFailThrottle::BUDGET; for (int n = 0; n < budget; ++n) send(uint8_t(n)); CHECK(calls == budget); send(100); CHECK(calls == budget); // budget spent: not decrypted f.clock.now += mesh::MacFailThrottle::WINDOW_MS; send(101); CHECK(calls == budget + 1); // the next window decrypts again } TEST(mac_fail_throttle_shared, "UNIT-CHAT-009", "Two Meshes of one node (forwarder sides) spend one failed-datagram budget") { static int calls; calls = 0; struct ResetHook { ~ResetHook() { test_mac_then_decrypt = nullptr; } } reset; test_mac_then_decrypt = [](const uint8_t*, uint8_t*, const uint8_t*, int) { ++calls; return 0; }; ChatFixture a, b; b.mesh.shared_fails = &a.mesh.ownFails(); uint8_t seed[SEED_SIZE]; memset(seed, 7, sizeof(seed)); mesh::LocalIdentity peer; uint8_t peer_seed[SEED_SIZE]; memset(peer_seed, 9, sizeof(peer_seed)); peer.fromSeed(peer_seed); for (ChatFixture* f : {&a, &b}) { f->mesh.self_id.fromSeed(seed); ContactInfo c = makeContact(0, ADV_TYPE_CHAT, "bob"); memcpy(c.id.pub_key, peer.pub_key, PUB_KEY_SIZE); CHECK(f->mesh.addContact(c)); f->mesh.begin(); } auto send = [&peer](ChatFixture& f, uint8_t fill) { std::vector raw = {uint8_t((PAYLOAD_TYPE_TXT_MSG << 2) | ROUTE_TYPE_FLOOD), 0, f.mesh.self_id.pub_key[0], peer.pub_key[0]}; raw.insert(raw.end(), 2 + 16, fill); f.radio.incoming.push_back(raw); f.mesh.loop(); }; const int budget = mesh::MacFailThrottle::BUDGET; for (int n = 0; n < budget / 2; ++n) send(a, uint8_t(n)); for (int n = budget / 2; n < budget; ++n) send(b, uint8_t(n)); // spends the same budget CHECK(calls == budget); send(a, 100); send(b, 101); CHECK(calls == budget); // spent on both }