Skip to content

Commit 462ccee

Browse files
authored
Fix ~PcapFileWriterDevice() to close the pcap file. (#1577)
1 parent dc8ecc7 commit 462ccee

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

Pcap++/header/PcapFileDevice.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,9 @@ namespace pcpp
458458
* A destructor for this class
459459
*/
460460
~PcapFileWriterDevice()
461-
{}
461+
{
462+
PcapFileWriterDevice::close();
463+
}
462464

463465
/**
464466
* Write a RawPacket to the file. Before using this method please verify the file is opened using open(). This

Tests/Pcap++Test/Common/PcapFileNamesDef.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@
3030
#define SLL2_PCAP_PATH "PcapExamples/sll2.pcap"
3131
#define SLL2_PCAP_WRITE_PATH "PcapExamples/sll2_copy.pcap"
3232
#define EXAMPLE_PCAP_MICRO_PATH "PcapExamples/microsecs.pcap"
33+
#define EXAMPLE_PCAP_DESTRUCTOR1_PATH "PcapExamples/destructor1.pcap"
34+
#define EXAMPLE_PCAP_DESTRUCTOR2_PATH "PcapExamples/destructor2.pcap"
3335
#define EXAMPLE_PCAP_NANO_PATH "PcapExamples/nanosecs.pcap"
3436
#define EXAMPLE_PCAPNG_NANO_PATH "PcapExamples/nanosecs.pcapng"

Tests/Pcap++Test/TestDefinition.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ PTF_TEST_CASE(TestPcapFileReadLinkTypeIPv6);
3030
PTF_TEST_CASE(TestPcapFileReadLinkTypeIPv4);
3131
PTF_TEST_CASE(TestSolarisSnoopFileRead);
3232
PTF_TEST_CASE(TestPcapNgFilePrecision);
33+
PTF_TEST_CASE(TestPcapFileWriterDeviceDestructor);
3334

3435
// Implemented in LiveDeviceTests.cpp
3536
PTF_TEST_CASE(TestPcapLiveDeviceList);

Tests/Pcap++Test/Tests/FileTests.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,3 +974,45 @@ PTF_TEST_CASE(TestSolarisSnoopFileRead)
974974

975975
readerDev.close();
976976
} // TestSolarisSnoopFileRead
977+
978+
PTF_TEST_CASE(TestPcapFileWriterDeviceDestructor)
979+
{
980+
std::array<uint8_t, 16> testPayload = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
981+
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
982+
pcpp::RawPacket rawPacket1(testPayload.data(), testPayload.size(), timeval{}, false);
983+
pcpp::RawPacket rawPacket2(testPayload.data(), testPayload.size(), timeval{}, false);
984+
985+
// Create some pcaps in a nested scope to test cleanup on destruction.
986+
{
987+
// create a file to leave open on destruction. If close is properly done on destruction, the contents & size of
988+
// this file should match the next explicitly closed file.
989+
pcpp::PcapFileWriterDevice writerDevDestructorNoClose(EXAMPLE_PCAP_DESTRUCTOR1_PATH, pcpp::LINKTYPE_ETHERNET,
990+
false);
991+
PTF_ASSERT_TRUE(writerDevDestructorNoClose.open());
992+
PTF_ASSERT_TRUE(writerDevDestructorNoClose.writePacket(rawPacket1));
993+
PTF_ASSERT_TRUE(writerDevDestructorNoClose.writePacket(rawPacket2));
994+
995+
// create a file that will be explicitly closed before construction
996+
pcpp::PcapFileWriterDevice writerDevDestructorExplicitClose(EXAMPLE_PCAP_DESTRUCTOR2_PATH,
997+
pcpp::LINKTYPE_ETHERNET, false);
998+
PTF_ASSERT_TRUE(writerDevDestructorExplicitClose.open());
999+
PTF_ASSERT_TRUE(writerDevDestructorExplicitClose.writePacket(rawPacket1));
1000+
PTF_ASSERT_TRUE(writerDevDestructorExplicitClose.writePacket(rawPacket2));
1001+
writerDevDestructorExplicitClose.close();
1002+
}
1003+
1004+
// Check that file sizes are equal. This should fail if the pcpp::PcapFileWriterDevice destructor does not close
1005+
// properly.
1006+
std::ifstream fileDestructorNoClose(EXAMPLE_PCAP_DESTRUCTOR1_PATH, std::ios::binary | std::ios::in);
1007+
fileDestructorNoClose.seekg(0, std::ios::end);
1008+
auto posNoClose = fileDestructorNoClose.tellg();
1009+
1010+
std::ifstream fileDestructorExplicitClose(EXAMPLE_PCAP_DESTRUCTOR2_PATH, std::ios::binary | std::ios::in);
1011+
fileDestructorExplicitClose.seekg(0, std::ios::end);
1012+
auto posExplicitClose = fileDestructorExplicitClose.tellg();
1013+
1014+
// sizes should be non-zero and match if files both got closed properly
1015+
PTF_ASSERT_NOT_EQUAL(0, posNoClose);
1016+
PTF_ASSERT_NOT_EQUAL(0, posExplicitClose);
1017+
PTF_ASSERT_EQUAL(posNoClose, posExplicitClose);
1018+
} // TestPcapFileWriterDeviceDestructor

Tests/Pcap++Test/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ int main(int argc, char* argv[])
227227
PTF_RUN_TEST(TestPcapFileReadLinkTypeIPv6, "no_network;pcap");
228228
PTF_RUN_TEST(TestPcapFileReadLinkTypeIPv4, "no_network;pcap");
229229
PTF_RUN_TEST(TestSolarisSnoopFileRead, "no_network;pcap;snoop");
230+
PTF_RUN_TEST(TestPcapFileWriterDeviceDestructor, "no_network;pcap");
230231

231232
PTF_RUN_TEST(TestPcapLiveDeviceList, "no_network;live_device;skip_mem_leak_check");
232233
PTF_RUN_TEST(TestPcapLiveDeviceListSearch, "live_device");

0 commit comments

Comments
 (0)