dhcp4_srv_unittest.cc 134 KB
Newer Older
1
// Copyright (C) 2011-2014 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
// 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 <sstream>

18
#include <asiolink/io_address.h>
19
#include <config/ccsession.h>
20
#include <dhcp4/tests/dhcp4_test_utils.h>
21
#include <dhcp/dhcp4.h>
22
#include <dhcp/iface_mgr.h>
23
#include <dhcp/option.h>
24
#include <dhcp/option_int.h>
25
#include <dhcp/option4_addrlst.h>
26
#include <dhcp/option_custom.h>
27
#include <dhcp/option_int_array.h>
28
#include <dhcp/option_vendor.h>
29 30
#include <dhcp/pkt_filter.h>
#include <dhcp/pkt_filter_inet.h>
31
#include <dhcp/docsis3_option_defs.h>
32
#include <dhcp/tests/iface_mgr_test_config.h>
33
#include <dhcp4/dhcp4_srv.h>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
34
#include <dhcp4/dhcp4_log.h>
35
#include <dhcp4/json_config_parser.h>
36
#include <hooks/server_hooks.h>
37 38 39 40
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/lease_mgr.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <dhcpsrv/utils.h>
41
#include <gtest/gtest.h>
42 43
#include <hooks/server_hooks.h>
#include <hooks/hooks_manager.h>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
44
#include <config/ccsession.h>
45

46 47
#include <boost/scoped_ptr.hpp>

48 49 50
#include <iostream>

#include <arpa/inet.h>
51 52 53 54

using namespace std;
using namespace isc;
using namespace isc::dhcp;
55
using namespace isc::data;
56
using namespace isc::asiolink;
57
using namespace isc::hooks;
58
using namespace isc::dhcp::test;
59 60 61

namespace {

62 63 64
// This test verifies that the destination address of the response
// message is set to giaddr, when giaddr is set to non-zero address
// in the received message.
65 66 67 68
TEST_F(Dhcpv4SrvTest, adjustIfaceDataRelay) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

69
    // Create the instance of the incoming packet.
70
    boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
71 72
    // Set the giaddr to non-zero address and hops to non-zero value
    // as if it was relayed.
73
    req->setGiaddr(IOAddress("192.0.1.1"));
74
    req->setHops(2);
75 76
    // Set ciaddr to zero. This simulates the client which applies
    // for the new lease.
77
    req->setCiaddr(IOAddress("0.0.0.0"));
78 79 80
    // Clear broadcast flag.
    req->setFlags(0x0000);

81
    // Set local address, port and interface.
82
    req->setLocalAddr(IOAddress("192.0.2.1"));
83
    req->setLocalPort(1001);
84
    req->setIface("eth1");
85 86
    req->setIndex(1);

87 88 89 90
    // Create a response packet. Assume that the new lease have
    // been created and new address allocated. This address is
    // stored in yiaddr field.
    boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
91
    resp->setYiaddr(IOAddress("192.0.1.100"));
92 93
    // Clear the remote address.
    resp->setRemoteAddr(IOAddress("0.0.0.0"));
94 95
    // Set hops value for the response.
    resp->setHops(req->getHops());
96

97
    // This function never throws.
98
    ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
99

100
    // Now the destination address should be relay's address.
101
    EXPECT_EQ("192.0.1.1", resp->getRemoteAddr().toText());
102 103
    // The query has been relayed, so the response must be sent to the port 67.
    EXPECT_EQ(DHCP4_SERVER_PORT, resp->getRemotePort());
104 105
    // Local address should be the address assigned to interface eth1.
    EXPECT_EQ("192.0.2.3", resp->getLocalAddr().toText());
106 107 108 109
    // The local port is always DHCPv4 server port 67.
    EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
    // We will send response over the same interface which was used to receive
    // query.
110
    EXPECT_EQ("eth1", resp->getIface());
111
    EXPECT_EQ(1, resp->getIndex());
112 113 114 115

    // Let's do another test and set other fields: ciaddr and
    // flags. By doing it, we want to make sure that the relay
    // address will take precedence.
116 117
    req->setGiaddr(IOAddress("192.0.1.50"));
    req->setCiaddr(IOAddress("192.0.1.11"));
118
    req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
119

120
    resp->setYiaddr(IOAddress("192.0.1.100"));
121 122 123
    // Clear remote address.
    resp->setRemoteAddr(IOAddress("0.0.0.0"));

124
    ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
125 126

    // Response should be sent back to the relay address.
127
    EXPECT_EQ("192.0.1.50", resp->getRemoteAddr().toText());
128 129
}

130 131 132 133
// This test verifies that the destination address of the response message
// is set to ciaddr when giaddr is set to zero and the ciaddr is set to
// non-zero address in the received message. This is the case when the
// client is in Renew or Rebind state.
134 135 136 137
TEST_F(Dhcpv4SrvTest, adjustIfaceDataRenew) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

138 139 140 141 142 143 144 145
    // Create instance of the incoming packet.
    boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));

    // Clear giaddr to simulate direct packet.
    req->setGiaddr(IOAddress("0.0.0.0"));
    // Set ciaddr to non-zero address. The response should be sent to this
    // address as the client is in renewing or rebinding state (it is fully
    // configured).
146
    req->setCiaddr(IOAddress("192.0.1.15"));
147 148 149 150 151 152
    // Let's configure broadcast flag. It should be ignored because
    // we are responding directly to the client having an address
    // and trying to extend his lease. Broadcast flag is only used
    // when new lease is acquired and server must make a decision
    // whether to unicast the response to the acquired address or
    // broadcast it.
