[Lldb-commits] [lldb] 1b988ff - [test] Use either `127.0.0.1` or `[::1]` to run in ipv6-only environments.

Jordan Rupprecht via lldb-commits lldb-commits at lists.llvm.org
Fri Sep 9 14:09:18 PDT 2022


Author: Jordan Rupprecht
Date: 2022-09-09T14:00:35-07:00
New Revision: 1b988ff092a0a713f0bb4712bfe6cb5ba85b725c

URL: https://github.com/llvm/llvm-project/commit/1b988ff092a0a713f0bb4712bfe6cb5ba85b725c
DIFF: https://github.com/llvm/llvm-project/commit/1b988ff092a0a713f0bb4712bfe6cb5ba85b725c.diff

LOG: [test] Use either `127.0.0.1` or `[::1]` to run in ipv6-only environments.

Test for both IPv4 and IPv6 support to determine if either `127.0.0.1` or `[::1]` are appropriate IP addresses to attempt to connect to. In an IPv6-only environment, `127.0.0.1` is not available.

Using `localhost` is problematic because we might not be able to get the same port on each IP flavor, and later try to connect to the wrong thing.

Reviewed By: labath

Differential Revision: https://reviews.llvm.org/D133393

Added: 
    

Modified: 
    lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
    lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
    lldb/unittests/tools/lldb-server/tests/CMakeLists.txt
    lldb/unittests/tools/lldb-server/tests/TestClient.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
index 4ab65aa6208c8..2455a4f6f5d49 100644
--- a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
+++ b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
@@ -126,3 +126,13 @@ bool lldb_private::HostSupportsIPv4() {
 bool lldb_private::HostSupportsIPv6() {
   return CheckIPSupport("IPv6", "[::1]:0");
 }
+
+llvm::Expected<std::string> lldb_private::GetLocalhostIP() {
+  if (HostSupportsIPv4())
+    return "127.0.0.1";
+  if (HostSupportsIPv6())
+    return "[::1]";
+  return llvm::make_error<llvm::StringError>(
+      "Neither IPv4 nor IPv6 appear to be supported",
+      llvm::inconvertibleErrorCode());
+}

diff  --git a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
index 2130cc33dd5b0..efd17428ceefb 100644
--- a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
+++ b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
@@ -42,6 +42,13 @@ void CreateDomainConnectedSockets(llvm::StringRef path,
 
 bool HostSupportsIPv6();
 bool HostSupportsIPv4();
+
+/// 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
+/// is detected. If neither are detected, return an error.
+llvm::Expected<std::string> GetLocalhostIP();
+
 } // namespace lldb_private
 
 #endif

diff  --git a/lldb/unittests/tools/lldb-server/tests/CMakeLists.txt b/lldb/unittests/tools/lldb-server/tests/CMakeLists.txt
index cb9e138fbe61b..1d5f0c789a364 100644
--- a/lldb/unittests/tools/lldb-server/tests/CMakeLists.txt
+++ b/lldb/unittests/tools/lldb-server/tests/CMakeLists.txt
@@ -7,6 +7,7 @@ add_lldb_unittest(LLDBServerTests
   LINK_LIBS
     lldbHost
     lldbCore
+    lldbHostHelpers
     lldbInterpreter
     lldbTarget
     lldbPluginPlatformLinux

diff  --git a/lldb/unittests/tools/lldb-server/tests/TestClient.cpp b/lldb/unittests/tools/lldb-server/tests/TestClient.cpp
index 46ca10a03f69a..f762fbc1c9c24 100644
--- a/lldb/unittests/tools/lldb-server/tests/TestClient.cpp
+++ b/lldb/unittests/tools/lldb-server/tests/TestClient.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "TestClient.h"
+#include "TestingSupport/Host/SocketTestUtilities.h"
 #include "lldb/Host/HostInfo.h"
 #include "lldb/Host/common/TCPSocket.h"
 #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
@@ -77,14 +78,20 @@ Expected<std::unique_ptr<TestClient>> TestClient::launchCustom(StringRef Log, Ar
       args.AppendArgument("--log-flags=0x800000");
   }
 
+  auto LocalhostIPOrErr = GetLocalhostIP();
+  if (!LocalhostIPOrErr)
+    return LocalhostIPOrErr.takeError();
+  const std::string &LocalhostIP = *LocalhostIPOrErr;
+
   Status status;
   TCPSocket listen_socket(true, false);
-  status = listen_socket.Listen("127.0.0.1:0", 5);
+  status = listen_socket.Listen(LocalhostIP + ":0", 5);
   if (status.Fail())
     return status.ToError();
 
   args.AppendArgument(
-      ("127.0.0.1:" + Twine(listen_socket.GetLocalPortNumber())).str());
+      formatv("{0}:{1}", LocalhostIP, listen_socket.GetLocalPortNumber())
+          .str());
 
   for (StringRef arg : ServerArgs)
     args.AppendArgument(arg);


        


More information about the lldb-commits mailing list