OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file contains unit tests for PEImage. | 5 // This file contains unit tests for PEImage. |
| 6 #include <algorithm> |
| 7 #include <vector> |
6 | 8 |
7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "base/win/pe_image.h" | 10 #include "base/win/pe_image.h" |
9 #include "base/win/windows_version.h" | 11 #include "base/win/windows_version.h" |
10 | 12 |
| 13 namespace { |
| 14 |
| 15 class Expectations { |
| 16 public: |
| 17 enum Value { |
| 18 SECTIONS = 0, |
| 19 IMPORTS_DLLS, |
| 20 DELAY_DLLS, |
| 21 EXPORTS, |
| 22 IMPORTS, |
| 23 DELAY_IMPORTS, |
| 24 RELOCS |
| 25 }; |
| 26 |
| 27 enum Arch { |
| 28 ARCH_X86 = 0, |
| 29 ARCH_X64, |
| 30 ARCH_ALL |
| 31 }; |
| 32 |
| 33 Expectations(); |
| 34 |
| 35 void SetDefault(Value value, int count); |
| 36 void SetOverride(Value value, base::win::Version version, |
| 37 Arch arch, int count); |
| 38 void SetOverride(Value value, base::win::Version version, int count); |
| 39 void SetOverride(Value value, Arch arch, int count); |
| 40 |
| 41 // returns -1 on failure. |
| 42 int GetExpectation(Value value); |
| 43 |
| 44 private: |
| 45 class Override { |
| 46 public: |
| 47 enum MatchType { MATCH_VERSION, MATCH_ARCH, MATCH_BOTH, MATCH_NONE }; |
| 48 |
| 49 Override(Value value, base::win::Version version, Arch arch, int count) |
| 50 : value_(value), version_(version), arch_(arch), count_(count) { |
| 51 }; |
| 52 |
| 53 bool Matches(Value value, base::win::Version version, |
| 54 Arch arch, MatchType type) { |
| 55 if (value_ != value) |
| 56 return false; |
| 57 |
| 58 switch (type) { |
| 59 case MATCH_BOTH: |
| 60 return (arch == arch_ && version == version_); |
| 61 case MATCH_ARCH: |
| 62 return (arch == arch_ && version_ == base::win::VERSION_WIN_LAST); |
| 63 case MATCH_VERSION: |
| 64 return (arch_ == ARCH_ALL && version == version_); |
| 65 case MATCH_NONE: |
| 66 return (arch_ == ARCH_ALL && version_ == base::win::VERSION_WIN_LAST); |
| 67 } |
| 68 return false; |
| 69 } |
| 70 |
| 71 int GetCount() { return count_; } |
| 72 |
| 73 private: |
| 74 Value value_; |
| 75 base::win::Version version_; |
| 76 Arch arch_; |
| 77 int count_; |
| 78 }; |
| 79 |
| 80 bool MatchesMyArch(Arch arch); |
| 81 |
| 82 std::vector<Override> overrides_; |
| 83 Arch my_arch_; |
| 84 base::win::Version my_version_; |
| 85 }; |
| 86 |
| 87 Expectations::Expectations() { |
| 88 my_version_ = base::win::GetVersion(); |
| 89 #if defined(ARCH_CPU_64_BITS) |
| 90 my_arch_ = ARCH_X64; |
| 91 #else |
| 92 my_arch_ = ARCH_X86; |
| 93 #endif |
| 94 } |
| 95 |
| 96 int Expectations::GetExpectation(Value value) { |
| 97 // Prefer OS version specificity over Arch specificity. |
| 98 for (auto type : { Override::MATCH_BOTH, |
| 99 Override::MATCH_VERSION, |
| 100 Override::MATCH_ARCH, |
| 101 Override::MATCH_NONE }) { |
| 102 for (auto override : overrides_) { |
| 103 if (override.Matches(value, my_version_, my_arch_, type)) |
| 104 return override.GetCount(); |
| 105 } |
| 106 } |
| 107 return -1; |
| 108 } |
| 109 |
| 110 void Expectations::SetDefault(Value value, int count) { |
| 111 SetOverride(value, base::win::VERSION_WIN_LAST, ARCH_ALL, count); |
| 112 } |
| 113 |
| 114 void Expectations::SetOverride(Value value, |
| 115 base::win::Version version, |
| 116 Arch arch, |
| 117 int count) { |
| 118 overrides_.push_back(Override(value, version, arch, count)); |
| 119 } |
| 120 |
| 121 void Expectations::SetOverride(Value value, |
| 122 base::win::Version version, |
| 123 int count) { |
| 124 SetOverride(value, version, ARCH_ALL, count); |
| 125 } |
| 126 |
| 127 void Expectations::SetOverride(Value value, Arch arch, int count) { |
| 128 SetOverride(value, base::win::VERSION_WIN_LAST, arch, count); |
| 129 } |
| 130 |
| 131 } // namespace |
| 132 |
11 namespace base { | 133 namespace base { |
12 namespace win { | 134 namespace win { |
13 | 135 |
14 // Just counts the number of invocations. | 136 namespace { |
15 bool ExportsCallback(const PEImage &image, | |
16 DWORD ordinal, | |
17 DWORD hint, | |
18 LPCSTR name, | |
19 PVOID function, | |
20 LPCSTR forward, | |
21 PVOID cookie) { | |
22 int* count = reinterpret_cast<int*>(cookie); | |
23 (*count)++; | |
24 return true; | |
25 } | |
26 | 137 |
27 // Just counts the number of invocations. | 138 // Just counts the number of invocations. |
28 bool ImportsCallback(const PEImage &image, | 139 bool ImportsCallback(const PEImage& image, |
29 LPCSTR module, | 140 LPCSTR module, |
30 DWORD ordinal, | 141 DWORD ordinal, |
31 LPCSTR name, | 142 LPCSTR name, |
32 DWORD hint, | 143 DWORD hint, |
33 PIMAGE_THUNK_DATA iat, | 144 PIMAGE_THUNK_DATA iat, |
34 PVOID cookie) { | 145 PVOID cookie) { |
35 int* count = reinterpret_cast<int*>(cookie); | 146 int* count = reinterpret_cast<int*>(cookie); |
36 (*count)++; | 147 (*count)++; |
37 return true; | 148 return true; |
38 } | 149 } |
39 | 150 |
40 // Just counts the number of invocations. | 151 // Just counts the number of invocations. |
41 bool SectionsCallback(const PEImage &image, | 152 bool SectionsCallback(const PEImage& image, |
42 PIMAGE_SECTION_HEADER header, | 153 PIMAGE_SECTION_HEADER header, |
43 PVOID section_start, | 154 PVOID section_start, |
44 DWORD section_size, | 155 DWORD section_size, |
45 PVOID cookie) { | 156 PVOID cookie) { |
46 int* count = reinterpret_cast<int*>(cookie); | 157 int* count = reinterpret_cast<int*>(cookie); |
47 (*count)++; | 158 (*count)++; |
48 return true; | 159 return true; |
49 } | 160 } |
50 | 161 |
51 // Just counts the number of invocations. | 162 // Just counts the number of invocations. |
52 bool RelocsCallback(const PEImage &image, | 163 bool RelocsCallback(const PEImage& image, |
53 WORD type, | 164 WORD type, |
54 PVOID address, | 165 PVOID address, |
55 PVOID cookie) { | 166 PVOID cookie) { |
56 int* count = reinterpret_cast<int*>(cookie); | 167 int* count = reinterpret_cast<int*>(cookie); |
57 (*count)++; | 168 (*count)++; |
58 return true; | 169 return true; |
59 } | 170 } |
60 | 171 |
61 // Just counts the number of invocations. | 172 // Just counts the number of invocations. |
62 bool ImportChunksCallback(const PEImage &image, | 173 bool ImportChunksCallback(const PEImage& image, |
63 LPCSTR module, | 174 LPCSTR module, |
64 PIMAGE_THUNK_DATA name_table, | 175 PIMAGE_THUNK_DATA name_table, |
65 PIMAGE_THUNK_DATA iat, | 176 PIMAGE_THUNK_DATA iat, |
66 PVOID cookie) { | 177 PVOID cookie) { |
67 int* count = reinterpret_cast<int*>(cookie); | 178 int* count = reinterpret_cast<int*>(cookie); |
68 (*count)++; | 179 (*count)++; |
69 return true; | 180 return true; |
70 } | 181 } |
71 | 182 |
72 // Just counts the number of invocations. | 183 // Just counts the number of invocations. |
73 bool DelayImportChunksCallback(const PEImage &image, | 184 bool DelayImportChunksCallback(const PEImage& image, |
74 PImgDelayDescr delay_descriptor, | 185 PImgDelayDescr delay_descriptor, |
75 LPCSTR module, | 186 LPCSTR module, |
76 PIMAGE_THUNK_DATA name_table, | 187 PIMAGE_THUNK_DATA name_table, |
77 PIMAGE_THUNK_DATA iat, | 188 PIMAGE_THUNK_DATA iat, |
78 PIMAGE_THUNK_DATA bound_iat, | 189 PIMAGE_THUNK_DATA bound_iat, |
79 PIMAGE_THUNK_DATA unload_iat, | 190 PIMAGE_THUNK_DATA unload_iat, |
80 PVOID cookie) { | 191 PVOID cookie) { |
81 int* count = reinterpret_cast<int*>(cookie); | 192 int* count = reinterpret_cast<int*>(cookie); |
82 (*count)++; | 193 (*count)++; |
83 return true; | 194 return true; |
84 } | 195 } |
85 | 196 |
86 // Identifiers for the set of supported expectations. | 197 // Just counts the number of invocations. |
87 enum ExpectationSet { | 198 bool ExportsCallback(const PEImage& image, |
88 WIN_2K_SET, | 199 DWORD ordinal, |
89 WIN_XP_SET, | 200 DWORD hint, |
90 WIN_VISTA_SET, | 201 LPCSTR name, |
91 WIN_7_SET, | 202 PVOID function, |
92 WIN_8_SET, | 203 LPCSTR forward, |
93 UNSUPPORTED_SET, | 204 PVOID cookie) { |
94 }; | 205 int* count = reinterpret_cast<int*>(cookie); |
95 | 206 (*count)++; |
96 // We'll be using some known values for the tests. | 207 return true; |
97 enum Value { | |
98 sections = 0, | |
99 imports_dlls, | |
100 delay_dlls, | |
101 exports, | |
102 imports, | |
103 delay_imports, | |
104 relocs | |
105 }; | |
106 | |
107 ExpectationSet GetExpectationSet(DWORD os) { | |
108 if (os == 50) | |
109 return WIN_2K_SET; | |
110 if (os == 51) | |
111 return WIN_XP_SET; | |
112 if (os == 60) | |
113 return WIN_VISTA_SET; | |
114 if (os == 61) | |
115 return WIN_7_SET; | |
116 if (os >= 62) | |
117 return WIN_8_SET; | |
118 return UNSUPPORTED_SET; | |
119 } | 208 } |
120 | 209 |
121 // Retrieves the expected value from advapi32.dll based on the OS. | 210 } // namespace |
122 int GetExpectedValue(Value value, DWORD os) { | |
123 const int xp_delay_dlls = 2; | |
124 const int xp_exports = 675; | |
125 const int xp_imports = 422; | |
126 const int xp_delay_imports = 8; | |
127 const int xp_relocs = 9180; | |
128 const int vista_delay_dlls = 4; | |
129 const int vista_exports = 799; | |
130 const int vista_imports = 476; | |
131 const int vista_delay_imports = 24; | |
132 const int vista_relocs = 10188; | |
133 const int w2k_delay_dlls = 0; | |
134 const int w2k_exports = 566; | |
135 const int w2k_imports = 357; | |
136 const int w2k_delay_imports = 0; | |
137 const int w2k_relocs = 7388; | |
138 const int win7_delay_dlls = 7; | |
139 const int win7_exports = 806; | |
140 const int win7_imports = 568; | |
141 const int win7_delay_imports = 71; | |
142 int win7_relocs = 7812; | |
143 int win7_sections = 4; | |
144 const int win8_delay_dlls = 9; | |
145 const int win8_exports = 806; | |
146 const int win8_imports = 568; | |
147 const int win8_delay_imports = 113; | |
148 const int win8_relocs = 9478; | |
149 int win8_sections = 4; | |
150 int win8_import_dlls = 17; | |
151 | |
152 base::win::OSInfo* os_info = base::win::OSInfo::GetInstance(); | |
153 // 32-bit process on a 32-bit system. | |
154 if (os_info->architecture() == base::win::OSInfo::X86_ARCHITECTURE) { | |
155 win8_sections = 5; | |
156 win8_import_dlls = 19; | |
157 | |
158 // 64-bit process on a 64-bit system. | |
159 } else if (os_info->wow64_status() == base::win::OSInfo::WOW64_DISABLED) { | |
160 win7_sections = 6; | |
161 win7_relocs = 2712; | |
162 } | |
163 | |
164 // Contains the expected value, for each enumerated property (Value), and the | |
165 // OS version: [Value][os_version] | |
166 const int expected[][5] = { | |
167 {4, 4, 4, win7_sections, win8_sections}, | |
168 {3, 3, 3, 13, win8_import_dlls}, | |
169 {w2k_delay_dlls, xp_delay_dlls, vista_delay_dlls, win7_delay_dlls, | |
170 win8_delay_dlls}, | |
171 {w2k_exports, xp_exports, vista_exports, win7_exports, win8_exports}, | |
172 {w2k_imports, xp_imports, vista_imports, win7_imports, win8_imports}, | |
173 {w2k_delay_imports, xp_delay_imports, | |
174 vista_delay_imports, win7_delay_imports, win8_delay_imports}, | |
175 {w2k_relocs, xp_relocs, vista_relocs, win7_relocs, win8_relocs} | |
176 }; | |
177 COMPILE_ASSERT(arraysize(expected[0]) == UNSUPPORTED_SET, | |
178 expected_value_set_mismatch); | |
179 | |
180 if (value > relocs) | |
181 return 0; | |
182 ExpectationSet expected_set = GetExpectationSet(os); | |
183 if (expected_set >= arraysize(expected)) { | |
184 // This should never happen. Log a failure if it does. | |
185 EXPECT_NE(UNSUPPORTED_SET, expected_set); | |
186 expected_set = WIN_2K_SET; | |
187 } | |
188 | |
189 return expected[value][expected_set]; | |
190 } | |
191 | |
192 | |
193 // TODO(jschuh): crbug.com/167707 Need to fix test on Win64 bots | |
194 #if defined(OS_WIN) && defined(ARCH_CPU_X86_64) | |
195 #define MAYBE_EnumeratesPE DISABLED_EnumeratesPE | |
196 #else | |
197 #define MAYBE_EnumeratesPE EnumeratesPE | |
198 #endif | |
199 | 211 |
200 // Tests that we are able to enumerate stuff from a PE file, and that | 212 // Tests that we are able to enumerate stuff from a PE file, and that |
201 // the actual number of items found is within the expected range. | 213 // the actual number of items found is within the expected range. |
202 TEST(PEImageTest, MAYBE_EnumeratesPE) { | 214 TEST(PEImageTest, EnumeratesPE) { |
203 HMODULE module = LoadLibrary(L"advapi32.dll"); | 215 Expectations expectations; |
| 216 |
| 217 #ifndef NDEBUG |
| 218 // Default Debug expectations. |
| 219 expectations.SetDefault(Expectations::SECTIONS, 7); |
| 220 expectations.SetDefault(Expectations::IMPORTS_DLLS, 3); |
| 221 expectations.SetDefault(Expectations::DELAY_DLLS, 2); |
| 222 expectations.SetDefault(Expectations::EXPORTS, 2); |
| 223 expectations.SetDefault(Expectations::IMPORTS, 49); |
| 224 expectations.SetDefault(Expectations::DELAY_IMPORTS, 2); |
| 225 expectations.SetDefault(Expectations::RELOCS, 438); |
| 226 |
| 227 // 64-bit Debug expectations. |
| 228 expectations.SetOverride(Expectations::SECTIONS, Expectations::ARCH_X64, 8); |
| 229 expectations.SetOverride(Expectations::IMPORTS, Expectations::ARCH_X64, 69); |
| 230 expectations.SetOverride(Expectations::RELOCS, Expectations::ARCH_X64, 632); |
| 231 #else |
| 232 // Default Release expectations. |
| 233 expectations.SetDefault(Expectations::SECTIONS, 5); |
| 234 expectations.SetDefault(Expectations::IMPORTS_DLLS, 2); |
| 235 expectations.SetDefault(Expectations::DELAY_DLLS, 2); |
| 236 expectations.SetDefault(Expectations::EXPORTS, 2); |
| 237 expectations.SetDefault(Expectations::IMPORTS, 66); |
| 238 expectations.SetDefault(Expectations::DELAY_IMPORTS, 2); |
| 239 expectations.SetDefault(Expectations::RELOCS, 1586); |
| 240 |
| 241 // 64-bit Release expectations. |
| 242 expectations.SetOverride(Expectations::SECTIONS, Expectations::ARCH_X64, 6); |
| 243 expectations.SetOverride(Expectations::IMPORTS, Expectations::ARCH_X64, 69); |
| 244 expectations.SetOverride(Expectations::RELOCS, Expectations::ARCH_X64, 632); |
| 245 #endif |
| 246 |
| 247 HMODULE module = LoadLibrary(L"pe_image_test.dll"); |
204 ASSERT_TRUE(NULL != module); | 248 ASSERT_TRUE(NULL != module); |
205 | 249 |
206 PEImage pe(module); | 250 PEImage pe(module); |
207 int count = 0; | 251 int count = 0; |
208 EXPECT_TRUE(pe.VerifyMagic()); | 252 EXPECT_TRUE(pe.VerifyMagic()); |
209 | 253 |
210 DWORD os = pe.GetNTHeaders()->OptionalHeader.MajorOperatingSystemVersion; | |
211 os = os * 10 + pe.GetNTHeaders()->OptionalHeader.MinorOperatingSystemVersion; | |
212 | |
213 // Skip this test for unsupported OS versions. | |
214 if (GetExpectationSet(os) == UNSUPPORTED_SET) | |
215 return; | |
216 | |
217 pe.EnumSections(SectionsCallback, &count); | 254 pe.EnumSections(SectionsCallback, &count); |
218 EXPECT_EQ(GetExpectedValue(sections, os), count); | 255 EXPECT_EQ(expectations.GetExpectation(Expectations::SECTIONS), count); |
219 | 256 |
220 count = 0; | 257 count = 0; |
221 pe.EnumImportChunks(ImportChunksCallback, &count); | 258 pe.EnumImportChunks(ImportChunksCallback, &count); |
222 EXPECT_EQ(GetExpectedValue(imports_dlls, os), count); | 259 EXPECT_EQ(expectations.GetExpectation(Expectations::IMPORTS_DLLS), count); |
223 | 260 |
224 count = 0; | 261 count = 0; |
225 pe.EnumDelayImportChunks(DelayImportChunksCallback, &count); | 262 pe.EnumDelayImportChunks(DelayImportChunksCallback, &count); |
226 EXPECT_EQ(GetExpectedValue(delay_dlls, os), count); | 263 EXPECT_EQ(expectations.GetExpectation(Expectations::DELAY_DLLS), count); |
227 | 264 |
228 count = 0; | 265 count = 0; |
229 pe.EnumExports(ExportsCallback, &count); | 266 pe.EnumExports(ExportsCallback, &count); |
230 EXPECT_GT(count, GetExpectedValue(exports, os) - 20); | 267 EXPECT_EQ(expectations.GetExpectation(Expectations::EXPORTS), count); |
231 EXPECT_LT(count, GetExpectedValue(exports, os) + 100); | |
232 | 268 |
233 count = 0; | 269 count = 0; |
234 pe.EnumAllImports(ImportsCallback, &count); | 270 pe.EnumAllImports(ImportsCallback, &count); |
235 EXPECT_GT(count, GetExpectedValue(imports, os) - 20); | 271 EXPECT_EQ(expectations.GetExpectation(Expectations::IMPORTS), count); |
236 EXPECT_LT(count, GetExpectedValue(imports, os) + 100); | |
237 | 272 |
238 count = 0; | 273 count = 0; |
239 pe.EnumAllDelayImports(ImportsCallback, &count); | 274 pe.EnumAllDelayImports(ImportsCallback, &count); |
240 EXPECT_GT(count, GetExpectedValue(delay_imports, os) - 2); | 275 EXPECT_EQ(expectations.GetExpectation(Expectations::DELAY_IMPORTS), count); |
241 EXPECT_LT(count, GetExpectedValue(delay_imports, os) + 8); | |
242 | 276 |
243 count = 0; | 277 count = 0; |
244 pe.EnumRelocs(RelocsCallback, &count); | 278 pe.EnumRelocs(RelocsCallback, &count); |
245 EXPECT_GT(count, GetExpectedValue(relocs, os) - 150); | 279 EXPECT_EQ(expectations.GetExpectation(Expectations::RELOCS), count); |
246 EXPECT_LT(count, GetExpectedValue(relocs, os) + 1500); | |
247 | 280 |
248 FreeLibrary(module); | 281 FreeLibrary(module); |
249 } | 282 } |
250 | 283 |
251 // Tests that we can locate an specific exported symbol, by name and by ordinal. | 284 // Tests that we can locate an specific exported symbol, by name and by ordinal. |
252 TEST(PEImageTest, RetrievesExports) { | 285 TEST(PEImageTest, RetrievesExports) { |
253 HMODULE module = LoadLibrary(L"advapi32.dll"); | 286 HMODULE module = LoadLibrary(L"advapi32.dll"); |
254 ASSERT_TRUE(NULL != module); | 287 ASSERT_TRUE(NULL != module); |
255 | 288 |
256 PEImage pe(module); | 289 PEImage pe(module); |
(...skipping 21 matching lines...) Expand all Loading... |
278 EXPECT_TRUE(pe.GetDebugId(&guid, &age)); | 311 EXPECT_TRUE(pe.GetDebugId(&guid, &age)); |
279 | 312 |
280 GUID empty_guid = {0}; | 313 GUID empty_guid = {0}; |
281 EXPECT_TRUE(!IsEqualGUID(empty_guid, guid)); | 314 EXPECT_TRUE(!IsEqualGUID(empty_guid, guid)); |
282 EXPECT_NE(0U, age); | 315 EXPECT_NE(0U, age); |
283 FreeLibrary(module); | 316 FreeLibrary(module); |
284 } | 317 } |
285 | 318 |
286 } // namespace win | 319 } // namespace win |
287 } // namespace base | 320 } // namespace base |
OLD | NEW |