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 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | namespace stdplus |
| 9 | { |
| 10 | |
| 11 | TEST(EqualOperator, In4Addr) |
| 12 | { |
| 13 | EXPECT_EQ((In4Addr{255, 0, 255, 0}), (in_addr{hton(0xff00ff00)})); |
| 14 | EXPECT_EQ((in_addr{}), (In4Addr{})); |
| 15 | EXPECT_NE((In4Addr{1}), (In4Addr{in_addr{}})); |
| 16 | std::hash<In4Addr>{}(In4Addr{}); |
| 17 | } |
| 18 | |
William A. Kennington III | df1e473 | 2023-06-14 17:29:31 -0700 | [diff] [blame] | 19 | TEST(FromStr, In4Addr) |
| 20 | { |
| 21 | EXPECT_THROW(fromStr<In4Addr>(""), std::invalid_argument); |
| 22 | EXPECT_THROW(fromStr<In4Addr>("0"), std::invalid_argument); |
| 23 | EXPECT_THROW(fromStr<In4Addr>("0.0.0"), std::invalid_argument); |
| 24 | EXPECT_THROW(fromStr<In4Addr>("0.0.0."), std::invalid_argument); |
| 25 | EXPECT_THROW(fromStr<In4Addr>(".0.0.0"), std::invalid_argument); |
| 26 | EXPECT_THROW(fromStr<In4Addr>("0.0.0.0.0"), std::invalid_argument); |
| 27 | EXPECT_THROW(fromStr<In4Addr>("x.0.0.0"), std::invalid_argument); |
| 28 | EXPECT_THROW(fromStr<In4Addr>("ff.0.0.0"), std::invalid_argument); |
| 29 | EXPECT_THROW(fromStr<In4Addr>("256.0.0.0"), std::overflow_error); |
| 30 | |
| 31 | EXPECT_EQ((In4Addr{}), fromStr<In4Addr>("0.0.0.0")); |
| 32 | EXPECT_EQ((In4Addr{192, 168, 1, 1}), fromStr<In4Addr>("192.168.001.1")); |
| 33 | } |
| 34 | |
William A. Kennington III | 7c9365c | 2023-06-14 17:41:13 -0700 | [diff] [blame^] | 35 | TEST(ToStr, In4Addr) |
| 36 | { |
| 37 | ToStrHandle<ToStr<In4Addr>> tsh; |
| 38 | EXPECT_EQ("255.255.255.255", tsh(in_addr{0xffffffff})); |
| 39 | EXPECT_EQ("1.15.3.4", tsh(In4Addr{1, 15, 3, 4})); |
| 40 | EXPECT_EQ("0.0.0.0", tsh(In4Addr{})); |
| 41 | EXPECT_EQ("a 1.15.3.4 b", fmt::format("a {} b", In4Addr{1, 15, 3, 4})); |
| 42 | } |
| 43 | |
William A. Kennington III | 9330c87 | 2023-01-04 15:41:53 -0800 | [diff] [blame] | 44 | TEST(EqualOperator, In6Addr) |
| 45 | { |
| 46 | EXPECT_EQ((In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}), |
| 47 | (in6_addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff})); |
| 48 | EXPECT_EQ((in6_addr{}), (In6Addr{})); |
| 49 | EXPECT_NE((In6Addr{1}), (In6Addr{in6_addr{}})); |
| 50 | std::hash<In6Addr>{}(In6Addr{}); |
| 51 | } |
| 52 | |
William A. Kennington III | ea3aeb7 | 2023-01-05 04:22:19 -0800 | [diff] [blame] | 53 | TEST(EqualOperator, InAnyAddr) |
| 54 | { |
| 55 | EXPECT_EQ(InAnyAddr(In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 56 | 0xff}), |
| 57 | (In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff})); |
| 58 | EXPECT_NE(InAnyAddr(In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 59 | 0xff}), |
| 60 | (in_addr{})); |
| 61 | EXPECT_EQ((In6Addr{}), InAnyAddr(In6Addr{})); |
| 62 | EXPECT_NE((In4Addr{}), InAnyAddr(In6Addr{})); |
| 63 | EXPECT_EQ((In4Addr{}), InAnyAddr(In4Addr{})); |
| 64 | EXPECT_NE((In6Addr{}), InAnyAddr(In4Addr{})); |
| 65 | EXPECT_EQ(InAnyAddr(In6Addr{}), (in6_addr{})); |
| 66 | EXPECT_EQ(InAnyAddr(In4Addr{}), (in_addr{})); |
| 67 | EXPECT_NE(InAnyAddr(In6Addr{1}), InAnyAddr(In6Addr{})); |
| 68 | EXPECT_EQ(InAnyAddr(In6Addr{1}), InAnyAddr(In6Addr{1})); |
| 69 | |
| 70 | std::hash<InAnyAddr>{}(In4Addr{}); |
| 71 | std::hash<InAnyAddr>{}(In6Addr{}); |
| 72 | } |
| 73 | |
William A. Kennington III | 9330c87 | 2023-01-04 15:41:53 -0800 | [diff] [blame] | 74 | } // namespace stdplus |