cfg_hosts_unittest.cc 24.9 KB
Newer Older
1
// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <config.h>
#include <asiolink/io_address.h>
#include <dhcp/duid.h>
#include <dhcp/hwaddr.h>
#include <dhcpsrv/cfg_hosts.h>
#include <dhcpsrv/host.h>
#include <gtest/gtest.h>
22
#include <sstream>
23
#include <set>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

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

namespace {

/// @brief Test fixture class for testing @c CfgHost object holding
/// host reservations specified in the configuration file.
class CfgHostsTest : public ::testing::Test {
public:

    /// @brief Constructor.
    ///
    /// This constructor allocates a collection of @c HWAddr and @c DuidPtr
    /// objects used by the unit tests.
    ///
    /// The allocated HW addresses use the following pattern: 01:02:0A:BB:03:XX
    /// where XX is a number between 0 and 0x32. All of them are of the
    /// HTYPE_ETHER type.
    ///
    /// The allocated DUID LLTs use the following pattern:
    /// 01:02:03:04:05:06:07:08:09:0A:XX where the XX is a number between
    /// 0 and 0x32.
    CfgHostsTest();

    /// @brief Increases last byte of an address.
    ///
    /// @param address Address to be increased.
    IOAddress increase(const IOAddress& address, const uint8_t num) const;

    /// @brief Collection of HW address objects allocated for unit tests.
    std::vector<HWAddrPtr> hwaddrs_;
    /// @brief Collection of DUIDs allocated for unit tests.
    std::vector<DuidPtr> duids_;
59 60 61
    /// @brief Collection of IPv4 address objects allocated for unit tests.
    std::vector<IOAddress> addressesa_;
    std::vector<IOAddress> addressesb_;
62 63 64 65 66 67
};

CfgHostsTest::CfgHostsTest() {
    const uint8_t mac_template[] = {
        0x01, 0x02, 0x0A, 0xBB, 0x03, 0x00
    };
68
    for (unsigned i = 0; i < 50; ++i) {
69 70 71 72 73 74 75 76 77 78
        std::vector<uint8_t> vec(mac_template,
                                 mac_template + sizeof(mac_template));
        vec[vec.size() - 1] = i;
        HWAddrPtr hwaddr(new HWAddr(vec, HTYPE_ETHER));
        hwaddrs_.push_back(hwaddr);
    }

    const uint8_t duid_template[] = {
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x00
    };
79
    for (unsigned i = 0; i < 50; ++i) {
80 81 82 83 84 85
        std::vector<uint8_t> vec(duid_template,
                                 duid_template + sizeof(mac_template));
        vec[vec.size() - 1] = i;
        DuidPtr duid(new DUID(vec));
        duids_.push_back(duid);
    }
86 87 88 89 90 91 92 93 94

    const uint32_t addra_template = 0xc0000205; // 192.0.2.5
    const uint32_t addrb_template = 0xc00a020a; // 192.10.2.10
    for (int i = 0; i < 50; ++i) {
        IOAddress addra(addra_template + i);
        addressesa_.push_back(addra);
        IOAddress addrb(addrb_template + i);
        addressesb_.push_back(addrb);
    }
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
}

IOAddress
CfgHostsTest::increase(const IOAddress& address, const uint8_t num) const {
    std::vector<uint8_t> vec = address.toBytes();
    if (!vec.empty()) {
        vec[vec.size() - 1] += num;
        return (IOAddress::fromBytes(address.getFamily(), &vec[0]));
    }
    return (address);
}

// This test checks that hosts with unique HW addresses and DUIDs can be
// retrieved from the host configuration.
TEST_F(CfgHostsTest, getAllNonRepeatingHosts) {
    CfgHosts cfg;
    // Add 25 hosts identified by HW address and 25 hosts identified by
    // DUID. They are added to different subnets.
    for (int i = 0; i < 25; ++i) {
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                 "hw-address",
116
                                 SubnetID(i % 10 + 1), SubnetID(i % 5 + 1),
117
                                 addressesa_[i])));
118 119

        cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
