alloc_engine_unittest.cc 18 KB
Newer Older
1
// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14 15
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <config.h>
16

17 18
#include <asiolink/io_address.h>
#include <dhcp/duid.h>
19 20 21 22 23 24
#include <dhcpsrv/alloc_engine.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/lease_mgr.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <dhcpsrv/memfile_lease_mgr.h>

Tomek Mrugalski's avatar
Tomek Mrugalski committed
25 26
#include <dhcpsrv/tests/test_utils.h>

27
#include <boost/shared_ptr.hpp>
28
#include <boost/scoped_ptr.hpp>
29
#include <gtest/gtest.h>
30

31 32
#include <iostream>
#include <sstream>
33
#include <map>
34
#include <time.h>
35

36 37 38 39
using namespace std;
using namespace isc;
using namespace isc::asiolink;
using namespace isc::dhcp;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
40
using namespace isc::dhcp::test;
41 42

namespace {
43 44 45

class NakedAllocEngine : public AllocEngine {
public:
46 47 48
    NakedAllocEngine(AllocEngine::AllocType engine_type, unsigned int attempts)
        :AllocEngine(engine_type, attempts) {
    }
49 50 51 52
    using AllocEngine::Allocator;
    using AllocEngine::IterativeAllocator;
};

53 54 55 56
// empty class for now, but may be extended once Addr6 becomes bigger
class AllocEngineTest : public ::testing::Test {
public:
    AllocEngineTest() {
57 58 59 60 61 62 63 64 65 66 67 68
        duid_ = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0x42)));
        iaid_ = 42;

        // instantiate cfg_mgr
        CfgMgr& cfg_mgr = CfgMgr::instance();

        subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
        pool_ = Pool6Ptr(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1::10"),
                                   IOAddress("2001:db8:1::20")));
        subnet_->addPool6(pool_);
        cfg_mgr.addSubnet6(subnet_);

69
        factory_.create("type=memfile");
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    }

    void checkLease6(const Lease6Ptr& lease) {
        // that is belongs to the right subnet
        EXPECT_EQ(lease->subnet_id_, subnet_->getID());
        EXPECT_TRUE(subnet_->inRange(lease->addr_));
        EXPECT_TRUE(subnet_->inPool(lease->addr_));

        // that it have proper parameters
        EXPECT_EQ(iaid_, lease->iaid_);
        EXPECT_EQ(subnet_->getValid(), lease->valid_lft_);
        EXPECT_EQ(subnet_->getPreferred(), lease->preferred_lft_);
        EXPECT_EQ(subnet_->getT1(), lease->t1_);
        EXPECT_EQ(subnet_->getT2(), lease->t2_);
        EXPECT_EQ(0, lease->prefixlen_); // this is IA_NA, not IA_PD
        EXPECT_TRUE(false == lease->fqdn_fwd_);
        EXPECT_TRUE(false == lease->fqdn_rev_);
        EXPECT_TRUE(*lease->duid_ == *duid_);
        // @todo: check cltt
     }

    ~AllocEngineTest() {
92
        factory_.destroy();
93
    }
94 95 96 97 98

    DuidPtr duid_;
    uint32_t iaid_;
    Subnet6Ptr subnet_;
    Pool6Ptr pool_;
99
    LeaseMgrFactory factory_;
100 101 102 103 104
};

// This test checks if the Allocation Engine can be instantiated and that it
// parses parameters string properly.
TEST_F(AllocEngineTest, constructor) {
105
    boost::scoped_ptr<AllocEngine> x;
106 107

    // Hashed and random allocators are not supported yet
108 109
    ASSERT_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_HASHED, 5)), NotImplemented);
    ASSERT_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_RANDOM, 5)), NotImplemented);
110

111
    ASSERT_NO_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
112 113
}

114 115
// This test checks if the simple allocation can succeed
TEST_F(AllocEngineTest, simpleAlloc) {
116
    boost::scoped_ptr<AllocEngine> engine;
117
    ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
118
    ASSERT_TRUE(engine);
119

120 121
    Lease6Ptr lease = engine->allocateAddress6(subnet_, duid_, iaid_, IOAddress("::"),
                                               false);
122

123 124
    // check that we got a lease
    ASSERT_TRUE(lease);
125

126 127
    // do all checks on the lease
    checkLease6(lease);
128

129
    // Check that the lease is indeed in LeaseMgr
130
    Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->addr_);
131
    ASSERT_TRUE(from_mgr);
132

133
    // Now check that the lease in LeaseMgr has the same parameters
Tomek Mrugalski's avatar
Tomek Mrugalski committed
134
    detailCompareLease(lease, from_mgr);
135
}
136

