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
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",
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
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",
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 {
276 }
277