100 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "GDBRemoteTestUtils.h"10#include "lldb/Host/ConnectionFileDescriptor.h"11#include "llvm/Testing/Support/Error.h"12 13using namespace lldb_private::process_gdb_remote;14using namespace lldb_private;15using namespace lldb;16typedef GDBRemoteCommunication::PacketResult PacketResult;17 18namespace {19 20class TestClient : public GDBRemoteCommunication {21public: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 30class GDBRemoteCommunicationTest : public GDBRemoteTest {31public: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 41protected: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 namespace52 53// Test that we can decode packets correctly. In particular, verify that54// checksum calculation works.55TEST_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"}}, // RLE65 {{"+$#00"}, {""}}, // Spurious ACK66 {{"-$#00"}, {""}}, // Spurious NAK67 };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 and79// reported as invalid.80TEST_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 count89 {{"$*#00"}, PacketType::Invalid}, // '*' without a preceding character90 {{"$xx}#00"}, PacketType::Invalid}, // bare escape character '}'91 {{"%#00"}, PacketType::Notify}, // a correct packet after an invalid92 };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}100