A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dhcp6-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 NITK Surathkal
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Kavya Bhat <kavyabhat@gmail.com>
7 *
8 */
9
10#include "ns3/applications-module.h"
11#include "ns3/core-module.h"
12#include "ns3/csma-module.h"
13#include "ns3/data-rate.h"
14#include "ns3/dhcp6-header.h"
15#include "ns3/dhcp6-helper.h"
16#include "ns3/header-serialization-test.h"
17#include "ns3/internet-apps-module.h"
18#include "ns3/internet-module.h"
19#include "ns3/internet-stack-helper.h"
20#include "ns3/ipv6-address-helper.h"
21#include "ns3/log.h"
22#include "ns3/mobility-module.h"
23#include "ns3/network-module.h"
24#include "ns3/ping-helper.h"
25#include "ns3/point-to-point-module.h"
26#include "ns3/simple-net-device-helper.h"
27#include "ns3/simple-net-device.h"
28#include "ns3/simulator.h"
29#include "ns3/ssid.h"
30#include "ns3/test.h"
31#include "ns3/trace-helper.h"
32
33using namespace ns3;
34
35/**
36 * @ingroup dhcp6
37 * @defgroup dhcp6-test DHCPv6 module tests
38 */
39
40/**
41 * @ingroup dhcp6-test
42 * @ingroup tests
43 *
44 * @brief DHCPv6 header tests
45 */
46class Dhcp6TestCase : public TestCase
47{
48 public:
50 ~Dhcp6TestCase() override;
51
52 /**
53 * Triggered by an address lease on a client.
54 * @param context The test name.
55 * @param newAddress The leased address.
56 */
57 void LeaseObtained(std::string context, const Ipv6Address& newAddress);
58
59 private:
60 void DoRun() override;
61 Ipv6Address m_leasedAddress[2]; //!< Address given to the nodes
62};
63
65 : TestCase("Dhcp6 test case ")
66{
67}
68
72
73void
74Dhcp6TestCase::LeaseObtained(std::string context, const Ipv6Address& newAddress)
75{
76 uint8_t numericalContext = std::stoi(context, nullptr, 10);
77
78 if (numericalContext >= 0 && numericalContext < std::size(m_leasedAddress))
79 {
80 m_leasedAddress[numericalContext] = newAddress;
81 }
82}
83
84void
86{
87 NodeContainer nonRouterNodes;
88 nonRouterNodes.Create(3);
90 NodeContainer all(nonRouterNodes, router);
91
92 SimpleNetDeviceHelper simpleNetDevice;
93 simpleNetDevice.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
94 simpleNetDevice.SetDeviceAttribute("DataRate", DataRateValue(DataRate("5Mbps")));
95 NetDeviceContainer devices = simpleNetDevice.Install(all); // all nodes
96
97 InternetStackHelper internetv6;
98 internetv6.Install(all);
99
101 ipv6.SetBase(Ipv6Address("2001:cafe::"), Ipv6Prefix(64));
102 NetDeviceContainer nonRouterDevices;
103 nonRouterDevices.Add(devices.Get(0)); // The server node, S0.
104 nonRouterDevices.Add(devices.Get(1)); // The first client node, N0.
105 nonRouterDevices.Add(devices.Get(2)); // The second client node, N1.
106 Ipv6InterfaceContainer i = ipv6.AssignWithoutAddress(nonRouterDevices);
107
108 NetDeviceContainer routerDevice;
109 routerDevice.Add(devices.Get(3)); // CSMA interface of the node R0.
110 Ipv6InterfaceContainer r1 = ipv6.Assign(routerDevice);
111 r1.SetForwarding(0, true);
112
113 RadvdHelper radvdHelper;
114
115 /* Set up unsolicited RAs */
116 radvdHelper.AddAnnouncedPrefix(r1.GetInterfaceIndex(0), Ipv6Address("2001:cafe::1"), 64);
117 radvdHelper.GetRadvdInterface(r1.GetInterfaceIndex(0))->SetManagedFlag(true);
118
119 Dhcp6Helper dhcp6Helper;
120
121 dhcp6Helper.SetServerAttribute("RenewTime", StringValue("10s"));
122 dhcp6Helper.SetServerAttribute("RebindTime", StringValue("16s"));
123 dhcp6Helper.SetServerAttribute("PreferredLifetime", StringValue("18s"));
124 dhcp6Helper.SetServerAttribute("ValidLifetime", StringValue("20s"));
125
126 // DHCPv6 clients
127 NodeContainer nodes = NodeContainer(nonRouterNodes.Get(1), nonRouterNodes.Get(2));
128 ApplicationContainer dhcpClients = dhcp6Helper.InstallDhcp6Client(nodes);
129 dhcpClients.Start(Seconds(1.0));
130 dhcpClients.Stop(Seconds(20.0));
131
132 // DHCPv6 server
133 NetDeviceContainer serverNetDevices;
134 serverNetDevices.Add(nonRouterDevices.Get(0));
135 ApplicationContainer dhcpServerApp = dhcp6Helper.InstallDhcp6Server(serverNetDevices);
136
137 Ptr<Dhcp6Server> server = DynamicCast<Dhcp6Server>(dhcpServerApp.Get(0));
138 server->AddSubnet(Ipv6Address("2001:cafe::"),
139 Ipv6Prefix(64),
140 Ipv6Address("2001:cafe::42:1"),
141 Ipv6Address("2001:cafe::42:ffff"));
142
143 dhcpServerApp.Start(Seconds(0.0));
144 dhcpServerApp.Stop(Seconds(20.0));
145
146 ApplicationContainer radvdApps = radvdHelper.Install(router);
147 radvdApps.Start(Seconds(1.0));
148 radvdApps.Stop(Seconds(20.0));
149
150 dhcpClients.Get(0)->TraceConnect("NewLease",
151 "0",
153 dhcpClients.Get(1)->TraceConnect("NewLease",
154 "1",
156
159
160 // Validate that the client nodes have three addresses on each interface -
161 // link-local, autoconfigured, and leased from DHCPv6 server.
162 Ptr<Ipv6> ipv6_client1 = nonRouterNodes.Get(1)->GetObject<Ipv6>();
163 Ptr<Ipv6L3Protocol> l3_client1 = ipv6_client1->GetObject<Ipv6L3Protocol>();
164 int32_t ifIndex_client1 = ipv6_client1->GetInterfaceForDevice(nonRouterDevices.Get(1));
165
166 NS_TEST_ASSERT_MSG_EQ(l3_client1->GetNAddresses(ifIndex_client1),
167 3,
168 "Incorrect number of addresses.");
169
170 Ptr<Ipv6> ipv6_client2 = nonRouterNodes.Get(2)->GetObject<Ipv6>();
171 Ptr<Ipv6L3Protocol> l3_client2 = ipv6_client2->GetObject<Ipv6L3Protocol>();
172 int32_t ifIndex_client2 = ipv6_client2->GetInterfaceForDevice(nonRouterDevices.Get(2));
173 NS_TEST_ASSERT_MSG_EQ(l3_client2->GetNAddresses(ifIndex_client2),
174 3,
175 "Incorrect number of addresses.");
176
178}
179
180/**
181 * @ingroup dhcp6-test
182 * @ingroup tests
183 *
184 * @brief DHCPv6 TestSuite
185 */
187{
188 public:
190};
191
197
198static Dhcp6TestSuite dhcp6TestSuite; //!< Static variable for test initialization
DHCPv6 header tests.
Definition dhcp6-test.cc:47
void DoRun() override
Implementation to actually run this TestCase.
Definition dhcp6-test.cc:85
~Dhcp6TestCase() override
Definition dhcp6-test.cc:69
Ipv6Address m_leasedAddress[2]
Address given to the nodes.
Definition dhcp6-test.cc:61
void LeaseObtained(std::string context, const Ipv6Address &newAddress)
Triggered by an address lease on a client.
Definition dhcp6-test.cc:74
DHCPv6 TestSuite.
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
ApplicationContainer Install(NodeContainer c)
Install an application on each node of the input container configured with all the attributes set wit...
Class for representing data rates.
Definition data-rate.h:78
AttributeValue implementation for DataRate.
Definition data-rate.h:285
The helper class used to configure and install DHCPv6 applications on nodes.
void SetServerAttribute(std::string name, const AttributeValue &value)
Set DHCPv6 server attributes.
ApplicationContainer InstallDhcp6Client(NodeContainer clientNodes) const
Install DHCPv6 client on a set of nodes.
ApplicationContainer InstallDhcp6Server(NetDeviceContainer netDevices)
Install DHCPv6 server on a node / NetDevice.
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Helper class to auto-assign global IPv6 unicast addresses.
Ipv6InterfaceContainer AssignWithoutAddress(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer but do not assign any IPv6 addresses.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Describes an IPv6 address.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition ipv6.h:71
Keep track of a set of IPv6 interfaces.
void SetForwarding(uint32_t i, bool state)
Set the state of the stack (act as a router or as an host) for the specified index.
uint32_t GetInterfaceIndex(uint32_t i) const
Get the interface index for the specified node index.
IPv6 layer implementation.
Describes an IPv6 prefix.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
Radvd application helper.
void AddAnnouncedPrefix(uint32_t interface, const Ipv6Address &prefix, uint32_t prefixLength, bool slaac=true)
Add a new prefix to be announced through an interface.
Ptr< RadvdInterface > GetRadvdInterface(uint32_t interface)
Get the low-level RadvdInterface specification for an interface.
build a set of SimpleNetDevice objects
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
@ QUICK
Fast test.
Definition test.h:1055
TestCase(const TestCase &)=delete
Type
Type of test.
Definition test.h:1274
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:490
static constexpr auto UNIT
Definition test.h:1291
AttributeValue implementation for Time.
Definition nstime.h:1456
static Dhcp6TestSuite dhcp6TestSuite
Static variable for test initialization.
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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1369
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1381
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:585