153
    req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
154 155 156
    // This is a direct message, so the hops should be cleared.
    req->setHops(0);
    // Set local unicast address as if we are renewing a lease.
157
    req->setLocalAddr(IOAddress("192.0.2.1"));
158 159 160
    // Request is received on the DHCPv4 server port.
    req->setLocalPort(DHCP4_SERVER_PORT);
    // Set the interface. The response should be sent over the same interface.
161
    req->setIface("eth1");
162
    req->setIndex(1);
163 164 165 166 167 168 169

    // Create a response.
    boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
    // Let's extend the lease for the client in such a way that
    // it will actually get different address. The response
    // should not be sent to this address but rather to ciaddr
    // as client still have ciaddr configured.
170
    resp->setYiaddr(IOAddress("192.0.1.13"));
171 172
    // Clear the remote address.
    resp->setRemoteAddr(IOAddress("0.0.0.0"));
173 174
    // Copy hops value from the query.
    resp->setHops(req->getHops());
175

176
    ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
177 178

    // Check that server responds to ciaddr
179
    EXPECT_EQ("192.0.1.15", resp->getRemoteAddr().toText());
180 181 182 183 184
    // The query was non-relayed, so the response should be sent to a DHCPv4
    // client port 68.
    EXPECT_EQ(DHCP4_CLIENT_PORT, resp->getRemotePort());
    // The response should be sent from the unicast address on which the
    // query has been received.
185
    EXPECT_EQ("192.0.2.3", resp->getLocalAddr().toText());
186 187 188
    // The response should be sent from the DHCPv4 server port.
    EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
    // The interface data should match the data in the query.
189
    EXPECT_EQ("eth1", resp->getIface());
190 191
    EXPECT_EQ(1, resp->getIndex());

192 193
}

194 195 196 197 198 199 200
// This test verifies that the destination address of the response message
// is set correctly when giaddr and ciaddr is zeroed in the received message
// and the new lease is acquired. The lease address is carried in the
// response message in the yiaddr field. In this case destination address
// of the response should be set to yiaddr if server supports direct responses
// to the client which doesn't have an address yet or broadcast if the server
// doesn't support direct responses.
201 202 203 204
TEST_F(Dhcpv4SrvTest, adjustIfaceDataSelect) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

205 206 207 208 209 210 211 212 213 214 215
    // Create instance of the incoming packet.
    boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));

    // Clear giaddr to simulate direct packet.
    req->setGiaddr(IOAddress("0.0.0.0"));
    // Clear client address as it hasn't got any address configured yet.
    req->setCiaddr(IOAddress("0.0.0.0"));

    // Let's clear the broadcast flag.
    req->setFlags(0);

216 217 218 219 220 221 222
    // This is a non-relayed message, so let's clear hops count.
    req->setHops(0);
    // The query is sent to the broadcast address in the Select state.
    req->setLocalAddr(IOAddress("255.255.255.255"));
    // The query has been received on the DHCPv4 server port 67.
    req->setLocalPort(DHCP4_SERVER_PORT);
    // Set the interface. The response should be sent via the same interface.
223
    req->setIface("eth1");
224 225
    req->setIndex(1);

226 227 228
    // Create a response.
    boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
    // Assign some new address for this client.
229
    resp->setYiaddr(IOAddress("192.0.1.13"));
230 231
    // Clear the remote address.
    resp->setRemoteAddr(IOAddress("0.0.0.0"));
232 233 234 235 236 237 238 239
    // Copy hops count.
    resp->setHops(req->getHops());

    // We want to test the case, when the server (packet filter) doesn't support
    // ddirect responses to the client which doesn't have an address yet. In
    // case, the server should send its response to the broadcast address.
    // We can control whether the current packet filter returns that its support
    // direct responses or not.
240
    test_config.setDirectResponse(false);
241 242 243 244 245 246 247

    // When running unit tests, the IfaceMgr is using the default Packet
    // Filtering class, PktFilterInet. This class does not support direct
    // responses to clients without address assigned. When giaddr and ciaddr
    // are zero and client has just got new lease, the assigned address is
    // carried in yiaddr. In order to send this address to the client,
    // server must broadcast its response.
248
    ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
249 250 251 252 253

    // Check that the response is sent to broadcast address as the
    // server doesn't have capability to respond directly.
    EXPECT_EQ("255.255.255.255", resp->getRemoteAddr().toText());

254 255 256
    // Although the query has been sent to the broadcast address, the
    // server should select a unicast address on the particular interface
    // as a source address for the response.
257
    EXPECT_EQ("192.0.2.3", resp->getLocalAddr().toText());
258 259 260 261 262 263

    // The response should be sent from the DHCPv4 server port.
    EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());

    // The response should be sent via the same interface through which
    // query has been received.
264
    EXPECT_EQ("eth1", resp->getIface());
265 266
    EXPECT_EQ(1, resp->getIndex());

267 268 269
    // We also want to test the case when the server has capability to
    // respond directly to the client which is not configured. Server
    // makes decision whether it responds directly or broadcast its
270 271 272
    // response based on the capability reported by IfaceMgr. We can
    // control whether the current packet filter returns that it supports
    // direct responses or not.
273
    test_config.setDirectResponse(true);
274 275 276

    // Now we expect that the server will send its response to the
    // address assigned for the client.
277
    ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
278

279
    EXPECT_EQ("192.0.1.13", resp->getRemoteAddr().toText());
280 281
}

