dhcp4_srv_unittest.cc 117 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 <dhcp4/dhcp4_srv.h>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
33
#include <dhcp4/dhcp4_log.h>
34
#include <dhcp4/config_parser.h>
35
#include <hooks/server_hooks.h>
36 37 38 39
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/lease_mgr.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <dhcpsrv/utils.h>
40
#include <gtest/gtest.h>
41 42
#include <hooks/server_hooks.h>
#include <hooks/hooks_manager.h>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
43
#include <config/ccsession.h>
44

45 46
#include <boost/scoped_ptr.hpp>

47 48 49
#include <iostream>

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

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

namespace {

61 62 63
// 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.
64
TEST_F(Dhcpv4SrvFakeIfaceTest, adjustIfaceDataRelay) {
65
    // Create the instance of the incoming packet.
66
    boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
67 68
    // Set the giaddr to non-zero address and hops to non-zero value
    // as if it was relayed.
69
    req->setGiaddr(IOAddress("192.0.2.1"));
70
    req->setHops(2);
71 72
    // Set ciaddr to zero. This simulates the client which applies
    // for the new lease.
73
    req->setCiaddr(IOAddress("0.0.0.0"));
74 75 76
    // Clear broadcast flag.
    req->setFlags(0x0000);

77 78 79 80 81 82
    // Set local address, port and interface.
    req->setLocalAddr(IOAddress("192.0.3.1"));
    req->setLocalPort(1001);
    req->setIface("eth0");
    req->setIndex(1);

83 84 85 86
    // 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));
87
    resp->setYiaddr(IOAddress("192.0.2.100"));
88 89
    // Clear the remote address.
    resp->setRemoteAddr(IOAddress("0.0.0.0"));
90 91
    // Set hops value for the response.
    resp->setHops(req->getHops());
92

93
    // This function never throws.
94
    ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
95

96
    // Now the destination address should be relay's address.
97
    EXPECT_EQ("192.0.2.1", resp->getRemoteAddr().toText());
98 99 100 101 102 103 104 105 106 107
    // The query has been relayed, so the response must be sent to the port 67.
    EXPECT_EQ(DHCP4_SERVER_PORT, resp->getRemotePort());
    // Local address should be copied from the query message.
    EXPECT_EQ("192.0.3.1", resp->getLocalAddr().toText());
    // 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.
    EXPECT_EQ("eth0", resp->getIface());
    EXPECT_EQ(1, resp->getIndex());
108 109 110 111 112 113

    // 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.
    req->setGiaddr(IOAddress("192.0.2.50"));
    req->setCiaddr(IOAddress("192.0.2.11"));
114
    req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
115 116 117 118 119

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

120
    ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
121 122 123 124 125

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

126 127 128 129
// 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.
130
TEST_F(Dhcpv4SrvFakeIfaceTest, adjustIfaceDataRenew) {
131 132 133 134 135 136 137 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).
    req->setCiaddr(IOAddress("192.0.2.15"));
    // 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.
146
    req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
147 148 149 150 151 152 153 154 155
    // 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.
    req->setLocalAddr(IOAddress("192.0.3.1"));
    // 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.
    req->setIface("eth0");
    req->setIndex(1);
156 157 158 159 160 161 162 163 164 165

    // 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.
    resp->setYiaddr(IOAddress("192.0.2.13"));
    // Clear the remote address.
    resp->setRemoteAddr(IOAddress("0.0.0.0"));
166 167
    // Copy hops value from the query.
    resp->setHops(req->getHops());
168

169
    ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
170 171 172

    // Check that server responds to ciaddr
    EXPECT_EQ("192.0.2.15", resp->getRemoteAddr().toText());
173 174 175 176 177 178 179 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.
    EXPECT_EQ("192.0.3.1", resp->getLocalAddr().toText());
    // 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.
    EXPECT_EQ("eth0", resp->getIface());
    EXPECT_EQ(1, resp->getIndex());

185 186
}

187 188 189 190 191 192 193
// 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.
194
TEST_F(Dhcpv4SrvFakeIfaceTest, adjustIfaceDataSelect) {
195 196 197 198 199 200 201 202 203 204 205
    // 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);

206 207 208 209 210 211 212 213 214 215
    // 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.
    req->setIface("eth0");
    req->setIndex(1);

216 217 218 219 220 221
    // 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"));
222 223 224 225 226 227 228 229 230
    // 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.
    current_pkt_filter_->direct_resp_supported_ = false;
231 232 233 234 235 236 237

    // 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.
238
    ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
239 240 241 242 243

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

244 245 246 247 248 249 250 251 252 253 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.
    EXPECT_EQ("192.0.3.1", resp->getLocalAddr().toText());

    // 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.
    EXPECT_EQ("eth0", resp->getIface());
    EXPECT_EQ(1, resp->getIndex());

257 258 259
    // 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
260 261 262 263
    // response based on the capability reported by IfaceMgr. We can
    // control whether the current packet filter returns that it supports
    // direct responses or not.
    current_pkt_filter_->direct_resp_supported_ = true;
264 265 266

    // Now we expect that the server will send its response to the
    // address assigned for the client.
267
    ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
268 269 270 271

    EXPECT_EQ("192.0.2.13", resp->getRemoteAddr().toText());
}

272 273 274 275 276
// 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.
277
TEST_F(Dhcpv4SrvFakeIfaceTest, adjustIfaceDataBroadcast) {
278 279 280 281 282 283 284
    // 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"));
285 286 287 288 289 290 291
    // 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);
292 293

    // Let's set the broadcast flag.
294
    req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
295 296 297 298 299 300 301 302 303

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

304
    ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
305 306 307 308

    // 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());
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

    // 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.
    EXPECT_EQ("192.0.3.1", resp->getLocalAddr().toText());

    // 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.
    EXPECT_EQ("eth0", resp->getIface());
    EXPECT_EQ(1, resp->getIndex());

}

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
// This test verifies that exception is thrown of the invalid combination
// of giaddr and hops is specified in a client's message.
TEST_F(Dhcpv4SrvFakeIfaceTest, adjustIfaceDataInvalid) {
    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);
}

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
// 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());
383 384
}

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
// 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;
    ASSERT_NO_THROW(srv.reset(new Dhcpv4Srv(DHCP4_SERVER_PORT + 10000, "type=memfile",
                                            false, false)));
    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));
