A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
pcap-explicit-filename-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Gabriel Ferreira <gabrielcarvfer@gmail.com>
7 */
8
9#include "ns3/iana-link-type-numbers.h"
10#include "ns3/log.h"
11#include "ns3/packet.h"
12#include "ns3/pcap-file-wrapper.h"
13#include "ns3/pcap-file.h"
14#include "ns3/ptr.h"
15#include "ns3/simulator.h"
16#include "ns3/test.h"
17#include "ns3/trace-helper.h"
18
19#include <cstdint>
20#include <cstdio>
21#include <ios>
22#include <string>
23#include <vector>
24
25using namespace ns3;
26
27NS_LOG_COMPONENT_DEFINE("pcap-explicit-filename-test-suite");
28
29/**
30 * @ingroup network-test
31 * @ingroup tests
32 *
33 * @brief Regression test for @issueid{1150}.
34 *
35 * Reusing the same pcap filename for more than one interface, e.g. calling
36 * EnablePcapIpv4(prefix, ..., explicitFilename = true) for two different
37 * interfaces with the same @c prefix, must produce a single well-formed pcap
38 * file containing every record written by every interface.
39 */
41{
42 public:
45
46 private:
47 void DoSetup() override;
48 void DoRun() override;
49 void DoTeardown() override;
50
51 /// Magic number of a standard (microsecond) little-endian pcap file.
52 static constexpr uint32_t PCAP_MAGIC = 0xa1b2c3d4;
53 /// Size, in bytes, of the pcap global header.
54 static constexpr uint32_t PCAP_GLOBAL_HEADER_LEN = 24;
55 /// Size, in bytes, of a per-packet pcap record header.
56 static constexpr uint32_t PCAP_RECORD_HEADER_LEN = 16;
57 /// Snap length used when initializing the pcap files.
58 static constexpr uint32_t SNAP_LEN = 65535;
59
60 std::string m_testFilename; //!< Explicit pcap file name shared by both "interfaces"
61};
62
64 : TestCase("Reusing one explicit pcap filename for two interfaces yields a valid pcap (#1150)")
65{
66}
67
71
72void
74{
75 m_testFilename = CreateTempDirFilename("pcap-explicit-filename-1150.pcap");
76 std::remove(m_testFilename.c_str());
77}
78
79void
81{
82 if (std::remove(m_testFilename.c_str()))
83 {
84 NS_LOG_ERROR("Failed to delete file " << m_testFilename);
85 }
86}
87
88void
90{
91 PcapHelper pcapHelper;
92
94 pcapHelper.CreateFile(m_testFilename, std::ios::out, iana::linktype::RAW, SNAP_LEN);
95 NS_TEST_ASSERT_MSG_EQ(fileA->Fail(),
96 false,
97 "CreateFile() for the first interface should succeed");
98
100 pcapHelper.CreateFile(m_testFilename, std::ios::out, iana::linktype::RAW, SNAP_LEN);
101 NS_TEST_ASSERT_MSG_EQ(fileB->Fail(),
102 false,
103 "CreateFile() for the second interface should succeed");
104
105 std::vector<uint8_t> payloadA(40, 0xa1);
106 std::vector<uint8_t> payloadB(80, 0xb2);
107 Ptr<Packet> pktA = Create<Packet>(payloadA.data(), payloadA.size());
108 Ptr<Packet> pktB = Create<Packet>(payloadB.data(), payloadB.size());
109
110 // Interleaved writes, mirroring the trace sinks of the two interfaces.
111 fileA->Write(Seconds(1), pktA);
112 fileB->Write(Seconds(2), pktB);
113 fileA->Write(Seconds(3), pktA);
114 fileB->Write(Seconds(4), pktB);
115
116 // Flush and close both file handles.
117 fileA = nullptr;
118 fileB = nullptr;
119
120 PcapFile reader;
121 reader.Open(m_testFilename, std::ios::in);
123 false,
124 "Resulting pcap file should have a valid global header");
127 "Resulting pcap file should carry the standard pcap magic number");
128
129 FILE* raw = std::fopen(m_testFilename.c_str(), "rb");
130 NS_TEST_ASSERT_MSG_NE(raw, nullptr, "Should be able to open the resulting pcap file");
131 std::fseek(raw, 0, SEEK_END);
132 auto fileSize = static_cast<uint64_t>(std::ftell(raw));
133 std::fclose(raw);
134
135 // PcapFile::Read() positions the reader just past the global header on
136 // Open(), so account for that header up front.
137 uint64_t consumed = PCAP_GLOBAL_HEADER_LEN;
138 uint32_t recordCount = 0;
139 std::vector<uint8_t> buffer(SNAP_LEN);
140
141 while (consumed + PCAP_RECORD_HEADER_LEN <= fileSize)
142 {
143 uint32_t tsSec = 0;
144 uint32_t tsUsec = 0;
145 uint32_t inclLen = 0;
146 uint32_t origLen = 0;
147 uint32_t readLen = 0;
148
149 reader.Read(buffer.data(), buffer.size(), tsSec, tsUsec, inclLen, origLen, readLen);
150
151 if (reader.Eof())
152 {
153 break;
154 }
156 false,
157 "Record " << recordCount << " should read without error");
158
160 SNAP_LEN,
161 "Record " << recordCount
162 << " has an implausible included length");
163
165 fileSize,
166 "Record " << recordCount << " runs past the end of the file");
167
168 consumed += PCAP_RECORD_HEADER_LEN + inclLen;
169 recordCount++;
170 }
171
172 NS_TEST_ASSERT_MSG_EQ(consumed,
173 fileSize,
174 "Record stream should end exactly at the end of the file");
175
176 NS_TEST_ASSERT_MSG_EQ(recordCount, 4, "Resulting pcap should contain all four written records");
177
178 reader.Close();
179}
180
181/**
182 * @ingroup network-test
183 * @ingroup tests
184 *
185 * @brief Test suite for the @issueid{1150} explicit-pcap-filename regression test.
186 */
188{
189 public:
191};
192
198
200 g_pcapExplicitFilenameTestSuite; //!< Static variable for test initialization
static constexpr uint32_t PCAP_RECORD_HEADER_LEN
Size, in bytes, of a per-packet pcap record header.
static constexpr uint32_t PCAP_MAGIC
Magic number of a standard (microsecond) little-endian pcap file.
std::string m_testFilename
Explicit pcap file name shared by both "interfaces".
static constexpr uint32_t SNAP_LEN
Snap length used when initializing the pcap files.
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.
static constexpr uint32_t PCAP_GLOBAL_HEADER_LEN
Size, in bytes, of the pcap global header.
void DoRun() override
Implementation to actually run this TestCase.
Test suite for the Issue 1150 explicit-pcap-filename regression test.
A class representing a pcap file.
Definition pcap-file.h:32
void Close()
Close the underlying file.
Definition pcap-file.cc:81
void Open(const std::string &filename, std::ios::openmode mode)
Create a new pcap file or open an existing pcap file.
Definition pcap-file.cc:320
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.
Definition pcap-file.cc:468
uint32_t GetMagic()
Returns the magic number of the pcap file as defined by the magic_number field in the pcap global hea...
Definition pcap-file.cc:88
bool Eof() const
Definition pcap-file.cc:67
bool Fail() const
Definition pcap-file.cc:60
Manage pcap files for device models.
Ptr< PcapFileWrapper > CreateFile(std::string filename, std::ios::openmode filemode, uint16_t dataLinkType, uint32_t snapLen=std::numeric_limits< uint32_t >::max(), int32_t tzCorrection=0)
Create and initialize a pcap file.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
std::string CreateTempDirFilename(std::string filename)
Construct the full path to a file in a temporary directory.
Definition test.cc:436
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:253
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
#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.
Definition test.h:133
#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.
Definition test.h:553
#define NS_TEST_ASSERT_MSG_LT_OR_EQ(actual, limit, msg)
Test that an actual value is less than or equal to a limit and report and abort if not.
Definition test.h:739
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1313
constexpr uint16_t RAW
Raw IP; begins with an IPv4 or IPv6 header.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static PcapExplicitFilenameTestSuite g_pcapExplicitFilenameTestSuite
Static variable for test initialization.