dhcp4_test_utils.cc 24.7 KB
Newer Older
1
// Copyright (C) 2013-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 7 8 9

#include <config.h>

#include <asiolink/io_address.h>
10
#include <cc/data.h>
11
#include <cc/command_interpreter.h>
12
#include <dhcp4/json_config_parser.h>
13
#include <dhcp4/tests/dhcp4_test_utils.h>
14
#include <dhcp/libdhcp++.h>
15
#include <dhcp/option4_addrlst.h>
16
#include <dhcp/option_int.h>
17 18 19
#include <dhcp/option_int_array.h>
#include <dhcp/option_custom.h>
#include <dhcp/iface_mgr.h>
20
#include <dhcp/tests/iface_mgr_test_config.h>
21
#include <dhcp/tests/pkt_captures.h>
22
#include <dhcpsrv/cfg_db_access.h>
23
#include <dhcpsrv/cfgmgr.h>
24
#include <dhcpsrv/lease.h>
25 26
#include <dhcpsrv/lease_mgr.h>
#include <dhcpsrv/lease_mgr_factory.h>
27
#include <log/logger_support.h>
28
#include <stats/stats_mgr.h>
29 30 31

using namespace std;
using namespace isc::asiolink;
32
using namespace isc::data;
33 34 35 36 37

namespace isc {
namespace dhcp {
namespace test {

38 39 40 41 42 43
BaseServerTest::BaseServerTest()
    : original_datadir_(CfgMgr::instance().getDataDir()) {
    CfgMgr::instance().setDataDir(TEST_DATA_BUILDDIR);
}

BaseServerTest::~BaseServerTest() {
44 45 46 47 48
    // Remove default lease file.
    std::ostringstream s2;
    s2 << CfgMgr::instance().getDataDir() << "/" << "kea-leases4.csv";
    static_cast<void>(::remove(s2.str().c_str()));

49 50
    // Revert to original data directory.
    CfgMgr::instance().setDataDir(original_datadir_);
51 52 53

    // Revert to unit test logging, in case the test reconfigured it.
    isc::log::initLogger();
54 55
}

56
Dhcpv4SrvTest::Dhcpv4SrvTest()
57
:rcode_(-1), srv_(0) {
58 59 60 61

    // Wipe any existing statistics
    isc::stats::StatsMgr::instance().removeAll();

62 63 64 65 66 67 68 69
    subnet_ = Subnet4Ptr(new Subnet4(IOAddress("192.0.2.0"), 24, 1000,
                                     2000, 3000));
    pool_ = Pool4Ptr(new Pool4(IOAddress("192.0.2.100"), IOAddress("192.0.2.110")));
    subnet_->addPool(pool_);

    // Add Router option.
    Option4AddrLstPtr opt_routers(new Option4AddrLst(DHO_ROUTERS));
    opt_routers->setAddress(IOAddress("192.0.2.2"));
70
    subnet_->getCfgOption()->add(opt_routers, false, DHCP4_OPTION_SPACE);
71 72

    CfgMgr::instance().clear();
73
    CfgMgr::instance().setFamily(AF_INET);
74 75
    CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnet_);
    CfgMgr::instance().commit();
76

77 78
    LibDHCP::clearRuntimeOptionDefs();

79 80
    // Let's wipe all existing statistics.
    isc::stats::StatsMgr::instance().removeAll();
81 82
}

83 84 85
Dhcpv4SrvTest::~Dhcpv4SrvTest() {

    // Make sure that we revert to default value
86
    CfgMgr::instance().clear();
87

88 89
    LibDHCP::clearRuntimeOptionDefs();

90 91
    // Let's wipe all existing statistics.
    isc::stats::StatsMgr::instance().removeAll();
92 93
}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
void Dhcpv4SrvTest::addPrlOption(Pkt4Ptr& pkt) {

    OptionUint8ArrayPtr option_prl =
        OptionUint8ArrayPtr(new OptionUint8Array(Option::V4,
                                                 DHO_DHCP_PARAMETER_REQUEST_LIST));

    // Let's request options that have been configured for the subnet.
    option_prl->addValue(DHO_DOMAIN_NAME_SERVERS);
    option_prl->addValue(DHO_DOMAIN_NAME);
    option_prl->addValue(DHO_LOG_SERVERS);
    option_prl->addValue(DHO_COOKIE_SERVERS);
    // Let's also request the option that hasn't been configured. In such
    // case server should ignore request for this particular option.
    option_prl->addValue(DHO_LPR_SERVERS);
    // And add 'Parameter Request List' option into the DISCOVER packet.
    pkt->addOption(option_prl);
}

void Dhcpv4SrvTest::configureRequestedOptions() {
    // dns-servers
    Option4AddrLstPtr
        option_dns_servers(new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS));
    option_dns_servers->addAddress(IOAddress("192.0.2.1"));
    option_dns_servers->addAddress(IOAddress("192.0.2.100"));
118
    ASSERT_NO_THROW(subnet_->getCfgOption()->add(option_dns_servers, false, DHCP4_OPTION_SPACE));
119 120 121 122 123

