A Discrete-Event Network Simulator
API
ipv4-click-routing-test.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2010 Lalith Suresh
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: Lalith Suresh <suresh.lalith@gmail.com>
19 */
20
21#ifdef NS3_CLICK
22
23#include "ns3/test.h"
24#include "ns3/log.h"
25#include "ns3/node.h"
26#include "ns3/ipv4-l3-protocol.h"
27#include "ns3/simple-net-device.h"
28#include "ns3/ipv4-click-routing.h"
29#include "ns3/click-internet-stack-helper.h"
30
31#include <click/simclick.h>
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{
61public:
62 ClickIfidFromNameTest ();
63 virtual void DoRun ();
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, Mac48Address ("00:00:00:00:00:01"), Ipv4Address ("10.1.1.1"), Ipv4Mask ("255.255.255.0"));
77 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
78 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting> (ipv4->GetRoutingProtocol ());
79 click->DoInitialize ();
80
81 int ret;
82
83 ret = simclick_sim_command (click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tap0");
84 NS_TEST_EXPECT_MSG_EQ (ret, 0, "tap0 is interface 0");
85
86 ret = simclick_sim_command (click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tun0");
87 NS_TEST_EXPECT_MSG_EQ (ret, 0, "tun0 is interface 0");
88
89 ret = simclick_sim_command (click->m_simNode, SIMCLICK_IFID_FROM_NAME, "eth0");
90 NS_TEST_EXPECT_MSG_EQ (ret, 1, "Eth0 is interface 1");
91
92 ret = simclick_sim_command (click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tap1");
93 NS_TEST_EXPECT_MSG_EQ (ret, 0, "tap1 is interface 0");
94
95 ret = simclick_sim_command (click->m_simNode, SIMCLICK_IFID_FROM_NAME, "tun1");
96 NS_TEST_EXPECT_MSG_EQ (ret, 0, "tun1 is interface 0");
97
98 ret = simclick_sim_command (click->m_simNode, SIMCLICK_IFID_FROM_NAME, "eth1");
99 NS_TEST_EXPECT_MSG_EQ (ret, -1, "No eth1 on node");
100}
101
102class ClickIpMacAddressFromNameTest : public TestCase
103{
104public:
105 ClickIpMacAddressFromNameTest ();
106 virtual void DoRun ();
107};
108
109ClickIpMacAddressFromNameTest::ClickIpMacAddressFromNameTest ()
110 : TestCase ("Test SIMCLICK_IPADDR_FROM_NAME")
111{
112}
113
114void
115ClickIpMacAddressFromNameTest::DoRun ()
116{
117 Ptr<Node> node = CreateObject<Node> ();
118 AddClickInternetStack (node);
119 AddNetworkDevice (node, Mac48Address ("00:00:00:00:00:01"), Ipv4Address ("10.1.1.1"), Ipv4Mask ("255.255.255.0"));
120 AddNetworkDevice (node, Mac48Address ("00:00:00:00:00:02"), Ipv4Address ("10.1.1.2"), Ipv4Mask ("255.255.255.0"));
121 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
122 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting> (ipv4->GetRoutingProtocol ());
123 click->DoInitialize ();
124
125 char *buf = NULL;
126 buf = new char [255];
127
128 simclick_sim_command (click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth0", buf, 255);
129 NS_TEST_EXPECT_MSG_EQ (strcmp (buf, "10.1.1.1"), 0, "eth0 has IP 10.1.1.1");
130
131 simclick_sim_command (click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "eth0", buf, 255);
132 NS_TEST_EXPECT_MSG_EQ (strcmp (buf, "00:00:00:00:00:01"), 0, "eth0 has Mac Address 00:00:00:00:00:01");
133
134 simclick_sim_command (click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth1", buf, 255);
135 NS_TEST_EXPECT_MSG_EQ (strcmp (buf, "10.1.1.2"), 0, "eth1 has IP 10.1.1.2");
136
137 simclick_sim_command (click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "eth1", buf, 255);
138 NS_TEST_EXPECT_MSG_EQ (strcmp (buf, "00:00:00:00:00:02"), 0, "eth0 has Mac Address 00:00:00:00:00:02");
139
140 // Not sure how to test the below case, because the Ipv4ClickRouting code is to ASSERT for such inputs
141 // simclick_sim_command (click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "eth2", buf, 255);
142 // NS_TEST_EXPECT_MSG_EQ (buf, NULL, "No eth2");
143
144 simclick_sim_command (click->m_simNode, SIMCLICK_IPADDR_FROM_NAME, "tap0", buf, 255);
145 NS_TEST_EXPECT_MSG_EQ (strcmp (buf, "127.0.0.1"), 0, "tun0 has IP 127.0.0.1");
146
147 simclick_sim_command (click->m_simNode, SIMCLICK_MACADDR_FROM_NAME, "tap0", buf, 255);
148 NS_TEST_EXPECT_MSG_EQ (strcmp (buf, "00:00:00:00:00:00"), 0, "tun0 has IP 127.0.0.1");
149
150 delete [] buf;
151}
152
153class ClickTrivialTest : public TestCase
154{
155public:
156 ClickTrivialTest ();
157 virtual void DoRun ();
158};
159
160ClickTrivialTest::ClickTrivialTest ()
161 : TestCase ("Test SIMCLICK_GET_NODE_NAME and SIMCLICK_IF_READY")
162{
163}
164
165void
166ClickTrivialTest::DoRun ()
167{
168 Ptr<Node> node = CreateObject<Node> ();
169 AddClickInternetStack (node);
170 AddNetworkDevice (node, Mac48Address ("00:00:00:00:00:01"), Ipv4Address ("10.1.1.1"), Ipv4Mask ("255.255.255.0"));
171 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
172 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting> (ipv4->GetRoutingProtocol ());
173 click->SetNodeName ("myNode");
174 click->DoInitialize ();
175
176 int ret = 0;
177 char *buf = NULL;
178 buf = new char [255];
179
180 ret = simclick_sim_command (click->m_simNode, SIMCLICK_GET_NODE_NAME, buf, 255);
181 NS_TEST_EXPECT_MSG_EQ (strcmp (buf, "myNode"), 0, "Node name is Node");
182
183 ret = simclick_sim_command (click->m_simNode, SIMCLICK_IF_READY, 0);
184 NS_TEST_EXPECT_MSG_EQ (ret, 1, "tap0 is ready");
185
186 ret = simclick_sim_command (click->m_simNode, SIMCLICK_IF_READY, 1);
187 NS_TEST_EXPECT_MSG_EQ (ret, 1, "eth0 is ready");
188
189 ret = simclick_sim_command (click->m_simNode, SIMCLICK_IF_READY, 2);
190 NS_TEST_EXPECT_MSG_EQ (ret, 0, "eth1 does not exist, so return 0");
191
192 delete [] buf;
193}
194
195class ClickIfidFromNameTestSuite : public TestSuite
196{
197public:
198 ClickIfidFromNameTestSuite () : TestSuite ("routing-click", UNIT)
199 {
200 AddTestCase (new ClickTrivialTest, TestCase::QUICK);
201 AddTestCase (new ClickIfidFromNameTest, TestCase::QUICK);
202 AddTestCase (new ClickIpMacAddressFromNameTest, TestCase::QUICK);
203 }
204} g_ipv4ClickRoutingTestSuite;
205
206#endif // NS3_CLICK
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:77
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:256
an EUI-48 address
Definition: mac48-address.h:44
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
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
virtual void DoRun(void)=0
Implementation to actually run this TestCase.
A suite of tests to run.
Definition: test.h:1188
#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:240
Every class exported by the ns3 library is enclosed in the ns3 namespace.