library_manager_collection_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Copyright (C) 2013-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <hooks/callout_handle.h>
  7. #include <hooks/callout_manager.h>
  8. #include <hooks/library_manager.h>
  9. #include <hooks/library_manager_collection.h>
  10. #include <hooks/libinfo.h>
  11. #include <hooks/tests/common_test_class.h>
  12. #include <hooks/tests/test_libraries.h>
  13. #include <boost/shared_ptr.hpp>
  14. #include <gtest/gtest.h>
  15. #include <algorithm>
  16. #include <string>
  17. using namespace isc;
  18. using namespace isc::hooks;
  19. using namespace std;
  20. namespace {
  21. /// @brief Library manager collection test class
  22. class LibraryManagerCollectionTest : public ::testing::Test,
  23. public HooksCommonTestClass {
  24. private:
  25. /// To avoid unused variable errors
  26. std::string dummy(int i) {
  27. if (i == 0) {
  28. return (MARKER_FILE);
  29. } else if (i > 0) {
  30. return (LOAD_CALLOUT_LIBRARY);
  31. } else {
  32. return (LOAD_ERROR_CALLOUT_LIBRARY);
  33. }
  34. }
  35. };
  36. /// @brief Public library manager collection class
  37. ///
  38. /// This is an instance of the LibraryManagerCollection class but with the
  39. /// protected methods made public for test purposes.
  40. class PublicLibraryManagerCollection
  41. : public isc::hooks::LibraryManagerCollection {
  42. public:
  43. /// @brief Constructor
  44. ///
  45. /// @param List of libraries that this collection will manage. The order
  46. /// of the libraries is important.
  47. PublicLibraryManagerCollection(const HookLibsCollection& libraries)
  48. : LibraryManagerCollection(libraries)
  49. {}
  50. /// Public methods that call protected methods on the superclass.
  51. using LibraryManagerCollection::unloadLibraries;
  52. };
  53. // This is effectively the same test as for LibraryManager, but using the
  54. // LibraryManagerCollection object.
  55. TEST_F(LibraryManagerCollectionTest, LoadLibraries) {
  56. // Set up the list of libraries to be loaded.
  57. HookLibsCollection library_names;
  58. library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
  59. data::ConstElementPtr()));
  60. library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
  61. data::ConstElementPtr()));
  62. // Set up the library manager collection and get the callout manager we'll
  63. // be using.
  64. PublicLibraryManagerCollection lm_collection(library_names);
  65. // Load the libraries.
  66. EXPECT_TRUE(lm_collection.loadLibraries());
  67. EXPECT_EQ(2, lm_collection.getLoadedLibraryCount());
  68. // Execute the callouts. The first library implements the calculation.
  69. //
  70. // r3 = (7 * d1 - d2) * d3
  71. //
  72. // The last-loaded library implements the calculation
  73. //
  74. // r3 = (10 + d1) * d2 - d3
  75. //
  76. // Putting the processing for each library together in the appropriate
  77. // order, we get:
  78. //
  79. // r3 = ((10 * d1 + d1) - d2) * d2 * d3 - d3
  80. boost::shared_ptr<CalloutManager> manager =
  81. lm_collection.getCalloutManager();
  82. {
  83. SCOPED_TRACE("Doing calculation with libraries loaded");
  84. executeCallCallouts(manager, 10, 3, 33, 2, 62, 3, 183);
  85. }
  86. // Try unloading the libraries.
  87. EXPECT_NO_THROW(lm_collection.unloadLibraries());
  88. EXPECT_EQ(0, lm_collection.getLoadedLibraryCount());
  89. // Re-execute the calculation - callouts can be called but as nothing
  90. // happens, the result should always be -1.
  91. {
  92. SCOPED_TRACE("Doing calculation with libraries not loaded");
  93. executeCallCallouts(manager, -1, 3, -1, 22, -1, 83, -1);
  94. }
  95. }
  96. // This is effectively the same test as above, but with a library generating
  97. // an error when loaded. It is expected that no libraries will be loaded.
  98. TEST_F(LibraryManagerCollectionTest, LoadLibrariesWithError) {
  99. // Set up the list of libraries to be loaded.
  100. HookLibsCollection library_names;
  101. library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
  102. data::ConstElementPtr()));
  103. library_names.push_back(make_pair(std::string(INCORRECT_VERSION_LIBRARY),
  104. data::ConstElementPtr()));
  105. library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
  106. data::ConstElementPtr()));
  107. // Set up the library manager collection and get the callout manager we'll
  108. // be using.
  109. PublicLibraryManagerCollection lm_collection(library_names);
  110. // Load the libraries. We expect a failure status to be returned as
  111. // one of the libraries failed to load.
  112. EXPECT_FALSE(lm_collection.loadLibraries());
  113. // Expect no libraries were loaded.
  114. EXPECT_EQ(0, lm_collection.getLoadedLibraryCount());
  115. }
  116. // Check that everything works even with no libraries loaded.
  117. TEST_F(LibraryManagerCollectionTest, NoLibrariesLoaded) {
  118. // Set up the list of libraries to be loaded.
  119. HookLibsCollection library_names;
  120. // Set up the library manager collection and get the callout manager we'll
  121. // be using.
  122. LibraryManagerCollection lm_collection(library_names);
  123. EXPECT_TRUE(lm_collection.loadLibraries());
  124. EXPECT_EQ(0, lm_collection.getLoadedLibraryCount());
  125. boost::shared_ptr<CalloutManager> manager =
  126. lm_collection.getCalloutManager();
  127. // Execute the calculation - callouts can be called but as nothing
  128. // happens, the result should always be -1.
  129. executeCallCallouts(manager, -1, 3, -1, 22, -1, 83, -1);
  130. }
  131. // Check that we can get the names of the libraries.
  132. TEST_F(LibraryManagerCollectionTest, LibraryNames) {
  133. // Set up the list of libraries to be loaded.
  134. HookLibsCollection libraries;
  135. libraries.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
  136. data::ConstElementPtr()));
  137. libraries.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
  138. data::ConstElementPtr()));
  139. // Set up the library manager collection and get the callout manager we'll
  140. // be using.
  141. PublicLibraryManagerCollection lm_collection(libraries);
  142. // Check the names before the libraries are loaded.
  143. std::vector<std::string> collection_names = lm_collection.getLibraryNames();
  144. EXPECT_TRUE(extractNames(libraries) == collection_names);
  145. // Load the libraries and check the names again.
  146. EXPECT_TRUE(lm_collection.loadLibraries());
  147. EXPECT_EQ(2, lm_collection.getLoadedLibraryCount());
  148. collection_names = lm_collection.getLibraryNames();
  149. EXPECT_TRUE(extractNames(libraries) == collection_names);
  150. }
  151. // Test the library validation function.
  152. TEST_F(LibraryManagerCollectionTest, validateLibraries) {
  153. // Vector of libraries that failed validation
  154. std::vector<std::string> failed;
  155. // Test different vectors of libraries.
  156. // No libraries should return a success.
  157. std::vector<std::string> libraries;
  158. failed = LibraryManagerCollection::validateLibraries(libraries);
  159. EXPECT_TRUE(failed.empty());
  160. // Single valid library should validate.
  161. libraries.clear();
  162. libraries.push_back(BASIC_CALLOUT_LIBRARY);
  163. failed = LibraryManagerCollection::validateLibraries(libraries);
  164. EXPECT_TRUE(failed.empty());
  165. // Multiple valid libraries should succeed.
  166. libraries.clear();
  167. libraries.push_back(BASIC_CALLOUT_LIBRARY);
  168. libraries.push_back(FULL_CALLOUT_LIBRARY);
  169. libraries.push_back(UNLOAD_CALLOUT_LIBRARY);
  170. libraries.push_back(CALLOUT_PARAMS_LIBRARY);
  171. failed = LibraryManagerCollection::validateLibraries(libraries);
  172. EXPECT_TRUE(failed.empty());
  173. // Single invalid library should fail.
  174. libraries.clear();
  175. libraries.push_back(NOT_PRESENT_LIBRARY);
  176. failed = LibraryManagerCollection::validateLibraries(libraries);
  177. EXPECT_TRUE(failed == libraries);
  178. // Multiple invalid libraries should fail.
  179. libraries.clear();
  180. libraries.push_back(INCORRECT_VERSION_LIBRARY);
  181. libraries.push_back(NO_VERSION_LIBRARY);
  182. libraries.push_back(FRAMEWORK_EXCEPTION_LIBRARY);
  183. failed = LibraryManagerCollection::validateLibraries(libraries);
  184. EXPECT_TRUE(failed == libraries);
  185. // Combination of valid and invalid (first one valid) should fail.
  186. libraries.clear();
  187. libraries.push_back(FULL_CALLOUT_LIBRARY);
  188. libraries.push_back(INCORRECT_VERSION_LIBRARY);
  189. libraries.push_back(NO_VERSION_LIBRARY);
  190. std::vector<std::string> expected_failures;
  191. expected_failures.push_back(INCORRECT_VERSION_LIBRARY);
  192. expected_failures.push_back(NO_VERSION_LIBRARY);
  193. failed = LibraryManagerCollection::validateLibraries(libraries);
  194. EXPECT_TRUE(failed == expected_failures);
  195. // Combination of valid and invalid (first one invalid) should fail.
  196. libraries.clear();
  197. libraries.push_back(NO_VERSION_LIBRARY);
  198. libraries.push_back(FULL_CALLOUT_LIBRARY);
  199. libraries.push_back(INCORRECT_VERSION_LIBRARY);
  200. expected_failures.clear();
  201. expected_failures.push_back(NO_VERSION_LIBRARY);
  202. expected_failures.push_back(INCORRECT_VERSION_LIBRARY);
  203. failed = LibraryManagerCollection::validateLibraries(libraries);
  204. EXPECT_TRUE(failed == expected_failures);
  205. }
  206. // This test verifies if getLibraryNames and getLibraryInfo are returning
  207. // expected values if there are no libraries configured.
  208. TEST_F(LibraryManagerCollectionTest, libraryGetEmpty) {
  209. HookLibsCollection empty;
  210. boost::shared_ptr<LibraryManagerCollection> mgr;
  211. // Instantiate library manager collection with no libraries
  212. EXPECT_NO_THROW(mgr.reset(new LibraryManagerCollection(empty)));
  213. // Check that getLibraryInfo returns empty list properly.
  214. HookLibsCollection returned = mgr->getLibraryInfo();
  215. EXPECT_TRUE(returned.empty());
  216. // Check that getLibraryNames return empty list, too.
  217. vector<string> names(3, "rubbish"); // just put something in it.
  218. EXPECT_NO_THROW(names = mgr->getLibraryNames());
  219. EXPECT_TRUE(names.empty());
  220. }
  221. // This test verifies if getLibraryNames and getLibraryInfo are returning
  222. // expected values when there are libraries configured.
  223. TEST_F(LibraryManagerCollectionTest, libraryGet) {
  224. using namespace data;
  225. HookLibsCollection libs;
  226. ElementPtr param1(Element::fromJSON("{ \"param1\": \"foo\" }"));
  227. ElementPtr param2(Element::fromJSON("{ \"param2\": \"bar\" }"));
  228. libs.push_back(make_pair("libone", param1));
  229. libs.push_back(make_pair("libtwo", param2));
  230. boost::shared_ptr<LibraryManagerCollection> mgr;
  231. EXPECT_NO_THROW(mgr.reset(new LibraryManagerCollection(libs)));
  232. EXPECT_TRUE(libs == mgr->getLibraryInfo());
  233. vector<string> exp_names;
  234. exp_names.push_back("libone");
  235. exp_names.push_back("libtwo");
  236. EXPECT_TRUE(exp_names == mgr->getLibraryNames());
  237. }
  238. } // Anonymous namespace