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, it may load a | |
98 // different file than the one that’s already resident, which may have | |
99 // different VS_FIXEDFILEINFO information. | |
100 VS_FIXEDFILEINFO expected; | |
101 const bool expected_rv = GetModuleVersionAndType( | |
102 base::FilePath(module.name).BaseName(), &expected); | |
Mark Mentovai
2015/12/01 23:42:54
This was really weird. Without the BaseName(), I g
scottmg
2015/12/01 23:57:38
I don't know, but because it's reporting 8.0, I wo
| |
103 ASSERT_TRUE(expected_rv || !known_dll); | |
104 | |
105 EXPECT_EQ(expected_rv, observed_rv); | |
106 | |
107 if (observed_rv && expected_rv) { | |
108 EXPECT_EQ(expected.dwSignature, observed.dwSignature); | |
109 EXPECT_EQ(expected.dwStrucVersion, observed.dwStrucVersion); | |
110 EXPECT_EQ(expected.dwFileVersionMS, observed.dwFileVersionMS); | |
111 EXPECT_EQ(expected.dwFileVersionLS, observed.dwFileVersionLS); | |
112 EXPECT_EQ(expected.dwProductVersionMS, observed.dwProductVersionMS); | |
113 EXPECT_EQ(expected.dwProductVersionLS, observed.dwProductVersionLS); | |
114 EXPECT_EQ(expected.dwFileFlagsMask, observed.dwFileFlagsMask); | |
115 EXPECT_EQ(expected.dwFileFlags, observed.dwFileFlags); | |
116 EXPECT_EQ(expected.dwFileOS, observed.dwFileOS); | |
117 EXPECT_EQ(expected.dwFileType, observed.dwFileType); | |
118 EXPECT_EQ(expected.dwFileSubtype, observed.dwFileSubtype); | |
119 EXPECT_EQ(expected.dwFileDateMS, observed.dwFileDateMS); | |
120 EXPECT_EQ(expected.dwFileDateLS, observed.dwFileDateLS); | |
121 } | |
122 } | |
123 | |
124 TEST(PEImageReader, VSFixedFileInfo_OneModule) { | |
70 ProcessReaderWin process_reader; | 125 ProcessReaderWin process_reader; |
71 ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), | 126 ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), |
72 ProcessSuspensionState::kRunning)); | 127 ProcessSuspensionState::kRunning)); |
73 | 128 |
74 const wchar_t kModuleName[] = L"kernel32.dll"; | 129 const wchar_t kModuleName[] = L"kernel32.dll"; |
75 | 130 const HMODULE module_handle = GetModuleHandle(kModuleName); |
76 HMODULE module_handle = GetModuleHandle(kModuleName); | |
77 ASSERT_TRUE(module_handle) << ErrorMessage("GetModuleHandle"); | 131 ASSERT_TRUE(module_handle) << ErrorMessage("GetModuleHandle"); |
78 | 132 |
79 MODULEINFO module_info; | 133 MODULEINFO module_info; |
80 ASSERT_TRUE(CrashpadGetModuleInformation( | 134 ASSERT_TRUE(CrashpadGetModuleInformation( |
81 GetCurrentProcess(), module_handle, &module_info, sizeof(module_info))) | 135 GetCurrentProcess(), module_handle, &module_info, sizeof(module_info))) |
82 << ErrorMessage("GetModuleInformation"); | 136 << ErrorMessage("GetModuleInformation"); |
83 EXPECT_EQ(module_handle, module_info.lpBaseOfDll); | 137 EXPECT_EQ(module_handle, module_info.lpBaseOfDll); |
84 | 138 |
85 PEImageReader pe_image_reader; | 139 ProcessInfo::Module module; |
86 ASSERT_TRUE( | 140 module.name = kModuleName; |
87 pe_image_reader.Initialize(&process_reader, | 141 module.dll_base = reinterpret_cast<WinVMAddress>(module_info.lpBaseOfDll); |
88 reinterpret_cast<WinVMAddress>(module_handle), | 142 module.size = module_info.SizeOfImage; |
89 module_info.SizeOfImage, | |
90 base::UTF16ToUTF8(kModuleName))); | |
91 | 143 |
92 VS_FIXEDFILEINFO observed; | 144 TestVSFixedFileInfo(&process_reader, module, true); |
93 ASSERT_TRUE(pe_image_reader.VSFixedFileInfo(&observed)); | 145 } |
94 | 146 |
95 EXPECT_EQ(VS_FFI_SIGNATURE, observed.dwSignature); | 147 TEST(PEImageReader, VSFixedFileInfo_AllModules) { |
96 EXPECT_EQ(VS_FFI_STRUCVERSION, observed.dwStrucVersion); | 148 ProcessReaderWin process_reader; |
97 EXPECT_EQ(0, observed.dwFileFlags & ~observed.dwFileFlagsMask); | 149 ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), |
98 EXPECT_EQ(VOS_NT_WINDOWS32, observed.dwFileOS); | 150 ProcessSuspensionState::kRunning)); |
99 EXPECT_EQ(VFT_DLL, observed.dwFileType); | |
100 | 151 |
101 VS_FIXEDFILEINFO expected; | 152 const std::vector<ProcessInfo::Module>& modules = process_reader.Modules(); |
102 ASSERT_TRUE(GetModuleVersionAndType(base::FilePath(kModuleName), &expected)); | 153 EXPECT_GT(modules.size(), 2u); |
103 | 154 |
104 EXPECT_EQ(expected.dwSignature, observed.dwSignature); | 155 for (const auto& module : modules) { |
105 EXPECT_EQ(expected.dwStrucVersion, observed.dwStrucVersion); | 156 SCOPED_TRACE(base::UTF16ToUTF8(module.name)); |
106 EXPECT_EQ(expected.dwFileVersionMS, observed.dwFileVersionMS); | 157 TestVSFixedFileInfo(&process_reader, module, false); |
107 EXPECT_EQ(expected.dwFileVersionLS, observed.dwFileVersionLS); | 158 } |
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 } | 159 } |
118 | 160 |
119 } // namespace | 161 } // namespace |
120 } // namespace test | 162 } // namespace test |
121 } // namespace crashpad | 163 } // namespace crashpad |
OLD | NEW |