428 429
}

430
// Verifies that DISCOVER message can be processed correctly,
Tomek Mrugalski's avatar
Tomek Mrugalski committed
431 432 433 434 435 436 437
// 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.
438
TEST_F(Dhcpv4SrvFakeIfaceTest, processDiscover) {
439
    testDiscoverRequest(DHCPDISCOVER);
440
}
441

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

454
TEST_F(Dhcpv4SrvFakeIfaceTest, processRelease) {
455
    NakedDhcpv4Srv srv;
456
    Pkt4Ptr pkt(new Pkt4(DHCPRELEASE, 1234));
457

458
    // Should not throw
459
    EXPECT_NO_THROW(srv.processRelease(pkt));
460 461
}

462
TEST_F(Dhcpv4SrvFakeIfaceTest, processDecline) {
463
    NakedDhcpv4Srv srv;
464
    Pkt4Ptr pkt(new Pkt4(DHCPDECLINE, 1234));
465

466
    // Should not throw
467
    EXPECT_NO_THROW(srv.processDecline(pkt));
468 469
}

470
TEST_F(Dhcpv4SrvFakeIfaceTest, processInform) {
471
    NakedDhcpv4Srv srv;
472
    Pkt4Ptr pkt(new Pkt4(DHCPINFORM, 1234));
473

474
    // Should not throw
475
    EXPECT_NO_THROW(srv.processInform(pkt));
476

477
    // Should return something
478
    EXPECT_TRUE(srv.processInform(pkt));
479

480
    // @todo Implement more reasonable tests before starting
481
    // work on processSomething() method.
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 515
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));
        }
    }
}

