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 <algorithm> | |
6 #include <vector> | |
7 #include <stdint.h> | |
8 #include <windows.h> | |
robertshield
2013/12/09 22:23:13
nit: C headers before C++ headers
Cait (Slow)
2013/12/09 23:34:54
Done.
| |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/compiler_specific.h" | |
12 #include "base/win/pe_image.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace { | |
16 | |
17 const char* kValidELFImports[] = {"KERNEL32.dll", | |
18 "MSVCP100.dll", | |
19 "MSVCP100D.dll", | |
20 "MSVCR100.dll", | |
21 "MSVCR100D.dll"}; | |
robertshield
2013/12/09 22:23:13
I think these DLL names change with different run
Cait (Slow)
2013/12/09 23:34:54
Done.
| |
22 | |
23 class ELFImportsTest : public testing::Test { | |
24 protected: | |
25 virtual void SetUp() OVERRIDE { | |
26 for (int i = 0; i<arraysize(kValidELFImports); i++) | |
robertshield
2013/12/09 22:23:13
nit: i < arraysize (spaces)
Cait (Slow)
2013/12/09 23:34:54
Done.
| |
27 valid_elf_imports_.push_back(std::string(kValidELFImports[i])); | |
28 } | |
29 | |
30 void GetImports(wchar_t* module_name, std::vector<std::string>* imports) { | |
31 ::LoadLibrary(module_name); | |
robertshield
2013/12/09 22:23:13
LoadLibrary returns the module handle, so you don'
| |
32 HMODULE module_handle = ::GetModuleHandle(module_name); | |
robertshield
2013/12/09 22:23:13
I think there's also a class in base called base::
| |
33 | |
34 if (!module_handle || !imports) | |
35 return; | |
36 | |
37 base::win::PEImage pe_image(module_handle); | |
38 PIMAGE_IMPORT_DESCRIPTOR image_descriptor = pe_image.GetFirstImportChunk(); | |
39 | |
40 // The list of Import Address Tables ends with an empty {0} descriptor. | |
41 while (image_descriptor->Characteristics) { | |
42 char* name = | |
43 (char*) ( (DWORD) module_handle + (DWORD) image_descriptor->Name); | |
robertshield
2013/12/09 22:23:13
no c-style casts:
char* name = reinterpret_cast<c
| |
44 imports->push_back(std::string(name)); | |
robertshield
2013/12/09 22:23:13
don't think you need to explicitly call this const
| |
45 image_descriptor += sizeof(image_descriptor) / sizeof(uintptr_t); | |
robertshield
2013/12/09 22:23:13
silly question: should this be a uintptr_t or a ui
Cait (Slow)
2013/12/09 23:34:54
I *think* this one should be a uintptr_t (since we
| |
46 } | |
47 } | |
48 | |
49 std::vector<std::string> valid_elf_imports_; | |
50 }; | |
51 | |
52 TEST_F(ELFImportsTest, ChromeElfSanityCheck) { | |
53 std::vector<std::string> elf_imports; | |
54 GetImports(L"chrome_elf.dll", &elf_imports); | |
55 | |
56 // Check that ELF has imports. | |
57 ASSERT_TRUE(elf_imports.size() > 0); | |
58 | |
59 std::vector<std::string>::iterator it = elf_imports.begin(); | |
60 std::vector<std::string>::iterator valid_imports_it; | |
61 | |
62 // Make sure all of ELF's imports are in are valid imports list. | |
63 for (; it != elf_imports.end(); it++) { | |
64 valid_imports_it = std::find(valid_elf_imports_.begin(), | |
65 valid_elf_imports_.end(), *it); | |
66 EXPECT_NE(valid_imports_it, valid_elf_imports_.end()); | |
67 } | |
68 } | |
69 | |
70 TEST_F(ELFImportsTest, ChromeExeSanityCheck) { | |
71 std::vector<std::string> exe_imports; | |
72 GetImports(L"chrome.exe", &exe_imports); | |
73 | |
74 // Check that chrome.exe has imports. | |
75 ASSERT_TRUE(exe_imports.size() > 0); | |
76 | |
77 // Chrome.exe's first import must be ELF. | |
78 EXPECT_EQ("chrome_elf.dll", exe_imports[0]); | |
79 } | |
80 | |
81 } // namespace | |
OLD | NEW |