282 283 284 285 286
// This test verifies that the destination address of the response message
// is set to broadcast address when client set broadcast flag in its
// query. Client sets this flag to indicate that it can't receive direct
// responses from the server when it doesn't have its interface configured.
// Server must respect broadcast flag.
287 288 289 290
TEST_F(Dhcpv4SrvTest, adjustIfaceDataBroadcast) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

291 292 293 294 295 296 297
    // Create instance of the incoming packet.
    boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));

    // Clear giaddr to simulate direct packet.
    req->setGiaddr(IOAddress("0.0.0.0"));
    // Clear client address as it hasn't got any address configured yet.
    req->setCiaddr(IOAddress("0.0.0.0"));
298 299 300 301 302
    // The query is sent to the broadcast address in the Select state.
    req->setLocalAddr(IOAddress("255.255.255.255"));
    // The query has been received on the DHCPv4 server port 67.
    req->setLocalPort(DHCP4_SERVER_PORT);
    // Set the interface. The response should be sent via the same interface.
303
    req->setIface("eth1");
304
    req->setIndex(1);
305 306

    // Let's set the broadcast flag.
307
    req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
308 309 310 311

    // Create a response.
    boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
    // Assign some new address for this client.
312
    resp->setYiaddr(IOAddress("192.0.1.13"));
313 314 315 316

    // Clear the remote address.
    resp->setRemoteAddr(IOAddress("0.0.0.0"));

317
    ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
318 319 320 321

    // Server must repond to broadcast address when client desired that
    // by setting the broadcast flag in its request.
    EXPECT_EQ("255.255.255.255", resp->getRemoteAddr().toText());
322 323 324 325

    // Although the query has been sent to the broadcast address, the
    // server should select a unicast address on the particular interface
    // as a source address for the response.
326
    EXPECT_EQ("192.0.2.3", resp->getLocalAddr().toText());
327 328 329 330 331 332

    // The response should be sent from the DHCPv4 server port.
    EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());

    // The response should be sent via the same interface through which
    // query has been received.
333
    EXPECT_EQ("eth1", resp->getIface());
334 335 336 337
    EXPECT_EQ(1, resp->getIndex());

}

338 339
// This test verifies that exception is thrown of the invalid combination
// of giaddr and hops is specified in a client's message.
340
TEST_F(Dhcpv4SrvTest, adjustIfaceDataInvalid) {
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));

    // The hops and giaddr values are used to determine if the client's
    // message has been relayed or sent directly. The allowed combinations
    // are (giaddr = 0 and hops = 0) or (giaddr != 0 and hops != 0). Any
    // other combination is invalid and the adjustIfaceData should throw
    // an exception. We will test that exception is indeed thrown.
    req->setGiaddr(IOAddress("0.0.0.0"));
    req->setHops(1);

    // Clear client address as it hasn't got any address configured yet.
    req->setCiaddr(IOAddress("0.0.0.0"));
    // The query is sent to the broadcast address in the Select state.
    req->setLocalAddr(IOAddress("255.255.255.255"));
    // The query has been received on the DHCPv4 server port 67.
    req->setLocalPort(DHCP4_SERVER_PORT);
    // Set the interface. The response should be sent via the same interface.
    req->setIface("eth0");
    req->setIndex(1);

    // Create a response.
    boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
    // Assign some new address for this client.
    resp->setYiaddr(IOAddress("192.0.2.13"));

    // Clear the remote address.
    resp->setRemoteAddr(IOAddress("0.0.0.0"));

    EXPECT_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp), isc::BadValue);
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
// This test verifies that the server identifier option is appended to
// a specified DHCPv4 message and the server identifier is correct.
TEST_F(Dhcpv4SrvTest, appendServerID) {
    Pkt4Ptr response(new Pkt4(DHCPDISCOVER, 1234));
    // Set a local address. It is required by the function under test
    // to create the Server Identifier option.
    response->setLocalAddr(IOAddress("192.0.3.1"));

    // Append the Server Identifier.
    ASSERT_NO_THROW(NakedDhcpv4Srv::appendServerID(response));

    // Make sure that the option has been added.
    OptionPtr opt = response->getOption(DHO_DHCP_SERVER_IDENTIFIER);
    ASSERT_TRUE(opt);
    Option4AddrLstPtr opt_server_id =
        boost::dynamic_pointer_cast<Option4AddrLst>(opt);
    ASSERT_TRUE(opt_server_id);

    // The option is represented as a list of IPv4 addresses but with
    // only one address added.
    Option4AddrLst::AddressContainer addrs = opt_server_id->getAddresses();
    ASSERT_EQ(1, addrs.size());
    // This address should match the local address of the packet.
    EXPECT_EQ("192.0.3.1", addrs[0].toText());
396 397
}

398 399 400 401 402 403
// Sanity check. Verifies that both Dhcpv4Srv and its derived
// class NakedDhcpv4Srv can be instantiated and destroyed.
TEST_F(Dhcpv4SrvTest, basic) {

    // Check that the base class can be instantiated
    boost::scoped_ptr<Dhcpv4Srv> srv;
404 405
    ASSERT_NO_THROW(srv.reset(new Dhcpv4Srv(DHCP4_SERVER_PORT + 10000, false,
                                            false)));
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    srv.reset();
    // We have to close open sockets because further in this test we will
    // call the Dhcpv4Srv constructor again. This constructor will try to
    // set the appropriate packet filter class for IfaceMgr. This requires
    // that all sockets are closed.
    IfaceMgr::instance().closeSockets();

    // Check that the derived class can be instantiated
    boost::scoped_ptr<NakedDhcpv4Srv> naked_srv;
    ASSERT_NO_THROW(
        naked_srv.reset(new NakedDhcpv4Srv(DHCP4_SERVER_PORT + 10000)));
    // Close sockets again for the next test.
    IfaceMgr::instance().closeSockets();

    ASSERT_NO_THROW(naked_srv.reset(new NakedDhcpv4Srv(0)));
}

