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

#include <config.h>

#include <asiolink/io_address.h>
#include <cc/data.h>
#include <dhcp/hwaddr.h>
#include <dhcp/duid.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/subnet_id.h>
#include <ha_config.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ha_server_type.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ha_test.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <lease_sync_filter.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/make_shared.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::ha;
using namespace isc::ha::test;

namespace {

/// @brief Test fixture class for testing @c LeaseSyncFilterTest.
class LeaseSyncFilterTest : public HATest {
public:

    /// @brief Constructor.
    ///
    /// Clears configuration in the configuration manager.
    LeaseSyncFilterTest() : HATest() {
        CfgMgr::instance().clear();
    };

    /// @brief Destructor.
    ///
    /// Clears configuration in the configuration manager.
    virtual ~LeaseSyncFilterTest() {
        CfgMgr::instance().clear();
    }
};

// This test verifies that lease sync filter correctly identifies that
// IPv4 leases belong to the configured subnets.
TEST_F(LeaseSyncFilterTest, explicitSubnets4) {<--- syntax error
    for (auto i = 0; i < 10; ++i) {
        auto subnet = Subnet4::create(IOAddress(static_cast<uint32_t>(i << 24)), 24,
                                      30, 40, 50, SubnetID(i+1));
        auto context = Element::createMap();
        context->set("ha-server-name", i < 7 ? Element::create("server2") : Element::create("server5"));
        subnet->setContext(context);
        CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnet);
    }
    CfgMgr::instance().commit();

    auto config = createValidConfiguration(HAConfig::HOT_STANDBY);
    LeaseSyncFilter filter(HAServerType::DHCPv4, config);
    filter.apply();

    for (auto i = 0; i < 10; ++i) {
        auto lease = boost::make_shared<Lease4>(IOAddress("192.0.2.1"), HWAddrPtr(), ClientIdPtr(),
                                                100, 0, SubnetID(i+1));
        if (i < 7) {
            EXPECT_TRUE(filter.shouldSync(lease));
        } else {
            EXPECT_FALSE(filter.shouldSync(lease));
        }
    }
}

// This test verifies that lease sync filter correctly identifies that
// IPv6 leases belong to the configured subnets.
TEST_F(LeaseSyncFilterTest, explicitSubnets6) {
    for (auto i = 0; i < 10; ++i) {
        std::ostringstream s;
        s << i << "::";
        auto subnet = Subnet6::create(IOAddress(s.str()), 64,
                                      30, 40, 50, 60, SubnetID(i+1));
        auto context = Element::createMap();
        context->set("ha-server-name", i < 7 ? Element::create("server2") : Element::create("server5"));
        subnet->setContext(context);
        CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet);
    }
    CfgMgr::instance().commit();

    auto config = createValidConfiguration(HAConfig::HOT_STANDBY);
    LeaseSyncFilter filter(HAServerType::DHCPv6, config);
    filter.apply();

    for (auto i = 0; i < 10; ++i) {
        std::ostringstream s;
        s << i << "::";
        auto lease = boost::make_shared<Lease4>(IOAddress(s.str()), HWAddrPtr(), ClientIdPtr(),
                                                100, 0, SubnetID(i+1));
        if (i < 7) {
            EXPECT_TRUE(filter.shouldSync(lease));
        } else {
            EXPECT_FALSE(filter.shouldSync(lease));
        }
    }
}

// This test verifies that lease sync filter correctly identifies that
// leases belong to the configured subnets when the server name is
// specified at shared network level.
TEST_F(LeaseSyncFilterTest, explicitSharedNetworks) {
    for (auto i = 0; i < 10; ++i) {
        std::ostringstream s;
        s << "net" << i;
        auto shared_network = SharedNetwork4::create(s.str());
        auto subnet = Subnet4::create(IOAddress(static_cast<uint32_t>(i << 24)), 24,
                                      30, 40, 50, SubnetID(i+1));
        auto context = Element::createMap();
        context->set("ha-server-name", i < 7 ? Element::create("server2") : Element::create("server5"));
        shared_network->setContext(context);
        shared_network->add(subnet);
        CfgMgr::instance().getStagingCfg()->getCfgSharedNetworks4()->add(shared_network);
        CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnet);
    }
    CfgMgr::instance().commit();

    auto config = createValidConfiguration(HAConfig::HOT_STANDBY);
    LeaseSyncFilter filter(HAServerType::DHCPv4, config);
    filter.apply();

    for (auto i = 0; i < 10; ++i) {
        auto lease = boost::make_shared<Lease4>(IOAddress("192.0.2.1"), HWAddrPtr(), ClientIdPtr(),
                                                100, 0, SubnetID(i+1));
        if (i < 7) {
            EXPECT_TRUE(filter.shouldSync(lease));
        } else {
            EXPECT_FALSE(filter.shouldSync(lease));
        }
    }
}

// This test verifies that lease sync filter accepts all leases when no
// subnets have been specified in the configuration manager.
TEST_F(LeaseSyncFilterTest, noSubnets4) {
    auto config = createValidConfiguration(HAConfig::HOT_STANDBY);
    LeaseSyncFilter filter(HAServerType::DHCPv4, config);
    filter.apply();

    for (auto i = 0; i < 10; ++i) {
        auto lease = boost::make_shared<Lease4>(IOAddress("192.0.2.1"), HWAddrPtr(), ClientIdPtr(),
                                                100, 0, SubnetID(i));
        EXPECT_TRUE(filter.shouldSync(lease));
    }
}

// This test verifies that lease sync filter accepts all leases when
// there are no associations specified in the subnets.
TEST_F(LeaseSyncFilterTest, noAssociationsInSubnets4) {
    for (auto i = 0; i < 10; ++i) {
        auto subnet = Subnet4::create(IOAddress(static_cast<uint32_t>(i << 24)), 24,
                                      30, 40, 50, SubnetID(i+1));
        CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnet);
    }
    CfgMgr::instance().commit();

    auto config = createValidConfiguration(HAConfig::HOT_STANDBY);
    LeaseSyncFilter filter(HAServerType::DHCPv4, config);
    filter.apply();

    for (auto i = 0; i < 10; ++i) {
        auto lease = boost::make_shared<Lease4>(IOAddress("192.0.2.1"), HWAddrPtr(), ClientIdPtr(),
                                                100, 0, SubnetID(i));
        EXPECT_TRUE(filter.shouldSync(lease));
    }
}


} // end of anonymous namespace