library_manager_collection_unittest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/library_manager_collection.h>
  18. #include <hooks/tests/common_test_class.h>
  19. #include <hooks/tests/test_libraries.h>
  20. #include <boost/shared_ptr.hpp>
  21. #include <gtest/gtest.h>
  22. #include <algorithm>
  23. #include <string>
  24. using namespace isc;
  25. using namespace isc::hooks;
  26. using namespace std;
  27. namespace {
  28. /// @brief Library manager collection test class
  29. class LibraryManagerCollectionTest : public ::testing::Test,
  30. public HooksCommonTestClass {
  31. };
  32. /// @brief Public library manager collection class
  33. ///
  34. /// This is an instance of the LibraryManagerCollection class but with the
  35. /// protected methods made public for test purposes.
  36. class PublicLibraryManagerCollection
  37. : public isc::hooks::LibraryManagerCollection {
  38. public:
  39. /// @brief Constructor
  40. ///
  41. /// @param List of libraries that this collection will manage. The order
  42. /// of the libraries is important.
  43. PublicLibraryManagerCollection(const std::vector<std::string>& libraries)
  44. : LibraryManagerCollection(libraries)
  45. {}
  46. /// Public methods that call protected methods on the superclass.
  47. using LibraryManagerCollection::unloadLibraries;
  48. };
  49. // This is effectively the same test as for LibraryManager, but using the
  50. // LibraryManagerCollection object.
  51. TEST_F(LibraryManagerCollectionTest, LoadLibraries) {
  52. // Set up the list of libraries to be loaded.
  53. std::vector<std::string> library_names;
  54. library_names.push_back(std::string(FULL_CALLOUT_LIBRARY));
  55. library_names.push_back(std::string(BASIC_CALLOUT_LIBRARY));
  56. // Set up the library manager collection and get the callout manager we'll
  57. // be using.
  58. PublicLibraryManagerCollection lm_collection(library_names);
  59. // Load the libraries.
  60. EXPECT_TRUE(lm_collection.loadLibraries());
  61. boost::shared_ptr<CalloutManager> manager =
  62. lm_collection.getCalloutManager();
  63. // Execute the callouts. The first library implements the calculation.
  64. //
  65. // r3 = (7 * d1 - d2) * d3
  66. //
  67. // The last-loaded library implements the calculation
  68. //
  69. // r3 = (10 + d1) * d2 - d3
  70. //
  71. // Putting the processing for each library together in the appropriate
  72. // order, we get:
  73. //
  74. // r3 = ((10 * d1 + d1) - d2) * d2 * d3 - d3
  75. {
  76. SCOPED_TRACE("Doing calculation with libraries loaded");
  77. executeCallCallouts(manager, 10, 3, 33, 2, 62, 3, 183);
  78. }
  79. // Try unloading the libraries.
  80. EXPECT_NO_THROW(lm_collection.unloadLibraries());
  81. // Re-execute the calculation - callouts can be called but as nothing
  82. // happens, the result should always be -1.
  83. {
  84. SCOPED_TRACE("Doing calculation with libraries not loaded");
  85. executeCallCallouts(manager, -1, 3, -1, 22, -1, 83, -1);
  86. }
  87. }
  88. // This is effectively the same test as above, but with a library generating
  89. // an error when loaded. It is expected that the failing library will not be
  90. // loaded, but others will be.
  91. TEST_F(LibraryManagerCollectionTest, LoadLibrariesWithError) {
  92. // Set up the list of libraries to be loaded.
  93. std::vector<std::string> library_names;
  94. library_names.push_back(std::string(FULL_CALLOUT_LIBRARY));
  95. library_names.push_back(std::string(INCORRECT_VERSION_LIBRARY));
  96. library_names.push_back(std::string(BASIC_CALLOUT_LIBRARY));
  97. // Set up the library manager collection and get the callout manager we'll
  98. // be using.
  99. PublicLibraryManagerCollection lm_collection(library_names);
  100. // Load the libraries. We expect a failure status to be returned as
  101. // one of the libraries failed to load.
  102. EXPECT_FALSE(lm_collection.loadLibraries());
  103. boost::shared_ptr<CalloutManager> manager =
  104. lm_collection.getCalloutManager();
  105. // Expect only two libraries were loaded.
  106. EXPECT_EQ(2, manager->getNumLibraries());
  107. // Execute the callouts. The first library implements the calculation.
  108. //
  109. // r3 = (7 * d1 - d2) * d3
  110. //
  111. // The last-loaded library implements the calculation
  112. //
  113. // r3 = (10 + d1) * d2 - d3
  114. //
  115. // Putting the processing for each library together in the appropriate
  116. // order, we get:
  117. //
  118. // r3 = ((10 * d1 + d1) - d2) * d2 * d3 - d3
  119. {
  120. SCOPED_TRACE("Doing calculation with libraries loaded");
  121. executeCallCallouts(manager, 10, 3, 33, 2, 62, 3, 183);
  122. }
  123. // Try unloading the libraries.
  124. EXPECT_NO_THROW(lm_collection.unloadLibraries());
  125. // Re-execute the calculation - callouts can be called but as nothing
  126. // happens, the result should always be -1.
  127. {
  128. SCOPED_TRACE("Doing calculation with libraries not loaded");
  129. executeCallCallouts(manager, -1, 3, -1, 22, -1, 83, -1);
  130. }
  131. }
  132. // Check that everything works even with no libraries loaded.
  133. TEST_F(LibraryManagerCollectionTest, NoLibrariesLoaded) {
  134. // Set up the list of libraries to be loaded.
  135. std::vector<std::string> library_names;
  136. // Set up the library manager collection and get the callout manager we'll
  137. // be using.
  138. LibraryManagerCollection lm_collection(library_names);
  139. EXPECT_TRUE(lm_collection.loadLibraries());
  140. boost::shared_ptr<CalloutManager> manager =
  141. lm_collection.getCalloutManager();
  142. // Load the libraries.
  143. EXPECT_TRUE(lm_collection.loadLibraries());
  144. // Eecute the calculation - callouts can be called but as nothing
  145. // happens, the result should always be -1.
  146. executeCallCallouts(manager, -1, 3, -1, 22, -1, 83, -1);
  147. }
  148. } // Anonymous namespace