diff --git a/examples/simple_repeater/ChatBot.cpp b/examples/simple_repeater/ChatBot.cpp index 0882c3be..b7809379 100644 --- a/examples/simple_repeater/ChatBot.cpp +++ b/examples/simple_repeater/ChatBot.cpp @@ -14,11 +14,12 @@ ChatBot::ChatBot() : ctx(NULL), send(NULL), jitter(NULL) { void ChatBot::resetToDefaults() { enabled = false; - name[0] = 0; + memset(name, 0, sizeof(name)); // the whole array is written to /bot_cfg memset(psk, 0, sizeof(psk)); psk_len = 0; chan_hash = 0; last_reply_ms = 0; + has_replied = false; // ctx/send/jitter are wiring, not config state: a load() must not clear them save_flag.reset(); } @@ -142,7 +143,7 @@ void ChatBot::onGroupData(const mesh::Packet* pkt, uint8_t type, if (memcmp(psk, channel.secret, sizeof(psk)) != 0) return; // hash collision guard const uint32_t now = millis(); - if (last_reply_ms && (uint32_t)(now - last_reply_ms) < BOT_MIN_INTERVAL_MS) return; + if (has_replied && (uint32_t)(now - last_reply_ms) < BOT_MIN_INTERVAL_MS) return; char sender[24], text[24]; bool has_text = false, has_sender = false; @@ -180,7 +181,8 @@ void ChatBot::onGroupData(const mesh::Packet* pkt, uint8_t type, uint32_t delay = jitter ? jitter(ctx) : BOT_REPLY_DELAY_MIN_MS; if (!send(ctx, psk, chan_hash, pkt->getRxProfile(), delay, reply, reply_len)) return; - last_reply_ms = now; // a suppressed send leaves no state behind + last_reply_ms = now; // a failed send leaves no state behind + has_replied = true; } // ---------------------------------------------------------------- persistence @@ -254,7 +256,9 @@ void ChatBot::load(FILESYSTEM* fs) { // ---------------------------------------------------------------- CLI void botCLI(ChatBot& bot, const char* command, char* reply) { - char scratch[64]; + // "chan " + 64 hex digits is 69 bytes, so the scratch must hold at least + // that plus the quoting/whitespace cliPrepare() may hand us. + char scratch[96]; if (!cliPrepare(command, scratch, sizeof(scratch), reply)) return; char* p = scratch; char* verb = nextToken(&p); diff --git a/examples/simple_repeater/ChatBot.h b/examples/simple_repeater/ChatBot.h index 4b6f3bff..1b2879cf 100644 --- a/examples/simple_repeater/ChatBot.h +++ b/examples/simple_repeater/ChatBot.h @@ -31,7 +31,8 @@ private: uint8_t psk[32]; uint8_t psk_len; uint8_t chan_hash; - uint32_t last_reply_ms; // wrap-safe millis() stamp of the last TX; 0 = none + uint32_t last_reply_ms; // wrap-safe millis() stamp of the last successful TX + bool has_replied; // a stamp of 0 is a valid time, so it needs its own flag void* ctx; SendFn send; JitterFn jitter; diff --git a/test/test_bot/test_bot.cpp b/test/test_bot/test_bot.cpp index f6a7c217..8d663417 100644 --- a/test/test_bot/test_bot.cpp +++ b/test/test_bot/test_bot.cpp @@ -358,6 +358,20 @@ TEST_F(BotTest, CliStatusOnOffNameAndChan) { EXPECT_EQ(cli("bot off"), "OK - bot off"); } +TEST_F(BotTest, CliAcceptsAFullLength32BytePsk) { + const char* hex64 = "000102030405060708090A0B0C0D0E0F" + "101112131415161718191A1B1C1D1E1F"; + std::string cmd = std::string("bot chan ") + hex64; + uint8_t key[32], h; + int key_len = 0; + ASSERT_TRUE(cliDecodePskHex(hex64, key, sizeof(key), &key_len)); + mesh::Utils::sha256(&h, 1, key, 32); + char expected[24]; + snprintf(expected, sizeof(expected), "OK - chan h=%02X", h); + EXPECT_EQ(cli(cmd.c_str()), expected); + EXPECT_EQ(bot.getPskLen(), 32); +} + TEST_F(BotTest, CliRejectsBothChanFormsAndBadValues) { std::string both = std::string("bot chan #bottest ") + HEX16; EXPECT_EQ(cli(both.c_str()), "Err - usage: chan #| | chan off"); @@ -370,6 +384,70 @@ TEST_F(BotTest, CliRejectsBothChanFormsAndBadValues) { EXPECT_EQ(cli("bot on junk"), "Err - usage: bot on|off"); } +TEST_F(BotTest, NonPlainTextWrongTypeAndShortPayloadAreIgnored) { + arm(); + uint8_t data[64]; + mesh::Packet pkt; + mesh::GroupChannel chan; + memset(&chan, 0, sizeof(chan)); + chan.hash[0] = bot.getChanHash(); + memcpy(chan.secret, bot.getPsk(), sizeof(chan.secret)); + + // too short for the 5-byte group-text header + bot.onGroupData(&pkt, PAYLOAD_TYPE_GRP_TXT, chan, data, 4, NULL, "NodeName"); + EXPECT_EQ(sends, 0); + + memset(data, 0, sizeof(data)); + data[4] = TXT_TYPE_CLI_DATA << 2; + memcpy(data + 5, "Alice: test", 12); + bot.onGroupData(&pkt, PAYLOAD_TYPE_GRP_TXT, chan, data, 17, NULL, "NodeName"); + EXPECT_EQ(sends, 0); + + data[4] = TXT_TYPE_PLAIN; + bot.onGroupData(&pkt, PAYLOAD_TYPE_GRP_DATA, chan, data, 17, NULL, "NodeName"); + EXPECT_EQ(sends, 0); +} + +TEST_F(BotTest, LongPskWorksAndOffKeepsTheChannel) { + const char* hex64 = "000102030405060708090A0B0C0D0E0F" + "101112131415161718191A1B1C1D1E1F"; + ASSERT_TRUE(bot.setChannel(hex64)); + EXPECT_EQ(bot.getPskLen(), 32); + bot.setEnabled(true); + feed("Alice", "test"); + ASSERT_EQ(sends, 1); + uint8_t expected_hash; + mesh::Utils::sha256(&expected_hash, 1, bot.getPsk(), 32); + EXPECT_EQ(bot.getChanHash(), expected_hash); + + bot.setEnabled(false); + EXPECT_EQ(bot.getPskLen(), 32); // off preserves the key + bot.setEnabled(true); // one command to re-enable + g_mock_millis += BOT_MIN_INTERVAL_MS + 1; + feed("Alice", "test"); + EXPECT_EQ(sends, 2); +} + +TEST_F(BotTest, RecordBytesAreDeterministic) { + arm(); + bot.setName("TestBot"); + flushSave(); + std::vector first = fs.files[BOT_CFG_FILE]; + ASSERT_EQ(first.size(), BOT_CFG_RECORD_BYTES); + fs.files.erase(BOT_CFG_FILE); + bot.save(&fs); // write the same state again + EXPECT_EQ(fs.files[BOT_CFG_FILE], first); +} + +TEST_F(BotTest, FirstReplyAtMillisZeroClosesTheGate) { + g_mock_millis = 0; + arm(); + feed("Alice", "test"); + ASSERT_EQ(sends, 1); + feed("Alice", "test"); // millis still 0: must be suppressed + EXPECT_EQ(sends, 1); +} + int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS();