2016-05-25 18:48:16 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
|
|
|
#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
|
2018-11-01 05:49:27 +08:00
|
|
|
#include "TestingSupport/TestUtilities.h"
|
2016-05-25 18:48:16 +08:00
|
|
|
#include "lldb/Core/Module.h"
|
|
|
|
#include "lldb/Core/ModuleSpec.h"
|
2018-11-01 05:49:27 +08:00
|
|
|
#include "lldb/Host/FileSystem.h"
|
2016-05-25 18:48:16 +08:00
|
|
|
#include "lldb/Host/HostInfo.h"
|
|
|
|
#include "lldb/Symbol/SymbolContext.h"
|
2017-02-15 03:06:07 +08:00
|
|
|
#include "lldb/Target/ModuleCache.h"
|
2016-05-25 18:48:16 +08:00
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
using namespace lldb;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
namespace {
|
2016-05-25 18:48:16 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
class ModuleCacheTest : public testing::Test {
|
2016-05-25 18:48:16 +08:00
|
|
|
public:
|
2016-09-07 04:57:50 +08:00
|
|
|
static void SetUpTestCase();
|
2016-05-25 18:48:16 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
static void TearDownTestCase();
|
2016-05-25 18:48:16 +08:00
|
|
|
|
|
|
|
protected:
|
2016-09-07 04:57:50 +08:00
|
|
|
static FileSpec s_cache_dir;
|
2017-06-29 21:02:11 +08:00
|
|
|
static std::string s_test_executable;
|
2016-05-25 18:48:16 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void TryGetAndPut(const FileSpec &cache_dir, const char *hostname,
|
|
|
|
bool expect_download);
|
2016-05-25 18:48:16 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSpec ModuleCacheTest::s_cache_dir;
|
2017-06-29 21:02:11 +08:00
|
|
|
std::string ModuleCacheTest::s_test_executable;
|
2016-05-25 18:48:16 +08:00
|
|
|
|
|
|
|
static const char dummy_hostname[] = "dummy_hostname";
|
|
|
|
static const char dummy_remote_dir[] = "bin";
|
|
|
|
static const char module_name[] = "TestModule.so";
|
2016-09-07 04:57:50 +08:00
|
|
|
static const char module_uuid[] =
|
|
|
|
"F4E7E991-9B61-6AD4-0073-561AC3D9FA10-C043A476";
|
2016-05-25 18:48:16 +08:00
|
|
|
static const uint32_t uuid_bytes = 20;
|
|
|
|
static const size_t module_size = 5602;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
static FileSpec GetDummyRemotePath() {
|
2018-05-14 22:52:47 +08:00
|
|
|
FileSpec fs("/", false, FileSpec::Style::posix);
|
2016-09-07 04:57:50 +08:00
|
|
|
fs.AppendPathComponent(dummy_remote_dir);
|
|
|
|
fs.AppendPathComponent(module_name);
|
|
|
|
return fs;
|
2016-05-25 18:48:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
static FileSpec GetUuidView(FileSpec spec) {
|
|
|
|
spec.AppendPathComponent(".cache");
|
|
|
|
spec.AppendPathComponent(module_uuid);
|
|
|
|
spec.AppendPathComponent(module_name);
|
|
|
|
return spec;
|
2016-05-25 18:48:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
static FileSpec GetSysrootView(FileSpec spec, const char *hostname) {
|
|
|
|
spec.AppendPathComponent(hostname);
|
|
|
|
spec.AppendPathComponent(dummy_remote_dir);
|
|
|
|
spec.AppendPathComponent(module_name);
|
|
|
|
return spec;
|
2016-05-25 18:48:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void ModuleCacheTest::SetUpTestCase() {
|
2018-11-01 05:49:27 +08:00
|
|
|
FileSystem::Initialize();
|
2016-09-07 04:57:50 +08:00
|
|
|
HostInfo::Initialize();
|
|
|
|
ObjectFileELF::Initialize();
|
2016-05-25 18:48:16 +08:00
|
|
|
|
2018-06-19 23:09:07 +08:00
|
|
|
s_cache_dir = HostInfo::GetProcessTempDir();
|
2017-06-29 21:02:11 +08:00
|
|
|
s_test_executable = GetInputFilePath(module_name);
|
2016-05-25 18:48:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void ModuleCacheTest::TearDownTestCase() {
|
|
|
|
ObjectFileELF::Terminate();
|
|
|
|
HostInfo::Terminate();
|
2018-11-01 05:49:27 +08:00
|
|
|
FileSystem::Terminate();
|
2016-05-25 18:48:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
static void VerifyDiskState(const FileSpec &cache_dir, const char *hostname) {
|
|
|
|
FileSpec uuid_view = GetUuidView(cache_dir);
|
2018-11-02 01:09:25 +08:00
|
|
|
EXPECT_TRUE(FileSystem::Instance().Exists(uuid_view))
|
|
|
|
<< "uuid_view is: " << uuid_view.GetCString();
|
2018-11-01 12:45:28 +08:00
|
|
|
EXPECT_EQ(module_size, FileSystem::Instance().GetByteSize(uuid_view));
|
2016-05-25 18:48:16 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
FileSpec sysroot_view = GetSysrootView(cache_dir, hostname);
|
2018-11-02 01:09:25 +08:00
|
|
|
EXPECT_TRUE(FileSystem::Instance().Exists(sysroot_view))
|
|
|
|
<< "sysroot_view is: " << sysroot_view.GetCString();
|
2018-11-01 12:45:28 +08:00
|
|
|
EXPECT_EQ(module_size, FileSystem::Instance().GetByteSize(sysroot_view));
|
2016-05-25 18:48:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void ModuleCacheTest::TryGetAndPut(const FileSpec &cache_dir,
|
|
|
|
const char *hostname, bool expect_download) {
|
|
|
|
ModuleCache mc;
|
|
|
|
ModuleSpec module_spec;
|
|
|
|
module_spec.GetFileSpec() = GetDummyRemotePath();
|
2018-06-21 23:24:39 +08:00
|
|
|
module_spec.GetUUID().SetFromStringRef(module_uuid, uuid_bytes);
|
2016-09-07 04:57:50 +08:00
|
|
|
module_spec.SetObjectSize(module_size);
|
|
|
|
ModuleSP module_sp;
|
|
|
|
bool did_create;
|
|
|
|
bool download_called = false;
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error = mc.GetAndPut(
|
2016-09-07 04:57:50 +08:00
|
|
|
cache_dir, hostname, module_spec,
|
2018-02-24 07:18:27 +08:00
|
|
|
[&download_called](const ModuleSpec &module_spec,
|
|
|
|
const FileSpec &tmp_download_file_spec) {
|
2016-09-07 04:57:50 +08:00
|
|
|
download_called = true;
|
|
|
|
EXPECT_STREQ(GetDummyRemotePath().GetCString(),
|
|
|
|
module_spec.GetFileSpec().GetCString());
|
|
|
|
std::error_code ec = llvm::sys::fs::copy_file(
|
|
|
|
s_test_executable, tmp_download_file_spec.GetCString());
|
|
|
|
EXPECT_FALSE(ec);
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status();
|
2016-09-07 04:57:50 +08:00
|
|
|
},
|
|
|
|
[](const ModuleSP &module_sp, const FileSpec &tmp_download_file_spec) {
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status("Not supported.");
|
2016-09-07 04:57:50 +08:00
|
|
|
},
|
|
|
|
module_sp, &did_create);
|
|
|
|
EXPECT_EQ(expect_download, download_called);
|
|
|
|
|
|
|
|
EXPECT_TRUE(error.Success()) << "Error was: " << error.AsCString();
|
|
|
|
EXPECT_TRUE(did_create);
|
|
|
|
ASSERT_TRUE(bool(module_sp));
|
|
|
|
|
|
|
|
SymbolContextList sc_list;
|
|
|
|
EXPECT_EQ(1u, module_sp->FindFunctionSymbols(ConstString("boom"),
|
|
|
|
eFunctionNameTypeFull, sc_list));
|
|
|
|
EXPECT_STREQ(GetDummyRemotePath().GetCString(),
|
|
|
|
module_sp->GetPlatformFileSpec().GetCString());
|
|
|
|
EXPECT_STREQ(module_uuid, module_sp->GetUUID().GetAsString().c_str());
|
2016-05-25 18:48:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
TEST_F(ModuleCacheTest, GetAndPut) {
|
|
|
|
FileSpec test_cache_dir = s_cache_dir;
|
|
|
|
test_cache_dir.AppendPathComponent("GetAndPut");
|
2016-05-25 18:48:16 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const bool expect_download = true;
|
|
|
|
TryGetAndPut(test_cache_dir, dummy_hostname, expect_download);
|
|
|
|
VerifyDiskState(test_cache_dir, dummy_hostname);
|
2016-05-25 18:48:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
TEST_F(ModuleCacheTest, GetAndPutUuidExists) {
|
|
|
|
FileSpec test_cache_dir = s_cache_dir;
|
|
|
|
test_cache_dir.AppendPathComponent("GetAndPutUuidExists");
|
2016-05-25 18:48:16 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
FileSpec uuid_view = GetUuidView(test_cache_dir);
|
|
|
|
std::error_code ec =
|
|
|
|
llvm::sys::fs::create_directories(uuid_view.GetDirectory().GetCString());
|
|
|
|
ASSERT_FALSE(ec);
|
|
|
|
ec = llvm::sys::fs::copy_file(s_test_executable, uuid_view.GetCString());
|
|
|
|
ASSERT_FALSE(ec);
|
2016-05-25 18:48:16 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const bool expect_download = false;
|
|
|
|
TryGetAndPut(test_cache_dir, dummy_hostname, expect_download);
|
|
|
|
VerifyDiskState(test_cache_dir, dummy_hostname);
|
2016-05-25 18:48:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
TEST_F(ModuleCacheTest, GetAndPutStrangeHostname) {
|
|
|
|
FileSpec test_cache_dir = s_cache_dir;
|
|
|
|
test_cache_dir.AppendPathComponent("GetAndPutStrangeHostname");
|
2016-05-25 18:48:16 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const bool expect_download = true;
|
|
|
|
TryGetAndPut(test_cache_dir, "tab\tcolon:asterisk*", expect_download);
|
|
|
|
VerifyDiskState(test_cache_dir, "tab_colon_asterisk_");
|
2016-05-25 18:48:16 +08:00
|
|
|
}
|