fqdn_unittest.cc 86.1 KB
Newer Older
1
// Copyright (C) 2013-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

#include <config.h>
#include <asiolink/io_address.h>
#include <dhcp/option4_client_fqdn.h>
10
#include <dhcp/option_int_array.h>
11
#include <dhcp/tests/iface_mgr_test_config.h>
12
#include <dhcp4/tests/dhcp4_client.h>
13
#include <dhcp4/tests/dhcp4_test_utils.h>
14
#include <dhcp_ddns/ncr_msg.h>
15
#include <dhcpsrv/cfgmgr.h>
16 17
#include <dhcpsrv/lease_mgr.h>
#include <dhcpsrv/lease_mgr_factory.h>
18 19 20 21 22 23 24

#include <gtest/gtest.h>
#include <boost/scoped_ptr.hpp>

using namespace isc;
using namespace isc::asiolink;
using namespace isc::dhcp;
25
using namespace isc::dhcp::test;
26
using namespace isc::dhcp_ddns;
27 28

namespace {
29

30 31
/// @brief Set of JSON configurations used by the FQDN tests.
const char* CONFIGS[] = {
32
    // 0
33 34 35 36 37 38 39 40 41 42
    "{ \"interfaces-config\": {"
        "      \"interfaces\": [ \"*\" ]"
        "},"
        "\"valid-lifetime\": 3000,"
        "\"subnet4\": [ { "
        "    \"subnet\": \"10.0.0.0/24\", "
        "    \"id\": 1,"
        "    \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
        "    \"option-data\": [ {"
        "        \"name\": \"routers\","
43
        "        \"data\": \"10.0.0.200,10.0.0.201\""
44 45 46 47 48 49 50 51 52 53
        "    } ],"
        "    \"reservations\": ["
        "       {"
        "         \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
        "         \"hostname\":   \"unique-host.example.org\""
        "       }"
        "    ]"
        " }],"
        "\"dhcp-ddns\": {"
            "\"enable-updates\": true,"
54
            "\"qualifying-suffix\": \"\""
55 56
        "}"
    "}",
57
    // 1
58 59 60 61 62 63 64 65 66 67
    "{ \"interfaces-config\": {"
        "      \"interfaces\": [ \"*\" ]"
        "},"
        "\"valid-lifetime\": 3000,"
        "\"subnet4\": [ { "
        "    \"subnet\": \"10.0.0.0/24\", "
        "    \"id\": 1,"
        "    \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
        "    \"option-data\": [ {"
        "        \"name\": \"routers\","
68
        "        \"data\": \"10.0.0.200,10.0.0.201\""
69 70 71 72
        "    } ],"
        "    \"reservations\": ["
        "       {"
        "         \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
73
        "         \"hostname\":   \"foobar\""
74 75 76 77 78 79 80
        "       }"
        "    ]"
        " }],"
        "\"dhcp-ddns\": {"
            "\"enable-updates\": true,"
            "\"qualifying-suffix\": \"fake-suffix.isc.org.\""
        "}"
81
    "}",
82
    // 2
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    // Simple config with DDNS updates disabled.  Note pool is one address
    // large to ensure we get a specific address back.
    "{ \"interfaces-config\": {"
        "      \"interfaces\": [ \"*\" ]"
        "},"
        "\"valid-lifetime\": 3000,"
        "\"subnet4\": [ { "
        "    \"subnet\": \"10.0.0.0/24\", "
        "    \"id\": 1,"
        "    \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.10\" } ]"
        " }],"
        "\"dhcp-ddns\": {"
            "\"enable-updates\": false,"
            "\"qualifying-suffix\": \"fake-suffix.isc.org.\""
        "}"
    "}",
99
    // 3
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    // Simple config with DDNS updates enabled.  Note pool is one address
    // large to ensure we get a specific address back.
    "{ \"interfaces-config\": {"
        "      \"interfaces\": [ \"*\" ]"
        "},"
        "\"valid-lifetime\": 3000,"
        "\"subnet4\": [ { "
        "    \"subnet\": \"10.0.0.0/24\", "
        "    \"id\": 1,"
        "    \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.10\" } ]"
        " }],"
        "\"dhcp-ddns\": {"
            "\"enable-updates\": true,"
            "\"qualifying-suffix\": \"fake-suffix.isc.org.\""
        "}"
115
    "}",
116
    // 4
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    // Configuration which disables DNS updates but contains a reservation
    // for a hostname. Reserved hostname should be assigned to a client if
    // the client includes it in the Parameter Request List option.
    "{ \"interfaces-config\": {"
        "      \"interfaces\": [ \"*\" ]"
        "},"
        "\"valid-lifetime\": 3000,"
        "\"subnet4\": [ { "
        "    \"subnet\": \"10.0.0.0/24\", "
        "    \"id\": 1,"
        "    \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
        "    \"option-data\": [ {"
        "        \"name\": \"routers\","
        "        \"data\": \"10.0.0.200,10.0.0.201\""
        "    } ],"
        "    \"reservations\": ["
        "       {"
        "         \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
        "         \"hostname\":   \"reserved.example.org\""
        "       }"
        "    ]"
        " }],"
        "\"dhcp-ddns\": {"
            "\"enable-updates\": false,"
            "\"qualifying-suffix\": \"\""
        "}"
    "}",
144
    // 5
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    // Configuration which disables DNS updates but contains a reservation
    // for a hostname and the qualifying-suffix which should be appended to
    // the reserved hostname in the Hostname option returned to a client.
    "{ \"interfaces-config\": {"
        "      \"interfaces\": [ \"*\" ]"
        "},"
        "\"valid-lifetime\": 3000,"
        "\"subnet4\": [ { "
        "    \"subnet\": \"10.0.0.0/24\", "
        "    \"id\": 1,"
        "    \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
        "    \"option-data\": [ {"
        "        \"name\": \"routers\","
        "        \"data\": \"10.0.0.200,10.0.0.201\""
        "    } ],"
        "    \"reservations\": ["
        "       {"
        "         \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
        "         \"hostname\":   \"foo-bar\""
        "       }"
        "    ]"
        " }],"
        "\"dhcp-ddns\": {"
            "\"enable-updates\": false,"
            "\"qualifying-suffix\": \"example.isc.org\""
        "}"
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    "}",
    // 6
    // Configuration which enables DNS updates and hostname sanitization
    "{ \"interfaces-config\": {"
        "      \"interfaces\": [ \"*\" ]"
        "},"
        "\"valid-lifetime\": 3000,"
        "\"subnet4\": [ { "
        "    \"subnet\": \"10.0.0.0/24\", "
        "    \"id\": 1,"
        "    \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
        "    \"option-data\": [ {"
        "        \"name\": \"routers\","
        "        \"data\": \"10.0.0.200,10.0.0.201\""
        "    } ],"
        "    \"reservations\": ["
        "       {"
        "         \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
        "         \"hostname\":   \"unique-xxx-host.example.org\""
        "       }"
        "    ]"
        " }],"
        "\"dhcp-ddns\": {"
            "\"enable-updates\": true,"
            "\"hostname-char-set\" : \"[^A-Za-z0-9.-]\","
            "\"hostname-char-replacement\" : \"x\","
            "\"qualifying-suffix\": \"example.org\""
        "}"
    "}",
200
    // 7
201 202
    // Configuration with disabled DNS updates (default) and
    // hostname sanitization defined at global scope.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    "{ \"interfaces-config\": {"
        "      \"interfaces\": [ \"*\" ]"
        "},"
        "\"valid-lifetime\": 3000,"
        "\"subnet4\": [ { "
        "    \"subnet\": \"10.0.0.0/24\", "
        "    \"id\": 1,"
        "    \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
        "    \"option-data\": [ {"
        "        \"name\": \"routers\","
        "        \"data\": \"10.0.0.200,10.0.0.201\""
        "    } ],"
        "    \"reservations\": ["
        "       {"
        "         \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
        "         \"hostname\":   \"unique-xxx-host.example.org\""
        "       }"
        "    ]"
        " }],"
        "\"hostname-char-set\" : \"[^A-Za-z0-9.-]\","
        "\"hostname-char-replacement\" : \"x\""
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    "}",
    // 8
    // D2 enabled
    // global ddns-send-updates is false
    // one subnet does not enable updates
    // one subnet does enables updates
    "{ \"interfaces-config\": {\n"
        "      \"interfaces\": [ \"*\" ]\n"
        "},\n"
        "\"dhcp-ddns\": {\n"
            "\"enable-updates\": true\n"
        "},\n"
        "\"ddns-send-updates\": false,\n"
        "\"subnet4\": [ {\n"
        "       \"subnet\": \"192.0.2.0/24\",\n"
        "       \"id\": 1,\n"
        "       \"pools\": [ { \"pool\": \"192.0.2.10-192.0.2.100\" } ],\n"
        "       \"interface\": \"eth0\"\n"
        "   },\n"
        "   {\n"
        "       \"subnet\": \"192.0.3.0/24\", \n"
        "       \"id\": 2,\n"
        "       \"pools\": [ { \"pool\": \"192.0.3.10-192.0.3.100\" } ],\n"
        "       \"interface\": \"eth1\",\n"
        "       \"ddns-send-updates\": true\n"
        "   }\n"
        "]\n"
251
    "}"
252 253
};

254
class NameDhcpv4SrvTest : public Dhcpv4SrvTest {
255
public:
256 257 258
    // Reference to D2ClientMgr singleton
    D2ClientMgr& d2_mgr_;

259 260 261 262 263 264
    /// @brief Pointer to the DHCP server instance.
    NakedDhcpv4Srv* srv_;

