hooks_manager_unittest.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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/hooks_manager.h>
  8. #include <hooks/server_hooks.h>
  9. #include <hooks/tests/common_test_class.h>
  10. #include <hooks/tests/test_libraries.h>
  11. #include <cc/data.h>
  12. #include <boost/shared_ptr.hpp>
  13. #include <gtest/gtest.h>
  14. #include <algorithm>
  15. #include <string>
  16. using namespace isc;
  17. using namespace isc::hooks;
  18. using namespace isc::data;
  19. using namespace std;
  20. namespace {
  21. /// @brief Hooks manager collection test class
  22. class HooksManagerTest : public ::testing::Test,
  23. public HooksCommonTestClass {
  24. public:
  25. /// @brief Constructor
  26. ///
  27. /// Reset the hooks manager. The hooks manager is a singleton, so needs
  28. /// to be reset for each test.
  29. HooksManagerTest() {
  30. HooksManager::unloadLibraries();
  31. }
  32. /// @brief Destructor
  33. ///
  34. /// Unload all libraries and reset the shared manager.
  35. ~HooksManagerTest() {
  36. HooksManager::unloadLibraries();
  37. CalloutManager::getSharedManager().reset();
  38. }
  39. /// @brief Call callouts test
  40. ///
  41. /// See the header for HooksCommonTestClass::execute for details.
  42. ///
  43. /// @param r0...r3, d1..d3 Values and intermediate values expected. They
  44. /// are ordered so that the variables appear in the argument list in
  45. /// the order they are used.
  46. void executeCallCallouts(int r0, int d1, int r1, int d2, int r2, int d3,
  47. int r3) {
  48. static const char* COMMON_TEXT = " callout returned the wrong value";
  49. static const char* RESULT = "result";
  50. // Get a CalloutHandle for the calculation.
  51. CalloutHandlePtr handle = HooksManager::createCalloutHandle();
  52. // Initialize the argument RESULT. This simplifies testing by
  53. // eliminating the generation of an exception when we try the unload
  54. // test. In that case, RESULT is unchanged.
  55. int result = -1;
  56. handle->setArgument(RESULT, result);
  57. // Seed the calculation.
  58. HooksManager::callCallouts(isc::hooks::ServerHooks::CONTEXT_CREATE,
  59. *handle);
  60. handle->getArgument(RESULT, result);
  61. EXPECT_EQ(r0, result) << "context_create" << COMMON_TEXT;
  62. // Perform the first calculation.
  63. handle->setArgument("data_1", d1);
  64. HooksManager::callCallouts(hookpt_one_index_, *handle);
  65. handle->getArgument(RESULT, result);
  66. EXPECT_EQ(r1, result) << "hookpt_one" << COMMON_TEXT;
  67. // ... the second ...
  68. handle->setArgument("data_2", d2);
  69. HooksManager::callCallouts(hookpt_two_index_, *handle);
  70. handle->getArgument(RESULT, result);
  71. EXPECT_EQ(r2, result) << "hookpt_two" << COMMON_TEXT;
  72. // ... and the third.
  73. handle->setArgument("data_3", d3);
  74. HooksManager::callCallouts(hookpt_three_index_, *handle);
  75. handle->getArgument(RESULT, result);
  76. EXPECT_EQ(r3, result) << "hookpt_three" << COMMON_TEXT;
  77. }
  78. private:
  79. /// To avoid unused variable errors
  80. std::string dummy(int i) {
  81. if (i == 0) {
  82. return (MARKER_FILE);
  83. } else if (i > 0) {
  84. return (LOAD_CALLOUT_LIBRARY);
  85. } else {
  86. return (LOAD_ERROR_CALLOUT_LIBRARY);
  87. }
  88. }
  89. };
  90. // This is effectively the same test as for LibraryManager, but using the
  91. // HooksManager object.
  92. TEST_F(HooksManagerTest, LoadLibraries) {
  93. // Set up the list of libraries to be loaded.
  94. HookLibsCollection library_names;
  95. library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
  96. data::ConstElementPtr()));
  97. library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
  98. data::ConstElementPtr()));
  99. // Load the libraries.
  100. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  101. // Execute the callouts. The first library implements the calculation.
  102. //
  103. // r3 = (7 * d1 - d2) * d3
  104. //
  105. // The last-loaded library implements the calculation
  106. //
  107. // r3 = (10 + d1) * d2 - d3
  108. //
  109. // Putting the processing for each library together in the appropriate
  110. // order, we get:
  111. //
  112. // r3 = ((10 * d1 + d1) - d2) * d2 * d3 - d3
  113. {
  114. SCOPED_TRACE("Calculation with libraries loaded");
  115. executeCallCallouts(10, 3, 33, 2, 62, 3, 183);
  116. }
  117. // Try unloading the libraries.
  118. EXPECT_NO_THROW(HooksManager::unloadLibraries());
  119. // Re-execute the calculation - callouts can be called but as nothing
  120. // happens, the result should always be -1.
  121. {
  122. SCOPED_TRACE("Calculation with libraries not loaded");
  123. executeCallCallouts(-1, 3, -1, 22, -1, 83, -1);
  124. }
  125. }
  126. // This is effectively the same test as above, but with a library generating
  127. // an error when loaded. It is expected that the failing library will not be
  128. // loaded, but others will be.
  129. TEST_F(HooksManagerTest, LoadLibrariesWithError) {
  130. // Set up the list of libraries to be loaded.
  131. HookLibsCollection library_names;
  132. library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
  133. data::ConstElementPtr()));
  134. library_names.push_back(make_pair(std::string(INCORRECT_VERSION_LIBRARY),
  135. data::ConstElementPtr()));
  136. library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
  137. data::ConstElementPtr()));
  138. // Load the libraries. We expect a failure return because one of the
  139. // libraries fails to load.
  140. EXPECT_FALSE(HooksManager::loadLibraries(library_names));
  141. }
  142. // Test that we can unload a set of libraries while we have a CalloutHandle
  143. // created on them in existence, and can delete the handle afterwards.
  144. TEST_F(HooksManagerTest, CalloutHandleUnloadLibrary) {
  145. // Set up the list of libraries to be loaded.
  146. HookLibsCollection library_names;
  147. library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
  148. data::ConstElementPtr()));
  149. // Load the libraries.
  150. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  151. // Execute the callouts. This library implements:
  152. //
  153. // r3 = (7 * d1 - d2) * d3
  154. {
  155. SCOPED_TRACE("Calculation with full callout library loaded");
  156. executeCallCallouts(7, 4, 28, 8, 20, 2, 40);
  157. }
  158. // Get an outstanding callout handle on this library.
  159. CalloutHandlePtr handle = HooksManager::createCalloutHandle();
  160. // Execute once of the callouts again to ensure that the handle contains
  161. // memory allocated by the library.
  162. HooksManager::callCallouts(ServerHooks::CONTEXT_CREATE, *handle);
  163. // Unload the libraries.
  164. HooksManager::unloadLibraries();
  165. // Deleting the callout handle should not cause a segmentation fault.
  166. handle.reset();
  167. }
  168. // Test that we can load a new set of libraries while we have a CalloutHandle
  169. // created on them in existence, and can delete the handle afterwards.
  170. TEST_F(HooksManagerTest, CalloutHandleLoadLibrary) {
  171. // Set up the list of libraries to be loaded.
  172. HookLibsCollection library_names;
  173. library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
  174. data::ConstElementPtr()));
  175. // Load the libraries.
  176. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  177. // Execute the callouts. This library implements:
  178. //
  179. // r3 = (7 * d1 - d2) * d3
  180. {
  181. SCOPED_TRACE("Calculation with full callout library loaded");
  182. executeCallCallouts(7, 4, 28, 8, 20, 2, 40);
  183. }
  184. // Get an outstanding callout handle on this library and execute one of
  185. // the callouts again to ensure that the handle contains memory allocated
  186. // by the library.
  187. CalloutHandlePtr handle = HooksManager::createCalloutHandle();
  188. HooksManager::callCallouts(ServerHooks::CONTEXT_CREATE, *handle);
  189. // Load a new library that implements the calculation
  190. //
  191. // r3 = (10 + d1) * d2 - d3
  192. HookLibsCollection new_library_names;
  193. new_library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
  194. data::ConstElementPtr()));
  195. // Load the libraries.
  196. EXPECT_TRUE(HooksManager::loadLibraries(new_library_names));
  197. // Execute the calculation. Note that we still have the CalloutHandle
  198. // for the old library: however, this should not affect the new calculation.
  199. {
  200. SCOPED_TRACE("Calculation with basic callout library loaded");
  201. executeCallCallouts(10, 7, 17, 3, 51, 16, 35);
  202. }
  203. // Deleting the old callout handle should not cause a segmentation fault.
  204. handle.reset();
  205. }
  206. // This is effectively the same test as the LoadLibraries test.
  207. TEST_F(HooksManagerTest, ReloadSameLibraries) {
  208. // Set up the list of libraries to be loaded.
  209. HookLibsCollection library_names;
  210. library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
  211. data::ConstElementPtr()));
  212. library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
  213. data::ConstElementPtr()));
  214. // Load the libraries.
  215. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  216. // Execute the callouts. See the LoadLibraries test for an explanation of
  217. // the calculation.
  218. {
  219. SCOPED_TRACE("Calculation with libraries loaded");
  220. executeCallCallouts(10, 3, 33, 2, 62, 3, 183);
  221. }
  222. // Try reloading the libraries and re-execute the calculation - we should
  223. // get the same results.
  224. EXPECT_NO_THROW(HooksManager::loadLibraries(library_names));
  225. {
  226. SCOPED_TRACE("Calculation with libraries reloaded");
  227. executeCallCallouts(10, 3, 33, 2, 62, 3, 183);
  228. }
  229. }
  230. TEST_F(HooksManagerTest, ReloadLibrariesReverseOrder) {
  231. // Set up the list of libraries to be loaded and load them.
  232. HookLibsCollection library_names;
  233. library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
  234. data::ConstElementPtr()));
  235. library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
  236. data::ConstElementPtr()));
  237. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  238. // Execute the callouts. The first library implements the calculation.
  239. //
  240. // r3 = (7 * d1 - d2) * d3
  241. //
  242. // The last-loaded library implements the calculation
  243. //
  244. // r3 = (10 + d1) * d2 - d3
  245. //
  246. // Putting the processing for each library together in the given order
  247. // gives.
  248. //
  249. // r3 = ((10 * d1 + d1) - d2) * d2 * d3 - d3
  250. {
  251. SCOPED_TRACE("Calculation with libraries loaded");
  252. executeCallCallouts(10, 3, 33, 2, 62, 3, 183);
  253. }
  254. // Reload the libraries in the reverse order.
  255. std::reverse(library_names.begin(), library_names.end());
  256. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  257. // The calculation in the reverse order gives:
  258. //
  259. // r3 = ((((7 + d1) * d1) * d2 - d2) - d3) * d3
  260. {
  261. SCOPED_TRACE("Calculation with libraries loaded in reverse order");
  262. executeCallCallouts(7, 3, 30, 3, 87, 7, 560);
  263. }
  264. }
  265. // Local callouts for the test of server-registered callouts.
  266. namespace {
  267. int
  268. testPreCallout(CalloutHandle& handle) {
  269. handle.setArgument("result", static_cast<int>(1027));
  270. return (0);
  271. }
  272. int
  273. testPostCallout(CalloutHandle& handle) {
  274. int result;
  275. handle.getArgument("result", result);
  276. result *= 2;
  277. handle.setArgument("result", result);
  278. return (0);
  279. }
  280. }
  281. // The next test registers the pre and post- callouts above for hook hookpt_two,
  282. // and checks they are called.
  283. TEST_F(HooksManagerTest, PrePostCalloutTest) {
  284. // Load a single library.
  285. HookLibsCollection library_names;
  286. library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
  287. data::ConstElementPtr()));
  288. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  289. // Load the pre- and post- callouts.
  290. HooksManager::preCalloutsLibraryHandle().registerCallout("hookpt_two",
  291. testPreCallout);
  292. HooksManager::postCalloutsLibraryHandle().registerCallout("hookpt_two",
  293. testPostCallout);
  294. // Execute the callouts. hookpt_two implements the calculation:
  295. //
  296. // "result - data_2"
  297. //
  298. // With the pre- and post- callouts above, the result expected is
  299. //
  300. // (1027 - data_2) * 2
  301. CalloutHandlePtr handle = HooksManager::createCalloutHandle();
  302. handle->setArgument("result", static_cast<int>(0));
  303. handle->setArgument("data_2", static_cast<int>(15));
  304. HooksManager::callCallouts(hookpt_two_index_, *handle);
  305. int result = 0;
  306. handle->getArgument("result", result);
  307. EXPECT_EQ(2024, result);
  308. // ... and check that the pre- and post- callout functions don't survive a
  309. // reload.
  310. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  311. handle = HooksManager::createCalloutHandle();
  312. handle->setArgument("result", static_cast<int>(0));
  313. handle->setArgument("data_2", static_cast<int>(15));
  314. HooksManager::callCallouts(hookpt_two_index_, *handle);
  315. result = 0;
  316. handle->getArgument("result", result);
  317. EXPECT_EQ(-15, result);
  318. }
  319. // Test with a shared manager the pre- and post- callout functions survive
  320. // a reload
  321. TEST_F(HooksManagerTest, PrePostCalloutShared) {
  322. HookLibsCollection library_names;
  323. // Initialize the shared manager.
  324. CalloutManager::getSharedManager().reset(new CalloutManager(0));
  325. // Load the pre- and post- callouts.
  326. HooksManager::preCalloutsLibraryHandle().registerCallout("hookpt_two",
  327. testPreCallout);
  328. HooksManager::postCalloutsLibraryHandle().registerCallout("hookpt_two",
  329. testPostCallout);
  330. // With the pre- and post- callouts above, the result expected is
  331. //
  332. // 1027 * 2
  333. CalloutHandlePtr handle = HooksManager::createCalloutHandle();
  334. handle->setArgument("result", static_cast<int>(0));
  335. handle->setArgument("data_2", static_cast<int>(15));
  336. HooksManager::callCallouts(hookpt_two_index_, *handle);
  337. int result = 0;
  338. handle->getArgument("result", result);
  339. EXPECT_EQ(2054, result);
  340. // ... and check that the pre- and post- callout functions survive a
  341. // reload.
  342. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  343. handle = HooksManager::createCalloutHandle();
  344. handle->setArgument("result", static_cast<int>(0));
  345. handle->setArgument("data_2", static_cast<int>(15));
  346. HooksManager::callCallouts(hookpt_two_index_, *handle);
  347. // Expect same value i.e. 1027 * 2
  348. result = 0;
  349. handle->getArgument("result", result);
  350. EXPECT_EQ(2054, result);
  351. }
  352. // Test with a shared manager the pre- and post- callout functions survive
  353. // a reload but not with a not empty list of libraries
  354. TEST_F(HooksManagerTest, PrePostCalloutSharedNotEmpty) {
  355. HookLibsCollection library_names;
  356. library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
  357. data::ConstElementPtr()));
  358. // Initialize the shared manager.
  359. CalloutManager::getSharedManager().reset(new CalloutManager(0));
  360. // Load the pre- and post- callouts.
  361. HooksManager::preCalloutsLibraryHandle().registerCallout("hookpt_two",
  362. testPreCallout);
  363. HooksManager::postCalloutsLibraryHandle().registerCallout("hookpt_two",
  364. testPostCallout);
  365. // With the pre- and post- callouts above, the result expected is
  366. //
  367. // 1027 * 2
  368. CalloutHandlePtr handle = HooksManager::createCalloutHandle();
  369. handle->setArgument("result", static_cast<int>(0));
  370. handle->setArgument("data_2", static_cast<int>(15));
  371. HooksManager::callCallouts(hookpt_two_index_, *handle);
  372. int result = 0;
  373. handle->getArgument("result", result);
  374. EXPECT_EQ(2054, result);
  375. // ... and check that the pre- and post- callout functions don't survive a
  376. // reload with a not empty list of libraries.
  377. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  378. handle = HooksManager::createCalloutHandle();
  379. handle->setArgument("result", static_cast<int>(0));
  380. handle->setArgument("data_2", static_cast<int>(15));
  381. HooksManager::callCallouts(hookpt_two_index_, *handle);
  382. // Expect result - data_2
  383. result = 0;
  384. handle->getArgument("result", result);
  385. EXPECT_EQ(-15, result);
  386. }
  387. // Test with a shared manager the pre- and post- callout functions don't
  388. // survive a reload if the shared manager is initialized too late.
  389. TEST_F(HooksManagerTest, PrePostCalloutSharedTooLate) {
  390. HookLibsCollection library_names;
  391. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  392. // Initialize the shared manager (after loadLibraries so too late)
  393. CalloutManager::getSharedManager().reset(new CalloutManager(0));
  394. // Load the pre- and post- callouts.
  395. HooksManager::preCalloutsLibraryHandle().registerCallout("hookpt_two",
  396. testPreCallout);
  397. HooksManager::postCalloutsLibraryHandle().registerCallout("hookpt_two",
  398. testPostCallout);
  399. // With the pre- and post- callouts above, the result expected is
  400. //
  401. // 1027 * 2
  402. CalloutHandlePtr handle = HooksManager::createCalloutHandle();
  403. handle->setArgument("result", static_cast<int>(0));
  404. handle->setArgument("data_2", static_cast<int>(15));
  405. HooksManager::callCallouts(hookpt_two_index_, *handle);
  406. int result = 0;
  407. handle->getArgument("result", result);
  408. EXPECT_EQ(2054, result);
  409. // ... and check that the pre- and post- callout functions don't survive a
  410. // reload.
  411. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  412. handle = HooksManager::createCalloutHandle();
  413. handle->setArgument("result", static_cast<int>(0));
  414. handle->setArgument("data_2", static_cast<int>(15));
  415. HooksManager::callCallouts(hookpt_two_index_, *handle);
  416. // Expect no change so result = 0
  417. result = 0;
  418. handle->getArgument("result", result);
  419. EXPECT_EQ(0, result);
  420. }
  421. // Check that everything works even with no libraries loaded. First that
  422. // calloutsPresent() always returns false.
  423. TEST_F(HooksManagerTest, NoLibrariesCalloutsPresent) {
  424. // No callouts should be present on any hooks.
  425. EXPECT_FALSE(HooksManager::calloutsPresent(hookpt_one_index_));
  426. EXPECT_FALSE(HooksManager::calloutsPresent(hookpt_two_index_));
  427. EXPECT_FALSE(HooksManager::calloutsPresent(hookpt_three_index_));
  428. }
  429. TEST_F(HooksManagerTest, NoLibrariesCallCallouts) {
  430. executeCallCallouts(-1, 3, -1, 22, -1, 83, -1);
  431. }
  432. // Test the encapsulation of the ServerHooks::registerHook() method.
  433. TEST_F(HooksManagerTest, RegisterHooks) {
  434. ServerHooks::getServerHooks().reset();
  435. EXPECT_EQ(2, ServerHooks::getServerHooks().getCount());
  436. // Check that the hook indexes are as expected. (Use temporary variables
  437. // as it appears that Google test can't access the constants.)
  438. int sh_cc = ServerHooks::CONTEXT_CREATE;
  439. int hm_cc = HooksManager::CONTEXT_CREATE;
  440. EXPECT_EQ(sh_cc, hm_cc);
  441. int sh_cd = ServerHooks::CONTEXT_DESTROY;
  442. int hm_cd = HooksManager::CONTEXT_DESTROY;
  443. EXPECT_EQ(sh_cd, hm_cd);
  444. // Register a few hooks and check we have the indexes as expected.
  445. EXPECT_EQ(2, HooksManager::registerHook(string("alpha")));
  446. EXPECT_EQ(3, HooksManager::registerHook(string("beta")));
  447. EXPECT_EQ(4, HooksManager::registerHook(string("gamma")));
  448. EXPECT_THROW(static_cast<void>(HooksManager::registerHook(string("alpha"))),
  449. DuplicateHook);
  450. // ... an check the hooks are as we expect.
  451. EXPECT_EQ(5, ServerHooks::getServerHooks().getCount());
  452. vector<string> names = ServerHooks::getServerHooks().getHookNames();
  453. sort(names.begin(), names.end());
  454. EXPECT_EQ(string("alpha"), names[0]);
  455. EXPECT_EQ(string("beta"), names[1]);
  456. EXPECT_EQ(string("context_create"), names[2]);
  457. EXPECT_EQ(string("context_destroy"), names[3]);
  458. EXPECT_EQ(string("gamma"), names[4]);
  459. }
  460. // Check that we can get the names of the libraries.
  461. TEST_F(HooksManagerTest, LibraryNames) {
  462. // Set up the list of libraries to be loaded.
  463. HookLibsCollection library_names;
  464. library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
  465. data::ConstElementPtr()));
  466. library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
  467. data::ConstElementPtr()));
  468. // Check the names before the libraries are loaded.
  469. std::vector<std::string> loaded_names = HooksManager::getLibraryNames();
  470. EXPECT_TRUE(loaded_names.empty());
  471. // Load the libraries and check the names again.
  472. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  473. loaded_names = HooksManager::getLibraryNames();
  474. EXPECT_TRUE(extractNames(library_names) == loaded_names);
  475. // Unload the libraries and check again.
  476. EXPECT_NO_THROW(HooksManager::unloadLibraries());
  477. loaded_names = HooksManager::getLibraryNames();
  478. EXPECT_TRUE(loaded_names.empty());
  479. }
  480. // Test the library validation function.
  481. TEST_F(HooksManagerTest, validateLibraries) {
  482. // Vector of libraries that failed validation
  483. std::vector<std::string> failed;
  484. // Test different vectors of libraries.
  485. // No libraries should return a success.
  486. std::vector<std::string> libraries;
  487. failed = HooksManager::validateLibraries(libraries);
  488. EXPECT_TRUE(failed.empty());
  489. // Single valid library should validate.
  490. libraries.clear();
  491. libraries.push_back(BASIC_CALLOUT_LIBRARY);
  492. failed = HooksManager::validateLibraries(libraries);
  493. EXPECT_TRUE(failed.empty());
  494. // Multiple valid libraries should succeed.
  495. libraries.clear();
  496. libraries.push_back(BASIC_CALLOUT_LIBRARY);
  497. libraries.push_back(FULL_CALLOUT_LIBRARY);
  498. libraries.push_back(UNLOAD_CALLOUT_LIBRARY);
  499. failed = HooksManager::validateLibraries(libraries);
  500. EXPECT_TRUE(failed.empty());
  501. // Single invalid library should fail.
  502. libraries.clear();
  503. libraries.push_back(NOT_PRESENT_LIBRARY);
  504. failed = HooksManager::validateLibraries(libraries);
  505. EXPECT_TRUE(failed == libraries);
  506. // Multiple invalid libraries should fail.
  507. libraries.clear();
  508. libraries.push_back(INCORRECT_VERSION_LIBRARY);
  509. libraries.push_back(NO_VERSION_LIBRARY);
  510. libraries.push_back(FRAMEWORK_EXCEPTION_LIBRARY);
  511. failed = HooksManager::validateLibraries(libraries);
  512. EXPECT_TRUE(failed == libraries);
  513. // Combination of valid and invalid (first one valid) should fail.
  514. libraries.clear();
  515. libraries.push_back(FULL_CALLOUT_LIBRARY);
  516. libraries.push_back(INCORRECT_VERSION_LIBRARY);
  517. libraries.push_back(NO_VERSION_LIBRARY);
  518. std::vector<std::string> expected_failures;
  519. expected_failures.push_back(INCORRECT_VERSION_LIBRARY);
  520. expected_failures.push_back(NO_VERSION_LIBRARY);
  521. failed = HooksManager::validateLibraries(libraries);
  522. EXPECT_TRUE(failed == expected_failures);
  523. // Combination of valid and invalid (first one invalid) should fail.
  524. libraries.clear();
  525. libraries.push_back(NO_VERSION_LIBRARY);
  526. libraries.push_back(FULL_CALLOUT_LIBRARY);
  527. libraries.push_back(INCORRECT_VERSION_LIBRARY);
  528. expected_failures.clear();
  529. expected_failures.push_back(NO_VERSION_LIBRARY);
  530. expected_failures.push_back(INCORRECT_VERSION_LIBRARY);
  531. failed = HooksManager::validateLibraries(libraries);
  532. EXPECT_TRUE(failed == expected_failures);
  533. }
  534. // This test verifies that the specified parameters are accessed properly.
  535. TEST_F(HooksManagerTest, LibraryParameters) {
  536. // Prepare paramters for the callout parameters library.
  537. ElementPtr params = Element::createMap();
  538. params->set("svalue", Element::create("string value"));
  539. params->set("ivalue", Element::create(42));
  540. params->set("bvalue", Element::create(true));
  541. // Set up the list of libraries to be loaded.
  542. HookLibsCollection library_names;
  543. library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
  544. data::ConstElementPtr()));
  545. library_names.push_back(make_pair(std::string(CALLOUT_PARAMS_LIBRARY),
  546. params));
  547. // Load the libraries. Note that callout params library checks if
  548. // all mandatory parameters are there, so if anything is missing, its
  549. // load() function will return error, thus causing the library to not
  550. // load.
  551. EXPECT_TRUE(HooksManager::loadLibraries(library_names));
  552. // Try unloading the libraries.
  553. EXPECT_NO_THROW(HooksManager::unloadLibraries());
  554. }
  555. } // Anonymous namespace