120
                                 SubnetID(i % 5 + 1), SubnetID(i % 10 + 1),
121
                                 addressesb_[i])));
122 123 124 125 126 127 128 129 130 131

    }

    // Try to retrieve each added reservation using HW address and DUID. Do it
    // in the reverse order to make sure that the order doesn't matter.
    for (int i = 24; i >= 0; --i) {
        // Get host identified by HW address. The DUID is non-zero but it
        // points to a host for which the reservation hasn't been added.
        HostCollection hosts = cfg.getAll(hwaddrs_[i], duids_[i + 25]);
        ASSERT_EQ(1, hosts.size());
132
        EXPECT_EQ(i % 10 + 1, hosts[0]->getIPv4SubnetID());
133 134
        EXPECT_EQ(addressesa_[i].toText(),
                  hosts[0]->getIPv4Reservation().toText());
135 136 137 138 139

        // Get host identified by DUID. The HW address is non-null but it
        // points to a host for which the reservation hasn't been added.
        hosts = cfg.getAll(hwaddrs_[i + 25], duids_[i]);
        ASSERT_EQ(1, hosts.size());
140
        EXPECT_EQ(i % 5 + 1, hosts[0]->getIPv4SubnetID());
141 142
        EXPECT_EQ(addressesb_[i].toText(),
                  hosts[0]->getIPv4Reservation().toText());
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    }

    // Make sure that the reservations do not exist for the hardware addresses
    // and DUIDs from the range of 25 to 49.
    for (int i = 49; i >= 25; --i) {
        EXPECT_TRUE(cfg.getAll(hwaddrs_[i]).empty());
        EXPECT_TRUE(cfg.getAll(HWAddrPtr(), duids_[i]).empty());
    }
}

// This test verifies that the host can be added to multiple subnets and
// that the getAll message retrieves all instances of the host.
TEST_F(CfgHostsTest, getAllRepeatingHosts) {
    CfgHosts cfg;
    // Add hosts.
    for (int i = 0; i < 25; ++i) {
159
        // Add two hosts, using the same HW address to two distinct subnets.
160 161 162
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                 "hw-address",
                                 SubnetID(1), SubnetID(2),
163
                                 addressesa_[i])));
164 165 166
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                 "hw-address",
                                 SubnetID(2), SubnetID(3),
167
                                 addressesb_[i])));
168

169
        // Add two hosts, using the same DUID to two distinct subnets.
170 171
        cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
                                 SubnetID(1), SubnetID(2),
172
                                 addressesb_[i])));
173 174
        cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
                                 SubnetID(2), SubnetID(3),
175
                                 addressesa_[i])));
176 177 178 179 180 181 182 183 184 185
    }

    // Verify that hosts can be retrieved.
    for (int i = 0; i < 25; ++i) {
        // Get host by HW address. The DUID is non-null but the reservation
        // should be returned for the HW address because there are no
        // reservations for the DUIDs from the range of 25 to 49.
        HostCollection hosts = cfg.getAll(hwaddrs_[i], duids_[i + 25]);
        ASSERT_EQ(2, hosts.size());
        EXPECT_EQ(1, hosts[0]->getIPv4SubnetID());
186
        EXPECT_EQ(addressesa_[i], hosts[0]->getIPv4Reservation().toText());
187
        EXPECT_EQ(2, hosts[1]->getIPv4SubnetID());
188
        EXPECT_EQ(addressesb_[i], hosts[1]->getIPv4Reservation().toText());
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

        // Get host by DUID. The HW address is non-null but the reservation
        // should be returned for the DUID because there are no
        // reservations for the HW addresses from the range of 25 to 49.
        hosts = cfg.getAll(hwaddrs_[i + 25], duids_[i]);
        ASSERT_EQ(2, hosts.size());
        EXPECT_EQ(1, hosts[0]->getIPv4SubnetID());
        EXPECT_EQ(2, hosts[1]->getIPv4SubnetID());
    }

    // The getAll function should return empty containers for the HW addresses
    //  and DUIDs for which the reservations haven't been added.
    for (int i = 25; i < 50; ++i) {
        EXPECT_TRUE(cfg.getAll(hwaddrs_[i]).empty());
        EXPECT_TRUE(cfg.getAll(HWAddrPtr(), duids_[i]).empty());
    }
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
// This test checks that all reservations for the specified IPv4 address can
// be retrieved.
TEST_F(CfgHostsTest, getAll4ByAddress) {
    CfgHosts cfg;
    // Add hosts.
    for (int i = 0; i < 25; ++i) {
        // Add host identified by the HW address.
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                 "hw-address",
                                 SubnetID(1 + i), SubnetID(0),
                                 IOAddress("192.0.2.5"))));
        // Add host identified by the DUID.
        cfg.add(HostPtr(new Host(duids_[i]->toText(),
                                 "duid",
                                 SubnetID(1 + i), SubnetID(0),
                                 IOAddress("192.0.2.10"))));
    }

    HostCollection hosts = cfg.getAll4(IOAddress("192.0.2.10"));
    std::set<uint32_t> subnet_ids;
    for (HostCollection::const_iterator host = hosts.begin(); host != hosts.end();
         ++host) {
        subnet_ids.insert((*host)->getIPv4SubnetID());
    }
    ASSERT_EQ(25, subnet_ids.size());
    EXPECT_EQ(1, *subnet_ids.begin());
    EXPECT_EQ(25, *subnet_ids.rbegin());
}

