[Lldb-commits] [PATCH] D133393: [test] Use localhost in place of 127.0.0.1 to run in ipv6-only environments.

Jordan Rupprecht via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Wed Sep 7 16:56:52 PDT 2022


rupprecht updated this revision to Diff 458609.
rupprecht added a comment.

- Add a GetLocalhostIp helper to socket host utilities


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133393/new/

https://reviews.llvm.org/D133393

Files:
  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


Index: lldb/unittests/tools/lldb-server/tests/TestClient.cpp
===================================================================
--- lldb/unittests/tools/lldb-server/tests/TestClient.cpp
+++ 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 @@
       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);
Index: lldb/unittests/tools/lldb-server/tests/CMakeLists.txt
===================================================================
--- lldb/unittests/tools/lldb-server/tests/CMakeLists.txt
+++ lldb/unittests/tools/lldb-server/tests/CMakeLists.txt
@@ -7,6 +7,7 @@
   LINK_LIBS
     lldbHost
     lldbCore
+    lldbHostHelpers
     lldbInterpreter
     lldbTarget
     lldbPluginPlatformLinux
Index: lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
===================================================================
--- lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
+++ lldb/unittests/TestingSupport/Host/SocketTestUtilities.h
@@ -42,6 +42,13 @@
 
 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
Index: lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
===================================================================
--- lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
+++ lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp
@@ -126,3 +126,13 @@
 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());
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133393.458609.patch
Type: text/x-patch
Size: 2977 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20220907/19a9aa0e/attachment.bin>


More information about the lldb-commits mailing list