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 * 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/arp-l3-protocol.h"
26#include "ns3/attribute.h"
27#include "ns3/icmpv4-l4-protocol.h"
28#include "ns3/inet-socket-address.h"
29#include "ns3/internet-stack-helper.h"
30#include "ns3/ipv4-address.h"
31#include "ns3/ipv4-interface.h"
32#include "ns3/ipv4-l3-protocol.h"
33#include "ns3/ipv4-list-routing.h"
34#include "ns3/ipv4-packet-info-tag.h"
35#include "ns3/ipv4-raw-socket-factory.h"
36#include "ns3/ipv4-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/tcp-l4-protocol.h"
45#include "ns3/test.h"
46#include "ns3/traffic-control-layer.h"
47#include "ns3/udp-l4-protocol.h"
48#include "ns3/udp-socket-factory.h"
49#include "ns3/udp-socket.h"
50
51#include <string>
52
53using namespace ns3;
54
61{
62 public:
64
65 private:
66 void DoRun() override;
67
72 void RxCb(Ptr<Socket> socket);
78 void DoSendData(Ptr<Socket> socket, std::string to);
79};
80
82 : TestCase("Ipv4PacketInfoTagTest")
83{
84}
85
86void
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
97 bool found;
98 found = m_receivedPacket->RemovePacketTag(tag);
99 NS_TEST_ASSERT_MSG_EQ(found, true, "Could not find tag");
100}
101
102void
104{
105 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 200);
106 if (DynamicCast<UdpSocket>(socket))
107 {
108 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
109 }
110 // Should only Ipv4RawSock
111 else
112 {
113 socket->SendTo(Create<Packet>(123), 0, realTo);
114 }
115}
116
117void
119{
120 Ptr<Node> node0 = CreateObject<Node>();
121 Ptr<Node> node1 = CreateObject<Node>();
122
123 SimpleNetDeviceHelper simpleNetDevHelper;
124 NetDeviceContainer devs = simpleNetDevHelper.Install(NodeContainer(node0, node1));
125 Ptr<SimpleNetDevice> device = DynamicCast<SimpleNetDevice>(devs.Get(0));
126 Ptr<SimpleNetDevice> device2 = DynamicCast<SimpleNetDevice>(devs.Get(1));
127
128 InternetStackHelper internet;
129 internet.SetIpv6StackInstall(false);
130
131 // For Node 0
132 node0->AddDevice(device);
133 internet.Install(node0);
134 Ptr<Ipv4> ipv4 = node0->GetObject<Ipv4>();
135
136 uint32_t index = ipv4->AddInterface(device);
137 Ipv4InterfaceAddress ifaceAddr1 = Ipv4InterfaceAddress("10.1.1.1", "255.255.255.0");
138 ipv4->AddAddress(index, ifaceAddr1);
139 ipv4->SetMetric(index, 1);
140 ipv4->SetUp(index);
141
142 // For Node 1
143 node1->AddDevice(device2);
144 internet.Install(node1);
145 ipv4 = node1->GetObject<Ipv4>();
146
147 index = ipv4->AddInterface(device2);
148 Ipv4InterfaceAddress ifaceAddr2 = Ipv4InterfaceAddress("10.1.1.2", "255.255.255.0");
149 ipv4->AddAddress(index, ifaceAddr2);
150 ipv4->SetMetric(index, 1);
151 ipv4->SetUp(index);
152
153 // IPv4 test
154 Ptr<SocketFactory> factory = node0->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
155 Ptr<Socket> socket = factory->CreateSocket();
157 socket->Bind(local);
158 socket->SetRecvPktInfo(true);
159 socket->SetRecvCallback(MakeCallback(&Ipv4PacketInfoTagTest::RxCb, this));
160
161 // receive on loopback
162 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
163 Seconds(0),
165 this,
166 socket,
167 "127.0.0.1");
169
170 Ptr<SocketFactory> factory2 = node1->GetObject<SocketFactory>(UdpSocketFactory::GetTypeId());
171 Ptr<Socket> socket2 = factory2->CreateSocket();
172 Simulator::ScheduleWithContext(socket2->GetNode()->GetId(),
173 Seconds(0),
175 this,
176 socket,
177 "10.1.1.1");
179
180 // ipv4 w rawsocket
181 factory = node0->GetObject<SocketFactory>(Ipv4RawSocketFactory::GetTypeId());
182 socket = factory->CreateSocket();
184 socket->Bind(local);
185 socket->SetRecvPktInfo(true);
186 socket->SetRecvCallback(MakeCallback(&Ipv4PacketInfoTagTest::RxCb, this));
187
188 // receive on loopback
189 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
190 Seconds(0),
192 this,
193 socket,
194 "127.0.0.1");
196
197 factory2 = node1->GetObject<SocketFactory>(Ipv4RawSocketFactory::GetTypeId());
198 socket2 = factory2->CreateSocket();
199 Simulator::ScheduleWithContext(socket2->GetNode()->GetId(),
200 Seconds(0),
202 this,
203 socket,
204 "10.1.1.1");
207}
208
215{
216 public:
218
219 private:
220};
221
223 : TestSuite("ipv4-packet-info-tag", UNIT)
224{
226}
227
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:100
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
static Ipv4Address GetAny()
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:79
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.
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.
#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 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:702