    // domain-name
    OptionDefinition def("domain-name", DHO_DOMAIN_NAME, OPT_FQDN_TYPE);
    OptionCustomPtr option_domain_name(new OptionCustom(def, Option::V4));
    option_domain_name->writeFqdn("example.com");
124
    subnet_->getCfgOption()->add(option_domain_name, false, DHCP4_OPTION_SPACE);
125 126 127 128 129

    // log-servers
    Option4AddrLstPtr option_log_servers(new Option4AddrLst(DHO_LOG_SERVERS));
    option_log_servers->addAddress(IOAddress("192.0.2.2"));
    option_log_servers->addAddress(IOAddress("192.0.2.10"));
130
    ASSERT_NO_THROW(subnet_->getCfgOption()->add(option_log_servers, false, DHCP4_OPTION_SPACE));
131 132 133 134

    // cookie-servers
    Option4AddrLstPtr option_cookie_servers(new Option4AddrLst(DHO_COOKIE_SERVERS));
    option_cookie_servers->addAddress(IOAddress("192.0.2.1"));
135
    ASSERT_NO_THROW(subnet_->getCfgOption()->add(option_cookie_servers, false, DHCP4_OPTION_SPACE));
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
}

void Dhcpv4SrvTest::messageCheck(const Pkt4Ptr& q, const Pkt4Ptr& a) {
    ASSERT_TRUE(q);
    ASSERT_TRUE(a);

    EXPECT_EQ(q->getHops(),   a->getHops());
    EXPECT_EQ(q->getIface(),  a->getIface());
    EXPECT_EQ(q->getIndex(),  a->getIndex());
    EXPECT_EQ(q->getGiaddr(), a->getGiaddr());
    // When processing an incoming packet the remote address
    // is copied as a src address, and the source address is
    // copied as a remote address to the response.
    EXPECT_TRUE(q->getLocalHWAddr() == a->getLocalHWAddr());
    EXPECT_TRUE(q->getRemoteHWAddr() == a->getRemoteHWAddr());

152 153
    // Check that the server identifier is present in the response.
    // Presence (or absence) of other options is checked elsewhere.
154 155 156
    EXPECT_TRUE(a->getOption(DHO_DHCP_SERVER_IDENTIFIER));

    // Check that something is offered
157
    EXPECT_NE("0.0.0.0", a->getYiaddr().toText());
158 159
}

160 161 162 163 164
::testing::AssertionResult
Dhcpv4SrvTest::basicOptionsPresent(const Pkt4Ptr& pkt) {
    std::ostringstream errmsg;
    errmsg << "option missing in the response";
    if (!pkt->getOption(DHO_DOMAIN_NAME)) {
165
        return (::testing::AssertionFailure(::testing::Message()
166
                                            << "domain-name " << errmsg.str()));
167 168

    } else if (!pkt->getOption(DHO_DOMAIN_NAME_SERVERS)) {
169
        return (::testing::AssertionFailure(::testing::Message()
170
                                            << "dns-servers " << errmsg.str()));
171 172

    } else if (!pkt->getOption(DHO_SUBNET_MASK)) {
173
        return (::testing::AssertionFailure(::testing::Message()
174
                                            << "subnet-mask " << errmsg.str()));
175 176

    } else if (!pkt->getOption(DHO_ROUTERS)) {
177
        return (::testing::AssertionFailure(::testing::Message() << "routers "
178
                                            << errmsg.str()));
179 180

    } else if (!pkt->getOption(DHO_DHCP_LEASE_TIME)) {
181
        return (::testing::AssertionFailure(::testing::Message() <<
182
                                            "dhcp-lease-time " << errmsg.str()));
183 184 185 186

    }
    return (::testing::AssertionSuccess());

187 188
}

189 190 191 192 193
::testing::AssertionResult
Dhcpv4SrvTest::noBasicOptions(const Pkt4Ptr& pkt) {
    std::ostringstream errmsg;
    errmsg << "option present in the response";
    if (pkt->getOption(DHO_DOMAIN_NAME)) {
194
        return (::testing::AssertionFailure(::testing::Message()
195
                                            << "domain-name " << errmsg.str()));
196 197

    } else if (pkt->getOption(DHO_DOMAIN_NAME_SERVERS)) {
198
        return (::testing::AssertionFailure(::testing::Message()
199
                                            << "dns-servers " << errmsg.str()));
200 201

    } else if (pkt->getOption(DHO_SUBNET_MASK)) {
202
        return (::testing::AssertionFailure(::testing::Message()
203
                                            << "subnet-mask " << errmsg.str()));
204 205

    } else if (pkt->getOption(DHO_ROUTERS)) {
206
        return (::testing::AssertionFailure(::testing::Message() << "routers "
207
                                            << errmsg.str()));
208 209

    } else if (pkt->getOption(DHO_DHCP_LEASE_TIME)) {
210
        return (::testing::AssertionFailure(::testing::Message()
211
                                            << "dhcp-lease-time " << errmsg.str()));
212 213 214 215 216 217 218 219 220 221

    }
    return (::testing::AssertionSuccess());
}

::testing::AssertionResult
Dhcpv4SrvTest::requestedOptionsPresent(const Pkt4Ptr& pkt) {
    std::ostringstream errmsg;
    errmsg << "option missing in the response";
    if (!pkt->getOption(DHO_LOG_SERVERS)) {
222
        return (::testing::AssertionFailure(::testing::Message()
223
                                            << "log-servers " << errmsg.str()));
224 225

    } else if (!pkt->getOption(DHO_COOKIE_SERVERS)) {
226
        return (::testing::AssertionFailure(::testing::Message()
227
                                            << "cookie-servers " << errmsg.str()));
228 229 230 231 232 233 234 235 236 237

    }
    return (::testing::AssertionSuccess());
}

::testing::AssertionResult
Dhcpv4SrvTest::noRequestedOptions(const Pkt4Ptr& pkt) {
    std::ostringstream errmsg;
    errmsg << "option present in the response";
    if (pkt->getOption(DHO_LOG_SERVERS)) {
238
        return (::testing::AssertionFailure(::testing::Message()
239
                                            << "log-servers " << errmsg.str()));
240 241

    } else if (pkt->getOption(DHO_COOKIE_SERVERS)) {
242
        return (::testing::AssertionFailure(::testing::Message()
243
                                            << "cookie-servers " << errmsg.str()));
244 245 246

    }
    return (::testing::AssertionSuccess());
247 248
}

249 250 251
OptionPtr Dhcpv4SrvTest::generateClientId(size_t size /*= 4*/) {

    OptionBuffer clnt_id(size);
252
    for (size_t i = 0; i < size; i++) {
253 254 255 256 257 258 259 260 261 262 263 264 265
        clnt_id[i] = 100 + i;
    }

    client_id_ = ClientIdPtr(new ClientId(clnt_id));

    return (OptionPtr(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER,
                                 clnt_id.begin(),
                                 clnt_id.begin() + size)));
}