// This test verifies that exception is not thrown when an error occurs during
// opening sockets. This test forces an error by adding a fictious interface
// to the IfaceMgr. An attempt to open socket on this interface must always
// fail. The DHCPv4 installs the error handler function to prevent exceptions
// being thrown from the openSockets4 function.
// @todo The server tests for socket should be extended but currently the
// ability to unit test the sockets code is somewhat limited.
TEST_F(Dhcpv4SrvTest, openActiveSockets) {
    ASSERT_NO_THROW(CfgMgr::instance().activateAllIfaces());

    Iface iface("bogusiface", 255);
    iface.flag_loopback_ = false;
    iface.flag_up_ = true;
    iface.flag_running_ = true;
    iface.inactive4_ = false;
    iface.addAddress(IOAddress("192.0.0.0"));
    IfaceMgr::instance().addInterface(iface);
    ASSERT_NO_THROW(Dhcpv4Srv::openActiveSockets(DHCP4_SERVER_PORT, false));
441 442
}

443
// Verifies that DISCOVER message can be processed correctly,
Tomek Mrugalski's avatar
Tomek Mrugalski committed
444 445 446 447 448 449 450
// that the OFFER message generated in response is valid and
// contains necessary options.
//
// Note: this test focuses on the packet correctness. There
// are other tests that verify correctness of the allocation
// engine. See DiscoverBasic, DiscoverHint, DiscoverNoClientId
// and DiscoverInvalidHint.
451
TEST_F(Dhcpv4SrvTest, processDiscover) {
452
    testDiscoverRequest(DHCPDISCOVER);
453
}
454

455
// Verifies that REQUEST message can be processed correctly,
456
// that the OFFER message generated in response is valid and
Tomek Mrugalski's avatar
Tomek Mrugalski committed
457 458 459 460
// contains necessary options.
//
// Note: this test focuses on the packet correctness. There
// are other tests that verify correctness of the allocation
461 462
// engine. See DiscoverBasic, DiscoverHint, DiscoverNoClientId
// and DiscoverInvalidHint.
463
TEST_F(Dhcpv4SrvTest, processRequest) {
464
    testDiscoverRequest(DHCPREQUEST);
465 466
}

467
TEST_F(Dhcpv4SrvTest, processRelease) {
468
    NakedDhcpv4Srv srv;
469
    Pkt4Ptr pkt(new Pkt4(DHCPRELEASE, 1234));
470

471
    // Should not throw
472
    EXPECT_NO_THROW(srv.processRelease(pkt));
473 474
}

475
TEST_F(Dhcpv4SrvTest, processDecline) {
476
    NakedDhcpv4Srv srv;
477
    Pkt4Ptr pkt(new Pkt4(DHCPDECLINE, 1234));
478

479
    // Should not throw
480
    EXPECT_NO_THROW(srv.processDecline(pkt));
481 482
}

483 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 509 510 511 512 513 514
TEST_F(Dhcpv4SrvTest, serverReceivedPacketName) {
    // Check all possible packet types
    for (int itype = 0; itype < 256; ++itype) {
        uint8_t type = itype;

        switch (type) {
        case DHCPDECLINE:
            EXPECT_STREQ("DECLINE", Dhcpv4Srv::serverReceivedPacketName(type));
            break;

        case DHCPDISCOVER:
            EXPECT_STREQ("DISCOVER", Dhcpv4Srv::serverReceivedPacketName(type));
            break;

        case DHCPINFORM:
            EXPECT_STREQ("INFORM", Dhcpv4Srv::serverReceivedPacketName(type));
            break;

        case DHCPRELEASE:
            EXPECT_STREQ("RELEASE", Dhcpv4Srv::serverReceivedPacketName(type));
            break;

        case DHCPREQUEST:
            EXPECT_STREQ("REQUEST", Dhcpv4Srv::serverReceivedPacketName(type));
            break;

        default:
            EXPECT_STREQ("UNKNOWN", Dhcpv4Srv::serverReceivedPacketName(type));
        }
    }
}

515 516 517 518 519 520 521 522 523 524 525
// This test verifies that incoming DISCOVER can be handled properly, that an
// OFFER is generated, that the response has an address and that address
// really belongs to the configured pool.
//
// constructed very simple DISCOVER message with:
// - client-id option
//
// expected returned OFFER message:
// - copy of client-id
// - server-id
// - offered address
526 527 528 529
TEST_F(Dhcpv4SrvTest, DiscoverBasic) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

530
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
531
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
532 533 534 535 536

    Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
    dis->setRemoteAddr(IOAddress("192.0.2.1"));
    OptionPtr clientid = generateClientId();
    dis->addOption(clientid);
537
    dis->setIface("eth1");
538

539
    // Pass it to the server and get an offer
540 541
    Pkt4Ptr offer = srv->processDiscover(dis);

542
    // Check if we get response at all
543 544
    checkResponse(offer, DHCPOFFER, 1234);

545
    // Check that address was returned from proper range, that its lease
546
    // lifetime is correct, that T1 and T2 are returned properly
547
    checkAddressParams(offer, subnet_, true, true);
548

549
    // Check identifiers
