src/test/ns3tcp/ns3tcp-socket-test-suite.cc
changeset 5978 36c591bbdabd
child 6045 648d35d8fed6
equal deleted inserted replaced
5977:288416b082a4 5978:36c591bbdabd
       
     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 
       
    38 using namespace ns3;
       
    39 
       
    40 NS_LOG_COMPONENT_DEFINE ("Ns3SocketTest");
       
    41 
       
    42 // ===========================================================================
       
    43 // Tests of TCP implementations from the application/socket perspective
       
    44 // ===========================================================================
       
    45 //
       
    46 //
       
    47 //  Simple class to write data to sockets
       
    48 class SocketWriter : public Application
       
    49 {
       
    50 public:
       
    51   SocketWriter ();
       
    52   virtual ~SocketWriter ();
       
    53   void Setup (Ptr<Node> node, Address peer);
       
    54   void Connect ();
       
    55   void Write (uint32_t numBytes);
       
    56   void Close ();
       
    57 
       
    58 private:
       
    59   virtual void StartApplication (void);
       
    60   virtual void StopApplication (void);
       
    61   Address m_peer;
       
    62   Ptr<Node> m_node;
       
    63   Ptr<Socket> m_socket;
       
    64   bool m_isSetup;
       
    65   bool m_isConnected;
       
    66 };
       
    67 
       
    68 SocketWriter::SocketWriter () : m_node (0), m_socket (0), m_isSetup (false), m_isConnected (false)
       
    69 {
       
    70 }
       
    71 
       
    72 SocketWriter::~SocketWriter ()
       
    73 {
       
    74   m_socket = 0;
       
    75   m_node = 0;
       
    76 }
       
    77 
       
    78 void
       
    79 SocketWriter::StartApplication ()
       
    80 {
       
    81   m_socket = Socket::CreateSocket (m_node, TcpSocketFactory::GetTypeId ());
       
    82   m_socket->Bind ();
       
    83 }
       
    84 
       
    85 void
       
    86 SocketWriter::StopApplication ()
       
    87 {
       
    88 }
       
    89 
       
    90 void
       
    91 SocketWriter::Setup (Ptr<Node> node, Address peer)
       
    92 {
       
    93   m_peer = peer;
       
    94   m_node = node;
       
    95   m_isSetup = true;
       
    96 }
       
    97 
       
    98 void
       
    99 SocketWriter::Connect ()
       
   100 {
       
   101   if (!m_isSetup)
       
   102     {
       
   103       NS_FATAL_ERROR ("Forgot to call Setup() first");
       
   104     }
       
   105   m_socket->Connect (m_peer);
       
   106   m_isConnected = true;
       
   107 }
       
   108 
       
   109 void
       
   110 SocketWriter::Write (uint32_t numBytes)
       
   111 {
       
   112   if (!m_isConnected)
       
   113     {
       
   114       Connect ();
       
   115     }
       
   116   Ptr<Packet> packet = Create<Packet> (numBytes);
       
   117   m_socket->Send (packet);
       
   118 }
       
   119 
       
   120 void
       
   121 SocketWriter::Close ()
       
   122 {
       
   123   m_socket->Close ();
       
   124 }
       
   125 
       
   126 class Ns3TcpSocketTestCase1 : public TestCase
       
   127 {
       
   128 public:
       
   129   Ns3TcpSocketTestCase1 ();
       
   130   virtual ~Ns3TcpSocketTestCase1 () {}
       
   131 
       
   132 private:
       
   133   virtual bool DoRun (void);
       
   134   bool m_writeResults;
       
   135 
       
   136   void SinkRx (std::string path, Ptr<const Packet> p, const Address &address);
       
   137 
       
   138   TestVectors<uint32_t> m_inputs;
       
   139   TestVectors<uint32_t> m_responses;
       
   140 };
       
   141 
       
   142 Ns3TcpSocketTestCase1::Ns3TcpSocketTestCase1 ()
       
   143   : TestCase ("Check that ns-3 TCP successfully transfers an application data write of various sizes (point-to-point)"),
       
   144     m_writeResults (false)
       
   145 {
       
   146 }
       
   147 
       
   148 void 
       
   149 Ns3TcpSocketTestCase1::SinkRx (std::string path, Ptr<const Packet> p, const Address &address)
       
   150 {
       
   151   m_responses.Add (p->GetSize ());
       
   152 }
       
   153 
       
   154 bool
       
   155 Ns3TcpSocketTestCase1::DoRun (void)
       
   156 {
       
   157   uint16_t sinkPort = 50000;
       
   158   double sinkStopTime = 40;  // sec; will trigger Socket::Close
       
   159   double writerStopTime = 30;  // sec; will trigger Socket::Close
       
   160   double simStopTime = 60;  // sec
       
   161   Time sinkStopTimeObj = Seconds (sinkStopTime);
       
   162   Time writerStopTimeObj = Seconds (writerStopTime);
       
   163   Time simStopTimeObj= Seconds (simStopTime);
       
   164 
       
   165   Ptr<Node> n0 = CreateObject<Node> ();
       
   166   Ptr<Node> n1 = CreateObject<Node> ();
       
   167 
       
   168   PointToPointHelper pointToPoint;
       
   169   pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
       
   170   pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
       
   171 
       
   172   NetDeviceContainer devices;
       
   173   devices = pointToPoint.Install (n0, n1);
       
   174 
       
   175   InternetStackHelper internet;
       
   176   internet.InstallAll ();
       
   177 
       
   178   Ipv4AddressHelper address;
       
   179   address.SetBase ("10.1.1.0", "255.255.255.252");
       
   180   Ipv4InterfaceContainer ifContainer = address.Assign (devices);
       
   181 
       
   182   Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter> ();
       
   183   Address sinkAddress (InetSocketAddress (ifContainer.GetAddress (1), sinkPort));
       
   184   socketWriter->Setup (n0, sinkAddress);
       
   185   n0->AddApplication (socketWriter);
       
   186   socketWriter->SetStartTime (Seconds (0.));
       
   187   socketWriter->SetStopTime (writerStopTimeObj);
       
   188 
       
   189   PacketSinkHelper sink ("ns3::TcpSocketFactory",
       
   190                          InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
       
   191   ApplicationContainer apps = sink.Install (n1);
       
   192   // Start the sink application at time zero, and stop it at sinkStopTime
       
   193   apps.Start (Seconds (0.0));
       
   194   apps.Stop (sinkStopTimeObj);
       
   195 
       
   196   Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
       
   197                    MakeCallback (&Ns3TcpSocketTestCase1::SinkRx, this));
       
   198 
       
   199   Simulator::Schedule(Seconds (2), &SocketWriter::Connect, socketWriter);
       
   200   // Send 1, 10, 100, 1000 bytes
       
   201   Simulator::Schedule(Seconds (10), &SocketWriter::Write, socketWriter, 1);
       
   202   m_inputs.Add (1);
       
   203   Simulator::Schedule(Seconds (12), &SocketWriter::Write, socketWriter, 10);
       
   204   m_inputs.Add (10);
       
   205   Simulator::Schedule(Seconds (14), &SocketWriter::Write, socketWriter, 100);
       
   206   m_inputs.Add (100);
       
   207   Simulator::Schedule(Seconds (16), &SocketWriter::Write, socketWriter, 1000);
       
   208   m_inputs.Add (536);
       
   209   m_inputs.Add (464);  // ns-3 TCP default segment size of 536
       
   210   Simulator::Schedule(writerStopTimeObj, &SocketWriter::Close, socketWriter);
       
   211 
       
   212   if (m_writeResults)
       
   213     {
       
   214       PointToPointHelper::EnablePcapAll ("tcp-socket-test-case-1");
       
   215     }
       
   216 
       
   217   Simulator::Stop (simStopTimeObj);
       
   218   Simulator::Run ();
       
   219   Simulator::Destroy ();
       
   220 
       
   221   // Compare inputs and outputs
       
   222   NS_TEST_ASSERT_MSG_EQ (m_inputs.GetN (), m_responses.GetN (), "Incorrect number of expected receive events");
       
   223   for (uint32_t i = 0; i < m_responses.GetN (); i++)
       
   224     {
       
   225       uint32_t in = m_inputs.Get (i);
       
   226       uint32_t out = m_responses.Get (i);
       
   227       NS_TEST_ASSERT_MSG_EQ (in, out, "Mismatch:  expected " << in << " bytes, got " << out << " bytes");
       
   228     }
       
   229 
       
   230   return GetErrorStatus ();
       
   231 }
       
   232 
       
   233 class Ns3TcpSocketTestCase2 : public TestCase
       
   234 {
       
   235 public:
       
   236   Ns3TcpSocketTestCase2 ();
       
   237   virtual ~Ns3TcpSocketTestCase2 () {}
       
   238 
       
   239 private:
       
   240   virtual bool DoRun (void);
       
   241   bool m_writeResults;
       
   242 
       
   243   void SinkRx (std::string path, Ptr<const Packet> p, const Address &address);
       
   244 
       
   245   TestVectors<uint32_t> m_inputs;
       
   246   TestVectors<uint32_t> m_responses;
       
   247 };
       
   248 
       
   249 Ns3TcpSocketTestCase2::Ns3TcpSocketTestCase2 ()
       
   250   : TestCase ("Check to see that ns-3 TCP successfully transfers an application data write of various sizes (CSMA)"),
       
   251     m_writeResults (false)
       
   252 {
       
   253 }
       
   254 
       
   255 void 
       
   256 Ns3TcpSocketTestCase2::SinkRx (std::string path, Ptr<const Packet> p, const Address &address)
       
   257 {
       
   258   m_responses.Add (p->GetSize ());
       
   259 }
       
   260 
       
   261 bool
       
   262 Ns3TcpSocketTestCase2::DoRun (void)
       
   263 {
       
   264   uint16_t sinkPort = 50000;
       
   265   double sinkStopTime = 40;  // sec; will trigger Socket::Close
       
   266   double writerStopTime = 30;  // sec; will trigger Socket::Close
       
   267   double simStopTime = 60;  // sec
       
   268   Time sinkStopTimeObj = Seconds (sinkStopTime);
       
   269   Time writerStopTimeObj = Seconds (writerStopTime);
       
   270   Time simStopTimeObj= Seconds (simStopTime);
       
   271 
       
   272   Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (1000));
       
   273 
       
   274   NodeContainer nodes;
       
   275   nodes.Create (2);
       
   276   Ptr<Node> n0 = nodes.Get (0);
       
   277   Ptr<Node> n1 = nodes.Get (1);
       
   278 
       
   279   CsmaHelper csma;
       
   280   csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps"));
       
   281   csma.SetChannelAttribute ("Delay", StringValue ("2ms"));
       
   282 
       
   283   NetDeviceContainer devices;
       
   284   devices = csma.Install (nodes);
       
   285 
       
   286   InternetStackHelper internet;
       
   287   internet.InstallAll ();
       
   288 
       
   289   Ipv4AddressHelper address;
       
   290   address.SetBase ("10.1.1.0", "255.255.255.252");
       
   291   Ipv4InterfaceContainer ifContainer = address.Assign (devices);
       
   292 
       
   293   Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter> ();
       
   294   Address sinkAddress (InetSocketAddress (ifContainer.GetAddress (1), sinkPort));
       
   295   socketWriter->Setup (n0, sinkAddress);
       
   296   n0->AddApplication (socketWriter);
       
   297   socketWriter->SetStartTime (Seconds (0.));
       
   298   socketWriter->SetStopTime (writerStopTimeObj);
       
   299 
       
   300   PacketSinkHelper sink ("ns3::TcpSocketFactory",
       
   301                          InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
       
   302   ApplicationContainer apps = sink.Install (n1);
       
   303   // Start the sink application at time zero, and stop it at sinkStopTime
       
   304   apps.Start (Seconds (0.0));
       
   305   apps.Stop (sinkStopTimeObj);
       
   306 
       
   307   Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
       
   308                    MakeCallback (&Ns3TcpSocketTestCase2::SinkRx, this));
       
   309 
       
   310   Simulator::Schedule(Seconds (2), &SocketWriter::Connect, socketWriter);
       
   311   // Send 1, 10, 100, 1000 bytes
       
   312   // PointToPoint default MTU is 576 bytes, which leaves 536 bytes for TCP
       
   313   Simulator::Schedule(Seconds (10), &SocketWriter::Write, socketWriter, 1);
       
   314   m_inputs.Add (1);
       
   315   Simulator::Schedule(Seconds (12), &SocketWriter::Write, socketWriter, 10);
       
   316   m_inputs.Add (10);
       
   317   Simulator::Schedule(Seconds (14), &SocketWriter::Write, socketWriter, 100);
       
   318   m_inputs.Add (100);
       
   319   Simulator::Schedule(Seconds (16), &SocketWriter::Write, socketWriter, 1000);
       
   320   m_inputs.Add (1000);
       
   321   // Next packet will fragment
       
   322   Simulator::Schedule(Seconds (16), &SocketWriter::Write, socketWriter, 1001);
       
   323   m_inputs.Add (1000);
       
   324   m_inputs.Add (1);
       
   325   Simulator::Schedule(writerStopTimeObj, &SocketWriter::Close, socketWriter);
       
   326 
       
   327   if (m_writeResults)
       
   328     {
       
   329       CsmaHelper::EnablePcapAll ("tcp-socket-test-case-2", false);
       
   330     }
       
   331   Simulator::Stop (simStopTimeObj);
       
   332   Simulator::Run ();
       
   333   Simulator::Destroy ();
       
   334 
       
   335   // Compare inputs and outputs
       
   336   NS_TEST_ASSERT_MSG_EQ (m_inputs.GetN (), m_responses.GetN (), "Incorrect number of expected receive events");
       
   337   for (uint32_t i = 0; i < m_responses.GetN (); i++)
       
   338     {
       
   339       uint32_t in = m_inputs.Get (i);
       
   340       uint32_t out = m_responses.Get (i);
       
   341       NS_TEST_ASSERT_MSG_EQ (in, out, "Mismatch:  expected " << in << " bytes, got " << out << " bytes");
       
   342     }
       
   343 
       
   344   return GetErrorStatus ();
       
   345 }
       
   346 
       
   347 class Ns3TcpSocketTestSuite : public TestSuite
       
   348 {
       
   349 public:
       
   350   Ns3TcpSocketTestSuite ();
       
   351 };
       
   352 
       
   353 Ns3TcpSocketTestSuite::Ns3TcpSocketTestSuite ()
       
   354   : TestSuite ("ns3-tcp-socket", SYSTEM)
       
   355 {
       
   356   AddTestCase (new Ns3TcpSocketTestCase1);
       
   357   AddTestCase (new Ns3TcpSocketTestCase2);
       
   358 }
       
   359 
       
   360 Ns3TcpSocketTestSuite ns3TcpSocketTestSuite;