A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
21
using namespace
ns3
;
22
23
/**
24
* @ingroup internet-test
25
*
26
* @brief IPv4 Test
27
*/
28
class
Ipv4L3ProtocolTestCase
:
public
TestCase
29
{
30
public
:
31
Ipv4L3ProtocolTestCase
();
32
~Ipv4L3ProtocolTestCase
()
override
;
33
void
DoRun
()
override
;
34
};
35
36
Ipv4L3ProtocolTestCase::Ipv4L3ProtocolTestCase
()
37
:
TestCase
(
"Verify the IPv4 layer 3 protocol"
)
38
{
39
}
40
41
Ipv4L3ProtocolTestCase::~Ipv4L3ProtocolTestCase
()
42
{
43
}
44
45
void
46
Ipv4L3ProtocolTestCase::DoRun
()
47
{
48
Ptr<Node>
node =
CreateObject<Node>
();
49
Ptr<Ipv4L3Protocol>
ipv4 =
CreateObject<Ipv4L3Protocol>
();
50
Ptr<Ipv4Interface>
interface
=
CreateObject
<
Ipv4Interface
>();
51
Ptr<SimpleNetDevice>
device =
CreateObject<SimpleNetDevice>
();
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
117
Simulator::Destroy
();
118
}
119
120
/**
121
* @ingroup internet-test
122
*
123
* @brief IPv4 TestSuite
124
*/
125
class
IPv4L3ProtocolTestSuite
:
public
TestSuite
126
{
127
public
:
128
IPv4L3ProtocolTestSuite
()
129
:
TestSuite
(
"ipv4-protocol"
,
Type
::
UNIT
)
130
{
131
AddTestCase
(
new
Ipv4L3ProtocolTestCase
(), TestCase::Duration::QUICK);
132
}
133
};
134
135
static
IPv4L3ProtocolTestSuite
g_ipv4protocolTestSuite
;
//!< Static variable for test initialization
IPv4L3ProtocolTestSuite
IPv4 TestSuite.
Definition
ipv4-test.cc:126
IPv4L3ProtocolTestSuite::IPv4L3ProtocolTestSuite
IPv4L3ProtocolTestSuite()
Definition
ipv4-test.cc:128
Ipv4L3ProtocolTestCase
IPv4 Test.
Definition
ipv4-test.cc:29
Ipv4L3ProtocolTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
ipv4-test.cc:46
Ipv4L3ProtocolTestCase::Ipv4L3ProtocolTestCase
Ipv4L3ProtocolTestCase()
Definition
ipv4-test.cc:36
Ipv4L3ProtocolTestCase::~Ipv4L3ProtocolTestCase
~Ipv4L3ProtocolTestCase() override
Definition
ipv4-test.cc:41
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition
boolean.h:26
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition
ipv4-address.h:31
ns3::Ipv4Address::GetLoopback
static Ipv4Address GetLoopback()
Definition
ipv4-address.cc:392
ns3::Ipv4InterfaceAddress
a class to store IPv4 address information on an interface
Definition
ipv4-interface-address.h:34
ns3::Ipv4InterfaceAddress::GetAddress
Ipv4Address GetAddress() const
Get the local address.
Definition
ipv4-interface-address.cc:71
ns3::Ipv4Interface
The IPv4 representation of a network interface.
Definition
ipv4-interface.h:45
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
mpi-test-fixtures.h:37
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1274
ns3::TestSuite::UNIT
static constexpr auto UNIT
Definition
test.h:1291
uint32_t
ns3::CreateObject
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition
object.h:619
NS_TEST_ASSERT_MSG_EQ
#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
g_ipv4protocolTestSuite
static IPv4L3ProtocolTestSuite g_ipv4protocolTestSuite
Static variable for test initialization.
Definition
ipv4-test.cc:135
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
internet
test
ipv4-test.cc
Generated on Tue Apr 29 2025 18:20:45 for ns-3 by
1.11.0