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 
33 using namespace ns3;
34 
35 static void
36 AddClickInternetStack (Ptr<Node> node)
37 {
38  ClickInternetStackHelper internet;
39  internet.SetClickFile (node, "src/click/examples/nsclick-lan-single-interface.click");
40  internet.Install (node);
41 }
42 
43 static void
44 AddNetworkDevice (Ptr<Node> node, Mac48Address macaddr, Ipv4Address ipv4addr, Ipv4Mask ipv4mask)
45 {
46  Ptr<SimpleNetDevice> rxDev1;
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 
59 class ClickIfidFromNameTest : public TestCase
60 {
61 public:
62  ClickIfidFromNameTest ();
63  virtual void DoRun ();
64 };
65 
66 ClickIfidFromNameTest::ClickIfidFromNameTest ()
67  : TestCase ("Test SIMCLICK_IFID_FROM_NAME")
68 {
69 }
70 
71 void
72 ClickIfidFromNameTest::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 
102 class ClickIpMacAddressFromNameTest : public TestCase
103 {
104 public:
105  ClickIpMacAddressFromNameTest ();
106  virtual void DoRun ();
107 };
108 
109 ClickIpMacAddressFromNameTest::ClickIpMacAddressFromNameTest ()
110  : TestCase ("Test SIMCLICK_IPADDR_FROM_NAME")
111 {
112 }
113 
114 void
115 ClickIpMacAddressFromNameTest::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 
153 class ClickTrivialTest : public TestCase
154 {
155 public:
156  ClickTrivialTest ();
157  virtual void DoRun ();
158 };
159 
160 ClickTrivialTest::ClickTrivialTest ()
161  : TestCase ("Test SIMCLICK_GET_NODE_NAME and SIMCLICK_IF_READY")
162 {
163 }
164 
165 void
166 ClickTrivialTest::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 
195 class ClickIfidFromNameTestSuite : public TestSuite
196 {
197 public:
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
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
A suite of tests to run.
Definition: test.h:1342
#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:285
encapsulates test code
Definition: test.h:1155
virtual Ptr< Ipv4RoutingProtocol > GetRoutingProtocol(void) const =0
Get the routing protocol to be used by this Ipv4 stack.
virtual void SetUp(uint32_t interface)=0
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Every class exported by the ns3 library is enclosed in the ns3 namespace.
an EUI-48 address
Definition: mac48-address.h:43
Fast test.
Definition: test.h:1160
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
a class to store IPv4 address information on an interface
virtual void SetAddress(Address address)
Set the address of this interface.
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0