    /// @brief Interface Manager's fake configuration control.
    IfaceMgrTestConfig iface_mgr_test_config_;

265
    // Bit Constants for turning on and off DDNS configuration options.
266 267 268
    static const uint16_t OVERRIDE_NO_UPDATE = 1;
    static const uint16_t OVERRIDE_CLIENT_UPDATE = 2;
    static const uint16_t REPLACE_CLIENT_NAME = 4;
269

270 271 272 273 274 275 276 277 278 279 280 281 282 283
    // Enum used to specify whether a client (packet) should include
    // the hostname option
    enum ClientNameFlag {
        CLIENT_NAME_PRESENT,
        CLIENT_NAME_NOT_PRESENT
    };

    // Enum used to specify whether the server should replace/supply
    // the hostname or not
    enum ReplacementFlag {
        NAME_REPLACED,
        NAME_NOT_REPLACED
    };

284 285 286 287 288 289
    NameDhcpv4SrvTest()
        : Dhcpv4SrvTest(),
          d2_mgr_(CfgMgr::instance().getD2ClientMgr()),
          srv_(NULL),
          iface_mgr_test_config_(true)
    {
290
        srv_ = new NakedDhcpv4Srv(0);
291
        IfaceMgr::instance().openSockets4();
292 293
        // Config DDNS to be enabled, all controls off
        enableD2();
294
    }
295

296
    virtual ~NameDhcpv4SrvTest() {
297
        delete srv_;
298
        // CfgMgr singleton doesn't get wiped between tests, so  we'll
Francis Dupont's avatar
Francis Dupont committed
299
        // disable D2 explicitly between tests.
300
        disableD2();
301 302
    }

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    /// @brief Sets the server's DDNS configuration to ddns updates disabled.
    void disableD2() {
        // Default constructor creates a config with DHCP-DDNS updates
        // disabled.
        D2ClientConfigPtr cfg(new D2ClientConfig());
        CfgMgr::instance().setD2ClientConfig(cfg);
    }

    /// @brief Enables DHCP-DDNS updates with the given options enabled.
    ///
    /// Replaces the current D2ClientConfiguration with a configuration
    /// which as updates enabled and the control options set based upon
    /// the bit mask of options.
    ///
    /// @param mask Bit mask of configuration options that should be enabled.
    void enableD2(const uint16_t mask = 0) {
        D2ClientConfigPtr cfg;

        ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
322
                                  isc::asiolink::IOAddress("127.0.0.1"), 53001,
323
                                  isc::asiolink::IOAddress("0.0.0.0"), 0, 1024,
324 325
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON)));

326
        ASSERT_NO_THROW(CfgMgr::instance().setD2ClientConfig(cfg));
