A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-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/arp-l3-protocol.h"
15#include "ns3/attribute.h"
16#include "ns3/icmpv4-l4-protocol.h"
17#include "ns3/inet-socket-address.h"
18#include "ns3/internet-stack-helper.h"
19#include "ns3/ipv4-address.h"
20#include "ns3/ipv4-interface.h"
21#include "ns3/ipv4-l3-protocol.h"
22#include "ns3/ipv4-list-routing.h"
23#include "ns3/ipv4-packet-info-tag.h"
24#include "ns3/ipv4-raw-socket-factory.h"
25#include "ns3/ipv4-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/tcp-l4-protocol.h"
34#include "ns3/test.h"
35#include "ns3/traffic-control-layer.h"
36#include "ns3/udp-l4-protocol.h"
37#include "ns3/udp-socket-factory.h"
38#include "ns3/udp-socket.h"
39
40#include <string>
41
42using namespace ns3;
43
44/**
45 * @ingroup internet-test
46 *
47 * @brief IPv4 PacketInfoTag Test
48 */
50{
51 public:
53
54 private:
55 void DoRun() override;
56
57 /**
58 * @brief Receive callback.
59 * @param socket Receiving socket.
60 */
61 void RxCb(Ptr<Socket> socket);
62 /**
63 * @brief Send data.
64 * @param socket Sending socket.
65 * @param to Destination address.
66 */
67 void DoSendData(Ptr<Socket> socket, std::string to);
68};
69
71 : TestCase("Ipv4PacketInfoTagTest")
72{
73}
74
75void
77{
78 uint32_t availableData;
79 Ptr<Packet> m_receivedPacket;
80
81 availableData = socket->GetRxAvailable();
82 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
83 NS_TEST_ASSERT_MSG_EQ(availableData, m_receivedPacket->GetSize(), "Did not read expected data");
84
86 bool found;
87 found = m_receivedPacket->RemovePacketTag(tag);
88 NS_TEST_ASSERT_MSG_EQ(found, true, "Could not find tag");
89}
90
91void
93{
94 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 200);
95 if (DynamicCast<UdpSocket>(socket))
96 {
97 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
98 }
99 // Should only Ipv4RawSock
100 else
101 {
102 socket->SendTo(Create<Packet>(123), 0, realTo);
103 }
104}
105
106void
108{
111
112 SimpleNetDeviceHelper simpleNetDevHelper;
113 NetDeviceContainer devs = simpleNetDevHelper.Install(NodeContainer(node0, node1));
116
117 InternetStackHelper internet;
118 internet.SetIpv6StackInstall(false);
119
120 // For Node 0
121 node0->AddDevice(device);
122 internet.Install(node0);
123 Ptr<Ipv4> ipv4 = node0->GetObject<Ipv4>();
124
125 uint32_t index = ipv4->AddInterface(device);
126 Ipv4InterfaceAddress ifaceAddr1 = Ipv4InterfaceAddress("10.1.1.1", "255.255.255.0");
127 ipv4->AddAddress(index, ifaceAddr1);
128 ipv4->SetMetric(index, 1);
129 ipv4->SetUp(index);
130
131 // For Node 1
132 node1->AddDevice(device2);
133 internet.Install(node1);
134 ipv4 = node1->GetObject<Ipv4>();
135
136 index = ipv4->AddInterface(device2);
137 Ipv4InterfaceAddress ifaceAddr2 = Ipv4InterfaceAddress("10.1.1.2", "255.255.255.0");
138 ipv4->AddAddress(index, ifaceAddr2);
139 ipv4->SetMetric(index, 1);
140 ipv4->SetUp(index);
141
142 // IPv4 test
143 Ptr<SocketFactory> factory = node0->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
144 Ptr<Socket> socket = factory->CreateSocket();
146 socket->Bind(local);
147 socket->SetRecvPktInfo(true);
148 socket->SetRecvCallback(MakeCallback(&Ipv4PacketInfoTagTest::RxCb, this));
149
150 // receive on loopback
151 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
152 Seconds(0),
154 this,
155 socket,
156 "127.0.0.1");
158
159 Ptr<SocketFactory> factory2 = node1->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
160 Ptr<Socket> socket2 = factory2->CreateSocket();
161 Simulator::ScheduleWithContext(socket2->GetNode()->GetId(),
162 Seconds(0),
164 this,
165 socket,
166 "10.1.1.1");
168
169 // ipv4 w rawsocket
170 factory = node0->GetObject<SocketFactory>(Ipv4RawSocketFactory::GetTypeId());
171 socket = factory->CreateSocket();
173 socket->Bind(local);
174 socket->SetRecvPktInfo(true);
175 socket->SetRecvCallback(MakeCallback(&Ipv4PacketInfoTagTest::RxCb, this));
176
177 // receive on loopback
178 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
179 Seconds(0),
181 this,
182 socket,
183 "127.0.0.1");
185
186 factory2 = node1->GetObject<SocketFactory>(Ipv4RawSocketFactory::GetTypeId());
187 socket2 = factory2->CreateSocket();
188 Simulator::ScheduleWithContext(socket2->GetNode()->GetId(),
189 Seconds(0),
191 this,
192 socket,
193 "10.1.1.1");
196}
197
198/**
199 * @ingroup internet-test
200 *
201 * @brief IPv4 PacketInfoTag TestSuite
202 */
204{
205 public:
207
208 private:
209};
210
212 : TestSuite("ipv4-packet-info-tag", Type::UNIT)
213{
214 AddTestCase(new Ipv4PacketInfoTagTest(), TestCase::Duration::QUICK);
215}
216
217static Ipv4PacketInfoTagTestSuite g_packetinfotagTests; //!< Static variable for test initialization
void DoRun() override
Implementation to actually run this TestCase.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data.
void RxCb(Ptr< Socket > socket)
Receive callback.
a polymophic address class
Definition address.h:90
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetAny()
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
a class to store IPv4 address information on an interface
This class implements Linux struct pktinfo in order to deliver ancillary information to the socket in...
static TypeId GetTypeId()
Get the type ID.
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.
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.
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:1344
static Ipv4PacketInfoTagTestSuite 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