123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708 |
- #include <hooks/callout_handle.h>
- #include <hooks/hooks_manager.h>
- #include <hooks/server_hooks.h>
- #include <hooks/tests/common_test_class.h>
- #include <hooks/tests/test_libraries.h>
- #include <cc/data.h>
- #include <boost/shared_ptr.hpp>
- #include <gtest/gtest.h>
- #include <algorithm>
- #include <string>
- using namespace isc;
- using namespace isc::hooks;
- using namespace isc::data;
- using namespace std;
- namespace {
- class HooksManagerTest : public ::testing::Test,
- public HooksCommonTestClass {
- public:
-
-
-
-
- HooksManagerTest() {
- HooksManager::unloadLibraries();
- }
-
-
-
- ~HooksManagerTest() {
- HooksManager::unloadLibraries();
- CalloutManager::getSharedManager().reset();
- }
-
-
-
-
-
-
-
- void executeCallCallouts(int r0, int d1, int r1, int d2, int r2, int d3,
- int r3) {
- static const char* COMMON_TEXT = " callout returned the wrong value";
- static const char* RESULT = "result";
-
- CalloutHandlePtr handle = HooksManager::createCalloutHandle();
-
-
-
- int result = -1;
- handle->setArgument(RESULT, result);
-
- HooksManager::callCallouts(isc::hooks::ServerHooks::CONTEXT_CREATE,
- *handle);
- handle->getArgument(RESULT, result);
- EXPECT_EQ(r0, result) << "context_create" << COMMON_TEXT;
-
- handle->setArgument("data_1", d1);
- HooksManager::callCallouts(hookpt_one_index_, *handle);
- handle->getArgument(RESULT, result);
- EXPECT_EQ(r1, result) << "hookpt_one" << COMMON_TEXT;
-
- handle->setArgument("data_2", d2);
- HooksManager::callCallouts(hookpt_two_index_, *handle);
- handle->getArgument(RESULT, result);
- EXPECT_EQ(r2, result) << "hookpt_two" << COMMON_TEXT;
-
- handle->setArgument("data_3", d3);
- HooksManager::callCallouts(hookpt_three_index_, *handle);
- handle->getArgument(RESULT, result);
- EXPECT_EQ(r3, result) << "hookpt_three" << COMMON_TEXT;
- }
- private:
-
- std::string dummy(int i) {
- if (i == 0) {
- return (MARKER_FILE);
- } else if (i > 0) {
- return (LOAD_CALLOUT_LIBRARY);
- } else {
- return (LOAD_ERROR_CALLOUT_LIBRARY);
- }
- }
- };
- TEST_F(HooksManagerTest, LoadLibraries) {
-
- HookLibsCollection library_names;
- library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
- library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
-
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
-
-
-
-
-
-
-
-
-
-
-
-
- {
- SCOPED_TRACE("Calculation with libraries loaded");
- executeCallCallouts(10, 3, 33, 2, 62, 3, 183);
- }
-
- EXPECT_NO_THROW(HooksManager::unloadLibraries());
-
-
- {
- SCOPED_TRACE("Calculation with libraries not loaded");
- executeCallCallouts(-1, 3, -1, 22, -1, 83, -1);
- }
- }
- TEST_F(HooksManagerTest, LoadLibrariesWithError) {
-
- HookLibsCollection library_names;
- library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
- library_names.push_back(make_pair(std::string(INCORRECT_VERSION_LIBRARY),
- data::ConstElementPtr()));
- library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
-
-
- EXPECT_FALSE(HooksManager::loadLibraries(library_names));
- }
- TEST_F(HooksManagerTest, CalloutHandleUnloadLibrary) {
-
- HookLibsCollection library_names;
- library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
-
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
-
-
-
- {
- SCOPED_TRACE("Calculation with full callout library loaded");
- executeCallCallouts(7, 4, 28, 8, 20, 2, 40);
- }
-
- CalloutHandlePtr handle = HooksManager::createCalloutHandle();
-
-
- HooksManager::callCallouts(ServerHooks::CONTEXT_CREATE, *handle);
-
- HooksManager::unloadLibraries();
-
- handle.reset();
- }
- TEST_F(HooksManagerTest, CalloutHandleLoadLibrary) {
-
- HookLibsCollection library_names;
- library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
-
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
-
-
-
- {
- SCOPED_TRACE("Calculation with full callout library loaded");
- executeCallCallouts(7, 4, 28, 8, 20, 2, 40);
- }
-
-
-
- CalloutHandlePtr handle = HooksManager::createCalloutHandle();
- HooksManager::callCallouts(ServerHooks::CONTEXT_CREATE, *handle);
-
-
-
- HookLibsCollection new_library_names;
- new_library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
-
- EXPECT_TRUE(HooksManager::loadLibraries(new_library_names));
-
-
- {
- SCOPED_TRACE("Calculation with basic callout library loaded");
- executeCallCallouts(10, 7, 17, 3, 51, 16, 35);
- }
-
- handle.reset();
- }
- TEST_F(HooksManagerTest, ReloadSameLibraries) {
-
- HookLibsCollection library_names;
- library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
- library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
-
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
-
-
- {
- SCOPED_TRACE("Calculation with libraries loaded");
- executeCallCallouts(10, 3, 33, 2, 62, 3, 183);
- }
-
-
- EXPECT_NO_THROW(HooksManager::loadLibraries(library_names));
- {
- SCOPED_TRACE("Calculation with libraries reloaded");
- executeCallCallouts(10, 3, 33, 2, 62, 3, 183);
- }
- }
- TEST_F(HooksManagerTest, ReloadLibrariesReverseOrder) {
-
- HookLibsCollection library_names;
- library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
- library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
-
-
-
-
-
-
-
-
-
-
-
-
- {
- SCOPED_TRACE("Calculation with libraries loaded");
- executeCallCallouts(10, 3, 33, 2, 62, 3, 183);
- }
-
- std::reverse(library_names.begin(), library_names.end());
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
-
-
-
- {
- SCOPED_TRACE("Calculation with libraries loaded in reverse order");
- executeCallCallouts(7, 3, 30, 3, 87, 7, 560);
- }
- }
- namespace {
- int
- testPreCallout(CalloutHandle& handle) {
- handle.setArgument("result", static_cast<int>(1027));
- return (0);
- }
- int
- testPostCallout(CalloutHandle& handle) {
- int result;
- handle.getArgument("result", result);
- result *= 2;
- handle.setArgument("result", result);
- return (0);
- }
- }
- TEST_F(HooksManagerTest, PrePostCalloutTest) {
-
- HookLibsCollection library_names;
- library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
-
- HooksManager::preCalloutsLibraryHandle().registerCallout("hookpt_two",
- testPreCallout);
- HooksManager::postCalloutsLibraryHandle().registerCallout("hookpt_two",
- testPostCallout);
-
-
-
-
-
-
-
- CalloutHandlePtr handle = HooksManager::createCalloutHandle();
- handle->setArgument("result", static_cast<int>(0));
- handle->setArgument("data_2", static_cast<int>(15));
- HooksManager::callCallouts(hookpt_two_index_, *handle);
- int result = 0;
- handle->getArgument("result", result);
- EXPECT_EQ(2024, result);
-
-
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
- handle = HooksManager::createCalloutHandle();
- handle->setArgument("result", static_cast<int>(0));
- handle->setArgument("data_2", static_cast<int>(15));
- HooksManager::callCallouts(hookpt_two_index_, *handle);
- result = 0;
- handle->getArgument("result", result);
- EXPECT_EQ(-15, result);
- }
- TEST_F(HooksManagerTest, PrePostCalloutShared) {
- HookLibsCollection library_names;
-
- CalloutManager::getSharedManager().reset(new CalloutManager(0));
-
- HooksManager::preCalloutsLibraryHandle().registerCallout("hookpt_two",
- testPreCallout);
- HooksManager::postCalloutsLibraryHandle().registerCallout("hookpt_two",
- testPostCallout);
-
-
-
- CalloutHandlePtr handle = HooksManager::createCalloutHandle();
- handle->setArgument("result", static_cast<int>(0));
- handle->setArgument("data_2", static_cast<int>(15));
- HooksManager::callCallouts(hookpt_two_index_, *handle);
- int result = 0;
- handle->getArgument("result", result);
- EXPECT_EQ(2054, result);
-
-
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
- handle = HooksManager::createCalloutHandle();
- handle->setArgument("result", static_cast<int>(0));
- handle->setArgument("data_2", static_cast<int>(15));
- HooksManager::callCallouts(hookpt_two_index_, *handle);
-
- result = 0;
- handle->getArgument("result", result);
- EXPECT_EQ(2054, result);
- }
- TEST_F(HooksManagerTest, PrePostCalloutSharedNotEmpty) {
- HookLibsCollection library_names;
- library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
-
- CalloutManager::getSharedManager().reset(new CalloutManager(0));
-
- HooksManager::preCalloutsLibraryHandle().registerCallout("hookpt_two",
- testPreCallout);
- HooksManager::postCalloutsLibraryHandle().registerCallout("hookpt_two",
- testPostCallout);
-
-
-
- CalloutHandlePtr handle = HooksManager::createCalloutHandle();
- handle->setArgument("result", static_cast<int>(0));
- handle->setArgument("data_2", static_cast<int>(15));
- HooksManager::callCallouts(hookpt_two_index_, *handle);
- int result = 0;
- handle->getArgument("result", result);
- EXPECT_EQ(2054, result);
-
-
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
- handle = HooksManager::createCalloutHandle();
- handle->setArgument("result", static_cast<int>(0));
- handle->setArgument("data_2", static_cast<int>(15));
- HooksManager::callCallouts(hookpt_two_index_, *handle);
-
- result = 0;
- handle->getArgument("result", result);
- EXPECT_EQ(-15, result);
- }
- TEST_F(HooksManagerTest, PrePostCalloutSharedTooLate) {
- HookLibsCollection library_names;
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
-
- CalloutManager::getSharedManager().reset(new CalloutManager(0));
-
- HooksManager::preCalloutsLibraryHandle().registerCallout("hookpt_two",
- testPreCallout);
- HooksManager::postCalloutsLibraryHandle().registerCallout("hookpt_two",
- testPostCallout);
-
-
-
- CalloutHandlePtr handle = HooksManager::createCalloutHandle();
- handle->setArgument("result", static_cast<int>(0));
- handle->setArgument("data_2", static_cast<int>(15));
- HooksManager::callCallouts(hookpt_two_index_, *handle);
- int result = 0;
- handle->getArgument("result", result);
- EXPECT_EQ(2054, result);
-
-
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
- handle = HooksManager::createCalloutHandle();
- handle->setArgument("result", static_cast<int>(0));
- handle->setArgument("data_2", static_cast<int>(15));
- HooksManager::callCallouts(hookpt_two_index_, *handle);
-
- result = 0;
- handle->getArgument("result", result);
- EXPECT_EQ(0, result);
- }
- TEST_F(HooksManagerTest, NoLibrariesCalloutsPresent) {
-
- EXPECT_FALSE(HooksManager::calloutsPresent(hookpt_one_index_));
- EXPECT_FALSE(HooksManager::calloutsPresent(hookpt_two_index_));
- EXPECT_FALSE(HooksManager::calloutsPresent(hookpt_three_index_));
- }
- TEST_F(HooksManagerTest, NoLibrariesCallCallouts) {
- executeCallCallouts(-1, 3, -1, 22, -1, 83, -1);
- }
- TEST_F(HooksManagerTest, RegisterHooks) {
- ServerHooks::getServerHooks().reset();
- EXPECT_EQ(2, ServerHooks::getServerHooks().getCount());
-
-
- int sh_cc = ServerHooks::CONTEXT_CREATE;
- int hm_cc = HooksManager::CONTEXT_CREATE;
- EXPECT_EQ(sh_cc, hm_cc);
- int sh_cd = ServerHooks::CONTEXT_DESTROY;
- int hm_cd = HooksManager::CONTEXT_DESTROY;
- EXPECT_EQ(sh_cd, hm_cd);
-
- EXPECT_EQ(2, HooksManager::registerHook(string("alpha")));
- EXPECT_EQ(3, HooksManager::registerHook(string("beta")));
- EXPECT_EQ(4, HooksManager::registerHook(string("gamma")));
- EXPECT_THROW(static_cast<void>(HooksManager::registerHook(string("alpha"))),
- DuplicateHook);
-
- EXPECT_EQ(5, ServerHooks::getServerHooks().getCount());
- vector<string> names = ServerHooks::getServerHooks().getHookNames();
- sort(names.begin(), names.end());
- EXPECT_EQ(string("alpha"), names[0]);
- EXPECT_EQ(string("beta"), names[1]);
- EXPECT_EQ(string("context_create"), names[2]);
- EXPECT_EQ(string("context_destroy"), names[3]);
- EXPECT_EQ(string("gamma"), names[4]);
- }
- TEST_F(HooksManagerTest, LibraryNames) {
-
- HookLibsCollection library_names;
- library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
- library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
-
- std::vector<std::string> loaded_names = HooksManager::getLibraryNames();
- EXPECT_TRUE(loaded_names.empty());
-
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
- loaded_names = HooksManager::getLibraryNames();
- EXPECT_TRUE(extractNames(library_names) == loaded_names);
-
- EXPECT_NO_THROW(HooksManager::unloadLibraries());
- loaded_names = HooksManager::getLibraryNames();
- EXPECT_TRUE(loaded_names.empty());
- }
- TEST_F(HooksManagerTest, validateLibraries) {
-
- std::vector<std::string> failed;
-
-
- std::vector<std::string> libraries;
- failed = HooksManager::validateLibraries(libraries);
- EXPECT_TRUE(failed.empty());
-
- libraries.clear();
- libraries.push_back(BASIC_CALLOUT_LIBRARY);
- failed = HooksManager::validateLibraries(libraries);
- EXPECT_TRUE(failed.empty());
-
- libraries.clear();
- libraries.push_back(BASIC_CALLOUT_LIBRARY);
- libraries.push_back(FULL_CALLOUT_LIBRARY);
- libraries.push_back(UNLOAD_CALLOUT_LIBRARY);
- failed = HooksManager::validateLibraries(libraries);
- EXPECT_TRUE(failed.empty());
-
- libraries.clear();
- libraries.push_back(NOT_PRESENT_LIBRARY);
- failed = HooksManager::validateLibraries(libraries);
- EXPECT_TRUE(failed == libraries);
-
- libraries.clear();
- libraries.push_back(INCORRECT_VERSION_LIBRARY);
- libraries.push_back(NO_VERSION_LIBRARY);
- libraries.push_back(FRAMEWORK_EXCEPTION_LIBRARY);
- failed = HooksManager::validateLibraries(libraries);
- EXPECT_TRUE(failed == libraries);
-
- libraries.clear();
- libraries.push_back(FULL_CALLOUT_LIBRARY);
- libraries.push_back(INCORRECT_VERSION_LIBRARY);
- libraries.push_back(NO_VERSION_LIBRARY);
- std::vector<std::string> expected_failures;
- expected_failures.push_back(INCORRECT_VERSION_LIBRARY);
- expected_failures.push_back(NO_VERSION_LIBRARY);
- failed = HooksManager::validateLibraries(libraries);
- EXPECT_TRUE(failed == expected_failures);
-
- libraries.clear();
- libraries.push_back(NO_VERSION_LIBRARY);
- libraries.push_back(FULL_CALLOUT_LIBRARY);
- libraries.push_back(INCORRECT_VERSION_LIBRARY);
- expected_failures.clear();
- expected_failures.push_back(NO_VERSION_LIBRARY);
- expected_failures.push_back(INCORRECT_VERSION_LIBRARY);
- failed = HooksManager::validateLibraries(libraries);
- EXPECT_TRUE(failed == expected_failures);
- }
- TEST_F(HooksManagerTest, LibraryParameters) {
-
- ElementPtr params = Element::createMap();
- params->set("svalue", Element::create("string value"));
- params->set("ivalue", Element::create(42));
- params->set("bvalue", Element::create(true));
-
- HookLibsCollection library_names;
- library_names.push_back(make_pair(std::string(BASIC_CALLOUT_LIBRARY),
- data::ConstElementPtr()));
- library_names.push_back(make_pair(std::string(CALLOUT_PARAMS_LIBRARY),
- params));
-
-
-
-
- EXPECT_TRUE(HooksManager::loadLibraries(library_names));
-
- EXPECT_NO_THROW(HooksManager::unloadLibraries());
- }
- }
|