27 #include "ns3/pcap-file.h" 40 return ((val >> 8) & 0x00ff) | ((val << 8) & 0xff00);
46 return ((val >> 24) & 0x000000ff) | ((val >> 8) & 0x0000ff00) | ((val << 8) & 0x00ff0000) | ((val << 24) & 0xff000000);
52 FILE * p = std::fopen (filename.c_str (),
"rb");
66 FILE * p = std::fopen (filename.c_str (),
"rb");
72 std::fseek (p, 0, SEEK_END);
74 uint64_t sizeActual = std::ftell (p);
77 return sizeActual == sizeExpected;
94 virtual void DoSetup (
void);
95 virtual void DoRun (
void);
96 virtual void DoTeardown (
void);
102 :
TestCase (
"Check to see that PcapFile::Open with mode std::ios::out works")
113 std::stringstream filename;
114 uint32_t
n = rand ();
143 "Open (" <<
m_testFilename <<
", \"std::ios::out\") does not create file");
145 "Open (" <<
m_testFilename <<
", \"std::ios::out\") does not result in an empty file");
153 ", \"std::ios::out\") returns error");
155 f.Init (1234, 5678, 7);
161 "Init () does not result in a file with a pcap file header");
169 ", \"std::ios::out\") returns error");
174 "Open (" <<
m_testFilename <<
", \"w\") does not result in an empty file");
183 f.Init (1234, 5678, 7);
192 memset (buffer, 0,
sizeof(buffer));
193 f.Write (0, 0, buffer, 128);
213 virtual void DoRun (
void);
220 :
TestCase (
"Check to see that PcapFile::Open with mode std::ios::in works")
231 std::stringstream filename;
232 uint32_t
n = rand ();
256 ", \"std::ios::in\") does not return error");
260 ", \"std::ios::in\") unexpectedly created a file");
274 ", \"std::ios::in\") does not return error");
284 ", \"std::ios::out\") returns error");
286 f.Init (1234, 5678, 7);
295 ", \"std::ios::in\") returns error");
302 f.Write (0, 0, buffer, 128);
304 ") does not return error");
314 class AppendModeCreateTestCase :
public TestCase 317 AppendModeCreateTestCase ();
318 virtual ~AppendModeCreateTestCase ();
322 virtual void DoRun (
void);
325 std::string m_testFilename;
328 AppendModeCreateTestCase::AppendModeCreateTestCase ()
329 :
TestCase (
"Check to see that PcapFile::Open with mode std::ios::app works")
333 AppendModeCreateTestCase::~AppendModeCreateTestCase ()
338 AppendModeCreateTestCase::DoSetup (
void)
340 std::stringstream filename;
341 uint32_t
n = rand ();
343 m_testFilename = CreateTempDirFilename (filename.str () +
".pcap");
347 AppendModeCreateTestCase::DoTeardown (
void)
349 if (
remove (m_testFilename.c_str ()))
351 NS_LOG_ERROR (
"Failed to delete file " << m_testFilename);
356 AppendModeCreateTestCase::DoRun (
void)
363 f.Open (m_testFilename, std::ios::out | std::ios::app);
365 ", \"std::ios::app\") does not return error");
370 "Open (" << m_testFilename <<
", \"std::ios::app\") unexpectedly created a file");
375 f.Open (m_testFilename, std::ios::out);
377 ", \"std::ios::out\") returns error");
383 f.Open (m_testFilename, std::ios::out | std::ios::app);
385 ", \"std::ios::app\") does not return error");
392 f.Open (m_testFilename, std::ios::out);
394 ", \"std::ios::out\") returns error");
396 f.Init (1234, 5678, 7);
403 f.Open (m_testFilename, std::ios::out | std::ios::app);
405 ", \"std::ios::app\") returns error");
411 memset (buffer, 0,
sizeof(buffer));
412 f.Write (0, 0, buffer, 128);
413 NS_TEST_ASSERT_MSG_EQ (
f.Fail (),
false,
"Write (append-mode-file " << m_testFilename <<
") returns error");
435 virtual void DoRun (
void);
442 :
TestCase (
"Check to see that PcapFileHeader is managed correctly")
453 std::stringstream filename;
454 uint32_t
n = rand ();
478 ", \"std::ios::out\") returns error");
483 f.Init (1234, 5678, 7);
485 "Init (1234, 5678, 7) returns error");
520 bool bigEndian = u.b[3];
522 size_t result = std::fread (&val32,
sizeof(val32), 1, p);
524 if (bigEndian) val32 =
Swap (val32);
527 result = std::fread (&val16,
sizeof(val16), 1, p);
529 if (bigEndian) val16 =
Swap (val16);
532 result = std::fread (&val16,
sizeof(val16), 1, p);
534 if (bigEndian) val16 =
Swap (val16);
537 result = std::fread (&val32,
sizeof(val32), 1, p);
539 if (bigEndian) val32 =
Swap (val32);
542 result = std::fread (&val32,
sizeof(val32), 1, p);
544 if (bigEndian) val32 =
Swap (val32);
547 result = std::fread (&val32,
sizeof(val32), 1, p);
549 if (bigEndian) val32 =
Swap (val32);
552 result = std::fread (&val32,
sizeof(val32), 1, p);
554 if (bigEndian) val32 =
Swap (val32);
571 ", \"std::ios::in\") returns error");
587 ", \"std::ios::out\") returns error");
597 f.Init (1234, 5678, 7,
true);
608 result = std::fread (&val32,
sizeof(val32), 1, p);
612 result = std::fread (&val16,
sizeof(val16), 1, p);
616 result = std::fread (&val16,
sizeof(val16), 1, p);
620 result = std::fread (&val32,
sizeof(val32), 1, p);
624 result = std::fread (&val32,
sizeof(val32), 1, p);
628 result = std::fread (&val32,
sizeof(val32), 1, p);
632 result = std::fread (&val32,
sizeof(val32), 1, p);
648 ", \"std::ios::in\") returns error");
679 virtual void DoRun (
void);
686 :
TestCase (
"Check to see that PcapRecordHeader is managed correctly")
697 std::stringstream filename;
698 uint32_t
n = rand ();
722 ", \"std::ios::out\") returns error");
733 uint8_t bufferOut[128];
734 for (uint32_t i = 0; i < 128; ++i)
744 f.Write (1234, 5678, bufferOut, 128);
760 std::fseek (p, 0, SEEK_END);
761 uint64_t size = std::ftell (p);
769 std::fseek (p, 24, SEEK_SET);
796 bool bigEndian = u.b[3];
798 size_t result = std::fread (&val32,
sizeof(val32), 1, p);
800 if (bigEndian) val32 =
Swap (val32);
803 result = std::fread (&val32,
sizeof(val32), 1, p);
805 if (bigEndian) val32 =
Swap (val32);
808 result = std::fread (&val32,
sizeof(val32), 1, p);
810 if (bigEndian) val32 =
Swap (val32);
813 result = std::fread (&val32,
sizeof(val32), 1, p);
815 if (bigEndian) val32 =
Swap (val32);
822 uint8_t bufferIn[128];
824 result = std::fread (bufferIn, 1, 43, p);
827 for (uint32_t i = 0; i < 43; ++i)
841 ", \"std::ios::in\") of existing good file returns error");
843 uint32_t tsSec, tsUsec, inclLen, origLen, readLen;
845 f.Read (bufferIn,
sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen);
848 NS_TEST_ASSERT_MSG_EQ (tsUsec, 5678,
"Incorrectly read microseconds timestap from known good packet");
851 NS_TEST_ASSERT_MSG_EQ (readLen, 43,
"Incorrectly constructed actual read length from known good packet given buffer size");
857 for (uint32_t i = 0; i < 43; ++i)
859 NS_TEST_ASSERT_MSG_EQ (bufferIn[i], bufferOut[i],
"Incorrect packet data read from known good packet");
873 ", \"std::ios::out\") returns error");
878 f.Init (37, 43, -7,
true);
886 f.Write (1234, 5678, bufferOut, 128);
902 std::fseek (p, 0, SEEK_END);
903 size = std::ftell (p);
911 result = std::fseek (p, 24, SEEK_SET);
914 result = std::fread (&val32,
sizeof(val32), 1, p);
918 result = std::fread (&val32,
sizeof(val32), 1, p);
922 result = std::fread (&val32,
sizeof(val32), 1, p);
926 result = std::fread (&val32,
sizeof(val32), 1, p);
934 result = std::fread (bufferIn, 1, 43, p);
937 for (uint32_t i = 0; i < 43; ++i)
952 ", \"std::ios::in\") of existing good file returns error");
954 f.Read (bufferIn,
sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen);
957 NS_TEST_ASSERT_MSG_EQ (tsUsec, 5678,
"Incorrectly read microseconds timestap from known good packet");
960 NS_TEST_ASSERT_MSG_EQ (readLen, 43,
"Incorrectly constructed actual read length from known good packet given buffer size");
965 for (uint32_t i = 0; i < 43; ++i)
967 NS_TEST_ASSERT_MSG_EQ (bufferIn[i], bufferOut[i],
"Incorrect packet data read from known good packet");
988 virtual void DoRun (
void);
995 :
TestCase (
"Check to see that PcapFile can read out a known good pcap file")
1019 typedef struct PACKET_ENTRY {
1028 { 2, 3696, 46, 46, { 0x0001, 0x0800, 0x0604, 0x0001, 0x0000, 0x0000, 0x0003, 0x0a01,
1029 0x0201, 0xffff, 0xffff, 0xffff, 0x0a01, 0x0204, 0x0000, 0x0000}},
1030 { 2, 3707, 46, 46, { 0x0001, 0x0800, 0x0604, 0x0002, 0x0000, 0x0000, 0x0006, 0x0a01,
1031 0x0204, 0x0000, 0x0000, 0x0003, 0x0a01, 0x0201, 0x0000, 0x0000}},
1032 { 2, 3801, 1070, 1070, { 0x4500, 0x041c, 0x0000, 0x0000, 0x3f11, 0x0000, 0x0a01, 0x0101,
1033 0x0a01, 0x0204, 0xc001, 0x0009, 0x0408, 0x0000, 0x0000, 0x0000}},
1034 { 2, 3811, 46, 46, { 0x0001, 0x0800, 0x0604, 0x0001, 0x0000, 0x0000, 0x0006, 0x0a01,
1035 0x0204, 0xffff, 0xffff, 0xffff, 0x0a01, 0x0201, 0x0000, 0x0000}},
1036 { 2, 3822, 46, 46, { 0x0001, 0x0800, 0x0604, 0x0002, 0x0000, 0x0000, 0x0003, 0x0a01,
1037 0x0201, 0x0000, 0x0000, 0x0006, 0x0a01, 0x0204, 0x0000, 0x0000}},
1038 { 2, 3915, 1070, 1070, { 0x4500, 0x041c, 0x0000, 0x0000, 0x4011, 0x0000, 0x0a01, 0x0204,
1039 0x0a01, 0x0101, 0x0009, 0xc001, 0x0408, 0x0000, 0x0000, 0x0000}}
1051 f.Open (filename, std::ios::in);
1053 ", \"std::ios::in\") returns error");
1064 uint32_t tsSec, tsUsec, inclLen, origLen, readLen;
1070 f.Read (
data,
sizeof(
data), tsSec, tsUsec, inclLen, origLen, readLen);
1083 f.Read (
data, 1, tsSec, tsUsec, inclLen, origLen, readLen);
1101 virtual void DoRun (
void);
1105 :
TestCase (
"Check that PcapFile::Diff works as expected")
1116 uint32_t sec (0), usec (0), packets (0);
1117 bool diff = PcapFile::Diff (filename, filename, sec, usec, packets);
1123 std::string filename2 =
"different.pcap";
1126 f.Open (filename2, std::ios::out);
1141 diff = PcapFile::Diff (filename, filename2, sec, usec, packets);
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
static const PacketEntry knownPackets[]
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
virtual ~ReadModeCreateTestCase()
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
PCAP file utils TestSuite.
uint32_t tsUsec
Time (micro seconds part)
WriteModeCreateTestCase()
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
static const uint32_t N_KNOWN_PACKETS
Test case to make sure that the Pcap::Diff method works as expected.
A class representing a pcap file.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
uint32_t inclLen
Length of the entry in the PCAP.
static bool CheckFileExists(std::string filename)
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
#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.
static const uint32_t N_PACKET_BYTES
std::string m_testFilename
File name.
virtual void DoRun(void)
Implementation to actually run this TestCase.
double f(double x, void *params)
Test case to make sure that the Pcap File Object can open an existing pcap file.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::string CreateDataDirFilename(std::string filename)
Construct the full path to a file in the data directory.
virtual void DoRun(void)=0
Implementation to actually run this TestCase.
std::string m_testFilename
File name.
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
virtual void DoRun(void)
Implementation to actually run this TestCase.
#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...
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.
uint32_t origLen
length of the original packet
static bool CheckFileLength(std::string filename, uint64_t sizeExpected)
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
virtual void DoRun(void)
Implementation to actually run this TestCase.
static PcapFileTestSuite pcapFileTestSuite
Static variable for test initialization.
virtual ~ReadFileTestCase()
Test case to make sure that the Pcap File Object can do its most basic job and create an empty pcap f...
virtual ~WriteModeCreateTestCase()
uint32_t tsSec
Time (seconds part)
static uint16_t Swap(uint16_t val)
Test case to make sure that the Pcap File Object can read out the contents of a known good pcap file...
std::string m_testFilename
File name.
uint16_t data[N_PACKET_BYTES]
Packet data.