summaryrefslogtreecommitdiffstats
path: root/src/lib/hooks/tests/callout_params_library.cc
blob: d35fc008f02d386c746afe1c5d2d785170b95b77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (C) 2016-2020 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 std;
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 elems        = handle.getParameters();
    ConstElementPtr string_elem  = handle.getParameter("svalue");
    ConstElementPtr int_elem     = handle.getParameter("ivalue");
    ConstElementPtr bool_elem    = handle.getParameter("bvalue");
    ConstElementPtr nonexistent  = handle.getParameter("nonexistent");
    vector<string>  names        = handle.getParameterNames();

    // 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 (2);
    }

    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 (3);
    }

    // Integer handling example
    if (!int_elem) {
        // Parameter was not specified at all.
        return (4);
    }

    if (int_elem->getType() != Element::integer) {
        // Parameter is specified, but it's not an integer.
        return (5);
    }

    int int_value = int_elem->intValue();
    if (int_value != 42) {
        // Parameter specified, is an integer, but has a value different than
        // expected.
        return (6);
    }

    // Boolean handling example
    if (!bool_elem) {
        // Parameter was not specified at all.
        return (7);
    }

    if (bool_elem->getType() != Element::boolean) {
        // Parameter is specified, but it's not a boolean.
        return (8);
    }

    bool flag = bool_elem->boolValue();
    if (flag != true) {
        // Parameter specified, is a boolean, but has a value different than
        // expected.
        return (9);
    }

    // Check names. As a side effect of what maps using strings are
    // implemented names are sorted in alphabetical order.
    if ((names.size() != 3) || (names[0] != "bvalue") ||
        (names[1] != "ivalue") || (names[2] != "svalue")) {
        // Expect 3 names: bvalue, ivalue, svalue.
        return (10);
    }

    // Check elems map.
    if (!elems) {
        return (11);
    }
    string expected_str = "{ "
        "\"bvalue\": true, "
        "\"ivalue\": 42, "
        "\"svalue\": \"string value\""
        " }";
    if (expected_str != elems->str()) {
        return (12);
    }

    // All validation steps were successful. The library has all the parameters
    // it needs, so we should report a success.
    return (0);
}

}