diff --git a/src/internet/model/global-route-manager-impl.cc b/src/internet/model/global-route-manager-impl.cc index a1d62178e..4bb163f0b 100644 --- a/src/internet/model/global-route-manager-impl.cc +++ b/src/internet/model/global-route-manager-impl.cc @@ -2191,211 +2191,3 @@ GlobalRouteManagerImpl::SPFVertexAddParent (SPFVertex* v) } // namespace ns3 -#include "ns3/test.h" -#include "ns3/simulator.h" -#include // for rand() - -namespace ns3 { - -class GlobalRouteManagerImplTestCase : public TestCase -{ -public: - GlobalRouteManagerImplTestCase(); - virtual void DoRun(void); -}; - -GlobalRouteManagerImplTestCase::GlobalRouteManagerImplTestCase() - : TestCase("GlobalRouteManagerImplTestCase") -{} -void -GlobalRouteManagerImplTestCase::DoRun(void) -{ - CandidateQueue candidate; - - for (int i = 0; i < 100; ++i) - { - SPFVertex *v = new SPFVertex; - v->SetDistanceFromRoot (rand () % 100); - candidate.Push (v); - } - - uint32_t lastDistance = 0; - - for (int i = 0; i < 100; ++i) - { - SPFVertex *v = candidate.Pop (); - if (v->GetDistanceFromRoot () < lastDistance) - { - // XXX does nothing. - UpdateErrorStatus (false); - } - lastDistance = v->GetDistanceFromRoot (); - delete v; - v = 0; - } - - // Build fake link state database; four routers (0-3), 3 point-to-point - // links - // - // n0 - // \ link 0 - // \ link 2 - // n2 -------------------------n3 - // / - // / link 1 - // n1 - // - // link0: 10.1.1.1/30, 10.1.1.2/30 - // link1: 10.1.2.1/30, 10.1.2.2/30 - // link2: 10.1.3.1/30, 10.1.3.2/30 - // - // Router 0 - GlobalRoutingLinkRecord* lr0 = new GlobalRoutingLinkRecord ( - GlobalRoutingLinkRecord::PointToPoint, - "0.0.0.2", // router ID 0.0.0.2 - "10.1.1.1", // local ID - 1); // metric - - GlobalRoutingLinkRecord* lr1 = new GlobalRoutingLinkRecord ( - GlobalRoutingLinkRecord::StubNetwork, - "10.1.1.1", - "255.255.255.252", - 1); - - GlobalRoutingLSA* lsa0 = new GlobalRoutingLSA (); - lsa0->SetLSType (GlobalRoutingLSA::RouterLSA); - lsa0->SetLinkStateId ("0.0.0.0"); - lsa0->SetAdvertisingRouter ("0.0.0.0"); - lsa0->AddLinkRecord (lr0); - lsa0->AddLinkRecord (lr1); - - // Router 1 - GlobalRoutingLinkRecord* lr2 = new GlobalRoutingLinkRecord ( - GlobalRoutingLinkRecord::PointToPoint, - "0.0.0.2", - "10.1.2.1", - 1); - - GlobalRoutingLinkRecord* lr3 = new GlobalRoutingLinkRecord ( - GlobalRoutingLinkRecord::StubNetwork, - "10.1.2.1", - "255.255.255.252", - 1); - - GlobalRoutingLSA* lsa1 = new GlobalRoutingLSA (); - lsa1->SetLSType (GlobalRoutingLSA::RouterLSA); - lsa1->SetLinkStateId ("0.0.0.1"); - lsa1->SetAdvertisingRouter ("0.0.0.1"); - lsa1->AddLinkRecord (lr2); - lsa1->AddLinkRecord (lr3); - - // Router 2 - GlobalRoutingLinkRecord* lr4 = new GlobalRoutingLinkRecord ( - GlobalRoutingLinkRecord::PointToPoint, - "0.0.0.0", - "10.1.1.2", - 1); - - GlobalRoutingLinkRecord* lr5 = new GlobalRoutingLinkRecord ( - GlobalRoutingLinkRecord::StubNetwork, - "10.1.1.2", - "255.255.255.252", - 1); - - GlobalRoutingLinkRecord* lr6 = new GlobalRoutingLinkRecord ( - GlobalRoutingLinkRecord::PointToPoint, - "0.0.0.1", - "10.1.2.2", - 1); - - GlobalRoutingLinkRecord* lr7 = new GlobalRoutingLinkRecord ( - GlobalRoutingLinkRecord::StubNetwork, - "10.1.2.2", - "255.255.255.252", - 1); - - GlobalRoutingLinkRecord* lr8 = new GlobalRoutingLinkRecord ( - GlobalRoutingLinkRecord::PointToPoint, - "0.0.0.3", - "10.1.3.2", - 1); - - GlobalRoutingLinkRecord* lr9 = new GlobalRoutingLinkRecord ( - GlobalRoutingLinkRecord::StubNetwork, - "10.1.3.2", - "255.255.255.252", - 1); - - GlobalRoutingLSA* lsa2 = new GlobalRoutingLSA (); - lsa2->SetLSType (GlobalRoutingLSA::RouterLSA); - lsa2->SetLinkStateId ("0.0.0.2"); - lsa2->SetAdvertisingRouter ("0.0.0.2"); - lsa2->AddLinkRecord (lr4); - lsa2->AddLinkRecord (lr5); - lsa2->AddLinkRecord (lr6); - lsa2->AddLinkRecord (lr7); - lsa2->AddLinkRecord (lr8); - lsa2->AddLinkRecord (lr9); - - // Router 3 - GlobalRoutingLinkRecord* lr10 = new GlobalRoutingLinkRecord ( - GlobalRoutingLinkRecord::PointToPoint, - "0.0.0.2", - "10.1.2.1", - 1); - - GlobalRoutingLinkRecord* lr11 = new GlobalRoutingLinkRecord ( - GlobalRoutingLinkRecord::StubNetwork, - "10.1.2.1", - "255.255.255.252", - 1); - - GlobalRoutingLSA* lsa3 = new GlobalRoutingLSA (); - lsa3->SetLSType (GlobalRoutingLSA::RouterLSA); - lsa3->SetLinkStateId ("0.0.0.3"); - lsa3->SetAdvertisingRouter ("0.0.0.3"); - lsa3->AddLinkRecord (lr10); - lsa3->AddLinkRecord (lr11); - - // Test the database - GlobalRouteManagerLSDB* srmlsdb = new GlobalRouteManagerLSDB (); - srmlsdb->Insert (lsa0->GetLinkStateId (), lsa0); - srmlsdb->Insert (lsa1->GetLinkStateId (), lsa1); - srmlsdb->Insert (lsa2->GetLinkStateId (), lsa2); - srmlsdb->Insert (lsa3->GetLinkStateId (), lsa3); - NS_ASSERT (lsa2 == srmlsdb->GetLSA (lsa2->GetLinkStateId ())); - - // next, calculate routes based on the manually created LSDB - GlobalRouteManagerImpl* srm = new GlobalRouteManagerImpl (); - srm->DebugUseLsdb (srmlsdb); // manually add in an LSDB - // Note-- this will succeed without any nodes in the topology - // because the NodeList is empty - srm->DebugSPFCalculate (lsa0->GetLinkStateId ()); // node n0 - - Simulator::Run (); - -// XXX here we should do some verification of the routes built - - Simulator::Destroy (); - - // This delete clears the srm, which deletes the LSDB, which clears - // all of the LSAs, which each destroys the attached LinkRecords. - delete srm; - - // XXX - // No testing has actually been done other than making sure that this code - // does not crash -} - - -static class GlobalRouteManagerImplTestSuite : public TestSuite -{ -public: - GlobalRouteManagerImplTestSuite() - : TestSuite("global-route-manager-impl", UNIT) - { - AddTestCase(new GlobalRouteManagerImplTestCase()); - } -} g_globalRoutingManagerImplTestSuite; - -} // namespace ns3 diff --git a/src/internet/model/ipv4-list-routing.cc b/src/internet/model/ipv4-list-routing.cc index a5d017a65..39c76c77c 100644 --- a/src/internet/model/ipv4-list-routing.cc +++ b/src/internet/model/ipv4-list-routing.cc @@ -295,111 +295,3 @@ Ipv4ListRouting::Compare (const Ipv4RoutingProtocolEntry& a, const Ipv4RoutingPr } // namespace ns3 -#include "ns3/test.h" -#include "ipv4-list-routing.h" -#include "ns3/ipv4-routing-protocol.h" - -namespace ns3 { - -class Ipv4ARouting : public Ipv4RoutingProtocol { -public: - Ptr RouteOutput (Ptr p, const Ipv4Header &header, Ptr oif, Socket::SocketErrno &sockerr) { return 0;} - bool RouteInput (Ptr p, const Ipv4Header &header, Ptr idev, - UnicastForwardCallback ucb, MulticastForwardCallback mcb, - LocalDeliverCallback lcb, ErrorCallback ecb) {return false;} - void NotifyInterfaceUp (uint32_t interface) {} - void NotifyInterfaceDown (uint32_t interface) {} - void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address) {} - void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address) {} - void SetIpv4 (Ptr ipv4) {} - void PrintRoutingTable (Ptr stream) const {} -}; - -class Ipv4BRouting : public Ipv4RoutingProtocol { -public: - Ptr RouteOutput (Ptr p, const Ipv4Header &header, Ptr oif, Socket::SocketErrno &sockerr) { return 0;} - bool RouteInput (Ptr p, const Ipv4Header &header, Ptr idev, - UnicastForwardCallback ucb, MulticastForwardCallback mcb, - LocalDeliverCallback lcb, ErrorCallback ecb) {return false;} - void NotifyInterfaceUp (uint32_t interface) {} - void NotifyInterfaceDown (uint32_t interface) {} - void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address) {} - void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address) {} - void SetIpv4 (Ptr ipv4) {} - void PrintRoutingTable (Ptr stream) const {} -}; - -class Ipv4ListRoutingNegativeTestCase : public TestCase -{ -public: - Ipv4ListRoutingNegativeTestCase(); - virtual void DoRun (void); -}; - -Ipv4ListRoutingNegativeTestCase::Ipv4ListRoutingNegativeTestCase() - : TestCase("Check negative priorities") -{} -void -Ipv4ListRoutingNegativeTestCase::DoRun (void) -{ - Ptr lr = CreateObject (); - Ptr aRouting = CreateObject (); - Ptr bRouting = CreateObject (); - // The Ipv4BRouting should be added with higher priority (larger integer value) - lr->AddRoutingProtocol (aRouting, -10); - lr->AddRoutingProtocol (bRouting, -5); - int16_t first = 3; - uint32_t num = lr->GetNRoutingProtocols (); - NS_TEST_ASSERT_MSG_EQ (num, 2, "XXX"); - Ptr firstRp = lr->GetRoutingProtocol (0, first); - NS_TEST_ASSERT_MSG_EQ (-5, first, "XXX"); - NS_TEST_ASSERT_MSG_EQ (firstRp, bRouting, "XXX"); -} - -class Ipv4ListRoutingPositiveTestCase : public TestCase -{ -public: - Ipv4ListRoutingPositiveTestCase(); - virtual void DoRun (void); -}; - -Ipv4ListRoutingPositiveTestCase::Ipv4ListRoutingPositiveTestCase() - : TestCase("Check positive priorities") -{} -void -Ipv4ListRoutingPositiveTestCase::DoRun (void) -{ - Ptr lr = CreateObject (); - Ptr aRouting = CreateObject (); - Ptr bRouting = CreateObject (); - // The Ipv4ARouting should be added with higher priority (larger integer - // value) and will be fetched first below - lr->AddRoutingProtocol (aRouting, 10); - lr->AddRoutingProtocol (bRouting, 5); - int16_t first = 3; - int16_t second = 3; - uint32_t num = lr->GetNRoutingProtocols (); - NS_TEST_ASSERT_MSG_EQ (num, 2, "XXX"); - Ptr firstRp = lr->GetRoutingProtocol (0, first); - NS_TEST_ASSERT_MSG_EQ (10, first, "XXX"); - NS_TEST_ASSERT_MSG_EQ (firstRp, aRouting, "XXX"); - Ptr secondRp = lr->GetRoutingProtocol (1, second); - NS_TEST_ASSERT_MSG_EQ (5, second, "XXX"); - NS_TEST_ASSERT_MSG_EQ (secondRp, bRouting, "XXX"); -} - -static class Ipv4ListRoutingTestSuite : public TestSuite -{ -public: - Ipv4ListRoutingTestSuite() - : TestSuite("ipv4-list-routing", UNIT) - { - AddTestCase(new Ipv4ListRoutingPositiveTestCase()); - AddTestCase(new Ipv4ListRoutingNegativeTestCase()); - } - -} g_ipv4ListRoutingTestSuite; - -} // namespace ns3 - - diff --git a/src/internet/model/ipv4-packet-info-tag.cc b/src/internet/model/ipv4-packet-info-tag.cc index 53dafb736..74ad0ae6d 100644 --- a/src/internet/model/ipv4-packet-info-tag.cc +++ b/src/internet/model/ipv4-packet-info-tag.cc @@ -21,7 +21,6 @@ #include #include "ns3/ipv4-address.h" -#include "ns3/test.h" #include "ipv4-packet-info-tag.h" namespace ns3 { @@ -139,187 +138,3 @@ Ipv4PacketInfoTag::Print (std::ostream &os) const } }//namespace ns3 -//----------------------------------------------------------------------------- -// Unit tests -//----------------------------------------------------------------------------- - -#include "ns3/log.h" -#include "ns3/abort.h" -#include "ns3/attribute.h" -#include "ns3/simple-net-device.h" -#include "ns3/object-factory.h" -#include "ns3/socket-factory.h" -#include "ns3/udp-socket-factory.h" -#include "ns3/udp-socket.h" -#include "ns3/inet-socket-address.h" -#include "ns3/ipv4-l3-protocol.h" -#include "ns3/ipv4-raw-socket-factory.h" -#include "ns3/ipv4-interface.h" -#include "ns3/arp-l3-protocol.h" -#include "ns3/icmpv4-l4-protocol.h" -#include "ns3/ipv4-static-routing.h" -#include "ns3/ipv4-list-routing.h" -#include "ns3/udp-l4-protocol.h" -#include "ns3/tcp-l4-protocol.h" -#include "ns3/simulator.h" -#include "ns3/node.h" - -namespace ns3 { - -static void -AddInternetStack (Ptr node) -{ - //ARP - Ptr arp = CreateObject (); - node->AggregateObject(arp); - //IPV4 - Ptr ipv4 = CreateObject (); - //Routing for Ipv4 - Ptr ipv4Routing = CreateObject (); - ipv4->SetRoutingProtocol (ipv4Routing); - Ptr ipv4staticRouting = CreateObject (); - ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0); - node->AggregateObject(ipv4); - //ICMP - Ptr icmp = CreateObject (); - node->AggregateObject(icmp); - //UDP - Ptr udp = CreateObject (); - node->AggregateObject(udp); -} - -class Ipv4PacketInfoTagTest: public TestCase -{ -public: - Ipv4PacketInfoTagTest (); -private: - virtual void DoRun (void); - void RxCb (Ptr socket); - void DoSendData (Ptr socket, std::string to); -}; - -Ipv4PacketInfoTagTest::Ipv4PacketInfoTagTest () - : TestCase ("Ipv4PacketInfoTagTest") -{} - -void -Ipv4PacketInfoTagTest::RxCb (Ptr socket) -{ - uint32_t availableData; - Ptr m_receivedPacket; - - availableData = socket->GetRxAvailable (); - m_receivedPacket = socket->Recv (std::numeric_limits::max(), 0); - NS_ASSERT (availableData == m_receivedPacket->GetSize ()); - - Ipv4PacketInfoTag tag; - bool found; - found = m_receivedPacket->RemovePacketTag (tag); - NS_ASSERT (found); - tag.Print (std::cout); -} - -void -Ipv4PacketInfoTagTest::DoSendData (Ptr socket, std::string to) -{ - Address realTo = InetSocketAddress (Ipv4Address (to.c_str()), 200); - if (DynamicCast (socket) != 0) - { - NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create (123), 0, realTo), - 123, "XXX"); - } - // Should only Ipv4RawSock - else - { - socket->SendTo (Create (123), 0, realTo); - } -} - -void -Ipv4PacketInfoTagTest::DoRun (void) -{ - Ptr node0 = CreateObject (); - Ptr node1 = CreateObject (); - - Ptr device = CreateObject (); - Ptr device2 = CreateObject (); - - // For Node 0 - node0->AddDevice (device); - AddInternetStack (node0); - Ptr ipv4 = node0->GetObject (); - - uint32_t index = ipv4->AddInterface (device); - Ipv4InterfaceAddress ifaceAddr1 = Ipv4InterfaceAddress ("10.1.1.1", - "255.255.255.0"); - ipv4->AddAddress (index, ifaceAddr1); - ipv4->SetMetric (index, 1); - ipv4->SetUp (index); - - // For Node 1 - node1->AddDevice (device2); - AddInternetStack (node1); - ipv4 = node1->GetObject (); - - index = ipv4->AddInterface (device2); - Ipv4InterfaceAddress ifaceAddr2 = Ipv4InterfaceAddress ("10.1.1.2", - "255.255.255.0"); - ipv4->AddAddress (index, ifaceAddr2); - ipv4->SetMetric (index, 1); - ipv4->SetUp (index); - - // IPv4 test - Ptr factory = node0->GetObject (UdpSocketFactory::GetTypeId ()); - Ptr socket = factory->CreateSocket (); - InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 200); - socket->Bind (local); - socket->SetRecvPktInfo (true); - socket->SetRecvCallback (MakeCallback (&ns3::Ipv4PacketInfoTagTest::RxCb, this)); - - // receive on loopback - Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0), - &Ipv4PacketInfoTagTest::DoSendData, this, socket, "127.0.0.1"); - Simulator::Run (); - - Ptr factory2 = node1->GetObject (UdpSocketFactory::GetTypeId ()); - Ptr socket2 = factory2->CreateSocket (); - Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0), - &Ipv4PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1"); - Simulator::Run (); - - // ipv4 w rawsocket - factory = node0->GetObject (Ipv4RawSocketFactory::GetTypeId ()); - socket = factory->CreateSocket (); - local = InetSocketAddress (Ipv4Address::GetAny (), 0); - socket->Bind (local); - socket->SetRecvPktInfo (true); - socket->SetRecvCallback (MakeCallback (&ns3::Ipv4PacketInfoTagTest::RxCb, this)); - - // receive on loopback - Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0), - &Ipv4PacketInfoTagTest::DoSendData, this, socket, "127.0.0.1"); - Simulator::Run (); - - factory2 = node1->GetObject (Ipv4RawSocketFactory::GetTypeId ()); - socket2 = factory2->CreateSocket (); - Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0), - &Ipv4PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1"); - Simulator::Run (); - Simulator::Destroy (); -} - -static class Ipv4PacketInfoTagTestSuite : public TestSuite -{ -public: - Ipv4PacketInfoTagTestSuite (); -private: -} g_packetinfotagTests; - -Ipv4PacketInfoTagTestSuite::Ipv4PacketInfoTagTestSuite () - : TestSuite ("ipv4-packet-info-tag", UNIT) -{ - AddTestCase (new Ipv4PacketInfoTagTest ()); -} - - -} diff --git a/src/internet/model/ipv4-packet-info-tag.h b/src/internet/model/ipv4-packet-info-tag.h index 2fcf69044..645526856 100644 --- a/src/internet/model/ipv4-packet-info-tag.h +++ b/src/internet/model/ipv4-packet-info-tag.h @@ -22,6 +22,7 @@ #define __IPV4_PACKET_INFO_TAG_H__ #include "ns3/tag.h" +#include "ns3/ipv4-address.h" namespace ns3 { diff --git a/src/internet/model/ipv6-list-routing.cc b/src/internet/model/ipv6-list-routing.cc index f06b7489a..77b12bce5 100644 --- a/src/internet/model/ipv6-list-routing.cc +++ b/src/internet/model/ipv6-list-routing.cc @@ -333,114 +333,3 @@ Ipv6ListRouting::Compare (const Ipv6RoutingProtocolEntry& a, const Ipv6RoutingPr } // namespace ns3 - -#include "ns3/test.h" -#include "ipv6-list-routing.h" -#include "ns3/ipv6-routing-protocol.h" - -namespace ns3 { - - class Ipv6ARouting : public Ipv6RoutingProtocol { - public: - Ptr RouteOutput (Ptr p, const Ipv6Header &header, Ptr oif, Socket::SocketErrno &sockerr) { return 0;} - bool RouteInput (Ptr p, const Ipv6Header &header, Ptr idev, - UnicastForwardCallback ucb, MulticastForwardCallback mcb, - LocalDeliverCallback lcb, ErrorCallback ecb) {return false;} - void NotifyInterfaceUp (uint32_t interface) {} - void NotifyInterfaceDown (uint32_t interface) {} - void NotifyAddAddress (uint32_t interface, Ipv6InterfaceAddress address) {} - void NotifyRemoveAddress (uint32_t interface, Ipv6InterfaceAddress address) {} - void NotifyAddRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse = Ipv6Address:: - GetZero ()) {} - void NotifyRemoveRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse) {} - void SetIpv6 (Ptr ipv6) {} - }; - - class Ipv6BRouting : public Ipv6RoutingProtocol { - public: - Ptr RouteOutput (Ptr p, const Ipv6Header &header, Ptr oif, Socket::SocketErrno &sockerr) { return 0;} - bool RouteInput (Ptr p, const Ipv6Header &header, Ptr idev, - UnicastForwardCallback ucb, MulticastForwardCallback mcb, - LocalDeliverCallback lcb, ErrorCallback ecb) {return false;} - void NotifyInterfaceUp (uint32_t interface) {} - void NotifyInterfaceDown (uint32_t interface) {} - void NotifyAddAddress (uint32_t interface, Ipv6InterfaceAddress address) {} - void NotifyRemoveAddress (uint32_t interface, Ipv6InterfaceAddress address) {} - void NotifyAddRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse = Ipv6Address:: - GetZero ()) {} - void NotifyRemoveRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse) {} - void SetIpv6 (Ptr ipv6) {} - }; - - class Ipv6ListRoutingNegativeTestCase : public TestCase - { - public: - Ipv6ListRoutingNegativeTestCase(); - virtual void DoRun (void); - }; - - Ipv6ListRoutingNegativeTestCase::Ipv6ListRoutingNegativeTestCase() - : TestCase("Check negative priorities") - {} - void - Ipv6ListRoutingNegativeTestCase::DoRun (void) - { - Ptr lr = CreateObject (); - Ptr aRouting = CreateObject (); - Ptr bRouting = CreateObject (); - // The Ipv6BRouting should be added with higher priority (larger integer value) - lr->AddRoutingProtocol (aRouting, -10); - lr->AddRoutingProtocol (bRouting, -5); - int16_t first = 3; - uint32_t num = lr->GetNRoutingProtocols (); - NS_TEST_ASSERT_MSG_EQ (num, 2, "XXX"); - Ptr firstRp = lr->GetRoutingProtocol (0, first); - NS_TEST_ASSERT_MSG_EQ (-5, first, "XXX"); - NS_TEST_ASSERT_MSG_EQ (firstRp, bRouting, "XXX"); - } - - class Ipv6ListRoutingPositiveTestCase : public TestCase - { - public: - Ipv6ListRoutingPositiveTestCase(); - virtual void DoRun (void); - }; - - Ipv6ListRoutingPositiveTestCase::Ipv6ListRoutingPositiveTestCase() - : TestCase("Check positive priorities") - {} - void - Ipv6ListRoutingPositiveTestCase::DoRun (void) - { - Ptr lr = CreateObject (); - Ptr aRouting = CreateObject (); - Ptr bRouting = CreateObject (); - // The Ipv6ARouting should be added with higher priority (larger integer - // value) and will be fetched first below - lr->AddRoutingProtocol (aRouting, 10); - lr->AddRoutingProtocol (bRouting, 5); - int16_t first = 3; - int16_t second = 3; - uint32_t num = lr->GetNRoutingProtocols (); - NS_TEST_ASSERT_MSG_EQ (num, 2, "XXX"); - Ptr firstRp = lr->GetRoutingProtocol (0, first); - NS_TEST_ASSERT_MSG_EQ (10, first, "XXX"); - NS_TEST_ASSERT_MSG_EQ (firstRp, aRouting, "XXX"); - Ptr secondRp = lr->GetRoutingProtocol (1, second); - NS_TEST_ASSERT_MSG_EQ (5, second, "XXX"); - NS_TEST_ASSERT_MSG_EQ (secondRp, bRouting, "XXX"); - } - - static class Ipv6ListRoutingTestSuite : public TestSuite - { - public: - Ipv6ListRoutingTestSuite() - : TestSuite("ipv6-list-routing", UNIT) - { - AddTestCase(new Ipv6ListRoutingPositiveTestCase()); - AddTestCase(new Ipv6ListRoutingNegativeTestCase()); - } - - } g_ipv6ListRoutingTestSuite; - -} // namespace ns3 diff --git a/src/internet/model/ipv6-packet-info-tag.cc b/src/internet/model/ipv6-packet-info-tag.cc index 3a2b01c10..5c1ad1597 100644 --- a/src/internet/model/ipv6-packet-info-tag.cc +++ b/src/internet/model/ipv6-packet-info-tag.cc @@ -21,7 +21,6 @@ #include #include "ns3/ipv6-address.h" -#include "ns3/test.h" #include "ipv6-packet-info-tag.h" namespace ns3 { @@ -137,196 +136,3 @@ Ipv6PacketInfoTag::Print (std::ostream &os) const } }//namespace ns3 -//----------------------------------------------------------------------------- -// Unit tests -//----------------------------------------------------------------------------- - -#include "ns3/log.h" -#include "ns3/abort.h" -#include "ns3/attribute.h" -#include "ns3/simple-net-device.h" -#include "ns3/object-factory.h" -#include "ns3/socket-factory.h" -#include "ns3/udp-socket-factory.h" -#include "ns3/udp-socket.h" -#include "ns3/ipv6-l3-protocol.h" -#include "ns3/ipv6-raw-socket-factory.h" -#include "ns3/ipv6-interface.h" -#include "ns3/icmpv6-l4-protocol.h" -#include "ns3/ipv6-static-routing.h" -#include "ns3/ipv6-list-routing.h" -#include "ns3/inet6-socket-address.h" -#include "ns3/simulator.h" -#include "ns3/uinteger.h" -#include "ns3/boolean.h" -#include "ns3/node.h" - -namespace ns3 { - -static void -AddInternetStack (Ptr node) -{ - Ptr ipv6 = CreateObject (); - Ptr icmpv6 = CreateObject (); - node->AggregateObject (ipv6); - node->AggregateObject (icmpv6); - ipv6->Insert (icmpv6); - icmpv6->SetAttribute ("DAD", BooleanValue (false)); - - //Routing for Ipv6 - Ptr ipv6Routing = CreateObject (); - ipv6->SetRoutingProtocol (ipv6Routing); - Ptr ipv6staticRouting = CreateObject (); - ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0); - - /* register IPv6 extensions and options */ - ipv6->RegisterExtensions (); - ipv6->RegisterOptions (); -} - -class Ipv6PacketInfoTagTest: public TestCase -{ -public: - Ipv6PacketInfoTagTest (); -private: - virtual void DoRun (void); - void RxCb (Ptr socket); - void DoSendData (Ptr socket, std::string to); -}; - -Ipv6PacketInfoTagTest::Ipv6PacketInfoTagTest () - : TestCase ("Ipv6PacketInfoTagTest") -{} - -void -Ipv6PacketInfoTagTest::RxCb (Ptr socket) -{ - uint32_t availableData; - Ptr m_receivedPacket; - - availableData = socket->GetRxAvailable (); - m_receivedPacket = socket->Recv (std::numeric_limits::max(), 0); - NS_ASSERT (availableData == m_receivedPacket->GetSize ()); - - Ipv6PacketInfoTag tag; - bool found; - found = m_receivedPacket->RemovePacketTag (tag); - NS_ASSERT (found); - tag.Print (std::cout); - std::cout << std::endl; -} - -void -Ipv6PacketInfoTagTest::DoSendData (Ptr socket, std::string to) -{ - Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str()), 200); - if (DynamicCast (socket) != 0) - { - NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create (123), 0, realTo), - 123, "XXX"); - } - // Should only Ipv6RawSock - else - { - socket->SendTo (Create (123), 0, realTo); - } -} - -void -Ipv6PacketInfoTagTest::DoRun (void) -{ - Ptr node0 = CreateObject (); - Ptr node1 = CreateObject (); - - Ptr device = CreateObject (); - Ptr device2 = CreateObject (); - - // For Node 0 - node0->AddDevice (device); - AddInternetStack (node0); - Ptr ipv6 = node0->GetObject (); - - uint32_t index = ipv6->AddInterface (device); - Ipv6InterfaceAddress ifaceAddr1 = Ipv6InterfaceAddress (Ipv6Address("2000:1000:0:2000::1"), - Ipv6Prefix(64)); - ipv6->AddAddress (index, ifaceAddr1); - ipv6->SetMetric (index, 1); - ipv6->SetUp (index); - - // For Node 1 - node1->AddDevice (device2); - AddInternetStack (node1); - ipv6 = node1->GetObject (); - - index = ipv6->AddInterface (device2); - Ipv6InterfaceAddress ifaceAddr2 = Ipv6InterfaceAddress (Ipv6Address("2000:1000:0:2000::2"), - Ipv6Prefix(64)); - ipv6->AddAddress (index, ifaceAddr2); - ipv6->SetMetric (index, 1); - ipv6->SetUp (index); - - // ipv6 w rawsocket - Ptr factory = node0->GetObject (Ipv6RawSocketFactory::GetTypeId ()); - Ptr socket = factory->CreateSocket (); - Inet6SocketAddress local = Inet6SocketAddress (Ipv6Address::GetAny (), 0); - socket->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6)); - socket->Bind (local); - socket->SetRecvPktInfo (true); - socket->SetRecvCallback (MakeCallback (&ns3::Ipv6PacketInfoTagTest::RxCb, this)); - - // receive on loopback - Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0), - &Ipv6PacketInfoTagTest::DoSendData, this, socket, "::1"); - Simulator::Run (); - - Ptr factory2 = node1->GetObject (Ipv6RawSocketFactory::GetTypeId ()); - Ptr socket2 = factory2->CreateSocket (); - std::stringstream dst; - dst << ifaceAddr1.GetAddress (); - Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0), - &Ipv6PacketInfoTagTest::DoSendData, this, socket, - dst.str ()); - Simulator::Run (); - -#ifdef UDP6_SUPPORTED - // IPv6 test - factory = node0->GetObject (UdpSocketFactory::GetTypeId ()); - socket = factory->CreateSocket (); - local = Inet6SocketAddress (Ipv6Address::GetAny (), 200); - socket->Bind (local); - socket->SetRecvPktInfo (true); - socket->SetRecvCallback (MakeCallback (&ns3::Ipv6PacketInfoTagTest::RxCb, this)); - - // receive on loopback - Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0), - &Ipv6PacketInfoTagTest::DoSendData, this, socket, "::1"); - Simulator::Run (); - - factory2 = node1->GetObject (UdpSocketFactory::GetTypeId ()); - socket2 = factory2->CreateSocket (); - Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0), - &Ipv6PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1"); - Simulator::Run (); - -#endif // UDP6_SUPPORTED - - Simulator::Destroy (); - // IPv6 test -} - -static class Ipv6PacketInfoTagTestSuite : public TestSuite -{ -public: - Ipv6PacketInfoTagTestSuite (); -private: -} g_packetinfotagTests; - -Ipv6PacketInfoTagTestSuite::Ipv6PacketInfoTagTestSuite () - : TestSuite ("ipv6-packet-info-tag", UNIT) -{ - AddTestCase (new Ipv6PacketInfoTagTest ()); -} - - -} - diff --git a/src/internet/model/ipv6-packet-info-tag.h b/src/internet/model/ipv6-packet-info-tag.h index 4931a9741..996b146a5 100644 --- a/src/internet/model/ipv6-packet-info-tag.h +++ b/src/internet/model/ipv6-packet-info-tag.h @@ -22,6 +22,7 @@ #define __IPV6_PACKET_INFO_TAG_H__ #include "ns3/tag.h" +#include "ns3/ipv6-address.h" namespace ns3 { diff --git a/src/internet/test/global-route-manager-impl-test-suite.cc b/src/internet/test/global-route-manager-impl-test-suite.cc new file mode 100644 index 000000000..97fa2f14b --- /dev/null +++ b/src/internet/test/global-route-manager-impl-test-suite.cc @@ -0,0 +1,234 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright 2007 University of Washington + * Copyright (C) 1999, 2000 Kunihiro Ishiguro, Toshiaki Takada + * + * 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 + * + * Authors: Tom Henderson (tomhend@u.washington.edu) + * + * Kunihiro Ishigura, Toshiaki Takada (GNU Zebra) are attributed authors + * of the quagga 0.99.7/src/ospfd/ospf_spf.c code which was ported here + */ + +#include "ns3/test.h" +#include "ns3/global-route-manager-impl.h" +#include "ns3/candidate-queue.h" +#include "ns3/simulator.h" +#include // for rand() + +namespace ns3 { + +class GlobalRouteManagerImplTestCase : public TestCase +{ +public: + GlobalRouteManagerImplTestCase(); + virtual void DoRun(void); +}; + +GlobalRouteManagerImplTestCase::GlobalRouteManagerImplTestCase() + : TestCase("GlobalRouteManagerImplTestCase") +{} +void +GlobalRouteManagerImplTestCase::DoRun(void) +{ + CandidateQueue candidate; + + for (int i = 0; i < 100; ++i) + { + SPFVertex *v = new SPFVertex; + v->SetDistanceFromRoot (rand () % 100); + candidate.Push (v); + } + + uint32_t lastDistance = 0; + + for (int i = 0; i < 100; ++i) + { + SPFVertex *v = candidate.Pop (); + if (v->GetDistanceFromRoot () < lastDistance) + { + // XXX does nothing. + UpdateErrorStatus (false); + } + lastDistance = v->GetDistanceFromRoot (); + delete v; + v = 0; + } + + // Build fake link state database; four routers (0-3), 3 point-to-point + // links + // + // n0 + // \ link 0 + // \ link 2 + // n2 -------------------------n3 + // / + // / link 1 + // n1 + // + // link0: 10.1.1.1/30, 10.1.1.2/30 + // link1: 10.1.2.1/30, 10.1.2.2/30 + // link2: 10.1.3.1/30, 10.1.3.2/30 + // + // Router 0 + GlobalRoutingLinkRecord* lr0 = new GlobalRoutingLinkRecord ( + GlobalRoutingLinkRecord::PointToPoint, + "0.0.0.2", // router ID 0.0.0.2 + "10.1.1.1", // local ID + 1); // metric + + GlobalRoutingLinkRecord* lr1 = new GlobalRoutingLinkRecord ( + GlobalRoutingLinkRecord::StubNetwork, + "10.1.1.1", + "255.255.255.252", + 1); + + GlobalRoutingLSA* lsa0 = new GlobalRoutingLSA (); + lsa0->SetLSType (GlobalRoutingLSA::RouterLSA); + lsa0->SetLinkStateId ("0.0.0.0"); + lsa0->SetAdvertisingRouter ("0.0.0.0"); + lsa0->AddLinkRecord (lr0); + lsa0->AddLinkRecord (lr1); + + // Router 1 + GlobalRoutingLinkRecord* lr2 = new GlobalRoutingLinkRecord ( + GlobalRoutingLinkRecord::PointToPoint, + "0.0.0.2", + "10.1.2.1", + 1); + + GlobalRoutingLinkRecord* lr3 = new GlobalRoutingLinkRecord ( + GlobalRoutingLinkRecord::StubNetwork, + "10.1.2.1", + "255.255.255.252", + 1); + + GlobalRoutingLSA* lsa1 = new GlobalRoutingLSA (); + lsa1->SetLSType (GlobalRoutingLSA::RouterLSA); + lsa1->SetLinkStateId ("0.0.0.1"); + lsa1->SetAdvertisingRouter ("0.0.0.1"); + lsa1->AddLinkRecord (lr2); + lsa1->AddLinkRecord (lr3); + + // Router 2 + GlobalRoutingLinkRecord* lr4 = new GlobalRoutingLinkRecord ( + GlobalRoutingLinkRecord::PointToPoint, + "0.0.0.0", + "10.1.1.2", + 1); + + GlobalRoutingLinkRecord* lr5 = new GlobalRoutingLinkRecord ( + GlobalRoutingLinkRecord::StubNetwork, + "10.1.1.2", + "255.255.255.252", + 1); + + GlobalRoutingLinkRecord* lr6 = new GlobalRoutingLinkRecord ( + GlobalRoutingLinkRecord::PointToPoint, + "0.0.0.1", + "10.1.2.2", + 1); + + GlobalRoutingLinkRecord* lr7 = new GlobalRoutingLinkRecord ( + GlobalRoutingLinkRecord::StubNetwork, + "10.1.2.2", + "255.255.255.252", + 1); + + GlobalRoutingLinkRecord* lr8 = new GlobalRoutingLinkRecord ( + GlobalRoutingLinkRecord::PointToPoint, + "0.0.0.3", + "10.1.3.2", + 1); + + GlobalRoutingLinkRecord* lr9 = new GlobalRoutingLinkRecord ( + GlobalRoutingLinkRecord::StubNetwork, + "10.1.3.2", + "255.255.255.252", + 1); + + GlobalRoutingLSA* lsa2 = new GlobalRoutingLSA (); + lsa2->SetLSType (GlobalRoutingLSA::RouterLSA); + lsa2->SetLinkStateId ("0.0.0.2"); + lsa2->SetAdvertisingRouter ("0.0.0.2"); + lsa2->AddLinkRecord (lr4); + lsa2->AddLinkRecord (lr5); + lsa2->AddLinkRecord (lr6); + lsa2->AddLinkRecord (lr7); + lsa2->AddLinkRecord (lr8); + lsa2->AddLinkRecord (lr9); + + // Router 3 + GlobalRoutingLinkRecord* lr10 = new GlobalRoutingLinkRecord ( + GlobalRoutingLinkRecord::PointToPoint, + "0.0.0.2", + "10.1.2.1", + 1); + + GlobalRoutingLinkRecord* lr11 = new GlobalRoutingLinkRecord ( + GlobalRoutingLinkRecord::StubNetwork, + "10.1.2.1", + "255.255.255.252", + 1); + + GlobalRoutingLSA* lsa3 = new GlobalRoutingLSA (); + lsa3->SetLSType (GlobalRoutingLSA::RouterLSA); + lsa3->SetLinkStateId ("0.0.0.3"); + lsa3->SetAdvertisingRouter ("0.0.0.3"); + lsa3->AddLinkRecord (lr10); + lsa3->AddLinkRecord (lr11); + + // Test the database + GlobalRouteManagerLSDB* srmlsdb = new GlobalRouteManagerLSDB (); + srmlsdb->Insert (lsa0->GetLinkStateId (), lsa0); + srmlsdb->Insert (lsa1->GetLinkStateId (), lsa1); + srmlsdb->Insert (lsa2->GetLinkStateId (), lsa2); + srmlsdb->Insert (lsa3->GetLinkStateId (), lsa3); + NS_ASSERT (lsa2 == srmlsdb->GetLSA (lsa2->GetLinkStateId ())); + + // next, calculate routes based on the manually created LSDB + GlobalRouteManagerImpl* srm = new GlobalRouteManagerImpl (); + srm->DebugUseLsdb (srmlsdb); // manually add in an LSDB + // Note-- this will succeed without any nodes in the topology + // because the NodeList is empty + srm->DebugSPFCalculate (lsa0->GetLinkStateId ()); // node n0 + + Simulator::Run (); + +// XXX here we should do some verification of the routes built + + Simulator::Destroy (); + + // This delete clears the srm, which deletes the LSDB, which clears + // all of the LSAs, which each destroys the attached LinkRecords. + delete srm; + + // XXX + // No testing has actually been done other than making sure that this code + // does not crash +} + + +static class GlobalRouteManagerImplTestSuite : public TestSuite +{ +public: + GlobalRouteManagerImplTestSuite() + : TestSuite("global-route-manager-impl", UNIT) + { + AddTestCase(new GlobalRouteManagerImplTestCase()); + } +} g_globalRoutingManagerImplTestSuite; + +} // namespace ns3 diff --git a/src/internet/test/ipv4-list-routing-test-suite.cc b/src/internet/test/ipv4-list-routing-test-suite.cc new file mode 100644 index 000000000..01005eef5 --- /dev/null +++ b/src/internet/test/ipv4-list-routing-test-suite.cc @@ -0,0 +1,127 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2009 University of Washington + * + * 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 + * + */ + +#include "ns3/test.h" +#include "ns3/ipv4-list-routing.h" +#include "ns3/ipv4-routing-protocol.h" + +namespace ns3 { + +class Ipv4ARouting : public Ipv4RoutingProtocol { +public: + Ptr RouteOutput (Ptr p, const Ipv4Header &header, Ptr oif, Socket::SocketErrno &sockerr) { return 0;} + bool RouteInput (Ptr p, const Ipv4Header &header, Ptr idev, + UnicastForwardCallback ucb, MulticastForwardCallback mcb, + LocalDeliverCallback lcb, ErrorCallback ecb) {return false;} + void NotifyInterfaceUp (uint32_t interface) {} + void NotifyInterfaceDown (uint32_t interface) {} + void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address) {} + void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address) {} + void SetIpv4 (Ptr ipv4) {} + void PrintRoutingTable (Ptr stream) const {} +}; + +class Ipv4BRouting : public Ipv4RoutingProtocol { +public: + Ptr RouteOutput (Ptr p, const Ipv4Header &header, Ptr oif, Socket::SocketErrno &sockerr) { return 0;} + bool RouteInput (Ptr p, const Ipv4Header &header, Ptr idev, + UnicastForwardCallback ucb, MulticastForwardCallback mcb, + LocalDeliverCallback lcb, ErrorCallback ecb) {return false;} + void NotifyInterfaceUp (uint32_t interface) {} + void NotifyInterfaceDown (uint32_t interface) {} + void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address) {} + void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address) {} + void SetIpv4 (Ptr ipv4) {} + void PrintRoutingTable (Ptr stream) const {} +}; + +class Ipv4ListRoutingNegativeTestCase : public TestCase +{ +public: + Ipv4ListRoutingNegativeTestCase(); + virtual void DoRun (void); +}; + +Ipv4ListRoutingNegativeTestCase::Ipv4ListRoutingNegativeTestCase() + : TestCase("Check negative priorities") +{} +void +Ipv4ListRoutingNegativeTestCase::DoRun (void) +{ + Ptr lr = CreateObject (); + Ptr aRouting = CreateObject (); + Ptr bRouting = CreateObject (); + // The Ipv4BRouting should be added with higher priority (larger integer value) + lr->AddRoutingProtocol (aRouting, -10); + lr->AddRoutingProtocol (bRouting, -5); + int16_t first = 3; + uint32_t num = lr->GetNRoutingProtocols (); + NS_TEST_ASSERT_MSG_EQ (num, 2, "XXX"); + Ptr firstRp = lr->GetRoutingProtocol (0, first); + NS_TEST_ASSERT_MSG_EQ (-5, first, "XXX"); + NS_TEST_ASSERT_MSG_EQ (firstRp, bRouting, "XXX"); +} + +class Ipv4ListRoutingPositiveTestCase : public TestCase +{ +public: + Ipv4ListRoutingPositiveTestCase(); + virtual void DoRun (void); +}; + +Ipv4ListRoutingPositiveTestCase::Ipv4ListRoutingPositiveTestCase() + : TestCase("Check positive priorities") +{} +void +Ipv4ListRoutingPositiveTestCase::DoRun (void) +{ + Ptr lr = CreateObject (); + Ptr aRouting = CreateObject (); + Ptr bRouting = CreateObject (); + // The Ipv4ARouting should be added with higher priority (larger integer + // value) and will be fetched first below + lr->AddRoutingProtocol (aRouting, 10); + lr->AddRoutingProtocol (bRouting, 5); + int16_t first = 3; + int16_t second = 3; + uint32_t num = lr->GetNRoutingProtocols (); + NS_TEST_ASSERT_MSG_EQ (num, 2, "XXX"); + Ptr firstRp = lr->GetRoutingProtocol (0, first); + NS_TEST_ASSERT_MSG_EQ (10, first, "XXX"); + NS_TEST_ASSERT_MSG_EQ (firstRp, aRouting, "XXX"); + Ptr secondRp = lr->GetRoutingProtocol (1, second); + NS_TEST_ASSERT_MSG_EQ (5, second, "XXX"); + NS_TEST_ASSERT_MSG_EQ (secondRp, bRouting, "XXX"); +} + +static class Ipv4ListRoutingTestSuite : public TestSuite +{ +public: + Ipv4ListRoutingTestSuite() + : TestSuite("ipv4-list-routing", UNIT) + { + AddTestCase(new Ipv4ListRoutingPositiveTestCase()); + AddTestCase(new Ipv4ListRoutingNegativeTestCase()); + } + +} g_ipv4ListRoutingTestSuite; + +} // namespace ns3 + + diff --git a/src/internet/test/ipv4-packet-info-tag-test-suite.cc b/src/internet/test/ipv4-packet-info-tag-test-suite.cc new file mode 100644 index 000000000..e64a2b6d8 --- /dev/null +++ b/src/internet/test/ipv4-packet-info-tag-test-suite.cc @@ -0,0 +1,209 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2010 Hajime Tazaki + * + * 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 + * + * Authors: Hajime Tazaki + */ + +//----------------------------------------------------------------------------- +// Unit tests +//----------------------------------------------------------------------------- + +#include + +#include "ns3/test.h" +#include "ns3/ipv4-packet-info-tag.h" +#include "ns3/ipv4-address.h" +#include "ns3/log.h" +#include "ns3/abort.h" +#include "ns3/attribute.h" +#include "ns3/simple-net-device.h" +#include "ns3/object-factory.h" +#include "ns3/socket-factory.h" +#include "ns3/udp-socket-factory.h" +#include "ns3/udp-socket.h" +#include "ns3/inet-socket-address.h" +#include "ns3/ipv4-l3-protocol.h" +#include "ns3/ipv4-raw-socket-factory.h" +#include "ns3/ipv4-interface.h" +#include "ns3/arp-l3-protocol.h" +#include "ns3/icmpv4-l4-protocol.h" +#include "ns3/ipv4-static-routing.h" +#include "ns3/ipv4-list-routing.h" +#include "ns3/udp-l4-protocol.h" +#include "ns3/tcp-l4-protocol.h" +#include "ns3/simulator.h" +#include "ns3/node.h" + +namespace ns3 { + +static void +AddInternetStack (Ptr node) +{ + //ARP + Ptr arp = CreateObject (); + node->AggregateObject(arp); + //IPV4 + Ptr ipv4 = CreateObject (); + //Routing for Ipv4 + Ptr ipv4Routing = CreateObject (); + ipv4->SetRoutingProtocol (ipv4Routing); + Ptr ipv4staticRouting = CreateObject (); + ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0); + node->AggregateObject(ipv4); + //ICMP + Ptr icmp = CreateObject (); + node->AggregateObject(icmp); + //UDP + Ptr udp = CreateObject (); + node->AggregateObject(udp); +} + +class Ipv4PacketInfoTagTest: public TestCase +{ +public: + Ipv4PacketInfoTagTest (); +private: + virtual void DoRun (void); + void RxCb (Ptr socket); + void DoSendData (Ptr socket, std::string to); +}; + +Ipv4PacketInfoTagTest::Ipv4PacketInfoTagTest () + : TestCase ("Ipv4PacketInfoTagTest") +{} + +void +Ipv4PacketInfoTagTest::RxCb (Ptr socket) +{ + uint32_t availableData; + Ptr m_receivedPacket; + + availableData = socket->GetRxAvailable (); + m_receivedPacket = socket->Recv (std::numeric_limits::max(), 0); + NS_ASSERT (availableData == m_receivedPacket->GetSize ()); + + Ipv4PacketInfoTag tag; + bool found; + found = m_receivedPacket->RemovePacketTag (tag); + NS_ASSERT (found); + tag.Print (std::cout); +} + +void +Ipv4PacketInfoTagTest::DoSendData (Ptr socket, std::string to) +{ + Address realTo = InetSocketAddress (Ipv4Address (to.c_str()), 200); + if (DynamicCast (socket) != 0) + { + NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create (123), 0, realTo), + 123, "XXX"); + } + // Should only Ipv4RawSock + else + { + socket->SendTo (Create (123), 0, realTo); + } +} + +void +Ipv4PacketInfoTagTest::DoRun (void) +{ + Ptr node0 = CreateObject (); + Ptr node1 = CreateObject (); + + Ptr device = CreateObject (); + Ptr device2 = CreateObject (); + + // For Node 0 + node0->AddDevice (device); + AddInternetStack (node0); + Ptr ipv4 = node0->GetObject (); + + uint32_t index = ipv4->AddInterface (device); + Ipv4InterfaceAddress ifaceAddr1 = Ipv4InterfaceAddress ("10.1.1.1", + "255.255.255.0"); + ipv4->AddAddress (index, ifaceAddr1); + ipv4->SetMetric (index, 1); + ipv4->SetUp (index); + + // For Node 1 + node1->AddDevice (device2); + AddInternetStack (node1); + ipv4 = node1->GetObject (); + + index = ipv4->AddInterface (device2); + Ipv4InterfaceAddress ifaceAddr2 = Ipv4InterfaceAddress ("10.1.1.2", + "255.255.255.0"); + ipv4->AddAddress (index, ifaceAddr2); + ipv4->SetMetric (index, 1); + ipv4->SetUp (index); + + // IPv4 test + Ptr factory = node0->GetObject (UdpSocketFactory::GetTypeId ()); + Ptr socket = factory->CreateSocket (); + InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 200); + socket->Bind (local); + socket->SetRecvPktInfo (true); + socket->SetRecvCallback (MakeCallback (&ns3::Ipv4PacketInfoTagTest::RxCb, this)); + + // receive on loopback + Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0), + &Ipv4PacketInfoTagTest::DoSendData, this, socket, "127.0.0.1"); + Simulator::Run (); + + Ptr factory2 = node1->GetObject (UdpSocketFactory::GetTypeId ()); + Ptr socket2 = factory2->CreateSocket (); + Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0), + &Ipv4PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1"); + Simulator::Run (); + + // ipv4 w rawsocket + factory = node0->GetObject (Ipv4RawSocketFactory::GetTypeId ()); + socket = factory->CreateSocket (); + local = InetSocketAddress (Ipv4Address::GetAny (), 0); + socket->Bind (local); + socket->SetRecvPktInfo (true); + socket->SetRecvCallback (MakeCallback (&ns3::Ipv4PacketInfoTagTest::RxCb, this)); + + // receive on loopback + Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0), + &Ipv4PacketInfoTagTest::DoSendData, this, socket, "127.0.0.1"); + Simulator::Run (); + + factory2 = node1->GetObject (Ipv4RawSocketFactory::GetTypeId ()); + socket2 = factory2->CreateSocket (); + Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0), + &Ipv4PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1"); + Simulator::Run (); + Simulator::Destroy (); +} + +static class Ipv4PacketInfoTagTestSuite : public TestSuite +{ +public: + Ipv4PacketInfoTagTestSuite (); +private: +} g_packetinfotagTests; + +Ipv4PacketInfoTagTestSuite::Ipv4PacketInfoTagTestSuite () + : TestSuite ("ipv4-packet-info-tag", UNIT) +{ + AddTestCase (new Ipv4PacketInfoTagTest ()); +} + + +} diff --git a/src/internet/test/ipv6-list-routing-test-suite.cc b/src/internet/test/ipv6-list-routing-test-suite.cc new file mode 100644 index 000000000..5f24af5b0 --- /dev/null +++ b/src/internet/test/ipv6-list-routing-test-suite.cc @@ -0,0 +1,130 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2009 University of Washington + * + * 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 + * + */ + +#include "ns3/test.h" +#include "ns3/ipv6-list-routing.h" +#include "ns3/ipv6-route.h" +#include "ns3/ipv6-routing-protocol.h" + +namespace ns3 { + + class Ipv6ARouting : public Ipv6RoutingProtocol { + public: + Ptr RouteOutput (Ptr p, const Ipv6Header &header, Ptr oif, Socket::SocketErrno &sockerr) { return 0;} + bool RouteInput (Ptr p, const Ipv6Header &header, Ptr idev, + UnicastForwardCallback ucb, MulticastForwardCallback mcb, + LocalDeliverCallback lcb, ErrorCallback ecb) {return false;} + void NotifyInterfaceUp (uint32_t interface) {} + void NotifyInterfaceDown (uint32_t interface) {} + void NotifyAddAddress (uint32_t interface, Ipv6InterfaceAddress address) {} + void NotifyRemoveAddress (uint32_t interface, Ipv6InterfaceAddress address) {} + void NotifyAddRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse = Ipv6Address:: + GetZero ()) {} + void NotifyRemoveRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse) {} + void SetIpv6 (Ptr ipv6) {} + }; + + class Ipv6BRouting : public Ipv6RoutingProtocol { + public: + Ptr RouteOutput (Ptr p, const Ipv6Header &header, Ptr oif, Socket::SocketErrno &sockerr) { return 0;} + bool RouteInput (Ptr p, const Ipv6Header &header, Ptr idev, + UnicastForwardCallback ucb, MulticastForwardCallback mcb, + LocalDeliverCallback lcb, ErrorCallback ecb) {return false;} + void NotifyInterfaceUp (uint32_t interface) {} + void NotifyInterfaceDown (uint32_t interface) {} + void NotifyAddAddress (uint32_t interface, Ipv6InterfaceAddress address) {} + void NotifyRemoveAddress (uint32_t interface, Ipv6InterfaceAddress address) {} + void NotifyAddRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse = Ipv6Address:: + GetZero ()) {} + void NotifyRemoveRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse) {} + void SetIpv6 (Ptr ipv6) {} + }; + + class Ipv6ListRoutingNegativeTestCase : public TestCase + { + public: + Ipv6ListRoutingNegativeTestCase(); + virtual void DoRun (void); + }; + + Ipv6ListRoutingNegativeTestCase::Ipv6ListRoutingNegativeTestCase() + : TestCase("Check negative priorities") + {} + void + Ipv6ListRoutingNegativeTestCase::DoRun (void) + { + Ptr lr = CreateObject (); + Ptr aRouting = CreateObject (); + Ptr bRouting = CreateObject (); + // The Ipv6BRouting should be added with higher priority (larger integer value) + lr->AddRoutingProtocol (aRouting, -10); + lr->AddRoutingProtocol (bRouting, -5); + int16_t first = 3; + uint32_t num = lr->GetNRoutingProtocols (); + NS_TEST_ASSERT_MSG_EQ (num, 2, "XXX"); + Ptr firstRp = lr->GetRoutingProtocol (0, first); + NS_TEST_ASSERT_MSG_EQ (-5, first, "XXX"); + NS_TEST_ASSERT_MSG_EQ (firstRp, bRouting, "XXX"); + } + + class Ipv6ListRoutingPositiveTestCase : public TestCase + { + public: + Ipv6ListRoutingPositiveTestCase(); + virtual void DoRun (void); + }; + + Ipv6ListRoutingPositiveTestCase::Ipv6ListRoutingPositiveTestCase() + : TestCase("Check positive priorities") + {} + void + Ipv6ListRoutingPositiveTestCase::DoRun (void) + { + Ptr lr = CreateObject (); + Ptr aRouting = CreateObject (); + Ptr bRouting = CreateObject (); + // The Ipv6ARouting should be added with higher priority (larger integer + // value) and will be fetched first below + lr->AddRoutingProtocol (aRouting, 10); + lr->AddRoutingProtocol (bRouting, 5); + int16_t first = 3; + int16_t second = 3; + uint32_t num = lr->GetNRoutingProtocols (); + NS_TEST_ASSERT_MSG_EQ (num, 2, "XXX"); + Ptr firstRp = lr->GetRoutingProtocol (0, first); + NS_TEST_ASSERT_MSG_EQ (10, first, "XXX"); + NS_TEST_ASSERT_MSG_EQ (firstRp, aRouting, "XXX"); + Ptr secondRp = lr->GetRoutingProtocol (1, second); + NS_TEST_ASSERT_MSG_EQ (5, second, "XXX"); + NS_TEST_ASSERT_MSG_EQ (secondRp, bRouting, "XXX"); + } + + static class Ipv6ListRoutingTestSuite : public TestSuite + { + public: + Ipv6ListRoutingTestSuite() + : TestSuite("ipv6-list-routing", UNIT) + { + AddTestCase(new Ipv6ListRoutingPositiveTestCase()); + AddTestCase(new Ipv6ListRoutingNegativeTestCase()); + } + + } g_ipv6ListRoutingTestSuite; + +} // namespace ns3 diff --git a/src/internet/test/ipv6-packet-info-tag-test-suite.cc b/src/internet/test/ipv6-packet-info-tag-test-suite.cc new file mode 100644 index 000000000..ca79e65c0 --- /dev/null +++ b/src/internet/test/ipv6-packet-info-tag-test-suite.cc @@ -0,0 +1,216 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2010 Hajime Tazaki + * + * 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 + * + * Authors: Hajime Tazaki + */ + +//----------------------------------------------------------------------------- +// Unit tests +//----------------------------------------------------------------------------- + +#include "ns3/test.h" +#include "ns3/ipv6-address.h" +#include "ns3/ipv6-packet-info-tag.h" +#include "ns3/log.h" +#include "ns3/abort.h" +#include "ns3/attribute.h" +#include "ns3/simple-net-device.h" +#include "ns3/object-factory.h" +#include "ns3/socket-factory.h" +#include "ns3/udp-socket-factory.h" +#include "ns3/udp-socket.h" +#include "ns3/ipv6-l3-protocol.h" +#include "ns3/ipv6-raw-socket-factory.h" +#include "ns3/ipv6-interface.h" +#include "ns3/icmpv6-l4-protocol.h" +#include "ns3/ipv6-static-routing.h" +#include "ns3/ipv6-list-routing.h" +#include "ns3/inet6-socket-address.h" +#include "ns3/simulator.h" +#include "ns3/uinteger.h" +#include "ns3/boolean.h" +#include "ns3/node.h" + +namespace ns3 { + +static void +AddInternetStack (Ptr node) +{ + Ptr ipv6 = CreateObject (); + Ptr icmpv6 = CreateObject (); + node->AggregateObject (ipv6); + node->AggregateObject (icmpv6); + ipv6->Insert (icmpv6); + icmpv6->SetAttribute ("DAD", BooleanValue (false)); + + //Routing for Ipv6 + Ptr ipv6Routing = CreateObject (); + ipv6->SetRoutingProtocol (ipv6Routing); + Ptr ipv6staticRouting = CreateObject (); + ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0); + + /* register IPv6 extensions and options */ + ipv6->RegisterExtensions (); + ipv6->RegisterOptions (); +} + +class Ipv6PacketInfoTagTest: public TestCase +{ +public: + Ipv6PacketInfoTagTest (); +private: + virtual void DoRun (void); + void RxCb (Ptr socket); + void DoSendData (Ptr socket, std::string to); +}; + +Ipv6PacketInfoTagTest::Ipv6PacketInfoTagTest () + : TestCase ("Ipv6PacketInfoTagTest") +{} + +void +Ipv6PacketInfoTagTest::RxCb (Ptr socket) +{ + uint32_t availableData; + Ptr m_receivedPacket; + + availableData = socket->GetRxAvailable (); + m_receivedPacket = socket->Recv (std::numeric_limits::max(), 0); + NS_ASSERT (availableData == m_receivedPacket->GetSize ()); + + Ipv6PacketInfoTag tag; + bool found; + found = m_receivedPacket->RemovePacketTag (tag); + NS_ASSERT (found); + tag.Print (std::cout); + std::cout << std::endl; +} + +void +Ipv6PacketInfoTagTest::DoSendData (Ptr socket, std::string to) +{ + Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str()), 200); + if (DynamicCast (socket) != 0) + { + NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create (123), 0, realTo), + 123, "XXX"); + } + // Should only Ipv6RawSock + else + { + socket->SendTo (Create (123), 0, realTo); + } +} + +void +Ipv6PacketInfoTagTest::DoRun (void) +{ + Ptr node0 = CreateObject (); + Ptr node1 = CreateObject (); + + Ptr device = CreateObject (); + Ptr device2 = CreateObject (); + + // For Node 0 + node0->AddDevice (device); + AddInternetStack (node0); + Ptr ipv6 = node0->GetObject (); + + uint32_t index = ipv6->AddInterface (device); + Ipv6InterfaceAddress ifaceAddr1 = Ipv6InterfaceAddress (Ipv6Address("2000:1000:0:2000::1"), + Ipv6Prefix(64)); + ipv6->AddAddress (index, ifaceAddr1); + ipv6->SetMetric (index, 1); + ipv6->SetUp (index); + + // For Node 1 + node1->AddDevice (device2); + AddInternetStack (node1); + ipv6 = node1->GetObject (); + + index = ipv6->AddInterface (device2); + Ipv6InterfaceAddress ifaceAddr2 = Ipv6InterfaceAddress (Ipv6Address("2000:1000:0:2000::2"), + Ipv6Prefix(64)); + ipv6->AddAddress (index, ifaceAddr2); + ipv6->SetMetric (index, 1); + ipv6->SetUp (index); + + // ipv6 w rawsocket + Ptr factory = node0->GetObject (Ipv6RawSocketFactory::GetTypeId ()); + Ptr socket = factory->CreateSocket (); + Inet6SocketAddress local = Inet6SocketAddress (Ipv6Address::GetAny (), 0); + socket->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6)); + socket->Bind (local); + socket->SetRecvPktInfo (true); + socket->SetRecvCallback (MakeCallback (&ns3::Ipv6PacketInfoTagTest::RxCb, this)); + + // receive on loopback + Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0), + &Ipv6PacketInfoTagTest::DoSendData, this, socket, "::1"); + Simulator::Run (); + + Ptr factory2 = node1->GetObject (Ipv6RawSocketFactory::GetTypeId ()); + Ptr socket2 = factory2->CreateSocket (); + std::stringstream dst; + dst << ifaceAddr1.GetAddress (); + Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0), + &Ipv6PacketInfoTagTest::DoSendData, this, socket, + dst.str ()); + Simulator::Run (); + +#ifdef UDP6_SUPPORTED + // IPv6 test + factory = node0->GetObject (UdpSocketFactory::GetTypeId ()); + socket = factory->CreateSocket (); + local = Inet6SocketAddress (Ipv6Address::GetAny (), 200); + socket->Bind (local); + socket->SetRecvPktInfo (true); + socket->SetRecvCallback (MakeCallback (&ns3::Ipv6PacketInfoTagTest::RxCb, this)); + + // receive on loopback + Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0), + &Ipv6PacketInfoTagTest::DoSendData, this, socket, "::1"); + Simulator::Run (); + + factory2 = node1->GetObject (UdpSocketFactory::GetTypeId ()); + socket2 = factory2->CreateSocket (); + Simulator::ScheduleWithContext (socket2->GetNode ()->GetId (), Seconds (0), + &Ipv6PacketInfoTagTest::DoSendData, this, socket, "10.1.1.1"); + Simulator::Run (); + +#endif // UDP6_SUPPORTED + + Simulator::Destroy (); + // IPv6 test +} + +static class Ipv6PacketInfoTagTestSuite : public TestSuite +{ +public: + Ipv6PacketInfoTagTestSuite (); +private: +} g_packetinfotagTests; + +Ipv6PacketInfoTagTestSuite::Ipv6PacketInfoTagTestSuite () + : TestSuite ("ipv6-packet-info-tag", UNIT) +{ + AddTestCase (new Ipv6PacketInfoTagTest ()); +} + + +} + diff --git a/src/internet/wscript b/src/internet/wscript index 91e979688..33f377fb0 100644 --- a/src/internet/wscript +++ b/src/internet/wscript @@ -187,9 +187,14 @@ def build(bld): internet_test = bld.create_ns3_module_test_library('internet') internet_test.source = [ + 'test/global-route-manager-impl-test-suite.cc', + 'test/ipv4-list-routing-test-suite.cc', + 'test/ipv4-packet-info-tag-test-suite.cc', 'test/ipv4-raw-test.cc', 'test/ipv4-test.cc', 'test/ipv6-extension-header-test-suite.cc', + 'test/ipv6-list-routing-test-suite.cc', + 'test/ipv6-packet-info-tag-test-suite.cc', 'test/ipv6-test.cc', 'test/tcp-test.cc', 'test/udp-test.cc', @@ -253,6 +258,8 @@ def build(bld): 'helper/ipv6-static-routing-helper.h', 'model/global-router-interface.h', 'model/global-route-manager.h', + 'model/global-route-manager-impl.h', + 'model/candidate-queue.h', 'model/ipv4-global-routing.h', 'helper/ipv4-global-routing-helper.h', 'helper/internet-stack-helper.h',