A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv4-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  * Author: Faker Moatamri <faker.moatamri@sophia.inria.fr>
16  *
17  */
22 #include "ns3/simulator.h"
23 #include "ns3/test.h"
24 #include "ns3/log.h"
25 #include "ns3/inet-socket-address.h"
26 #include "ns3/node.h"
27 
28 #include "ns3/ipv4-l3-protocol.h"
29 #include "ns3/arp-l3-protocol.h"
30 #include "ns3/ipv4-interface.h"
31 #include "ns3/loopback-net-device.h"
32 
33 using namespace ns3;
34 
36 {
37 public:
45  virtual
51  virtual void
52  DoRun (void);
53 
54 };
55 
57  TestCase ("Verify the IPv4 layer 3 protocol")
58 {
59 }
60 
62 {
63 }
64 void
66 {
67  Ptr<Node> node = CreateObject<Node> ();
68  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
69  Ptr<Ipv4Interface> interface = CreateObject<Ipv4Interface> ();
70  Ptr<LoopbackNetDevice> device = CreateObject<LoopbackNetDevice> ();
71  node->AddDevice (device);
72  interface->SetDevice (device);
73  interface->SetNode (node);
74  uint32_t index = ipv4->AddIpv4Interface (interface);
75  NS_TEST_ASSERT_MSG_EQ (index, 0, "No interface should be found??");
76  interface->SetUp ();
77  Ipv4InterfaceAddress ifaceAddr1 = Ipv4InterfaceAddress ("192.168.0.1",
78  "255.255.255.0");
79  interface->AddAddress (ifaceAddr1);
80  Ipv4InterfaceAddress ifaceAddr2 = Ipv4InterfaceAddress ("192.168.0.2",
81  "255.255.255.0");
82  interface->AddAddress (ifaceAddr2);
83  Ipv4InterfaceAddress ifaceAddr3 = Ipv4InterfaceAddress ("10.30.0.1",
84  "255.255.255.0");
85  interface->AddAddress (ifaceAddr3);
86  Ipv4InterfaceAddress ifaceAddr4 = Ipv4InterfaceAddress ("250.0.0.1",
87  "255.255.255.0");
88  interface->AddAddress (ifaceAddr4);
89  uint32_t num = interface->GetNAddresses ();
90  NS_TEST_ASSERT_MSG_EQ (num, 4, "Should find 4 interfaces??");
91  interface->RemoveAddress (2);
92  num = interface->GetNAddresses ();
93  NS_TEST_ASSERT_MSG_EQ (num, 3, "Should find 3 interfaces??");
94  Ipv4InterfaceAddress output = interface->GetAddress (2);
95  NS_TEST_ASSERT_MSG_EQ (ifaceAddr4, output,
96  "The addresses should be identical");
97 
98  /* Test Ipv4Interface()::RemoveAddress(address) */
99  output = interface->RemoveAddress (Ipv4Address ("250.0.0.1"));
100  NS_TEST_ASSERT_MSG_EQ (ifaceAddr4, output,
101  "Wrong Interface Address Removed??");
102  num = interface->GetNAddresses ();
103  NS_TEST_ASSERT_MSG_EQ (num, 2, "Should find 2 addresses??");
104 
105  /* Remove a non-existent Address */
106  output = interface->RemoveAddress (Ipv4Address ("253.123.9.81"));
108  "Removed non-existent address??");
109  num = interface->GetNAddresses ();
110  NS_TEST_ASSERT_MSG_EQ (num, 2, "Should find 2 addresses??");
111 
112  /* Remove a Loopback Address */
113  output = interface->RemoveAddress (Ipv4Address::GetLoopback ());
115  "Able to remove loopback address??");
116  num = interface->GetNAddresses ();
117  NS_TEST_ASSERT_MSG_EQ (num, 2, "Should find 2 addresses??");
118 
119  /* Test Ipv4Address::RemoveAddress(i, addresss) */
120  bool result = ipv4->RemoveAddress (index, Ipv4Address
121 ("192.168.0.2"));
122  NS_TEST_ASSERT_MSG_EQ (true, result, "Unable to remove Address??");
123  num = interface->GetNAddresses ();
124  NS_TEST_ASSERT_MSG_EQ (num, 1, "Should find 1 addresses??");
125 
126  /* Remove a non-existent Address */
127  result = ipv4->RemoveAddress (index, Ipv4Address ("189.0.0.1"));
128  NS_TEST_ASSERT_MSG_EQ (false, result,
129  "Removed non-existent address??");
130  num = interface->GetNAddresses ();
131  NS_TEST_ASSERT_MSG_EQ (num, 1, "Should find 1 addresses??");
132 
133  /* Remove a loopback Address */
134  result = ipv4->RemoveAddress (index, Ipv4Address::GetLoopback ());
135  NS_TEST_ASSERT_MSG_EQ (false, result,
136  "Able to remove loopback address??");
137  num = interface->GetNAddresses ();
138  NS_TEST_ASSERT_MSG_EQ (num, 1, "Should find 1 addresses??");
139 
140  Simulator::Destroy ();
141 }
142 
143 
144 static class IPv4L3ProtocolTestSuite : public TestSuite
145 {
146 public:
148  TestSuite ("ipv4-protocol", UNIT)
149  {
150  AddTestCase (new Ipv4L3ProtocolTestCase (), TestCase::QUICK);
151  }
IPv4L3ProtocolTestSuite g_ipv4protocolTestSuite
A suite of tests to run.
Definition: test.h:1105
encapsulates test code
Definition: test.h:929
virtual ~Ipv4L3ProtocolTestCase()
Destructor.
Definition: ipv4-test.cc:61
Ptr< T > CreateObject(void)
Definition: object.h:421
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:148
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
Ipv4L3ProtocolTestCase()
Constructor.
Definition: ipv4-test.cc:56
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:120
a class to store IPv4 address information on an interface
EventId output
virtual void DoRun(void)
Run unit tests for this class.
Definition: ipv4-test.cc:65
This test suite implements a Unit Test.
Definition: test.h:1115