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
// Copyright (C) 2022-2024 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.

#include <config.h>

#include <asiolink/io_service.h>
#include <cc/data.h>
#include <database/audit_entry.h>
#include <dhcp/pkt4.h>
#include <dhcp/pkt6.h>
#include <dhcpsrv/network_state.h>
#include <dhcpsrv/srv_config.h>
#include <dhcpsrv/subnet.h>
#include <dhcpsrv/testutils/lib_load_test_fixture.h>
#include <hooks/callout_handle.h>
#include <hooks/hooks_manager.h>
#include <hooks/library_handle.h>
#include <limits/limit_manager.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <testutils/gtest_utils.h>

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

#include <algorithm><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace {

using namespace isc;
using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::db;
using namespace isc::dhcp;
using namespace isc::hooks;
using namespace isc::limits;
using namespace isc::util;
using namespace std;

// Check that null parameters are handled correctly sans crashes.
TEST(LimitManagerTest, v4NullHandleParameters) {<--- syntax error
    CalloutHandlePtr cb_updated_handle(HooksManager::createCalloutHandle());
    cb_updated_handle->setArgument("audit_entries", AuditEntryCollectionPtr());
    ASSERT_THROW_MSG(LimitManager::instance().cb_updated<DHCPv4>(*cb_updated_handle), Unexpected,
                     "null audit_entries in LimitManager::cb_updated");

    CalloutHandlePtr pkt_receive_handle(HooksManager::createCalloutHandle());
    pkt_receive_handle->setArgument("query4", Pkt4Ptr());
    ASSERT_THROW_MSG(LimitManager::instance().pkt_receive<DHCPv4>(*pkt_receive_handle), Unexpected,
                     "null packet in LimitManager::pkt_receive");

    CalloutHandlePtr subnet_select_handle(HooksManager::createCalloutHandle());
    subnet_select_handle->setArgument("query4", Pkt4Ptr());
    subnet_select_handle->setArgument("subnet4", Subnet4Ptr());
    subnet_select_handle->setArgument("subnet4collection", Subnet4Collection());
    ASSERT_NO_THROW_LOG(LimitManager::instance().subnet_select<DHCPv4>(*subnet_select_handle));

    CalloutHandlePtr lease_select_handle(HooksManager::createCalloutHandle());
    lease_select_handle->setArgument("query4", Pkt4Ptr());
    lease_select_handle->setArgument("subnet4", Subnet4Ptr());
    lease_select_handle->setArgument("fake_allocation", false);
    lease_select_handle->setArgument("lease4", Lease4Ptr());
    ASSERT_THROW_MSG(LimitManager::instance().lease_callout<DHCPv4>(*lease_select_handle),
                     Unexpected, "null lease in LimitManager::lease_callout");

    // Check that limits can be configured without a LeaseMgr.
    LeaseMgrFactory::destroy();
    EXPECT_FALSE(LeaseMgrFactory::haveInstance());
    CalloutHandlePtr srv_configured_handle(HooksManager::createCalloutHandle());
    srv_configured_handle->setArgument("network_state", NetworkStatePtr());
    srv_configured_handle->setArgument("json_config", ConstElementPtr());
    srv_configured_handle->setArgument("server_config", SrvConfigPtr());
    ASSERT_NO_THROW_LOG(
        LimitManager::instance().dhcp_srv_configured<DHCPv4>(*srv_configured_handle));
    vector<string> arguments(srv_configured_handle->getArgumentNames());
    EXPECT_EQ(std::find(arguments.begin(), arguments.end(), "error"), arguments.end());
    EXPECT_EQ(CalloutHandle::NEXT_STEP_CONTINUE, srv_configured_handle->getStatus());

    // Check that limits can be configured with a LeaseMgr.
    LeaseMgrFactory::destroy();
    LeaseMgrFactory::create("type=memfile persist=false universe=4");
    EXPECT_TRUE(LeaseMgrFactory::haveInstance());
    srv_configured_handle = HooksManager::createCalloutHandle();
    srv_configured_handle->setArgument("network_state", NetworkStatePtr());
    srv_configured_handle->setArgument("json_config", ConstElementPtr());
    srv_configured_handle->setArgument("server_config", SrvConfigPtr());
    ASSERT_NO_THROW_LOG(
        LimitManager::instance().dhcp_srv_configured<DHCPv4>(*srv_configured_handle));
    arguments = srv_configured_handle->getArgumentNames();
    EXPECT_EQ(std::find(arguments.begin(), arguments.end(), "error"), arguments.end());
    EXPECT_EQ(CalloutHandle::NEXT_STEP_CONTINUE, srv_configured_handle->getStatus());

}

