pgsql_lease_mgr_unittest.cc 31.2 KB
Newer Older
1
// Copyright (C) 2014-2019 Internet Systems Consortium, Inc. ("ISC")
2
//
3 4 5
// 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/.
6 7 8 9 10 11 12 13

#include <config.h>

#include <asiolink/io_address.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <dhcpsrv/pgsql_lease_mgr.h>
#include <dhcpsrv/tests/test_utils.h>
#include <dhcpsrv/tests/generic_lease_mgr_unittest.h>
14
#include <exceptions/exceptions.h>
15
#include <pgsql/pgsql_connection.h>
16
#include <pgsql/testutils/pgsql_schema.h>
17
#include <util/multi_threading_mgr.h>
18 19 20

#include <gtest/gtest.h>

21 22 23 24 25 26
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>

27 28
using namespace isc;
using namespace isc::asiolink;
29
using namespace isc::db;
30
using namespace isc::db::test;
31 32
using namespace isc::dhcp;
using namespace isc::dhcp::test;
33
using namespace isc::util;
34 35 36 37
using namespace std;

namespace {

38

39 40 41 42 43 44 45
/// @brief Test fixture class for testing PostgreSQL Lease Manager
///
/// Opens the database prior to each test and closes it afterwards.
/// All pending transactions are deleted prior to closure.

class PgSqlLeaseMgrTest : public GenericLeaseMgrTest {
public:
46 47
    /// @brief Clears the database and opens connection to it.
    void initializeTest() {
48
        // Ensure we have the proper schema with no transient data.
49
        createPgSQLSchema();
50 51 52

        // Connect to the database
        try {
53
            LeaseMgrFactory::create(validPgSQLConnectionString());
54 55 56 57 58 59 60 61
        } catch (...) {
            std::cerr << "*** ERROR: unable to open database. The test\n"
                         "*** environment is broken and must be fixed before\n"
                         "*** the PostgreSQL tests will run correctly.\n"
                         "*** The reason for the problem is described in the\n"
                         "*** accompanying exception output.\n";
            throw;
        }
62

63
        lmptr_ = &(LeaseMgrFactory::instance());
64 65

        MultiThreadingMgr::instance().setMode(false);
66 67
    }

68 69 70
    /// @brief Destroys the LM and the schema.
    void destroyTest() {
        LeaseMgrFactory::destroy();
71
        // If data wipe enabled, delete transient data otherwise destroy the schema
72
        destroyPgSQLSchema();
73 74 75

        // Disable Multi-Threading.
        MultiThreadingMgr::instance().setMode(false);
76 77 78 79 80 81 82 83 84
    }

    /// @brief Constructor
    ///
    /// Deletes everything from the database and opens it.
    PgSqlLeaseMgrTest() {
        initializeTest();
    }

85 86 87 88 89
    /// @brief Destructor
    ///
    /// Rolls back all pending transactions.  The deletion of lmptr_ will close
    /// the database.  Then reopen it and delete everything created by the test.
    virtual ~PgSqlLeaseMgrTest() {
90
        destroyTest();
91 92 93 94 95 96
    }

