Browse Source

[4297] Added missing callout_params_library.cc

Tomek Mrugalski 9 years ago
parent
commit
7eeb15908b
1 changed files with 106 additions and 0 deletions
  1. 106 0
      src/lib/hooks/tests/callout_params_library.cc

+ 106 - 0
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 <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);
+}
+    
+
+};