add bundle protocol test suite default tip
authorDizhi Zhou <dizhi.zhou@gmail.com>
Wed, 08 Jan 2014 11:20:19 -0400
changeset 10445 c71c921a005d
parent 10444 63139fa1a708
add bundle protocol test suite
src/bundle-protocol/examples/bundle-protocol-simple.cc
src/bundle-protocol/test/bundle-protocol-test-suite.cc
--- a/src/bundle-protocol/examples/bundle-protocol-simple.cc	Tue Jan 07 11:46:33 2014 -0400
+++ b/src/bundle-protocol/examples/bundle-protocol-simple.cc	Wed Jan 08 11:20:19 2014 -0400
@@ -29,10 +29,7 @@
 #include "ns3/core-module.h"
 #include "ns3/point-to-point-module.h"
 #include "ns3/internet-module.h"
-#include "ns3/applications-module.h"
 #include "ns3/network-module.h"
-#include "ns3/packet-sink.h"
-
 #include "ns3/bp-endpoint-id.h"
 #include "ns3/bundle-protocol.h"
 #include "ns3/bp-static-routing-protocol.h"
@@ -96,7 +93,7 @@
   std::ostringstream l4type;
   l4type << "Tcp";
   Config::SetDefault ("ns3::BundleProtocol::L4Type", StringValue (l4type.str ()));
-  Config::SetDefault ("ns3::BundleProtocol::BundleSize", UintegerValue (300)); 
+  Config::SetDefault ("ns3::BundleProtocol::BundleSize", UintegerValue (400)); 
   Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (512));
 
   // build endpoint ids
@@ -113,8 +110,8 @@
   bpSenderHelper.SetRoutingProtocol (route);
   bpSenderHelper.SetBpEndpointId (eidSender);
   BundleProtocolContainer bpSenders = bpSenderHelper.Install (nodes.Get (0));
-  bpSenders.Start (Seconds (1.0));
-  bpSenders.Stop (Seconds (5.0));
+  bpSenders.Start (Seconds (0.1));
+  bpSenders.Stop (Seconds (1.0));
 
   // receiver
   BundleProtocolHelper bpReceiverHelper;
@@ -122,14 +119,14 @@
   bpReceiverHelper.SetBpEndpointId (eidRecv);
   BundleProtocolContainer bpReceivers = bpReceiverHelper.Install (nodes.Get (1));
   bpReceivers.Start (Seconds (0.0));
-  bpReceivers.Stop (Seconds (5.0));
+  bpReceivers.Stop (Seconds (1.0));
 
   // send 1000 bytes bundle 
   uint32_t size = 1000;
-  Simulator::Schedule (Seconds (2.0), &Send, bpSenders.Get (0), size, eidSender, eidRecv);
+  Simulator::Schedule (Seconds (0.2), &Send, bpSenders.Get (0), size, eidSender, eidRecv);
 
   // receive function
-  Simulator::Schedule (Seconds (4.0), &Receive, bpReceivers.Get (0), eidRecv);
+  Simulator::Schedule (Seconds (0.8), &Receive, bpReceivers.Get (0), eidRecv);
 
   if (tracing)
     {
@@ -139,7 +136,7 @@
     }
 
   NS_LOG_INFO ("Run Simulation.");
-  Simulator::Stop (Seconds (5.0));
+  Simulator::Stop (Seconds (1.0));
   Simulator::Run ();
   Simulator::Destroy ();
   NS_LOG_INFO ("Done.");
--- a/src/bundle-protocol/test/bundle-protocol-test-suite.cc	Tue Jan 07 11:46:33 2014 -0400
+++ b/src/bundle-protocol/test/bundle-protocol-test-suite.cc	Wed Jan 08 11:20:19 2014 -0400
@@ -1,68 +1,181 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ *  Copyright (c) 2013 University of New Brunswick
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Dizhi Zhou <dizhi.zhou@gmail.com>
+ */
 
-// Include a header file from your module to test.
+#include <string>
+#include <fstream>
+#include <tgmath.h>
+#include "ns3/bp-endpoint-id.h"
 #include "ns3/bundle-protocol.h"
