| 1 |
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 |
// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are |
| 6 |
// met: |
| 7 |
// |
| 8 |
// * Redistributions of source code must retain the above copyright |
| 9 |
// notice, this list of conditions and the following disclaimer. |
| 10 |
// * Redistributions in binary form must reproduce the above |
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
| 12 |
// in the documentation and/or other materials provided with the |
| 13 |
// distribution. |
| 14 |
// * Neither the name of Google Inc. nor the names of its |
| 15 |
// contributors may be used to endorse or promote products derived from |
| 16 |
// this software without specific prior written permission. |
| 17 |
// |
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
// |
| 30 |
// Utility for using SideStep with unit tests. |
| 31 |
|
| 32 |
#ifndef CEEE_TESTING_SIDESTEP_AUTO_TESTING_HOOK_H_ |
| 33 |
#define CEEE_TESTING_SIDESTEP_AUTO_TESTING_HOOK_H_ |
| 34 |
|
| 35 |
#include "base/basictypes.h" |
| 36 |
#include "base/logging.h" |
| 37 |
#include "preamble_patcher.h" |
| 38 |
|
| 39 |
#define SIDESTEP_CHK(x) CHECK(x) |
| 40 |
#define SIDESTEP_EXPECT_TRUE(x) SIDESTEP_CHK(x) |
| 41 |
|
| 42 |
namespace sidestep { |
| 43 |
|
| 44 |
// Same trick as common/scope_cleanup.h ScopeGuardImplBase |
| 45 |
class AutoTestingHookBase { |
| 46 |
public: |
| 47 |
virtual ~AutoTestingHookBase() {} |
| 48 |
}; |
| 49 |
|
| 50 |
// This is the typedef you normally use for the class, e.g. |
| 51 |
// |
| 52 |
// AutoTestingHook hook = MakeTestingHook(TargetFunc, HookTargetFunc); |
| 53 |
// |
| 54 |
// The 'hook' variable will then be destroyed when it goes out of scope. |
| 55 |
// |
| 56 |
// NOTE: You must not hold this type as a member of another class. Its |
| 57 |
// destructor will not get called. |
| 58 |
typedef const AutoTestingHookBase& AutoTestingHook; |
| 59 |
|
| 60 |
// This is the class you must use when holding a hook as a member of another |
| 61 |
// class, e.g. |
| 62 |
// |
| 63 |
// public: |
| 64 |
// AutoTestingHookHolder holder_; |
| 65 |
// MyClass() : my_hook_holder(MakeTestingHookHolder(Target, Hook)) {} |
| 66 |
class AutoTestingHookHolder { |
| 67 |
public: |
| 68 |
explicit AutoTestingHookHolder(AutoTestingHookBase* hook) : hook_(hook) {} |
| 69 |
~AutoTestingHookHolder() { delete hook_; } |
| 70 |
private: |
| 71 |
AutoTestingHookHolder() {} // disallow |
| 72 |
AutoTestingHookBase* hook_; |
| 73 |
}; |
| 74 |
|
| 75 |
// This class helps patch a function, then unpatch it when the object exits |
| 76 |
// scope, and also maintains the pointer to the original function stub. |
| 77 |
// |
| 78 |
// To enable use of the class without having to explicitly provide the |
| 79 |
// type of the function pointers (and instead only providing it |
| 80 |
// implicitly) we use the same trick as ScopeGuard (see |
| 81 |
// common/scope_cleanup.h) uses, so to create a hook you use the MakeHook |
| 82 |
// function rather than a constructor. |
| 83 |
// |
| 84 |
// NOTE: This function is only safe for e.g. unit tests and _not_ for |
| 85 |
// production code. See PreamblePatcher class for details. |
| 86 |
template <typename T> |
| 87 |
class AutoTestingHookImpl : public AutoTestingHookBase { |
| 88 |
public: |
| 89 |
static AutoTestingHookImpl<T> MakeTestingHook(T target_function, |
| 90 |
T replacement_function, |
| 91 |
bool do_it) { |
| 92 |
return AutoTestingHookImpl<T>(target_function, replacement_function, do_it); |
| 93 |
} |
| 94 |
|
| 95 |
static AutoTestingHookImpl<T>* MakeTestingHookHolder(T target_function, |
| 96 |
T replacement_function, |
| 97 |
bool do_it) { |
| 98 |
return new AutoTestingHookImpl<T>(target_function, |
| 99 |
replacement_function, do_it); |
| 100 |
} |
| 101 |
|
| 102 |
~AutoTestingHookImpl() { |
| 103 |
if (did_it_) { |
| 104 |
SIDESTEP_CHK(SIDESTEP_SUCCESS == PreamblePatcher::Unpatch( |
| 105 |
(void*)target_function_, (void*)replacement_function_, |
| 106 |
(void*)original_function_)); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
// Returns a pointer to the original function. To use this method you will |
| 111 |
// have to explicitly create an AutoTestingHookImpl of the specific |
| 112 |
// function pointer type (i.e. not use the AutoTestingHook typedef). |
| 113 |
T original_function() { |
| 114 |
return original_function_; |
| 115 |
} |
| 116 |
|
| 117 |
private: |
| 118 |
AutoTestingHookImpl(T target_function, T replacement_function, bool do_it) |
| 119 |
: target_function_(target_function), |
| 120 |
original_function_(NULL), |
| 121 |
replacement_function_(replacement_function), |
| 122 |
did_it_(do_it) { |
| 123 |
if (do_it) { |
| 124 |
SIDESTEP_CHK(SIDESTEP_SUCCESS == PreamblePatcher::Patch(target_function, |
| 125 |
replacement_function, |
| 126 |
&original_function_)); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
T target_function_; // always valid |
| 131 |
T original_function_; // always valid |
| 132 |
T replacement_function_; // always valid |
| 133 |
bool did_it_; // Remember if we did it or not... |
| 134 |
}; |
| 135 |
|
| 136 |
template <typename T> |
| 137 |
inline AutoTestingHookImpl<T> MakeTestingHook(T target, |
| 138 |
T replacement, |
| 139 |
bool do_it) { |
| 140 |
return AutoTestingHookImpl<T>::MakeTestingHook(target, replacement, do_it); |
| 141 |
} |
| 142 |
|
| 143 |
template <typename T> |
| 144 |
inline AutoTestingHookImpl<T> MakeTestingHook(T target, T replacement) { |
| 145 |
return AutoTestingHookImpl<T>::MakeTestingHook(target, replacement, true); |
| 146 |
} |
| 147 |
|
| 148 |
template <typename T> |
| 149 |
inline AutoTestingHookImpl<T>* MakeTestingHookHolder(T target, T replacement) { |
| 150 |
return AutoTestingHookImpl<T>::MakeTestingHookHolder(target, replacement, |
| 151 |
true); |
| 152 |
} |
| 153 |
|
| 154 |
}; // namespace sidestep |
| 155 |
|
| 156 |
#endif // CEEE_TESTING_SIDESTEP_AUTO_TESTING_HOOK_H_ |