From 7eeb15908bc3be5dbfe80a4091325ce01c69222c Mon Sep 17 00:00:00 2001 From: Tomek Mrugalski Date: Tue, 15 Mar 2016 10:34:44 +0100 Subject: [PATCH] [4297] Added missing callout_params_library.cc --- src/lib/hooks/tests/callout_params_library.cc | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/lib/hooks/tests/callout_params_library.cc diff --git a/src/lib/hooks/tests/callout_params_library.cc b/src/lib/hooks/tests/callout_params_library.cc new file mode 100644 index 0000000000..d934f5b1fa --- /dev/null +++ b/src/lib/hooks/tests/callout_params_library.cc @@ -0,0 +1,106 @@ +// Copyright (C) 2016 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 +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +/// @file +/// @brief Callout Library +/// +/// This is the source of a test library for the DHCP parser tests that +/// specify parameters. It will attempt to obtain its own parameters. + +#include +#include +#include + +using namespace isc::hooks; +using namespace isc::data; + +extern "C" { + +// Framework functions +int +version() { + return (KEA_HOOKS_VERSION); +} + +/// @brief This method will be called when the hook library is loaded +/// +/// While its primary usage is for unit-testing, it also doubles as an +/// illustration referred to from Hooks Developer's Guide. As such, please +/// keep it simple, tidy and try to avoid referencing unnecessary code. +/// Parts of it can be used as copy-paste examples. +/// +/// @param handle passed by the hooks framework +/// @return 0 if load was successful, non-zero for errors +int load(LibraryHandle& handle) { + ConstElementPtr string_elem = handle.getParameter("svalue"); + ConstElementPtr int_elem = handle.getParameter("ivalue"); + ConstElementPtr bool_elem = handle.getParameter("bvalue"); + ConstElementPtr nonexistent = handle.getParameter("nonexistent"); + + // String handling example. + if (!string_elem) { + // Parameter was not specified at all. + return (1); + } + + if (string_elem->getType() != Element::string) { + // Parameter is specified, but it's not a string. + return (1); + } + + std::string str = string_elem->stringValue(); + if (str != "string value") { + // Parameter is specified, is a string, but has unexpected value. + // + // This library is used for testing, so it expects exact value of the + // parameter. Normal library would likely use whatever value user + // specified. + return (1); + } + + // Integer handling example + if (!int_elem) { + // Parameter was not specified at all. + return (1); + } + + if (int_elem->getType() != Element::integer) { + // Parameter is specified, but it's not an integer. + return (1); + } + + int int_value = int_elem->intValue(); + if (int_value != 42) { + // Parameter specified, is an integer, but has a value different than + // expected. + return (1); + } + + // Boolean handling example + if (!bool_elem) { + // Parameter was not specified at all. + return (1); + } + + if (bool_elem->getType() != Element::boolean) { + // Parameter is specified, but it's not a boolean. + return (1); + } + + bool flag = bool_elem->boolValue(); + if (flag != true) { + // Parameter specified, is a boolean, but has a value different than + // expected. + return (1); + } + + // All validation steps were successful. The library has all the parameters + // it needs, so we should report a success. + return (0); +} + + +};