From: Marcin Siodelski Date: Thu, 13 Jul 2017 08:29:10 +0000 (+0200) Subject: [5329] Added tests for registering command handlers in a hook library. X-Git-Tag: trac5124a_base~42^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=34325d6bf9f5a73fd627f042edfe1e0f53edfe69;p=thirdparty%2Fkea.git [5329] Added tests for registering command handlers in a hook library. --- diff --git a/src/lib/hooks/tests/common_test_class.h b/src/lib/hooks/tests/common_test_class.h index 48251e41b1..5cfef3d578 100644 --- a/src/lib/hooks/tests/common_test_class.h +++ b/src/lib/hooks/tests/common_test_class.h @@ -1,4 +1,4 @@ -// Copyright (C) 2013-2015 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -125,6 +125,46 @@ public: EXPECT_EQ(r3, result) << "hookpt_three" << COMMON_TEXT; } + /// @brief Call command handlers test. + /// + /// This test is similar to @c executeCallCallouts but it uses + /// @ref CalloutManager::callCommandHandlers to execute the command + /// handlers for the following commands: 'command-one' and 'command-two'. + /// + /// @param manager CalloutManager to use for the test + /// @param r1..r2, d1..d2 Data (dN) and expected results (rN). + void executeCallCommandHandlers( + const boost::shared_ptr& manager, + int d1, int r1, int d2, int r2) { + static const char* COMMON_TEXT = " command handler returned the wrong value"; + static const char* RESULT = "result"; + + int result; + + // Set up a callout handle for the calls. + isc::hooks::CalloutHandle handle(manager); + + // Initialize the argument RESULT. This simplifies testing by + // eliminating the generation of an exception when we try the unload + // test. In that case, RESULT is unchanged. + handle.setArgument(RESULT, -1); + + // Perform the first calculation: it should assign the data to the + // result. + handle.setArgument("data_1", d1); + manager->callCommandHandlers("command-one", handle); + handle.getArgument(RESULT, result); + EXPECT_EQ(d1, result) << "command-one" << COMMON_TEXT; + + // Perform the second calculation: it should multiply the data by 10 + // and return in the result. + handle.setArgument("data_2", d2); + manager->callCommandHandlers("command-two", handle); + handle.getArgument(RESULT, result); + EXPECT_EQ(r2, result) << "command-two" << COMMON_TEXT; + } + + /// Hook indexes. These are are made public for ease of reference. int hookpt_one_index_; int hookpt_two_index_; diff --git a/src/lib/hooks/tests/library_manager_unittest.cc b/src/lib/hooks/tests/library_manager_unittest.cc index 7c8783c46f..b97e514638 100644 --- a/src/lib/hooks/tests/library_manager_unittest.cc +++ b/src/lib/hooks/tests/library_manager_unittest.cc @@ -93,6 +93,17 @@ public: r1, d2, r2, d3, r3); } + /// @brief Call command handlers test. + /// + /// A wrapper around the method of the same name in the HooksCommonTestClass + /// object, this passes this class's CalloutManager to that method. + /// + /// @param r1..r2, d1..d2 Values and intermediate values expected. + void executeCallCommandHandlers(int d1, int r1, int d2, int r2) { + HooksCommonTestClass::executeCallCommandHandlers(callout_manager_, + d1, r1, d2, r2); + } + /// Callout manager used for the test. boost::shared_ptr callout_manager_; }; @@ -271,6 +282,8 @@ TEST_F(LibraryManagerTest, CheckLoadCalled) { EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_one_index_)); EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_)); EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_)); + EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-one")); + EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-two")); EXPECT_FALSE(callout_manager_->calloutsPresent( ServerHooks::CONTEXT_DESTROY)); @@ -281,6 +294,8 @@ TEST_F(LibraryManagerTest, CheckLoadCalled) { EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_one_index_)); EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_two_index_)); EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_three_index_)); + EXPECT_TRUE(callout_manager_->commandHandlersPresent("command-one")); + EXPECT_TRUE(callout_manager_->commandHandlersPresent("command-two")); EXPECT_FALSE(callout_manager_->calloutsPresent( ServerHooks::CONTEXT_DESTROY)); @@ -290,6 +305,11 @@ TEST_F(LibraryManagerTest, CheckLoadCalled) { // r3 = (5 * d1 + d2) * d3 executeCallCallouts(5, 5, 25, 7, 32, 10, 320); + // Execute command handlers for 'command-one' and 'command-two'. + // + // r2 = d1 * d2 * 10; + executeCallCommandHandlers(5, 5, 7, 350); + // Tidy up EXPECT_TRUE(lib_manager.closeLibrary()); } @@ -414,7 +434,7 @@ TEST_F(LibraryManagerTest, LibUnload) { // Load the only library, specifying the index of 0 as it's the only // library. This should load all callouts. - PublicLibraryManager lib_manager(std::string(FULL_CALLOUT_LIBRARY), + PublicLibraryManager lib_manager(std::string(LOAD_CALLOUT_LIBRARY), 0, callout_manager_); EXPECT_TRUE(lib_manager.openLibrary()); @@ -425,18 +445,24 @@ TEST_F(LibraryManagerTest, LibUnload) { EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_one_index_)); EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_)); EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_)); + EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-one")); + EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-two")); // Load the single standard callout and check it is registered correctly. EXPECT_NO_THROW(lib_manager.registerStandardCallouts()); EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_one_index_)); EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_)); EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_)); + EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-one")); + EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-two")); // Call the load function to load the other callouts. EXPECT_TRUE(lib_manager.runLoad()); EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_one_index_)); EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_two_index_)); EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_three_index_)); + EXPECT_TRUE(callout_manager_->commandHandlersPresent("command-one")); + EXPECT_TRUE(callout_manager_->commandHandlersPresent("command-two")); // Unload the library and check that the callouts have been removed from // the CalloutManager. @@ -444,6 +470,8 @@ TEST_F(LibraryManagerTest, LibUnload) { EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_one_index_)); EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_)); EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_)); + EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-one")); + EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-two")); } // Now come the loadLibrary() tests that make use of all the methods tested diff --git a/src/lib/hooks/tests/load_callout_library.cc b/src/lib/hooks/tests/load_callout_library.cc index 5b96f32166..40606f7466 100644 --- a/src/lib/hooks/tests/load_callout_library.cc +++ b/src/lib/hooks/tests/load_callout_library.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2013-2015 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -77,7 +77,7 @@ hook_nonstandard_two(CalloutHandle& handle) { return (0); } -// Final callout adds "data_3" to the result. +// Third callout adds "data_3" to the result. static int hook_nonstandard_three(CalloutHandle& handle) { @@ -93,6 +93,38 @@ hook_nonstandard_three(CalloutHandle& handle) { return (0); } +// First command handler assigns data to a result. + +static int +command_handler_one(CalloutHandle& handle) { + int data; + handle.getArgument("data_1", data); + + int result; + handle.getArgument("result", result); + + result = data; + handle.setArgument("result", result); + + return (0); +} + +// Second command handler multiples the result by data by 10. + +static int +command_handler_two(CalloutHandle& handle) { + int data; + handle.getArgument("data_2", data); + + int result; + handle.getArgument("result", result); + + result *= data * 10; + handle.setArgument("result", result); + + return (0); +} + // Framework functions int @@ -109,6 +141,10 @@ int load(LibraryHandle& handle) { handle.registerCallout("hookpt_two", hook_nonstandard_two); handle.registerCallout("hookpt_three", hook_nonstandard_three); + // Register command_handler_one as control command handler. + handle.registerCommandHandler("command-one", command_handler_one); + handle.registerCommandHandler("command-two", command_handler_two); + return (0); }