William A. Kennington III | 7c9365c | 2023-06-14 17:41:13 -0700 | [diff] [blame] | 1 | #include <fmt/format.h> |
| 2 | |
William A. Kennington III | 9330c87 | 2023-01-04 15:41:53 -0800 | [diff] [blame] | 3 | #include <stdplus/net/addr/ip.hpp> |
| 4 | #include <stdplus/numeric/endian.hpp> |
| 5 | |
William A. Kennington III | 751a6cc | 2023-06-16 15:41:05 -0700 | [diff] [blame^] | 6 | #include <string_view> |
| 7 | |
William A. Kennington III | 9330c87 | 2023-01-04 15:41:53 -0800 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
| 9 | |
William A. Kennington III | 751a6cc | 2023-06-16 15:41:05 -0700 | [diff] [blame^] | 10 | using std::literals::string_view_literals::operator""sv; |
| 11 | |
William A. Kennington III | 9330c87 | 2023-01-04 15:41:53 -0800 | [diff] [blame] | 12 | namespace stdplus |
| 13 | { |
| 14 | |
| 15 | TEST(EqualOperator, In4Addr) |
| 16 | { |
| 17 | EXPECT_EQ((In4Addr{255, 0, 255, 0}), (in_addr{hton(0xff00ff00)})); |
| 18 | EXPECT_EQ((in_addr{}), (In4Addr{})); |
| 19 | EXPECT_NE((In4Addr{1}), (In4Addr{in_addr{}})); |
| 20 | std::hash<In4Addr>{}(In4Addr{}); |
| 21 | } |
| 22 | |
William A. Kennington III | df1e473 | 2023-06-14 17:29:31 -0700 | [diff] [blame] | 23 | TEST(FromStr, In4Addr) |
| 24 | { |
| 25 | EXPECT_THROW(fromStr<In4Addr>(""), std::invalid_argument); |
| 26 | EXPECT_THROW(fromStr<In4Addr>("0"), std::invalid_argument); |
| 27 | EXPECT_THROW(fromStr<In4Addr>("0.0.0"), std::invalid_argument); |
| 28 | EXPECT_THROW(fromStr<In4Addr>("0.0.0."), std::invalid_argument); |
| 29 | EXPECT_THROW(fromStr<In4Addr>(".0.0.0"), std::invalid_argument); |
| 30 | EXPECT_THROW(fromStr<In4Addr>("0.0.0.0.0"), std::invalid_argument); |
| 31 | EXPECT_THROW(fromStr<In4Addr>("x.0.0.0"), std::invalid_argument); |
| 32 | EXPECT_THROW(fromStr<In4Addr>("ff.0.0.0"), std::invalid_argument); |
| 33 | EXPECT_THROW(fromStr<In4Addr>("256.0.0.0"), std::overflow_error); |
| 34 | |
| 35 | EXPECT_EQ((In4Addr{}), fromStr<In4Addr>("0.0.0.0")); |
| 36 | EXPECT_EQ((In4Addr{192, 168, 1, 1}), fromStr<In4Addr>("192.168.001.1")); |
| 37 | } |
| 38 | |
William A. Kennington III | 7c9365c | 2023-06-14 17:41:13 -0700 | [diff] [blame] | 39 | TEST(ToStr, In4Addr) |
| 40 | { |
| 41 | ToStrHandle<ToStr<In4Addr>> tsh; |
| 42 | EXPECT_EQ("255.255.255.255", tsh(in_addr{0xffffffff})); |
| 43 | EXPECT_EQ("1.15.3.4", tsh(In4Addr{1, 15, 3, 4})); |
| 44 | EXPECT_EQ("0.0.0.0", tsh(In4Addr{})); |
| 45 | EXPECT_EQ("a 1.15.3.4 b", fmt::format("a {} b", In4Addr{1, 15, 3, 4})); |
| 46 | } |
| 47 | |
William A. Kennington III | 9330c87 | 2023-01-04 15:41:53 -0800 | [diff] [blame] | 48 | TEST(EqualOperator, In6Addr) |
| 49 | { |
| 50 | EXPECT_EQ((In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}), |
| 51 | (in6_addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff})); |
| 52 | EXPECT_EQ((in6_addr{}), (In6Addr{})); |
| 53 | EXPECT_NE((In6Addr{1}), (In6Addr{in6_addr{}})); |
| 54 | std::hash<In6Addr>{}(In6Addr{}); |
| 55 | } |
| 56 | |
William A. Kennington III | 751a6cc | 2023-06-16 15:41:05 -0700 | [diff] [blame^] | 57 | TEST(FromStr, In6Addr) |
| 58 | { |
| 59 | constexpr FromStr<In6Addr> fs; |
| 60 | EXPECT_THROW(fs(""sv), std::invalid_argument); |
| 61 | EXPECT_THROW(fs("0"sv), std::invalid_argument); |
| 62 | EXPECT_THROW(fs("0:0"sv), std::invalid_argument); |
| 63 | EXPECT_THROW(fs("0::0:"sv), std::invalid_argument); |
| 64 | EXPECT_THROW(fs("0:::"sv), std::invalid_argument); |
| 65 | EXPECT_THROW(fs(":::0"sv), std::invalid_argument); |
| 66 | EXPECT_THROW(fs("0:::0"sv), std::invalid_argument); |
| 67 | EXPECT_THROW(fs("0::0::0"sv), std::invalid_argument); |
| 68 | EXPECT_THROW(fs("1::0.0.0."sv), std::invalid_argument); |
| 69 | EXPECT_THROW(fs("1::.0.0.0"sv), std::invalid_argument); |
| 70 | EXPECT_THROW(fs("x::0"sv), std::invalid_argument); |
| 71 | EXPECT_THROW(fs("g::0"sv), std::invalid_argument); |
| 72 | EXPECT_THROW(fs("0:1:2:3:4::5:6:7"sv), std::invalid_argument); |
| 73 | EXPECT_THROW(fs("::0:1:2:3:4:5:6:7"sv), std::invalid_argument); |
| 74 | EXPECT_THROW(fs("0:1:2:3:4:5:6:7::"sv), std::invalid_argument); |
| 75 | EXPECT_THROW(fs("0:1:2:3:4:5:6:7:8"sv), std::invalid_argument); |
| 76 | EXPECT_THROW(fs("0:1:2:3:4:5:6:0.0.0.0"sv), std::invalid_argument); |
| 77 | EXPECT_THROW(fs("0:1:2:3:4:5::0.0.0.0"sv), std::invalid_argument); |
| 78 | EXPECT_THROW(fs("ffff0::0"sv), std::overflow_error); |
| 79 | |
| 80 | EXPECT_EQ((In6Addr{}), fs("::"sv)); |
| 81 | EXPECT_EQ((In6Addr{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 82 | 255, 255, 255, 255, 255}), |
| 83 | fs("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"sv)); |
| 84 | EXPECT_EQ((In6Addr{}), fs("0:0:0:0:0:0:0:0"sv)); |
| 85 | EXPECT_EQ((In6Addr{0, 0xff}), fs("ff::"sv)); |
| 86 | EXPECT_EQ((In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}), |
| 87 | fs("::ff"sv)); |
| 88 | EXPECT_EQ((In6Addr{0, 0, 0, 0, 0, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}), |
| 89 | fs("0:0:ff::ff"sv)); |
| 90 | EXPECT_EQ((In6Addr{0, 1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 6, 0, 7, 0, 8}), |
| 91 | fs("1:2:3:4::6:7:8"sv)); |
| 92 | EXPECT_EQ((In6Addr{0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0}), |
| 93 | fs("1:2:3:4:5:6:7::"sv)); |
| 94 | EXPECT_EQ((In6Addr{0, 0, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8}), |
| 95 | fs("::2:3:4:5:6:7:8"sv)); |
| 96 | EXPECT_EQ( |
| 97 | (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}), |
| 98 | fs("::ffff:192.168.0.1"sv)); |
| 99 | EXPECT_EQ((In6Addr{0, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 168, 0, 1}), |
| 100 | fs("ff::255.168.0.1"sv)); |
| 101 | EXPECT_EQ((In6Addr{0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 255, 168, 0, 1}), |
| 102 | fs("0:1:2:3:4:5:255.168.0.1"sv)); |
| 103 | } |
| 104 | |
William A. Kennington III | ea3aeb7 | 2023-01-05 04:22:19 -0800 | [diff] [blame] | 105 | TEST(EqualOperator, InAnyAddr) |
| 106 | { |
| 107 | EXPECT_EQ(InAnyAddr(In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 108 | 0xff}), |
| 109 | (In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff})); |
| 110 | EXPECT_NE(InAnyAddr(In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 111 | 0xff}), |
| 112 | (in_addr{})); |
| 113 | EXPECT_EQ((In6Addr{}), InAnyAddr(In6Addr{})); |
| 114 | EXPECT_NE((In4Addr{}), InAnyAddr(In6Addr{})); |
| 115 | EXPECT_EQ((In4Addr{}), InAnyAddr(In4Addr{})); |
| 116 | EXPECT_NE((In6Addr{}), InAnyAddr(In4Addr{})); |
| 117 | EXPECT_EQ(InAnyAddr(In6Addr{}), (in6_addr{})); |
| 118 | EXPECT_EQ(InAnyAddr(In4Addr{}), (in_addr{})); |
| 119 | EXPECT_NE(InAnyAddr(In6Addr{1}), InAnyAddr(In6Addr{})); |
| 120 | EXPECT_EQ(InAnyAddr(In6Addr{1}), InAnyAddr(In6Addr{1})); |
| 121 | |
| 122 | std::hash<InAnyAddr>{}(In4Addr{}); |
| 123 | std::hash<InAnyAddr>{}(In6Addr{}); |
| 124 | } |
| 125 | |
William A. Kennington III | 9330c87 | 2023-01-04 15:41:53 -0800 | [diff] [blame] | 126 | } // namespace stdplus |