lease_mgr_unittest.cc 27.7 KB
Newer Older
1
// Copyright (C) 2012-2013 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 19

#include <asiolink/io_address.h>
#include <dhcpsrv/lease_mgr.h>

20 21
#include <gtest/gtest.h>

22 23 24
#include <iostream>
#include <sstream>

25 26
#include <time.h>

27 28 29 30 31
using namespace std;
using namespace isc;
using namespace isc::asiolink;
using namespace isc::dhcp;

32 33 34
// 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 {
35 36 37 38 39 40 41 42
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.
    ///
43 44
    /// @param parameters A data structure relating keywords and values
    ///        concerned with the database.
45 46 47
    ConcreteLeaseMgr(const LeaseMgr::ParameterMap& parameters)
        : LeaseMgr(parameters)
    {}
48

49 50 51
    /// @brief Destructor
    virtual ~ConcreteLeaseMgr()
    {}
52 53 54 55

    /// @brief Adds an IPv4 lease.
    ///
    /// @param lease lease to be added
56 57 58
    virtual bool addLease(const Lease4Ptr&) {
        return (false);
    }
59 60 61 62

    /// @brief Adds an IPv6 lease.
    ///
    /// @param lease lease to be added
63 64 65
    virtual bool addLease(const Lease6Ptr&) {
        return (false);
    }
66 67 68 69 70

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

76 77 78 79 80 81
    /// @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.
82 83 84
    ///
    /// @param hwaddr hardware address of the client
    ///
85
    /// @return lease collection
86 87 88
    virtual Lease4Collection getLease4(const HWAddr&) const {
        return (Lease4Collection());
    }
89 90 91 92 93 94 95 96 97 98 99

    /// @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)
100 101 102
    virtual Lease4Ptr getLease4(const HWAddr&, SubnetID) const {
        return (Lease4Ptr());
    }
103 104 105 106

    /// @brief Returns existing IPv4 lease for specified client-id
    ///
    /// @param clientid client identifier
107 108 109 110 111
    ///
    /// @return lease collection
    virtual Lease4Collection getLease4(const ClientId&) const {
        return (Lease4Collection());
    }
112 113 114

    /// @brief Returns existing IPv4 lease for specified client-id
    ///
115 116 117
    /// 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.
    ///
118
    /// @param clientid client identifier
119 120 121
    /// @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)
122 123 124
    virtual Lease4Ptr getLease4(const ClientId&, SubnetID) const {
        return (Lease4Ptr());
    }
125 126 127 128 129 130

    /// @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)
131 132
    virtual Lease6Ptr getLease6(Lease6::LeaseType /* not used yet */,
                                const isc::asiolink::IOAddress&) const {
133 134
        return (Lease6Ptr());
    }
135 136 137 138 139 140

    /// @brief Returns existing IPv6 lease for a given DUID+IA combination
    ///
    /// @param duid client DUID
    /// @param iaid IA identifier
    ///
141
    /// @return collection of IPv6 leases
142 143
    virtual Lease6Collection getLease6(Lease6::LeaseType /* not used yet */,
                                       const DUID&, uint32_t) const {
144 145
        return (Lease6Collection());
    }
146 147 148 149 150 151 152

    /// @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
    ///
153
    /// @return smart pointer to the lease (or NULL if a lease is not found)
154 155
    virtual Lease6Ptr getLease6(Lease6::LeaseType /* not used yet */,
                                const DUID&, uint32_t, SubnetID) const {
156 157
        return (Lease6Ptr());
    }
158 159 160 161 162 163

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

    /// @brief Updates IPv4 lease.
    ///
    /// @param lease4 The lease to be updated.
    ///
    /// If no such lease is present, an exception will be thrown.
171
    virtual void updateLease6(const Lease6Ptr&) {}
172 173 174

    /// @brief Deletes a lease.
    ///
175 176
    /// @param addr Address of the lease to be deleted. (This can be either
    ///        a V4 address or a V6 address.)
177 178
    ///
    /// @return true if deletion was successful, false if no such lease exists
179
    virtual bool deleteLease(const isc::asiolink::IOAddress&) {
180 181
        return (false);
    }
182

183 184 185 186 187 188 189 190 191
    /// @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"));
    }

192 193
    /// @brief Returns backend name.
    ///
194 195 196 197 198
    /// 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 {
199 200
        return (std::string("concrete"));
    }
201 202 203 204

    /// @brief Returns description of the backend.
    ///
    /// This description may be multiline text that describes the backend.