516 517 518 519 520 521 522 523 524 525 526
// 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
527
TEST_F(Dhcpv4SrvFakeIfaceTest, DiscoverBasic) {
528
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
529
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
530 531 532 533 534

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

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

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

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

547
    // Check identifiers
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
    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
565
TEST_F(Dhcpv4SrvFakeIfaceTest, DiscoverHint) {
566
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
567
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
568 569 570 571 572 573 574
    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);
575
    dis->setIface("eth0");
576

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

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

583
    // Check that address was returned from proper range, that its lease
584 585 586 587 588
    // lifetime is correct, that T1 and T2 are returned properly
    checkAddressParams(offer, subnet_);

    EXPECT_EQ(offer->getYiaddr().toText(), hint.toText());

589
    // Check identifiers
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
    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
606
TEST_F(Dhcpv4SrvFakeIfaceTest, DiscoverNoClientId) {
607
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
608
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
609 610 611 612 613
    IOAddress hint("192.0.2.107");

    Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
    dis->setRemoteAddr(IOAddress("192.0.2.1"));
    dis->setYiaddr(hint);
614
    dis->setHWAddr(generateHWAddr(6));
615
    dis->setIface("eth0");
616

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

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

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

    EXPECT_EQ(offer->getYiaddr().toText(), hint.toText());

629
    // Check identifiers
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
    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)
645
TEST_F(Dhcpv4SrvFakeIfaceTest, DiscoverInvalidHint) {
646
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
647
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
648 649 650 651 652 653 654
    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);
655
    dis->setIface("eth0");
656

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

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

663
    // Check that address was returned from proper range, that its lease
664 665 666 667 668
    // lifetime is correct, that T1 and T2 are returned properly
    checkAddressParams(offer, subnet_);

    EXPECT_NE(offer->getYiaddr().toText(), hint.toText());

669
    // Check identifiers
670 671 672 673 674 675 676 677
    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
678 679
// 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
680
// 2 addresses, the third client would get the same offer as the first one
681
// and this is a correct behavior. It is REQUEST that will fail for the third
Tomek Mrugalski's avatar
Tomek Mrugalski committed
682
// client. OFFER is basically saying "if you send me a request, you will
683
// probably get an address like this" (there are no guarantees).
684
TEST_F(Dhcpv4SrvFakeIfaceTest, ManyDiscovers) {
685
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
686
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
687 688 689 690 691 692 693 694 695

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

696 697 698 699 700
    // Assign interfaces
    dis1->setIface("eth0");
    dis2->setIface("eth0");
    dis3->setIface("eth0");

701
    // Different client-id sizes
702 703 704 705 706 707 708 709
    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);

710 711 712 713
    // Pass it to the server and get an offer
    Pkt4Ptr offer1 = srv->processDiscover(dis1);
    Pkt4Ptr offer2 = srv->processDiscover(dis2);
    Pkt4Ptr offer3 = srv->processDiscover(dis3);
714

715 716 717 718
    // Check if we get response at all
    checkResponse(offer1, DHCPOFFER, 1234);
    checkResponse(offer2, DHCPOFFER, 2345);
    checkResponse(offer3, DHCPOFFER, 3456);
719

720 721 722
    IOAddress addr1 = offer1->getYiaddr();
    IOAddress addr2 = offer2->getYiaddr();
    IOAddress addr3 = offer3->getYiaddr();
723 724

    // Check that the assigned address is indeed from the configured pool
725 726 727 728
    checkAddressParams(offer1, subnet_);
    checkAddressParams(offer2, subnet_);
    checkAddressParams(offer3, subnet_);

729
    // Check server-ids
730 731 732 733 734 735
    checkServerId(offer1, srv->getServerID());
    checkServerId(offer2, srv->getServerID());
    checkServerId(offer3, srv->getServerID());
    checkClientId(offer1, clientid1);
    checkClientId(offer2, clientid2);
    checkClientId(offer3, clientid3);
736 737 738 739 740 741 742 743 744 745

    // Finally check that the addresses offered are different
    EXPECT_NE(addr1.toText(), addr2.toText());
    EXPECT_NE(addr2.toText(), addr3.toText());
    EXPECT_NE(addr3.toText(), addr1.toText());
    cout << "Offered address to client1=" << addr1.toText() << endl;
    cout << "Offered address to client2=" << addr2.toText() << endl;
    cout << "Offered address to client3=" << addr3.toText() << endl;
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
746 747 748
// Checks whether echoing back client-id is controllable, i.e.
// whether the server obeys echo-client-id and sends (or not)
// client-id
749
TEST_F(Dhcpv4SrvFakeIfaceTest, discoverEchoClientId) {
750 751 752 753 754 755
    NakedDhcpv4Srv srv(0);

    Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
    dis->setRemoteAddr(IOAddress("192.0.2.1"));
    OptionPtr clientid = generateClientId();
    dis->addOption(clientid);
756
    dis->setIface("eth0");
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

    // 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
773 774
// This test verifies that incoming REQUEST can be handled properly, that an
// ACK is generated, that the response has an address and that address
775 776
// really belongs to the configured pool.
//
Tomek Mrugalski's avatar
Tomek Mrugalski committed
777
// constructed a single REQUEST message with:
778
// - client-id option
Tomek Mrugalski's avatar
Tomek Mrugalski committed
779 780
// - hwaddr information
// - requested address (that the client received in DISCOVER/OFFER exchange)
781
//
Tomek Mrugalski's avatar
Tomek Mrugalski committed
782
// expected returned ACK message:
783 784
// - copy of client-id
// - server-id
Tomek Mrugalski's avatar
Tomek Mrugalski committed
785 786 787
// - assigned address
//
// Test verifies that the lease is actually in the database.
788
TEST_F(Dhcpv4SrvFakeIfaceTest, RequestBasic) {
789
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
790
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
791 792 793 794 795 796 797

    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);
798
    req->setIface("eth0");
799 800 801 802

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

803
    // Check if we get response at all
804 805 806
    checkResponse(ack, DHCPACK, 1234);
    EXPECT_EQ(hint.toText(), ack->getYiaddr().toText());

807
    // Check that address was returned from proper range, that its lease
808 809 810
    // lifetime is correct, that T1 and T2 are returned properly
    checkAddressParams(ack, subnet_);

811
    // Check identifiers
812 813 814
    checkServerId(ack, srv->getServerID());
    checkClientId(ack, clientid);

815
    // Check that the lease is really in the database
816 817 818 819 820 821
    Lease4Ptr l = checkLease(ack, clientid, req->getHWAddr(), hint);

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

Tomek Mrugalski's avatar
Tomek Mrugalski committed
822 823 824 825 826 827 828 829 830 831 832 833
// 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)
834
TEST_F(Dhcpv4SrvFakeIfaceTest, ManyRequests) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
835 836

    boost::scoped_ptr<NakedDhcpv4Srv> srv;
837
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
838 839 840 841 842 843 844 845 846 847 848 849 850 851

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

852 853 854 855 856
    // Assign interfaces
    req1->setIface("eth0");
    req2->setIface("eth0");
    req3->setIface("eth0");

Tomek Mrugalski's avatar
Tomek Mrugalski committed
857 858 859 860 861 862 863 864
    req1->setYiaddr(req_addr1);
    req2->setYiaddr(req_addr2);
    req3->setYiaddr(req_addr3);

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

865
    // Different client-id sizes
Tomek Mrugalski's avatar
Tomek Mrugalski committed
866 867 868 869 870 871 872 873 874 875 876 877 878
    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);

879
    // Check if we get response at all
Tomek Mrugalski's avatar
Tomek Mrugalski committed
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
    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
    EXPECT_EQ(req_addr1.toText(), addr1.toText());
    EXPECT_EQ(req_addr2.toText(), addr2.toText());
    EXPECT_EQ(req_addr3.toText(), addr3.toText());

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

898
    // Check DUIDs
Tomek Mrugalski's avatar
Tomek Mrugalski committed
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
    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
    EXPECT_NE(addr1.toText(), addr2.toText());
    EXPECT_NE(addr2.toText(), addr3.toText());
    EXPECT_NE(addr3.toText(), addr1.toText());
    cout << "Offered address to client1=" << addr1.toText() << endl;
    cout << "Offered address to client2=" << addr2.toText() << endl;
    cout << "Offered address to client3=" << addr3.toText() << endl;
}

