[Lldb-commits] [lldb] [lldb-dap] Adding unit tests for Transport. (PR #139926)
John Harrison via lldb-commits
lldb-commits at lists.llvm.org
Wed May 14 09:32:37 PDT 2025
https://github.com/ashgti updated https://github.com/llvm/llvm-project/pull/139926
>From 506e8107a397e2ae88d8b952c0a5351cda9fa161 Mon Sep 17 00:00:00 2001
From: John Harrison <harjohn at google.com>
Date: Wed, 14 May 2025 09:13:32 -0700
Subject: [PATCH 1/3] [lldb-dap] Adding unittests for Transport.
This adds basic support for testing the Transport class and includes tests for 'Read'.
---
lldb/unittests/DAP/CMakeLists.txt | 1 +
lldb/unittests/DAP/TransportTest.cpp | 81 ++++++++++++++++++++++++++++
2 files changed, 82 insertions(+)
create mode 100644 lldb/unittests/DAP/TransportTest.cpp
diff --git a/lldb/unittests/DAP/CMakeLists.txt b/lldb/unittests/DAP/CMakeLists.txt
index 8b240654046e2..110733e93b192 100644
--- a/lldb/unittests/DAP/CMakeLists.txt
+++ b/lldb/unittests/DAP/CMakeLists.txt
@@ -1,6 +1,7 @@
add_lldb_unittest(DAPTests
JSONUtilsTest.cpp
LLDBUtilsTest.cpp
+ TransportTest.cpp
ProtocolTypesTest.cpp
LINK_LIBS
diff --git a/lldb/unittests/DAP/TransportTest.cpp b/lldb/unittests/DAP/TransportTest.cpp
new file mode 100644
index 0000000000000..d27167cf9da61
--- /dev/null
+++ b/lldb/unittests/DAP/TransportTest.cpp
@@ -0,0 +1,81 @@
+//===-- LLDBUtilsTest.cpp -------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "Transport.h"
+#include "Protocol/ProtocolBase.h"
+#include "lldb/Host/File.h"
+#include "lldb/Host/Pipe.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/FormatVariadic.h"
+#include "llvm/Testing/Support/Error.h"
+#include "gtest/gtest.h"
+#include <chrono>
+#include <memory>
+#include <optional>
+
+using namespace llvm;
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_dap;
+using namespace lldb_dap::protocol;
+
+class TransportTest : public testing::Test {
+protected:
+ Pipe input;
+ Pipe output;
+ std::unique_ptr<Transport> transport;
+
+ void SetUp() override {
+ ASSERT_THAT_ERROR(input.CreateNew(false).ToError(), llvm::Succeeded());
+ ASSERT_THAT_ERROR(output.CreateNew(false).ToError(), llvm::Succeeded());
+ transport = std::make_unique<Transport>(
+ "stdio", nullptr,
+ std::make_shared<NativeFile>(input.GetReadFileDescriptor(),
+ File::eOpenOptionReadOnly,
+ NativeFile::Unowned),
+ std::make_shared<NativeFile>(output.GetWriteFileDescriptor(),
+ File::eOpenOptionWriteOnly,
+ NativeFile::Unowned));
+ }
+
+ void Write(StringRef json) {
+ std::string message =
+ formatv("Content-Length: {0}\r\n\r\n{1}", json.size(), json).str();
+ ASSERT_THAT_EXPECTED(input.Write(message.data(), message.size()),
+ Succeeded());
+ }
+};
+
+TEST_F(TransportTest, MalformedRequests) {
+ std::string malformed_header = "COnTent-LenGth: -1{}\r\n\r\nnotjosn";
+ ASSERT_THAT_EXPECTED(
+ input.Write(malformed_header.data(), malformed_header.size()),
+ Succeeded());
+ ASSERT_THAT_EXPECTED(
+ transport->Read(std::chrono::milliseconds(1)),
+ FailedWithMessage(
+ "expected 'Content-Length: ' and got 'COnTent-LenGth: '"));
+}
+
+TEST_F(TransportTest, Read) {
+ Write(R"json({"seq": 1, "type": "request", "command": "abc"})json");
+ ASSERT_THAT_EXPECTED(transport->Read(std::chrono::milliseconds(1)),
+ HasValue(testing::VariantWith<Request>(
+ testing::FieldsAre(1, "abc", std::nullopt))));
+}
+
+TEST_F(TransportTest, ReadWithTimeout) {
+ ASSERT_THAT_EXPECTED(transport->Read(std::chrono::milliseconds(1)),
+ Failed<TimeoutError>());
+}
+
+TEST_F(TransportTest, ReadWithEOF) {
+ input.CloseWriteFileDescriptor();
+ ASSERT_THAT_EXPECTED(transport->Read(std::chrono::milliseconds(1)),
+ Failed<EndOfFileError>());
+}
>From 8d5ffc83c6fce70a3fe66fb726781e7bd2b08d77 Mon Sep 17 00:00:00 2001
From: John Harrison <harjohn at google.com>
Date: Wed, 14 May 2025 09:16:34 -0700
Subject: [PATCH 2/3] Fixing the filename in the top comment.
---
lldb/unittests/DAP/TransportTest.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lldb/unittests/DAP/TransportTest.cpp b/lldb/unittests/DAP/TransportTest.cpp
index d27167cf9da61..b8eade5442814 100644
--- a/lldb/unittests/DAP/TransportTest.cpp
+++ b/lldb/unittests/DAP/TransportTest.cpp
@@ -1,4 +1,4 @@
-//===-- LLDBUtilsTest.cpp -------------------------------------------------===//
+//===-- TransportTest.cpp -------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
>From 8de3cd6569f6a17de1f0607666953f06b1593e5b Mon Sep 17 00:00:00 2001
From: John Harrison <harjohn at google.com>
Date: Wed, 14 May 2025 09:32:23 -0700
Subject: [PATCH 3/3] Also add a Write test.
---
lldb/unittests/DAP/TransportTest.cpp | 29 ++++++++++++++++++++++------
1 file changed, 23 insertions(+), 6 deletions(-)
diff --git a/lldb/unittests/DAP/TransportTest.cpp b/lldb/unittests/DAP/TransportTest.cpp
index b8eade5442814..5c77b4bb26343 100644
--- a/lldb/unittests/DAP/TransportTest.cpp
+++ b/lldb/unittests/DAP/TransportTest.cpp
@@ -20,9 +20,11 @@
using namespace llvm;
using namespace lldb;
-using namespace lldb_private;
using namespace lldb_dap;
using namespace lldb_dap::protocol;
+using lldb_private::File;
+using lldb_private::NativeFile;
+using lldb_private::Pipe;
class TransportTest : public testing::Test {
protected:
@@ -31,8 +33,8 @@ class TransportTest : public testing::Test {
std::unique_ptr<Transport> transport;
void SetUp() override {
- ASSERT_THAT_ERROR(input.CreateNew(false).ToError(), llvm::Succeeded());
- ASSERT_THAT_ERROR(output.CreateNew(false).ToError(), llvm::Succeeded());
+ ASSERT_THAT_ERROR(input.CreateNew(false).ToError(), Succeeded());
+ ASSERT_THAT_ERROR(output.CreateNew(false).ToError(), Succeeded());
transport = std::make_unique<Transport>(
"stdio", nullptr,
std::make_shared<NativeFile>(input.GetReadFileDescriptor(),
@@ -64,9 +66,10 @@ TEST_F(TransportTest, MalformedRequests) {
TEST_F(TransportTest, Read) {
Write(R"json({"seq": 1, "type": "request", "command": "abc"})json");
- ASSERT_THAT_EXPECTED(transport->Read(std::chrono::milliseconds(1)),
- HasValue(testing::VariantWith<Request>(
- testing::FieldsAre(1, "abc", std::nullopt))));
+ ASSERT_THAT_EXPECTED(
+ transport->Read(std::chrono::milliseconds(1)),
+ HasValue(testing::VariantWith<Request>(testing::FieldsAre(
+ /*seq=*/1, /*command=*/"abc", /*arguments=*/std::nullopt))));
}
TEST_F(TransportTest, ReadWithTimeout) {
@@ -79,3 +82,17 @@ TEST_F(TransportTest, ReadWithEOF) {
ASSERT_THAT_EXPECTED(transport->Read(std::chrono::milliseconds(1)),
Failed<EndOfFileError>());
}
+
+TEST_F(TransportTest, Write) {
+ ASSERT_THAT_ERROR(transport->Write(Event{"my-event", std::nullopt}),
+ Succeeded());
+ output.CloseWriteFileDescriptor();
+ char buf[1024];
+ Expected<size_t> bytes_read =
+ output.Read(buf, sizeof(buf), std::chrono::milliseconds(1));
+ ASSERT_THAT_EXPECTED(bytes_read, Succeeded());
+ ASSERT_EQ(
+ StringRef(buf, *bytes_read),
+ StringRef("Content-Length: 43\r\n\r\n"
+ R"json({"event":"my-event","seq":0,"type":"event"})json"));
+}
More information about the lldb-commits
mailing list