|
| 1 | +//===-- LLDBUtilsTest.cpp -------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "Transport.h" |
| 10 | +#include "Protocol/ProtocolBase.h" |
| 11 | +#include "lldb/Host/File.h" |
| 12 | +#include "lldb/Host/Pipe.h" |
| 13 | +#include "llvm/ADT/StringRef.h" |
| 14 | +#include "llvm/Support/FormatVariadic.h" |
| 15 | +#include "llvm/Testing/Support/Error.h" |
| 16 | +#include "gtest/gtest.h" |
| 17 | +#include <chrono> |
| 18 | +#include <memory> |
| 19 | +#include <optional> |
| 20 | + |
| 21 | +using namespace llvm; |
| 22 | +using namespace lldb; |
| 23 | +using namespace lldb_private; |
| 24 | +using namespace lldb_dap; |
| 25 | +using namespace lldb_dap::protocol; |
| 26 | + |
| 27 | +class TransportTest : public testing::Test { |
| 28 | +protected: |
| 29 | + Pipe input; |
| 30 | + Pipe output; |
| 31 | + std::unique_ptr<Transport> transport; |
| 32 | + |
| 33 | + void SetUp() override { |
| 34 | + ASSERT_THAT_ERROR(input.CreateNew(false).ToError(), llvm::Succeeded()); |
| 35 | + ASSERT_THAT_ERROR(output.CreateNew(false).ToError(), llvm::Succeeded()); |
| 36 | + transport = std::make_unique<Transport>( |
| 37 | + "stdio", nullptr, |
| 38 | + std::make_shared<NativeFile>(input.GetReadFileDescriptor(), |
| 39 | + File::eOpenOptionReadOnly, |
| 40 | + NativeFile::Unowned), |
| 41 | + std::make_shared<NativeFile>(output.GetWriteFileDescriptor(), |
| 42 | + File::eOpenOptionWriteOnly, |
| 43 | + NativeFile::Unowned)); |
| 44 | + } |
| 45 | + |
| 46 | + void Write(StringRef json) { |
| 47 | + std::string message = |
| 48 | + formatv("Content-Length: {0}\r\n\r\n{1}", json.size(), json).str(); |
| 49 | + ASSERT_THAT_EXPECTED(input.Write(message.data(), message.size()), |
| 50 | + Succeeded()); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +TEST_F(TransportTest, MalformedRequests) { |
| 55 | + std::string malformed_header = "COnTent-LenGth: -1{}\r\n\r\nnotjosn"; |
| 56 | + ASSERT_THAT_EXPECTED( |
| 57 | + input.Write(malformed_header.data(), malformed_header.size()), |
| 58 | + Succeeded()); |
| 59 | + ASSERT_THAT_EXPECTED( |
| 60 | + transport->Read(std::chrono::milliseconds(1)), |
| 61 | + FailedWithMessage( |
| 62 | + "expected 'Content-Length: ' and got 'COnTent-LenGth: '")); |
| 63 | +} |
| 64 | + |
| 65 | +TEST_F(TransportTest, Read) { |
| 66 | + Write(R"json({"seq": 1, "type": "request", "command": "abc"})json"); |
| 67 | + ASSERT_THAT_EXPECTED(transport->Read(std::chrono::milliseconds(1)), |
| 68 | + HasValue(testing::VariantWith<Request>( |
| 69 | + testing::FieldsAre(1, "abc", std::nullopt)))); |
| 70 | +} |
| 71 | + |
| 72 | +TEST_F(TransportTest, ReadWithTimeout) { |
| 73 | + ASSERT_THAT_EXPECTED(transport->Read(std::chrono::milliseconds(1)), |
| 74 | + Failed<TimeoutError>()); |
| 75 | +} |
| 76 | + |
| 77 | +TEST_F(TransportTest, ReadWithEOF) { |
| 78 | + input.CloseWriteFileDescriptor(); |
| 79 | + ASSERT_THAT_EXPECTED(transport->Read(std::chrono::milliseconds(1)), |
| 80 | + Failed<EndOfFileError>()); |
| 81 | +} |
0 commit comments