Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lldb/unittests/Host/SocketTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ TEST_P(SocketTest, TCPListen0GetListeningConnectionURI) {
}

TEST_F(SocketTest, TCPListen0MultiListenerGetListeningConnectionURI) {
if (!HostSupportsIPv6() || !HostSupportsIPv4())
if (!HostSupportsLocalhostToIPv4() || !HostSupportsLocalhostToIPv6())
return;

llvm::Expected<std::unique_ptr<TCPSocket>> sock =
Expand Down
24 changes: 24 additions & 0 deletions lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,30 @@ bool lldb_private::HostSupportsIPv6() {
return CheckIPSupport("IPv6", "[::1]:0");
}

bool lldb_private::HostSupportsLocalhostToIPv4() {
if (!HostSupportsIPv4())
return false;

auto addresses = SocketAddress::GetAddressInfo(
"localhost", nullptr, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
return std::find_if(addresses.begin(), addresses.end(),
[](SocketAddress &addr) {
return addr.GetFamily() == AF_INET;
}) != addresses.end();
Comment on lines +125 to +128
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small llvm-ization:

return llvm::any_of(addresses, [](const SocketAddress &addr) { return addr.GetFamily() == AF_INET; });

}

bool lldb_private::HostSupportsLocalhostToIPv6() {
if (!HostSupportsIPv6())
return false;

auto addresses = SocketAddress::GetAddressInfo(
"localhost", nullptr, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
return std::find_if(addresses.begin(), addresses.end(),
[](SocketAddress &addr) {
return addr.GetFamily() == AF_INET6;
}) != addresses.end();
}

llvm::Expected<std::string> lldb_private::GetLocalhostIP() {
if (HostSupportsIPv4())
return "127.0.0.1";
Expand Down
5 changes: 5 additions & 0 deletions lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ void CreateDomainConnectedSockets(llvm::StringRef path,
bool HostSupportsIPv6();
bool HostSupportsIPv4();

/// Returns true if the name `localhost` maps to a loopback IPv4 address.
bool HostSupportsLocalhostToIPv4();
/// Returns true if the name `localhost` maps to a loopback IPv6 address.
bool HostSupportsLocalhostToIPv6();

/// Return an IP for localhost based on host support.
///
/// This will return either "127.0.0.1" if IPv4 is detected, or "[::1]" if IPv6
Expand Down
Loading