xref: /llvm-project/lldb/unittests/Target/ModuleCacheTest.cpp (revision 59b78bcba2c7c20fbf5b4dcd14ea7a23c196a289)
1 #include "gtest/gtest.h"
2 
3 #include "llvm/ADT/SmallString.h"
4 #include "llvm/Support/FileSystem.h"
5 #include "llvm/Support/Path.h"
6 
7 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
8 #include "TestingSupport/TestUtilities.h"
9 #include "lldb/Core/Module.h"
10 #include "lldb/Core/ModuleSpec.h"
11 #include "lldb/Host/FileSystem.h"
12 #include "lldb/Host/HostInfo.h"
13 #include "lldb/Symbol/SymbolContext.h"
14 #include "lldb/Target/ModuleCache.h"
15 
16 using namespace lldb_private;
17 using namespace lldb;
18 
19 namespace {
20 
21 class ModuleCacheTest : public testing::Test {
22 public:
23   static void SetUpTestCase();
24 
25   static void TearDownTestCase();
26 
27 protected:
28   static FileSpec s_cache_dir;
29   static std::string s_test_executable;
30 
31   void TryGetAndPut(const FileSpec &cache_dir, const char *hostname,
32                     bool expect_download);
33 };
34 }
35 
36 FileSpec ModuleCacheTest::s_cache_dir;
37 std::string ModuleCacheTest::s_test_executable;
38 
39 static const char dummy_hostname[] = "dummy_hostname";
40 static const char dummy_remote_dir[] = "bin";
41 static const char module_name[] = "TestModule.so";
42 static const char module_uuid[] =
43     "F4E7E991-9B61-6AD4-0073-561AC3D9FA10-C043A476";
44 static const uint32_t uuid_bytes = 20;
45 static const size_t module_size = 5602;
46 
47 static FileSpec GetDummyRemotePath() {
48   FileSpec fs("/", false, FileSpec::Style::posix);
49   fs.AppendPathComponent(dummy_remote_dir);
50   fs.AppendPathComponent(module_name);
51   return fs;
52 }
53 
54 static FileSpec GetUuidView(FileSpec spec) {
55   spec.AppendPathComponent(".cache");
56   spec.AppendPathComponent(module_uuid);
57   spec.AppendPathComponent(module_name);
58   return spec;
59 }
60 
61 static FileSpec GetSysrootView(FileSpec spec, const char *hostname) {
62   spec.AppendPathComponent(hostname);
63   spec.AppendPathComponent(dummy_remote_dir);
64   spec.AppendPathComponent(module_name);
65   return spec;
66 }
67 
68 void ModuleCacheTest::SetUpTestCase() {
69   FileSystem::Initialize();
70   HostInfo::Initialize();
71   ObjectFileELF::Initialize();
72 
73   s_cache_dir = HostInfo::GetProcessTempDir();
74   s_test_executable = GetInputFilePath(module_name);
75 }
76 
77 void ModuleCacheTest::TearDownTestCase() {
78   ObjectFileELF::Terminate();
79   HostInfo::Terminate();
80   FileSystem::Terminate();
81 }
82 
83 static void VerifyDiskState(const FileSpec &cache_dir, const char *hostname) {
84   FileSpec uuid_view = GetUuidView(cache_dir);
85   EXPECT_TRUE(uuid_view.Exists()) << "uuid_view is: " << uuid_view.GetCString();
86   EXPECT_EQ(module_size, FileSystem::Instance().GetByteSize(uuid_view));
87 
88   FileSpec sysroot_view = GetSysrootView(cache_dir, hostname);
89   EXPECT_TRUE(sysroot_view.Exists()) << "sysroot_view is: "
90                                      << sysroot_view.GetCString();
91   EXPECT_EQ(module_size, FileSystem::Instance().GetByteSize(sysroot_view));
92 }
93 
94 void ModuleCacheTest::TryGetAndPut(const FileSpec &cache_dir,
95                                    const char *hostname, bool expect_download) {
96   ModuleCache mc;
97   ModuleSpec module_spec;
98   module_spec.GetFileSpec() = GetDummyRemotePath();
99   module_spec.GetUUID().SetFromStringRef(module_uuid, uuid_bytes);
100   module_spec.SetObjectSize(module_size);
101   ModuleSP module_sp;
102   bool did_create;
103   bool download_called = false;
104 
105   Status error = mc.GetAndPut(
106       cache_dir, hostname, module_spec,
107       [&download_called](const ModuleSpec &module_spec,
108                          const FileSpec &tmp_download_file_spec) {
109         download_called = true;
110         EXPECT_STREQ(GetDummyRemotePath().GetCString(),
111                      module_spec.GetFileSpec().GetCString());
112         std::error_code ec = llvm::sys::fs::copy_file(
113             s_test_executable, tmp_download_file_spec.GetCString());
114         EXPECT_FALSE(ec);
115         return Status();
116       },
117       [](const ModuleSP &module_sp, const FileSpec &tmp_download_file_spec) {
118         return Status("Not supported.");
119       },
120       module_sp, &did_create);
121   EXPECT_EQ(expect_download, download_called);
122 
123   EXPECT_TRUE(error.Success()) << "Error was: " << error.AsCString();
124   EXPECT_TRUE(did_create);
125   ASSERT_TRUE(bool(module_sp));
126 
127   SymbolContextList sc_list;
128   EXPECT_EQ(1u, module_sp->FindFunctionSymbols(ConstString("boom"),
129                                                eFunctionNameTypeFull, sc_list));
130   EXPECT_STREQ(GetDummyRemotePath().GetCString(),
131                module_sp->GetPlatformFileSpec().GetCString());
132   EXPECT_STREQ(module_uuid, module_sp->GetUUID().GetAsString().c_str());
133 }
134 
135 TEST_F(ModuleCacheTest, GetAndPut) {
136   FileSpec test_cache_dir = s_cache_dir;
137   test_cache_dir.AppendPathComponent("GetAndPut");
138 
139   const bool expect_download = true;
140   TryGetAndPut(test_cache_dir, dummy_hostname, expect_download);
141   VerifyDiskState(test_cache_dir, dummy_hostname);
142 }
143 
144 TEST_F(ModuleCacheTest, GetAndPutUuidExists) {
145   FileSpec test_cache_dir = s_cache_dir;
146   test_cache_dir.AppendPathComponent("GetAndPutUuidExists");
147 
148   FileSpec uuid_view = GetUuidView(test_cache_dir);
149   std::error_code ec =
150       llvm::sys::fs::create_directories(uuid_view.GetDirectory().GetCString());
151   ASSERT_FALSE(ec);
152   ec = llvm::sys::fs::copy_file(s_test_executable, uuid_view.GetCString());
153   ASSERT_FALSE(ec);
154 
155   const bool expect_download = false;
156   TryGetAndPut(test_cache_dir, dummy_hostname, expect_download);
157   VerifyDiskState(test_cache_dir, dummy_hostname);
158 }
159 
160 TEST_F(ModuleCacheTest, GetAndPutStrangeHostname) {
161   FileSpec test_cache_dir = s_cache_dir;
162   test_cache_dir.AppendPathComponent("GetAndPutStrangeHostname");
163 
164   const bool expect_download = true;
165   TryGetAndPut(test_cache_dir, "tab\tcolon:asterisk*", expect_download);
166   VerifyDiskState(test_cache_dir, "tab_colon_asterisk_");
167 }
168