xref: /llvm-project/lldb/unittests/Target/RemoteAwarePlatformTest.cpp (revision b798f4bd50bbf0f5eb46804afad10629797c73aa)
1631048e8SLevon Ter-Grigoryan //===-- RemoteAwarePlatformTest.cpp ---------------------------------------===//
2631048e8SLevon Ter-Grigoryan //
3631048e8SLevon Ter-Grigoryan // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4631048e8SLevon Ter-Grigoryan // See https://llvm.org/LICENSE.txt for license information.
5631048e8SLevon Ter-Grigoryan // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6631048e8SLevon Ter-Grigoryan //
7631048e8SLevon Ter-Grigoryan //===----------------------------------------------------------------------===//
8631048e8SLevon Ter-Grigoryan 
9631048e8SLevon Ter-Grigoryan #include "lldb/Target/RemoteAwarePlatform.h"
10631048e8SLevon Ter-Grigoryan #include "lldb/Core/Debugger.h"
11631048e8SLevon Ter-Grigoryan #include "lldb/Core/Module.h"
12631048e8SLevon Ter-Grigoryan #include "lldb/Core/ModuleSpec.h"
13631048e8SLevon Ter-Grigoryan #include "lldb/Host/FileSystem.h"
14631048e8SLevon Ter-Grigoryan #include "lldb/Target/Platform.h"
15631048e8SLevon Ter-Grigoryan #include "lldb/Target/Process.h"
16631048e8SLevon Ter-Grigoryan #include "gmock/gmock.h"
17631048e8SLevon Ter-Grigoryan #include "gtest/gtest.h"
18631048e8SLevon Ter-Grigoryan 
19631048e8SLevon Ter-Grigoryan using namespace lldb_private;
20631048e8SLevon Ter-Grigoryan using namespace lldb;
21631048e8SLevon Ter-Grigoryan using namespace testing;
22631048e8SLevon Ter-Grigoryan 
23631048e8SLevon Ter-Grigoryan class RemoteAwarePlatformTester : public RemoteAwarePlatform {
24631048e8SLevon Ter-Grigoryan public:
25631048e8SLevon Ter-Grigoryan   using RemoteAwarePlatform::RemoteAwarePlatform;
26631048e8SLevon Ter-Grigoryan 
27a458ef4fSPavel Labath   MOCK_METHOD0(GetDescription, llvm::StringRef());
28a3939e15SPavel Labath   MOCK_METHOD0(GetPluginName, llvm::StringRef());
29dde487e5SJonas Devlieghere   MOCK_METHOD1(GetSupportedArchitectures,
30dde487e5SJonas Devlieghere                std::vector<ArchSpec>(const ArchSpec &process_host_arch));
31631048e8SLevon Ter-Grigoryan   MOCK_METHOD4(Attach,
32631048e8SLevon Ter-Grigoryan                ProcessSP(ProcessAttachInfo &, Debugger &, Target *, Status &));
33631048e8SLevon Ter-Grigoryan   MOCK_METHOD0(CalculateTrapHandlerSymbolNames, void());
34631048e8SLevon Ter-Grigoryan 
35bf3e3289SJonas Devlieghere   MOCK_METHOD2(ResolveExecutable,
36*b798f4bdSAdrian Prantl                std::pair<bool, ModuleSP>(const ModuleSpec &,
37098c01c1SPavel Labath                                          const FileSpecList *));
38bf3e3289SJonas Devlieghere   Status
39bf3e3289SJonas Devlieghere   ResolveExecutable(const ModuleSpec &module_spec,
40bf3e3289SJonas Devlieghere                     lldb::ModuleSP &exe_module_sp,
41fd146460SShafik Yaghmour                     const FileSpecList *module_search_paths_ptr) /*override*/
42fd146460SShafik Yaghmour   { // NOLINT(modernize-use-override)
43bf3e3289SJonas Devlieghere     auto pair = ResolveExecutable(module_spec, module_search_paths_ptr);
44098c01c1SPavel Labath     exe_module_sp = pair.second;
45*b798f4bdSAdrian Prantl     return pair.first ? Status() : Status::FromErrorString("error");
46098c01c1SPavel Labath   }
47098c01c1SPavel Labath 
48631048e8SLevon Ter-Grigoryan   void SetRemotePlatform(lldb::PlatformSP platform) {
49631048e8SLevon Ter-Grigoryan     m_remote_platform_sp = platform;
50631048e8SLevon Ter-Grigoryan   }
51631048e8SLevon Ter-Grigoryan };
52631048e8SLevon Ter-Grigoryan 
53631048e8SLevon Ter-Grigoryan class TargetPlatformTester : public Platform {
54631048e8SLevon Ter-Grigoryan public:
55631048e8SLevon Ter-Grigoryan   using Platform::Platform;
56631048e8SLevon Ter-Grigoryan 
57a458ef4fSPavel Labath   MOCK_METHOD0(GetDescription, llvm::StringRef());
58a3939e15SPavel Labath   MOCK_METHOD0(GetPluginName, llvm::StringRef());
59dde487e5SJonas Devlieghere   MOCK_METHOD1(GetSupportedArchitectures,
60dde487e5SJonas Devlieghere                std::vector<ArchSpec>(const ArchSpec &process_host_arch));
61631048e8SLevon Ter-Grigoryan   MOCK_METHOD4(Attach,
62631048e8SLevon Ter-Grigoryan                ProcessSP(ProcessAttachInfo &, Debugger &, Target *, Status &));
63631048e8SLevon Ter-Grigoryan   MOCK_METHOD0(CalculateTrapHandlerSymbolNames, void());
64631048e8SLevon Ter-Grigoryan   MOCK_METHOD0(GetUserIDResolver, UserIDResolver &());
65631048e8SLevon Ter-Grigoryan };
66631048e8SLevon Ter-Grigoryan 
67631048e8SLevon Ter-Grigoryan namespace {
68631048e8SLevon Ter-Grigoryan class RemoteAwarePlatformTest : public testing::Test {
69631048e8SLevon Ter-Grigoryan public:
70631048e8SLevon Ter-Grigoryan   static void SetUpTestCase() { FileSystem::Initialize(); }
71631048e8SLevon Ter-Grigoryan   static void TearDownTestCase() { FileSystem::Terminate(); }
72631048e8SLevon Ter-Grigoryan };
73631048e8SLevon Ter-Grigoryan } // namespace
74631048e8SLevon Ter-Grigoryan 
75631048e8SLevon Ter-Grigoryan TEST_F(RemoteAwarePlatformTest, TestResolveExecutabelOnClientByPlatform) {
76631048e8SLevon Ter-Grigoryan   ModuleSpec executable_spec;
77631048e8SLevon Ter-Grigoryan   ModuleSP expected_executable(new Module(executable_spec));
78631048e8SLevon Ter-Grigoryan 
79631048e8SLevon Ter-Grigoryan   RemoteAwarePlatformTester platform(false);
80dde487e5SJonas Devlieghere   static const ArchSpec process_host_arch;
81dde487e5SJonas Devlieghere   EXPECT_CALL(platform, GetSupportedArchitectures(process_host_arch))
8296beb30fSPavel Labath       .WillRepeatedly(Return(std::vector<ArchSpec>()));
83bf3e3289SJonas Devlieghere   EXPECT_CALL(platform, ResolveExecutable(_, _))
84*b798f4bdSAdrian Prantl       .WillRepeatedly(Return(std::make_pair(true, expected_executable)));
85631048e8SLevon Ter-Grigoryan 
86098c01c1SPavel Labath   platform.SetRemotePlatform(std::make_shared<TargetPlatformTester>(false));
87631048e8SLevon Ter-Grigoryan 
88631048e8SLevon Ter-Grigoryan   ModuleSP resolved_sp;
89631048e8SLevon Ter-Grigoryan   lldb_private::Status status =
90631048e8SLevon Ter-Grigoryan       platform.ResolveExecutable(executable_spec, resolved_sp, nullptr);
91631048e8SLevon Ter-Grigoryan 
92631048e8SLevon Ter-Grigoryan   ASSERT_TRUE(status.Success());
93631048e8SLevon Ter-Grigoryan   EXPECT_EQ(expected_executable.get(), resolved_sp.get());
94631048e8SLevon Ter-Grigoryan }
95