| 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, uintptr_t arg)
{ | 77 static uintptr_t CALLBACK FooCallback1(FunctionStubTest* test, |
| 78 uintptr_t arg) { |
| 78 return test->Foo1(arg); | 79 return test->Foo1(arg); |
| 79 } | 80 } |
| 80 static uintptr_t CALLBACK BarCallback0(FunctionStubTest* test) { | 81 static uintptr_t CALLBACK BarCallback0(FunctionStubTest* test) { |
| 81 return test->Foo0(); | 82 return test->Foo0(); |
| 82 } | 83 } |
| 83 static uintptr_t CALLBACK BarCallback1(FunctionStubTest* test, uintptr_t arg)
{ | 84 static uintptr_t CALLBACK BarCallback1(FunctionStubTest* test, |
| 85 uintptr_t arg) { |
| 84 return test->Foo1(arg); | 86 return test->Foo1(arg); |
| 85 } | 87 } |
| 86 | 88 |
| 87 // If a stub is allocated during testing, assigning it here | 89 // If a stub is allocated during testing, assigning it here |
| 88 // will deallocate it at the end of test. | 90 // will deallocate it at the end of test. |
| 89 FunctionStub* stub_; | 91 FunctionStub* stub_; |
| 90 | 92 |
| 91 // playpen_[0 .. playpen_size_ - 1] is committed, writable memory. | 93 // playpen_[0 .. playpen_size_ - 1] is committed, writable memory. |
| 92 // playpen_[playpen_size_] is uncommitted, defined memory. | 94 // playpen_[playpen_size_] is uncommitted, defined memory. |
| 93 uint8* playpen_; | 95 uint8* playpen_; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 112 uintptr_t argument = reinterpret_cast<uintptr_t>(this); | 114 uintptr_t argument = reinterpret_cast<uintptr_t>(this); |
| 113 uintptr_t dest_fn = reinterpret_cast<uintptr_t>(FooDivert); | 115 uintptr_t dest_fn = reinterpret_cast<uintptr_t>(FooDivert); |
| 114 stub_ = FunctionStub::Create(argument, FooDivert); | 116 stub_ = FunctionStub::Create(argument, FooDivert); |
| 115 | 117 |
| 116 EXPECT_FALSE(stub_->is_bypassed()); | 118 EXPECT_FALSE(stub_->is_bypassed()); |
| 117 EXPECT_TRUE(stub_->is_valid()); | 119 EXPECT_TRUE(stub_->is_valid()); |
| 118 EXPECT_TRUE(stub_->code() != NULL); | 120 EXPECT_TRUE(stub_->code() != NULL); |
| 119 | 121 |
| 120 // Check that the stub code is executable. | 122 // Check that the stub code is executable. |
| 121 MEMORY_BASIC_INFORMATION info = {}; | 123 MEMORY_BASIC_INFORMATION info = {}; |
| 122 EXPECT_NE(0, ::VirtualQuery(stub_->code(), &info, sizeof(info))); | 124 EXPECT_NE(0u, ::VirtualQuery(stub_->code(), &info, sizeof(info))); |
| 123 const DWORD kExecutableMask = PAGE_EXECUTE | PAGE_EXECUTE_READ | | 125 const DWORD kExecutableMask = PAGE_EXECUTE | PAGE_EXECUTE_READ | |
| 124 PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; | 126 PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; |
| 125 EXPECT_NE(0, info.Protect & kExecutableMask); | 127 EXPECT_NE(0u, info.Protect & kExecutableMask); |
| 126 | 128 |
| 127 EXPECT_EQ(argument, stub_->argument()); | 129 EXPECT_EQ(argument, stub_->argument()); |
| 128 EXPECT_TRUE(stub_->bypass_address() != NULL); | 130 EXPECT_TRUE(stub_->bypass_address() != NULL); |
| 129 EXPECT_EQ(dest_fn, stub_->destination_function()); | 131 EXPECT_EQ(dest_fn, stub_->destination_function()); |
| 130 } | 132 } |
| 131 | 133 |
| 132 TEST_F(FunctionStubTest, ZeroArgumentStub) { | 134 TEST_F(FunctionStubTest, ZeroArgumentStub) { |
| 133 stub_ = FunctionStub::Create(reinterpret_cast<uintptr_t>(this), | 135 stub_ = FunctionStub::Create(reinterpret_cast<uintptr_t>(this), |
| 134 &FunctionStubTest::FooCallback0); | 136 &FunctionStubTest::FooCallback0); |
| 135 | 137 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 HMODULE my_module = NULL; | 200 HMODULE my_module = NULL; |
| 199 EXPECT_TRUE(::GetModuleHandleEx(kFlags, | 201 EXPECT_TRUE(::GetModuleHandleEx(kFlags, |
| 200 reinterpret_cast<const wchar_t*>(&kDivertedRetVal), | 202 reinterpret_cast<const wchar_t*>(&kDivertedRetVal), |
| 201 &my_module)); | 203 &my_module)); |
| 202 | 204 |
| 203 // Set our module as signature. | 205 // Set our module as signature. |
| 204 stub->set_signature(my_module); | 206 stub->set_signature(my_module); |
| 205 EXPECT_EQ(stub, FunctionStub::FromCode(stub)); | 207 EXPECT_EQ(stub, FunctionStub::FromCode(stub)); |
| 206 } | 208 } |
| 207 | 209 |
| OLD | NEW |