blob: 625633e3642947f5ad04407586f8ec482824c47f [file] [log] [blame]
William A. Kennington III9330c872023-01-04 15:41:53 -08001#include <stdplus/net/addr/ip.hpp>
2#include <stdplus/numeric/endian.hpp>
3
4#include <gtest/gtest.h>
5
6namespace stdplus
7{
8
9TEST(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 IIIdf1e4732023-06-14 17:29:31 -070017TEST(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 III9330c872023-01-04 15:41:53 -080033TEST(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 IIIea3aeb72023-01-05 04:22:19 -080042TEST(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 III9330c872023-01-04 15:41:53 -080063} // namespace stdplus