137 138
// This test checks if the fake allocation (for SOLICIT) can succeed
TEST_F(AllocEngineTest, fakeAlloc) {
139
    boost::scoped_ptr<AllocEngine> engine;
140
    ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
141 142 143 144 145 146 147 148 149 150 151 152
    ASSERT_TRUE(engine);

    Lease6Ptr lease = engine->allocateAddress6(subnet_, duid_, iaid_, IOAddress("::"),
                                               true);

    // check that we got a lease
    ASSERT_TRUE(lease);

    // do all checks on the lease
    checkLease6(lease);

    // Check that the lease is NOT in LeaseMgr
153
    Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->addr_);
154 155 156
    ASSERT_FALSE(from_mgr);
}

157 158 159
// This test checks if the allocation with a hint that is valid (in range,
// in pool and free) can succeed
TEST_F(AllocEngineTest, allocWithValidHint) {
160
    boost::scoped_ptr<AllocEngine> engine;
161
    ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
162 163
    ASSERT_TRUE(engine);

164 165
    Lease6Ptr lease = engine->allocateAddress6(subnet_, duid_, iaid_,
                                               IOAddress("2001:db8:1::15"),
166 167 168 169 170
                                               false);

    // check that we got a lease
    ASSERT_TRUE(lease);

171 172 173 174 175
    // we should get what we asked for
    EXPECT_EQ(lease->addr_.toText(), "2001:db8:1::15");

    // do all checks on the lease
    checkLease6(lease);
176 177

    // Check that the lease is indeed in LeaseMgr
178
    Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->addr_);
179 180 181
    ASSERT_TRUE(from_mgr);

    // Now check that the lease in LeaseMgr has the same parameters
Tomek Mrugalski's avatar
Tomek Mrugalski committed
182
    detailCompareLease(lease, from_mgr);
183 184 185 186 187
}

// This test checks if the allocation with a hint that is in range,
// in pool, but is currently used) can succeed
TEST_F(AllocEngineTest, allocWithUsedHint) {
188
    boost::scoped_ptr<AllocEngine> engine;
189
    ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
190 191 192 193 194 195
    ASSERT_TRUE(engine);

    // let's create a lease and put it in the LeaseMgr
    DuidPtr duid2 = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0xff)));
    Lease6Ptr used(new Lease6(Lease6::LEASE_IA_NA, IOAddress("2001:db8:1::1f"),
                              duid2, 1, 2, 3, 4, 5, subnet_->getID()));
196
    ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
197 198 199 200 201 202 203 204 205

    // another client comes in and request an address that is in pool, but
    // unfortunately it is used already. The same address must not be allocated
    // twice.
    Lease6Ptr lease = engine->allocateAddress6(subnet_, duid_, iaid_,
                                               IOAddress("2001:db8:1::1f"),
                                               false);
    // check that we got a lease
    ASSERT_TRUE(lease);
206

207 208 209 210 211 212 213 214 215 216
    // allocated address must be different
    EXPECT_TRUE(used->addr_.toText() != lease->addr_.toText());

    // we should NOT get what we asked for, because it is used already
    EXPECT_TRUE(lease->addr_.toText() != "2001:db8:1::1f");

    // do all checks on the lease
    checkLease6(lease);

    // Check that the lease is indeed in LeaseMgr
217
    Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->addr_);
218 219 220
    ASSERT_TRUE(from_mgr);

    // Now check that the lease in LeaseMgr has the same parameters
Tomek Mrugalski's avatar
Tomek Mrugalski committed
221
    detailCompareLease(lease, from_mgr);
222 223 224 225 226
}

// This test checks if the allocation with a hint that is out the blue
// can succeed. The invalid hint should be ignored completely.
TEST_F(AllocEngineTest, allocBogusHint) {
227
    boost::scoped_ptr<AllocEngine> engine;
228
    ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    ASSERT_TRUE(engine);

    // Client would like to get a 3000::abc lease, which does not belong to any
    // supported lease. Allocation engine should ignore it and carry on
    // with the normal allocation
    Lease6Ptr lease = engine->allocateAddress6(subnet_, duid_, iaid_,
                                               IOAddress("3000::abc"),
                                               false);
    // check that we got a lease
    ASSERT_TRUE(lease);

    // we should NOT get what we asked for, because it is used already
    EXPECT_TRUE(lease->addr_.toText() != "3000::abc");

    // do all checks on the lease
    checkLease6(lease);

    // Check that the lease is indeed in LeaseMgr
247
    Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->addr_);
248 249 250
    ASSERT_TRUE(from_mgr);

    // Now check that the lease in LeaseMgr has the same parameters