-
-// An essential include is test.h
+#include "ns3/core-module.h"
+#include "ns3/point-to-point-module.h"
+#include "ns3/internet-module.h"
+#include "ns3/network-module.h"
+#include "ns3/bp-endpoint-id.h"
+#include "ns3/bundle-protocol.h"
+#include "ns3/bp-static-routing-protocol.h"
+#include "ns3/bundle-protocol-helper.h"
+#include "ns3/bundle-protocol-container.h"
 #include "ns3/test.h"
 
-// Do not put your test classes in namespace ns3.  You may find it useful
-// to use the using directive to access the ns3 namespace directly
+NS_LOG_COMPONENT_DEFINE ("BundleProtocolTestSuite");
+
 using namespace ns3;
 
-// This is an example TestCase.
-class BundleProtocolTestCase1 : public TestCase
+class BundleProtocolTestCase : public TestCase
 {
 public:
-  BundleProtocolTestCase1 ();
-  virtual ~BundleProtocolTestCase1 ();
+  BundleProtocolTestCase (uint32_t sentBundleSize, uint32_t bundleSize, uint32_t segmentSize, std::string claType);
+  virtual ~BundleProtocolTestCase ();
 
 private:
   virtual void DoRun (void);
+  void Send (Ptr<BundleProtocol> sender, uint32_t size, BpEndpointId src, BpEndpointId dst);
+  void Receive (Ptr<BundleProtocol> receiver, BpEndpointId eid);
+
+private:
+  uint32_t m_sentBundleSize;   
+  uint32_t m_receivedBundleSize;
+  uint32_t m_receivedBundleNumber;
+  uint32_t m_bundleSize;
+  uint32_t m_tcpSegmentSize;
+  std::string m_claType;
 };
 
-// Add some help text to this case to describe what it is intended to test
-BundleProtocolTestCase1::BundleProtocolTestCase1 ()
-  : TestCase ("BundleProtocol test case (does nothing)")
+static class BundleProtocolTestSuite : public TestSuite
+{
+public:
+  BundleProtocolTestSuite ()
+  : TestSuite ("bundle-protocol", UNIT)
+    {
+      NS_LOG_INFO ("creating BundleProtocolTestSuite");
+
+      AddTestCase (new BundleProtocolTestCase (1000, 400, 512, "Tcp"), TestCase::QUICK);
+      AddTestCase (new BundleProtocolTestCase (1000, 512, 512, "Tcp"), TestCase::QUICK);
+      AddTestCase (new BundleProtocolTestCase (1000, 1000, 512, "Tcp"), TestCase::QUICK);
+    }
+
+} g_bundleProtocolTestSuite;
+
+BundleProtocolTestCase::BundleProtocolTestCase (uint32_t sentBundleSize, uint32_t bundleSize, uint32_t segmentSize, 
+    std::string claType)
+  : TestCase ("Test that all the bundles generated by a sender bundle node are correctly received by a receiver bundle node"),
+    m_sentBundleSize (sentBundleSize),
+    m_receivedBundleSize (0),
+    m_receivedBundleNumber (0),
+    m_bundleSize (bundleSize),
+    m_tcpSegmentSize (segmentSize),
+    m_claType (claType)
+{
+}
+
+BundleProtocolTestCase::~BundleProtocolTestCase ()
 {
 }
 
-// This destructor does nothing but we include it as a reminder that
-// the test case should clean up after itself
-BundleProtocolTestCase1::~BundleProtocolTestCase1 ()
+void
+BundleProtocolTestCase::DoRun (void)
 {
-}
+  ns3::PacketMetadata::Enable ();
+
+  NodeContainer nodes;
+  nodes.Create (2);
+
+  PointToPointHelper pointToPoint;
+  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("500Kbps"));
+  pointToPoint.SetChannelAttribute ("Delay", StringValue ("5ms"));
+
+  NetDeviceContainer devices;
+  devices = pointToPoint.Install (nodes);
+
+  InternetStackHelper internet;
+  internet.Install (nodes);
+
+  Ipv4AddressHelper ipv4;
+  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
+  Ipv4InterfaceContainer i = ipv4.Assign (devices);
+
+  std::ostringstream l4type;
+  l4type << m_claType;
+  Config::SetDefault ("ns3::BundleProtocol::L4Type", StringValue (l4type.str ()));
+  Config::SetDefault ("ns3::BundleProtocol::BundleSize", UintegerValue (m_bundleSize)); 
+  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (m_tcpSegmentSize));
+
+  // build endpoint ids
+  BpEndpointId eidSender ("dtn", "node0");
+  BpEndpointId eidRecv ("dtn", "node1");
 
