20#include <gtest/gtest.h>
42std::uint16_t GetLocalPort(
const UdpSocket& socket)
44 return socket.LocalEndpoint().port;
51TEST(UdpSocketTests, RecvFromReturnsPayloadAndSourceEndpoint)
53 UdpSocket receiver(0, std::chrono::duration<double>(1.0));
55 const std::uint16_t receiverPort = GetLocalPort(receiver);
56 ASSERT_NE(receiverPort, 0U);
58 UdpSocket sender(
"127.0.0.1", receiverPort, std::chrono::duration<double>(1.0));
60 const std::vector<std::uint8_t>
payload{0x01, 0x02, 0x03, 0x04};
73TEST(UdpSocketTests, SendToSendsToSuppliedEndpoint)
75 UdpSocket receiver(0, std::chrono::duration<double>(1.0));
77 const std::uint16_t receiverPort = GetLocalPort(receiver);
78 ASSERT_NE(receiverPort, 0U);
80 UdpSocket sender(
"127.0.0.1", receiverPort, std::chrono::duration<double>(1.0));
82 const std::vector<std::uint8_t>
payload{0x10, 0x20, 0x30, 0x40};
90 const std::vector<std::uint8_t> response{0xAA, 0xBB, 0xCC};
94 const std::vector<std::uint8_t> echoed = sender.
Recv(128);
96 EXPECT_EQ(echoed, response);
100TEST(UdpSocketTests, RecvFromReturnsEmptyDatagramOnTimeout)
102 UdpSocket receiver(0, std::chrono::duration<double>(0.02));
106 EXPECT_TRUE(datagram.
data.empty());
112TEST(UdpSocketTests, MoveConstructionTransfersSocketOwnership)
114 UdpSocket original(0, std::chrono::duration<double>(1.0));
124TEST(UdpSocketTests, MoveAssignmentTransfersSocketOwnership)
126 UdpSocket source(0, std::chrono::duration<double>(1.0));
129 UdpSocket destination(0, std::chrono::duration<double>(1.0));
131 destination = std::move(source);
RAII wrapper around a portable IPv4 UDP socket.
std::vector< std::uint8_t > Recv(std::size_t maxLen=defaultReceiveBufferSize) const
Receive a UDP datagram from the connected peer.
void SendTo(const std::vector< std::uint8_t > &data, const UdpEndpoint &) override
Send a datagram to a specific IPv4 endpoint.
void Send(const std::vector< std::uint8_t > &data) const
Send a UDP datagram to the connected peer.
UdpDatagram RecvFrom(std::size_t maxLen) override
Receive a datagram together with its source endpoint.
UdpEndpoint LocalEndpoint() const
Return the local IPv4 endpoint assigned to the socket.
TEST(AlarmEndpointTests, DefaultTransportIsLayer2)
A UDP datagram together with the IPv4 address of its sender.
std::vector< std::uint8_t > data
Datagram payload.
UdpEndpoint source
IPv4 address and UDP port of the datagram sender.
Portable IPv4 UDP endpoint.
std::string address
Numeric IPv4 address in dotted-decimal notation.
std::uint16_t port
UDP port in host byte order.
Declares the portable IPv4 UDP socket implementation.
Declares Linux Ethernet, addressing, timing, and utility helpers used by the PROFINET stack.