PROFINET IO Controller Stack 1.0.0
Modern C++ implementation of a PROFINET IO Controller stack
Loading...
Searching...
No Matches
test_util.cpp
Go to the documentation of this file.
1
16
17#include <gtest/gtest.h>
18
19#include "profinet/exceptions.h"
20#include "profinet/util.h"
21
22namespace profinet
23{
25TEST(UtilTest, Mac2SS2MacRoundtrip)
26{
27 const MacAddress mac = String2Mac("aa:bb:cc:dd:ee:ff");
28
29 EXPECT_EQ(Mac2String(mac), "aa:bb:cc:dd:ee:ff");
30}
31
33TEST(UtilTest, S2MacRejectsMalformedInput)
34{
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);
38}
39
41TEST(UtilTest, Ip2SS2IpRoundtrip)
42{
43 auto readBytes = Ip2String("192.168.1.42");
44 EXPECT_EQ(String2Ip(readBytes.data(), readBytes.size()), "192.168.1.42");
45}
46
48TEST(UtilTest, Ip2SRejectsMalformedInput)
49{
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);
53}
54
56TEST(UtilTest, ToHexEncodesBytes)
57{
58 const std::vector<std::uint8_t> data = {0xDE, 0xAD, 0xBE, 0xEF};
59 EXPECT_EQ(ToHex(data), "deadbeef");
60}
61
63TEST(UtilTest, DecodeBytesStripsTrailingNuls)
64{
65 const std::vector<std::uint8_t> data = {'h', 'i', 0, 0, 0};
66 EXPECT_EQ(DecodeBytes(data), "hi");
67}
68
70TEST(UtilTest, MaxTimeoutReportsElapsed)
71{
72 const MaxTimeout t(std::chrono::duration<double>(0.0));
73 EXPECT_TRUE(t.TimedOut());
74}
76TEST(UtilTest, ByteSwapU16)
77{
78 EXPECT_EQ(ByteSwap<std::uint16_t>(0x1234U), 0x3412U);
79}
80
82TEST(UtilTest, ByteSwapU32)
83{
84 EXPECT_EQ(ByteSwap<std::uint32_t>(0x07B280DEU), 0xDE80B207U);
85}
86
88TEST(UtilTest, ByteSwapU64)
89{
90 EXPECT_EQ(ByteSwap<std::uint64_t>(0x0123456789ABCDEFULL), 0xEFCDAB8967452301ULL);
91}
92
94TEST(UtilTest, ByteSwapU8)
95{
96 EXPECT_EQ(ByteSwap<std::uint8_t>(0x42U), 0x42U);
97}
99TEST(UtilTest, ByteAtU8)
100{
101 constexpr std::uint8_t value = 0x42U;
102
103 EXPECT_EQ(ByteAt(value, 0U), 0x42U);
104}
105
108TEST(UtilTest, ByteAtU16)
109{
110 constexpr std::uint16_t value = 0x1234U;
111
112 EXPECT_EQ(ByteAt(value, 0U), 0x34U);
113 EXPECT_EQ(ByteAt(value, 1U), 0x12U);
114}
115
118TEST(UtilTest, ByteAtU32)
119{
120 constexpr std::uint32_t value = 0x12345678U;
121
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);
126}
127
130TEST(UtilTest, ByteAtU64)
131{
132 constexpr std::uint64_t value = 0x0123456789ABCDEFULL;
133
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);
142}
143} // namespace profinet
Time-limited-operation helper.
Definition util.h:267
bool TimedOut() const
Whether the countdown has elapsed.
Definition util.h:279
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.
Definition util.cpp:206
std::array< std::uint8_t, macAddressLength > MacAddress
A 6-byte Ethernet MAC address.
Definition util.h:48
std::array< std::uint8_t, 4 > Ip2String(const std::string &ipStr)
Parse a dotted-decimal IPv4 address string.
Definition util.cpp:222
MacAddress String2Mac(const std::string &macStr)
Parse a colon-separated MAC address string.
Definition util.cpp:122
std::string ToHex(const std::uint8_t *data, std::size_t len)
Hex-encode a byte buffer.
Definition util.cpp:104
std::string Mac2String(const MacAddress &mac)
Format a MAC address as a colon-separated string.
Definition util.cpp:188
std::string DecodeBytes(const std::uint8_t *data, std::size_t len)
Decode bytes to a UTF-8 string, stripping trailing NUL bytes.
Definition util.cpp:246
constexpr std::uint8_t ByteAt(T value, std::size_t index)
Return one byte from an integral value.
Definition util.h:244
Declares Linux Ethernet, addressing, timing, and utility helpers used by the PROFINET stack.