A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 Georgia Tech Research Corporation
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 * Author: Raj Bhattacharjea <raj.b@gatech.edu>
18 */
19/**
20 * This is the test code for udp-socket-impl.cc, it was moved out of udp-socket-impl.cc to
21 * be in an independent file for clarity purposes.
22 */
23
24#include "ns3/arp-l3-protocol.h"
25#include "ns3/boolean.h"
26#include "ns3/icmpv4-l4-protocol.h"
27#include "ns3/icmpv6-l4-protocol.h"
28#include "ns3/inet-socket-address.h"
29#include "ns3/inet6-socket-address.h"
30#include "ns3/internet-stack-helper.h"
31#include "ns3/ipv4-l3-protocol.h"
32#include "ns3/ipv4-list-routing.h"
33#include "ns3/ipv4-queue-disc-item.h"
34#include "ns3/ipv4-static-routing.h"
35#include "ns3/ipv6-address-helper.h"
36#include "ns3/ipv6-l3-protocol.h"
37#include "ns3/ipv6-list-routing.h"
38#include "ns3/ipv6-static-routing.h"
39#include "ns3/log.h"
40#include "ns3/node.h"
41#include "ns3/simple-channel.h"
42#include "ns3/simple-net-device-helper.h"
43#include "ns3/simple-net-device.h"
44#include "ns3/simulator.h"
45#include "ns3/socket-factory.h"
46#include "ns3/socket.h"
47#include "ns3/tcp-l4-protocol.h"
48#include "ns3/test.h"
49#include "ns3/traffic-control-helper.h"
50#include "ns3/udp-l4-protocol.h"
51#include "ns3/udp-socket-factory.h"
52
53#include <limits>
54#include <string>
55
56using namespace ns3;
57
58/**
59 * \ingroup internet-test
60 *
61 * \brief UDP Socket Loopback over IPv4 Test
62 */
64{
65 public:
67 void DoRun() override;
68
69 /**
70 * \brief Receive a packet.
71 * \param socket The receiving socket.
72 */
73 void ReceivePkt(Ptr<Socket> socket);
74 Ptr<Packet> m_receivedPacket; //!< Received packet
75};
76
78 : TestCase("UDP loopback test")
79{
80}
81
82void
84{
85 uint32_t availableData;
86 availableData = socket->GetRxAvailable();
87 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
88 NS_TEST_ASSERT_MSG_EQ(availableData,
90 "ReceivedPacket size is not equal to the Rx buffer size");
91}
92
93void
95{
96 Ptr<Node> rxNode = CreateObject<Node>();
97 InternetStackHelper internet;
98 internet.Install(rxNode);
99
100 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
101 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
102 rxSocket->Bind(InetSocketAddress(Ipv4Address::GetAny(), 80));
103 rxSocket->SetRecvCallback(MakeCallback(&UdpSocketLoopbackTest::ReceivePkt, this));
104
105 Ptr<Socket> txSocket = rxSocketFactory->CreateSocket();
106 txSocket->SendTo(Create<Packet>(246), 0, InetSocketAddress("127.0.0.1", 80));
110 246,
111 "first socket should not receive it (it is bound specifically to the "
112 "second interface's address");
113}
114
115/**
116 * \ingroup internet-test
117 *
118 * \brief UDP Socket Loopback over IPv6 Test
119 */
121{
122 public:
124 void DoRun() override;
125
126 /**
127 * \brief Receive a packet.
128 * \param socket The receiving socket.
129 */
130 void ReceivePkt(Ptr<Socket> socket);
131 Ptr<Packet> m_receivedPacket; //!< Received packet
132};
133
135 : TestCase("UDP6 loopback test")
136{
137}
138
139void
141{
142 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
143 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
144 NS_TEST_ASSERT_MSG_EQ(availableData,
146 "ReceivedPacket size is not equal to the Rx buffer size");
147}
148
149void
151{
152 Ptr<Node> rxNode = CreateObject<Node>();
153 InternetStackHelper internet;
154 internet.Install(rxNode);
155
156 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
157 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
158 rxSocket->Bind(Inet6SocketAddress(Ipv6Address::GetAny(), 80));
159 rxSocket->SetRecvCallback(MakeCallback(&Udp6SocketLoopbackTest::ReceivePkt, this));
160
161 Ptr<Socket> txSocket = rxSocketFactory->CreateSocket();
162 txSocket->SendTo(Create<Packet>(246), 0, Inet6SocketAddress("::1", 80));
166 246,
167 "first socket should not receive it (it is bound specifically to the "
168 "second interface's address");
169}
170
171/**
172 * \ingroup internet-test
173 *
174 * \brief UDP Socket over IPv4 Test
175 */
177{
178 Ptr<Packet> m_receivedPacket; //!< Received packet (1).
179 Ptr<Packet> m_receivedPacket2; //!< Received packet (2).
181
182 /**
183 * \brief Get the TOS of the received packet.
184 * \returns The TOS.
185 */
187
188 /**
189 * \brief Get the priority of the received packet.
190 * \returns The priority.
191 */
193
194 /**
195 * \brief Send data.
196 * \param socket The sending socket.
197 * \param to The destination address.
198 */
199 void DoSendDataTo(Ptr<Socket> socket, std::string to);
200 /**
201 * \brief Send data.
202 * \param socket The sending socket.
203 * \param to The destination address.
204 */
205 void SendDataTo(Ptr<Socket> socket, std::string to);
206 /**
207 * \brief Send data.
208 * \param socket The sending socket.
209 */
210 void DoSendData(Ptr<Socket> socket);
211 /**
212 * \brief Send data.
213 * \param socket The sending socket.
214 */
215 void SendData(Ptr<Socket> socket);
216
217 public:
218 void DoRun() override;
220
221 /**
222 * \brief Receive packets (1).
223 * \param socket The receiving socket.
224 */
225 void ReceivePkt(Ptr<Socket> socket);
226 /**
227 * \brief Receive packets (2).
228 * \param socket The receiving socket.
229 */
230 void ReceivePkt2(Ptr<Socket> socket);
231
232 /**
233 * \brief Adds a packet to the list of sent packets.
234 * \param item The sent packet.
235 */
237};
238
240 : TestCase("UDP socket implementation")
241{
242}
243
244void
246{
247 uint32_t availableData;
248 availableData = socket->GetRxAvailable();
249 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
250 NS_TEST_ASSERT_MSG_EQ(availableData,
252 "ReceivedPacket size is not equal to the Rx buffer size");
253}
254
255void
257{
258 uint32_t availableData;
259 availableData = socket->GetRxAvailable();
260 m_receivedPacket2 = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
261 NS_TEST_ASSERT_MSG_EQ(availableData,
263 "ReceivedPacket size is not equal to the Rx buffer size");
264}
265
266void
268{
269 Ptr<const Ipv4QueueDiscItem> ipv4Item = DynamicCast<const Ipv4QueueDiscItem>(item);
270 NS_TEST_EXPECT_MSG_NE(ipv4Item, nullptr, "no IPv4 packet");
271 Address addr;
273 Create<Ipv4QueueDiscItem>(ipv4Item->GetPacket()->Copy(), addr, 0, ipv4Item->GetHeader());
274}
275
278{
279 return static_cast<uint32_t>(m_sentPacket->GetHeader().GetTos());
280}
281
284{
285 SocketPriorityTag priorityTag;
286 bool found = m_sentPacket->GetPacket()->PeekPacketTag(priorityTag);
287 NS_TEST_EXPECT_MSG_EQ(found, true, "the packet should carry a SocketPriorityTag");
288 return static_cast<uint32_t>(priorityTag.GetPriority());
289}
290
291void
293{
294 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 1234);
295 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
296}
297
298void
300{
301 m_receivedPacket = Create<Packet>();
302 m_receivedPacket2 = Create<Packet>();
303 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
304 Seconds(0),
306 this,
307 socket,
308 to);
310}
311
312void
314{
315 NS_TEST_EXPECT_MSG_EQ(socket->Send(Create<Packet>(123), 0), 123, "100");
316}
317
318void
320{
321 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
322 Seconds(0),
324 this,
325 socket);
327}
328
329void
331{
332 // Create topology
333
334 // Receiver Node
335 Ptr<Node> rxNode = CreateObject<Node>();
336 // Sender Node
337 Ptr<Node> txNode = CreateObject<Node>();
338
339 NodeContainer nodes(rxNode, txNode);
340
341 SimpleNetDeviceHelper helperChannel1;
342 helperChannel1.SetNetDevicePointToPointMode(true);
343 NetDeviceContainer net1 = helperChannel1.Install(nodes);
344
345 SimpleNetDeviceHelper helperChannel2;
346 helperChannel2.SetNetDevicePointToPointMode(true);
347 NetDeviceContainer net2 = helperChannel2.Install(nodes);
348
349 InternetStackHelper internet;
350 internet.Install(nodes);
351
353 QueueDiscContainer qdiscs = tch.Install(net1.Get(1));
354
355 Ptr<Ipv4> ipv4;
356 uint32_t netdev_idx;
357 Ipv4InterfaceAddress ipv4Addr;
358
359 // Receiver Node
360 ipv4 = rxNode->GetObject<Ipv4>();
361 netdev_idx = ipv4->AddInterface(net1.Get(0));
362 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.0.1"), Ipv4Mask("/24"));
363 ipv4->AddAddress(netdev_idx, ipv4Addr);
364 ipv4->SetUp(netdev_idx);
365
366 netdev_idx = ipv4->AddInterface(net2.Get(0));
367 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.1.1"), Ipv4Mask("/24"));
368 ipv4->AddAddress(netdev_idx, ipv4Addr);
369 ipv4->SetUp(netdev_idx);
370
371 // Sender Node
372 ipv4 = txNode->GetObject<Ipv4>();
373 netdev_idx = ipv4->AddInterface(net1.Get(1));
374 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.0.2"), Ipv4Mask("/24"));
375 ipv4->AddAddress(netdev_idx, ipv4Addr);
376 ipv4->SetUp(netdev_idx);
377
378 netdev_idx = ipv4->AddInterface(net2.Get(1));
379 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.1.2"), Ipv4Mask("/24"));
380 ipv4->AddAddress(netdev_idx, ipv4Addr);
381 ipv4->SetUp(netdev_idx);
382
383 // Create the UDP sockets
384 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
385
386 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
387 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("10.0.0.1"), 1234)),
388 0,
389 "trivial");
390 rxSocket->SetRecvCallback(MakeCallback(&UdpSocketImplTest::ReceivePkt, this));
391
392 Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket();
393 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(InetSocketAddress(Ipv4Address("10.0.1.1"), 1234)),
394 0,
395 "trivial");
396 rxSocket2->SetRecvCallback(MakeCallback(&UdpSocketImplTest::ReceivePkt2, this));
397
398 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory>();
399 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
400 txSocket->SetAllowBroadcast(true);
401
402 // ------ Now the tests ------------
403
404 // Unicast test
405 SendDataTo(txSocket, "10.0.0.1");
408 0,
409 "second interface should not receive it");
410
413
414 // Simple broadcast test
415
416 SendDataTo(txSocket, "255.255.255.255");
418 0,
419 "first socket should not receive it (it is bound specifically to the "
420 "first interface's address");
422 0,
423 "second socket should not receive it (it is bound specifically to the "
424 "second interface's address");
425
428
429 // Broadcast test with multiple receiving sockets
430
431 // When receiving broadcast packets, all sockets sockets bound to
432 // the address/port should receive a copy of the same packet -- if
433 // the socket address matches.
434 rxSocket2->Dispose();
435 rxSocket2 = rxSocketFactory->CreateSocket();
436 rxSocket2->SetRecvCallback(MakeCallback(&UdpSocketImplTest::ReceivePkt2, this));
437 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(InetSocketAddress(Ipv4Address("0.0.0.0"), 1234)),
438 0,
439 "trivial");
440
441 SendDataTo(txSocket, "255.255.255.255");
443 0,
444 "first socket should not receive it (it is bound specifically to the "
445 "first interface's address");
447
448 m_receivedPacket = nullptr;
449 m_receivedPacket2 = nullptr;
450
451 // Simple Link-local multicast test
452
453 txSocket->BindToNetDevice(net1.Get(1));
454 SendDataTo(txSocket, "224.0.0.9");
456 0,
457 "first socket should not receive it (it is bound specifically to the "
458 "first interface's address");
459 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket2->GetSize(), 123, "recv2: 224.0.0.9");
460
463
464 // Simple getpeername tests
465
466 Address peerAddress;
467 int err = txSocket->GetPeerName(peerAddress);
468 NS_TEST_EXPECT_MSG_EQ(err, -1, "socket GetPeerName() should fail when socket is not connected");
469 NS_TEST_EXPECT_MSG_EQ(txSocket->GetErrno(),
471 "socket error code should be ERROR_NOTCONN");
472
473 InetSocketAddress peer("10.0.0.1", 1234);
474 err = txSocket->Connect(peer);
475 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket Connect() should succeed");
476
477 err = txSocket->GetPeerName(peerAddress);
478 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket GetPeerName() should succeed when socket is connected");
479 NS_TEST_EXPECT_MSG_EQ(peerAddress,
480 peer,
481 "address from socket GetPeerName() should equal the connected address");
482
485
486 // TOS and priority tests
487
488 // Intercept the packets dequeued by the queue disc on the sender node
489 qdiscs.Get(0)->TraceConnectWithoutContext("Dequeue",
491
492 // The socket is not connected.
493 txSocket->SetPriority(6); // Interactive
494 // Send a packet to a specified destination:
495 // - since the tos is zero, the priority set for the socket is used
496 SendDataTo(txSocket, "10.0.0.1");
498
499 NS_TEST_EXPECT_MSG_EQ(GetTos(), 0, "the TOS should be set to 0");
500 NS_TEST_EXPECT_MSG_EQ(GetPriority(), 6, "Interactive (6)");
501
503
504 InetSocketAddress dest("10.0.0.1", 1234);
505 txSocket->SetIpTos(0xb8); // EF
506 // the connect operation sets the tos (and priority) for the socket
507 NS_TEST_EXPECT_MSG_EQ(txSocket->Connect(dest), 0, "the connect operation failed");
508
509 SendData(txSocket);
511
512 NS_TEST_EXPECT_MSG_EQ(GetTos(), 0xb8, "the TOS should be set to 0xb8");
513 NS_TEST_EXPECT_MSG_EQ(GetPriority(), 4, "Interactive bulk (4)");
514
516
518}
519
520/**
521 * \ingroup internet-test
522 *
523 * \brief UDP Socket over IPv6 Test
524 */
526{
527 Ptr<Packet> m_receivedPacket; //!< Received packet (1).
528 Ptr<Packet> m_receivedPacket2; //!< Received packet (2).
529
530 /**
531 * \brief Send data.
532 * \param socket The sending socket.
533 * \param to The destination address.
534 */
535 void DoSendDataTo(Ptr<Socket> socket, std::string to);
536 /**
537 * \brief Send data.
538 * \param socket The sending socket.
539 * \param to The destination address.
540 */
541 void SendDataTo(Ptr<Socket> socket, std::string to);
542
543 public:
544 void DoRun() override;
546
547 /**
548 * \brief Receive packets (1).
549 * \param socket The receiving socket.
550 * \param packet The received packet.
551 * \param from The source address.
552 */
553 void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
554 /**
555 * \brief Receive packets (2).
556 * \param socket The receiving socket.
557 * \param packet The received packet.
558 * \param from The source address.
559 */
560 void ReceivePacket2(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
561 /**
562 * \brief Receive packets (1).
563 * \param socket The receiving socket.
564 */
565 void ReceivePkt(Ptr<Socket> socket);
566 /**
567 * \brief Receive packets (2).
568 * \param socket The receiving socket.
569 */
570 void ReceivePkt2(Ptr<Socket> socket);
571};
572
574 : TestCase("UDP6 socket implementation")
575{
576}
577
578void
580{
581 m_receivedPacket = packet;
582}
583
584void
586{
587 m_receivedPacket2 = packet;
588}
589
590void
592{
593 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
594 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
595 NS_TEST_ASSERT_MSG_EQ(availableData,
597 "ReceivedPacket size is not equal to the Rx buffer size");
598}
599
600void
602{
603 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
604 m_receivedPacket2 = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
605 NS_TEST_ASSERT_MSG_EQ(availableData,
607 "ReceivedPacket size is not equal to the Rx buffer size");
608}
609
610void
612{
613 Address realTo = Inet6SocketAddress(Ipv6Address(to.c_str()), 1234);
614 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "200");
615}
616
617void
619{
620 m_receivedPacket = Create<Packet>();
621 m_receivedPacket2 = Create<Packet>();
622 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
623 Seconds(0),
625 this,
626 socket,
627 to);
629}
630
631void
633{
634 // Create topology
635
636 // Receiver Node
637 Ptr<Node> rxNode = CreateObject<Node>();
638 // Sender Node
639 Ptr<Node> txNode = CreateObject<Node>();
640
641 NodeContainer nodes(rxNode, txNode);
642
643 SimpleNetDeviceHelper helperChannel1;
644 helperChannel1.SetNetDevicePointToPointMode(true);
645 NetDeviceContainer net1 = helperChannel1.Install(nodes);
646
647 SimpleNetDeviceHelper helperChannel2;
648 helperChannel2.SetNetDevicePointToPointMode(true);
649 NetDeviceContainer net2 = helperChannel2.Install(nodes);
650
651 InternetStackHelper internetv6;
652 internetv6.Install(nodes);
653
654 txNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
655 rxNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
656
657 Ipv6AddressHelper ipv6helper;
658 Ipv6InterfaceContainer iic1 = ipv6helper.AssignWithoutAddress(net1);
659 Ipv6InterfaceContainer iic2 = ipv6helper.AssignWithoutAddress(net2);
660
661 Ptr<NetDevice> device;
662 Ptr<Ipv6> ipv6;
663 int32_t ifIndex;
664 Ipv6InterfaceAddress ipv6Addr;
665
666 ipv6 = rxNode->GetObject<Ipv6>();
667 device = net1.Get(0);
668 ifIndex = ipv6->GetInterfaceForDevice(device);
669 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:0100::1"), Ipv6Prefix(64));
670 ipv6->AddAddress(ifIndex, ipv6Addr);
671 ipv6->SetUp(ifIndex);
672
673 device = net2.Get(0);
674 ifIndex = ipv6->GetInterfaceForDevice(device);
675 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:0100:1::1"), Ipv6Prefix(64));
676 ipv6->AddAddress(ifIndex, ipv6Addr);
677 ipv6->SetUp(ifIndex);
678
679 ipv6 = txNode->GetObject<Ipv6>();
680 device = net1.Get(1);
681 ifIndex = ipv6->GetInterfaceForDevice(device);
682 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:0100::2"), Ipv6Prefix(64));
683 ipv6->AddAddress(ifIndex, ipv6Addr);
684 ipv6->SetUp(ifIndex);
685
686 device = net2.Get(1);
687 ifIndex = ipv6->GetInterfaceForDevice(device);
688 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:0100:1::2"), Ipv6Prefix(64));
689 ipv6->AddAddress(ifIndex, ipv6Addr);
690 ipv6->SetUp(ifIndex);
691
692 // Create the UDP sockets
693 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
694 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
695 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(Inet6SocketAddress(Ipv6Address("2001:0100::1"), 1234)),
696 0,
697 "trivial");
698 rxSocket->SetRecvCallback(MakeCallback(&Udp6SocketImplTest::ReceivePkt, this));
699
700 Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket();
701 rxSocket2->SetRecvCallback(MakeCallback(&Udp6SocketImplTest::ReceivePkt2, this));
702 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(Inet6SocketAddress(Ipv6Address("2001:0100:1::1"), 1234)),
703 0,
704 "trivial");
705
706 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory>();
707 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
708 txSocket->SetAllowBroadcast(true);
709 // ------ Now the tests ------------
710
711 // Unicast test
712 SendDataTo(txSocket, "2001:0100::1");
714 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket2->GetSize(), 0, "second interface should receive it");
715
718
719 // Simple Link-local multicast test
720
721 // When receiving broadcast packets, all sockets sockets bound to
722 // the address/port should receive a copy of the same packet -- if
723 // the socket address matches.
724 rxSocket2->Dispose();
725 rxSocket2 = rxSocketFactory->CreateSocket();
726 rxSocket2->SetRecvCallback(MakeCallback(&Udp6SocketImplTest::ReceivePkt2, this));
727 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(Inet6SocketAddress(Ipv6Address("::"), 1234)),
728 0,
729 "trivial");
730
731 txSocket->BindToNetDevice(net1.Get(1));
732 SendDataTo(txSocket, "ff02::1");
734 0,
735 "first socket should not receive it (it is bound specifically to the "
736 "second interface's address");
737 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket2->GetSize(), 123, "recv2: ff02::1");
738
741
742 // Simple getpeername tests
743 Address peerAddress;
744 int err = txSocket->GetPeerName(peerAddress);
745 NS_TEST_EXPECT_MSG_EQ(err, -1, "socket GetPeerName() should fail when socket is not connected");
746 NS_TEST_EXPECT_MSG_EQ(txSocket->GetErrno(),
748 "socket error code should be ERROR_NOTCONN");
749
750 Inet6SocketAddress peer("2001:0100::1", 1234);
751 err = txSocket->Connect(peer);
752 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket Connect() should succeed");
753
754 err = txSocket->GetPeerName(peerAddress);
755 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket GetPeerName() should succeed when socket is connected");
756 NS_TEST_EXPECT_MSG_EQ(peerAddress,
757 peer,
758 "address from socket GetPeerName() should equal the connected address");
759
761}
762
763/**
764 * \ingroup internet-test
765 *
766 * \brief UDP TestSuite
767 */
769{
770 public:
772 : TestSuite("udp", Type::UNIT)
773 {
774 AddTestCase(new UdpSocketImplTest, TestCase::Duration::QUICK);
775 AddTestCase(new UdpSocketLoopbackTest, TestCase::Duration::QUICK);
776 AddTestCase(new Udp6SocketImplTest, TestCase::Duration::QUICK);
777 AddTestCase(new Udp6SocketLoopbackTest, TestCase::Duration::QUICK);
778 }
779};
780
781static UdpTestSuite g_udpTestSuite; //!< Static variable for test initialization
UDP Socket over IPv6 Test.
Definition: udp-test.cc:526
Ptr< Packet > m_receivedPacket
Received packet (1).
Definition: udp-test.cc:527
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive packets (1).
Definition: udp-test.cc:579
void SendDataTo(Ptr< Socket > socket, std::string to)
Send data.
Definition: udp-test.cc:618
void ReceivePkt2(Ptr< Socket > socket)
Receive packets (2).
Definition: udp-test.cc:601
void DoRun() override
Implementation to actually run this TestCase.
Definition: udp-test.cc:632
Ptr< Packet > m_receivedPacket2
Received packet (2).
Definition: udp-test.cc:528
void ReceivePkt(Ptr< Socket > socket)
Receive packets (1).
Definition: udp-test.cc:591
void ReceivePacket2(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive packets (2).
Definition: udp-test.cc:585
void DoSendDataTo(Ptr< Socket > socket, std::string to)
Send data.
Definition: udp-test.cc:611
UDP Socket Loopback over IPv6 Test.
Definition: udp-test.cc:121
void ReceivePkt(Ptr< Socket > socket)
Receive a packet.
Definition: udp-test.cc:140
Ptr< Packet > m_receivedPacket
Received packet.
Definition: udp-test.cc:131
void DoRun() override
Implementation to actually run this TestCase.
Definition: udp-test.cc:150
UDP Socket over IPv4 Test.
Definition: udp-test.cc:177
uint32_t GetTos()
Get the TOS of the received packet.
Definition: udp-test.cc:277
uint32_t GetPriority()
Get the priority of the received packet.
Definition: udp-test.cc:283
void SendData(Ptr< Socket > socket)
Send data.
Definition: udp-test.cc:319
void ReceivePkt2(Ptr< Socket > socket)
Receive packets (2).
Definition: udp-test.cc:256
void SendDataTo(Ptr< Socket > socket, std::string to)
Send data.
Definition: udp-test.cc:299
Ptr< Packet > m_receivedPacket
Received packet (1).
Definition: udp-test.cc:178
void DoRun() override
Implementation to actually run this TestCase.
Definition: udp-test.cc:330
void DoSendData(Ptr< Socket > socket)
Send data.
Definition: udp-test.cc:313
Ptr< Ipv4QueueDiscItem > m_sentPacket
Sent packet.
Definition: udp-test.cc:180
void ReceivePkt(Ptr< Socket > socket)
Receive packets (1).
Definition: udp-test.cc:245
void SentPkt(Ptr< const QueueDiscItem > item)
Adds a packet to the list of sent packets.
Definition: udp-test.cc:267
Ptr< Packet > m_receivedPacket2
Received packet (2).
Definition: udp-test.cc:179
void DoSendDataTo(Ptr< Socket > socket, std::string to)
Send data.
Definition: udp-test.cc:292
UDP Socket Loopback over IPv4 Test.
Definition: udp-test.cc:64
Ptr< Packet > m_receivedPacket
Received packet.
Definition: udp-test.cc:74
void ReceivePkt(Ptr< Socket > socket)
Receive a packet.
Definition: udp-test.cc:83
void DoRun() override
Implementation to actually run this TestCase.
Definition: udp-test.cc:94
UDP TestSuite.
Definition: udp-test.cc:769
a polymophic address class
Definition: address.h:101
AttributeValue implementation for Boolean.
Definition: boolean.h:37
An implementation of the ICMPv6 protocol.
An Inet6 address class.
an Inet address class
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...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
static Ipv4Address GetAny()
uint8_t GetTos() const
Definition: ipv4-header.cc:196
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
const Ipv4Header & GetHeader() const
Helper class to auto-assign global IPv6 unicast addresses.
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.
Keep track of a set of IPv6 interfaces.
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
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.
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:322
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition: packet.cc:393
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:983
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Holds a vector of ns3::QueueDisc pointers.
Ptr< QueueDisc > Get(std::size_t i) const
Get the Ptr<QueueDisc> stored in this container at a given index.
Ptr< Packet > GetPacket() const
Definition: queue-item.cc:43
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:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static void Run()
Run the simulation.
Definition: simulator.cc:178
@ ERROR_NOTCONN
Definition: socket.h:87
indicates whether the socket has a priority set.
Definition: socket.h:1318
uint8_t GetPriority() const
Get the tag's priority.
Definition: socket.cc:860
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
static constexpr auto UNIT
Definition: test.h:1286
Build a set of QueueDisc objects.
QueueDiscContainer Install(NetDeviceContainer c)
static TrafficControlHelper Default(std::size_t nTxQueues=1)
API to create UDP socket instances.
#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:145
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report if not.
Definition: test.h:667
#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:252
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
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:706
static UdpTestSuite g_udpTestSuite
Static variable for test initialization.
Definition: udp-test.cc:781