A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ns3tcp-socket-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
8
9#include "ns3/abort.h"
10#include "ns3/config.h"
11#include "ns3/csma-helper.h"
12#include "ns3/data-rate.h"
13#include "ns3/inet-socket-address.h"
14#include "ns3/internet-stack-helper.h"
15#include "ns3/ipv4-address-helper.h"
16#include "ns3/ipv4-global-routing-helper.h"
17#include "ns3/log.h"
18#include "ns3/node-container.h"
19#include "ns3/packet-sink-helper.h"
20#include "ns3/pcap-file.h"
21#include "ns3/point-to-point-helper.h"
22#include "ns3/simulator.h"
23#include "ns3/string.h"
24#include "ns3/tcp-socket-factory.h"
25#include "ns3/test.h"
26#include "ns3/uinteger.h"
27
28using namespace ns3;
29
30NS_LOG_COMPONENT_DEFINE("Ns3SocketTest");
31
32/**
33 * \ingroup system-tests-tcp
34 *
35 * \brief Tests of TCP implementations from the application/socket perspective
36 * using point-to-point links.
37 */
39{
40 public:
42
44 {
45 }
46
47 private:
48 void DoRun() override;
49 bool m_writeResults; //!< True if write PCAP files.
50
51 /**
52 * Receive a TCP packet.
53 * \param path The callback context (unused).
54 * \param p The received packet.
55 * \param address The sender's address (unused).
56 */
57 void SinkRx(std::string path, Ptr<const Packet> p, const Address& address);
58
59 TestVectors<uint32_t> m_inputs; //!< Sent packets test vector.
60 TestVectors<uint32_t> m_responses; //!< Received packets test vector.
61};
62
64 : TestCase("Check that ns-3 TCP successfully transfers an application data write of various "
65 "sizes (point-to-point)"),
66 m_writeResults(false)
67{
68}
69
70void
71Ns3TcpSocketTestCaseP2P::SinkRx(std::string path, Ptr<const Packet> p, const Address& address)
72{
73 m_responses.Add(p->GetSize());
74}
75
76void
78{
79 uint16_t sinkPort = 50000;
80 double sinkStopTime = 40; // sec; will trigger Socket::Close
81 double writerStopTime = 30; // sec; will trigger Socket::Close
82 double simStopTime = 60; // sec
83 Time sinkStopTimeObj = Seconds(sinkStopTime);
84 Time writerStopTimeObj = Seconds(writerStopTime);
85 Time simStopTimeObj = Seconds(simStopTime);
86
89
90 PointToPointHelper pointToPoint;
91 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
92 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
93
94 NetDeviceContainer devices;
95 devices = pointToPoint.Install(n0, n1);
96
97 InternetStackHelper internet;
98 internet.InstallAll();
99
100 Ipv4AddressHelper address;
101 address.SetBase("10.1.1.0", "255.255.255.252");
102 Ipv4InterfaceContainer ifContainer = address.Assign(devices);
103
105 Address sinkAddress(InetSocketAddress(ifContainer.GetAddress(1), sinkPort));
106 socketWriter->Setup(n0, sinkAddress);
107 n0->AddApplication(socketWriter);
108 socketWriter->SetStartTime(Seconds(0.));
109 socketWriter->SetStopTime(writerStopTimeObj);
110
111 PacketSinkHelper sink("ns3::TcpSocketFactory",
113 ApplicationContainer apps = sink.Install(n1);
114 // Start the sink application at time zero, and stop it at sinkStopTime
115 apps.Start(Seconds(0.0));
116 apps.Stop(sinkStopTimeObj);
117
118 Config::Connect("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
120
122 // Send 1, 10, 100, 1000 bytes
123 Simulator::Schedule(Seconds(10), &SocketWriter::Write, socketWriter, 1);
124 m_inputs.Add(1);
125 Simulator::Schedule(Seconds(12), &SocketWriter::Write, socketWriter, 10);
126 m_inputs.Add(10);
127 Simulator::Schedule(Seconds(14), &SocketWriter::Write, socketWriter, 100);
128 m_inputs.Add(100);
129 Simulator::Schedule(Seconds(16), &SocketWriter::Write, socketWriter, 1000);
130 m_inputs.Add(536);
131 m_inputs.Add(464); // ns-3 TCP default segment size of 536
132 Simulator::Schedule(writerStopTimeObj, &SocketWriter::Close, socketWriter);
133
134 if (m_writeResults)
135 {
136 pointToPoint.EnablePcapAll("tcp-socket-test-case-1");
137 }
138
139 Simulator::Stop(simStopTimeObj);
142
143 // Compare inputs and outputs
146 "Incorrect number of expected receive events");
147 for (uint32_t i = 0; i < m_responses.GetN(); i++)
148 {
149 uint32_t in = m_inputs.Get(i);
150 uint32_t out = m_responses.Get(i);
152 out,
153 "Mismatch: expected " << in << " bytes, got " << out << " bytes");
154 }
155}
156
157/**
158 * \ingroup system-tests-tcp
159 *
160 * \brief Tests of TCP implementations from the application/socket perspective
161 * using CSMA links.
162 */
164{
165 public:
167
169 {
170 }
171
172 private:
173 void DoRun() override;
174 bool m_writeResults; //!< True if write PCAP files.
175
176 /**
177 * Receive a TCP packet.
178 * \param path The callback context (unused).
179 * \param p The received packet.
180 * \param address The sender's address (unused).
181 */
182 void SinkRx(std::string path, Ptr<const Packet> p, const Address& address);
183
184 TestVectors<uint32_t> m_inputs; //!< Sent packets test vector.
185 TestVectors<uint32_t> m_responses; //!< Received packets test vector.
186};
187
189 : TestCase("Check to see that ns-3 TCP successfully transfers an application data write of "
190 "various sizes (CSMA)"),
191 m_writeResults(false)
192{
193}
194
195void
197{
198 m_responses.Add(p->GetSize());
199}
200
201void
203{
204 uint16_t sinkPort = 50000;
205 double sinkStopTime = 40; // sec; will trigger Socket::Close
206 double writerStopTime = 30; // sec; will trigger Socket::Close
207 double simStopTime = 60; // sec
208 Time sinkStopTimeObj = Seconds(sinkStopTime);
209 Time writerStopTimeObj = Seconds(writerStopTime);
210 Time simStopTimeObj = Seconds(simStopTime);
211
212 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(1000));
213
215 nodes.Create(2);
216 Ptr<Node> n0 = nodes.Get(0);
217 Ptr<Node> n1 = nodes.Get(1);
218
219 CsmaHelper csma;
220 csma.SetChannelAttribute("DataRate", StringValue("5Mbps"));
221 csma.SetChannelAttribute("Delay", StringValue("2ms"));
222
223 NetDeviceContainer devices;
224 devices = csma.Install(nodes);
225
226 InternetStackHelper internet;
227 internet.InstallAll();
228
229 Ipv4AddressHelper address;
230 address.SetBase("10.1.1.0", "255.255.255.252");
231 Ipv4InterfaceContainer ifContainer = address.Assign(devices);
232
234 Address sinkAddress(InetSocketAddress(ifContainer.GetAddress(1), sinkPort));
235 socketWriter->Setup(n0, sinkAddress);
236 n0->AddApplication(socketWriter);
237 socketWriter->SetStartTime(Seconds(0.));
238 socketWriter->SetStopTime(writerStopTimeObj);
239
240 PacketSinkHelper sink("ns3::TcpSocketFactory",
242 ApplicationContainer apps = sink.Install(n1);
243 // Start the sink application at time zero, and stop it at sinkStopTime
244 apps.Start(Seconds(0.0));
245 apps.Stop(sinkStopTimeObj);
246
247 Config::Connect("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
249
251 // Send 1, 10, 100, 1000 bytes
252 // PointToPoint default MTU is 576 bytes, which leaves 536 bytes for TCP
253 Simulator::Schedule(Seconds(10), &SocketWriter::Write, socketWriter, 1);
254 m_inputs.Add(1);
255 Simulator::Schedule(Seconds(12), &SocketWriter::Write, socketWriter, 10);
256 m_inputs.Add(10);
257 Simulator::Schedule(Seconds(14), &SocketWriter::Write, socketWriter, 100);
258 m_inputs.Add(100);
259 Simulator::Schedule(Seconds(16), &SocketWriter::Write, socketWriter, 1000);
260 m_inputs.Add(1000);
261 // Next packet will fragment
262 Simulator::Schedule(Seconds(16), &SocketWriter::Write, socketWriter, 1001);
263 m_inputs.Add(1000);
264 m_inputs.Add(1);
265 Simulator::Schedule(writerStopTimeObj, &SocketWriter::Close, socketWriter);
266
267 if (m_writeResults)
268 {
269 csma.EnablePcapAll("tcp-socket-test-case-2", false);
270 }
271 Simulator::Stop(simStopTimeObj);
274
275 // Compare inputs and outputs
278 "Incorrect number of expected receive events");
279 for (uint32_t i = 0; i < m_responses.GetN(); i++)
280 {
281 uint32_t in = m_inputs.Get(i);
282 uint32_t out = m_responses.Get(i);
284 out,
285 "Mismatch: expected " << in << " bytes, got " << out << " bytes");
286 }
287}
288
289/**
290 * \ingroup system-tests-tcp
291 *
292 * TCP implementations from the application/socket perspective TestSuite.
293 */
295{
296 public:
298};
299
301 : TestSuite("ns3-tcp-socket", Type::SYSTEM)
302{
303 AddTestCase(new Ns3TcpSocketTestCaseP2P, TestCase::Duration::QUICK);
304 AddTestCase(new Ns3TcpSocketTestCaseCsma, TestCase::Duration::QUICK);
305}
306
307/// Do not forget to allocate an instance of this TestSuite.
Tests of TCP implementations from the application/socket perspective using CSMA links.
TestVectors< uint32_t > m_inputs
Sent packets test vector.
TestVectors< uint32_t > m_responses
Received packets test vector.
bool m_writeResults
True if write PCAP files.
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
Receive a TCP packet.
void DoRun() override
Implementation to actually run this TestCase.
Tests of TCP implementations from the application/socket perspective using point-to-point links.
TestVectors< uint32_t > m_responses
Received packets test vector.
TestVectors< uint32_t > m_inputs
Sent packets test vector.
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
Receive a TCP packet.
void DoRun() override
Implementation to actually run this TestCase.
bool m_writeResults
True if write PCAP files.
TCP implementations from the application/socket perspective TestSuite.
a polymophic address class
Definition address.h:90
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
void Connect()
Connect the socket.
void Write(uint32_t numBytes)
Write to the socket.
void Close()
Close the socket.
Hold variables of type string.
Definition string.h:45
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
A simple way to store test vectors (for stimulus or from responses)
Definition test.h:1348
T Get(std::size_t i) const
Get the i'th test vector.
Definition test.h:1462
std::size_t GetN() const
Get the total number of test vectors.
Definition test.h:1455
std::size_t Add(T vector)
Definition test.h:1446
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Hold an unsigned integer type.
Definition uinteger.h:34
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
void Connect(std::string path, const CallbackBase &cb)
Definition config.cc:967
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:134
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
static Ns3TcpSocketTestSuite g_ns3TcpSocketTestSuite
Do not forget to allocate an instance of this TestSuite.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44