A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv6-packet-info-tag-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Hajime Tazaki
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: Hajime Tazaki <tazaki@sfc.wide.ad.jp>
18 */
19
20//-----------------------------------------------------------------------------
21// Unit tests
22//-----------------------------------------------------------------------------
23
24#include "ns3/abort.h"
25#include "ns3/attribute.h"
26#include "ns3/boolean.h"
27#include "ns3/icmpv6-l4-protocol.h"
28#include "ns3/inet6-socket-address.h"
29#include "ns3/internet-stack-helper.h"
30#include "ns3/ipv6-address.h"
31#include "ns3/ipv6-interface.h"
32#include "ns3/ipv6-l3-protocol.h"
33#include "ns3/ipv6-list-routing.h"
34#include "ns3/ipv6-packet-info-tag.h"
35#include "ns3/ipv6-raw-socket-factory.h"
36#include "ns3/ipv6-static-routing.h"
37#include "ns3/log.h"
38#include "ns3/node.h"
39#include "ns3/object-factory.h"
40#include "ns3/simple-net-device-helper.h"
41#include "ns3/simple-net-device.h"
42#include "ns3/simulator.h"
43#include "ns3/socket-factory.h"
44#include "ns3/test.h"
45#include "ns3/traffic-control-layer.h"
46#include "ns3/udp-socket-factory.h"
47#include "ns3/udp-socket.h"
48#include "ns3/uinteger.h"
49
50using namespace ns3;
51
52// static void
53// AddInternetStack (Ptr<Node> node)
54//{
55// Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
56// Ptr<Icmpv6L4Protocol> icmpv6 = CreateObject<Icmpv6L4Protocol> ();
57// node->AggregateObject (ipv6);
58// node->AggregateObject (icmpv6);
59// ipv6->Insert (icmpv6);
60// icmpv6->SetAttribute ("DAD", BooleanValue (false));
61//
62// //Routing for Ipv6
63// Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting> ();
64// ipv6->SetRoutingProtocol (ipv6Routing);
65// Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting> ();
66// ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0);
67//
68// /* register IPv6 extensions and options */
69// ipv6->RegisterExtensions ();
70// ipv6->RegisterOptions ();
71//
72// // Traffic Control
73// Ptr<TrafficControlLayer> tc = CreateObject<TrafficControlLayer> ();
74// node->AggregateObject (tc);
75// }
76
83{
84 public:
86
87 private:
88 void DoRun() override;
93 void RxCb(Ptr<Socket> socket);
99 void DoSendData(Ptr<Socket> socket, std::string to);
100};
101
103 : TestCase("Ipv6PacketInfoTagTest")
104{
105}
106
107void
109{
110 uint32_t availableData;
111 Ptr<Packet> m_receivedPacket;
112
113 availableData = socket->GetRxAvailable();
114 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
115 NS_TEST_ASSERT_MSG_EQ(availableData, m_receivedPacket->GetSize(), "Did not read expected data");
116
118 bool found;
119 found = m_receivedPacket->RemovePacketTag(tag);
120 NS_TEST_ASSERT_MSG_EQ(found, true, "Could not find tag");
121}
122
123void
125{
126 Address realTo = Inet6SocketAddress(Ipv6Address(to.c_str()), 200);
127 if (DynamicCast<UdpSocket>(socket))
128 {
129 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
130 }
131 // Should only Ipv6RawSock
132 else
133 {
134 socket->SendTo(Create<Packet>(123), 0, realTo);
135 }
136}
137
138void
140{
141 Ptr<Node> node0 = CreateObject<Node>();
142 Ptr<Node> node1 = CreateObject<Node>();
143
144 SimpleNetDeviceHelper simpleNetDevHelper;
145 NetDeviceContainer devs = simpleNetDevHelper.Install(NodeContainer(node0, node1));
146 Ptr<SimpleNetDevice> device = DynamicCast<SimpleNetDevice>(devs.Get(0));
147 Ptr<SimpleNetDevice> device2 = DynamicCast<SimpleNetDevice>(devs.Get(1));
148
149 InternetStackHelper internet;
150 internet.SetIpv4StackInstall(false);
151
152 // For Node 0
153 node0->AddDevice(device);
154 internet.Install(node0);
155 Ptr<Ipv6> ipv6 = node0->GetObject<Ipv6>();
156 Ptr<Icmpv6L4Protocol> icmpv6 = node0->GetObject<Icmpv6L4Protocol>();
157 icmpv6->SetAttribute("DAD", BooleanValue(false));
158
159 uint32_t index = ipv6->AddInterface(device);
160 Ipv6InterfaceAddress ifaceAddr1 =
161 Ipv6InterfaceAddress(Ipv6Address("2000:1000:0:2000::1"), Ipv6Prefix(64));
162 ipv6->AddAddress(index, ifaceAddr1);
163 ipv6->SetMetric(index, 1);
164 ipv6->SetUp(index);
165
166 // For Node 1
167 node1->AddDevice(device2);
168 internet.Install(node1);
169 ipv6 = node1->GetObject<Ipv6>();
170 icmpv6 = node0->GetObject<Icmpv6L4Protocol>();
171 icmpv6->SetAttribute("DAD", BooleanValue(false));
172
173 index = ipv6->AddInterface(device2);
174 Ipv6InterfaceAddress ifaceAddr2 =
175 Ipv6InterfaceAddress(Ipv6Address("2000:1000:0:2000::2"), Ipv6Prefix(64));
176 ipv6->AddAddress(index, ifaceAddr2);
177 ipv6->SetMetric(index, 1);
178 ipv6->SetUp(index);
179
180 // ipv6 w rawsocket
182 Ptr<Socket> socket = factory->CreateSocket();
184 socket->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
185 socket->Bind(local);
186 socket->SetRecvPktInfo(true);
187 socket->SetRecvCallback(MakeCallback(&Ipv6PacketInfoTagTest::RxCb, this));
188
189 // receive on loopback
190 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
191 Seconds(0),
193 this,
194 socket,
195 "::1");
197
198 Ptr<SocketFactory> factory2 =
200 Ptr<Socket> socket2 = factory2->CreateSocket();
201 std::stringstream dst;
202 dst << ifaceAddr1.GetAddress();
203 Simulator::ScheduleWithContext(socket2->GetNode()->GetId(),
204 Seconds(0),
206 this,
207 socket,
208 dst.str());
210
211#ifdef UDP6_SUPPORTED
212 // IPv6 test
213 factory = node0->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
214 socket = factory->CreateSocket();
216 socket->Bind(local);
217 socket->SetRecvPktInfo(true);
218 socket->SetRecvCallback(MakeCallback(&Ipv6PacketInfoTagTest::RxCb, this));
219
220 // receive on loopback
221 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
222 Seconds(0),
224 this,
225 socket,
226 "::1");
228
229 factory2 = node1->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
230 socket2 = factory2->CreateSocket();
231 Simulator::ScheduleWithContext(socket2->GetNode()->GetId(),
232 Seconds(0),
234 this,
235 socket,
236 "10.1.1.1");
238
239#endif // UDP6_SUPPORTED
240
242 // IPv6 test
243}
244
251{
252 public:
254
255 private:
256};
257
259 : TestSuite("ipv6-packet-info-tag", UNIT)
260{
262}
263
void RxCb(Ptr< Socket > socket)
Receive callback.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data.
void DoRun() override
Implementation to actually run this TestCase.
a polymophic address class
Definition: address.h:100
AttributeValue implementation for Boolean.
Definition: boolean.h:37
An implementation of the ICMPv6 protocol.
An Inet6 address class.
aggregate IP/TCP/UDP functionality to existing Nodes.
Describes an IPv6 address.
Definition: ipv6-address.h:49
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:82
IPv6 address associated with an interface.
Ipv6Address GetAddress() const
Get the IPv6 address.
This class implements a tag that carries socket ancillary data to the socket interface.
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
static TypeId GetTypeId()
Get the type ID of this class.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:200
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
build a set of SimpleNetDevice objects
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:587
static void Run()
Run the simulation.
Definition: simulator.cc:176
Object to create transport layer instances that provide a socket API to applications.
encapsulates test code
Definition: test.h:1060
@ QUICK
Fast test.
Definition: test.h:1065
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
static TypeId GetTypeId()
Get the type ID.
Hold an unsigned integer type.
Definition: uinteger.h:45
#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:144
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:251
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
static Ipv6PacketInfoTagTestSuite g_packetinfotagTests
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:702