327 328 329 330 331 332 333 334

        // Now we'll set the DDNS parameters at the subnet level.
        // These should get fetched when getDdnsParams() is invoked.
        ASSERT_TRUE(subnet_) << "enableD2 called without subnet_ set";
        subnet_->setDdnsSendUpdates(true);
        subnet_->setDdnsOverrideNoUpdate(mask & OVERRIDE_NO_UPDATE);
        subnet_->setDdnsOverrideClientUpdate(mask & OVERRIDE_CLIENT_UPDATE);
        subnet_->setDdnsReplaceClientNameMode((mask & REPLACE_CLIENT_NAME) ?
335 336
                                              D2ClientConfig::RCM_WHEN_PRESENT
                                              : D2ClientConfig::RCM_NEVER);
337 338 339
        subnet_->setDdnsGeneratedPrefix("myhost");
        subnet_->setDdnsQualifyingSuffix("example.com");

340
        ASSERT_NO_THROW(srv_->startD2());
341 342
    }

343 344 345 346 347 348 349 350 351 352 353
    // Fetch DDNS parameter set scoped to the current subnet_.
    DdnsParamsPtr getDdnsParams() {
        ConstElementPtr cfg = CfgMgr::instance().getCurrentCfg()->toElement();
        if (!subnet_) {
            ADD_FAILURE() << "getDdnsParams() - subnet_ is empty!";
            return (DdnsParamsPtr(new DdnsParams()));
        }

        return(CfgMgr::instance().getCurrentCfg()->getDdnsParams(*subnet_));
    }

354 355 356 357 358
    // Create a lease to be used by various tests.
    Lease4Ptr createLease(const isc::asiolink::IOAddress& addr,
                          const std::string& hostname,
                          const bool fqdn_fwd,
                          const bool fqdn_rev) {
359
        const uint8_t hwaddr_data[] = { 0, 1, 2, 3, 4, 5, 6 };
360 361
        HWAddrPtr hwaddr(new HWAddr(hwaddr_data, sizeof(hwaddr_data),
                                    HTYPE_ETHER));
362
        Lease4Ptr lease(new Lease4(addr, hwaddr,
363 364
                                   &generateClientId()->getData()[0],
                                   generateClientId()->getData().size(),
365
                                   100, time(NULL), subnet_->getID()));
366 367 368 369 370 371 372 373
        // @todo Set this through the Lease4 constructor.
        lease->hostname_ = hostname;
        lease->fqdn_fwd_ = fqdn_fwd;
        lease->fqdn_rev_ = fqdn_rev;

        return (lease);
    }

374 375 376 377
    // Create an instance of the DHCPv4 Client FQDN Option.
    Option4ClientFqdnPtr
    createClientFqdn(const uint8_t flags,
                     const std::string& fqdn_name,
378
                     Option4ClientFqdn::DomainNameType fqdn_type) {
379 380 381 382 383
        return (Option4ClientFqdnPtr(new Option4ClientFqdn(flags,
                                                           Option4ClientFqdn::
                                                           RCODE_CLIENT(),
                                                           fqdn_name,
                                                           fqdn_type)));
384 385 386
   }

    // Create an instance of the Hostname option.
387
    OptionStringPtr
388
    createHostname(const std::string& hostname) {
389 390 391
        OptionStringPtr opt_hostname(new OptionString(Option::V4,
                                                      DHO_HOST_NAME,
                                                      hostname));
392
        return (opt_hostname);
393 394
    }

395 396 397 398 399 400 401 402
    /// @brief Convenience method for generating an FQDN from an IP address.
    ///
    /// This is just a wrapper method around the D2ClientMgr's method for
    /// generating domain names from the configured prefix, suffix, and a
    /// given IP address.  This is useful for verifying that fully generated
    /// names are correct.
    ///
    /// @param addr IP address used in the lease.
403 404
    /// @param trailing_dot A boolean flag which indicates whether the
    /// trailing dot should be appended to the end of the hostname.
Francis Dupont's avatar
Francis Dupont committed
405
    /// The default value is "true" which means that it should.
406 407
    ///
    /// @return An std::string contained the generated FQDN.
408 409 410
    std::string generatedNameFromAddress(const IOAddress& addr,
                                         const bool trailing_dot = true) {
        return(CfgMgr::instance().getD2ClientMgr()
411
               .generateFqdn(addr, *getDdnsParams(), trailing_dot));
412 413
    }

414
    // Get the Client FQDN Option from the given message.
415 416 417 418 419
    Option4ClientFqdnPtr getClientFqdnOption(const Pkt4Ptr& pkt) {
        return (boost::dynamic_pointer_cast<
                Option4ClientFqdn>(pkt->getOption(DHO_FQDN)));
    }

420
    // get the Hostname option from the given message.
421
    OptionStringPtr getHostnameOption(const Pkt4Ptr& pkt) {
422
        return (boost::dynamic_pointer_cast<
423
                OptionString>(pkt->getOption(DHO_HOST_NAME)));
424 425
    }

426 427 428 429
    // Create a message holding DHCPv4 Client FQDN Option.
    Pkt4Ptr generatePktWithFqdn(const uint8_t msg_type,
                                const uint8_t fqdn_flags,
                                const std::string& fqdn_domain_name,
430
                                Option4ClientFqdn::DomainNameType fqdn_type,
431 432
                                const bool include_prl,
                                const bool include_clientid = true) {
433 434
        Pkt4Ptr pkt = Pkt4Ptr(new Pkt4(msg_type, 1234));
        pkt->setRemoteAddr(IOAddress("192.0.2.3"));
435
        pkt->setIface("eth1");
436 437 438 439 440
        // For DISCOVER we don't include server id, because client broadcasts
        // the message to all servers.
        if (msg_type != DHCPDISCOVER) {
            pkt->addOption(srv_->getServerID());
        }
441 442 443 444

        if (include_clientid) {
            pkt->addOption(generateClientId());
        }
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

        // Create Client FQDN Option with the specified flags and
        // domain-name.
        pkt->addOption(createClientFqdn(fqdn_flags, fqdn_domain_name,
                                        fqdn_type));

        // Control whether or not to request that server returns the FQDN
        // option. Server may be configured to always return it or return
        // only in case client requested it.
        if (include_prl) {
            OptionUint8ArrayPtr option_prl =
                OptionUint8ArrayPtr(new OptionUint8Array(Option::V4,
                                    DHO_DHCP_PARAMETER_REQUEST_LIST));
            option_prl->addValue(DHO_FQDN);
        }
        return (pkt);
    }

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    // Create a message holding a Hostname option.
    Pkt4Ptr generatePktWithHostname(const uint8_t msg_type,
                                    const std::string& hostname) {

        Pkt4Ptr pkt = Pkt4Ptr(new Pkt4(msg_type, 1234));
        pkt->setRemoteAddr(IOAddress("192.0.2.3"));
        // For DISCOVER we don't include server id, because client broadcasts
        // the message to all servers.
        if (msg_type != DHCPDISCOVER) {
            pkt->addOption(srv_->getServerID());
        }

        pkt->addOption(generateClientId());


        // Create Client FQDN Option with the specified flags and
        // domain-name.
        pkt->addOption(createHostname(hostname));

        return (pkt);

484
    }