Tomek Mrugalski's avatar
Tomek Mrugalski committed
251
    detailCompareLease(lease, from_mgr);
252 253
}

254 255 256
// This test verifies that the allocator picks addresses that belong to the
// pool
TEST_F(AllocEngineTest, IterativeAllocator) {
257 258
    boost::scoped_ptr<NakedAllocEngine::Allocator>
        alloc(new NakedAllocEngine::IterativeAllocator());
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

    for (int i = 0; i < 1000; ++i) {
        IOAddress candidate = alloc->pickAddress(subnet_, duid_, IOAddress("::"));
        EXPECT_TRUE(subnet_->inPool(candidate));
    }
}


// This test verifies that the iterative allocator really walks over all addresses
// in all pools in specified subnet. It also must not pick the same address twice
// unless it runs out of pool space and must start over.
TEST_F(AllocEngineTest, IterativeAllocator_manyPools) {
    NakedAllocEngine::IterativeAllocator* alloc = new NakedAllocEngine::IterativeAllocator();

    // let's start from 2, as there is 2001:db8:1::10 - 2001:db8:1::20 pool already.
    for (int i = 2; i < 10; ++i) {
        stringstream min, max;

        min << "2001:db8:1::" << hex << i*16 + 1;
        max << "2001:db8:1::" << hex << i*16 + 9;

        Pool6Ptr pool(new Pool6(Pool6::TYPE_IA, IOAddress(min.str()),
                                IOAddress(max.str())));
        // cout << "Adding pool: " << min.str() << "-" << max.str() << endl;
        subnet_->addPool6(pool);
    }

    int total = 17 + 8*9; // first pool (::10 - ::20) has 17 addresses in it,
                          // there are 8 extra pools with 9 addresses in each.

    // Let's keep picked addresses here and check their uniqueness.
290
    std::map<IOAddress, int> generated_addrs;
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    int cnt = 0;
    while (++cnt) {
        IOAddress candidate = alloc->pickAddress(subnet_, duid_, IOAddress("::"));
        EXPECT_TRUE(subnet_->inPool(candidate));

        // One way to easily verify that the iterative allocator really works is
        // to uncomment the following line and observe its output that it
        // covers all defined subnets.
        // cout << candidate.toText() << endl;

        if (generated_addrs.find(candidate) == generated_addrs.end()) {
            // we haven't had this
            generated_addrs[candidate] = 0;
        } else {
            // we have seen this address before. That should mean that we
            // iterated over all addresses.
            if (generated_addrs.size() == total) {
                // we have exactly the number of address in all pools
                break;
            }
            ADD_FAILURE() << "Too many or not enough unique addresses generated.";
            break;
        }

        if ( cnt>total ) {
            ADD_FAILURE() << "Too many unique addresses generated.";
            break;
        }
    }

    delete alloc;
}
323

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
// This test checks if really small pools are working
TEST_F(AllocEngineTest, smallPool) {
    boost::scoped_ptr<AllocEngine> engine;
    ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
    ASSERT_TRUE(engine);

    IOAddress addr("2001:db8:1::ad");
    CfgMgr& cfg_mgr = CfgMgr::instance();
    cfg_mgr.deleteSubnets6(); // Get rid of the default test configuration

    // Create configuration similar to other tests, but with a single address pool
    subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
    pool_ = Pool6Ptr(new Pool6(Pool6::TYPE_IA, addr, addr)); // just a single address
    subnet_->addPool6(pool_);
    cfg_mgr.addSubnet6(subnet_);

    Lease6Ptr lease = engine->allocateAddress6(subnet_, duid_, iaid_, IOAddress("::"),
                                               false);

    // Check that we got that single lease
    ASSERT_TRUE(lease);

    EXPECT_EQ("2001:db8:1::ad", lease->addr_.toText());

    // do all checks on the lease
    checkLease6(lease);

    // Check that the lease is indeed in LeaseMgr
    Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->addr_);
    ASSERT_TRUE(from_mgr);

    // Now check that the lease in LeaseMgr has the same parameters
Tomek Mrugalski's avatar
Tomek Mrugalski committed
356
    detailCompareLease(lease, from_mgr);
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
}

