PROFINET IO Controller Stack 1.0.0
Modern C++ implementation of a PROFINET IO Controller stack
Loading...
Searching...
No Matches
test_udpSocket.cpp
Go to the documentation of this file.
1
19
20#include <gtest/gtest.h>
21
22#include <chrono>
23#include <cstdint>
24#include <string>
25#include <vector>
26
27#include "profinet/udpSocket.h"
28#include "profinet/util.h"
29
30namespace profinet
31{
32
33namespace
34{
35
42std::uint16_t GetLocalPort(const UdpSocket& socket)
43{
44 return socket.LocalEndpoint().port;
45}
46
47} // namespace
48
51TEST(UdpSocketTests, RecvFromReturnsPayloadAndSourceEndpoint)
52{
53 UdpSocket receiver(0, std::chrono::duration<double>(1.0));
54
55 const std::uint16_t receiverPort = GetLocalPort(receiver);
56 ASSERT_NE(receiverPort, 0U);
57
58 UdpSocket sender("127.0.0.1", receiverPort, std::chrono::duration<double>(1.0));
59
60 const std::vector<std::uint8_t> payload{0x01, 0x02, 0x03, 0x04};
61
62 sender.Send(payload);
63
64 const UdpDatagram datagram = receiver.RecvFrom(128);
65
66 EXPECT_EQ(datagram.data, payload);
67 EXPECT_EQ(datagram.source.address, "127.0.0.1");
68 EXPECT_NE(datagram.source.port, 0U);
69}
70
73TEST(UdpSocketTests, SendToSendsToSuppliedEndpoint)
74{
75 UdpSocket receiver(0, std::chrono::duration<double>(1.0));
76
77 const std::uint16_t receiverPort = GetLocalPort(receiver);
78 ASSERT_NE(receiverPort, 0U);
79
80 UdpSocket sender("127.0.0.1", receiverPort, std::chrono::duration<double>(1.0));
81
82 const std::vector<std::uint8_t> payload{0x10, 0x20, 0x30, 0x40};
83
84 sender.Send(payload);
85
86 const UdpDatagram received = receiver.RecvFrom(128);
87
88 ASSERT_EQ(received.data, payload);
89
90 const std::vector<std::uint8_t> response{0xAA, 0xBB, 0xCC};
91
92 receiver.SendTo(response, received.source);
93
94 const std::vector<std::uint8_t> echoed = sender.Recv(128);
95
96 EXPECT_EQ(echoed, response);
97}
98
100TEST(UdpSocketTests, RecvFromReturnsEmptyDatagramOnTimeout)
101{
102 UdpSocket receiver(0, std::chrono::duration<double>(0.02));
103
104 const UdpDatagram datagram = receiver.RecvFrom(128);
105
106 EXPECT_TRUE(datagram.data.empty());
107 EXPECT_TRUE(datagram.source.address.empty());
108 EXPECT_EQ(datagram.source.port, 0U);
109}
110
112TEST(UdpSocketTests, MoveConstructionTransfersSocketOwnership)
113{
114 UdpSocket original(0, std::chrono::duration<double>(1.0));
115
116 const UdpEndpoint endpoint = original.LocalEndpoint();
117
118 UdpSocket moved(std::move(original));
119
120 EXPECT_EQ(moved.LocalEndpoint(), endpoint);
121}
122
124TEST(UdpSocketTests, MoveAssignmentTransfersSocketOwnership)
125{
126 UdpSocket source(0, std::chrono::duration<double>(1.0));
127 const UdpEndpoint sourceEndpoint = source.LocalEndpoint();
128
129 UdpSocket destination(0, std::chrono::duration<double>(1.0));
130
131 destination = std::move(source);
132
133 EXPECT_EQ(destination.LocalEndpoint(), sourceEndpoint);
134}
135
136} // namespace profinet
RAII wrapper around a portable IPv4 UDP socket.
Definition udpSocket.h:40
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.
Definition IUdpSocket.h:65
std::vector< std::uint8_t > data
Datagram payload.
Definition IUdpSocket.h:67
UdpEndpoint source
IPv4 address and UDP port of the datagram sender.
Definition IUdpSocket.h:70
Portable IPv4 UDP endpoint.
Definition IUdpSocket.h:42
std::string address
Numeric IPv4 address in dotted-decimal notation.
Definition IUdpSocket.h:44
std::uint16_t port
UDP port in host byte order.
Definition IUdpSocket.h:47
Bytes payload
Definition test_dcp.cpp:392
Declares the portable IPv4 UDP socket implementation.
Declares Linux Ethernet, addressing, timing, and utility helpers used by the PROFINET stack.