Skip to content

Commit

Permalink
Test received fields
Browse files Browse the repository at this point in the history
  • Loading branch information
esev committed Jan 3, 2025
1 parent 1a71d6b commit 269b12a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/mesh/RadioLibInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ void RadioLibInterface::handleReceiveInterrupt()
// nodes.
meshtastic_MeshPacket *mp = packetPool.allocZeroed();

// Keep the assigned fields in sync with src/mqtt/MQTT.cpp:onReceiveProto
mp->from = radioBuffer.header.from;
mp->to = radioBuffer.header.to;
mp->id = radioBuffer.header.id;
Expand Down
18 changes: 14 additions & 4 deletions src/mqtt/MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,22 @@ inline void onReceiveProto(char *topic, byte *payload, size_t length)
return;
}
LOG_INFO("Received MQTT topic %s, len=%u", topic, length);
if (e.packet->hop_limit > HOP_MAX || e.packet->hop_start > HOP_MAX) {
LOG_INFO("Invalid hop_limit(%u) or hop_start(%u)", e.packet->hop_limit, e.packet->hop_start);
return;
}

UniquePacketPoolPacket p = packetPool.allocUniqueCopy(*e.packet);
UniquePacketPoolPacket p = packetPool.allocUniqueZeroed();
p->from = e.packet->from;
p->to = e.packet->to;
p->id = e.packet->id;
p->channel = e.packet->channel;
p->hop_limit = e.packet->hop_limit;
p->hop_start = e.packet->hop_start;
p->want_ack = e.packet->want_ack;
p->via_mqtt = true; // Mark that the packet was received via MQTT
// Unset received SNR/RSSI which might have been added by the MQTT gateway
p->rx_snr = 0;
p->rx_rssi = 0;
p->which_payload_variant = e.packet->which_payload_variant;
memcpy(&p->decoded, &e.packet->decoded, std::max(sizeof(p->decoded), sizeof(p->encrypted)));

if (p->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
if (moduleConfig.mqtt.encryption_enabled) {
Expand Down
28 changes: 28 additions & 0 deletions test/test_mqtt/MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,32 @@ void test_receiveIgnoresDecodedAdminApp(void)
TEST_ASSERT_TRUE(mockRouter->packets_.empty());
}

// Only the same fields that are transmitted over LoRa should be set in MQTT messages.
void test_receiveIgnoresUnexpectedFields(void)
{
meshtastic_MeshPacket input = decoded;
input.rx_snr = 10;
input.rx_rssi = 20;

unitTest->publish(&input);

TEST_ASSERT_EQUAL(1, mockRouter->packets_.size());
const meshtastic_MeshPacket &p = mockRouter->packets_.front();
TEST_ASSERT_EQUAL(0, p.rx_snr);
TEST_ASSERT_EQUAL(0, p.rx_rssi);
}

// Messages with an invalid hop_limit are ignored.
void test_receiveIgnoresInvalidHopLimit(void)
{
meshtastic_MeshPacket p = decoded;
p.hop_limit = 10;

unitTest->publish(&p);

TEST_ASSERT_TRUE(mockRouter->packets_.empty());
}

// Publishing to a text channel.
void test_publishTextMessageDirect(void)
{
Expand Down Expand Up @@ -801,6 +827,8 @@ void setup()
RUN_TEST(test_receiveIgnoresSentMessagesFromOthers);
RUN_TEST(test_receiveIgnoresDecodedWhenEncryptionEnabled);
RUN_TEST(test_receiveIgnoresDecodedAdminApp);
RUN_TEST(test_receiveIgnoresUnexpectedFields);
RUN_TEST(test_receiveIgnoresInvalidHopLimit);
RUN_TEST(test_publishTextMessageDirect);
RUN_TEST(test_publishTextMessageWithProxy);
RUN_TEST(test_reportToMapDefaultImprecise);
Expand Down

0 comments on commit 269b12a

Please sign in to comment.