550 551 552 553
    checkServerId(offer, srv->getServerID());
    checkClientId(offer, clientid);
}

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
// Check that option 58 and 59 are not included if they are not specified.
TEST_F(Dhcpv4SrvTest, DiscoverNoTimers) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

    boost::scoped_ptr<NakedDhcpv4Srv> srv;
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));

    Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
    dis->setRemoteAddr(IOAddress("192.0.2.1"));
    OptionPtr clientid = generateClientId();
    dis->addOption(clientid);
    dis->setIface("eth1");

    // Recreate a subnet but set T1 and T2 to "unspecified".
    subnet_.reset(new Subnet4(IOAddress("192.0.2.0"), 24,
                              Triplet<uint32_t>(),
                              Triplet<uint32_t>(),
                              3000));
    pool_ = Pool4Ptr(new Pool4(IOAddress("192.0.2.100"),
                               IOAddress("192.0.2.110")));
    subnet_->addPool(pool_);
    CfgMgr::instance().deleteSubnets4();
    CfgMgr::instance().addSubnet4(subnet_);

    // Pass it to the server and get an offer
    Pkt4Ptr offer = srv->processDiscover(dis);

    // Check if we get response at all
    checkResponse(offer, DHCPOFFER, 1234);

    // T1 and T2 timers must not be present.
    checkAddressParams(offer, subnet_, false, false);

    // Check identifiers
    checkServerId(offer, srv->getServerID());
    checkClientId(offer, clientid);
}
592 593 594 595 596 597 598 599 600 601 602 603 604

// This test verifies that incoming DISCOVER can be handled properly, that an
// OFFER is generated, that the response has an address and that address
// really belongs to the configured pool.
//
// constructed very simple DISCOVER message with:
// - client-id option
// - address set to specific value as hint
//
// expected returned OFFER message:
// - copy of client-id
// - server-id
// - offered address
605 606 607 608
TEST_F(Dhcpv4SrvTest, DiscoverHint) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

609
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
610
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
611 612 613 614 615 616 617
    IOAddress hint("192.0.2.107");

    Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
    dis->setRemoteAddr(IOAddress("192.0.2.1"));
    OptionPtr clientid = generateClientId();
    dis->addOption(clientid);
    dis->setYiaddr(hint);
618
    dis->setIface("eth1");
619

620
    // Pass it to the server and get an offer
621 622
    Pkt4Ptr offer = srv->processDiscover(dis);

623
    // Check if we get response at all
624 625
    checkResponse(offer, DHCPOFFER, 1234);

626
    // Check that address was returned from proper range, that its lease
627
    // lifetime is correct, that T1 and T2 are returned properly
628
    checkAddressParams(offer, subnet_, true, true);
629

630
    EXPECT_EQ(offer->getYiaddr(), hint);
631

632
    // Check identifiers
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    checkServerId(offer, srv->getServerID());
    checkClientId(offer, clientid);
}


// This test verifies that incoming DISCOVER can be handled properly, that an
// OFFER is generated, that the response has an address and that address
// really belongs to the configured pool.
//
// constructed very simple DISCOVER message with:
// - address set to specific value as hint
//
// expected returned OFFER message:
// - copy of client-id
// - server-id
// - offered address
649 650 651 652
TEST_F(Dhcpv4SrvTest, DiscoverNoClientId) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

653
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
654
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
655 656 657 658 659
    IOAddress hint("192.0.2.107");

    Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
    dis->setRemoteAddr(IOAddress("192.0.2.1"));
    dis->setYiaddr(hint);
660
    dis->setHWAddr(generateHWAddr(6));
661
    dis->setIface("eth1");
662

663
    // Pass it to the server and get an offer
664 665
    Pkt4Ptr offer = srv->processDiscover(dis);

666
    // Check if we get response at all
667 668
    checkResponse(offer, DHCPOFFER, 1234);

669
    // Check that address was returned from proper range, that its lease
670
    // lifetime is correct, that T1 and T2 are returned properly
671
    checkAddressParams(offer, subnet_, true, true);
672

673
    EXPECT_EQ(offer->getYiaddr(), hint);
674

675
    // Check identifiers
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
    checkServerId(offer, srv->getServerID());
}

// This test verifies that incoming DISCOVER can be handled properly, that an
// OFFER is generated, that the response has an address and that address
// really belongs to the configured pool.
//
// constructed very simple DISCOVER message with:
// - client-id option
// - address set to specific value as hint, but that hint is invalid
//
// expected returned OFFER message:
// - copy of client-id
// - server-id
// - offered address (!= hint)
691 692 693 694
TEST_F(Dhcpv4SrvTest, DiscoverInvalidHint) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

695
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
696
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
697 698 699 700 701 702 703
    IOAddress hint("10.1.2.3");

    Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
    dis->setRemoteAddr(IOAddress("192.0.2.107"));
    OptionPtr clientid = generateClientId();
    dis->addOption(clientid);
    dis->setYiaddr(hint);
704
    dis->setIface("eth1");
705

706
    // Pass it to the server and get an offer
707 708
    Pkt4Ptr offer = srv->processDiscover(dis);

709
    // Check if we get response at all
710 711
    checkResponse(offer, DHCPOFFER, 1234);

712
    // Check that address was returned from proper range, that its lease
713
    // lifetime is correct, that T1 and T2 are returned properly
714
    checkAddressParams(offer, subnet_, true, true);
715

716
    EXPECT_NE(offer->getYiaddr(), hint);
717

718
    // Check identifiers
719 720 721 722 723 724 725 726
    checkServerId(offer, srv->getServerID());
    checkClientId(offer, clientid);
}

/// @todo: Add a test that client sends hint that is in pool, but currently
/// being used by a different client.

