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 timestamp 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 timestamp 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);
1126 f.Open (filename2, std::ios::out);
1141 diff = PcapFile::Diff (filename, filename2, sec, usec, packets);