A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv6-raw-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Hajime Tazaki
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Hajime Tazaki <tazaki@sfc.wide.ad.jp>
7 */
8/**
9 * This is the test code for ipv6-raw-socket-impl.cc.
10 */
11
12#include "ns3/boolean.h"
13#include "ns3/icmpv6-l4-protocol.h"
14#include "ns3/inet6-socket-address.h"
15#include "ns3/internet-stack-helper.h"
16#include "ns3/ipv6-address-helper.h"
17#include "ns3/ipv6-l3-protocol.h"
18#include "ns3/ipv6-list-routing.h"
19#include "ns3/ipv6-raw-socket-factory.h"
20#include "ns3/ipv6-static-routing.h"
21#include "ns3/log.h"
22#include "ns3/node-container.h"
23#include "ns3/node.h"
24#include "ns3/simple-channel.h"
25#include "ns3/simple-net-device-helper.h"
26#include "ns3/simple-net-device.h"
27#include "ns3/simulator.h"
28#include "ns3/socket-factory.h"
29#include "ns3/socket.h"
30#include "ns3/test.h"
31#include "ns3/uinteger.h"
32
33#ifdef __WIN32__
34#include "ns3/win32-internet.h"
35#else
36#include <netinet/in.h>
37#include <sys/socket.h>
38#endif
39
40#include <limits>
41#include <string>
42#include <sys/types.h>
43
44using namespace ns3;
45
46/**
47 * \ingroup internet-test
48 *
49 * \brief IPv6 RAW Socket Test
50 */
52{
53 Ptr<Packet> m_receivedPacket; //!< Received packet (1).
54 Ptr<Packet> m_receivedPacket2; //!< Received packet (2).
55
56 /**
57 * \brief Send data.
58 * \param socket The sending socket.
59 * \param to Destination address.
60 */
61 void DoSendData(Ptr<Socket> socket, std::string to);
62 /**
63 * \brief Send data.
64 * \param socket The sending socket.
65 * \param to Destination address.
66 */
67 void SendData(Ptr<Socket> socket, std::string to);
68
69 public:
70 void DoRun() override;
72
73 /**
74 * \brief Receive data.
75 * \param socket The receiving socket.
76 * \param packet The received packet.
77 * \param from The sender.
78 */
79 void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
80 /**
81 * \brief Receive data.
82 * \param socket The receiving socket.
83 * \param packet The received packet.
84 * \param from The sender.
85 */
86 void ReceivePacket2(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
87 /**
88 * \brief Receive data.
89 * \param socket The receiving socket.
90 */
91 void ReceivePkt(Ptr<Socket> socket);
92 /**
93 * \brief Receive data.
94 * \param socket The receiving socket.
95 */
96 void ReceivePkt2(Ptr<Socket> socket);
97};
98
100 : TestCase("Ipv6 Raw socket implementation")
101{
102}
103
104void
106{
107 m_receivedPacket = packet;
108}
109
110void
112{
113 m_receivedPacket2 = packet;
114}
115
116void
118{
119 uint32_t availableData;
120 availableData = socket->GetRxAvailable();
121 m_receivedPacket = socket->Recv(2, MSG_PEEK);
122 NS_TEST_ASSERT_MSG_EQ(m_receivedPacket->GetSize(), 2, "ReceivedPacket size is not equal to 2");
123 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
124 NS_TEST_ASSERT_MSG_EQ(availableData,
126 "Received packet size is not equal to Rx buffer size");
127}
128
129void
131{
132 uint32_t availableData;
133 Address addr;
134 availableData = socket->GetRxAvailable();
135 m_receivedPacket2 = socket->Recv(2, MSG_PEEK);
136 NS_TEST_ASSERT_MSG_EQ(m_receivedPacket2->GetSize(), 2, "ReceivedPacket size is not equal to 2");
137 m_receivedPacket2 = socket->RecvFrom(std::numeric_limits<uint32_t>::max(), 0, addr);
138 NS_TEST_ASSERT_MSG_EQ(availableData,
140 "Received packet size is not equal to Rx buffer size");
142 NS_TEST_EXPECT_MSG_EQ(v6addr.GetIpv6(), Ipv6Address("2001:db8::2"), "recvfrom");
143}
144
145void
147{
148 Address realTo = Inet6SocketAddress(Ipv6Address(to.c_str()), 0);
149 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, to);
150}
151
152void
154{
157 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
158 Seconds(0),
160 this,
161 socket,
162 to);
164}
165
166void
168{
169 // Create topology
170
171 // Receiver Node
172 Ptr<Node> rxNode = CreateObject<Node>();
173 // Sender Node
174 Ptr<Node> txNode = CreateObject<Node>();
175
176 NodeContainer nodes(rxNode, txNode);
177
178 SimpleNetDeviceHelper helperChannel1;
179 helperChannel1.SetNetDevicePointToPointMode(true);
180 NetDeviceContainer net1 = helperChannel1.Install(nodes);
181
182 SimpleNetDeviceHelper helperChannel2;
183 helperChannel2.SetNetDevicePointToPointMode(true);
184 NetDeviceContainer net2 = helperChannel2.Install(nodes);
185
186 InternetStackHelper internetv6;
187 internetv6.Install(nodes);
188
189 txNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
190 rxNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
191
192 Ipv6AddressHelper ipv6helper;
193 Ipv6InterfaceContainer iic1 = ipv6helper.AssignWithoutAddress(net1);
194 Ipv6InterfaceContainer iic2 = ipv6helper.AssignWithoutAddress(net2);
195
196 Ptr<NetDevice> device;
197 Ptr<Ipv6> ipv6;
198 int32_t ifIndex;
199 Ipv6InterfaceAddress ipv6Addr;
200
201 ipv6 = rxNode->GetObject<Ipv6>();
202 device = net1.Get(0);
203 ifIndex = ipv6->GetInterfaceForDevice(device);
204 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:db8::1"), Ipv6Prefix(64));
205 ipv6->AddAddress(ifIndex, ipv6Addr);
206
207 device = net2.Get(0);
208 ifIndex = ipv6->GetInterfaceForDevice(device);
209 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:db8:1::3"), Ipv6Prefix(64));
210 ipv6->AddAddress(ifIndex, ipv6Addr);
211
212 ipv6 = txNode->GetObject<Ipv6>();
213 device = net1.Get(1);
214 ifIndex = ipv6->GetInterfaceForDevice(device);
215 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:db8::2"), Ipv6Prefix(64));
216 ipv6->AddAddress(ifIndex, ipv6Addr);
217 ipv6->SetForwarding(ifIndex, true);
218
219 device = net2.Get(1);
220 ifIndex = ipv6->GetInterfaceForDevice(device);
221 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:db8:1::4"), Ipv6Prefix(64));
222 ipv6->AddAddress(ifIndex, ipv6Addr);
223 ipv6->SetForwarding(ifIndex, true);
224
225 // Create the Ipv6 Raw sockets
226 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv6RawSocketFactory>();
227 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
229 0,
230 "trivial");
231 rxSocket->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
232 rxSocket->SetRecvCallback(MakeCallback(&Ipv6RawSocketImplTest::ReceivePkt, this));
233
234 Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket();
235 rxSocket2->SetRecvCallback(MakeCallback(&Ipv6RawSocketImplTest::ReceivePkt2, this));
236 rxSocket2->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
237 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(Inet6SocketAddress(Ipv6Address("2001:db8:1::3"), 0)),
238 0,
239 "trivial");
240
241 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv6RawSocketFactory>();
242 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
243 txSocket->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
244
245 // ------ Now the tests ------------
246
247 // Unicast test
248 SendData(txSocket, "2001:db8::1");
249
250 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 163, "recv: 2001:db8::1");
252 0,
253 "second interface should not receive it");
254
257
258 // Simple Link-local multicast test
259 txSocket->Bind(Inet6SocketAddress(Ipv6Address("2001:db8::2"), 0));
260 SendData(txSocket, "ff02::1");
261 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 163, "recv: ff02::1");
263 0,
264 "second socket should not receive it (it is bound specifically to the "
265 "second interface's address");
266
269
270 // Broadcast test with multiple receiving sockets
271
272 // When receiving broadcast packets, all sockets sockets bound to
273 // the address/port should receive a copy of the same packet -- if
274 // the socket address matches.
275 rxSocket2->Dispose();
276 rxSocket2 = rxSocketFactory->CreateSocket();
277 rxSocket2->SetRecvCallback(MakeCallback(&Ipv6RawSocketImplTest::ReceivePkt2, this));
278 rxSocket2->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
280 0,
281 "trivial");
282
283 SendData(txSocket, "ff02::1");
284 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 163, "recv: ff02::1");
285 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket2->GetSize(), 163, "recv: ff02::1");
286
287 m_receivedPacket = nullptr;
288 m_receivedPacket2 = nullptr;
289
290 // Simple getpeername tests
291
292 Address peerAddress;
293 int err = txSocket->GetPeerName(peerAddress);
294 NS_TEST_EXPECT_MSG_EQ(err, -1, "socket GetPeerName() should fail when socket is not connected");
295 NS_TEST_EXPECT_MSG_EQ(txSocket->GetErrno(),
297 "socket error code should be ERROR_NOTCONN");
298
299 Inet6SocketAddress peer("2001:db8::1", 1234);
300 err = txSocket->Connect(peer);
301 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket Connect() should succeed");
302
303 err = txSocket->GetPeerName(peerAddress);
304 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket GetPeerName() should succeed when socket is connected");
305 peer.SetPort(0);
306 NS_TEST_EXPECT_MSG_EQ(peerAddress,
307 peer,
308 "address from socket GetPeerName() should equal the connected address");
309
311}
312
313/**
314 * \ingroup internet-test
315 *
316 * \brief IPv6 RAW Socket TestSuite
317 */
319{
320 public:
322 : TestSuite("ipv6-raw", Type::UNIT)
323 {
324 AddTestCase(new Ipv6RawSocketImplTest, TestCase::Duration::QUICK);
325 }
326};
327
328static Ipv6RawTestSuite g_ipv6rawTestSuite; //!< Static variable for test initialization
IPv6 RAW Socket Test.
Ptr< Packet > m_receivedPacket2
Received packet (2).
Ptr< Packet > m_receivedPacket
Received packet (1).
void ReceivePacket2(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive data.
void ReceivePkt2(Ptr< Socket > socket)
Receive data.
void DoRun() override
Implementation to actually run this TestCase.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data.
void ReceivePkt(Ptr< Socket > socket)
Receive data.
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive data.
void SendData(Ptr< Socket > socket, std::string to)
Send data.
IPv6 RAW Socket TestSuite.
a polymophic address class
Definition address.h:90
An implementation of the ICMPv6 protocol.
An Inet6 address class.
void SetPort(uint16_t port)
Set the port.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
Ipv6Address GetIpv6() const
Get the IPv6 address.
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Helper class to auto-assign global IPv6 unicast addresses.
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.
Keep track of a set of IPv6 interfaces.
Describes an IPv6 prefix.
API to create IPv6 RAW socket instances.
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.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition packet.cc:382
Smart pointer class similar to boost::intrusive_ptr.
build a set of SimpleNetDevice objects
void SetNetDevicePointToPointMode(bool pointToPointMode)
SimpleNetDevice is Broadcast capable and ARP needing.
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
@ ERROR_NOTCONN
Definition socket.h:76
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 constexpr auto UNIT
Definition test.h:1291
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 Ipv6RawTestSuite g_ipv6rawTestSuite
Static variable for test initialization.
NodeContainer nodes
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