HWAddrPtr Dhcpv4SrvTest::generateHWAddr(size_t size /*= 6*/) {
    const uint8_t hw_type = 123; // Just a fake number (typically 6=HTYPE_ETHER, see dhcp4.h)
    OptionBuffer mac(size);
266
    for (size_t i = 0; i < size; ++i) {
267 268 269 270 271
        mac[i] = 50 + i;
    }
    return (HWAddrPtr(new HWAddr(mac, hw_type)));
}

272 273 274
void Dhcpv4SrvTest::checkAddressParams(const Pkt4Ptr& rsp,
                                       const SubnetPtr subnet,
                                       bool t1_present,
275
                                       bool t2_present) {
276 277 278 279 280 281 282

    // Technically inPool implies inRange, but let's be on the safe
    // side and check both.
    EXPECT_TRUE(subnet->inRange(rsp->getYiaddr()));
    EXPECT_TRUE(subnet->inPool(Lease::TYPE_V4, rsp->getYiaddr()));

    // Check lease time
283 284
    OptionUint32Ptr opt = boost::dynamic_pointer_cast<
        OptionUint32>(rsp->getOption(DHO_DHCP_LEASE_TIME));
285
    if (!opt) {
286 287
        ADD_FAILURE() << "Lease time option missing in response or the"
            " option has unexpected type";
288
    } else {
289
        EXPECT_EQ(opt->getValue(), subnet->getValid());
290 291 292
    }

    // Check T1 timer
293 294 295
    opt = boost::dynamic_pointer_cast<
        OptionUint32>(rsp->getOption(DHO_DHCP_RENEWAL_TIME));
    if (t1_present) {
296 297
        ASSERT_TRUE(opt) << "Required T1 option missing or it has"
            " an unexpected type";
298
        EXPECT_EQ(opt->getValue(), subnet->getT1());
299
    } else {
300
        EXPECT_FALSE(opt);
301 302 303
    }

    // Check T2 timer
304 305 306
    opt = boost::dynamic_pointer_cast<
        OptionUint32>(rsp->getOption(DHO_DHCP_REBINDING_TIME));
    if (t2_present) {
307 308
        ASSERT_TRUE(opt) << "Required T2 option missing or it has"
            " an unexpected type";
309
        EXPECT_EQ(opt->getValue(), subnet->getT2());
310
    } else {
311
        EXPECT_FALSE(opt);
312 313 314
    }
}

