| 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> | 6 #include <algorithm> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/scoped_native_library.h" |
| 11 #include "base/win/pe_image.h" | 12 #include "base/win/pe_image.h" |
| 12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace base { | 16 namespace base { |
| 16 namespace win { | 17 namespace win { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // Just counts the number of invocations. | 21 // Just counts the number of invocations. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 base::FilePath pe_image_test_path; | 96 base::FilePath pe_image_test_path; |
| 96 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &pe_image_test_path)); | 97 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &pe_image_test_path)); |
| 97 pe_image_test_path = pe_image_test_path.Append(FILE_PATH_LITERAL("pe_image")); | 98 pe_image_test_path = pe_image_test_path.Append(FILE_PATH_LITERAL("pe_image")); |
| 98 | 99 |
| 99 #if defined(ARCH_CPU_64_BITS) | 100 #if defined(ARCH_CPU_64_BITS) |
| 100 pe_image_test_path = | 101 pe_image_test_path = |
| 101 pe_image_test_path.Append(FILE_PATH_LITERAL("pe_image_test_64.dll")); | 102 pe_image_test_path.Append(FILE_PATH_LITERAL("pe_image_test_64.dll")); |
| 102 const int sections = 6; | 103 const int sections = 6; |
| 103 const int imports_dlls = 2; | 104 const int imports_dlls = 2; |
| 104 const int delay_dlls = 2; | 105 const int delay_dlls = 2; |
| 105 const int exports = 2; | 106 const int exports = 3; |
| 106 const int imports = 69; | 107 const int imports = 70; |
| 107 const int delay_imports = 2; | 108 const int delay_imports = 2; |
| 108 const int relocs = 632; | 109 const int relocs = 976; |
| 109 #else | 110 #else |
| 110 pe_image_test_path = | 111 pe_image_test_path = |
| 111 pe_image_test_path.Append(FILE_PATH_LITERAL("pe_image_test_32.dll")); | 112 pe_image_test_path.Append(FILE_PATH_LITERAL("pe_image_test_32.dll")); |
| 112 const int sections = 5; | 113 const int sections = 5; |
| 113 const int imports_dlls = 2; | 114 const int imports_dlls = 2; |
| 114 const int delay_dlls = 2; | 115 const int delay_dlls = 2; |
| 115 const int exports = 2; | 116 const int exports = 3; |
| 116 const int imports = 66; | 117 const int imports = 66; |
| 117 const int delay_imports = 2; | 118 const int delay_imports = 2; |
| 118 const int relocs = 1586; | 119 const int relocs = 2114; |
| 119 #endif | 120 #endif |
| 120 | 121 |
| 121 HMODULE module = LoadLibrary(pe_image_test_path.value().c_str()); | 122 ScopedNativeLibrary module(pe_image_test_path); |
| 122 ASSERT_TRUE(NULL != module); | 123 ASSERT_TRUE(module.is_valid()); |
| 123 | 124 |
| 124 PEImage pe(module); | 125 PEImage pe(module.get()); |
| 125 int count = 0; | 126 int count = 0; |
| 126 EXPECT_TRUE(pe.VerifyMagic()); | 127 EXPECT_TRUE(pe.VerifyMagic()); |
| 127 | 128 |
| 128 pe.EnumSections(SectionsCallback, &count); | 129 pe.EnumSections(SectionsCallback, &count); |
| 129 EXPECT_EQ(sections, count); | 130 EXPECT_EQ(sections, count); |
| 130 | 131 |
| 131 count = 0; | 132 count = 0; |
| 132 pe.EnumImportChunks(ImportChunksCallback, &count); | 133 pe.EnumImportChunks(ImportChunksCallback, &count); |
| 133 EXPECT_EQ(imports_dlls, count); | 134 EXPECT_EQ(imports_dlls, count); |
| 134 | 135 |
| 135 count = 0; | 136 count = 0; |
| 136 pe.EnumDelayImportChunks(DelayImportChunksCallback, &count); | 137 pe.EnumDelayImportChunks(DelayImportChunksCallback, &count); |
| 137 EXPECT_EQ(delay_dlls, count); | 138 EXPECT_EQ(delay_dlls, count); |
| 138 | 139 |
| 139 count = 0; | 140 count = 0; |
| 140 pe.EnumExports(ExportsCallback, &count); | 141 pe.EnumExports(ExportsCallback, &count); |
| 141 EXPECT_EQ(exports, count); | 142 EXPECT_EQ(exports, count); |
| 142 | 143 |
| 143 count = 0; | 144 count = 0; |
| 144 pe.EnumAllImports(ImportsCallback, &count); | 145 pe.EnumAllImports(ImportsCallback, &count); |
| 145 EXPECT_EQ(imports, count); | 146 EXPECT_EQ(imports, count); |
| 146 | 147 |
| 147 count = 0; | 148 count = 0; |
| 148 pe.EnumAllDelayImports(ImportsCallback, &count); | 149 pe.EnumAllDelayImports(ImportsCallback, &count); |
| 149 EXPECT_EQ(delay_imports, count); | 150 EXPECT_EQ(delay_imports, count); |
| 150 | 151 |
| 151 count = 0; | 152 count = 0; |
| 152 pe.EnumRelocs(RelocsCallback, &count); | 153 pe.EnumRelocs(RelocsCallback, &count); |
| 153 EXPECT_EQ(relocs, count); | 154 EXPECT_EQ(relocs, count); |
| 154 | |
| 155 FreeLibrary(module); | |
| 156 } | 155 } |
| 157 | 156 |
| 158 // Tests that we can locate an specific exported symbol, by name and by ordinal. | 157 // Tests that we can locate an specific exported symbol, by name and by ordinal. |
| 159 TEST(PEImageTest, RetrievesExports) { | 158 TEST(PEImageTest, RetrievesExports) { |
| 160 HMODULE module = LoadLibrary(L"advapi32.dll"); | 159 ScopedNativeLibrary module(FilePath(L"advapi32.dll")); |
| 161 ASSERT_TRUE(NULL != module); | 160 ASSERT_TRUE(module.is_valid()); |
| 162 | 161 |
| 163 PEImage pe(module); | 162 PEImage pe(module.get()); |
| 164 WORD ordinal; | 163 WORD ordinal; |
| 165 | 164 |
| 166 EXPECT_TRUE(pe.GetProcOrdinal("RegEnumKeyExW", &ordinal)); | 165 EXPECT_TRUE(pe.GetProcOrdinal("RegEnumKeyExW", &ordinal)); |
| 167 | 166 |
| 168 FARPROC address1 = pe.GetProcAddress("RegEnumKeyExW"); | 167 FARPROC address1 = pe.GetProcAddress("RegEnumKeyExW"); |
| 169 FARPROC address2 = pe.GetProcAddress(reinterpret_cast<char*>(ordinal)); | 168 FARPROC address2 = pe.GetProcAddress(reinterpret_cast<char*>(ordinal)); |
| 170 EXPECT_TRUE(address1 != NULL); | 169 EXPECT_TRUE(address1 != NULL); |
| 171 EXPECT_TRUE(address2 != NULL); | 170 EXPECT_TRUE(address2 != NULL); |
| 172 EXPECT_TRUE(address1 == address2); | 171 EXPECT_TRUE(address1 == address2); |
| 172 } |
| 173 | 173 |
| 174 FreeLibrary(module); | 174 // Tests that we can locate a forwarded export. |
| 175 TEST(PEImageTest, ForwardedExport) { |
| 176 base::FilePath pe_image_test_path; |
| 177 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &pe_image_test_path)); |
| 178 pe_image_test_path = pe_image_test_path.Append(FILE_PATH_LITERAL("pe_image")); |
| 179 |
| 180 #if defined(ARCH_CPU_64_BITS) |
| 181 pe_image_test_path = |
| 182 pe_image_test_path.Append(FILE_PATH_LITERAL("pe_image_test_64.dll")); |
| 183 #else |
| 184 pe_image_test_path = |
| 185 pe_image_test_path.Append(FILE_PATH_LITERAL("pe_image_test_32.dll")); |
| 186 #endif |
| 187 |
| 188 ScopedNativeLibrary module(pe_image_test_path); |
| 189 |
| 190 ASSERT_TRUE(module.is_valid()); |
| 191 |
| 192 PEImage pe(module.get()); |
| 193 |
| 194 FARPROC addr = pe.GetProcAddress("FwdExport"); |
| 195 EXPECT_EQ(FARPROC(-1), addr); |
| 196 |
| 197 PDWORD export_entry = pe.GetExportEntry("FwdExport"); |
| 198 EXPECT_NE(nullptr, export_entry); |
| 199 PVOID fwd_addr = pe.RVAToAddr(*export_entry); |
| 200 const char expected_fwd[] = "KERNEL32.CreateFileA"; |
| 201 EXPECT_STREQ(expected_fwd, reinterpret_cast<char*>(fwd_addr)); |
| 175 } | 202 } |
| 176 | 203 |
| 177 // Test that we can get debug id out of a module. | 204 // Test that we can get debug id out of a module. |
| 178 TEST(PEImageTest, GetDebugId) { | 205 TEST(PEImageTest, GetDebugId) { |
| 179 HMODULE module = LoadLibrary(L"advapi32.dll"); | 206 ScopedNativeLibrary module(FilePath(L"advapi32.dll")); |
| 180 ASSERT_TRUE(NULL != module); | 207 ASSERT_TRUE(module.is_valid()); |
| 181 | 208 |
| 182 PEImage pe(module); | 209 PEImage pe(module.get()); |
| 183 GUID guid = {0}; | 210 GUID guid = {0}; |
| 184 DWORD age = 0; | 211 DWORD age = 0; |
| 185 EXPECT_TRUE(pe.GetDebugId(&guid, &age)); | 212 EXPECT_TRUE(pe.GetDebugId(&guid, &age)); |
| 186 | 213 |
| 187 GUID empty_guid = {0}; | 214 GUID empty_guid = {0}; |
| 188 EXPECT_TRUE(!IsEqualGUID(empty_guid, guid)); | 215 EXPECT_TRUE(!IsEqualGUID(empty_guid, guid)); |
| 189 EXPECT_NE(0U, age); | 216 EXPECT_NE(0U, age); |
| 190 FreeLibrary(module); | |
| 191 } | 217 } |
| 192 | 218 |
| 193 } // namespace win | 219 } // namespace win |
| 194 } // namespace base | 220 } // namespace base |
| OLD | NEW |