mirror of
https://github.com/meshtastic/firmware.git
synced 2025-08-27 23:26:49 +00:00
Add more text message test cases for meshpacket serializer (#7709)
Some checks are pending
CI / setup (check) (push) Waiting to run
CI / setup (esp32) (push) Waiting to run
CI / setup (esp32c3) (push) Waiting to run
CI / setup (esp32c6) (push) Waiting to run
CI / setup (esp32s3) (push) Waiting to run
CI / setup (nrf52840) (push) Waiting to run
CI / setup (rp2040) (push) Waiting to run
CI / setup (rp2350) (push) Waiting to run
CI / setup (stm32) (push) Waiting to run
CI / version (push) Waiting to run
CI / check (push) Blocked by required conditions
CI / build-esp32 (push) Blocked by required conditions
CI / build-esp32s3 (push) Blocked by required conditions
CI / build-esp32c3 (push) Blocked by required conditions
CI / build-esp32c6 (push) Blocked by required conditions
CI / build-nrf52840 (push) Blocked by required conditions
CI / build-rp2040 (push) Blocked by required conditions
CI / build-rp2350 (push) Blocked by required conditions
CI / build-stm32 (push) Blocked by required conditions
CI / build-debian-src (push) Waiting to run
CI / package-pio-deps-native-tft (push) Waiting to run
CI / test-native (push) Waiting to run
CI / docker-deb-amd64 (push) Waiting to run
CI / docker-deb-amd64-tft (push) Waiting to run
CI / docker-alp-amd64 (push) Waiting to run
CI / docker-alp-amd64-tft (push) Waiting to run
CI / docker-deb-arm64 (push) Waiting to run
CI / docker-deb-armv7 (push) Waiting to run
CI / gather-artifacts (esp32) (push) Blocked by required conditions
CI / gather-artifacts (esp32c3) (push) Blocked by required conditions
CI / gather-artifacts (esp32c6) (push) Blocked by required conditions
CI / gather-artifacts (esp32s3) (push) Blocked by required conditions
CI / gather-artifacts (nrf52840) (push) Blocked by required conditions
CI / gather-artifacts (rp2040) (push) Blocked by required conditions
CI / gather-artifacts (rp2350) (push) Blocked by required conditions
CI / gather-artifacts (stm32) (push) Blocked by required conditions
CI / release-artifacts (push) Blocked by required conditions
CI / release-firmware (esp32) (push) Blocked by required conditions
CI / release-firmware (esp32c3) (push) Blocked by required conditions
CI / release-firmware (esp32c6) (push) Blocked by required conditions
CI / release-firmware (esp32s3) (push) Blocked by required conditions
CI / release-firmware (nrf52840) (push) Blocked by required conditions
CI / release-firmware (rp2040) (push) Blocked by required conditions
CI / release-firmware (rp2350) (push) Blocked by required conditions
CI / release-firmware (stm32) (push) Blocked by required conditions
CI / publish-firmware (push) Blocked by required conditions
Some checks are pending
CI / setup (check) (push) Waiting to run
CI / setup (esp32) (push) Waiting to run
CI / setup (esp32c3) (push) Waiting to run
CI / setup (esp32c6) (push) Waiting to run
CI / setup (esp32s3) (push) Waiting to run
CI / setup (nrf52840) (push) Waiting to run
CI / setup (rp2040) (push) Waiting to run
CI / setup (rp2350) (push) Waiting to run
CI / setup (stm32) (push) Waiting to run
CI / version (push) Waiting to run
CI / check (push) Blocked by required conditions
CI / build-esp32 (push) Blocked by required conditions
CI / build-esp32s3 (push) Blocked by required conditions
CI / build-esp32c3 (push) Blocked by required conditions
CI / build-esp32c6 (push) Blocked by required conditions
CI / build-nrf52840 (push) Blocked by required conditions
CI / build-rp2040 (push) Blocked by required conditions
CI / build-rp2350 (push) Blocked by required conditions
CI / build-stm32 (push) Blocked by required conditions
CI / build-debian-src (push) Waiting to run
CI / package-pio-deps-native-tft (push) Waiting to run
CI / test-native (push) Waiting to run
CI / docker-deb-amd64 (push) Waiting to run
CI / docker-deb-amd64-tft (push) Waiting to run
CI / docker-alp-amd64 (push) Waiting to run
CI / docker-alp-amd64-tft (push) Waiting to run
CI / docker-deb-arm64 (push) Waiting to run
CI / docker-deb-armv7 (push) Waiting to run
CI / gather-artifacts (esp32) (push) Blocked by required conditions
CI / gather-artifacts (esp32c3) (push) Blocked by required conditions
CI / gather-artifacts (esp32c6) (push) Blocked by required conditions
CI / gather-artifacts (esp32s3) (push) Blocked by required conditions
CI / gather-artifacts (nrf52840) (push) Blocked by required conditions
CI / gather-artifacts (rp2040) (push) Blocked by required conditions
CI / gather-artifacts (rp2350) (push) Blocked by required conditions
CI / gather-artifacts (stm32) (push) Blocked by required conditions
CI / release-artifacts (push) Blocked by required conditions
CI / release-firmware (esp32) (push) Blocked by required conditions
CI / release-firmware (esp32c3) (push) Blocked by required conditions
CI / release-firmware (esp32c6) (push) Blocked by required conditions
CI / release-firmware (esp32s3) (push) Blocked by required conditions
CI / release-firmware (nrf52840) (push) Blocked by required conditions
CI / release-firmware (rp2040) (push) Blocked by required conditions
CI / release-firmware (rp2350) (push) Blocked by required conditions
CI / release-firmware (stm32) (push) Blocked by required conditions
CI / publish-firmware (push) Blocked by required conditions
* Add more text message test cases for meshpacket serializer * fix the trunk issue
This commit is contained in:
parent
4fef890466
commit
103ea2f168
@ -1,42 +1,105 @@
|
|||||||
#include "../test_helpers.h"
|
#include "../test_helpers.h"
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
// Helper function to test common packet fields and structure
|
||||||
|
void verify_text_message_packet_structure(const std::string &json, const char *expected_text)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(json.length() > 0);
|
||||||
|
|
||||||
|
// Use smart pointer for automatic memory management
|
||||||
|
std::unique_ptr<JSONValue> root(JSON::Parse(json.c_str()));
|
||||||
|
TEST_ASSERT_NOT_NULL(root.get());
|
||||||
|
TEST_ASSERT_TRUE(root->IsObject());
|
||||||
|
|
||||||
|
JSONObject jsonObj = root->AsObject();
|
||||||
|
|
||||||
|
// Check basic packet fields - use helper function to reduce duplication
|
||||||
|
auto check_field = [&](const char *field, uint32_t expected_value) {
|
||||||
|
auto it = jsonObj.find(field);
|
||||||
|
TEST_ASSERT_TRUE(it != jsonObj.end());
|
||||||
|
TEST_ASSERT_EQUAL(expected_value, (uint32_t)it->second->AsNumber());
|
||||||
|
};
|
||||||
|
|
||||||
|
check_field("from", 0x11223344);
|
||||||
|
check_field("to", 0x55667788);
|
||||||
|
check_field("id", 0x9999);
|
||||||
|
|
||||||
|
// Check message type
|
||||||
|
auto type_it = jsonObj.find("type");
|
||||||
|
TEST_ASSERT_TRUE(type_it != jsonObj.end());
|
||||||
|
TEST_ASSERT_EQUAL_STRING("text", type_it->second->AsString().c_str());
|
||||||
|
|
||||||
|
// Check payload
|
||||||
|
auto payload_it = jsonObj.find("payload");
|
||||||
|
TEST_ASSERT_TRUE(payload_it != jsonObj.end());
|
||||||
|
TEST_ASSERT_TRUE(payload_it->second->IsObject());
|
||||||
|
|
||||||
|
JSONObject payload = payload_it->second->AsObject();
|
||||||
|
auto text_it = payload.find("text");
|
||||||
|
TEST_ASSERT_TRUE(text_it != payload.end());
|
||||||
|
TEST_ASSERT_EQUAL_STRING(expected_text, text_it->second->AsString().c_str());
|
||||||
|
|
||||||
|
// No need for manual delete with smart pointer
|
||||||
|
}
|
||||||
|
|
||||||
// Test TEXT_MESSAGE_APP port
|
// Test TEXT_MESSAGE_APP port
|
||||||
void test_text_message_serialization()
|
void test_text_message_serialization()
|
||||||
{
|
{
|
||||||
const char *test_text = "Hello Meshtastic!";
|
const char *test_text = "Hello Meshtastic!";
|
||||||
meshtastic_MeshPacket packet =
|
meshtastic_MeshPacket packet =
|
||||||
create_test_packet(meshtastic_PortNum_TEXT_MESSAGE_APP, (const uint8_t *)test_text, strlen(test_text));
|
create_test_packet(meshtastic_PortNum_TEXT_MESSAGE_APP, reinterpret_cast<const uint8_t *>(test_text), strlen(test_text));
|
||||||
|
|
||||||
std::string json = MeshPacketSerializer::JsonSerialize(&packet, false);
|
std::string json = MeshPacketSerializer::JsonSerialize(&packet, false);
|
||||||
TEST_ASSERT_TRUE(json.length() > 0);
|
verify_text_message_packet_structure(json, test_text);
|
||||||
|
}
|
||||||
JSONValue *root = JSON::Parse(json.c_str());
|
|
||||||
TEST_ASSERT_NOT_NULL(root);
|
// Test with nullptr to check robustness
|
||||||
TEST_ASSERT_TRUE(root->IsObject());
|
void test_text_message_serialization_null()
|
||||||
|
{
|
||||||
JSONObject jsonObj = root->AsObject();
|
meshtastic_MeshPacket packet = create_test_packet(meshtastic_PortNum_TEXT_MESSAGE_APP, nullptr, 0);
|
||||||
|
|
||||||
// Check basic packet fields
|
std::string json = MeshPacketSerializer::JsonSerialize(&packet, false);
|
||||||
TEST_ASSERT_TRUE(jsonObj.find("from") != jsonObj.end());
|
verify_text_message_packet_structure(json, "");
|
||||||
TEST_ASSERT_EQUAL(0x11223344, (uint32_t)jsonObj["from"]->AsNumber());
|
}
|
||||||
|
|
||||||
TEST_ASSERT_TRUE(jsonObj.find("to") != jsonObj.end());
|
// Test TEXT_MESSAGE_APP port with very long message (boundary testing)
|
||||||
TEST_ASSERT_EQUAL(0x55667788, (uint32_t)jsonObj["to"]->AsNumber());
|
void test_text_message_serialization_long_text()
|
||||||
|
{
|
||||||
TEST_ASSERT_TRUE(jsonObj.find("id") != jsonObj.end());
|
// Test with actual message size limits
|
||||||
TEST_ASSERT_EQUAL(0x9999, (uint32_t)jsonObj["id"]->AsNumber());
|
constexpr size_t MAX_MESSAGE_SIZE = 200; // Typical LoRa payload limit
|
||||||
|
std::string long_text(MAX_MESSAGE_SIZE, 'A');
|
||||||
// Check message type
|
|
||||||
TEST_ASSERT_TRUE(jsonObj.find("type") != jsonObj.end());
|
meshtastic_MeshPacket packet = create_test_packet(meshtastic_PortNum_TEXT_MESSAGE_APP,
|
||||||
TEST_ASSERT_EQUAL_STRING("text", jsonObj["type"]->AsString().c_str());
|
reinterpret_cast<const uint8_t *>(long_text.c_str()), long_text.length());
|
||||||
|
|
||||||
// Check payload
|
std::string json = MeshPacketSerializer::JsonSerialize(&packet, false);
|
||||||
TEST_ASSERT_TRUE(jsonObj.find("payload") != jsonObj.end());
|
verify_text_message_packet_structure(json, long_text.c_str());
|
||||||
TEST_ASSERT_TRUE(jsonObj["payload"]->IsObject());
|
}
|
||||||
|
|
||||||
JSONObject payload = jsonObj["payload"]->AsObject();
|
// Test with message over size limit (should fail)
|
||||||
TEST_ASSERT_TRUE(payload.find("text") != payload.end());
|
void test_text_message_serialization_oversized()
|
||||||
TEST_ASSERT_EQUAL_STRING("Hello Meshtastic!", payload["text"]->AsString().c_str());
|
{
|
||||||
|
constexpr size_t OVERSIZED_MESSAGE = 250; // Over the limit
|
||||||
delete root;
|
std::string oversized_text(OVERSIZED_MESSAGE, 'B');
|
||||||
|
|
||||||
|
meshtastic_MeshPacket packet = create_test_packet(
|
||||||
|
meshtastic_PortNum_TEXT_MESSAGE_APP, reinterpret_cast<const uint8_t *>(oversized_text.c_str()), oversized_text.length());
|
||||||
|
|
||||||
|
// Should fail or return empty/error
|
||||||
|
std::string json = MeshPacketSerializer::JsonSerialize(&packet, false);
|
||||||
|
// Should only verify first 234 characters for oversized messages
|
||||||
|
std::string expected_text = oversized_text.substr(0, 234);
|
||||||
|
verify_text_message_packet_structure(json, expected_text.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add test for malformed UTF-8 sequences
|
||||||
|
void test_text_message_serialization_invalid_utf8()
|
||||||
|
{
|
||||||
|
const uint8_t invalid_utf8[] = {0xFF, 0xFE, 0xFD, 0x00}; // Invalid UTF-8
|
||||||
|
meshtastic_MeshPacket packet =
|
||||||
|
create_test_packet(meshtastic_PortNum_TEXT_MESSAGE_APP, invalid_utf8, sizeof(invalid_utf8) - 1);
|
||||||
|
|
||||||
|
// Should not crash, may produce replacement characters
|
||||||
|
std::string json = MeshPacketSerializer::JsonSerialize(&packet, false);
|
||||||
|
TEST_ASSERT_TRUE(json.length() > 0);
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,10 @@
|
|||||||
|
|
||||||
// Forward declarations for test functions
|
// Forward declarations for test functions
|
||||||
void test_text_message_serialization();
|
void test_text_message_serialization();
|
||||||
|
void test_text_message_serialization_null();
|
||||||
|
void test_text_message_serialization_long_text();
|
||||||
|
void test_text_message_serialization_oversized();
|
||||||
|
void test_text_message_serialization_invalid_utf8();
|
||||||
void test_position_serialization();
|
void test_position_serialization();
|
||||||
void test_nodeinfo_serialization();
|
void test_nodeinfo_serialization();
|
||||||
void test_waypoint_serialization();
|
void test_waypoint_serialization();
|
||||||
@ -21,6 +25,10 @@ void setup()
|
|||||||
|
|
||||||
// Text message tests
|
// Text message tests
|
||||||
RUN_TEST(test_text_message_serialization);
|
RUN_TEST(test_text_message_serialization);
|
||||||
|
RUN_TEST(test_text_message_serialization_null);
|
||||||
|
RUN_TEST(test_text_message_serialization_long_text);
|
||||||
|
RUN_TEST(test_text_message_serialization_oversized);
|
||||||
|
RUN_TEST(test_text_message_serialization_invalid_utf8);
|
||||||
|
|
||||||
// Position tests
|
// Position tests
|
||||||
RUN_TEST(test_position_serialization);
|
RUN_TEST(test_position_serialization);
|
||||||
|
Loading…
Reference in New Issue
Block a user