lease_mgr_unittest.cc 9.88 KB
Newer Older
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
// Copyright (C) 2011-2012 Internet Systems Consortium, Inc. ("ISC")
//
// 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>

using namespace std;
using namespace isc;
using namespace isc::asiolink;
using namespace isc::dhcp;

28 29 30
// This is a concrete implementation of a Lease database.  It does not do
// anything useful and is used for abstract LeaseMgr class testing.
class ConcreteLeaseMgr : public LeaseMgr {
31 32 33 34 35 36 37 38
public:

    /// @brief The sole lease manager constructor
    ///
    /// dbconfig is a generic way of passing parameters. Parameters
    /// are passed in the "name=value" format, separated by spaces.
    /// Values may be enclosed in double quotes, if needed.
    ///
39 40
    /// @param parameters A data structure relating keywords and values
    ///        concerned with the database.
41 42 43
    ConcreteLeaseMgr(const LeaseMgr::ParameterMap& parameters)
        : LeaseMgr(parameters)
    {}
44

45 46 47
    /// @brief Destructor
    virtual ~ConcreteLeaseMgr()
    {}
48 49 50 51

    /// @brief Adds an IPv4 lease.
    ///
    /// @param lease lease to be added
52 53 54
    virtual bool addLease(const Lease4Ptr&) {
        return (false);
    }
55 56 57 58

    /// @brief Adds an IPv6 lease.
    ///
    /// @param lease lease to be added
59 60 61
    virtual bool addLease(const Lease6Ptr&) {
        return (false);
    }
62 63 64 65 66

    /// @brief Returns existing IPv4 lease for specified IPv4 address.
    ///
    /// @param addr address of the searched lease
    ///
67 68 69 70
    /// @return smart pointer to the lease (or NULL if a lease is not found)
    virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress&) const {
        return (Lease4Ptr());
    }
71

72 73 74 75 76
    /// @brief Returns existing IPv4 lease for specific address and subnet
    /// @param addr address of the searched lease
    /// @param subnet_id ID of the subnet the lease must belong to
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
77 78 79 80
    virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress&,
                                SubnetID) const {
        return (Lease4Ptr());
    }
81 82 83 84 85 86 87

    /// @brief Returns existing IPv4 leases for specified hardware address.
    ///
    /// Although in the usual case there will be only one lease, for mobile
    /// clients or clients with multiple static/fixed/reserved leases there
    /// can be more than one. Thus return type is a container, not a single
    /// pointer.
88 89 90
    ///
    /// @param hwaddr hardware address of the client
    ///
91
    /// @return lease collection
92 93 94
    virtual Lease4Collection getLease4(const HWAddr&) const {
        return (Lease4Collection());
    }
95 96 97 98 99 100 101 102 103 104 105

    /// @brief Returns existing IPv4 leases for specified hardware address
    ///        and a subnet
    ///
    /// There can be at most one lease for a given HW address in a single
    /// pool, so this method with either return a single lease or NULL.
    ///
    /// @param hwaddr hardware address of the client
    /// @param subnet_id identifier of the subnet that lease must belong to
    ///
    /// @return a pointer to the lease (or NULL if a lease is not found)
106 107 108
    virtual Lease4Ptr getLease4(const HWAddr&, SubnetID) const {
        return (Lease4Ptr());
    }
109 110 111 112

    /// @brief Returns existing IPv4 lease for specified client-id
    ///
    /// @param clientid client identifier
113 114 115 116 117
    ///
    /// @return lease collection
    virtual Lease4Collection getLease4(const ClientId&) const {
        return (Lease4Collection());
    }
118 119 120

    /// @brief Returns existing IPv4 lease for specified client-id
    ///
121 122 123
    /// There can be at most one lease for a given HW address in a single
    /// pool, so this method with either return a single lease or NULL.
    ///
124
    /// @param clientid client identifier
125 126 127
    /// @param subnet_id identifier of the subnet that lease must belong to
    ///
    /// @return a pointer to the lease (or NULL if a lease is not found)
128 129 130
    virtual Lease4Ptr getLease4(const ClientId&, SubnetID) const {
        return (Lease4Ptr());
    }
131 132 133 134 135 136

    /// @brief Returns existing IPv6 lease for a given IPv6 address.
    ///
    /// @param addr address of the searched lease
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
137
    virtual Lease6Ptr getLease6(const isc::asiolink::IOAddress&) const {
138 139
        return (Lease6Ptr());
    }
