diff --git a/snapshot/win/pe_image_reader_test.cc b/snapshot/win/pe_image_reader_test.cc index c4aa5f0c..ac456e2d 100644 --- a/snapshot/win/pe_image_reader_test.cc +++ b/snapshot/win/pe_image_reader_test.cc @@ -23,42 +23,52 @@ #include "gtest/gtest.h" #include "snapshot/win/process_reader_win.h" #include "test/errors.h" +#include "test/scoped_module_handle.h" #include "test/test_paths.h" #include "util/misc/from_pointer_cast.h" #include "util/win/get_module_information.h" #include "util/win/module_version.h" #include "util/win/process_info.h" -extern "C" IMAGE_DOS_HEADER __ImageBase; - namespace crashpad { namespace test { namespace { TEST(PEImageReader, DebugDirectory) { + base::FilePath module_path = + TestPaths::BuildArtifact(L"snapshot", + L"image_reader_module", + TestPaths::FileType::kLoadableModule); + ScopedModuleHandle module_handle(LoadLibrary(module_path.value().c_str())); + ASSERT_TRUE(module_handle.valid()) << ErrorMessage("LoadLibrary"); + PEImageReader pe_image_reader; ProcessReaderWin process_reader; ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), ProcessSuspensionState::kRunning)); - HMODULE self = reinterpret_cast(&__ImageBase); + MODULEINFO module_info; - ASSERT_TRUE(CrashpadGetModuleInformation( - GetCurrentProcess(), self, &module_info, sizeof(module_info))) + ASSERT_TRUE(CrashpadGetModuleInformation(GetCurrentProcess(), + module_handle.get(), + &module_info, + sizeof(module_info))) << ErrorMessage("GetModuleInformation"); - EXPECT_EQ(module_info.lpBaseOfDll, self); - ASSERT_TRUE(pe_image_reader.Initialize(&process_reader, - FromPointerCast(self), - module_info.SizeOfImage, - "self")); + EXPECT_EQ(module_info.lpBaseOfDll, module_handle.get()); + + base::FilePath module_basename = module_path.BaseName(); + ASSERT_TRUE(pe_image_reader.Initialize( + &process_reader, + FromPointerCast(module_handle.get()), + module_info.SizeOfImage, + base::UTF16ToUTF8(module_basename.value()))); + UUID uuid; DWORD age; std::string pdbname; ASSERT_TRUE(pe_image_reader.DebugDirectoryInformation(&uuid, &age, &pdbname)); - std::string self_name = base::UTF16ToUTF8( - TestPaths::ExpectedExecutableBasename(L"crashpad_snapshot_test") - .RemoveFinalExtension() - .value()); - EXPECT_NE(pdbname.find(self_name), std::string::npos); + std::string module_name = + base::UTF16ToUTF8(module_basename.RemoveFinalExtension().value()); + EXPECT_NE(pdbname.find(module_name), std::string::npos); const std::string suffix(".pdb"); EXPECT_EQ( pdbname.compare(pdbname.size() - suffix.size(), suffix.size(), suffix), diff --git a/test/scoped_module_handle.h b/test/scoped_module_handle.h index 3863cad3..20b5e153 100644 --- a/test/scoped_module_handle.h +++ b/test/scoped_module_handle.h @@ -59,6 +59,9 @@ class ScopedModuleHandle { explicit ScopedModuleHandle(ModuleHandle handle); ~ScopedModuleHandle(); + //! \return The module handle being managed. + ModuleHandle get() const { return handle_; } + //! \return `true` if this object manages a valid loadable module handle. bool valid() const { return handle_ != nullptr; }