| 1 | //===-- GDBRemoteTestUtils.h ------------------------------------*- C++ -*-===// |
| 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 | #ifndef LLDB_UNITTESTS_PROCESS_GDB_REMOTE_GDBREMOTETESTUTILS_H |
| 9 | #define LLDB_UNITTESTS_PROCESS_GDB_REMOTE_GDBREMOTETESTUTILS_H |
| 10 | |
| 11 | #include "gmock/gmock.h" |
| 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.h" |
| 15 | #include "lldb/Utility/Connection.h" |
| 16 | |
| 17 | namespace lldb_private { |
| 18 | namespace process_gdb_remote { |
| 19 | |
| 20 | class GDBRemoteTest : public testing::Test { |
| 21 | public: |
| 22 | static void SetUpTestCase(); |
| 23 | static void TearDownTestCase(); |
| 24 | }; |
| 25 | |
| 26 | class MockConnection : public lldb_private::Connection { |
| 27 | public: |
| 28 | MockConnection(std::vector<std::string> &packets) { m_packets = &packets; }; |
| 29 | |
| 30 | MOCK_METHOD2(Connect, |
| 31 | lldb::ConnectionStatus(llvm::StringRef url, Status *error_ptr)); |
| 32 | MOCK_METHOD5(Read, size_t(void *dst, size_t dst_len, |
| 33 | const Timeout<std::micro> &timeout, |
| 34 | lldb::ConnectionStatus &status, Status *error_ptr)); |
| 35 | MOCK_METHOD0(GetURI, std::string()); |
| 36 | MOCK_METHOD0(InterruptRead, bool()); |
| 37 | |
| 38 | lldb::ConnectionStatus Disconnect(Status *error_ptr) { |
| 39 | return lldb::eConnectionStatusSuccess; |
| 40 | }; |
| 41 | |
| 42 | bool IsConnected() const { return true; }; |
| 43 | size_t Write(const void *dst, size_t dst_len, lldb::ConnectionStatus &status, |
| 44 | Status *error_ptr) { |
| 45 | m_packets->emplace_back(args: static_cast<const char *>(dst), args&: dst_len); |
| 46 | return dst_len; |
| 47 | }; |
| 48 | |
| 49 | lldb::IOObjectSP GetReadObject() { return lldb::IOObjectSP(); } |
| 50 | |
| 51 | std::vector<std::string> *m_packets; |
| 52 | }; |
| 53 | |
| 54 | class MockServer : public GDBRemoteCommunicationServer { |
| 55 | public: |
| 56 | MockServer() : GDBRemoteCommunicationServer() { |
| 57 | m_send_acks = false; |
| 58 | m_send_error_strings = true; |
| 59 | } |
| 60 | |
| 61 | PacketResult SendPacket(llvm::StringRef payload) { |
| 62 | return GDBRemoteCommunicationServer::SendPacketNoLock(payload); |
| 63 | } |
| 64 | |
| 65 | PacketResult (StringExtractorGDBRemote &response) { |
| 66 | const bool sync_on_timeout = false; |
| 67 | return ReadPacket(response, timeout: std::chrono::seconds(1), sync_on_timeout); |
| 68 | } |
| 69 | |
| 70 | using GDBRemoteCommunicationServer::SendErrorResponse; |
| 71 | using GDBRemoteCommunicationServer::SendOKResponse; |
| 72 | using GDBRemoteCommunicationServer::SendUnimplementedResponse; |
| 73 | }; |
| 74 | |
| 75 | class MockServerWithMockConnection : public MockServer { |
| 76 | public: |
| 77 | MockServerWithMockConnection() : MockServer() { |
| 78 | SetConnection(std::make_unique<MockConnection>(args&: m_packets)); |
| 79 | } |
| 80 | |
| 81 | llvm::ArrayRef<std::string> GetPackets() { return m_packets; }; |
| 82 | |
| 83 | std::vector<std::string> m_packets; |
| 84 | }; |
| 85 | |
| 86 | } // namespace process_gdb_remote |
| 87 | } // namespace lldb_private |
| 88 | |
| 89 | #endif // LLDB_UNITTESTS_PROCESS_GDB_REMOTE_GDBREMOTETESTUTILS_H |
| 90 | |