1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
// Copyright (c) 2010 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.
//
// Utility for using SideStep with unit tests.
#ifndef CEEE_TESTING_SIDESTEP_AUTO_TESTING_HOOK_H_
#define CEEE_TESTING_SIDESTEP_AUTO_TESTING_HOOK_H_
#include "base/basictypes.h"
#include "ceee/testing/sidestep/integration.h"
#include "ceee/testing/sidestep/preamble_patcher.h"
namespace sidestep {
// Same trick as common/scope_cleanup.h ScopeGuardImplBase
class AutoTestingHookBase {
public:
virtual ~AutoTestingHookBase() {}
};
// This is the typedef you normally use for the class, e.g.
//
// AutoTestingHook hook = MakeTestingHook(TargetFunc, HookTargetFunc);
//
// The 'hook' variable will then be destroyed when it goes out of scope.
//
// NOTE: You must not hold this type as a member of another class. Its
// destructor will not get called.
typedef const AutoTestingHookBase& AutoTestingHook;
// This is the class you must use when holding a hook as a member of another
// class, e.g.
//
// public:
// AutoTestingHookHolder holder_;
// MyClass() : my_hook_holder(MakeTestingHookHolder(Target, Hook)) {}
class AutoTestingHookHolder {
public:
explicit AutoTestingHookHolder(AutoTestingHookBase* hook) : hook_(hook) {}
~AutoTestingHookHolder() { delete hook_; }
private:
AutoTestingHookHolder() {} // disallow
AutoTestingHookBase* hook_;
};
// This class helps patch a function, then unpatch it when the object exits
// scope, and also maintains the pointer to the original function stub.
//
// To enable use of the class without having to explicitly provide the
// type of the function pointers (and instead only providing it
// implicitly) we use the same trick as ScopeGuard (see
// common/scope_cleanup.h) uses, so to create a hook you use the MakeHook
// function rather than a constructor.
//
// NOTE: This function is only safe for e.g. unit tests and _not_ for
// production code. See PreamblePatcher class for details.
template <typename T>
class AutoTestingHookImpl : public AutoTestingHookBase {
public:
static AutoTestingHookImpl<T> MakeTestingHook(T target_function,
T replacement_function,
bool do_it) {
return AutoTestingHookImpl<T>(target_function, replacement_function, do_it);
}
static AutoTestingHookImpl<T>* MakeTestingHookHolder(T target_function,
T replacement_function,
bool do_it) {
return new AutoTestingHookImpl<T>(target_function,
replacement_function, do_it);
}
~AutoTestingHookImpl() {
if (did_it_) {
SIDESTEP_CHK(SIDESTEP_SUCCESS == PreamblePatcher::Unpatch(
target_function_, replacement_function_,
original_function_));
}
}
// Returns a pointer to the original function. To use this method you will
// have to explicitly create an AutoTestingHookImpl of the specific
// function pointer type (i.e. not use the AutoTestingHook typedef).
T original_function() {
return original_function_;
}
private:
AutoTestingHookImpl(T target_function, T replacement_function, bool do_it)
: target_function_(target_function),
replacement_function_(replacement_function),
original_function_(NULL),
did_it_(do_it) {
if (do_it) {
SIDESTEP_CHK(SIDESTEP_SUCCESS == PreamblePatcher::Patch(target_function,
replacement_function,
&original_function_));
}
}
T target_function_; // always valid
T original_function_; // always valid
T replacement_function_; // always valid
bool did_it_; // Remember if we did it or not...
};
template <typename T>
inline AutoTestingHookImpl<T> MakeTestingHook(T target,
T replacement,
bool do_it) {
return AutoTestingHookImpl<T>::MakeTestingHook(target, replacement, do_it);
}
template <typename T>
inline AutoTestingHookImpl<T> MakeTestingHook(T target, T replacement) {
return AutoTestingHookImpl<T>::MakeTestingHook(target, replacement, true);
}
template <typename T>
inline AutoTestingHookImpl<T>* MakeTestingHookHolder(T target, T replacement) {
return AutoTestingHookImpl<T>::MakeTestingHookHolder(target, replacement,
true);
}
}; // namespace sidestep
#endif // CEEE_TESTING_SIDESTEP_AUTO_TESTING_HOOK_H_
|