205 206 207
    ///
    /// @return Description of the backend.
    virtual std::string getDescription() const {
208 209
        return (std::string("This is a dummy concrete backend implementation."));
    }
210 211

    /// @brief Returns backend version.
212
    virtual std::pair<uint32_t, uint32_t> getVersion() const {
213 214
        return (make_pair(uint32_t(0), uint32_t(0)));
    }
Tomek Mrugalski's avatar
Tomek Mrugalski committed
215

216
    /// @brief Commit transactions
217
    virtual void commit() {
218 219 220
    }

    /// @brief Rollback transactions
221
    virtual void rollback() {
222
    }
223 224 225 226
};

namespace {

227 228 229 230 231
/// @brief getParameter test
///
/// This test checks if the LeaseMgr can be instantiated and that it
/// parses parameters string properly.
TEST(LeaseMgr, getParameter) {
232

233 234 235
    LeaseMgr::ParameterMap pmap;
    pmap[std::string("param1")] = std::string("value1");
    pmap[std::string("param2")] = std::string("value2");
236
    ConcreteLeaseMgr leasemgr(pmap);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
237

238 239 240
    EXPECT_EQ("value1", leasemgr.getParameter("param1"));
    EXPECT_EQ("value2", leasemgr.getParameter("param2"));
    EXPECT_THROW(leasemgr.getParameter("param3"), BadValue);
241 242
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
243
// There's no point in calling any other methods in LeaseMgr, as they
244 245
// 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
246

247 248 249
/// @brief Lease4 Constructor Test
///
/// Lease4 is also defined in lease_mgr.h, so is tested in this file as well.
250
// This test checks if the Lease4 structure can be instantiated correctly
251
TEST(Lease4, constructor) {
252 253 254 255 256 257 258 259 260 261 262 263

    // Random values for the tests
    const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
    std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));

    const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
    std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
    ClientId clientid(clientid_vec);

    // ...and a time
    const time_t current_time = time(NULL);

264
    // Other random constants.
265
    const uint32_t SUBNET_ID = 42;
266 267 268 269 270 271 272 273 274 275 276 277
    const uint32_t VALID_LIFETIME = 500;

    // We want to check that various addresses work, so let's iterate over
    // these.
    const uint32_t ADDRESS[] = {
        0x00000000, 0x01020304, 0x7fffffff, 0x80000000, 0x80000001, 0xffffffff
    };

    for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {

        // Create the lease
        Lease4 lease(ADDRESS[i], HWADDR, sizeof(HWADDR),
278 279 280
                     CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, 0, 0,
                     current_time, SUBNET_ID, true, true,
                     "hostname.example.com.");
281 282 283 284 285 286 287 288 289 290 291

        EXPECT_EQ(ADDRESS[i], static_cast<uint32_t>(lease.addr_));
        EXPECT_EQ(0, lease.ext_);
        EXPECT_TRUE(hwaddr == lease.hwaddr_);
        EXPECT_TRUE(clientid == *lease.client_id_);
        EXPECT_EQ(0, lease.t1_);
        EXPECT_EQ(0, lease.t2_);
        EXPECT_EQ(VALID_LIFETIME, lease.valid_lft_);
        EXPECT_EQ(current_time, lease.cltt_);
        EXPECT_EQ(SUBNET_ID, lease.subnet_id_);
        EXPECT_FALSE(lease.fixed_);
292 293 294
        EXPECT_EQ("hostname.example.com.", lease.hostname_);
        EXPECT_TRUE(lease.fqdn_fwd_);
        EXPECT_TRUE(lease.fqdn_rev_);
295 296 297 298
        EXPECT_TRUE(lease.comments_.empty());
    }
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 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 356 357 358 359 360 361 362 363 364 365
// This test verfies that copy constructor copies Lease4 fields correctly.
TEST(Lease4, copyConstructor) {

    // Random values for the tests
    const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
    std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));

    const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
    std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
    ClientId clientid(clientid_vec);

    // ...and a time
    const time_t current_time = time(NULL);

    // Other random constants.
    const uint32_t SUBNET_ID = 42;
    const uint32_t VALID_LIFETIME = 500;

    // Create the lease
    Lease4 lease(0xffffffff, HWADDR, sizeof(HWADDR),
                 CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, 0, 0, current_time,
                 SUBNET_ID);

    // Use copy constructor to copy the lease.
    Lease4 copied_lease(lease);

    // Both leases should be now equal. When doing this check we assume that
    // the equality operator works correctly.
    EXPECT_TRUE(lease == copied_lease);
    // Client IDs are equal, but they should be in two distinct pointers.
    EXPECT_FALSE(lease.client_id_ == copied_lease.client_id_);
}

