xref: /llvm-project/lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationTest.cpp (revision 9e046f02e34fbb93eea1bd8b4136da7fd8d06612)
1 //===-- GDBRemoteCommunicationTest.cpp --------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "GDBRemoteTestUtils.h"
10 #include "llvm/Testing/Support/Error.h"
11 
12 using namespace lldb_private::process_gdb_remote;
13 using namespace lldb_private;
14 using namespace lldb;
15 typedef GDBRemoteCommunication::PacketResult PacketResult;
16 
17 namespace {
18 
19 class TestClient : public GDBRemoteCommunication {
20 public:
21   TestClient()
22       : GDBRemoteCommunication("test.client", "test.client.listener") {}
23 
24   PacketResult ReadPacket(StringExtractorGDBRemote &response) {
25     return GDBRemoteCommunication::ReadPacket(response, std::chrono::seconds(1),
26                                               /*sync_on_timeout*/ false);
27   }
28 };
29 
30 class GDBRemoteCommunicationTest : public GDBRemoteTest {
31 public:
32   void SetUp() override {
33     ASSERT_THAT_ERROR(GDBRemoteCommunication::ConnectLocally(client, server),
34                       llvm::Succeeded());
35   }
36 
37 protected:
38   TestClient client;
39   MockServer server;
40 
41   bool Write(llvm::StringRef packet) {
42     ConnectionStatus status;
43     return server.Write(packet.data(), packet.size(), status, nullptr) ==
44            packet.size();
45   }
46 };
47 } // end anonymous namespace
48 
49 TEST_F(GDBRemoteCommunicationTest, ReadPacket_checksum) {
50   struct TestCase {
51     llvm::StringLiteral Packet;
52     llvm::StringLiteral Payload;
53   };
54   static constexpr TestCase Tests[] = {
55       {{"$#00"}, {""}},
56       {{"$foobar#79"}, {"foobar"}},
57       {{"$}}#fa"}, {"]"}},
58       {{"$x*%#c7"}, {"xxxxxxxxx"}},
59   };
60   for (const auto &Test : Tests) {
61     SCOPED_TRACE(Test.Packet + " -> " + Test.Payload);
62     StringExtractorGDBRemote response;
63     ASSERT_TRUE(Write(Test.Packet));
64     ASSERT_EQ(PacketResult::Success, client.ReadPacket(response));
65     ASSERT_EQ(Test.Payload, response.GetStringRef());
66     ASSERT_EQ(PacketResult::Success, server.GetAck());
67   }
68 }
69