17#include <gtest/gtest.h>
25TEST(UtilTest, Mac2SS2MacRoundtrip)
29 EXPECT_EQ(
Mac2String(mac),
"aa:bb:cc:dd:ee:ff");
33TEST(UtilTest, S2MacRejectsMalformedInput)
35 EXPECT_THROW(
String2Mac(
"not-a-mac"), std::exception);
36 EXPECT_THROW(
String2Mac(
"aa:bb:cc:dd:ee"), std::exception);
37 EXPECT_THROW(
String2Mac(
"gg:bb:cc:dd:ee:ff"), std::exception);
41TEST(UtilTest, Ip2SS2IpRoundtrip)
43 auto readBytes =
Ip2String(
"192.168.1.42");
44 EXPECT_EQ(
String2Ip(readBytes.data(), readBytes.size()),
"192.168.1.42");
48TEST(UtilTest, Ip2SRejectsMalformedInput)
50 EXPECT_THROW(
Ip2String(
"not.an.ip"), std::exception);
51 EXPECT_THROW(
Ip2String(
""), std::exception);
52 EXPECT_THROW(
Ip2String(
"1.2.3.256"), std::exception);
56TEST(UtilTest, ToHexEncodesBytes)
58 const std::vector<std::uint8_t> data = {0xDE, 0xAD, 0xBE, 0xEF};
59 EXPECT_EQ(
ToHex(data),
"deadbeef");
63TEST(UtilTest, DecodeBytesStripsTrailingNuls)
65 const std::vector<std::uint8_t> data = {
'h',
'i', 0, 0, 0};
70TEST(UtilTest, MaxTimeoutReportsElapsed)
72 const MaxTimeout t(std::chrono::duration<double>(0.0));
78 EXPECT_EQ(ByteSwap<std::uint16_t>(0x1234U), 0x3412U);
84 EXPECT_EQ(ByteSwap<std::uint32_t>(0x07B280DEU), 0xDE80B207U);
90 EXPECT_EQ(ByteSwap<std::uint64_t>(0x0123456789ABCDEFULL), 0xEFCDAB8967452301ULL);
96 EXPECT_EQ(ByteSwap<std::uint8_t>(0x42U), 0x42U);
101 constexpr std::uint8_t value = 0x42U;
103 EXPECT_EQ(
ByteAt(value, 0U), 0x42U);
110 constexpr std::uint16_t value = 0x1234U;
112 EXPECT_EQ(
ByteAt(value, 0U), 0x34U);
113 EXPECT_EQ(
ByteAt(value, 1U), 0x12U);
120 constexpr std::uint32_t value = 0x12345678U;
122 EXPECT_EQ(
ByteAt(value, 0U), 0x78U);
123 EXPECT_EQ(
ByteAt(value, 1U), 0x56U);
124 EXPECT_EQ(
ByteAt(value, 2U), 0x34U);
125 EXPECT_EQ(
ByteAt(value, 3U), 0x12U);
132 constexpr std::uint64_t value = 0x0123456789ABCDEFULL;
134 EXPECT_EQ(
ByteAt(value, 0U), 0xEFU);
135 EXPECT_EQ(
ByteAt(value, 1U), 0xCDU);
136 EXPECT_EQ(
ByteAt(value, 2U), 0xABU);
137 EXPECT_EQ(
ByteAt(value, 3U), 0x89U);
138 EXPECT_EQ(
ByteAt(value, 4U), 0x67U);
139 EXPECT_EQ(
ByteAt(value, 5U), 0x45U);
140 EXPECT_EQ(
ByteAt(value, 6U), 0x23U);
141 EXPECT_EQ(
ByteAt(value, 7U), 0x01U);
Time-limited-operation helper.
bool TimedOut() const
Whether the countdown has elapsed.
Declares exceptions and error types used by the PROFINET IO controller stack.
TEST(AlarmEndpointTests, DefaultTransportIsLayer2)
std::string String2Ip(std::span< const std::uint8_t > ipBytes)
Format raw IPv4 address bytes as a dotted string.
std::array< std::uint8_t, macAddressLength > MacAddress
A 6-byte Ethernet MAC address.
std::array< std::uint8_t, 4 > Ip2String(const std::string &ipStr)
Parse a dotted-decimal IPv4 address string.
MacAddress String2Mac(const std::string &macStr)
Parse a colon-separated MAC address string.
std::string ToHex(const std::uint8_t *data, std::size_t len)
Hex-encode a byte buffer.
std::string Mac2String(const MacAddress &mac)
Format a MAC address as a colon-separated string.
std::string DecodeBytes(const std::uint8_t *data, std::size_t len)
Decode bytes to a UTF-8 string, stripping trailing NUL bytes.
constexpr std::uint8_t ByteAt(T value, std::size_t index)
Return one byte from an integral value.
Declares Linux Ethernet, addressing, timing, and utility helpers used by the PROFINET stack.