A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dhcp-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 NITK Surathkal
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Ankit Deepak <adadeepak8@gmail.com>
7 * Deepti Rajagopal <deeptir96@gmail.com>
8 *
9 */
10
11#include "ns3/data-rate.h"
12#include "ns3/dhcp-client.h"
13#include "ns3/dhcp-helper.h"
14#include "ns3/dhcp-server.h"
15#include "ns3/internet-stack-helper.h"
16#include "ns3/ipv4-address-helper.h"
17#include "ns3/simple-net-device-helper.h"
18#include "ns3/simple-net-device.h"
19#include "ns3/test.h"
20
21using namespace ns3;
22
23/**
24 * \ingroup dhcp
25 * \defgroup dhcp-test DHCP module tests
26 */
27
28/**
29 * \ingroup dhcp-test
30 * \ingroup tests
31 *
32 * \brief DHCP basic tests
33 */
34class DhcpTestCase : public TestCase
35{
36 public:
38 ~DhcpTestCase() override;
39 /**
40 * Triggered by an address lease on a client.
41 * \param context The test name.
42 * \param newAddress The leased address.
43 */
44 void LeaseObtained(std::string context, const Ipv4Address& newAddress);
45
46 private:
47 void DoRun() override;
48 Ipv4Address m_leasedAddress[3]; //!< Address given to the nodes
49};
50
52 : TestCase("Dhcp test case ")
53{
54}
55
59
60void
61DhcpTestCase::LeaseObtained(std::string context, const Ipv4Address& newAddress)
62{
63 uint8_t numericalContext = std::stoi(context, nullptr, 10);
64
65 if (numericalContext >= 0 && numericalContext <= 2)
66 {
67 m_leasedAddress[numericalContext] = newAddress;
68 }
69}
70
71void
73{
74 /*Set up devices*/
76 NodeContainer routers;
77 nodes.Create(3);
78 routers.Create(1);
79
80 NodeContainer net(routers, nodes);
81
82 SimpleNetDeviceHelper simpleNetDevice;
83 simpleNetDevice.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
84 simpleNetDevice.SetDeviceAttribute("DataRate", DataRateValue(DataRate("5Mbps")));
85 NetDeviceContainer devNet = simpleNetDevice.Install(net);
86
88 tcpip.Install(routers);
89 tcpip.Install(nodes);
90
91 DhcpHelper dhcpHelper;
92
93 ApplicationContainer dhcpServerApp = dhcpHelper.InstallDhcpServer(devNet.Get(0),
94 Ipv4Address("172.30.0.12"),
95 Ipv4Address("172.30.0.0"),
96 Ipv4Mask("/24"),
97 Ipv4Address("172.30.0.10"),
98 Ipv4Address("172.30.0.15"),
99 Ipv4Address("172.30.0.17"));
100 dhcpServerApp.Start(Seconds(0.0));
101 dhcpServerApp.Stop(Seconds(20.0));
102
103 DynamicCast<DhcpServer>(dhcpServerApp.Get(0))
104 ->AddStaticDhcpEntry(devNet.Get(3)->GetAddress(), Ipv4Address("172.30.0.14"));
105
106 NetDeviceContainer dhcpClientNetDevs;
107 dhcpClientNetDevs.Add(devNet.Get(1));
108 dhcpClientNetDevs.Add(devNet.Get(2));
109 dhcpClientNetDevs.Add(devNet.Get(3));
110
111 ApplicationContainer dhcpClientApps = dhcpHelper.InstallDhcpClient(dhcpClientNetDevs);
112 dhcpClientApps.Start(Seconds(1.0));
113 dhcpClientApps.Stop(Seconds(20.0));
114
115 dhcpClientApps.Get(0)->TraceConnect("NewLease",
116 "0",
118 dhcpClientApps.Get(1)->TraceConnect("NewLease",
119 "1",
121 dhcpClientApps.Get(2)->TraceConnect("NewLease",
122 "2",
124
126
128
130 Ipv4Address("172.30.0.10"),
131 m_leasedAddress[0] << " instead of "
132 << "172.30.0.10");
133
135 Ipv4Address("172.30.0.11"),
136 m_leasedAddress[1] << " instead of "
137 << "172.30.0.11");
138
140 Ipv4Address("172.30.0.14"),
141 m_leasedAddress[2] << " instead of "
142 << "172.30.0.14");
143
145}
146
147/**
148 * \ingroup dhcp-test
149 * \ingroup tests
150 *
151 * \brief DHCP TestSuite
152 */
154{
155 public:
157};
158
160 : TestSuite("dhcp", Type::UNIT)
161{
162 AddTestCase(new DhcpTestCase, TestCase::Duration::QUICK);
163}
164
165static DhcpTestSuite dhcpTestSuite; //!< Static variable for test initialization
DHCP basic tests.
Definition dhcp-test.cc:35
void DoRun() override
Implementation to actually run this TestCase.
Definition dhcp-test.cc:72
Ipv4Address m_leasedAddress[3]
Address given to the nodes.
Definition dhcp-test.cc:48
void LeaseObtained(std::string context, const Ipv4Address &newAddress)
Triggered by an address lease on a client.
Definition dhcp-test.cc:61
~DhcpTestCase() override
Definition dhcp-test.cc:56
DHCP TestSuite.
Definition dhcp-test.cc:154
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.
Class for representing data rates.
Definition data-rate.h:78
The helper class used to configure and install DHCP applications on nodes.
Definition dhcp-helper.h:34
ApplicationContainer InstallDhcpServer(Ptr< NetDevice > netDevice, Ipv4Address serverAddr, Ipv4Address poolAddr, Ipv4Mask poolMask, Ipv4Address minAddr, Ipv4Address maxAddr, Ipv4Address gateway=Ipv4Address())
Install DHCP server of a node / NetDevice.
ApplicationContainer InstallDhcpClient(Ptr< NetDevice > netDevice) const
Install DHCP client of a nodes / 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...
Ipv4 addresses are stored in host order in this class.
a class to represent an Ipv4 address mask
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.
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
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
static DhcpTestSuite dhcpTestSuite
Static variable for test initialization.
Definition dhcp-test.cc:165
#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:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
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