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;
91 virtual void DoSetup (
void);
92 virtual void DoRun (
void);
93 virtual void DoTeardown (
void);
99 :
TestCase (
"Check to see that PcapFile::Open with mode std::ios::out works")
110 std::stringstream filename;
111 uint32_t n = rand ();
140 "Open (" <<
m_testFilename <<
", \"std::ios::out\") does not create file");
142 "Open (" <<
m_testFilename <<
", \"std::ios::out\") does not result in an empty file");
150 ", \"std::ios::out\") returns error");
152 f.
Init (1234, 5678, 7);
158 "Init () does not result in a file with a pcap file header");
166 ", \"std::ios::out\") returns error");
171 "Open (" <<
m_testFilename <<
", \"w\") does not result in an empty file");
180 f.
Init (1234, 5678, 7);
189 memset (buffer, 0,
sizeof(buffer));
190 f.
Write (0, 0, buffer, 128);
207 virtual void DoRun (
void);
214 :
TestCase (
"Check to see that PcapFile::Open with mode \"std::ios::in\" works")
225 std::stringstream filename;
226 uint32_t n = rand ();
250 ", \"std::ios::in\") does not return error");
254 ", \"std::ios::in\") unexpectedly created a file");
268 ", \"std::ios::in\") does not return error");
278 ", \"std::ios::out\") returns error");
280 f.
Init (1234, 5678, 7);
289 ", \"std::ios::in\") returns error");
296 f.
Write (0, 0, buffer, 128);
298 ") does not return error");
308 class AppendModeCreateTestCase :
public TestCase
311 AppendModeCreateTestCase ();
312 virtual ~AppendModeCreateTestCase ();
316 virtual void DoRun (
void);
319 std::string m_testFilename;
322 AppendModeCreateTestCase::AppendModeCreateTestCase ()
323 :
TestCase (
"Check to see that PcapFile::Open with mode \"std::ios::app\" works")
327 AppendModeCreateTestCase::~AppendModeCreateTestCase ()
334 std::stringstream filename;
335 uint32_t n = rand ();
337 m_testFilename = CreateTempDirFilename (filename.str () +
".pcap");
343 if (
remove (m_testFilename.c_str ()))
345 NS_LOG_ERROR (
"Failed to delete file " << m_testFilename);
357 f.
Open (m_testFilename, std::ios::out | std::ios::app);
359 ", \"std::ios::app\") does not return error");
364 "Open (" << m_testFilename <<
", \"std::ios::app\") unexpectedly created a file");
369 f.
Open (m_testFilename, std::ios::out);
371 ", \"std::ios::out\") returns error");
377 f.
Open (m_testFilename, std::ios::out | std::ios::app);
379 ", \"std::ios::app\") does not return error");
386 f.
Open (m_testFilename, std::ios::out);
388 ", \"std::ios::out\") returns error");
390 f.
Init (1234, 5678, 7);
397 f.
Open (m_testFilename, std::ios::out | std::ios::app);
399 ", \"std::ios::app\") returns error");
405 memset (buffer, 0,
sizeof(buffer));
406 f.
Write (0, 0, buffer, 128);
425 virtual void DoRun (
void);
432 :
TestCase (
"Check to see that PcapFileHeader is managed correctly")
443 std::stringstream filename;
444 uint32_t n = rand ();
468 ", \"std::ios::out\") returns error");
473 f.
Init (1234, 5678, 7);
475 "Init (1234, 5678, 7) returns error");
510 bool bigEndian = u.b[3];
512 size_t result = std::fread (&val32,
sizeof(val32), 1, p);
514 if (bigEndian) val32 =
Swap (val32);
517 result = std::fread (&val16,
sizeof(val16), 1, p);
519 if (bigEndian) val16 =
Swap (val16);
522 result = std::fread (&val16,
sizeof(val16), 1, p);
524 if (bigEndian) val16 =
Swap (val16);
527 result = std::fread (&val32,
sizeof(val32), 1, p);
529 if (bigEndian) val32 =
Swap (val32);
532 result = std::fread (&val32,
sizeof(val32), 1, p);
534 if (bigEndian) val32 =
Swap (val32);
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);
561 ", \"std::ios::in\") returns error");
577 ", \"std::ios::out\") returns error");
587 f.
Init (1234, 5678, 7,
true);
598 result = std::fread (&val32,
sizeof(val32), 1, p);
602 result = std::fread (&val16,
sizeof(val16), 1, p);
606 result = std::fread (&val16,
sizeof(val16), 1, p);
610 result = std::fread (&val32,
sizeof(val32), 1, p);
614 result = std::fread (&val32,
sizeof(val32), 1, p);
618 result = std::fread (&val32,
sizeof(val32), 1, p);
622 result = std::fread (&val32,
sizeof(val32), 1, p);
638 ", \"std::ios::in\") returns error");
665 virtual void DoRun (
void);
672 :
TestCase (
"Check to see that PcapRecordHeader is managed correctly")
683 std::stringstream filename;
684 uint32_t n = rand ();
708 ", \"std::ios::out\") returns error");
719 uint8_t bufferOut[128];
720 for (uint32_t i = 0; i < 128; ++i)
730 f.
Write (1234, 5678, bufferOut, 128);
746 std::fseek (p, 0, SEEK_END);
747 uint64_t size = std::ftell (p);
755 std::fseek (p, 24, SEEK_SET);
782 bool bigEndian = u.b[3];
784 size_t result = std::fread (&val32,
sizeof(val32), 1, p);
786 if (bigEndian) val32 =
Swap (val32);
789 result = std::fread (&val32,
sizeof(val32), 1, p);
791 if (bigEndian) val32 =
Swap (val32);
794 result = std::fread (&val32,
sizeof(val32), 1, p);
796 if (bigEndian) val32 =
Swap (val32);
799 result = std::fread (&val32,
sizeof(val32), 1, p);
801 if (bigEndian) val32 =
Swap (val32);
808 uint8_t bufferIn[128];
810 result = std::fread (bufferIn, 1, 43, p);
813 for (uint32_t i = 0; i < 43; ++i)
827 ", \"std::ios::in\") of existing good file returns error");
829 uint32_t tsSec, tsUsec, inclLen, origLen, readLen;
831 f.
Read (bufferIn,
sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen);
834 NS_TEST_ASSERT_MSG_EQ (tsUsec, 5678,
"Incorrectly read microseconds timestap from known good packet");
837 NS_TEST_ASSERT_MSG_EQ (readLen, 43,
"Incorrectly constructed actual read length from known good packet given buffer size");
843 for (uint32_t i = 0; i < 43; ++i)
845 NS_TEST_ASSERT_MSG_EQ (bufferIn[i], bufferOut[i],
"Incorrect packet data read from known good packet");
859 ", \"std::ios::out\") returns error");
864 f.
Init (37, 43, -7,
true);
872 f.
Write (1234, 5678, bufferOut, 128);
888 std::fseek (p, 0, SEEK_END);
889 size = std::ftell (p);
897 result = std::fseek (p, 24, SEEK_SET);
900 result = std::fread (&val32,
sizeof(val32), 1, p);
904 result = std::fread (&val32,
sizeof(val32), 1, p);
908 result = std::fread (&val32,
sizeof(val32), 1, p);
912 result = std::fread (&val32,
sizeof(val32), 1, p);
920 result = std::fread (bufferIn, 1, 43, p);
923 for (uint32_t i = 0; i < 43; ++i)
938 ", \"std::ios::in\") of existing good file returns error");
940 f.
Read (bufferIn,
sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen);
943 NS_TEST_ASSERT_MSG_EQ (tsUsec, 5678,
"Incorrectly read microseconds timestap from known good packet");
946 NS_TEST_ASSERT_MSG_EQ (readLen, 43,
"Incorrectly constructed actual read length from known good packet given buffer size");
951 for (uint32_t i = 0; i < 43; ++i)
953 NS_TEST_ASSERT_MSG_EQ (bufferIn[i], bufferOut[i],
"Incorrect packet data read from known good packet");
971 virtual void DoRun (
void);
978 :
TestCase (
"Check to see that PcapFile can read out a known good pcap file")
999 typedef struct PACKET_ENTRY {
1008 { 2, 3696, 46, 46, { 0x0001, 0x0800, 0x0604, 0x0001, 0x0000, 0x0000, 0x0003, 0x0a01,
1009 0x0201, 0xffff, 0xffff, 0xffff, 0x0a01, 0x0204, 0x0000, 0x0000}},
1010 { 2, 3707, 46, 46, { 0x0001, 0x0800, 0x0604, 0x0002, 0x0000, 0x0000, 0x0006, 0x0a01,
1011 0x0204, 0x0000, 0x0000, 0x0003, 0x0a01, 0x0201, 0x0000, 0x0000}},
1012 { 2, 3801, 1070, 1070, { 0x4500, 0x041c, 0x0000, 0x0000, 0x3f11, 0x0000, 0x0a01, 0x0101,
1013 0x0a01, 0x0204, 0xc001, 0x0009, 0x0408, 0x0000, 0x0000, 0x0000}},
1014 { 2, 3811, 46, 46, { 0x0001, 0x0800, 0x0604, 0x0001, 0x0000, 0x0000, 0x0006, 0x0a01,
1015 0x0204, 0xffff, 0xffff, 0xffff, 0x0a01, 0x0201, 0x0000, 0x0000}},
1016 { 2, 3822, 46, 46, { 0x0001, 0x0800, 0x0604, 0x0002, 0x0000, 0x0000, 0x0003, 0x0a01,
1017 0x0201, 0x0000, 0x0000, 0x0006, 0x0a01, 0x0204, 0x0000, 0x0000}},
1018 { 2, 3915, 1070, 1070, { 0x4500, 0x041c, 0x0000, 0x0000, 0x4011, 0x0000, 0x0a01, 0x0204,
1019 0x0a01, 0x0101, 0x0009, 0xc001, 0x0408, 0x0000, 0x0000, 0x0000}}
1031 f.
Open (filename, std::ios::in);
1033 ", \"std::ios::in\") returns error");
1044 uint32_t tsSec, tsUsec, inclLen, origLen, readLen;
1050 f.
Read (data,
sizeof(data), tsSec, tsUsec, inclLen, origLen, readLen);
1063 f.
Read (data, 1, tsSec, tsUsec, inclLen, origLen, readLen);
1078 virtual void DoRun (
void);
1082 :
TestCase (
"Check that PcapFile::Diff works as expected")
1093 uint32_t sec (0), usec (0);
1094 bool diff = PcapFile::Diff (filename, filename, sec, usec);
1100 std::string filename2 =
"different.pcap";
1103 f.
Open (filename2, std::ios::out);
1117 diff = PcapFile::Diff (filename, filename2, sec, usec);