485

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    // Create a message holding an empty Hostname option.
    Pkt4Ptr generatePktWithEmptyHostname(const uint8_t msg_type) {

        Pkt4Ptr pkt = Pkt4Ptr(new Pkt4(msg_type, 1234));
        pkt->setRemoteAddr(IOAddress("192.0.2.3"));
        // For DISCOVER we don't include server id, because client broadcasts
        // the message to all servers.
        if (msg_type != DHCPDISCOVER) {
            pkt->addOption(srv_->getServerID());
        }

        pkt->addOption(generateClientId());

        // Create Hostname option.
        std::string hostname(" ");
        OptionPtr opt = createHostname(hostname);
        opt->setData(hostname.begin(), hostname.begin());
        pkt->addOption(opt);

        return (pkt);

    }

509 510 511 512 513 514 515 516 517 518 519 520 521 522
    // Create a message holding of a given type
    Pkt4Ptr generatePkt(const uint8_t msg_type) {
        Pkt4Ptr pkt = Pkt4Ptr(new Pkt4(msg_type, 1234));
        pkt->setRemoteAddr(IOAddress("192.0.2.3"));
        // For DISCOVER we don't include server id, because client broadcasts
        // the message to all servers.
        if (msg_type != DHCPDISCOVER) {
            pkt->addOption(srv_->getServerID());
        }

        pkt->addOption(generateClientId());
        return (pkt);
    }

523 524 525
    // Test that server generates the appropriate FQDN option in response to
    // client's FQDN option.
    void testProcessFqdn(const Pkt4Ptr& query, const uint8_t exp_flags,
526
                         const std::string& exp_domain_name,
527
                         Option4ClientFqdn::DomainNameType
528
                         exp_domain_type = Option4ClientFqdn::FULL) {
529 530 531 532 533 534 535 536 537 538
        ASSERT_TRUE(getClientFqdnOption(query));

        Pkt4Ptr answer;
        if (query->getType() == DHCPDISCOVER) {
            answer.reset(new Pkt4(DHCPOFFER, 1234));

        } else {
            answer.reset(new Pkt4(DHCPACK, 1234));

        }
539
        Dhcpv4Exchange ex = createExchange(query);
540
        ASSERT_NO_THROW(srv_->processClientName(ex));
541

542
        Option4ClientFqdnPtr fqdn = getClientFqdnOption(ex.getResponse());
543 544
        ASSERT_TRUE(fqdn);

545
        checkFqdnFlags(ex.getResponse(), exp_flags);
546 547 548 549 550 551

        EXPECT_EQ(exp_domain_name, fqdn->getDomainName());
        EXPECT_EQ(exp_domain_type, fqdn->getDomainNameType());

    }

552 553
    // Test that the server's processes the hostname (or lack thereof)
    // in a client request correctly, according to the replace-client-name
554 555
    // mode configuration parameter.  We include hostname sanitizer to ensure
    // it does not interfere with name replacement.
556
    //
557
    // @param mode - value to use for replace-client-name
558 559 560 561 562 563 564 565 566 567 568 569 570 571
    // @param client_name_flag - specifies whether or not the client request
    // should contain a hostname option
    // @param exp_replacement_flag - specifies whether or not the server is
    // expected to replace (or supply) the hostname in its response
    void testReplaceClientNameMode(const char* mode,
                                   enum ClientNameFlag client_name_flag,
                                   enum ReplacementFlag exp_replacement_flag) {
        // Configuration "template" with a replaceable mode parameter
        const char* config_template =
            "{ \"interfaces-config\": {"
            "      \"interfaces\": [ \"*\" ]"
            "},"
            "\"valid-lifetime\": 3000,"
            "\"subnet4\": [ { "
572
            "    \"subnet\": \"192.0.2.0/24\", "
573
            "    \"id\": 1,"
574
            "    \"pools\": [ { \"pool\": \"192.0.2.10-192.0.2.10\" } ]"
575 576
            " }],"
            "\"dhcp-ddns\": {"
577 578 579 580 581
            "  \"enable-updates\": true,"
            "  \"qualifying-suffix\": \"fake-suffix.isc.org.\","
            "  \"hostname-char-set\": \"[^A-Za-z0-9.-]\","
            "  \"hostname-char-replacement\": \"x\","
            "  \"replace-client-name\": \"%s\""
582 583 584 585 586
            "}}";

        // Create the configuration and configure the server
        char config_buf[1024];
        sprintf(config_buf, config_template, mode);
587
        ASSERT_NO_THROW(configure(config_buf, srv_)) << "configuration failed";
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604

        // Build our client packet
        Pkt4Ptr query;
        if (client_name_flag == CLIENT_NAME_PRESENT) {
            ASSERT_NO_THROW(query = generatePktWithHostname(DHCPREQUEST,
                                                            "my.example.com."));
        } else {
            ASSERT_NO_THROW(query = generatePkt(DHCPREQUEST));
        }

        // Run the packet through the server, extracting the hostname option
        // from the response.  If the option isn't present the returned pointer
        // will be null.
        OptionStringPtr hostname;
        ASSERT_NO_THROW(
            hostname = processHostname(query,
                                       client_name_flag == CLIENT_NAME_PRESENT)
605
        ) << "processHostname throw an exception";
606 607 608

        // Verify the contents (or lack thereof) of the hostname
        if (exp_replacement_flag == NAME_REPLACED) {
609 610
            ASSERT_TRUE(hostname)
                << "No host name, it should have the replacement name \".\"";
611 612 613
            EXPECT_EQ(".", hostname->getValue());
        } else {
            if (client_name_flag == CLIENT_NAME_PRESENT) {
614 615
                ASSERT_TRUE(hostname)
                    << "No host name, expected original from client";
616 617
                EXPECT_EQ("my.example.com.", hostname->getValue());
            } else {
618 619 620
                ASSERT_FALSE(hostname)
                    << "Host name is: " << hostname
                    << ", it should have been null";
621 622 623 624
            }
        }
    }

