Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Unified Diff: chrome_frame/test/function_stub_unittest.cc

Issue 218019: Initial import of the Chrome Frame codebase. Integration in chrome.gyp coming... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome_frame/test/data/version.html ('k') | chrome_frame/test/helper_gmock.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome_frame/test/function_stub_unittest.cc
===================================================================
--- chrome_frame/test/function_stub_unittest.cc (revision 0)
+++ chrome_frame/test/function_stub_unittest.cc (revision 0)
@@ -0,0 +1,66 @@
+// Copyright (c) 2009 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 "chrome_frame/function_stub.h"
+
+#include "chrome_frame/test/chrome_frame_unittests.h"
+
+#define NO_INLINE __declspec(noinline)
+
+namespace {
+
+typedef int (__stdcall* FooPrototype)();
+
+NO_INLINE int __stdcall Foo() {
+ return 1;
+}
+
+NO_INLINE int __stdcall PatchedFoo(FooPrototype original) {
+ return original() + 1;
+}
+
+} // end namespace
+
+TEST(PatchTests, FunctionStub) {
+ EXPECT_EQ(Foo(), 1);
+ // Create a function stub that calls PatchedFoo and supplies it with
+ // a pointer to Foo.
+ FunctionStub* stub = FunctionStub::Create(reinterpret_cast<uintptr_t>(&Foo),
+ &PatchedFoo);
+ EXPECT_TRUE(stub != NULL);
+ // Call the stub as it were Foo(). The call should get forwarded to Foo().
+ FooPrototype patch = reinterpret_cast<FooPrototype>(stub->code());
+ EXPECT_EQ(patch(), 2);
+ // Now neutralize the stub so that it calls Foo() directly without touching
+ // PatchedFoo().
+ // stub->BypassStub(&Foo);
+ stub->BypassStub(reinterpret_cast<void*>(stub->argument()));
+ EXPECT_EQ(patch(), 1);
+ // We're done with the stub.
+ FunctionStub::Destroy(stub);
+}
+
+// Basic tests to check the validity of a stub.
+TEST(PatchTests, FunctionStubCompare) {
+ EXPECT_EQ(Foo(), 1);
+
+ // Detect the absence of a stub
+ FunctionStub* stub = reinterpret_cast<FunctionStub*>(&Foo);
+ EXPECT_FALSE(stub->is_valid());
+
+ stub = FunctionStub::Create(reinterpret_cast<uintptr_t>(&Foo), &PatchedFoo);
+ EXPECT_TRUE(stub != NULL);
+ EXPECT_TRUE(stub->is_valid());
+
+ FooPrototype patch = reinterpret_cast<FooPrototype>(stub->code());
+ EXPECT_EQ(patch(), 2);
+
+ // See if we can get the correct absolute pointer to the hook function
+ // back from the stub.
+ EXPECT_EQ(stub->absolute_target(), reinterpret_cast<uintptr_t>(&PatchedFoo));
+
+ // Also verify that the argument being passed to the hook function is indeed
+ // the pointer to the original function (again, absolute not relative).
+ EXPECT_EQ(stub->argument(), reinterpret_cast<uintptr_t>(&Foo));
+}
« no previous file with comments | « chrome_frame/test/data/version.html ('k') | chrome_frame/test/helper_gmock.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698