| OLD | NEW |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "snapshot/win/pe_image_reader.h" | 15 #include "snapshot/win/pe_image_reader.h" |
| 16 | 16 |
| 17 #define PSAPI_VERSION 1 | 17 #define PSAPI_VERSION 1 |
| 18 #include <psapi.h> | 18 #include <psapi.h> |
| 19 | 19 |
| 20 #include "base/files/file_path.h" |
| 21 #include "base/strings/utf_string_conversions.h" |
| 20 #include "gtest/gtest.h" | 22 #include "gtest/gtest.h" |
| 21 #include "snapshot/win/process_reader_win.h" | 23 #include "snapshot/win/process_reader_win.h" |
| 24 #include "test/errors.h" |
| 22 #include "util/win/get_function.h" | 25 #include "util/win/get_function.h" |
| 26 #include "util/win/module_version.h" |
| 23 | 27 |
| 24 extern "C" IMAGE_DOS_HEADER __ImageBase; | 28 extern "C" IMAGE_DOS_HEADER __ImageBase; |
| 25 | 29 |
| 26 namespace crashpad { | 30 namespace crashpad { |
| 27 namespace test { | 31 namespace test { |
| 28 namespace { | 32 namespace { |
| 29 | 33 |
| 30 BOOL CrashpadGetModuleInformation(HANDLE process, | 34 BOOL CrashpadGetModuleInformation(HANDLE process, |
| 31 HMODULE module, | 35 HMODULE module, |
| 32 MODULEINFO* module_info, | 36 MODULEINFO* module_info, |
| 33 DWORD cb) { | 37 DWORD cb) { |
| 34 static const auto get_module_information = | 38 static const auto get_module_information = |
| 35 GET_FUNCTION_REQUIRED(L"psapi.dll", ::GetModuleInformation); | 39 GET_FUNCTION_REQUIRED(L"psapi.dll", ::GetModuleInformation); |
| 36 return get_module_information(process, module, module_info, cb); | 40 return get_module_information(process, module, module_info, cb); |
| 37 } | 41 } |
| 38 | 42 |
| 39 TEST(PEImageReader, DebugDirectory) { | 43 TEST(PEImageReader, DebugDirectory) { |
| 40 PEImageReader pe_image_reader; | 44 PEImageReader pe_image_reader; |
| 41 ProcessReaderWin process_reader; | 45 ProcessReaderWin process_reader; |
| 42 ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), | 46 ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), |
| 43 ProcessSuspensionState::kRunning)); | 47 ProcessSuspensionState::kRunning)); |
| 44 HMODULE self = reinterpret_cast<HMODULE>(&__ImageBase); | 48 HMODULE self = reinterpret_cast<HMODULE>(&__ImageBase); |
| 45 MODULEINFO module_info; | 49 MODULEINFO module_info; |
| 46 ASSERT_TRUE(CrashpadGetModuleInformation( | 50 ASSERT_TRUE(CrashpadGetModuleInformation( |
| 47 GetCurrentProcess(), self, &module_info, sizeof(module_info))); | 51 GetCurrentProcess(), self, &module_info, sizeof(module_info))) |
| 52 << ErrorMessage("GetModuleInformation"); |
| 48 EXPECT_EQ(self, module_info.lpBaseOfDll); | 53 EXPECT_EQ(self, module_info.lpBaseOfDll); |
| 49 EXPECT_TRUE(pe_image_reader.Initialize(&process_reader, | 54 ASSERT_TRUE(pe_image_reader.Initialize(&process_reader, |
| 50 reinterpret_cast<WinVMAddress>(self), | 55 reinterpret_cast<WinVMAddress>(self), |
| 51 module_info.SizeOfImage, | 56 module_info.SizeOfImage, |
| 52 "self")); | 57 "self")); |
| 53 UUID uuid; | 58 UUID uuid; |
| 54 DWORD age; | 59 DWORD age; |
| 55 std::string pdbname; | 60 std::string pdbname; |
| 56 EXPECT_TRUE(pe_image_reader.DebugDirectoryInformation(&uuid, &age, &pdbname)); | 61 EXPECT_TRUE(pe_image_reader.DebugDirectoryInformation(&uuid, &age, &pdbname)); |
| 57 EXPECT_NE(std::string::npos, pdbname.find("crashpad_snapshot_test")); | 62 EXPECT_NE(std::string::npos, pdbname.find("crashpad_snapshot_test")); |
| 58 const std::string suffix(".pdb"); | 63 const std::string suffix(".pdb"); |
| 59 EXPECT_EQ( | 64 EXPECT_EQ( |
| 60 0, | 65 0, |
| 61 pdbname.compare(pdbname.size() - suffix.size(), suffix.size(), suffix)); | 66 pdbname.compare(pdbname.size() - suffix.size(), suffix.size(), suffix)); |
| 62 } | 67 } |
| 63 | 68 |
| 69 TEST(PEImageReader, VSFixedFileInfo) { |
| 70 ProcessReaderWin process_reader; |
| 71 ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), |
| 72 ProcessSuspensionState::kRunning)); |
| 73 |
| 74 const wchar_t kModuleName[] = L"kernel32.dll"; |
| 75 |
| 76 HMODULE module_handle = GetModuleHandle(kModuleName); |
| 77 ASSERT_TRUE(module_handle) << ErrorMessage("GetModuleHandle"); |
| 78 |
| 79 MODULEINFO module_info; |
| 80 ASSERT_TRUE(CrashpadGetModuleInformation( |
| 81 GetCurrentProcess(), module_handle, &module_info, sizeof(module_info))) |
| 82 << ErrorMessage("GetModuleInformation"); |
| 83 EXPECT_EQ(module_handle, module_info.lpBaseOfDll); |
| 84 |
| 85 PEImageReader pe_image_reader; |
| 86 ASSERT_TRUE( |
| 87 pe_image_reader.Initialize(&process_reader, |
| 88 reinterpret_cast<WinVMAddress>(module_handle), |
| 89 module_info.SizeOfImage, |
| 90 base::UTF16ToUTF8(kModuleName))); |
| 91 |
| 92 VS_FIXEDFILEINFO observed; |
| 93 ASSERT_TRUE(pe_image_reader.VSFixedFileInfo(&observed)); |
| 94 |
| 95 EXPECT_EQ(VS_FFI_SIGNATURE, observed.dwSignature); |
| 96 EXPECT_EQ(VS_FFI_STRUCVERSION, observed.dwStrucVersion); |
| 97 EXPECT_EQ(0, observed.dwFileFlags & ~observed.dwFileFlagsMask); |
| 98 EXPECT_EQ(VOS_NT_WINDOWS32, observed.dwFileOS); |
| 99 EXPECT_EQ(VFT_DLL, observed.dwFileType); |
| 100 |
| 101 VS_FIXEDFILEINFO expected; |
| 102 ASSERT_TRUE(GetModuleVersionAndType(base::FilePath(kModuleName), &expected)); |
| 103 |
| 104 EXPECT_EQ(expected.dwSignature, observed.dwSignature); |
| 105 EXPECT_EQ(expected.dwStrucVersion, observed.dwStrucVersion); |
| 106 EXPECT_EQ(expected.dwFileVersionMS, observed.dwFileVersionMS); |
| 107 EXPECT_EQ(expected.dwFileVersionLS, observed.dwFileVersionLS); |
| 108 EXPECT_EQ(expected.dwProductVersionMS, observed.dwProductVersionMS); |
| 109 EXPECT_EQ(expected.dwProductVersionLS, observed.dwProductVersionLS); |
| 110 EXPECT_EQ(expected.dwFileFlagsMask, observed.dwFileFlagsMask); |
| 111 EXPECT_EQ(expected.dwFileFlags, observed.dwFileFlags); |
| 112 EXPECT_EQ(expected.dwFileOS, observed.dwFileOS); |
| 113 EXPECT_EQ(expected.dwFileType, observed.dwFileType); |
| 114 EXPECT_EQ(expected.dwFileSubtype, observed.dwFileSubtype); |
| 115 EXPECT_EQ(expected.dwFileDateMS, observed.dwFileDateMS); |
| 116 EXPECT_EQ(expected.dwFileDateLS, observed.dwFileDateLS); |
| 117 } |
| 118 |
| 64 } // namespace | 119 } // namespace |
| 65 } // namespace test | 120 } // namespace test |
| 66 } // namespace crashpad | 121 } // namespace crashpad |
| OLD | NEW |