A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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 */
17/**
18 * This is the test code for ipv4-l3-protocol.cc
19 */
20
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
33/**
34 * \ingroup internet-test
35 *
36 * \brief IPv4 Test
37 */
39{
40 public:
42 ~Ipv4L3ProtocolTestCase() override;
43 void DoRun() override;
44};
45
47 : TestCase("Verify the IPv4 layer 3 protocol")
48{
49}
50
52{
53}
54
55void
57{
58 Ptr<Node> node = CreateObject<Node>();
59 Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol>();
61 Ptr<LoopbackNetDevice> device = CreateObject<LoopbackNetDevice>();
62 node->AddDevice(device);
63 interface->SetDevice(device);
64 interface->SetNode(node);
65 uint32_t index = ipv4->AddIpv4Interface(interface);
66 NS_TEST_ASSERT_MSG_EQ(index, 0, "No interface should be found??");
67 interface->SetUp();
68 Ipv4InterfaceAddress ifaceAddr1 = Ipv4InterfaceAddress("192.168.0.1", "255.255.255.0");
69 interface->AddAddress(ifaceAddr1);
70 Ipv4InterfaceAddress ifaceAddr2 = Ipv4InterfaceAddress("192.168.0.2", "255.255.255.0");
71 interface->AddAddress(ifaceAddr2);
72 Ipv4InterfaceAddress ifaceAddr3 = Ipv4InterfaceAddress("10.30.0.1", "255.255.255.0");
73 interface->AddAddress(ifaceAddr3);
74 Ipv4InterfaceAddress ifaceAddr4 = Ipv4InterfaceAddress("250.0.0.1", "255.255.255.0");
75 interface->AddAddress(ifaceAddr4);
76 uint32_t num = interface->GetNAddresses();
77 NS_TEST_ASSERT_MSG_EQ(num, 4, "Should find 4 interfaces??");
78 interface->RemoveAddress(2);
79 num = interface->GetNAddresses();
80 NS_TEST_ASSERT_MSG_EQ(num, 3, "Should find 3 interfaces??");
81 Ipv4InterfaceAddress output = interface->GetAddress(2);
82 NS_TEST_ASSERT_MSG_EQ(ifaceAddr4, output, "The addresses should be identical");
83
84 /* Test Ipv4Interface()::RemoveAddress(address) */
85 output = interface->RemoveAddress(Ipv4Address("250.0.0.1"));
86 NS_TEST_ASSERT_MSG_EQ(ifaceAddr4, output, "Wrong Interface Address Removed??");
87 num = interface->GetNAddresses();
88 NS_TEST_ASSERT_MSG_EQ(num, 2, "Should find 2 addresses??");
89
90 /* Remove a non-existent Address */
91 output = interface->RemoveAddress(Ipv4Address("253.123.9.81"));
92 NS_TEST_ASSERT_MSG_EQ(Ipv4InterfaceAddress(), output, "Removed non-existent address??");
93 num = interface->GetNAddresses();
94 NS_TEST_ASSERT_MSG_EQ(num, 2, "Should find 2 addresses??");
95
96 /* Remove a Loopback Address */
97 output = interface->RemoveAddress(Ipv4Address::GetLoopback());
98 NS_TEST_ASSERT_MSG_EQ(Ipv4InterfaceAddress(), output, "Able to remove loopback address??");
99 num = interface->GetNAddresses();
100 NS_TEST_ASSERT_MSG_EQ(num, 2, "Should find 2 addresses??");
101
102 /* Test Ipv4Address::RemoveAddress(i, address) */
103 bool result = ipv4->RemoveAddress(index, Ipv4Address("192.168.0.2"));
104 NS_TEST_ASSERT_MSG_EQ(true, result, "Unable to remove Address??");
105 num = interface->GetNAddresses();
106 NS_TEST_ASSERT_MSG_EQ(num, 1, "Should find 1 addresses??");
107
108 /* Remove a non-existent Address */
109 result = ipv4->RemoveAddress(index, Ipv4Address("189.0.0.1"));
110 NS_TEST_ASSERT_MSG_EQ(false, result, "Removed non-existent address??");
111 num = interface->GetNAddresses();
112 NS_TEST_ASSERT_MSG_EQ(num, 1, "Should find 1 addresses??");
113
114 /* Remove a loopback Address */
115 result = ipv4->RemoveAddress(index, Ipv4Address::GetLoopback());
116 NS_TEST_ASSERT_MSG_EQ(false, result, "Able to remove loopback address??");
117 num = interface->GetNAddresses();
118 NS_TEST_ASSERT_MSG_EQ(num, 1, "Should find 1 addresses??");
119
121}
122
123/**
124 * \ingroup internet-test
125 *
126 * \brief IPv4 TestSuite
127 */
129{
130 public:
132 : TestSuite("ipv4-protocol", Type::UNIT)
133 {
134 AddTestCase(new Ipv4L3ProtocolTestCase(), TestCase::Duration::QUICK);
135 }
136};
137
138static IPv4L3ProtocolTestSuite g_ipv4protocolTestSuite; //!< Static variable for test initialization
IPv4 TestSuite.
Definition: ipv4-test.cc:129
void DoRun() override
Implementation to actually run this TestCase.
Definition: ipv4-test.cc:56
~Ipv4L3ProtocolTestCase() override
Definition: ipv4-test.cc:51
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
static Ipv4Address GetLoopback()
a class to store IPv4 address information on an interface
Ipv4Address GetAddress() const
Get the local address.
The IPv4 representation of a network interface.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
static constexpr auto UNIT
Definition: test.h:1286
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:630
#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:145
static IPv4L3ProtocolTestSuite g_ipv4protocolTestSuite
Static variable for test initialization.
Definition: ipv4-test.cc:138
Every class exported by the ns3 library is enclosed in the ns3 namespace.