    /// @brief Reopen the database
    ///
    /// Closes the database and re-open it.  Anything committed should be
    /// visible.
97
    ///
98
    /// Parameter is ignored for PostgreSQL backend as the v4 and v6 leases share
99 100
    /// the same database.
    void reopen(Universe) {
101
        LeaseMgrFactory::destroy();
102
        LeaseMgrFactory::create(validPgSQLConnectionString());
103 104 105 106 107 108 109 110 111 112 113
        lmptr_ = &(LeaseMgrFactory::instance());
    }
};

/// @brief Check that database can be opened
///
/// This test checks if the PgSqlLeaseMgr can be instantiated.  This happens
/// only if the database can be opened.  Note that this is not part of the
/// PgSqlLeaseMgr test fixure set.  This test checks that the database can be
/// opened: the fixtures assume that and check basic operations.
TEST(PgSqlOpenTest, OpenDatabase) {
114 115 116
    // Explicitely disable Multi-Threading.
    MultiThreadingMgr::instance().setMode(false);

117
    // Schema needs to be created for the test to work.
118
    createPgSQLSchema();
119 120

    // Check that lease manager open the database opens correctly and tidy up.
121
    // If it fails, print the error message.
122
    try {
123
        LeaseMgrFactory::create(validPgSQLConnectionString());
124
        EXPECT_NO_THROW((void)LeaseMgrFactory::instance());
125 126 127 128 129 130 131
        LeaseMgrFactory::destroy();
    } catch (const isc::Exception& ex) {
        FAIL() << "*** ERROR: unable to open database, reason:\n"
               << "    " << ex.what() << "\n"
               << "*** The test environment is broken and must be fixed\n"
               << "*** before the PostgreSQL tests will run correctly.\n";
    }
132 133 134 135

    // Check that lease manager open the database opens correctly with a longer
    // timeout.  If it fails, print the error message.
    try {
136
        string connection_string = validPgSQLConnectionString() + string(" ") +
137 138 139 140 141 142 143 144
                                   string(VALID_TIMEOUT);
        LeaseMgrFactory::create(connection_string);
        EXPECT_NO_THROW((void) LeaseMgrFactory::instance());
        LeaseMgrFactory::destroy();
    } catch (const isc::Exception& ex) {
        FAIL() << "*** ERROR: unable to open database, reason:\n"
               << "    " << ex.what() << "\n"
               << "*** The test environment is broken and must be fixed\n"
145
               << "*** before the PostgreSQL tests will run correctly.\n";
146 147
    }

148 149 150 151 152 153 154 155 156 157
    // Check that attempting to get an instance of the lease manager when
    // none is set throws an exception.
    EXPECT_THROW(LeaseMgrFactory::instance(), NoLeaseManager);

    // Check that wrong specification of backend throws an exception.
    // (This is really a check on LeaseMgrFactory, but is convenient to
    // perform here.)
    EXPECT_THROW(LeaseMgrFactory::create(connectionString(
        NULL, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
        InvalidParameter);
158

159 160 161 162 163 164
    EXPECT_THROW(LeaseMgrFactory::create(connectionString(
        INVALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
        InvalidType);

    // Check that invalid login data causes an exception.
    EXPECT_THROW(LeaseMgrFactory::create(connectionString(
165
        PGSQL_VALID_TYPE, INVALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
166
        DbOpenError);
167

168
    EXPECT_THROW(LeaseMgrFactory::create(connectionString(
169
        PGSQL_VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)),
170
        DbOpenError);
171

172
    EXPECT_THROW(LeaseMgrFactory::create(connectionString(
173
        PGSQL_VALID_TYPE, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
174
        DbOpenError);
175

176 177 178
    // This test might fail if 'auth-method' in PostgresSQL host-based authentication
    // file (/var/lib/pgsql/9.4/data/pg_hba.conf) is set to 'trust',
    // which allows logging without password. 'Auth-method' should be changed to 'password'.
179
    EXPECT_THROW(LeaseMgrFactory::create(connectionString(
180
        PGSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, INVALID_PASSWORD)),
181 182
        DbOpenError);

183 184 185 186
    // Check for invalid timeouts
    EXPECT_THROW(LeaseMgrFactory::create(connectionString(
        PGSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD, INVALID_TIMEOUT_1)),
        DbInvalidTimeout);
187

188 189 190 191
    EXPECT_THROW(LeaseMgrFactory::create(connectionString(
        PGSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD, INVALID_TIMEOUT_2)),
        DbInvalidTimeout);

192 193
    // Check for missing parameters
    EXPECT_THROW(LeaseMgrFactory::create(connectionString(
194
        PGSQL_VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
195 196 197
        NoDatabaseName);

    // Tidy up after the test
198
    destroyPgSQLSchema();
199 200
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
/// @brief Check that database can be opened with Multi-Threading
TEST(PgSqlOpenTest, OpenDatabaseMultiThreading) {
    // Enable Multi-Threading.
    MultiThreadingMgr::instance().setMode(true);

    // Schema needs to be created for the test to work.
    createPgSQLSchema();

    // Check that lease manager open the database opens correctly and tidy up.
    // If it fails, print the error message.
    try {
        LeaseMgrFactory::create(validPgSQLConnectionString());
        EXPECT_NO_THROW((void)LeaseMgrFactory::instance());
        LeaseMgrFactory::destroy();
    } catch (const isc::Exception& ex) {
        FAIL() << "*** ERROR: unable to open database, reason:\n"
               << "    " << ex.what() << "\n"
               << "*** The test environment is broken and must be fixed\n"
               << "*** before the PostgreSQL tests will run correctly.\n";
    }

    // Tidy up after the test
    destroyPgSQLSchema();
Razvan Becheriu's avatar
Razvan Becheriu committed
224
    LeaseMgrFactory::destroy();
225 226 227 228 229

    // Disable Multi-Threading.
    MultiThreadingMgr::instance().setMode(false);
}

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
/// @brief Check the getType() method
///
/// getType() returns a string giving the type of the backend, which should
/// always be "postgresql".
TEST_F(PgSqlLeaseMgrTest, getType) {
    EXPECT_EQ(std::string("postgresql"), lmptr_->getType());
}

/// @brief Check getName() returns correct database name
TEST_F(PgSqlLeaseMgrTest, getName) {
    EXPECT_EQ(std::string("keatest"), lmptr_->getName());
}

/// @brief Check that getVersion() returns the expected version
TEST_F(PgSqlLeaseMgrTest, checkVersion) {
    // Check version
    pair<uint32_t, uint32_t> version;
    ASSERT_NO_THROW(version = lmptr_->getVersion());
248 249
    EXPECT_EQ(PG_SCHEMA_VERSION_MAJOR, version.first);
    EXPECT_EQ(PG_SCHEMA_VERSION_MINOR, version.second);
250 251
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
252 253 254 255
////////////////////////////////////////////////////////////////////////////////
/// LEASE4 /////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

256 257 258 259 260
/// @brief Basic Lease4 Checks
///
/// Checks that the addLease, getLease4 (by address) and deleteLease (with an
/// IPv4 address) works.
TEST_F(PgSqlLeaseMgrTest, basicLease4) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
261
    testBasicLease4();
262 263
}

264 265 266 267 268 269
/// @brief Basic Lease4 Checks
TEST_F(PgSqlLeaseMgrTest, basicLease4MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testBasicLease4();
}

270 271 272 273 274
/// @brief Check that Lease4 code safely handles invalid dates.
TEST_F(PgSqlLeaseMgrTest, maxDate4) {
    testMaxDate4();
}

275 276 277 278 279 280
/// @brief Check that Lease4 code safely handles invalid dates.
TEST_F(PgSqlLeaseMgrTest, maxDate4MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testMaxDate4();
}

281 282 283 284 285
/// @brief checks that infinite lifetimes do not overflow.
TEST_F(PgSqlLeaseMgrTest, infiniteLifeTime4) {
    testInfiniteLifeTime4();
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
286 287 288
/// @brief Lease4 update tests
///
/// Checks that we are able to update a lease in the database.
289 290 291 292
TEST_F(PgSqlLeaseMgrTest, updateLease4) {
    testUpdateLease4();
}

293 294 295 296 297 298
/// @brief Lease4 update tests
TEST_F(PgSqlLeaseMgrTest, updateLease4MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testUpdateLease4();
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
299 300 301 302 303
/// @brief Check GetLease4 methods - access by Hardware Address
TEST_F(PgSqlLeaseMgrTest, getLease4HWAddr1) {
    testGetLease4HWAddr1();
}

304 305 306 307 308 309
/// @brief Check GetLease4 methods - access by Hardware Address
TEST_F(PgSqlLeaseMgrTest, getLease4HWAddr1MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLease4HWAddr1();
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
310 311 312 313 314
/// @brief Check GetLease4 methods - access by Hardware Address
TEST_F(PgSqlLeaseMgrTest, getLease4HWAddr2) {
    testGetLease4HWAddr2();
}

315 316 317 318 319 320
/// @brief Check GetLease4 methods - access by Hardware Address
TEST_F(PgSqlLeaseMgrTest, getLease4HWAddr2MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLease4HWAddr2();
}

321 322 323 324
/// @brief Get lease4 by hardware address (2)
///
/// Check that the system can cope with getting a hardware address of
/// any size.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
325 326 327 328
TEST_F(PgSqlLeaseMgrTest, getLease4HWAddrSize) {
    testGetLease4HWAddrSize();
}

329
/// @brief Get lease4 by hardware address (2)
330 331 332 333 334
TEST_F(PgSqlLeaseMgrTest, getLease4HWAddrSizeMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLease4HWAddrSize();
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
335 336 337 338 339 340 341 342
/// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
///
/// Adds leases to the database and checks that they can be accessed via
/// a combination of hardware address and subnet ID
TEST_F(PgSqlLeaseMgrTest, getLease4HwaddrSubnetId) {
    testGetLease4HWAddrSubnetId();
}

343 344 345 346 347 348
/// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
TEST_F(PgSqlLeaseMgrTest, getLease4HwaddrSubnetIdMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLease4HWAddrSubnetId();
}

349 350 351 352
/// @brief Get lease4 by hardware address and subnet ID (2)
///
/// Check that the system can cope with getting a hardware address of
/// any size.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
353 354 355 356
TEST_F(PgSqlLeaseMgrTest, getLease4HWAddrSubnetIdSize) {
    testGetLease4HWAddrSubnetIdSize();
}

357
/// @brief Get lease4 by hardware address and subnet ID (2)
358 359 360 361 362
TEST_F(PgSqlLeaseMgrTest, getLease4HWAddrSubnetIdSizeMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLease4HWAddrSubnetIdSize();
}

363
/// @brief This test was derived from memfile.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
364 365 366 367
TEST_F(PgSqlLeaseMgrTest, getLease4ClientId) {
    testGetLease4ClientId();
}

368
/// @brief This test was derived from memfile.
369 370 371 372 373
TEST_F(PgSqlLeaseMgrTest, getLease4ClientIdMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLease4ClientId();
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
374 375 376 377 378 379 380 381
/// @brief Check GetLease4 methods - access by Client ID
///
/// Adds leases to the database and checks that they can be accessed via
/// the Client ID.
TEST_F(PgSqlLeaseMgrTest, getLease4ClientId2) {
    testGetLease4ClientId2();
}

382 383 384 385 386 387
/// @brief Check GetLease4 methods - access by Client ID
TEST_F(PgSqlLeaseMgrTest, getLease4ClientId2MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLease4ClientId2();
}

388 389 390
/// @brief Get Lease4 by client ID (2)
///
/// Check that the system can cope with a client ID of any size.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
391 392 393 394
TEST_F(PgSqlLeaseMgrTest, getLease4ClientIdSize) {
    testGetLease4ClientIdSize();
}

395
/// @brief Get Lease4 by client ID (2)
396 397 398 399 400
TEST_F(PgSqlLeaseMgrTest, getLease4ClientIdSizeMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLease4ClientIdSize();
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
401 402 403 404 405 406 407 408
/// @brief Check GetLease4 methods - access by Client ID & Subnet ID
///
/// Adds leases to the database and checks that they can be accessed via
/// a combination of client and subnet IDs.
TEST_F(PgSqlLeaseMgrTest, getLease4ClientIdSubnetId) {
    testGetLease4ClientIdSubnetId();
}

409 410 411 412 413 414
/// @brief Check GetLease4 methods - access by Client ID & Subnet ID
TEST_F(PgSqlLeaseMgrTest, getLease4ClientIdSubnetIdMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLease4ClientIdSubnetId();
}

415
/// @brief This test checks that all IPv4 leases for a specified subnet id are returned.
416 417 418 419
TEST_F(PgSqlLeaseMgrTest, getLeases4SubnetId) {
    testGetLeases4SubnetId();
}

420
/// @brief This test checks that all IPv4 leases for a specified subnet id are returned.
421 422 423 424 425
TEST_F(PgSqlLeaseMgrTest, getLeases4SubnetIdMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLeases4SubnetId();
}

426
/// @brief This test checks that all IPv4 leases with a specified hostname are returned.
427 428 429 430
TEST_F(PgSqlLeaseMgrTest, getLeases4Hostname) {
    testGetLeases4Hostname();
}

431
/// @brief This test checks that all IPv4 leases with a specified hostname are returned.
432 433 434 435 436
TEST_F(PgSqlLeaseMgrTest, getLeases4HostnameMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLeases4Hostname();
}

437
/// @brief This test checks that all IPv4 leases are returned.
438 439 440 441
TEST_F(PgSqlLeaseMgrTest, getLeases4) {
    testGetLeases4();
}

442
/// @brief This test checks that all IPv4 leases are returned.
443 444 445 446 447
TEST_F(PgSqlLeaseMgrTest, getLeases4MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLeases4();
}

448
/// @brief Test that a range of IPv4 leases is returned with paging.
449 450 451 452
TEST_F(PgSqlLeaseMgrTest, getLeases4Paged) {
    testGetLeases4Paged();
}

453
/// @brief Test that a range of IPv4 leases is returned with paging.
454 455 456 457 458
TEST_F(PgSqlLeaseMgrTest, getLeases4PagedMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLeases4Paged();
}

459
/// @brief This test checks that all IPv6 leases for a specified subnet id are returned.
460 461 462 463
TEST_F(PgSqlLeaseMgrTest, getLeases6SubnetId) {
    testGetLeases6SubnetId();
}

464
/// @brief This test checks that all IPv6 leases for a specified subnet id are returned.
465 466 467 468 469
TEST_F(PgSqlLeaseMgrTest, getLeases6SubnetIdMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLeases6SubnetId();
}

470
/// @brief This test checks that all IPv6 leases with a specified hostname are returned.
471 472 473 474
TEST_F(PgSqlLeaseMgrTest, getLeases6Hostname) {
    testGetLeases6Hostname();
}

475
/// @brief This test checks that all IPv6 leases with a specified hostname are returned.
476 477 478 479 480
TEST_F(PgSqlLeaseMgrTest, getLeases6HostnameMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLeases6Hostname();
}

481
/// @brief This test checks that all IPv6 leases are returned.
482 483 484 485
TEST_F(PgSqlLeaseMgrTest, getLeases6) {
    testGetLeases6();
}

486
/// @brief This test checks that all IPv6 leases are returned.
487 488 489 490 491
TEST_F(PgSqlLeaseMgrTest, getLeases6MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLeases6();
}

492
/// @brief Test that a range of IPv6 leases is returned with paging.
493 494 495 496
TEST_F(PgSqlLeaseMgrTest, getLeases6Paged) {
    testGetLeases6Paged();
}

497
/// @brief Test that a range of IPv6 leases is returned with paging.
498 499 500 501 502
TEST_F(PgSqlLeaseMgrTest, getLeases6PagedMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLeases6Paged();
}

503 504 505
/// @brief Basic Lease4 Checks
///
/// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id),
506
/// updateLease4() and deleteLease can handle NULL client-id.
507 508
/// (client-id is optional and may not be present)
TEST_F(PgSqlLeaseMgrTest, lease4NullClientId) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
509 510
    testLease4NullClientId();
}
511

512 513 514 515 516 517
/// @brief Basic Lease4 Checks
TEST_F(PgSqlLeaseMgrTest, lease4NullClientIdMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testLease4NullClientId();
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
518 519 520 521 522 523 524 525
/// @brief Verify that too long hostname for Lease4 is not accepted.
///
/// Checks that the it is not possible to create a lease when the hostname
/// length exceeds 255 characters.
TEST_F(PgSqlLeaseMgrTest, lease4InvalidHostname) {
    testLease4InvalidHostname();
}

526 527 528 529 530 531
/// @brief Verify that too long hostname for Lease4 is not accepted.
TEST_F(PgSqlLeaseMgrTest, lease4InvalidHostnameMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testLease4InvalidHostname();
}

532 533 534 535 536 537 538 539 540 541 542
/// @brief Check that the expired DHCPv4 leases can be retrieved.
///
/// This test adds a number of leases to the lease database and marks
/// some of them as expired. Then it queries for expired leases and checks
/// whether only expired leases are returned, and that they are returned in
/// the order from most to least expired. It also checks that the lease
/// which is marked as 'reclaimed' is not returned.
TEST_F(PgSqlLeaseMgrTest, getExpiredLeases4) {
    testGetExpiredLeases4();
}

543 544 545 546 547 548
/// @brief Check that the expired DHCPv4 leases can be retrieved.
TEST_F(PgSqlLeaseMgrTest, getExpiredLeases4MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetExpiredLeases4();
}

Francis Dupont's avatar
Francis Dupont committed
549 550
/// @brief Checks that DHCPv4 leases with infinite valid lifetime
/// will never expire.
551 552
TEST_F(PgSqlLeaseMgrTest, infiniteAreNotExpired4) {
    testInfiniteAreNotExpired4();
553 554
}

555 556 557 558 559
/// @brief Check that expired reclaimed DHCPv4 leases are removed.
TEST_F(PgSqlLeaseMgrTest, deleteExpiredReclaimedLeases4) {
    testDeleteExpiredReclaimedLeases4();
}

560 561 562 563 564 565
/// @brief Check that expired reclaimed DHCPv4 leases are removed.
TEST_F(PgSqlLeaseMgrTest, deleteExpiredReclaimedLeases4MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testDeleteExpiredReclaimedLeases4();
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
566 567 568 569
////////////////////////////////////////////////////////////////////////////////
/// LEASE6 /////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

570 571
/// @brief Test checks whether simple add, get and delete operations
/// are possible on Lease6
Tomek Mrugalski's avatar
Tomek Mrugalski committed
572
TEST_F(PgSqlLeaseMgrTest, testAddGetDelete6) {
573
    testAddGetDelete6();
574 575
}

576 577
/// @brief Test checks whether simple add, get and delete operations
/// are possible on Lease6
578 579 580 581 582
TEST_F(PgSqlLeaseMgrTest, testAddGetDelete6MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testAddGetDelete6();
}

583 584 585 586 587
/// @brief Basic Lease6 Checks
///
/// Checks that the addLease, getLease6 (by address) and deleteLease (with an
/// IPv6 address) works.
TEST_F(PgSqlLeaseMgrTest, basicLease6) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
588 589 590
    testBasicLease6();
}

591 592 593 594 595 596
/// @brief Basic Lease6 Checks
TEST_F(PgSqlLeaseMgrTest, basicLease6MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testBasicLease6();
}

597 598 599 600 601
/// @brief Check that Lease6 code safely handles invalid dates.
TEST_F(PgSqlLeaseMgrTest, maxDate6) {
    testMaxDate6();
}

602 603 604 605 606 607
/// @brief Check that Lease6 code safely handles invalid dates.
TEST_F(PgSqlLeaseMgrTest, maxDate6MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testMaxDate6();
}

608 609 610 611 612
/// @brief checks that infinite lifetimes do not overflow.
TEST_F(PgSqlLeaseMgrTest, infiniteLifeTime6) {
    testInfiniteLifeTime6();
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
613 614 615 616 617 618 619 620
/// @brief Verify that too long hostname for Lease6 is not accepted.
///
/// Checks that the it is not possible to create a lease when the hostname
/// length exceeds 255 characters.
TEST_F(PgSqlLeaseMgrTest, lease6InvalidHostname) {
    testLease6InvalidHostname();
}

621 622 623 624 625 626
/// @brief Verify that too long hostname for Lease6 is not accepted.
TEST_F(PgSqlLeaseMgrTest, lease6InvalidHostnameMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testLease6InvalidHostname();
}

627 628 629 630 631 632 633 634
/// @brief Verify that large IAID values work correctly.
///
/// Adds lease with a large IAID to the database and verifies it can
/// fetched correclty.
TEST_F(PgSqlLeaseMgrTest, leases6LargeIaidCheck) {
    testLease6LargeIaidCheck();
}

635 636 637 638 639 640
/// @brief Verify that large IAID values work correctly.
TEST_F(PgSqlLeaseMgrTest, leases6LargeIaidCheckMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testLease6LargeIaidCheck();
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
641 642 643 644 645 646 647 648
/// @brief Check GetLease6 methods - access by DUID/IAID
///
/// Adds leases to the database and checks that they can be accessed via
/// a combination of DUID and IAID.
TEST_F(PgSqlLeaseMgrTest, getLeases6DuidIaid) {
    testGetLeases6DuidIaid();
}

649 650 651 652 653 654
/// @brief Check GetLease6 methods - access by DUID/IAID
TEST_F(PgSqlLeaseMgrTest, getLeases6DuidIaidMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLeases6DuidIaid();
}

655
/// @brief Check that the system can cope with a DUID of allowed size.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
656 657 658 659
TEST_F(PgSqlLeaseMgrTest, getLeases6DuidSize) {
    testGetLeases6DuidSize();
}

660
/// @brief Check that the system can cope with a DUID of allowed size.
661 662 663 664 665
TEST_F(PgSqlLeaseMgrTest, getLeases6DuidSizeMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLeases6DuidSize();
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
666 667 668 669 670 671 672 673 674 675
/// @brief Check that getLease6 methods discriminate by lease type.
///
/// Adds six leases, two per lease type all with the same duid and iad but
/// with alternating subnet_ids.
/// It then verifies that all of getLeases6() method variants correctly
/// discriminate between the leases based on lease type alone.
TEST_F(PgSqlLeaseMgrTest, lease6LeaseTypeCheck) {
    testLease6LeaseTypeCheck();
}

676 677 678 679 680 681
/// @brief Check that getLease6 methods discriminate by lease type.
TEST_F(PgSqlLeaseMgrTest, lease6LeaseTypeCheckMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testLease6LeaseTypeCheck();
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
682 683 684 685 686 687 688 689
/// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
///
/// Adds leases to the database and checks that they can be accessed via
/// a combination of DIUID and IAID.
TEST_F(PgSqlLeaseMgrTest, getLease6DuidIaidSubnetId) {
    testGetLease6DuidIaidSubnetId();
}

690 691 692 693 694 695
/// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
TEST_F(PgSqlLeaseMgrTest, getLease6DuidIaidSubnetIdMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLease6DuidIaidSubnetId();
}

696
/// @brief Test checks that getLease6() works with different DUID sizes
Tomek Mrugalski's avatar
Tomek Mrugalski committed
697 698 699 700
TEST_F(PgSqlLeaseMgrTest, getLease6DuidIaidSubnetIdSize) {
    testGetLease6DuidIaidSubnetIdSize();
}

701
/// @brief Test checks that getLease6() works with different DUID sizes
702 703 704 705 706
TEST_F(PgSqlLeaseMgrTest, getLease6DuidIaidSubnetIdSizeMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLease6DuidIaidSubnetIdSize();
}

707 708 709 710 711 712 713 714 715 716 717 718 719 720
/// @brief check leases could be retrieved by DUID
///
/// Create leases, add them to backend and verify if it can be queried
/// using DUID index
TEST_F(PgSqlLeaseMgrTest, getLeases6Duid) {
    testGetLeases6Duid();
}

/// @brief check leases could be retrieved by DUID
TEST_F(PgSqlLeaseMgrTest, getLeases6DuidMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetLeases6Duid();
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
721 722 723 724 725
/// @brief Lease6 update tests
///
/// Checks that we are able to update a lease in the database.
TEST_F(PgSqlLeaseMgrTest, updateLease6) {
    testUpdateLease6();
726 727
}

728 729 730 731 732 733
/// @brief Lease6 update tests
TEST_F(PgSqlLeaseMgrTest, updateLease6MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testUpdateLease6();
}

734 735 736 737 738 739 740 741 742
/// @brief DHCPv4 Lease recreation tests
///
/// Checks that the lease can be created, deleted and recreated with
/// different parameters. It also checks that the re-created lease is
/// correctly stored in the lease database.
TEST_F(PgSqlLeaseMgrTest, testRecreateLease4) {
    testRecreateLease4();
}

743 744 745 746 747 748
/// @brief DHCPv4 Lease recreation tests
TEST_F(PgSqlLeaseMgrTest, testRecreateLease4MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testRecreateLease4();
}

749 750 751 752 753 754 755 756 757
/// @brief DHCPv6 Lease recreation tests
///
/// Checks that the lease can be created, deleted and recreated with
/// different parameters. It also checks that the re-created lease is
/// correctly stored in the lease database.
TEST_F(PgSqlLeaseMgrTest, testRecreateLease6) {
    testRecreateLease6();
}

758 759 760 761 762 763
/// @brief DHCPv6 Lease recreation tests
TEST_F(PgSqlLeaseMgrTest, testRecreateLease6MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testRecreateLease6();
}

764
/// @brief Checks that null DUID is not allowed.
765 766 767 768
TEST_F(PgSqlLeaseMgrTest, nullDuid) {
    testNullDuid();
}

769 770 771 772 773 774
/// @brief Checks that null DUID is not allowed.
TEST_F(PgSqlLeaseMgrTest, nullDuidMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testNullDuid();
}

775
/// @brief Tests whether PostgreSQL can store and retrieve hardware addresses
776 777 778 779
TEST_F(PgSqlLeaseMgrTest, testLease6Mac) {
    testLease6MAC();
}

780 781 782 783 784 785
/// @brief Tests whether PostgreSQL can store and retrieve hardware addresses
TEST_F(PgSqlLeaseMgrTest, testLease6MacMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testLease6MAC();
}

786
/// @brief Tests whether PostgreSQL can store and retrieve hardware addresses
787 788 789 790
TEST_F(PgSqlLeaseMgrTest, testLease6HWTypeAndSource) {
    testLease6HWTypeAndSource();
}

791 792 793 794 795 796
/// @brief Tests whether PostgreSQL can store and retrieve hardware addresses
TEST_F(PgSqlLeaseMgrTest, testLease6HWTypeAndSourceMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testLease6HWTypeAndSource();
}

797 798 799 800 801 802 803 804 805 806 807
/// @brief Check that the expired DHCPv6 leases can be retrieved.
///
/// This test adds a number of leases to the lease database and marks
/// some of them as expired. Then it queries for expired leases and checks
/// whether only expired leases are returned, and that they are returned in
/// the order from most to least expired. It also checks that the lease
/// which is marked as 'reclaimed' is not returned.
TEST_F(PgSqlLeaseMgrTest, getExpiredLeases6) {
    testGetExpiredLeases6();
}

808 809 810 811 812 813
/// @brief Check that the expired DHCPv6 leases can be retrieved.
TEST_F(PgSqlLeaseMgrTest, getExpiredLeases6MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testGetExpiredLeases6();
}

Francis Dupont's avatar
Francis Dupont committed
814 815
/// @brief Checks that DHCPv6 leases with infinite valid lifetime
/// will never expire.
816 817
TEST_F(PgSqlLeaseMgrTest, infiniteAreNotExpired6) {
    testInfiniteAreNotExpired6();
818 819
}

820 821 822 823 824
/// @brief Check that expired reclaimed DHCPv6 leases are removed.
TEST_F(PgSqlLeaseMgrTest, deleteExpiredReclaimedLeases6) {
    testDeleteExpiredReclaimedLeases6();
}

825 826 827 828 829 830
/// @brief Check that expired reclaimed DHCPv6 leases are removed.
TEST_F(PgSqlLeaseMgrTest, deleteExpiredReclaimedLeases6MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testDeleteExpiredReclaimedLeases6();
}

Razvan Becheriu's avatar
Razvan Becheriu committed
831
/// @brief Verifies that IPv4 lease statistics can be recalculated.
832 833
TEST_F(PgSqlLeaseMgrTest, recountLeaseStats4) {
    testRecountLeaseStats4();
834 835
}

836 837 838 839 840 841
/// @brief Verifies that IPv4 lease statistics can be recalculated.
TEST_F(PgSqlLeaseMgrTest, recountLeaseStats4MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testRecountLeaseStats4();
}

Razvan Becheriu's avatar
Razvan Becheriu committed
842
/// @brief Verifies that IPv6 lease statistics can be recalculated.
843 844
TEST_F(PgSqlLeaseMgrTest, recountLeaseStats6) {
    testRecountLeaseStats6();
845
}
846

847 848 849 850 851 852
/// @brief Verifies that IPv6 lease statistics can be recalculated.
TEST_F(PgSqlLeaseMgrTest, recountLeaseStats6MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testRecountLeaseStats6();
}

853
/// @brief Tests that leases from specific subnet can be removed.
854 855 856 857
TEST_F(PgSqlLeaseMgrTest, DISABLED_wipeLeases4) {
    testWipeLeases4();
}

858 859 860 861 862 863
/// @brief Tests that leases from specific subnet can be removed.
TEST_F(PgSqlLeaseMgrTest, DISABLED_wipeLeases4MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testWipeLeases4();
}

864
/// @brief Tests that leases from specific subnet can be removed.
865 866 867 868
TEST_F(PgSqlLeaseMgrTest, DISABLED_wipeLeases6) {
    testWipeLeases6();
}

869 870 871 872 873 874
/// @brief Tests that leases from specific subnet can be removed.
TEST_F(PgSqlLeaseMgrTest, DISABLED_wipeLeases6MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testWipeLeases6();
}

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
/// @brief Test fixture class for validating @c LeaseMgr using
/// PostgreSQL as back end and PostgreSQL connectivity loss.
class PgSqlLeaseMgrDbLostCallbackTest : public LeaseMgrDbLostCallbackTest {
public:
    virtual void destroySchema() {
        destroyPgSQLSchema();
    }

    virtual void createSchema() {
        createPgSQLSchema();
    }

    virtual std::string validConnectString() {
        return (validPgSQLConnectionString());
    }

    virtual std::string invalidConnectString() {
        return (connectionString(PGSQL_VALID_TYPE, VALID_NAME, INVALID_HOST,
                        VALID_USER, VALID_PASSWORD));
    }
};

/// @brief Verifies that db lost callback is not invoked on an open failure
TEST_F(PgSqlLeaseMgrDbLostCallbackTest, testNoCallbackOnOpenFailure) {
    MultiThreadingMgr::instance().setMode(false);
    testDbLostCallback();
}

/// @brief Verifies that db lost callback is not invoked on an open failure
TEST_F(PgSqlLeaseMgrDbLostCallbackTest, testNoCallbackOnOpenFailureMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testDbLostCallback();
}

/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLeaseMgrDbLostCallbackTest, testDbLostCallback) {
    MultiThreadingMgr::instance().setMode(false);
    testDbLostCallback();
}

/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLeaseMgrDbLostCallbackTest, testDbLostCallbackMultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testDbLostCallback();
}

/// @brief Tests v4 lease stats query variants.
922 923 924 925
TEST_F(PgSqlLeaseMgrTest, leaseStatsQuery4) {
    testLeaseStatsQuery4();
}

926
/// @brief Tests v4 lease stats query variants.
927 928 929 930 931
TEST_F(PgSqlLeaseMgrTest, leaseStatsQuery4MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testLeaseStatsQuery4();
}

932
/// @brief Tests v6 lease stats query variants.
933 934 935 936
TEST_F(PgSqlLeaseMgrTest, leaseStatsQuery6) {
    testLeaseStatsQuery6();
}

937
/// @brief Tests v6 lease stats query variants.
938 939 940 941 942
TEST_F(PgSqlLeaseMgrTest, leaseStatsQuery6MultiThreading) {
    MultiThreadingMgr::instance().setMode(true);
    testLeaseStatsQuery6();
}

943
}  // namespace