blob: 474bd96b998c79695a25a456c650fbca0789656f [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
William A. Kennington III5b20b0a2023-07-18 13:38:37 -07005#include <format>
6
William A. Kennington IIIb25086a2023-01-09 18:53:29 -08007#include <gtest/gtest.h>
8
9namespace stdplus
10{
11
12TEST(EqualOperator, EtherAddr)
13{
14 EXPECT_EQ((EtherAddr{1}), (ether_addr{1}));
15 EXPECT_EQ((EtherAddr{}), (EtherAddr{}));
16 EXPECT_NE((EtherAddr{1}), (EtherAddr{ether_addr{}}));
17 std::hash<EtherAddr>{}(EtherAddr{});
18}
19
William A. Kennington III459bd0d2023-06-17 00:29:47 -070020TEST(Query, IsMulticast)
21{
22 EXPECT_TRUE((EtherAddr{255, 255, 255, 255, 255, 255}.isMulticast()));
23 EXPECT_TRUE((EtherAddr{1}.isMulticast()));
24 EXPECT_FALSE((EtherAddr{0, 1, 2, 3, 4, 5}.isMulticast()));
25 EXPECT_FALSE((EtherAddr{0xfe, 255, 255, 255, 255, 255}.isMulticast()));
26}
27
28TEST(Query, IsUnicast)
29{
30 EXPECT_TRUE((EtherAddr{0, 1, 2, 3, 4, 5}.isUnicast()));
31 EXPECT_TRUE((EtherAddr{0xfe, 255, 255, 255, 255, 255}.isUnicast()));
32 EXPECT_FALSE((EtherAddr{}.isUnicast()));
33 EXPECT_FALSE((EtherAddr{1}.isUnicast()));
34 EXPECT_FALSE((EtherAddr{255, 255, 255, 255, 255, 255}.isUnicast()));
35}
36
William A. Kennington III8955b5e2023-06-14 16:30:51 -070037TEST(ToStr, EthAddr)
38{
39 ToStrHandle<ToStr<EtherAddr>> tsh;
40 EXPECT_EQ("11:22:33:44:55:66",
41 tsh(EtherAddr{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}));
42 EXPECT_EQ("01:02:03:04:05:67",
43 tsh(EtherAddr{0x01, 0x02, 0x03, 0x04, 0x05, 0x67}));
44 EXPECT_EQ("00:00:00:00:00:00",
45 tsh(ether_addr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}));
Patrick Williamsd8e0af52024-08-16 15:21:12 -040046 EXPECT_EQ("a 01:02:03:04:05:67 b",
47 fmt::format("a {} b",
48 EtherAddr{0x01, 0x02, 0x03, 0x04, 0x05, 0x67}));
49 EXPECT_EQ("a 01:02:03:04:05:67 b",
50 std::format("a {} b",
51 EtherAddr{0x01, 0x02, 0x03, 0x04, 0x05, 0x67}));
William A. Kennington III8955b5e2023-06-14 16:30:51 -070052}
53
William A. Kennington III84c7aa22023-06-14 13:26:47 -070054TEST(FromStr, EtherAddr)
55{
56 EXPECT_THROW(fromStr<EtherAddr>("0x:00:00:00:00:00"),
57 std::invalid_argument);
58 EXPECT_THROW(fromStr<EtherAddr>("00:00:00:00:00"), std::invalid_argument);
59 EXPECT_THROW(fromStr<EtherAddr>("00:00:00:00:00:"), std::invalid_argument);
60 EXPECT_THROW(fromStr<EtherAddr>("00:00:00:00::00"), std::invalid_argument);
61 EXPECT_THROW(fromStr<EtherAddr>(":00:00:00:00:00"), std::invalid_argument);
62 EXPECT_THROW(fromStr<EtherAddr>("00::00:00:00:00"), std::invalid_argument);
63 EXPECT_THROW(fromStr<EtherAddr>(":::::"), std::invalid_argument);
64 EXPECT_THROW(fromStr<EtherAddr>("00:0:0:0:0"), std::invalid_argument);
65 EXPECT_THROW(fromStr<EtherAddr>("00:00:00:00:00:00:00"),
66 std::invalid_argument);
67 EXPECT_THROW(fromStr<EtherAddr>(""), std::invalid_argument);
68 EXPECT_THROW(fromStr<EtherAddr>("123456789XYZ"), std::invalid_argument);
69 EXPECT_THROW(fromStr<EtherAddr>("123456789AB"), std::overflow_error);
70 EXPECT_THROW(fromStr<EtherAddr>("123456789ABCD"), std::overflow_error);
71
72 EXPECT_EQ((EtherAddr{}), fromStr<EtherAddr>("00:00:00:00:00:00"));
73 EXPECT_EQ((EtherAddr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}),
74 fromStr<EtherAddr>("FF:EE:DD:cc:bb:aa"));
75 EXPECT_EQ((EtherAddr{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}),
76 fromStr<EtherAddr>("0:1:2:3:4:5"));
77 EXPECT_EQ((EtherAddr{0x01, 0x23, 0x45, 0x67, 0x89, 0xab}),
78 fromStr<EtherAddr>("0123456789AB"));
79 EXPECT_EQ((EtherAddr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}),
80 fromStr<EtherAddr>("FFEEDDccbbaa"));
81}
82
William A. Kennington IIIb25086a2023-01-09 18:53:29 -080083} // namespace stdplus