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"
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 
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
void InstallAll(void) const
Aggregate IPv4, IPv6, UDP, and TCP stacks to all nodes in the simulation.
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:159
void SetStopTime(Time stop)
Specify application stop time.
Definition: application.cc:75
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
an Inet address class
TestVectors< uint32_t > m_responses
static Ns3TcpSocketTestSuite ns3TcpSocketTestSuite
holds a vector of std::pair of Ptr<Ipv4> and interface index.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
Hold variables of type string.
Definition: string.h:41
A suite of tests to run.
Definition: test.h:1343
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
aggregate IP/TCP/UDP functionality to existing Nodes.
T Get(std::size_t i) const
Get the i&#39;th test vector.
Definition: test.h:1483
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:1153
virtual void DoRun(void)
Implementation to actually run this TestCase.
a polymophic address class
Definition: address.h:90
std::size_t GetN(void) const
Get the total number of test vectors.
Definition: test.h:1476
void Setup(Ptr< Node > node, Address peer)
nodes
Definition: first.py:32
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Hold an unsigned integer type.
Definition: uinteger.h:44
#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:166
pointToPoint
Definition: first.py:35
holds a vector of ns3::NetDevice pointers
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
csma
Definition: second.py:63
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
address
Definition: first.py:44
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...
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
std::size_t Add(T vector)
Definition: test.h:1467
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
devices
Definition: first.py:39
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:69
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:1642