1 //===-- GDBRemoteCommunicationTest.cpp ------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
8 #include "GDBRemoteTestUtils.h"
9 #include "llvm/Testing/Support/Error.h"
11 using namespace lldb_private::process_gdb_remote
;
12 using namespace lldb_private
;
14 typedef GDBRemoteCommunication::PacketResult PacketResult
;
18 class TestClient
: public GDBRemoteCommunication
{
20 TestClient() : GDBRemoteCommunication() {}
22 PacketResult
ReadPacket(StringExtractorGDBRemote
&response
) {
23 return GDBRemoteCommunication::ReadPacket(response
, std::chrono::seconds(1),
24 /*sync_on_timeout*/ false);
28 class GDBRemoteCommunicationTest
: public GDBRemoteTest
{
30 void SetUp() override
{
31 ASSERT_THAT_ERROR(GDBRemoteCommunication::ConnectLocally(client
, server
),
39 bool Write(llvm::StringRef packet
) {
40 ConnectionStatus status
;
41 return server
.WriteAll(packet
.data(), packet
.size(), status
, nullptr) ==
45 } // end anonymous namespace
47 // Test that we can decode packets correctly. In particular, verify that
48 // checksum calculation works.
49 TEST_F(GDBRemoteCommunicationTest
, ReadPacket
) {
51 llvm::StringLiteral Packet
;
52 llvm::StringLiteral Payload
;
54 static constexpr TestCase Tests
[] = {
56 {{"$foobar#79"}, {"foobar"}},
57 {{"$}]#da"}, {"}"}}, // Escaped }
58 {{"$x*%#c7"}, {"xxxxxxxxx"}}, // RLE
59 {{"+$#00"}, {""}}, // Spurious ACK
60 {{"-$#00"}, {""}}, // Spurious NAK
62 for (const auto &Test
: Tests
) {
63 SCOPED_TRACE(Test
.Packet
+ " -> " + Test
.Payload
);
64 StringExtractorGDBRemote response
;
65 ASSERT_TRUE(Write(Test
.Packet
));
66 ASSERT_EQ(PacketResult::Success
, client
.ReadPacket(response
));
67 ASSERT_EQ(Test
.Payload
, response
.GetStringRef());
68 ASSERT_EQ(PacketResult::Success
, server
.GetAck());