|
1 | 1 | #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 | +} |
2 | 44 |
|
3 | 45 | // Test TEXT_MESSAGE_APP port
|
4 | 46 | void test_text_message_serialization()
|
5 | 47 | {
|
6 | 48 | const char *test_text = "Hello Meshtastic!";
|
7 | 49 | 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)); |
9 | 51 |
|
10 | 52 | std::string json = MeshPacketSerializer::JsonSerialize(&packet, false);
|
11 |
| - TEST_ASSERT_TRUE(json.length() > 0); |
| 53 | + verify_text_message_packet_structure(json, test_text); |
| 54 | +} |
12 | 55 |
|
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); |
16 | 60 |
|
17 |
| - JSONObject jsonObj = root->AsObject(); |
| 61 | + std::string json = MeshPacketSerializer::JsonSerialize(&packet, false); |
| 62 | + verify_text_message_packet_structure(json, ""); |
| 63 | +} |
18 | 64 |
|
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'); |
22 | 71 |
|
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()); |
25 | 74 |
|
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 | +} |
28 | 78 |
|
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'); |
32 | 84 |
|
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()); |
36 | 87 |
|
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 | +} |
40 | 94 |
|
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); |
42 | 105 | }
|
0 commit comments