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:
MockConnection(std::vector<std::string> & packets)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 
Disconnect(Status * error_ptr)38   lldb::ConnectionStatus Disconnect(Status *error_ptr) {
39     return lldb::eConnectionStatusSuccess;
40   };
41 
IsConnected()42   bool IsConnected() const { return true; };
Write(const void * dst,size_t dst_len,lldb::ConnectionStatus & status,Status * error_ptr)43   size_t Write(const void *dst, size_t dst_len, lldb::ConnectionStatus &status,
44                Status *error_ptr) {
45     m_packets->emplace_back(static_cast<const char *>(dst), dst_len);
46     return dst_len;
47   };
48 
GetReadObject()49   lldb::IOObjectSP GetReadObject() { return lldb::IOObjectSP(); }
50 
51   std::vector<std::string> *m_packets;
52 };
53 
54 class MockServer : public GDBRemoteCommunicationServer {
55 public:
MockServer()56   MockServer()
57       : GDBRemoteCommunicationServer("mock-server", "mock-server.listener") {
58     m_send_acks = false;
59     m_send_error_strings = true;
60   }
61 
SendPacket(llvm::StringRef payload)62   PacketResult SendPacket(llvm::StringRef payload) {
63     return GDBRemoteCommunicationServer::SendPacketNoLock(payload);
64   }
65 
GetPacket(StringExtractorGDBRemote & response)66   PacketResult GetPacket(StringExtractorGDBRemote &response) {
67     const bool sync_on_timeout = false;
68     return WaitForPacketNoLock(response, std::chrono::seconds(1),
69                                sync_on_timeout);
70   }
71 
72   using GDBRemoteCommunicationServer::SendErrorResponse;
73   using GDBRemoteCommunicationServer::SendOKResponse;
74   using GDBRemoteCommunicationServer::SendUnimplementedResponse;
75 };
76 
77 class MockServerWithMockConnection : public MockServer {
78 public:
MockServerWithMockConnection()79   MockServerWithMockConnection() : MockServer() {
80     SetConnection(std::make_unique<MockConnection>(m_packets));
81   }
82 
GetPackets()83   llvm::ArrayRef<std::string> GetPackets() { return m_packets; };
84 
85   std::vector<std::string> m_packets;
86 };
87 
88 } // namespace process_gdb_remote
89 } // namespace lldb_private
90 
91 #endif // LLDB_UNITTESTS_PROCESS_GDB_REMOTE_GDBREMOTETESTUTILS_H
92