Debian Codegraph @

lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationTest.cpp

raw
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
9 #include "GDBRemoteTestUtils.h"
10 #include "lldb/Host/ConnectionFileDescriptor.h"
11 #include "llvm/Testing/Support/Error.h"
12
13 using namespace lldb_private::process_gdb_remote;
14 using namespace lldb_private;
15 using namespace lldb;
16 typedef GDBRemoteCommunication::PacketResult PacketResult;
17
18 namespace {
19
20 class TestClient : public GDBRemoteCommunication {
21 public:
22 TestClient() : GDBRemoteCommunication() {}
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 llvm::Expected<Socket::Pair> pair = Socket::CreatePair();
34 ASSERT_THAT_EXPECTED(pair, llvm::Succeeded());
35 client.SetConnection(
36 std::make_unique<ConnectionFileDescriptor>(std::move(pair->first)));
37 server.SetConnection(
38 std::make_unique<ConnectionFileDescriptor>(std::move(pair->second)));
39 }
40
41 protected:
42 TestClient client;
43 MockServer server;
44
45 bool Write(llvm::StringRef packet) {
46 ConnectionStatus status;
47 return server.WriteAll(packet.data(), packet.size(), status, nullptr) ==
48 packet.size();
49 }
50 };
51 } // end anonymous namespace
52
53 // Test that we can decode packets correctly. In particular, verify that
54 // checksum calculation works.
55 TEST_F(GDBRemoteCommunicationTest, ReadPacket) {
56 struct TestCase {
57 llvm::StringLiteral Packet;
58 llvm::StringLiteral Payload;
59 };
60 static constexpr TestCase Tests[] = {
61 {{"$#00"}, {""}},
62 {{"$foobar#79"}, {"foobar"}},
63 {{"$}]#da"}, {"}"}}, // Escaped }
64 {{"$x*%#c7"}, {"xxxxxxxxx"}}, // RLE
65 {{"+$#00"}, {""}}, // Spurious ACK
66 {{"-$#00"}, {""}}, // Spurious NAK
67 };
68 for (const auto &Test : Tests) {
69 SCOPED_TRACE(Test.Packet + " -> " + Test.Payload);
70 StringExtractorGDBRemote response;
71 ASSERT_TRUE(Write(Test.Packet));
72 ASSERT_EQ(PacketResult::Success, client.ReadPacket(response));
73 ASSERT_EQ(Test.Payload, response.GetStringRef());
74 ASSERT_EQ(PacketResult::Success, server.GetAck());
75 }
76 }
77
78 // Test that packets with incorrect RLE sequences do not cause a crash and
79 // reported as invalid.
80 TEST_F(GDBRemoteCommunicationTest, CheckForPacket) {
81 using PacketType = GDBRemoteCommunication::PacketType;
82 struct TestCase {
83 llvm::StringLiteral Packet;
84 PacketType Result;
85 };
86 static constexpr TestCase Tests[] = {
87 {{"$#00"}, PacketType::Standard},
88 {{"$xx*#00"}, PacketType::Invalid}, // '*' without a count
89 {{"$*#00"}, PacketType::Invalid}, // '*' without a preceding character
90 {{"$xx}#00"}, PacketType::Invalid}, // bare escape character '}'
91 {{"%#00"}, PacketType::Notify}, // a correct packet after an invalid
92 };
93 for (const auto &Test : Tests) {
94 SCOPED_TRACE(Test.Packet);
95 StringExtractorGDBRemote response;
96 EXPECT_EQ(Test.Result, client.CheckForPacket(Test.Packet.bytes_begin(),
97 Test.Packet.size(), response));
98 }
99 }