newhook.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __UTIL_UNITTESTS_NEWHOOK_H
  15. #define __UTIL_UNITTESTS_NEWHOOK_H 1
  16. /**
  17. * \file newhook.h
  18. * \brief Enable the use of special operator new that throws for testing.
  19. *
  20. * This small utility allows a test case to force the global operator new
  21. * to throw for a given size to test a case where memory allocation fails
  22. * (which normally doesn't happen). To enable the feature, everything must
  23. * be built with defining ENABLE_CUSTOM_OPERATOR_NEW beforehand, and set
  24. * \c force_throw_on_new to \c true and \c throw_size_on_new to the size
  25. * of data that should trigger the exception, immediately before starting
  26. * the specific test that needs the exception.
  27. *
  28. * Example:
  29. * \code #include <util/unittests/newhook.h>
  30. * ...
  31. * TEST(SomeTest, newException) {
  32. * isc::util::unittests::force_throw_on_new = true;
  33. * isc::util::unittests::throw_size_on_new = sizeof(Foo);
  34. * try {
  35. * // this will do 'new Foo()' internally and should throw
  36. * createFoo();
  37. * isc::util::unittests::force_throw_on_new = false;
  38. * ASSERT_FALSE(true) << "Expected throw on new";
  39. * } catch (const std::bad_alloc&) {
  40. * isc::util::unittests::force_throw_on_new = false;
  41. * // do some integrity check, etc, if necessary
  42. * }
  43. * } \endcode
  44. *
  45. * Replacing the global operator new (and delete) is a dangerous technique,
  46. * and triggering an exception solely based on the allocation size is not
  47. * reliable, so this feature is disabled by default two-fold: The
  48. * ENABLE_CUSTOM_OPERATOR_NEW build time variable, and run-time
  49. * \c force_throw_on_new.
  50. */
  51. namespace isc {
  52. namespace util {
  53. namespace unittests {
  54. /// Switch to enable the use of special operator new
  55. ///
  56. /// This is set to \c false by default.
  57. extern bool force_throw_on_new;
  58. /// The allocation size that triggers an exception in the special operator new
  59. ///
  60. /// This is the exact size that causes an exception to be thrown;
  61. /// for example, if it is set to 100, an attempt of allocating 100 bytes
  62. /// will result in an exception, but allocation attempt for 101 bytes won't
  63. /// (unless, of course, memory is really exhausted and allocation really
  64. /// fails).
  65. ///
  66. /// The default value is 0. The value of this variable has no meaning
  67. /// unless the use of the special operator is enabled at build time and
  68. /// via \c force_throw_on_new.
  69. extern size_t throw_size_on_new;
  70. }
  71. }
  72. }
  73. #endif // __UTIL_UNITTESTS_NEWHOOK_H
  74. // Local Variables:
  75. // mode: c++
  76. // End: