common_test_class.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (C) 2013 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 COMMON_HOOKS_TEST_CLASS_H
  15. #define COMMON_HOOKS_TEST_CLASS_H
  16. #include <hooks/callout_handle.h>
  17. #include <hooks/callout_manager.h>
  18. #include <hooks/server_hooks.h>
  19. #include <hooks/tests/marker_file.h>
  20. #include <boost/shared_ptr.hpp>
  21. #include <gtest/gtest.h>
  22. /// @brief Common hooks test class
  23. ///
  24. /// This class is a shared parent of the test fixture class in the tests of the
  25. /// higher-level hooks classes (LibraryManager, LibraryManagerCollection and
  26. /// HooksManager). It
  27. ///
  28. /// - sets the the ServerHooks object with three hooks and stores their
  29. /// indexes.
  30. /// - executes the callouts (which are assumed to perform a calculation)
  31. /// and checks the results.
  32. class HooksCommonTestClass {
  33. public:
  34. /// @brief Constructor
  35. HooksCommonTestClass() {
  36. // Set up the server hooks. ServerHooks is a singleton, so we reset it
  37. // between each test.
  38. isc::hooks::ServerHooks& hooks =
  39. isc::hooks::ServerHooks::getServerHooks();
  40. hooks.reset();
  41. hookpt_one_index_ = hooks.registerHook("hookpt_one");
  42. hookpt_two_index_ = hooks.registerHook("hookpt_two");
  43. hookpt_three_index_ = hooks.registerHook("hookpt_three");
  44. }
  45. /// @brief Call callouts test
  46. ///
  47. /// All of the loaded libraries for which callouts are called register four
  48. /// callouts: a context_create callout and three callouts that are attached
  49. /// to hooks hookpt_one, hookpt_two and hookpt_three. These four callouts,
  50. /// executed in sequence, perform a series of calculations. Data is passed
  51. /// between callouts in the argument list, in a variable named "result".
  52. ///
  53. /// context_create initializes the calculation by setting a seed
  54. /// value, called r0 here. This value is dependent on the library being
  55. /// loaded. Prior to that, the argument "result" is initialized to -1,
  56. /// the purpose being to avoid exceptions when running this test with no
  57. /// libraries loaded.
  58. ///
  59. /// Callout hookpt_one is passed a value d1 and performs a simple arithmetic
  60. /// operation on it and r0 yielding a result r1. Hence we can say that
  61. /// @f[ r1 = hookpt_one(r0, d1) @f]
  62. ///
  63. /// Callout hookpt_two is passed a value d2 and performs another simple
  64. /// arithmetic operation on it and d2, yielding r2, i.e.
  65. /// @f[ r2 = hookpt_two(d1, d2) @f]
  66. ///
  67. /// hookpt_three does a similar operation giving
  68. /// @f[ r3 = hookpt_three(r2, d3) @f].
  69. ///
  70. /// The details of the operations hookpt_one, hookpt_two and hookpt_three
  71. /// depend on the library, so the results obtained not only depend on
  72. /// the data, but also on the library loaded. This method is passed both
  73. /// data and expected results. It executes the three callouts in sequence,
  74. /// checking the intermediate and final results. Only if the expected
  75. /// library has been loaded correctly and the callouts in it registered
  76. /// correctly will be the results be as expected.
  77. ///
  78. /// It is assumed that callout_manager_ has been set up appropriately.
  79. ///
  80. /// @note The CalloutHandle used in the calls is declared locally here.
  81. /// The advantage of this (apart from scope reduction) is that on
  82. /// exit, it is destroyed. This removes any references to memory
  83. /// allocated by loaded libraries while they are still loaded.
  84. ///
  85. /// @param manager CalloutManager to use for the test
  86. /// @param r0...r3, d1..d3 Data (dN) and expected results (rN) - both
  87. /// intermediate and final. The arguments are ordered so that they
  88. /// appear in the argument list in the order they are used.
  89. void executeCallCallouts(
  90. const boost::shared_ptr<isc::hooks::CalloutManager>& manager,
  91. int r0, int d1, int r1, int d2, int r2, int d3, int r3) {
  92. static const char* COMMON_TEXT = " callout returned the wrong value";
  93. static const char* RESULT = "result";
  94. int result;
  95. // Set up a callout handle for the calls.
  96. isc::hooks::CalloutHandle handle(manager);
  97. // Initialize the argument RESULT. This simplifies testing by
  98. // eliminating the generation of an exception when we try the unload
  99. // test. In that case, RESULT is unchanged.
  100. handle.setArgument(RESULT, -1);
  101. // Seed the calculation.
  102. manager->callCallouts(isc::hooks::ServerHooks::CONTEXT_CREATE, handle);
  103. handle.getArgument(RESULT, result);
  104. EXPECT_EQ(r0, result) << "context_create" << COMMON_TEXT;
  105. // Perform the first calculation.
  106. handle.setArgument("data_1", d1);
  107. manager->callCallouts(hookpt_one_index_, handle);
  108. handle.getArgument(RESULT, result);
  109. EXPECT_EQ(r1, result) << "hookpt_one" << COMMON_TEXT;
  110. // ... the second ...
  111. handle.setArgument("data_2", d2);
  112. manager->callCallouts(hookpt_two_index_, handle);
  113. handle.getArgument(RESULT, result);
  114. EXPECT_EQ(r2, result) << "hookpt_two" << COMMON_TEXT;
  115. // ... and the third.
  116. handle.setArgument("data_3", d3);
  117. manager->callCallouts(hookpt_three_index_, handle);
  118. handle.getArgument(RESULT, result);
  119. EXPECT_EQ(r3, result) << "hookpt_three" << COMMON_TEXT;
  120. }
  121. /// Hook indexes. These are are made public for ease of reference.
  122. int hookpt_one_index_;
  123. int hookpt_two_index_;
  124. int hookpt_three_index_;
  125. };
  126. #endif // COMMON_HOOKS_TEST_CLASS_H