921
// Checks whether echoing back client-id is controllable
922
TEST_F(Dhcpv4SrvFakeIfaceTest, requestEchoClientId) {
923 924 925 926 927 928
    NakedDhcpv4Srv srv(0);

    Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
    dis->setRemoteAddr(IOAddress("192.0.2.1"));
    OptionPtr clientid = generateClientId();
    dis->addOption(clientid);
929
    dis->setIface("eth0");
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945

    // 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
946 947

// This test verifies that incoming (positive) REQUEST/Renewing can be handled properly, that a
948 949 950 951 952 953 954 955
// 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
956
TEST_F(Dhcpv4SrvFakeIfaceTest, RenewBasic) {
957
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
958
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
959 960

    const IOAddress addr("192.0.2.106");
961 962 963 964
    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;
965

966
    // Generate client-id also sets client_id_ member
967 968 969
    OptionPtr clientid = generateClientId();

    // Check that the address we are about to use is indeed in pool
970
    ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994

    // 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
995
    req->setCiaddr(addr); // client's address
996
    req->setIface("eth0");
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007

    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);
    EXPECT_EQ(addr.toText(), ack->getYiaddr().toText());

1008
    // Check that address was returned from proper range, that its lease
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
    // lifetime is correct, that T1 and T2 are returned properly
    checkAddressParams(ack, subnet_);

    // 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));
1028
    // Equality or difference by 1 between cltt and expected is ok.
1029 1030 1031 1032 1033
    EXPECT_GE(1, abs(cltt - expected));

    EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
}

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
// This test verifies that the logic which matches server identifier in the
// received message with server identifiers used by a server works correctly:
// - a message with no server identifier is accepted,
// - a message with a server identifier which matches one of the server
// identifiers used by a server is accepted,
// - a message with a server identifier which doesn't match any server
// identifier used by a server, is not accepted.
TEST_F(Dhcpv4SrvFakeIfaceTest, acceptServerId) {
    NakedDhcpv4Srv srv(0);

    Pkt4Ptr pkt(new Pkt4(DHCPREQUEST, 1234));
    // If no server identifier option is present, the message is always
    // accepted.
    EXPECT_TRUE(srv.acceptServerId(pkt));

    // Add a server identifier option which doesn't match server ids being
    // used by the server. The accepted server ids are the IPv4 addresses
    // configured on the interfaces. The 10.1.2.3 is not configured on
    // any interface.
    OptionPtr other_serverid(new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER,
                                                IOAddress("10.1.2.3")));
    pkt->addOption(other_serverid);
    EXPECT_FALSE(srv.acceptServerId(pkt));

    // Remove the server identifier.
    ASSERT_NO_THROW(pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER));

    // Add a server id being an IPv4 address configured on eth0 interface.
    // A DHCPv4 message holding this server identifier should be accepted.
    OptionPtr eth0_serverid(new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER,
                                               IOAddress("192.0.3.1")));
    ASSERT_NO_THROW(pkt->addOption(eth0_serverid));
    EXPECT_TRUE(srv.acceptServerId(pkt));

    // Remove the server identifier.
    ASSERT_NO_THROW(pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER));

    // Add a server id being an IPv4 address configured on eth1 interface.
    // A DHCPv4 message holding this server identifier should be accepted.
    OptionPtr eth1_serverid(new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER,
                                               IOAddress("10.0.0.1")));
    ASSERT_NO_THROW(pkt->addOption(eth1_serverid));
    EXPECT_TRUE(srv.acceptServerId(pkt));

}

