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(FileSystem::Instance().Exists(uuid_view)) 86 << "uuid_view is: " << uuid_view.GetCString(); 87 EXPECT_EQ(module_size, FileSystem::Instance().GetByteSize(uuid_view)); 88 89 FileSpec sysroot_view = GetSysrootView(cache_dir, hostname); 90 EXPECT_TRUE(FileSystem::Instance().Exists(sysroot_view)) 91 << "sysroot_view is: " << sysroot_view.GetCString(); 92 EXPECT_EQ(module_size, FileSystem::Instance().GetByteSize(sysroot_view)); 93 } 94 95 void ModuleCacheTest::TryGetAndPut(const FileSpec &cache_dir, 96 const char *hostname, bool expect_download) { 97 ModuleCache mc; 98 ModuleSpec module_spec; 99 module_spec.GetFileSpec() = GetDummyRemotePath(); 100 module_spec.GetUUID().SetFromStringRef(module_uuid, uuid_bytes); 101 module_spec.SetObjectSize(module_size); 102 ModuleSP module_sp; 103 bool did_create; 104 bool download_called = false; 105 106 Status error = mc.GetAndPut( 107 cache_dir, hostname, module_spec, 108 [&download_called](const ModuleSpec &module_spec, 109 const FileSpec &tmp_download_file_spec) { 110 download_called = true; 111 EXPECT_STREQ(GetDummyRemotePath().GetCString(), 112 module_spec.GetFileSpec().GetCString()); 113 std::error_code ec = llvm::sys::fs::copy_file( 114 s_test_executable, tmp_download_file_spec.GetCString()); 115 EXPECT_FALSE(ec); 116 return Status(); 117 }, 118 [](const ModuleSP &module_sp, const FileSpec &tmp_download_file_spec) { 119 return Status("Not supported."); 120 }, 121 module_sp, &did_create); 122 EXPECT_EQ(expect_download, download_called); 123 124 EXPECT_TRUE(error.Success()) << "Error was: " << error.AsCString(); 125 EXPECT_TRUE(did_create); 126 ASSERT_TRUE(bool(module_sp)); 127 128 SymbolContextList sc_list; 129 EXPECT_EQ(1u, module_sp->FindFunctionSymbols(ConstString("boom"), 130 eFunctionNameTypeFull, sc_list)); 131 EXPECT_STREQ(GetDummyRemotePath().GetCString(), 132 module_sp->GetPlatformFileSpec().GetCString()); 133 EXPECT_STREQ(module_uuid, module_sp->GetUUID().GetAsString().c_str()); 134 } 135 136 TEST_F(ModuleCacheTest, GetAndPut) { 137 FileSpec test_cache_dir = s_cache_dir; 138 test_cache_dir.AppendPathComponent("GetAndPut"); 139 140 const bool expect_download = true; 141 TryGetAndPut(test_cache_dir, dummy_hostname, expect_download); 142 VerifyDiskState(test_cache_dir, dummy_hostname); 143 } 144 145 TEST_F(ModuleCacheTest, GetAndPutUuidExists) { 146 FileSpec test_cache_dir = s_cache_dir; 147 test_cache_dir.AppendPathComponent("GetAndPutUuidExists"); 148 149 FileSpec uuid_view = GetUuidView(test_cache_dir); 150 std::error_code ec = 151 llvm::sys::fs::create_directories(uuid_view.GetDirectory().GetCString()); 152 ASSERT_FALSE(ec); 153 ec = llvm::sys::fs::copy_file(s_test_executable, uuid_view.GetCString()); 154 ASSERT_FALSE(ec); 155 156 const bool expect_download = false; 157 TryGetAndPut(test_cache_dir, dummy_hostname, expect_download); 158 VerifyDiskState(test_cache_dir, dummy_hostname); 159 } 160 161 TEST_F(ModuleCacheTest, GetAndPutStrangeHostname) { 162 FileSpec test_cache_dir = s_cache_dir; 163 test_cache_dir.AppendPathComponent("GetAndPutStrangeHostname"); 164 165 const bool expect_download = true; 166 TryGetAndPut(test_cache_dir, "tab\tcolon:asterisk*", expect_download); 167 VerifyDiskState(test_cache_dir, "tab_colon_asterisk_"); 168 } 169