xref: /llvm-project/lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationTest.cpp (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
1 //===-- GDBRemoteCommunicationTest.cpp --------------------------*- C++ -*-===//
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.Write(packet.data(), packet.size(), status, nullptr) ==
43            packet.size();
44   }
45 };
46 } // end anonymous namespace
47 
48 TEST_F(GDBRemoteCommunicationTest, ReadPacket_checksum) {
49   struct TestCase {
50     llvm::StringLiteral Packet;
51     llvm::StringLiteral Payload;
52   };
53   static constexpr TestCase Tests[] = {
54       {{"$#00"}, {""}},
55       {{"$foobar#79"}, {"foobar"}},
56       {{"$}}#fa"}, {"]"}},
57       {{"$x*%#c7"}, {"xxxxxxxxx"}},
58   };
59   for (const auto &Test : Tests) {
60     SCOPED_TRACE(Test.Packet + " -> " + Test.Payload);
61     StringExtractorGDBRemote response;
62     ASSERT_TRUE(Write(Test.Packet));
63     ASSERT_EQ(PacketResult::Success, client.ReadPacket(response));
64     ASSERT_EQ(Test.Payload, response.GetStringRef());
65     ASSERT_EQ(PacketResult::Success, server.GetAck());
66   }
67 }
68