// This test checks that the server is offering different addresses to different
Tomek Mrugalski's avatar
Tomek Mrugalski committed
727 728
// clients in OFFERs. Please note that OFFER is not a guarantee that such
// an address will be assigned. Had the pool was very small and contained only
729
// 2 addresses, the third client would get the same offer as the first one
730
// and this is a correct behavior. It is REQUEST that will fail for the third
Tomek Mrugalski's avatar
Tomek Mrugalski committed
731
// client. OFFER is basically saying "if you send me a request, you will
732
// probably get an address like this" (there are no guarantees).
733 734 735 736
TEST_F(Dhcpv4SrvTest, ManyDiscovers) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

737
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
738
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
739 740 741 742 743 744 745 746 747

    Pkt4Ptr dis1 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
    Pkt4Ptr dis2 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 2345));
    Pkt4Ptr dis3 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 3456));

    dis1->setRemoteAddr(IOAddress("192.0.2.1"));
    dis2->setRemoteAddr(IOAddress("192.0.2.2"));
    dis3->setRemoteAddr(IOAddress("192.0.2.3"));

748
    // Assign interfaces
749 750 751
    dis1->setIface("eth1");
    dis2->setIface("eth1");
    dis3->setIface("eth1");
752

753
    // Different client-id sizes
754 755 756 757 758 759 760 761
    OptionPtr clientid1 = generateClientId(4); // length 4
    OptionPtr clientid2 = generateClientId(5); // length 5
    OptionPtr clientid3 = generateClientId(6); // length 6

    dis1->addOption(clientid1);
    dis2->addOption(clientid2);
    dis3->addOption(clientid3);

762 763 764 765
    // Pass it to the server and get an offer
    Pkt4Ptr offer1 = srv->processDiscover(dis1);
    Pkt4Ptr offer2 = srv->processDiscover(dis2);
    Pkt4Ptr offer3 = srv->processDiscover(dis3);
766

767 768 769 770
    // Check if we get response at all
    checkResponse(offer1, DHCPOFFER, 1234);
    checkResponse(offer2, DHCPOFFER, 2345);
    checkResponse(offer3, DHCPOFFER, 3456);
771

772 773 774
    IOAddress addr1 = offer1->getYiaddr();
    IOAddress addr2 = offer2->getYiaddr();
    IOAddress addr3 = offer3->getYiaddr();
775 776

    // Check that the assigned address is indeed from the configured pool
777 778 779
    checkAddressParams(offer1, subnet_, true, true);
    checkAddressParams(offer2, subnet_, true, true);
    checkAddressParams(offer3, subnet_, true, true);
780

781
    // Check server-ids
782 783 784 785 786 787
    checkServerId(offer1, srv->getServerID());
    checkServerId(offer2, srv->getServerID());
    checkServerId(offer3, srv->getServerID());
    checkClientId(offer1, clientid1);
    checkClientId(offer2, clientid2);
    checkClientId(offer3, clientid3);
788 789

    // Finally check that the addresses offered are different
790 791 792 793 794 795
    EXPECT_NE(addr1, addr2);
    EXPECT_NE(addr2, addr3);
    EXPECT_NE(addr3, addr1);
    cout << "Offered address to client1=" << addr1 << endl;
    cout << "Offered address to client2=" << addr2 << endl;
    cout << "Offered address to client3=" << addr3 << endl;
796 797
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
798 799 800
// Checks whether echoing back client-id is controllable, i.e.
// whether the server obeys echo-client-id and sends (or not)
// client-id
801 802 803 804
TEST_F(Dhcpv4SrvTest, discoverEchoClientId) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

805 806 807 808 809 810
    NakedDhcpv4Srv srv(0);

    Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
    dis->setRemoteAddr(IOAddress("192.0.2.1"));
    OptionPtr clientid = generateClientId();
    dis->addOption(clientid);
811
    dis->setIface("eth1");
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827

    // Pass it to the server and get an offer
    Pkt4Ptr offer = srv.processDiscover(dis);

    // Check if we get response at all
    checkResponse(offer, DHCPOFFER, 1234);
    checkClientId(offer, clientid);

    CfgMgr::instance().echoClientId(false);
    offer = srv.processDiscover(dis);

    // Check if we get response at all
    checkResponse(offer, DHCPOFFER, 1234);
    checkClientId(offer, clientid);
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
828 829
// This test verifies that incoming REQUEST can be handled properly, that an
// ACK is generated, that the response has an address and that address
830 831
// really belongs to the configured pool.
//
Tomek Mrugalski's avatar
Tomek Mrugalski committed
832
// constructed a single REQUEST message with:
833
// - client-id option
Tomek Mrugalski's avatar
Tomek Mrugalski committed
834 835
// - hwaddr information
// - requested address (that the client received in DISCOVER/OFFER exchange)
836
//
Tomek Mrugalski's avatar
Tomek Mrugalski committed
837
// expected returned ACK message:
838 839
// - copy of client-id
// - server-id
Tomek Mrugalski's avatar
Tomek Mrugalski committed
840 841 842
// - assigned address
//
// Test verifies that the lease is actually in the database.
843 844 845 846
TEST_F(Dhcpv4SrvTest, RequestBasic) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

847
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
848
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
849 850 851 852 853 854 855

    IOAddress hint("192.0.2.107");
    Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
    req->setRemoteAddr(IOAddress("192.0.2.1"));
    OptionPtr clientid = generateClientId();
    req->addOption(clientid);
    req->setYiaddr(hint);
856
    req->setIface("eth1");
857 858 859 860

    // Pass it to the server and get an advertise
    Pkt4Ptr ack = srv->processRequest(req);

861
    // Check if we get response at all
862
    checkResponse(ack, DHCPACK, 1234);
