xref: /llvm-project/lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationTest.cpp (revision bdb4468d39496088fc05d8c5575647fac9c8062a)
180814287SRaphael Isemann //===-- GDBRemoteCommunicationTest.cpp ------------------------------------===//
25a841234SPavel Labath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a841234SPavel Labath //
75a841234SPavel Labath //===----------------------------------------------------------------------===//
85a841234SPavel Labath #include "GDBRemoteTestUtils.h"
95a841234SPavel Labath #include "llvm/Testing/Support/Error.h"
105a841234SPavel Labath 
115a841234SPavel Labath using namespace lldb_private::process_gdb_remote;
125a841234SPavel Labath using namespace lldb_private;
135a841234SPavel Labath using namespace lldb;
145a841234SPavel Labath typedef GDBRemoteCommunication::PacketResult PacketResult;
155a841234SPavel Labath 
165a841234SPavel Labath namespace {
175a841234SPavel Labath 
185a841234SPavel Labath class TestClient : public GDBRemoteCommunication {
195a841234SPavel Labath public:
TestClient()20*bdb4468dSMichał Górny   TestClient() : GDBRemoteCommunication() {}
215a841234SPavel Labath 
ReadPacket(StringExtractorGDBRemote & response)225a841234SPavel Labath   PacketResult ReadPacket(StringExtractorGDBRemote &response) {
235a841234SPavel Labath     return GDBRemoteCommunication::ReadPacket(response, std::chrono::seconds(1),
245a841234SPavel Labath                                               /*sync_on_timeout*/ false);
255a841234SPavel Labath   }
265a841234SPavel Labath };
275a841234SPavel Labath 
285a841234SPavel Labath class GDBRemoteCommunicationTest : public GDBRemoteTest {
295a841234SPavel Labath public:
SetUp()305a841234SPavel Labath   void SetUp() override {
319e046f02SJonas Devlieghere     ASSERT_THAT_ERROR(GDBRemoteCommunication::ConnectLocally(client, server),
329e046f02SJonas Devlieghere                       llvm::Succeeded());
335a841234SPavel Labath   }
345a841234SPavel Labath 
355a841234SPavel Labath protected:
365a841234SPavel Labath   TestClient client;
375a841234SPavel Labath   MockServer server;
385a841234SPavel Labath 
Write(llvm::StringRef packet)395a841234SPavel Labath   bool Write(llvm::StringRef packet) {
405a841234SPavel Labath     ConnectionStatus status;
4103b8f790SMichał Górny     return server.WriteAll(packet.data(), packet.size(), status, nullptr) ==
425a841234SPavel Labath            packet.size();
435a841234SPavel Labath   }
445a841234SPavel Labath };
455a841234SPavel Labath } // end anonymous namespace
465a841234SPavel Labath 
47165545c7SPavel Labath // Test that we can decode packets correctly. In particular, verify that
48165545c7SPavel Labath // checksum calculation works.
TEST_F(GDBRemoteCommunicationTest,ReadPacket)49165545c7SPavel Labath TEST_F(GDBRemoteCommunicationTest, ReadPacket) {
505a841234SPavel Labath   struct TestCase {
515a841234SPavel Labath     llvm::StringLiteral Packet;
525a841234SPavel Labath     llvm::StringLiteral Payload;
535a841234SPavel Labath   };
545a841234SPavel Labath   static constexpr TestCase Tests[] = {
555a841234SPavel Labath       {{"$#00"}, {""}},
565a841234SPavel Labath       {{"$foobar#79"}, {"foobar"}},
57165545c7SPavel Labath       {{"$}]#da"}, {"}"}},          // Escaped }
58165545c7SPavel Labath       {{"$x*%#c7"}, {"xxxxxxxxx"}}, // RLE
59165545c7SPavel Labath       {{"+$#00"}, {""}},            // Spurious ACK
60165545c7SPavel Labath       {{"-$#00"}, {""}},            // Spurious NAK
615a841234SPavel Labath   };
625a841234SPavel Labath   for (const auto &Test : Tests) {
635a841234SPavel Labath     SCOPED_TRACE(Test.Packet + " -> " + Test.Payload);
645a841234SPavel Labath     StringExtractorGDBRemote response;
655a841234SPavel Labath     ASSERT_TRUE(Write(Test.Packet));
665a841234SPavel Labath     ASSERT_EQ(PacketResult::Success, client.ReadPacket(response));
675a841234SPavel Labath     ASSERT_EQ(Test.Payload, response.GetStringRef());
685a841234SPavel Labath     ASSERT_EQ(PacketResult::Success, server.GetAck());
695a841234SPavel Labath   }
705a841234SPavel Labath }
71