315
void Dhcpv4SrvTest::checkResponse(const Pkt4Ptr& rsp, int expected_message_type,
316 317
                                  uint32_t expected_transid) {
    ASSERT_TRUE(rsp);
318 319
    EXPECT_EQ(expected_message_type,
              static_cast<int>(rsp->getType()));
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    EXPECT_EQ(expected_transid, rsp->getTransid());
}

Lease4Ptr Dhcpv4SrvTest::checkLease(const Pkt4Ptr& rsp,
                                    const OptionPtr& client_id,
                                    const HWAddrPtr&,
                                    const IOAddress& expected_addr) {

    ClientIdPtr id;
    if (client_id) {
        OptionBuffer data = client_id->getData();
        id.reset(new ClientId(data));
    }

    Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(expected_addr);
    if (!lease) {
336
        cout << "Lease for " << expected_addr
337 338 339 340
             << " not found in the database backend.";
        return (Lease4Ptr());
    }

341
    EXPECT_EQ(rsp->getYiaddr(), expected_addr);
342

343
    EXPECT_EQ(expected_addr, lease->addr_);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
    if (client_id) {
        EXPECT_TRUE(*lease->client_id_ == *id);
    }
    EXPECT_EQ(subnet_->getID(), lease->subnet_id_);

    return (lease);
}

void Dhcpv4SrvTest::checkServerId(const Pkt4Ptr& rsp, const OptionPtr& expected_srvid) {
    // Check that server included its server-id
    OptionPtr opt = rsp->getOption(DHO_DHCP_SERVER_IDENTIFIER);
    ASSERT_TRUE(opt);
    EXPECT_EQ(opt->getType(), expected_srvid->getType() );
    EXPECT_EQ(opt->len(), expected_srvid->len() );
    EXPECT_TRUE(opt->getData() == expected_srvid->getData());
}

void Dhcpv4SrvTest::checkClientId(const Pkt4Ptr& rsp, const OptionPtr& expected_clientid) {
362

363 364
    bool include_clientid =
        CfgMgr::instance().getCurrentCfg()->getEchoClientId();
365

366 367
    // check that server included our own client-id
    OptionPtr opt = rsp->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
368 369 370 371 372 373 374 375 376 377
    if (include_clientid) {
        // Normal mode: echo back (see RFC6842)
        ASSERT_TRUE(opt);
        EXPECT_EQ(expected_clientid->getType(), opt->getType());
        EXPECT_EQ(expected_clientid->len(), opt->len());
        EXPECT_TRUE(expected_clientid->getData() == opt->getData());
    } else {
        // Backward compatibility mode for pre-RFC6842 devices
        ASSERT_FALSE(opt);
    }
378 379
}