// This test verfies that the assignment operator copies all Lease4 fields
// correctly.
TEST(Lease4, operatorAssign) {

    // Random values for the tests
    const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
    std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));

    const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
    std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
    ClientId clientid(clientid_vec);

    // ...and a time
    const time_t current_time = time(NULL);

    // Other random constants.
    const uint32_t SUBNET_ID = 42;
    const uint32_t VALID_LIFETIME = 500;

    // Create the lease
    Lease4 lease(0xffffffff, HWADDR, sizeof(HWADDR),
                 CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, 0, 0, current_time,
                 SUBNET_ID);

    // Use assignment operator to assign the lease.
    Lease4 copied_lease = lease;

    // Both leases should be now equal. When doing this check we assume that
    // the equality operator works correctly.
    EXPECT_TRUE(lease == copied_lease);
    // Client IDs are equal, but they should be in two distinct pointers.
    EXPECT_FALSE(lease.client_id_ == copied_lease.client_id_);
}

366 367 368 369 370
/// @brief Lease4 Equality Test
///
/// Checks that the operator==() correctly compares two leases for equality.
/// As operator!=() is also defined for this class, every check on operator==()
/// is followed by the reverse check on operator!=().
371
TEST(Lease4, operatorEquals) {
372

373 374 375 376 377 378 379 380 381 382 383 384 385
    // Random values for the tests
    const uint32_t ADDRESS = 0x01020304;
    const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
    std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));
    const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
    std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
    ClientId clientid(clientid_vec);
    const time_t current_time = time(NULL);
    const uint32_t SUBNET_ID = 42;
    const uint32_t VALID_LIFETIME = 500;

    // Check when the leases are equal.
    Lease4 lease1(ADDRESS, HWADDR, sizeof(HWADDR),
386 387
                  CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, current_time, 0,
                  0, SUBNET_ID);
388
    Lease4 lease2(ADDRESS, HWADDR, sizeof(HWADDR),
389
                  CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, current_time, 0, 0,
390 391 392 393 394 395 396 397 398 399 400
                  SUBNET_ID);
    EXPECT_TRUE(lease1 == lease2);
    EXPECT_FALSE(lease1 != lease2);

    // Now vary individual fields in a lease and check that the leases compare
    // not equal in every case.
    lease1.addr_ = IOAddress(ADDRESS + 1);
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.addr_ = lease2.addr_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
401
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
402 403 404 405 406 407

    ++lease1.ext_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.ext_ = lease2.ext_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
408
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
409 410 411 412 413 414

    ++lease1.hwaddr_[0];
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.hwaddr_ = lease2.hwaddr_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
415
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
416 417 418 419 420 421 422 423

    ++clientid_vec[0];
    lease1.client_id_.reset(new ClientId(clientid_vec));
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    --clientid_vec[0];
    lease1.client_id_.reset(new ClientId(clientid_vec));
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
424
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
425 426 427 428 429 430

    ++lease1.t1_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.t1_ = lease2.t1_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
431
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
432 433 434 435 436 437

    ++lease1.t2_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.t2_ = lease2.t2_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
438
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
439 440 441 442 443 444

    ++lease1.valid_lft_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.valid_lft_ = lease2.valid_lft_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
445
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
446 447 448 449 450 451

    ++lease1.cltt_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.cltt_ = lease2.cltt_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
452
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
453 454 455 456 457 458

    ++lease1.subnet_id_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.subnet_id_ = lease2.subnet_id_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
459
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
460 461 462 463 464 465

    lease1.fixed_ = !lease1.fixed_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.fixed_ = lease2.fixed_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
466
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
467 468 469 470 471 472

    lease1.hostname_ += string("Something random");
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.hostname_ = lease2.hostname_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
473
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
474 475 476 477 478 479

    lease1.fqdn_fwd_ = !lease1.fqdn_fwd_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.fqdn_fwd_ = lease2.fqdn_fwd_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
480
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
481 482 483 484 485 486

    lease1.fqdn_rev_ = !lease1.fqdn_rev_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.fqdn_rev_ = lease2.fqdn_rev_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
487
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
488 489 490 491 492 493

    lease1.comments_ += string("Something random");
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.comments_ = lease2.comments_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
494
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
495 496
}

