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
// Copyright (C) 2018-2024 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/.
#ifndef TEST_CONFIG_BACKEND_H
#define TEST_CONFIG_BACKEND_H

#include <config.h>

#include <database/database_connection.h>
#include <dhcpsrv/config_backend_dhcp4_mgr.h>
#include <dhcpsrv/config_backend_dhcp6_mgr.h>
#include <boost/shared_ptr.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/lexical_cast.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace dhcp {
namespace test {

/// @brief Type of pointers to stamped elements.
typedef boost::shared_ptr<data::StampedElement> StampedElementPtr;

/// @brief Base class for implementing fake backends
template<typename ConfigBackendType>
class TestConfigBackend : public ConfigBackendType {
public:
    /// @brief Constructor
    ///
    /// @param params database connection parameters
    /// @throw BadValue if parameters do not include "type"
    TestConfigBackend(const db::DatabaseConnection::ParameterMap& params)
     : connection_(params) {
        try {
            db_type_ = connection_.getParameter("type");
        } catch (...) {
            isc_throw(BadValue, "Backend parameters must include \"type\"");
        }

        try {
            host_ = connection_.getParameter("host");
        } catch (...) {
            host_ = "default_host";
        }

        try {
            port_ = boost::lexical_cast<uint16_t>(connection_.getParameter("host"));
        } catch (...) {
            port_ = 0;
        }
    }

    /// @brief virtual Destructor.
    virtual ~TestConfigBackend(){};

    /// @brief Returns backend type.
    ///
    /// @return string db_type name
    virtual std::string getType() const {
        return (db_type_);
    }

    /// @brief Returns backend host.
    ///
    /// @return string host
    virtual std::string getHost() const {
        return (host_);
    }

    /// @brief Returns backend port.
    ///
    /// @return uint16_t port
    virtual uint16_t getPort() const {
        return (port_);
    }

    /// @brief Returns server tag to be associated with the stored configuration.
    ///
    /// @param server_selector Server selector.
    std::string getServerTag(const db::ServerSelector& server_selector) const {
        if (server_selector.getType() == db::ServerSelector::Type::ALL) {
            return ("all");
        }
        // Return first tag found.
        auto tags = server_selector.getTags();
        if (!tags.empty()) {
            return (tags.begin()->get());
        }
        return ("");
    }

    /// @brief Merge server tags for a stamped element and a server selector.
    ///
    /// @param elem Stamped element to update.
    /// @param server_selector Server selector.
    void mergeServerTags(const StampedElementPtr& elem,
                         const db::ServerSelector& server_selector) const {
        auto tags = server_selector.getTags();
        for (auto const& tag : tags) {
            elem->setServerTag(tag.get());
        }
    }

    /// @brief Fake database connection
    db::DatabaseConnection connection_;

    /// @brief  Back end type
    std::string db_type_;

    /// @brief  Back end host
    std::string host_;

    /// @brief  Back end port
    uint16_t port_;
};

} // namespace test
} // namespace dhcp
} // namespace isc

#endif // TEST_CONFIG_BACKEND_H