auto_testing_hook.h 6.1 KB

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