8#include "ns3/pcap-file.h"
28 return ((val >> 8) & 0x00ff) | ((val << 8) & 0xff00);
34 return ((val >> 24) & 0x000000ff) | ((val >> 8) & 0x0000ff00) | ((val << 8) & 0x00ff0000) |
35 ((val << 24) & 0xff000000);
41 FILE* p = std::fopen(filename.c_str(),
"rb");
54 FILE* p = std::fopen(filename.c_str(),
"rb");
60 std::fseek(p, 0, SEEK_END);
62 auto sizeActual = std::ftell(p);
65 return sizeActual == sizeExpected;
83 void DoRun()
override;
90 :
TestCase(
"Check to see that PcapFile::Open with mode std::ios::out works")
101 std::stringstream filename;
133 <<
", \"std::ios::out\") does not create file");
137 <<
", \"std::ios::out\") does not result in an empty file");
146 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
148 f.
Init(1234, 5678, 7);
155 "Init () does not result in a file with a pcap file header");
164 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
171 <<
", \"w\") does not result in an empty file");
179 f.
Init(1234, 5678, 7);
188 memset(buffer, 0,
sizeof(buffer));
189 f.
Write(0, 0, buffer, 128);
192 "Write (write-only-file " <<
m_testFilename <<
") returns error");
210 void DoRun()
override;
217 :
TestCase(
"Check to see that PcapFile::Open with mode std::ios::in works")
228 std::stringstream filename;
254 "Open (non-existing-filename "
255 <<
m_testFilename <<
", \"std::ios::in\") does not return error");
261 <<
", \"std::ios::in\") unexpectedly created a file");
276 "Open (non-initialized-filename "
277 <<
m_testFilename <<
", \"std::ios::in\") does not return error");
288 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
290 f.
Init(1234, 5678, 7);
301 <<
", \"std::ios::in\") returns error");
308 f.
Write(0, 0, buffer, 128);
311 "Write (read-only-file " <<
m_testFilename <<
") does not return error");
321class AppendModeCreateTestCase :
public TestCase
324 AppendModeCreateTestCase ();
325 virtual ~AppendModeCreateTestCase ();
329 virtual void DoRun ();
332 std::string m_testFilename;
335AppendModeCreateTestCase::AppendModeCreateTestCase ()
336 :
TestCase (
"Check to see that PcapFile::Open with mode std::ios::app works")
340AppendModeCreateTestCase::~AppendModeCreateTestCase ()
345AppendModeCreateTestCase::DoSetup ()
347 std::stringstream filename;
350 m_testFilename = CreateTempDirFilename (filename.str () +
".pcap");
354AppendModeCreateTestCase::DoTeardown ()
356 if (remove (m_testFilename.c_str ()))
358 NS_LOG_ERROR (
"Failed to delete file " << m_testFilename);
363AppendModeCreateTestCase::DoRun ()
370 f.
Open (m_testFilename, std::ios::out | std::ios::app);
372 ", \"std::ios::app\") does not return error");
377 "Open (" << m_testFilename <<
", \"std::ios::app\") unexpectedly created a file");
382 f.
Open (m_testFilename, std::ios::out);
384 ", \"std::ios::out\") returns error");
390 f.
Open (m_testFilename, std::ios::out | std::ios::app);
392 ", \"std::ios::app\") does not return error");
399 f.
Open (m_testFilename, std::ios::out);
401 ", \"std::ios::out\") returns error");
403 f.
Init (1234, 5678, 7);
410 f.
Open (m_testFilename, std::ios::out | std::ios::app);
412 ", \"std::ios::app\") returns error");
418 memset (buffer, 0,
sizeof(buffer));
419 f.
Write (0, 0, buffer, 128);
442 void DoRun()
override;
449 :
TestCase(
"Check to see that PcapFileHeader is managed correctly")
460 std::stringstream filename;
486 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
491 f.
Init(1234, 5678, 7);
503 <<
") should have been able to open a correctly created pcap file");
531 bool bigEndian = u.b[3];
533 size_t result = std::fread(&val32,
sizeof(val32), 1, p);
541 result = std::fread(&val16,
sizeof(val16), 1, p);
549 result = std::fread(&val16,
sizeof(val16), 1, p);
557 result = std::fread(&val32,
sizeof(val32), 1, p);
565 result = std::fread(&val32,
sizeof(val32), 1, p);
573 result = std::fread(&val32,
sizeof(val32), 1, p);
581 result = std::fread(&val32,
sizeof(val32), 1, p);
604 "Open (existing-initialized-file "
622 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
632 f.
Init(1234, 5678, 7,
true);
645 <<
") should have been able to open a correctly created pcap file");
647 result = std::fread(&val32,
sizeof(val32), 1, p);
651 result = std::fread(&val16,
sizeof(val16), 1, p);
655 result = std::fread(&val16,
sizeof(val16), 1, p);
659 result = std::fread(&val32,
sizeof(val32), 1, p);
663 result = std::fread(&val32,
sizeof(val32), 1, p);
667 result = std::fread(&val32,
sizeof(val32), 1, p);
671 result = std::fread(&val32,
sizeof(val32), 1, p);
688 "Open (existing-initialized-file "
720 void DoRun()
override;
727 :
TestCase(
"Check to see that PcapRecordHeader is managed correctly")
738 std::stringstream filename;
764 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
775 uint8_t bufferOut[128];
786 f.
Write(1234, 5678, bufferOut, 128);
789 "Write (write-only-file " <<
m_testFilename <<
") returns error");
799 "fopen() should have been able to open a correctly created pcap file");
806 std::fseek(p, 0, SEEK_END);
807 auto size = std::ftell(p);
815 std::fseek(p, 24, SEEK_SET);
842 bool bigEndian = u.b[3];
844 size_t result = std::fread(&val32,
sizeof(val32), 1, p);
852 result = std::fread(&val32,
sizeof(val32), 1, p);
860 result = std::fread(&val32,
sizeof(val32), 1, p);
868 result = std::fread(&val32,
sizeof(val32), 1, p);
880 uint8_t bufferIn[128];
882 result = std::fread(bufferIn, 1, 43, p);
901 <<
", \"std::ios::in\") of existing good file returns error");
909 f.
Read(bufferIn,
sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen);
914 "Incorrectly read microseconds timestamp from known good packet");
920 "Incorrectly constructed actual read length from known good packet given buffer size");
930 "Incorrect packet data read from known good packet");
945 "Open (" <<
m_testFilename <<
", \"std::ios::out\") returns error");
950 f.
Init(37, 43, -7,
true);
958 f.
Write(1234, 5678, bufferOut, 128);
961 "Write (write-only-file " <<
m_testFilename <<
") returns error");
971 "fopen() should have been able to open a correctly created pcap file");
978 std::fseek(p, 0, SEEK_END);
979 size = std::ftell(p);
987 result = std::fseek(p, 24, SEEK_SET);
990 result = std::fread(&val32,
sizeof(val32), 1, p);
994 "Swapped seconds timestamp written incorrectly");
996 result = std::fread(&val32,
sizeof(val32), 1, p);
1000 "Swapped microseconds timestamp written incorrectly");
1002 result = std::fread(&val32,
sizeof(val32), 1, p);
1006 result = std::fread(&val32,
sizeof(val32), 1, p);
1014 result = std::fread(bufferIn, 1, 43, p);
1034 <<
", \"std::ios::in\") of existing good file returns error");
1036 f.
Read(bufferIn,
sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen);
1041 "Incorrectly read microseconds timestamp from known good packet");
1047 "Incorrectly constructed actual read length from known good packet given buffer size");
1056 "Incorrect packet data read from known good packet");
1077 void DoRun()
override;
1084 :
TestCase(
"Check to see that PcapFile can read out a known good pcap file")
1248 f.
Open(filename, std::ios::in);
1251 "Open (" << filename <<
", \"std::ios::in\") returns error");
1272 f.
Read(
data,
sizeof(
data), tsSec, tsUsec, inclLen, origLen, readLen);
1276 "Incorrectly read seconds timestamp from known good pcap file");
1279 "Incorrectly read microseconds timestamp from known good pcap file");
1282 "Incorrectly read included length from known good packet");
1285 "Incorrectly read original length from known good packet");
1289 "Incorrect actual read length from known good packet given buffer size");
1296 f.
Read(
data, 1, tsSec, tsUsec, inclLen, origLen, readLen);
1299 "Read() of known good pcap file at EOF does not return error");
1316 void DoRun()
override;
1320 :
TestCase(
"Check that PcapFile::Diff works as expected")
1334 bool diff =
PcapFile::Diff(filename, filename, sec, usec, packets);
1344 f.
Open(filename2, std::ios::out);
1347 "Open (" << filename2 <<
", \"std::ios::out\") returns error");
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.
void Close()
Close the underlying file.
static bool Diff(const std::string &f1, const std::string &f2, uint32_t &sec, uint32_t &usec, uint32_t &packets, uint32_t snapLen=SNAPLEN_DEFAULT)
Compare two PCAP files packet-by-packet.
void Open(const std::string &filename, std::ios::openmode mode)
Create a new pcap file or open an existing pcap file.
uint32_t GetDataLinkType()
Returns the data link type field of the pcap file as defined by the network field in the pcap global ...
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.
uint32_t GetMagic()
Returns the magic number of the pcap file as defined by the magic_number field in the pcap global hea...
uint16_t GetVersionMajor()
Returns the major version of the pcap file as defined by the version_major field in the pcap global h...
uint16_t GetVersionMinor()
Returns the minor version of the pcap file as defined by the version_minor field in the pcap global h...
void Clear()
Clear all state bits of the underlying iostream.
void Init(uint32_t dataLinkType, uint32_t snapLen=SNAPLEN_DEFAULT, int32_t timeZoneCorrection=ZONE_DEFAULT, bool swapMode=false, bool nanosecMode=false)
Initialize the pcap file associated with this object.
void Write(uint32_t tsSec, uint32_t tsUsec, const uint8_t *const data, uint32_t totalLen)
Write next packet to file.
uint32_t GetSnapLen()
Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the...
bool GetSwapMode()
Get the swap mode of the file.
int32_t GetTimeZoneOffset()
Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global hea...
uint32_t GetSigFigs()
Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap...
std::string CreateDataDirFilename(std::string filename)
Construct the full path to a file in the data directory.
void AddTestCase(TestCase *testCase, Duration duration=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.