380 381 382 383 384 385 386 387 388
::testing::AssertionResult
Dhcpv4SrvTest::createPacketFromBuffer(const Pkt4Ptr& src_pkt,
                                      Pkt4Ptr& dst_pkt) {
    // Create on-wire format of the packet. If pack() has been called
    // on this instance of the packet already, the next call to pack()
    // should remove all contents of the output buffer.
    try {
        src_pkt->pack();
    } catch (const Exception& ex) {
389 390 391
        return (::testing::AssertionFailure(::testing::Message()
                                            << "Failed to parse source packet: "
                                            << ex.what()));
392 393 394 395 396 397 398 399 400
    }
    // Get the output buffer from the source packet.
    const util::OutputBuffer& buf = src_pkt->getBuffer();
    // Create a copy of the packet using the output buffer from the source
    // packet.
    try {
        dst_pkt.reset(new Pkt4(static_cast<const uint8_t*>(buf.getData()),
                               buf.getLength()));
    } catch (const Exception& ex) {
401 402 403 404 405
        return (::testing::AssertionFailure(::testing::Message()
                                            << "Failed to create a"
                                            " destination packet from"
                                            " the buffer: "
                                            << ex.what()));
406 407 408 409 410 411
    }

    try {
        // Parse the new packet and return to the caller.
        dst_pkt->unpack();
    } catch (const Exception& ex) {
412 413 414 415
        return (::testing::AssertionFailure(::testing::Message()
                                            << "Failed to parse a"
                                            << " destination packet: "
                                            << ex.what()));
416 417 418 419 420
    }

    return (::testing::AssertionSuccess());
}

421 422 423
void
// cppcheck-suppress unusedFunction
Dhcpv4SrvTest::TearDown() {
424

425
    CfgMgr::instance().clear();
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

    // Close all open sockets.
    IfaceMgr::instance().closeSockets();

    // Some unit tests override the default packet filtering class, used
    // by the IfaceMgr. The dummy class, called PktFilterTest, reports the
    // capability to directly respond to the clients without IP address
    // assigned. This capability is not supported by the default packet
    // filtering class: PktFilterInet. Therefore setting the dummy class
    // allows to test scenarios, when server responds to the broadcast address
    // on client's request, despite having support for direct response.
    // The following call restores the use of original packet filtering class
    // after the test.
    try {
        IfaceMgr::instance().setPacketFilter(PktFilterPtr(new PktFilterInet()));

    } catch (const Exception& ex) {
        FAIL() << "Failed to restore the default (PktFilterInet) packet filtering"
               << " class after the test. Exception has been caught: "
               << ex.what();
    }

}

void
451 452 453
Dhcpv4SrvTest::testDiscoverRequest(const uint8_t msg_type) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();
454

455 456 457 458 459
    // Create an instance of the tested class.
    boost::scoped_ptr<NakedDhcpv4Srv> srv(new NakedDhcpv4Srv(0));

    // Initialize the source HW address.
    vector<uint8_t> mac(6);
460
    for (uint8_t i = 0; i < 6; ++i) {
461 462 463 464
        mac[i] = i * 10;
    }
    // Initialized the destination HW address.
    vector<uint8_t> dst_mac(6);
465
    for (uint8_t i = 0; i < 6; ++i) {
466 467 468 469 470
        dst_mac[i] = i * 20;
    }
    // Create a DHCP message. It will be used to simulate the
    // incoming message.
    boost::shared_ptr<Pkt4> req(new Pkt4(msg_type, 1234));
Francis Dupont's avatar
Francis Dupont committed
471
    // Create a response message. It will hold a response packet.
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
    // Initially, set it to NULL.
    boost::shared_ptr<Pkt4> rsp;
    // Set the name of the interface on which packet is received.
    req->setIface("eth0");
    // Set the interface index. It is just a dummy value and will
    // not be interpreted.
    req->setIndex(17);
    // Set the target HW address. This value is normally used to
    // construct the data link layer header.
    req->setRemoteHWAddr(1, 6, dst_mac);
    // Set the HW address. This value is set on DHCP level (in chaddr).
    req->setHWAddr(1, 6, mac);
    // Set local HW address. It is used to construct the data link layer
    // header.
    req->setLocalHWAddr(1, 6, mac);
    // Set target IP address.
    req->setRemoteAddr(IOAddress("192.0.2.55"));
