diff --git a/Tests/Packet++Test/PacketExamples/igmpv3_report_truncated.dat b/Tests/Packet++Test/PacketExamples/igmpv3_report_truncated.dat new file mode 100644 index 0000000000..4ba8c47772 --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/igmpv3_report_truncated.dat @@ -0,0 +1 @@ +01005e00001600000000000108004500002000000000010200000a000001e0000016220000000000000101000001 \ No newline at end of file diff --git a/Tests/Packet++Test/Tests/IgmpTests.cpp b/Tests/Packet++Test/Tests/IgmpTests.cpp index 7a74688d13..fb17c3f8b6 100644 --- a/Tests/Packet++Test/Tests/IgmpTests.cpp +++ b/Tests/Packet++Test/Tests/IgmpTests.cpp @@ -153,18 +153,12 @@ PTF_TEST_CASE(Igmpv3ParsingTest) // Only the start of a record was checked against the layer, so a truncated one was returned // and its fields were then read past the end of the buffer. { - uint8_t truncated[] = { // Ethernet - 0x01, 0x00, 0x5e, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, - // IPv4, total length 32, protocol 2 (IGMP) - 0x45, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x0a, 0x00, - 0x00, 0x01, 0xe0, 0x00, 0x00, 0x16, - // IGMPv3 report: 8 byte header claiming one group record, then only 4 bytes of it - 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01 - }; - - timeval truncatedTime = {}; - pcpp::RawPacket truncatedRawPacket(truncated, sizeof(truncated), truncatedTime, false, pcpp::LINKTYPE_ETHERNET); - pcpp::Packet truncatedPacket(&truncatedRawPacket); + // Packet contents + // Ethernet + // IPv4, total length 32, protocol 2 (IGMP) + // IGMPv3 report: 8 byte header claiming one group record, then only 4 bytes of it + auto truncatedRawPacket = createPacketFromHexResource("PacketExamples/igmpv3_report_truncated.dat"); + pcpp::Packet truncatedPacket(truncatedRawPacket.get()); auto truncatedReportLayer = truncatedPacket.getLayerOfType(); PTF_ASSERT_NOT_NULL(truncatedReportLayer); diff --git a/Tests/PcppTestUtilities/Resources.cpp b/Tests/PcppTestUtilities/Resources.cpp index ef991223ce..11d76e7ee5 100644 --- a/Tests/PcppTestUtilities/Resources.cpp +++ b/Tests/PcppTestUtilities/Resources.cpp @@ -104,7 +104,7 @@ namespace pcpp_tests } void ResourceProvider::saveResource(ResourceType resourceType, const char* filename, const uint8_t* data, - size_t length) const + size_t length) { if (m_Frozen) { diff --git a/Tests/PcppTestUtilities/Resources.h b/Tests/PcppTestUtilities/Resources.h index f18fce0274..11b9209e94 100644 --- a/Tests/PcppTestUtilities/Resources.h +++ b/Tests/PcppTestUtilities/Resources.h @@ -32,6 +32,18 @@ namespace pcpp_tests /// @param frozen If true, the provider is read-only and does not allow saving resources. explicit ResourceProvider(std::string dataRoot, bool frozen = true); + /// @brief Freezes the provider, preventing any modifications or saving of resources. + void freeze() + { + m_Frozen = true; + } + + /// @brief Allows the provider to be modified, enabling saving of resources. + void unfreeze() + { + m_Frozen = false; + } + /// @brief Loads a resource from resource provider. /// @param filename The name of the resource file to load. /// @param resourceType The type of the loaded resource. Determines how the resource is processed. @@ -50,8 +62,7 @@ namespace pcpp_tests /// @param data Pointer to the data to be saved. /// @param length The length of the data in bytes. /// @throw std::runtime_error if the provider is frozen and does not allow saving. - void saveResource(ResourceType resourceType, const char* filename, const uint8_t* data, - size_t length) const; + void saveResource(ResourceType resourceType, const char* filename, const uint8_t* data, size_t length); private: std::string m_DataRoot; ///< The root directory for test data files