A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-test.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 * Author: Faker Moatamri <faker.moatamri@sophia.inria.fr>
4 *
5 */
6/**
7 * This is the test code for ipv4-l3-protocol.cc
8 */
9
10#include "ns3/arp-l3-protocol.h"
11#include "ns3/boolean.h"
12#include "ns3/inet-socket-address.h"
13#include "ns3/ipv4-interface.h"
14#include "ns3/ipv4-l3-protocol.h"
15#include "ns3/log.h"
16#include "ns3/node.h"
17#include "ns3/simple-net-device.h"
18#include "ns3/simulator.h"
19#include "ns3/test.h"
20
21using namespace ns3;
22
23/**
24 * @ingroup internet-test
25 *
26 * @brief IPv4 Test
27 */
29{
30 public:
32 ~Ipv4L3ProtocolTestCase() override;
33 void DoRun() override;
34};
35
37 : TestCase("Verify the IPv4 layer 3 protocol")
38{
39}
40
44
45void
47{
52
53 // The following allows the interface to run without ARP
54 device->SetAttribute("PointToPointMode", BooleanValue(true));
55
56 node->AddDevice(device);
57 node->AggregateObject(ipv4);
58 interface->SetDevice(device);
59 interface->SetNode(node);
60
61 // Interface 0 is the Loopback
62 uint32_t index = ipv4->AddIpv4Interface(interface);
63 NS_TEST_ASSERT_MSG_EQ(index, 1, "The index is not 1??");
64 interface->SetUp();
65 Ipv4InterfaceAddress ifaceAddr1 = Ipv4InterfaceAddress("192.168.0.1", "255.255.255.0");
66 interface->AddAddress(ifaceAddr1);
67 Ipv4InterfaceAddress ifaceAddr2 = Ipv4InterfaceAddress("192.168.0.2", "255.255.255.0");
68 interface->AddAddress(ifaceAddr2);
69 Ipv4InterfaceAddress ifaceAddr3 = Ipv4InterfaceAddress("10.30.0.1", "255.255.255.0");
70 interface->AddAddress(ifaceAddr3);
71 Ipv4InterfaceAddress ifaceAddr4 = Ipv4InterfaceAddress("250.0.0.1", "255.255.255.0");
72 interface->AddAddress(ifaceAddr4);
73 uint32_t num = interface->GetNAddresses();
74 NS_TEST_ASSERT_MSG_EQ(num, 4, "Should find 4 interfaces??");
75 interface->RemoveAddress(2);
76 num = interface->GetNAddresses();
77 NS_TEST_ASSERT_MSG_EQ(num, 3, "Should find 3 interfaces??");
78 Ipv4InterfaceAddress output = interface->GetAddress(2);
79 NS_TEST_ASSERT_MSG_EQ(ifaceAddr4, output, "The addresses should be identical");
80
81 /* Test Ipv4Interface()::RemoveAddress(address) */
82 output = interface->RemoveAddress(Ipv4Address("250.0.0.1"));
83 NS_TEST_ASSERT_MSG_EQ(ifaceAddr4, output, "Wrong Interface Address Removed??");
84 num = interface->GetNAddresses();
85 NS_TEST_ASSERT_MSG_EQ(num, 2, "Should find 2 addresses??");
86
87 /* Remove a non-existent Address */
88 output = interface->RemoveAddress(Ipv4Address("253.123.9.81"));
89 NS_TEST_ASSERT_MSG_EQ(Ipv4InterfaceAddress(), output, "Removed non-existent address??");
90 num = interface->GetNAddresses();
91 NS_TEST_ASSERT_MSG_EQ(num, 2, "Should find 2 addresses??");
92
93 /* Remove a Loopback Address */
94 output = interface->RemoveAddress(Ipv4Address::GetLoopback());
95 NS_TEST_ASSERT_MSG_EQ(Ipv4InterfaceAddress(), output, "Able to remove loopback address??");
96 num = interface->GetNAddresses();
97 NS_TEST_ASSERT_MSG_EQ(num, 2, "Should find 2 addresses??");
98
99 /* Test Ipv4Address::RemoveAddress(i, address) */
100 bool result = ipv4->RemoveAddress(index, Ipv4Address("192.168.0.2"));
101 NS_TEST_ASSERT_MSG_EQ(true, result, "Unable to remove Address??");
102 num = interface->GetNAddresses();
103 NS_TEST_ASSERT_MSG_EQ(num, 1, "Should find 1 addresses??");
104
105 /* Remove a non-existent Address */
106 result = ipv4->RemoveAddress(index, Ipv4Address("189.0.0.1"));
107 NS_TEST_ASSERT_MSG_EQ(false, result, "Removed non-existent address??");
108 num = interface->GetNAddresses();
109 NS_TEST_ASSERT_MSG_EQ(num, 1, "Should find 1 addresses??");
110
111 /* Remove a loopback Address */
112 result = ipv4->RemoveAddress(index, Ipv4Address::GetLoopback());
113 NS_TEST_ASSERT_MSG_EQ(false, result, "Able to remove loopback address??");
114 num = interface->GetNAddresses();
115 NS_TEST_ASSERT_MSG_EQ(num, 1, "Should find 1 addresses??");
116
118}
119
120/**
121 * @ingroup internet-test
122 *
123 * @brief IPv4 TestSuite
124 */
126{
127 public:
129 : TestSuite("ipv4-protocol", Type::UNIT)
130 {
131 AddTestCase(new Ipv4L3ProtocolTestCase(), TestCase::Duration::QUICK);
132 }
133};
134
135static IPv4L3ProtocolTestSuite g_ipv4protocolTestSuite; //!< Static variable for test initialization
IPv4 TestSuite.
Definition ipv4-test.cc:126
void DoRun() override
Implementation to actually run this TestCase.
Definition ipv4-test.cc:46
~Ipv4L3ProtocolTestCase() override
Definition ipv4-test.cc:41
AttributeValue implementation for Boolean.
Definition boolean.h:26
Ipv4 addresses are stored in host order in this class.
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.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static constexpr auto UNIT
Definition test.h:1291
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:134
static IPv4L3ProtocolTestSuite g_ipv4protocolTestSuite
Static variable for test initialization.
Definition ipv4-test.cc:135
Every class exported by the ns3 library is enclosed in the ns3 namespace.