Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / win / pe_image_unittest.cc
blob28b65a4e0ba6102ff10161af4edcf8d7f7d6eb27
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
3 // found in the LICENSE file.
5 // This file contains unit tests for PEImage.
6 #include <algorithm>
7 #include <vector>
9 #include "base/files/file_path.h"
10 #include "base/path_service.h"
11 #include "base/win/pe_image.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace base {
15 namespace win {
17 namespace {
19 // Just counts the number of invocations.
20 bool ImportsCallback(const PEImage& image,
21 LPCSTR module,
22 DWORD ordinal,
23 LPCSTR name,
24 DWORD hint,
25 PIMAGE_THUNK_DATA iat,
26 PVOID cookie) {
27 int* count = reinterpret_cast<int*>(cookie);
28 (*count)++;
29 return true;
32 // Just counts the number of invocations.
33 bool SectionsCallback(const PEImage& image,
34 PIMAGE_SECTION_HEADER header,
35 PVOID section_start,
36 DWORD section_size,
37 PVOID cookie) {
38 int* count = reinterpret_cast<int*>(cookie);
39 (*count)++;
40 return true;
43 // Just counts the number of invocations.
44 bool RelocsCallback(const PEImage& image,
45 WORD type,
46 PVOID address,
47 PVOID cookie) {
48 int* count = reinterpret_cast<int*>(cookie);
49 (*count)++;
50 return true;
53 // Just counts the number of invocations.
54 bool ImportChunksCallback(const PEImage& image,
55 LPCSTR module,
56 PIMAGE_THUNK_DATA name_table,
57 PIMAGE_THUNK_DATA iat,
58 PVOID cookie) {
59 int* count = reinterpret_cast<int*>(cookie);
60 (*count)++;
61 return true;
64 // Just counts the number of invocations.
65 bool DelayImportChunksCallback(const PEImage& image,
66 PImgDelayDescr delay_descriptor,
67 LPCSTR module,
68 PIMAGE_THUNK_DATA name_table,
69 PIMAGE_THUNK_DATA iat,
70 PIMAGE_THUNK_DATA bound_iat,
71 PIMAGE_THUNK_DATA unload_iat,
72 PVOID cookie) {
73 int* count = reinterpret_cast<int*>(cookie);
74 (*count)++;
75 return true;
78 // Just counts the number of invocations.
79 bool ExportsCallback(const PEImage& image,
80 DWORD ordinal,
81 DWORD hint,
82 LPCSTR name,
83 PVOID function,
84 LPCSTR forward,
85 PVOID cookie) {
86 int* count = reinterpret_cast<int*>(cookie);
87 (*count)++;
88 return true;
91 } // namespace
93 // Tests that we are able to enumerate stuff from a PE file, and that
94 // the actual number of items found matches an expected value.
95 TEST(PEImageTest, EnumeratesPE) {
96 base::FilePath pe_image_test_path;
97 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &pe_image_test_path));
98 pe_image_test_path = pe_image_test_path.Append(FILE_PATH_LITERAL("pe_image"));
100 #if defined(ARCH_CPU_64_BITS)
101 pe_image_test_path =
102 pe_image_test_path.Append(FILE_PATH_LITERAL("pe_image_test_64.dll"));
103 const int sections = 6;
104 const int imports_dlls = 2;
105 const int delay_dlls = 2;
106 const int exports = 2;
107 const int imports = 69;
108 const int delay_imports = 2;
109 const int relocs = 632;
110 #else
111 pe_image_test_path =
112 pe_image_test_path.Append(FILE_PATH_LITERAL("pe_image_test_32.dll"));
113 const int sections = 5;
114 const int imports_dlls = 2;
115 const int delay_dlls = 2;
116 const int exports = 2;
117 const int imports = 66;
118 const int delay_imports = 2;
119 const int relocs = 1586;
120 #endif
122 HMODULE module = LoadLibrary(pe_image_test_path.value().c_str());
123 ASSERT_TRUE(NULL != module);
125 PEImage pe(module);
126 int count = 0;
127 EXPECT_TRUE(pe.VerifyMagic());
129 pe.EnumSections(SectionsCallback, &count);
130 EXPECT_EQ(sections, count);
132 count = 0;
133 pe.EnumImportChunks(ImportChunksCallback, &count);
134 EXPECT_EQ(imports_dlls, count);
136 count = 0;
137 pe.EnumDelayImportChunks(DelayImportChunksCallback, &count);
138 EXPECT_EQ(delay_dlls, count);
140 count = 0;
141 pe.EnumExports(ExportsCallback, &count);
142 EXPECT_EQ(exports, count);
144 count = 0;
145 pe.EnumAllImports(ImportsCallback, &count);
146 EXPECT_EQ(imports, count);
148 count = 0;
149 pe.EnumAllDelayImports(ImportsCallback, &count);
150 EXPECT_EQ(delay_imports, count);
152 count = 0;
153 pe.EnumRelocs(RelocsCallback, &count);
154 EXPECT_EQ(relocs, count);
156 FreeLibrary(module);
159 // Tests that we can locate an specific exported symbol, by name and by ordinal.
160 TEST(PEImageTest, RetrievesExports) {
161 HMODULE module = LoadLibrary(L"advapi32.dll");
162 ASSERT_TRUE(NULL != module);
164 PEImage pe(module);
165 WORD ordinal;
167 EXPECT_TRUE(pe.GetProcOrdinal("RegEnumKeyExW", &ordinal));
169 FARPROC address1 = pe.GetProcAddress("RegEnumKeyExW");
170 FARPROC address2 = pe.GetProcAddress(reinterpret_cast<char*>(ordinal));
171 EXPECT_TRUE(address1 != NULL);
172 EXPECT_TRUE(address2 != NULL);
173 EXPECT_TRUE(address1 == address2);
175 FreeLibrary(module);
178 // Test that we can get debug id out of a module.
179 TEST(PEImageTest, GetDebugId) {
180 HMODULE module = LoadLibrary(L"advapi32.dll");
181 ASSERT_TRUE(NULL != module);
183 PEImage pe(module);
184 GUID guid = {0};
185 DWORD age = 0;
186 EXPECT_TRUE(pe.GetDebugId(&guid, &age));
188 GUID empty_guid = {0};
189 EXPECT_TRUE(!IsEqualGUID(empty_guid, guid));
190 EXPECT_NE(0U, age);
191 FreeLibrary(module);
194 } // namespace win
195 } // namespace base