Index: chrome_elf/hook_util/test/hook_util_test.cc |
diff --git a/chrome_elf/hook_util/test/hook_util_test.cc b/chrome_elf/hook_util/test/hook_util_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ccd6cb3e84e8dc32512997eb8b6d2b33da8cc61c |
--- /dev/null |
+++ b/chrome_elf/hook_util/test/hook_util_test.cc |
@@ -0,0 +1,68 @@ |
+// Copyright 2016 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 <windows.h> |
+ |
+#include "chrome_elf/hook_util/hook_util.h" |
+// Compile in this test DLL, so that it's in the IAT. |
+#include "chrome_elf/hook_util/test/hook_util_test_dll.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+// IATHook test constants. |
+const char kIATTestDllName[] = "hook_util_test_dll.dll"; |
+const char kIATExportedApiFunction[] = "ExportedApi"; |
+ |
+// IATHook function, which does nothing. |
+void IATHookedExportedApi() { |
+ return; |
+} |
+ |
+class HookTest : public testing::Test { |
+ protected: |
+ HookTest() {} |
+}; |
+ |
+//------------------------------------------------------------------------------ |
+// IATHook tests |
+//------------------------------------------------------------------------------ |
+ |
+TEST_F(HookTest, IATHook) { |
robertshield
2016/08/10 20:14:48
Can you test that double Hook()ing fails as expect
penny
2016/08/13 19:22:15
So I'm not exactly sure what you meant here... but
|
+ elf_hook::IATHook iat_hook; |
+ |
+ // Sanity test with no hook. |
+ ASSERT_EQ(0, ExportedApiCallCount()); |
+ ExportedApi(); |
+ ExportedApi(); |
+ ASSERT_EQ(2, ExportedApiCallCount()); |
+ |
+ // Apply IAT hook. |
+ ASSERT_EQ(NO_ERROR, |
+ iat_hook.Hook(::GetModuleHandle(nullptr), kIATTestDllName, |
+ kIATExportedApiFunction, IATHookedExportedApi)); |
+ |
+ // Call count should not change with hook. |
+ ExportedApi(); |
+ ExportedApi(); |
+ ExportedApi(); |
+ EXPECT_EQ(2, ExportedApiCallCount()); |
+ |
+ // Remove hook. |
+ EXPECT_EQ(NO_ERROR, iat_hook.Unhook()); |
+ |
+ // Sanity test things are back to normal. |
+ ExportedApi(); |
+ EXPECT_EQ(3, ExportedApiCallCount()); |
+ |
+ // Double unhook should fail. |
+ EXPECT_EQ(ERROR_INVALID_PARAMETER, iat_hook.Unhook()); |
+ |
+ // Try hooking a non-existent function. |
+ EXPECT_EQ(ERROR_PROC_NOT_FOUND, |
+ iat_hook.Hook(::GetModuleHandle(nullptr), kIATTestDllName, |
+ "FooBarred", IATHookedExportedApi)); |
+} |
+ |
+} // namespace |