236 237 238 239 240
// This test checks that the reservations can be retrieved for the particular
// host connected to the specific IPv4 subnet (by subnet id).
TEST_F(CfgHostsTest, get4) {
    CfgHosts cfg;
    // Add hosts.
241
    for (unsigned i = 0; i < 25; ++i) {
242 243 244 245 246 247 248 249 250 251 252 253
        // Add host identified by HW address.
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                 "hw-address",
                                 SubnetID(1 + i % 2), SubnetID(13),
                                 increase(IOAddress("192.0.2.5"), i))));

        // Add host identified by DUID.
        cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
                                 SubnetID(1 + i % 2), SubnetID(13),
                                 increase(IOAddress("192.0.2.100"), i))));
    }

254
    for (unsigned i = 0; i < 25; ++i) {
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
        // Retrieve host by HW address. The DUID is non-null but there is no
        // reservation made for the DUID so the reservation is returned for
        // HW address.
        HostPtr host = cfg.get4(SubnetID(1 + i % 2), hwaddrs_[i],
                                duids_[i + 25]);
        ASSERT_TRUE(host);
        EXPECT_EQ(1 + i % 2, host->getIPv4SubnetID());
        EXPECT_EQ(increase(IOAddress("192.0.2.5"), i),
                  host->getIPv4Reservation());

        // Retrieve host by DUID. The HW address is non-null but there is no
        // reservation made for the HW address so the reservation is returned
        // for the DUID.
        host = cfg.get4(SubnetID(1 + i % 2), hwaddrs_[i + 25], duids_[i]);
        ASSERT_TRUE(host);
        EXPECT_EQ(1 + i % 2, host->getIPv4SubnetID());
        EXPECT_EQ(increase(IOAddress("192.0.2.100"), i),
                  host->getIPv4Reservation());

    }

    // Also check that when the get4 finds multiple Host objects that fulfil
    // search criteria, it will throw an exception. Note that the reservation
    // exist both for hwaddrs_[0] and duids_[0].
    EXPECT_THROW(cfg.get4(SubnetID(1), hwaddrs_[0], duids_[0]), DuplicateHost);
}

// This test checks that the reservations can be retrieved for the particular
// host connected to the specific IPv6 subnet (by subnet id).
TEST_F(CfgHostsTest, get6) {
    CfgHosts cfg;
    // Add hosts.
287
    for (unsigned i = 0; i < 25; ++i) {
288
        // Add host identified by HW address.
289 290 291 292
        HostPtr host = HostPtr(new Host(hwaddrs_[i]->toText(false),
                                        "hw-address",
                                        SubnetID(10), SubnetID(1 + i % 2),
                                        IOAddress("0.0.0.0")));
293 294
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress("2001:db8:1::1"),
295 296
                                                i)));
        cfg.add(host);
