Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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. | |
| 4 | |
| 5 #include <stdint.h> | |
| 6 #include <windows.h> | |
| 7 | |
| 8 #include <algorithm> | |
| 9 #include <vector> | |
| 10 | |
| 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/path_service.h" | |
| 16 #include "base/strings/string_util.h" | |
| 17 #include "base/win/pe_image.h" | |
| 18 #include "base/win/scoped_handle.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 const char* kPatterns[] = { "KERNEL32.dll", "MSVC*", "ADVAPI32.dll" }; | |
|
Sigurður Ásgeirsson
2013/12/18 21:30:48
nit: since this is used only once, I'd move it to
Cait (Slow)
2013/12/19 19:52:00
Done.
| |
| 24 | |
| 25 bool ImportsCallback(const base::win::PEImage &image, | |
|
Sigurður Ásgeirsson
2013/12/18 21:30:48
nit: might as well make this a static member of th
Cait (Slow)
2013/12/19 19:52:00
Done.
| |
| 26 LPCSTR module, | |
| 27 PIMAGE_THUNK_DATA name_table, | |
| 28 PIMAGE_THUNK_DATA iat, | |
| 29 PVOID cookie) { | |
| 30 std::vector<std::string>* import_list = | |
| 31 reinterpret_cast<std::vector<std::string>*>(cookie); | |
| 32 LOG(INFO) << module; | |
|
Sigurður Ásgeirsson
2013/12/18 21:30:48
did you intend to leave this in?
Cait (Slow)
2013/12/19 19:52:00
oops
| |
| 33 import_list->push_back(module); | |
| 34 return true; | |
| 35 } | |
| 36 | |
| 37 class ELFImportsTest : public testing::Test { | |
| 38 protected: | |
| 39 void GetImports(const wchar_t* module_name, | |
|
Sigurður Ásgeirsson
2013/12/18 21:30:48
might as well pass a FilePath in here, it's more e
Cait (Slow)
2013/12/19 19:52:00
Done.
| |
| 40 std::vector<std::string>* imports) { | |
| 41 if (!imports) | |
|
Sigurður Ásgeirsson
2013/12/18 21:30:48
ASSERT_TRUE(imports != NULL) or ASSERT_TRUE(import
Cait (Slow)
2013/12/19 19:52:00
Done.
| |
| 42 return; | |
| 43 | |
| 44 base::win::ScopedHandle module_file_handle(::CreateFile(module_name, | |
|
Sigurður Ásgeirsson
2013/12/18 21:30:48
Is base::MemoryMappedFile (base/files/...) not sui
Cait (Slow)
2013/12/19 19:52:00
Yes it is..and it simplifies things a lot -- thank
| |
| 45 GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)); | |
| 46 | |
| 47 ASSERT_TRUE(module_file_handle.IsValid()); | |
| 48 | |
| 49 base::win::ScopedHandle module_file_mapping(::CreateFileMapping( | |
| 50 module_file_handle, NULL, PAGE_READWRITE, 0, 0, NULL)); | |
| 51 | |
| 52 ASSERT_TRUE(module_file_mapping.IsValid()); | |
| 53 | |
| 54 void* module_file_view = MapViewOfFile( | |
|
Sigurður Ásgeirsson
2013/12/18 21:30:48
::MapViewOfFile?
Cait (Slow)
2013/12/19 19:52:00
Not needed any more
| |
| 55 module_file_mapping, FILE_MAP_WRITE, 0, 0, 0); | |
| 56 | |
| 57 if (module_file_view != NULL) { | |
|
Sigurður Ásgeirsson
2013/12/18 21:30:48
Better to ASSERT consistently. This will ensure yo
| |
| 58 base::win::PEImageAsData pe_image_data( | |
| 59 reinterpret_cast<HMODULE>(module_file_view)); | |
| 60 pe_image_data.EnumImportChunks(ImportsCallback, imports); | |
| 61 | |
| 62 EXPECT_TRUE(::UnmapViewOfFile(module_file_view)); | |
| 63 module_file_mapping.Close(); | |
|
Sigurður Ásgeirsson
2013/12/18 21:30:48
This close operation is weirdly scoped, but it's a
| |
| 64 } | |
| 65 | |
| 66 module_file_handle.Close(); | |
|
Sigurður Ásgeirsson
2013/12/18 21:30:48
also redundant, I think?
| |
| 67 } | |
| 68 }; | |
| 69 | |
| 70 TEST_F(ELFImportsTest, ChromeElfSanityCheck) { | |
| 71 std::vector<std::string> elf_imports; | |
| 72 | |
| 73 base::FilePath dll; | |
| 74 PathService::Get(base::DIR_MODULE, &dll); | |
| 75 dll = dll.Append(L"chrome_elf.dll"); | |
| 76 GetImports(dll.value().c_str(), &elf_imports); | |
| 77 | |
| 78 // Check that ELF has imports. | |
| 79 ASSERT_GT(elf_imports.size(), 0u); | |
|
Sigurður Ásgeirsson
2013/12/18 21:30:48
nit: the ASSERT_XX macros want ASSERT_XX(expected,
Cait (Slow)
2013/12/19 19:52:00
Done.
| |
| 80 | |
| 81 std::vector<std::string>::iterator it = elf_imports.begin(); | |
| 82 | |
| 83 // Make sure all of ELF's imports are in are valid imports list. | |
| 84 for (; it != elf_imports.end(); it++) { | |
| 85 bool match = false; | |
| 86 for (int i = 0; i < arraysize(kPatterns); i++) { | |
| 87 if (MatchPattern(*it, kPatterns[i])) | |
| 88 match = true; | |
| 89 } | |
| 90 if (!match) | |
|
Sigurður Ásgeirsson
2013/12/18 21:30:48
hint; you can also
ASSERT_TRUE(match) << "....";
Cait (Slow)
2013/12/19 19:52:00
Done.
| |
| 91 FAIL() << "Illegal import in chrome_elf.dll."; | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 TEST_F(ELFImportsTest, ChromeExeSanityCheck) { | |
| 96 std::vector<std::string> exe_imports; | |
| 97 | |
| 98 base::FilePath exe; | |
| 99 PathService::Get(base::DIR_EXE, &exe); | |
|
Sigurður Ásgeirsson
2013/12/18 21:30:48
DIR_EXE and DIR_MODULE will produce the same resul
Cait (Slow)
2013/12/19 19:52:00
Done.
| |
| 100 exe = exe.Append(L"chrome.exe"); | |
| 101 GetImports(exe.value().c_str(), &exe_imports); | |
| 102 | |
| 103 // Check that chrome.exe has imports. | |
| 104 ASSERT_GT(exe_imports.size(), 0u); | |
| 105 | |
| 106 // Chrome.exe's first import must be ELF. | |
| 107 EXPECT_EQ("chrome_elf.dll", exe_imports[0]); | |
| 108 } | |
| 109 | |
| 110 } // namespace | |
| OLD | NEW |