blob: 0a2bc30dc629d0f1f8631f5721b11739b6fc2a8b [file] [log] [blame]
William A. Kennington III7c9365c2023-06-14 17:41:13 -07001#include <fmt/format.h>
2
William A. Kennington III9330c872023-01-04 15:41:53 -08003#include <stdplus/net/addr/ip.hpp>
4#include <stdplus/numeric/endian.hpp>
5
6#include <gtest/gtest.h>
7
8namespace stdplus
9{
10
11TEST(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 IIIdf1e4732023-06-14 17:29:31 -070019TEST(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 III7c9365c2023-06-14 17:41:13 -070035TEST(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 III9330c872023-01-04 15:41:53 -080044TEST(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 IIIea3aeb72023-01-05 04:22:19 -080053TEST(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 III9330c872023-01-04 15:41:53 -080074} // namespace stdplus