625 626 627 628 629 630 631 632
    /// @brief Checks the packet's FQDN option flags against a given mask
    ///
    /// @param pkt IPv4 packet whose FQDN flags should be checked.
    /// @param exp_flags Bit mask of flags that are expected to be true.
    void checkFqdnFlags(const Pkt4Ptr& pkt, const uint8_t exp_flags) {
        Option4ClientFqdnPtr fqdn = getClientFqdnOption(pkt);
        ASSERT_TRUE(fqdn);

633 634 635 636 637 638 639 640 641 642 643
        const bool flag_n = (exp_flags & Option4ClientFqdn::FLAG_N) != 0;
        const bool flag_s = (exp_flags & Option4ClientFqdn::FLAG_S) != 0;
        const bool flag_o = (exp_flags & Option4ClientFqdn::FLAG_O) != 0;
        const bool flag_e = (exp_flags & Option4ClientFqdn::FLAG_E) != 0;

        EXPECT_EQ(flag_n, fqdn->getFlag(Option4ClientFqdn::FLAG_N));
        EXPECT_EQ(flag_s, fqdn->getFlag(Option4ClientFqdn::FLAG_S));
        EXPECT_EQ(flag_o, fqdn->getFlag(Option4ClientFqdn::FLAG_O));
        EXPECT_EQ(flag_e, fqdn->getFlag(Option4ClientFqdn::FLAG_E));
    }

644

645
    /// @brief  Invokes Dhcpv4Srv::processHostname on the given packet
646 647 648 649 650 651 652 653 654 655 656 657
    ///
    /// Processes the Hostname option in the client's message and returns
    /// the hostname option which would be sent to the client. It will
    /// return empty if the hostname option is not to be included
    /// server's response.
    /// @param query - client packet to process
    /// @param must_have_host - flag indicating whether or not the client
    /// packet must contain the hostname option
    ///
    /// @return a pointer to the hostname option constructed by the server
    OptionStringPtr processHostname(const Pkt4Ptr& query,
                                    bool must_have_host = true) {
658
        if (!getHostnameOption(query) && must_have_host) {
659 660
            ADD_FAILURE() << "Hostname option not carried in the query";
        }
661 662 663 664 665 666 667 668 669

        Pkt4Ptr answer;
        if (query->getType() == DHCPDISCOVER) {
            answer.reset(new Pkt4(DHCPOFFER, 1234));

        } else {
            answer.reset(new Pkt4(DHCPACK, 1234));

        }
670

671
        Dhcpv4Exchange ex = createExchange(query);
672 673 674 675
        if (!ex.getContext()->subnet_) {
            ADD_FAILURE() << "createExchange did not select a subnet";
        }

676
        srv_->processClientName(ex);
677

678
        OptionStringPtr hostname = getHostnameOption(ex.getResponse());
679
        return (hostname);
680 681 682

    }

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
    ///@brief Verify that NameChangeRequest holds valid values.
    ///
    /// Pulls the NCR from the top of the send queue and checks it's content
    ///  against a number of expected parameters.
    ///
    /// @param type - expected NCR change type, CHG_ADD or CHG_REMOVE
    /// @param reverse - flag indicating whether or not the NCR specifies
    /// reverse change
    /// @param forward - flag indication whether or not the NCR specifies
    /// forward change
    /// @param addr  - expected lease address in the NCR
    /// @param fqdn  - expected FQDN in the NCR
    /// @param dhcid - expected DHCID in the NCR (comparison is performed only
    /// if the value supplied is not empty):w
    /// @param cltt - cltt value from the lease the NCR for which the NCR
    /// was generated expected value for
    /// @param len - expected lease length in the NCR
    /// @param not_strict_expire_check - when true the comparison of the NCR
    /// lease expiration time is conducted as greater than or equal to rather
Josh Soref's avatar
Josh Soref committed
702
    /// equal to CLTT plus lease length.
703 704 705
    void verifyNameChangeRequest(const isc::dhcp_ddns::NameChangeType type,
                                 const bool reverse, const bool forward,
                                 const std::string& addr,
706
                                 const std::string& fqdn,
707
                                 const std::string& dhcid,
708 709 710
                                 const time_t cltt,
                                 const uint16_t len,
                                 const bool not_strict_expire_check = false) {
711 712 713 714 715 716 717 718 719
        NameChangeRequestPtr ncr;
        ASSERT_NO_THROW(ncr = d2_mgr_.peekAt(0));
        ASSERT_TRUE(ncr);

        EXPECT_EQ(type, ncr->getChangeType());
        EXPECT_EQ(forward, ncr->isForwardChange());
        EXPECT_EQ(reverse, ncr->isReverseChange());
        EXPECT_EQ(addr, ncr->getIpAddress());
        EXPECT_EQ(fqdn, ncr->getFqdn());
720 721 722
        // Compare dhcid if it is not empty. In some cases, the DHCID is
        // not known in advance and can't be compared.
        if (!dhcid.empty()) {
723
            EXPECT_EQ(dhcid, ncr->getDhcid().toStr());
724
        }
725 726
        // In some cases, the test doesn't have access to the last transmission
        // time for the particular client. In such cases, the test can use the
727 728 729
        // current time as cltt but the it may not check the lease expiration
        // time for equality but rather check that the lease expiration time
        // is not greater than the current time + lease lifetime.
730
        if (not_strict_expire_check) {
731
            EXPECT_GE(cltt + len, ncr->getLeaseExpiresOn());
732
        } else {
733
            EXPECT_EQ(cltt + len, ncr->getLeaseExpiresOn());
734
        }
735 736 737 738 739
        EXPECT_EQ(len, ncr->getLeaseLength());
        EXPECT_EQ(isc::dhcp_ddns::ST_NEW, ncr->getStatus());

        // Process the message off the queue
        ASSERT_NO_THROW(d2_mgr_.runReadyIO());
740 741
    }

