1//===-- GDBRemoteCommunicationTest.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 "GDBRemoteTestUtils.h"
10#include "lldb/Host/ConnectionFileDescriptor.h"
11#include "llvm/Testing/Support/Error.h"
12
13using namespace lldb_private::process_gdb_remote;
14using namespace lldb_private;
15using namespace lldb;
16typedef GDBRemoteCommunication::PacketResult PacketResult;
17
18namespace {
19
20class TestClient : public GDBRemoteCommunication {
21public:
22 TestClient() : GDBRemoteCommunication() {}
23
24 PacketResult ReadPacket(StringExtractorGDBRemote &response) {
25 return GDBRemoteCommunication::ReadPacket(response, timeout: std::chrono::seconds(1),
26 /*sync_on_timeout*/ false);
27 }
28};
29
30class GDBRemoteCommunicationTest : public GDBRemoteTest {
31public:
32 void SetUp() override {
33 llvm::Expected<Socket::Pair> pair = Socket::CreatePair();
34 ASSERT_THAT_EXPECTED(pair, llvm::Succeeded());
35 client.SetConnection(
36 std::make_unique<ConnectionFileDescriptor>(args: std::move(pair->first)));
37 server.SetConnection(
38 std::make_unique<ConnectionFileDescriptor>(args: std::move(pair->second)));
39 }
40
41protected:
42 TestClient client;
43 MockServer server;
44
45 bool Write(llvm::StringRef packet) {
46 ConnectionStatus status;
47 return server.WriteAll(src: packet.data(), src_len: packet.size(), status, error_ptr: nullptr) ==
48 packet.size();
49 }
50};
51} // end anonymous namespace
52
53// Test that we can decode packets correctly. In particular, verify that
54// checksum calculation works.
55TEST_F(GDBRemoteCommunicationTest, ReadPacket) {
56 struct TestCase {
57 llvm::StringLiteral Packet;
58 llvm::StringLiteral Payload;
59 };
60 static constexpr TestCase Tests[] = {
61 {.Packet: {"$#00"}, .Payload: {""}},
62 {.Packet: {"$foobar#79"}, .Payload: {"foobar"}},
63 {.Packet: {"$}]#da"}, .Payload: {"}"}}, // Escaped }
64 {.Packet: {"$x*%#c7"}, .Payload: {"xxxxxxxxx"}}, // RLE
65 {.Packet: {"+$#00"}, .Payload: {""}}, // Spurious ACK
66 {.Packet: {"-$#00"}, .Payload: {""}}, // Spurious NAK
67 };
68 for (const auto &Test : Tests) {
69 SCOPED_TRACE(Test.Packet + " -> " + Test.Payload);
70 StringExtractorGDBRemote response;
71 ASSERT_TRUE(Write(Test.Packet));
72 ASSERT_EQ(PacketResult::Success, client.ReadPacket(response));
73 ASSERT_EQ(Test.Payload, response.GetStringRef());
74 ASSERT_EQ(PacketResult::Success, server.GetAck());
75 }
76}
77
78// Test that packets with incorrect RLE sequences do not cause a crash and
79// reported as invalid.
80TEST_F(GDBRemoteCommunicationTest, CheckForPacket) {
81 using PacketType = GDBRemoteCommunication::PacketType;
82 struct TestCase {
83 llvm::StringLiteral Packet;
84 PacketType Result;
85 };
86 static constexpr TestCase Tests[] = {
87 {.Packet: {"$#00"}, .Result: PacketType::Standard},
88 {.Packet: {"$xx*#00"}, .Result: PacketType::Invalid}, // '*' without a count
89 {.Packet: {"$*#00"}, .Result: PacketType::Invalid}, // '*' without a preceding character
90 {.Packet: {"$xx}#00"}, .Result: PacketType::Invalid}, // bare escape character '}'
91 {.Packet: {"%#00"}, .Result: PacketType::Notify}, // a correct packet after an invalid
92 };
93 for (const auto &Test : Tests) {
94 SCOPED_TRACE(Test.Packet);
95 StringExtractorGDBRemote response;
96 EXPECT_EQ(Test.Result, client.CheckForPacket(Test.Packet.bytes_begin(),
97 Test.Packet.size(), response));
98 }
99}
100

source code of lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationTest.cpp