A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
20
using namespace
ns3
;
21
22
/**
23
* @ingroup internet-test
24
*
25
* @brief IPv6 Test
26
*/
27
class
Ipv6L3ProtocolTestCase
:
public
TestCase
28
{
29
public
:
30
Ipv6L3ProtocolTestCase
();
31
32
~Ipv6L3ProtocolTestCase
()
override
;
33
void
DoRun
()
override
;
34
};
35
36
Ipv6L3ProtocolTestCase::Ipv6L3ProtocolTestCase
()
37
:
TestCase
(
"Verify the IPv6 layer 3 protocol"
)
38
{
39
}
40
41
Ipv6L3ProtocolTestCase::~Ipv6L3ProtocolTestCase
()
42
{
43
}
44
45
void
46
Ipv6L3ProtocolTestCase::DoRun
()
47
{
48
Ptr<Node>
node =
CreateObject<Node>
();
49
Ptr<Ipv6L3Protocol>
ipv6 =
CreateObject<Ipv6L3Protocol>
();
50
Ptr<Icmpv6L4Protocol>
icmpv6 =
CreateObject<Icmpv6L4Protocol>
();
51
Ptr<Ipv6Interface>
interface
=
CreateObject
<
Ipv6Interface
>();
52
Ptr<Ipv6Interface>
interface2
=
CreateObject<Ipv6Interface>
();
53
Ptr<SimpleNetDevice>
device =
CreateObject<SimpleNetDevice>
();
54
Ptr<SimpleNetDevice>
device2
=
CreateObject<SimpleNetDevice>
();
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();
100
NS_TEST_ASSERT_MSG_EQ
(
101
num,
102
3,
103
"Number of addresses should be 3??"
);
/* 2 global addresses + link-local ones */
104
105
num =
interface2
->GetNAddresses();
106
NS_TEST_ASSERT_MSG_EQ
(
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));
119
NS_TEST_ASSERT_MSG_EQ
(index,
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
166
Simulator::Destroy
();
167
}
// end DoRun
168
169
/**
170
* @ingroup internet-test
171
*
172
* @brief IPv6 TestSuite
173
*/
174
class
IPv6L3ProtocolTestSuite
:
public
TestSuite
175
{
176
public
:
177
IPv6L3ProtocolTestSuite
()
178
:
TestSuite
(
"ipv6-protocol"
,
Type
::
UNIT
)
179
{
180
AddTestCase
(
new
Ipv6L3ProtocolTestCase
(), TestCase::Duration::QUICK);
181
}
182
};
183
184
static
IPv6L3ProtocolTestSuite
g_ipv6protocolTestSuite
;
//!< Static variable for test initialization
IPv6L3ProtocolTestSuite
IPv6 TestSuite.
Definition
ipv6-test.cc:175
IPv6L3ProtocolTestSuite::IPv6L3ProtocolTestSuite
IPv6L3ProtocolTestSuite()
Definition
ipv6-test.cc:177
Ipv6L3ProtocolTestCase
IPv6 Test.
Definition
ipv6-test.cc:28
Ipv6L3ProtocolTestCase::Ipv6L3ProtocolTestCase
Ipv6L3ProtocolTestCase()
Definition
ipv6-test.cc:36
Ipv6L3ProtocolTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
ipv6-test.cc:46
Ipv6L3ProtocolTestCase::~Ipv6L3ProtocolTestCase
~Ipv6L3ProtocolTestCase() override
Definition
ipv6-test.cc:41
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition
boolean.h:26
ns3::Ipv6Address
Describes an IPv6 address.
Definition
ipv6-address.h:38
ns3::Ipv6Address::IsLinkLocal
bool IsLinkLocal() const
If the IPv6 address is a link-local address (fe80::/64).
Definition
ipv6-address.cc:751
ns3::Ipv6Address::GetLoopback
static Ipv6Address GetLoopback()
Get the loopback address.
Definition
ipv6-address.cc:712
ns3::Ipv6InterfaceAddress
IPv6 address associated with an interface.
Definition
ipv6-interface-address.h:26
ns3::Ipv6InterfaceAddress::GetAddress
Ipv6Address GetAddress() const
Get the IPv6 address.
Definition
ipv6-interface-address.cc:78
ns3::Ipv6Interface
The IPv6 representation of a network interface.
Definition
ipv6-interface.h:42
ns3::Ipv6Prefix
Describes an IPv6 prefix.
Definition
ipv6-address.h:444
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
ptr.h:66
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1274
ns3::TestSuite::UNIT
static constexpr auto UNIT
Definition
test.h:1291
uint32_t
ns3::CreateObject
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition
object.h:619
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
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:134
g_ipv6protocolTestSuite
static IPv6L3ProtocolTestSuite g_ipv6protocolTestSuite
Static variable for test initialization.
Definition
ipv6-test.cc:184
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
internet
test
ipv6-test.cc
Generated on Mon Dec 15 2025 15:21:54 for ns-3 by
1.9.8