generic_host_data_source_unittest.cc 63.1 KB
Newer Older
1
// Copyright (C) 2015-2017 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

Andrei Pavel's avatar
Andrei Pavel committed
7
#include <config.h>
Andrei Pavel's avatar
Andrei Pavel committed
8 9 10

#include <asiolink/io_address.h>
#include <boost/foreach.hpp>
11 12 13 14 15
#include <dhcp/dhcp6.h>
#include <dhcp/libdhcp++.h>
#include <dhcp/option4_addrlst.h>
#include <dhcp/option6_addrlst.h>
#include <dhcp/option_int.h>
Andrei Pavel's avatar
Andrei Pavel committed
16
#include <dhcp/option_string.h>
17
#include <dhcp/option_vendor.h>
Andrei Pavel's avatar
Andrei Pavel committed
18
#include <dhcpsrv/database_connection.h>
19
#include <dhcpsrv/host_data_source_factory.h>
20 21
#include <dhcpsrv/tests/generic_host_data_source_unittest.h>
#include <dhcpsrv/tests/test_utils.h>
22
#include <dhcpsrv/testutils/schema.h>
23
#include <gtest/gtest.h>
Andrei Pavel's avatar
Andrei Pavel committed
24 25 26
#include <util/buffer.h>

#include <chrono>
27 28
#include <cstring>
#include <list>
29
#include <sstream>
Andrei Pavel's avatar
Andrei Pavel committed
30
#include <string>
31
#include <typeinfo>
32 33 34

using namespace std;
using namespace isc::asiolink;
35
using namespace isc::util;
36 37 38 39 40