1080 1081 1082 1083 1084
// @todo: Implement tests for rejecting renewals

// This test verifies if the sanityCheck() really checks options presence.
TEST_F(Dhcpv4SrvTest, sanityCheck) {
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
1085
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
1086 1087

    Pkt4Ptr pkt = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
1088
    pkt->setHWAddr(generateHWAddr(6));
1089

1090
    // Server-id is optional for information-request, so
1091
    EXPECT_NO_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::OPTIONAL));
1092

1093
    // Empty packet, no server-id
1094 1095
    EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY),
                 RFCViolation);
1096 1097 1098

    pkt->addOption(srv->getServerID());

1099
    // Server-id is mandatory and present = no exception
1100
    EXPECT_NO_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY));
1101

1102
    // Server-id is forbidden, but present => exception
1103
    EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::FORBIDDEN),
1104
                 RFCViolation);
1105 1106 1107 1108

    // There's no client-id and no HWADDR. Server needs something to
    // identify the client
    pkt->setHWAddr(generateHWAddr(0));
1109 1110
    EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY),
                 RFCViolation);
1111 1112
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
1113 1114 1115
// This test verifies that incoming (positive) RELEASE can be handled properly.
// As there is no REPLY in DHCPv4, the only thing to verify here is that
// the lease is indeed removed from the database.
1116
TEST_F(Dhcpv4SrvFakeIfaceTest, ReleaseBasic) {
1117
    boost::scoped_ptr<NakedDhcpv4Srv> srv;
1118
    ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
1119