A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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"
37 #include "ns3tcp-socket-writer.h"
38 
39 using namespace ns3;
40 
41 NS_LOG_COMPONENT_DEFINE ("Ns3SocketTest");
42 
43 // ===========================================================================
44 // Tests of TCP implementations from the application/socket perspective
45 // ===========================================================================
46 //
47 //
49 {
50 public:
52  virtual ~Ns3TcpSocketTestCase1 () {}
53 
54 private:
55  virtual void DoRun (void);
57 
58  void SinkRx (std::string path, Ptr<const Packet> p, const Address &address);
59 
62 };
63 
65  : TestCase ("Check that ns-3 TCP successfully transfers an application data write of various sizes (point-to-point)"),
66  m_writeResults (false)
67 {
68 }
69 
70 void
72 {
73  m_responses.Add (p->GetSize ());
74 }
75 
76 void
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 
87  Ptr<Node> n0 = CreateObject<Node> ();
88  Ptr<Node> n1 = CreateObject<Node> ();
89 
91  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
92  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
93 
95  devices = pointToPoint.Install (n0, n1);
96 
97  InternetStackHelper internet;
98  internet.InstallAll ();
99 
101  address.SetBase ("10.1.1.0", "255.255.255.252");
102  Ipv4InterfaceContainer ifContainer = address.Assign (devices);
103 
104  Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter> ();
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",
112  InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
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 
121  Simulator::Schedule (Seconds (2), &SocketWriter::Connect, socketWriter);
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);
140  Simulator::Run ();
141  Simulator::Destroy ();
142 
143  // Compare inputs and outputs
144  NS_TEST_ASSERT_MSG_EQ (m_inputs.GetN (), m_responses.GetN (), "Incorrect number of expected receive events");
145  for (uint32_t i = 0; i < m_responses.GetN (); i++)
146  {
147  uint32_t in = m_inputs.Get (i);
148  uint32_t out = m_responses.Get (i);
149  NS_TEST_ASSERT_MSG_EQ (in, out, "Mismatch: expected " << in << " bytes, got " << out << " bytes");
150  }
151 }
152 
154 {
155 public:
158 
159 private:
160  virtual void DoRun (void);
162 
163  void SinkRx (std::string path, Ptr<const Packet> p, const Address &address);
164 
167 };
168 
170  : TestCase ("Check to see that ns-3 TCP successfully transfers an application data write of various sizes (CSMA)"),
171  m_writeResults (false)
172 {
173 }
174 
175 void
177 {
178  m_responses.Add (p->GetSize ());
179 }
180 
181 void
183 {
184  uint16_t sinkPort = 50000;
185  double sinkStopTime = 40; // sec; will trigger Socket::Close
186  double writerStopTime = 30; // sec; will trigger Socket::Close
187  double simStopTime = 60; // sec
188  Time sinkStopTimeObj = Seconds (sinkStopTime);
189  Time writerStopTimeObj = Seconds (writerStopTime);
190  Time simStopTimeObj= Seconds (simStopTime);
191 
192  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (1000));
193 
195  nodes.Create (2);
196  Ptr<Node> n0 = nodes.Get (0);
197  Ptr<Node> n1 = nodes.Get (1);
198 
199  CsmaHelper csma;
200  csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps"));
201  csma.SetChannelAttribute ("Delay", StringValue ("2ms"));
202 
204  devices = csma.Install (nodes);
205 
206  InternetStackHelper internet;
207  internet.InstallAll ();
208 
210  address.SetBase ("10.1.1.0", "255.255.255.252");
211  Ipv4InterfaceContainer ifContainer = address.Assign (devices);
212 
213  Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter> ();
214  Address sinkAddress (InetSocketAddress (ifContainer.GetAddress (1), sinkPort));
215  socketWriter->Setup (n0, sinkAddress);
216  n0->AddApplication (socketWriter);
217  socketWriter->SetStartTime (Seconds (0.));
218  socketWriter->SetStopTime (writerStopTimeObj);
219 
220  PacketSinkHelper sink ("ns3::TcpSocketFactory",
221  InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
222  ApplicationContainer apps = sink.Install (n1);
223  // Start the sink application at time zero, and stop it at sinkStopTime
224  apps.Start (Seconds (0.0));
225  apps.Stop (sinkStopTimeObj);
226 
227  Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
229 
230  Simulator::Schedule (Seconds (2), &SocketWriter::Connect, socketWriter);
231  // Send 1, 10, 100, 1000 bytes
232  // PointToPoint default MTU is 576 bytes, which leaves 536 bytes for TCP
233  Simulator::Schedule (Seconds (10), &SocketWriter::Write, socketWriter, 1);
234  m_inputs.Add (1);
235  Simulator::Schedule (Seconds (12), &SocketWriter::Write, socketWriter, 10);
236  m_inputs.Add (10);
237  Simulator::Schedule (Seconds (14), &SocketWriter::Write, socketWriter, 100);
238  m_inputs.Add (100);
239  Simulator::Schedule (Seconds (16), &SocketWriter::Write, socketWriter, 1000);
240  m_inputs.Add (1000);
241  // Next packet will fragment
242  Simulator::Schedule (Seconds (16), &SocketWriter::Write, socketWriter, 1001);
243  m_inputs.Add (1000);
244  m_inputs.Add (1);
245  Simulator::Schedule (writerStopTimeObj, &SocketWriter::Close, socketWriter);
246 
247  if (m_writeResults)
248  {
249  csma.EnablePcapAll ("tcp-socket-test-case-2", false);
250  }
251  Simulator::Stop (simStopTimeObj);
252  Simulator::Run ();
253  Simulator::Destroy ();
254 
255  // Compare inputs and outputs
256  NS_TEST_ASSERT_MSG_EQ (m_inputs.GetN (), m_responses.GetN (), "Incorrect number of expected receive events");
257  for (uint32_t i = 0; i < m_responses.GetN (); i++)
258  {
259  uint32_t in = m_inputs.Get (i);
260  uint32_t out = m_responses.Get (i);
261  NS_TEST_ASSERT_MSG_EQ (in, out, "Mismatch: expected " << in << " bytes, got " << out << " bytes");
262  }
263 }
264 
266 {
267 public:
269 };
270 
272  : TestSuite ("ns3-tcp-socket", SYSTEM)
273 {
274  AddTestCase (new Ns3TcpSocketTestCase1, TestCase::QUICK);
275  AddTestCase (new Ns3TcpSocketTestCase2, TestCase::QUICK);
276 }
277 
TestVectors< uint32_t > m_inputs
holds a vector of ns3::Application pointers.
uint32_t AddApplication(Ptr< Application > application)
Definition: node.cc:149
tuple pointToPoint
Definition: first.py:28
void SetStopTime(Time stop)
Specify application stop time.
Definition: application.cc:74
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
an Inet address class
TestVectors< uint32_t > m_responses
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
tuple devices
Definition: first.py:32
static Ns3TcpSocketTestSuite ns3TcpSocketTestSuite
holds a vector of std::pair of Ptr and interface index.
T Get(uint32_t i) const
Get the i'th test vector.
Definition: test.h:1255
hold variables of type string
Definition: string.h:18
A suite of tests to run.
Definition: test.h:1105
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
aggregate IP/TCP/UDP functionality to existing Nodes.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
Build a set of PointToPointNetDevice objects.
TestVectors< uint32_t > m_responses
encapsulates test code
Definition: test.h:929
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
uint32_t GetN(void) const
Definition: test.h:1248
virtual void DoRun(void)
Implementation to actually run this TestCase.
a polymophic address class
Definition: address.h:86
tuple nodes
Definition: first.py:25
void Setup(Ptr< Node > node, Address peer)
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
void InstallAll(void) const
Aggregate IPv4, IPv6, UDP, and TCP stacks to all nodes in the simulation.
Hold an unsigned integer type.
Definition: uinteger.h:46
#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:148
uint32_t Add(T vector)
Definition: test.h:1239
holds a vector of ns3::NetDevice pointers
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
keep track of a set of node pointers.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
virtual void DoRun(void)
Implementation to actually run this TestCase.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
tuple address
Definition: first.py:37
TestVectors< uint32_t > m_inputs
static void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
void SetStartTime(Time start)
Specify application start time.
Definition: application.cc:68
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const