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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Raj Bhattacharjea <raj.b@gatech.edu>
7 */
8/**
9 * This is the test code for udp-socket-impl.cc, it was moved out of udp-socket-impl.cc to
10 * be in an independent file for clarity purposes.
11 */
12
13#include "ns3/arp-l3-protocol.h"
14#include "ns3/boolean.h"
15#include "ns3/icmpv4-l4-protocol.h"
16#include "ns3/icmpv6-l4-protocol.h"
17#include "ns3/inet-socket-address.h"
18#include "ns3/inet6-socket-address.h"
19#include "ns3/internet-stack-helper.h"
20#include "ns3/ipv4-l3-protocol.h"
21#include "ns3/ipv4-list-routing.h"
22#include "ns3/ipv4-queue-disc-item.h"
23#include "ns3/ipv4-static-routing.h"
24#include "ns3/ipv6-address-helper.h"
25#include "ns3/ipv6-l3-protocol.h"
26#include "ns3/ipv6-list-routing.h"
27#include "ns3/ipv6-static-routing.h"
28#include "ns3/log.h"
29#include "ns3/node.h"
30#include "ns3/simple-channel.h"
31#include "ns3/simple-net-device-helper.h"
32#include "ns3/simple-net-device.h"
33#include "ns3/simulator.h"
34#include "ns3/socket-factory.h"
35#include "ns3/socket.h"
36#include "ns3/tcp-l4-protocol.h"
37#include "ns3/test.h"
38#include "ns3/traffic-control-helper.h"
39#include "ns3/udp-l4-protocol.h"
40#include "ns3/udp-socket-factory.h"
41
42#include <limits>
43#include <string>
44
45using namespace ns3;
46
47/**
48 * \ingroup internet-test
49 *
50 * \brief UDP Socket Loopback over IPv4 Test
51 */
53{
54 public:
56 void DoRun() override;
57
58 /**
59 * \brief Receive a packet.
60 * \param socket The receiving socket.
61 */
62 void ReceivePkt(Ptr<Socket> socket);
63 Ptr<Packet> m_receivedPacket; //!< Received packet
64};
65
67 : TestCase("UDP loopback test")
68{
69}
70
71void
73{
74 uint32_t availableData;
75 availableData = socket->GetRxAvailable();
76 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
77 NS_TEST_ASSERT_MSG_EQ(availableData,
79 "ReceivedPacket size is not equal to the Rx buffer size");
80}
81
82void
84{
86 InternetStackHelper internet;
87 internet.Install(rxNode);
88
89 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
90 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
91 rxSocket->Bind(InetSocketAddress(Ipv4Address::GetAny(), 80));
92 rxSocket->SetRecvCallback(MakeCallback(&UdpSocketLoopbackTest::ReceivePkt, this));
93
94 Ptr<Socket> txSocket = rxSocketFactory->CreateSocket();
95 txSocket->SendTo(Create<Packet>(246), 0, InetSocketAddress("127.0.0.1", 80));
99 246,
100 "first socket should not receive it (it is bound specifically to the "
101 "second interface's address");
102}
103
104/**
105 * \ingroup internet-test
106 *
107 * \brief UDP Socket Loopback over IPv6 Test
108 */
110{
111 public:
113 void DoRun() override;
114
115 /**
116 * \brief Receive a packet.
117 * \param socket The receiving socket.
118 */
119 void ReceivePkt(Ptr<Socket> socket);
120 Ptr<Packet> m_receivedPacket; //!< Received packet
121};
122
124 : TestCase("UDP6 loopback test")
125{
126}
127
128void
130{
131 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
132 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
133 NS_TEST_ASSERT_MSG_EQ(availableData,
135 "ReceivedPacket size is not equal to the Rx buffer size");
136}
137
138void
140{
141 Ptr<Node> rxNode = CreateObject<Node>();
142 InternetStackHelper internet;
143 internet.Install(rxNode);
144
145 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
146 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
147 rxSocket->Bind(Inet6SocketAddress(Ipv6Address::GetAny(), 80));
148 rxSocket->SetRecvCallback(MakeCallback(&Udp6SocketLoopbackTest::ReceivePkt, this));
149
150 Ptr<Socket> txSocket = rxSocketFactory->CreateSocket();
151 txSocket->SendTo(Create<Packet>(246), 0, Inet6SocketAddress("::1", 80));
155 246,
156 "first socket should not receive it (it is bound specifically to the "
157 "second interface's address");
158}
159
160/**
161 * \ingroup internet-test
162 *
163 * \brief UDP Socket over IPv4 Test
164 */
166{
167 Ptr<Packet> m_receivedPacket; //!< Received packet (1).
168 Ptr<Packet> m_receivedPacket2; //!< Received packet (2).
170
171 /**
172 * \brief Get the TOS of the received packet.
173 * \returns The TOS.
174 */
176
177 /**
178 * \brief Get the priority of the received packet.
179 * \returns The priority.
180 */
182
183 /**
184 * \brief Send data.
185 * \param socket The sending socket.
186 * \param to The destination address.
187 */
188 void DoSendDataTo(Ptr<Socket> socket, std::string to);
189 /**
190 * \brief Send data.
191 * \param socket The sending socket.
192 * \param to The destination address.
193 */
194 void SendDataTo(Ptr<Socket> socket, std::string to);
195 /**
196 * \brief Send data.
197 * \param socket The sending socket.
198 */
199 void DoSendData(Ptr<Socket> socket);
200 /**
201 * \brief Send data.
202 * \param socket The sending socket.
203 */
204 void SendData(Ptr<Socket> socket);
205
206 public:
207 void DoRun() override;
209
210 /**
211 * \brief Receive packets (1).
212 * \param socket The receiving socket.
213 */
214 void ReceivePkt(Ptr<Socket> socket);
215 /**
216 * \brief Receive packets (2).
217 * \param socket The receiving socket.
218 */
219 void ReceivePkt2(Ptr<Socket> socket);
220
221 /**
222 * \brief Adds a packet to the list of sent packets.
223 * \param item The sent packet.
224 */
226};
227
229 : TestCase("UDP socket implementation")
230{
231}
232
233void
235{
236 uint32_t availableData;
237 availableData = socket->GetRxAvailable();
238 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
239 NS_TEST_ASSERT_MSG_EQ(availableData,
241 "ReceivedPacket size is not equal to the Rx buffer size");
242}
243
244void
246{
247 uint32_t availableData;
248 availableData = socket->GetRxAvailable();
249 m_receivedPacket2 = 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{
259 NS_TEST_EXPECT_MSG_NE(ipv4Item, nullptr, "no IPv4 packet");
260 Address addr;
262 Create<Ipv4QueueDiscItem>(ipv4Item->GetPacket()->Copy(), addr, 0, ipv4Item->GetHeader());
263}
264
267{
268 return static_cast<uint32_t>(m_sentPacket->GetHeader().GetTos());
269}
270
273{
274 SocketPriorityTag priorityTag;
275 bool found = m_sentPacket->GetPacket()->PeekPacketTag(priorityTag);
276 NS_TEST_EXPECT_MSG_EQ(found, true, "the packet should carry a SocketPriorityTag");
277 return static_cast<uint32_t>(priorityTag.GetPriority());
278}
279
280void
282{
283 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 1234);
284 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
285}
286
287void
289{
292 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
293 Seconds(0),
295 this,
296 socket,
297 to);
299}
300
301void
303{
304 NS_TEST_EXPECT_MSG_EQ(socket->Send(Create<Packet>(123), 0), 123, "100");
305}
306
307void
309{
310 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
311 Seconds(0),
313 this,
314 socket);
316}
317
318void
320{
321 // Create topology
322
323 // Receiver Node
324 Ptr<Node> rxNode = CreateObject<Node>();
325 // Sender Node
326 Ptr<Node> txNode = CreateObject<Node>();
327
328 NodeContainer nodes(rxNode, txNode);
329
330 SimpleNetDeviceHelper helperChannel1;
331 helperChannel1.SetNetDevicePointToPointMode(true);
332 NetDeviceContainer net1 = helperChannel1.Install(nodes);
333
334 SimpleNetDeviceHelper helperChannel2;
335 helperChannel2.SetNetDevicePointToPointMode(true);
336 NetDeviceContainer net2 = helperChannel2.Install(nodes);
337
338 InternetStackHelper internet;
339 internet.Install(nodes);
340
342 QueueDiscContainer qdiscs = tch.Install(net1.Get(1));
343
344 Ptr<Ipv4> ipv4;
345 uint32_t netdev_idx;
346 Ipv4InterfaceAddress ipv4Addr;
347
348 // Receiver Node
349 ipv4 = rxNode->GetObject<Ipv4>();
350 netdev_idx = ipv4->AddInterface(net1.Get(0));
351 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.0.1"), Ipv4Mask("/24"));
352 ipv4->AddAddress(netdev_idx, ipv4Addr);
353 ipv4->SetUp(netdev_idx);
354
355 netdev_idx = ipv4->AddInterface(net2.Get(0));
356 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.1.1"), Ipv4Mask("/24"));
357 ipv4->AddAddress(netdev_idx, ipv4Addr);
358 ipv4->SetUp(netdev_idx);
359
360 // Sender Node
361 ipv4 = txNode->GetObject<Ipv4>();
362 netdev_idx = ipv4->AddInterface(net1.Get(1));
363 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.0.2"), Ipv4Mask("/24"));
364 ipv4->AddAddress(netdev_idx, ipv4Addr);
365 ipv4->SetUp(netdev_idx);
366
367 netdev_idx = ipv4->AddInterface(net2.Get(1));
368 ipv4Addr = Ipv4InterfaceAddress(Ipv4Address("10.0.1.2"), Ipv4Mask("/24"));
369 ipv4->AddAddress(netdev_idx, ipv4Addr);
370 ipv4->SetUp(netdev_idx);
371
372 // Create the UDP sockets
373 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
374
375 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
376 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("10.0.0.1"), 1234)),
377 0,
378 "trivial");
379 rxSocket->SetRecvCallback(MakeCallback(&UdpSocketImplTest::ReceivePkt, this));
380
381 Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket();
382 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(InetSocketAddress(Ipv4Address("10.0.1.1"), 1234)),
383 0,
384 "trivial");
385 rxSocket2->SetRecvCallback(MakeCallback(&UdpSocketImplTest::ReceivePkt2, this));
386
387 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory>();
388 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
389 txSocket->SetAllowBroadcast(true);
390
391 // ------ Now the tests ------------
392
393 // Unicast test
394 SendDataTo(txSocket, "10.0.0.1");
397 0,
398 "second interface should not receive it");
399
402
403 // Simple broadcast test
404
405 SendDataTo(txSocket, "255.255.255.255");
407 0,
408 "first socket should not receive it (it is bound specifically to the "
409 "first interface's address");
411 0,
412 "second socket should not receive it (it is bound specifically to the "
413 "second interface's address");
414
417
418 // Broadcast test with multiple receiving sockets
419
420 // When receiving broadcast packets, all sockets sockets bound to
421 // the address/port should receive a copy of the same packet -- if
422 // the socket address matches.
423 rxSocket2->Dispose();
424 rxSocket2 = rxSocketFactory->CreateSocket();
425 rxSocket2->SetRecvCallback(MakeCallback(&UdpSocketImplTest::ReceivePkt2, this));
426 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(InetSocketAddress(Ipv4Address("0.0.0.0"), 1234)),
427 0,
428 "trivial");
429
430 SendDataTo(txSocket, "255.255.255.255");
432 0,
433 "first socket should not receive it (it is bound specifically to the "
434 "first interface's address");
436
437 m_receivedPacket = nullptr;
438 m_receivedPacket2 = nullptr;
439
440 // Simple Link-local multicast test
441
442 txSocket->BindToNetDevice(net1.Get(1));
443 SendDataTo(txSocket, "224.0.0.9");
445 0,
446 "first socket should not receive it (it is bound specifically to the "
447 "first interface's address");
448 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket2->GetSize(), 123, "recv2: 224.0.0.9");
449
452
453 // Simple getpeername tests
454
455 Address peerAddress;
456 int err = txSocket->GetPeerName(peerAddress);
457 NS_TEST_EXPECT_MSG_EQ(err, -1, "socket GetPeerName() should fail when socket is not connected");
458 NS_TEST_EXPECT_MSG_EQ(txSocket->GetErrno(),
460 "socket error code should be ERROR_NOTCONN");
461
462 InetSocketAddress peer("10.0.0.1", 1234);
463 err = txSocket->Connect(peer);
464 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket Connect() should succeed");
465
466 err = txSocket->GetPeerName(peerAddress);
467 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket GetPeerName() should succeed when socket is connected");
468 NS_TEST_EXPECT_MSG_EQ(peerAddress,
469 peer,
470 "address from socket GetPeerName() should equal the connected address");
471
474
475 // TOS and priority tests
476
477 // Intercept the packets dequeued by the queue disc on the sender node
478 qdiscs.Get(0)->TraceConnectWithoutContext("Dequeue",
480
481 // The socket is not connected.
482 txSocket->SetPriority(6); // Interactive
483 // Send a packet to a specified destination:
484 // - since the tos is zero, the priority set for the socket is used
485 SendDataTo(txSocket, "10.0.0.1");
487
488 NS_TEST_EXPECT_MSG_EQ(GetTos(), 0, "the TOS should be set to 0");
489 NS_TEST_EXPECT_MSG_EQ(GetPriority(), 6, "Interactive (6)");
490
492
493 InetSocketAddress dest("10.0.0.1", 1234);
494 txSocket->SetIpTos(0xb8); // EF
495 // the connect operation sets the tos (and priority) for the socket
496 NS_TEST_EXPECT_MSG_EQ(txSocket->Connect(dest), 0, "the connect operation failed");
497
498 SendData(txSocket);
500
501 NS_TEST_EXPECT_MSG_EQ(GetTos(), 0xb8, "the TOS should be set to 0xb8");
502 NS_TEST_EXPECT_MSG_EQ(GetPriority(), 4, "Interactive bulk (4)");
503
505
507}
508
509/**
510 * \ingroup internet-test
511 *
512 * \brief UDP Socket over IPv6 Test
513 */
515{
516 Ptr<Packet> m_receivedPacket; //!< Received packet (1).
517 Ptr<Packet> m_receivedPacket2; //!< Received packet (2).
518
519 /**
520 * \brief Send data.
521 * \param socket The sending socket.
522 * \param to The destination address.
523 */
524 void DoSendDataTo(Ptr<Socket> socket, std::string to);
525 /**
526 * \brief Send data.
527 * \param socket The sending socket.
528 * \param to The destination address.
529 */
530 void SendDataTo(Ptr<Socket> socket, std::string to);
531
532 public:
533 void DoRun() override;
535
536 /**
537 * \brief Receive packets (1).
538 * \param socket The receiving socket.
539 * \param packet The received packet.
540 * \param from The source address.
541 */
542 void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
543 /**
544 * \brief Receive packets (2).
545 * \param socket The receiving socket.
546 * \param packet The received packet.
547 * \param from The source address.
548 */
549 void ReceivePacket2(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
550 /**
551 * \brief Receive packets (1).
552 * \param socket The receiving socket.
553 */
554 void ReceivePkt(Ptr<Socket> socket);
555 /**
556 * \brief Receive packets (2).
557 * \param socket The receiving socket.
558 */
559 void ReceivePkt2(Ptr<Socket> socket);
560};
561
563 : TestCase("UDP6 socket implementation")
564{
565}
566
567void
569{
570 m_receivedPacket = packet;
571}
572
573void
575{
576 m_receivedPacket2 = packet;
577}
578
579void
581{
582 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
583 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
584 NS_TEST_ASSERT_MSG_EQ(availableData,
586 "ReceivedPacket size is not equal to the Rx buffer size");
587}
588
589void
591{
592 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
593 m_receivedPacket2 = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
594 NS_TEST_ASSERT_MSG_EQ(availableData,
596 "ReceivedPacket size is not equal to the Rx buffer size");
597}
598
599void
601{
602 Address realTo = Inet6SocketAddress(Ipv6Address(to.c_str()), 1234);
603 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "200");
604}
605
606void
608{
611 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
612 Seconds(0),
614 this,
615 socket,
616 to);
618}
619
620void
622{
623 // Create topology
624
625 // Receiver Node
626 Ptr<Node> rxNode = CreateObject<Node>();
627 // Sender Node
628 Ptr<Node> txNode = CreateObject<Node>();
629
630 NodeContainer nodes(rxNode, txNode);
631
632 SimpleNetDeviceHelper helperChannel1;
633 helperChannel1.SetNetDevicePointToPointMode(true);
634 NetDeviceContainer net1 = helperChannel1.Install(nodes);
635
636 SimpleNetDeviceHelper helperChannel2;
637 helperChannel2.SetNetDevicePointToPointMode(true);
638 NetDeviceContainer net2 = helperChannel2.Install(nodes);
639
640 InternetStackHelper internetv6;
641 internetv6.Install(nodes);
642
643 txNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
644 rxNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
645
646 Ipv6AddressHelper ipv6helper;
647 Ipv6InterfaceContainer iic1 = ipv6helper.AssignWithoutAddress(net1);
648 Ipv6InterfaceContainer iic2 = ipv6helper.AssignWithoutAddress(net2);
649
650 Ptr<NetDevice> device;
651 Ptr<Ipv6> ipv6;
652 int32_t ifIndex;
653 Ipv6InterfaceAddress ipv6Addr;
654
655 ipv6 = rxNode->GetObject<Ipv6>();
656 device = net1.Get(0);
657 ifIndex = ipv6->GetInterfaceForDevice(device);
658 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:0100::1"), Ipv6Prefix(64));
659 ipv6->AddAddress(ifIndex, ipv6Addr);
660 ipv6->SetUp(ifIndex);
661
662 device = net2.Get(0);
663 ifIndex = ipv6->GetInterfaceForDevice(device);
664 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:0100:1::1"), Ipv6Prefix(64));
665 ipv6->AddAddress(ifIndex, ipv6Addr);
666 ipv6->SetUp(ifIndex);
667
668 ipv6 = txNode->GetObject<Ipv6>();
669 device = net1.Get(1);
670 ifIndex = ipv6->GetInterfaceForDevice(device);
671 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:0100::2"), Ipv6Prefix(64));
672 ipv6->AddAddress(ifIndex, ipv6Addr);
673 ipv6->SetUp(ifIndex);
674
675 device = net2.Get(1);
676 ifIndex = ipv6->GetInterfaceForDevice(device);
677 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:0100:1::2"), Ipv6Prefix(64));
678 ipv6->AddAddress(ifIndex, ipv6Addr);
679 ipv6->SetUp(ifIndex);
680
681 // Create the UDP sockets
682 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
683 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
684 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(Inet6SocketAddress(Ipv6Address("2001:0100::1"), 1234)),
685 0,
686 "trivial");
687 rxSocket->SetRecvCallback(MakeCallback(&Udp6SocketImplTest::ReceivePkt, this));
688
689 Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket();
690 rxSocket2->SetRecvCallback(MakeCallback(&Udp6SocketImplTest::ReceivePkt2, this));
691 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(Inet6SocketAddress(Ipv6Address("2001:0100:1::1"), 1234)),
692 0,
693 "trivial");
694
695 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory>();
696 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
697 txSocket->SetAllowBroadcast(true);
698 // ------ Now the tests ------------
699
700 // Unicast test
701 SendDataTo(txSocket, "2001:0100::1");
703 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket2->GetSize(), 0, "second interface should receive it");
704
707
708 // Simple Link-local multicast test
709
710 // When receiving broadcast packets, all sockets sockets bound to
711 // the address/port should receive a copy of the same packet -- if
712 // the socket address matches.
713 rxSocket2->Dispose();
714 rxSocket2 = rxSocketFactory->CreateSocket();
715 rxSocket2->SetRecvCallback(MakeCallback(&Udp6SocketImplTest::ReceivePkt2, this));
716 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(Inet6SocketAddress(Ipv6Address("::"), 1234)),
717 0,
718 "trivial");
719
720 txSocket->BindToNetDevice(net1.Get(1));
721 SendDataTo(txSocket, "ff02::1");
723 0,
724 "first socket should not receive it (it is bound specifically to the "
725 "second interface's address");
726 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket2->GetSize(), 123, "recv2: ff02::1");
727
730
731 // Simple getpeername tests
732 Address peerAddress;
733 int err = txSocket->GetPeerName(peerAddress);
734 NS_TEST_EXPECT_MSG_EQ(err, -1, "socket GetPeerName() should fail when socket is not connected");
735 NS_TEST_EXPECT_MSG_EQ(txSocket->GetErrno(),
737 "socket error code should be ERROR_NOTCONN");
738
739 Inet6SocketAddress peer("2001:0100::1", 1234);
740 err = txSocket->Connect(peer);
741 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket Connect() should succeed");
742
743 err = txSocket->GetPeerName(peerAddress);
744 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket GetPeerName() should succeed when socket is connected");
745 NS_TEST_EXPECT_MSG_EQ(peerAddress,
746 peer,
747 "address from socket GetPeerName() should equal the connected address");
748
750}
751
752/**
753 * \ingroup internet-test
754 *
755 * \brief UDP TestSuite
756 */
758{
759 public:
761 : TestSuite("udp", Type::UNIT)
762 {
763 AddTestCase(new UdpSocketImplTest, TestCase::Duration::QUICK);
764 AddTestCase(new UdpSocketLoopbackTest, TestCase::Duration::QUICK);
765 AddTestCase(new Udp6SocketImplTest, TestCase::Duration::QUICK);
766 AddTestCase(new Udp6SocketLoopbackTest, TestCase::Duration::QUICK);
767 }
768};
769
770static UdpTestSuite g_udpTestSuite; //!< Static variable for test initialization
UDP Socket over IPv6 Test.
Definition udp-test.cc:515
Ptr< Packet > m_receivedPacket
Received packet (1).
Definition udp-test.cc:516
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive packets (1).
Definition udp-test.cc:568
void SendDataTo(Ptr< Socket > socket, std::string to)
Send data.
Definition udp-test.cc:607
void ReceivePkt2(Ptr< Socket > socket)
Receive packets (2).
Definition udp-test.cc:590
void DoRun() override
Implementation to actually run this TestCase.
Definition udp-test.cc:621
Ptr< Packet > m_receivedPacket2
Received packet (2).
Definition udp-test.cc:517
void ReceivePkt(Ptr< Socket > socket)
Receive packets (1).
Definition udp-test.cc:580
void ReceivePacket2(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive packets (2).
Definition udp-test.cc:574
void DoSendDataTo(Ptr< Socket > socket, std::string to)
Send data.
Definition udp-test.cc:600
UDP Socket Loopback over IPv6 Test.
Definition udp-test.cc:110
void ReceivePkt(Ptr< Socket > socket)
Receive a packet.
Definition udp-test.cc:129
Ptr< Packet > m_receivedPacket
Received packet.
Definition udp-test.cc:120
void DoRun() override
Implementation to actually run this TestCase.
Definition udp-test.cc:139
UDP Socket over IPv4 Test.
Definition udp-test.cc:166
uint32_t GetTos()
Get the TOS of the received packet.
Definition udp-test.cc:266
uint32_t GetPriority()
Get the priority of the received packet.
Definition udp-test.cc:272
void SendData(Ptr< Socket > socket)
Send data.
Definition udp-test.cc:308
void ReceivePkt2(Ptr< Socket > socket)
Receive packets (2).
Definition udp-test.cc:245
void SendDataTo(Ptr< Socket > socket, std::string to)
Send data.
Definition udp-test.cc:288
Ptr< Packet > m_receivedPacket
Received packet (1).
Definition udp-test.cc:167
void DoRun() override
Implementation to actually run this TestCase.
Definition udp-test.cc:319
void DoSendData(Ptr< Socket > socket)
Send data.
Definition udp-test.cc:302
Ptr< Ipv4QueueDiscItem > m_sentPacket
Sent packet.
Definition udp-test.cc:169
void ReceivePkt(Ptr< Socket > socket)
Receive packets (1).
Definition udp-test.cc:234
void SentPkt(Ptr< const QueueDiscItem > item)
Adds a packet to the list of sent packets.
Definition udp-test.cc:256
Ptr< Packet > m_receivedPacket2
Received packet (2).
Definition udp-test.cc:168
void DoSendDataTo(Ptr< Socket > socket, std::string to)
Send data.
Definition udp-test.cc:281
UDP Socket Loopback over IPv4 Test.
Definition udp-test.cc:53
Ptr< Packet > m_receivedPacket
Received packet.
Definition udp-test.cc:63
void ReceivePkt(Ptr< Socket > socket)
Receive a packet.
Definition udp-test.cc:72
void DoRun() override
Implementation to actually run this TestCase.
Definition udp-test.cc:83
UDP TestSuite.
Definition udp-test.cc:758
a polymophic address class
Definition address.h:90
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.
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
a class to represent an Ipv4 address mask
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.
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.
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.
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.
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
indicates whether the socket has a priority set.
Definition socket.h:1307
uint8_t GetPriority() const
Get the tag's priority.
Definition socket.cc:849
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
Build a set of QueueDisc objects.
QueueDiscContainer Install(NetDeviceContainer c)
static TrafficControlHelper Default(std::size_t nTxQueues=1)
API to create UDP socket instances.
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_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report if not.
Definition test.h:656
#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
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
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
static UdpTestSuite g_udpTestSuite
Static variable for test initialization.
Definition udp-test.cc:770