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
ipv6-packet-info-tag-test-suite.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2010 Hajime Tazaki
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Authors: Hajime Tazaki <tazaki@sfc.wide.ad.jp>
19
*/
20
21
//-----------------------------------------------------------------------------
22
// Unit tests
23
//-----------------------------------------------------------------------------
24
25
#include "ns3/test.h"
26
#include "ns3/ipv6-address.h"
27
#include "ns3/ipv6-packet-info-tag.h"
28
#include "ns3/log.h"
29
#include "ns3/abort.h"
30
#include "ns3/attribute.h"
31
#include "ns3/simple-net-device.h"
32
#include "ns3/object-factory.h"
33
#include "ns3/socket-factory.h"
34
#include "ns3/udp-socket-factory.h"
35
#include "ns3/udp-socket.h"
36
#include "ns3/ipv6-l3-protocol.h"
37
#include "ns3/ipv6-raw-socket-factory.h"
38
#include "ns3/ipv6-interface.h"
39
#include "ns3/icmpv6-l4-protocol.h"
40
#include "ns3/ipv6-static-routing.h"
41
#include "ns3/ipv6-list-routing.h"
42
#include "ns3/inet6-socket-address.h"
43
#include "ns3/simulator.h"
44
#include "ns3/uinteger.h"
45
#include "ns3/boolean.h"
46
#include "ns3/node.h"
47
48
namespace
ns3 {
49
50
static
void
51
AddInternetStack
(
Ptr<Node>
node)
52
{
53
Ptr<Ipv6L3Protocol>
ipv6 = CreateObject<Ipv6L3Protocol> ();
54
Ptr<Icmpv6L4Protocol>
icmpv6 = CreateObject<Icmpv6L4Protocol> ();
55
node->
AggregateObject
(ipv6);
56
node->
AggregateObject
(icmpv6);
57
ipv6->
Insert
(icmpv6);
58
icmpv6->SetAttribute (
"DAD"
,
BooleanValue
(
false
));
59
60
//Routing for Ipv6
61
Ptr<Ipv6ListRouting>
ipv6Routing = CreateObject<Ipv6ListRouting> ();
62
ipv6->
SetRoutingProtocol
(ipv6Routing);
63
Ptr<Ipv6StaticRouting>
ipv6staticRouting = CreateObject<Ipv6StaticRouting> ();
64
ipv6Routing->
AddRoutingProtocol
(ipv6staticRouting, 0);
65
66
/* register IPv6 extensions and options */
67
ipv6->
RegisterExtensions
();
68
ipv6->
RegisterOptions
();
69
}
70
71
class
Ipv6PacketInfoTagTest
:
public
TestCase
72
{
73
public
:
74
Ipv6PacketInfoTagTest
();
75
private
:
76
virtual
void
DoRun
(
void
);
77
void
RxCb
(
Ptr<Socket>
socket);
78
void
DoSendData
(
Ptr<Socket>
socket, std::string to);
79
};
80
81
Ipv6PacketInfoTagTest::Ipv6PacketInfoTagTest
()
82
:
TestCase
(
"Ipv6PacketInfoTagTest"
)
83
{
84
}
85
86
void
87
Ipv6PacketInfoTagTest::RxCb
(
Ptr<Socket>
socket)
88
{
89
uint32_t availableData;
90
Ptr<Packet>
m_receivedPacket;
91
92
availableData = socket->
GetRxAvailable
();
93
m_receivedPacket = socket->
Recv
(std::numeric_limits<uint32_t>::max (), 0);
94
NS_TEST_ASSERT_MSG_EQ
(availableData, m_receivedPacket->
GetSize
(),
"Did not read expected data"
);
95
96
Ipv6PacketInfoTag
tag;
97
bool
found;
98
found = m_receivedPacket->
RemovePacketTag
(tag);
99
NS_TEST_ASSERT_MSG_EQ
(found,
true
,
"Could not find tag"
);
100
}
101
102
void
103
Ipv6PacketInfoTagTest::DoSendData
(
Ptr<Socket>
socket, std::string to)
104
{
105
Address
realTo =
Inet6SocketAddress
(
Ipv6Address
(to.c_str ()), 200);
106
if
(DynamicCast<UdpSocket> (socket) != 0)
107
{
108
NS_TEST_EXPECT_MSG_EQ
(socket->
SendTo
(Create<Packet> (123), 0, realTo),
109
123,
"XXX"
);
110
}
111
// Should only Ipv6RawSock
112
else
113
{
114
socket->
SendTo
(Create<Packet> (123), 0, realTo);
115
}
116
}
117
118
void
119
Ipv6PacketInfoTagTest::DoRun
(
void
)
120
{
121
Ptr<Node>
node0 = CreateObject<Node> ();
122
Ptr<Node>
node1 = CreateObject<Node> ();
123
124
Ptr<SimpleNetDevice>
device = CreateObject<SimpleNetDevice> ();
125
Ptr<SimpleNetDevice>
device2 = CreateObject<SimpleNetDevice> ();
126
127
// For Node 0
128
node0->
AddDevice
(device);
129
AddInternetStack
(node0);
130
Ptr<Ipv6>
ipv6 = node0->
GetObject
<
Ipv6
> ();
131
132
uint32_t index = ipv6->
AddInterface
(device);
133
Ipv6InterfaceAddress
ifaceAddr1 =
Ipv6InterfaceAddress
(
Ipv6Address
(
"2000:1000:0:2000::1"
),
134
Ipv6Prefix
(64));
135
ipv6->
AddAddress
(index, ifaceAddr1);
136
ipv6->
SetMetric
(index, 1);
137
ipv6->
SetUp
(index);
138
139
// For Node 1
140
node1->AddDevice (device2);
141
AddInternetStack
(node1);
142
ipv6 = node1->
GetObject
<
Ipv6
> ();
143
144
index = ipv6->
AddInterface
(device2);
145
Ipv6InterfaceAddress
ifaceAddr2 =
Ipv6InterfaceAddress
(
Ipv6Address
(
"2000:1000:0:2000::2"
),
146
Ipv6Prefix
(64));
147
ipv6->
AddAddress
(index, ifaceAddr2);
148
ipv6->
SetMetric
(index, 1);
149
ipv6->
SetUp
(index);
150
151
// ipv6 w rawsocket
152
Ptr<SocketFactory>
factory = node0->
GetObject
<
SocketFactory
> (
Ipv6RawSocketFactory::GetTypeId
());
153
Ptr<Socket>
socket = factory->CreateSocket ();
154
Inet6SocketAddress
local =
Inet6SocketAddress
(
Ipv6Address::GetAny
(), 0);
155
socket->SetAttribute (
"Protocol"
,
UintegerValue
(
Ipv6Header::IPV6_ICMPV6
));
156
socket->Bind (local);
157
socket->SetRecvPktInfo (
true
);
158
socket->SetRecvCallback (
MakeCallback
(&
ns3::Ipv6PacketInfoTagTest::RxCb
,
this
));
159
160
// receive on loopback
161
Simulator::ScheduleWithContext
(socket->GetNode ()->GetId (),
Seconds
(0),
162
&
Ipv6PacketInfoTagTest::DoSendData
,
this
, socket,
"::1"
);
163
Simulator::Run
();
164
165
Ptr<SocketFactory>
factory2 = node1->GetObject<
SocketFactory
> (
Ipv6RawSocketFactory::GetTypeId
());
166
Ptr<Socket>
socket2 = factory2->CreateSocket ();
167
std::stringstream dst;
168
dst << ifaceAddr1.
GetAddress
();
169
Simulator::ScheduleWithContext
(socket2->GetNode ()->GetId (),
Seconds
(0),
170
&
Ipv6PacketInfoTagTest::DoSendData
,
this
, socket,
171
dst.str ());
172
Simulator::Run
();
173
174
#ifdef UDP6_SUPPORTED
175
// IPv6 test
176
factory = node0->
GetObject
<
SocketFactory
> (
UdpSocketFactory::GetTypeId
());
177
socket = factory->CreateSocket ();
178
local =
Inet6SocketAddress
(
Ipv6Address::GetAny
(), 200);
179
socket->Bind (local);
180
socket->SetRecvPktInfo (
true
);
181
socket->SetRecvCallback (
MakeCallback
(&
ns3::Ipv6PacketInfoTagTest::RxCb
,
this
));
182
183
// receive on loopback
184
Simulator::ScheduleWithContext
(socket->GetNode ()->GetId (),
Seconds
(0),
185
&
Ipv6PacketInfoTagTest::DoSendData
,
this
, socket,
"::1"
);
186
Simulator::Run
();
187
188
factory2 = node1->GetObject<
SocketFactory
> (
UdpSocketFactory::GetTypeId
());
189
socket2 = factory2->CreateSocket ();
190
Simulator::ScheduleWithContext
(socket2->GetNode ()->GetId (),
Seconds
(0),
191
&
Ipv6PacketInfoTagTest::DoSendData
,
this
, socket,
"10.1.1.1"
);
192
Simulator::Run
();
193
194
#endif // UDP6_SUPPORTED
195
196
Simulator::Destroy
();
197
// IPv6 test
198
}
199
200
static
class
Ipv6PacketInfoTagTestSuite
:
public
TestSuite
201
{
202
public
:
203
Ipv6PacketInfoTagTestSuite
();
204
private
:
205
}
g_packetinfotagTests
;
206
207
Ipv6PacketInfoTagTestSuite::Ipv6PacketInfoTagTestSuite
()
208
:
TestSuite
(
"ipv6-packet-info-tag"
, UNIT)
209
{
210
AddTestCase
(
new
Ipv6PacketInfoTagTest
());
211
}
212
213
214
}
215
src
internet
test
ipv6-packet-info-tag-test-suite.cc
Generated on Tue Oct 9 2012 16:45:40 for ns-3 by
1.8.1.2