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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
19
20#include "ns3/abort.h"
21#include "ns3/config.h"
22#include "ns3/csma-helper.h"
23#include "ns3/data-rate.h"
24#include "ns3/inet-socket-address.h"
25#include "ns3/internet-stack-helper.h"
26#include "ns3/ipv4-address-helper.h"
27#include "ns3/ipv4-global-routing-helper.h"
28#include "ns3/log.h"
29#include "ns3/node-container.h"
30#include "ns3/packet-sink-helper.h"
31#include "ns3/pcap-file.h"
32#include "ns3/point-to-point-helper.h"
33#include "ns3/simulator.h"
34#include "ns3/string.h"
35#include "ns3/tcp-socket-factory.h"
36#include "ns3/test.h"
37#include "ns3/uinteger.h"
38
39using namespace ns3;
40
41NS_LOG_COMPONENT_DEFINE("Ns3SocketTest");
42
43/**
44 * \ingroup system-tests-tcp
45 *
46 * \brief Tests of TCP implementations from the application/socket perspective
47 * using point-to-point links.
48 */
50{
51 public:
53
55 {
56 }
57
58 private:
59 void DoRun() override;
60 bool m_writeResults; //!< True if write PCAP files.
61
62 /**
63 * Receive a TCP packet.
64 * \param path The callback context (unused).
65 * \param p The received packet.
66 * \param address The sender's address (unused).
67 */
68 void SinkRx(std::string path, Ptr<const Packet> p, const Address& address);
69
70 TestVectors<uint32_t> m_inputs; //!< Sent packets test vector.
71 TestVectors<uint32_t> m_responses; //!< Received packets test vector.
72};
73
75 : TestCase("Check that ns-3 TCP successfully transfers an application data write of various "
76 "sizes (point-to-point)"),
77 m_writeResults(false)
78{
79}
80
81void
82Ns3TcpSocketTestCaseP2P::SinkRx(std::string path, Ptr<const Packet> p, const Address& address)
83{
84 m_responses.Add(p->GetSize());
85}
86
87void
89{
90 uint16_t sinkPort = 50000;
91 double sinkStopTime = 40; // sec; will trigger Socket::Close
92 double writerStopTime = 30; // sec; will trigger Socket::Close
93 double simStopTime = 60; // sec
94 Time sinkStopTimeObj = Seconds(sinkStopTime);
95 Time writerStopTimeObj = Seconds(writerStopTime);
96 Time simStopTimeObj = Seconds(simStopTime);
97
98 Ptr<Node> n0 = CreateObject<Node>();
99 Ptr<Node> n1 = CreateObject<Node>();
100
101 PointToPointHelper pointToPoint;
102 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
103 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
104
105 NetDeviceContainer devices;
106 devices = pointToPoint.Install(n0, n1);
107
108 InternetStackHelper internet;
109 internet.InstallAll();
110
111 Ipv4AddressHelper address;
112 address.SetBase("10.1.1.0", "255.255.255.252");
113 Ipv4InterfaceContainer ifContainer = address.Assign(devices);
114
115 Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter>();
116 Address sinkAddress(InetSocketAddress(ifContainer.GetAddress(1), sinkPort));
117 socketWriter->Setup(n0, sinkAddress);
118 n0->AddApplication(socketWriter);
119 socketWriter->SetStartTime(Seconds(0.));
120 socketWriter->SetStopTime(writerStopTimeObj);
121
122 PacketSinkHelper sink("ns3::TcpSocketFactory",
124 ApplicationContainer apps = sink.Install(n1);
125 // Start the sink application at time zero, and stop it at sinkStopTime
126 apps.Start(Seconds(0.0));
127 apps.Stop(sinkStopTimeObj);
128
129 Config::Connect("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
131
133 // Send 1, 10, 100, 1000 bytes
134 Simulator::Schedule(Seconds(10), &SocketWriter::Write, socketWriter, 1);
135 m_inputs.Add(1);
136 Simulator::Schedule(Seconds(12), &SocketWriter::Write, socketWriter, 10);
137 m_inputs.Add(10);
138 Simulator::Schedule(Seconds(14), &SocketWriter::Write, socketWriter, 100);
139 m_inputs.Add(100);
140 Simulator::Schedule(Seconds(16), &SocketWriter::Write, socketWriter, 1000);
141 m_inputs.Add(536);
142 m_inputs.Add(464); // ns-3 TCP default segment size of 536
143 Simulator::Schedule(writerStopTimeObj, &SocketWriter::Close, socketWriter);
144
145 if (m_writeResults)
146 {
147 pointToPoint.EnablePcapAll("tcp-socket-test-case-1");
148 }
149
150 Simulator::Stop(simStopTimeObj);
153
154 // Compare inputs and outputs
157 "Incorrect number of expected receive events");
158 for (uint32_t i = 0; i < m_responses.GetN(); i++)
159 {
160 uint32_t in = m_inputs.Get(i);
161 uint32_t out = m_responses.Get(i);
163 out,
164 "Mismatch: expected " << in << " bytes, got " << out << " bytes");
165 }
166}
167
168/**
169 * \ingroup system-tests-tcp
170 *
171 * \brief Tests of TCP implementations from the application/socket perspective
172 * using CSMA links.
173 */
175{
176 public:
178
180 {
181 }
182
183 private:
184 void DoRun() override;
185 bool m_writeResults; //!< True if write PCAP files.
186
187 /**
188 * Receive a TCP packet.
189 * \param path The callback context (unused).
190 * \param p The received packet.
191 * \param address The sender's address (unused).
192 */
193 void SinkRx(std::string path, Ptr<const Packet> p, const Address& address);
194
195 TestVectors<uint32_t> m_inputs; //!< Sent packets test vector.
196 TestVectors<uint32_t> m_responses; //!< Received packets test vector.
197};
198
200 : TestCase("Check to see that ns-3 TCP successfully transfers an application data write of "
201 "various sizes (CSMA)"),
202 m_writeResults(false)
203{
204}
205
206void
208{
209 m_responses.Add(p->GetSize());
210}
211
212void
214{
215 uint16_t sinkPort = 50000;
216 double sinkStopTime = 40; // sec; will trigger Socket::Close
217 double writerStopTime = 30; // sec; will trigger Socket::Close
218 double simStopTime = 60; // sec
219 Time sinkStopTimeObj = Seconds(sinkStopTime);
220 Time writerStopTimeObj = Seconds(writerStopTime);
221 Time simStopTimeObj = Seconds(simStopTime);
222
223 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(1000));
224
226 nodes.Create(2);
227 Ptr<Node> n0 = nodes.Get(0);
228 Ptr<Node> n1 = nodes.Get(1);
229
230 CsmaHelper csma;
231 csma.SetChannelAttribute("DataRate", StringValue("5Mbps"));
232 csma.SetChannelAttribute("Delay", StringValue("2ms"));
233
234 NetDeviceContainer devices;
235 devices = csma.Install(nodes);
236
237 InternetStackHelper internet;
238 internet.InstallAll();
239
240 Ipv4AddressHelper address;
241 address.SetBase("10.1.1.0", "255.255.255.252");
242 Ipv4InterfaceContainer ifContainer = address.Assign(devices);
243
244 Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter>();
245 Address sinkAddress(InetSocketAddress(ifContainer.GetAddress(1), sinkPort));
246 socketWriter->Setup(n0, sinkAddress);
247 n0->AddApplication(socketWriter);
248 socketWriter->SetStartTime(Seconds(0.));
249 socketWriter->SetStopTime(writerStopTimeObj);
250
251 PacketSinkHelper sink("ns3::TcpSocketFactory",
253 ApplicationContainer apps = sink.Install(n1);
254 // Start the sink application at time zero, and stop it at sinkStopTime
255 apps.Start(Seconds(0.0));
256 apps.Stop(sinkStopTimeObj);
257
258 Config::Connect("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
260
262 // Send 1, 10, 100, 1000 bytes
263 // PointToPoint default MTU is 576 bytes, which leaves 536 bytes for TCP
264 Simulator::Schedule(Seconds(10), &SocketWriter::Write, socketWriter, 1);
265 m_inputs.Add(1);
266 Simulator::Schedule(Seconds(12), &SocketWriter::Write, socketWriter, 10);
267 m_inputs.Add(10);
268 Simulator::Schedule(Seconds(14), &SocketWriter::Write, socketWriter, 100);
269 m_inputs.Add(100);
270 Simulator::Schedule(Seconds(16), &SocketWriter::Write, socketWriter, 1000);
271 m_inputs.Add(1000);
272 // Next packet will fragment
273 Simulator::Schedule(Seconds(16), &SocketWriter::Write, socketWriter, 1001);
274 m_inputs.Add(1000);
275 m_inputs.Add(1);
276 Simulator::Schedule(writerStopTimeObj, &SocketWriter::Close, socketWriter);
277
278 if (m_writeResults)
279 {
280 csma.EnablePcapAll("tcp-socket-test-case-2", false);
281 }
282 Simulator::Stop(simStopTimeObj);
285
286 // Compare inputs and outputs
289 "Incorrect number of expected receive events");
290 for (uint32_t i = 0; i < m_responses.GetN(); i++)
291 {
292 uint32_t in = m_inputs.Get(i);
293 uint32_t out = m_responses.Get(i);
295 out,
296 "Mismatch: expected " << in << " bytes, got " << out << " bytes");
297 }
298}
299
300/**
301 * \ingroup system-tests-tcp
302 *
303 * TCP implementations from the application/socket perspective TestSuite.
304 */
306{
307 public:
309};
310
312 : TestSuite("ns3-tcp-socket", Type::SYSTEM)
313{
314 AddTestCase(new Ns3TcpSocketTestCaseP2P, TestCase::Duration::QUICK);
315 AddTestCase(new Ns3TcpSocketTestCaseCsma, TestCase::Duration::QUICK);
316}
317
318/// 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:101
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:48
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.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
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:56
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
A simple way to store test vectors (for stimulus or from responses)
Definition: test.h:1342
T Get(std::size_t i) const
Get the i'th test vector.
Definition: test.h:1424
std::size_t GetN() const
Get the total number of test vectors.
Definition: test.h:1417
std::size_t Add(T vector)
Definition: test.h:1408
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Hold an unsigned integer type.
Definition: uinteger.h:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:978
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:145
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
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:706
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:55