1 //===-- GDBRemoteTestUtils.h ------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
8 #ifndef LLDB_UNITTESTS_PROCESS_GDB_REMOTE_GDBREMOTETESTUTILS_H
9 #define LLDB_UNITTESTS_PROCESS_GDB_REMOTE_GDBREMOTETESTUTILS_H
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
14 #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.h"
15 #include "lldb/Utility/Connection.h"
17 namespace lldb_private
{
18 namespace process_gdb_remote
{
20 class GDBRemoteTest
: public testing::Test
{
22 static void SetUpTestCase();
23 static void TearDownTestCase();
26 class MockConnection
: public lldb_private::Connection
{
28 MockConnection(std::vector
<std::string
> &packets
) { m_packets
= &packets
; };
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());
38 lldb::ConnectionStatus
Disconnect(Status
*error_ptr
) {
39 return lldb::eConnectionStatusSuccess
;
42 bool IsConnected() const { return true; };
43 size_t Write(const void *dst
, size_t dst_len
, lldb::ConnectionStatus
&status
,
45 m_packets
->emplace_back(static_cast<const char *>(dst
), dst_len
);
49 lldb::IOObjectSP
GetReadObject() { return lldb::IOObjectSP(); }
51 std::vector
<std::string
> *m_packets
;
54 class MockServer
: public GDBRemoteCommunicationServer
{
56 MockServer() : GDBRemoteCommunicationServer() {
58 m_send_error_strings
= true;
61 PacketResult
SendPacket(llvm::StringRef payload
) {
62 return GDBRemoteCommunicationServer::SendPacketNoLock(payload
);
65 PacketResult
GetPacket(StringExtractorGDBRemote
&response
) {
66 const bool sync_on_timeout
= false;
67 return ReadPacket(response
, std::chrono::seconds(1), sync_on_timeout
);
70 using GDBRemoteCommunicationServer::SendErrorResponse
;
71 using GDBRemoteCommunicationServer::SendOKResponse
;
72 using GDBRemoteCommunicationServer::SendUnimplementedResponse
;
75 class MockServerWithMockConnection
: public MockServer
{
77 MockServerWithMockConnection() : MockServer() {
78 SetConnection(std::make_unique
<MockConnection
>(m_packets
));
81 llvm::ArrayRef
<std::string
> GetPackets() { return m_packets
; };
83 std::vector
<std::string
> m_packets
;
86 } // namespace process_gdb_remote
87 } // namespace lldb_private
89 #endif // LLDB_UNITTESTS_PROCESS_GDB_REMOTE_GDBREMOTETESTUTILS_H