xref: /llvm-project/lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationTest.cpp (revision 03b8f79048bfc32ade0404df3d3931266a06dc92)
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 #include "GDBRemoteTestUtils.h"
9 #include "llvm/Testing/Support/Error.h"
10 
11 using namespace lldb_private::process_gdb_remote;
12 using namespace lldb_private;
13 using namespace lldb;
14 typedef GDBRemoteCommunication::PacketResult PacketResult;
15 
16 namespace {
17 
18 class TestClient : public GDBRemoteCommunication {
19 public:
20   TestClient()
21       : GDBRemoteCommunication("test.client", "test.client.listener") {}
22 
23   PacketResult ReadPacket(StringExtractorGDBRemote &response) {
24     return GDBRemoteCommunication::ReadPacket(response, std::chrono::seconds(1),
25                                               /*sync_on_timeout*/ false);
26   }
27 };
28 
29 class GDBRemoteCommunicationTest : public GDBRemoteTest {
30 public:
31   void SetUp() override {
32     ASSERT_THAT_ERROR(GDBRemoteCommunication::ConnectLocally(client, server),
33                       llvm::Succeeded());
34   }
35 
36 protected:
37   TestClient client;
38   MockServer server;
39 
40   bool Write(llvm::StringRef packet) {
41     ConnectionStatus status;
42     return server.WriteAll(packet.data(), packet.size(), status, nullptr) ==
43            packet.size();
44   }
45 };
46 } // end anonymous namespace
47 
48 // Test that we can decode packets correctly. In particular, verify that
49 // checksum calculation works.
50 TEST_F(GDBRemoteCommunicationTest, ReadPacket) {
51   struct TestCase {
52     llvm::StringLiteral Packet;
53     llvm::StringLiteral Payload;
54   };
55   static constexpr TestCase Tests[] = {
56       {{"$#00"}, {""}},
57       {{"$foobar#79"}, {"foobar"}},
58       {{"$}]#da"}, {"}"}},          // Escaped }
59       {{"$x*%#c7"}, {"xxxxxxxxx"}}, // RLE
60       {{"+$#00"}, {""}},            // Spurious ACK
61       {{"-$#00"}, {""}},            // Spurious NAK
62   };
63   for (const auto &Test : Tests) {
64     SCOPED_TRACE(Test.Packet + " -> " + Test.Payload);
65     StringExtractorGDBRemote response;
66     ASSERT_TRUE(Write(Test.Packet));
67     ASSERT_EQ(PacketResult::Success, client.ReadPacket(response));
68     ASSERT_EQ(Test.Payload, response.GetStringRef());
69     ASSERT_EQ(PacketResult::Success, server.GetAck());
70   }
71 }
72