497 498


499
// Lease6 is also defined in lease_mgr.h, so is tested in this file as well.
500
// This test checks if the Lease6 structure can be instantiated correctly
501
TEST(Lease6, Lease6ConstructorDefault) {
502

503 504 505 506 507 508 509
    // check a variety of addresses with different bits set.
    const char* ADDRESS[] = {
        "::", "::1", "2001:db8:1::456",
        "7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
        "8000::", "8000::1",
        "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
    };
510

511
    // Other values
512 513
    uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
    DuidPtr duid(new DUID(llt, sizeof(llt)));
514 515
    uint32_t iaid = 7;      // Just a number
    SubnetID subnet_id = 8; // Just another number
516

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
    for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
        IOAddress addr(ADDRESS[i]);
        Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr,
                               duid, iaid, 100, 200, 50, 80,
                               subnet_id));

        EXPECT_TRUE(lease->addr_ == addr);
        EXPECT_TRUE(*lease->duid_ == *duid);
        EXPECT_TRUE(lease->iaid_ == iaid);
        EXPECT_TRUE(lease->subnet_id_ == subnet_id);
        EXPECT_TRUE(lease->type_ == Lease6::LEASE_IA_NA);
        EXPECT_TRUE(lease->preferred_lft_ == 100);
        EXPECT_TRUE(lease->valid_lft_ == 200);
        EXPECT_TRUE(lease->t1_ == 50);
        EXPECT_TRUE(lease->t2_ == 80);
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
        EXPECT_FALSE(lease->fqdn_fwd_);
        EXPECT_FALSE(lease->fqdn_rev_);
        EXPECT_TRUE(lease->hostname_.empty());

    }

    // Lease6 must be instantiated with a DUID, not with NULL pointer
    IOAddress addr(ADDRESS[0]);
    Lease6Ptr lease2;
    EXPECT_THROW(lease2.reset(new Lease6(Lease6::LEASE_IA_NA, addr,
                                         DuidPtr(), iaid, 100, 200, 50, 80,
                                         subnet_id)), InvalidOperation);
}

// This test verifies that the Lease6 constructor which accepts FQDN data,
// sets the data correctly for the lease.
TEST(Lease6, Lease6ConstructorWithFQDN) {

    // check a variety of addresses with different bits set.
    const char* ADDRESS[] = {
        "::", "::1", "2001:db8:1::456",
        "7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
        "8000::", "8000::1",
        "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
    };

    // Other values
    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

    for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
        IOAddress addr(ADDRESS[i]);
        Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr,
                               duid, iaid, 100, 200, 50, 80, subnet_id,
                                   true, true, "host.example.com."));

        EXPECT_TRUE(lease->addr_ == addr);
        EXPECT_TRUE(*lease->duid_ == *duid);
        EXPECT_TRUE(lease->iaid_ == iaid);
        EXPECT_TRUE(lease->subnet_id_ == subnet_id);
        EXPECT_TRUE(lease->type_ == Lease6::LEASE_IA_NA);
        EXPECT_TRUE(lease->preferred_lft_ == 100);
        EXPECT_TRUE(lease->valid_lft_ == 200);
        EXPECT_TRUE(lease->t1_ == 50);
        EXPECT_TRUE(lease->t2_ == 80);
        EXPECT_TRUE(lease->fqdn_fwd_);
        EXPECT_TRUE(lease->fqdn_rev_);
        EXPECT_EQ("host.example.com.", lease->hostname_);
582
    }
583 584

    // Lease6 must be instantiated with a DUID, not with NULL pointer
585
    IOAddress addr(ADDRESS[0]);
586 587 588 589
    Lease6Ptr lease2;
    EXPECT_THROW(lease2.reset(new Lease6(Lease6::LEASE_IA_NA, addr,
                                         DuidPtr(), iaid, 100, 200, 50, 80,
                                         subnet_id)), InvalidOperation);
590
}
591

592

