A Discrete-Event Network Simulator
API
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#ifdef NS3_CLICK
21
22#include "ns3/click-internet-stack-helper.h"
23#include "ns3/ipv4-click-routing.h"
24#include "ns3/ipv4-l3-protocol.h"
25#include "ns3/log.h"
26#include "ns3/node.h"
27#include "ns3/simple-net-device.h"
28#include "ns3/test.h"
29
30#include <click/simclick.h>
31#include <string>
32
33using namespace ns3;
34
35static void
36AddClickInternetStack(Ptr<Node> node)
37{
38 ClickInternetStackHelper internet;
39 internet.SetClickFile(node, "src/click/test/nsclick-test-lan-single-interface.click");
40 internet.Install(node);
41}
42
43static void
44AddNetworkDevice(Ptr<Node> node, Mac48Address macaddr, Ipv4Address ipv4addr, Ipv4Mask ipv4mask)
45{
47
48 rxDev1 = CreateObject<SimpleNetDevice>();
49 rxDev1->SetAddress(Mac48Address(macaddr));
50 node->AddDevice(rxDev1);
51
52 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
53 uint32_t netdev_idx = ipv4->AddInterface(rxDev1);
54 Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress(ipv4addr, ipv4mask);
55 ipv4->AddAddress(netdev_idx, ipv4Addr);
56 ipv4->SetUp(netdev_idx);
57}
58
59class ClickIfidFromNameTest : public TestCase
60{
61 public:
62 ClickIfidFromNameTest();
63 void DoRun() override;
64};
65
66ClickIfidFromNameTest::ClickIfidFromNameTest()
67 : TestCase("Test SIMCLICK_IFID_FROM_NAME")
68{
69}
70
71void
72ClickIfidFromNameTest::DoRun()
73{
74 Ptr<Node> node = CreateObject<Node>();
75 AddClickInternetStack(node);
76 AddNetworkDevice(node,
77 Mac48Address("00:00:00:00:00:01"),
78 Ipv4Address("10.1.1.1"),
79 Ipv4Mask("255.255.255.0"));
80 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
81 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting>(ipv4->GetRoutingProtocol());
82 click->DoInitialize();
83
84 int ret;
85
86 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tap0");
87 NS_TEST_EXPECT_MSG_EQ(ret, 0, "tap0 is interface 0");
88
89 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tun0");
90 NS_TEST_EXPECT_MSG_EQ(ret, 0, "tun0 is interface 0");
91
92 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "eth0");
93 NS_TEST_EXPECT_MSG_EQ(ret, 1, "Eth0 is interface 1");
94
95 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tap1");
96 NS_TEST_EXPECT_MSG_EQ(ret, 0, "tap1 is interface 0");
97
98 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tun1");
99 NS_TEST_EXPECT_MSG_EQ(ret, 0, "tun1 is interface 0");
100
101 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "eth1");
102 NS_TEST_EXPECT_MSG_EQ(ret, -1, "No eth1 on node");
103}
104
105class ClickIpMacAddressFromNameTest : public TestCase
106{
107 public:
108 ClickIpMacAddressFromNameTest();
109 void DoRun() override;
110};
111
112ClickIpMacAddressFromNameTest::ClickIpMacAddressFromNameTest()
113 : TestCase("Test SIMCLICK_IPADDR_FROM_NAME")
114{
115}
116
117void
118ClickIpMacAddressFromNameTest::DoRun()
119{
120 Ptr<Node> node = CreateObject<Node>();
121 AddClickInternetStack(node);
122 AddNetworkDevice(node,
123 Mac48Address("00:00:00:00:00:01"),
124 Ipv4Address("10.1.1.1"),
125 Ipv4Mask("255.255.255.0"));
126 AddNetworkDevice(node,
127 Mac48Address("00:00:00:00:00:02"),
128 Ipv4Address("10.1.1.2"),
129 Ipv4Mask("255.255.255.0"));
130 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
131 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting>(ipv4->GetRoutingProtocol());
132 click->DoInitialize();
133
134 char* buf = new char[255];
135
136 simclick_sim_command(click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth0", buf, 255);
137 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "10.1.1.1", "eth0 has IP 10.1.1.1");
138
139 simclick_sim_command(click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "eth0", buf, 255);
140 NS_TEST_EXPECT_MSG_EQ(std::string(buf),
141 "00:00:00:00:00:01",
142 "eth0 has Mac Address 00:00:00:00:00:01");
143
144 simclick_sim_command(click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth1", buf, 255);
145 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "10.1.1.2", "eth1 has IP 10.1.1.2");
146
147 simclick_sim_command(click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "eth1", buf, 255);
148 NS_TEST_EXPECT_MSG_EQ(std::string(buf),
149 "00:00:00:00:00:02",
150 "eth0 has Mac Address 00:00:00:00:00:02");
151
152 // Not sure how to test the below case, because the Ipv4ClickRouting code is to ASSERT for such
153 // inputs simclick_sim_command (click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth2", buf, 255);
154 // NS_TEST_EXPECT_MSG_EQ (buf, nullptr, "No eth2");
155
156 simclick_sim_command(click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "tap0", buf, 255);
157 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "127.0.0.1", "tun0 has IP 127.0.0.1");
158
159 simclick_sim_command(click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "tap0", buf, 255);
160 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "00:00:00:00:00:00", "tun0 has IP 127.0.0.1");
161
162 delete[] buf;
163}
164
165class ClickTrivialTest : public TestCase
166{
167 public:
168 ClickTrivialTest();
169 void DoRun() override;
170};
171
172ClickTrivialTest::ClickTrivialTest()
173 : TestCase("Test SIMCLICK_GET_NODE_NAME and SIMCLICK_IF_READY")
174{
175}
176
177void
178ClickTrivialTest::DoRun()
179{
180 Ptr<Node> node = CreateObject<Node>();
181 AddClickInternetStack(node);
182 AddNetworkDevice(node,
183 Mac48Address("00:00:00:00:00:01"),
184 Ipv4Address("10.1.1.1"),
185 Ipv4Mask("255.255.255.0"));
186 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
187 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting>(ipv4->GetRoutingProtocol());
188 click->SetNodeName("myNode");
189 click->DoInitialize();
190
191 int ret = 0;
192 char* buf = new char[255];
193
194 ret = simclick_sim_command(click->m_simNode, SIMCLICK_GET_NODE_NAME, buf, 255);
195 NS_TEST_EXPECT_MSG_EQ(std::string(buf), "myNode", "Node name is Node");
196
197 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IF_READY, 0);
198 NS_TEST_EXPECT_MSG_EQ(ret, 1, "tap0 is ready");
199
200 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IF_READY, 1);
201 NS_TEST_EXPECT_MSG_EQ(ret, 1, "eth0 is ready");
202
203 ret = simclick_sim_command(click->m_simNode, SIMCLICK_IF_READY, 2);
204 NS_TEST_EXPECT_MSG_EQ(ret, 0, "eth1 does not exist, so return 0");
205
206 delete[] buf;
207}
208
209class ClickIfidFromNameTestSuite : public TestSuite
210{
211 public:
212 ClickIfidFromNameTestSuite()
213 : TestSuite("routing-click", UNIT)
214 {
215 AddTestCase(new ClickTrivialTest, TestCase::QUICK);
216 AddTestCase(new ClickIfidFromNameTest, TestCase::QUICK);
217 AddTestCase(new ClickIpMacAddressFromNameTest, TestCase::QUICK);
218 }
219} g_ipv4ClickRoutingTestSuite;
220
221#endif // NS3_CLICK
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:79
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
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:305
virtual void DoRun()=0
Implementation to actually run this TestCase.
A suite of tests to run.
Definition: test.h:1256
#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:251
Every class exported by the ns3 library is enclosed in the ns3 namespace.