library_manager_unittest.cc.in 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #include <hooks/callout_handle.h>
  15. #include <hooks/callout_manager.h>
  16. #include <hooks/library_manager.h>
  17. #include <hooks/server_hooks.h>
  18. #include <gtest/gtest.h>
  19. #include <algorithm>
  20. #include <string>
  21. #include <vector>
  22. #include <iostream>
  23. using namespace isc;
  24. using namespace isc::hooks;
  25. using namespace std;
  26. /// @brief Library manager test class
  27. class LibraryManagerTest : public ::testing::Test {
  28. public:
  29. /// @brief Constructor
  30. ///
  31. /// Sets up a collection of three LibraryHandle objects to use in the test.
  32. LibraryManagerTest() {
  33. // Set up the server hooks. There is sone singleton for all tests,
  34. // so reset it and explicitly set up the hooks for the test.
  35. ServerHooks& hooks = ServerHooks::getServerHooks();
  36. hooks.reset();
  37. lm_one_index_ = hooks.registerHook("lm_one");
  38. lm_two_index_ = hooks.registerHook("lm_two");
  39. lm_three_index_ = hooks.registerHook("lm_three");
  40. // Set up the callout manager with these hooks. Assume a maximum of
  41. // four libraries.
  42. callout_manager_.reset(new CalloutManager(1));
  43. // Set up the callout handle.
  44. callout_handle_.reset(new CalloutHandle(callout_manager_));
  45. }
  46. /// Hook indexes. These are somewhat ubiquitous, so are made public for
  47. /// ease of reference instead of being accessible by a function.
  48. int lm_one_index_;
  49. int lm_two_index_;
  50. int lm_three_index_;
  51. /// Callout handle used in calls
  52. boost::shared_ptr<CalloutHandle> callout_handle_;
  53. /// Callout manager used for the test
  54. boost::shared_ptr<CalloutManager> callout_manager_;
  55. };
  56. /// @brief Library manager class
  57. ///
  58. /// This is an instance of the LibraryManager class but with the protected
  59. /// methods made public for test purposes.
  60. class PublicLibraryManager : public isc::hooks::LibraryManager {
  61. public:
  62. /// @brief Constructor
  63. ///
  64. /// Stores the library name. The actual loading is done in loadLibrary().
  65. ///
  66. /// @param name Name of the library to load. This should be an absolute
  67. /// path name.
  68. /// @param index Index of this library. For all these tests, it will be
  69. /// zero, as we are only using one library.
  70. /// @param manager CalloutManager object
  71. PublicLibraryManager(const std::string& name, int index,
  72. const boost::shared_ptr<CalloutManager>& manager)
  73. : LibraryManager(name, index, manager)
  74. {}
  75. /// @brief Destructor
  76. ///
  77. /// Ensures that the library is closed after the test.
  78. ~PublicLibraryManager() {
  79. static_cast<void>(closeLibrary());
  80. }
  81. /// Public methods that call protected methods on the superclass.
  82. using LibraryManager::openLibrary;
  83. using LibraryManager::closeLibrary;
  84. using LibraryManager::checkVersion;
  85. using LibraryManager::registerStandardCallouts;
  86. };
  87. // Names of the libraries used in these tests. These libraries are built using
  88. // libtool, so we need to look in the hidden ".libs" directory to locate the
  89. // .so file. Note that we access the .so file - libtool creates this as a
  90. // like to the real shared library.
  91. static const char* NOT_PRESENT_LIBRARY = "@abs_builddir@/.libs/libnothere.so";
  92. static const char* NO_VERSION_LIBRARY = "@abs_builddir@/.libs/libnv.so";
  93. static const char* INCORRECT_VERSION_LIBRARY = "@abs_builddir@/.libs/libiv.so";
  94. static const char* BASIC_CALLOUT_LIBRARY = "@abs_builddir@/.libs/libbco.so";
  95. namespace {
  96. // Tests that OpenLibrary reports an error for an unknown library.
  97. TEST_F(LibraryManagerTest, NonExistentLibrary) {
  98. PublicLibraryManager lib_manager(std::string(NOT_PRESENT_LIBRARY),
  99. 0, callout_manager_);
  100. EXPECT_FALSE(lib_manager.openLibrary());
  101. }
  102. // Tests that the openLibrary() and closeLibrary() methods work.
  103. TEST_F(LibraryManagerTest, OpenClose) {
  104. PublicLibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY),
  105. 0, callout_manager_);
  106. // Open and close the library
  107. EXPECT_TRUE(lib_manager.openLibrary());
  108. EXPECT_TRUE(lib_manager.closeLibrary());
  109. // Check that a second close does not report an error.
  110. EXPECT_TRUE(lib_manager.closeLibrary());
  111. }
  112. // Check that the code handles the case of a library with no version function.
  113. TEST_F(LibraryManagerTest, NoVersionFunction) {
  114. PublicLibraryManager lib_manager(std::string(NO_VERSION_LIBRARY),
  115. 0, callout_manager_);
  116. // Open should succeed.
  117. EXPECT_TRUE(lib_manager.openLibrary());
  118. // Version check should fail.
  119. EXPECT_FALSE(lib_manager.checkVersion());
  120. // Tidy up.
  121. EXPECT_TRUE(lib_manager.closeLibrary());
  122. }
  123. // Check that the code handles the case of a library with a version function
  124. // that returns an incorrect version number.
  125. TEST_F(LibraryManagerTest, IncorrectVersionReturned) {
  126. PublicLibraryManager lib_manager(std::string(INCORRECT_VERSION_LIBRARY),
  127. 0, callout_manager_);
  128. // Open should succeed.
  129. EXPECT_TRUE(lib_manager.openLibrary());
  130. // Version check should fail.
  131. EXPECT_FALSE(lib_manager.checkVersion());
  132. // Tidy up.
  133. EXPECT_TRUE(lib_manager.closeLibrary());
  134. }
  135. // Tests that checkVersion() function succeeds in the case of a library with a
  136. // version function that returns the correct version number.
  137. TEST_F(LibraryManagerTest, CorrectVersionReturned) {
  138. PublicLibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY),
  139. 0, callout_manager_);
  140. // Open should succeed.
  141. EXPECT_TRUE(lib_manager.openLibrary());
  142. // Version check should succeed.
  143. EXPECT_TRUE(lib_manager.checkVersion());
  144. // Tidy up.
  145. EXPECT_TRUE(lib_manager.closeLibrary());
  146. }
  147. // Checks the registration of standard callouts.
  148. TEST_F(LibraryManagerTest, RegisterStandardCallouts) {
  149. // Load the only library, specifying the index of 0 as it's the only
  150. // library. This should load all callouts.
  151. PublicLibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY),
  152. 0, callout_manager_);
  153. EXPECT_TRUE(lib_manager.openLibrary());
  154. // Check the version of the library.
  155. EXPECT_TRUE(lib_manager.checkVersion());
  156. // Load the standard callouts
  157. EXPECT_NO_THROW(lib_manager.registerStandardCallouts());
  158. int result = 0;
  159. // Now execute the callouts in the order expected. context_create
  160. // always comes first. This sets the context value to 10.
  161. callout_manager_->callCallouts(ServerHooks::CONTEXT_CREATE,
  162. *callout_handle_);
  163. // First callout adds 5 to the context value.
  164. callout_handle_->setArgument("data_1", static_cast<int>(5));
  165. callout_manager_->callCallouts(lm_one_index_, *callout_handle_);
  166. callout_handle_->getArgument("result", result);
  167. EXPECT_EQ(15, result);
  168. // Second callout multiples the context value by 7
  169. callout_handle_->setArgument("data_2", static_cast<int>(7));
  170. callout_manager_->callCallouts(lm_two_index_, *callout_handle_);
  171. callout_handle_->getArgument("result", result);
  172. EXPECT_EQ(105, result);
  173. // Third callout subtracts 17.
  174. callout_handle_->setArgument("data_3", static_cast<int>(17));
  175. callout_manager_->callCallouts(lm_three_index_, *callout_handle_);
  176. callout_handle_->getArgument("result", result);
  177. EXPECT_EQ(88, result);
  178. // Explicitly clear the callout_handle_ so that we can delete the library.
  179. // This is the only object to contain memory allocated by it.
  180. callout_handle_.reset();
  181. // Tidy up
  182. EXPECT_TRUE(lib_manager.closeLibrary());
  183. }
  184. } // Anonymous namespace