Skip to content

Commit

Permalink
Fix ~PcapFileWriterDevice() to close the pcap file. (#1577)
Browse files Browse the repository at this point in the history
  • Loading branch information
rndx21033 authored Oct 1, 2024
1 parent dc8ecc7 commit 462ccee
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Pcap++/header/PcapFileDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,9 @@ namespace pcpp
* A destructor for this class
*/
~PcapFileWriterDevice()
{}
{
PcapFileWriterDevice::close();
}

/**
* Write a RawPacket to the file. Before using this method please verify the file is opened using open(). This
Expand Down
2 changes: 2 additions & 0 deletions Tests/Pcap++Test/Common/PcapFileNamesDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@
#define SLL2_PCAP_PATH "PcapExamples/sll2.pcap"
#define SLL2_PCAP_WRITE_PATH "PcapExamples/sll2_copy.pcap"
#define EXAMPLE_PCAP_MICRO_PATH "PcapExamples/microsecs.pcap"
#define EXAMPLE_PCAP_DESTRUCTOR1_PATH "PcapExamples/destructor1.pcap"
#define EXAMPLE_PCAP_DESTRUCTOR2_PATH "PcapExamples/destructor2.pcap"
#define EXAMPLE_PCAP_NANO_PATH "PcapExamples/nanosecs.pcap"
#define EXAMPLE_PCAPNG_NANO_PATH "PcapExamples/nanosecs.pcapng"
1 change: 1 addition & 0 deletions Tests/Pcap++Test/TestDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ PTF_TEST_CASE(TestPcapFileReadLinkTypeIPv6);
PTF_TEST_CASE(TestPcapFileReadLinkTypeIPv4);
PTF_TEST_CASE(TestSolarisSnoopFileRead);
PTF_TEST_CASE(TestPcapNgFilePrecision);
PTF_TEST_CASE(TestPcapFileWriterDeviceDestructor);

// Implemented in LiveDeviceTests.cpp
PTF_TEST_CASE(TestPcapLiveDeviceList);
Expand Down
42 changes: 42 additions & 0 deletions Tests/Pcap++Test/Tests/FileTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,3 +974,45 @@ PTF_TEST_CASE(TestSolarisSnoopFileRead)

readerDev.close();
} // TestSolarisSnoopFileRead

PTF_TEST_CASE(TestPcapFileWriterDeviceDestructor)
{
std::array<uint8_t, 16> testPayload = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
pcpp::RawPacket rawPacket1(testPayload.data(), testPayload.size(), timeval{}, false);
pcpp::RawPacket rawPacket2(testPayload.data(), testPayload.size(), timeval{}, false);

// Create some pcaps in a nested scope to test cleanup on destruction.
{
// create a file to leave open on destruction. If close is properly done on destruction, the contents & size of
// this file should match the next explicitly closed file.
pcpp::PcapFileWriterDevice writerDevDestructorNoClose(EXAMPLE_PCAP_DESTRUCTOR1_PATH, pcpp::LINKTYPE_ETHERNET,
false);
PTF_ASSERT_TRUE(writerDevDestructorNoClose.open());
PTF_ASSERT_TRUE(writerDevDestructorNoClose.writePacket(rawPacket1));
PTF_ASSERT_TRUE(writerDevDestructorNoClose.writePacket(rawPacket2));

// create a file that will be explicitly closed before construction
pcpp::PcapFileWriterDevice writerDevDestructorExplicitClose(EXAMPLE_PCAP_DESTRUCTOR2_PATH,
pcpp::LINKTYPE_ETHERNET, false);
PTF_ASSERT_TRUE(writerDevDestructorExplicitClose.open());
PTF_ASSERT_TRUE(writerDevDestructorExplicitClose.writePacket(rawPacket1));
PTF_ASSERT_TRUE(writerDevDestructorExplicitClose.writePacket(rawPacket2));
writerDevDestructorExplicitClose.close();
}

// Check that file sizes are equal. This should fail if the pcpp::PcapFileWriterDevice destructor does not close
// properly.
std::ifstream fileDestructorNoClose(EXAMPLE_PCAP_DESTRUCTOR1_PATH, std::ios::binary | std::ios::in);
fileDestructorNoClose.seekg(0, std::ios::end);
auto posNoClose = fileDestructorNoClose.tellg();

std::ifstream fileDestructorExplicitClose(EXAMPLE_PCAP_DESTRUCTOR2_PATH, std::ios::binary | std::ios::in);
fileDestructorExplicitClose.seekg(0, std::ios::end);
auto posExplicitClose = fileDestructorExplicitClose.tellg();

// sizes should be non-zero and match if files both got closed properly
PTF_ASSERT_NOT_EQUAL(0, posNoClose);
PTF_ASSERT_NOT_EQUAL(0, posExplicitClose);
PTF_ASSERT_EQUAL(posNoClose, posExplicitClose);
} // TestPcapFileWriterDeviceDestructor
1 change: 1 addition & 0 deletions Tests/Pcap++Test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ int main(int argc, char* argv[])
PTF_RUN_TEST(TestPcapFileReadLinkTypeIPv6, "no_network;pcap");
PTF_RUN_TEST(TestPcapFileReadLinkTypeIPv4, "no_network;pcap");
PTF_RUN_TEST(TestSolarisSnoopFileRead, "no_network;pcap;snoop");
PTF_RUN_TEST(TestPcapFileWriterDeviceDestructor, "no_network;pcap");

PTF_RUN_TEST(TestPcapLiveDeviceList, "no_network;live_device;skip_mem_leak_check");
PTF_RUN_TEST(TestPcapLiveDeviceListSearch, "live_device");
Expand Down

0 comments on commit 462ccee

Please sign in to comment.