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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Hajime Tazaki <tazaki@sfc.wide.ad.jp>
7 */
8
9//-----------------------------------------------------------------------------
10// Unit tests
11//-----------------------------------------------------------------------------
12
13#include "ns3/abort.h"
14#include "ns3/attribute.h"
15#include "ns3/boolean.h"
16#include "ns3/icmpv6-l4-protocol.h"
17#include "ns3/inet6-socket-address.h"
18#include "ns3/internet-stack-helper.h"
19#include "ns3/ipv6-address.h"
20#include "ns3/ipv6-interface.h"
21#include "ns3/ipv6-l3-protocol.h"
22#include "ns3/ipv6-list-routing.h"
23#include "ns3/ipv6-packet-info-tag.h"
24#include "ns3/ipv6-raw-socket-factory.h"
25#include "ns3/ipv6-static-routing.h"
26#include "ns3/log.h"
27#include "ns3/node.h"
28#include "ns3/object-factory.h"
29#include "ns3/simple-net-device-helper.h"
30#include "ns3/simple-net-device.h"
31#include "ns3/simulator.h"
32#include "ns3/socket-factory.h"
33#include "ns3/test.h"
34#include "ns3/traffic-control-layer.h"
35#include "ns3/udp-socket-factory.h"
36#include "ns3/udp-socket.h"
37#include "ns3/uinteger.h"
38
39using namespace ns3;
40
41// static void
42// AddInternetStack (Ptr<Node> node)
43//{
44// Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
45// Ptr<Icmpv6L4Protocol> icmpv6 = CreateObject<Icmpv6L4Protocol> ();
46// node->AggregateObject (ipv6);
47// node->AggregateObject (icmpv6);
48// ipv6->Insert (icmpv6);
49// icmpv6->SetAttribute ("DAD", BooleanValue (false));
50//
51// //Routing for Ipv6
52// Ptr<Ipv6ListRouting> ipv6Routing = CreateObject<Ipv6ListRouting> ();
53// ipv6->SetRoutingProtocol (ipv6Routing);
54// Ptr<Ipv6StaticRouting> ipv6staticRouting = CreateObject<Ipv6StaticRouting> ();
55// ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0);
56//
57// /* register IPv6 extensions and options */
58// ipv6->RegisterExtensions ();
59// ipv6->RegisterOptions ();
60//
61// // Traffic Control
62// Ptr<TrafficControlLayer> tc = CreateObject<TrafficControlLayer> ();
63// node->AggregateObject (tc);
64// }
65
66/**
67 * \ingroup internet-test
68 *
69 * \brief IPv6 PacketInfoTag Test
70 */
72{
73 public:
75
76 private:
77 void DoRun() override;
78 /**
79 * \brief Receive callback.
80 * \param socket Receiving socket.
81 */
82 void RxCb(Ptr<Socket> socket);
83 /**
84 * \brief Send data.
85 * \param socket Sending socket.
86 * \param to Destination address.
87 */
88 void DoSendData(Ptr<Socket> socket, std::string to);
89};
90
92 : TestCase("Ipv6PacketInfoTagTest")
93{
94}
95
96void
98{
99 uint32_t availableData;
100 Ptr<Packet> m_receivedPacket;
101
102 availableData = socket->GetRxAvailable();
103 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
104 NS_TEST_ASSERT_MSG_EQ(availableData, m_receivedPacket->GetSize(), "Did not read expected data");
105
107 bool found;
108 found = m_receivedPacket->RemovePacketTag(tag);
109 NS_TEST_ASSERT_MSG_EQ(found, true, "Could not find tag");
110}
111
112void
114{
115 Address realTo = Inet6SocketAddress(Ipv6Address(to.c_str()), 200);
116 if (DynamicCast<UdpSocket>(socket))
117 {
118 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
119 }
120 // Should only Ipv6RawSock
121 else
122 {
123 socket->SendTo(Create<Packet>(123), 0, realTo);
124 }
125}
126
127void
129{
132
133 SimpleNetDeviceHelper simpleNetDevHelper;
134 NetDeviceContainer devs = simpleNetDevHelper.Install(NodeContainer(node0, node1));
137
138 InternetStackHelper internet;
139 internet.SetIpv4StackInstall(false);
140
141 // For Node 0
142 node0->AddDevice(device);
143 internet.Install(node0);
144 Ptr<Ipv6> ipv6 = node0->GetObject<Ipv6>();
145 Ptr<Icmpv6L4Protocol> icmpv6 = node0->GetObject<Icmpv6L4Protocol>();
146 icmpv6->SetAttribute("DAD", BooleanValue(false));
147
148 uint32_t index = ipv6->AddInterface(device);
149 Ipv6InterfaceAddress ifaceAddr1 =
150 Ipv6InterfaceAddress(Ipv6Address("2000:1000:0:2000::1"), Ipv6Prefix(64));
151 ipv6->AddAddress(index, ifaceAddr1);
152 ipv6->SetMetric(index, 1);
153 ipv6->SetUp(index);
154
155 // For Node 1
156 node1->AddDevice(device2);
157 internet.Install(node1);
158 ipv6 = node1->GetObject<Ipv6>();
159 icmpv6 = node0->GetObject<Icmpv6L4Protocol>();
160 icmpv6->SetAttribute("DAD", BooleanValue(false));
161
162 index = ipv6->AddInterface(device2);
163 Ipv6InterfaceAddress ifaceAddr2 =
164 Ipv6InterfaceAddress(Ipv6Address("2000:1000:0:2000::2"), Ipv6Prefix(64));
165 ipv6->AddAddress(index, ifaceAddr2);
166 ipv6->SetMetric(index, 1);
167 ipv6->SetUp(index);
168
169 // ipv6 w rawsocket
171 Ptr<Socket> socket = factory->CreateSocket();
173 socket->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
174 socket->Bind(local);
175 socket->SetRecvPktInfo(true);
176 socket->SetRecvCallback(MakeCallback(&Ipv6PacketInfoTagTest::RxCb, this));
177
178 // receive on loopback
179 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
180 Seconds(0),
182 this,
183 socket,
184 "::1");
186
187 Ptr<SocketFactory> factory2 =
189 Ptr<Socket> socket2 = factory2->CreateSocket();
190 std::stringstream dst;
191 dst << ifaceAddr1.GetAddress();
192 Simulator::ScheduleWithContext(socket2->GetNode()->GetId(),
193 Seconds(0),
195 this,
196 socket,
197 dst.str());
199
200#ifdef UDP6_SUPPORTED
201 // IPv6 test
202 factory = node0->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
203 socket = factory->CreateSocket();
205 socket->Bind(local);
206 socket->SetRecvPktInfo(true);
207 socket->SetRecvCallback(MakeCallback(&Ipv6PacketInfoTagTest::RxCb, this));
208
209 // receive on loopback
210 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
211 Seconds(0),
213 this,
214 socket,
215 "::1");
217
218 factory2 = node1->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
219 socket2 = factory2->CreateSocket();
220 Simulator::ScheduleWithContext(socket2->GetNode()->GetId(),
221 Seconds(0),
223 this,
224 socket,
225 "10.1.1.1");
227
228#endif // UDP6_SUPPORTED
229
231 // IPv6 test
232}
233
234/**
235 * \ingroup internet-test
236 *
237 * \brief IPv6 PacketInfoTag TestSuite
238 */
240{
241 public:
243
244 private:
245};
246
248 : TestSuite("ipv6-packet-info-tag", Type::UNIT)
249{
250 AddTestCase(new Ipv6PacketInfoTagTest(), TestCase::Duration::QUICK);
251}
252
253static Ipv6PacketInfoTagTestSuite g_packetinfotagTests; //!< Static variable for test initialization
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:90
An implementation of the ICMPv6 protocol.
An Inet6 address class.
aggregate IP/TCP/UDP functionality to existing Nodes.
Describes an IPv6 address.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition ipv6.h:71
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.
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.
Smart pointer class similar to boost::intrusive_ptr.
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:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static void Run()
Run the simulation.
Definition simulator.cc:167
Object to create transport layer instances that provide a socket API to applications.
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static TypeId GetTypeId()
Get the type ID.
Hold an unsigned integer type.
Definition uinteger.h:34
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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
#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:241
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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:684
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580