callout_params_library.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. /// @file
  7. /// @brief Callout Library
  8. ///
  9. /// This is the source of a test library for the DHCP parser tests that
  10. /// specify parameters. It will attempt to obtain its own parameters.
  11. #include <config.h>
  12. #include <hooks/hooks.h>
  13. #include <iostream>
  14. using namespace isc::hooks;
  15. using namespace isc::data;
  16. extern "C" {
  17. // Framework functions
  18. int
  19. version() {
  20. return (KEA_HOOKS_VERSION);
  21. }
  22. /// @brief This method will be called when the hook library is loaded
  23. ///
  24. /// While its primary usage is for unit-testing, it also doubles as an
  25. /// illustration referred to from Hooks Developer's Guide. As such, please
  26. /// keep it simple, tidy and try to avoid referencing unnecessary code.
  27. /// Parts of it can be used as copy-paste examples.
  28. ///
  29. /// @param handle passed by the hooks framework
  30. /// @return 0 if load was successful, non-zero for errors
  31. int load(LibraryHandle& handle) {
  32. ConstElementPtr string_elem = handle.getParameter("svalue");
  33. ConstElementPtr int_elem = handle.getParameter("ivalue");
  34. ConstElementPtr bool_elem = handle.getParameter("bvalue");
  35. ConstElementPtr nonexistent = handle.getParameter("nonexistent");
  36. // String handling example.
  37. if (!string_elem) {
  38. // Parameter was not specified at all.
  39. return (1);
  40. }
  41. if (string_elem->getType() != Element::string) {
  42. // Parameter is specified, but it's not a string.
  43. return (1);
  44. }
  45. std::string str = string_elem->stringValue();
  46. if (str != "string value") {
  47. // Parameter is specified, is a string, but has unexpected value.
  48. //
  49. // This library is used for testing, so it expects exact value of the
  50. // parameter. Normal library would likely use whatever value user
  51. // specified.
  52. return (1);
  53. }
  54. // Integer handling example
  55. if (!int_elem) {
  56. // Parameter was not specified at all.
  57. return (1);
  58. }
  59. if (int_elem->getType() != Element::integer) {
  60. // Parameter is specified, but it's not an integer.
  61. return (1);
  62. }
  63. int int_value = int_elem->intValue();
  64. if (int_value != 42) {
  65. // Parameter specified, is an integer, but has a value different than
  66. // expected.
  67. return (1);
  68. }
  69. // Boolean handling example
  70. if (!bool_elem) {
  71. // Parameter was not specified at all.
  72. return (1);
  73. }
  74. if (bool_elem->getType() != Element::boolean) {
  75. // Parameter is specified, but it's not a boolean.
  76. return (1);
  77. }
  78. bool flag = bool_elem->boolValue();
  79. if (flag != true) {
  80. // Parameter specified, is a boolean, but has a value different than
  81. // expected.
  82. return (1);
  83. }
  84. // All validation steps were successful. The library has all the parameters
  85. // it needs, so we should report a success.
  86. return (0);
  87. }
  88. };