summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcpsrv/tests/random_allocation_state_unittest.cc
blob: d80d200e50a1fa0ae6898dd22282d66dd27afd1b (plain)
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
// 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/lease.h>
#include <dhcpsrv/pool.h>
#include <dhcpsrv/random_allocation_state.h>
#include <testutils/multi_threading_utils.h>
#include <boost/make_shared.hpp>
#include <gtest/gtest.h>
#include <set>

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

namespace {

// Test creating the random allocation state for an IPv4 pool.
TEST(PoolRandomAllocationStateTest, ipv4Pool) {
    // Create the pool and state.
    IOAddress first("192.0.2.1");
    IOAddress last("192.0.2.255");
    auto pool = boost::make_shared<Pool4>(first, last);
    auto state = PoolRandomAllocationState::create(pool);
    ASSERT_TRUE(state);

    // Make sure that the permutation has been initialized.
    auto permutation = state->getPermutation();
    ASSERT_TRUE(permutation);

    // Keep the record of the addresses returned by the permutation
    // to ensure it returns unique addresses.
    std::set<IOAddress> addresses;
    for (auto i = 0; i < 10; ++i) {
        auto done = true;
        auto next = permutation->next(done);
        // Returned address must belong to the pool.
        EXPECT_TRUE(pool->inRange(next));
        EXPECT_FALSE(done);
        addresses.insert(next);
    }
    // Make sure that unique addresses were returned.
    EXPECT_EQ(10, addresses.size());
}

// Test creating the random allocation state for an IPv6 pool.
TEST(PoolRandomAllocationStateTest, ipv6AddressPool) {
    // Create the pool and state.
    IOAddress first("2001:db8::1");
    IOAddress last("2001:db8::1:0");
    auto pool = boost::make_shared<Pool6>(Lease::TYPE_NA, first, last);
    auto state = PoolRandomAllocationState::create(pool);
    ASSERT_TRUE(state);

    // Make sure that the permutation has been initialized.
    auto permutation = state->getPermutation();
    ASSERT_TRUE(permutation);

    // Keep the record of the addresses returned by the permutation
    // to ensure it returns unique addresses.
    std::set<IOAddress> addresses;
    for (auto i = 0; i < 10; ++i) {
        auto done = true;
        auto next = permutation->next(done);
        // Returned address must belong to the pool.
        EXPECT_TRUE(pool->inRange(next));
        EXPECT_FALSE(done);
        addresses.insert(next);
    }
    // Make sure that unique addresses were returned.
    EXPECT_EQ(10, addresses.size());
}

// Test creating the random allocation state for an IPv6 prefix pool.
TEST(PoolRandomAllocationStateTest, ipv6PrefixPool) {
    // Create the pool and state.
    auto pool = boost::make_shared<Pool6>(Lease::TYPE_PD, IOAddress("2001:db8::"), 64, 96);
    auto state = PoolRandomAllocationState::create(pool);
    ASSERT_TRUE(state);

    // Make sure that the permutation has been initialized.
    auto permutation = state->getPermutation();
    ASSERT_TRUE(permutation);

    // Keep the record of the addresses returned by the permutation
    // to ensure it returns unique prefixes.
    std::set<IOAddress> prefixes;
    for (auto i = 0; i < 10; ++i) {
        auto done = true;
        auto next = permutation->next(done);
        // Returned prefix must belong to the pool.
        EXPECT_TRUE(pool->inRange(next));
        EXPECT_FALSE(done);
        prefixes.insert(next);
    }
    // Make sure that unique prefixes were returned.
    EXPECT_EQ(10, prefixes.size());
}

} // end of anonymous namespace