diff --git a/src/internet/test/ipv4-global-routing-test-suite.cc b/src/internet/test/ipv4-global-routing-test-suite.cc index 676f537ba..8501922b3 100644 --- a/src/internet/test/ipv4-global-routing-test-suite.cc +++ b/src/internet/test/ipv4-global-routing-test-suite.cc @@ -23,6 +23,7 @@ #include "ns3/ipv4-global-routing-helper.h" #include "ns3/ipv4-static-routing-helper.h" #include "ns3/node.h" +#include "ns3/log.h" #include "ns3/node-container.h" #include "ns3/packet.h" #include "ns3/simple-net-device-helper.h" @@ -36,9 +37,420 @@ #include "ns3/simple-channel.h" #include "ns3/socket-factory.h" #include "ns3/udp-socket-factory.h" +#include "ns3/ipv4-l3-protocol.h" +#include "ns3/ipv4-routing-protocol.h" +#include "ns3/ipv4-routing-table-entry.h" +#include "ns3/ipv4-global-routing.h" using namespace ns3; +NS_LOG_COMPONENT_DEFINE ("Ipv4GlobalRoutingTestSuite"); + +// This test suite tests the operation of global routing on a few sample +// networks to ensure that routes are built correctly +// +// Link test: +// n0 <--------> n1 (point-to-point link) +// 10.1.1.1 10.1.1.2 +// Expected routes: +// n0: route to 0.0.0.0 gw 10.1.1.2 +// n1: route to 0.0.0.0 gw 10.1.1.1 +// Note: These default routes to 0.0.0.0 are generated by the extension +// in the global route manager to install default routes via the +// peer node on a point-to-point link, when the node is on a +// stub link +// +// LAN test: +// n0 <--------> n1 (broadcast link on subnet 10.1.1.0/24) +// Expected routes: +// n0: route to 10.1.1.0 gw 0.0.0.0 +// n1: route to 10.1.1.0 gw 0.0.0.0 +// Two link test: +// n0 <--------> n1 <--------> n2 (point-to-point links) +// 10.1.1.1 10.1.1.2/ 10.1.2.2 +// 10.1.2.1 +// Expected routes: +// n0: route to 0.0.0.0 gw 10.1.1.2 +// n1: route to 10.1.1.1 gw 10.1.1.1 +// route to 10.1.2.2 gw 10.1.2.2 +// route to 10.1.1.0 gw 10.1.1.1 +// route to 10.1.2.0 gw 10.1.2.2 +// n2: route to 0.0.0.0 gw 10.1.2.1 +// Note: These default routes to 0.0.0.0 are generated by the extension +// in the global route manager to install default routes via the +// peer node on a point-to-point link, when the node is on a +// stub link +// Two LANs test: +// n0 <--------> n1 <--------> n2 (broadcast links) +// Expected routes: +// n0: route to 10.1.1.0 gw 0.0.0.0 +// route to 0.0.0.0 gw 10.1.1.2 +// n1: route to 10.1.1.1 gw 10.1.1.1 +// route to 10.1.2.2 gw 10.1.2.2 +// route to 10.1.1.0 gw 10.1.1.1 +// route to 10.1.2.0 gw 10.1.2.2 +// n2: route to 0.0.0.0 gw 10.1.2.1 + +class LinkTest : public TestCase +{ +public: + virtual void DoSetup (void); + virtual void DoRun (void); + LinkTest (); +private: + NodeContainer m_nodes; +}; + +LinkTest::LinkTest () + : TestCase ("Global routing on point-to-point link") +{ +} + +void +LinkTest::DoSetup () +{ + m_nodes.Create (2); + + Ptr channel = CreateObject (); + SimpleNetDeviceHelper simpleHelper; + simpleHelper.SetNetDevicePointToPointMode (true); + NetDeviceContainer net = simpleHelper.Install (m_nodes, channel); + + InternetStackHelper internet; + // By default, InternetStackHelper adds a static and global routing + // implementation. We just want the global for this test. + Ipv4GlobalRoutingHelper ipv4RoutingHelper; + internet.SetRoutingHelper (ipv4RoutingHelper); + internet.Install (m_nodes); + + Ipv4AddressHelper ipv4; + ipv4.SetBase ("10.1.1.0", "255.255.255.252"); + Ipv4InterfaceContainer i = ipv4.Assign (net); +} + +void +LinkTest::DoRun () +{ + Ipv4GlobalRoutingHelper::PopulateRoutingTables (); + + Ptr ip0 = m_nodes.Get (0)->GetObject (); + NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object"); + Ptr ip1 = m_nodes.Get (1)->GetObject (); + NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object"); + Ptr routing0 = ip0->GetRoutingProtocol (); + Ptr globalRouting0 = routing0->GetObject (); + NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object"); + Ptr routing1 = ip1->GetRoutingProtocol (); + Ptr globalRouting1 = routing1->GetObject (); + NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object"); + + // Test that the right number of routes found + uint32_t nRoutes0 = globalRouting0->GetNRoutes (); + NS_LOG_DEBUG ("LinkTest nRoutes0 " << nRoutes0); + NS_TEST_ASSERT_MSG_EQ (nRoutes0, 1, "Error-- not one route"); + Ipv4RoutingTableEntry* route = globalRouting0->GetRoute (0); + NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("0.0.0.0"), "Error-- wrong destination"); + NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.2"), "Error-- wrong gateway"); + + // Test that the right number of routes found + uint32_t nRoutes1 = globalRouting1->GetNRoutes (); + NS_TEST_ASSERT_MSG_EQ (nRoutes1, 1, "Error-- not one route"); + NS_LOG_DEBUG ("LinkTest nRoutes1 " << nRoutes1); + route = globalRouting1->GetRoute (0); + NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("0.0.0.0"), "Error-- wrong destination"); + NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.1"), "Error-- wrong gateway"); + + bool result = true; + + NS_TEST_ASSERT_MSG_EQ (result, true, "Message"); + Simulator::Run (); + Simulator::Destroy (); +} + +class LanTest : public TestCase +{ +public: + virtual void DoSetup (void); + virtual void DoRun (void); + LanTest (); +private: + NodeContainer m_nodes; +}; + +LanTest::LanTest () + : TestCase ("Global routing on broadcast link") +{ +} + +void +LanTest::DoSetup () +{ + m_nodes.Create (2); + + Ptr channel = CreateObject (); + SimpleNetDeviceHelper simpleHelper; + NetDeviceContainer net = simpleHelper.Install (m_nodes, channel); + + InternetStackHelper internet; + // By default, InternetStackHelper adds a static and global routing + // implementation. We just want the global for this test. + Ipv4GlobalRoutingHelper ipv4RoutingHelper; + internet.SetRoutingHelper (ipv4RoutingHelper); + internet.Install (m_nodes); + + Ipv4AddressHelper ipv4; + ipv4.SetBase ("10.1.1.0", "255.255.255.0"); + Ipv4InterfaceContainer i = ipv4.Assign (net); +} + +void +LanTest::DoRun () +{ + Ipv4GlobalRoutingHelper::PopulateRoutingTables (); + + Ptr ip0 = m_nodes.Get (0)->GetObject (); + NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object"); + Ptr ip1 = m_nodes.Get (1)->GetObject (); + NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object"); + Ptr routing0 = ip0->GetRoutingProtocol (); + Ptr globalRouting0 = routing0->GetObject (); + NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object"); + Ptr routing1 = ip1->GetRoutingProtocol (); + Ptr globalRouting1 = routing1->GetObject (); + NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object"); + + // Test that the right number of routes found + uint32_t nRoutes0 = globalRouting0->GetNRoutes (); + NS_LOG_DEBUG ("LanTest nRoutes0 " << nRoutes0); + NS_TEST_ASSERT_MSG_EQ (nRoutes0, 1, "Error-- more than one entry"); + for (uint32_t i = 0; i < globalRouting0->GetNRoutes (); i++) + { + Ipv4RoutingTableEntry* route = globalRouting0->GetRoute (i); + NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + } + + // Test that the right number of routes found + uint32_t nRoutes1 = globalRouting1->GetNRoutes (); + NS_LOG_DEBUG ("LanTest nRoutes1 " << nRoutes1); + NS_TEST_ASSERT_MSG_EQ (nRoutes1, 1, "Error-- more than one entry"); + for (uint32_t i = 0; i < globalRouting0->GetNRoutes (); i++) + { + Ipv4RoutingTableEntry* route = globalRouting1->GetRoute (i); + NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + } + + Simulator::Destroy (); +} + +class TwoLinkTest : public TestCase +{ +public: + virtual void DoSetup (void); + virtual void DoRun (void); + TwoLinkTest (); +private: + NodeContainer m_nodes; +}; + +TwoLinkTest::TwoLinkTest () + : TestCase ("Global routing across two hops (point-to-point links)") +{ +} + +void +TwoLinkTest::DoSetup () +{ + m_nodes.Create (3); + + Ptr channel = CreateObject (); + SimpleNetDeviceHelper simpleHelper; + simpleHelper.SetNetDevicePointToPointMode (true); + NetDeviceContainer net = simpleHelper.Install (m_nodes.Get (0), channel); + net.Add (simpleHelper.Install (m_nodes.Get (1), channel)); + + Ptr channel2 = CreateObject (); + SimpleNetDeviceHelper simpleHelper2; + simpleHelper2.SetNetDevicePointToPointMode (true); + NetDeviceContainer net2 = simpleHelper.Install (m_nodes.Get (1), channel2); + net2.Add (simpleHelper2.Install (m_nodes.Get (2), channel2)); + + InternetStackHelper internet; + // By default, InternetStackHelper adds a static and global routing + // implementation. We just want the global for this test. + Ipv4GlobalRoutingHelper ipv4RoutingHelper; + internet.SetRoutingHelper (ipv4RoutingHelper); + internet.Install (m_nodes); + + Ipv4AddressHelper ipv4; + ipv4.SetBase ("10.1.1.0", "255.255.255.252"); + Ipv4InterfaceContainer i = ipv4.Assign (net); + ipv4.SetBase ("10.1.2.0", "255.255.255.252"); + Ipv4InterfaceContainer i2 = ipv4.Assign (net2); +} + +void +TwoLinkTest::DoRun () +{ + Ipv4GlobalRoutingHelper::PopulateRoutingTables (); + + Ptr ip0 = m_nodes.Get (0)->GetObject (); + NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object"); + Ptr ip1 = m_nodes.Get (1)->GetObject (); + NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object"); + Ptr ip2 = m_nodes.Get (2)->GetObject (); + NS_TEST_ASSERT_MSG_NE (ip2, 0, "Error-- no Ipv4 object"); + Ptr routing0 = ip0->GetRoutingProtocol (); + Ptr globalRouting0 = routing0->GetObject (); + NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object"); + Ptr routing1 = ip1->GetRoutingProtocol (); + Ptr globalRouting1 = routing1->GetObject (); + NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object"); + Ptr routing2 = ip2->GetRoutingProtocol (); + Ptr globalRouting2 = routing2->GetObject (); + NS_TEST_ASSERT_MSG_NE (globalRouting2, 0, "Error-- no Ipv4GlobalRouting object"); + + // node n0 + // Test that the right number of routes found + uint32_t nRoutes0 = globalRouting0->GetNRoutes (); + NS_LOG_DEBUG ("TwoLinkTest nRoutes0 " << nRoutes0); + NS_TEST_ASSERT_MSG_EQ (nRoutes0, 1, "Error-- wrong number of links"); + + Ipv4RoutingTableEntry* route = globalRouting0->GetRoute (0); + NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("0.0.0.0"), "Error-- wrong destination"); + NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.2"), "Error-- wrong gateway"); + + // node n1 + // Test that the right number of routes found + uint32_t nRoutes1 = globalRouting1->GetNRoutes (); + NS_LOG_DEBUG ("TwoLinkTest nRoutes1 " << nRoutes1); + route = globalRouting1->GetRoute (0); + NS_LOG_DEBUG ("TwoLinkTest entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.1"), "Error-- wrong destination"); + NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.1"), "Error-- wrong gateway"); + route = globalRouting1->GetRoute (1); + NS_LOG_DEBUG ("TwoLinkTest entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.2"), "Error-- wrong destination"); + NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.2.2"), "Error-- wrong gateway"); + route = globalRouting1->GetRoute (2); + NS_LOG_DEBUG ("TwoLinkTest entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.0"), "Error-- wrong destination"); + NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.1"), "Error-- wrong gateway"); + route = globalRouting1->GetRoute (3); + NS_LOG_DEBUG ("TwoLinkTest entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.0"), "Error-- wrong destination"); + NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.2.2"), "Error-- wrong gateway"); + + // node n2 + // Test that the right number of routes found + uint32_t nRoutes2 = globalRouting2->GetNRoutes (); + NS_LOG_DEBUG ("TwoLinkTest nRoutes2 " << nRoutes2); + NS_TEST_ASSERT_MSG_EQ (nRoutes2, 1, "Error-- wrong number of links"); + + route = globalRouting2->GetRoute (0); + NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("0.0.0.0"), "Error-- wrong destination"); + NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.2.1"), "Error-- wrong gateway"); + + Simulator::Destroy (); +} + +class TwoLanTest : public TestCase +{ +public: + virtual void DoSetup (void); + virtual void DoRun (void); + TwoLanTest (); +private: + NodeContainer m_nodes; +}; + +TwoLanTest::TwoLanTest () + : TestCase ("Global routing across two hops (broadcast links)") +{ +} + +void +TwoLanTest::DoSetup () +{ + m_nodes.Create (3); + + Ptr channel = CreateObject (); + SimpleNetDeviceHelper simpleHelper; + NetDeviceContainer net = simpleHelper.Install (m_nodes.Get (0), channel); + net.Add (simpleHelper.Install (m_nodes.Get (1), channel)); + + Ptr channel2 = CreateObject (); + SimpleNetDeviceHelper simpleHelper2; + NetDeviceContainer net2 = simpleHelper.Install (m_nodes.Get (1), channel2); + net2.Add (simpleHelper2.Install (m_nodes.Get (2), channel2)); + + InternetStackHelper internet; + // By default, InternetStackHelper adds a static and global routing + // implementation. We just want the global for this test. + Ipv4GlobalRoutingHelper ipv4RoutingHelper; + internet.SetRoutingHelper (ipv4RoutingHelper); + internet.Install (m_nodes); + + Ipv4AddressHelper ipv4; + ipv4.SetBase ("10.1.1.0", "255.255.255.0"); + Ipv4InterfaceContainer i = ipv4.Assign (net); + ipv4.SetBase ("10.1.2.0", "255.255.255.0"); + Ipv4InterfaceContainer i2 = ipv4.Assign (net2); +} + +void +TwoLanTest::DoRun () +{ + Ipv4GlobalRoutingHelper::PopulateRoutingTables (); + + Ptr ip0 = m_nodes.Get (0)->GetObject (); + NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object"); + Ptr ip1 = m_nodes.Get (1)->GetObject (); + NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object"); + Ptr ip2 = m_nodes.Get (2)->GetObject (); + NS_TEST_ASSERT_MSG_NE (ip2, 0, "Error-- no Ipv4 object"); + Ptr routing0 = ip0->GetRoutingProtocol (); + Ptr globalRouting0 = routing0->GetObject (); + NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object"); + Ptr routing1 = ip1->GetRoutingProtocol (); + Ptr globalRouting1 = routing1->GetObject (); + NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object"); + Ptr routing2 = ip2->GetRoutingProtocol (); + Ptr globalRouting2 = routing2->GetObject (); + NS_TEST_ASSERT_MSG_NE (globalRouting2, 0, "Error-- no Ipv4GlobalRouting object"); + + // Test that the right number of routes found + uint32_t nRoutes0 = globalRouting0->GetNRoutes (); + NS_LOG_DEBUG ("TwoLanTest nRoutes0 " << nRoutes0); + NS_TEST_ASSERT_MSG_EQ (nRoutes0, 2, "Error-- not two entries"); + Ipv4RoutingTableEntry* route = globalRouting0->GetRoute (0); + NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.0"), "Error-- wrong destination"); + NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("0.0.0.0"), "Error-- wrong gateway"); + route = globalRouting0->GetRoute (1); + NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.0"), "Error-- wrong destination"); + NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.2"), "Error-- wrong gateway"); + + // Test that the right number of routes found + uint32_t nRoutes1 = globalRouting1->GetNRoutes (); + NS_LOG_DEBUG ("TwoLanTest nRoutes1 " << nRoutes1); + NS_TEST_ASSERT_MSG_EQ (nRoutes1, 2, "Error-- not two entries"); + route = globalRouting1->GetRoute (0); + NS_LOG_DEBUG ("TwoLanTest entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.0"), "Error-- wrong destination"); + NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("0.0.0.0"), "Error-- wrong gateway"); + route = globalRouting1->GetRoute (1); + NS_LOG_DEBUG ("TwoLanTest entry dest " << route->GetDest () << " gw " << route->GetGateway ()); + NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.0"), "Error-- wrong destination"); + NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("0.0.0.0"), "Error-- wrong gateway"); + + Simulator::Destroy (); +} + class Ipv4DynamicGlobalRoutingTestCase : public TestCase { public: @@ -419,7 +831,6 @@ Ipv4GlobalRoutingSlash32TestCase::DoRun (void) Simulator::Destroy (); } - class Ipv4GlobalRoutingTestSuite : public TestSuite { public: @@ -428,10 +839,14 @@ public: Ipv4GlobalRoutingTestSuite::Ipv4GlobalRoutingTestSuite () : TestSuite ("ipv4-global-routing", UNIT) -{ - AddTestCase (new Ipv4DynamicGlobalRoutingTestCase, TestCase::QUICK); - AddTestCase (new Ipv4GlobalRoutingSlash32TestCase, TestCase::QUICK); -} + { + AddTestCase (new LinkTest, TestCase::QUICK); + AddTestCase (new LanTest, TestCase::QUICK); + AddTestCase (new TwoLinkTest, TestCase::QUICK); + AddTestCase (new TwoLanTest, TestCase::QUICK); + AddTestCase (new Ipv4DynamicGlobalRoutingTestCase, TestCase::QUICK); + AddTestCase (new Ipv4GlobalRoutingSlash32TestCase, TestCase::QUICK); + } // Do not forget to allocate an instance of this TestSuite static Ipv4GlobalRoutingTestSuite globalRoutingTestSuite;