297 298

        // Add host identified by DUID.
299 300 301
        host = HostPtr(new Host(duids_[i]->toText(), "duid",
                                SubnetID(10), SubnetID(1 + i % 2),
                                IOAddress("0.0.0.0")));
302 303
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress("2001:db8:2::1"),
304 305
                                                i)));
        cfg.add(host);
306 307
    }

308
    for (unsigned i = 0; i < 25; ++i) {
309 310 311 312 313 314 315
        // Retrieve host by HW address. The DUID is non-null but there is no
        // reservation made for the DUID so the reservation is returned for
        // HW address.
        HostPtr host = cfg.get6(SubnetID(1 + i % 2), duids_[i + 25],
                                hwaddrs_[i]);
        ASSERT_TRUE(host);
        EXPECT_EQ(1 + i % 2, host->getIPv6SubnetID());
316 317 318
        IPv6ResrvRange reservations =
            host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
        ASSERT_EQ(1, std::distance(reservations.first, reservations.second));
319
        EXPECT_EQ(increase(IOAddress("2001:db8:1::1"), i),
320
                  reservations.first->second.getPrefix());
321 322 323 324 325 326 327

        // Retrieve host by DUID. The HW address is non-null but there is no
        // reservation made for the HW address so the reservation is returned
        // for the DUID.
        host = cfg.get6(SubnetID(1 + i % 2), duids_[i], hwaddrs_[i + 25]);
        ASSERT_TRUE(host);
        EXPECT_EQ(1 + i % 2, host->getIPv6SubnetID());
328 329
        reservations = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
        ASSERT_EQ(1, std::distance(reservations.first, reservations.second));
330
        EXPECT_EQ(increase(IOAddress("2001:db8:2::1"), i),
331
                  reservations.first->second.getPrefix());
332 333 334 335 336 337 338 339
    }

    // Also check that when the get6 finds multiple Host objects that fulfil
    // search criteria, it will throw an exception. Note that the reservation
    // exist both for hwaddrs_[0] and duids_[0].
    EXPECT_THROW(cfg.get6(SubnetID(1), duids_[0], hwaddrs_[0]), DuplicateHost);
}

340 341 342 343 344
// This test checks that the IPv6 reservations can be retrieved for a particular
// (subnet-id, address) tuple.
TEST_F(CfgHostsTest, get6ByAddr) {
    CfgHosts cfg;
    // Add hosts.
345
    for (unsigned i = 0; i < 25; ++i) {
346 347 348 349 350 351 352 353 354 355 356

        // Add host identified by DUID.
        HostPtr host = HostPtr(new Host(duids_[i]->toText(), "duid",
                                        SubnetID(0), SubnetID(1 + i % 2),
                                        IOAddress("0.0.0.0")));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress("2001:db8:2::1"),
                                                i)));
        cfg.add(host);
    }

357
    for (unsigned i = 0; i < 25; ++i) {
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
        // Retrieve host by (subnet-id,address).
        HostPtr host = cfg.get6(SubnetID(1 + i % 2),
                                increase(IOAddress("2001:db8:2::1"), i));
        ASSERT_TRUE(host);

        EXPECT_EQ(1 + i % 2, host->getIPv6SubnetID());
        IPv6ResrvRange reservations =
            host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
        ASSERT_EQ(1, std::distance(reservations.first, reservations.second));
        EXPECT_EQ(increase(IOAddress("2001:db8:2::1"), i),
                  reservations.first->second.getPrefix());
    }
}

