William A. Kennington III | 9330c87 | 2023-01-04 15:41:53 -0800 | [diff] [blame] | 1 | #include <stdplus/net/addr/ip.hpp> |
| 2 | #include <stdplus/numeric/endian.hpp> |
| 3 | |
| 4 | #include <gtest/gtest.h> |
| 5 | |
| 6 | namespace stdplus |
| 7 | { |
| 8 | |
| 9 | TEST(EqualOperator, In4Addr) |
| 10 | { |
| 11 | EXPECT_EQ((In4Addr{255, 0, 255, 0}), (in_addr{hton(0xff00ff00)})); |
| 12 | EXPECT_EQ((in_addr{}), (In4Addr{})); |
| 13 | EXPECT_NE((In4Addr{1}), (In4Addr{in_addr{}})); |
| 14 | std::hash<In4Addr>{}(In4Addr{}); |
| 15 | } |
| 16 | |
William A. Kennington III | df1e473 | 2023-06-14 17:29:31 -0700 | [diff] [blame^] | 17 | TEST(FromStr, In4Addr) |
| 18 | { |
| 19 | EXPECT_THROW(fromStr<In4Addr>(""), std::invalid_argument); |
| 20 | EXPECT_THROW(fromStr<In4Addr>("0"), std::invalid_argument); |
| 21 | EXPECT_THROW(fromStr<In4Addr>("0.0.0"), std::invalid_argument); |
| 22 | EXPECT_THROW(fromStr<In4Addr>("0.0.0."), std::invalid_argument); |
| 23 | EXPECT_THROW(fromStr<In4Addr>(".0.0.0"), std::invalid_argument); |
| 24 | EXPECT_THROW(fromStr<In4Addr>("0.0.0.0.0"), std::invalid_argument); |
| 25 | EXPECT_THROW(fromStr<In4Addr>("x.0.0.0"), std::invalid_argument); |
| 26 | EXPECT_THROW(fromStr<In4Addr>("ff.0.0.0"), std::invalid_argument); |
| 27 | EXPECT_THROW(fromStr<In4Addr>("256.0.0.0"), std::overflow_error); |
| 28 | |
| 29 | EXPECT_EQ((In4Addr{}), fromStr<In4Addr>("0.0.0.0")); |
| 30 | EXPECT_EQ((In4Addr{192, 168, 1, 1}), fromStr<In4Addr>("192.168.001.1")); |
| 31 | } |
| 32 | |
William A. Kennington III | 9330c87 | 2023-01-04 15:41:53 -0800 | [diff] [blame] | 33 | TEST(EqualOperator, In6Addr) |
| 34 | { |
| 35 | EXPECT_EQ((In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff}), |
| 36 | (in6_addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff})); |
| 37 | EXPECT_EQ((in6_addr{}), (In6Addr{})); |
| 38 | EXPECT_NE((In6Addr{1}), (In6Addr{in6_addr{}})); |
| 39 | std::hash<In6Addr>{}(In6Addr{}); |
| 40 | } |
| 41 | |
William A. Kennington III | ea3aeb7 | 2023-01-05 04:22:19 -0800 | [diff] [blame] | 42 | TEST(EqualOperator, InAnyAddr) |
| 43 | { |
| 44 | EXPECT_EQ(InAnyAddr(In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 45 | 0xff}), |
| 46 | (In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff})); |
| 47 | EXPECT_NE(InAnyAddr(In6Addr{0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 48 | 0xff}), |
| 49 | (in_addr{})); |
| 50 | EXPECT_EQ((In6Addr{}), InAnyAddr(In6Addr{})); |
| 51 | EXPECT_NE((In4Addr{}), InAnyAddr(In6Addr{})); |
| 52 | EXPECT_EQ((In4Addr{}), InAnyAddr(In4Addr{})); |
| 53 | EXPECT_NE((In6Addr{}), InAnyAddr(In4Addr{})); |
| 54 | EXPECT_EQ(InAnyAddr(In6Addr{}), (in6_addr{})); |
| 55 | EXPECT_EQ(InAnyAddr(In4Addr{}), (in_addr{})); |
| 56 | EXPECT_NE(InAnyAddr(In6Addr{1}), InAnyAddr(In6Addr{})); |
| 57 | EXPECT_EQ(InAnyAddr(In6Addr{1}), InAnyAddr(In6Addr{1})); |
| 58 | |
| 59 | std::hash<InAnyAddr>{}(In4Addr{}); |
| 60 | std::hash<InAnyAddr>{}(In6Addr{}); |
| 61 | } |
| 62 | |
William A. Kennington III | 9330c87 | 2023-01-04 15:41:53 -0800 | [diff] [blame] | 63 | } // namespace stdplus |