A Discrete-Event Network Simulator
API
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 
42 {
43 public:
45  virtual ~Ipv4L3ProtocolTestCase ();
46  virtual void DoRun (void);
47 
48 };
49 
51  TestCase ("Verify the IPv4 layer 3 protocol")
52 {
53 }
54 
56 {
57 }
58 void
60 {
61  Ptr<Node> node = CreateObject<Node> ();
62  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
64  Ptr<LoopbackNetDevice> device = CreateObject<LoopbackNetDevice> ();
65  node->AddDevice (device);
66  interface->SetDevice (device);
67  interface->SetNode (node);
68  uint32_t index = ipv4->AddIpv4Interface (interface);
69  NS_TEST_ASSERT_MSG_EQ (index, 0, "No interface should be found??");
70  interface->SetUp ();
71  Ipv4InterfaceAddress ifaceAddr1 = Ipv4InterfaceAddress ("192.168.0.1",
72  "255.255.255.0");
73  interface->AddAddress (ifaceAddr1);
74  Ipv4InterfaceAddress ifaceAddr2 = Ipv4InterfaceAddress ("192.168.0.2",
75  "255.255.255.0");
76  interface->AddAddress (ifaceAddr2);
77  Ipv4InterfaceAddress ifaceAddr3 = Ipv4InterfaceAddress ("10.30.0.1",
78  "255.255.255.0");
79  interface->AddAddress (ifaceAddr3);
80  Ipv4InterfaceAddress ifaceAddr4 = Ipv4InterfaceAddress ("250.0.0.1",
81  "255.255.255.0");
82  interface->AddAddress (ifaceAddr4);
83  uint32_t num = interface->GetNAddresses ();
84  NS_TEST_ASSERT_MSG_EQ (num, 4, "Should find 4 interfaces??");
85  interface->RemoveAddress (2);
86  num = interface->GetNAddresses ();
87  NS_TEST_ASSERT_MSG_EQ (num, 3, "Should find 3 interfaces??");
88  Ipv4InterfaceAddress output = interface->GetAddress (2);
89  NS_TEST_ASSERT_MSG_EQ (ifaceAddr4, output,
90  "The addresses should be identical");
91 
92  /* Test Ipv4Interface()::RemoveAddress(address) */
93  output = interface->RemoveAddress (Ipv4Address ("250.0.0.1"));
94  NS_TEST_ASSERT_MSG_EQ (ifaceAddr4, output,
95  "Wrong Interface Address Removed??");
96  num = interface->GetNAddresses ();
97  NS_TEST_ASSERT_MSG_EQ (num, 2, "Should find 2 addresses??");
98 
99  /* Remove a non-existent Address */
100  output = interface->RemoveAddress (Ipv4Address ("253.123.9.81"));
102  "Removed non-existent address??");
103  num = interface->GetNAddresses ();
104  NS_TEST_ASSERT_MSG_EQ (num, 2, "Should find 2 addresses??");
105 
106  /* Remove a Loopback Address */
107  output = interface->RemoveAddress (Ipv4Address::GetLoopback ());
109  "Able to remove loopback address??");
110  num = interface->GetNAddresses ();
111  NS_TEST_ASSERT_MSG_EQ (num, 2, "Should find 2 addresses??");
112 
113  /* Test Ipv4Address::RemoveAddress(i, address) */
114  bool result = ipv4->RemoveAddress (index, Ipv4Address
115 ("192.168.0.2"));
116  NS_TEST_ASSERT_MSG_EQ (true, result, "Unable to remove Address??");
117  num = interface->GetNAddresses ();
118  NS_TEST_ASSERT_MSG_EQ (num, 1, "Should find 1 addresses??");
119 
120  /* Remove a non-existent Address */
121  result = ipv4->RemoveAddress (index, Ipv4Address ("189.0.0.1"));
123  "Removed non-existent address??");
124  num = interface->GetNAddresses ();
125  NS_TEST_ASSERT_MSG_EQ (num, 1, "Should find 1 addresses??");
126 
127  /* Remove a loopback Address */
128  result = ipv4->RemoveAddress (index, Ipv4Address::GetLoopback ());
130  "Able to remove loopback address??");
131  num = interface->GetNAddresses ();
132  NS_TEST_ASSERT_MSG_EQ (num, 1, "Should find 1 addresses??");
133 
134  Simulator::Destroy ();
135 }
136 
137 
145 {
146 public:
148  TestSuite ("ipv4-protocol", UNIT)
149  {
150  AddTestCase (new Ipv4L3ProtocolTestCase (), TestCase::QUICK);
151  }
152 };
153 
IPv4 TestSuite.
Definition: ipv4-test.cc:145
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: ipv4-test.cc:59
virtual ~Ipv4L3ProtocolTestCase()
Definition: ipv4-test.cc:55
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
a class to store IPv4 address information on an interface
Ipv4Address GetAddress(void) const
Get the local address.
The IPv4 representation of a network interface.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
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
A suite of tests to run.
Definition: test.h:1188
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1197
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:576
#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:141
static IPv4L3ProtocolTestSuite g_ipv4protocolTestSuite
Static variable for test initialization.
Definition: ipv4-test.cc:154
Every class exported by the ns3 library is enclosed in the ns3 namespace.