library_manager_unittest.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. // Copyright (C) 2013-2017 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/server_hooks.h>
  10. #include <hooks/tests/common_test_class.h>
  11. #include <hooks/tests/marker_file.h>
  12. #include <hooks/tests/test_libraries.h>
  13. #include <log/message_dictionary.h>
  14. #include <log/message_initializer.h>
  15. #include <gtest/gtest.h>
  16. #include <algorithm>
  17. #include <fstream>
  18. #include <string>
  19. #include <unistd.h>
  20. using namespace isc;
  21. using namespace isc::hooks;
  22. using namespace isc::log;
  23. using namespace std;
  24. namespace {
  25. /// @brief Library manager test class
  26. class LibraryManagerTest : public ::testing::Test,
  27. public HooksCommonTestClass {
  28. public:
  29. /// @brief Constructor
  30. ///
  31. /// Initializes the CalloutManager object used in the tests. It sets it
  32. /// up with the hooks initialized in the HooksCommonTestClass object and
  33. /// with four libraries.
  34. LibraryManagerTest() {
  35. callout_manager_.reset(new CalloutManager(4));
  36. // Ensure the marker file is not present at the start of a test.
  37. static_cast<void>(remove(MARKER_FILE));
  38. }
  39. /// @brief Destructor
  40. ///
  41. /// Ensures a marker file is removed after each test.
  42. ~LibraryManagerTest() {
  43. static_cast<void>(remove(MARKER_FILE));
  44. }
  45. /// @brief Marker file present
  46. ///
  47. /// Convenience function to check whether a marker file is present. It
  48. /// does this by opening the file.
  49. ///
  50. /// @return true if the marker file is present.
  51. bool markerFilePresent() const {
  52. // Try to open it.
  53. std::fstream marker;
  54. marker.open(MARKER_FILE, std::fstream::in);
  55. // Check if it is open and close it if so.
  56. bool exists = marker.is_open();
  57. if (exists) {
  58. marker.close();
  59. }
  60. return (exists);
  61. }
  62. /// @brief Call callouts test
  63. ///
  64. /// A wrapper around the method of the same name in the HooksCommonTestClass
  65. /// object, this passes this class's CalloutManager to that method.
  66. ///
  67. /// @param r0...r3, d1..d3 Values and intermediate values expected. They
  68. /// are ordered so that the variables appear in the argument list in
  69. /// the order they are used. See HooksCommonTestClass::execute for
  70. /// a full description. (rN is used to indicate an expected result,
  71. /// dN is data to be passed to the calculation.)
  72. void executeCallCallouts(int r0, int d1, int r1, int d2, int r2, int d3,
  73. int r3) {
  74. HooksCommonTestClass::executeCallCallouts(callout_manager_, r0, d1,
  75. r1, d2, r2, d3, r3);
  76. }
  77. /// Callout manager used for the test.
  78. boost::shared_ptr<CalloutManager> callout_manager_;
  79. };
  80. /// @brief Library manager class
  81. ///
  82. /// This is an instance of the LibraryManager class but with the protected
  83. /// methods made public for test purposes.
  84. class PublicLibraryManager : public isc::hooks::LibraryManager {
  85. public:
  86. /// @brief Constructor
  87. ///
  88. /// Stores the library name. The actual loading is done in loadLibrary().
  89. ///
  90. /// @param name Name of the library to load. This should be an absolute
  91. /// path name.
  92. /// @param index Index of this library. For all these tests, it will be
  93. /// zero, as we are only using one library.
  94. /// @param manager CalloutManager object
  95. PublicLibraryManager(const std::string& name, int index,
  96. const boost::shared_ptr<CalloutManager>& manager)
  97. : LibraryManager(name, index, manager)
  98. {}
  99. /// Public methods that call protected methods on the superclass.
  100. using LibraryManager::openLibrary;
  101. using LibraryManager::closeLibrary;
  102. using LibraryManager::checkVersion;
  103. using LibraryManager::registerStandardCallouts;
  104. using LibraryManager::runLoad;
  105. using LibraryManager::runUnload;
  106. };
  107. // Check that LibraryManager constructor requires a not null manager
  108. TEST_F(LibraryManagerTest, NullManager) {
  109. EXPECT_THROW(PublicLibraryManager(std::string("foo"), 0, 0),
  110. NoCalloutManager);
  111. }
  112. // Check that openLibrary() reports an error when it can't find the specified
  113. // library.
  114. TEST_F(LibraryManagerTest, NoLibrary) {
  115. PublicLibraryManager lib_manager(std::string(NOT_PRESENT_LIBRARY),
  116. 0, callout_manager_);
  117. EXPECT_FALSE(lib_manager.openLibrary());
  118. }
  119. // Check that the openLibrary() and closeLibrary() methods work.
  120. TEST_F(LibraryManagerTest, OpenClose) {
  121. PublicLibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY),
  122. 0, callout_manager_);
  123. // Open and close the library
  124. EXPECT_TRUE(lib_manager.openLibrary());
  125. EXPECT_TRUE(lib_manager.closeLibrary());
  126. // Check that a second close on an already closed library does not report
  127. // an error.
  128. EXPECT_TRUE(lib_manager.closeLibrary());
  129. }
  130. // Check that the code handles the case of a library with no version function.
  131. TEST_F(LibraryManagerTest, NoVersion) {
  132. PublicLibraryManager lib_manager(std::string(NO_VERSION_LIBRARY),
  133. 0, callout_manager_);
  134. // Open should succeed.
  135. EXPECT_TRUE(lib_manager.openLibrary());
  136. // Version check should fail.
  137. EXPECT_FALSE(lib_manager.checkVersion());
  138. // Tidy up.
  139. EXPECT_TRUE(lib_manager.closeLibrary());
  140. }
  141. // Check that the code handles the case of a library with a version function
  142. // that returns an incorrect version number.
  143. TEST_F(LibraryManagerTest, WrongVersion) {
  144. PublicLibraryManager lib_manager(std::string(INCORRECT_VERSION_LIBRARY),
  145. 0, callout_manager_);
  146. // Open should succeed.
  147. EXPECT_TRUE(lib_manager.openLibrary());
  148. // Version check should fail.
  149. EXPECT_FALSE(lib_manager.checkVersion());
  150. // Tidy up.
  151. EXPECT_TRUE(lib_manager.closeLibrary());
  152. }
  153. // Check that the code handles the case of a library where the version function
  154. // throws an exception.
  155. TEST_F(LibraryManagerTest, VersionException) {
  156. PublicLibraryManager lib_manager(std::string(FRAMEWORK_EXCEPTION_LIBRARY),
  157. 0, callout_manager_);
  158. // Open should succeed.
  159. EXPECT_TRUE(lib_manager.openLibrary());
  160. // Version check should fail.
  161. EXPECT_FALSE(lib_manager.checkVersion());
  162. // Tidy up.
  163. EXPECT_TRUE(lib_manager.closeLibrary());
  164. }
  165. // Tests that checkVersion() function succeeds in the case of a library with a
  166. // version function that returns the correct version number.
  167. TEST_F(LibraryManagerTest, CorrectVersionReturned) {
  168. PublicLibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY),
  169. 0, callout_manager_);
  170. // Open should succeed.
  171. EXPECT_TRUE(lib_manager.openLibrary());
  172. // Version check should succeed.
  173. EXPECT_TRUE(lib_manager.checkVersion());
  174. // Tidy up.
  175. EXPECT_TRUE(lib_manager.closeLibrary());
  176. }
  177. // Checks the registration of standard callouts.
  178. TEST_F(LibraryManagerTest, RegisterStandardCallouts) {
  179. // Load the only library, specifying the index of 0 as it's the only
  180. // library. This should load all callouts.
  181. PublicLibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY),
  182. 0, callout_manager_);
  183. EXPECT_TRUE(lib_manager.openLibrary());
  184. // Check the version of the library.
  185. EXPECT_TRUE(lib_manager.checkVersion());
  186. // Load the standard callouts
  187. EXPECT_NO_THROW(lib_manager.registerStandardCallouts());
  188. // Now execute the callouts in the order expected. The library performs
  189. // the calculation:
  190. //
  191. // r3 = (10 + d1) * d2 - d3
  192. executeCallCallouts(10, 5, 15, 7, 105, 17, 88);
  193. // Tidy up
  194. EXPECT_TRUE(lib_manager.closeLibrary());
  195. }
  196. // Test that the "load" function is called correctly.
  197. TEST_F(LibraryManagerTest, CheckLoadCalled) {
  198. // Load the only library, specifying the index of 0 as it's the only
  199. // library. This should load all callouts.
  200. PublicLibraryManager lib_manager(std::string(LOAD_CALLOUT_LIBRARY),
  201. 0, callout_manager_);
  202. EXPECT_TRUE(lib_manager.openLibrary());
  203. // Check the version of the library.
  204. EXPECT_TRUE(lib_manager.checkVersion());
  205. // Load the standard callouts
  206. EXPECT_NO_THROW(lib_manager.registerStandardCallouts());
  207. // Check that only context_create and hookpt_one have callouts registered.
  208. EXPECT_TRUE(callout_manager_->calloutsPresent(
  209. ServerHooks::CONTEXT_CREATE));
  210. EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_one_index_));
  211. EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_));
  212. EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_));
  213. EXPECT_FALSE(callout_manager_->calloutsPresent(
  214. ServerHooks::CONTEXT_DESTROY));
  215. // Call the runLoad() method to run the load() function.
  216. EXPECT_TRUE(lib_manager.runLoad());
  217. EXPECT_TRUE(callout_manager_->calloutsPresent(
  218. ServerHooks::CONTEXT_CREATE));
  219. EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_one_index_));
  220. EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_two_index_));
  221. EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_three_index_));
  222. EXPECT_FALSE(callout_manager_->calloutsPresent(
  223. ServerHooks::CONTEXT_DESTROY));
  224. // Now execute the callouts in the order expected. The library performs
  225. // the calculation:
  226. //
  227. // r3 = (5 * d1 + d2) * d3
  228. executeCallCallouts(5, 5, 25, 7, 32, 10, 320);
  229. // Tidy up
  230. EXPECT_TRUE(lib_manager.closeLibrary());
  231. }
  232. // Check handling of a "load" function that throws an exception
  233. TEST_F(LibraryManagerTest, CheckLoadException) {
  234. // Load the only library, specifying the index of 0 as it's the only
  235. // library. This should load all callouts.
  236. PublicLibraryManager lib_manager(std::string(FRAMEWORK_EXCEPTION_LIBRARY),
  237. 0, callout_manager_);
  238. EXPECT_TRUE(lib_manager.openLibrary());
  239. // Running the load function should fail.
  240. EXPECT_FALSE(lib_manager.runLoad());
  241. // Tidy up
  242. EXPECT_TRUE(lib_manager.closeLibrary());
  243. }
  244. // Check handling of a "load" function that returns an error.
  245. TEST_F(LibraryManagerTest, CheckLoadError) {
  246. // Load the only library, specifying the index of 0 as it's the only
  247. // library. This should load all callouts.
  248. PublicLibraryManager lib_manager(std::string(LOAD_ERROR_CALLOUT_LIBRARY),
  249. 0, callout_manager_);
  250. EXPECT_TRUE(lib_manager.openLibrary());
  251. // Check that we catch a load error
  252. EXPECT_FALSE(lib_manager.runLoad());
  253. // Tidy up
  254. EXPECT_TRUE(lib_manager.closeLibrary());
  255. }
  256. // No unload function
  257. TEST_F(LibraryManagerTest, CheckNoUnload) {
  258. // Load the only library, specifying the index of 0 as it's the only
  259. // library. This should load all callouts.
  260. PublicLibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY),
  261. 0, callout_manager_);
  262. EXPECT_TRUE(lib_manager.openLibrary());
  263. // Check that no unload function returns true.
  264. EXPECT_TRUE(lib_manager.runUnload());
  265. // Tidy up
  266. EXPECT_TRUE(lib_manager.closeLibrary());
  267. }
  268. // Unload function returns an error
  269. TEST_F(LibraryManagerTest, CheckUnloadError) {
  270. // Load the only library, specifying the index of 0 as it's the only
  271. // library. This should load all callouts.
  272. PublicLibraryManager lib_manager(std::string(LOAD_ERROR_CALLOUT_LIBRARY),
  273. 0, callout_manager_);
  274. EXPECT_TRUE(lib_manager.openLibrary());
  275. // Check that unload function returning an error returns false.
  276. EXPECT_FALSE(lib_manager.runUnload());
  277. // Tidy up
  278. EXPECT_TRUE(lib_manager.closeLibrary());
  279. }
  280. // Unload function throws an exception.
  281. TEST_F(LibraryManagerTest, CheckUnloadException) {
  282. // Load the only library, specifying the index of 0 as it's the only
  283. // library. This should load all callouts.
  284. PublicLibraryManager lib_manager(std::string(FRAMEWORK_EXCEPTION_LIBRARY),
  285. 0, callout_manager_);
  286. EXPECT_TRUE(lib_manager.openLibrary());
  287. // Check that we detect that the unload function throws an exception.
  288. EXPECT_FALSE(lib_manager.runUnload());
  289. // Tidy up
  290. EXPECT_TRUE(lib_manager.closeLibrary());
  291. }
  292. // Check that the case of the library's unload() function returning a
  293. // success is handled correctly.
  294. TEST_F(LibraryManagerTest, CheckUnload) {
  295. // Load the only library, specifying the index of 0 as it's the only
  296. // library. This should load all callouts.
  297. PublicLibraryManager lib_manager(std::string(UNLOAD_CALLOUT_LIBRARY),
  298. 0, callout_manager_);
  299. EXPECT_TRUE(lib_manager.openLibrary());
  300. // Check that the marker file is not present (at least that the file
  301. // open fails).
  302. EXPECT_FALSE(markerFilePresent());
  303. // Check that unload function runs and returns a success
  304. EXPECT_TRUE(lib_manager.runUnload());
  305. // Check that the marker file was created.
  306. EXPECT_TRUE(markerFilePresent());
  307. // Tidy up
  308. EXPECT_TRUE(lib_manager.closeLibrary());
  309. }
  310. // Test the operation of unloadLibrary(). We load a library with a set
  311. // of callouts then unload it. We need to check that the callouts have been
  312. // removed. We'll also check that the library's unload() function was called
  313. // as well.
  314. TEST_F(LibraryManagerTest, LibUnload) {
  315. // Load the only library, specifying the index of 0 as it's the only
  316. // library. This should load all callouts.
  317. PublicLibraryManager lib_manager(std::string(FULL_CALLOUT_LIBRARY),
  318. 0, callout_manager_);
  319. EXPECT_TRUE(lib_manager.openLibrary());
  320. // Check the version of the library.
  321. EXPECT_TRUE(lib_manager.checkVersion());
  322. // No callouts should be registered at the moment.
  323. EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_one_index_));
  324. EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_));
  325. EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_));
  326. // Load the single standard callout and check it is registered correctly.
  327. EXPECT_NO_THROW(lib_manager.registerStandardCallouts());
  328. EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_one_index_));
  329. EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_));
  330. EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_));
  331. // Call the load function to load the other callouts.
  332. EXPECT_TRUE(lib_manager.runLoad());
  333. EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_one_index_));
  334. EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_two_index_));
  335. EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_three_index_));
  336. // Unload the library and check that the callouts have been removed from
  337. // the CalloutManager.
  338. lib_manager.unloadLibrary();
  339. EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_one_index_));
  340. EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_));
  341. EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_));
  342. }
  343. // Now come the loadLibrary() tests that make use of all the methods tested
  344. // above. These tests are really to make sure that the methods have been
  345. // tied together correctly.
  346. // First test the basic error cases - no library, no version function, version
  347. // function returning an error.
  348. TEST_F(LibraryManagerTest, LoadLibraryNoLibrary) {
  349. LibraryManager lib_manager(std::string(NOT_PRESENT_LIBRARY), 0,
  350. callout_manager_);
  351. EXPECT_FALSE(lib_manager.loadLibrary());
  352. }
  353. // Check that the code handles the case of a library with no version function.
  354. TEST_F(LibraryManagerTest, LoadLibraryNoVersion) {
  355. LibraryManager lib_manager(std::string(NO_VERSION_LIBRARY), 0,
  356. callout_manager_);
  357. EXPECT_FALSE(lib_manager.loadLibrary());
  358. }
  359. // Check that the code handles the case of a library with a version function
  360. // that returns an incorrect version number.
  361. TEST_F(LibraryManagerTest, LoadLibraryWrongVersion) {
  362. LibraryManager lib_manager(std::string(INCORRECT_VERSION_LIBRARY), 0,
  363. callout_manager_);
  364. EXPECT_FALSE(lib_manager.loadLibrary());
  365. }
  366. // Check that the full loadLibrary call works.
  367. TEST_F(LibraryManagerTest, LoadLibrary) {
  368. LibraryManager lib_manager(std::string(FULL_CALLOUT_LIBRARY), 0,
  369. callout_manager_);
  370. EXPECT_TRUE(lib_manager.loadLibrary());
  371. // Now execute the callouts in the order expected. The library performs
  372. // the calculation:
  373. //
  374. // r3 = (7 * d1 - d2) * d3
  375. executeCallCallouts(7, 5, 35, 9, 26, 3, 78);
  376. EXPECT_TRUE(lib_manager.unloadLibrary());
  377. // Check that the callouts have been removed from the callout manager.
  378. EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_one_index_));
  379. EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_));
  380. EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_));
  381. }
  382. // Now test for multiple libraries. We'll load the full callout library
  383. // first, then load some of the libraries with missing framework functions.
  384. // This will check that when searching for framework functions, only the
  385. // specified library is checked, not other loaded libraries. We will
  386. // load a second library with suitable callouts and check that the callouts
  387. // are added correctly. Finally, we'll unload one of the libraries and
  388. // check that only the callouts belonging to that library were removed.
  389. TEST_F(LibraryManagerTest, LoadMultipleLibraries) {
  390. // Load a library with all framework functions.
  391. LibraryManager lib_manager_1(std::string(FULL_CALLOUT_LIBRARY), 0,
  392. callout_manager_);
  393. EXPECT_TRUE(lib_manager_1.loadLibrary());
  394. // Attempt to load a library with no version() function. We should detect
  395. // this and not end up calling the function from the already loaded
  396. // library.
  397. LibraryManager lib_manager_2(std::string(NO_VERSION_LIBRARY), 1,
  398. callout_manager_);
  399. EXPECT_FALSE(lib_manager_2.loadLibrary());
  400. // Attempt to load the library with an incorrect version. This should
  401. // be detected.
  402. LibraryManager lib_manager_3(std::string(INCORRECT_VERSION_LIBRARY), 1,
  403. callout_manager_);
  404. EXPECT_FALSE(lib_manager_3.loadLibrary());
  405. // Load the basic callout library. This only has standard callouts so,
  406. // if the first library's load() function gets called, some callouts
  407. // will be registered twice and lead to incorrect results.
  408. LibraryManager lib_manager_4(std::string(BASIC_CALLOUT_LIBRARY), 1,
  409. callout_manager_);
  410. EXPECT_TRUE(lib_manager_4.loadLibrary());
  411. // Execute the callouts. The first library implements the calculation.
  412. //
  413. // r3 = (7 * d1 - d2) * d3
  414. //
  415. // The last-loaded library implements the calculation
  416. //
  417. // r3 = (10 + d1) * d2 - d3
  418. //
  419. // Putting the processing for each library together in the appropriate
  420. // order, we get:
  421. //
  422. // r3 = ((10 * d1 + d1) - d2) * d2 * d3 - d3
  423. executeCallCallouts(10, 3, 33, 2, 62, 3, 183);
  424. // All done, so unload the first library.
  425. EXPECT_TRUE(lib_manager_1.unloadLibrary());
  426. // Now execute the callouts again and check that the results are as
  427. // expected for the new calculation.
  428. executeCallCallouts(10, 5, 15, 7, 105, 17, 88);
  429. // ... and tidy up.
  430. EXPECT_TRUE(lib_manager_4.unloadLibrary());
  431. }
  432. // Check that libraries can be validated.
  433. TEST_F(LibraryManagerTest, validateLibraries) {
  434. EXPECT_TRUE(LibraryManager::validateLibrary(BASIC_CALLOUT_LIBRARY));
  435. EXPECT_TRUE(LibraryManager::validateLibrary(FULL_CALLOUT_LIBRARY));
  436. EXPECT_FALSE(LibraryManager::validateLibrary(FRAMEWORK_EXCEPTION_LIBRARY));
  437. EXPECT_FALSE(LibraryManager::validateLibrary(INCORRECT_VERSION_LIBRARY));
  438. EXPECT_TRUE(LibraryManager::validateLibrary(LOAD_CALLOUT_LIBRARY));
  439. EXPECT_TRUE(LibraryManager::validateLibrary(LOAD_ERROR_CALLOUT_LIBRARY));
  440. EXPECT_FALSE(LibraryManager::validateLibrary(NOT_PRESENT_LIBRARY));
  441. EXPECT_FALSE(LibraryManager::validateLibrary(NO_VERSION_LIBRARY));
  442. EXPECT_TRUE(LibraryManager::validateLibrary(UNLOAD_CALLOUT_LIBRARY));
  443. EXPECT_TRUE(LibraryManager::validateLibrary(CALLOUT_PARAMS_LIBRARY));
  444. }
  445. // Check that log messages are properly registered and unregistered.
  446. TEST_F(LibraryManagerTest, libraryLoggerSetup) {
  447. // Load a library with all framework functions.
  448. LibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY), 0,
  449. callout_manager_);
  450. EXPECT_TRUE(lib_manager.loadLibrary());
  451. // After loading the library, the global logging dictionary should
  452. // contain log messages registered for this library.
  453. const MessageDictionaryPtr& dict = MessageDictionary::globalDictionary();
  454. EXPECT_EQ("basic callout load %1", dict->getText("BCL_LOAD_START"));
  455. EXPECT_EQ("basic callout load end", dict->getText("BCL_LOAD_END"));
  456. // Some of the messages defined by the hook library are duplicates. But,
  457. // the loadLibrary function should have logged the duplicates and clear
  458. // the duplicates list. By checking that the list of duplicates is empty
  459. // we test that the LibraryManager handles the duplicates (logs and
  460. // clears them).
  461. EXPECT_TRUE(MessageInitializer::getDuplicates().empty());
  462. // After unloading the library, the messages should be unregistered.
  463. EXPECT_TRUE(lib_manager.unloadLibrary());
  464. EXPECT_TRUE(dict->getText("BCL_LOAD_START").empty());
  465. EXPECT_TRUE(dict->getText("BCL_LOAD_END").empty());
  466. }
  467. } // Anonymous namespace