742 743 744 745 746 747 748 749 750 751 752 753 754 755

    /// @brief Tests processing a request with the given client flags
    ///
    /// This method creates a request with its FQDN flags set to the given
    /// value and submits it to the server for processing.  It then checks
    /// the following:
    /// 1. Did the server generate an ACK with the correct FQDN flags
    /// 2. If the server should have generated an NCR, did it? and If
    /// so was it correct?
    ///
    /// @param client_flags Mask of client FQDN flags which are true
    /// @param response_flags Mask of expected FQDN flags in the response
    void flagVsConfigScenario(const uint8_t client_flags,
                       const uint8_t response_flags) {
756 757 758 759
        // Create fake interfaces and open fake sockets.
        IfaceMgrTestConfig iface_config(true);
        IfaceMgr::instance().openSockets4();

760 761 762 763 764 765 766 767 768 769 770 771
        Pkt4Ptr req = generatePktWithFqdn(DHCPREQUEST, client_flags,
                                          "myhost.example.com.",
                                          Option4ClientFqdn::FULL, true);

        // Process the request.
        Pkt4Ptr reply;
        ASSERT_NO_THROW(reply = srv_->processRequest(req));

        // Verify the response and flags.
        checkResponse(reply, DHCPACK, 1234);
        checkFqdnFlags(reply, response_flags);

772 773
        // NCRs cannot be sent to the d2_mgr unless updates are enabled.
        if (d2_mgr_.ddnsEnabled()) {
774 775 776 777
            // There should be an NCR if response S flag is 1 or N flag is 0.
            bool exp_fwd = (response_flags & Option4ClientFqdn::FLAG_S);
            bool exp_rev = (!(response_flags & Option4ClientFqdn::FLAG_N));
            if (!exp_fwd && !exp_rev) {
778 779 780 781
                ASSERT_EQ(0, d2_mgr_.getQueueSize());
            } else {
                // Verify that there is one NameChangeRequest as expected.
                ASSERT_EQ(1, d2_mgr_.getQueueSize());
782 783
                verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD,
                                        exp_rev, exp_fwd,
784 785 786 787 788 789
                                        reply->getYiaddr().toText(),
                                        "myhost.example.com.",
                                        "", // empty DHCID means don't check it
                                        time(NULL) + subnet_->getValid(),
                                        subnet_->getValid(), true);
            }
790 791
        }
    }
792 793
};

794 795 796 797 798 799
// Tests the following scenario:
//  - Updates are enabled
//  - All overrides are off
//  - Client requests forward update  (N = 0, S = 1)
//
//  Server should perform the update:
Francis Dupont's avatar
Francis Dupont committed
800
//  - Response flags should N = 0, S = 1, O = 0
801 802 803 804 805 806 807
//  - Should queue an NCR
TEST_F(NameDhcpv4SrvTest, updatesEnabled) {
    flagVsConfigScenario((Option4ClientFqdn::FLAG_E |
                          Option4ClientFqdn::FLAG_S),
                          (Option4ClientFqdn::FLAG_E |
                           Option4ClientFqdn::FLAG_S));
}
808

809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
// Tests the following scenario
//  - Updates are disabled
//  - Client requests forward update  (N = 0, S = 1)
//
//  Server should NOT perform updates:
//   - Response flags should N = 1, S = 0, O = 1
//   - Should not queue any NCRs
TEST_F(NameDhcpv4SrvTest, updatesDisabled) {
    disableD2();
    flagVsConfigScenario((Option4ClientFqdn::FLAG_E |
                          Option4ClientFqdn::FLAG_S),
                          (Option4ClientFqdn::FLAG_E |
                           Option4ClientFqdn::FLAG_N |
                           Option4ClientFqdn::FLAG_O));
}
824

825 826 827 828 829 830 831 832 833 834 835 836 837 838
// Tests the following scenario:
//  - Updates are enabled
//  - All overrides are off.
//  - Client requests no updates  (N = 1, S = 0)
//
//  Server should NOT perform updates:
//  - Response flags should N = 1, S = 0, O = 0
//  - Should not queue any NCRs
TEST_F(NameDhcpv4SrvTest, respectNoUpdate) {
    flagVsConfigScenario((Option4ClientFqdn::FLAG_E |
                          Option4ClientFqdn::FLAG_N),
                          (Option4ClientFqdn::FLAG_E |
                           Option4ClientFqdn::FLAG_N));
}
839

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
// Tests the following scenario:
//  - Updates are enabled
//  - override-no-update is on
//  - Client requests no updates  (N = 1, S = 0)
//
// Server should override "no update" request and perform updates:
// - Response flags should be  N = 0, S = 1, O = 1
// - Should queue an NCR
TEST_F(NameDhcpv4SrvTest, overrideNoUpdate) {
    enableD2(OVERRIDE_NO_UPDATE);
    flagVsConfigScenario((Option4ClientFqdn::FLAG_E |
                          Option4ClientFqdn::FLAG_N),
                          (Option4ClientFqdn::FLAG_E |
                           Option4ClientFqdn::FLAG_S |
                           Option4ClientFqdn::FLAG_O));
}

// Tests the following scenario:
//  - Updates are enabled
//  - All overrides are off.
//  - Client requests delegation  (N = 0, S = 0)
//
// Server should respect client's delegation request and NOT do updates:

864
// - Response flags should be  N = 0, S = 0, O = 0
865 866 867 868
// - Should not queue any NCRs
TEST_F(NameDhcpv4SrvTest, respectClientDelegation) {

    flagVsConfigScenario(Option4ClientFqdn::FLAG_E,
869
                         Option4ClientFqdn::FLAG_E);
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
}

// Tests the following scenario:
//  - Updates are enabled
//  - override-client-update is on.
//  - Client requests delegation  (N = 0, S = 0)
//
// Server should override client's delegation request and do updates:
// - Response flags should be  N = 0, S = 1, O = 1
// - Should queue an NCR
TEST_F(NameDhcpv4SrvTest, overrideClientDelegation) {
    // Turn on override-client-update.
    enableD2(OVERRIDE_CLIENT_UPDATE);

    flagVsConfigScenario(Option4ClientFqdn::FLAG_E,
                         (Option4ClientFqdn::FLAG_E |
                          Option4ClientFqdn::FLAG_S |
                          Option4ClientFqdn::FLAG_O));
888 889
}

