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(Connect(client, server), 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