OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 #include "chrome_frame/function_stub.h" | 6 #include "chrome_frame/function_stub.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 typedef uintptr_t (CALLBACK *FuncPtr1)(uintptr_t arg); | 67 typedef uintptr_t (CALLBACK *FuncPtr1)(uintptr_t arg); |
68 | 68 |
69 MOCK_METHOD0(Foo0, uintptr_t()); | 69 MOCK_METHOD0(Foo0, uintptr_t()); |
70 MOCK_METHOD1(Foo1, uintptr_t(uintptr_t)); | 70 MOCK_METHOD1(Foo1, uintptr_t(uintptr_t)); |
71 MOCK_METHOD0(Bar0, uintptr_t()); | 71 MOCK_METHOD0(Bar0, uintptr_t()); |
72 MOCK_METHOD1(Bar1, uintptr_t(uintptr_t)); | 72 MOCK_METHOD1(Bar1, uintptr_t(uintptr_t)); |
73 | 73 |
74 static uintptr_t CALLBACK FooCallback0(FunctionStubTest* test) { | 74 static uintptr_t CALLBACK FooCallback0(FunctionStubTest* test) { |
75 return test->Foo0(); | 75 return test->Foo0(); |
76 } | 76 } |
77 static uintptr_t CALLBACK FooCallback1(FunctionStubTest* test, | 77 static uintptr_t CALLBACK FooCallback1(FunctionStubTest* test, uintptr_t arg)
{ |
78 uintptr_t arg) { | |
79 return test->Foo1(arg); | 78 return test->Foo1(arg); |
80 } | 79 } |
81 static uintptr_t CALLBACK BarCallback0(FunctionStubTest* test) { | 80 static uintptr_t CALLBACK BarCallback0(FunctionStubTest* test) { |
82 return test->Foo0(); | 81 return test->Foo0(); |
83 } | 82 } |
84 static uintptr_t CALLBACK BarCallback1(FunctionStubTest* test, | 83 static uintptr_t CALLBACK BarCallback1(FunctionStubTest* test, uintptr_t arg)
{ |
85 uintptr_t arg) { | |
86 return test->Foo1(arg); | 84 return test->Foo1(arg); |
87 } | 85 } |
88 | 86 |
89 // If a stub is allocated during testing, assigning it here | 87 // If a stub is allocated during testing, assigning it here |
90 // will deallocate it at the end of test. | 88 // will deallocate it at the end of test. |
91 FunctionStub* stub_; | 89 FunctionStub* stub_; |
92 | 90 |
93 // playpen_[0 .. playpen_size_ - 1] is committed, writable memory. | 91 // playpen_[0 .. playpen_size_ - 1] is committed, writable memory. |
94 // playpen_[playpen_size_] is uncommitted, defined memory. | 92 // playpen_[playpen_size_] is uncommitted, defined memory. |
95 uint8* playpen_; | 93 uint8* playpen_; |
(...skipping 18 matching lines...) Expand all Loading... |
114 uintptr_t argument = reinterpret_cast<uintptr_t>(this); | 112 uintptr_t argument = reinterpret_cast<uintptr_t>(this); |
115 uintptr_t dest_fn = reinterpret_cast<uintptr_t>(FooDivert); | 113 uintptr_t dest_fn = reinterpret_cast<uintptr_t>(FooDivert); |
116 stub_ = FunctionStub::Create(argument, FooDivert); | 114 stub_ = FunctionStub::Create(argument, FooDivert); |
117 | 115 |
118 EXPECT_FALSE(stub_->is_bypassed()); | 116 EXPECT_FALSE(stub_->is_bypassed()); |
119 EXPECT_TRUE(stub_->is_valid()); | 117 EXPECT_TRUE(stub_->is_valid()); |
120 EXPECT_TRUE(stub_->code() != NULL); | 118 EXPECT_TRUE(stub_->code() != NULL); |
121 | 119 |
122 // Check that the stub code is executable. | 120 // Check that the stub code is executable. |
123 MEMORY_BASIC_INFORMATION info = {}; | 121 MEMORY_BASIC_INFORMATION info = {}; |
124 EXPECT_NE(0u, ::VirtualQuery(stub_->code(), &info, sizeof(info))); | 122 EXPECT_NE(0, ::VirtualQuery(stub_->code(), &info, sizeof(info))); |
125 const DWORD kExecutableMask = PAGE_EXECUTE | PAGE_EXECUTE_READ | | 123 const DWORD kExecutableMask = PAGE_EXECUTE | PAGE_EXECUTE_READ | |
126 PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; | 124 PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; |
127 EXPECT_NE(0u, info.Protect & kExecutableMask); | 125 EXPECT_NE(0, info.Protect & kExecutableMask); |
128 | 126 |
129 EXPECT_EQ(argument, stub_->argument()); | 127 EXPECT_EQ(argument, stub_->argument()); |
130 EXPECT_TRUE(stub_->bypass_address() != NULL); | 128 EXPECT_TRUE(stub_->bypass_address() != NULL); |
131 EXPECT_EQ(dest_fn, stub_->destination_function()); | 129 EXPECT_EQ(dest_fn, stub_->destination_function()); |
132 } | 130 } |
133 | 131 |
134 TEST_F(FunctionStubTest, ZeroArgumentStub) { | 132 TEST_F(FunctionStubTest, ZeroArgumentStub) { |
135 stub_ = FunctionStub::Create(reinterpret_cast<uintptr_t>(this), | 133 stub_ = FunctionStub::Create(reinterpret_cast<uintptr_t>(this), |
136 &FunctionStubTest::FooCallback0); | 134 &FunctionStubTest::FooCallback0); |
137 | 135 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 HMODULE my_module = NULL; | 198 HMODULE my_module = NULL; |
201 EXPECT_TRUE(::GetModuleHandleEx(kFlags, | 199 EXPECT_TRUE(::GetModuleHandleEx(kFlags, |
202 reinterpret_cast<const wchar_t*>(&kDivertedRetVal), | 200 reinterpret_cast<const wchar_t*>(&kDivertedRetVal), |
203 &my_module)); | 201 &my_module)); |
204 | 202 |
205 // Set our module as signature. | 203 // Set our module as signature. |
206 stub->set_signature(my_module); | 204 stub->set_signature(my_module); |
207 EXPECT_EQ(stub, FunctionStub::FromCode(stub)); | 205 EXPECT_EQ(stub, FunctionStub::FromCode(stub)); |
208 } | 206 } |
209 | 207 |
OLD | NEW |