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/Utility/UriParser.h"
13 #include "llvm/Testing/Support/Error.h"
14 #include "gtest/gtest.h"
16 using namespace lldb_private
;
18 struct SocketTestParams
{
20 std::string localhost_ip
;
23 class SocketTest
: public testing::TestWithParam
<SocketTestParams
> {
25 SubsystemRAII
<Socket
> subsystems
;
28 bool HostSupportsProtocol() const {
29 if (GetParam().is_ipv6
)
30 return HostSupportsIPv6();
31 return HostSupportsIPv4();
35 TEST_P(SocketTest
, DecodeHostAndPort
) {
36 EXPECT_THAT_EXPECTED(Socket::DecodeHostAndPort("localhost:1138"),
37 llvm::HasValue(Socket::HostAndPort
{"localhost", 1138}));
40 Socket::DecodeHostAndPort("google.com:65536"),
41 llvm::FailedWithMessage(
42 "invalid host:port specification: 'google.com:65536'"));
45 Socket::DecodeHostAndPort("google.com:-1138"),
46 llvm::FailedWithMessage(
47 "invalid host:port specification: 'google.com:-1138'"));
50 Socket::DecodeHostAndPort("google.com:65536"),
51 llvm::FailedWithMessage(
52 "invalid host:port specification: 'google.com:65536'"));
54 EXPECT_THAT_EXPECTED(Socket::DecodeHostAndPort("12345"),
55 llvm::HasValue(Socket::HostAndPort
{"", 12345}));
57 EXPECT_THAT_EXPECTED(Socket::DecodeHostAndPort("*:0"),
58 llvm::HasValue(Socket::HostAndPort
{"*", 0}));
60 EXPECT_THAT_EXPECTED(Socket::DecodeHostAndPort("*:65535"),
61 llvm::HasValue(Socket::HostAndPort
{"*", 65535}));
64 Socket::DecodeHostAndPort("[::1]:12345"),
65 llvm::HasValue(Socket::HostAndPort
{"::1", 12345}));
68 Socket::DecodeHostAndPort("[abcd:12fg:AF58::1]:12345"),
69 llvm::HasValue(Socket::HostAndPort
{"abcd:12fg:AF58::1", 12345}));
73 TEST_P(SocketTest
, DomainListenConnectAccept
) {
74 llvm::SmallString
<64> Path
;
75 std::error_code EC
= llvm::sys::fs::createUniqueDirectory("DomainListenConnectAccept", Path
);
77 llvm::sys::path::append(Path
, "test");
79 // Skip the test if the $TMPDIR is too long to hold a domain socket.
80 if (Path
.size() > 107u)
83 std::unique_ptr
<DomainSocket
> socket_a_up
;
84 std::unique_ptr
<DomainSocket
> socket_b_up
;
85 CreateDomainConnectedSockets(Path
, &socket_a_up
, &socket_b_up
);
89 TEST_P(SocketTest
, TCPListen0ConnectAccept
) {
90 if (!HostSupportsProtocol())
92 std::unique_ptr
<TCPSocket
> socket_a_up
;
93 std::unique_ptr
<TCPSocket
> socket_b_up
;
94 CreateTCPConnectedSockets(GetParam().localhost_ip
, &socket_a_up
,
98 TEST_P(SocketTest
, TCPGetAddress
) {
99 std::unique_ptr
<TCPSocket
> socket_a_up
;
100 std::unique_ptr
<TCPSocket
> socket_b_up
;
101 if (!HostSupportsProtocol())
103 CreateTCPConnectedSockets(GetParam().localhost_ip
, &socket_a_up
,
106 EXPECT_EQ(socket_a_up
->GetLocalPortNumber(),
107 socket_b_up
->GetRemotePortNumber());
108 EXPECT_EQ(socket_b_up
->GetLocalPortNumber(),
109 socket_a_up
->GetRemotePortNumber());
110 EXPECT_NE(socket_a_up
->GetLocalPortNumber(),
111 socket_b_up
->GetLocalPortNumber());
112 EXPECT_STREQ(GetParam().localhost_ip
.c_str(),
113 socket_a_up
->GetRemoteIPAddress().c_str());
114 EXPECT_STREQ(GetParam().localhost_ip
.c_str(),
115 socket_b_up
->GetRemoteIPAddress().c_str());
118 TEST_P(SocketTest
, UDPConnect
) {
119 // UDPSocket::Connect() creates sockets with AF_INET (IPv4).
120 if (!HostSupportsIPv4())
122 llvm::Expected
<std::unique_ptr
<UDPSocket
>> socket
=
123 UDPSocket::Connect("127.0.0.1:0", /*child_processes_inherit=*/false);
125 ASSERT_THAT_EXPECTED(socket
, llvm::Succeeded());
126 EXPECT_TRUE(socket
.get()->IsValid());
129 TEST_P(SocketTest
, TCPListen0GetPort
) {
130 if (!HostSupportsIPv4())
132 llvm::Expected
<std::unique_ptr
<TCPSocket
>> sock
=
133 Socket::TcpListen("10.10.12.3:0", false);
134 ASSERT_THAT_EXPECTED(sock
, llvm::Succeeded());
135 ASSERT_TRUE(sock
.get()->IsValid());
136 EXPECT_NE(sock
.get()->GetLocalPortNumber(), 0);
139 TEST_P(SocketTest
, TCPGetConnectURI
) {
140 std::unique_ptr
<TCPSocket
> socket_a_up
;
141 std::unique_ptr
<TCPSocket
> socket_b_up
;
142 if (!HostSupportsProtocol())
144 CreateTCPConnectedSockets(GetParam().localhost_ip
, &socket_a_up
,
147 std::string
uri(socket_a_up
->GetRemoteConnectionURI());
148 EXPECT_EQ((URI
{"connect", GetParam().localhost_ip
,
149 socket_a_up
->GetRemotePortNumber(), "/"}),
153 TEST_P(SocketTest
, UDPGetConnectURI
) {
154 // UDPSocket::Connect() creates sockets with AF_INET (IPv4).
155 if (!HostSupportsIPv4())
157 llvm::Expected
<std::unique_ptr
<UDPSocket
>> socket
=
158 UDPSocket::Connect("127.0.0.1:0", /*child_processes_inherit=*/false);
159 ASSERT_THAT_EXPECTED(socket
, llvm::Succeeded());
161 std::string uri
= socket
.get()->GetRemoteConnectionURI();
162 EXPECT_EQ((URI
{"udp", "127.0.0.1", 0, "/"}), URI::Parse(uri
));
165 #if LLDB_ENABLE_POSIX
166 TEST_P(SocketTest
, DomainGetConnectURI
) {
167 llvm::SmallString
<64> domain_path
;
169 llvm::sys::fs::createUniqueDirectory("DomainListenConnectAccept", domain_path
);
171 llvm::sys::path::append(domain_path
, "test");
173 // Skip the test if the $TMPDIR is too long to hold a domain socket.
174 if (domain_path
.size() > 107u)
177 std::unique_ptr
<DomainSocket
> socket_a_up
;
178 std::unique_ptr
<DomainSocket
> socket_b_up
;
179 CreateDomainConnectedSockets(domain_path
, &socket_a_up
, &socket_b_up
);
181 std::string
uri(socket_a_up
->GetRemoteConnectionURI());
182 EXPECT_EQ((URI
{"unix-connect", "", std::nullopt
, domain_path
}),
185 EXPECT_EQ(socket_b_up
->GetRemoteConnectionURI(), "");
189 INSTANTIATE_TEST_SUITE_P(
190 SocketTests
, SocketTest
,
191 testing::Values(SocketTestParams
{/*is_ipv6=*/false,
192 /*localhost_ip=*/"127.0.0.1"},
193 SocketTestParams
{/*is_ipv6=*/true, /*localhost_ip=*/"::1"}),
194 // Prints "SocketTests/SocketTest.DecodeHostAndPort/ipv4" etc. in test logs.
195 [](const testing::TestParamInfo
<SocketTestParams
> &info
) {
196 return info
.param
.is_ipv6
? "ipv6" : "ipv4";