A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv6-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 Strasbourg University
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
7 * Faker Moatamri <faker.moatamri@sophia.inria.fr>
8 */
9#include "ns3/boolean.h"
10#include "ns3/icmpv6-l4-protocol.h"
11#include "ns3/inet6-socket-address.h"
12#include "ns3/ipv6-interface.h"
13#include "ns3/ipv6-l3-protocol.h"
14#include "ns3/log.h"
15#include "ns3/node.h"
16#include "ns3/simple-net-device.h"
17#include "ns3/simulator.h"
18#include "ns3/test.h"
19
20using namespace ns3;
21
22/**
23 * \ingroup internet-test
24 *
25 * \brief IPv6 Test
26 */
28{
29 public:
31
32 ~Ipv6L3ProtocolTestCase() override;
33 void DoRun() override;
34};
35
37 : TestCase("Verify the IPv6 layer 3 protocol")
38{
39}
40
44
45void
47{
55 uint32_t index = 0;
56
57 /* init */
58 icmpv6->SetAttribute("DAD", BooleanValue(false));
59 node->AggregateObject(ipv6);
60 node->AggregateObject(icmpv6);
61 ipv6->Insert(icmpv6);
62
63 /* first real interface (loopback is also installed) */
64 node->AddDevice(device);
65 interface->SetDevice(device);
66 interface->SetNode(node);
67 index = ipv6->AddIpv6Interface(interface);
68 NS_TEST_ASSERT_MSG_EQ(index, 1, "The index is not 1??");
69
70 /* second interface */
71 node->AddDevice(device2);
72 interface2->SetDevice(device2);
73 interface2->SetNode(node);
74 index = ipv6->AddIpv6Interface(interface2);
75 NS_TEST_ASSERT_MSG_EQ(index, 2, "The index is not 2??");
76
77 interface->SetUp();
78 interface2->SetUp();
79
80 Ipv6InterfaceAddress ifaceAddr = interface->GetLinkLocalAddress();
81 NS_TEST_ASSERT_MSG_EQ(ifaceAddr.GetAddress().IsLinkLocal(), true, "Should be link local??");
82
83 NS_TEST_ASSERT_MSG_EQ(interface->GetNAddresses(),
84 1,
85 "interface has always a link-local address"); /* interface has always a
86 link-local address */
87
88 Ipv6InterfaceAddress ifaceAddr1 =
89 Ipv6InterfaceAddress("2001:1234:5678:9000::1", Ipv6Prefix(64));
90 interface->AddAddress(ifaceAddr1);
91 Ipv6InterfaceAddress ifaceAddr2 =
92 Ipv6InterfaceAddress("2001:ffff:5678:9000::1", Ipv6Prefix(64));
93 interface->AddAddress(ifaceAddr2);
94
95 Ipv6InterfaceAddress ifaceAddr3 =
96 Ipv6InterfaceAddress("2001:ffff:5678:9001::2", Ipv6Prefix(64));
97 interface2->AddAddress(ifaceAddr3);
98
99 uint32_t num = interface->GetNAddresses();
101 num,
102 3,
103 "Number of addresses should be 3??"); /* 2 global addresses + link-local ones */
104
105 num = interface2->GetNAddresses();
107 num,
108 2,
109 "1 global addresses + link-local ones"); /* 1 global addresses + link-local ones */
110
111 interface->RemoveAddress(2);
112 num = interface->GetNAddresses();
113 NS_TEST_ASSERT_MSG_EQ(num, 2, "Number of addresses should be 2??");
114
115 Ipv6InterfaceAddress output = interface->GetAddress(1);
116 NS_TEST_ASSERT_MSG_EQ(ifaceAddr1, output, "Should be the interface address 1?");
117
118 index = ipv6->GetInterfaceForPrefix("2001:1234:5678:9000::0", Ipv6Prefix(64));
120 1,
121 "We should get one address??"); /* link-local address is always index 0 */
122
123 index = ipv6->GetInterfaceForAddress("2001:ffff:5678:9001::2");
124 NS_TEST_ASSERT_MSG_EQ(index, 2, "Number of addresses should be 2??");
125
126 index = ipv6->GetInterfaceForAddress("2001:ffff:5678:9000::1"); /* address we just remove */
127 NS_TEST_ASSERT_MSG_EQ(index, (uint32_t)-1, "Address should not be found??");
128
129 /* Test Ipv6Interface()::RemoveAddress(address) */
130 output = interface->RemoveAddress(Ipv6Address("2001:1234:5678:9000::1"));
131 NS_TEST_ASSERT_MSG_EQ(ifaceAddr1, output, "Wrong Interface Address Removed??");
132 num = interface->GetNAddresses();
133 NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
134
135 /* Remove a non-existent Address */
136 output = interface->RemoveAddress(Ipv6Address("2001:1234:5678:9000::1"));
137 NS_TEST_ASSERT_MSG_EQ(Ipv6InterfaceAddress(), output, "Removed non-existent address??");
138 num = interface->GetNAddresses();
139 NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
140
141 /* Remove a loopback Address */
142 output = interface->RemoveAddress(Ipv6Address::GetLoopback());
143 NS_TEST_ASSERT_MSG_EQ(Ipv6InterfaceAddress(), output, "Able to remove loopback address??");
144 num = interface->GetNAddresses();
145 NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
146
147 /* Test Ipv6Address::RemoveAddress(index, address) */
148 index = ipv6->GetInterfaceForAddress("2001:ffff:5678:9001::2");
149 bool result = ipv6->RemoveAddress(index, Ipv6Address("2001:ffff:5678:9001::2"));
150 NS_TEST_ASSERT_MSG_EQ(result, true, "Unable to remove Address??");
151 num = interface2->GetNAddresses();
152 NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
153
154 /* Remove a non-existent Address */
155 result = ipv6->RemoveAddress(index, Ipv6Address("2001:ffff:5678:9001::2"));
156 NS_TEST_ASSERT_MSG_EQ(result, false, "Removed Non-existent address??");
157 num = interface2->GetNAddresses();
158 NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
159
160 /* Remove a loopback Address */
161 result = ipv6->RemoveAddress(index, Ipv6Address::GetLoopback());
162 NS_TEST_ASSERT_MSG_EQ(result, false, "Able to remove loopback address??");
163 num = interface2->GetNAddresses();
164 NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
165
167} // end DoRun
168
169/**
170 * \ingroup internet-test
171 *
172 * \brief IPv6 TestSuite
173 */
175{
176 public:
178 : TestSuite("ipv6-protocol", Type::UNIT)
179 {
180 AddTestCase(new Ipv6L3ProtocolTestCase(), TestCase::Duration::QUICK);
181 }
182};
183
184static IPv6L3ProtocolTestSuite g_ipv6protocolTestSuite; //!< Static variable for test initialization
IPv6 TestSuite.
Definition ipv6-test.cc:175
void DoRun() override
Implementation to actually run this TestCase.
Definition ipv6-test.cc:46
~Ipv6L3ProtocolTestCase() override
Definition ipv6-test.cc:41
Describes an IPv6 address.
bool IsLinkLocal() const
If the IPv6 address is a link-local address (fe80::/64).
static Ipv6Address GetLoopback()
Get the loopback address.
IPv6 address associated with an interface.
Ipv6Address GetAddress() const
Get the IPv6 address.
The IPv6 representation of a network interface.
Describes an IPv6 prefix.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static constexpr auto UNIT
Definition test.h:1291
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:134
static IPv6L3ProtocolTestSuite g_ipv6protocolTestSuite
Static variable for test initialization.
Definition ipv6-test.cc:184
Every class exported by the ns3 library is enclosed in the ns3 namespace.