890
// Test that server processes the Hostname option sent by a client and
891 892
// responds with the Hostname option to confirm that the server has
// taken responsibility for the update.
893
TEST_F(NameDhcpv4SrvTest, serverUpdateHostname) {
894 895 896
    Pkt4Ptr query;
    ASSERT_NO_THROW(query = generatePktWithHostname(DHCPREQUEST,
                                                    "myhost.example.com."));
897
    OptionStringPtr hostname;
898 899 900
    ASSERT_NO_THROW(hostname = processHostname(query));

    ASSERT_TRUE(hostname);
901
    EXPECT_EQ("myhost.example.com.", hostname->getValue());
902 903 904

}

905
// Test that the server skips processing of a mal-formed Hostname options.
906 907 908
// - First scenario the hostname has an empty label
// - Second scenario the hostname option causes an internal parsing error
// in dns::Name(). The content was created by fuzz testing.
909
TEST_F(NameDhcpv4SrvTest, serverUpdateMalformedHostname) {
910
    Pkt4Ptr query;
911 912

    // Hostname should not be able to have an emtpy label.
913 914
    ASSERT_NO_THROW(query = generatePktWithHostname(DHCPREQUEST,
                                                    "abc..example.com"));
915
    OptionStringPtr hostname;
916 917
    ASSERT_NO_THROW(hostname = processHostname(query));
    EXPECT_FALSE(hostname);
918

919 920
    // The following vector matches malformed hostname data produced by
    // fuzz testing that causes an internal error in dns::Name parsing logic.
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
    std::vector<uint8_t> badname  {
        0xff,0xff,0x7f,0x00,0x00,0x00,0x7f,0x00,0x00,0x00,0x00,
        0x00,0x00,0x04,0x63,0x82,0x53,0x63,0x35,0x01,0x01,0x3d,0x07,0x01,0x00,0x00,0x00,
        0x00,0x00,0x00,0x19,0x0c,0x4e,0x01,0x00,0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x35,0x01,0x05,0x3a,0x04,0x00,0x00,0x07,0x08,0x3b,0x04,0x00,
        0x00,0x2e,0x3b,0x04,0x00,0x19,0x2e,0x00,0x00,0x00,0x0a,0x00,0x12,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x0b,0x82,0x01,0xfc,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x35,0x01,0x05,0x3a,0x04,0x00,0x00,0x07,0x08,0x3b,0x04,
        0x00,0x00,0x2e,0x3b,0x04,0x00,0x19,0x2e,0x56,0x00,0x00,0x0a,0x00,0x12,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x0b,0x82,0x01,0xfc,0x42,0x00,0x00,0x00,0x00,0x19,0x0c,
        0x4e,0x01,0x05,0x3a,0x04,0xde,0x00,0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x35,0x01,0x05,0x3a,0x07,0x08,0x3b,0x04,0x00,0x00,0x2e,0x3b,0x04,
        0x00,0x19,0x2e,0x56,0x40,0x00,0x00,0x00,0x00,0x00,0x0a,0x00,0x12,0x00,0x00,0x00,
        0x00,0x00,0x19,0x00,0x0b,0x82,0x01,0xfc,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0xfc,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x35,0x01,0x05,0xff,0xff,0x05,0x00,0x07,0x08,0x3b,0x04,
        0x00,0x00,0x2e,0x3b
    };

    std::string badnamestr(badname.begin(), badname.end());
    ASSERT_NO_THROW(query = generatePktWithHostname(DHCPREQUEST,
                                                    badnamestr));
    ASSERT_NO_THROW(hostname = processHostname(query));
    EXPECT_FALSE(hostname);
946 947
}

948 949 950 951
// Test that the server does not see an empty Hostname option.
// Suppressing the empty Hostname is done in libdhcp++ during
// unpackcing, so technically we don't need this test but,
// hey it's already written.
952 953 954 955 956 957 958 959
TEST_F(NameDhcpv4SrvTest, serverUpdateEmptyHostname) {
    Pkt4Ptr query;
    ASSERT_NO_THROW(query = generatePktWithEmptyHostname(DHCPREQUEST));
    OptionStringPtr hostname;
    ASSERT_NO_THROW(hostname = processHostname(query));
    EXPECT_FALSE(hostname);
}

960 961
// Test that server generates the fully qualified domain name for the client
// if client supplies the partial name.
962
TEST_F(NameDhcpv4SrvTest, serverUpdateForwardPartialNameFqdn) {
963 964 965 966 967 968 969 970 971 972 973 974 975
    Pkt4Ptr query = generatePktWithFqdn(DHCPREQUEST,
                                        Option4ClientFqdn::FLAG_E |
                                        Option4ClientFqdn::FLAG_S,
                                        "myhost",
                                        Option4ClientFqdn::PARTIAL,
                                        true);

    testProcessFqdn(query,
                    Option4ClientFqdn::FLAG_E | Option4ClientFqdn::FLAG_S,
                    "myhost.example.com.");

}

976 977 978
// Test that server generates the fully qualified domain name for the client
// if client supplies the unqualified name in the Hostname option.
TEST_F(NameDhcpv4SrvTest, serverUpdateUnqualifiedHostname) {
979 980
    Pkt4Ptr query;
    ASSERT_NO_THROW(query = generatePktWithHostname(DHCPREQUEST, "myhost"));
981
    OptionStringPtr hostname;
982 983 984
    ASSERT_NO_THROW(hostname =  processHostname(query));

    ASSERT_TRUE(hostname);
985
    EXPECT_EQ("myhost.example.com", hostname->getValue());
986

987 988
}

989 990 991 992
// Test that server sets empty domain-name in the FQDN option when client
// supplied no domain-name. The domain-name is supposed to be set after the
// lease is acquired. The domain-name is then generated from the IP address
// assigned to a client.
993
TEST_F(NameDhcpv4SrvTest, serverUpdateForwardNoNameFqdn) {
994 995 996 997 998 999 1000 1001 1002
    Pkt4Ptr query = generatePktWithFqdn(DHCPREQUEST,
                                        Option4ClientFqdn::FLAG_E |
                                        Option4ClientFqdn::FLAG_S,
                                        "",
                                        Option4ClientFqdn::PARTIAL,
                                        true);

    testProcessFqdn(query,
                    Option4ClientFqdn::FLAG_E | Option4ClientFqdn::FLAG_S,
1003
                    "", Option4ClientFqdn::PARTIAL);
1004 1005 1006

}