863
    EXPECT_EQ(hint, ack->getYiaddr());
864

865
    // Check that address was returned from proper range, that its lease
866
    // lifetime is correct, that T1 and T2 are returned properly
867
    checkAddressParams(ack, subnet_, true, true);
868

869
    // Check identifiers
870 871 872
    checkServerId(ack, srv->getServerID());
    checkClientId(ack, clientid);

873
    // Check that the lease is really in the database
874 875 876 877 878 879
    Lease4Ptr l = checkLease(ack, clientid, req->getHWAddr(), hint);

    ASSERT_TRUE(l);
    LeaseMgrFactory::instance().deleteLease(l->addr_);
}

880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
// Check that option 58 and 59 are not included if they are not specified.
TEST_F(Dhcpv4SrvTest, RequestNoTimers) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

    boost::scoped_ptr<NakedDhcpv4Srv> srv;
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));

    Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
    req->setRemoteAddr(IOAddress("192.0.2.1"));
    OptionPtr clientid = generateClientId();
    req->addOption(clientid);
    req->setIface("eth1");

    // Recreate a subnet but set T1 and T2 to "unspecified".
    subnet_.reset(new Subnet4(IOAddress("192.0.2.0"), 24,
                              Triplet<uint32_t>(),
                              Triplet<uint32_t>(),
                              3000));
    pool_ = Pool4Ptr(new Pool4(IOAddress("192.0.2.100"),
                               IOAddress("192.0.2.110")));
    subnet_->addPool(pool_);
    CfgMgr::instance().deleteSubnets4();
    CfgMgr::instance().addSubnet4(subnet_);

    // Pass it to the server and get an ACK.
    Pkt4Ptr ack = srv->processRequest(req);

    // Check if we get response at all
    checkResponse(ack, DHCPACK, 1234);

    // T1 and T2 timers must not be present.
    checkAddressParams(ack, subnet_, false, false);

    // Check identifiers
    checkServerId(ack, srv->getServerID());
    checkClientId(ack, clientid);
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
919 920 921 922 923 924 925 926 927 928 929 930
// This test verifies that incoming REQUEST can be handled properly, that an
// ACK is generated, that the response has an address and that address
// really belongs to the configured pool.
//
// constructed 3 REQUEST messages with:
// - client-id option (differs between messages)
// - hwaddr information (differs between messages)
//
// expected returned ACK message:
// - copy of client-id
// - server-id
// - assigned address (different for each client)
931 932 933
TEST_F(Dhcpv4SrvTest, ManyRequests) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();
Tomek Mrugalski's avatar
Tomek Mrugalski committed
934 935

    boost::scoped_ptr<NakedDhcpv4Srv> srv;
936
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
937 938 939 940 941 942 943 944 945 946 947 948 949 950

    const IOAddress req_addr1("192.0.2.105");
    const IOAddress req_addr2("192.0.2.101");
    const IOAddress req_addr3("192.0.2.109");
    const IOAddress relay("192.0.2.1");

    Pkt4Ptr req1 = Pkt4Ptr(new Pkt4(DHCPOFFER, 1234));
    Pkt4Ptr req2 = Pkt4Ptr(new Pkt4(DHCPOFFER, 2345));
    Pkt4Ptr req3 = Pkt4Ptr(new Pkt4(DHCPOFFER, 3456));

    req1->setRemoteAddr(relay);
    req2->setRemoteAddr(relay);
    req3->setRemoteAddr(relay);

951
    // Assign interfaces
952 953 954
    req1->setIface("eth1");
    req2->setIface("eth1");
    req3->setIface("eth1");
955

Tomek Mrugalski's avatar
Tomek Mrugalski committed
956 957 958 959 960 961 962 963
    req1->setYiaddr(req_addr1);
    req2->setYiaddr(req_addr2);
    req3->setYiaddr(req_addr3);

    req1->setHWAddr(generateHWAddr(6));
    req2->setHWAddr(generateHWAddr(7));
    req3->setHWAddr(generateHWAddr(8));

964
    // Different client-id sizes
Tomek Mrugalski's avatar
Tomek Mrugalski committed
965 966 967 968 969 970 971 972 973 974 975 976 977
    OptionPtr clientid1 = generateClientId(4); // length 4
    OptionPtr clientid2 = generateClientId(5); // length 5
    OptionPtr clientid3 = generateClientId(6); // length 6

    req1->addOption(clientid1);
    req2->addOption(clientid2);
    req3->addOption(clientid3);

    // Pass it to the server and get an advertise
    Pkt4Ptr ack1 = srv->processRequest(req1);
    Pkt4Ptr ack2 = srv->processRequest(req2);
    Pkt4Ptr ack3 = srv->processRequest(req3);

978
    // Check if we get response at all
Tomek Mrugalski's avatar
Tomek Mrugalski committed
979 980 981 982 983 984 985 986 987
    checkResponse(ack1, DHCPACK, 1234);
    checkResponse(ack2, DHCPACK, 2345);
    checkResponse(ack3, DHCPACK, 3456);

    IOAddress addr1 = ack1->getYiaddr();
    IOAddress addr2 = ack2->getYiaddr();
    IOAddress addr3 = ack3->getYiaddr();

    // Check that every client received the address it requested
988 989 990
    EXPECT_EQ(req_addr1, addr1);
    EXPECT_EQ(req_addr2, addr2);
    EXPECT_EQ(req_addr3, addr3);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
991 992

    // Check that the assigned address is indeed from the configured pool
