fix(mqtt): address review of the demand-driven slot work
[P1] A failed setup no longer strands the slot. setupSlot() returns bool and the startup loops count only successful activations, so a slot that fails on a client allocation neither consumes an active-slot position (starving a later healthy broker on capped hardware) nor sits dead forever: maintainSlotConnections() previously skipped clientless slots and the reconnect ladder is gated on initial_connect_done, so nothing retried it. It now retries an enabled but unactivated slot on a 60 s timer, one per cycle, gated on the same _slots_setup_done ordering so the NTP-deferred setup sequence is preserved. [P2] JWT setup no longer proceeds without a usable token. Both the preset and custom-audience paths returned after ignoring createSlotAuthToken()'s result, then called connect() and latched initial_connect_done -- so the token-allocation failure introduced by the previous commit produced an unauthenticated attempt exactly when memory was exhausted. They now return false and let the retry path handle it. [P2] ensureSlotAuthToken() no longer clears an existing token. It cleared unconditionally, so every renewal wiped the current token before JWTHelper ran; a renewal that then failed left an empty password where the inline buffer used to preserve working credentials (JWTHelper writes only on success). Only freshly allocated buffers are initialised now. [P2] Raw publications reuse the shared document. buildRawJSON() reached MQTTPayloadBuilder::buildRawMessage(), which constructed its own default JsonDocument and therefore malloc'd and freed an internal-heap variant pool per message -- on the highest-rate topic. The document is threaded through both builders and the bridge passes _json_scratch_doc. [P3] The writeTo() guard validates the source fields, not just the destination. A corrupt payload_len of MAX_PACKET_PAYLOAD + 1 still leaves getRawLength() inside MAX_TRANS_UNIT, so writeTo() read past packet->payload. Sizing and validation moved to a pure MQTTWireScratch header with host tests covering the accept/reject edges, matching the MQTTPacketFilter/MQTTConnectionPolicy pattern. Two findings fell out: MAX_PATH_SIZE one-byte hops is not encodable (the hop count is 6 bits, so 64 & 63 == 0; 32 two-byte hops is the widest real path), and a zero-payload packet serializes but does not survive readFrom() -- pinned as a test because it constrains any future wire-only queue. [P3] Corrected the pool-size comment: these targets are 32-bit, so ARDUINOJSON_SLOT_ID_SIZE is 2 and a pool block is 128 slots / 1024 bytes, not 4096. The 4096 figure came from a pre-existing comment near NEIGHBORS_DOC_POOL_BUDGET, which is left alone -- its byte measurements are empirical and still stand, only the block-size attribution is wrong. Activation is now centralized in activatedSlotCount()/canActivateSlot(), used by both startup loops, the retry path, and applySlotPreset(). That closes the pre-existing divergence where a live preset change called setupSlot() without consulting _max_active_slots, letting a non-PSRAM board reach three concurrent TLS sessions against a cap of two. BEHAVIOUR CHANGE: a reconfigure that would exceed the cap now logs and leaves the slot inactive instead of connecting. Reconfiguring an already-active slot still works, because teardownSlot() releases its position first. 272/272 native tests pass (5 new); both observer envs and an nRF52 repeater build clean. Flash 1593249 B non-PSRAM, 1555625 B PSRAM.
This commit is contained in:
1 parent
b93b6b1560
commit
e242cc6fd3
9 files changed
+272
-42
No files matched your search
@@ -171,8 +171,9 @@ TEST(MQTTPayloadBuilder, RxPacketOmitsUnknownNanScore) {
|
||||
|
||||
TEST(MQTTPayloadBuilder, RawMessageHasExactContractAndEscapesData) {
|
||||
char buffer[512];
|
||||
JsonDocument doc;
|
||||
int len = MQTTPayloadBuilder::buildRawMessage(
|
||||
"node \"A\"", "id\\1", kTimestamp, "AA\nBB", buffer, sizeof(buffer));
|
||||
doc, "node \"A\"", "id\\1", kTimestamp, "AA\nBB", buffer, sizeof(buffer));
|
||||
|
||||
ASSERT_GT(len, 0);
|
||||
EXPECT_EQ(static_cast<size_t>(len), strlen(buffer));
|
||||
@@ -224,8 +225,9 @@ TEST(MQTTPayloadBuilder, MaximumRepresentativePacketAndRawPayloadsRemainValid) {
|
||||
EXPECT_STREQ("3c3d3e3f", parsed_path[15].as<const char*>());
|
||||
|
||||
char raw_buffer[1024];
|
||||
JsonDocument raw_doc;
|
||||
int raw_len = MQTTPayloadBuilder::buildRawMessage(
|
||||
"node", "0123456789ABCDEF", kTimestamp, raw.c_str(),
|
||||
raw_doc, "node", "0123456789ABCDEF", kTimestamp, raw.c_str(),
|
||||
raw_buffer, sizeof(raw_buffer));
|
||||
ASSERT_GT(raw_len, 0);
|
||||
JsonDocument parsed_raw;
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
// Boundary tests for the wire-format scratch sizing used by the MQTT raw/packet
|
||||
// publish paths. Packet::writeTo() cannot report an overrun (uint8_t return) and
|
||||
// trusts the packet's own length fields, so canSerialize() is what keeps it in
|
||||
// bounds — these cases pin the exact accept/reject edges.
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "helpers/MQTTWireScratch.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// A packet that serializes to the largest legal wire form: transport codes present,
|
||||
// a full path, and a full payload.
|
||||
mesh::Packet maxPacket() {
|
||||
mesh::Packet p;
|
||||
p.header = ROUTE_TYPE_TRANSPORT_DIRECT | (PAYLOAD_TYPE_TXT_MSG << PH_TYPE_SHIFT);
|
||||
p.transport_codes[0] = 0x1234;
|
||||
p.transport_codes[1] = 0x5678;
|
||||
// The hop count field is 6 bits, so MAX_PATH_SIZE one-byte hops is NOT encodable
|
||||
// (64 & 63 == 0). 32 hops of 2 bytes is the widest path that reaches MAX_PATH_SIZE.
|
||||
p.setPathHashSizeAndCount(2, 32);
|
||||
EXPECT_EQ(MAX_PATH_SIZE, p.getPathByteLen());
|
||||
p.payload_len = MAX_PACKET_PAYLOAD;
|
||||
memset(p.payload, 0xAB, sizeof(p.payload));
|
||||
return p;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(MQTTWireScratch, MaxLegalPacketFitsTheScratchBuffer) {
|
||||
mesh::Packet p = maxPacket();
|
||||
// 1 header + 4 transport + 1 path_len + 64 path + 184 payload = 254.
|
||||
EXPECT_EQ(254, p.getRawLength());
|
||||
EXPECT_TRUE(MQTTWireScratch::canSerialize(p, MQTTWireScratch::kWireBytes));
|
||||
|
||||
uint8_t buf[MQTTWireScratch::kWireBytes];
|
||||
const uint8_t written = p.writeTo(buf);
|
||||
EXPECT_EQ(254, (int)written);
|
||||
EXPECT_LE((size_t)written, sizeof(buf));
|
||||
// The hex buffer must hold two chars per byte plus the NUL.
|
||||
EXPECT_GE(MQTTWireScratch::kWireHexChars, (size_t)written * 2 + 1);
|
||||
}
|
||||
|
||||
TEST(MQTTWireScratch, RejectsPayloadLenPastTheArrayEvenWhenEncodedLengthFits) {
|
||||
mesh::Packet p;
|
||||
p.header = ROUTE_TYPE_FLOOD;
|
||||
p.setPathHashSizeAndCount(1, 0);
|
||||
// getRawLength() == 2 + 0 + 185 == 187, comfortably inside MAX_TRANS_UNIT, but
|
||||
// writeTo() would memcpy 185 bytes out of a 184-byte array.
|
||||
p.payload_len = MAX_PACKET_PAYLOAD + 1;
|
||||
EXPECT_LE(p.getRawLength(), (int)MQTTWireScratch::kWireBytes);
|
||||
EXPECT_FALSE(MQTTWireScratch::canSerialize(p, MQTTWireScratch::kWireBytes));
|
||||
|
||||
p.payload_len = MAX_PACKET_PAYLOAD;
|
||||
EXPECT_TRUE(MQTTWireScratch::canSerialize(p, MQTTWireScratch::kWireBytes));
|
||||
}
|
||||
|
||||
TEST(MQTTWireScratch, RejectsPathLenThatWouldTruncateIntoOneWireByte) {
|
||||
mesh::Packet p;
|
||||
p.header = ROUTE_TYPE_FLOOD;
|
||||
p.payload_len = 4;
|
||||
p.path_len = 0x100; // writeTo() stores this in a single byte
|
||||
EXPECT_FALSE(MQTTWireScratch::canSerialize(p, MQTTWireScratch::kWireBytes));
|
||||
|
||||
p.path_len = 0xFF;
|
||||
// Still rejected, but now on the destination check rather than truncation:
|
||||
// 0xFF encodes 63 hops of 4 bytes.
|
||||
EXPECT_GT(p.getRawLength(), (int)MQTTWireScratch::kWireBytes);
|
||||
EXPECT_FALSE(MQTTWireScratch::canSerialize(p, MQTTWireScratch::kWireBytes));
|
||||
}
|
||||
|
||||
TEST(MQTTWireScratch, DestinationEdgeIsInclusive) {
|
||||
mesh::Packet p = maxPacket();
|
||||
const size_t exact = (size_t)p.getRawLength();
|
||||
EXPECT_TRUE(MQTTWireScratch::canSerialize(p, exact));
|
||||
EXPECT_FALSE(MQTTWireScratch::canSerialize(p, exact - 1));
|
||||
}
|
||||
|
||||
// A zero-payload packet serializes fine but does not survive readFrom(), which
|
||||
// requires at least one payload byte. canSerialize() deliberately does not reject it:
|
||||
// the raw/packet publish paths only ever write the bytes out. Any future change that
|
||||
// reconstructs a Packet from queued wire bytes has to handle this asymmetry.
|
||||
TEST(MQTTWireScratch, ZeroPayloadPacketSerializesButDoesNotRoundTrip) {
|
||||
mesh::Packet p;
|
||||
p.header = ROUTE_TYPE_FLOOD;
|
||||
p.setPathHashSizeAndCount(1, 0);
|
||||
p.payload_len = 0;
|
||||
EXPECT_EQ(2, p.getRawLength());
|
||||
EXPECT_TRUE(MQTTWireScratch::canSerialize(p, MQTTWireScratch::kWireBytes));
|
||||
|
||||
uint8_t buf[MQTTWireScratch::kWireBytes];
|
||||
const uint8_t written = p.writeTo(buf);
|
||||
EXPECT_EQ(2, (int)written);
|
||||
|
||||
mesh::Packet restored;
|
||||
EXPECT_FALSE(restored.readFrom(buf, written));
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in new issue
Block a user