William A. Kennington III | 8955b5e | 2023-06-14 16:30:51 -0700 | [diff] [blame] | 1 | #include <fmt/format.h> |
| 2 | |
William A. Kennington III | b25086a | 2023-01-09 18:53:29 -0800 | [diff] [blame] | 3 | #include <stdplus/net/addr/ether.hpp> |
| 4 | |
William A. Kennington III | 5b20b0a | 2023-07-18 13:38:37 -0700 | [diff] [blame] | 5 | #include <format> |
| 6 | |
William A. Kennington III | b25086a | 2023-01-09 18:53:29 -0800 | [diff] [blame] | 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | namespace stdplus |
| 10 | { |
| 11 | |
| 12 | TEST(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 III | 459bd0d | 2023-06-17 00:29:47 -0700 | [diff] [blame] | 20 | TEST(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 | |
| 28 | TEST(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 III | 8955b5e | 2023-06-14 16:30:51 -0700 | [diff] [blame] | 37 | TEST(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 Williams | d8e0af5 | 2024-08-16 15:21:12 -0400 | [diff] [blame^] | 46 | 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 III | 8955b5e | 2023-06-14 16:30:51 -0700 | [diff] [blame] | 52 | } |
| 53 | |
William A. Kennington III | 84c7aa2 | 2023-06-14 13:26:47 -0700 | [diff] [blame] | 54 | TEST(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 III | b25086a | 2023-01-09 18:53:29 -0800 | [diff] [blame] | 83 | } // namespace stdplus |