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/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
20
using namespace
ns3
;
21
22
/**
23
* @ingroup internet-test
24
*
25
* @brief IPv4 Test
26
*/
27
class
Ipv4L3ProtocolTestCase
:
public
TestCase
28
{
29
public
:
30
Ipv4L3ProtocolTestCase
();
31
~Ipv4L3ProtocolTestCase
()
override
;
32
void
DoRun
()
override
;
33
};
34
35
Ipv4L3ProtocolTestCase::Ipv4L3ProtocolTestCase
()
36
:
TestCase
(
"Verify the IPv4 layer 3 protocol"
)
37
{
38
}
39
40
Ipv4L3ProtocolTestCase::~Ipv4L3ProtocolTestCase
()
41
{
42
}
43
44
void
45
Ipv4L3ProtocolTestCase::DoRun
()
46
{
47
Ptr<Node>
node =
CreateObject<Node>
();
48
Ptr<Ipv4L3Protocol>
ipv4 =
CreateObject<Ipv4L3Protocol>
();
49
Ptr<Ipv4Interface>
interface
=
CreateObject
<
Ipv4Interface
>();
50
Ptr<LoopbackNetDevice>
device =
CreateObject<LoopbackNetDevice>
();
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
109
Simulator::Destroy
();
110
}
111
112
/**
113
* @ingroup internet-test
114
*
115
* @brief IPv4 TestSuite
116
*/
117
class
IPv4L3ProtocolTestSuite
:
public
TestSuite
118
{
119
public
:
120
IPv4L3ProtocolTestSuite
()
121
:
TestSuite
(
"ipv4-protocol"
,
Type
::
UNIT
)
122
{
123
AddTestCase
(
new
Ipv4L3ProtocolTestCase
(), TestCase::Duration::QUICK);
124
}
125
};
126
127
static
IPv4L3ProtocolTestSuite
g_ipv4protocolTestSuite
;
//!< Static variable for test initialization
IPv4L3ProtocolTestSuite
IPv4 TestSuite.
Definition
ipv4-test.cc:118
IPv4L3ProtocolTestSuite::IPv4L3ProtocolTestSuite
IPv4L3ProtocolTestSuite()
Definition
ipv4-test.cc:120
Ipv4L3ProtocolTestCase
IPv4 Test.
Definition
ipv4-test.cc:28
Ipv4L3ProtocolTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
ipv4-test.cc:45
Ipv4L3ProtocolTestCase::Ipv4L3ProtocolTestCase
Ipv4L3ProtocolTestCase()
Definition
ipv4-test.cc:35
Ipv4L3ProtocolTestCase::~Ipv4L3ProtocolTestCase
~Ipv4L3ProtocolTestCase() override
Definition
ipv4-test.cc:40
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:384
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:44
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:127
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
internet
test
ipv4-test.cc
Generated on Wed Dec 11 2024 09:20:50 for ns-3 by
1.11.0