A Discrete-Event Network Simulator
API
ns3tcp-socket-test-suite.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2010 University of Washington
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19#include "ns3/log.h"
20#include "ns3/abort.h"
21#include "ns3/test.h"
22#include "ns3/pcap-file.h"
23#include "ns3/config.h"
24#include "ns3/string.h"
25#include "ns3/uinteger.h"
26#include "ns3/data-rate.h"
27#include "ns3/inet-socket-address.h"
28#include "ns3/point-to-point-helper.h"
29#include "ns3/csma-helper.h"
30#include "ns3/internet-stack-helper.h"
31#include "ns3/ipv4-global-routing-helper.h"
32#include "ns3/ipv4-address-helper.h"
33#include "ns3/packet-sink-helper.h"
34#include "ns3/tcp-socket-factory.h"
35#include "ns3/node-container.h"
36#include "ns3/simulator.h"
38
39using namespace ns3;
40
41NS_LOG_COMPONENT_DEFINE ("Ns3SocketTest");
42
50{
51public:
54
55private:
56 virtual void DoRun (void);
58
65 void SinkRx (std::string path, Ptr<const Packet> p, const Address &address);
66
69};
70
72 : TestCase ("Check that ns-3 TCP successfully transfers an application data write of various sizes (point-to-point)"),
73 m_writeResults (false)
74{
75}
76
77void
79{
80 m_responses.Add (p->GetSize ());
81}
82
83void
85{
86 uint16_t sinkPort = 50000;
87 double sinkStopTime = 40; // sec; will trigger Socket::Close
88 double writerStopTime = 30; // sec; will trigger Socket::Close
89 double simStopTime = 60; // sec
90 Time sinkStopTimeObj = Seconds (sinkStopTime);
91 Time writerStopTimeObj = Seconds (writerStopTime);
92 Time simStopTimeObj= Seconds (simStopTime);
93
94 Ptr<Node> n0 = CreateObject<Node> ();
95 Ptr<Node> n1 = CreateObject<Node> ();
96
98 pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
99 pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
100
102 devices = pointToPoint.Install (n0, n1);
103
104 InternetStackHelper internet;
105 internet.InstallAll ();
106
108 address.SetBase ("10.1.1.0", "255.255.255.252");
109 Ipv4InterfaceContainer ifContainer = address.Assign (devices);
110
111 Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter> ();
112 Address sinkAddress (InetSocketAddress (ifContainer.GetAddress (1), sinkPort));
113 socketWriter->Setup (n0, sinkAddress);
114 n0->AddApplication (socketWriter);
115 socketWriter->SetStartTime (Seconds (0.));
116 socketWriter->SetStopTime (writerStopTimeObj);
117
118 PacketSinkHelper sink ("ns3::TcpSocketFactory",
119 InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
120 ApplicationContainer apps = sink.Install (n1);
121 // Start the sink application at time zero, and stop it at sinkStopTime
122 apps.Start (Seconds (0.0));
123 apps.Stop (sinkStopTimeObj);
124
125 Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
127
128 Simulator::Schedule (Seconds (2), &SocketWriter::Connect, socketWriter);
129 // Send 1, 10, 100, 1000 bytes
130 Simulator::Schedule (Seconds (10), &SocketWriter::Write, socketWriter, 1);
131 m_inputs.Add (1);
132 Simulator::Schedule (Seconds (12), &SocketWriter::Write, socketWriter, 10);
133 m_inputs.Add (10);
134 Simulator::Schedule (Seconds (14), &SocketWriter::Write, socketWriter, 100);
135 m_inputs.Add (100);
136 Simulator::Schedule (Seconds (16), &SocketWriter::Write, socketWriter, 1000);
137 m_inputs.Add (536);
138 m_inputs.Add (464); // ns-3 TCP default segment size of 536
139 Simulator::Schedule (writerStopTimeObj, &SocketWriter::Close, socketWriter);
140
141 if (m_writeResults)
142 {
143 pointToPoint.EnablePcapAll ("tcp-socket-test-case-1");
144 }
145
146 Simulator::Stop (simStopTimeObj);
147 Simulator::Run ();
148 Simulator::Destroy ();
149
150 // Compare inputs and outputs
151 NS_TEST_ASSERT_MSG_EQ (m_inputs.GetN (), m_responses.GetN (), "Incorrect number of expected receive events");
152 for (uint32_t i = 0; i < m_responses.GetN (); i++)
153 {
154 uint32_t in = m_inputs.Get (i);
155 uint32_t out = m_responses.Get (i);
156 NS_TEST_ASSERT_MSG_EQ (in, out, "Mismatch: expected " << in << " bytes, got " << out << " bytes");
157 }
158}
159
167{
168public:
171
172private:
173 virtual void DoRun (void);
175
182 void SinkRx (std::string path, Ptr<const Packet> p, const Address &address);
183
186};
187
189 : TestCase ("Check to see that ns-3 TCP successfully transfers an application data write of various sizes (CSMA)"),
190 m_writeResults (false)
191{
192}
193
194void
196{
197 m_responses.Add (p->GetSize ());
198}
199
200void
202{
203 uint16_t sinkPort = 50000;
204 double sinkStopTime = 40; // sec; will trigger Socket::Close
205 double writerStopTime = 30; // sec; will trigger Socket::Close
206 double simStopTime = 60; // sec
207 Time sinkStopTimeObj = Seconds (sinkStopTime);
208 Time writerStopTimeObj = Seconds (writerStopTime);
209 Time simStopTimeObj= Seconds (simStopTime);
210
211 Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (1000));
212
214 nodes.Create (2);
215 Ptr<Node> n0 = nodes.Get (0);
216 Ptr<Node> n1 = nodes.Get (1);
217
219 csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps"));
220 csma.SetChannelAttribute ("Delay", StringValue ("2ms"));
221
223 devices = csma.Install (nodes);
224
225 InternetStackHelper internet;
226 internet.InstallAll ();
227
229 address.SetBase ("10.1.1.0", "255.255.255.252");
230 Ipv4InterfaceContainer ifContainer = address.Assign (devices);
231
232 Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter> ();
233 Address sinkAddress (InetSocketAddress (ifContainer.GetAddress (1), sinkPort));
234 socketWriter->Setup (n0, sinkAddress);
235 n0->AddApplication (socketWriter);
236 socketWriter->SetStartTime (Seconds (0.));
237 socketWriter->SetStopTime (writerStopTimeObj);
238
239 PacketSinkHelper sink ("ns3::TcpSocketFactory",
240 InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
241 ApplicationContainer apps = sink.Install (n1);
242 // Start the sink application at time zero, and stop it at sinkStopTime
243 apps.Start (Seconds (0.0));
244 apps.Stop (sinkStopTimeObj);
245
246 Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
248
249 Simulator::Schedule (Seconds (2), &SocketWriter::Connect, socketWriter);
250 // Send 1, 10, 100, 1000 bytes
251 // PointToPoint default MTU is 576 bytes, which leaves 536 bytes for TCP
252 Simulator::Schedule (Seconds (10), &SocketWriter::Write, socketWriter, 1);
253 m_inputs.Add (1);
254 Simulator::Schedule (Seconds (12), &SocketWriter::Write, socketWriter, 10);
255 m_inputs.Add (10);
256 Simulator::Schedule (Seconds (14), &SocketWriter::Write, socketWriter, 100);
257 m_inputs.Add (100);
258 Simulator::Schedule (Seconds (16), &SocketWriter::Write, socketWriter, 1000);
259 m_inputs.Add (1000);
260 // Next packet will fragment
261 Simulator::Schedule (Seconds (16), &SocketWriter::Write, socketWriter, 1001);
262 m_inputs.Add (1000);
263 m_inputs.Add (1);
264 Simulator::Schedule (writerStopTimeObj, &SocketWriter::Close, socketWriter);
265
266 if (m_writeResults)
267 {
268 csma.EnablePcapAll ("tcp-socket-test-case-2", false);
269 }
270 Simulator::Stop (simStopTimeObj);
271 Simulator::Run ();
272 Simulator::Destroy ();
273
274 // Compare inputs and outputs
275 NS_TEST_ASSERT_MSG_EQ (m_inputs.GetN (), m_responses.GetN (), "Incorrect number of expected receive events");
276 for (uint32_t i = 0; i < m_responses.GetN (); i++)
277 {
278 uint32_t in = m_inputs.Get (i);
279 uint32_t out = m_responses.Get (i);
280 NS_TEST_ASSERT_MSG_EQ (in, out, "Mismatch: expected " << in << " bytes, got " << out << " bytes");
281 }
282}
283
290{
291public:
293};
294
296 : TestSuite ("ns3-tcp-socket", SYSTEM)
297{
298 AddTestCase (new Ns3TcpSocketTestCaseP2P, TestCase::QUICK);
299 AddTestCase (new Ns3TcpSocketTestCaseCsma, TestCase::QUICK);
300}
301
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.
virtual void DoRun(void)
Implementation to actually run this TestCase.
bool m_writeResults
True if write PCAP files.
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
Receive a TCP packet.
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.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
Receive a TCP packet.
bool m_writeResults
True if write PCAP files.
TCP implementations from the application/socket perspective TestSuite.
a polymophic address class
Definition: address.h:91
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
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:47
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void InstallAll(void) const
Aggregate IPv4, IPv6, UDP, and TCP stacks to all nodes in the simulation.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
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.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:159
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Hold variables of type string.
Definition: string.h:41
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
T Get(std::size_t i) const
Get the i'th test vector.
Definition: test.h:1331
std::size_t GetN(void) const
Get the total number of test vectors.
Definition: test.h:1324
std::size_t Add(T vector)
Definition: test.h:1315
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Hold an unsigned integer type.
Definition: uinteger.h:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:141
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
address
Definition: first.py:44
pointToPoint
Definition: first.py:35
devices
Definition: first.py:39
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
csma
Definition: second.py:63
static Ns3TcpSocketTestSuite g_ns3TcpSocketTestSuite
Do not forget to allocate an instance of this TestSuite.
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56