489
    // Set relay address and hops.
490
    req->setGiaddr(IOAddress("192.0.2.10"));
491
    req->setHops(1);
492 493 494 495 496 497 498

    // We are going to test that certain options are returned
    // in the response message when requested using 'Parameter
    // Request List' option. Let's configure those options that
    // are returned when requested.
    configureRequestedOptions();

499 500 501 502 503
    // Create a copy of the original packet by parsing its wire format.
    // This simulates the real life scenario when we process the packet
    // which was parsed from its wire format.
    Pkt4Ptr received;
    ASSERT_TRUE(createPacketFromBuffer(req, received));
504 505
    // Set interface. It is required for the server to generate server id.
    received->setIface("eth0");
506 507
    if (msg_type == DHCPDISCOVER) {
        ASSERT_NO_THROW(
508
            rsp = srv->processDiscover(received);
509
        );
510 511 512 513 514 515

        // Should return OFFER
        ASSERT_TRUE(rsp);
        EXPECT_EQ(DHCPOFFER, rsp->getType());

    } else {
516
        ASSERT_NO_THROW(rsp = srv->processRequest(received));
517 518 519 520 521 522 523

        // Should return ACK
        ASSERT_TRUE(rsp);
        EXPECT_EQ(DHCPACK, rsp->getType());

    }

524
    messageCheck(received, rsp);
525

526 527
    // Basic options should be present when we got the lease.
    EXPECT_TRUE(basicOptionsPresent(rsp));
528 529
    // We did not request any options so these should not be present
    // in the RSP.
530
    EXPECT_TRUE(noRequestedOptions(rsp));
531 532 533 534 535

    // Repeat the test but request some options.
    // Add 'Parameter Request List' option.
    addPrlOption(req);

536 537 538
    ASSERT_TRUE(createPacketFromBuffer(req, received));
    ASSERT_TRUE(received->getOption(DHO_DHCP_PARAMETER_REQUEST_LIST));

539 540 541
    // Set interface. It is required for the server to generate server id.
    received->setIface("eth0");

542
    if (msg_type == DHCPDISCOVER) {
543
        ASSERT_NO_THROW(rsp = srv->processDiscover(received));
544 545 546 547 548 549

        // Should return non-NULL packet.
        ASSERT_TRUE(rsp);
        EXPECT_EQ(DHCPOFFER, rsp->getType());

    } else {
550
        ASSERT_NO_THROW(rsp = srv->processRequest(received));
551 552 553 554 555 556 557

        // Should return non-NULL packet.
        ASSERT_TRUE(rsp);
        EXPECT_EQ(DHCPACK, rsp->getType());
    }

    // Check that the requested options are returned.
558 559
    EXPECT_TRUE(basicOptionsPresent(rsp));
    EXPECT_TRUE(requestedOptionsPresent(rsp));
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

    // The following part of the test will test that the NAK is sent when
    // there is no address pool configured. In the same time, we expect
    // that the requested options are not included in NAK message, but that
    // they are only included when yiaddr is set to non-zero value.
    ASSERT_NO_THROW(subnet_->delPools(Lease::TYPE_V4));

    // There has been a lease allocated for the particular client. So,
    // even though we deleted the subnet, the client would get the
    // existing lease (not a NAK). Therefore, we have to change the chaddr
    // in the packet so as the existing lease is not returned.
    req->setHWAddr(1, 6, std::vector<uint8_t>(2, 6));
    ASSERT_TRUE(createPacketFromBuffer(req, received));
    ASSERT_TRUE(received->getOption(DHO_DHCP_PARAMETER_REQUEST_LIST));

575 576 577
    // Set interface. It is required for the server to generate server id.
    received->setIface("eth0");

