156d7262bSPavel Labath //===-- GDBRemoteTestUtils.h ------------------------------------*- C++ -*-===// 256d7262bSPavel 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 656d7262bSPavel Labath // 756d7262bSPavel Labath //===----------------------------------------------------------------------===// 8cdc514e4SJonas Devlieghere #ifndef LLDB_UNITTESTS_PROCESS_GDB_REMOTE_GDBREMOTETESTUTILS_H 9cdc514e4SJonas Devlieghere #define LLDB_UNITTESTS_PROCESS_GDB_REMOTE_GDBREMOTETESTUTILS_H 1056d7262bSPavel Labath 1157e2da4fSAntonio Afonso #include "gmock/gmock.h" 12b9c1b51eSKate Stone #include "gtest/gtest.h" 1356d7262bSPavel Labath 1446031e6fSPavel Labath #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.h" 1557e2da4fSAntonio Afonso #include "lldb/Utility/Connection.h" 1646031e6fSPavel Labath 17b9c1b51eSKate Stone namespace lldb_private { 18b9c1b51eSKate Stone namespace process_gdb_remote { 1956d7262bSPavel Labath 20b9c1b51eSKate Stone class GDBRemoteTest : public testing::Test { 2156d7262bSPavel Labath public: 22b9c1b51eSKate Stone static void SetUpTestCase(); 23b9c1b51eSKate Stone static void TearDownTestCase(); 2493df2fbeSPavel Labath }; 2556d7262bSPavel Labath 2657e2da4fSAntonio Afonso class MockConnection : public lldb_private::Connection { 2757e2da4fSAntonio Afonso public: MockConnection(std::vector<std::string> & packets)2857e2da4fSAntonio Afonso MockConnection(std::vector<std::string> &packets) { m_packets = &packets; }; 2957e2da4fSAntonio Afonso 3057e2da4fSAntonio Afonso MOCK_METHOD2(Connect, 3157e2da4fSAntonio Afonso lldb::ConnectionStatus(llvm::StringRef url, Status *error_ptr)); 3257e2da4fSAntonio Afonso MOCK_METHOD5(Read, size_t(void *dst, size_t dst_len, 3357e2da4fSAntonio Afonso const Timeout<std::micro> &timeout, 3457e2da4fSAntonio Afonso lldb::ConnectionStatus &status, Status *error_ptr)); 3557e2da4fSAntonio Afonso MOCK_METHOD0(GetURI, std::string()); 3657e2da4fSAntonio Afonso MOCK_METHOD0(InterruptRead, bool()); 3757e2da4fSAntonio Afonso Disconnect(Status * error_ptr)3857e2da4fSAntonio Afonso lldb::ConnectionStatus Disconnect(Status *error_ptr) { 3957e2da4fSAntonio Afonso return lldb::eConnectionStatusSuccess; 4057e2da4fSAntonio Afonso }; 4157e2da4fSAntonio Afonso IsConnected()4257e2da4fSAntonio Afonso bool IsConnected() const { return true; }; Write(const void * dst,size_t dst_len,lldb::ConnectionStatus & status,Status * error_ptr)4357e2da4fSAntonio Afonso size_t Write(const void *dst, size_t dst_len, lldb::ConnectionStatus &status, 4457e2da4fSAntonio Afonso Status *error_ptr) { 4557e2da4fSAntonio Afonso m_packets->emplace_back(static_cast<const char *>(dst), dst_len); 4657e2da4fSAntonio Afonso return dst_len; 4757e2da4fSAntonio Afonso }; 4857e2da4fSAntonio Afonso GetReadObject()4933c3e0b9SJonas Devlieghere lldb::IOObjectSP GetReadObject() { return lldb::IOObjectSP(); } 5033c3e0b9SJonas Devlieghere 5157e2da4fSAntonio Afonso std::vector<std::string> *m_packets; 5257e2da4fSAntonio Afonso }; 5357e2da4fSAntonio Afonso 5457e2da4fSAntonio Afonso class MockServer : public GDBRemoteCommunicationServer { 5557e2da4fSAntonio Afonso public: MockServer()56*bdb4468dSMichał Górny MockServer() : GDBRemoteCommunicationServer() { 57b9c1b51eSKate Stone m_send_acks = false; 5857e2da4fSAntonio Afonso m_send_error_strings = true; 59b9c1b51eSKate Stone } 6056d7262bSPavel Labath SendPacket(llvm::StringRef payload)61b9c1b51eSKate Stone PacketResult SendPacket(llvm::StringRef payload) { 6226709df8SZachary Turner return GDBRemoteCommunicationServer::SendPacketNoLock(payload); 6356d7262bSPavel Labath } 6456d7262bSPavel Labath GetPacket(StringExtractorGDBRemote & response)65b9c1b51eSKate Stone PacketResult GetPacket(StringExtractorGDBRemote &response) { 6656d7262bSPavel Labath const bool sync_on_timeout = false; 67165545c7SPavel Labath return ReadPacket(response, std::chrono::seconds(1), sync_on_timeout); 6856d7262bSPavel Labath } 6956d7262bSPavel Labath 7057e2da4fSAntonio Afonso using GDBRemoteCommunicationServer::SendErrorResponse; 7156d7262bSPavel Labath using GDBRemoteCommunicationServer::SendOKResponse; 7256d7262bSPavel Labath using GDBRemoteCommunicationServer::SendUnimplementedResponse; 7356d7262bSPavel Labath }; 7456d7262bSPavel Labath 7557e2da4fSAntonio Afonso class MockServerWithMockConnection : public MockServer { 7657e2da4fSAntonio Afonso public: MockServerWithMockConnection()7757e2da4fSAntonio Afonso MockServerWithMockConnection() : MockServer() { 78451741a9SPavel Labath SetConnection(std::make_unique<MockConnection>(m_packets)); 7957e2da4fSAntonio Afonso } 8057e2da4fSAntonio Afonso GetPackets()8157e2da4fSAntonio Afonso llvm::ArrayRef<std::string> GetPackets() { return m_packets; }; 8257e2da4fSAntonio Afonso 8357e2da4fSAntonio Afonso std::vector<std::string> m_packets; 8457e2da4fSAntonio Afonso }; 8557e2da4fSAntonio Afonso 8656d7262bSPavel Labath } // namespace process_gdb_remote 8756d7262bSPavel Labath } // namespace lldb_private 8856d7262bSPavel Labath 89cdc514e4SJonas Devlieghere #endif // LLDB_UNITTESTS_PROCESS_GDB_REMOTE_GDBREMOTETESTUTILS_H 90