1 //===-- RemoteAwarePlatformTest.cpp ---------------------------------------===//
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 
9 #include "lldb/Target/RemoteAwarePlatform.h"
10 #include "lldb/Core/Debugger.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/ModuleSpec.h"
13 #include "lldb/Host/FileSystem.h"
14 #include "lldb/Target/Platform.h"
15 #include "lldb/Target/Process.h"
16 #include "gmock/gmock.h"
17 #include "gtest/gtest.h"
18 
19 using namespace lldb_private;
20 using namespace lldb;
21 using namespace testing;
22 
23 class RemoteAwarePlatformTester : public RemoteAwarePlatform {
24 public:
25   using RemoteAwarePlatform::RemoteAwarePlatform;
26 
27   MOCK_METHOD0(GetDescription, llvm::StringRef());
28   MOCK_METHOD0(GetPluginName, llvm::StringRef());
29   MOCK_METHOD0(GetSupportedArchitectures, std::vector<ArchSpec>());
30   MOCK_METHOD4(Attach,
31                ProcessSP(ProcessAttachInfo &, Debugger &, Target *, Status &));
32   MOCK_METHOD0(CalculateTrapHandlerSymbolNames, void());
33 
34   MOCK_METHOD2(ResolveRemoteExecutable,
35                std::pair<Status, ModuleSP>(const ModuleSpec &,
36                                            const FileSpecList *));
37   Status ResolveRemoteExecutable(
38       const ModuleSpec &module_spec, lldb::ModuleSP &exe_module_sp,
39       const FileSpecList *module_search_paths_ptr) /*override*/ {
40     auto pair = ResolveRemoteExecutable(module_spec, module_search_paths_ptr);
41     exe_module_sp = pair.second;
42     return pair.first;
43   }
44 
45   void SetRemotePlatform(lldb::PlatformSP platform) {
46     m_remote_platform_sp = platform;
47   }
48 };
49 
50 class TargetPlatformTester : public Platform {
51 public:
52   using Platform::Platform;
53 
54   MOCK_METHOD0(GetDescription, llvm::StringRef());
55   MOCK_METHOD0(GetPluginName, llvm::StringRef());
56   MOCK_METHOD0(GetSupportedArchitectures, std::vector<ArchSpec>());
57   MOCK_METHOD4(Attach,
58                ProcessSP(ProcessAttachInfo &, Debugger &, Target *, Status &));
59   MOCK_METHOD0(CalculateTrapHandlerSymbolNames, void());
60   MOCK_METHOD0(GetUserIDResolver, UserIDResolver &());
61 };
62 
63 namespace {
64 class RemoteAwarePlatformTest : public testing::Test {
65 public:
66   static void SetUpTestCase() { FileSystem::Initialize(); }
67   static void TearDownTestCase() { FileSystem::Terminate(); }
68 };
69 } // namespace
70 
71 TEST_F(RemoteAwarePlatformTest, TestResolveExecutabelOnClientByPlatform) {
72   ModuleSpec executable_spec;
73   ModuleSP expected_executable(new Module(executable_spec));
74 
75   RemoteAwarePlatformTester platform(false);
76   EXPECT_CALL(platform, GetSupportedArchitectures())
77       .WillRepeatedly(Return(std::vector<ArchSpec>()));
78   EXPECT_CALL(platform, ResolveRemoteExecutable(_, _))
79       .WillRepeatedly(Return(std::make_pair(Status(), expected_executable)));
80 
81   platform.SetRemotePlatform(std::make_shared<TargetPlatformTester>(false));
82 
83   ModuleSP resolved_sp;
84   lldb_private::Status status =
85       platform.ResolveExecutable(executable_spec, resolved_sp, nullptr);
86 
87   ASSERT_TRUE(status.Success());
88   EXPECT_EQ(expected_executable.get(), resolved_sp.get());
89 }
90