// This test checks that the IPv6 reservations can be retrieved for a particular
// (subnet-id, address) tuple.
TEST_F(CfgHostsTest, get6MultipleAddrs) {
    CfgHosts cfg;

    // Add 25 hosts. Each host has reservations for 5 addresses.
378
    for (unsigned i = 0; i < 25; ++i) {
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

        // Add host identified by DUID.
        HostPtr host = HostPtr(new Host(duids_[i]->toText(), "duid",
                                        SubnetID(0), SubnetID(1 + i % 2),
                                        IOAddress("0.0.0.0")));

        // Generate 5 unique addresses for this host.
        for (int j = 0; j < 5; ++j) {
            std::stringstream tmp;
            tmp << "2001:db8:" << i << "::" << j;
            host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA, tmp.str()));
        }
        cfg.add(host);
    }

    // We don't care about HW/MAC addresses for now.
    HWAddrPtr hwaddr_not_used;

    // Now check if we can retrieve each of those 25 hosts by using each
    // of their addresses.
399
    for (unsigned i = 0; i < 25; ++i) {
400 401 402 403 404

        // Check that the host is there.
        HostPtr by_duid = cfg.get6(SubnetID(1 + i % 2), duids_[i], hwaddr_not_used);
        ASSERT_TRUE(by_duid);

405
        for (unsigned j = 0; j < 5; ++j) {
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
            std::stringstream tmp;
            tmp << "2001:db8:" << i << "::" << j;

            // Retrieve host by (subnet-id,address).
            HostPtr by_addr = cfg.get6(SubnetID(1 + i % 2), tmp.str());
            ASSERT_TRUE(by_addr);

            // The pointers should match. Maybe we should compare contents
            // rather than just pointers? I think there's no reason why
            // the code would make any copies of the Host object, so
            // the pointers should always point to the same object.
            EXPECT_EQ(by_duid, by_addr);
        }
    }
}


423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
// Checks that it's not possible for a second host to reserve an address
// which is already reserved.
TEST_F(CfgHostsTest, add4AlreadyReserved) {
    CfgHosts cfg;

    // First host has a reservation for address 192.0.2.1
    HostPtr host1 = HostPtr(new Host(hwaddrs_[0]->toText(false),
                                     "hw-address",
                                     SubnetID(1), SubnetID(0),
                                     IOAddress("192.0.2.1")));
    // Adding this should work.
    EXPECT_NO_THROW(cfg.add(host1));

    // The second host has a reservation for the same address.
    HostPtr host2 = HostPtr(new Host(hwaddrs_[1]->toText(false),
                                     "hw-address",
                                     SubnetID(1), SubnetID(0),
                                     IOAddress("192.0.2.1")));

    // This second host has a reservation for an address that is already
    // reserved for the first host, so it should be rejected.
    EXPECT_THROW(cfg.add(host2), isc::dhcp::ReservedAddress);
}

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
// Checks that it's not possible for two hosts to have the same address
// reserved at the same time.
TEST_F(CfgHostsTest, add6Invalid2Hosts) {
    CfgHosts cfg;

    // First host has a reservation for address 2001:db8::1
    HostPtr host1 = HostPtr(new Host(duids_[0]->toText(), "duid",
                                     SubnetID(0), SubnetID(1),
                                     IOAddress("0.0.0.0")));
    host1->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                    IOAddress("2001:db8::1")));
    // Adding this should work.
    EXPECT_NO_THROW(cfg.add(host1));

    // The second host has a reservation for the same address.
    HostPtr host2 = HostPtr(new Host(duids_[1]->toText(), "duid",
                                     SubnetID(0), SubnetID(1),
                                     IOAddress("0.0.0.0")));
    host2->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                    IOAddress("2001:db8::1")));

    // This second host has a reservation for an address that is already
    // reserved for the first host, so it should be rejected.
    EXPECT_THROW(cfg.add(host2), isc::dhcp::DuplicateHost);
}

473 474 475 476 477 478 479 480 481
TEST_F(CfgHostsTest, zeroSubnetIDs) {
    CfgHosts cfg;
    ASSERT_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                          "hw-address",
                                          SubnetID(0), SubnetID(0),
                                          IOAddress("10.0.0.1")))),
                 isc::BadValue);
}

