A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-click-routing-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Lalith Suresh
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: Lalith Suresh <suresh.lalith@gmail.com>
18 */
19
20#include "ns3/click-internet-stack-helper.h"
21#include "ns3/ipv4-click-routing.h"
22#include "ns3/ipv4-l3-protocol.h"
23#include "ns3/log.h"
24#include "ns3/node.h"
25#include "ns3/simple-net-device.h"
26#include "ns3/test.h"
27
28#include <click/simclick.h>
29#include <string>
30
31using namespace ns3;
32
33/**
34 * \ingroup click
35 * \defgroup click-tests click module tests
36 */
37
38/**
39 * \file
40 * \ingroup click-tests
41 * Click test suite.
42 */
43
44/**
45 * \ingroup click-tests
46 * Add Click Internet stack.
47 *
48 * \param node Node.
49 */
50static void
52{
54 internet.SetClickFile(node, "src/click/test/nsclick-test-lan-single-interface.click");
55 internet.Install(node);
56}
57
58/**
59 * \ingroup click-tests
60 * Add network device.
61 *
62 * \param node Node.
63 * \param macaddr MAC address.
64 * \param ipv4addr IPv4 address.
65 * \param ipv4mask IPv4 mask.
66 */
67static void
69{
71
72 rxDev1 = CreateObject<SimpleNetDevice>();
73 rxDev1->SetAddress(Mac48Address(macaddr));
74 node->AddDevice(rxDev1);
75
76 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
77 uint32_t netdev_idx = ipv4->AddInterface(rxDev1);
78 Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress(ipv4addr, ipv4mask);
79 ipv4->AddAddress(netdev_idx, ipv4Addr);
80 ipv4->SetUp(netdev_idx);
81}
82
83/**
84 * \ingroup click-tests
85 * Click interface ID from name test.
86 */
88{
89 public:
91 void DoRun() override;
92};
93
95 : TestCase("Test SIMCLICK_IFID_FROM_NAME")
96{
97}
98
99void
101{
102 Ptr<Node> node = CreateObject<Node>();
104 AddNetworkDevice(node,
105 Mac48Address("00:00:00:00:00:01"),
106 Ipv4Address("10.1.1.1"),
107 Ipv4Mask("255.255.255.0"));
108 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
109 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting>(ipv4->GetRoutingProtocol());
110 click->DoInitialize();
111
112 int ret;
113
114 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tap0");
115 NS_TEST_EXPECT_MSG_EQ(ret, 0, "tap0 is interface 0");
116
117 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tun0");
118 NS_TEST_EXPECT_MSG_EQ(ret, 0, "tun0 is interface 0");
119
120 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "eth0");
121 NS_TEST_EXPECT_MSG_EQ(ret, 1, "Eth0 is interface 1");
122
123 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tap1");
124 NS_TEST_EXPECT_MSG_EQ(ret, 0, "tap1 is interface 0");
125
126 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tun1");
127 NS_TEST_EXPECT_MSG_EQ(ret, 0, "tun1 is interface 0");
128
129 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "eth1");
130 NS_TEST_EXPECT_MSG_EQ(ret, -1, "No eth1 on node");
131}
132
133/**
134 * \ingroup click-tests
135 * Click IP MAC address from name test.
136 */
138{
139 public:
141 void DoRun() override;
142};
143
145 : TestCase("Test SIMCLICK_IPADDR_FROM_NAME")
146{
147}
148
149void
151{
152 Ptr<Node> node = CreateObject<Node>();
154 AddNetworkDevice(node,
155 Mac48Address("00:00:00:00:00:01"),
156 Ipv4Address("10.1.1.1"),
157 Ipv4Mask("255.255.255.0"));
158 AddNetworkDevice(node,
159 Mac48Address("00:00:00:00:00:02"),
160 Ipv4Address("10.1.1.2"),
161 Ipv4Mask("255.255.255.0"));
162 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
163 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting>(ipv4->GetRoutingProtocol());
164 click->DoInitialize();
165
166 char* buf = new char[255];
167
168 simclick_sim_command(click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth0", buf, 255);
169 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "10.1.1.1", "eth0 has IP 10.1.1.1");
170
171 simclick_sim_command(click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "eth0", buf, 255);
172 NS_TEST_EXPECT_MSG_EQ(std::string(buf),
173 "00:00:00:00:00:01",
174 "eth0 has Mac Address 00:00:00:00:00:01");
175
176 simclick_sim_command(click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth1", buf, 255);
177 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "10.1.1.2", "eth1 has IP 10.1.1.2");
178
179 simclick_sim_command(click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "eth1", buf, 255);
180 NS_TEST_EXPECT_MSG_EQ(std::string(buf),
181 "00:00:00:00:00:02",
182 "eth0 has Mac Address 00:00:00:00:00:02");
183
184 // Not sure how to test the below case, because the Ipv4ClickRouting code is to ASSERT for such
185 // inputs simclick_sim_command (click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth2", buf, 255);
186 // NS_TEST_EXPECT_MSG_EQ (buf, nullptr, "No eth2");
187
188 simclick_sim_command(click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "tap0", buf, 255);
189 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "127.0.0.1", "tun0 has IP 127.0.0.1");
190
191 simclick_sim_command(click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "tap0", buf, 255);
192 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "00:00:00:00:00:00", "tun0 has IP 127.0.0.1");
193
194 delete[] buf;
195}
196
197/**
198 * \ingroup click-tests
199 * Click trivial test.
200 */
202{
203 public:
205 void DoRun() override;
206};
207
209 : TestCase("Test SIMCLICK_GET_NODE_NAME and SIMCLICK_IF_READY")
210{
211}
212
213void
215{
216 Ptr<Node> node = CreateObject<Node>();
218 AddNetworkDevice(node,
219 Mac48Address("00:00:00:00:00:01"),
220 Ipv4Address("10.1.1.1"),
221 Ipv4Mask("255.255.255.0"));
222 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
223 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting>(ipv4->GetRoutingProtocol());
224 click->SetNodeName("myNode");
225 click->DoInitialize();
226
227 int ret = 0;
228 char* buf = new char[255];
229
230 ret = simclick_sim_command(click->m_simNode, SIMCLICK_GET_NODE_NAME, buf, 255);
231 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "myNode", "Node name is Node");
232
233 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IF_READY, 0);
234 NS_TEST_EXPECT_MSG_EQ(ret, 1, "tap0 is ready");
235
236 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IF_READY, 1);
237 NS_TEST_EXPECT_MSG_EQ(ret, 1, "eth0 is ready");
238
239 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IF_READY, 2);
240 NS_TEST_EXPECT_MSG_EQ(ret, 0, "eth1 does not exist, so return 0");
241
242 delete[] buf;
243}
244
245/**
246 * \ingroup click-tests
247 * Click interface ID from name test.
248 */
250{
251 public:
253 : TestSuite("routing-click", Type::UNIT)
254 {
255 AddTestCase(new ClickTrivialTest, TestCase::Duration::QUICK);
256 AddTestCase(new ClickIfidFromNameTest, TestCase::Duration::QUICK);
257 AddTestCase(new ClickIpMacAddressFromNameTest, TestCase::Duration::QUICK);
258 }
259};
260
261/// Static variable for test initialization
Click interface ID from name test.
void DoRun() override
Implementation to actually run this TestCase.
Click interface ID from name test.
Click IP MAC address from name test.
void DoRun() override
Implementation to actually run this TestCase.
Click trivial test.
void DoRun() override
Implementation to actually run this TestCase.
aggregate Click/IP/TCP/UDP functionality to existing Nodes.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
an EUI-48 address
Definition: mac48-address.h:46
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
static constexpr auto UNIT
Definition: test.h:1286
static void AddNetworkDevice(Ptr< Node > node, Mac48Address macaddr, Ipv4Address ipv4addr, Ipv4Mask ipv4mask)
Add network device.
static void AddClickInternetStack(Ptr< Node > node)
Add Click Internet stack.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:252
static ClickIfidFromNameTestSuite g_ipv4ClickRoutingTestSuite
Static variable for test initialization.
int simclick_sim_command(simclick_node_t *simnode, int cmd,...)
Every class exported by the ns3 library is enclosed in the ns3 namespace.