A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
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
namespace
ns3 {
34
35
class
Ipv4L3ProtocolTestCase
:
public
TestCase
36
{
37
public
:
41
Ipv4L3ProtocolTestCase
();
45
virtual
46
~Ipv4L3ProtocolTestCase
();
51
virtual
void
52
DoRun
(
void
);
53
54
};
55
56
Ipv4L3ProtocolTestCase::Ipv4L3ProtocolTestCase
() :
57
TestCase
(
"Verify the IPv4 layer 3 protocol"
)
58
{
59
}
60
61
Ipv4L3ProtocolTestCase::~Ipv4L3ProtocolTestCase
()
62
{
63
}
64
void
65
Ipv4L3ProtocolTestCase::DoRun
(
void
)
66
{
67
Ptr<Node>
node = CreateObject<Node> ();
68
Ptr<Ipv4L3Protocol>
ipv4 = CreateObject<Ipv4L3Protocol> ();
69
Ptr<Ipv4Interface>
interface
=
CreateObject
<Ipv4Interface> ();
70
Ptr<LoopbackNetDevice>
device = CreateObject<LoopbackNetDevice> ();
71
node->
AddDevice
(device);
72
interface->SetDevice (device);
73
interface->SetNode (node);
74
uint32_t index = ipv4->AddIpv4Interface (interface);
75
NS_TEST_ASSERT_MSG_EQ
(index, 0,
"No interface should be found??"
);
76
interface->SetUp ();
77
Ipv4InterfaceAddress
ifaceAddr1 =
Ipv4InterfaceAddress
(
"192.168.0.1"
,
78
"255.255.255.0"
);
79
interface->AddAddress (ifaceAddr1);
80
Ipv4InterfaceAddress
ifaceAddr2 =
Ipv4InterfaceAddress
(
"192.168.0.2"
,
81
"255.255.255.0"
);
82
interface->AddAddress (ifaceAddr2);
83
Ipv4InterfaceAddress
ifaceAddr3 =
Ipv4InterfaceAddress
(
"10.30.0.1"
,
84
"255.255.255.0"
);
85
interface->AddAddress (ifaceAddr3);
86
Ipv4InterfaceAddress
ifaceAddr4 =
Ipv4InterfaceAddress
(
"250.0.0.1"
,
87
"255.255.255.0"
);
88
interface->AddAddress (ifaceAddr4);
89
uint32_t num = interface->GetNAddresses ();
90
NS_TEST_ASSERT_MSG_EQ
(num, 4,
"Should find 4 interfaces??"
);
91
interface->RemoveAddress (2);
92
num = interface->GetNAddresses ();
93
NS_TEST_ASSERT_MSG_EQ
(num, 3,
"Should find 3 interfaces??"
);
94
Ipv4InterfaceAddress
output
= interface->GetAddress (2);
95
NS_TEST_ASSERT_MSG_EQ
(ifaceAddr4, output,
96
"The addresses should be identical"
);
97
98
/* Test Ipv4Interface()::RemoveAddress(address) */
99
output = interface->RemoveAddress (
Ipv4Address
(
"250.0.0.1"
));
100
NS_TEST_ASSERT_MSG_EQ
(ifaceAddr4, output,
101
"Wrong Interface Address Removed??"
);
102
num = interface->GetNAddresses ();
103
NS_TEST_ASSERT_MSG_EQ
(num, 2,
"Should find 2 addresses??"
);
104
105
/* Remove a non-existent Address */
106
output = interface->RemoveAddress (
Ipv4Address
(
"253.123.9.81"
));
107
NS_TEST_ASSERT_MSG_EQ
(
Ipv4InterfaceAddress
(), output,
108
"Removed non-existent address??"
);
109
num = interface->GetNAddresses ();
110
NS_TEST_ASSERT_MSG_EQ
(num, 2,
"Should find 2 addresses??"
);
111
112
/* Remove a Loopback Address */
113
output = interface->RemoveAddress (
Ipv4Address::GetLoopback
());
114
NS_TEST_ASSERT_MSG_EQ
(
Ipv4InterfaceAddress
(), output,
115
"Able to remove loopback address??"
);
116
num = interface->GetNAddresses ();
117
NS_TEST_ASSERT_MSG_EQ
(num, 2,
"Should find 2 addresses??"
);
118
119
/* Test Ipv4Address::RemoveAddress(i, addresss) */
120
bool
result = ipv4->RemoveAddress (index,
Ipv4Address
121
(
"192.168.0.2"
));
122
NS_TEST_ASSERT_MSG_EQ
(
true
, result,
"Unable to remove Address??"
);
123
num = interface->GetNAddresses ();
124
NS_TEST_ASSERT_MSG_EQ
(num, 1,
"Should find 1 addresses??"
);
125
126
/* Remove a non-existent Address */
127
result = ipv4->RemoveAddress (index,
Ipv4Address
(
"189.0.0.1"
));
128
NS_TEST_ASSERT_MSG_EQ
(
false
, result,
129
"Removed non-existent address??"
);
130
num = interface->GetNAddresses ();
131
NS_TEST_ASSERT_MSG_EQ
(num, 1,
"Should find 1 addresses??"
);
132
133
/* Remove a loopback Address */
134
result = ipv4->RemoveAddress (index,
Ipv4Address::GetLoopback
());
135
NS_TEST_ASSERT_MSG_EQ
(
false
, result,
136
"Able to remove loopback address??"
);
137
num = interface->GetNAddresses ();
138
NS_TEST_ASSERT_MSG_EQ
(num, 1,
"Should find 1 addresses??"
);
139
140
Simulator::Destroy
();
141
}
142
143
144
static
class
IPv4L3ProtocolTestSuite
:
public
TestSuite
145
{
146
public
:
147
IPv4L3ProtocolTestSuite
() :
148
TestSuite
(
"ipv4-protocol"
,
UNIT
)
149
{
150
AddTestCase
(
new
Ipv4L3ProtocolTestCase
(),
TestCase::QUICK
);
151
}
152
}
g_ipv4protocolTestSuite
;
153
154
}
// namespace ns3
src
internet
test
ipv4-test.cc
Generated on Fri Aug 30 2013 01:42:53 for ns-3 by
1.8.1.2