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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Lalith Suresh <suresh.lalith@gmail.com>
7 */
8
9#include "ns3/click-internet-stack-helper.h"
10#include "ns3/ipv4-click-routing.h"
11#include "ns3/ipv4-l3-protocol.h"
12#include "ns3/log.h"
13#include "ns3/node.h"
14#include "ns3/simple-net-device.h"
15#include "ns3/test.h"
16
17#include <click/simclick.h>
18#include <string>
19
20using namespace ns3;
21
22/**
23 * @ingroup click
24 * @defgroup click-tests click module tests
25 */
26
27/**
28 * @file
29 * @ingroup click-tests
30 * Click test suite.
31 */
32
33/**
34 * @ingroup click-tests
35 * Add Click Internet stack.
36 *
37 * @param node Node.
38 */
39static void
41{
43 internet.SetClickFile(node, "src/click/test/nsclick-test-lan-single-interface.click");
44 internet.Install(node);
45}
46
47/**
48 * @ingroup click-tests
49 * Add network device.
50 *
51 * @param node Node.
52 * @param macaddr MAC address.
53 * @param ipv4addr IPv4 address.
54 * @param ipv4mask IPv4 mask.
55 */
56static void
58{
60
62 rxDev1->SetAddress(Mac48Address(macaddr));
63 node->AddDevice(rxDev1);
64
65 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
66 uint32_t netdev_idx = ipv4->AddInterface(rxDev1);
67 Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress(ipv4addr, ipv4mask);
68 ipv4->AddAddress(netdev_idx, ipv4Addr);
69 ipv4->SetUp(netdev_idx);
70}
71
72/**
73 * @ingroup click-tests
74 * Click interface ID from name test.
75 */
77{
78 public:
80 void DoRun() override;
81};
82
84 : TestCase("Test SIMCLICK_IFID_FROM_NAME")
85{
86}
87
88void
90{
94 Mac48Address("00:00:00:00:00:01"),
95 Ipv4Address("10.1.1.1"),
96 Ipv4Mask("255.255.255.0"));
97 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
98 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting>(ipv4->GetRoutingProtocol());
99 click->DoInitialize();
100
101 int ret;
102
103 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tap0");
104 NS_TEST_EXPECT_MSG_EQ(ret, 0, "tap0 is interface 0");
105
106 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tun0");
107 NS_TEST_EXPECT_MSG_EQ(ret, 0, "tun0 is interface 0");
108
109 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "eth0");
110 NS_TEST_EXPECT_MSG_EQ(ret, 1, "Eth0 is interface 1");
111
112 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tap1");
113 NS_TEST_EXPECT_MSG_EQ(ret, 0, "tap1 is interface 0");
114
115 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tun1");
116 NS_TEST_EXPECT_MSG_EQ(ret, 0, "tun1 is interface 0");
117
118 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "eth1");
119 NS_TEST_EXPECT_MSG_EQ(ret, -1, "No eth1 on node");
120}
121
122/**
123 * @ingroup click-tests
124 * Click IP MAC address from name test.
125 */
127{
128 public:
130 void DoRun() override;
131};
132
134 : TestCase("Test SIMCLICK_IPADDR_FROM_NAME")
135{
136}
137
138void
140{
143 AddNetworkDevice(node,
144 Mac48Address("00:00:00:00:00:01"),
145 Ipv4Address("10.1.1.1"),
146 Ipv4Mask("255.255.255.0"));
147 AddNetworkDevice(node,
148 Mac48Address("00:00:00:00:00:02"),
149 Ipv4Address("10.1.1.2"),
150 Ipv4Mask("255.255.255.0"));
151 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
152 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting>(ipv4->GetRoutingProtocol());
153 click->DoInitialize();
154
155 char* buf = new char[255];
156
157 simclick_sim_command(click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth0", buf, 255);
158 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "10.1.1.1", "eth0 has IP 10.1.1.1");
159
160 simclick_sim_command(click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "eth0", buf, 255);
161 NS_TEST_EXPECT_MSG_EQ(std::string(buf),
162 "00:00:00:00:00:01",
163 "eth0 has Mac Address 00:00:00:00:00:01");
164
165 simclick_sim_command(click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth1", buf, 255);
166 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "10.1.1.2", "eth1 has IP 10.1.1.2");
167
168 simclick_sim_command(click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "eth1", buf, 255);
169 NS_TEST_EXPECT_MSG_EQ(std::string(buf),
170 "00:00:00:00:00:02",
171 "eth0 has Mac Address 00:00:00:00:00:02");
172
173 // Not sure how to test the below case, because the Ipv4ClickRouting code is to ASSERT for such
174 // inputs simclick_sim_command (click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth2", buf, 255);
175 // NS_TEST_EXPECT_MSG_EQ (buf, nullptr, "No eth2");
176
177 simclick_sim_command(click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "tap0", buf, 255);
178 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "127.0.0.1", "tun0 has IP 127.0.0.1");
179
180 simclick_sim_command(click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "tap0", buf, 255);
181 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "00:00:00:00:00:00", "tun0 has IP 127.0.0.1");
182
183 delete[] buf;
184}
185
186/**
187 * @ingroup click-tests
188 * Click trivial test.
189 */
191{
192 public:
194 void DoRun() override;
195};
196
198 : TestCase("Test SIMCLICK_GET_NODE_NAME and SIMCLICK_IF_READY")
199{
200}
201
202void
204{
207 AddNetworkDevice(node,
208 Mac48Address("00:00:00:00:00:01"),
209 Ipv4Address("10.1.1.1"),
210 Ipv4Mask("255.255.255.0"));
211 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
212 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting>(ipv4->GetRoutingProtocol());
213 click->SetNodeName("myNode");
214 click->DoInitialize();
215
216 int ret = 0;
217 char* buf = new char[255];
218
219 ret = simclick_sim_command(click->m_simNode, SIMCLICK_GET_NODE_NAME, buf, 255);
220 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "myNode", "Node name is Node");
221
222 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IF_READY, 0);
223 NS_TEST_EXPECT_MSG_EQ(ret, 1, "tap0 is ready");
224
225 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IF_READY, 1);
226 NS_TEST_EXPECT_MSG_EQ(ret, 1, "eth0 is ready");
227
228 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IF_READY, 2);
229 NS_TEST_EXPECT_MSG_EQ(ret, 0, "eth1 does not exist, so return 0");
230
231 delete[] buf;
232}
233
234/**
235 * @ingroup click-tests
236 * Click interface ID from name test.
237 */
239{
240 public:
242 : TestSuite("routing-click", Type::UNIT)
243 {
244 AddTestCase(new ClickTrivialTest, TestCase::Duration::QUICK);
245 AddTestCase(new ClickIfidFromNameTest, TestCase::Duration::QUICK);
246 AddTestCase(new ClickIpMacAddressFromNameTest, TestCase::Duration::QUICK);
247 }
248};
249
250/// 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.
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.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
an EUI-48 address
Smart pointer class similar to boost::intrusive_ptr.
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 constexpr auto UNIT
Definition test.h:1291
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.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:241
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.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580