593 594 595 596 597 598 599
/// @brief Lease6 Equality Test
///
/// Checks that the operator==() correctly compares two leases for equality.
/// As operator!=() is also defined for this class, every check on operator==()
/// is followed by the reverse check on operator!=().
TEST(Lease6, OperatorEquals) {

600
    // check a variety of addresses with different bits set.
601 602 603 604 605 606 607 608 609 610 611
    const IOAddress addr("2001:db8:1::456");
    uint8_t duid_array[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
    DuidPtr duid(new DUID(duid_array, sizeof(duid_array)));
    uint32_t iaid = 7; // just a number
    SubnetID subnet_id = 8; // just another number

    // Check for equality.
    Lease6 lease1(Lease6::LEASE_IA_NA, addr, duid, iaid, 100, 200, 50, 80,
                               subnet_id);
    Lease6 lease2(Lease6::LEASE_IA_NA, addr, duid, iaid, 100, 200, 50, 80,
                               subnet_id);
612 613 614 615

    // cltt_ constructs with time(NULL), make sure they are always equal
    lease1.cltt_ = lease2.cltt_;

616 617 618 619 620 621 622 623 624 625
    EXPECT_TRUE(lease1 == lease2);
    EXPECT_FALSE(lease1 != lease2);

    // Go through and alter all the fields one by one

    lease1.addr_ = IOAddress("::1");
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.addr_ = lease2.addr_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
626
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
627 628 629 630 631 632

    lease1.type_ = Lease6::LEASE_IA_PD;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.type_ = lease2.type_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
633
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
634 635 636 637 638 639

    ++lease1.prefixlen_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.prefixlen_ = lease2.prefixlen_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
640
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
641 642 643 644 645 646

    ++lease1.iaid_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.iaid_ = lease2.iaid_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
647
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
648 649 650 651 652 653 654 655

    ++duid_array[0];
    lease1.duid_.reset(new DUID(duid_array, sizeof(duid_array)));
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    --duid_array[0];
    lease1.duid_.reset(new DUID(duid_array, sizeof(duid_array)));
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
656
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
657 658 659 660 661 662

    ++lease1.preferred_lft_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.preferred_lft_ = lease2.preferred_lft_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
663
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
664 665 666 667 668 669

    ++lease1.valid_lft_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.valid_lft_ = lease2.valid_lft_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
670
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
671 672 673 674 675 676

    ++lease1.t1_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.t1_ = lease2.t1_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
677
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
678 679 680 681 682 683

    ++lease1.t2_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.t2_ = lease2.t2_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
684
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
685 686 687 688 689 690

    ++lease1.cltt_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.cltt_ = lease2.cltt_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
691
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
692 693 694 695 696 697

    ++lease1.subnet_id_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.subnet_id_ = lease2.subnet_id_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
698
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
699 700 701 702 703 704

    lease1.fixed_ = !lease1.fixed_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.fixed_ = lease2.fixed_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
705
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
706 707 708 709 710 711

    lease1.hostname_ += string("Something random");
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.hostname_ = lease2.hostname_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
712
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
713 714 715 716 717 718

    lease1.fqdn_fwd_ = !lease1.fqdn_fwd_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.fqdn_fwd_ = lease2.fqdn_fwd_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
719
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
720 721 722 723 724 725

    lease1.fqdn_rev_ = !lease1.fqdn_rev_;
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.fqdn_rev_ = lease2.fqdn_rev_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
726
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
727 728 729 730 731 732

    lease1.comments_ += string("Something random");
    EXPECT_FALSE(lease1 == lease2);
    EXPECT_TRUE(lease1 != lease2);
    lease1.comments_ = lease2.comments_;
    EXPECT_TRUE(lease1 == lease2);  // Check that the reversion has made the
Stephen Morris's avatar
Stephen Morris committed
733
    EXPECT_FALSE(lease1 != lease2); // ... leases equal
734
}
735 736 737 738 739 740

// Checks if lease expiration is calculated properly
TEST(Lease6, Lease6Expired) {
    const IOAddress addr("2001:db8:1::456");
    const uint8_t duid_array[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
    const DuidPtr duid(new DUID(duid_array, sizeof(duid_array)));
741 742
    const uint32_t iaid = 7;        // Just a number
    const SubnetID subnet_id = 8;   // Just another number
743 744 745
    Lease6 lease(Lease6::LEASE_IA_NA, addr, duid, iaid, 100, 200, 50, 80,
                               subnet_id);

746
    // Case 1: a second before expiration
747 748 749 750
    lease.cltt_ = time(NULL) - 100;
    lease.valid_lft_ = 101;
    EXPECT_FALSE(lease.expired());

751
    // Case 2: the lease will expire after this second is concluded
752 753 754
    lease.cltt_ = time(NULL) - 101;
    EXPECT_FALSE(lease.expired());

755
    // Case 3: the lease is expired
756 757 758 759
    lease.cltt_ = time(NULL) - 102;
    EXPECT_TRUE(lease.expired());
}

760
}; // end of anonymous namespace