1007 1008
// Test that exactly one NameChangeRequest is generated when the new lease
// has been acquired (old lease is NULL).
1009
TEST_F(NameDhcpv4SrvTest, createNameChangeRequestsNewLease) {
1010 1011 1012 1013 1014
    Lease4Ptr lease = createLease(IOAddress("192.0.2.3"), "myhost.example.com.",
                                  true, true);
    Lease4Ptr old_lease;

    ASSERT_NO_THROW(srv_->createNameChangeRequests(lease, old_lease));
1015
    ASSERT_EQ(1, d2_mgr_.getQueueSize());
1016 1017

    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
1018 1019 1020
                            "192.0.2.3", "myhost.example.com.",
                            "00010132E91AA355CFBB753C0F0497A5A940436965"
                            "B68B6D438D98E680BF10B09F3BCF",
1021
                            lease->cltt_, 100);
1022 1023 1024 1025
}

// Test that no NameChangeRequest is generated when a lease is renewed and
// the FQDN data hasn't changed.
1026
TEST_F(NameDhcpv4SrvTest, createNameChangeRequestsRenewNoChange) {
1027 1028
    Lease4Ptr lease = createLease(IOAddress("192.0.2.3"), "myhost.example.com.",
                                  true, true);
1029 1030 1031
    // Comparison should be case insensitive, so turning some of the
    // characters of the old lease hostname to upper case should not
    // trigger NCRs.
1032
    Lease4Ptr old_lease = createLease(IOAddress("192.0.2.3"),
1033
                                      "Myhost.Example.Com.", true, true);
1034 1035 1036
    old_lease->valid_lft_ += 100;

    ASSERT_NO_THROW(srv_->createNameChangeRequests(lease, old_lease));
1037
    ASSERT_EQ(0, d2_mgr_.getQueueSize());
1038
}
1039

1040 1041
// Test that the OFFER message generated as a result of the DISCOVER message
// processing will not result in generation of the NameChangeRequests.
1042
TEST_F(NameDhcpv4SrvTest, processDiscover) {
1043 1044 1045
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

1046 1047 1048 1049 1050 1051 1052 1053 1054
    Pkt4Ptr req = generatePktWithFqdn(DHCPDISCOVER, Option4ClientFqdn::FLAG_S |
                                      Option4ClientFqdn::FLAG_E,
                                      "myhost.example.com.",
                                      Option4ClientFqdn::FULL, true);

    Pkt4Ptr reply;
    ASSERT_NO_THROW(reply = srv_->processDiscover(req));
    checkResponse(reply, DHCPOFFER, 1234);

1055
    EXPECT_EQ(0, d2_mgr_.getQueueSize());
1056 1057
}

1058 1059 1060
// Test that server generates client's hostname from the IP address assigned
// to it when DHCPv4 Client FQDN option specifies an empty domain-name.
TEST_F(NameDhcpv4SrvTest, processRequestFqdnEmptyDomainName) {
1061 1062 1063
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
    Pkt4Ptr req = generatePktWithFqdn(DHCPREQUEST, Option4ClientFqdn::FLAG_S |
                                      Option4ClientFqdn::FLAG_E,
                                      "", Option4ClientFqdn::PARTIAL, true);

    Pkt4Ptr reply;
    ASSERT_NO_THROW(reply = srv_->processRequest(req));

    checkResponse(reply, DHCPACK, 1234);

    // Verify that there is one NameChangeRequest generated.
1074
    ASSERT_EQ(1, d2_mgr_.getQueueSize());
1075

1076
    // The hostname is generated from the IP address acquired (yiaddr).
1077 1078
    std::string hostname = generatedNameFromAddress(reply->getYiaddr());

1079
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
1080
                            reply->getYiaddr().toText(), hostname,
1081
                            "", // empty DHCID forces that it is not checked
1082 1083
                            time(NULL) + subnet_->getValid(),
                            subnet_->getValid(), true);
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094

    req = generatePktWithFqdn(DHCPREQUEST, Option4ClientFqdn::FLAG_S |
                              Option4ClientFqdn::FLAG_E,
                              "", Option4ClientFqdn::PARTIAL, true);

    ASSERT_NO_THROW(reply = srv_->processRequest(req));

    checkResponse(reply, DHCPACK, 1234);

    // Verify that there are no NameChangeRequests generated.
    ASSERT_EQ(0, d2_mgr_.getQueueSize());
1095 1096
}

1097 1098 1099 1100
// Test that server generates client's hostname from the IP address assigned
// to it when DHCPv4 Client FQDN option specifies an empty domain-name  AND
// ddns updates are disabled.
TEST_F(NameDhcpv4SrvTest, processRequestEmptyDomainNameDisabled) {
1101 1102 1103 1104
    // Create fake interfaces and open fake sockets.
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
    disableD2();
    Pkt4Ptr req = generatePktWithFqdn(DHCPREQUEST, Option4ClientFqdn::FLAG_S |
                                      Option4ClientFqdn::FLAG_E,
                                      "", Option4ClientFqdn::PARTIAL, true);
    Pkt4Ptr reply;
    ASSERT_NO_THROW(reply = srv_->processRequest(req));

    checkResponse(reply, DHCPACK, 1234);

    Option4ClientFqdnPtr fqdn = getClientFqdnOption(reply);
    ASSERT_TRUE(fqdn);

    // The hostname is generated from the IP address acquired (yiaddr).
    std::string hostname = generatedNameFromAddress(reply->getYiaddr());

    EXPECT_EQ(hostname, fqdn->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::FULL, fqdn->getDomainNameType());
}


1125 1126
// Test that server generates client's hostname from the IP address assigned
// to it when Hostname option carries the top level domain-name.
1127
TEST_F(NameDhcpv4SrvTest, processRequestTopLevelHostname) {
1128 1129 1130
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

1131
    Pkt4Ptr req = generatePktWithHostname(DHCPREQUEST, ".");
1132 1133
    // Set interface for the incoming