19 #include "ns3/pcap-file.h"
39 return ((val >> 8) & 0x00ff) | ((val << 8) & 0xff00);
45 return ((val >> 24) & 0x000000ff) | ((val >> 8) & 0x0000ff00) | ((val << 8) & 0x00ff0000) |
46 ((val << 24) & 0xff000000);
52 FILE* p = std::fopen(filename.c_str(),
"rb");
65 FILE* p = std::fopen(filename.c_str(),
"rb");
71 std::fseek(p, 0, SEEK_END);
73 auto sizeActual = std::ftell(p);
76 return sizeActual == sizeExpected;
93 void DoSetup()
override;
94 void DoRun()
override;
95 void DoTeardown()
override;
101 :
TestCase(
"Check to see that PcapFile::Open with mode std::ios::out works")
112 std::stringstream filename;
144 <<
", \"std::ios::out\") does not create file");
148 <<
", \"std::ios::out\") does not result in an empty file");
157 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
159 f.Init(1234, 5678, 7);
166 "Init () does not result in a file with a pcap file header");
175 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
182 <<
", \"w\") does not result in an empty file");
190 f.Init(1234, 5678, 7);
199 memset(buffer, 0,
sizeof(buffer));
200 f.Write(0, 0, buffer, 128);
203 "Write (write-only-file " <<
m_testFilename <<
") returns error");
221 void DoRun()
override;
228 :
TestCase(
"Check to see that PcapFile::Open with mode std::ios::in works")
239 std::stringstream filename;
265 "Open (non-existing-filename "
266 <<
m_testFilename <<
", \"std::ios::in\") does not return error");
272 <<
", \"std::ios::in\") unexpectedly created a file");
287 "Open (non-initialized-filename "
288 <<
m_testFilename <<
", \"std::ios::in\") does not return error");
299 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
301 f.Init(1234, 5678, 7);
312 <<
", \"std::ios::in\") returns error");
319 f.Write(0, 0, buffer, 128);
322 "Write (read-only-file " <<
m_testFilename <<
") does not return error");
332 class AppendModeCreateTestCase :
public TestCase
335 AppendModeCreateTestCase ();
336 virtual ~AppendModeCreateTestCase ();
340 virtual void DoRun ();
343 std::string m_testFilename;
346 AppendModeCreateTestCase::AppendModeCreateTestCase ()
347 :
TestCase (
"Check to see that PcapFile::Open with mode std::ios::app works")
351 AppendModeCreateTestCase::~AppendModeCreateTestCase ()
356 AppendModeCreateTestCase::DoSetup ()
358 std::stringstream filename;
359 uint32_t n = rand ();
361 m_testFilename = CreateTempDirFilename (filename.str () +
".pcap");
365 AppendModeCreateTestCase::DoTeardown ()
367 if (remove (m_testFilename.c_str ()))
369 NS_LOG_ERROR (
"Failed to delete file " << m_testFilename);
374 AppendModeCreateTestCase::DoRun ()
383 ", \"std::ios::app\") does not return error");
388 "Open (" << m_testFilename <<
", \"std::ios::app\") unexpectedly created a file");
393 f.Open (m_testFilename, std::ios::out);
395 ", \"std::ios::out\") returns error");
403 ", \"std::ios::app\") does not return error");
410 f.Open (m_testFilename, std::ios::out);
412 ", \"std::ios::out\") returns error");
414 f.Init (1234, 5678, 7);
423 ", \"std::ios::app\") returns error");
429 memset (buffer, 0,
sizeof(buffer));
430 f.Write (0, 0, buffer, 128);
431 NS_TEST_ASSERT_MSG_EQ (
f.Fail (),
false,
"Write (append-mode-file " << m_testFilename <<
") returns error");
453 void DoRun()
override;
460 :
TestCase(
"Check to see that PcapFileHeader is managed correctly")
471 std::stringstream filename;
497 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
502 f.Init(1234, 5678, 7);
514 <<
") should have been able to open a correctly created pcap file");
542 bool bigEndian = u.b[3];
544 size_t result = std::fread(&val32,
sizeof(val32), 1, p);
552 result = std::fread(&val16,
sizeof(val16), 1, p);
560 result = std::fread(&val16,
sizeof(val16), 1, p);
568 result = std::fread(&val32,
sizeof(val32), 1, p);
576 result = std::fread(&val32,
sizeof(val32), 1, p);
584 result = std::fread(&val32,
sizeof(val32), 1, p);
592 result = std::fread(&val32,
sizeof(val32), 1, p);
615 "Open (existing-initialized-file "
633 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
643 f.Init(1234, 5678, 7,
true);
656 <<
") should have been able to open a correctly created pcap file");
658 result = std::fread(&val32,
sizeof(val32), 1, p);
662 result = std::fread(&val16,
sizeof(val16), 1, p);
666 result = std::fread(&val16,
sizeof(val16), 1, p);
670 result = std::fread(&val32,
sizeof(val32), 1, p);
674 result = std::fread(&val32,
sizeof(val32), 1, p);
678 result = std::fread(&val32,
sizeof(val32), 1, p);
682 result = std::fread(&val32,
sizeof(val32), 1, p);
699 "Open (existing-initialized-file "
731 void DoRun()
override;
738 :
TestCase(
"Check to see that PcapRecordHeader is managed correctly")
749 std::stringstream filename;
775 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
786 uint8_t bufferOut[128];
787 for (uint32_t i = 0; i < 128; ++i)
797 f.Write(1234, 5678, bufferOut, 128);
800 "Write (write-only-file " <<
m_testFilename <<
") returns error");
810 "fopen() should have been able to open a correctly created pcap file");
817 std::fseek(p, 0, SEEK_END);
818 auto size = std::ftell(p);
826 std::fseek(p, 24, SEEK_SET);
853 bool bigEndian = u.b[3];
855 size_t result = std::fread(&val32,
sizeof(val32), 1, p);
863 result = std::fread(&val32,
sizeof(val32), 1, p);
871 result = std::fread(&val32,
sizeof(val32), 1, p);
879 result = std::fread(&val32,
sizeof(val32), 1, p);
891 uint8_t bufferIn[128];
893 result = std::fread(bufferIn, 1, 43, p);
896 for (uint32_t i = 0; i < 43; ++i)
912 <<
", \"std::ios::in\") of existing good file returns error");
920 f.Read(bufferIn,
sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen);
925 "Incorrectly read microseconds timestamp from known good packet");
931 "Incorrectly constructed actual read length from known good packet given buffer size");
937 for (uint32_t i = 0; i < 43; ++i)
941 "Incorrect packet data read from known good packet");
956 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
961 f.Init(37, 43, -7,
true);
969 f.Write(1234, 5678, bufferOut, 128);
972 "Write (write-only-file " <<
m_testFilename <<
") returns error");
982 "fopen() should have been able to open a correctly created pcap file");
989 std::fseek(p, 0, SEEK_END);
990 size = std::ftell(p);
998 result = std::fseek(p, 24, SEEK_SET);
1001 result = std::fread(&val32,
sizeof(val32), 1, p);
1004 Swap(uint32_t(1234)),
1005 "Swapped seconds timestamp written incorrectly");
1007 result = std::fread(&val32,
sizeof(val32), 1, p);
1010 Swap(uint32_t(5678)),
1011 "Swapped microseconds timestamp written incorrectly");
1013 result = std::fread(&val32,
sizeof(val32), 1, p);
1017 result = std::fread(&val32,
sizeof(val32), 1, p);
1025 result = std::fread(bufferIn, 1, 43, p);
1028 for (uint32_t i = 0; i < 43; ++i)
1045 <<
", \"std::ios::in\") of existing good file returns error");
1047 f.Read(bufferIn,
sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen);
1052 "Incorrectly read microseconds timestamp from known good packet");
1058 "Incorrectly constructed actual read length from known good packet given buffer size");
1063 for (uint32_t i = 0; i < 43; ++i)
1067 "Incorrect packet data read from known good packet");
1088 void DoRun()
override;
1095 :
TestCase(
"Check to see that PcapFile can read out a known good pcap file")
1259 f.Open(filename, std::ios::in);
1262 "Open (" << filename <<
", \"std::ios::in\") returns error");
1283 f.Read(
data,
sizeof(
data), tsSec, tsUsec, inclLen, origLen, readLen);
1287 "Incorrectly read seconds timestamp from known good pcap file");
1290 "Incorrectly read microseconds timestamp from known good pcap file");
1293 "Incorrectly read included length from known good packet");
1296 "Incorrectly read original length from known good packet");
1300 "Incorrect actual read length from known good packet given buffer size");
1307 f.Read(
data, 1, tsSec, tsUsec, inclLen, origLen, readLen);
1310 "Read() of known good pcap file at EOF does not return error");
1327 void DoRun()
override;
1331 :
TestCase(
"Check that PcapFile::Diff works as expected")
1344 uint32_t packets(0);
1345 bool diff = PcapFile::Diff(filename, filename, sec, usec, packets);
1355 f.Open(filename2, std::ios::out);
1358 "Open (" << filename2 <<
", \"std::ios::out\") returns error");
1372 diff = PcapFile::Diff(filename, filename2, sec, usec, packets);
double f(double x, void *params)
Test case to make sure that the Pcap::Diff method works as expected.
void DoRun() override
Implementation to actually run this TestCase.
PCAP file utils TestSuite.
Test case to make sure that the Pcap File Object can read out the contents of a known good pcap file.
void DoSetup() override
Implementation to do any local setup required for this TestCase.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
~ReadFileTestCase() override
std::string m_testFilename
File name.
void DoRun() override
Implementation to actually run this TestCase.
Test case to make sure that the Pcap File Object can open an existing pcap file.
std::string m_testFilename
File name.
~ReadModeCreateTestCase() override
void DoSetup() override
Implementation to do any local setup required for this TestCase.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
Test case to make sure that the Pcap File Object can do its most basic job and create an empty pcap f...
~WriteModeCreateTestCase() override
std::string m_testFilename
File name.
WriteModeCreateTestCase()
void DoSetup() override
Implementation to do any local setup required for this TestCase.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
A class representing a pcap file.
std::string CreateDataDirFilename(std::string filename)
Construct the full path to a file in the data directory.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
virtual void DoSetup()
Implementation to do any local setup required for this TestCase.
std::string CreateTempDirFilename(std::string filename)
Construct the full path to a file in a temporary directory.
void SetDataDir(std::string directory)
Set the data directory where reference trace files can be found.
virtual void DoTeardown()
Implementation to do any local setup required for this TestCase.
virtual void DoRun()=0
Implementation to actually run this TestCase.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static bool CheckFileLength(std::string filename, long sizeExpected)
static const uint32_t N_KNOWN_PACKETS
static bool CheckFileExists(std::string filename)
static uint16_t Swap(uint16_t val)
static const PacketEntry knownPackets[]
static PcapFileTestSuite pcapFileTestSuite
Static variable for test initialization.
static const uint32_t N_PACKET_BYTES
uint32_t tsUsec
Time (micro seconds part)
uint32_t origLen
length of the original packet
uint16_t data[N_PACKET_BYTES]
Packet data.
uint32_t tsSec
Time (seconds part)
uint32_t inclLen
Length of the entry in the PCAP.