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" | 20 #include "base/files/file_path.h" |
21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "gtest/gtest.h" | 22 #include "gtest/gtest.h" |
23 #include "snapshot/win/process_reader_win.h" | 23 #include "snapshot/win/process_reader_win.h" |
24 #include "test/errors.h" | 24 #include "test/errors.h" |
25 #include "util/win/get_function.h" | 25 #include "util/win/get_function.h" |
26 #include "util/win/module_version.h" | 26 #include "util/win/module_version.h" |
| 27 #include "util/win/process_info.h" |
27 | 28 |
28 extern "C" IMAGE_DOS_HEADER __ImageBase; | 29 extern "C" IMAGE_DOS_HEADER __ImageBase; |
29 | 30 |
30 namespace crashpad { | 31 namespace crashpad { |
31 namespace test { | 32 namespace test { |
32 namespace { | 33 namespace { |
33 | 34 |
34 BOOL CrashpadGetModuleInformation(HANDLE process, | 35 BOOL CrashpadGetModuleInformation(HANDLE process, |
35 HMODULE module, | 36 HMODULE module, |
36 MODULEINFO* module_info, | 37 MODULEINFO* module_info, |
(...skipping 22 matching lines...) Expand all Loading... |
59 DWORD age; | 60 DWORD age; |
60 std::string pdbname; | 61 std::string pdbname; |
61 EXPECT_TRUE(pe_image_reader.DebugDirectoryInformation(&uuid, &age, &pdbname)); | 62 EXPECT_TRUE(pe_image_reader.DebugDirectoryInformation(&uuid, &age, &pdbname)); |
62 EXPECT_NE(std::string::npos, pdbname.find("crashpad_snapshot_test")); | 63 EXPECT_NE(std::string::npos, pdbname.find("crashpad_snapshot_test")); |
63 const std::string suffix(".pdb"); | 64 const std::string suffix(".pdb"); |
64 EXPECT_EQ( | 65 EXPECT_EQ( |
65 0, | 66 0, |
66 pdbname.compare(pdbname.size() - suffix.size(), suffix.size(), suffix)); | 67 pdbname.compare(pdbname.size() - suffix.size(), suffix.size(), suffix)); |
67 } | 68 } |
68 | 69 |
69 TEST(PEImageReader, VSFixedFileInfo) { | 70 void TestVSFixedFileInfo(ProcessReaderWin* process_reader, |
| 71 const ProcessInfo::Module& module, |
| 72 bool known_dll) { |
| 73 PEImageReader pe_image_reader; |
| 74 ASSERT_TRUE(pe_image_reader.Initialize(process_reader, |
| 75 module.dll_base, |
| 76 module.size, |
| 77 base::UTF16ToUTF8(module.name))); |
| 78 |
| 79 VS_FIXEDFILEINFO observed; |
| 80 const bool observed_rv = pe_image_reader.VSFixedFileInfo(&observed); |
| 81 ASSERT_TRUE(observed_rv || !known_dll); |
| 82 |
| 83 if (observed_rv) { |
| 84 EXPECT_EQ(VS_FFI_SIGNATURE, observed.dwSignature); |
| 85 EXPECT_EQ(VS_FFI_STRUCVERSION, observed.dwStrucVersion); |
| 86 EXPECT_EQ(0, observed.dwFileFlags & ~observed.dwFileFlagsMask); |
| 87 EXPECT_EQ(VOS_NT_WINDOWS32, observed.dwFileOS); |
| 88 if (known_dll) { |
| 89 EXPECT_EQ(VFT_DLL, observed.dwFileType); |
| 90 } else { |
| 91 EXPECT_TRUE(observed.dwFileType == VFT_APP || |
| 92 observed.dwFileType == VFT_DLL); |
| 93 } |
| 94 } |
| 95 |
| 96 // Use BaseName() to ensure that GetModuleVersionAndType() finds the |
| 97 // already-loaded module with the specified name. Otherwise, dwFileVersionMS |
| 98 // may not match. |
| 99 VS_FIXEDFILEINFO expected; |
| 100 const bool expected_rv = GetModuleVersionAndType( |
| 101 base::FilePath(module.name).BaseName(), &expected); |
| 102 ASSERT_TRUE(expected_rv || !known_dll); |
| 103 |
| 104 EXPECT_EQ(expected_rv, observed_rv); |
| 105 |
| 106 if (observed_rv && expected_rv) { |
| 107 EXPECT_EQ(expected.dwSignature, observed.dwSignature); |
| 108 EXPECT_EQ(expected.dwStrucVersion, observed.dwStrucVersion); |
| 109 EXPECT_EQ(expected.dwFileVersionMS, observed.dwFileVersionMS); |
| 110 EXPECT_EQ(expected.dwFileVersionLS, observed.dwFileVersionLS); |
| 111 EXPECT_EQ(expected.dwProductVersionMS, observed.dwProductVersionMS); |
| 112 EXPECT_EQ(expected.dwProductVersionLS, observed.dwProductVersionLS); |
| 113 EXPECT_EQ(expected.dwFileFlagsMask, observed.dwFileFlagsMask); |
| 114 EXPECT_EQ(expected.dwFileFlags, observed.dwFileFlags); |
| 115 EXPECT_EQ(expected.dwFileOS, observed.dwFileOS); |
| 116 EXPECT_EQ(expected.dwFileType, observed.dwFileType); |
| 117 EXPECT_EQ(expected.dwFileSubtype, observed.dwFileSubtype); |
| 118 EXPECT_EQ(expected.dwFileDateMS, observed.dwFileDateMS); |
| 119 EXPECT_EQ(expected.dwFileDateLS, observed.dwFileDateLS); |
| 120 } |
| 121 } |
| 122 |
| 123 TEST(PEImageReader, VSFixedFileInfo_OneModule) { |
70 ProcessReaderWin process_reader; | 124 ProcessReaderWin process_reader; |
71 ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), | 125 ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), |
72 ProcessSuspensionState::kRunning)); | 126 ProcessSuspensionState::kRunning)); |
73 | 127 |
74 const wchar_t kModuleName[] = L"kernel32.dll"; | 128 const wchar_t kModuleName[] = L"kernel32.dll"; |
75 | 129 const HMODULE module_handle = GetModuleHandle(kModuleName); |
76 HMODULE module_handle = GetModuleHandle(kModuleName); | |
77 ASSERT_TRUE(module_handle) << ErrorMessage("GetModuleHandle"); | 130 ASSERT_TRUE(module_handle) << ErrorMessage("GetModuleHandle"); |
78 | 131 |
79 MODULEINFO module_info; | 132 MODULEINFO module_info; |
80 ASSERT_TRUE(CrashpadGetModuleInformation( | 133 ASSERT_TRUE(CrashpadGetModuleInformation( |
81 GetCurrentProcess(), module_handle, &module_info, sizeof(module_info))) | 134 GetCurrentProcess(), module_handle, &module_info, sizeof(module_info))) |
82 << ErrorMessage("GetModuleInformation"); | 135 << ErrorMessage("GetModuleInformation"); |
83 EXPECT_EQ(module_handle, module_info.lpBaseOfDll); | 136 EXPECT_EQ(module_handle, module_info.lpBaseOfDll); |
84 | 137 |
85 PEImageReader pe_image_reader; | 138 ProcessInfo::Module module; |
86 ASSERT_TRUE( | 139 module.name = kModuleName; |
87 pe_image_reader.Initialize(&process_reader, | 140 module.dll_base = reinterpret_cast<WinVMAddress>(module_info.lpBaseOfDll); |
88 reinterpret_cast<WinVMAddress>(module_handle), | 141 module.size = module_info.SizeOfImage; |
89 module_info.SizeOfImage, | |
90 base::UTF16ToUTF8(kModuleName))); | |
91 | 142 |
92 VS_FIXEDFILEINFO observed; | 143 TestVSFixedFileInfo(&process_reader, module, true); |
93 ASSERT_TRUE(pe_image_reader.VSFixedFileInfo(&observed)); | 144 } |
94 | 145 |
95 EXPECT_EQ(VS_FFI_SIGNATURE, observed.dwSignature); | 146 TEST(PEImageReader, VSFixedFileInfo_AllModules) { |
96 EXPECT_EQ(VS_FFI_STRUCVERSION, observed.dwStrucVersion); | 147 ProcessReaderWin process_reader; |
97 EXPECT_EQ(0, observed.dwFileFlags & ~observed.dwFileFlagsMask); | 148 ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), |
98 EXPECT_EQ(VOS_NT_WINDOWS32, observed.dwFileOS); | 149 ProcessSuspensionState::kRunning)); |
99 EXPECT_EQ(VFT_DLL, observed.dwFileType); | |
100 | 150 |
101 VS_FIXEDFILEINFO expected; | 151 const std::vector<ProcessInfo::Module>& modules = process_reader.Modules(); |
102 ASSERT_TRUE(GetModuleVersionAndType(base::FilePath(kModuleName), &expected)); | 152 EXPECT_GT(modules.size(), 2u); |
103 | 153 |
104 EXPECT_EQ(expected.dwSignature, observed.dwSignature); | 154 for (const auto& module : modules) { |
105 EXPECT_EQ(expected.dwStrucVersion, observed.dwStrucVersion); | 155 SCOPED_TRACE(base::UTF16ToUTF8(module.name)); |
106 EXPECT_EQ(expected.dwFileVersionMS, observed.dwFileVersionMS); | 156 TestVSFixedFileInfo(&process_reader, module, false); |
107 EXPECT_EQ(expected.dwFileVersionLS, observed.dwFileVersionLS); | 157 } |
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 } | 158 } |
118 | 159 |
119 } // namespace | 160 } // namespace |
120 } // namespace test | 161 } // namespace test |
121 } // namespace crashpad | 162 } // namespace crashpad |
OLD | NEW |