dhcp4_srv_unittest.cc 131 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
TEST_F(Dhcpv4SrvTest, processInform) {
484
    NakedDhcpv4Srv srv;
485
    Pkt4Ptr pkt(new Pkt4(DHCPINFORM, 1234));
486

487
    // Should not throw
488
    EXPECT_NO_THROW(srv.processInform(pkt));
489

490
    // Should return something
491
    EXPECT_TRUE(srv.processInform(pkt));
492

493
    // @todo Implement more reasonable tests before starting
494
    // work on processSomething() method.
495 496
}

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
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));
        }
    }
}

529 530 531 532 533 534 535 536 537 538 539
// 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
540 541 542 543
TEST_F(Dhcpv4SrvTest, DiscoverBasic) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

544
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
545
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
546 547 548 549 550

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

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

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

559
    // Check that address was returned from proper range, that its lease
560 561 562
    // lifetime is correct, that T1 and T2 are returned properly
    checkAddressParams(offer, subnet_);

563
    // Check identifiers
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    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:
// - client-id option
// - address set to specific value as hint
//
// expected returned OFFER message:
// - copy of client-id
// - server-id
// - offered address
581 582 583 584
TEST_F(Dhcpv4SrvTest, DiscoverHint) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

585
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
586
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
587 588 589 590 591 592 593
    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);
594
    dis->setIface("eth1");
595

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

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

602
    // Check that address was returned from proper range, that its lease
603 604 605
    // lifetime is correct, that T1 and T2 are returned properly
    checkAddressParams(offer, subnet_);

606
    EXPECT_EQ(offer->getYiaddr(), hint);
607

608
    // Check identifiers
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    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
625 626 627 628
TEST_F(Dhcpv4SrvTest, DiscoverNoClientId) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

629
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
630
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
631 632 633 634 635
    IOAddress hint("192.0.2.107");

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

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

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

645
    // Check that address was returned from proper range, that its lease
646 647 648
    // lifetime is correct, that T1 and T2 are returned properly
    checkAddressParams(offer, subnet_);

649
    EXPECT_EQ(offer->getYiaddr(), hint);
650

651
    // Check identifiers
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
    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)
667 668 669 670
TEST_F(Dhcpv4SrvTest, DiscoverInvalidHint) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

671
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
672
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
673 674 675 676 677 678 679
    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);
680
    dis->setIface("eth1");
681

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

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

688
    // Check that address was returned from proper range, that its lease
689 690 691
    // lifetime is correct, that T1 and T2 are returned properly
    checkAddressParams(offer, subnet_);

692
    EXPECT_NE(offer->getYiaddr(), hint);
693

694
    // Check identifiers
695 696 697 698 699 700 701 702
    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
703 704
// 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
705
// 2 addresses, the third client would get the same offer as the first one
706
// and this is a correct behavior. It is REQUEST that will fail for the third
Tomek Mrugalski's avatar
Tomek Mrugalski committed
707
// client. OFFER is basically saying "if you send me a request, you will
708
// probably get an address like this" (there are no guarantees).
709 710 711 712
TEST_F(Dhcpv4SrvTest, ManyDiscovers) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

713
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
714
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
715 716 717 718 719 720 721 722 723

    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"));

724
    // Assign interfaces
725 726 727
    dis1->setIface("eth1");
    dis2->setIface("eth1");
    dis3->setIface("eth1");
728

729
    // Different client-id sizes
730 731 732 733 734 735 736 737
    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);

738 739 740 741
    // Pass it to the server and get an offer
    Pkt4Ptr offer1 = srv->processDiscover(dis1);
    Pkt4Ptr offer2 = srv->processDiscover(dis2);
    Pkt4Ptr offer3 = srv->processDiscover(dis3);
742

743 744 745 746
    // Check if we get response at all
    checkResponse(offer1, DHCPOFFER, 1234);
    checkResponse(offer2, DHCPOFFER, 2345);
    checkResponse(offer3, DHCPOFFER, 3456);
747

748 749 750
    IOAddress addr1 = offer1->getYiaddr();
    IOAddress addr2 = offer2->getYiaddr();
    IOAddress addr3 = offer3->getYiaddr();
751 752

    // Check that the assigned address is indeed from the configured pool
753 754 755 756
    checkAddressParams(offer1, subnet_);
    checkAddressParams(offer2, subnet_);
    checkAddressParams(offer3, subnet_);

757
    // Check server-ids
758 759 760 761 762 763
    checkServerId(offer1, srv->getServerID());
    checkServerId(offer2, srv->getServerID());
    checkServerId(offer3, srv->getServerID());
    checkClientId(offer1, clientid1);
    checkClientId(offer2, clientid2);
    checkClientId(offer3, clientid3);
764 765

    // Finally check that the addresses offered are different
766 767 768 769 770 771
    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;