482 483 484 485 486 487 488
// This test verifies that it is not possible to add the same Host to the
// same IPv4 subnet twice.
TEST_F(CfgHostsTest, duplicatesSubnet4HWAddr) {
    CfgHosts cfg;
    // Add a host.
    ASSERT_NO_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                             "hw-address",
489
                                             SubnetID(10), SubnetID(0),
490 491 492 493 494 495
                                             IOAddress("10.0.0.1")))));

    // Try to add the host with the same HW address to the same subnet. The fact
    // that the IP address is different here shouldn't really matter.
    EXPECT_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                          "hw-address",
496
                                          SubnetID(10), SubnetID(0),
497 498 499 500 501 502
                                          IOAddress("10.0.0.10")))),
                 isc::dhcp::DuplicateHost);

    // Now try to add it to a different subnet. It should go through.
    EXPECT_NO_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                             "hw-address",
503
                                             SubnetID(11), SubnetID(0),
504 505 506 507 508 509 510 511 512 513
                                             IOAddress("10.0.0.10")))));
}

// This test verifies that it is not possible to add the same Host to the
// same IPv4 subnet twice.
TEST_F(CfgHostsTest, duplicatesSubnet4DUID) {
    CfgHosts cfg;
    // Add a host.
    ASSERT_NO_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
                                             "duid",
514
                                             SubnetID(10), SubnetID(0),
515 516 517 518 519 520
                                             IOAddress("10.0.0.1")))));

    // Try to add the host with the same DUID to the same subnet. The fact
    // that the IP address is different here shouldn't really matter.
    EXPECT_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
                                          "duid",
521
                                          SubnetID(10), SubnetID(0),
522 523 524 525 526 527
                                          IOAddress("10.0.0.10")))),
                 isc::dhcp::DuplicateHost);

    // Now try to add it to a different subnet. It should go through.
    EXPECT_NO_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
                                             "duid",
528
                                             SubnetID(11), SubnetID(0),
529 530 531 532
                                             IOAddress("10.0.0.10")))));
}

// This test verifies that it is not possible to add the same Host to the
533
// same IPv6 subnet twice.
534 535 536 537 538
TEST_F(CfgHostsTest, duplicatesSubnet6HWAddr) {
    CfgHosts cfg;
    // Add a host.
    ASSERT_NO_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                             "hw-address",
539
                                             SubnetID(0), SubnetID(1),
540 541
                                             IOAddress("0.0.0.0"),
                                             "foo.example.com"))));
542 543 544 545 546

    // Try to add the host with the same HW address to the same subnet. The fact
    // that the IP address is different here shouldn't really matter.
    EXPECT_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                          "hw-address",
547
                                          SubnetID(0), SubnetID(1),
548 549
                                          IOAddress("0.0.0.0"),
                                          "foo.example.com"))),
550 551 552 553 554
                 isc::dhcp::DuplicateHost);

    // Now try to add it to a different subnet. It should go through.
    EXPECT_NO_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                             "hw-address",
555
                                             SubnetID(0), SubnetID(2),
556 557
                                             IOAddress("0.0.0.0"),
                                             "foo.example.com"))));
558 559 560
}

// This test verifies that it is not possible to add the same Host to the
561
// same IPv6 subnet twice.
562 563 564 565 566
TEST_F(CfgHostsTest, duplicatesSubnet6DUID) {
    CfgHosts cfg;
    // Add a host.
    ASSERT_NO_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
                                             "duid",
567
                                             SubnetID(0), SubnetID(1),
568 569
                                             IOAddress("0.0.0.0"),
                                             "foo.example.com"))));
570 571 572 573 574

    // Try to add the host with the same DUID to the same subnet. The fact
    // that the IP address is different here shouldn't really matter.
    EXPECT_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
                                          "duid",
575
                                          SubnetID(0), SubnetID(1),
576 577
                                          IOAddress("0.0.0.0"),
                                          "foo.example.com"))),
578 579 580 581 582
                 isc::dhcp::DuplicateHost);

    // Now try to add it to a different subnet. It should go through.
    EXPECT_NO_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
                                             "duid",
583
                                             SubnetID(0), SubnetID(2),
584 585
                                             IOAddress("0.0.0.0"),
                                             "foo.example.com"))));
586 587 588 589
}


} // end of anonymous namespace