From 59656c0492224a2da590b913959630107e0a31f4 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Wed, 24 Aug 2022 15:59:46 +0200 Subject: [PATCH] [lldb] Make CommunicationTest compatible with windows Our (TCP) socket support is in a much better state than pipes. Use that for testing the Communication class. Move the CreateTCPConnectedSockets function (SocketTestUtilities.{h,cpp}) to a place where it can be used from Communication tests. --- lldb/unittests/Core/CMakeLists.txt | 1 + lldb/unittests/Core/CommunicationTest.cpp | 51 +++++++++++----------- lldb/unittests/Core/FormatEntityTest.cpp | 14 +++--- lldb/unittests/Host/CMakeLists.txt | 2 +- .../Host/ConnectionFileDescriptorTest.cpp | 3 +- lldb/unittests/Host/SocketTest.cpp | 2 +- lldb/unittests/TestingSupport/CMakeLists.txt | 1 + lldb/unittests/TestingSupport/Host/CMakeLists.txt | 9 ++++ .../Host/SocketTestUtilities.cpp | 2 +- .../Host/SocketTestUtilities.h | 4 +- 10 files changed, 49 insertions(+), 40 deletions(-) create mode 100644 lldb/unittests/TestingSupport/Host/CMakeLists.txt rename lldb/unittests/{ => TestingSupport}/Host/SocketTestUtilities.cpp (98%) rename lldb/unittests/{ => TestingSupport}/Host/SocketTestUtilities.h (92%) diff --git a/lldb/unittests/Core/CMakeLists.txt b/lldb/unittests/Core/CMakeLists.txt index ea3f652..bda0ac7 100644 --- a/lldb/unittests/Core/CMakeLists.txt +++ b/lldb/unittests/Core/CMakeLists.txt @@ -21,6 +21,7 @@ add_lldb_unittest(LLDBCoreTests lldbPluginSymbolFileSymtab lldbSymbol lldbUtilityHelpers + lldbHostHelpers LLVMTestingSupport LINK_COMPONENTS Support diff --git a/lldb/unittests/Core/CommunicationTest.cpp b/lldb/unittests/Core/CommunicationTest.cpp index 13ebfae..5cb1d4a 100644 --- a/lldb/unittests/Core/CommunicationTest.cpp +++ b/lldb/unittests/Core/CommunicationTest.cpp @@ -12,6 +12,8 @@ #include "lldb/Host/Pipe.h" #include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" +#include "TestingSupport/Host/SocketTestUtilities.h" +#include "TestingSupport/SubsystemRAII.h" #include @@ -21,30 +23,31 @@ using namespace lldb_private; -#ifndef _WIN32 +class CommunicationTest : public testing::Test { +private: + SubsystemRAII m_subsystems; +}; + static void CommunicationReadTest(bool use_read_thread) { - Pipe pipe; - ASSERT_THAT_ERROR(pipe.CreateNew(/*child_process_inherit=*/false).ToError(), - llvm::Succeeded()); + std::unique_ptr a, b; + ASSERT_TRUE(CreateTCPConnectedSockets("localhost", &a, &b)); - Status error; - size_t bytes_written; - ASSERT_THAT_ERROR(pipe.Write("test", 4, bytes_written).ToError(), - llvm::Succeeded()); - ASSERT_EQ(bytes_written, 4U); + size_t num_bytes = 4; + ASSERT_THAT_ERROR(a->Write("test", num_bytes).ToError(), llvm::Succeeded()); + ASSERT_EQ(num_bytes, 4U); Communication comm("test"); - comm.SetConnection(std::make_unique( - pipe.ReleaseReadFileDescriptor(), /*owns_fd=*/true)); + comm.SetConnection(std::make_unique(b.release())); comm.SetCloseOnEOF(true); - if (use_read_thread) + if (use_read_thread) { ASSERT_TRUE(comm.StartReadThread()); + } // This read should wait for the data to become available and return it. lldb::ConnectionStatus status = lldb::eConnectionStatusSuccess; char buf[16]; - error.Clear(); + Status error; EXPECT_EQ( comm.Read(buf, sizeof(buf), std::chrono::seconds(5), status, &error), 4U); EXPECT_EQ(status, lldb::eConnectionStatusSuccess); @@ -68,7 +71,7 @@ static void CommunicationReadTest(bool use_read_thread) { EXPECT_THAT_ERROR(error.ToError(), llvm::Failed()); // This read should return EOF. - pipe.CloseWriteFileDescriptor(); + ASSERT_THAT_ERROR(a->Close().ToError(), llvm::Succeeded()); error.Clear(); EXPECT_EQ( comm.Read(buf, sizeof(buf), std::chrono::seconds(5), status, &error), 0U); @@ -80,37 +83,33 @@ static void CommunicationReadTest(bool use_read_thread) { EXPECT_TRUE(comm.JoinReadThread()); } -TEST(CommunicationTest, Read) { +TEST_F(CommunicationTest, Read) { CommunicationReadTest(/*use_thread=*/false); } -TEST(CommunicationTest, ReadThread) { +TEST_F(CommunicationTest, ReadThread) { CommunicationReadTest(/*use_thread=*/true); } -TEST(CommunicationTest, SynchronizeWhileClosing) { - // Set up a communication object reading from a pipe. - Pipe pipe; - ASSERT_THAT_ERROR(pipe.CreateNew(/*child_process_inherit=*/false).ToError(), - llvm::Succeeded()); +TEST_F(CommunicationTest, SynchronizeWhileClosing) { + std::unique_ptr a, b; + ASSERT_TRUE(CreateTCPConnectedSockets("localhost", &a, &b)); Communication comm("test"); - comm.SetConnection(std::make_unique( - pipe.ReleaseReadFileDescriptor(), /*owns_fd=*/true)); + comm.SetConnection(std::make_unique(b.release())); comm.SetCloseOnEOF(true); ASSERT_TRUE(comm.StartReadThread()); // Ensure that we can safely synchronize with the read thread while it is // closing the read end (in response to us closing the write end). - pipe.CloseWriteFileDescriptor(); + ASSERT_THAT_ERROR(a->Close().ToError(), llvm::Succeeded()); comm.SynchronizeWithReadThread(); ASSERT_TRUE(comm.StopReadThread()); } -#endif #if LLDB_ENABLE_POSIX -TEST(CommunicationTest, WriteAll) { +TEST_F(CommunicationTest, WriteAll) { Pipe pipe; ASSERT_THAT_ERROR(pipe.CreateNew(/*child_process_inherit=*/false).ToError(), llvm::Succeeded()); diff --git a/lldb/unittests/Core/FormatEntityTest.cpp b/lldb/unittests/Core/FormatEntityTest.cpp index c22dd43..0a68c93 100644 --- a/lldb/unittests/Core/FormatEntityTest.cpp +++ b/lldb/unittests/Core/FormatEntityTest.cpp @@ -20,7 +20,7 @@ using Entry = FormatEntity::Entry; TEST(FormatEntityTest, DefinitionConstructionNameAndType) { Definition d("foo", FormatEntity::Entry::Type::Invalid); - EXPECT_EQ(d.name, "foo"); + EXPECT_STREQ(d.name, "foo"); EXPECT_EQ(d.string, nullptr); EXPECT_EQ(d.type, FormatEntity::Entry::Type::Invalid); EXPECT_EQ(d.data, 0UL); @@ -32,8 +32,8 @@ TEST(FormatEntityTest, DefinitionConstructionNameAndType) { TEST(FormatEntityTest, DefinitionConstructionNameAndString) { Definition d("foo", "string"); - EXPECT_EQ(d.name, "foo"); - EXPECT_EQ(d.string, "string"); + EXPECT_STREQ(d.name, "foo"); + EXPECT_STREQ(d.string, "string"); EXPECT_EQ(d.type, FormatEntity::Entry::Type::EscapeCode); EXPECT_EQ(d.data, 0UL); EXPECT_EQ(d.num_children, 0UL); @@ -44,7 +44,7 @@ TEST(FormatEntityTest, DefinitionConstructionNameAndString) { TEST(FormatEntityTest, DefinitionConstructionNameTypeData) { Definition d("foo", FormatEntity::Entry::Type::Invalid, 33); - EXPECT_EQ(d.name, "foo"); + EXPECT_STREQ(d.name, "foo"); EXPECT_EQ(d.string, nullptr); EXPECT_EQ(d.type, FormatEntity::Entry::Type::Invalid); EXPECT_EQ(d.data, 33UL); @@ -56,14 +56,14 @@ TEST(FormatEntityTest, DefinitionConstructionNameTypeData) { TEST(FormatEntityTest, DefinitionConstructionNameTypeChildren) { Definition d("foo", FormatEntity::Entry::Type::Invalid, 33); Definition parent("parent", FormatEntity::Entry::Type::Invalid, 1, &d); - EXPECT_EQ(parent.name, "parent"); - EXPECT_EQ(parent.string, nullptr); + EXPECT_STREQ(parent.name, "parent"); + EXPECT_STREQ(parent.string, nullptr); EXPECT_EQ(parent.type, FormatEntity::Entry::Type::Invalid); EXPECT_EQ(parent.num_children, 1UL); EXPECT_EQ(parent.children, &d); EXPECT_FALSE(parent.keep_separator); - EXPECT_EQ(parent.children[0].name, "foo"); + EXPECT_STREQ(parent.children[0].name, "foo"); EXPECT_EQ(parent.children[0].string, nullptr); EXPECT_EQ(parent.children[0].type, FormatEntity::Entry::Type::Invalid); EXPECT_EQ(parent.children[0].data, 33UL); diff --git a/lldb/unittests/Host/CMakeLists.txt b/lldb/unittests/Host/CMakeLists.txt index bf14bf1..23a6aae 100644 --- a/lldb/unittests/Host/CMakeLists.txt +++ b/lldb/unittests/Host/CMakeLists.txt @@ -11,7 +11,6 @@ set (FILES ProcessLaunchInfoTest.cpp SocketAddressTest.cpp SocketTest.cpp - SocketTestUtilities.cpp ThreadLauncherTest.cpp XMLTest.cpp ) @@ -34,5 +33,6 @@ add_lldb_unittest(HostTests LINK_LIBS lldbHost lldbUtilityHelpers + lldbHostHelpers LLVMTestingSupport ) diff --git a/lldb/unittests/Host/ConnectionFileDescriptorTest.cpp b/lldb/unittests/Host/ConnectionFileDescriptorTest.cpp index 0c99d14..82cd49f 100644 --- a/lldb/unittests/Host/ConnectionFileDescriptorTest.cpp +++ b/lldb/unittests/Host/ConnectionFileDescriptorTest.cpp @@ -6,9 +6,8 @@ // //===----------------------------------------------------------------------===// -#include "SocketTestUtilities.h" +#include "TestingSupport/Host/SocketTestUtilities.h" #include "gtest/gtest.h" - #include "TestingSupport/SubsystemRAII.h" #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h" #include "lldb/Utility/UriParser.h" diff --git a/lldb/unittests/Host/SocketTest.cpp b/lldb/unittests/Host/SocketTest.cpp index 6af0b3c..a209600 100644 --- a/lldb/unittests/Host/SocketTest.cpp +++ b/lldb/unittests/Host/SocketTest.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "SocketTestUtilities.h" +#include "TestingSupport/Host/SocketTestUtilities.h" #include "TestingSupport/SubsystemRAII.h" #include "lldb/Host/Config.h" #include "lldb/Utility/UriParser.h" diff --git a/lldb/unittests/TestingSupport/CMakeLists.txt b/lldb/unittests/TestingSupport/CMakeLists.txt index 4dc77e4..df0a370 100644 --- a/lldb/unittests/TestingSupport/CMakeLists.txt +++ b/lldb/unittests/TestingSupport/CMakeLists.txt @@ -12,4 +12,5 @@ add_lldb_library(lldbUtilityHelpers ObjectYAML ) +add_subdirectory(Host) add_subdirectory(Symbol) diff --git a/lldb/unittests/TestingSupport/Host/CMakeLists.txt b/lldb/unittests/TestingSupport/Host/CMakeLists.txt new file mode 100644 index 0000000..e81d6a9 --- /dev/null +++ b/lldb/unittests/TestingSupport/Host/CMakeLists.txt @@ -0,0 +1,9 @@ +set_property(DIRECTORY PROPERTY EXCLUDE_FROM_ALL ON) +add_lldb_library(lldbHostHelpers + SocketTestUtilities.cpp + + LINK_LIBS + lldbHost + LLVMTestingSupport + llvm_gtest + ) diff --git a/lldb/unittests/Host/SocketTestUtilities.cpp b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp similarity index 98% rename from lldb/unittests/Host/SocketTestUtilities.cpp rename to lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp index a37a2cd..4ab65aa 100644 --- a/lldb/unittests/Host/SocketTestUtilities.cpp +++ b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "SocketTestUtilities.h" +#include "TestingSupport/Host/SocketTestUtilities.h" #include "lldb/Host/Config.h" #include "lldb/Utility/StreamString.h" diff --git a/lldb/unittests/Host/SocketTestUtilities.h b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h similarity index 92% rename from lldb/unittests/Host/SocketTestUtilities.h rename to lldb/unittests/TestingSupport/Host/SocketTestUtilities.h index 943d98a..2130cc3 100644 --- a/lldb/unittests/Host/SocketTestUtilities.h +++ b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLDB_UNITTESTS_HOST_SOCKETTESTUTILITIES_H -#define LLDB_UNITTESTS_HOST_SOCKETTESTUTILITIES_H +#ifndef LLDB_UNITTESTS_TESTINGSUPPORT_HOST_SOCKETTESTUTILITIES_H +#define LLDB_UNITTESTS_TESTINGSUPPORT_HOST_SOCKETTESTUTILITIES_H #include #include -- 2.7.4