A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ns3tcp-socket-test-suite.cc
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);
56  bool m_writeResults;
57 
58  void SinkRx (std::string path, Ptr<const Packet> p, const Address &address);
59 
60  TestVectors<uint32_t> m_inputs;
61  TestVectors<uint32_t> m_responses;
62 };
63 
64 Ns3TcpSocketTestCase1::Ns3TcpSocketTestCase1 ()
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
71 Ns3TcpSocketTestCase1::SinkRx (std::string path, Ptr<const Packet> p, const Address &address)
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 
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 
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",
119  MakeCallback (&Ns3TcpSocketTestCase1::SinkRx, this));
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:
157  virtual ~Ns3TcpSocketTestCase2 () {}
158 
159 private:
160  virtual void DoRun (void);
161  bool m_writeResults;
162 
163  void SinkRx (std::string path, Ptr<const Packet> p, const Address &address);
164 
165  TestVectors<uint32_t> m_inputs;
166  TestVectors<uint32_t> m_responses;
167 };
168 
169 Ns3TcpSocketTestCase2::Ns3TcpSocketTestCase2 ()
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
176 Ns3TcpSocketTestCase2::SinkRx (std::string path, Ptr<const Packet> p, const Address &address)
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 
194  NodeContainer nodes;
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 
203  NetDeviceContainer devices;
204  devices = csma.Install (nodes);
205 
206  InternetStackHelper internet;
207  internet.InstallAll ();
208 
209  Ipv4AddressHelper address;
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",
228  MakeCallback (&Ns3TcpSocketTestCase2::SinkRx, this));
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 
271 Ns3TcpSocketTestSuite::Ns3TcpSocketTestSuite ()
272  : TestSuite ("ns3-tcp-socket", SYSTEM)
273 {
274  AddTestCase (new Ns3TcpSocketTestCase1, TestCase::QUICK);
275  AddTestCase (new Ns3TcpSocketTestCase2, TestCase::QUICK);
276 }
277 
278 static Ns3TcpSocketTestSuite ns3TcpSocketTestSuite;
holds a vector of ns3::Application pointers.
uint32_t AddApplication(Ptr< Application > application)
Definition: node.cc:148
void SetStopTime(Time stop)
Specify application stop time.
Definition: application.cc:74
keep track of time unit.
Definition: nstime.h:149
an Inet address class
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
holds a vector of std::pair of Ptr<Ipv4> and interface index.
hold variables of type string
Definition: string.h:19
A suite of tests to run.
Definition: test.h:962
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
aggregate IP/TCP/UDP functionality to existing Nodes.
uint32_t GetSize(void) const
Definition: packet.h:620
NetDeviceContainer Install(Ptr< Node > node) const
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.
encapsulates test code
Definition: test.h:834
void SetDeviceAttribute(std::string name, const AttributeValue &value)
virtual void DoRun(void)
Implementation to actually run this test case.
a polymophic address class
Definition: address.h:86
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 ...
Hold an unsigned integer type.
Definition: uinteger.h:46
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
keep track of a set of node pointers.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
virtual void DoRun(void)
Implementation to actually run this test case.
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...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
ApplicationContainer Install(NodeContainer c) const
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.
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