1 : // Copyright 2013 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Unit-tests for the jump table count client.
16 :
17 : #include "syzygy/agent/jump_table_count/jump_table_count.h"
18 :
19 : #include "gmock/gmock.h"
20 : #include "gtest/gtest.h"
21 :
22 : namespace agent {
23 : namespace jump_table_count {
24 :
25 : namespace {
26 :
27 : const wchar_t kJumpTableCountClientDll[] = L"jump_table_count_client.dll";
28 :
29 : // The test fixture for the jump table count agent.
30 : class JumpTableCountTest : public testing::Test {
31 : public:
32 E : JumpTableCountTest()
33 : : agent_module_(NULL) { }
34 :
35 E : void LoadDll() {
36 E : ASSERT_EQ(NULL, agent_module_);
37 E : ASSERT_EQ(NULL, jump_table_case_counter_stub_);
38 E : ASSERT_EQ(NULL, ::GetModuleHandle(kJumpTableCountClientDll));
39 :
40 E : agent_module_ = ::LoadLibrary(kJumpTableCountClientDll);
41 E : ASSERT_TRUE(agent_module_ != NULL);
42 :
43 : jump_table_case_counter_stub_ =
44 E : ::GetProcAddress(agent_module_, "_jump_table_case_counter");
45 E : ASSERT_TRUE(jump_table_case_counter_stub_ != NULL);
46 :
47 : indirect_penter_dllmain_stub_ =
48 E : ::GetProcAddress(agent_module_, "_indirect_penter_dllmain");
49 E : ASSERT_TRUE(indirect_penter_dllmain_stub_ != NULL);
50 :
51 : indirect_penter_exemain_stub_ =
52 E : ::GetProcAddress(agent_module_, "_indirect_penter_exemain");
53 E : ASSERT_TRUE(indirect_penter_exemain_stub_ != NULL);
54 E : }
55 :
56 E : void UnloadDll() {
57 E : if (agent_module_ != NULL) {
58 E : ASSERT_TRUE(::FreeLibrary(agent_module_));
59 E : agent_module_ = NULL;
60 E : jump_table_case_counter_stub_ = NULL;
61 E : indirect_penter_dllmain_stub_ = NULL;
62 E : indirect_penter_exemain_stub_ = NULL;
63 : }
64 E : }
65 :
66 : protected:
67 : // The jump table count client module.
68 : HMODULE agent_module_;
69 :
70 : // The jump table case counter hook.
71 : static FARPROC jump_table_case_counter_stub_;
72 :
73 : // The DllMain entry stub.
74 : static FARPROC indirect_penter_dllmain_stub_;
75 :
76 : // The ExeMain entry stub.
77 : static FARPROC indirect_penter_exemain_stub_;
78 : };
79 :
80 : FARPROC JumpTableCountTest::jump_table_case_counter_stub_ = NULL;
81 : FARPROC JumpTableCountTest::indirect_penter_dllmain_stub_ = NULL;
82 : FARPROC JumpTableCountTest::indirect_penter_exemain_stub_ = NULL;
83 :
84 : } // namespace
85 :
86 E : TEST_F(JumpTableCountTest, LoadUnload) {
87 E : ASSERT_NO_FATAL_FAILURE(LoadDll());
88 E : ASSERT_NO_FATAL_FAILURE(UnloadDll());
89 E : }
90 :
91 : } // namespace jump_table_count
92 : } // namespace agent
|