blob: bff72484fc971470a549021ba0140bdb2c3da356 [file] [log] [blame]
William A. Kennington III8955b5e2023-06-14 16:30:51 -07001#include <fmt/format.h>
2
William A. Kennington IIIb25086a2023-01-09 18:53:29 -08003#include <stdplus/net/addr/ether.hpp>
4
5#include <gtest/gtest.h>
6
7namespace stdplus
8{
9
10TEST(EqualOperator, EtherAddr)
11{
12 EXPECT_EQ((EtherAddr{1}), (ether_addr{1}));
13 EXPECT_EQ((EtherAddr{}), (EtherAddr{}));
14 EXPECT_NE((EtherAddr{1}), (EtherAddr{ether_addr{}}));
15 std::hash<EtherAddr>{}(EtherAddr{});
16}
17
William A. Kennington III459bd0d2023-06-17 00:29:47 -070018TEST(Query, IsMulticast)
19{
20 EXPECT_TRUE((EtherAddr{255, 255, 255, 255, 255, 255}.isMulticast()));
21 EXPECT_TRUE((EtherAddr{1}.isMulticast()));
22 EXPECT_FALSE((EtherAddr{0, 1, 2, 3, 4, 5}.isMulticast()));
23 EXPECT_FALSE((EtherAddr{0xfe, 255, 255, 255, 255, 255}.isMulticast()));
24}
25
26TEST(Query, IsUnicast)
27{
28 EXPECT_TRUE((EtherAddr{0, 1, 2, 3, 4, 5}.isUnicast()));
29 EXPECT_TRUE((EtherAddr{0xfe, 255, 255, 255, 255, 255}.isUnicast()));
30 EXPECT_FALSE((EtherAddr{}.isUnicast()));
31 EXPECT_FALSE((EtherAddr{1}.isUnicast()));
32 EXPECT_FALSE((EtherAddr{255, 255, 255, 255, 255, 255}.isUnicast()));
33}
34
William A. Kennington III8955b5e2023-06-14 16:30:51 -070035TEST(ToStr, EthAddr)
36{
37 ToStrHandle<ToStr<EtherAddr>> tsh;
38 EXPECT_EQ("11:22:33:44:55:66",
39 tsh(EtherAddr{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}));
40 EXPECT_EQ("01:02:03:04:05:67",
41 tsh(EtherAddr{0x01, 0x02, 0x03, 0x04, 0x05, 0x67}));
42 EXPECT_EQ("00:00:00:00:00:00",
43 tsh(ether_addr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}));
44 EXPECT_EQ(
45 "a 01:02:03:04:05:67 b",
46 fmt::format("a {} b", EtherAddr{0x01, 0x02, 0x03, 0x04, 0x05, 0x67}));
47}
48
William A. Kennington III84c7aa22023-06-14 13:26:47 -070049TEST(FromStr, EtherAddr)
50{
51 EXPECT_THROW(fromStr<EtherAddr>("0x:00:00:00:00:00"),
52 std::invalid_argument);
53 EXPECT_THROW(fromStr<EtherAddr>("00:00:00:00:00"), std::invalid_argument);
54 EXPECT_THROW(fromStr<EtherAddr>("00:00:00:00:00:"), std::invalid_argument);
55 EXPECT_THROW(fromStr<EtherAddr>("00:00:00:00::00"), std::invalid_argument);
56 EXPECT_THROW(fromStr<EtherAddr>(":00:00:00:00:00"), std::invalid_argument);
57 EXPECT_THROW(fromStr<EtherAddr>("00::00:00:00:00"), std::invalid_argument);
58 EXPECT_THROW(fromStr<EtherAddr>(":::::"), std::invalid_argument);
59 EXPECT_THROW(fromStr<EtherAddr>("00:0:0:0:0"), std::invalid_argument);
60 EXPECT_THROW(fromStr<EtherAddr>("00:00:00:00:00:00:00"),
61 std::invalid_argument);
62 EXPECT_THROW(fromStr<EtherAddr>(""), std::invalid_argument);
63 EXPECT_THROW(fromStr<EtherAddr>("123456789XYZ"), std::invalid_argument);
64 EXPECT_THROW(fromStr<EtherAddr>("123456789AB"), std::overflow_error);
65 EXPECT_THROW(fromStr<EtherAddr>("123456789ABCD"), std::overflow_error);
66
67 EXPECT_EQ((EtherAddr{}), fromStr<EtherAddr>("00:00:00:00:00:00"));
68 EXPECT_EQ((EtherAddr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}),
69 fromStr<EtherAddr>("FF:EE:DD:cc:bb:aa"));
70 EXPECT_EQ((EtherAddr{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}),
71 fromStr<EtherAddr>("0:1:2:3:4:5"));
72 EXPECT_EQ((EtherAddr{0x01, 0x23, 0x45, 0x67, 0x89, 0xab}),
73 fromStr<EtherAddr>("0123456789AB"));
74 EXPECT_EQ((EtherAddr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}),
75 fromStr<EtherAddr>("FFEEDDccbbaa"));
76}
77
William A. Kennington IIIb25086a2023-01-09 18:53:29 -080078} // namespace stdplus