[Lldb-commits] [lldb] [lldb] Fix the SocketTest failure on unsupported hosts (PR #118673)

John Harrison via lldb-commits lldb-commits at lists.llvm.org
Thu Dec 5 08:53:57 PST 2024


https://github.com/ashgti updated https://github.com/llvm/llvm-project/pull/118673

>From becab1c1b0a3a9237637909212f7ea7586b82321 Mon Sep 17 00:00:00 2001
From: John Harrison <harjohn at google.com>
Date: Wed, 4 Dec 2024 09:39:12 -0800
Subject: [PATCH 1/2] [lldb] Fix the
 SocketTest::TCPListen0MultiListenerGetListeningConnectionURI failure on
 unsupported hosts.

This failure https://lab.llvm.org/buildbot/#/builders/195/builds/1909 happened due to the host not having a `localhost` /etc/hosts entry for an ipv6 address.

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.
---
 lldb/unittests/Host/SocketTest.cpp             |  2 +-
 .../Host/SocketTestUtilities.cpp               | 18 ++++++++++++++++++
 .../TestingSupport/Host/SocketTestUtilities.h  |  5 +++++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/lldb/unittests/Host/SocketTest.cpp b/lldb/unittests/Host/SocketTest.cpp
index 689ef4019c618c..9cbf3e26b0883f 100644
--- a/lldb/unittests/Host/SocketTest.cpp
+++ b/lldb/unittests/Host/SocketTest.cpp
@@ -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 =
diff --git a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
index eb5bda0fabc770..86349351b0c3a4 100644
--- a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
+++ b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
@@ -116,6 +116,24 @@ bool lldb_private::HostSupportsIPv6() {
   return CheckIPSupport("IPv6", "[::1]:0");
 }
 
+bool lldb_private::HostSupportsLocalhostToIPv4() {
+  if (!HostSupportsIPv4())
+    return false;
+
+  auto addresses = SocketAddress::GetAddressInfo("localhost", nullptr, AF_INET,
+                                                 SOCK_STREAM, IPPROTO_TCP);
+  return !addresses.empty();
+}
+
+bool lldb_private::HostSupportsLocalhostToIPv6() {
+  if (!HostSupportsIPv6())
+    return false;
+
+  auto addresses = SocketAddress::GetAddressInfo("localhost", nullptr, AF_INET6,
+                                                 SOCK_STREAM, IPPROTO_TCP);
+  return !addresses.empty();
+}
+
 llvm::Expected<std::string> lldb_private::GetLocalhostIP() {
   if (HostSupportsIPv4())
     return "127.0.0.1";
diff --git a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
index efd17428ceefb6..e5bab163cf82eb 100644
--- a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
+++ b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
@@ -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

>From 47803e31fa247d3058aa2c64d785cd7b71b027a0 Mon Sep 17 00:00:00 2001
From: John Harrison <harjohn at google.com>
Date: Thu, 5 Dec 2024 08:53:38 -0800
Subject: [PATCH 2/2] Applying suggestions from reviewers.

---
 .../Host/SocketTestUtilities.cpp               | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
index 86349351b0c3a4..80545b8c533a03 100644
--- a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
+++ b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
@@ -120,18 +120,24 @@ bool lldb_private::HostSupportsLocalhostToIPv4() {
   if (!HostSupportsIPv4())
     return false;
 
-  auto addresses = SocketAddress::GetAddressInfo("localhost", nullptr, AF_INET,
-                                                 SOCK_STREAM, IPPROTO_TCP);
-  return !addresses.empty();
+  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();
 }
 
 bool lldb_private::HostSupportsLocalhostToIPv6() {
   if (!HostSupportsIPv6())
     return false;
 
-  auto addresses = SocketAddress::GetAddressInfo("localhost", nullptr, AF_INET6,
-                                                 SOCK_STREAM, IPPROTO_TCP);
-  return !addresses.empty();
+  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() {



More information about the lldb-commits mailing list