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