// This test checks if all addresses in a pool are currently used, the attempt
// to find out a new lease fails.
TEST_F(AllocEngineTest, outOfAddresses) {
    boost::scoped_ptr<AllocEngine> engine;
    ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
    ASSERT_TRUE(engine);

    IOAddress addr("2001:db8:1::ad");
    CfgMgr& cfg_mgr = CfgMgr::instance();
    cfg_mgr.deleteSubnets6(); // Get rid of the default test configuration

    // Create configuration similar to other tests, but with a single address pool
    subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
    pool_ = Pool6Ptr(new Pool6(Pool6::TYPE_IA, addr, addr)); // just a single address
    subnet_->addPool6(pool_);
    cfg_mgr.addSubnet6(subnet_);

    // Just a different duid
    DuidPtr other_duid = DuidPtr(new DUID(vector<uint8_t>(12, 0xff)));
    const uint32_t other_iaid = 3568;
    Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, other_duid, other_iaid,
                               501, 502, 503, 504, subnet_->getID(), 0));
    lease->cltt_ = time(NULL) - 10; // Allocated 10 seconds ago
    ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));

    // There is just a single address in the pool and allocated it to someone
    // else, so the allocation should fail

    EXPECT_THROW(engine->allocateAddress6(subnet_, duid_, iaid_, IOAddress("::"),false),
                 AllocFailed);
}

// This test checks if an expired lease can be reused in SOLICIT (fake allocation)
TEST_F(AllocEngineTest, solicitReuseExpiredLease) {
    boost::scoped_ptr<AllocEngine> engine;
    ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
    ASSERT_TRUE(engine);

    IOAddress addr("2001:db8:1::ad");
    CfgMgr& cfg_mgr = CfgMgr::instance();
    cfg_mgr.deleteSubnets6(); // Get rid of the default test configuration

    // Create configuration similar to other tests, but with a single address pool
    subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
    pool_ = Pool6Ptr(new Pool6(Pool6::TYPE_IA, addr, addr)); // just a single address
    subnet_->addPool6(pool_);
    cfg_mgr.addSubnet6(subnet_);

    // Just a different duid
    DuidPtr other_duid = DuidPtr(new DUID(vector<uint8_t>(12, 0xff)));
    const uint32_t other_iaid = 3568;
    Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, other_duid, other_iaid,
                               501, 502, 503, 504, subnet_->getID(), 0));
    lease->cltt_ = time(NULL) - 500; // Allocated 500 seconds ago
    lease->valid_lft_ = 495; // Lease was valid for 495 seconds
    ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));

    // CASE 1: Asking for any address
    lease = engine->allocateAddress6(subnet_, duid_, iaid_, IOAddress("::"),
                                     true);
    // Check that we got that single lease
    ASSERT_TRUE(lease);
    EXPECT_EQ(addr.toText(), lease->addr_.toText());

    // Do all checks on the lease (if subnet-id, preferred/valid times are ok etc.)
    checkLease6(lease);

    // CASE 2: Asking specifically for this address
    lease = engine->allocateAddress6(subnet_, duid_, iaid_, IOAddress(addr.toText()),
                                     true);
    // Check that we got that single lease
    ASSERT_TRUE(lease);
    EXPECT_EQ(addr.toText(), lease->addr_.toText());
}

// This test checks if an expired lease can be reused in REQUEST (actual allocation)
TEST_F(AllocEngineTest, requestReuseExpiredLease) {
    boost::scoped_ptr<AllocEngine> engine;
    ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
    ASSERT_TRUE(engine);

    IOAddress addr("2001:db8:1::ad");
    CfgMgr& cfg_mgr = CfgMgr::instance();
    cfg_mgr.deleteSubnets6(); // Get rid of the default test configuration

    // Create configuration similar to other tests, but with a single address pool
    subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
    pool_ = Pool6Ptr(new Pool6(Pool6::TYPE_IA, addr, addr)); // just a single address
    subnet_->addPool6(pool_);
    cfg_mgr.addSubnet6(subnet_);

    // Let's create an expired lease
    DuidPtr other_duid = DuidPtr(new DUID(vector<uint8_t>(12, 0xff)));
    const uint32_t other_iaid = 3568;
    const SubnetID other_subnetid = 999;
    Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, other_duid, other_iaid,
                               501, 502, 503, 504, other_subnetid, 0));
    lease->cltt_ = time(NULL) - 500; // Allocated 500 seconds ago
    lease->valid_lft_ = 495; // Lease was valid for 495 seconds
    ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));

    // A client comes along, asking specifically for this address
    lease = engine->allocateAddress6(subnet_, duid_, iaid_,
                                     IOAddress(addr.toText()), false);

    // Check that he got that single lease
    ASSERT_TRUE(lease);
    EXPECT_EQ(addr.toText(), lease->addr_.toText());

    // Check that the lease is indeed updated in LeaseMgr
    Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(addr);
    ASSERT_TRUE(from_mgr);

    // Now check that the lease in LeaseMgr has the same parameters
Tomek Mrugalski's avatar
Tomek Mrugalski committed
473
    detailCompareLease(lease, from_mgr);
474 475
}

476
}; // end of anonymous namespace