dhcp4_srv_unittest.cc 84.9 KB
 Tomek Mrugalski committed Jan 03, 2013 1 // Copyright (C) 2011-2013 Internet Systems Consortium, Inc. ("ISC")  Tomek Mrugalski committed Nov 30, 2011 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 #include  Stephen Morris committed Nov 16, 2012 18 #include  Tomek Mrugalski committed Jul 15, 2013 19 #include  Tomek Mrugalski committed Nov 30, 2011 20 #include  Marcin Siodelski committed Mar 28, 2013 21 #include  Tomek Mrugalski committed Nov 30, 2011 22 #include  Marcin Siodelski committed Jan 15, 2013 23 #include  Marcin Siodelski committed Jan 15, 2013 24 #include  Marcin Siodelski committed Jan 15, 2013 25 #include  Marcin Siodelski committed May 21, 2013 26 27 #include #include  Stephen Morris committed Nov 16, 2012 28 #include  Tomek Mrugalski committed Jan 03, 2013 29 #include  Tomek Mrugalski committed Jul 15, 2013 30 #include  Tomek Mrugalski committed Jun 27, 2013 31 #include  Tomek Mrugalski committed Dec 31, 2012 32 33 34 35 #include #include #include #include  Stephen Morris committed Nov 16, 2012 36 #include  Tomek Mrugalski committed Jul 15, 2013 37 38 #include #include  Stephen Morris committed Nov 16, 2012 39   Stephen Morris committed Apr 11, 2013 40 41 #include  Stephen Morris committed Nov 16, 2012 42 43 44 45 #include #include #include  Tomek Mrugalski committed Nov 30, 2011 46 47 48 49  using namespace std; using namespace isc; using namespace isc::dhcp;  Tomek Mrugalski committed Jul 15, 2013 50 using namespace isc::data;  Tomek Mrugalski committed Dec 22, 2011 51 using namespace isc::asiolink;  Tomek Mrugalski committed Jul 15, 2013 52 using namespace isc::hooks;  Tomek Mrugalski committed Nov 30, 2011 53 54 55 56  namespace { class NakedDhcpv4Srv: public Dhcpv4Srv {  Tomek Mrugalski committed Jan 08, 2013 57  // "Naked" DHCPv4 server, exposes internal fields  Tomek Mrugalski committed Nov 30, 2011 58 public:  Marcin Siodelski committed Apr 04, 2013 59 60 61  /// @brief Constructor. ///  Marcin Siodelski committed May 21, 2013 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81  /// This constructor disables default modes of operation used by the /// Dhcpv4Srv class: /// - Send/receive broadcast messages through sockets on interfaces /// which support broadcast traffic. /// - Direct DHCPv4 traffic - communication with clients which do not /// have IP address assigned yet. /// /// Enabling these modes requires root privilges so they must be /// disabled for unit testing. /// /// Note, that disabling broadcast options on sockets does not impact /// the operation of these tests because they use local loopback /// interface which doesn't have broadcast capability anyway. It rather /// prevents setting broadcast options on other (broadcast capable) /// sockets which are opened on other interfaces in Dhcpv4Srv constructor. /// /// The Direct DHCPv4 Traffic capability can be disabled here because /// it is tested with PktFilterLPFTest unittest. The tests which belong /// to PktFilterLPFTest can be enabled on demand when root privileges can /// be guaranteed.  Marcin Siodelski committed May 22, 2013 82 83 84  /// /// @param port port number to listen on; the default value 0 indicates /// that sockets should not be opened.  Marcin Siodelski committed Apr 04, 2013 85  NakedDhcpv4Srv(uint16_t port = 0)  Marcin Siodelski committed Apr 29, 2013 86  : Dhcpv4Srv(port, "type=memfile", false, false) {  Marcin Siodelski committed Apr 04, 2013 87  }  Tomek Mrugalski committed Dec 31, 2012 88   Tomek Mrugalski committed Jul 15, 2013 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141  /// @brief fakes packet reception /// @param timeout ignored /// /// The method receives all packets queued in receive /// queue, one after another. Once the queue is empty, /// it initiates the shutdown procedure. /// /// See fake_received_ field for description virtual Pkt4Ptr receivePacket(int /*timeout*/) { // If there is anything prepared as fake incoming // traffic, use it if (!fake_received_.empty()) { Pkt4Ptr pkt = fake_received_.front(); fake_received_.pop_front(); return (pkt); } // If not, just trigger shutdown and // return immediately shutdown(); return (Pkt4Ptr()); } /// @brief fake packet sending /// /// Pretend to send a packet, but instead just store /// it in fake_send_ list where test can later inspect /// server's response. virtual void sendPacket(const Pkt4Ptr& pkt) { fake_sent_.push_back(pkt); } /// @brief adds a packet to fake receive queue /// /// See fake_received_ field for description void fakeReceive(const Pkt4Ptr& pkt) { fake_received_.push_back(pkt); } virtual ~NakedDhcpv4Srv() { } /// @brief packets we pretend to receive /// /// Instead of setting up sockets on interfaces that change between OSes, it /// is much easier to fake packet reception. This is a list of packets that /// we pretend to have received. You can schedule new packets to be received /// using fakeReceive() and NakedDhcpv4Srv::receivePacket() methods. list fake_received_; list fake_sent_;  Marcin Siodelski committed May 21, 2013 142  using Dhcpv4Srv::adjustRemoteAddr;  Tomek Mrugalski committed Dec 31, 2012 143 144 145 146 147 148  using Dhcpv4Srv::processDiscover; using Dhcpv4Srv::processRequest; using Dhcpv4Srv::processRelease; using Dhcpv4Srv::processDecline; using Dhcpv4Srv::processInform; using Dhcpv4Srv::getServerID;  Tomek Mrugalski committed Jan 14, 2013 149 150 151  using Dhcpv4Srv::loadServerID; using Dhcpv4Srv::generateServerID; using Dhcpv4Srv::writeServerID;  Tomek Mrugalski committed Dec 31, 2012 152  using Dhcpv4Srv::sanityCheck;  Tomek Mrugalski committed Jan 14, 2013 153  using Dhcpv4Srv::srvidToString;  Tomek Mrugalski committed Nov 30, 2011 154 155 };  Tomek Mrugalski committed Jan 14, 2013 156 157 static const char* SRVID_FILE = "server-id-test.txt";  Marcin Siodelski committed May 21, 2013 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 /// @brief Dummy Packet Filtering class. /// /// This class reports capability to respond directly to the /// client which doesn't have address configured yet. /// /// All packet and socket handling functions do nothing because /// they are not used in unit tests. class PktFilterTest : public PktFilter { public: /// @brief Reports 'direct response' capability. /// /// @return always true. virtual bool isDirectResponseSupported() const { return (true); } /// Does nothing. virtual int openSocket(const Iface&, const IOAddress&, const uint16_t, const bool, const bool) { return (0); } /// Does nothing. virtual Pkt4Ptr receive(const Iface&, const SocketInfo&) { return Pkt4Ptr(); } /// Does nothing. virtual int send(const Iface&, uint16_t, const Pkt4Ptr&) { return (0); } };  Tomek Mrugalski committed Nov 30, 2011 193 194 class Dhcpv4SrvTest : public ::testing::Test { public:  Tomek Mrugalski committed Jan 08, 2013 195 196 197 198 199  /// @brief Constructor /// /// Initializes common objects used in many tests. /// Also sets up initial configuration in CfgMgr.  Tomek Mrugalski committed Nov 30, 2011 200  Dhcpv4SrvTest() {  Tomek Mrugalski committed Dec 31, 2012 201 202 203 204 205 206 207  subnet_ = Subnet4Ptr(new Subnet4(IOAddress("192.0.2.0"), 24, 1000, 2000, 3000)); pool_ = Pool4Ptr(new Pool4(IOAddress("192.0.2.100"), IOAddress("192.0.2.110"))); subnet_->addPool(pool_); CfgMgr::instance().deleteSubnets4(); CfgMgr::instance().addSubnet4(subnet_);  Tomek Mrugalski committed Jan 14, 2013 208   Marcin Siodelski committed Jan 22, 2013 209 210 211 212  // Add Router option. Option4AddrLstPtr opt_routers(new Option4AddrLst(DHO_ROUTERS)); opt_routers->setAddress(IOAddress("192.0.2.2")); subnet_->addOption(opt_routers, false, "dhcp4");  Marcin Siodelski committed Jan 23, 2013 213   Tomek Mrugalski committed Jan 14, 2013 214 215  // it's ok if that fails. There should not be such a file anyway unlink(SRVID_FILE);  Tomek Mrugalski committed Jul 15, 2013 216 217 218 219 220 221 222 223 224 225  const IfaceMgr::IfaceCollection& ifaces = IfaceMgr::instance().getIfaces(); // There must be some interface detected if (ifaces.empty()) { // We can't use ASSERT in constructor ADD_FAILURE() << "No interfaces detected."; } valid_iface_ = ifaces.begin()->getName();  Tomek Mrugalski committed Nov 30, 2011 226  }  Tomek Mrugalski committed Dec 05, 2011 227   Tomek Mrugalski committed Jun 27, 2013 228 229 230  virtual ~Dhcpv4SrvTest() { }  Marcin Siodelski committed Jan 22, 2013 231  /// @brief Add 'Parameter Request List' option to the packet.  Marcin Siodelski committed Jan 16, 2013 232  ///  Marcin Siodelski committed Jan 22, 2013 233  /// This function PRL option comprising the following option codes:  Marcin Siodelski committed Jan 16, 2013 234 235 236 237 238 239 240 241 242 243 244 245 246 247  /// - 5 - Name Server /// - 15 - Domain Name /// - 7 - Log Server /// - 8 - Quotes Server /// - 9 - LPR Server /// /// @param pkt packet to add PRL option to. void addPrlOption(Pkt4Ptr& pkt) { OptionUint8ArrayPtr option_prl = OptionUint8ArrayPtr(new OptionUint8Array(Option::V4, DHO_DHCP_PARAMETER_REQUEST_LIST)); // Let's request options that have been configured for the subnet.  Marcin Siodelski committed Jan 22, 2013 248 249 250 251  option_prl->addValue(DHO_DOMAIN_NAME_SERVERS); option_prl->addValue(DHO_DOMAIN_NAME); option_prl->addValue(DHO_LOG_SERVERS); option_prl->addValue(DHO_COOKIE_SERVERS);  Marcin Siodelski committed Jan 16, 2013 252 253  // Let's also request the option that hasn't been configured. In such // case server should ignore request for this particular option.  Marcin Siodelski committed Jan 22, 2013 254  option_prl->addValue(DHO_LPR_SERVERS);  Marcin Siodelski committed Jan 16, 2013 255 256 257 258 259 260  // And add 'Parameter Request List' option into the DISCOVER packet. pkt->addOption(option_prl); } /// @brief Configures options being requested in the PRL option. ///  Jeremy C. Reed committed Apr 09, 2013 261  /// The lpr-servers option is NOT configured here although it is  Marcin Siodelski committed Jan 16, 2013 262 263  /// added to the 'Parameter Request List' option in the /// \ref addPrlOption. When requested option is not configured  Jeremy C. Reed committed Apr 09, 2013 264  /// the server should not return it in its response. The goal  Marcin Siodelski committed Jan 16, 2013 265 266 267 268 269 270 271 272 273 274 275 276  /// of not configuring the requested option is to verify that /// the server will not return it. void configureRequestedOptions() { // dns-servers Option4AddrLstPtr option_dns_servers(new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS)); option_dns_servers->addAddress(IOAddress("192.0.2.1")); option_dns_servers->addAddress(IOAddress("192.0.2.100")); ASSERT_NO_THROW(subnet_->addOption(option_dns_servers, false, "dhcp4")); // domain-name OptionDefinition def("domain-name", DHO_DOMAIN_NAME, OPT_FQDN_TYPE);  Stephen Morris committed Apr 30, 2013 277  OptionCustomPtr option_domain_name(new OptionCustom(def, Option::V4));  Marcin Siodelski committed Jan 16, 2013 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292  option_domain_name->writeFqdn("example.com"); subnet_->addOption(option_domain_name, false, "dhcp4"); // log-servers Option4AddrLstPtr option_log_servers(new Option4AddrLst(DHO_LOG_SERVERS)); option_log_servers->addAddress(IOAddress("192.0.2.2")); option_log_servers->addAddress(IOAddress("192.0.2.10")); ASSERT_NO_THROW(subnet_->addOption(option_log_servers, false, "dhcp4")); // cookie-servers Option4AddrLstPtr option_cookie_servers(new Option4AddrLst(DHO_COOKIE_SERVERS)); option_cookie_servers->addAddress(IOAddress("192.0.2.1")); ASSERT_NO_THROW(subnet_->addOption(option_cookie_servers, false, "dhcp4")); }  Tomek Mrugalski committed Jan 08, 2013 293 294 295  /// @brief checks that the response matches request /// @param q query (client's message) /// @param a answer (server's message)  Stephen Morris committed Apr 30, 2013 296  void messageCheck(const Pkt4Ptr& q, const Pkt4Ptr& a) {  Tomek Mrugalski committed Dec 22, 2011 297 298 299 300 301 302 303  ASSERT_TRUE(q); ASSERT_TRUE(a); EXPECT_EQ(q->getHops(), a->getHops()); EXPECT_EQ(q->getIface(), a->getIface()); EXPECT_EQ(q->getIndex(), a->getIndex()); EXPECT_EQ(q->getGiaddr(), a->getGiaddr());  Marcin Siodelski committed Apr 29, 2013 304 305 306  // When processing an incoming packet the remote address // is copied as a src address, and the source address is // copied as a remote address to the response.  Marcin Siodelski committed Apr 29, 2013 307 308  EXPECT_TRUE(q->getLocalHWAddr() == a->getLocalHWAddr()); EXPECT_TRUE(q->getRemoteHWAddr() == a->getRemoteHWAddr());  Tomek Mrugalski committed Dec 22, 2011 309   Marcin Siodelski committed Jan 16, 2013 310 311 312  // Check that bare minimum of required options are there. // We don't check options requested by a client. Those // are checked elsewhere.  Tomek Mrugalski committed Dec 22, 2011 313 314 315 316 317  EXPECT_TRUE(a->getOption(DHO_SUBNET_MASK)); EXPECT_TRUE(a->getOption(DHO_ROUTERS)); EXPECT_TRUE(a->getOption(DHO_DHCP_SERVER_IDENTIFIER)); EXPECT_TRUE(a->getOption(DHO_DHCP_LEASE_TIME)); EXPECT_TRUE(a->getOption(DHO_SUBNET_MASK));  Marcin Siodelski committed Apr 09, 2013 318 319  EXPECT_TRUE(a->getOption(DHO_DOMAIN_NAME)); EXPECT_TRUE(a->getOption(DHO_DOMAIN_NAME_SERVERS));  Tomek Mrugalski committed Dec 22, 2011 320   Tomek Mrugalski committed Jan 08, 2013 321  // Check that something is offered  Tomek Mrugalski committed Dec 22, 2011 322 323 324  EXPECT_TRUE(a->getYiaddr().toText() != "0.0.0.0"); }  Marcin Siodelski committed Jan 16, 2013 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345  /// @brief Check that requested options are present. /// /// @param pkt packet to be checked. void optionsCheck(const Pkt4Ptr& pkt) { // Check that the requested and configured options are returned // in the ACK message. EXPECT_TRUE(pkt->getOption(DHO_DOMAIN_NAME)) << "domain-name not present in the response"; EXPECT_TRUE(pkt->getOption(DHO_DOMAIN_NAME_SERVERS)) << "dns-servers not present in the response"; EXPECT_TRUE(pkt->getOption(DHO_LOG_SERVERS)) << "log-servers not present in the response"; EXPECT_TRUE(pkt->getOption(DHO_COOKIE_SERVERS)) << "cookie-servers not present in the response"; // Check that the requested but not configured options are not // returned in the ACK message. EXPECT_FALSE(pkt->getOption(DHO_LPR_SERVERS)) << "domain-name present in the response but it is" << " expected not to be present"; }  Tomek Mrugalski committed Jan 08, 2013 346 347 348 349 350 351 352 353  /// @brief generates client-id option /// /// Generate client-id option of specified length /// Ids with different lengths are sufficent to generate /// unique ids. If more fine grained control is required, /// tests generate client-ids on their own. /// Sets client_id_ field. /// @param size size of the client-id to be generated  Tomek Mrugalski committed Dec 31, 2012 354 355 356 357 358 359 360 361 362 363 364 365 366 367  OptionPtr generateClientId(size_t size = 4) { OptionBuffer clnt_id(size); for (int i = 0; i < size; i++) { clnt_id[i] = 100 + i; } client_id_ = ClientIdPtr(new ClientId(clnt_id)); return (OptionPtr(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER, clnt_id.begin(), clnt_id.begin() + size))); }  Tomek Mrugalski committed Jan 08, 2013 368 369 370 371  /// @brief generate hardware address /// /// @param size size of the generated MAC address /// @param pointer to Hardware Address object  Tomek Mrugalski committed Jan 03, 2013 372  HWAddrPtr generateHWAddr(size_t size = 6) {  Tomek Mrugalski committed Jan 08, 2013 373  const uint8_t hw_type = 123; // Just a fake number (typically 6=HTYPE_ETHER, see dhcp4.h)  Tomek Mrugalski committed Jan 03, 2013 374 375 376 377 378 379 380  OptionBuffer mac(size); for (int i = 0; i < size; ++i) { mac[i] = 50 + i; } return (HWAddrPtr(new HWAddr(mac, hw_type))); }  Tomek Mrugalski committed Jan 08, 2013 381 382 383  /// Check that address was returned from proper range, that its lease /// lifetime is correct, that T1 and T2 are returned properly /// @param rsp response to be checked  Stephen Morris committed Feb 11, 2013 384 385  /// @param subnet subnet that should be used to verify assigned address /// and options  Tomek Mrugalski committed Jan 08, 2013 386 387  /// @param t1_mandatory is T1 mandatory? /// @param t2_mandatory is T2 mandatory?  Tomek Mrugalski committed Dec 31, 2012 388  void checkAddressParams(const Pkt4Ptr& rsp, const SubnetPtr subnet,  Stephen Morris committed Feb 11, 2013 389 390  bool t1_mandatory = false, bool t2_mandatory = false) {  Tomek Mrugalski committed Dec 31, 2012 391 392 393 394 395 396 397  // Technically inPool implies inRange, but let's be on the safe // side and check both. EXPECT_TRUE(subnet->inRange(rsp->getYiaddr())); EXPECT_TRUE(subnet->inPool(rsp->getYiaddr())); // Check lease time  Tomek Mrugalski committed Jan 08, 2013 398 399  OptionPtr opt = rsp->getOption(DHO_DHCP_LEASE_TIME); if (!opt) {  Tomek Mrugalski committed Dec 31, 2012 400 401  ADD_FAILURE() << "Lease time option missing in response"; } else {  Tomek Mrugalski committed Jan 08, 2013 402  EXPECT_EQ(opt->getUint32(), subnet->getValid());  Tomek Mrugalski committed Dec 31, 2012 403 404 405  } // Check T1 timer  Tomek Mrugalski committed Jan 08, 2013 406 407 408  opt = rsp->getOption(DHO_DHCP_RENEWAL_TIME); if (opt) { EXPECT_EQ(opt->getUint32(), subnet->getT1());  Tomek Mrugalski committed Dec 31, 2012 409 410 411 412 413 414 415  } else { if (t1_mandatory) { ADD_FAILURE() << "Required T1 option missing"; } } // Check T2 timer  Tomek Mrugalski committed Jan 08, 2013 416 417 418  opt = rsp->getOption(DHO_DHCP_REBINDING_TIME); if (opt) { EXPECT_EQ(opt->getUint32(), subnet->getT2());  Tomek Mrugalski committed Dec 31, 2012 419  } else {  Stephen Morris committed Feb 11, 2013 420  if (t2_mandatory) {  Tomek Mrugalski committed Dec 31, 2012 421 422 423 424 425  ADD_FAILURE() << "Required T2 option missing"; } } }  Tomek Mrugalski committed Jan 08, 2013 426 427 428 429 430  /// @brief Basic checks for generated response (message type and trans-id). /// /// @param rsp response packet to be validated /// @param expected_message_type expected message type /// @param expected_transid expected transaction-id  Tomek Mrugalski committed Dec 31, 2012 431 432 433 434 435 436 437  void checkResponse(const Pkt4Ptr& rsp, uint8_t expected_message_type, uint32_t expected_transid) { ASSERT_TRUE(rsp); EXPECT_EQ(expected_message_type, rsp->getType()); EXPECT_EQ(expected_transid, rsp->getTransid()); }  Tomek Mrugalski committed Jan 08, 2013 438 439 440 441 442 443  /// @brief Checks if the lease sent to client is present in the database /// /// @param rsp response packet to be validated /// @param client_id expected client-identifier (or NULL) /// @param HWAddr expected hardware address (not used now) /// @param expected_addr expected address  Tomek Mrugalski committed Dec 31, 2012 444  Lease4Ptr checkLease(const Pkt4Ptr& rsp, const OptionPtr& client_id,  Tomek Mrugalski committed Jan 08, 2013 445  const HWAddrPtr&, const IOAddress& expected_addr) {  Tomek Mrugalski committed Dec 31, 2012 446 447 448 449 450 451 452 453 454 455 456 457 458 459  ClientIdPtr id; if (client_id) { OptionBuffer data = client_id->getData(); id.reset(new ClientId(data)); } Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(expected_addr); if (!lease) { cout << "Lease for " << expected_addr.toText() << " not found in the database backend."; return (Lease4Ptr()); }  Tomek Mrugalski committed Jan 08, 2013 460 461  EXPECT_EQ(rsp->getYiaddr().toText(), expected_addr.toText());  Tomek Mrugalski committed Dec 31, 2012 462 463 464 465 466 467 468 469 470  EXPECT_EQ(expected_addr.toText(), lease->addr_.toText()); if (client_id) { EXPECT_TRUE(*lease->client_id_ == *id); } EXPECT_EQ(subnet_->getID(), lease->subnet_id_); return (lease); }  Tomek Mrugalski committed Jan 08, 2013 471 472 473  /// @brief Checks if server response (OFFER, ACK, NAK) includes proper server-id /// @param rsp response packet to be validated /// @param expected_srvid expected value of server-id  Tomek Mrugalski committed Dec 31, 2012 474  void checkServerId(const Pkt4Ptr& rsp, const OptionPtr& expected_srvid) {  Tomek Mrugalski committed Jan 08, 2013 475 476 477 478 479 480  // Check that server included its server-id OptionPtr opt = rsp->getOption(DHO_DHCP_SERVER_IDENTIFIER); ASSERT_TRUE(opt); EXPECT_EQ(opt->getType(), expected_srvid->getType() ); EXPECT_EQ(opt->len(), expected_srvid->len() ); EXPECT_TRUE(opt->getData() == expected_srvid->getData());  Tomek Mrugalski committed Dec 31, 2012 481 482  }  Tomek Mrugalski committed Jan 08, 2013 483 484 485  /// @brief Checks if server response (OFFER, ACK, NAK) includes proper client-id /// @param rsp response packet to be validated /// @param expected_clientid expected value of client-id  Tomek Mrugalski committed Dec 31, 2012 486 487  void checkClientId(const Pkt4Ptr& rsp, const OptionPtr& expected_clientid) { // check that server included our own client-id  Tomek Mrugalski committed Jan 08, 2013 488 489 490 491 492  OptionPtr opt = rsp->getOption(DHO_DHCP_CLIENT_IDENTIFIER); ASSERT_TRUE(opt); EXPECT_EQ(expected_clientid->getType(), opt->getType()); EXPECT_EQ(expected_clientid->len(), opt->len()); EXPECT_TRUE(expected_clientid->getData() == opt->getData());  Tomek Mrugalski committed Dec 31, 2012 493 494  }  Marcin Siodelski committed Mar 28, 2013 495 496 497  /// @brief Tests if Discover or Request message is processed correctly /// /// @param msg_type DHCPDISCOVER or DHCPREQUEST  Marcin Siodelski committed May 22, 2013 498  void testDiscoverRequest(const uint8_t msg_type) {  Marcin Siodelski committed May 21, 2013 499  // Create an instance of the tested class.  Marcin Siodelski committed Apr 11, 2013 500  boost::scoped_ptr srv(new NakedDhcpv4Srv(0));  Marcin Siodelski committed May 21, 2013 501 502  // Initialize the source HW address.  Marcin Siodelski committed Mar 28, 2013 503  vector mac(6);  Marcin Siodelski committed May 22, 2013 504  for (int i = 0; i < 6; ++i) {  Marcin Siodelski committed May 21, 2013 505  mac[i] = i * 10;  Marcin Siodelski committed Mar 28, 2013 506  }  Marcin Siodelski committed May 21, 2013 507  // Initialized the destination HW address.  Marcin Siodelski committed Apr 29, 2013 508  vector dst_mac(6);  Marcin Siodelski committed May 21, 2013 509  for (int i = 0; i < 6; ++i) {  Marcin Siodelski committed Apr 29, 2013 510 511  dst_mac[i] = i * 20; }  Marcin Siodelski committed May 21, 2013 512 513  // Create a DHCP message. It will be used to simulate the // incoming message.  Marcin Siodelski committed Mar 28, 2013 514  boost::shared_ptr req(new Pkt4(msg_type, 1234));  Marcin Siodelski committed May 21, 2013 515 516  // Create a response message. It will hold a reponse packet. // Initially, set it to NULL.  Marcin Siodelski committed Mar 28, 2013 517  boost::shared_ptr rsp;  Marcin Siodelski committed May 21, 2013 518  // Set the name of the interface on which packet is received.  Marcin Siodelski committed Mar 28, 2013 519  req->setIface("eth0");  Marcin Siodelski committed May 21, 2013 520  // Set the interface index. It is just a dummy value and will  Marcin Siodelski committed May 22, 2013 521  // not be interpreted.  Marcin Siodelski committed Mar 28, 2013 522  req->setIndex(17);  Marcin Siodelski committed May 21, 2013 523 524  // Set the target HW address. This value is normally used to // construct the data link layer header.  Marcin Siodelski committed Apr 29, 2013 525  req->setRemoteHWAddr(1, 6, dst_mac);  Marcin Siodelski committed May 21, 2013 526  // Set the HW address. This value is set on DHCP level (in chaddr).  Marcin Siodelski committed Mar 28, 2013 527  req->setHWAddr(1, 6, mac);  Marcin Siodelski committed May 21, 2013 528 529  // Set local HW address. It is used to construct the data link layer // header.  Marcin Siodelski committed Apr 29, 2013 530  req->setLocalHWAddr(1, 6, mac);  Marcin Siodelski committed May 21, 2013 531  // Set target IP address.  Marcin Siodelski committed May 22, 2013 532  req->setRemoteAddr(IOAddress("192.0.2.55"));  Marcin Siodelski committed May 21, 2013 533  // Set relay address.  Marcin Siodelski committed May 22, 2013 534  req->setGiaddr(IOAddress("192.0.2.10"));  Marcin Siodelski committed Mar 28, 2013 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569  // We are going to test that certain options are returned // in the response message when requested using 'Parameter // Request List' option. Let's configure those options that // are returned when requested. configureRequestedOptions(); if (msg_type == DHCPDISCOVER) { ASSERT_NO_THROW( rsp = srv->processDiscover(req); ); // Should return OFFER ASSERT_TRUE(rsp); EXPECT_EQ(DHCPOFFER, rsp->getType()); } else { ASSERT_NO_THROW( rsp = srv->processRequest(req); ); // Should return ACK ASSERT_TRUE(rsp); EXPECT_EQ(DHCPACK, rsp->getType()); } messageCheck(req, rsp); // We did not request any options so these should not be present // in the RSP. EXPECT_FALSE(rsp->getOption(DHO_LOG_SERVERS)); EXPECT_FALSE(rsp->getOption(DHO_COOKIE_SERVERS)); EXPECT_FALSE(rsp->getOption(DHO_LPR_SERVERS));  Marcin Siodelski committed Apr 09, 2013 570  // Repeat the test but request some options.  Marcin Siodelski committed Mar 28, 2013 571 572 573  // Add 'Parameter Request List' option. addPrlOption(req);  Marcin Siodelski committed Apr 09, 2013 574 575 576 577  if (msg_type == DHCPDISCOVER) { ASSERT_NO_THROW( rsp = srv->processDiscover(req); );  Marcin Siodelski committed Mar 28, 2013 578   Marcin Siodelski committed Apr 09, 2013 579 580 581  // Should return non-NULL packet. ASSERT_TRUE(rsp); EXPECT_EQ(DHCPOFFER, rsp->getType());  Marcin Siodelski committed Mar 28, 2013 582   Marcin Siodelski committed Apr 09, 2013 583 584 585 586 587 588 589 590 591 592  } else { ASSERT_NO_THROW( rsp = srv->processRequest(req); ); // Should return non-NULL packet. ASSERT_TRUE(rsp); EXPECT_EQ(DHCPACK, rsp->getType()); }  Marcin Siodelski committed Mar 28, 2013 593 594 595  // Check that the requested options are returned. optionsCheck(rsp);  Marcin Siodelski committed Apr 09, 2013 596   Marcin Siodelski committed Mar 28, 2013 597 598  }  Marcin Siodelski committed May 22, 2013 599 600 601  /// @brief This function cleans up after the test. virtual void TearDown() {  Tomek Mrugalski committed Dec 31, 2012 602  CfgMgr::instance().deleteSubnets4();  Tomek Mrugalski committed Jan 14, 2013 603 604 605  // Let's clean up if there is such a file. unlink(SRVID_FILE);  Marcin Siodelski committed May 22, 2013 606   Marcin Siodelski committed May 23, 2013 607 608 609  // Close all open sockets. IfaceMgr::instance().closeSockets();  Marcin Siodelski committed May 22, 2013 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627  // Some unit tests override the default packet filtering class, used // by the IfaceMgr. The dummy class, called PktFilterTest, reports the // capability to directly respond to the clients without IP address // assigned. This capability is not supported by the default packet // filtering class: PktFilterInet. Therefore setting the dummy class // allows to test scenarios, when server responds to the broadcast address // on client's request, despite having support for direct response. // The following call restores the use of original packet filtering class // after the test. try { IfaceMgr::instance().setPacketFilter(PktFilterPtr(new PktFilterInet())); } catch (const Exception& ex) { FAIL() << "Failed to restore the default (PktFilterInet) packet filtering" << " class after the test. Exception has been caught: " << ex.what(); } }  Tomek Mrugalski committed Dec 31, 2012 628   Tomek Mrugalski committed Jan 08, 2013 629  /// @brief A subnet used in most tests  Tomek Mrugalski committed Dec 31, 2012 630 631  Subnet4Ptr subnet_;  Tomek Mrugalski committed Jan 08, 2013 632  /// @brief A pool used in most tests  Tomek Mrugalski committed Dec 31, 2012 633 634  Pool4Ptr pool_;  Tomek Mrugalski committed Jan 08, 2013 635  /// @brief A client-id used in most tests  Tomek Mrugalski committed Dec 31, 2012 636  ClientIdPtr client_id_;  Tomek Mrugalski committed Jul 15, 2013 637 638 639 640 641 642 643  int rcode_; ConstElementPtr comment_; // Name of a valid network interface string valid_iface_;  Tomek Mrugalski committed Nov 30, 2011 644 645 };  Tomek Mrugalski committed Jan 03, 2013 646 647 // Sanity check. Verifies that both Dhcpv4Srv and its derived // class NakedDhcpv4Srv can be instantiated and destroyed.  Tomek Mrugalski committed Nov 30, 2011 648 649 TEST_F(Dhcpv4SrvTest, basic) {  Tomek Mrugalski committed Jan 08, 2013 650  // Check that the base class can be instantiated  Stephen Morris committed Apr 11, 2013 651  boost::scoped_ptr srv;  Marcin Siodelski committed May 23, 2013 652 653  ASSERT_NO_THROW(srv.reset(new Dhcpv4Srv(DHCP4_SERVER_PORT + 10000, "type=memfile", false, false)));  Stephen Morris committed Apr 11, 2013 654  srv.reset();  Marcin Siodelski committed May 23, 2013 655 656 657 658 659  // 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();  Tomek Mrugalski committed Dec 31, 2012 660   Tomek Mrugalski committed Jan 08, 2013 661  // Check that the derived class can be instantiated  Stephen Morris committed Apr 11, 2013 662 663  boost::scoped_ptr naked_srv; ASSERT_NO_THROW(  Marcin Siodelski committed May 23, 2013 664  naked_srv.reset(new NakedDhcpv4Srv(DHCP4_SERVER_PORT + 10000)));  Tomek Mrugalski committed Dec 31, 2012 665  EXPECT_TRUE(naked_srv->getServerID());  Marcin Siodelski committed May 23, 2013 666 667  // Close sockets again for the next test. IfaceMgr::instance().closeSockets();  Tomek Mrugalski committed Dec 31, 2012 668   Stephen Morris committed Apr 11, 2013 669  ASSERT_NO_THROW(naked_srv.reset(new NakedDhcpv4Srv(0)));  Tomek Mrugalski committed Dec 31, 2012 670  EXPECT_TRUE(naked_srv->getServerID());  Tomek Mrugalski committed Dec 05, 2011 671 672 }  Marcin Siodelski committed May 22, 2013 673 674 675 // 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.  Marcin Siodelski committed May 21, 2013 676 677 678 TEST_F(Dhcpv4SrvTest, adjustRemoteAddressRelay) { boost::scoped_ptr srv(new NakedDhcpv4Srv(0));  Marcin Siodelski committed May 21, 2013 679  // Create the instance of the incoming packet.  Marcin Siodelski committed May 21, 2013 680  boost::shared_ptr req(new Pkt4(DHCPDISCOVER, 1234));  Marcin Siodelski committed May 21, 2013 681  // Set the giaddr to non-zero address as if it was relayed.  Marcin Siodelski committed May 21, 2013 682  req->setGiaddr(IOAddress("192.0.2.1"));  Marcin Siodelski committed May 21, 2013 683 684  // Set ciaddr to zero. This simulates the client which applies // for the new lease.  Marcin Siodelski committed May 21, 2013 685  req->setCiaddr(IOAddress("0.0.0.0"));  Marcin Siodelski committed May 21, 2013 686 687 688 689 690 691 692  // Clear broadcast flag. req->setFlags(0x0000); // 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 resp(new Pkt4(DHCPOFFER, 1234));  Marcin Siodelski committed May 21, 2013 693  resp->setYiaddr(IOAddress("192.0.2.100"));  Marcin Siodelski committed May 21, 2013 694 695  // Clear the remote address. resp->setRemoteAddr(IOAddress("0.0.0.0"));  Marcin Siodelski committed May 21, 2013 696   Marcin Siodelski committed May 21, 2013 697  // This function never throws.  Marcin Siodelski committed May 21, 2013 698 699  ASSERT_NO_THROW(srv->adjustRemoteAddr(req, resp));  Marcin Siodelski committed May 21, 2013 700  // Now the destination address should be relay's address.  Marcin Siodelski committed May 21, 2013 701  EXPECT_EQ("192.0.2.1", resp->getRemoteAddr().toText());  Marcin Siodelski committed May 21, 2013 702 703 704 705 706 707  // 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"));  Marcin Siodelski committed May 22, 2013 708  req->setFlags(Pkt4::FLAG_BROADCAST_MASK);  Marcin Siodelski committed May 21, 2013 709 710 711 712 713 714 715 716 717 718 719  resp->setYiaddr(IOAddress("192.0.2.100")); // Clear remote address. resp->setRemoteAddr(IOAddress("0.0.0.0")); ASSERT_NO_THROW(srv->adjustRemoteAddr(req, resp)); // Response should be sent back to the relay address. EXPECT_EQ("192.0.2.50", resp->getRemoteAddr().toText()); }  Marcin Siodelski committed May 22, 2013 720 721 722 723 // 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.  Marcin Siodelski committed May 21, 2013 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 TEST_F(Dhcpv4SrvTest, adjustRemoteAddressRenewRebind) { boost::scoped_ptr srv(new NakedDhcpv4Srv(0)); // Create instance of the incoming packet. boost::shared_ptr 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.  Marcin Siodelski committed May 22, 2013 742  req->setFlags(Pkt4::FLAG_BROADCAST_MASK);  Marcin Siodelski committed May 21, 2013 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759  // Create a response. boost::shared_ptr 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")); ASSERT_NO_THROW(srv->adjustRemoteAddr(req, resp)); // Check that server responds to ciaddr EXPECT_EQ("192.0.2.15", resp->getRemoteAddr().toText()); }  Marcin Siodelski committed May 22, 2013 760 761 762 763 764 765 766 // 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.  Marcin Siodelski committed May 21, 2013 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 TEST_F(Dhcpv4SrvTest, adjustRemoteAddressSelect) { boost::scoped_ptr srv(new NakedDhcpv4Srv(0)); // Create instance of the incoming packet. boost::shared_ptr 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); // Create a response. boost::shared_ptr 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")); // 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. ASSERT_NO_THROW(srv->adjustRemoteAddr(req, resp)); // 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()); // 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 // response based on the capability reported by IfaceMgr. In order // to set this capability we have to provide a dummy Packet Filter // class which would report the support for direct responses. // This class is called PktFilterTest. IfaceMgr::instance().setPacketFilter(PktFilterPtr(new PktFilterTest())); // Now we expect that the server will send its response to the // address assigned for the client. ASSERT_NO_THROW(srv->adjustRemoteAddr(req, resp)); EXPECT_EQ("192.0.2.13", resp->getRemoteAddr().toText()); }  Marcin Siodelski committed May 22, 2013 817 818 819 820 821 // 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.  Marcin Siodelski committed May 21, 2013 822 823 824 825 826 827 828 829 830 831 832 833 TEST_F(Dhcpv4SrvTest, adjustRemoteAddressBroadcast) { boost::scoped_ptr srv(new NakedDhcpv4Srv(0)); // Create instance of the incoming packet. boost::shared_ptr 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 set the broadcast flag.  Marcin Siodelski committed May 22, 2013 834  req->setFlags(Pkt4::FLAG_BROADCAST_MASK);  Marcin Siodelski committed May 21, 2013 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862  // Create a response. boost::shared_ptr 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")); // When running unit tests, the IfaceMgr is using the default Packet // Filtering class, PktFilterInet. This class does not support direct // responses to the clients without address assigned. If giaddr and // ciaddr are zero and client has just got the new lease, the assigned // address is carried in yiaddr. In order to send this address to the // client, server must send the response to the broadcast address when // direct response is not supported. This conflicts with the purpose // of this test which is supposed to verify that responses are sent // to broadcast address only, when broadcast flag is set. Therefore, // in order to simulate that direct responses are supported we have // to replace the default packet filtering class with a dummy class // which reports direct response capability. IfaceMgr::instance().setPacketFilter(PktFilterPtr(new PktFilterTest())); ASSERT_NO_THROW(srv->adjustRemoteAddr(req, resp)); // 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());  Marcin Siodelski committed May 21, 2013 863 864 }  Marcin Siodelski committed May 22, 2013 865 // Verifies that DISCOVER message can be processed correctly,  Tomek Mrugalski committed Jan 03, 2013 866 867 868 869 870 871 872 // 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.  Marcin Siodelski committed May 22, 2013 873 874 TEST_F(Dhcpv4SrvTest, processDiscover) { testDiscoverRequest(DHCPDISCOVER);  Marcin Siodelski committed Mar 28, 2013 875 }  Marcin Siodelski committed Jan 15, 2013 876   Marcin Siodelski committed May 22, 2013 877 // Verifies that REQUEST message can be processed correctly,  Marcin Siodelski committed Mar 28, 2013 878 // that the OFFER message generated in response is valid and  Tomek Mrugalski committed Jan 03, 2013 879 880 881 882 // contains necessary options. // // Note: this test focuses on the packet correctness. There // are other tests that verify correctness of the allocation  Marcin Siodelski committed Mar 28, 2013 883 884 // engine. See DiscoverBasic, DiscoverHint, DiscoverNoClientId // and DiscoverInvalidHint.  Marcin Siodelski committed May 22, 2013 885 886 TEST_F(Dhcpv4SrvTest, processRequest) { testDiscoverRequest(DHCPREQUEST);  Tomek Mrugalski committed Dec 05, 2011 887 888 889 } TEST_F(Dhcpv4SrvTest, processRelease) {  Stephen Morris committed Apr 11, 2013 890  NakedDhcpv4Srv srv;  Stephen Morris committed Apr 30, 2013 891  Pkt4Ptr pkt(new Pkt4(DHCPRELEASE, 1234));  Tomek Mrugalski committed Dec 05, 2011 892   Tomek Mrugalski committed Jan 08, 2013 893  // Should not throw  Stephen Morris committed Apr 11, 2013 894  EXPECT_NO_THROW(srv.processRelease(pkt));  Tomek Mrugalski committed Dec 05, 2011 895 896 897 } TEST_F(Dhcpv4SrvTest, processDecline) {  Stephen Morris committed Apr 11, 2013 898  NakedDhcpv4Srv srv;  Stephen Morris committed Apr 30, 2013 899  Pkt4Ptr pkt(new Pkt4(DHCPDECLINE, 1234));  Tomek Mrugalski committed Dec 05, 2011 900   Tomek Mrugalski committed Jan 08, 2013 901  // Should not throw  Stephen Morris committed Apr 11, 2013 902  EXPECT_NO_THROW(srv.processDecline(pkt));  Tomek Mrugalski committed Dec 05, 2011 903 904 905 } TEST_F(Dhcpv4SrvTest, processInform) {  Stephen Morris committed Apr 11, 2013 906  NakedDhcpv4Srv srv;  Stephen Morris committed Apr 30, 2013 907  Pkt4Ptr pkt(new Pkt4(DHCPINFORM, 1234));  Tomek Mrugalski committed Dec 05, 2011 908   Tomek Mrugalski committed Jan 08, 2013 909  // Should not throw  Stephen Morris committed Apr 11, 2013 910  EXPECT_NO_THROW(srv.processInform(pkt));  Tomek Mrugalski committed Dec 05, 2011 911   Tomek Mrugalski committed Jan 08, 2013 912  // Should return something  Stephen Morris committed Apr 11, 2013 913  EXPECT_TRUE(srv.processInform(pkt));  Tomek Mrugalski committed Dec 05, 2011 914   Tomek Mrugalski committed Jan 08, 2013 915  // @todo Implement more reasonable tests before starting  Tomek Mrugalski committed Dec 05, 2011 916  // work on processSomething() method.  Tomek Mrugalski committed Nov 30, 2011 917 918 }  Stephen Morris committed Sep 06, 2012 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 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)); } } }  Tomek Mrugalski committed Dec 31, 2012 951 952 953 954 955 956 957 958 959 960 961 962 963 // 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 TEST_F(Dhcpv4SrvTest, DiscoverBasic) { boost::scoped_ptr srv;  Tomek Mrugalski committed Jan 08, 2013 964  ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));  Tomek Mrugalski committed Dec 31, 2012 965 966 967 968 969 970  Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234)); dis->setRemoteAddr(IOAddress("192.0.2.1")); OptionPtr clientid = generateClientId(); dis->addOption(clientid);  Tomek Mrugalski committed Jan 08, 2013 971  // Pass it to the server and get an offer  Tomek Mrugalski committed Dec 31, 2012 972 973  Pkt4Ptr offer = srv->processDiscover(dis);  Tomek Mrugalski committed Jan 08, 2013 974  // Check if we get response at all  Tomek Mrugalski committed Dec 31, 2012 975 976  checkResponse(offer, DHCPOFFER, 1234);  Tomek Mrugalski committed Jan 08, 2013 977  // Check that address was returned from proper range, that its lease  Tomek Mrugalski committed Dec 31, 2012 978 979 980  // lifetime is correct, that T1 and T2 are returned properly checkAddressParams(offer, subnet_);  Tomek Mrugalski committed Jan 08, 2013 981  // Check identifiers  Tomek Mrugalski committed Dec 31, 2012 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000  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 TEST_F(Dhcpv4SrvTest, DiscoverHint) { boost::scoped_ptr srv;  Tomek Mrugalski committed Jan 08, 2013 1001  ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));  Tomek Mrugalski committed Dec 31, 2012 1002 1003 1004 1005 1006 1007 1008 1009  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);  Tomek Mrugalski committed Jan 08, 2013 1010  // Pass it to the server and get an offer  Tomek Mrugalski committed Dec 31, 2012 1011 1012  Pkt4Ptr offer = srv->processDiscover(dis);  Tomek Mrugalski committed Jan 08, 2013 1013  // Check if we get response at all  Tomek Mrugalski committed Dec 31, 2012 1014 1015  checkResponse(offer, DHCPOFFER, 1234);  Tomek Mrugalski committed Jan 08, 2013 1016  // Check that address was returned from proper range, that its lease  Tomek Mrugalski committed Dec 31, 2012 1017 1018 1019 1020 1021  // lifetime is correct, that T1 and T2 are returned properly checkAddressParams(offer, subnet_); EXPECT_EQ(offer->getYiaddr().toText(), hint.toText());  Tomek Mrugalski committed Jan 08, 2013 1022  // Check identifiers  Tomek Mrugalski committed Dec 31, 2012 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040  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 TEST_F(Dhcpv4SrvTest, DiscoverNoClientId) { boost::scoped_ptr srv;  Tomek Mrugalski committed Jan 08, 2013 1041  ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));  Tomek Mrugalski committed Dec 31, 2012 1042 1043 1044 1045 1046  IOAddress hint("192.0.2.107"); Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234)); dis->setRemoteAddr(IOAddress("192.0.2.1")); dis->setYiaddr(hint);  Tomek Mrugalski committed Jul 16, 2013 1047  dis->setHWAddr(generateHWAddr(6));  Tomek Mrugalski committed Dec 31, 2012 1048   Tomek Mrugalski committed Jan 08, 2013 1049  // Pass it to the server and get an offer  Tomek Mrugalski committed Dec 31, 2012 1050 1051  Pkt4Ptr offer = srv->processDiscover(dis);  Tomek Mrugalski committed Jan 08, 2013 1052  // Check if we get response at all  Tomek Mrugalski committed Dec 31, 2012 1053 1054  checkResponse(offer, DHCPOFFER, 1234);  Tomek Mrugalski committed Jan 08, 2013 1055  // Check that address was returned from proper range, that its lease  Tomek Mrugalski committed Dec 31, 2012 1056 1057 1058 1059 1060  // lifetime is correct, that T1 and T2 are returned properly checkAddressParams(offer, subnet_); EXPECT_EQ(offer->getYiaddr().toText(), hint.toText());  Tomek Mrugalski committed Jan 08, 2013 1061  // Check identifiers  Tomek Mrugalski committed Dec 31, 2012 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078  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) TEST_F(Dhcpv4SrvTest, DiscoverInvalidHint) { boost::scoped_ptr srv;  Tomek Mrugalski committed Jan 08, 2013 1079  ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));  Tomek Mrugalski committed Dec 31, 2012 1080 1081 1082 1083 1084 1085 1086 1087  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);  Tomek Mrugalski committed Jan 08, 2013 1088  // Pass it to the server and get an offer  Tomek Mrugalski committed Dec 31, 2012 1089 1090  Pkt4Ptr offer = srv->processDiscover(dis);  Tomek Mrugalski committed Jan 08, 2013 1091  // Check if we get response at all  Tomek Mrugalski committed Dec 31, 2012 1092 1093  checkResponse(offer, DHCPOFFER, 1234);  Tomek Mrugalski committed Jan 08, 2013 1094  // Check that address was returned from proper range, that its lease  Tomek Mrugalski committed Dec 31, 2012 1095 1096 1097 1098 1099  // lifetime is correct, that T1 and T2 are returned properly checkAddressParams(offer, subnet_); EXPECT_NE(offer->getYiaddr().toText(), hint.toText());  Tomek Mrugalski committed Jan 08, 2013 1100  // Check identifiers  Tomek Mrugalski committed Dec 31, 2012 1101 1102 1103 1104 1105 1106 1107 1108  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 committed Jan 03, 2013 1109 1110 // 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  Tomek Mrugalski committed Jan 08, 2013 1111 // 2 addresses, the third client would get the same offer as the first one  Tomek Mrugalski committed Dec 31, 2012 1112 // and this is a correct behavior. It is REQUEST that will fail for the third  Tomek Mrugalski committed Jan 03, 2013 1113 // client. OFFER is basically saying "if you send me a request, you will  Tomek Mrugalski committed Dec 31, 2012 1114 1115 1116 // probably get an address like this" (there are no guarantees). TEST_F(Dhcpv4SrvTest, ManyDiscovers) { boost::scoped_ptr srv;  Tomek Mrugalski committed Jan 08, 2013 1117  ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));  Tomek Mrugalski committed Dec 31, 2012 1118 1119 1120 1121 1122 1123 1124 1125 1126  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"));  Tomek Mrugalski committed Jan 08, 2013 1127  // Different client-id sizes  Tomek Mrugalski committed Dec 31, 2012 1128 1129 1130 1131 1132 1133 1134 1135  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);  Tomek Mrugalski committed Jan 08, 2013 1136 1137 1138 1139  // Pass it to the server and get an offer Pkt4Ptr offer1 = srv->processDiscover(dis1); Pkt4Ptr offer2 = srv->processDiscover(dis2); Pkt4Ptr offer3 = srv->processDiscover(dis3);  Tomek Mrugalski committed Dec 31, 2012 1140   Tomek Mrugalski committed Jan 08, 2013 1141 1142 1143 1144  // Check if we get response at all checkResponse(offer1, DHCPOFFER, 1234); checkResponse(offer2, DHCPOFFER, 2345); checkResponse(offer3, DHCPOFFER, 3456);  Tomek Mrugalski committed Dec 31, 2012 1145   Tomek Mrugalski committed Jan 08, 2013 1146 1147 1148  IOAddress addr1 = offer1->getYiaddr(); IOAddress addr2 = offer2->getYiaddr(); IOAddress addr3 = offer3->getYiaddr();  Tomek Mrugalski committed Dec 31, 2012 1149 1150  // Check that the assigned address is indeed from the configured pool  Tomek Mrugalski committed Jan 08, 2013 1151 1152 1153 1154  checkAddressParams(offer1, subnet_); checkAddressParams(offer2, subnet_); checkAddressParams(offer3, subnet_);  Tomek Mrugalski committed Jan 14, 2013 1155  // Check server-ids  Tomek Mrugalski committed Jan 08, 2013 1156 1157 1158 1159 1160 1161  checkServerId(offer1, srv->getServerID()); checkServerId(offer2, srv->getServerID()); checkServerId(offer3, srv->getServerID()); checkClientId(offer1, clientid1); checkClientId(offer2, clientid2); checkClientId(offer3, clientid3);  Tomek Mrugalski committed Dec 31, 2012 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171  // 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 committed Jan 03, 2013 1172 1173 // This test verifies that incoming REQUEST can be handled properly, that an // ACK is generated, that the response has an address and that address  Tomek Mrugalski committed Dec 31, 2012 1174 1175 // really belongs to the configured pool. //  Tomek Mrugalski committed Jan 03, 2013 1176 // constructed a single REQUEST message with:  Tomek Mrugalski committed Dec 31, 2012 1177 // - client-id option  Tomek Mrugalski committed Jan 03, 2013 1178 1179 // - hwaddr information // - requested address (that the client received in DISCOVER/OFFER exchange)  Tomek Mrugalski committed Dec 31, 2012 1180 //  Tomek Mrugalski committed Jan 03, 2013 1181 // expected returned ACK message:  Tomek Mrugalski committed Dec 31, 2012 1182 1183 // - copy of client-id // - server-id  Tomek Mrugalski committed Jan 03, 2013 1184 1185 1186 // - assigned address // // Test verifies that the lease is actually in the database.  Tomek Mrugalski committed Dec 31, 2012 1187 1188 TEST_F(Dhcpv4SrvTest, RequestBasic) { boost::scoped_ptr srv;  Tomek Mrugalski committed Jan 08, 2013 1189  ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));  Tomek Mrugalski committed Dec 31, 2012 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200  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); // Pass it to the server and get an advertise Pkt4Ptr ack = srv->processRequest(req);  Tomek Mrugalski committed Jan 08, 2013 1201  // Check if we get response at all  Tomek Mrugalski committed Dec 31, 2012 1202 1203 1204  checkResponse(ack, DHCPACK, 1234); EXPECT_EQ(hint.toText(), ack->getYiaddr().toText());  Tomek Mrugalski committed Jan 08, 2013 1205  // Check that address was returned from proper range, that its lease  Tomek Mrugalski committed Dec 31, 2012 1206 1207 1208  // lifetime is correct, that T1 and T2 are returned properly checkAddressParams(ack, subnet_);  Tomek Mrugalski committed Jan 08, 2013 1209  // Check identifiers  Tomek Mrugalski committed Dec 31, 2012 1210 1211 1212  checkServerId(ack, srv->getServerID()); checkClientId(ack, clientid);  Tomek Mrugalski committed Jan 08, 2013 1213  // Check that the lease is really in the database  Tomek Mrugalski committed Dec 31, 2012 1214 1215 1216 1217 1218 1219  Lease4Ptr l = checkLease(ack, clientid, req->getHWAddr(), hint); ASSERT_TRUE(l); LeaseMgrFactory::instance().deleteLease(l->addr_); }  Tomek Mrugalski committed Jan 03, 2013 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 // 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) TEST_F(Dhcpv4SrvTest, ManyRequests) { boost::scoped_ptr srv;  Tomek Mrugalski committed Jan 08, 2013 1235  ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));  Tomek Mrugalski committed Jan 03, 2013 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257  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); req1->setYiaddr(req_addr1); req2->setYiaddr(req_addr2); req3->setYiaddr(req_addr3); req1->setHWAddr(generateHWAddr(6)); req2->setHWAddr(generateHWAddr(7)); req3->setHWAddr(generateHWAddr(8));  Tomek Mrugalski committed Jan 08, 2013 1258  // Different client-id sizes  Tomek Mrugalski committed Jan 03, 2013 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271  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);  Tomek Mrugalski committed Jan 08, 2013 1272  // Check if we get response at all  Tomek Mrugalski committed Jan 03, 2013 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290  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_);  Tomek Mrugalski committed Jan 08, 2013 1291  // Check DUIDs  Tomek Mrugalski committed Jan 03, 2013 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315  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; } // This test verifies that incoming (positive) REQUEST/Renewing can be handled properly, that a  Tomek Mrugalski committed Dec 31, 2012 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 // 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 TEST_F(Dhcpv4SrvTest, RenewBasic) { boost::scoped_ptr srv;  Tomek Mrugalski committed Jan 08, 2013 1326  ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));  Tomek Mrugalski committed Dec 31, 2012 1327 1328  const IOAddress addr("192.0.2.106");  Tomek Mrugalski committed Jan 02, 2013 1329 1330 1331 1332  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;  Tomek Mrugalski committed Dec 31, 2012 1333   Tomek Mrugalski committed Jan 08, 2013 1334  // Generate client-id also sets client_id_ member  Tomek Mrugalski committed Dec 31, 2012 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362  OptionPtr clientid = generateClientId(); // Check that the address we are about to use is indeed in pool ASSERT_TRUE(subnet_->inPool(addr)); // 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 committed Jan 03, 2013 1363  req->setCiaddr(addr); // client's address  Tomek Mrugalski committed Dec 31, 2012 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374  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());  Tomek Mrugalski committed Jan 08, 2013 1375  // Check that address was returned from proper range, that its lease  Tomek Mrugalski committed Dec 31, 2012 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394  // 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(l->cltt_); int32_t expected = static_cast(time(NULL));  Tomek Mrugalski committed Jan 08, 2013 1395  // Equality or difference by 1 between cltt and expected is ok.  Tomek Mrugalski committed Dec 31, 2012 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405  EXPECT_GE(1, abs(cltt - expected)); EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr)); } // @todo: Implement tests for rejecting renewals // This test verifies if the sanityCheck() really checks options presence. TEST_F(Dhcpv4SrvTest, sanityCheck) { boost::scoped_ptr srv;  Tomek Mrugalski committed Jan 08, 2013