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 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "base/win/pe_image.h" | 8 #include "base/win/pe_image.h" |
9 #include "base/win/windows_version.h" | 9 #include "base/win/windows_version.h" |
10 | 10 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 PIMAGE_THUNK_DATA name_table, | 76 PIMAGE_THUNK_DATA name_table, |
77 PIMAGE_THUNK_DATA iat, | 77 PIMAGE_THUNK_DATA iat, |
78 PIMAGE_THUNK_DATA bound_iat, | 78 PIMAGE_THUNK_DATA bound_iat, |
79 PIMAGE_THUNK_DATA unload_iat, | 79 PIMAGE_THUNK_DATA unload_iat, |
80 PVOID cookie) { | 80 PVOID cookie) { |
81 int* count = reinterpret_cast<int*>(cookie); | 81 int* count = reinterpret_cast<int*>(cookie); |
82 (*count)++; | 82 (*count)++; |
83 return true; | 83 return true; |
84 } | 84 } |
85 | 85 |
| 86 // Identifiers for the set of supported expectations. |
| 87 enum ExpectationSet { |
| 88 WIN_2K_SET, |
| 89 WIN_XP_SET, |
| 90 WIN_VISTA_SET, |
| 91 WIN_7_SET, |
| 92 WIN_8_SET, |
| 93 UNSUPPORTED_SET, |
| 94 }; |
| 95 |
86 // We'll be using some known values for the tests. | 96 // We'll be using some known values for the tests. |
87 enum Value { | 97 enum Value { |
88 sections = 0, | 98 sections = 0, |
89 imports_dlls, | 99 imports_dlls, |
90 delay_dlls, | 100 delay_dlls, |
91 exports, | 101 exports, |
92 imports, | 102 imports, |
93 delay_imports, | 103 delay_imports, |
94 relocs | 104 relocs |
95 }; | 105 }; |
96 | 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 } |
| 120 |
97 // Retrieves the expected value from advapi32.dll based on the OS. | 121 // Retrieves the expected value from advapi32.dll based on the OS. |
98 int GetExpectedValue(Value value, DWORD os) { | 122 int GetExpectedValue(Value value, DWORD os) { |
99 const int xp_delay_dlls = 2; | 123 const int xp_delay_dlls = 2; |
100 const int xp_exports = 675; | 124 const int xp_exports = 675; |
101 const int xp_imports = 422; | 125 const int xp_imports = 422; |
102 const int xp_delay_imports = 8; | 126 const int xp_delay_imports = 8; |
103 const int xp_relocs = 9180; | 127 const int xp_relocs = 9180; |
104 const int vista_delay_dlls = 4; | 128 const int vista_delay_dlls = 4; |
105 const int vista_exports = 799; | 129 const int vista_exports = 799; |
106 const int vista_imports = 476; | 130 const int vista_imports = 476; |
(...skipping 29 matching lines...) Expand all Loading... |
136 {4, 4, 4, 4, win8_sections}, | 160 {4, 4, 4, 4, win8_sections}, |
137 {3, 3, 3, 13, win8_import_dlls}, | 161 {3, 3, 3, 13, win8_import_dlls}, |
138 {w2k_delay_dlls, xp_delay_dlls, vista_delay_dlls, win7_delay_dlls, | 162 {w2k_delay_dlls, xp_delay_dlls, vista_delay_dlls, win7_delay_dlls, |
139 win8_delay_dlls}, | 163 win8_delay_dlls}, |
140 {w2k_exports, xp_exports, vista_exports, win7_exports, win8_exports}, | 164 {w2k_exports, xp_exports, vista_exports, win7_exports, win8_exports}, |
141 {w2k_imports, xp_imports, vista_imports, win7_imports, win8_imports}, | 165 {w2k_imports, xp_imports, vista_imports, win7_imports, win8_imports}, |
142 {w2k_delay_imports, xp_delay_imports, | 166 {w2k_delay_imports, xp_delay_imports, |
143 vista_delay_imports, win7_delay_imports, win8_delay_imports}, | 167 vista_delay_imports, win7_delay_imports, win8_delay_imports}, |
144 {w2k_relocs, xp_relocs, vista_relocs, win7_relocs, win8_relocs} | 168 {w2k_relocs, xp_relocs, vista_relocs, win7_relocs, win8_relocs} |
145 }; | 169 }; |
| 170 COMPILE_ASSERT(arraysize(expected[0]) == UNSUPPORTED_SET, |
| 171 expected_value_set_mismatch); |
146 | 172 |
147 if (value > relocs) | 173 if (value > relocs) |
148 return 0; | 174 return 0; |
149 if (50 == os) | 175 ExpectationSet expected_set = GetExpectationSet(os); |
150 os = 0; // 5.0 | 176 if (expected_set >= arraysize(expected)) { |
151 else if (51 == os || 52 == os) | 177 // This should never happen. Log a failure if it does. |
152 os = 1; | 178 EXPECT_NE(UNSUPPORTED_SET, expected_set); |
153 else if (os == 60) | 179 expected_set = WIN_2K_SET; |
154 os = 2; // 6.x | 180 } |
155 else if (os == 61) | |
156 os = 3; | |
157 else if (os >= 62) | |
158 os = 4; | |
159 else | |
160 return 0; | |
161 | 181 |
162 return expected[value][os]; | 182 return expected[value][expected_set]; |
163 } | 183 } |
164 | 184 |
165 // Tests that we are able to enumerate stuff from a PE file, and that | 185 // Tests that we are able to enumerate stuff from a PE file, and that |
166 // the actual number of items found is within the expected range. | 186 // the actual number of items found is within the expected range. |
167 TEST(PEImageTest, EnumeratesPE) { | 187 TEST(PEImageTest, EnumeratesPE) { |
168 // Windows Server 2003 is not supported as a test environment for this test. | |
169 if (base::win::GetVersion() == base::win::VERSION_SERVER_2003) | |
170 return; | |
171 HMODULE module = LoadLibrary(L"advapi32.dll"); | 188 HMODULE module = LoadLibrary(L"advapi32.dll"); |
172 ASSERT_TRUE(NULL != module); | 189 ASSERT_TRUE(NULL != module); |
173 | 190 |
174 PEImage pe(module); | 191 PEImage pe(module); |
175 int count = 0; | 192 int count = 0; |
176 EXPECT_TRUE(pe.VerifyMagic()); | 193 EXPECT_TRUE(pe.VerifyMagic()); |
177 | 194 |
178 DWORD os = pe.GetNTHeaders()->OptionalHeader.MajorOperatingSystemVersion; | 195 DWORD os = pe.GetNTHeaders()->OptionalHeader.MajorOperatingSystemVersion; |
179 os = os * 10 + pe.GetNTHeaders()->OptionalHeader.MinorOperatingSystemVersion; | 196 os = os * 10 + pe.GetNTHeaders()->OptionalHeader.MinorOperatingSystemVersion; |
180 | 197 |
| 198 // Skip this test for unsupported OS versions. |
| 199 if (GetExpectationSet(os) == UNSUPPORTED_SET) |
| 200 return; |
| 201 |
181 pe.EnumSections(SectionsCallback, &count); | 202 pe.EnumSections(SectionsCallback, &count); |
182 EXPECT_EQ(GetExpectedValue(sections, os), count); | 203 EXPECT_EQ(GetExpectedValue(sections, os), count); |
183 | 204 |
184 count = 0; | 205 count = 0; |
185 pe.EnumImportChunks(ImportChunksCallback, &count); | 206 pe.EnumImportChunks(ImportChunksCallback, &count); |
186 EXPECT_EQ(GetExpectedValue(imports_dlls, os), count); | 207 EXPECT_EQ(GetExpectedValue(imports_dlls, os), count); |
187 | 208 |
188 count = 0; | 209 count = 0; |
189 pe.EnumDelayImportChunks(DelayImportChunksCallback, &count); | 210 pe.EnumDelayImportChunks(DelayImportChunksCallback, &count); |
190 EXPECT_EQ(GetExpectedValue(delay_dlls, os), count); | 211 EXPECT_EQ(GetExpectedValue(delay_dlls, os), count); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 FARPROC address2 = pe.GetProcAddress(reinterpret_cast<char*>(ordinal)); | 247 FARPROC address2 = pe.GetProcAddress(reinterpret_cast<char*>(ordinal)); |
227 EXPECT_TRUE(address1 != NULL); | 248 EXPECT_TRUE(address1 != NULL); |
228 EXPECT_TRUE(address2 != NULL); | 249 EXPECT_TRUE(address2 != NULL); |
229 EXPECT_TRUE(address1 == address2); | 250 EXPECT_TRUE(address1 == address2); |
230 | 251 |
231 FreeLibrary(module); | 252 FreeLibrary(module); |
232 } | 253 } |
233 | 254 |
234 } // namespace win | 255 } // namespace win |
235 } // namespace base | 256 } // namespace base |
OLD | NEW |