namespace isc {
namespace dhcp {
namespace test {

Andrei Pavel's avatar
Andrei Pavel committed
41
GenericHostDataSourceTest::GenericHostDataSourceTest() : hdsptr_() {
42
    LibDHCP::clearRuntimeOptionDefs();
43 44 45
}

GenericHostDataSourceTest::~GenericHostDataSourceTest() {
46
    LibDHCP::clearRuntimeOptionDefs();
Andrei Pavel's avatar
Andrei Pavel committed
47
    hdsptr_.reset();
48 49
}

50
std::vector<uint8_t>
51
GenericHostDataSourceTest::generateHWAddr(const bool new_identifier) {
52 53 54 55 56 57
    /// @todo: Consider moving this somewhere to lib/testutils.

    // Let's use something that is easily printable. That's convenient
    // if you need to enter MySQL queries by hand.
    static uint8_t hwaddr[] = {65, 66, 67, 68, 69, 70};

58 59 60 61 62 63 64 65
    if (new_identifier) {
        // Increase the address for the next time we use it.
        // This is primitive, but will work for 65k unique
        // addresses.
        hwaddr[sizeof(hwaddr) - 1]++;
        if (hwaddr[sizeof(hwaddr) - 1] == 0) {
            hwaddr[sizeof(hwaddr) - 2]++;
        }
66
    }
67
    return (std::vector<uint8_t>(hwaddr, hwaddr + sizeof(hwaddr)));
68 69
}

70 71
std::vector<uint8_t>
GenericHostDataSourceTest::generateIdentifier(const bool new_identifier) {
72 73 74 75
    /// @todo: Consider moving this somewhere to lib/testutils.

    // Let's use something that is easily printable. That's convenient
    // if you need to enter MySQL queries by hand.
Andrei Pavel's avatar
Andrei Pavel committed
76
    static uint8_t ident[] = {65, 66, 67, 68, 69, 70, 71, 72, 73, 74};
77

78
    if (new_identifier) {
Andrei Pavel's avatar
Andrei Pavel committed
79 80
        // Increase the identifier for the next time we use it.
        // This is primitive, but will work for 65k unique identifiers.
81 82 83
        ident[sizeof(ident) - 1]++;
        if (ident[sizeof(ident) - 1] == 0) {
            ident[sizeof(ident) - 2]++;
84
        }
85
    }
86
    return (std::vector<uint8_t>(ident, ident + sizeof(ident)));
87 88
}

89 90 91 92 93
HostPtr
GenericHostDataSourceTest::initializeHost4(const std::string& address,
                                           const Host::IdentifierType& id) {
    std::vector<uint8_t> ident;
    if (id == Host::IDENT_HWADDR) {
94 95
        ident = generateHWAddr();
    } else {
96
        ident = generateIdentifier();
97 98 99 100 101 102 103
    }

    // Let's create ever increasing subnet-ids. Let's keep those different,
    // so subnet4 != subnet6. Useful for catching cases if the code confuses
    // subnet4 with subnet6.
    static SubnetID subnet4 = 0;
    static SubnetID subnet6 = 100;
104 105
    ++subnet4;
    ++subnet6;
106 107

    IOAddress addr(address);
108
    HostPtr host(new Host(&ident[0], ident.size(), id, subnet4, subnet6, addr));
109 110 111 112

    return (host);
}

Andrei Pavel's avatar
Andrei Pavel committed
113 114 115 116 117
HostPtr
GenericHostDataSourceTest::initializeHost6(std::string address,
                                           Host::IdentifierType identifier,
                                           bool prefix,
                                           bool new_identifier) {
118
    std::vector<uint8_t> ident;
119
    switch (identifier) {
120
    case Host::IDENT_HWADDR:
121
        ident = generateHWAddr(new_identifier);
122
        break;
123
    case Host::IDENT_DUID:
124
        ident = generateIdentifier(new_identifier);
125 126 127 128 129 130 131 132 133 134 135 136 137 138
        break;
    default:
        ADD_FAILURE() << "Unknown IdType: " << identifier;
        return HostPtr();
    }

    // Let's create ever increasing subnet-ids. Let's keep those different,
    // so subnet4 != subnet6. Useful for catching cases if the code confuses
    // subnet4 with subnet6.
    static SubnetID subnet4 = 0;
    static SubnetID subnet6 = 100;
    subnet4++;
    subnet6++;

Andrei Pavel's avatar
Andrei Pavel committed
139 140
    HostPtr host(new Host(&ident[0], ident.size(), identifier, subnet4, subnet6,
                          IOAddress("0.0.0.0")));
141 142 143 144 145 146 147 148 149 150 151 152 153

    if (!prefix) {
        // Create IPv6 reservation (for an address)
        IPv6Resrv resv(IPv6Resrv::TYPE_NA, IOAddress(address), 128);
        host->addReservation(resv);
    } else {
        // Create IPv6 reservation for a /64 prefix
        IPv6Resrv resv(IPv6Resrv::TYPE_PD, IOAddress(address), 64);
        host->addReservation(resv);
    }
    return (host);
}

Andrei Pavel's avatar
Andrei Pavel committed
154 155 156 157 158 159 160 161 162 163 164
bool
GenericHostDataSourceTest::reservationExists(const IPv6Resrv& resrv,
                                             const IPv6ResrvRange& range) {
    for (IPv6ResrvIterator it = range.first; it != range.second; ++it) {
        if (resrv == it->second) {
            return true;
        }
    }
    return false;
}

165 166 167 168 169 170 171 172 173 174 175 176 177 178
void
GenericHostDataSourceTest::compareHwaddrs(const ConstHostPtr& host1,
                                          const ConstHostPtr& host2,
                                          bool expect_match) {
    ASSERT_TRUE(host1);
    ASSERT_TRUE(host2);

    // Compare if both have or have not HWaddress set.
    if ((host1->getHWAddress() && !host2->getHWAddress()) ||
        (!host1->getHWAddress() && host2->getHWAddress())) {
        // One host has hardware address set while the other has not.
        // Let's see if it's a problem.
        if (expect_match) {
            ADD_FAILURE() << "Host comparison failed: host1 hwaddress="
Andrei Pavel's avatar
Andrei Pavel committed
179 180
                          << host1->getHWAddress()
                          << ", host2 hwaddress=" << host2->getHWAddress();
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        }
        return;
    }

    // Now we know that either both or neither have hw address set.
    // If host1 has it, we can proceed to value comparison.
    if (host1->getHWAddress()) {
        if (expect_match) {
            // Compare the actual address if they match.
            EXPECT_TRUE(*host1->getHWAddress() == *host2->getHWAddress());
        } else {
            EXPECT_FALSE(*host1->getHWAddress() == *host2->getHWAddress());
        }
        if (*host1->getHWAddress() != *host2->getHWAddress()) {
            cout << host1->getHWAddress()->toText(true) << endl;
            cout << host2->getHWAddress()->toText(true) << endl;
        }
    }
}

void
GenericHostDataSourceTest::compareDuids(const ConstHostPtr& host1,
                                        const ConstHostPtr& host2,
                                        bool expect_match) {
    ASSERT_TRUE(host1);
    ASSERT_TRUE(host2);

    // compare if both have or have not DUID set
    if ((host1->getDuid() && !host2->getDuid()) ||
        (!host1->getDuid() && host2->getDuid())) {
        // One host has a DUID and the other doesn't.
        // Let's see if it's a problem.
        if (expect_match) {
            ADD_FAILURE() << "DUID comparison failed: host1 duid="
Andrei Pavel's avatar
Andrei Pavel committed
215 216
                          << host1->getDuid()
                          << ", host2 duid=" << host2->getDuid();
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        }
        return;
    }

    // Now we know that either both or neither have DUID set.
    // If host1 has it, we can proceed to value comparison.
    if (host1->getDuid()) {
        if (expect_match) {
            EXPECT_TRUE(*host1->getDuid() == *host2->getDuid());
        } else {
            EXPECT_FALSE(*host1->getDuid() == *host2->getDuid());
        }
        if (*host1->getDuid() != *host2->getDuid()) {
            cout << host1->getDuid()->toText() << endl;
            cout << host2->getDuid()->toText() << endl;
        }
    }
}

Andrei Pavel's avatar
Andrei Pavel committed
236 237 238
void
GenericHostDataSourceTest::compareHosts(const ConstHostPtr& host1,
                                        const ConstHostPtr& host2) {
239 240 241 242 243 244 245 246
    // Let's compare HW addresses and expect match.
    compareHwaddrs(host1, host2, true);

    // Now compare DUIDs
    compareDuids(host1, host2, true);

    // Now check that the identifiers returned as vectors are the same
    EXPECT_EQ(host1->getIdentifierType(), host2->getIdentifierType());
247
    EXPECT_TRUE(host1->getIdentifier() == host2->getIdentifier());
248 249 250 251 252 253

    // Check host parameters
    EXPECT_EQ(host1->getIPv4SubnetID(), host2->getIPv4SubnetID());
    EXPECT_EQ(host1->getIPv6SubnetID(), host2->getIPv6SubnetID());
    EXPECT_EQ(host1->getIPv4Reservation(), host2->getIPv4Reservation());
    EXPECT_EQ(host1->getHostname(), host2->getHostname());
254 255 256
    EXPECT_EQ(host1->getNextServer(), host2->getNextServer());
    EXPECT_EQ(host1->getServerHostname(), host2->getServerHostname());
    EXPECT_EQ(host1->getBootFileName(), host2->getBootFileName());
257 258 259

    // Compare IPv6 reservations
    compareReservations6(host1->getIPv6Reservations(),
260
                         host2->getIPv6Reservations());
261

262
    // Compare client classification details
263 264 265 266 267
    compareClientClasses(host1->getClientClasses4(),
                         host2->getClientClasses4());

    compareClientClasses(host1->getClientClasses6(),
                         host2->getClientClasses6());
268

269
    // Compare DHCPv4 and DHCPv6 options.
270 271
    compareOptions(host1->getCfgOption4(), host2->getCfgOption4());
    compareOptions(host1->getCfgOption6(), host2->getCfgOption6());
272 273
}

Andrei Pavel's avatar
Andrei Pavel committed
274 275 276
bool
GenericHostDataSourceTest::compareHostsForSort4(const ConstHostPtr& host1,
                                                const ConstHostPtr& host2) {
Andrei Pavel's avatar
Andrei Pavel committed
277 278 279 280 281 282
    if (host1->getIPv4SubnetID() < host2->getIPv4SubnetID()) {
        return true;
    }
    return false;
}

Andrei Pavel's avatar
Andrei Pavel committed
283 284 285
bool
GenericHostDataSourceTest::compareHostsForSort6(const ConstHostPtr& host1,
                                                const ConstHostPtr& host2) {
Andrei Pavel's avatar
Andrei Pavel committed
286 287 288 289 290 291
    if (host1->getIPv6SubnetID() < host2->getIPv6SubnetID()) {
        return true;
    }
    return false;
}

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
DuidPtr
GenericHostDataSourceTest::HWAddrToDuid(const HWAddrPtr& hwaddr) {
    if (!hwaddr) {
        return (DuidPtr());
    }

    return (DuidPtr(new DUID(hwaddr->hwaddr_)));
}

HWAddrPtr
GenericHostDataSourceTest::DuidToHWAddr(const DuidPtr& duid) {
    if (!duid) {
        return (HWAddrPtr());
    }

    return (HWAddrPtr(new HWAddr(duid->getDuid(), HTYPE_ETHER)));
}

void
GenericHostDataSourceTest::compareReservations6(IPv6ResrvRange resrv1,
312
                                                IPv6ResrvRange resrv2) {
313 314
    // Compare number of reservations for both hosts
    if (std::distance(resrv1.first, resrv1.second) !=
Andrei Pavel's avatar
Andrei Pavel committed
315 316 317
        std::distance(resrv2.first, resrv2.second)) {
        ADD_FAILURE() << "Reservation comparison failed, "
                         "hosts got different number of reservations.";
318 319 320
        return;
    }

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    // Iterate over the range of reservations to find a match in the
    // reference range.
    for (IPv6ResrvIterator r1 = resrv1.first; r1 != resrv1.second; ++r1) {
        IPv6ResrvIterator r2 = resrv2.first;
        for (; r2 != resrv2.second; ++r2) {
            // IPv6Resrv object implements equality operator.
            if (r1->second == r2->second) {
                break;
            }
        }
        // If r2 iterator reached the end of the range it means that there
        // is no match.
        if (r2 == resrv2.second) {
            ADD_FAILURE() << "No match found for reservation: "
                          << resrv1.first->second.getPrefix().toText();
        }
    }

339
    if (std::distance(resrv1.first, resrv1.second) > 0) {
340 341 342
        for (; resrv1.first != resrv1.second; resrv1.first++) {
            IPv6ResrvIterator iter = resrv2.first;
            while (iter != resrv2.second) {
Andrei Pavel's avatar
Andrei Pavel committed
343 344 345 346 347 348
                if ((resrv1.first->second.getType() ==
                     iter->second.getType()) &&
                    (resrv1.first->second.getPrefixLen() ==
                     iter->second.getPrefixLen()) &&
                    (resrv1.first->second.getPrefix() ==
                     iter->second.getPrefix())) {
349 350 351
                    break;
                }
                iter++;
352
                if (iter == resrv2.second) {
Andrei Pavel's avatar
Andrei Pavel committed
353 354 355
                    ADD_FAILURE() << "Reservation comparison failed, "
                                     "no match for reservation: "
                                  << resrv1.first->second.getPrefix().toText();
356
                }
357
            }
358 359 360 361 362
        }
    }
}

void
363 364
GenericHostDataSourceTest::compareClientClasses(const ClientClasses& classes1,
                                                const ClientClasses& classes2) {
365
    EXPECT_TRUE(std::equal(classes1.begin(), classes1.end(), classes2.begin()));
366 367
}

368 369 370 371 372 373
void
GenericHostDataSourceTest::compareOptions(const ConstCfgOptionPtr& cfg1,
                                          const ConstCfgOptionPtr& cfg2) const {
    ASSERT_TRUE(cfg1);
    ASSERT_TRUE(cfg2);

374
    // Combine option space names with vendor space names in a single list.
375 376 377 378 379
    std::list<std::string> option_spaces = cfg2->getOptionSpaceNames();
    std::list<std::string> vendor_spaces = cfg2->getVendorIdsSpaceNames();
    option_spaces.insert(option_spaces.end(), vendor_spaces.begin(),
                         vendor_spaces.end());

380 381 382 383 384
    // Make sure that the number of option spaces is equal in both
    // configurations.
    EXPECT_EQ(option_spaces.size(), cfg1->getOptionSpaceNames().size());
    EXPECT_EQ(vendor_spaces.size(), cfg1->getVendorIdsSpaceNames().size());

385
    // Iterate over all option spaces existing in cfg2.
Andrei Pavel's avatar
Andrei Pavel committed
386
    BOOST_FOREACH (std::string space, option_spaces) {
387
        // Retrieve options belonging to the current option space.
388 389
        OptionContainerPtr options1 = cfg1->getAll(space);
        OptionContainerPtr options2 = cfg2->getAll(space);
390 391
        ASSERT_TRUE(options1) << "failed for option space " << space;
        ASSERT_TRUE(options2) << "failed for option space " << space;
392

393 394 395
        // If number of options doesn't match, the test fails.
        ASSERT_EQ(options1->size(), options2->size())
            << "failed for option space " << space;
396

397
        // Iterate over all options within this option space.
Andrei Pavel's avatar
Andrei Pavel committed
398
        BOOST_FOREACH (OptionDescriptor desc1, *options1) {
399
            OptionDescriptor desc2 = cfg2->get(space, desc1.option_->getType());
400 401
            // Compare persistent flag.
            EXPECT_EQ(desc1.persistent_, desc2.persistent_)
Andrei Pavel's avatar
Andrei Pavel committed
402 403
                << "failed for option " << space << "."
                << desc1.option_->getType();
404 405
            // Compare formatted value.
            EXPECT_EQ(desc1.formatted_value_, desc2.formatted_value_)
Andrei Pavel's avatar
Andrei Pavel committed
406 407
                << "failed for option " << space << "."
                << desc1.option_->getType();
408 409

            // Retrieve options.
410 411 412
            Option* option1 = desc1.option_.get();
            Option* option2 = desc2.option_.get();

413 414 415
            // Options must be represented by the same C++ class derived from
            // the Option class.
            EXPECT_TRUE(typeid(*option1) == typeid(*option2))
Andrei Pavel's avatar
Andrei Pavel committed
416
                << "Compared DHCP options, having option code "
Andrei Pavel's avatar
Andrei Pavel committed
417 418 419 420
                << desc1.option_->getType() << " and belonging to the " << space
                << " option space, are represented "
                   "by different C++ classes: "
                << typeid(*option1).name() << " vs " << typeid(*option2).name();
421

422 423 424
            // Because we use different C++ classes to represent different
            // options, the simplest way to make sure that the options are
            // equal is to simply compare them in wire format.
425 426 427 428 429
            OutputBuffer buf1(option1->len());
            ASSERT_NO_THROW(option1->pack(buf1));
            OutputBuffer buf2(option2->len());
            ASSERT_NO_THROW(option2->pack(buf2));

430
            ASSERT_EQ(buf1.getLength(), buf2.getLength())
Andrei Pavel's avatar
Andrei Pavel committed
431 432 433 434 435 436
                << "failed for option " << space << "."
                << desc1.option_->getType();
            EXPECT_EQ(0,
                      memcmp(buf1.getData(), buf2.getData(), buf1.getLength()))
                << "failed for option " << space << "."
                << desc1.option_->getType();
437 438 439 440
        }
    }
}

441 442 443 444 445 446 447 448 449
OptionDescriptor
GenericHostDataSourceTest::createEmptyOption(const Option::Universe& universe,
                                             const uint16_t option_type,
                                             const bool persist) const {
    OptionPtr option(new Option(universe, option_type));
    OptionDescriptor desc(option, persist);
    return (desc);
}

450 451 452
OptionDescriptor
GenericHostDataSourceTest::createVendorOption(const Option::Universe& universe,
                                              const bool persist,
453
                                              const bool formatted,
454 455
                                              const uint32_t vendor_id) const {
    OptionVendorPtr option(new OptionVendor(universe, vendor_id));
456 457 458

    std::ostringstream s;
    if (formatted) {
459 460
        // Vendor id comprises vendor-id field, for which we need to
        // assign a value in the textual (formatted) format.
461 462 463 464
        s << vendor_id;
    }

    OptionDescriptor desc(option, persist, s.str());
465 466 467 468
    return (desc);
}

void
Andrei Pavel's avatar
Andrei Pavel committed
469 470 471 472
GenericHostDataSourceTest::addTestOptions(
    const HostPtr& host,
    const bool formatted,
    const AddedOptions& added_options) const {
473 474
    OptionDefSpaceContainer defs;

475 476 477 478 479 480 481 482 483
    if ((added_options == DHCP4_ONLY) || (added_options == DHCP4_AND_DHCP6)) {
        // Add DHCPv4 options.
        CfgOptionPtr opts = host->getCfgOption4();
        opts->add(createOption<OptionString>(Option::V4, DHO_BOOT_FILE_NAME,
                                             true, formatted, "my-boot-file"),
                  DHCP4_OPTION_SPACE);
        opts->add(createOption<OptionUint8>(Option::V4, DHO_DEFAULT_IP_TTL,
                                            false, formatted, 64),
                  DHCP4_OPTION_SPACE);
Andrei Pavel's avatar
Andrei Pavel committed
484 485 486 487 488
        opts->add(
            createOption<OptionUint32>(Option::V4, 1, false, formatted, 312131),
            "vendor-encapsulated-options");
        opts->add(createAddressOption<Option4AddrLst>(254, false, formatted,
                                                      "192.0.2.3"),
489 490
                  DHCP4_OPTION_SPACE);
        opts->add(createEmptyOption(Option::V4, 1, true), "isc");
Andrei Pavel's avatar
Andrei Pavel committed
491 492
        opts->add(createAddressOption<Option4AddrLst>(
                      2, false, formatted, "10.0.0.5", "10.0.0.3", "10.0.3.4"),
493 494 495
                  "isc");

        // Add definitions for DHCPv4 non-standard options.
Andrei Pavel's avatar
Andrei Pavel committed
496 497
        defs.addItem(OptionDefinitionPtr(new OptionDefinition(
                         "vendor-encapsulated-1", 1, "uint32")),
498
                     "vendor-encapsulated-options");
Andrei Pavel's avatar
Andrei Pavel committed
499 500
        defs.addItem(OptionDefinitionPtr(new OptionDefinition(
                         "option-254", 254, "ipv4-address", true)),
501
                     DHCP4_OPTION_SPACE);
Andrei Pavel's avatar
Andrei Pavel committed
502 503 504 505 506
        defs.addItem(
            OptionDefinitionPtr(new OptionDefinition("isc-1", 1, "empty")),
            "isc");
        defs.addItem(OptionDefinitionPtr(new OptionDefinition(
                         "isc-2", 2, "ipv4-address", true)),
507 508 509 510 511 512
                     "isc");
    }

    if ((added_options == DHCP6_ONLY) || (added_options == DHCP4_AND_DHCP6)) {
        // Add DHCPv6 options.
        CfgOptionPtr opts = host->getCfgOption6();
Andrei Pavel's avatar
Andrei Pavel committed
513 514
        opts->add(createOption<OptionString>(Option::V6, D6O_BOOTFILE_URL, true,
                                             formatted, "my-boot-file"),
515
                  DHCP6_OPTION_SPACE);
Andrei Pavel's avatar
Andrei Pavel committed
516 517
        opts->add(createOption<OptionUint32>(Option::V6,
                                             D6O_INFORMATION_REFRESH_TIME,
518 519 520 521 522 523 524 525
                                             false, formatted, 3600),
                  DHCP6_OPTION_SPACE);
        opts->add(createVendorOption(Option::V6, false, formatted, 2495),
                  DHCP6_OPTION_SPACE);
        opts->add(createAddressOption<Option6AddrLst>(1024, false, formatted,
                                                      "2001:db8:1::1"),
                  DHCP6_OPTION_SPACE);
        opts->add(createEmptyOption(Option::V6, 1, true), "isc2");
Andrei Pavel's avatar
Andrei Pavel committed
526 527
        opts->add(createAddressOption<Option6AddrLst>(
                      2, false, formatted, "3000::1", "3000::2", "3000::3"),
528 529 530
                  "isc2");

        // Add definitions for DHCPv6 non-standard options.
Andrei Pavel's avatar
Andrei Pavel committed
531 532
        defs.addItem(OptionDefinitionPtr(new OptionDefinition(
                         "option-1024", 1024, "ipv6-address", true)),
533
                     DHCP6_OPTION_SPACE);
Andrei Pavel's avatar
Andrei Pavel committed
534 535 536 537 538
        defs.addItem(
            OptionDefinitionPtr(new OptionDefinition("option-1", 1, "empty")),
            "isc2");
        defs.addItem(OptionDefinitionPtr(new OptionDefinition(
                         "option-2", 2, "ipv6-address", true)),
539 540
                     "isc2");
    }
541

542 543 544
    // Register created "runtime" option definitions. They will be used by a
    // host data source to convert option data into the appropriate option
    // classes when the options are retrieved.
545 546 547
    LibDHCP::setRuntimeOptionDefs(defs);
}

548 549 550 551 552
void
GenericHostDataSourceTest::testReadOnlyDatabase(const char* valid_db_type) {
    ASSERT_TRUE(hdsptr_);

    // The database is initially opened in "read-write" mode. We can
Josh Soref's avatar
Josh Soref committed
553
    // insert some data to the database.
554
    HostPtr host = initializeHost6("2001:db8::1", Host::IDENT_DUID, false);
555
    ASSERT_TRUE(host);
556 557 558 559 560 561 562
    ASSERT_NO_THROW(hdsptr_->add(host));

    // Subnet id will be used in queries to the database.
    SubnetID subnet_id = host->getIPv6SubnetID();

    // Make sure that the host has been inserted and that the data can be
    // retrieved.
Andrei Pavel's avatar
Andrei Pavel committed
563 564 565
    ConstHostPtr host_by_id =
        hdsptr_->get6(subnet_id, host->getIdentifierType(),
                      &host->getIdentifier()[0], host->getIdentifier().size());
566
    ASSERT_TRUE(host_by_id);
567 568 569 570 571
    ASSERT_NO_FATAL_FAILURE(compareHosts(host, host_by_id));

    // Close the database connection and reopen in "read-only" mode as
    // specified by the "VALID_READONLY_DB" parameter.
    HostDataSourceFactory::destroy();
Andrei Pavel's avatar
Andrei Pavel committed
572 573 574
    HostDataSourceFactory::create(connectionString(
        valid_db_type, VALID_NAME, VALID_HOST, VALID_READONLY_USER,
        VALID_PASSWORD, VALID_READONLY_DB));
575

576 577
    hdsptr_ = HostDataSourceFactory::getHostDataSourcePtr();

578 579
    // Check that an attempt to insert new host would result in
    // exception.
580 581 582 583 584
    HostPtr host2 = initializeHost6("2001:db8::2", Host::IDENT_DUID, false);
    ASSERT_TRUE(host2);
    ASSERT_THROW(hdsptr_->add(host2), ReadOnlyDb);
    ASSERT_THROW(hdsptr_->commit(), ReadOnlyDb);
    ASSERT_THROW(hdsptr_->rollback(), ReadOnlyDb);
585 586

    // Reading from the database should still be possible, though.
Andrei Pavel's avatar
Andrei Pavel committed
587 588 589
    host_by_id =
        hdsptr_->get6(subnet_id, host->getIdentifierType(),
                      &host->getIdentifier()[0], host->getIdentifier().size());
590
    ASSERT_TRUE(host_by_id);
591 592 593
    ASSERT_NO_FATAL_FAILURE(compareHosts(host, host_by_id));
}

Andrei Pavel's avatar
Andrei Pavel committed
594 595
void
GenericHostDataSourceTest::testBasic4(const Host::IdentifierType& id) {
596 597 598 599
    // Make sure we have the pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

    // Create a host reservation.
600
    HostPtr host = initializeHost4("192.0.2.1", id);
Andrei Pavel's avatar
Andrei Pavel committed
601
    ASSERT_TRUE(host);  // Make sure the host is generate properly.
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
    SubnetID subnet = host->getIPv4SubnetID();

    // Try to add it to the host data source.
    ASSERT_NO_THROW(hdsptr_->add(host));

    // This should not return anything
    ConstHostPtr from_hds = hdsptr_->get4(subnet, IOAddress("10.10.10.10"));
    ASSERT_FALSE(from_hds);

    // This time it should return a host
    from_hds = hdsptr_->get4(subnet, IOAddress("192.0.2.1"));
    ASSERT_TRUE(from_hds);

    // Finally, let's check if what we got makes any sense.
    compareHosts(host, from_hds);
}

Andrei Pavel's avatar
Andrei Pavel committed
619 620
void
GenericHostDataSourceTest::testGetByIPv4(const Host::IdentifierType& id) {
621 622 623 624
    // Make sure we have a pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

    // Let's create a couple of hosts...
625 626 627 628
    HostPtr host1 = initializeHost4("192.0.2.1", id);
    HostPtr host2 = initializeHost4("192.0.2.2", id);
    HostPtr host3 = initializeHost4("192.0.2.3", id);
    HostPtr host4 = initializeHost4("192.0.2.4", id);
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663

    // ... and add them to the data source.
    ASSERT_NO_THROW(hdsptr_->add(host1));
    ASSERT_NO_THROW(hdsptr_->add(host2));
    ASSERT_NO_THROW(hdsptr_->add(host3));
    ASSERT_NO_THROW(hdsptr_->add(host4));

    SubnetID subnet1 = host1->getIPv4SubnetID();
    SubnetID subnet2 = host2->getIPv4SubnetID();
    SubnetID subnet3 = host3->getIPv4SubnetID();
    SubnetID subnet4 = host4->getIPv4SubnetID();

    // And then try to retrieve them back.
    ConstHostPtr from_hds1 = hdsptr_->get4(subnet1, IOAddress("192.0.2.1"));
    ConstHostPtr from_hds2 = hdsptr_->get4(subnet2, IOAddress("192.0.2.2"));
    ConstHostPtr from_hds3 = hdsptr_->get4(subnet3, IOAddress("192.0.2.3"));
    ConstHostPtr from_hds4 = hdsptr_->get4(subnet4, IOAddress("192.0.2.4"));

    // Make sure we got something back.
    ASSERT_TRUE(from_hds1);
    ASSERT_TRUE(from_hds2);
    ASSERT_TRUE(from_hds3);
    ASSERT_TRUE(from_hds4);

    // Then let's check that what we got seems correct.
    compareHosts(host1, from_hds1);
    compareHosts(host2, from_hds2);
    compareHosts(host3, from_hds3);
    compareHosts(host4, from_hds4);

    // Ok, finally let's check that getting by a different address
    // will not work.
    EXPECT_FALSE(hdsptr_->get4(subnet1, IOAddress("192.0.1.5")));
}

664
void
Andrei Pavel's avatar
Andrei Pavel committed
665 666
GenericHostDataSourceTest::testGet4ByIdentifier(
    const Host::IdentifierType& identifier_type) {
667 668 669
    // Make sure we have a pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

670 671
    HostPtr host1 = initializeHost4("192.0.2.1", identifier_type);
    HostPtr host2 = initializeHost4("192.0.2.2", identifier_type);
672

673 674
    // Sanity check: make sure the hosts have different identifiers..
    ASSERT_FALSE(host1->getIdentifier() == host2->getIdentifier());
675 676 677 678 679 680 681 682

    // Try to add both of them to the host data source.
    ASSERT_NO_THROW(hdsptr_->add(host1));
    ASSERT_NO_THROW(hdsptr_->add(host2));

    SubnetID subnet1 = host1->getIPv4SubnetID();
    SubnetID subnet2 = host2->getIPv4SubnetID();

Andrei Pavel's avatar
Andrei Pavel committed
683 684 685
    ConstHostPtr from_hds1 =
        hdsptr_->get4(subnet1, identifier_type, &host1->getIdentifier()[0],
                      host1->getIdentifier().size());
686

Andrei Pavel's avatar
Andrei Pavel committed
687 688 689
    ConstHostPtr from_hds2 =
        hdsptr_->get4(subnet2, identifier_type, &host2->getIdentifier()[0],
                      host2->getIdentifier().size());
690 691 692 693 694 695 696 697

    // Now let's check if we got what we expected.
    ASSERT_TRUE(from_hds1);
    ASSERT_TRUE(from_hds2);
    compareHosts(host1, from_hds1);
    compareHosts(host2, from_hds2);
}

Andrei Pavel's avatar
Andrei Pavel committed
698 699
void
GenericHostDataSourceTest::testHWAddrNotClientId() {
700 701 702 703
    // Make sure we have a pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

    // Create a host with HW address
704
    HostPtr host = initializeHost4("192.0.2.1", Host::IDENT_HWADDR);
705 706 707 708 709 710 711 712 713 714 715
    ASSERT_TRUE(host->getHWAddress());
    ASSERT_FALSE(host->getDuid());

    // Try to add it to the host data source.
    ASSERT_NO_THROW(hdsptr_->add(host));

    SubnetID subnet = host->getIPv4SubnetID();

    DuidPtr duid = HWAddrToDuid(host->getHWAddress());

    // Get the host by HW address (should succeed)
Andrei Pavel's avatar
Andrei Pavel committed
716 717 718
    ConstHostPtr by_hwaddr =
        hdsptr_->get4(subnet, Host::IDENT_HWADDR, &host->getIdentifier()[0],
                      host->getIdentifier().size());
719 720

    // Get the host by DUID (should fail)
Andrei Pavel's avatar
Andrei Pavel committed
721 722 723
    ConstHostPtr by_duid =
        hdsptr_->get4(subnet, Host::IDENT_DUID, &host->getIdentifier()[0],
                      host->getIdentifier().size());
724 725 726 727 728 729

    // Now let's check if we got what we expected.
    EXPECT_TRUE(by_hwaddr);
    EXPECT_FALSE(by_duid);
}

Andrei Pavel's avatar
Andrei Pavel committed
730 731
void
GenericHostDataSourceTest::testClientIdNotHWAddr() {
732 733 734 735
    // Make sure we have a pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

    // Create a host with client-id
736
    HostPtr host = initializeHost4("192.0.2.1", Host::IDENT_DUID);
737 738 739 740 741 742 743 744 745 746 747
    ASSERT_FALSE(host->getHWAddress());
    ASSERT_TRUE(host->getDuid());

    // Try to add it to the host data source.
    ASSERT_NO_THROW(hdsptr_->add(host));

    SubnetID subnet = host->getIPv4SubnetID();

    HWAddrPtr hwaddr = DuidToHWAddr(host->getDuid());

    // Get the host by DUID (should succeed)
Andrei Pavel's avatar
Andrei Pavel committed
748 749 750
    ConstHostPtr by_duid =
        hdsptr_->get4(subnet, Host::IDENT_DUID, &host->getIdentifier()[0],
                      host->getIdentifier().size());
751 752

    // Get the host by HW address (should fail)
Andrei Pavel's avatar
Andrei Pavel committed
753 754 755
    ConstHostPtr by_hwaddr =
        hdsptr_->get4(subnet, Host::IDENT_HWADDR, &host->getIdentifier()[0],
                      host->getIdentifier().size());
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776

    // Now let's check if we got what we expected.
    EXPECT_TRUE(by_duid);
    EXPECT_FALSE(by_hwaddr);
}

void
GenericHostDataSourceTest::testHostname(std::string name, int num) {
    // Make sure we have a pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

    // Initialize the address to 192.0.2.0 (this will be bumped
    // up to 192.0.2.1 in the first iteration)
    IOAddress addr("192.0.2.0");

    vector<HostPtr> hosts;

    // Prepare a vector of hosts with unique hostnames
    for (int i = 0; i < num; ++i) {
        addr = IOAddress::increase(addr);

777
        HostPtr host = initializeHost4(addr.toText(), Host::IDENT_DUID);
778 779 780 781 782 783 784 785 786 787 788 789 790

        stringstream hostname;
        hostname.str("");
        if (num > 1) {
            hostname << i;
        }
        hostname << name;
        host->setHostname(hostname.str());

        hosts.push_back(host);
    }

    // Now add them all to the host data source.
Andrei Pavel's avatar
Andrei Pavel committed
791 792
    for (vector<HostPtr>::const_iterator it = hosts.begin(); it != hosts.end();
         ++it) {
793 794 795 796 797 798
        // Try to add both of the to the host data source.
        ASSERT_NO_THROW(hdsptr_->add(*it));
    }

    // And finally retrieve them one by one and check
    // if the hostname was preserved.
Andrei Pavel's avatar
Andrei Pavel committed
799 800
    for (vector<HostPtr>::const_iterator it = hosts.begin(); it != hosts.end();
         ++it) {
801
        ConstHostPtr from_hds;
Andrei Pavel's avatar
Andrei Pavel committed
802 803
        ASSERT_NO_THROW(from_hds = hdsptr_->get4((*it)->getIPv4SubnetID(),
                                                 (*it)->getIPv4Reservation()));
804 805 806 807 808 809 810
        ASSERT_TRUE(from_hds);

        EXPECT_EQ((*it)->getHostname(), from_hds->getHostname());
    }
}

void
811 812
GenericHostDataSourceTest::testMultipleSubnets(int subnets,
                                               const Host::IdentifierType& id) {
813 814 815
    // Make sure we have a pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

816
    HostPtr host = initializeHost4("192.0.2.1", id);
817 818 819 820 821 822 823 824 825 826 827 828 829

    for (int i = 0; i < subnets; ++i) {
        host->setIPv4SubnetID(i + 1000);
        host->setIPv6SubnetID(i + 1000);

        // Check that the same host can have reservations in multiple subnets.
        EXPECT_NO_THROW(hdsptr_->add(host));
    }

    // Now check that the reservations can be retrieved by IPv4 address from
    // each subnet separately.
    for (int i = 0; i < subnets; ++i) {
        // Try to retrieve the host by IPv4 address.
Andrei Pavel's avatar
Andrei Pavel committed
830 831
        ConstHostPtr from_hds =
            hdsptr_->get4(i + 1000, host->getIPv4Reservation());
832 833 834 835 836

        ASSERT_TRUE(from_hds);
        EXPECT_EQ(i + 1000, from_hds->getIPv4SubnetID());

        // Try to retrieve the host by either HW address of client-id
Andrei Pavel's avatar
Andrei Pavel committed
837
        from_hds = hdsptr_->get4(i + 1000, id, &host->getIdentifier()[0],
838
                                 host->getIdentifier().size());
839 840 841 842 843 844 845 846 847 848
        ASSERT_TRUE(from_hds);
        EXPECT_EQ(i + 1000, from_hds->getIPv4SubnetID());
    }

    // Now check that they can be retrieved all at once, by IPv4 address.
    ConstHostCollection all_by_addr = hdsptr_->getAll4(IOAddress("192.0.2.1"));
    ASSERT_EQ(subnets, all_by_addr.size());

    // Verify that the values returned are proper.
    int i = 0;
Andrei Pavel's avatar
Andrei Pavel committed
849 850 851 852 853 854
    if (hdsptr_->getType() == "cql") {
        // There is no ORDER BY in Cassandra. Order here. Remove this if entries
        // are eventually implemented as ordered in the Cassandra host data
        // source.
        std::sort(all_by_addr.begin(), all_by_addr.end(), compareHostsForSort4);
    }
855 856 857 858 859 860
    for (ConstHostCollection::const_iterator it = all_by_addr.begin();
         it != all_by_addr.end(); ++it) {
        EXPECT_EQ(IOAddress("192.0.2.1"), (*it)->getIPv4Reservation());
        EXPECT_EQ(1000 + i++, (*it)->getIPv4SubnetID());
    }

Andrei Pavel's avatar
Andrei Pavel committed
861
    // Finally, check that the hosts can be retrieved by HW address or DUID
Andrei Pavel's avatar
Andrei Pavel committed
862 863
    ConstHostCollection all_by_id = hdsptr_->getAll(
        id, &host->getIdentifier()[0], host->getIdentifier().size());
864 865 866 867
    ASSERT_EQ(subnets, all_by_id.size());

    // Check that the returned values are as expected.
    i = 0;
Andrei Pavel's avatar
Andrei Pavel committed
868 869 870 871 872 873
    if (hdsptr_->getType() == "cql") {
        // There is no ORDER BY in Cassandra. Order here. Remove this if entries
        // are eventually implemented as ordered in the Cassandra host data
        // source.
        std::sort(all_by_id.begin(), all_by_id.end(), compareHostsForSort4);
    }
874 875 876 877 878 879 880
    for (ConstHostCollection::const_iterator it = all_by_id.begin();
         it != all_by_id.end(); ++it) {
        EXPECT_EQ(IOAddress("192.0.2.1"), (*it)->getIPv4Reservation());
        EXPECT_EQ(1000 + i++, (*it)->getIPv4SubnetID());
    }
}

Andrei Pavel's avatar
Andrei Pavel committed
881 882
void
GenericHostDataSourceTest::testGet6ByHWAddr() {
883 884 885 886
    // Make sure we have the pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

    // Create a host reservations.
887 888
    HostPtr host1 = initializeHost6("2001:db8::1", Host::IDENT_HWADDR, false);
    HostPtr host2 = initializeHost6("2001:db8::2", Host::IDENT_HWADDR, false);
889 890 891 892 893 894 895 896 897 898 899 900 901 902

    // Sanity check: make sure the hosts have different HW addresses.
    ASSERT_TRUE(host1->getHWAddress());
    ASSERT_TRUE(host2->getHWAddress());

    compareHwaddrs(host1, host2, false);

    // Try to add both of them to the host data source.
    ASSERT_NO_THROW(hdsptr_->add(host1));
    ASSERT_NO_THROW(hdsptr_->add(host2));

    SubnetID subnet1 = host1->getIPv6SubnetID();
    SubnetID subnet2 = host2->getIPv6SubnetID();

Andrei Pavel's avatar
Andrei Pavel committed
903 904 905
    ConstHostPtr from_hds1 =
        hdsptr_->get6(subnet1, Host::IDENT_HWADDR, &host1->getIdentifier()[0],
                      host1->getIdentifier().size());
906

Andrei Pavel's avatar
Andrei Pavel committed
907 908 909
    ConstHostPtr from_hds2 =
        hdsptr_->get6(subnet2, Host::IDENT_HWADDR, &host2->getIdentifier()[0],
                      host2->getIdentifier().size());
910 911 912 913 914 915 916 917

    // Now let's check if we got what we expected.
    ASSERT_TRUE(from_hds1);
    ASSERT_TRUE(from_hds2);
    compareHosts(host1, from_hds1);
    compareHosts(host2, from_hds2);
}

Andrei Pavel's avatar
Andrei Pavel committed
918 919
void
GenericHostDataSourceTest::testGet6ByClientId() {
920 921 922 923
    // Make sure we have the pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

    // Create a host reservations.
924 925
    HostPtr host1 = initializeHost6("2001:db8::1", Host::IDENT_DUID, false);
    HostPtr host2 = initializeHost6("2001:db8::2", Host::IDENT_DUID, false);
926 927 928 929 930 931 932 933 934 935 936 937 938 939

    // Sanity check: make sure the hosts have different HW addresses.
    ASSERT_TRUE(host1->getDuid());
    ASSERT_TRUE(host2->getDuid());

    compareDuids(host1, host2, false);

    // Try to add both of them to the host data source.
    ASSERT_NO_THROW(hdsptr_->add(host1));
    ASSERT_NO_THROW(hdsptr_->add(host2));

    SubnetID subnet1 = host1->getIPv6SubnetID();
    SubnetID subnet2 = host2->getIPv6SubnetID();

Andrei Pavel's avatar
Andrei Pavel committed
940 941 942
    ConstHostPtr from_hds1 =
        hdsptr_->get6(subnet1, Host::IDENT_DUID, &host1->getIdentifier()[0],
                      host1->getIdentifier().size());
943

Andrei Pavel's avatar
Andrei Pavel committed
944 945 946
    ConstHostPtr from_hds2 =
        hdsptr_->get6(subnet2, Host::IDENT_DUID, &host2->getIdentifier()[0],
                      host2->getIdentifier().size());
947 948 949 950 951 952 953 954 955

    // Now let's check if we got what we expected.
    ASSERT_TRUE(from_hds1);
    ASSERT_TRUE(from_hds2);
    compareHosts(host1, from_hds1);
    compareHosts(host2, from_hds2);
}

void
956
GenericHostDataSourceTest::testSubnetId6(int subnets, Host::IdentifierType id) {
957 958 959
    // Make sure we have a pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

960 961
    HostPtr host;
    IOAddress current_address("2001:db8::0");
962
    for (int i = 0; i < subnets; ++i) {
963 964 965 966
        // Last boolean value set to false indicates that the same identifier
        // must be used for each generated host.
        host = initializeHost6(current_address.toText(), id, true, false);

967 968 969 970 971
        host->setIPv4SubnetID(i + 1000);
        host->setIPv6SubnetID(i + 1000);

        // Check that the same host can have reservations in multiple subnets.
        EXPECT_NO_THROW(hdsptr_->add(host));
972 973 974 975

        // Increase address to make sure we don't assign the same address
        // in different subnets.
        current_address = IOAddress::increase(current_address);
976 977 978 979 980
    }

    // Check that the reservations can be retrieved from each subnet separately.
    for (int i = 0; i < subnets; ++i) {
        // Try to retrieve the host
Andrei Pavel's avatar
Andrei Pavel committed
981 982 983
        ConstHostPtr from_hds =
            hdsptr_->get6(i + 1000, id, &host->getIdentifier()[0],
                          host->getIdentifier().size());
984

985
        ASSERT_TRUE(from_hds) << "failed for i=" << i;
986 987 988
        EXPECT_EQ(i + 1000, from_hds->getIPv6SubnetID());
    }

Andrei Pavel's avatar
Andrei Pavel committed
989
    // Check that the hosts can all be retrieved by HW address or DUID
Andrei Pavel's avatar
Andrei Pavel committed
990 991
    ConstHostCollection all_by_id = hdsptr_->getAll(
        id, &host->getIdentifier()[0], host->getIdentifier().size());
992 993 994 995
    ASSERT_EQ(subnets, all_by_id.size());

    // Check that the returned values are as expected.
    int i = 0;
Andrei Pavel's avatar
Andrei Pavel committed
996 997 998 999 1000
    if (hdsptr_->getType() == "cql") {
        // There is no ORDER BY in Cassandra. Order here. Remove this if entries
        // are implemented as ordered in the Cassandra host data source.
        std::sort(all_by_id.begin(), all_by_id.end(), compareHostsForSort6);
    }
1001 1002 1003 1004 1005 1006 1007
    for (ConstHostCollection::const_iterator it = all_by_id.begin();
         it != all_by_id.end(); ++it) {
        EXPECT_EQ(IOAddress("0.0.0.0"), (*it)->getIPv4Reservation());
        EXPECT_EQ(1000 + i++, (*it)->getIPv6SubnetID());
    }
}

Andrei Pavel's avatar
Andrei Pavel committed
1008 1009
void
GenericHostDataSourceTest::testGetByIPv6(Host::IdentifierType id, bool prefix) {
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    // Make sure we have a pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

    // Let's create a couple of hosts...
    HostPtr host1 = initializeHost6("2001:db8::1", id, prefix);
    HostPtr host2 = initializeHost6("2001:db8::2", id, prefix);
    HostPtr host3 = initializeHost6("2001:db8::3", id, prefix);
    HostPtr host4 = initializeHost6("2001:db8::4", id, prefix);

    // ... and add them to the data source.
    ASSERT_NO_THROW(hdsptr_->add(host1));
    ASSERT_NO_THROW(hdsptr_->add(host2));
    ASSERT_NO_THROW(hdsptr_->add(host3));
    ASSERT_NO_THROW(hdsptr_->add(host4));

    // Are we talking about addresses or prefixes?
    uint8_t len = prefix ? 64 : 128;

    // And then try to retrieve them back.
    ConstHostPtr from_hds1 = hdsptr_->get6(IOAddress("2001:db8::1"), len);
    ConstHostPtr from_hds2 = hdsptr_->get6(IOAddress("2001:db8::2"), len);
    ConstHostPtr from_hds3 = hdsptr_->get6(IOAddress("2001:db8::3"), len);
    ConstHostPtr from_hds4 = hdsptr_->get6(IOAddress("2001:db8::4"), len);

    // Make sure we got something back.
    ASSERT_TRUE(from_hds1);
    ASSERT_TRUE(from_hds2);
    ASSERT_TRUE(from_hds3);
    ASSERT_TRUE(from_hds4);

    // Then let's check that what we got seems correct.
    compareHosts(host1, from_hds1);
    compareHosts(host2, from_hds2);
    compareHosts(host3, from_hds3);
    compareHosts(host4, from_hds4);

    // Ok, finally let's check that getting by a different address
    // will not work.
    EXPECT_FALSE(hdsptr_->get6(IOAddress("2001:db8::5"), len));
}

Andrei Pavel's avatar
Andrei Pavel committed
1051 1052
void
GenericHostDataSourceTest::testGetBySubnetIPv6() {
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    // Make sure we have a pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

    // Let's create a couple of hosts...
    HostPtr host1 = initializeHost6("2001:db8:1::", Host::IDENT_DUID, true);
    HostPtr host2 = initializeHost6("2001:db8:2::", Host::IDENT_DUID, true);
    HostPtr host3 = initializeHost6("2001:db8:3::", Host::IDENT_DUID, true);
    HostPtr host4 = initializeHost6("2001:db8:4::", Host::IDENT_DUID, true);

    // ... and add them to the data source.
    ASSERT_NO_THROW(hdsptr_->add(host1));
    ASSERT_NO_THROW(hdsptr_->add(host2));
    ASSERT_NO_THROW(hdsptr_->add(host3));
    ASSERT_NO_THROW(hdsptr_->add(host4));

    // And then try to retrieve them back.
Andrei Pavel's avatar
Andrei Pavel committed
1069 1070 1071 1072 1073 1074 1075 1076
    ConstHostPtr from_hds1 =
        hdsptr_->get6(host1->getIPv6SubnetID(), IOAddress("2001:db8:1::"));
    ConstHostPtr from_hds2 =
        hdsptr_->get6(host2->getIPv6SubnetID(), IOAddress("2001:db8:2::"));
    ConstHostPtr from_hds3 =
        hdsptr_->get6(host3->getIPv6SubnetID(), IOAddress("2001:db8:3::"));
    ConstHostPtr from_hds4 =
        hdsptr_->get6(host4->getIPv6SubnetID(), IOAddress("2001:db8:4::"));
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090

    // Make sure we got something back.
    ASSERT_TRUE(from_hds1);
    ASSERT_TRUE(from_hds2);
    ASSERT_TRUE(from_hds3);
    ASSERT_TRUE(from_hds4);

    // Then let's check that what we got seems correct.
    compareHosts(host1, from_hds1);
    compareHosts(host2, from_hds2);
    compareHosts(host3, from_hds3);
    compareHosts(host4, from_hds4);
}

Andrei Pavel's avatar
Andrei Pavel committed
1091 1092
void
GenericHostDataSourceTest::testAddDuplicate6WithSameDUID() {
1093 1094 1095 1096
    // Make sure we have the pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

    // Create a host reservations.
1097
    HostPtr host = initializeHost6("2001:db8::1", Host::IDENT_DUID, true);
1098 1099 1100 1101 1102 1103

    // Add this reservation once.
    ASSERT_NO_THROW(hdsptr_->add(host));

    // Then try to add it again, it should throw an exception.
    ASSERT_THROW(hdsptr_->add(host), DuplicateEntry);
1104 1105
}

Andrei Pavel's avatar
Andrei Pavel committed
1106 1107
void
GenericHostDataSourceTest::testAddDuplicate6WithSameHWAddr() {
1108 1109 1110 1111
    // Make sure we have the pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

    // Create a host reservations.
1112
    HostPtr host = initializeHost6("2001:db8::1", Host::IDENT_HWADDR, true);
1113 1114 1115 1116 1117 1118

    // Add this reservation once.
    ASSERT_NO_THROW(hdsptr_->add(host));

    // Then try to add it again, it should throw an exception.
    ASSERT_THROW(hdsptr_->add(host), DuplicateEntry);
1119 1120
}

Andrei Pavel's avatar
Andrei Pavel committed
1121 1122
void
GenericHostDataSourceTest::testAddDuplicate4() {
1123 1124 1125 1126
    // Make sure we have the pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

    // Create a host reservations.
1127
    HostPtr host = initializeHost4("192.0.2.1", Host::IDENT_DUID);
1128 1129 1130 1131 1132 1133

    // Add this reservation once.
    ASSERT_NO_THROW(hdsptr_->add(host));

    // Then try to add it again, it should throw an exception.
    ASSERT_THROW(hdsptr_->add(host), DuplicateEntry);
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144

    // This time use a different host identifier and try again.
    // This update should be rejected because of duplicated
    // address.
    ASSERT_NO_THROW(host->setIdentifier("01:02:03:04:05:06", "hw-address"));
    ASSERT_THROW(hdsptr_->add(host), DuplicateEntry);

    // Modify address to avoid its duplication and make sure
    // we can now add the host.
    ASSERT_NO_THROW(host->setIPv4Reservation(IOAddress("192.0.2.3")));
    EXPECT_NO_THROW(hdsptr_->add(host));
1145 1146
}

Andrei Pavel's avatar
Andrei Pavel committed
1147 1148
void
GenericHostDataSourceTest::testAddr6AndPrefix() {
1149 1150 1151 1152
    // Make sure we have the pointer to the host data source.
    ASSERT_TRUE(hdsptr_);

    // Create a host reservations with prefix reservation (prefix = true)
1153
    HostPtr host = initializeHost6("2001:db8::1", Host::IDENT_DUID, true);
1154 1155 1156 1157 1158 1159 1160 1161 1162

    // Create IPv6 reservation (for an address) and add it to the host
    IPv6Resrv resv(IPv6Resrv::TYPE_NA, IOAddress("2001:db8::2"), 128);
    host->addReservation(resv);

    // Add this reservation
    ASSERT_NO_THROW(hdsptr_->add(host));

    // Get this host by DUID
Andrei Pavel's avatar
Andrei Pavel committed
1163 1164 1165
    ConstHostPtr from_hds =
        hdsptr_->get6(host->getIPv6SubnetID(), Host::IDENT_DUID,
                      &host->getIdentifier()[0], host->getIdentifier().size());
1166 1167 1168 1169 1170

    // Make sure we got something back
    ASSERT_TRUE(from_hds);

    // Check if reservations are the same
1171 1172
    compareReservations6(host->getIPv6Reservations(),
                         from_hds->getIPv6Reservations());
1173 1174
}

Andrei Pavel's avatar
Andrei Pavel committed
1175 1176
void
GenericHostDataSourceTest::testMultipleReservations() {
1177 1178 1179 1180
    // Make sure we have the pointer to the host data source.
    ASSERT_TRUE(hdsptr_);
    uint8_t len = 128;

1181
    HostPtr host = initializeHost6("2001:db8::1", Host::IDENT_DUID, false);
1182 1183 1184 1185 1186

    // Add some reservations
    IPv6Resrv resv1(IPv6Resrv::TYPE_NA, IOAddress("2001:db8::6"), len);
    IPv6Resrv resv2(IPv6Resrv::TYPE_NA, IOAddress("2001:db8::7"), len);
    IPv6Resrv resv3(IPv6Resrv::TYPE_NA, IOAddress("2001:db8::8"), len);
1187
    IPv6Resrv resv4(IPv6Resrv::TYPE_NA, IOAddress("2001:db8::9"), len);
1188 1189 1190 1191

    host->addReservation(resv1);
    host->addReservation(resv2);
    host->addReservation(resv3);
1192
    host->addReservation(resv4);
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202

    ASSERT_NO_THROW(hdsptr_->add(host));

    ConstHostPtr from_hds = hdsptr_->get6(IOAddress("2001:db8::1"), len);

    // Make sure we got something back
    ASSERT_TRUE(from_hds);

    // Check if hosts are the same
    compareHosts(host, from_hds);
1203 1204
}

Andrei Pavel's avatar
Andrei Pavel committed
1205 1206
void
GenericHostDataSourceTest::testMultipleReservationsDifferentOrder() {
1207 1208 1209 1210
    // Make sure we have the pointer to the host data source.
    ASSERT_TRUE(hdsptr_);
    uint8_t len = 128;

1211 1212
    HostPtr host1 = initializeHost6("2001:db8::1", Host::IDENT_DUID, false);
    HostPtr host2 = initializeHost6("2001:db8::1", Host::IDENT_DUID, false);
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 <