From a90944e4bc8e60a00433c86536fc2465460c2408 Mon Sep 17 00:00:00 2001 From: seladb Date: Sat, 5 Aug 2023 19:36:54 -0700 Subject: [PATCH] - Remove the filter test - Instead, add a test to read and write SLL2 pcap files --- Tests/Pcap++Test/Common/PcapFileNamesDef.h | 3 +- .../{Sll2Packet.pcap => sll2.pcap} | Bin Tests/Pcap++Test/TestDefinition.h | 1 + Tests/Pcap++Test/Tests/FileTests.cpp | 44 ++++++++++++++++++ Tests/Pcap++Test/Tests/FilterTests.cpp | 13 ++---- Tests/Pcap++Test/main.cpp | 1 + 6 files changed, 51 insertions(+), 11 deletions(-) rename Tests/Pcap++Test/PcapExamples/{Sll2Packet.pcap => sll2.pcap} (100%) diff --git a/Tests/Pcap++Test/Common/PcapFileNamesDef.h b/Tests/Pcap++Test/Common/PcapFileNamesDef.h index 20c3ad3eb9..f737ac14a5 100644 --- a/Tests/Pcap++Test/Common/PcapFileNamesDef.h +++ b/Tests/Pcap++Test/Common/PcapFileNamesDef.h @@ -24,4 +24,5 @@ #define EXAMPLE_LINKTYPE_IPV6 "PcapExamples/linktype_ipv6.pcap" #define EXAMPLE_LINKTYPE_IPV4 "PcapExamples/linktype_ipv4.pcap" #define EXAMPLE_SOLARIS_SNOOP "PcapExamples/solaris.snoop" -#define SLL2_PCAP_PATH "PcapExamples/Sll2Packet.pcap" +#define SLL2_PCAP_PATH "PcapExamples/sll2.pcap" +#define SLL2_PCAP_WRITE_PATH "PcapExamples/sll2_copy.pcap" diff --git a/Tests/Pcap++Test/PcapExamples/Sll2Packet.pcap b/Tests/Pcap++Test/PcapExamples/sll2.pcap similarity index 100% rename from Tests/Pcap++Test/PcapExamples/Sll2Packet.pcap rename to Tests/Pcap++Test/PcapExamples/sll2.pcap diff --git a/Tests/Pcap++Test/TestDefinition.h b/Tests/Pcap++Test/TestDefinition.h index 48f9dfdb8b..bebcd9a917 100644 --- a/Tests/Pcap++Test/TestDefinition.h +++ b/Tests/Pcap++Test/TestDefinition.h @@ -19,6 +19,7 @@ PTF_TEST_CASE(TestLoggerMultiThread); // Implemented in FileTests.cpp PTF_TEST_CASE(TestPcapFileReadWrite); PTF_TEST_CASE(TestPcapSllFileReadWrite); +PTF_TEST_CASE(TestPcapSll2FileReadWrite); PTF_TEST_CASE(TestPcapRawIPFileReadWrite); PTF_TEST_CASE(TestPcapFileAppend); PTF_TEST_CASE(TestPcapNgFileReadWrite); diff --git a/Tests/Pcap++Test/Tests/FileTests.cpp b/Tests/Pcap++Test/Tests/FileTests.cpp index 2c1926ee0b..3014febae1 100644 --- a/Tests/Pcap++Test/Tests/FileTests.cpp +++ b/Tests/Pcap++Test/Tests/FileTests.cpp @@ -160,6 +160,50 @@ PTF_TEST_CASE(TestPcapSllFileReadWrite) +PTF_TEST_CASE(TestPcapSll2FileReadWrite) +{ + pcpp::PcapFileReaderDevice readerDev(SLL2_PCAP_PATH); + pcpp::PcapFileWriterDevice writerDev(SLL2_PCAP_WRITE_PATH, pcpp::LINKTYPE_LINUX_SLL2); + PTF_ASSERT_TRUE(readerDev.open()); + PTF_ASSERT_TRUE(writerDev.open()); + pcpp::RawPacket rawPacket; + int packetCount = 0; + int sll2Count = 0; + int ipCount = 0; + + while (readerDev.getNextPacket(rawPacket)) + { + packetCount++; + pcpp::Packet packet(&rawPacket); + if (packet.isPacketOfType(pcpp::SLL2)) + sll2Count++; + if (packet.isPacketOfType(pcpp::IP)) + ipCount++; + + PTF_ASSERT_TRUE(writerDev.writePacket(rawPacket)); + } + + pcpp::IPcapDevice::PcapStats readerStatistics; + pcpp::IPcapDevice::PcapStats writerStatistics; + + readerDev.getStatistics(readerStatistics); + PTF_ASSERT_EQUAL((uint32_t)readerStatistics.packetsRecv, 3); + PTF_ASSERT_EQUAL((uint32_t)readerStatistics.packetsDrop, 0); + + writerDev.getStatistics(writerStatistics); + PTF_ASSERT_EQUAL((uint32_t)writerStatistics.packetsRecv, 3); + PTF_ASSERT_EQUAL((uint32_t)writerStatistics.packetsDrop, 0); + + PTF_ASSERT_EQUAL(packetCount, 3); + PTF_ASSERT_EQUAL(sll2Count, 3); + PTF_ASSERT_EQUAL(ipCount, 3); + + readerDev.close(); + writerDev.close(); +} // TestPcapSll2FileReadWrite + + + PTF_TEST_CASE(TestPcapRawIPFileReadWrite) { pcpp::Logger::getInstance().suppressLogs(); diff --git a/Tests/Pcap++Test/Tests/FilterTests.cpp b/Tests/Pcap++Test/Tests/FilterTests.cpp index 77e521a39e..cb409d29a2 100644 --- a/Tests/Pcap++Test/Tests/FilterTests.cpp +++ b/Tests/Pcap++Test/Tests/FilterTests.cpp @@ -737,7 +737,7 @@ PTF_TEST_CASE(TestPcapFilters_LinkLayer) { // check if matchPacketWithFilter work properly for packets with different LinkLayerType -// pcpp::LINKTYPE_DLT_RAW1 layer + // pcpp::LINKTYPE_DLT_RAW1 layer pcpp::PcapFileReaderDevice fileReaderDev1(RAW_IP_PCAP_PATH); PTF_ASSERT_TRUE(fileReaderDev1.open()); pcpp::RawPacketVector rawPacketVec; @@ -764,7 +764,7 @@ PTF_TEST_CASE(TestPcapFilters_LinkLayer) rawPacketVec.clear(); -// pcpp::LINKTYPE_LINUX_SLL layer + // pcpp::LINKTYPE_LINUX_SLL layer pcpp::PcapFileReaderDevice fileReaderDev2(SLL_PCAP_PATH); PTF_ASSERT_TRUE(fileReaderDev2.open()); fileReaderDev2.getNextPackets(rawPacketVec); @@ -790,7 +790,7 @@ PTF_TEST_CASE(TestPcapFilters_LinkLayer) rawPacketVec.clear(); -// pcpp::LINKTYPE_ETHERNET layer + // pcpp::LINKTYPE_ETHERNET layer pcpp::PcapNgFileReaderDevice fileReaderDev3(EXAMPLE_PCAPNG_PATH); PTF_ASSERT_TRUE(fileReaderDev3.open()); fileReaderDev3.getNextPackets(rawPacketVec); @@ -814,11 +814,4 @@ PTF_TEST_CASE(TestPcapFilters_LinkLayer) } PTF_ASSERT_EQUAL(validCounter, 62); rawPacketVec.clear(); - - -// pcpp::LINKTYPE_LINUX_SLL2 layer - pcpp::PcapFileReaderDevice fileReaderDev4(SLL2_PCAP_PATH); - PTF_ASSERT_TRUE(fileReaderDev4.open()); - fileReaderDev4.getNextPackets(rawPacketVec); - fileReaderDev4.close(); } // TestPcapFilters_LinkLayer diff --git a/Tests/Pcap++Test/main.cpp b/Tests/Pcap++Test/main.cpp index 19dda878c4..a62f94fa56 100644 --- a/Tests/Pcap++Test/main.cpp +++ b/Tests/Pcap++Test/main.cpp @@ -213,6 +213,7 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(TestPcapFileReadWrite, "no_network;pcap"); PTF_RUN_TEST(TestPcapSllFileReadWrite, "no_network;pcap"); + PTF_RUN_TEST(TestPcapSll2FileReadWrite, "no_network;pcap"); PTF_RUN_TEST(TestPcapRawIPFileReadWrite, "no_network;pcap"); PTF_RUN_TEST(TestPcapFileAppend, "no_network;pcap"); PTF_RUN_TEST(TestPcapNgFileReadWrite, "no_network;pcap;pcapng");