OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 <windows.h> | |
6 | |
7 #include "chrome_elf/hook_util/hook_util.h" | |
8 // Compile in this test DLL, so that it's in the IAT. | |
9 #include "chrome_elf/hook_util/test/hook_util_test_dll.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace { | |
13 | |
14 // IATHook test constants. | |
15 const char kIATTestDllName[] = "hook_util_test_dll.dll"; | |
16 const char kIATExportedApiFunction[] = "ExportedApi"; | |
17 | |
18 // IATHook function, which does nothing. | |
19 void IATHookedExportedApi() { | |
20 return; | |
21 } | |
22 | |
23 class HookTest : public testing::Test { | |
24 protected: | |
25 HookTest() {} | |
26 }; | |
27 | |
28 //------------------------------------------------------------------------------ | |
29 // IATHook tests | |
30 //------------------------------------------------------------------------------ | |
31 | |
32 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
| |
33 elf_hook::IATHook iat_hook; | |
34 | |
35 // Sanity test with no hook. | |
36 ASSERT_EQ(0, ExportedApiCallCount()); | |
37 ExportedApi(); | |
38 ExportedApi(); | |
39 ASSERT_EQ(2, ExportedApiCallCount()); | |
40 | |
41 // Apply IAT hook. | |
42 ASSERT_EQ(NO_ERROR, | |
43 iat_hook.Hook(::GetModuleHandle(nullptr), kIATTestDllName, | |
44 kIATExportedApiFunction, IATHookedExportedApi)); | |
45 | |
46 // Call count should not change with hook. | |
47 ExportedApi(); | |
48 ExportedApi(); | |
49 ExportedApi(); | |
50 EXPECT_EQ(2, ExportedApiCallCount()); | |
51 | |
52 // Remove hook. | |
53 EXPECT_EQ(NO_ERROR, iat_hook.Unhook()); | |
54 | |
55 // Sanity test things are back to normal. | |
56 ExportedApi(); | |
57 EXPECT_EQ(3, ExportedApiCallCount()); | |
58 | |
59 // Double unhook should fail. | |
60 EXPECT_EQ(ERROR_INVALID_PARAMETER, iat_hook.Unhook()); | |
61 | |
62 // Try hooking a non-existent function. | |
63 EXPECT_EQ(ERROR_PROC_NOT_FOUND, | |
64 iat_hook.Hook(::GetModuleHandle(nullptr), kIATTestDllName, | |
65 "FooBarred", IATHookedExportedApi)); | |
66 } | |
67 | |
68 } // namespace | |
OLD | NEW |