A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
ipv4-test.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License version 2 as
5
* published by the Free Software Foundation;
6
*
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
11
*
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
* Author: Faker Moatamri <faker.moatamri@sophia.inria.fr>
16
*
17
*/
22
#include "ns3/simulator.h"
23
#include "ns3/test.h"
24
#include "ns3/log.h"
25
#include "ns3/inet-socket-address.h"
26
#include "ns3/node.h"
27
28
#include "ns3/ipv4-l3-protocol.h"
29
#include "ns3/arp-l3-protocol.h"
30
#include "ns3/ipv4-interface.h"
31
#include "ns3/loopback-net-device.h"
32
33
using namespace
ns3
;
34
41
class
Ipv4L3ProtocolTestCase
:
public
TestCase
42
{
43
public
:
44
Ipv4L3ProtocolTestCase
();
45
virtual
~
Ipv4L3ProtocolTestCase
();
46
virtual
void
DoRun (
void
);
47
48
};
49
50
Ipv4L3ProtocolTestCase::Ipv4L3ProtocolTestCase
() :
51
TestCase
(
"Verify the IPv4 layer 3 protocol"
)
52
{
53
}
54
55
Ipv4L3ProtocolTestCase::~Ipv4L3ProtocolTestCase
()
56
{
57
}
58
void
59
Ipv4L3ProtocolTestCase::DoRun
(
void
)
60
{
61
Ptr<Node>
node = CreateObject<Node> ();
62
Ptr<Ipv4L3Protocol>
ipv4 = CreateObject<Ipv4L3Protocol> ();
63
Ptr<Ipv4Interface>
interface
=
CreateObject
<
Ipv4Interface
> ();
64
Ptr<LoopbackNetDevice>
device = CreateObject<LoopbackNetDevice> ();
65
node->
AddDevice
(device);
66
interface->SetDevice (device);
67
interface->SetNode (node);
68
uint32_t index = ipv4->
AddIpv4Interface
(interface);
69
NS_TEST_ASSERT_MSG_EQ
(index, 0,
"No interface should be found??"
);
70
interface->SetUp ();
71
Ipv4InterfaceAddress
ifaceAddr1 =
Ipv4InterfaceAddress
(
"192.168.0.1"
,
72
"255.255.255.0"
);
73
interface->AddAddress (ifaceAddr1);
74
Ipv4InterfaceAddress
ifaceAddr2 =
Ipv4InterfaceAddress
(
"192.168.0.2"
,
75
"255.255.255.0"
);
76
interface->AddAddress (ifaceAddr2);
77
Ipv4InterfaceAddress
ifaceAddr3 =
Ipv4InterfaceAddress
(
"10.30.0.1"
,
78
"255.255.255.0"
);
79
interface->AddAddress (ifaceAddr3);
80
Ipv4InterfaceAddress
ifaceAddr4 =
Ipv4InterfaceAddress
(
"250.0.0.1"
,
81
"255.255.255.0"
);
82
interface->AddAddress (ifaceAddr4);
83
uint32_t num = interface->GetNAddresses ();
84
NS_TEST_ASSERT_MSG_EQ
(num, 4,
"Should find 4 interfaces??"
);
85
interface->RemoveAddress (2);
86
num = interface->GetNAddresses ();
87
NS_TEST_ASSERT_MSG_EQ
(num, 3,
"Should find 3 interfaces??"
);
88
Ipv4InterfaceAddress
output = interface->
GetAddress
(2);
89
NS_TEST_ASSERT_MSG_EQ
(ifaceAddr4, output,
90
"The addresses should be identical"
);
91
92
/* Test Ipv4Interface()::RemoveAddress(address) */
93
output = interface->RemoveAddress (
Ipv4Address
(
"250.0.0.1"
));
94
NS_TEST_ASSERT_MSG_EQ
(ifaceAddr4, output,
95
"Wrong Interface Address Removed??"
);
96
num = interface->GetNAddresses ();
97
NS_TEST_ASSERT_MSG_EQ
(num, 2,
"Should find 2 addresses??"
);
98
99
/* Remove a non-existent Address */
100
output = interface->RemoveAddress (
Ipv4Address
(
"253.123.9.81"
));
101
NS_TEST_ASSERT_MSG_EQ
(
Ipv4InterfaceAddress
(), output,
102
"Removed non-existent address??"
);
103
num = interface->GetNAddresses ();
104
NS_TEST_ASSERT_MSG_EQ
(num, 2,
"Should find 2 addresses??"
);
105
106
/* Remove a Loopback Address */
107
output = interface->RemoveAddress (Ipv4Address::GetLoopback ());
108
NS_TEST_ASSERT_MSG_EQ
(
Ipv4InterfaceAddress
(), output,
109
"Able to remove loopback address??"
);
110
num = interface->GetNAddresses ();
111
NS_TEST_ASSERT_MSG_EQ
(num, 2,
"Should find 2 addresses??"
);
112
113
/* Test Ipv4Address::RemoveAddress(i, address) */
114
bool
result = ipv4->
RemoveAddress
(index,
Ipv4Address
115
(
"192.168.0.2"
));
116
NS_TEST_ASSERT_MSG_EQ
(
true
, result,
"Unable to remove Address??"
);
117
num = interface->GetNAddresses ();
118
NS_TEST_ASSERT_MSG_EQ
(num, 1,
"Should find 1 addresses??"
);
119
120
/* Remove a non-existent Address */
121
result = ipv4->
RemoveAddress
(index,
Ipv4Address
(
"189.0.0.1"
));
122
NS_TEST_ASSERT_MSG_EQ
(
false
, result,
123
"Removed non-existent address??"
);
124
num = interface->GetNAddresses ();
125
NS_TEST_ASSERT_MSG_EQ
(num, 1,
"Should find 1 addresses??"
);
126
127
/* Remove a loopback Address */
128
result = ipv4->
RemoveAddress
(index, Ipv4Address::GetLoopback ());
129
NS_TEST_ASSERT_MSG_EQ
(
false
, result,
130
"Able to remove loopback address??"
);
131
num = interface->GetNAddresses ();
132
NS_TEST_ASSERT_MSG_EQ
(num, 1,
"Should find 1 addresses??"
);
133
134
Simulator::Destroy ();
135
}
136
137
144
class
IPv4L3ProtocolTestSuite
:
public
TestSuite
145
{
146
public
:
147
IPv4L3ProtocolTestSuite
() :
148
TestSuite
(
"ipv4-protocol"
,
UNIT
)
149
{
150
AddTestCase
(
new
Ipv4L3ProtocolTestCase
(), TestCase::QUICK);
151
}
152
};
153
154
static
IPv4L3ProtocolTestSuite
g_ipv4protocolTestSuite
;
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:299
Ipv4L3ProtocolTestCase::Ipv4L3ProtocolTestCase
Ipv4L3ProtocolTestCase()
Definition:
ipv4-test.cc:50
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ipv4L3Protocol::RemoveAddress
bool RemoveAddress(uint32_t interfaceIndex, uint32_t addressIndex)
Remove the address at addressIndex on named interface.
Definition:
ipv4-l3-protocol.cc:1184
ns3::Ipv4InterfaceAddress::GetAddress
Ipv4Address GetAddress(void) const
Get the local address.
Definition:
ipv4-interface-address.cc:81
ns3::Ipv4Interface
The IPv4 representation of a network interface.
Definition:
ipv4-interface.h:54
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition:
ipv4-address.h:41
Ipv4L3ProtocolTestCase
IPv4 Test.
Definition:
ipv4-test.cc:42
ns3::TestCase
encapsulates test code
Definition:
test.h:1154
IPv4L3ProtocolTestSuite
IPv4 TestSuite.
Definition:
ipv4-test.cc:145
ns3::Ptr< Node >
Ipv4L3ProtocolTestCase::~Ipv4L3ProtocolTestCase
virtual ~Ipv4L3ProtocolTestCase()
Definition:
ipv4-test.cc:55
ns3::Ipv4InterfaceAddress
a class to store IPv4 address information on an interface
Definition:
ipv4-interface-address.h:44
g_ipv4protocolTestSuite
static IPv4L3ProtocolTestSuite g_ipv4protocolTestSuite
Static variable for test initialization.
Definition:
ipv4-test.cc:154
ns3::Node::AddDevice
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition:
node.cc:130
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1344
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:166
ns3::Ipv4L3Protocol::AddIpv4Interface
uint32_t AddIpv4Interface(Ptr< Ipv4Interface > interface)
Add an IPv4 interface to the stack.
Definition:
ipv4-l3-protocol.cc:419
ns3::TestSuite::UNIT
@ UNIT
This test suite implements a Unit Test.
Definition:
test.h:1353
Ipv4L3ProtocolTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
ipv4-test.cc:59
IPv4L3ProtocolTestSuite::IPv4L3ProtocolTestSuite
IPv4L3ProtocolTestSuite()
Definition:
ipv4-test.cc:147
ns3::CreateObject
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition:
object.h:576
src
internet
test
ipv4-test.cc
Generated on Fri Oct 1 2021 17:03:11 for ns-3 by
1.8.20