140 141 142 143 144 145

    /// @brief Returns existing IPv6 lease for a given DUID+IA combination
    ///
    /// @param duid client DUID
    /// @param iaid IA identifier
    ///
146
    /// @return collection of IPv6 leases
147
    virtual Lease6Collection getLease6(const DUID&, uint32_t) const {
148 149
        return (Lease6Collection());
    }
150 151 152 153 154 155 156

    /// @brief Returns existing IPv6 lease for a given DUID+IA combination
    ///
    /// @param duid client DUID
    /// @param iaid IA identifier
    /// @param subnet_id identifier of the subnet the lease must belong to
    ///
157
    /// @return smart pointer to the lease (or NULL if a lease is not found)
158
    virtual Lease6Ptr getLease6(const DUID&, uint32_t, SubnetID) const {
159 160
        return (Lease6Ptr());
    }
161 162 163 164 165 166

    /// @brief Updates IPv4 lease.
    ///
    /// @param lease4 The lease to be updated.
    ///
    /// If no such lease is present, an exception will be thrown.
167
    virtual void updateLease4(const Lease4Ptr&) {}
168 169 170 171 172 173

    /// @brief Updates IPv4 lease.
    ///
    /// @param lease4 The lease to be updated.
    ///
    /// If no such lease is present, an exception will be thrown.
174
    virtual void updateLease6(const Lease6Ptr&) {}
175 176 177 178 179 180

    /// @brief Deletes a lease.
    ///
    /// @param addr IPv4 address of the lease to be deleted.
    ///
    /// @return true if deletion was successful, false if no such lease exists
181
    virtual bool deleteLease4(const isc::asiolink::IOAddress&) {
182 183
        return (false);
    }
184 185 186 187 188 189

    /// @brief Deletes a lease.
    ///
    /// @param addr IPv4 address of the lease to be deleted.
    ///
    /// @return true if deletion was successful, false if no such lease exists
190
    virtual bool deleteLease6(const isc::asiolink::IOAddress&) {
191 192
        return (false);
    }
193

194 195 196 197 198 199 200 201 202
    /// @brief Returns backend type.
    ///
    /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
    ///
    /// @return Type of the backend.
    virtual std::string getType() const {
        return (std::string("concrete"));
    }

203 204
    /// @brief Returns backend name.
    ///
205 206 207 208 209
    /// If the backend is a database, this is the name of the database or the
    /// file.  Otherwise it is just the same as the type.
    ///
    /// @return Name of the backend.
    virtual std::string getName() const {
210 211
        return (std::string("concrete"));
    }
212 213 214 215

    /// @brief Returns description of the backend.
    ///
    /// This description may be multiline text that describes the backend.
216 217 218
    ///
    /// @return Description of the backend.
    virtual std::string getDescription() const {
219 220
        return (std::string("This is a dummy concrete backend implementation."));
    }
221 222

    /// @brief Returns backend version.
223
    virtual std::pair<uint32_t, uint32_t> getVersion() const {
224 225
        return (make_pair(uint32_t(0), uint32_t(0)));
    }
Tomek Mrugalski's avatar
Tomek Mrugalski committed
226

227
    /// @brief Commit transactions
228
    virtual void commit() {
229 230 231
    }

    /// @brief Rollback transactions
232
    virtual void rollback() {
233
    }
234 235 236 237 238 239 240 241 242 243
};

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

244 245
// This test checks if the LeaseMgr can be instantiated and that it
// parses parameters string properly.
246
TEST_F(LeaseMgrTest, getParameter) {
247

248 249 250
    LeaseMgr::ParameterMap pmap;
    pmap[std::string("param1")] = std::string("value1");
    pmap[std::string("param2")] = std::string("value2");
251
    ConcreteLeaseMgr leasemgr(pmap);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
252

253 254 255
    EXPECT_EQ("value1", leasemgr.getParameter("param1"));
    EXPECT_EQ("value2", leasemgr.getParameter("param2"));
    EXPECT_THROW(leasemgr.getParameter("param3"), BadValue);
256 257
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
258
// There's no point in calling any other methods in LeaseMgr, as they
259 260
// are purely virtual, so we would only call ConcreteLeaseMgr methods.
// Those methods are just stubs that do not return anything.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
261

262
// Lease6 is also defined in lease_mgr.h, so is tested in this file as well.
263
// This test checks if the Lease6 structure can be instantiated correctly
264
TEST(Lease6, Lease6Constructor) {
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 290 291 292 293

    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);
}
294
}; // end of anonymous namespace