Index: chrome_elf/elf_imports_unittest.cc |
diff --git a/chrome_elf/elf_imports_unittest.cc b/chrome_elf/elf_imports_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..302315bafcac8d942d0d2f845a6c31e862c23763 |
--- /dev/null |
+++ b/chrome_elf/elf_imports_unittest.cc |
@@ -0,0 +1,81 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <algorithm> |
+#include <vector> |
+#include <stdint.h> |
+#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.
|
+ |
+#include "base/basictypes.h" |
+#include "base/compiler_specific.h" |
+#include "base/win/pe_image.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+const char* kValidELFImports[] = {"KERNEL32.dll", |
+ "MSVCP100.dll", |
+ "MSVCP100D.dll", |
+ "MSVCR100.dll", |
+ "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.
|
+ |
+class ELFImportsTest : public testing::Test { |
+ protected: |
+ virtual void SetUp() OVERRIDE { |
+ 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.
|
+ valid_elf_imports_.push_back(std::string(kValidELFImports[i])); |
+ } |
+ |
+ void GetImports(wchar_t* module_name, std::vector<std::string>* imports) { |
+ ::LoadLibrary(module_name); |
robertshield
2013/12/09 22:23:13
LoadLibrary returns the module handle, so you don'
|
+ HMODULE module_handle = ::GetModuleHandle(module_name); |
robertshield
2013/12/09 22:23:13
I think there's also a class in base called base::
|
+ |
+ if (!module_handle || !imports) |
+ return; |
+ |
+ base::win::PEImage pe_image(module_handle); |
+ PIMAGE_IMPORT_DESCRIPTOR image_descriptor = pe_image.GetFirstImportChunk(); |
+ |
+ // The list of Import Address Tables ends with an empty {0} descriptor. |
+ while (image_descriptor->Characteristics) { |
+ char* name = |
+ (char*) ( (DWORD) module_handle + (DWORD) image_descriptor->Name); |
robertshield
2013/12/09 22:23:13
no c-style casts:
char* name = reinterpret_cast<c
|
+ imports->push_back(std::string(name)); |
robertshield
2013/12/09 22:23:13
don't think you need to explicitly call this const
|
+ 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
|
+ } |
+ } |
+ |
+ std::vector<std::string> valid_elf_imports_; |
+}; |
+ |
+TEST_F(ELFImportsTest, ChromeElfSanityCheck) { |
+ std::vector<std::string> elf_imports; |
+ GetImports(L"chrome_elf.dll", &elf_imports); |
+ |
+ // Check that ELF has imports. |
+ ASSERT_TRUE(elf_imports.size() > 0); |
+ |
+ std::vector<std::string>::iterator it = elf_imports.begin(); |
+ std::vector<std::string>::iterator valid_imports_it; |
+ |
+ // Make sure all of ELF's imports are in are valid imports list. |
+ for (; it != elf_imports.end(); it++) { |
+ valid_imports_it = std::find(valid_elf_imports_.begin(), |
+ valid_elf_imports_.end(), *it); |
+ EXPECT_NE(valid_imports_it, valid_elf_imports_.end()); |
+ } |
+} |
+ |
+TEST_F(ELFImportsTest, ChromeExeSanityCheck) { |
+ std::vector<std::string> exe_imports; |
+ GetImports(L"chrome.exe", &exe_imports); |
+ |
+ // Check that chrome.exe has imports. |
+ ASSERT_TRUE(exe_imports.size() > 0); |
+ |
+ // Chrome.exe's first import must be ELF. |
+ EXPECT_EQ("chrome_elf.dll", exe_imports[0]); |
+} |
+ |
+} // namespace |