A Discrete-Event Network Simulator
API
ipv6-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 Strasbourg University
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Authors: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
18  * Faker Moatamri <faker.moatamri@sophia.inria.fr>
19  */
20 #include "ns3/boolean.h"
21 #include "ns3/icmpv6-l4-protocol.h"
22 #include "ns3/inet6-socket-address.h"
23 #include "ns3/ipv6-interface.h"
24 #include "ns3/ipv6-l3-protocol.h"
25 #include "ns3/log.h"
26 #include "ns3/node.h"
27 #include "ns3/simple-net-device.h"
28 #include "ns3/simulator.h"
29 #include "ns3/test.h"
30 
31 using namespace ns3;
32 
39 {
40  public:
42 
43  ~Ipv6L3ProtocolTestCase() override;
44  void DoRun() override;
45 };
46 
48  : TestCase("Verify the IPv6 layer 3 protocol")
49 {
50 }
51 
53 {
54 }
55 
56 void
58 {
59  Ptr<Node> node = CreateObject<Node>();
60  Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol>();
61  Ptr<Icmpv6L4Protocol> icmpv6 = CreateObject<Icmpv6L4Protocol>();
63  Ptr<Ipv6Interface> interface2 = CreateObject<Ipv6Interface>();
64  Ptr<SimpleNetDevice> device = CreateObject<SimpleNetDevice>();
65  Ptr<SimpleNetDevice> device2 = CreateObject<SimpleNetDevice>();
66  uint32_t index = 0;
67 
68  /* init */
69  icmpv6->SetAttribute("DAD", BooleanValue(false));
70  node->AggregateObject(ipv6);
71  node->AggregateObject(icmpv6);
72  ipv6->Insert(icmpv6);
73 
74  /* first real interface (loopback is also installed) */
75  node->AddDevice(device);
76  interface->SetDevice(device);
77  interface->SetNode(node);
78  index = ipv6->AddIpv6Interface(interface);
79  NS_TEST_ASSERT_MSG_EQ(index, 1, "The index is not 1??");
80 
81  /* second interface */
82  node->AddDevice(device2);
83  interface2->SetDevice(device2);
84  interface2->SetNode(node);
85  index = ipv6->AddIpv6Interface(interface2);
86  NS_TEST_ASSERT_MSG_EQ(index, 2, "The index is not 2??");
87 
88  interface->SetUp();
89  interface2->SetUp();
90 
91  Ipv6InterfaceAddress ifaceAddr = interface->GetLinkLocalAddress();
92  NS_TEST_ASSERT_MSG_EQ(ifaceAddr.GetAddress().IsLinkLocal(), true, "Should be link local??");
93 
94  NS_TEST_ASSERT_MSG_EQ(interface->GetNAddresses(),
95  1,
96  "interface has always a link-local address"); /* interface has always a
97  link-local address */
98 
99  Ipv6InterfaceAddress ifaceAddr1 =
100  Ipv6InterfaceAddress("2001:1234:5678:9000::1", Ipv6Prefix(64));
101  interface->AddAddress(ifaceAddr1);
102  Ipv6InterfaceAddress ifaceAddr2 =
103  Ipv6InterfaceAddress("2001:ffff:5678:9000::1", Ipv6Prefix(64));
104  interface->AddAddress(ifaceAddr2);
105 
106  Ipv6InterfaceAddress ifaceAddr3 =
107  Ipv6InterfaceAddress("2001:ffff:5678:9001::2", Ipv6Prefix(64));
108  interface2->AddAddress(ifaceAddr3);
109 
110  uint32_t num = interface->GetNAddresses();
112  num,
113  3,
114  "Number of addresses should be 3??"); /* 2 global addresses + link-local ones */
115 
116  num = interface2->GetNAddresses();
118  num,
119  2,
120  "1 global addresses + link-local ones"); /* 1 global addresses + link-local ones */
121 
122  interface->RemoveAddress(2);
123  num = interface->GetNAddresses();
124  NS_TEST_ASSERT_MSG_EQ(num, 2, "Number of addresses should be 2??");
125 
126  Ipv6InterfaceAddress output = interface->GetAddress(1);
127  NS_TEST_ASSERT_MSG_EQ(ifaceAddr1, output, "Should be the interface address 1?");
128 
129  index = ipv6->GetInterfaceForPrefix("2001:1234:5678:9000::0", Ipv6Prefix(64));
130  NS_TEST_ASSERT_MSG_EQ(index,
131  1,
132  "We should get one address??"); /* link-local address is always index 0 */
133 
134  index = ipv6->GetInterfaceForAddress("2001:ffff:5678:9001::2");
135  NS_TEST_ASSERT_MSG_EQ(index, 2, "Number of addresses should be 2??");
136 
137  index = ipv6->GetInterfaceForAddress("2001:ffff:5678:9000::1"); /* address we just remove */
138  NS_TEST_ASSERT_MSG_EQ(index, (uint32_t)-1, "Address should not be found??");
139 
140  /* Test Ipv6Interface()::RemoveAddress(address) */
141  output = interface->RemoveAddress(Ipv6Address("2001:1234:5678:9000::1"));
142  NS_TEST_ASSERT_MSG_EQ(ifaceAddr1, output, "Wrong Interface Address Removed??");
143  num = interface->GetNAddresses();
144  NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
145 
146  /* Remove a non-existent Address */
147  output = interface->RemoveAddress(Ipv6Address("2001:1234:5678:9000::1"));
148  NS_TEST_ASSERT_MSG_EQ(Ipv6InterfaceAddress(), output, "Removed non-existent address??");
149  num = interface->GetNAddresses();
150  NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
151 
152  /* Remove a loopback Address */
153  output = interface->RemoveAddress(Ipv6Address::GetLoopback());
154  NS_TEST_ASSERT_MSG_EQ(Ipv6InterfaceAddress(), output, "Able to remove loopback address??");
155  num = interface->GetNAddresses();
156  NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
157 
158  /* Test Ipv6Address::RemoveAddress(index, address) */
159  index = ipv6->GetInterfaceForAddress("2001:ffff:5678:9001::2");
160  bool result = ipv6->RemoveAddress(index, Ipv6Address("2001:ffff:5678:9001::2"));
161  NS_TEST_ASSERT_MSG_EQ(result, true, "Unable to remove Address??");
162  num = interface2->GetNAddresses();
163  NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
164 
165  /* Remove a non-existent Address */
166  result = ipv6->RemoveAddress(index, Ipv6Address("2001:ffff:5678:9001::2"));
167  NS_TEST_ASSERT_MSG_EQ(result, false, "Removed Non-existent address??");
168  num = interface2->GetNAddresses();
169  NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
170 
171  /* Remove a loopback Address */
172  result = ipv6->RemoveAddress(index, Ipv6Address::GetLoopback());
173  NS_TEST_ASSERT_MSG_EQ(result, false, "Able to remove loopback address??");
174  num = interface2->GetNAddresses();
175  NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
176 
177  Simulator::Destroy();
178 } // end DoRun
179 
186 {
187  public:
189  : TestSuite("ipv6-protocol", UNIT)
190  {
191  AddTestCase(new Ipv6L3ProtocolTestCase(), TestCase::QUICK);
192  }
193 };
194 
IPv6 TestSuite.
Definition: ipv6-test.cc:186
void DoRun() override
Implementation to actually run this TestCase.
Definition: ipv6-test.cc:57
~Ipv6L3ProtocolTestCase() override
Definition: ipv6-test.cc:52
Describes an IPv6 address.
Definition: ipv6-address.h:49
bool IsLinkLocal() const
If the IPv6 address is a link-local address (fe80::/64).
IPv6 address associated with an interface.
Ipv6Address GetAddress() const
Get the IPv6 address.
The IPv6 representation of a network interface.
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:259
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:579
#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:144
static IPv6L3ProtocolTestSuite g_ipv6protocolTestSuite
Static variable for test initialization.
Definition: ipv6-test.cc:195
Every class exported by the ns3 library is enclosed in the ns3 namespace.