Skip to content

Commit 0964328

Browse files
authored
[lldb] Fix the SocketTest failure on unsupported hosts (#118673)
The test `SocketTest::TCPListen0MultiListenerGetListeningConnectionURI` is failing on hosts that do not map `localhost` to both an ipv4 and ipv6 address. For example this build https://lab.llvm.org/buildbot/#/builders/195/builds/1909. To fix this, I added a helper to validate if the host has an /etc/hosts entry for both ipv4 and ipv6, otherwise we skip the test.
1 parent 245f26a commit 0964328

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

lldb/unittests/Host/SocketTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ TEST_P(SocketTest, TCPListen0GetListeningConnectionURI) {
271271
}
272272

273273
TEST_F(SocketTest, TCPListen0MultiListenerGetListeningConnectionURI) {
274-
if (!HostSupportsIPv6() || !HostSupportsIPv4())
274+
if (!HostSupportsLocalhostToIPv4() || !HostSupportsLocalhostToIPv6())
275275
return;
276276

277277
llvm::Expected<std::unique_ptr<TCPSocket>> sock =

lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,30 @@ bool lldb_private::HostSupportsIPv6() {
116116
return CheckIPSupport("IPv6", "[::1]:0");
117117
}
118118

119+
bool lldb_private::HostSupportsLocalhostToIPv4() {
120+
if (!HostSupportsIPv4())
121+
return false;
122+
123+
auto addresses = SocketAddress::GetAddressInfo(
124+
"localhost", nullptr, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
125+
return std::find_if(addresses.begin(), addresses.end(),
126+
[](SocketAddress &addr) {
127+
return addr.GetFamily() == AF_INET;
128+
}) != addresses.end();
129+
}
130+
131+
bool lldb_private::HostSupportsLocalhostToIPv6() {
132+
if (!HostSupportsIPv6())
133+
return false;
134+
135+
auto addresses = SocketAddress::GetAddressInfo(
136+
"localhost", nullptr, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
137+
return std::find_if(addresses.begin(), addresses.end(),
138+
[](SocketAddress &addr) {
139+
return addr.GetFamily() == AF_INET6;
140+
}) != addresses.end();
141+
}
142+
119143
llvm::Expected<std::string> lldb_private::GetLocalhostIP() {
120144
if (HostSupportsIPv4())
121145
return "127.0.0.1";

lldb/unittests/TestingSupport/Host/SocketTestUtilities.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ void CreateDomainConnectedSockets(llvm::StringRef path,
4343
bool HostSupportsIPv6();
4444
bool HostSupportsIPv4();
4545

46+
/// Returns true if the name `localhost` maps to a loopback IPv4 address.
47+
bool HostSupportsLocalhostToIPv4();
48+
/// Returns true if the name `localhost` maps to a loopback IPv6 address.
49+
bool HostSupportsLocalhostToIPv6();
50+
4651
/// Return an IP for localhost based on host support.
4752
///
4853
/// This will return either "127.0.0.1" if IPv4 is detected, or "[::1]" if IPv6

0 commit comments

Comments
 (0)