blob: e326572e96e72e0b60cc7e2dccb2130fe0781ced [file] [log] [blame]
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -08001#include <stdplus/net/addr/sock.hpp>
2
William A. Kennington III87a16972023-12-17 22:19:44 -08003#include <format>
4#include <string>
William A. Kennington IIIc03c91f2023-12-17 22:18:53 -08005#include <string_view>
6
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -08007#include <gtest/gtest.h>
8
William A. Kennington IIIc03c91f2023-12-17 22:18:53 -08009using std::literals::string_view_literals::operator""sv;
10
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -080011namespace stdplus
12{
13
14TEST(Sock4Addr, Basic)
15{
16 constexpr Sock4Addr addr1{.addr = In4Addr{255}, .port = 3959};
17 Sock4Addr addr2{.addr = In4Addr{255}, .port = 2000};
18 Sock4Addr addr3{.addr = In4Addr{0}, .port = 3959};
19
20 EXPECT_EQ(addr1, addr1);
21 EXPECT_NE(addr1, addr2);
22 EXPECT_NE(addr3, addr1);
23
24 auto addr = addr1.sockaddr();
25 EXPECT_EQ(AF_INET, addr.sin_family);
26 EXPECT_EQ(In4Addr{255}, addr.sin_addr);
27 EXPECT_EQ(3959, stdplus::ntoh(addr.sin_port));
28 EXPECT_EQ(sizeof(sockaddr_in), addr1.sockaddrLen());
29}
30
William A. Kennington III87a16972023-12-17 22:19:44 -080031TEST(Sock4Addr, FromStr)
32{
33 constexpr FromStr<Sock4Addr> fs;
34 EXPECT_THROW(fs("10"sv), std::invalid_argument);
35 EXPECT_THROW(fs(":3959"sv), std::invalid_argument);
36 EXPECT_THROW(fs("0.0.0.0"sv), std::invalid_argument);
37 EXPECT_THROW(fs("0.0.0.0:"sv), std::invalid_argument);
38 EXPECT_THROW(fs(":::80"sv), std::invalid_argument);
39 EXPECT_EQ((Sock4Addr{In4Addr{}, 30}), fs("0.0.0.0:30"sv));
40}
41
42TEST(Sock4Addr, ToStr)
43{
44 ToStrHandle<ToStr<Sock4Addr>> tsh;
45 EXPECT_EQ("0.0.0.0:3959", tsh(Sock4Addr({}, 3959)));
46 EXPECT_EQ("255.0.255.255:28",
47 tsh(Sock4Addr(In4Addr{255, 0, 255, 255}, 28)));
48 EXPECT_EQ("a 1.2.3.4:32 b",
49 std::format("a {} b", Sock4Addr(In4Addr{1, 2, 3, 4}, 32)));
50}
51
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -080052TEST(Sock6Addr, Basic)
53{
54 constexpr Sock6Addr addr1{.addr = In6Addr{255}, .port = 3959, .scope = 0};
55 Sock6Addr addr2{.addr = In6Addr{255}, .port = 2000, .scope = 0};
56 Sock6Addr addr3{.addr = In6Addr{0}, .port = 3959, .scope = 0};
57 Sock6Addr addr4{.addr = In6Addr{255}, .port = 3959, .scope = 1};
58
59 EXPECT_EQ(addr1, addr1);
60 EXPECT_NE(addr1, addr2);
61 EXPECT_NE(addr3, addr1);
62 EXPECT_NE(addr4, addr1);
63
64 auto addr = addr1.sockaddr();
65 EXPECT_EQ(AF_INET6, addr.sin6_family);
66 EXPECT_EQ(In6Addr{255}, addr.sin6_addr);
67 EXPECT_EQ(3959, stdplus::ntoh(addr.sin6_port));
68 EXPECT_EQ(sizeof(sockaddr_in6), addr1.sockaddrLen());
69}
70
William A. Kennington III87a16972023-12-17 22:19:44 -080071TEST(Sock6Addr, FromStr)
72{
73 constexpr FromStr<Sock6Addr> fs;
74 EXPECT_THROW(fs("10"sv), std::invalid_argument);
75 EXPECT_THROW(fs(":10"sv), std::invalid_argument);
76 EXPECT_THROW(fs("ff::"sv), std::invalid_argument);
77 EXPECT_THROW(fs("[ff::]"sv), std::invalid_argument);
78 EXPECT_THROW(fs("[::]:"sv), std::invalid_argument);
79 EXPECT_THROW(fs("0.0.0.0:0"sv), std::invalid_argument);
80 EXPECT_EQ((Sock6Addr{In6Addr{}, 80, 0}), fs("[::]:80"sv));
81}
82
83TEST(Sock6Addr, ToStr)
84{
85 ToStrHandle<ToStr<Sock6Addr>> tsh;
86 EXPECT_EQ("[::]:0", tsh(Sock6Addr({}, 0, 0)));
87 EXPECT_EQ("[ff00::]:128", tsh(Sock6Addr(In6Addr{0xff}, 128, 0)));
88 EXPECT_EQ("a [102:304::]:32 b",
89 std::format("a {} b", Sock6Addr(In6Addr{1, 2, 3, 4}, 32, 0)));
90}
91
William A. Kennington IIIc03c91f2023-12-17 22:18:53 -080092TEST(SockUAddr, Basic)
93{
94 // Non-abstract Requires null-terminator
95 EXPECT_THROW(SockUAddr(std::string(SockUAddr::maxLen, 'a')),
96 std::invalid_argument);
97 EXPECT_NO_THROW(SockUAddr(std::string(SockUAddr::maxLen - 1, 'a')));
98
99 // Abstract can use the full space
100 EXPECT_THROW(SockUAddr(std::string(SockUAddr::maxLen + 1, '\0')),
101 std::invalid_argument);
102 EXPECT_NO_THROW(SockUAddr(std::string(SockUAddr::maxLen, '\0')));
103
104 EXPECT_THROW(SockUAddr("hi\0o"sv), std::invalid_argument);
105
106 constexpr SockUAddr addr1{"@hi"sv};
107 constexpr SockUAddr addr2{"\0hi"sv};
108 constexpr SockUAddr addr3{"/nope"sv};
109 constexpr SockUAddr addr4{""sv};
110
111 EXPECT_EQ(addr1, addr1);
112 EXPECT_EQ(addr1, addr2);
113 EXPECT_NE(addr3, addr1);
114 EXPECT_NE(addr4, addr1);
115 EXPECT_NE(addr4, addr3);
116
117 EXPECT_EQ(addr1.path(), "@hi"sv);
118 EXPECT_EQ(addr2.path(), "@hi"sv);
119 EXPECT_EQ(addr3.path(), "/nope"sv);
120 EXPECT_EQ(addr4.path(), ""sv);
121
122 auto addr = addr1.sockaddr();
123 EXPECT_EQ(AF_UNIX, addr.sun_family);
124 EXPECT_EQ("\0hi"sv, std::string_view(addr.sun_path, 3));
125 auto buf = addr2.buf();
126 auto ptr = reinterpret_cast<std::uint8_t*>(&buf);
127 EXPECT_TRUE(
128 std::equal(ptr, ptr + buf.len, reinterpret_cast<std::uint8_t*>(&addr)));
129 addr = addr3;
130 EXPECT_EQ("/nope\0"sv, std::string_view(addr.sun_path, 6));
131 EXPECT_EQ(addr1.sockaddrLen(), sizeof(addr.sun_family) + 3);
132 EXPECT_EQ(addr2.sockaddrLen(), sizeof(addr.sun_family) + 3);
133 EXPECT_EQ(addr3.sockaddrLen(), sizeof(addr.sun_family) + 6);
134 EXPECT_EQ(addr4.sockaddrLen(), sizeof(addr.sun_family));
135}
136
William A. Kennington III87a16972023-12-17 22:19:44 -0800137TEST(SockUAddr, FromStr)
138{
139 constexpr FromStr<SockUAddr> fs;
140 std::string as(sizeof(sockaddr_un), 'a');
141 EXPECT_THROW(fs(std::string_view(as)), std::invalid_argument);
142 EXPECT_THROW(fs("a\0"sv), std::invalid_argument);
143 EXPECT_EQ((SockUAddr{"/nope"sv}), fs("unix:/nope"sv));
144 EXPECT_EQ((SockUAddr{"@hi"sv}), fs("@hi"sv));
145}
146
147TEST(SockUAddr, ToStr)
148{
149 ToStrHandle<ToStr<SockUAddr>> tsh;
150 EXPECT_EQ("unix:/nope", tsh(SockUAddr("/nope"sv)));
151 EXPECT_EQ("unix:@hi", tsh(SockUAddr("\0hi"sv)));
152 EXPECT_EQ("a unix:a b", std::format("a {} b", SockUAddr("a"sv)));
153}
154
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -0800155TEST(SockAnyAddr, Basic)
156{
157 constexpr SockAnyAddr addr1(std::in_place_type<Sock4Addr>, In4Addr{255},
158 3959);
William A. Kennington IIIc03c91f2023-12-17 22:18:53 -0800159 constexpr SockAnyAddr addr2(std::in_place_type<SockUAddr>, "/hi"sv);
160 EXPECT_NE(addr1, addr2);
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -0800161 EXPECT_EQ(addr1, addr1);
162 auto buf = addr1.buf();
163 EXPECT_EQ(buf.len, sizeof(sockaddr_in));
164 EXPECT_EQ(addr1.sockaddrLen(), sizeof(sockaddr_in));
William A. Kennington IIIc03c91f2023-12-17 22:18:53 -0800165 EXPECT_EQ(addr2.sockaddrLen(), sizeof(sa_family_t) + 4);
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -0800166}
167
William A. Kennington III87a16972023-12-17 22:19:44 -0800168TEST(SockAnyAddr, FromStr)
169{
170 constexpr FromStr<SockAnyAddr> fs;
171 EXPECT_THROW(fs("abcd"sv), std::invalid_argument);
172 EXPECT_THROW(fs("/nope"sv), std::invalid_argument);
173 EXPECT_EQ((Sock4Addr{In4Addr{}, 30}), fs("0.0.0.0:30"sv));
174 EXPECT_EQ((Sock6Addr{In6Addr{}, 80, 0}), fs("[::]:80"sv));
175 EXPECT_EQ((SockUAddr{"/nope"sv}), fs("unix:/nope"sv));
176}
177
178TEST(SockAnyAddr, ToStr)
179{
180 ToStrHandle<ToStr<SockAnyAddr>> tsh;
181 EXPECT_EQ("unix:/nope", tsh(SockUAddr("/nope"sv)));
182 EXPECT_EQ("0.0.0.0:3949", tsh(Sock4Addr(In4Addr{}, 3949)));
183 EXPECT_EQ("a [::]:356 b",
184 std::format("a {} b", Sock6Addr(In6Addr{}, 356, 0)));
185}
186
William A. Kennington IIIbe3fb712023-12-17 19:14:56 -0800187} // namespace stdplus