Skip to content

Commit 103ea2f

Browse files
authored
Add more text message test cases for meshpacket serializer (#7709)
* Add more text message test cases for meshpacket serializer * fix the trunk issue
1 parent 4fef890 commit 103ea2f

File tree

2 files changed

+94
-23
lines changed

2 files changed

+94
-23
lines changed
Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,105 @@
11
#include "../test_helpers.h"
2+
#include <memory>
3+
4+
// Helper function to test common packet fields and structure
5+
void verify_text_message_packet_structure(const std::string &json, const char *expected_text)
6+
{
7+
TEST_ASSERT_TRUE(json.length() > 0);
8+
9+
// Use smart pointer for automatic memory management
10+
std::unique_ptr<JSONValue> root(JSON::Parse(json.c_str()));
11+
TEST_ASSERT_NOT_NULL(root.get());
12+
TEST_ASSERT_TRUE(root->IsObject());
13+
14+
JSONObject jsonObj = root->AsObject();
15+
16+
// Check basic packet fields - use helper function to reduce duplication
17+
auto check_field = [&](const char *field, uint32_t expected_value) {
18+
auto it = jsonObj.find(field);
19+
TEST_ASSERT_TRUE(it != jsonObj.end());
20+
TEST_ASSERT_EQUAL(expected_value, (uint32_t)it->second->AsNumber());
21+
};
22+
23+
check_field("from", 0x11223344);
24+
check_field("to", 0x55667788);
25+
check_field("id", 0x9999);
26+
27+
// Check message type
28+
auto type_it = jsonObj.find("type");
29+
TEST_ASSERT_TRUE(type_it != jsonObj.end());
30+
TEST_ASSERT_EQUAL_STRING("text", type_it->second->AsString().c_str());
31+
32+
// Check payload
33+
auto payload_it = jsonObj.find("payload");
34+
TEST_ASSERT_TRUE(payload_it != jsonObj.end());
35+
TEST_ASSERT_TRUE(payload_it->second->IsObject());
36+
37+
JSONObject payload = payload_it->second->AsObject();
38+
auto text_it = payload.find("text");
39+
TEST_ASSERT_TRUE(text_it != payload.end());
40+
TEST_ASSERT_EQUAL_STRING(expected_text, text_it->second->AsString().c_str());
41+
42+
// No need for manual delete with smart pointer
43+
}
244

345
// Test TEXT_MESSAGE_APP port
446
void test_text_message_serialization()
547
{
648
const char *test_text = "Hello Meshtastic!";
749
meshtastic_MeshPacket packet =
8-
create_test_packet(meshtastic_PortNum_TEXT_MESSAGE_APP, (const uint8_t *)test_text, strlen(test_text));
50+
create_test_packet(meshtastic_PortNum_TEXT_MESSAGE_APP, reinterpret_cast<const uint8_t *>(test_text), strlen(test_text));
951

1052
std::string json = MeshPacketSerializer::JsonSerialize(&packet, false);
11-
TEST_ASSERT_TRUE(json.length() > 0);
53+
verify_text_message_packet_structure(json, test_text);
54+
}
1255

13-
JSONValue *root = JSON::Parse(json.c_str());
14-
TEST_ASSERT_NOT_NULL(root);
15-
TEST_ASSERT_TRUE(root->IsObject());
56+
// Test with nullptr to check robustness
57+
void test_text_message_serialization_null()
58+
{
59+
meshtastic_MeshPacket packet = create_test_packet(meshtastic_PortNum_TEXT_MESSAGE_APP, nullptr, 0);
1660

17-
JSONObject jsonObj = root->AsObject();
61+
std::string json = MeshPacketSerializer::JsonSerialize(&packet, false);
62+
verify_text_message_packet_structure(json, "");
63+
}
1864

19-
// Check basic packet fields
20-
TEST_ASSERT_TRUE(jsonObj.find("from") != jsonObj.end());
21-
TEST_ASSERT_EQUAL(0x11223344, (uint32_t)jsonObj["from"]->AsNumber());
65+
// Test TEXT_MESSAGE_APP port with very long message (boundary testing)
66+
void test_text_message_serialization_long_text()
67+
{
68+
// Test with actual message size limits
69+
constexpr size_t MAX_MESSAGE_SIZE = 200; // Typical LoRa payload limit
70+
std::string long_text(MAX_MESSAGE_SIZE, 'A');
2271

23-
TEST_ASSERT_TRUE(jsonObj.find("to") != jsonObj.end());
24-
TEST_ASSERT_EQUAL(0x55667788, (uint32_t)jsonObj["to"]->AsNumber());
72+
meshtastic_MeshPacket packet = create_test_packet(meshtastic_PortNum_TEXT_MESSAGE_APP,
73+
reinterpret_cast<const uint8_t *>(long_text.c_str()), long_text.length());
2574

26-
TEST_ASSERT_TRUE(jsonObj.find("id") != jsonObj.end());
27-
TEST_ASSERT_EQUAL(0x9999, (uint32_t)jsonObj["id"]->AsNumber());
75+
std::string json = MeshPacketSerializer::JsonSerialize(&packet, false);
76+
verify_text_message_packet_structure(json, long_text.c_str());
77+
}
2878

29-
// Check message type
30-
TEST_ASSERT_TRUE(jsonObj.find("type") != jsonObj.end());
31-
TEST_ASSERT_EQUAL_STRING("text", jsonObj["type"]->AsString().c_str());
79+
// Test with message over size limit (should fail)
80+
void test_text_message_serialization_oversized()
81+
{
82+
constexpr size_t OVERSIZED_MESSAGE = 250; // Over the limit
83+
std::string oversized_text(OVERSIZED_MESSAGE, 'B');
3284

33-
// Check payload
34-
TEST_ASSERT_TRUE(jsonObj.find("payload") != jsonObj.end());
35-
TEST_ASSERT_TRUE(jsonObj["payload"]->IsObject());
85+
meshtastic_MeshPacket packet = create_test_packet(
86+
meshtastic_PortNum_TEXT_MESSAGE_APP, reinterpret_cast<const uint8_t *>(oversized_text.c_str()), oversized_text.length());
3687

37-
JSONObject payload = jsonObj["payload"]->AsObject();
38-
TEST_ASSERT_TRUE(payload.find("text") != payload.end());
39-
TEST_ASSERT_EQUAL_STRING("Hello Meshtastic!", payload["text"]->AsString().c_str());
88+
// Should fail or return empty/error
89+
std::string json = MeshPacketSerializer::JsonSerialize(&packet, false);
90+
// Should only verify first 234 characters for oversized messages
91+
std::string expected_text = oversized_text.substr(0, 234);
92+
verify_text_message_packet_structure(json, expected_text.c_str());
93+
}
4094

41-
delete root;
95+
// Add test for malformed UTF-8 sequences
96+
void test_text_message_serialization_invalid_utf8()
97+
{
98+
const uint8_t invalid_utf8[] = {0xFF, 0xFE, 0xFD, 0x00}; // Invalid UTF-8
99+
meshtastic_MeshPacket packet =
100+
create_test_packet(meshtastic_PortNum_TEXT_MESSAGE_APP, invalid_utf8, sizeof(invalid_utf8) - 1);
101+
102+
// Should not crash, may produce replacement characters
103+
std::string json = MeshPacketSerializer::JsonSerialize(&packet, false);
104+
TEST_ASSERT_TRUE(json.length() > 0);
42105
}

test/test_meshpacket_serializer/test_serializer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
// Forward declarations for test functions
66
void test_text_message_serialization();
7+
void test_text_message_serialization_null();
8+
void test_text_message_serialization_long_text();
9+
void test_text_message_serialization_oversized();
10+
void test_text_message_serialization_invalid_utf8();
711
void test_position_serialization();
812
void test_nodeinfo_serialization();
913
void test_waypoint_serialization();
@@ -21,6 +25,10 @@ void setup()
2125

2226
// Text message tests
2327
RUN_TEST(test_text_message_serialization);
28+
RUN_TEST(test_text_message_serialization_null);
29+
RUN_TEST(test_text_message_serialization_long_text);
30+
RUN_TEST(test_text_message_serialization_oversized);
31+
RUN_TEST(test_text_message_serialization_invalid_utf8);
2432

2533
// Position tests
2634
RUN_TEST(test_position_serialization);

0 commit comments

Comments
 (0)