xref: /llvm-project/lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationTest.cpp (revision bdb4468d39496088fc05d8c5575647fac9c8062a)
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:
TestClient()20   TestClient() : GDBRemoteCommunication() {}
21 
ReadPacket(StringExtractorGDBRemote & response)22   PacketResult ReadPacket(StringExtractorGDBRemote &response) {
23     return GDBRemoteCommunication::ReadPacket(response, std::chrono::seconds(1),
24                                               /*sync_on_timeout*/ false);
25   }
26 };
27 
28 class GDBRemoteCommunicationTest : public GDBRemoteTest {
29 public:
SetUp()30   void SetUp() override {
31     ASSERT_THAT_ERROR(GDBRemoteCommunication::ConnectLocally(client, server),
32                       llvm::Succeeded());
33   }
34 
35 protected:
36   TestClient client;
37   MockServer server;
38 
Write(llvm::StringRef packet)39   bool Write(llvm::StringRef packet) {
40     ConnectionStatus status;
41     return server.WriteAll(packet.data(), packet.size(), status, nullptr) ==
42            packet.size();
43   }
44 };
45 } // end anonymous namespace
46 
47 // Test that we can decode packets correctly. In particular, verify that
48 // checksum calculation works.
TEST_F(GDBRemoteCommunicationTest,ReadPacket)49 TEST_F(GDBRemoteCommunicationTest, ReadPacket) {
50   struct TestCase {
51     llvm::StringLiteral Packet;
52     llvm::StringLiteral Payload;
53   };
54   static constexpr TestCase Tests[] = {
55       {{"$#00"}, {""}},
56       {{"$foobar#79"}, {"foobar"}},
57       {{"$}]#da"}, {"}"}},          // Escaped }
58       {{"$x*%#c7"}, {"xxxxxxxxx"}}, // RLE
59       {{"+$#00"}, {""}},            // Spurious ACK
60       {{"-$#00"}, {""}},            // Spurious NAK
61   };
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());
69   }
70 }
71