1 //===-- SocketTest.cpp ----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "TestingSupport/Host/SocketTestUtilities.h"
10 #include "TestingSupport/SubsystemRAII.h"
11 #include "lldb/Host/Config.h"
12 #include "lldb/Host/MainLoop.h"
13 #include "lldb/Utility/UriParser.h"
14 #include "llvm/Testing/Support/Error.h"
15 #include "gtest/gtest.h"
17 using namespace lldb_private
;
19 struct SocketTestParams
{
21 std::string localhost_ip
;
24 class SocketTest
: public testing::TestWithParam
<SocketTestParams
> {
26 SubsystemRAII
<Socket
> subsystems
;
29 bool HostSupportsProtocol() const {
30 if (GetParam().is_ipv6
)
31 return HostSupportsIPv6();
32 return HostSupportsIPv4();
36 TEST_P(SocketTest
, DecodeHostAndPort
) {
37 EXPECT_THAT_EXPECTED(Socket::DecodeHostAndPort("localhost:1138"),
38 llvm::HasValue(Socket::HostAndPort
{"localhost", 1138}));
41 Socket::DecodeHostAndPort("google.com:65536"),
42 llvm::FailedWithMessage(
43 "invalid host:port specification: 'google.com:65536'"));
46 Socket::DecodeHostAndPort("google.com:-1138"),
47 llvm::FailedWithMessage(
48 "invalid host:port specification: 'google.com:-1138'"));
51 Socket::DecodeHostAndPort("google.com:65536"),
52 llvm::FailedWithMessage(
53 "invalid host:port specification: 'google.com:65536'"));
55 EXPECT_THAT_EXPECTED(Socket::DecodeHostAndPort("12345"),
56 llvm::HasValue(Socket::HostAndPort
{"", 12345}));
58 EXPECT_THAT_EXPECTED(Socket::DecodeHostAndPort("*:0"),
59 llvm::HasValue(Socket::HostAndPort
{"*", 0}));
61 EXPECT_THAT_EXPECTED(Socket::DecodeHostAndPort("*:65535"),
62 llvm::HasValue(Socket::HostAndPort
{"*", 65535}));
65 Socket::DecodeHostAndPort("[::1]:12345"),
66 llvm::HasValue(Socket::HostAndPort
{"::1", 12345}));
69 Socket::DecodeHostAndPort("[abcd:12fg:AF58::1]:12345"),
70 llvm::HasValue(Socket::HostAndPort
{"abcd:12fg:AF58::1", 12345}));
74 TEST_P(SocketTest
, DomainListenConnectAccept
) {
75 llvm::SmallString
<64> Path
;
76 std::error_code EC
= llvm::sys::fs::createUniqueDirectory("DomainListenConnectAccept", Path
);
78 llvm::sys::path::append(Path
, "test");
80 // Skip the test if the $TMPDIR is too long to hold a domain socket.
81 if (Path
.size() > 107u)
84 std::unique_ptr
<DomainSocket
> socket_a_up
;
85 std::unique_ptr
<DomainSocket
> socket_b_up
;
86 CreateDomainConnectedSockets(Path
, &socket_a_up
, &socket_b_up
);
89 TEST_P(SocketTest
, DomainMainLoopAccept
) {
90 llvm::SmallString
<64> Path
;
92 llvm::sys::fs::createUniqueDirectory("DomainListenConnectAccept", Path
);
94 llvm::sys::path::append(Path
, "test");
96 // Skip the test if the $TMPDIR is too long to hold a domain socket.
97 if (Path
.size() > 107u)
100 auto listen_socket_up
= std::make_unique
<DomainSocket
>(
101 /*should_close=*/true, /*child_process_inherit=*/false);
102 Status error
= listen_socket_up
->Listen(Path
, 5);
103 ASSERT_THAT_ERROR(error
.ToError(), llvm::Succeeded());
104 ASSERT_TRUE(listen_socket_up
->IsValid());
107 std::unique_ptr
<Socket
> accepted_socket_up
;
108 auto expected_handles
= listen_socket_up
->Accept(
109 loop
, [&accepted_socket_up
, &loop
](std::unique_ptr
<Socket
> sock_up
) {
110 accepted_socket_up
= std::move(sock_up
);
111 loop
.RequestTermination();
113 ASSERT_THAT_EXPECTED(expected_handles
, llvm::Succeeded());
115 auto connect_socket_up
= std::make_unique
<DomainSocket
>(
116 /*should_close=*/true, /*child_process_inherit=*/false);
117 ASSERT_THAT_ERROR(connect_socket_up
->Connect(Path
).ToError(),
119 ASSERT_TRUE(connect_socket_up
->IsValid());
122 ASSERT_TRUE(accepted_socket_up
);
123 ASSERT_TRUE(accepted_socket_up
->IsValid());
127 TEST_P(SocketTest
, TCPListen0ConnectAccept
) {
128 if (!HostSupportsProtocol())
130 std::unique_ptr
<TCPSocket
> socket_a_up
;
131 std::unique_ptr
<TCPSocket
> socket_b_up
;
132 CreateTCPConnectedSockets(GetParam().localhost_ip
, &socket_a_up
,
136 TEST_P(SocketTest
, TCPMainLoopAccept
) {
137 if (!HostSupportsProtocol())
140 const bool child_processes_inherit
= false;
141 auto listen_socket_up
=
142 std::make_unique
<TCPSocket
>(true, child_processes_inherit
);
143 Status error
= listen_socket_up
->Listen(
144 llvm::formatv("[{0}]:0", GetParam().localhost_ip
).str(), 5);
145 ASSERT_THAT_ERROR(error
.ToError(), llvm::Succeeded());
146 ASSERT_TRUE(listen_socket_up
->IsValid());
149 std::unique_ptr
<Socket
> accepted_socket_up
;
150 auto expected_handles
= listen_socket_up
->Accept(
151 loop
, [&accepted_socket_up
, &loop
](std::unique_ptr
<Socket
> sock_up
) {
152 accepted_socket_up
= std::move(sock_up
);
153 loop
.RequestTermination();
155 ASSERT_THAT_EXPECTED(expected_handles
, llvm::Succeeded());
157 std::unique_ptr
<TCPSocket
> connect_socket_up(
158 new TCPSocket(true, child_processes_inherit
));
161 ->Connect(llvm::formatv("[{0}]:{1}", GetParam().localhost_ip
,
162 listen_socket_up
->GetLocalPortNumber())
166 ASSERT_TRUE(connect_socket_up
->IsValid());
169 ASSERT_TRUE(accepted_socket_up
);
170 ASSERT_TRUE(accepted_socket_up
->IsValid());
173 TEST_P(SocketTest
, TCPGetAddress
) {
174 std::unique_ptr
<TCPSocket
> socket_a_up
;
175 std::unique_ptr
<TCPSocket
> socket_b_up
;
176 if (!HostSupportsProtocol())
178 CreateTCPConnectedSockets(GetParam().localhost_ip
, &socket_a_up
,
181 EXPECT_EQ(socket_a_up
->GetLocalPortNumber(),
182 socket_b_up
->GetRemotePortNumber());
183 EXPECT_EQ(socket_b_up
->GetLocalPortNumber(),
184 socket_a_up
->GetRemotePortNumber());
185 EXPECT_NE(socket_a_up
->GetLocalPortNumber(),
186 socket_b_up
->GetLocalPortNumber());
187 EXPECT_STREQ(GetParam().localhost_ip
.c_str(),
188 socket_a_up
->GetRemoteIPAddress().c_str());
189 EXPECT_STREQ(GetParam().localhost_ip
.c_str(),
190 socket_b_up
->GetRemoteIPAddress().c_str());
193 TEST_P(SocketTest
, UDPConnect
) {
194 // UDPSocket::Connect() creates sockets with AF_INET (IPv4).
195 if (!HostSupportsIPv4())
197 llvm::Expected
<std::unique_ptr
<UDPSocket
>> socket
=
198 UDPSocket::Connect("127.0.0.1:0", /*child_processes_inherit=*/false);
200 ASSERT_THAT_EXPECTED(socket
, llvm::Succeeded());
201 EXPECT_TRUE(socket
.get()->IsValid());
204 TEST_P(SocketTest
, TCPListen0GetPort
) {
205 if (!HostSupportsIPv4())
207 llvm::Expected
<std::unique_ptr
<TCPSocket
>> sock
=
208 Socket::TcpListen("10.10.12.3:0", false);
209 ASSERT_THAT_EXPECTED(sock
, llvm::Succeeded());
210 ASSERT_TRUE(sock
.get()->IsValid());
211 EXPECT_NE(sock
.get()->GetLocalPortNumber(), 0);
214 TEST_P(SocketTest
, TCPGetConnectURI
) {
215 std::unique_ptr
<TCPSocket
> socket_a_up
;
216 std::unique_ptr
<TCPSocket
> socket_b_up
;
217 if (!HostSupportsProtocol())
219 CreateTCPConnectedSockets(GetParam().localhost_ip
, &socket_a_up
,
222 std::string
uri(socket_a_up
->GetRemoteConnectionURI());
223 EXPECT_EQ((URI
{"connect", GetParam().localhost_ip
,
224 socket_a_up
->GetRemotePortNumber(), "/"}),
228 TEST_P(SocketTest
, UDPGetConnectURI
) {
229 // UDPSocket::Connect() creates sockets with AF_INET (IPv4).
230 if (!HostSupportsIPv4())
232 llvm::Expected
<std::unique_ptr
<UDPSocket
>> socket
=
233 UDPSocket::Connect("127.0.0.1:0", /*child_processes_inherit=*/false);
234 ASSERT_THAT_EXPECTED(socket
, llvm::Succeeded());
236 std::string uri
= socket
.get()->GetRemoteConnectionURI();
237 EXPECT_EQ((URI
{"udp", "127.0.0.1", 0, "/"}), URI::Parse(uri
));
240 #if LLDB_ENABLE_POSIX
241 TEST_P(SocketTest
, DomainGetConnectURI
) {
242 llvm::SmallString
<64> domain_path
;
244 llvm::sys::fs::createUniqueDirectory("DomainListenConnectAccept", domain_path
);
246 llvm::sys::path::append(domain_path
, "test");
248 // Skip the test if the $TMPDIR is too long to hold a domain socket.
249 if (domain_path
.size() > 107u)
252 std::unique_ptr
<DomainSocket
> socket_a_up
;
253 std::unique_ptr
<DomainSocket
> socket_b_up
;
254 CreateDomainConnectedSockets(domain_path
, &socket_a_up
, &socket_b_up
);
256 std::string
uri(socket_a_up
->GetRemoteConnectionURI());
257 EXPECT_EQ((URI
{"unix-connect", "", std::nullopt
, domain_path
}),
260 EXPECT_EQ(socket_b_up
->GetRemoteConnectionURI(), "");
264 INSTANTIATE_TEST_SUITE_P(
265 SocketTests
, SocketTest
,
266 testing::Values(SocketTestParams
{/*is_ipv6=*/false,
267 /*localhost_ip=*/"127.0.0.1"},
268 SocketTestParams
{/*is_ipv6=*/true, /*localhost_ip=*/"::1"}),
269 // Prints "SocketTests/SocketTest.DecodeHostAndPort/ipv4" etc. in test logs.
270 [](const testing::TestParamInfo
<SocketTestParams
> &info
) {
271 return info
.param
.is_ipv6
? "ipv6" : "ipv4";