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/internet-apps-module.h"
17#include "ns3/internet-module.h"
18#include "ns3/internet-stack-helper.h"
19#include "ns3/ipv6-address-helper.h"
20#include "ns3/log.h"
21#include "ns3/mobility-module.h"
22#include "ns3/network-module.h"
23#include "ns3/ping-helper.h"
24#include "ns3/point-to-point-module.h"
25#include "ns3/simple-net-device-helper.h"
26#include "ns3/simple-net-device.h"
27#include "ns3/simulator.h"
28#include "ns3/test.h"
29#include "ns3/trace-helper.h"
30
31using namespace ns3;
32
33/**
34 * @ingroup dhcp6
35 * @defgroup dhcp6-test DHCPv6 module tests
36 */
37
38/**
39 * @ingroup dhcp6-test
40 * @ingroup tests
41 *
42 * @brief DHCPv6 header tests
43 */
44class Dhcp6TestCase : public TestCase
45{
46 public:
48 ~Dhcp6TestCase() override;
49
50 /**
51 * Triggered by an address lease on a client.
52 * @param context The test name.
53 * @param newAddress The leased address.
54 */
55 void LeaseObtained(std::string context, const Ipv6Address& newAddress);
56
57 private:
58 void DoRun() override;
59 Ipv6Address m_leasedAddress[2]; //!< Address given to the nodes
60};
61
63 : TestCase("Dhcp6 test case ")
64{
65}
66
70
71void
72Dhcp6TestCase::LeaseObtained(std::string context, const Ipv6Address& newAddress)
73{
74 uint8_t numericalContext = std::stoi(context, nullptr, 10);
75
76 if (numericalContext >= 0 && numericalContext < std::size(m_leasedAddress))
77 {
78 m_leasedAddress[numericalContext] = newAddress;
79 }
80}
81
82void
84{
85 NodeContainer nonRouterNodes;
86 nonRouterNodes.Create(3);
88 NodeContainer all(nonRouterNodes, router);
89
90 SimpleNetDeviceHelper simpleNetDevice;
91 simpleNetDevice.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
92 simpleNetDevice.SetDeviceAttribute("DataRate", DataRateValue(DataRate("5Mbps")));
93 NetDeviceContainer devices = simpleNetDevice.Install(all); // all nodes
94
95 InternetStackHelper internetv6;
96 internetv6.Install(all);
97
99 ipv6.SetBase(Ipv6Address("2001:cafe::"), Ipv6Prefix(64));
100 NetDeviceContainer nonRouterDevices;
101 nonRouterDevices.Add(devices.Get(0)); // The server node, S0.
102 nonRouterDevices.Add(devices.Get(1)); // The first client node, N0.
103 nonRouterDevices.Add(devices.Get(2)); // The second client node, N1.
104 Ipv6InterfaceContainer i = ipv6.AssignWithoutAddress(nonRouterDevices);
105
106 NetDeviceContainer routerDevice;
107 routerDevice.Add(devices.Get(3)); // CSMA interface of the node R0.
108 Ipv6InterfaceContainer r1 = ipv6.Assign(routerDevice);
109 r1.SetForwarding(0, true);
110
111 RadvdHelper radvdHelper;
112
113 /* Set up unsolicited RAs */
114 radvdHelper.AddAnnouncedPrefix(r1.GetInterfaceIndex(0), Ipv6Address("2001:cafe::1"), 64);
115 radvdHelper.GetRadvdInterface(r1.GetInterfaceIndex(0))->SetManagedFlag(true);
116
117 Dhcp6Helper dhcp6Helper;
118
119 dhcp6Helper.SetServerAttribute("RenewTime", StringValue("10s"));
120 dhcp6Helper.SetServerAttribute("RebindTime", StringValue("16s"));
121 dhcp6Helper.SetServerAttribute("PreferredLifetime", StringValue("18s"));
122 dhcp6Helper.SetServerAttribute("ValidLifetime", StringValue("20s"));
123
124 // DHCPv6 clients
125 NodeContainer nodes = NodeContainer(nonRouterNodes.Get(1), nonRouterNodes.Get(2));
126 ApplicationContainer dhcpClients = dhcp6Helper.InstallDhcp6Client(nodes);
127 dhcpClients.Start(Seconds(1.0));
128 dhcpClients.Stop(Seconds(20.0));
129
130 // DHCPv6 server
131 NetDeviceContainer serverNetDevices;
132 serverNetDevices.Add(nonRouterDevices.Get(0));
133 ApplicationContainer dhcpServerApp = dhcp6Helper.InstallDhcp6Server(serverNetDevices);
134
135 Ptr<Dhcp6Server> server = DynamicCast<Dhcp6Server>(dhcpServerApp.Get(0));
136 server->AddSubnet(Ipv6Address("2001:cafe::"),
137 Ipv6Prefix(64),
138 Ipv6Address("2001:cafe::42:1"),
139 Ipv6Address("2001:cafe::42:ffff"));
140
141 dhcpServerApp.Start(Seconds(0.0));
142 dhcpServerApp.Stop(Seconds(20.0));
143
144 ApplicationContainer radvdApps = radvdHelper.Install(router);
145 radvdApps.Start(Seconds(1.0));
146 radvdApps.Stop(Seconds(20.0));
147
148 dhcpClients.Get(0)->TraceConnect("NewLease",
149 "0",
151 dhcpClients.Get(1)->TraceConnect("NewLease",
152 "1",
154
157
158 // Validate that the client nodes have three addresses on each interface -
159 // link-local, autoconfigured, and leased from DHCPv6 server.
160 Ptr<Ipv6> ipv6_client1 = nonRouterNodes.Get(1)->GetObject<Ipv6>();
161 Ptr<Ipv6L3Protocol> l3_client1 = ipv6_client1->GetObject<Ipv6L3Protocol>();
162 int32_t ifIndex_client1 = ipv6_client1->GetInterfaceForDevice(nonRouterDevices.Get(1));
163
164 NS_TEST_ASSERT_MSG_EQ(l3_client1->GetNAddresses(ifIndex_client1),
165 3,
166 "Incorrect number of addresses.");
167
168 Ptr<Ipv6> ipv6_client2 = nonRouterNodes.Get(2)->GetObject<Ipv6>();
169 Ptr<Ipv6L3Protocol> l3_client2 = ipv6_client2->GetObject<Ipv6L3Protocol>();
170 int32_t ifIndex_client2 = ipv6_client2->GetInterfaceForDevice(nonRouterDevices.Get(2));
171 NS_TEST_ASSERT_MSG_EQ(l3_client2->GetNAddresses(ifIndex_client2),
172 3,
173 "Incorrect number of addresses.");
174
176}
177
178/**
179 * @ingroup dhcp6-test
180 * @ingroup tests
181 *
182 * @brief DHCPv6 TestSuite
183 */
185{
186 public:
188};
189
195
196static Dhcp6TestSuite dhcp6TestSuite; //!< Static variable for test initialization
DHCPv6 header tests.
Definition dhcp6-test.cc:45
void DoRun() override
Implementation to actually run this TestCase.
Definition dhcp6-test.cc:83
~Dhcp6TestCase() override
Definition dhcp6-test.cc:67
Ipv6Address m_leasedAddress[2]
Address given to the nodes.
Definition dhcp6-test.cc:59
void LeaseObtained(std::string context, const Ipv6Address &newAddress)
Triggered by an address lease on a client.
Definition dhcp6-test.cc:72
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:293
@ QUICK
Fast test.
Definition test.h:1054
TestCase(const TestCase &)=delete
Type
Type of test.
Definition test.h:1257
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:491
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:133
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