A Discrete-Event Network Simulator
API
ipv4-test.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 * Author: Faker Moatamri <faker.moatamri@sophia.inria.fr>
15 *
16 */
21#include "ns3/arp-l3-protocol.h"
22#include "ns3/inet-socket-address.h"
23#include "ns3/ipv4-interface.h"
24#include "ns3/ipv4-l3-protocol.h"
25#include "ns3/log.h"
26#include "ns3/loopback-net-device.h"
27#include "ns3/node.h"
28#include "ns3/simulator.h"
29#include "ns3/test.h"
30
31using namespace ns3;
32
40{
41 public:
43 ~Ipv4L3ProtocolTestCase() override;
44 void DoRun() override;
45};
46
48 : TestCase("Verify the IPv4 layer 3 protocol")
49{
50}
51
53{
54}
55
56void
58{
59 Ptr<Node> node = CreateObject<Node>();
60 Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol>();
62 Ptr<LoopbackNetDevice> device = CreateObject<LoopbackNetDevice>();
63 node->AddDevice(device);
64 interface->SetDevice(device);
65 interface->SetNode(node);
66 uint32_t index = ipv4->AddIpv4Interface(interface);
67 NS_TEST_ASSERT_MSG_EQ(index, 0, "No interface should be found??");
68 interface->SetUp();
69 Ipv4InterfaceAddress ifaceAddr1 = Ipv4InterfaceAddress("192.168.0.1", "255.255.255.0");
70 interface->AddAddress(ifaceAddr1);
71 Ipv4InterfaceAddress ifaceAddr2 = Ipv4InterfaceAddress("192.168.0.2", "255.255.255.0");
72 interface->AddAddress(ifaceAddr2);
73 Ipv4InterfaceAddress ifaceAddr3 = Ipv4InterfaceAddress("10.30.0.1", "255.255.255.0");
74 interface->AddAddress(ifaceAddr3);
75 Ipv4InterfaceAddress ifaceAddr4 = Ipv4InterfaceAddress("250.0.0.1", "255.255.255.0");
76 interface->AddAddress(ifaceAddr4);
77 uint32_t num = interface->GetNAddresses();
78 NS_TEST_ASSERT_MSG_EQ(num, 4, "Should find 4 interfaces??");
79 interface->RemoveAddress(2);
80 num = interface->GetNAddresses();
81 NS_TEST_ASSERT_MSG_EQ(num, 3, "Should find 3 interfaces??");
82 Ipv4InterfaceAddress output = interface->GetAddress(2);
83 NS_TEST_ASSERT_MSG_EQ(ifaceAddr4, output, "The addresses should be identical");
84
85 /* Test Ipv4Interface()::RemoveAddress(address) */
86 output = interface->RemoveAddress(Ipv4Address("250.0.0.1"));
87 NS_TEST_ASSERT_MSG_EQ(ifaceAddr4, output, "Wrong Interface Address Removed??");
88 num = interface->GetNAddresses();
89 NS_TEST_ASSERT_MSG_EQ(num, 2, "Should find 2 addresses??");
90
91 /* Remove a non-existent Address */
92 output = interface->RemoveAddress(Ipv4Address("253.123.9.81"));
93 NS_TEST_ASSERT_MSG_EQ(Ipv4InterfaceAddress(), output, "Removed non-existent address??");
94 num = interface->GetNAddresses();
95 NS_TEST_ASSERT_MSG_EQ(num, 2, "Should find 2 addresses??");
96
97 /* Remove a Loopback Address */
98 output = interface->RemoveAddress(Ipv4Address::GetLoopback());
99 NS_TEST_ASSERT_MSG_EQ(Ipv4InterfaceAddress(), output, "Able to remove loopback address??");
100 num = interface->GetNAddresses();
101 NS_TEST_ASSERT_MSG_EQ(num, 2, "Should find 2 addresses??");
102
103 /* Test Ipv4Address::RemoveAddress(i, address) */
104 bool result = ipv4->RemoveAddress(index, Ipv4Address("192.168.0.2"));
105 NS_TEST_ASSERT_MSG_EQ(true, result, "Unable to remove Address??");
106 num = interface->GetNAddresses();
107 NS_TEST_ASSERT_MSG_EQ(num, 1, "Should find 1 addresses??");
108
109 /* Remove a non-existent Address */
110 result = ipv4->RemoveAddress(index, Ipv4Address("189.0.0.1"));
111 NS_TEST_ASSERT_MSG_EQ(false, result, "Removed non-existent address??");
112 num = interface->GetNAddresses();
113 NS_TEST_ASSERT_MSG_EQ(num, 1, "Should find 1 addresses??");
114
115 /* Remove a loopback Address */
116 result = ipv4->RemoveAddress(index, Ipv4Address::GetLoopback());
117 NS_TEST_ASSERT_MSG_EQ(false, result, "Able to remove loopback address??");
118 num = interface->GetNAddresses();
119 NS_TEST_ASSERT_MSG_EQ(num, 1, "Should find 1 addresses??");
120
121 Simulator::Destroy();
122}
123
131{
132 public:
134 : TestSuite("ipv4-protocol", UNIT)
135 {
136 AddTestCase(new Ipv4L3ProtocolTestCase(), TestCase::QUICK);
137 }
138};
139
IPv4 TestSuite.
Definition: ipv4-test.cc:131
void DoRun() override
Implementation to actually run this TestCase.
Definition: ipv4-test.cc:57
~Ipv4L3ProtocolTestCase() override
Definition: ipv4-test.cc:52
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
a class to store IPv4 address information on an interface
Ipv4Address GetAddress() 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:138
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:305
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:579
#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:144
static IPv4L3ProtocolTestSuite g_ipv4protocolTestSuite
Static variable for test initialization.
Definition: ipv4-test.cc:140
Every class exported by the ns3 library is enclosed in the ns3 namespace.