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
31using namespace ns3;
32
40{
41 public:
43
44 ~Ipv6L3ProtocolTestCase() override;
45 void DoRun() override;
46};
47
49 : TestCase("Verify the IPv6 layer 3 protocol")
50{
51}
52
54{
55}
56
57void
59{
60 Ptr<Node> node = CreateObject<Node>();
61 Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol>();
62 Ptr<Icmpv6L4Protocol> icmpv6 = CreateObject<Icmpv6L4Protocol>();
64 Ptr<Ipv6Interface> interface2 = CreateObject<Ipv6Interface>();
65 Ptr<SimpleNetDevice> device = CreateObject<SimpleNetDevice>();
66 Ptr<SimpleNetDevice> device2 = CreateObject<SimpleNetDevice>();
67 uint32_t index = 0;
68
69 /* init */
70 icmpv6->SetAttribute("DAD", BooleanValue(false));
71 node->AggregateObject(ipv6);
72 node->AggregateObject(icmpv6);
73 ipv6->Insert(icmpv6);
74
75 /* first real interface (loopback is also installed) */
76 node->AddDevice(device);
77 interface->SetDevice(device);
78 interface->SetNode(node);
79 index = ipv6->AddIpv6Interface(interface);
80 NS_TEST_ASSERT_MSG_EQ(index, 1, "The index is not 1??");
81
82 /* second interface */
83 node->AddDevice(device2);
84 interface2->SetDevice(device2);
85 interface2->SetNode(node);
86 index = ipv6->AddIpv6Interface(interface2);
87 NS_TEST_ASSERT_MSG_EQ(index, 2, "The index is not 2??");
88
89 interface->SetUp();
90 interface2->SetUp();
91
92 Ipv6InterfaceAddress ifaceAddr = interface->GetLinkLocalAddress();
93 NS_TEST_ASSERT_MSG_EQ(ifaceAddr.GetAddress().IsLinkLocal(), true, "Should be link local??");
94
95 NS_TEST_ASSERT_MSG_EQ(interface->GetNAddresses(),
96 1,
97 "interface has always a link-local address"); /* interface has always a
98 link-local address */
99
100 Ipv6InterfaceAddress ifaceAddr1 =
101 Ipv6InterfaceAddress("2001:1234:5678:9000::1", Ipv6Prefix(64));
102 interface->AddAddress(ifaceAddr1);
103 Ipv6InterfaceAddress ifaceAddr2 =
104 Ipv6InterfaceAddress("2001:ffff:5678:9000::1", Ipv6Prefix(64));
105 interface->AddAddress(ifaceAddr2);
106
107 Ipv6InterfaceAddress ifaceAddr3 =
108 Ipv6InterfaceAddress("2001:ffff:5678:9001::2", Ipv6Prefix(64));
109 interface2->AddAddress(ifaceAddr3);
110
111 uint32_t num = interface->GetNAddresses();
113 num,
114 3,
115 "Number of addresses should be 3??"); /* 2 global addresses + link-local ones */
116
117 num = interface2->GetNAddresses();
119 num,
120 2,
121 "1 global addresses + link-local ones"); /* 1 global addresses + link-local ones */
122
123 interface->RemoveAddress(2);
124 num = interface->GetNAddresses();
125 NS_TEST_ASSERT_MSG_EQ(num, 2, "Number of addresses should be 2??");
126
127 Ipv6InterfaceAddress output = interface->GetAddress(1);
128 NS_TEST_ASSERT_MSG_EQ(ifaceAddr1, output, "Should be the interface address 1?");
129
130 index = ipv6->GetInterfaceForPrefix("2001:1234:5678:9000::0", Ipv6Prefix(64));
132 1,
133 "We should get one address??"); /* link-local address is always index 0 */
134
135 index = ipv6->GetInterfaceForAddress("2001:ffff:5678:9001::2");
136 NS_TEST_ASSERT_MSG_EQ(index, 2, "Number of addresses should be 2??");
137
138 index = ipv6->GetInterfaceForAddress("2001:ffff:5678:9000::1"); /* address we just remove */
139 NS_TEST_ASSERT_MSG_EQ(index, (uint32_t)-1, "Address should not be found??");
140
141 /* Test Ipv6Interface()::RemoveAddress(address) */
142 output = interface->RemoveAddress(Ipv6Address("2001:1234:5678:9000::1"));
143 NS_TEST_ASSERT_MSG_EQ(ifaceAddr1, output, "Wrong Interface Address Removed??");
144 num = interface->GetNAddresses();
145 NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
146
147 /* Remove a non-existent Address */
148 output = interface->RemoveAddress(Ipv6Address("2001:1234:5678:9000::1"));
149 NS_TEST_ASSERT_MSG_EQ(Ipv6InterfaceAddress(), output, "Removed non-existent address??");
150 num = interface->GetNAddresses();
151 NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
152
153 /* Remove a loopback Address */
154 output = interface->RemoveAddress(Ipv6Address::GetLoopback());
155 NS_TEST_ASSERT_MSG_EQ(Ipv6InterfaceAddress(), output, "Able to remove loopback address??");
156 num = interface->GetNAddresses();
157 NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
158
159 /* Test Ipv6Address::RemoveAddress(index, address) */
160 index = ipv6->GetInterfaceForAddress("2001:ffff:5678:9001::2");
161 bool result = ipv6->RemoveAddress(index, Ipv6Address("2001:ffff:5678:9001::2"));
162 NS_TEST_ASSERT_MSG_EQ(result, true, "Unable to remove Address??");
163 num = interface2->GetNAddresses();
164 NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
165
166 /* Remove a non-existent Address */
167 result = ipv6->RemoveAddress(index, Ipv6Address("2001:ffff:5678:9001::2"));
168 NS_TEST_ASSERT_MSG_EQ(result, false, "Removed Non-existent address??");
169 num = interface2->GetNAddresses();
170 NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
171
172 /* Remove a loopback Address */
173 result = ipv6->RemoveAddress(index, Ipv6Address::GetLoopback());
174 NS_TEST_ASSERT_MSG_EQ(result, false, "Able to remove loopback address??");
175 num = interface2->GetNAddresses();
176 NS_TEST_ASSERT_MSG_EQ(num, 1, "Number of addresses should be 1??");
177
178 Simulator::Destroy();
179} // end DoRun
180
188{
189 public:
191 : TestSuite("ipv6-protocol", UNIT)
192 {
193 AddTestCase(new Ipv6L3ProtocolTestCase(), TestCase::QUICK);
194 }
195};
196
IPv6 TestSuite.
Definition: ipv6-test.cc:188
void DoRun() override
Implementation to actually run this TestCase.
Definition: ipv6-test.cc:58
~Ipv6L3ProtocolTestCase() override
Definition: ipv6-test.cc:53
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Describes an IPv6 address.
Definition: ipv6-address.h:50
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:456
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:305
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:197
Every class exported by the ns3 library is enclosed in the ns3 namespace.