// Check that null parameters are handled correctly sans crashes.
TEST(LimitManagerTest, v6NullHandleParameters) {
    CalloutHandlePtr cb_updated_handle(HooksManager::createCalloutHandle());
    cb_updated_handle->setArgument("audit_entries", AuditEntryCollectionPtr());
    ASSERT_THROW_MSG(LimitManager::instance().cb_updated<DHCPv6>(*cb_updated_handle), Unexpected,
                     "null audit_entries in LimitManager::cb_updated");

    CalloutHandlePtr pkt_receive_handle(HooksManager::createCalloutHandle());
    pkt_receive_handle->setArgument("query6", Pkt6Ptr());
    ASSERT_THROW_MSG(LimitManager::instance().pkt_receive<DHCPv6>(*pkt_receive_handle), Unexpected,
                     "null packet in LimitManager::pkt_receive");

    CalloutHandlePtr subnet_select_handle(HooksManager::createCalloutHandle());
    subnet_select_handle->setArgument("query6", Pkt6Ptr());
    subnet_select_handle->setArgument("subnet6", Subnet6Ptr());
    subnet_select_handle->setArgument("subnet6collection", Subnet6Collection());
    ASSERT_NO_THROW_LOG(LimitManager::instance().subnet_select<DHCPv6>(*subnet_select_handle));

    CalloutHandlePtr lease_select_handle(HooksManager::createCalloutHandle());
    lease_select_handle->setArgument("query6", Pkt6Ptr());
    lease_select_handle->setArgument("subnet6", Subnet6Ptr());
    lease_select_handle->setArgument("fake_allocation", false);
    lease_select_handle->setArgument("lease6", Lease6Ptr());
    ASSERT_THROW_MSG(LimitManager::instance().lease_callout<DHCPv6>(*lease_select_handle),
                     Unexpected, "null lease in LimitManager::lease_callout");

    // Check that limits can be configured without a LeaseMgr.
    LeaseMgrFactory::destroy();
    EXPECT_FALSE(LeaseMgrFactory::haveInstance());
    CalloutHandlePtr srv_configured_handle(HooksManager::createCalloutHandle());
    srv_configured_handle->setArgument("network_state", NetworkStatePtr());
    srv_configured_handle->setArgument("json_config", ConstElementPtr());
    srv_configured_handle->setArgument("server_config", SrvConfigPtr());
    ASSERT_NO_THROW_LOG(
        LimitManager::instance().dhcp_srv_configured<DHCPv6>(*srv_configured_handle));
    vector<string> arguments(srv_configured_handle->getArgumentNames());
    EXPECT_EQ(std::find(arguments.begin(), arguments.end(), "error"), arguments.end());
    EXPECT_EQ(CalloutHandle::NEXT_STEP_CONTINUE, srv_configured_handle->getStatus());

    // Check that limits can be configured with a LeaseMgr.
    LeaseMgrFactory::destroy();
    LeaseMgrFactory::create("type=memfile persist=false universe=6");
    EXPECT_TRUE(LeaseMgrFactory::haveInstance());
    srv_configured_handle = HooksManager::createCalloutHandle();
    srv_configured_handle->setArgument("network_state", NetworkStatePtr());
    srv_configured_handle->setArgument("json_config", ConstElementPtr());
    srv_configured_handle->setArgument("server_config", SrvConfigPtr());
    ASSERT_NO_THROW_LOG(
        LimitManager::instance().dhcp_srv_configured<DHCPv6>(*srv_configured_handle));
    arguments = srv_configured_handle->getArgumentNames();
    EXPECT_EQ(std::find(arguments.begin(), arguments.end(), "error"), arguments.end());
    EXPECT_EQ(CalloutHandle::NEXT_STEP_CONTINUE, srv_configured_handle->getStatus());
}

}  // namespace