simple_repeater: filter hash throttle + opt-in message age limit (tth #3)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-02 17:40:09 +01:00
1 parent e3029de6e4
commit 548b96d15c
8 files changed
+370 -29

No files matched your search

@@ -0,0 +1,102 @@
#include <gtest/gtest.h>
#include "helpers/ChannelGate.h"
// ── HashThrottle ─────────────────────────────────────────────────────────────
TEST(HashThrottle, ZeroSecsBlocksEverything) {
HashThrottle t;
for (uint32_t ms = 0; ms < 10 * 60 * 1000; ms += 30 * 1000) EXPECT_FALSE(t.pass(0, 0, ms));
}
TEST(HashThrottle, FirstMatchAfterBootPasses) {
HashThrottle t;
EXPECT_TRUE(t.pass(0, 60, 123456));
EXPECT_FALSE(t.pass(0, 60, 123457));
}
TEST(HashThrottle, OnePassPerWindow) {
HashThrottle t;
EXPECT_TRUE(t.pass(0, 60, 0 + 1));
EXPECT_FALSE(t.pass(0, 60, 59999));
EXPECT_TRUE(t.pass(0, 60, 60001));
EXPECT_FALSE(t.pass(0, 60, 60002));
}
TEST(HashThrottle, OverRateDoesNotExtendWindow) {
HashThrottle t;
EXPECT_TRUE(t.pass(0, 60, 1000));
// hammering inside the window must not push the next pass further out
for (uint32_t ms = 2000; ms < 61000; ms += 1000) EXPECT_FALSE(t.pass(0, 60, ms));
EXPECT_TRUE(t.pass(0, 60, 61000));
}
TEST(HashThrottle, SlotsAreIndependent) {
HashThrottle t;
EXPECT_TRUE(t.pass(0, 60, 1000));
EXPECT_TRUE(t.pass(1, 60, 1001));
EXPECT_FALSE(t.pass(0, 60, 1002));
EXPECT_FALSE(t.pass(1, 60, 1003));
}
TEST(HashThrottle, RemoveShiftsLaterSlotsDown) {
HashThrottle t;
EXPECT_TRUE(t.pass(0, 60, 1000)); // slot 0 used
EXPECT_TRUE(t.pass(1, 60, 1000)); // slot 1 used
t.remove(0); // old slot 1 is now slot 0
EXPECT_FALSE(t.pass(0, 60, 2000)); // still inside ITS window
EXPECT_TRUE(t.pass(1, 60, 2000)); // freed slot starts fresh
}
TEST(HashThrottle, SurvivesMillisWrap) {
HashThrottle t;
EXPECT_TRUE(t.pass(0, 60, 0xFFFFFF00u));
EXPECT_FALSE(t.pass(0, 60, 0x00000100u)); // 512 ms later, across the wrap
EXPECT_TRUE(t.pass(0, 60, 0xFFFFFF00u + 60000u)); // wraps to 60 s later
}
TEST(HashThrottle, BadSlotNeverPasses) {
HashThrottle t;
EXPECT_FALSE(t.pass(-1, 60, 0));
EXPECT_FALSE(t.pass(HASH_THROTTLE_SLOTS, 60, 0));
}
// ── msgAgeCheck ──────────────────────────────────────────────────────────────
static const uint32_t NOW = 1793625936; // 2026-11-02 13:25 UTC
TEST(MsgAge, OffWhenZero) {
EXPECT_EQ(msgAgeCheck(0, NOW, 0), MSG_AGE_OFF);
}
TEST(MsgAge, InactiveWhileClockUnset) {
// RTC-less repeater after boot: 15 May 2024
EXPECT_EQ(msgAgeCheck(1715770351, 1715770351 + 3600, 60), MSG_AGE_CLOCK_UNSET);
EXPECT_EQ(msgAgeCheck(0, MSG_AGE_CLOCK_VALID_FROM - 1, 60), MSG_AGE_CLOCK_UNSET);
}
TEST(MsgAge, FreshAndOldAroundTheLimit) {
EXPECT_EQ(msgAgeCheck(NOW - 60 * 60, NOW, 60), MSG_AGE_FRESH); // exactly the limit
EXPECT_EQ(msgAgeCheck(NOW - 60 * 60 - 1, NOW, 60), MSG_AGE_OLD);
}
TEST(MsgAge, FutureIsNeverOld) {
EXPECT_EQ(msgAgeCheck(NOW + 8 * 3600, NOW, 1), MSG_AGE_FRESH);
EXPECT_EQ(msgAgeCheck(0xFFFFFFFFu, NOW, 1), MSG_AGE_FRESH);
}
TEST(MsgAge, SenderWithUnsetClockIsOld) {
// companion that never got a time sync stamps 2024 -- that IS what the
// limit drops; documented trade-off, measured on the CZ mesh 2. 11. 2026
EXPECT_EQ(msgAgeCheck(1715770351, NOW, MSG_AGE_MAX_MINUTES), MSG_AGE_OLD);
EXPECT_EQ(msgAgeCheck(0, NOW, MSG_AGE_MAX_MINUTES), MSG_AGE_OLD);
}
TEST(MsgAge, WeekLimitDoesNotOverflow) {
EXPECT_EQ(msgAgeCheck(NOW - 10080u * 60u, NOW, MSG_AGE_MAX_MINUTES), MSG_AGE_FRESH);
EXPECT_EQ(msgAgeCheck(NOW - 10080u * 60u - 1, NOW, MSG_AGE_MAX_MINUTES), MSG_AGE_OLD);
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}