993 994 995
    checkAddressParams(ack1, subnet_, true, true);
    checkAddressParams(ack2, subnet_, true, true);
    checkAddressParams(ack3, subnet_, true, true);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
996

997
    // Check DUIDs
Tomek Mrugalski's avatar
Tomek Mrugalski committed
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
    checkServerId(ack1, srv->getServerID());
    checkServerId(ack2, srv->getServerID());
    checkServerId(ack3, srv->getServerID());
    checkClientId(ack1, clientid1);
    checkClientId(ack2, clientid2);
    checkClientId(ack3, clientid3);

    // Check that leases are in the database
    Lease4Ptr l = checkLease(ack1, clientid1, req1->getHWAddr(), addr1);
    EXPECT_TRUE(l);
    l = checkLease(ack2, clientid2, req2->getHWAddr(), addr2);
    l = checkLease(ack3, clientid3, req3->getHWAddr(), addr3);

    // Finally check that the addresses offered are different
1012 1013 1014 1015 1016 1017
    EXPECT_NE(addr1, addr2);
    EXPECT_NE(addr2, addr3);
    EXPECT_NE(addr3, addr1);
    cout << "Offered address to client1=" << addr1 << endl;
    cout << "Offered address to client2=" << addr2 << endl;
    cout << "Offered address to client3=" << addr3 << endl;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1018 1019
}

1020
// Checks whether echoing back client-id is controllable
1021 1022 1023 1024
TEST_F(Dhcpv4SrvTest, requestEchoClientId) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

1025 1026 1027 1028 1029 1030
    NakedDhcpv4Srv srv(0);

    Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
    dis->setRemoteAddr(IOAddress("192.0.2.1"));
    OptionPtr clientid = generateClientId();
    dis->addOption(clientid);
1031
    dis->setIface("eth1");
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

    // Pass it to the server and get ACK
    Pkt4Ptr ack = srv.processRequest(dis);

    // Check if we get response at all
    checkResponse(ack, DHCPACK, 1234);
    checkClientId(ack, clientid);

    CfgMgr::instance().echoClientId(false);
    ack = srv.processDiscover(dis);

    // Check if we get response at all
    checkResponse(ack, DHCPOFFER, 1234);
    checkClientId(ack, clientid);
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
1048 1049

// This test verifies that incoming (positive) REQUEST/Renewing can be handled properly, that a
1050 1051 1052 1053 1054 1055 1056 1057
// REPLY is generated, that the response has an address and that address
// really belongs to the configured pool and that lease is actually renewed.
//
// expected:
// - returned REPLY message has copy of client-id
// - returned REPLY message has server-id
// - returned REPLY message has IA that includes IAADDR
// - lease is actually renewed in LeaseMgr
1058 1059 1060 1061
TEST_F(Dhcpv4SrvTest, RenewBasic) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

1062
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
1063
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
1064 1065

    const IOAddress addr("192.0.2.106");
1066 1067 1068 1069
    const uint32_t temp_t1 = 50;
    const uint32_t temp_t2 = 75;
    const uint32_t temp_valid = 100;
    const time_t temp_timestamp = time(NULL) - 10;
1070

1071
    // Generate client-id also sets client_id_ member
1072 1073 1074
    OptionPtr clientid = generateClientId();

    // Check that the address we are about to use is indeed in pool
1075
    ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099

    // let's create a lease and put it in the LeaseMgr
    uint8_t hwaddr2[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
    Lease4Ptr used(new Lease4(IOAddress("192.0.2.106"), hwaddr2, sizeof(hwaddr2),
                              &client_id_->getDuid()[0], client_id_->getDuid().size(),
                              temp_valid, temp_t1, temp_t2, temp_timestamp,
                              subnet_->getID()));
    ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));

    // Check that the lease is really in the database
    Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
    ASSERT_TRUE(l);

    // Check that T1, T2, preferred, valid and cltt really set.
    // Constructed lease looks as if it was assigned 10 seconds ago
    // EXPECT_EQ(l->t1_, temp_t1);
    // EXPECT_EQ(l->t2_, temp_t2);
    EXPECT_EQ(l->valid_lft_, temp_valid);
    EXPECT_EQ(l->cltt_, temp_timestamp);

    // Let's create a RENEW
    Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
    req->setRemoteAddr(IOAddress(addr));
    req->setYiaddr(addr);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1100
    req->setCiaddr(addr); // client's address
1101
    req->setIface("eth0");
1102 1103 1104 1105 1106 1107 1108 1109 1110

    req->addOption(clientid);
    req->addOption(srv->getServerID());

    // Pass it to the server and hope for a REPLY
    Pkt4Ptr ack = srv->processRequest(req);

    // Check if we get response at all
    checkResponse(ack, DHCPACK, 1234);
1111
    EXPECT_EQ(addr, ack->getYiaddr());
1112

1113
    // Check that address was returned from proper range, that its lease
1114
    // lifetime is correct, that T1 and T2 are returned properly
1115
    checkAddressParams(ack, subnet_, true, true);
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132

    // Check identifiers
    checkServerId(ack, srv->getServerID());
    checkClientId(ack, clientid);

    // Check that the lease is really in the database
    l = checkLease(ack, clientid, req->getHWAddr(), addr);
    ASSERT_TRUE(l);

    // Check that T1, T2, preferred, valid and cltt were really updated
    EXPECT_EQ(l->t1_, subnet_->getT1());
    EXPECT_EQ(l->t2_, subnet_->getT2());
    EXPECT_EQ(l->valid_lft_, subnet_->getValid());

    // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
    int32_t cltt = static_cast<int32_t>(l->cltt_);
    int32_t expected = static_cast<int32_t>(time(NULL));