lease_mgr_unittest.cc 5.6 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 16 17 18 19 20
//
// 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>
#include <iostream>
#include <sstream>
#include <gtest/gtest.h>
#include <asiolink/io_address.h>
#include <dhcp/lease_mgr.h>
21
#include <dhcp/duid.h>
22
#include "memfile_lease_mgr.h"
23 24 25 26 27

using namespace std;
using namespace isc;
using namespace isc::asiolink;
using namespace isc::dhcp;
28
using namespace isc::dhcp::test; // Memfile_LeaseMgr
29 30 31 32 33 34 35 36 37

namespace {
// empty class for now, but may be extended once Addr6 becomes bigger
class LeaseMgrTest : public ::testing::Test {
public:
    LeaseMgrTest() {
    }
};

38 39
// This test checks if the LeaseMgr can be instantiated and that it
// parses parameters string properly.
40 41
TEST_F(LeaseMgrTest, constructor) {

Tomek Mrugalski's avatar
Tomek Mrugalski committed
42 43 44 45 46 47 48 49 50
    // should not throw any exceptions here
    Memfile_LeaseMgr * leaseMgr = new Memfile_LeaseMgr("");
    delete leaseMgr;

    leaseMgr = new Memfile_LeaseMgr("param1=value1 param2=value2");

    EXPECT_EQ("value1", leaseMgr->getParameter("param1"));
    EXPECT_EQ("value2", leaseMgr->getParameter("param2"));
    EXPECT_THROW(leaseMgr->getParameter("param3"), BadValue);
51 52 53 54

    delete leaseMgr;
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
55 56
// There's no point in calling any other methods in LeaseMgr, as they
// are purely virtual, so we would only call Memfile_LeaseMgr methods.
57 58 59 60
// Those methods are just stubs that does not return anything.
// It seems likely that we will need to extend the memfile code for
// allocation engine tests, so we may implement tests that call
// Memfile_LeaseMgr methods then.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
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
TEST_F(LeaseMgrTest, addGetDelete) {
    Memfile_LeaseMgr * leaseMgr = new Memfile_LeaseMgr("");

    IOAddress addr("2001:db8:1::456");

    uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
    DuidPtr duid(new DUID(llt, sizeof(llt)));

    uint32_t iaid = 7; // just a number

    SubnetID subnet_id = 8; // just another number

    Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr,
                               duid, iaid, 100, 200, 50, 80,
                               subnet_id));

    EXPECT_TRUE(leaseMgr->addLease(lease));

    // should not be allowed to add a second lease with the same address
    EXPECT_FALSE(leaseMgr->addLease(lease));

    Lease6Ptr x = leaseMgr->getLease6(IOAddress("2001:db8:1::234"));
    EXPECT_EQ(Lease6Ptr(), x);

    x = leaseMgr->getLease6(IOAddress("2001:db8:1::456"));
    ASSERT_TRUE(x);

    EXPECT_TRUE(x->addr_ == addr);
    EXPECT_TRUE(*x->duid_ == *duid);
    EXPECT_TRUE(x->iaid_ == iaid);
    EXPECT_TRUE(x->subnet_id_ == subnet_id);

    // These are not important from lease management perspective, but
    // let's check them anyway.
    EXPECT_TRUE(x->type_ == Lease6::LEASE_IA_NA);
    EXPECT_TRUE(x->preferred_lft_ == 100);
    EXPECT_TRUE(x->valid_lft_ == 200);
    EXPECT_TRUE(x->t1_ == 50);
    EXPECT_TRUE(x->t2_ == 80);

    // should return false - there's no such address
    EXPECT_FALSE(leaseMgr->deleteLease6(IOAddress("2001:db8:1::789")));

    // this one should succeed
    EXPECT_TRUE(leaseMgr->deleteLease6(IOAddress("2001:db8:1::456")));

    // after the lease is deleted, it should really be gone
    x = leaseMgr->getLease6(IOAddress("2001:db8:1::456"));
    EXPECT_EQ(Lease6Ptr(), x);

    delete leaseMgr;
}

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
// This test checks there that leaseMgr is really a singleton and that
// no more than one can be created.
TEST_F(LeaseMgrTest, singleton) {
    Memfile_LeaseMgr* leaseMgr1 = NULL;
    Memfile_LeaseMgr* leaseMgr2 = NULL;

    EXPECT_THROW(LeaseMgr::instance(), InvalidOperation);

    EXPECT_NO_THROW( leaseMgr1 = new Memfile_LeaseMgr("") );

    EXPECT_NO_THROW(LeaseMgr::instance());

    // There can be only one instance of any LeaseMgr derived
    // objects instantiated at any time.
    ASSERT_THROW(leaseMgr2 = new Memfile_LeaseMgr(""), InvalidOperation);

    delete leaseMgr1;

    ASSERT_NO_THROW(leaseMgr2 = new Memfile_LeaseMgr("") );

    delete leaseMgr2;
}

// This test checks if the Lease6 structure can be instantiated correctly
TEST(Lease6, ctor) {

    IOAddress addr("2001:db8:1::456");

    uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
    DuidPtr duid(new DUID(llt, sizeof(llt)));

    uint32_t iaid = 7; // just a number

    SubnetID subnet_id = 8; // just another number

    Lease6Ptr x(new Lease6(Lease6::LEASE_IA_NA, addr,
                           duid, iaid, 100, 200, 50, 80,
                           subnet_id));

    EXPECT_TRUE(x->addr_ == addr);
    EXPECT_TRUE(*x->duid_ == *duid);
    EXPECT_TRUE(x->iaid_ == iaid);
    EXPECT_TRUE(x->subnet_id_ == subnet_id);
    EXPECT_TRUE(x->type_ == Lease6::LEASE_IA_NA);
    EXPECT_TRUE(x->preferred_lft_ == 100);
    EXPECT_TRUE(x->valid_lft_ == 200);
    EXPECT_TRUE(x->t1_ == 50);
    EXPECT_TRUE(x->t2_ == 80);

    // Lease6 must be instantiated with a DUID, not with NULL pointer
    EXPECT_THROW(new Lease6(Lease6::LEASE_IA_NA, addr,
                            DuidPtr(), iaid, 100, 200, 50, 80,
                            subnet_id), InvalidOperation);
}

170
}; // end of anonymous namespace