-//
-// This method is the pure virtual method from class TestCase that every
-// TestCase must implement
-//
-void
-BundleProtocolTestCase1::DoRun (void)
-{
-  // A wide variety of test macros are available in src/core/test.h
-  NS_TEST_ASSERT_MSG_EQ (true, true, "true doesn't equal true for some reason");
-  // Use this one for floating point comparisons
-  NS_TEST_ASSERT_MSG_EQ_TOL (0.01, 0.01, 0.001, "Numbers are not equal within tolerance");
+  // set bundle static routing
+  Ptr<BpStaticRoutingProtocol> route = CreateObject<BpStaticRoutingProtocol> ();
+  route->AddRoute (eidSender, InetSocketAddress (i.GetAddress (0), 9));
+  route->AddRoute (eidRecv, InetSocketAddress (i.GetAddress (1), 9));
+
+  // sender  
+  BundleProtocolHelper bpSenderHelper;
+  bpSenderHelper.SetRoutingProtocol (route);
+  bpSenderHelper.SetBpEndpointId (eidSender);
+  BundleProtocolContainer bpSenders = bpSenderHelper.Install (nodes.Get (0));
+  bpSenders.Start (Seconds (0.1));
+  bpSenders.Stop (Seconds (1.0));
+
+  // receiver
+  BundleProtocolHelper bpReceiverHelper;
+  bpReceiverHelper.SetRoutingProtocol (route);
+  bpReceiverHelper.SetBpEndpointId (eidRecv);
+  BundleProtocolContainer bpReceivers = bpReceiverHelper.Install (nodes.Get (1));
+  bpReceivers.Start (Seconds (0.0));
+  bpReceivers.Stop (Seconds (1.0));
+
+  Simulator::Schedule (Seconds (0.2), &BundleProtocolTestCase::Send, this, bpSenders.Get (0), 
+                       m_sentBundleSize, eidSender, eidRecv);
+  Simulator::Schedule (Seconds (0.8), &BundleProtocolTestCase::Receive, this, bpReceivers.Get (0), 
+                       eidRecv);
+
+  Simulator::Stop (Seconds (1.0));
+  Simulator::Run ();
+  Simulator::Destroy ();
+
+  double received = m_receivedBundleSize;
+  double bundleSize = m_bundleSize;
+  uint32_t num = ceil (received/bundleSize);
+  NS_TEST_EXPECT_MSG_EQ (m_receivedBundleSize, m_sentBundleSize, "All bundles are received at the receiver");
+  NS_TEST_EXPECT_MSG_EQ (m_receivedBundleNumber, num , "Correct number of bundles are received at the receiver");
+
 }
 
-// The TestSuite class names the TestSuite, identifies what type of TestSuite,
-// and enables the TestCases to be run.  Typically, only the constructor for
-// this class must be defined
-//
-class BundleProtocolTestSuite : public TestSuite
+
+void 
+BundleProtocolTestCase::Send (Ptr<BundleProtocol> sender, uint32_t size, BpEndpointId src, BpEndpointId dst)
 {
-public:
-  BundleProtocolTestSuite ();
-};
-
-BundleProtocolTestSuite::BundleProtocolTestSuite ()
-  : TestSuite ("bundle-protocol", UNIT)
-{
-  // TestDuration for TestCase can be QUICK, EXTENSIVE or TAKES_FOREVER
-  AddTestCase (new BundleProtocolTestCase1, TestCase::QUICK);
+  Ptr<Packet> packet = Create<Packet> (size);
+  sender->Send (packet, src, dst);
 }
 
-// Do not forget to allocate an instance of this TestSuite
-static BundleProtocolTestSuite bundleProtocolTestSuite;
+void 
+BundleProtocolTestCase::Receive (Ptr<BundleProtocol> receiver, BpEndpointId eid)
+{
+  Ptr<Packet> p = receiver->Receive (eid);
+  while (p != NULL)
+    {
+      m_receivedBundleSize += p->GetSize ();
+      m_receivedBundleNumber++;
+      p = receiver->Receive (eid);
+    }
+}