772 773
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
774 775 776
// Checks whether echoing back client-id is controllable, i.e.
// whether the server obeys echo-client-id and sends (or not)
// client-id
777 778 779 780
TEST_F(Dhcpv4SrvTest, discoverEchoClientId) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

781 782 783 784 785 786
    NakedDhcpv4Srv srv(0);

    Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
    dis->setRemoteAddr(IOAddress("192.0.2.1"));
    OptionPtr clientid = generateClientId();
    dis->addOption(clientid);
787
    dis->setIface("eth1");
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803

    // 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
804 805
// This test verifies that incoming REQUEST can be handled properly, that an
// ACK is generated, that the response has an address and that address
806 807
// really belongs to the configured pool.
//
Tomek Mrugalski's avatar
Tomek Mrugalski committed
808
// constructed a single REQUEST message with:
809
// - client-id option
Tomek Mrugalski's avatar
Tomek Mrugalski committed
810 811
// - hwaddr information
// - requested address (that the client received in DISCOVER/OFFER exchange)
812
//
Tomek Mrugalski's avatar
Tomek Mrugalski committed
813
// expected returned ACK message:
814 815
// - copy of client-id
// - server-id
Tomek Mrugalski's avatar
Tomek Mrugalski committed
816 817 818
// - assigned address
//
// Test verifies that the lease is actually in the database.
819 820 821 822
TEST_F(Dhcpv4SrvTest, RequestBasic) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

823
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
824
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
825 826 827 828 829 830 831

    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);
832
    req->setIface("eth1");
833 834 835 836

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

837
    // Check if we get response at all
838
    checkResponse(ack, DHCPACK, 1234);
839
    EXPECT_EQ(hint, ack->getYiaddr());
840

841
    // Check that address was returned from proper range, that its lease
842 843 844
    // lifetime is correct, that T1 and T2 are returned properly
    checkAddressParams(ack, subnet_);

845
    // Check identifiers
846 847 848
    checkServerId(ack, srv->getServerID());
    checkClientId(ack, clientid);

849
    // Check that the lease is really in the database
850 851 852 853 854 855
    Lease4Ptr l = checkLease(ack, clientid, req->getHWAddr(), hint);

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

Tomek Mrugalski's avatar
Tomek Mrugalski committed
856 857 858 859 860 861 862 863 864 865 866 867
// 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)
868 869 870
TEST_F(Dhcpv4SrvTest, ManyRequests) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();
Tomek Mrugalski's avatar
Tomek Mrugalski committed
871 872

    boost::scoped_ptr<NakedDhcpv4Srv> srv;
873
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
874 875 876 877 878 879 880 881 882 883 884 885 886 887

    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);

888
    // Assign interfaces
889 890 891
    req1->setIface("eth1");
    req2->setIface("eth1");
    req3->setIface("eth1");
892

Tomek Mrugalski's avatar
Tomek Mrugalski committed
893 894 895 896 897 898 899 900
    req1->setYiaddr(req_addr1);
    req2->setYiaddr(req_addr2);
    req3->setYiaddr(req_addr3);

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

901
    // Different client-id sizes
Tomek Mrugalski's avatar
Tomek Mrugalski committed
902 903 904 905 906 907 908 909 910 911 912 913 914
    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);

915
    // Check if we get response at all
Tomek Mrugalski's avatar
Tomek Mrugalski committed
916 917 918 919 920 921 922 923 924
    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
925 926 927
    EXPECT_EQ(req_addr1, addr1);
    EXPECT_EQ(req_addr2, addr2);
    EXPECT_EQ(req_addr3, addr3);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
928 929 930 931 932 933

    // Check that the assigned address is indeed from the configured pool
    checkAddressParams(ack1, subnet_);
    checkAddressParams(ack2, subnet_);
    checkAddressParams(ack3, subnet_);

934
    // Check DUIDs
Tomek Mrugalski's avatar
Tomek Mrugalski committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948
    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
949 950 951 952 953 954
    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
955 956
}

957
// Checks whether echoing back client-id is controllable
958 959 960 961
TEST_F(Dhcpv4SrvTest, requestEchoClientId) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

962 963 964 965 966 967
    NakedDhcpv4Srv srv(0);

    Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
    dis->setRemoteAddr(IOAddress("192.0.2.1"));
    OptionPtr clientid = generateClientId();
    dis->addOption(clientid);
968
    dis->setIface("eth1");
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984

    // 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
985 986

// This test verifies that incoming (positive) REQUEST/Renewing can be handled properly, that a
987 988 989 990 991 992 993 994
// 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
995 996 997 998
TEST_F(Dhcpv4SrvTest, RenewBasic) {
    IfaceMgrTestConfig test_config(true);
    IfaceMgr::instance().openSockets4();

999
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
1000
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
1001 1002

    const IOAddress addr("192.0.2.106");
1003 1004 1005 1006
    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;
1007

1008
    // Generate client-id also sets client_id_ member
1009 1010 1011
    OptionPtr clientid = generateClientId();

    // Check that the address we are about to use is indeed in pool
1012
    ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));