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/inet-socket-address.h"
12#include "ns3/ipv4-interface.h"
13#include "ns3/ipv4-l3-protocol.h"
14#include "ns3/log.h"
15#include "ns3/loopback-net-device.h"
16#include "ns3/node.h"
17#include "ns3/simulator.h"
18#include "ns3/test.h"
19
20using namespace ns3;
21
22/**
23 * @ingroup internet-test
24 *
25 * @brief IPv4 Test
26 */
28{
29 public:
31 ~Ipv4L3ProtocolTestCase() override;
32 void DoRun() override;
33};
34
36 : TestCase("Verify the IPv4 layer 3 protocol")
37{
38}
39
43
44void
46{
51 node->AddDevice(device);
52 interface->SetDevice(device);
53 interface->SetNode(node);
54 uint32_t index = ipv4->AddIpv4Interface(interface);
55 NS_TEST_ASSERT_MSG_EQ(index, 0, "No interface should be found??");
56 interface->SetUp();
57 Ipv4InterfaceAddress ifaceAddr1 = Ipv4InterfaceAddress("192.168.0.1", "255.255.255.0");
58 interface->AddAddress(ifaceAddr1);
59 Ipv4InterfaceAddress ifaceAddr2 = Ipv4InterfaceAddress("192.168.0.2", "255.255.255.0");
60 interface->AddAddress(ifaceAddr2);
61 Ipv4InterfaceAddress ifaceAddr3 = Ipv4InterfaceAddress("10.30.0.1", "255.255.255.0");
62 interface->AddAddress(ifaceAddr3);
63 Ipv4InterfaceAddress ifaceAddr4 = Ipv4InterfaceAddress("250.0.0.1", "255.255.255.0");
64 interface->AddAddress(ifaceAddr4);
65 uint32_t num = interface->GetNAddresses();
66 NS_TEST_ASSERT_MSG_EQ(num, 4, "Should find 4 interfaces??");
67 interface->RemoveAddress(2);
68 num = interface->GetNAddresses();
69 NS_TEST_ASSERT_MSG_EQ(num, 3, "Should find 3 interfaces??");
70 Ipv4InterfaceAddress output = interface->GetAddress(2);
71 NS_TEST_ASSERT_MSG_EQ(ifaceAddr4, output, "The addresses should be identical");
72
73 /* Test Ipv4Interface()::RemoveAddress(address) */
74 output = interface->RemoveAddress(Ipv4Address("250.0.0.1"));
75 NS_TEST_ASSERT_MSG_EQ(ifaceAddr4, output, "Wrong Interface Address Removed??");
76 num = interface->GetNAddresses();
77 NS_TEST_ASSERT_MSG_EQ(num, 2, "Should find 2 addresses??");
78
79 /* Remove a non-existent Address */
80 output = interface->RemoveAddress(Ipv4Address("253.123.9.81"));
81 NS_TEST_ASSERT_MSG_EQ(Ipv4InterfaceAddress(), output, "Removed non-existent address??");
82 num = interface->GetNAddresses();
83 NS_TEST_ASSERT_MSG_EQ(num, 2, "Should find 2 addresses??");
84
85 /* Remove a Loopback Address */
86 output = interface->RemoveAddress(Ipv4Address::GetLoopback());
87 NS_TEST_ASSERT_MSG_EQ(Ipv4InterfaceAddress(), output, "Able to remove loopback address??");
88 num = interface->GetNAddresses();
89 NS_TEST_ASSERT_MSG_EQ(num, 2, "Should find 2 addresses??");
90
91 /* Test Ipv4Address::RemoveAddress(i, address) */
92 bool result = ipv4->RemoveAddress(index, Ipv4Address("192.168.0.2"));
93 NS_TEST_ASSERT_MSG_EQ(true, result, "Unable to remove Address??");
94 num = interface->GetNAddresses();
95 NS_TEST_ASSERT_MSG_EQ(num, 1, "Should find 1 addresses??");
96
97 /* Remove a non-existent Address */
98 result = ipv4->RemoveAddress(index, Ipv4Address("189.0.0.1"));
99 NS_TEST_ASSERT_MSG_EQ(false, result, "Removed non-existent address??");
100 num = interface->GetNAddresses();
101 NS_TEST_ASSERT_MSG_EQ(num, 1, "Should find 1 addresses??");
102
103 /* Remove a loopback Address */
104 result = ipv4->RemoveAddress(index, Ipv4Address::GetLoopback());
105 NS_TEST_ASSERT_MSG_EQ(false, result, "Able to remove loopback address??");
106 num = interface->GetNAddresses();
107 NS_TEST_ASSERT_MSG_EQ(num, 1, "Should find 1 addresses??");
108
110}
111
112/**
113 * @ingroup internet-test
114 *
115 * @brief IPv4 TestSuite
116 */
118{
119 public:
121 : TestSuite("ipv4-protocol", Type::UNIT)
122 {
123 AddTestCase(new Ipv4L3ProtocolTestCase(), TestCase::Duration::QUICK);
124 }
125};
126
127static IPv4L3ProtocolTestSuite g_ipv4protocolTestSuite; //!< Static variable for test initialization
IPv4 TestSuite.
Definition ipv4-test.cc:118
void DoRun() override
Implementation to actually run this TestCase.
Definition ipv4-test.cc:45
~Ipv4L3ProtocolTestCase() override
Definition ipv4-test.cc:40
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:127
Every class exported by the ns3 library is enclosed in the ns3 namespace.