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
185
186
187
188
// Copyright (C) 2022-2023 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 <dhcpsrv/iterative_allocation_state.h>
#include <testutils/multi_threading_utils.h>
#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.

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

namespace {

// Checks that last allocated IPv4 address is remembered in the iterative
// allocation state.
TEST(IterativeAllocationStateTest, subnetLastAllocated4) {
    IOAddress addr("192.0.2.17");

    IOAddress last("192.0.2.255");

    auto subnet(boost::make_shared<Subnet4>(IOAddress("192.0.2.0"),
                                            24, 1, 2, 3, SubnetID(1)));
    auto state = SubnetIterativeAllocationState::create(subnet);

    // Check initial conditions (all should be set to the last address in range)
    EXPECT_EQ(last.toText(), state->getLastAllocated().toText());

    // Now set last allocated.
    EXPECT_NO_THROW(state->setLastAllocated(addr));
    EXPECT_EQ(addr.toText(), state->getLastAllocated().toText());
}

// Checks that the last allocated IPv4 address is remembered in the
// iterative allocation state when multi threading is turned on.
TEST(IterativeAllocationStateTest, subnetLastAllocated4MultiThreading) {<--- syntax error
    MultiThreadingTest mt(true);
    IOAddress addr("192.0.2.17");

    IOAddress last("192.0.2.255");

    auto subnet(boost::make_shared<Subnet4>(IOAddress("192.0.2.0"),
                                            24, 1, 2, 3, SubnetID(1)));
    auto state = SubnetIterativeAllocationState::create(subnet);

    // Check initial conditions (all should be set to the last address in range)
    EXPECT_EQ(last.toText(), state->getLastAllocated().toText());

    // Now set last allocated.
    EXPECT_NO_THROW(state->setLastAllocated(addr));
    EXPECT_EQ(addr.toText(), state->getLastAllocated().toText());
}

// Checks if last allocated address/prefix is stored/retrieved properly.
TEST(IterativeAllocationStateTest, subnetLastAllocated6) {
    IOAddress na("2001:db8:1::1");
    IOAddress ta("2001:db8:1::abcd");
    IOAddress pd("2001:db8:1::1234:5678");

    IOAddress last("2001:db8:1::ffff:ffff:ffff:ffff");

    auto subnet = Subnet6::create(IOAddress("2001:db8:1::"),
                                  64, 1, 2, 3, 4, SubnetID(1));
    auto state_na = boost::dynamic_pointer_cast<SubnetIterativeAllocationState>
        (subnet->getAllocationState(Lease::TYPE_NA));
    auto state_ta = boost::dynamic_pointer_cast<SubnetIterativeAllocationState>
        (subnet->getAllocationState(Lease::TYPE_TA));
    auto state_pd = boost::dynamic_pointer_cast<SubnetIterativeAllocationState>
        (subnet->getAllocationState(Lease::TYPE_PD));

    // Check initial conditions (all should be set to the last address in range)
    EXPECT_EQ(last.toText(), state_na->getLastAllocated().toText());
    EXPECT_EQ(last.toText(), state_ta->getLastAllocated().toText());
    EXPECT_EQ(last.toText(), state_pd->getLastAllocated().toText());

    // Now set last allocated for IA
    EXPECT_NO_THROW(state_na->setLastAllocated(na));
    EXPECT_EQ(na.toText(), state_na->getLastAllocated().toText());

    // TA and PD should be unchanged
    EXPECT_EQ(last.toText(), state_ta->getLastAllocated().toText());
    EXPECT_EQ(last.toText(), state_pd->getLastAllocated().toText());

    // Now set TA and PD
    EXPECT_NO_THROW(state_ta->setLastAllocated(ta));
    EXPECT_NO_THROW(state_pd->setLastAllocated(pd));

    EXPECT_EQ(na.toText(), state_na->getLastAllocated().toText());
    EXPECT_EQ(ta.toText(), state_ta->getLastAllocated().toText());
    EXPECT_EQ(pd.toText(), state_pd->getLastAllocated().toText());
}

// Checks if last allocated address/prefix is stored/retrieved properly when
// multi threading is turned on.
TEST(IterativeAllocationStateTest, subnetLastAllocated6MultiThreading) {
    MultiThreadingTest mt(true);
    IOAddress na("2001:db8:1::1");
    IOAddress ta("2001:db8:1::abcd");
    IOAddress pd("2001:db8:1::1234:5678");

    IOAddress last("2001:db8:1::ffff:ffff:ffff:ffff");

    auto subnet = Subnet6::create(IOAddress("2001:db8:1::"),
                                  64, 1, 2, 3, 4, SubnetID(1));
    auto state_na = boost::dynamic_pointer_cast<SubnetIterativeAllocationState>
        (subnet->getAllocationState(Lease::TYPE_NA));
    auto state_ta = boost::dynamic_pointer_cast<SubnetIterativeAllocationState>
        (subnet->getAllocationState(Lease::TYPE_TA));
    auto state_pd = boost::dynamic_pointer_cast<SubnetIterativeAllocationState>
        (subnet->getAllocationState(Lease::TYPE_PD));

    // Check initial conditions (all should be set to the last address in range)
    EXPECT_EQ(last.toText(), state_na->getLastAllocated().toText());
    EXPECT_EQ(last.toText(), state_ta->getLastAllocated().toText());
    EXPECT_EQ(last.toText(), state_pd->getLastAllocated().toText());

    // Now set last allocated for IA
    EXPECT_NO_THROW(state_na->setLastAllocated(na));
    EXPECT_EQ(na.toText(), state_na->getLastAllocated().toText());

    // TA and PD should be unchanged
    EXPECT_EQ(last.toText(), state_ta->getLastAllocated().toText());
    EXPECT_EQ(last.toText(), state_pd->getLastAllocated().toText());

    // Now set TA and PD
    EXPECT_NO_THROW(state_ta->setLastAllocated(ta));
    EXPECT_NO_THROW(state_pd->setLastAllocated(pd));

    EXPECT_EQ(na.toText(), state_na->getLastAllocated().toText());
    EXPECT_EQ(ta.toText(), state_ta->getLastAllocated().toText());
    EXPECT_EQ(pd.toText(), state_pd->getLastAllocated().toText());
}

// Checks that last allocated IPv4 address is stored in the pool-specific
// allocation state.
TEST(IterativeAllocationStateTest, poolLastAllocated4) {
    // Create a pool.
    IOAddress first("192.0.2.0");
    auto pool = boost::make_shared<Pool4>(first, IOAddress("192.0.2.255"));
    auto state = PoolIterativeAllocationState::create(pool);

    // Initial values are first invalid.
    EXPECT_EQ(first.toText(), state->getLastAllocated().toText());
    EXPECT_FALSE(state->isLastAllocatedValid());

    // Now set last allocated
    IOAddress addr("192.0.2.100");
    EXPECT_NO_THROW(state->setLastAllocated(addr));
    EXPECT_EQ(addr.toText(), state->getLastAllocated().toText());
    EXPECT_TRUE(state->isLastAllocatedValid());

    // Reset makes it invalid and does not touch address
    state->resetLastAllocated();
    EXPECT_EQ(addr.toText(), state->getLastAllocated().toText());
    EXPECT_FALSE(state->isLastAllocatedValid());
}

// Checks that last allocated IPv6 lease is stored in the pool-specific
// allocation state.
TEST(IterativeAllocationStateTest, poolLastAllocated6) {
    // Create a pool.
    IOAddress first("2001:db8::1");
    auto pool = boost::make_shared<Pool6>(Lease::TYPE_NA, first, IOAddress("2001:db8::200"));
    auto state = PoolIterativeAllocationState::create(pool);

    // Initial values are first invalid.
    EXPECT_EQ(first.toText(), state->getLastAllocated().toText());
    EXPECT_FALSE(state->isLastAllocatedValid());

    // Now set last allocated
    IOAddress addr("2001:db8::100");
    EXPECT_NO_THROW(state->setLastAllocated(addr));
    EXPECT_EQ(addr.toText(), state->getLastAllocated().toText());
    EXPECT_TRUE(state->isLastAllocatedValid());

    // Reset makes it invalid and does not touch address
    state->resetLastAllocated();
    EXPECT_EQ(addr.toText(), state->getLastAllocated().toText());
    EXPECT_FALSE(state->isLastAllocatedValid());
}

}  // anonymous namespace