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 * 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: Ankit Deepak <adadeepak8@gmail.com>
18 * Deepti Rajagopal <deeptir96@gmail.com>
19 *
20 */
21
22#include "ns3/data-rate.h"
23#include "ns3/dhcp-client.h"
24#include "ns3/dhcp-helper.h"
25#include "ns3/dhcp-server.h"
26#include "ns3/internet-stack-helper.h"
27#include "ns3/ipv4-address-helper.h"
28#include "ns3/simple-net-device-helper.h"
29#include "ns3/simple-net-device.h"
30#include "ns3/test.h"
31
32using namespace ns3;
33
45class DhcpTestCase : public TestCase
46{
47 public:
49 ~DhcpTestCase() override;
55 void LeaseObtained(std::string context, const Ipv4Address& newAddress);
56
57 private:
58 void DoRun() override;
60};
61
63 : TestCase("Dhcp test case ")
64{
65}
66
68{
69}
70
71void
72DhcpTestCase::LeaseObtained(std::string context, const Ipv4Address& newAddress)
73{
74 uint8_t numericalContext = std::stoi(context, nullptr, 10);
75
76 if (numericalContext >= 0 && numericalContext <= 2)
77 {
78 m_leasedAddress[numericalContext] = newAddress;
79 }
80}
81
82void
84{
85 /*Set up devices*/
87 NodeContainer routers;
88 nodes.Create(3);
89 routers.Create(1);
90
91 NodeContainer net(routers, nodes);
92
93 SimpleNetDeviceHelper simpleNetDevice;
94 simpleNetDevice.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
95 simpleNetDevice.SetDeviceAttribute("DataRate", DataRateValue(DataRate("5Mbps")));
96 NetDeviceContainer devNet = simpleNetDevice.Install(net);
97
99 tcpip.Install(routers);
100 tcpip.Install(nodes);
101
102 DhcpHelper dhcpHelper;
103
104 ApplicationContainer dhcpServerApp = dhcpHelper.InstallDhcpServer(devNet.Get(0),
105 Ipv4Address("172.30.0.12"),
106 Ipv4Address("172.30.0.0"),
107 Ipv4Mask("/24"),
108 Ipv4Address("172.30.0.10"),
109 Ipv4Address("172.30.0.15"),
110 Ipv4Address("172.30.0.17"));
111 dhcpServerApp.Start(Seconds(0.0));
112 dhcpServerApp.Stop(Seconds(20.0));
113
114 DynamicCast<DhcpServer>(dhcpServerApp.Get(0))
115 ->AddStaticDhcpEntry(devNet.Get(3)->GetAddress(), Ipv4Address("172.30.0.14"));
116
117 NetDeviceContainer dhcpClientNetDevs;
118 dhcpClientNetDevs.Add(devNet.Get(1));
119 dhcpClientNetDevs.Add(devNet.Get(2));
120 dhcpClientNetDevs.Add(devNet.Get(3));
121
122 ApplicationContainer dhcpClientApps = dhcpHelper.InstallDhcpClient(dhcpClientNetDevs);
123 dhcpClientApps.Start(Seconds(1.0));
124 dhcpClientApps.Stop(Seconds(20.0));
125
126 dhcpClientApps.Get(0)->TraceConnect("NewLease",
127 "0",
129 dhcpClientApps.Get(1)->TraceConnect("NewLease",
130 "1",
132 dhcpClientApps.Get(2)->TraceConnect("NewLease",
133 "2",
135
137
139
141 Ipv4Address("172.30.0.10"),
142 m_leasedAddress[0] << " instead of "
143 << "172.30.0.10");
144
146 Ipv4Address("172.30.0.11"),
147 m_leasedAddress[1] << " instead of "
148 << "172.30.0.11");
149
151 Ipv4Address("172.30.0.14"),
152 m_leasedAddress[2] << " instead of "
153 << "172.30.0.14");
154
156}
157
165{
166 public:
168};
169
171 : TestSuite("dhcp", UNIT)
172{
174}
175
DHCP basic tests.
Definition: dhcp-test.cc:46
void DoRun() override
Implementation to actually run this TestCase.
Definition: dhcp-test.cc:83
Ipv4Address m_leasedAddress[3]
Address given to the nodes.
Definition: dhcp-test.cc:59
void LeaseObtained(std::string context, const Ipv4Address &newAddress)
Triggered by an address lease on a client.
Definition: dhcp-test.cc:72
~DhcpTestCase() override
Definition: dhcp-test.cc:67
DHCP TestSuite.
Definition: dhcp-test.cc:165
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:89
AttributeValue implementation for DataRate.
The helper class used to configure and install DHCP applications on nodes.
Definition: dhcp-helper.h:45
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.
Definition: dhcp-helper.cc:129
ApplicationContainer InstallDhcpClient(Ptr< NetDevice > netDevice) const
Install DHCP client of a nodes / NetDevice.
Definition: dhcp-helper.cc:61
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.
Definition: ipv4-address.h:42
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
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.
bool TraceConnect(std::string name, std::string context, const CallbackBase &cb)
Connect a TraceSource to a Callback with a context.
Definition: object-base.cc:325
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:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
encapsulates test code
Definition: test.h:1060
@ QUICK
Fast test.
Definition: test.h:1065
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
AttributeValue implementation for Time.
Definition: nstime.h:1423
static DhcpTestSuite dhcpTestSuite
Static variable for test initialization.
Definition: dhcp-test.cc:176
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1348
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:702