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 #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 
31 using namespace ns3;
32 
44 static void
46 {
48  internet.SetClickFile(node, "src/click/test/nsclick-test-lan-single-interface.click");
49  internet.Install(node);
50 }
51 
60 static void
61 AddNetworkDevice(Ptr<Node> node, Mac48Address macaddr, Ipv4Address ipv4addr, Ipv4Mask ipv4mask)
62 {
63  Ptr<SimpleNetDevice> rxDev1;
64 
65  rxDev1 = CreateObject<SimpleNetDevice>();
66  rxDev1->SetAddress(Mac48Address(macaddr));
67  node->AddDevice(rxDev1);
68 
69  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
70  uint32_t netdev_idx = ipv4->AddInterface(rxDev1);
71  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress(ipv4addr, ipv4mask);
72  ipv4->AddAddress(netdev_idx, ipv4Addr);
73  ipv4->SetUp(netdev_idx);
74 }
75 
81 {
82  public:
84  void DoRun() override;
85 };
86 
88  : TestCase("Test SIMCLICK_IFID_FROM_NAME")
89 {
90 }
91 
92 void
94 {
95  Ptr<Node> node = CreateObject<Node>();
97  AddNetworkDevice(node,
98  Mac48Address("00:00:00:00:00:01"),
99  Ipv4Address("10.1.1.1"),
100  Ipv4Mask("255.255.255.0"));
101  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
102  Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting>(ipv4->GetRoutingProtocol());
103  click->DoInitialize();
104 
105  int ret;
106 
107  ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tap0");
108  NS_TEST_EXPECT_MSG_EQ(ret, 0, "tap0 is interface 0");
109 
110  ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tun0");
111  NS_TEST_EXPECT_MSG_EQ(ret, 0, "tun0 is interface 0");
112 
113  ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "eth0");
114  NS_TEST_EXPECT_MSG_EQ(ret, 1, "Eth0 is interface 1");
115 
116  ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tap1");
117  NS_TEST_EXPECT_MSG_EQ(ret, 0, "tap1 is interface 0");
118 
119  ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tun1");
120  NS_TEST_EXPECT_MSG_EQ(ret, 0, "tun1 is interface 0");
121 
122  ret = simclick_sim_command(click->m_simNode, SIMCLICK_IFID_FROM_NAME, "eth1");
123  NS_TEST_EXPECT_MSG_EQ(ret, -1, "No eth1 on node");
124 }
125 
131 {
132  public:
134  void DoRun() override;
135 };
136 
138  : TestCase("Test SIMCLICK_IPADDR_FROM_NAME")
139 {
140 }
141 
142 void
144 {
145  Ptr<Node> node = CreateObject<Node>();
146  AddClickInternetStack(node);
147  AddNetworkDevice(node,
148  Mac48Address("00:00:00:00:00:01"),
149  Ipv4Address("10.1.1.1"),
150  Ipv4Mask("255.255.255.0"));
151  AddNetworkDevice(node,
152  Mac48Address("00:00:00:00:00:02"),
153  Ipv4Address("10.1.1.2"),
154  Ipv4Mask("255.255.255.0"));
155  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
156  Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting>(ipv4->GetRoutingProtocol());
157  click->DoInitialize();
158 
159  char* buf = new char[255];
160 
161  simclick_sim_command(click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth0", buf, 255);
162  NS_TEST_EXPECT_MSG_EQ(std::string(buf), "10.1.1.1", "eth0 has IP 10.1.1.1");
163 
164  simclick_sim_command(click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "eth0", buf, 255);
165  NS_TEST_EXPECT_MSG_EQ(std::string(buf),
166  "00:00:00:00:00:01",
167  "eth0 has Mac Address 00:00:00:00:00:01");
168 
169  simclick_sim_command(click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth1", buf, 255);
170  NS_TEST_EXPECT_MSG_EQ(std::string(buf), "10.1.1.2", "eth1 has IP 10.1.1.2");
171 
172  simclick_sim_command(click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "eth1", buf, 255);
173  NS_TEST_EXPECT_MSG_EQ(std::string(buf),
174  "00:00:00:00:00:02",
175  "eth0 has Mac Address 00:00:00:00:00:02");
176 
177  // Not sure how to test the below case, because the Ipv4ClickRouting code is to ASSERT for such
178  // inputs simclick_sim_command (click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth2", buf, 255);
179  // NS_TEST_EXPECT_MSG_EQ (buf, nullptr, "No eth2");
180 
181  simclick_sim_command(click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "tap0", buf, 255);
182  NS_TEST_EXPECT_MSG_EQ(std::string(buf), "127.0.0.1", "tun0 has IP 127.0.0.1");
183 
184  simclick_sim_command(click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "tap0", buf, 255);
185  NS_TEST_EXPECT_MSG_EQ(std::string(buf), "00:00:00:00:00:00", "tun0 has IP 127.0.0.1");
186 
187  delete[] buf;
188 }
189 
195 {
196  public:
198  void DoRun() override;
199 };
200 
202  : TestCase("Test SIMCLICK_GET_NODE_NAME and SIMCLICK_IF_READY")
203 {
204 }
205 
206 void
208 {
209  Ptr<Node> node = CreateObject<Node>();
210  AddClickInternetStack(node);
211  AddNetworkDevice(node,
212  Mac48Address("00:00:00:00:00:01"),
213  Ipv4Address("10.1.1.1"),
214  Ipv4Mask("255.255.255.0"));
215  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
216  Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting>(ipv4->GetRoutingProtocol());
217  click->SetNodeName("myNode");
218  click->DoInitialize();
219 
220  int ret = 0;
221  char* buf = new char[255];
222 
223  ret = simclick_sim_command(click->m_simNode, SIMCLICK_GET_NODE_NAME, buf, 255);
224  NS_TEST_EXPECT_MSG_EQ(std::string(buf), "myNode", "Node name is Node");
225 
226  ret = simclick_sim_command(click->m_simNode, SIMCLICK_IF_READY, 0);
227  NS_TEST_EXPECT_MSG_EQ(ret, 1, "tap0 is ready");
228 
229  ret = simclick_sim_command(click->m_simNode, SIMCLICK_IF_READY, 1);
230  NS_TEST_EXPECT_MSG_EQ(ret, 1, "eth0 is ready");
231 
232  ret = simclick_sim_command(click->m_simNode, SIMCLICK_IF_READY, 2);
233  NS_TEST_EXPECT_MSG_EQ(ret, 0, "eth1 does not exist, so return 0");
234 
235  delete[] buf;
236 }
237 
243 {
244  public:
246  : TestSuite("routing-click", UNIT)
247  {
248  AddTestCase(new ClickTrivialTest, TestCase::QUICK);
249  AddTestCase(new ClickIfidFromNameTest, TestCase::QUICK);
250  AddTestCase(new ClickIpMacAddressFromNameTest, TestCase::QUICK);
251  }
252 };
253 
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
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:301
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
#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
static ClickIfidFromNameTestSuite g_ipv4ClickRoutingTestSuite
Static variable for test initialization.
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.
int simclick_sim_command(simclick_node_t *simnode, int cmd,...)
Every class exported by the ns3 library is enclosed in the ns3 namespace.