123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- // 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 <config.h>
- #include <hooks/hooks.h>
- #include <iostream>
- 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);
- }
-
- };
|