A Discrete-Event Network Simulator
API
ipv6-address-duplication-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 Universita' di Firenze
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  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 #include "ns3/test.h"
22 #include "ns3/socket-factory.h"
23 #include "ns3/udp-socket-factory.h"
24 #include "ns3/simulator.h"
25 #include "ns3/simple-channel.h"
26 #include "ns3/simple-net-device.h"
27 #include "ns3/simple-net-device-helper.h"
28 #include "ns3/socket.h"
29 #include "ns3/boolean.h"
30 
31 #include "ns3/log.h"
32 #include "ns3/node.h"
33 #include "ns3/inet6-socket-address.h"
34 
35 #include "ns3/ipv6-l3-protocol.h"
36 #include "ns3/icmpv6-l4-protocol.h"
37 #include "ns3/udp-l4-protocol.h"
38 #include "ns3/ipv6-static-routing.h"
39 #include "ns3/internet-stack-helper.h"
40 #include "ns3/ipv6-address-helper.h"
41 #include "ns3/ipv6-routing-helper.h"
42 
43 #include <string>
44 #include <limits>
45 
46 using namespace ns3;
47 
54 class Ipv6DadTest : public TestCase
55 {
56 public:
57  virtual void DoRun (void);
58  Ipv6DadTest ();
59 };
60 
62  : TestCase ("IPv6 Duplicate Address Detection")
63 {
64 }
65 
66 void
68 {
69  // Create topology
70 
71  Ptr<Node> Node1 = CreateObject<Node> ();
72  Ptr<Node> Node2 = CreateObject<Node> ();
73 
74  NodeContainer nodes (Node1, Node2);
75 
76  SimpleNetDeviceHelper helperChannel;
77  helperChannel.SetNetDevicePointToPointMode (true);
78  NetDeviceContainer net = helperChannel.Install (nodes);
79 
80  InternetStackHelper internetv6;
81  internetv6.Install (nodes);
82 
83  Ipv6AddressHelper ipv6helper;
84  Ipv6InterfaceContainer iic = ipv6helper.AssignWithoutAddress (net);
85 
86  Ptr<NetDevice> device;
87  Ipv6InterfaceAddress ipv6Addr;
88 
89  Ptr<Ipv6> ipv6node1;
90  int32_t ifIndexNode1;
91  ipv6node1 = Node1->GetObject<Ipv6> ();
92  device = net.Get (0);
93  ifIndexNode1 = ipv6node1->GetInterfaceForDevice (device);
94  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:1::1"), Ipv6Prefix (64));
95  ipv6node1->AddAddress (ifIndexNode1, ipv6Addr);
96 
97  Ptr<Ipv6> ipv6node2;
98  int32_t ifIndexNode2;
99  ipv6node2 = Node2->GetObject<Ipv6> ();
100  device = net.Get (1);
101  ifIndexNode2 = ipv6node2->GetInterfaceForDevice (device);
102  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:1::1"), Ipv6Prefix (64));
103  ipv6node2->AddAddress (ifIndexNode2, ipv6Addr);
104 
105  Simulator::Run ();
106 
107  Ipv6InterfaceAddress addr1 = ipv6node1->GetAddress (ifIndexNode1, 1);
108  Ipv6InterfaceAddress::State_e node1AddrState = addr1.GetState();
109  NS_TEST_ASSERT_MSG_EQ (node1AddrState, Ipv6InterfaceAddress::INVALID, "Failed to detect duplicate address on node 1");
110 
111  Ipv6InterfaceAddress addr2 = ipv6node2->GetAddress (ifIndexNode2, 1);
112  Ipv6InterfaceAddress::State_e node2AddrState = addr2.GetState();
113  NS_TEST_ASSERT_MSG_EQ (node2AddrState, Ipv6InterfaceAddress::INVALID, "Failed to detect duplicate address on node 2");
114 
115  Simulator::Destroy ();
116 }
117 
118 
126 {
127 public:
128  Ipv6DadTestSuite () : TestSuite ("ipv6-duplicate-address-detection", UNIT)
129  {
130  AddTestCase (new Ipv6DadTest, TestCase::QUICK);
131  }
132 };
133 
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
ns3::Ipv6InterfaceAddress
IPv6 address associated with an interface.
Definition: ipv6-interface-address.h:38
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
g_ipv6dadTestSuite
static Ipv6DadTestSuite g_ipv6dadTestSuite
Static variable for test initialization.
Definition: ipv6-address-duplication-test.cc:134
ns3::Object::GetObject
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
ns3::Ipv6AddressHelper::AssignWithoutAddress
Ipv6InterfaceContainer AssignWithoutAddress(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer but do not assign any IPv6 addresses.
Definition: ipv6-address-helper.cc:293
ns3::Ipv6::GetInterfaceForDevice
virtual int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const =0
Get the interface index of the specified NetDevice.
ns3::Ipv6Address
Describes an IPv6 address.
Definition: ipv6-address.h:50
ns3::Ipv6InterfaceAddress::State_e
State_e
State of an address associated with an interface.
Definition: ipv6-interface-address.h:45
ns3::Ipv6AddressHelper
Helper class to auto-assign global IPv6 unicast addresses.
Definition: ipv6-address-helper.h:83
ns3::Ipv6::AddAddress
virtual bool AddAddress(uint32_t interface, Ipv6InterfaceAddress address, bool addOnLinkRoute=true)=0
Add an address on the specified IPv6 interface.
Ipv6DadTest
IPv6 Duplicate Address Detection Test.
Definition: ipv6-address-duplication-test.cc:55
first.nodes
nodes
Definition: first.py:32
ns3::TestCase
encapsulates test code
Definition: test.h:1154
ns3::Ptr< Node >
Ipv6DadTestSuite
IPv6 Duplicate Address Detection TestSuite.
Definition: ipv6-address-duplication-test.cc:126
Ipv6DadTest::Ipv6DadTest
Ipv6DadTest()
Definition: ipv6-address-duplication-test.cc:61
ns3::InternetStackHelper::Install
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Definition: internet-stack-helper.cc:366
Ipv6DadTestSuite::Ipv6DadTestSuite
Ipv6DadTestSuite()
Definition: ipv6-address-duplication-test.cc:128
ns3::Ipv6InterfaceContainer
Keep track of a set of IPv6 interfaces.
Definition: ipv6-interface-container.h:42
ns3::SimpleNetDeviceHelper
build a set of SimpleNetDevice objects
Definition: simple-net-device-helper.h:37
ns3::SimpleNetDeviceHelper::Install
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
Definition: simple-net-device-helper.cc:100
Ipv6DadTest::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: ipv6-address-duplication-test.cc:67
ns3::aodv::INVALID
@ INVALID
INVALID.
Definition: aodv-rtable.h:51
ns3::TestSuite
A suite of tests to run.
Definition: test.h:1344
NS_TEST_ASSERT_MSG_EQ
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:166
ns3::TestSuite::UNIT
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1353
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::Ipv6
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:82
ns3::Ipv6InterfaceAddress::GetState
Ipv6InterfaceAddress::State_e GetState() const
Get the address state.
Definition: ipv6-interface-address.cc:136
ns3::Ipv6Prefix
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::NetDeviceContainer::Get
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Definition: net-device-container.cc:62
ns3::SimpleNetDeviceHelper::SetNetDevicePointToPointMode
void SetNetDevicePointToPointMode(bool pointToPointMode)
SimpleNetDevice is Broadcast capable and ARP needing.
Definition: simple-net-device-helper.cc:94
ns3::Ipv6::GetAddress
virtual Ipv6InterfaceAddress GetAddress(uint32_t interface, uint32_t addressIndex) const =0
Get IPv6 address on specified IPv6 interface.