26 #include "ns3/pcap-file.h"
37 return ((val >> 8) & 0x00ff) | ((val << 8) & 0xff00);
43 return ((val >> 24) & 0x000000ff) | ((val >> 8) & 0x0000ff00) | ((val << 8) & 0x00ff0000) | ((val << 24) & 0xff000000);
47 CheckFileExists (std::string filename)
49 FILE * p = std::fopen (filename.c_str (),
"rb");
61 CheckFileLength (std::string filename, uint64_t sizeExpected)
63 FILE * p = std::fopen (filename.c_str (),
"rb");
69 std::fseek (p, 0, SEEK_END);
71 uint64_t sizeActual = std::ftell (p);
74 return sizeActual == sizeExpected;
88 virtual void DoSetup (
void);
89 virtual void DoRun (
void);
90 virtual void DoTeardown (
void);
92 std::string m_testFilename;
95 WriteModeCreateTestCase::WriteModeCreateTestCase ()
96 :
TestCase (
"Check to see that PcapFile::Open with mode std::ios::out works")
100 WriteModeCreateTestCase::~WriteModeCreateTestCase ()
107 std::stringstream filename;
108 uint32_t n = rand ();
110 m_testFilename = CreateTempDirFilename (filename.str () +
".pcap");
116 remove (m_testFilename.c_str ());
128 f.
Open (m_testFilename, std::ios::out);
130 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (" << m_testFilename <<
", \"w\") returns error");
133 NS_TEST_ASSERT_MSG_EQ (CheckFileExists (m_testFilename),
true,
134 "Open (" << m_testFilename <<
", \"std::ios::out\") does not create file");
135 NS_TEST_ASSERT_MSG_EQ (CheckFileLength (m_testFilename, 0),
true,
136 "Open (" << m_testFilename <<
", \"std::ios::out\") does not result in an empty file");
142 f.
Open (m_testFilename, std::ios::out);
143 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (" << m_testFilename <<
144 ", \"std::ios::out\") returns error");
146 f.
Init (1234, 5678, 7);
147 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Init (1234, 5678, 7) returns error");
151 NS_TEST_ASSERT_MSG_EQ (CheckFileLength (m_testFilename, 24),
true,
152 "Init () does not result in a file with a pcap file header");
158 f.
Open (m_testFilename, std::ios::out);
159 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (" << m_testFilename <<
160 ", \"std::ios::out\") returns error");
164 NS_TEST_ASSERT_MSG_EQ (CheckFileLength (m_testFilename, 0),
true,
165 "Open (" << m_testFilename <<
", \"w\") does not result in an empty file");
170 f.
Open (m_testFilename, std::ios::out);
171 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
172 "Open (" << m_testFilename <<
", \"w\") returns error");
174 f.
Init (1234, 5678, 7);
175 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Init (1234, 5678, 7) returns error");
183 memset (buffer, 0,
sizeof(buffer));
184 f.
Write (0, 0, buffer, 128);
185 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Write (write-only-file " << m_testFilename <<
201 virtual void DoRun (
void);
204 std::string m_testFilename;
207 ReadModeCreateTestCase::ReadModeCreateTestCase ()
208 :
TestCase (
"Check to see that PcapFile::Open with mode \"std::ios::in\" works")
212 ReadModeCreateTestCase::~ReadModeCreateTestCase ()
219 std::stringstream filename;
220 uint32_t n = rand ();
222 m_testFilename = CreateTempDirFilename (filename.str () +
".pcap");
228 remove (m_testFilename.c_str ());
239 f.
Open (m_testFilename, std::ios::in);
240 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
true,
"Open (non-existing-filename " << m_testFilename <<
241 ", \"std::ios::in\") does not return error");
244 NS_TEST_ASSERT_MSG_EQ (CheckFileExists (m_testFilename),
false,
"Open (" << m_testFilename <<
245 ", \"std::ios::in\") unexpectedly created a file");
250 f.
Open (m_testFilename, std::ios::out);
251 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (filename, \"std::ios::out\") returns error");
257 f.
Open (m_testFilename, std::ios::in);
258 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
true,
"Open (non-initialized-filename " << m_testFilename <<
259 ", \"std::ios::in\") does not return error");
267 f.
Open (m_testFilename, std::ios::out);
268 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (" << m_testFilename <<
269 ", \"std::ios::out\") returns error");
271 f.
Init (1234, 5678, 7);
272 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Init (1234, 5678, 7) returns error");
278 f.
Open (m_testFilename, std::ios::in);
279 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (initialized-filename " << m_testFilename <<
280 ", \"std::ios::in\") returns error");
287 f.
Write (0, 0, buffer, 128);
288 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
true,
"Write (read-only-file " << m_testFilename <<
289 ") does not return error");
299 class AppendModeCreateTestCase :
public TestCase
302 AppendModeCreateTestCase ();
303 virtual ~AppendModeCreateTestCase ();
307 virtual void DoRun (
void);
310 std::string m_testFilename;
313 AppendModeCreateTestCase::AppendModeCreateTestCase ()
314 :
TestCase (
"Check to see that PcapFile::Open with mode \"std::ios::app\" works")
318 AppendModeCreateTestCase::~AppendModeCreateTestCase ()
323 AppendModeCreateTestCase::DoSetup (
void)
325 std::stringstream filename;
326 uint32_t n = rand ();
328 m_testFilename = CreateTempDirFilename (filename.str () +
".pcap");
332 AppendModeCreateTestCase::DoTeardown (
void)
334 remove (m_testFilename.c_str ());
338 AppendModeCreateTestCase::DoRun (
void)
345 f.
Open (m_testFilename, std::ios::out | std::ios::app);
346 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
true,
"Open (non-existing-filename " << m_testFilename <<
347 ", \"std::ios::app\") does not return error");
351 NS_TEST_ASSERT_MSG_EQ (CheckFileExists (m_testFilename),
false,
352 "Open (" << m_testFilename <<
", \"std::ios::app\") unexpectedly created a file");
357 f.
Open (m_testFilename, std::ios::out);
358 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (" << m_testFilename <<
359 ", \"std::ios::out\") returns error");
365 f.
Open (m_testFilename, std::ios::out | std::ios::app);
366 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
true,
"Open (non-initialized-filename " << m_testFilename <<
367 ", \"std::ios::app\") does not return error");
374 f.
Open (m_testFilename, std::ios::out);
375 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (non-initialized-filename " << m_testFilename <<
376 ", \"std::ios::out\") returns error");
378 f.
Init (1234, 5678, 7);
379 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Init (1234, 5678, 7) returns error");
385 f.
Open (m_testFilename, std::ios::out | std::ios::app);
386 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (initialized-filename " << m_testFilename <<
387 ", \"std::ios::app\") returns error");
393 memset (buffer, 0,
sizeof(buffer));
394 f.
Write (0, 0, buffer, 128);
395 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Write (append-mode-file " << m_testFilename <<
") returns error");
413 virtual void DoRun (
void);
416 std::string m_testFilename;
419 FileHeaderTestCase::FileHeaderTestCase ()
420 :
TestCase (
"Check to see that PcapFileHeader is managed correctly")
424 FileHeaderTestCase::~FileHeaderTestCase ()
431 std::stringstream filename;
432 uint32_t n = rand ();
434 m_testFilename = CreateTempDirFilename (filename.str () +
".pcap");
440 remove (m_testFilename.c_str ());
451 f.
Open (m_testFilename, std::ios::out);
452 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (" << m_testFilename <<
453 ", \"std::ios::out\") returns error");
458 f.
Init (1234, 5678, 7);
459 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
460 "Init (1234, 5678, 7) returns error");
466 FILE *p = std::fopen (m_testFilename.c_str (),
"r+b");
467 NS_TEST_ASSERT_MSG_NE (p, 0,
"fopen(" << m_testFilename <<
") should have been able to open a correctly created pcap file");
495 bool bigEndian = u.b[3];
497 size_t result = std::fread (&val32,
sizeof(val32), 1, p);
498 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() magic number");
499 if (bigEndian) val32 = Swap (val32);
500 NS_TEST_ASSERT_MSG_EQ (val32, 0xa1b2c3d4,
"Magic number written incorrectly");
502 result = std::fread (&val16,
sizeof(val16), 1, p);
503 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() version major");
504 if (bigEndian) val16 = Swap (val16);
505 NS_TEST_ASSERT_MSG_EQ (val16, 2,
"Version major written incorrectly");
507 result = std::fread (&val16,
sizeof(val16), 1, p);
508 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() version minor");
509 if (bigEndian) val16 = Swap (val16);
510 NS_TEST_ASSERT_MSG_EQ (val16, 4,
"Version minor written incorrectly");
512 result = std::fread (&val32,
sizeof(val32), 1, p);
513 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() time zone correction");
514 if (bigEndian) val32 = Swap (val32);
515 NS_TEST_ASSERT_MSG_EQ (val32, 7,
"Version minor written incorrectly");
517 result = std::fread (&val32,
sizeof(val32), 1, p);
518 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() sig figs");
519 if (bigEndian) val32 = Swap (val32);
520 NS_TEST_ASSERT_MSG_EQ (val32, 0,
"Sig figs written incorrectly");
522 result = std::fread (&val32,
sizeof(val32), 1, p);
523 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() snap length");
524 if (bigEndian) val32 = Swap (val32);
525 NS_TEST_ASSERT_MSG_EQ (val32, 5678,
"Snap length written incorrectly");
527 result = std::fread (&val32,
sizeof(val32), 1, p);
528 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() data link type");
529 if (bigEndian) val32 = Swap (val32);
530 NS_TEST_ASSERT_MSG_EQ (val32, 1234,
"Data length type written incorrectly");
544 f.
Open (m_testFilename, std::ios::in);
545 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (existing-initialized-file " << m_testFilename <<
546 ", \"std::ios::in\") returns error");
548 NS_TEST_ASSERT_MSG_EQ (f.GetMagic (), 0xa1b2c3d4,
"Read back magic number incorrectly");
549 NS_TEST_ASSERT_MSG_EQ (f.GetVersionMajor (), 2,
"Read back version major incorrectly");
550 NS_TEST_ASSERT_MSG_EQ (f.GetVersionMinor (), 4,
"Read back version minor incorrectly");
551 NS_TEST_ASSERT_MSG_EQ (f.GetTimeZoneOffset (), 7,
"Read back time zone offset incorrectly");
552 NS_TEST_ASSERT_MSG_EQ (f.GetSigFigs (), 0,
"Read back sig figs incorrectly");
553 NS_TEST_ASSERT_MSG_EQ (f.GetSnapLen (), 5678,
"Read back snap len incorrectly");
554 NS_TEST_ASSERT_MSG_EQ (f.GetDataLinkType (), 1234,
"Read back data link type incorrectly");
560 f.
Open (m_testFilename, std::ios::out);
561 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (" << m_testFilename <<
562 ", \"std::ios::out\") returns error");
572 f.
Init (1234, 5678, 7,
true);
573 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Init (1234, 5678, 7) returns error");
580 p = std::fopen (m_testFilename.c_str (),
"r+b");
581 NS_TEST_ASSERT_MSG_NE (p, 0,
"fopen(" << m_testFilename <<
") should have been able to open a correctly created pcap file");
583 result = std::fread (&val32,
sizeof(val32), 1, p);
584 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() magic number");
585 NS_TEST_ASSERT_MSG_EQ (val32, Swap (uint32_t (0xa1b2c3d4)),
"Magic number written incorrectly");
587 result = std::fread (&val16,
sizeof(val16), 1, p);
588 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() version major");
589 NS_TEST_ASSERT_MSG_EQ (val16, Swap (uint16_t (2)),
"Version major written incorrectly");
591 result = std::fread (&val16,
sizeof(val16), 1, p);
592 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() version minor");
593 NS_TEST_ASSERT_MSG_EQ (val16, Swap (uint16_t (4)),
"Version minor written incorrectly");
595 result = std::fread (&val32,
sizeof(val32), 1, p);
596 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() time zone correction");
597 NS_TEST_ASSERT_MSG_EQ (val32, Swap (uint32_t (7)),
"Version minor written incorrectly");
599 result = std::fread (&val32,
sizeof(val32), 1, p);
600 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() sig figs");
601 NS_TEST_ASSERT_MSG_EQ (val32, 0,
"Sig figs written incorrectly");
603 result = std::fread (&val32,
sizeof(val32), 1, p);
604 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() snap length");
605 NS_TEST_ASSERT_MSG_EQ (val32, Swap (uint32_t (5678)),
"Snap length written incorrectly");
607 result = std::fread (&val32,
sizeof(val32), 1, p);
608 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() data link type");
609 NS_TEST_ASSERT_MSG_EQ (val32, Swap (uint32_t (1234)),
"Data length type written incorrectly");
621 f.
Open (m_testFilename, std::ios::in);
622 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (existing-initialized-file " << m_testFilename <<
623 ", \"std::ios::in\") returns error");
625 NS_TEST_ASSERT_MSG_EQ (f.
GetSwapMode (),
true,
"Byte-swapped file not correctly indicated");
627 NS_TEST_ASSERT_MSG_EQ (f.GetMagic (), 0xa1b2c3d4,
"Read back magic number incorrectly");
628 NS_TEST_ASSERT_MSG_EQ (f.GetVersionMajor (), 2,
"Read back version major incorrectly");
629 NS_TEST_ASSERT_MSG_EQ (f.GetVersionMinor (), 4,
"Read back version minor incorrectly");
630 NS_TEST_ASSERT_MSG_EQ (f.GetTimeZoneOffset (), 7,
"Read back time zone offset incorrectly");
631 NS_TEST_ASSERT_MSG_EQ (f.GetSigFigs (), 0,
"Read back sig figs incorrectly");
632 NS_TEST_ASSERT_MSG_EQ (f.GetSnapLen (), 5678,
"Read back snap len incorrectly");
633 NS_TEST_ASSERT_MSG_EQ (f.GetDataLinkType (), 1234,
"Read back data link type incorrectly");
650 virtual void DoRun (
void);
653 std::string m_testFilename;
656 RecordHeaderTestCase::RecordHeaderTestCase ()
657 :
TestCase (
"Check to see that PcapRecordHeader is managed correctly")
661 RecordHeaderTestCase::~RecordHeaderTestCase ()
668 std::stringstream filename;
669 uint32_t n = rand ();
671 m_testFilename = CreateTempDirFilename (filename.str () +
".pcap");
677 remove (m_testFilename.c_str ());
688 f.
Open (m_testFilename, std::ios::out);
689 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (" << m_testFilename <<
690 ", \"std::ios::out\") returns error");
696 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Init (37, 43, -7) returns error");
701 uint8_t bufferOut[128];
702 for (uint32_t i = 0; i < 128; ++i)
712 f.
Write (1234, 5678, bufferOut, 128);
713 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Write (write-only-file " << m_testFilename <<
") returns error");
720 FILE *p = std::fopen (m_testFilename.c_str (),
"r+b");
721 NS_TEST_ASSERT_MSG_NE (p, 0,
"fopen() should have been able to open a correctly created pcap file");
728 std::fseek (p, 0, SEEK_END);
729 uint64_t size = std::ftell (p);
730 NS_TEST_ASSERT_MSG_EQ (size, 83,
"Pcap file with one 43 byte packet is incorrect size");
737 std::fseek (p, 24, SEEK_SET);
764 bool bigEndian = u.b[3];
766 size_t result = std::fread (&val32,
sizeof(val32), 1, p);
767 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() seconds timestamp");
768 if (bigEndian) val32 = Swap (val32);
769 NS_TEST_ASSERT_MSG_EQ (val32, 1234,
"Seconds timestamp written incorrectly");
771 result = std::fread (&val32,
sizeof(val32), 1, p);
772 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() microseconds timestamp");
773 if (bigEndian) val32 = Swap (val32);
774 NS_TEST_ASSERT_MSG_EQ (val32, 5678,
"Microseconds timestamp written incorrectly");
776 result = std::fread (&val32,
sizeof(val32), 1, p);
777 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() included length");
778 if (bigEndian) val32 = Swap (val32);
779 NS_TEST_ASSERT_MSG_EQ (val32, 43,
"Included length written incorrectly");
781 result = std::fread (&val32,
sizeof(val32), 1, p);
782 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() actual length");
783 if (bigEndian) val32 = Swap (val32);
784 NS_TEST_ASSERT_MSG_EQ (val32, 128,
"Actual length written incorrectly");
790 uint8_t bufferIn[128];
792 result = std::fread (bufferIn, 1, 43, p);
793 NS_TEST_ASSERT_MSG_EQ (result, 43,
"Unable to fread() packet data of expected length");
795 for (uint32_t i = 0; i < 43; ++i)
797 NS_TEST_ASSERT_MSG_EQ (bufferIn[i], bufferOut[i],
"Incorrect packet data written");
807 f.
Open (m_testFilename, std::ios::in);
808 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (" << m_testFilename <<
809 ", \"std::ios::in\") of existing good file returns error");
811 uint32_t tsSec, tsUsec, inclLen, origLen, readLen;
813 f.
Read (bufferIn,
sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen);
814 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Read() of known good packet returns error");
815 NS_TEST_ASSERT_MSG_EQ (tsSec, 1234,
"Incorrectly read seconds timestap from known good packet");
816 NS_TEST_ASSERT_MSG_EQ (tsUsec, 5678,
"Incorrectly read microseconds timestap from known good packet");
817 NS_TEST_ASSERT_MSG_EQ (inclLen, 43,
"Incorrectly read included length from known good packet");
818 NS_TEST_ASSERT_MSG_EQ (origLen, 128,
"Incorrectly read original length from known good packet");
819 NS_TEST_ASSERT_MSG_EQ (readLen, 43,
"Incorrectly constructed actual read length from known good packet given buffer size");
825 for (uint32_t i = 0; i < 43; ++i)
827 NS_TEST_ASSERT_MSG_EQ (bufferIn[i], bufferOut[i],
"Incorrect packet data read from known good packet");
839 f.
Open (m_testFilename, std::ios::out);
840 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (" << m_testFilename <<
841 ", \"std::ios::out\") returns error");
846 f.
Init (37, 43, -7,
true);
847 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Init (37, 43, -7) returns error");
854 f.
Write (1234, 5678, bufferOut, 128);
855 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Write (write-only-file " << m_testFilename <<
") returns error");
862 p = std::fopen (m_testFilename.c_str (),
"r+b");
863 NS_TEST_ASSERT_MSG_NE (p, 0,
"fopen() should have been able to open a correctly created pcap file");
870 std::fseek (p, 0, SEEK_END);
871 size = std::ftell (p);
872 NS_TEST_ASSERT_MSG_EQ (size, 83,
"Pcap file with one 43 byte packet is incorrect size");
879 std::fseek (p, 24, SEEK_SET);
881 result = std::fread (&val32,
sizeof(val32), 1, p);
882 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() seconds timestamp");
883 NS_TEST_ASSERT_MSG_EQ (val32, Swap (uint32_t (1234)),
"Swapped seconds timestamp written incorrectly");
885 result = std::fread (&val32,
sizeof(val32), 1, p);
886 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() microseconds timestamp");
887 NS_TEST_ASSERT_MSG_EQ (val32, Swap (uint32_t (5678)),
"Swapped microseconds timestamp written incorrectly");
889 result = std::fread (&val32,
sizeof(val32), 1, p);
890 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() included length");
891 NS_TEST_ASSERT_MSG_EQ (val32, Swap (uint32_t (43)),
"Swapped included length written incorrectly");
893 result = std::fread (&val32,
sizeof(val32), 1, p);
894 NS_TEST_ASSERT_MSG_EQ (result, 1,
"Unable to fread() actual length");
895 NS_TEST_ASSERT_MSG_EQ (val32, Swap (uint32_t (128)),
"Swapped Actual length written incorrectly");
901 result = std::fread (bufferIn, 1, 43, p);
902 NS_TEST_ASSERT_MSG_EQ (result, 43,
"Unable to fread() packet data of expected length");
904 for (uint32_t i = 0; i < 43; ++i)
906 NS_TEST_ASSERT_MSG_EQ (bufferIn[i], bufferOut[i],
"Incorrect packet data written");
917 f.
Open (m_testFilename, std::ios::in);
918 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (" << m_testFilename <<
919 ", \"std::ios::in\") of existing good file returns error");
921 f.
Read (bufferIn,
sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen);
922 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Read() of known good packet returns error");
923 NS_TEST_ASSERT_MSG_EQ (tsSec, 1234,
"Incorrectly read seconds timestap from known good packet");
924 NS_TEST_ASSERT_MSG_EQ (tsUsec, 5678,
"Incorrectly read microseconds timestap from known good packet");
925 NS_TEST_ASSERT_MSG_EQ (inclLen, 43,
"Incorrectly read included length from known good packet");
926 NS_TEST_ASSERT_MSG_EQ (origLen, 128,
"Incorrectly read original length from known good packet");
927 NS_TEST_ASSERT_MSG_EQ (readLen, 43,
"Incorrectly constructed actual read length from known good packet given buffer size");
932 for (uint32_t i = 0; i < 43; ++i)
934 NS_TEST_ASSERT_MSG_EQ (bufferIn[i], bufferOut[i],
"Incorrect packet data read from known good packet");
952 virtual void DoRun (
void);
955 std::string m_testFilename;
958 ReadFileTestCase::ReadFileTestCase ()
959 :
TestCase (
"Check to see that PcapFile can read out a known good pcap file")
963 ReadFileTestCase::~ReadFileTestCase ()
977 const uint32_t N_KNOWN_PACKETS = 6;
978 const uint32_t N_PACKET_BYTES = 16;
980 typedef struct PACKET_ENTRY {
985 uint16_t data[N_PACKET_BYTES];
989 { 2, 3696, 46, 46, { 0x0001, 0x0800, 0x0604, 0x0001, 0x0000, 0x0000, 0x0003, 0x0a01,
990 0x0201, 0xffff, 0xffff, 0xffff, 0x0a01, 0x0204, 0x0000, 0x0000}},
991 { 2, 3707, 46, 46, { 0x0001, 0x0800, 0x0604, 0x0002, 0x0000, 0x0000, 0x0006, 0x0a01,
992 0x0204, 0x0000, 0x0000, 0x0003, 0x0a01, 0x0201, 0x0000, 0x0000}},
993 { 2, 3801, 1070, 1070, { 0x4500, 0x041c, 0x0000, 0x0000, 0x3f11, 0x0000, 0x0a01, 0x0101,
994 0x0a01, 0x0204, 0xc001, 0x0009, 0x0408, 0x0000, 0x0000, 0x0000}},
995 { 2, 3811, 46, 46, { 0x0001, 0x0800, 0x0604, 0x0001, 0x0000, 0x0000, 0x0006, 0x0a01,
996 0x0204, 0xffff, 0xffff, 0xffff, 0x0a01, 0x0201, 0x0000, 0x0000}},
997 { 2, 3822, 46, 46, { 0x0001, 0x0800, 0x0604, 0x0002, 0x0000, 0x0000, 0x0003, 0x0a01,
998 0x0201, 0x0000, 0x0000, 0x0006, 0x0a01, 0x0204, 0x0000, 0x0000}},
999 { 2, 3915, 1070, 1070, { 0x4500, 0x041c, 0x0000, 0x0000, 0x4011, 0x0000, 0x0a01, 0x0204,
1000 0x0a01, 0x0101, 0x0009, 0xc001, 0x0408, 0x0000, 0x0000, 0x0000}}
1011 std::string filename = CreateDataDirFilename (
"known.pcap");
1012 f.
Open (filename, std::ios::in);
1013 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (" << filename <<
1014 ", \"std::ios::in\") returns error");
1024 uint8_t data[N_PACKET_BYTES];
1025 uint32_t tsSec, tsUsec, inclLen, origLen, readLen;
1029 for (uint32_t i = 0; i < N_KNOWN_PACKETS; ++i, ++p)
1031 f.
Read (data,
sizeof(data), tsSec, tsUsec, inclLen, origLen, readLen);
1032 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Read() of known good pcap file returns error");
1033 NS_TEST_ASSERT_MSG_EQ (tsSec, p->tsSec,
"Incorrectly read seconds timestap from known good pcap file");
1034 NS_TEST_ASSERT_MSG_EQ (tsUsec, p->tsUsec,
"Incorrectly read microseconds timestap from known good pcap file");
1035 NS_TEST_ASSERT_MSG_EQ (inclLen, p->inclLen,
"Incorrectly read included length from known good packet");
1036 NS_TEST_ASSERT_MSG_EQ (origLen, p->origLen,
"Incorrectly read original length from known good packet");
1037 NS_TEST_ASSERT_MSG_EQ (readLen, N_PACKET_BYTES,
"Incorrect actual read length from known good packet given buffer size");
1044 f.
Read (data, 1, tsSec, tsUsec, inclLen, origLen, readLen);
1045 NS_TEST_ASSERT_MSG_EQ (f.
Eof (),
true,
"Read() of known good pcap file at EOF does not return error");
1059 virtual void DoRun (
void);
1062 DiffTestCase::DiffTestCase ()
1063 :
TestCase (
"Check that PcapFile::Diff works as expected")
1073 std::string filename = CreateDataDirFilename (
"known.pcap");
1074 uint32_t sec (0), usec (0);
1075 bool diff = PcapFile::Diff (filename, filename, sec, usec);
1076 NS_TEST_EXPECT_MSG_EQ (diff,
false,
"PcapDiff(file, file) must always be false");
1081 std::string filename2 =
"different.pcap";
1084 f.
Open (filename2, std::ios::out);
1085 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Open (" << filename2 <<
", \"std::ios::out\") returns error");
1086 f.
Init (1, N_PACKET_BYTES);
1087 NS_TEST_ASSERT_MSG_EQ (f.
Fail (),
false,
"Init (1, " << N_PACKET_BYTES <<
") returns error");
1089 for (uint32_t i = 0; i < N_KNOWN_PACKETS; ++i)
1093 f.
Write (p.tsSec, p.tsUsec, (uint8_t
const *)p.data, p.origLen);
1094 NS_TEST_EXPECT_MSG_EQ (f.
Fail (),
false,
"Write must not fail");
1098 diff = PcapFile::Diff (filename, filename2, sec, usec);
1099 NS_TEST_EXPECT_MSG_EQ (diff,
true,
"PcapDiff(file, file2) must be true");
1100 NS_TEST_EXPECT_MSG_EQ (sec, 2,
"Files are different from 2.3696 seconds");
1101 NS_TEST_EXPECT_MSG_EQ (usec, 3696,
"Files are different from 2.3696 seconds");
1110 PcapFileTestSuite::PcapFileTestSuite ()
virtual void DoSetup(void)
Implementation to do any local setup required for this test case.
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
virtual void DoRun(void)
Implementation to actually run this test case.
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
bool GetSwapMode(void)
Get the swap mode of the file.
void Read(uint8_t *const data, uint32_t maxBytes, uint32_t &tsSec, uint32_t &tsUsec, uint32_t &inclLen, uint32_t &origLen, uint32_t &readLen)
Read next packet from file.
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
virtual void DoRun(void)
Implementation to actually run this test case.
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
virtual void DoRun(void)=0
Implementation to actually run this test case.
virtual void DoSetup(void)
Implementation to do any local setup required for this test case.
virtual void DoSetup(void)
Implementation to do any local setup required for this test case.
void Init(uint32_t dataLinkType, uint32_t snapLen=SNAPLEN_DEFAULT, int32_t timeZoneCorrection=ZONE_DEFAULT, bool swapMode=false)
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
void Open(std::string const &filename, std::ios::openmode mode)
virtual void DoRun(void)
Implementation to actually run this test case.
void SetDataDir(std::string directory)
virtual void DoSetup(void)
Implementation to do any local setup required for this test case.
virtual void DoRun(void)
Implementation to actually run this test case.
void Write(uint32_t tsSec, uint32_t tsUsec, uint8_t const *const data, uint32_t totalLen)
Write next packet to file.