Roll src/third_party/WebKit 10b2b4a:a6818f9 (svn 202548:202549)
[chromium-blink-merge.git] / chrome_elf / elf_imports_unittest.cc
blob941c6414ca0fc6c2b63e8e748459898804f0ea19
1 // Copyright 2014 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 #include <stdint.h>
6 #include <windows.h>
8 #include <algorithm>
9 #include <vector>
11 #include "base/base_paths.h"
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/files/file_path.h"
15 #include "base/files/memory_mapped_file.h"
16 #include "base/path_service.h"
17 #include "base/strings/pattern.h"
18 #include "base/strings/string_util.h"
19 #include "base/win/pe_image.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace {
24 class ELFImportsTest : public testing::Test {
25 protected:
26 static bool ImportsCallback(const base::win::PEImage &image,
27 LPCSTR module,
28 PIMAGE_THUNK_DATA name_table,
29 PIMAGE_THUNK_DATA iat,
30 PVOID cookie) {
31 std::vector<std::string>* import_list =
32 reinterpret_cast<std::vector<std::string>*>(cookie);
33 import_list->push_back(module);
34 return true;
37 void GetImports(const base::FilePath& module_path,
38 std::vector<std::string>* imports) {
39 ASSERT_TRUE(imports != NULL);
41 base::MemoryMappedFile module_mmap;
43 ASSERT_TRUE(module_mmap.Initialize(module_path));
44 base::win::PEImageAsData pe_image_data(
45 reinterpret_cast<HMODULE>(const_cast<uint8*>(module_mmap.data())));
46 pe_image_data.EnumImportChunks(ELFImportsTest::ImportsCallback, imports);
50 TEST_F(ELFImportsTest, ChromeElfSanityCheck) {
51 std::vector<std::string> elf_imports;
53 base::FilePath dll;
54 ASSERT_TRUE(PathService::Get(base::DIR_EXE, &dll));
55 dll = dll.Append(L"chrome_elf.dll");
56 GetImports(dll, &elf_imports);
58 // Check that ELF has imports.
59 ASSERT_LT(0u, elf_imports.size()) << "Ensure the chrome_elf_unittests "
60 "target was built, instead of chrome_elf_unittests.exe";
62 std::vector<std::string>::iterator it(elf_imports.begin());
64 static const char* const kValidFilePatterns[] = {
65 "KERNEL32.dll",
66 "MSVC*",
67 #if defined(SYZYASAN)
68 "syzyasan_rtl.dll",
69 #endif
70 #if defined(ADDRESS_SANITIZER) && defined(COMPONENT_BUILD)
71 "clang_rt.asan_dynamic-i386.dll",
72 #endif
73 "ADVAPI32.dll"
76 // Make sure all of ELF's imports are in the valid imports list.
77 for (; it != elf_imports.end(); it++) {
78 bool match = false;
79 for (int i = 0; i < arraysize(kValidFilePatterns); ++i) {
80 if (base::MatchPattern(*it, kValidFilePatterns[i]))
81 match = true;
83 ASSERT_TRUE(match) << "Illegal import in chrome_elf.dll: " << *it;
87 TEST_F(ELFImportsTest, ChromeExeSanityCheck) {
88 std::vector<std::string> exe_imports;
90 base::FilePath exe;
91 ASSERT_TRUE(PathService::Get(base::DIR_EXE, &exe));
92 exe = exe.Append(L"chrome.exe");
93 GetImports(exe, &exe_imports);
95 // Check that chrome.exe has imports.
96 ASSERT_LT(0u, exe_imports.size()) << "Ensure the chrome_elf_unittests "
97 "target was built, instead of chrome_elf_unittests.exe";
99 // Chrome.exe's first import must be ELF.
100 EXPECT_EQ("chrome_elf.dll", exe_imports[0]) <<
101 "Illegal import order in chrome.exe (ensure the chrome_elf_unittest "
102 "target was built, instead of just chrome_elf_unittests.exe)";
105 } // namespace