A Discrete-Event Network Simulator
API
dhcp-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2017 NITK Surathkal
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: Ankit Deepak <adadeepak8@gmail.com>
19  * Deepti Rajagopal <deeptir96@gmail.com>
20  *
21  */
22 
23 #include "ns3/data-rate.h"
24 #include "ns3/simple-net-device.h"
25 #include "ns3/simple-net-device-helper.h"
26 #include "ns3/internet-stack-helper.h"
27 #include "ns3/ipv4-address-helper.h"
28 #include "ns3/dhcp-client.h"
29 #include "ns3/dhcp-server.h"
30 #include "ns3/dhcp-helper.h"
31 #include "ns3/test.h"
32 
33 using namespace ns3;
34 
47 class DhcpTestCase : public TestCase
48 {
49 public:
50  DhcpTestCase ();
51  virtual ~DhcpTestCase ();
57  void LeaseObtained (std::string context, const Ipv4Address& newAddress);
58 private:
59  virtual void DoRun (void);
60  Ipv4Address m_leasedAddress[3];
61 };
62 
64  : TestCase ("Dhcp test case ")
65 {
66 }
67 
69 {
70 }
71 
72 void
73 DhcpTestCase::LeaseObtained (std::string context, const Ipv4Address& newAddress)
74 {
75  uint8_t numericalContext = std::stoi (context, nullptr, 10);
76 
77  if (numericalContext >=0 && numericalContext <=2)
78  {
79  m_leasedAddress[numericalContext] = newAddress;
80  }
81 }
82 
83 void
85 {
86  /*Set up devices*/
88  NodeContainer routers;
89  nodes.Create (3);
90  routers.Create (1);
91 
92  NodeContainer net (routers, nodes);
93 
94  SimpleNetDeviceHelper simpleNetDevice;
95  simpleNetDevice.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
96  simpleNetDevice.SetDeviceAttribute ("DataRate", DataRateValue (DataRate ("5Mbps")));
97  NetDeviceContainer devNet = simpleNetDevice.Install (net);
98 
99  InternetStackHelper tcpip;
100  tcpip.Install (routers);
101  tcpip.Install (nodes);
102 
103  DhcpHelper dhcpHelper;
104 
105  ApplicationContainer dhcpServerApp = dhcpHelper.InstallDhcpServer (devNet.Get (0), Ipv4Address ("172.30.0.12"),
106  Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
107  Ipv4Address ("172.30.0.10"), Ipv4Address ("172.30.0.15"),
108  Ipv4Address ("172.30.0.17"));
109  dhcpServerApp.Start (Seconds (0.0));
110  dhcpServerApp.Stop (Seconds (20.0));
111 
112  DynamicCast<DhcpServer> (dhcpServerApp.Get (0))->AddStaticDhcpEntry (devNet.Get (3)->GetAddress (), Ipv4Address ("172.30.0.14"));
113 
114  NetDeviceContainer dhcpClientNetDevs;
115  dhcpClientNetDevs.Add (devNet.Get (1));
116  dhcpClientNetDevs.Add (devNet.Get (2));
117  dhcpClientNetDevs.Add (devNet.Get (3));
118 
119  ApplicationContainer dhcpClientApps = dhcpHelper.InstallDhcpClient (dhcpClientNetDevs);
120  dhcpClientApps.Start (Seconds (1.0));
121  dhcpClientApps.Stop (Seconds (20.0));
122 
123 
124  dhcpClientApps.Get(0)->TraceConnect ("NewLease", "0", MakeCallback(&DhcpTestCase::LeaseObtained, this));
125  dhcpClientApps.Get(1)->TraceConnect ("NewLease", "1", MakeCallback(&DhcpTestCase::LeaseObtained, this));
126  dhcpClientApps.Get(2)->TraceConnect ("NewLease", "2", MakeCallback(&DhcpTestCase::LeaseObtained, this));
127 
128  Simulator::Stop (Seconds (21.0));
129 
130  Simulator::Run ();
131 
133  m_leasedAddress[0] << " instead of " << "172.30.0.10");
134 
136  m_leasedAddress[1] << " instead of " << "172.30.0.11");
137 
139  m_leasedAddress[2] << " instead of " << "172.30.0.14");
140 
141  Simulator::Destroy ();
142 }
143 
150 class DhcpTestSuite : public TestSuite
151 {
152 public:
153  DhcpTestSuite ();
154 };
155 
157  : TestSuite ("dhcp", UNIT)
158 {
159  AddTestCase (new DhcpTestCase, TestCase::QUICK);
160 }
161 
163 
DHCP basic tests.
Definition: dhcp-test.cc:48
virtual ~DhcpTestCase()
Definition: dhcp-test.cc:68
Ipv4Address m_leasedAddress[3]
Address given to the nodes.
Definition: dhcp-test.cc:60
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: dhcp-test.cc:84
void LeaseObtained(std::string context, const Ipv4Address &newAddress)
Triggered by an address lease on a client.
Definition: dhcp-test.cc:73
DHCP TestSuite.
Definition: dhcp-test.cc:151
holds a vector of ns3::Application pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
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.
Definition: data-rate.h:298
The helper class used to configure and install DHCP applications on nodes.
Definition: dhcp-helper.h:44
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:124
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:41
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:256
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.
virtual Address GetAddress(void) const =0
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:377
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...
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
AttributeValue implementation for Time.
Definition: nstime.h:1308
static DhcpTestSuite dhcpTestSuite
Static variable for test initialization.
Definition: dhcp-test.cc:162
#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:141
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648