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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (C) 2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

/// @file This file contains tests which exercise the load and unload
/// functions in the role based access control hook library. In order
/// to test the load function, one must be able to pass it hook
/// library parameters. The the only way to populate these parameters
/// is by actually loading the library via HooksManager::loadLibraries().

#include <config.h>

#include <hooks/hooks_manager.h>
#include <process/daemon.h>

#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <errno.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc;
using namespace isc::data;
using namespace isc::hooks;
using namespace isc::process;

namespace {

/// @brief Test fixture for testing loading and unloading the RBAC library
class LibLoadTest : public ::testing::Test {
public:
    /// @brief Constructor
    LibLoadTest() {
        reset();
    }

    /// @brief Destructor
    /// Removes files that may be left over from previous tests
    virtual ~LibLoadTest() {
        reset();
    }

    /// @brief Removes files that may be left over from previous tests
    virtual void reset() {
        HooksManager::unloadLibraries();
    }

    /// @brief Adds library/parameters to list of libraries to be loaded.
    void addLib(const std::string& lib, ConstElementPtr params) {
        libraries_.push_back(make_pair(lib, params));
    }

    /// @brief Load all specified libraries.
    ///
    /// The libraries are stored in libraries_.
    bool loadLibs() {
        return (HooksManager::loadLibraries(libraries_));
    }

    /// @brief Unloads all libraries.
    void unloadLibs() {
        EXPECT_NO_THROW(HooksManager::unloadLibraries());
    }

    /// @brief Return basic, valid RBAC configuration in JSON format.
    ElementPtr createValidJsonConfiguration() const {
        ElementPtr cfg = Element::createMap();
        cfg->set("assign-role-method",
                 Element::create(string("remote-address")));
        cfg->set("api-files", Element::create(string(API_DIR)));
        return (cfg);
    }

    /// @brief Libraries.
    HookLibsCollection libraries_;
};

// Simple test that checks the library can be loaded in a control agent.
TEST_F(LibLoadTest, validLoad) {<--- syntax error
    // Set proc name.
    Daemon::setProcName("kea-ctrl-agent");

    // Add library with valid configuration.
    addLib(RBAC_LIB_SO, createValidJsonConfiguration());

    // Library should load without issue.
    EXPECT_TRUE(loadLibs());
}

// Simple test that checks the library can be loaded and unloaded several times
// in a control agent.
TEST_F(LibLoadTest, validLoads) {
    // Set proc name.
    Daemon::setProcName("kea-ctrl-agent");

    // Add library with valid configuration.
    addLib(RBAC_LIB_SO, createValidJsonConfiguration());

    EXPECT_TRUE(loadLibs());
    unloadLibs();

    EXPECT_TRUE(loadLibs());
    unloadLibs();

    loadLibs();
    unloadLibs();
}

// Verifies that an unknown parameter in an otherwise valid configuration
// is detected.
TEST_F(LibLoadTest, unknownParameterLoad) {
    // Set proc name.
    Daemon::setProcName("kea-ctrl-agent");

    /// Add unknown element "foo" to valid config.
    ElementPtr cfg = createValidJsonConfiguration();
    cfg->set("foo", Element::create("bar"));

    // Add library with invalid configuration.
    addLib(RBAC_LIB_SO, cfg);

    // The load should fail.
    EXPECT_FALSE(loadLibs());
}

// Verifies that a bad type parameter in an otherwise valid configuration
// is detected.
TEST_F(LibLoadTest, badTypeParameterLoad) {
    // Set proc name.
    Daemon::setProcName("kea-ctrl-agent");

    /// Add bad type to valid config.
    ElementPtr cfg = createValidJsonConfiguration();
    cfg->set("require-tls", Element::create("bar"));

    // Add library with invalid configuration.
    addLib(RBAC_LIB_SO, cfg);

    // The load should fail.
    EXPECT_FALSE(loadLibs());
}

// Verifies that a bad parameter in an otherwise valid configuration
// is detected.
TEST_F(LibLoadTest, badParameterLoad) {
    // Set proc name.
    Daemon::setProcName("kea-ctrl-agent");

    /// Add bad value "foo" to valid config.
    ElementPtr cfg = createValidJsonConfiguration();
    cfg->set("list-match-first", Element::create("foo"));

    // Add library with invalid configuration.
    addLib(RBAC_LIB_SO, cfg);

    // The load should fail.
    EXPECT_FALSE(loadLibs());
}

// Verifies that the library can not be loaded in a DHCPv4 server.
TEST_F(LibLoadTest, badDhcpv4) {
    // Set proc name.
    Daemon::setProcName("kea-dhcp4");

    // Add library with valid configuration.
    addLib(RBAC_LIB_SO, createValidJsonConfiguration());

    // The load should fail.
    EXPECT_FALSE(loadLibs());
}

// Verifies that the library can not be loaded in a DHCPv6 server.
TEST_F(LibLoadTest, badDhcpv6) {
    // Set proc name.
    Daemon::setProcName("kea-dhcp6");

    // Add library with valid configuration.
    addLib(RBAC_LIB_SO, createValidJsonConfiguration());

    // The load should fail.
    EXPECT_FALSE(loadLibs());
}

} // end of anonymous namespace