578 579
    if (msg_type == DHCPDISCOVER) {
        ASSERT_NO_THROW(rsp = srv->processDiscover(received));
580 581 582
        // Should return NULL packet.
        ASSERT_FALSE(rsp);

583 584 585 586
    } else {
        ASSERT_NO_THROW(rsp = srv->processRequest(received));
        // Should return non-NULL packet.
        ASSERT_TRUE(rsp);
587 588 589 590 591 592 593 594 595
        // We should get the NAK packet with yiaddr set to 0.
        EXPECT_EQ(DHCPNAK, rsp->getType());
        ASSERT_EQ("0.0.0.0", rsp->getYiaddr().toText());

        // Make sure that none of the requested options is returned in NAK.
        // Also options such as Routers or Subnet Mask should not be there,
        // because lease hasn't been acquired.
        EXPECT_TRUE(noRequestedOptions(rsp));
        EXPECT_TRUE(noBasicOptions(rsp));
596
    }
597 598
}

599
void
600 601
Dhcpv4SrvTest::configure(const std::string& config, const bool commit) {
    configure(config, srv_, commit);
602 603 604
}

void
605 606
Dhcpv4SrvTest::configure(const std::string& config, NakedDhcpv4Srv& srv,
                         const bool commit) {
607 608
    ConstElementPtr json;
    ASSERT_NO_THROW(json = parseJSON(config));
609 610 611
    ConstElementPtr status;

    // Configure the server and make sure the config is accepted
612
    EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
613 614 615 616
    ASSERT_TRUE(status);
    int rcode;
    ConstElementPtr comment = config::parseAnswer(rcode, status);
    ASSERT_EQ(0, rcode);
617

618 619 620 621 622 623 624
    // Use specified lease database backend.
    ASSERT_NO_THROW( {
        CfgDbAccessPtr cfg_db = CfgMgr::instance().getStagingCfg()->getCfgDbAccess();
        cfg_db->setAppendedParameters("universe=4");
        cfg_db->createManagers();
    } );

625 626 627
    if (commit) {
        CfgMgr::instance().commit();
    }
628 629
 }

630
Dhcpv4Exchange
631
Dhcpv4SrvTest::createExchange(const Pkt4Ptr& query) {
632
    return (Dhcpv4Exchange(srv_.alloc_engine_, query, srv_.selectSubnet(query)));
633
}
634

635 636 637 638 639 640 641 642 643 644 645
void
Dhcpv4SrvTest::pretendReceivingPkt(NakedDhcpv4Srv& srv, const std::string& config,
                                   uint8_t pkt_type, const std::string& stat_name) {

    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

    // Apply the configuration we just received.
    configure(config);

    // Let's just use one of the actual captured packets that we have.
646
    Pkt4Ptr pkt = PktCaptures::captureRelayedDiscover();
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

    // We just need to tweak it a it, to pretend that it's type is as desired.
    // Note that when receiving a packet, its on-wire form is stored in data_
    // field. Most methods (including setType()) operates on option objects
    // (objects stored in options_ after unpack() is called). Finally, outgoing
    // packets are stored in out_buffer_. So we need to go through the full
    // unpack/tweak/pack cycle and do repack, i.e. move the output buffer back
    // to incoming buffer.
    pkt->unpack();
    pkt->setType(pkt_type); // Set message type.
    pkt->pack();
    pkt->data_.resize(pkt->getBuffer().getLength());
    // Copy out_buffer_ to data_ to pretend that it's what was just received.
    memcpy(&pkt->data_[0], pkt->getBuffer().getData(), pkt->getBuffer().getLength());

    // Simulate that we have received that traffic
    srv.fakeReceive(pkt);
    srv.run();

    using namespace isc::stats;
    StatsMgr& mgr = StatsMgr::instance();
    ObservationPtr pkt4_rcvd = mgr.getObservation("pkt4-received");
    ObservationPtr tested_stat = mgr.getObservation(stat_name);

Andrei Pavel's avatar
Andrei Pavel committed
671
    // All expected statistics must be present.
672 673 674 675 676 677 678
    ASSERT_TRUE(pkt4_rcvd);
    ASSERT_TRUE(tested_stat);

    // They also must have expected values.
    EXPECT_EQ(1, pkt4_rcvd->getInteger().first);
    EXPECT_EQ(1, tested_stat->getInteger().first);
}
679

680 681 682
}; // end of isc::dhcp::test namespace
}; // end of isc::dhcp namespace
}; // end of isc namespace