dhcp4_test_utils.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. // Copyright (C) 2013-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <asiolink/io_address.h>
  16. #include <cc/data.h>
  17. #include <config/ccsession.h>
  18. #include <dhcp4/json_config_parser.h>
  19. #include <dhcp4/tests/dhcp4_test_utils.h>
  20. #include <dhcp/option4_addrlst.h>
  21. #include <dhcp/option_int.h>
  22. #include <dhcp/option_int_array.h>
  23. #include <dhcp/option_custom.h>
  24. #include <dhcp/iface_mgr.h>
  25. #include <dhcp/tests/iface_mgr_test_config.h>
  26. #include <dhcpsrv/cfgmgr.h>
  27. #include <dhcpsrv/lease.h>
  28. #include <dhcpsrv/lease_mgr.h>
  29. #include <dhcpsrv/lease_mgr_factory.h>
  30. using namespace std;
  31. using namespace isc::asiolink;
  32. using namespace isc::data;
  33. namespace isc {
  34. namespace dhcp {
  35. namespace test {
  36. Dhcpv4SrvTest::Dhcpv4SrvTest()
  37. :rcode_(-1), srv_(0) {
  38. subnet_ = Subnet4Ptr(new Subnet4(IOAddress("192.0.2.0"), 24, 1000,
  39. 2000, 3000));
  40. pool_ = Pool4Ptr(new Pool4(IOAddress("192.0.2.100"), IOAddress("192.0.2.110")));
  41. subnet_->addPool(pool_);
  42. // Add Router option.
  43. Option4AddrLstPtr opt_routers(new Option4AddrLst(DHO_ROUTERS));
  44. opt_routers->setAddress(IOAddress("192.0.2.2"));
  45. subnet_->getCfgOption()->add(opt_routers, false, "dhcp4");
  46. CfgMgr::instance().clear();
  47. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnet_);
  48. CfgMgr::instance().commit();
  49. }
  50. Dhcpv4SrvTest::~Dhcpv4SrvTest() {
  51. // Make sure that we revert to default value
  52. CfgMgr::instance().clear();
  53. CfgMgr::instance().echoClientId(true);
  54. }
  55. void Dhcpv4SrvTest::addPrlOption(Pkt4Ptr& pkt) {
  56. OptionUint8ArrayPtr option_prl =
  57. OptionUint8ArrayPtr(new OptionUint8Array(Option::V4,
  58. DHO_DHCP_PARAMETER_REQUEST_LIST));
  59. // Let's request options that have been configured for the subnet.
  60. option_prl->addValue(DHO_DOMAIN_NAME_SERVERS);
  61. option_prl->addValue(DHO_DOMAIN_NAME);
  62. option_prl->addValue(DHO_LOG_SERVERS);
  63. option_prl->addValue(DHO_COOKIE_SERVERS);
  64. // Let's also request the option that hasn't been configured. In such
  65. // case server should ignore request for this particular option.
  66. option_prl->addValue(DHO_LPR_SERVERS);
  67. // And add 'Parameter Request List' option into the DISCOVER packet.
  68. pkt->addOption(option_prl);
  69. }
  70. void Dhcpv4SrvTest::configureRequestedOptions() {
  71. // dns-servers
  72. Option4AddrLstPtr
  73. option_dns_servers(new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS));
  74. option_dns_servers->addAddress(IOAddress("192.0.2.1"));
  75. option_dns_servers->addAddress(IOAddress("192.0.2.100"));
  76. ASSERT_NO_THROW(subnet_->getCfgOption()->add(option_dns_servers, false, "dhcp4"));
  77. // domain-name
  78. OptionDefinition def("domain-name", DHO_DOMAIN_NAME, OPT_FQDN_TYPE);
  79. OptionCustomPtr option_domain_name(new OptionCustom(def, Option::V4));
  80. option_domain_name->writeFqdn("example.com");
  81. subnet_->getCfgOption()->add(option_domain_name, false, "dhcp4");
  82. // log-servers
  83. Option4AddrLstPtr option_log_servers(new Option4AddrLst(DHO_LOG_SERVERS));
  84. option_log_servers->addAddress(IOAddress("192.0.2.2"));
  85. option_log_servers->addAddress(IOAddress("192.0.2.10"));
  86. ASSERT_NO_THROW(subnet_->getCfgOption()->add(option_log_servers, false, "dhcp4"));
  87. // cookie-servers
  88. Option4AddrLstPtr option_cookie_servers(new Option4AddrLst(DHO_COOKIE_SERVERS));
  89. option_cookie_servers->addAddress(IOAddress("192.0.2.1"));
  90. ASSERT_NO_THROW(subnet_->getCfgOption()->add(option_cookie_servers, false, "dhcp4"));
  91. }
  92. void Dhcpv4SrvTest::messageCheck(const Pkt4Ptr& q, const Pkt4Ptr& a) {
  93. ASSERT_TRUE(q);
  94. ASSERT_TRUE(a);
  95. EXPECT_EQ(q->getHops(), a->getHops());
  96. EXPECT_EQ(q->getIface(), a->getIface());
  97. EXPECT_EQ(q->getIndex(), a->getIndex());
  98. EXPECT_EQ(q->getGiaddr(), a->getGiaddr());
  99. // When processing an incoming packet the remote address
  100. // is copied as a src address, and the source address is
  101. // copied as a remote address to the response.
  102. EXPECT_TRUE(q->getLocalHWAddr() == a->getLocalHWAddr());
  103. EXPECT_TRUE(q->getRemoteHWAddr() == a->getRemoteHWAddr());
  104. // Check that the server identifier is present in the response.
  105. // Presence (or absence) of other options is checked elsewhere.
  106. EXPECT_TRUE(a->getOption(DHO_DHCP_SERVER_IDENTIFIER));
  107. // Check that something is offered
  108. EXPECT_NE("0.0.0.0", a->getYiaddr().toText());
  109. }
  110. ::testing::AssertionResult
  111. Dhcpv4SrvTest::basicOptionsPresent(const Pkt4Ptr& pkt) {
  112. std::ostringstream errmsg;
  113. errmsg << "option missing in the response";
  114. if (!pkt->getOption(DHO_DOMAIN_NAME)) {
  115. return (::testing::AssertionFailure(::testing::Message()
  116. << "domain-name " << errmsg));
  117. } else if (!pkt->getOption(DHO_DOMAIN_NAME_SERVERS)) {
  118. return (::testing::AssertionFailure(::testing::Message()
  119. << "dns-servers " << errmsg));
  120. } else if (!pkt->getOption(DHO_SUBNET_MASK)) {
  121. return (::testing::AssertionFailure(::testing::Message()
  122. << "subnet-mask " << errmsg));
  123. } else if (!pkt->getOption(DHO_ROUTERS)) {
  124. return (::testing::AssertionFailure(::testing::Message() << "routers "
  125. << errmsg));
  126. } else if (!pkt->getOption(DHO_DHCP_LEASE_TIME)) {
  127. return (::testing::AssertionFailure(::testing::Message() <<
  128. "dhcp-lease-time " << errmsg));
  129. }
  130. return (::testing::AssertionSuccess());
  131. }
  132. ::testing::AssertionResult
  133. Dhcpv4SrvTest::noBasicOptions(const Pkt4Ptr& pkt) {
  134. std::ostringstream errmsg;
  135. errmsg << "option present in the response";
  136. if (pkt->getOption(DHO_DOMAIN_NAME)) {
  137. return (::testing::AssertionFailure(::testing::Message()
  138. << "domain-name " << errmsg));
  139. } else if (pkt->getOption(DHO_DOMAIN_NAME_SERVERS)) {
  140. return (::testing::AssertionFailure(::testing::Message()
  141. << "dns-servers " << errmsg));
  142. } else if (pkt->getOption(DHO_SUBNET_MASK)) {
  143. return (::testing::AssertionFailure(::testing::Message()
  144. << "subnet-mask " << errmsg));
  145. } else if (pkt->getOption(DHO_ROUTERS)) {
  146. return (::testing::AssertionFailure(::testing::Message() << "routers "
  147. << errmsg));
  148. } else if (pkt->getOption(DHO_DHCP_LEASE_TIME)) {
  149. return (::testing::AssertionFailure(::testing::Message()
  150. << "dhcp-lease-time " << errmsg));
  151. }
  152. return (::testing::AssertionSuccess());
  153. }
  154. ::testing::AssertionResult
  155. Dhcpv4SrvTest::requestedOptionsPresent(const Pkt4Ptr& pkt) {
  156. std::ostringstream errmsg;
  157. errmsg << "option missing in the response";
  158. if (!pkt->getOption(DHO_LOG_SERVERS)) {
  159. return (::testing::AssertionFailure(::testing::Message()
  160. << "log-servers " << errmsg));
  161. } else if (!pkt->getOption(DHO_COOKIE_SERVERS)) {
  162. return (::testing::AssertionFailure(::testing::Message()
  163. << "cookie-servers " << errmsg));
  164. }
  165. return (::testing::AssertionSuccess());
  166. }
  167. ::testing::AssertionResult
  168. Dhcpv4SrvTest::noRequestedOptions(const Pkt4Ptr& pkt) {
  169. std::ostringstream errmsg;
  170. errmsg << "option present in the response";
  171. if (pkt->getOption(DHO_LOG_SERVERS)) {
  172. return (::testing::AssertionFailure(::testing::Message()
  173. << "log-servers " << errmsg));
  174. } else if (pkt->getOption(DHO_COOKIE_SERVERS)) {
  175. return (::testing::AssertionFailure(::testing::Message()
  176. << "cookie-servers " << errmsg));
  177. }
  178. return (::testing::AssertionSuccess());
  179. }
  180. OptionPtr Dhcpv4SrvTest::generateClientId(size_t size /*= 4*/) {
  181. OptionBuffer clnt_id(size);
  182. for (size_t i = 0; i < size; i++) {
  183. clnt_id[i] = 100 + i;
  184. }
  185. client_id_ = ClientIdPtr(new ClientId(clnt_id));
  186. return (OptionPtr(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER,
  187. clnt_id.begin(),
  188. clnt_id.begin() + size)));
  189. }
  190. HWAddrPtr Dhcpv4SrvTest::generateHWAddr(size_t size /*= 6*/) {
  191. const uint8_t hw_type = 123; // Just a fake number (typically 6=HTYPE_ETHER, see dhcp4.h)
  192. OptionBuffer mac(size);
  193. for (size_t i = 0; i < size; ++i) {
  194. mac[i] = 50 + i;
  195. }
  196. return (HWAddrPtr(new HWAddr(mac, hw_type)));
  197. }
  198. void Dhcpv4SrvTest::checkAddressParams(const Pkt4Ptr& rsp,
  199. const SubnetPtr subnet,
  200. bool t1_present,
  201. bool t2_present) {
  202. // Technically inPool implies inRange, but let's be on the safe
  203. // side and check both.
  204. EXPECT_TRUE(subnet->inRange(rsp->getYiaddr()));
  205. EXPECT_TRUE(subnet->inPool(Lease::TYPE_V4, rsp->getYiaddr()));
  206. // Check lease time
  207. OptionUint32Ptr opt = boost::dynamic_pointer_cast<
  208. OptionUint32>(rsp->getOption(DHO_DHCP_LEASE_TIME));
  209. if (!opt) {
  210. ADD_FAILURE() << "Lease time option missing in response or the"
  211. " option has unexpected type";
  212. } else {
  213. EXPECT_EQ(opt->getValue(), subnet->getValid());
  214. }
  215. // Check T1 timer
  216. opt = boost::dynamic_pointer_cast<
  217. OptionUint32>(rsp->getOption(DHO_DHCP_RENEWAL_TIME));
  218. if (t1_present) {
  219. ASSERT_TRUE(opt) << "Required T1 option missing or it has"
  220. " an unexpected type";
  221. EXPECT_EQ(opt->getValue(), subnet->getT1());
  222. } else {
  223. EXPECT_FALSE(opt);
  224. }
  225. // Check T2 timer
  226. opt = boost::dynamic_pointer_cast<
  227. OptionUint32>(rsp->getOption(DHO_DHCP_REBINDING_TIME));
  228. if (t2_present) {
  229. ASSERT_TRUE(opt) << "Required T2 option missing or it has"
  230. " an unexpected type";
  231. EXPECT_EQ(opt->getValue(), subnet->getT2());
  232. } else {
  233. EXPECT_FALSE(opt);
  234. }
  235. }
  236. void Dhcpv4SrvTest::checkResponse(const Pkt4Ptr& rsp, int expected_message_type,
  237. uint32_t expected_transid) {
  238. ASSERT_TRUE(rsp);
  239. EXPECT_EQ(expected_message_type,
  240. static_cast<int>(rsp->getType()));
  241. EXPECT_EQ(expected_transid, rsp->getTransid());
  242. }
  243. Lease4Ptr Dhcpv4SrvTest::checkLease(const Pkt4Ptr& rsp,
  244. const OptionPtr& client_id,
  245. const HWAddrPtr&,
  246. const IOAddress& expected_addr) {
  247. ClientIdPtr id;
  248. if (client_id) {
  249. OptionBuffer data = client_id->getData();
  250. id.reset(new ClientId(data));
  251. }
  252. Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(expected_addr);
  253. if (!lease) {
  254. cout << "Lease for " << expected_addr
  255. << " not found in the database backend.";
  256. return (Lease4Ptr());
  257. }
  258. EXPECT_EQ(rsp->getYiaddr(), expected_addr);
  259. EXPECT_EQ(expected_addr, lease->addr_);
  260. if (client_id) {
  261. EXPECT_TRUE(*lease->client_id_ == *id);
  262. }
  263. EXPECT_EQ(subnet_->getID(), lease->subnet_id_);
  264. return (lease);
  265. }
  266. void Dhcpv4SrvTest::checkServerId(const Pkt4Ptr& rsp, const OptionPtr& expected_srvid) {
  267. // Check that server included its server-id
  268. OptionPtr opt = rsp->getOption(DHO_DHCP_SERVER_IDENTIFIER);
  269. ASSERT_TRUE(opt);
  270. EXPECT_EQ(opt->getType(), expected_srvid->getType() );
  271. EXPECT_EQ(opt->len(), expected_srvid->len() );
  272. EXPECT_TRUE(opt->getData() == expected_srvid->getData());
  273. }
  274. void Dhcpv4SrvTest::checkClientId(const Pkt4Ptr& rsp, const OptionPtr& expected_clientid) {
  275. bool include_clientid = CfgMgr::instance().echoClientId();
  276. // check that server included our own client-id
  277. OptionPtr opt = rsp->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
  278. if (include_clientid) {
  279. // Normal mode: echo back (see RFC6842)
  280. ASSERT_TRUE(opt);
  281. EXPECT_EQ(expected_clientid->getType(), opt->getType());
  282. EXPECT_EQ(expected_clientid->len(), opt->len());
  283. EXPECT_TRUE(expected_clientid->getData() == opt->getData());
  284. } else {
  285. // Backward compatibility mode for pre-RFC6842 devices
  286. ASSERT_FALSE(opt);
  287. }
  288. }
  289. ::testing::AssertionResult
  290. Dhcpv4SrvTest::createPacketFromBuffer(const Pkt4Ptr& src_pkt,
  291. Pkt4Ptr& dst_pkt) {
  292. // Create on-wire format of the packet. If pack() has been called
  293. // on this instance of the packet already, the next call to pack()
  294. // should remove all contents of the output buffer.
  295. try {
  296. src_pkt->pack();
  297. } catch (const Exception& ex) {
  298. return (::testing::AssertionFailure(::testing::Message()
  299. << "Failed to parse source packet: "
  300. << ex.what()));
  301. }
  302. // Get the output buffer from the source packet.
  303. const util::OutputBuffer& buf = src_pkt->getBuffer();
  304. // Create a copy of the packet using the output buffer from the source
  305. // packet.
  306. try {
  307. dst_pkt.reset(new Pkt4(static_cast<const uint8_t*>(buf.getData()),
  308. buf.getLength()));
  309. } catch (const Exception& ex) {
  310. return (::testing::AssertionFailure(::testing::Message()
  311. << "Failed to create a"
  312. " destination packet from"
  313. " the buffer: "
  314. << ex.what()));
  315. }
  316. try {
  317. // Parse the new packet and return to the caller.
  318. dst_pkt->unpack();
  319. } catch (const Exception& ex) {
  320. return (::testing::AssertionFailure(::testing::Message()
  321. << "Failed to parse a"
  322. << " destination packet: "
  323. << ex.what()));
  324. }
  325. return (::testing::AssertionSuccess());
  326. }
  327. void
  328. // cppcheck-suppress unusedFunction
  329. Dhcpv4SrvTest::TearDown() {
  330. CfgMgr::instance().clear();
  331. // Close all open sockets.
  332. IfaceMgr::instance().closeSockets();
  333. // Some unit tests override the default packet filtering class, used
  334. // by the IfaceMgr. The dummy class, called PktFilterTest, reports the
  335. // capability to directly respond to the clients without IP address
  336. // assigned. This capability is not supported by the default packet
  337. // filtering class: PktFilterInet. Therefore setting the dummy class
  338. // allows to test scenarios, when server responds to the broadcast address
  339. // on client's request, despite having support for direct response.
  340. // The following call restores the use of original packet filtering class
  341. // after the test.
  342. try {
  343. IfaceMgr::instance().setPacketFilter(PktFilterPtr(new PktFilterInet()));
  344. } catch (const Exception& ex) {
  345. FAIL() << "Failed to restore the default (PktFilterInet) packet filtering"
  346. << " class after the test. Exception has been caught: "
  347. << ex.what();
  348. }
  349. }
  350. void
  351. Dhcpv4SrvTest::testDiscoverRequest(const uint8_t msg_type) {
  352. IfaceMgrTestConfig test_config(true);
  353. IfaceMgr::instance().openSockets4();
  354. // Create an instance of the tested class.
  355. boost::scoped_ptr<NakedDhcpv4Srv> srv(new NakedDhcpv4Srv(0));
  356. // Initialize the source HW address.
  357. vector<uint8_t> mac(6);
  358. for (uint8_t i = 0; i < 6; ++i) {
  359. mac[i] = i * 10;
  360. }
  361. // Initialized the destination HW address.
  362. vector<uint8_t> dst_mac(6);
  363. for (uint8_t i = 0; i < 6; ++i) {
  364. dst_mac[i] = i * 20;
  365. }
  366. // Create a DHCP message. It will be used to simulate the
  367. // incoming message.
  368. boost::shared_ptr<Pkt4> req(new Pkt4(msg_type, 1234));
  369. // Create a response message. It will hold a response packet.
  370. // Initially, set it to NULL.
  371. boost::shared_ptr<Pkt4> rsp;
  372. // Set the name of the interface on which packet is received.
  373. req->setIface("eth0");
  374. // Set the interface index. It is just a dummy value and will
  375. // not be interpreted.
  376. req->setIndex(17);
  377. // Set the target HW address. This value is normally used to
  378. // construct the data link layer header.
  379. req->setRemoteHWAddr(1, 6, dst_mac);
  380. // Set the HW address. This value is set on DHCP level (in chaddr).
  381. req->setHWAddr(1, 6, mac);
  382. // Set local HW address. It is used to construct the data link layer
  383. // header.
  384. req->setLocalHWAddr(1, 6, mac);
  385. // Set target IP address.
  386. req->setRemoteAddr(IOAddress("192.0.2.55"));
  387. // Set relay address and hops.
  388. req->setGiaddr(IOAddress("192.0.2.10"));
  389. req->setHops(1);
  390. // We are going to test that certain options are returned
  391. // in the response message when requested using 'Parameter
  392. // Request List' option. Let's configure those options that
  393. // are returned when requested.
  394. configureRequestedOptions();
  395. // Create a copy of the original packet by parsing its wire format.
  396. // This simulates the real life scenario when we process the packet
  397. // which was parsed from its wire format.
  398. Pkt4Ptr received;
  399. ASSERT_TRUE(createPacketFromBuffer(req, received));
  400. // Set interface. It is required for the server to generate server id.
  401. received->setIface("eth0");
  402. if (msg_type == DHCPDISCOVER) {
  403. ASSERT_NO_THROW(
  404. rsp = srv->processDiscover(received);
  405. );
  406. // Should return OFFER
  407. ASSERT_TRUE(rsp);
  408. EXPECT_EQ(DHCPOFFER, rsp->getType());
  409. } else {
  410. ASSERT_NO_THROW(rsp = srv->processRequest(received));
  411. // Should return ACK
  412. ASSERT_TRUE(rsp);
  413. EXPECT_EQ(DHCPACK, rsp->getType());
  414. }
  415. messageCheck(received, rsp);
  416. // Basic options should be present when we got the lease.
  417. EXPECT_TRUE(basicOptionsPresent(rsp));
  418. // We did not request any options so these should not be present
  419. // in the RSP.
  420. EXPECT_TRUE(noRequestedOptions(rsp));
  421. // Repeat the test but request some options.
  422. // Add 'Parameter Request List' option.
  423. addPrlOption(req);
  424. ASSERT_TRUE(createPacketFromBuffer(req, received));
  425. ASSERT_TRUE(received->getOption(DHO_DHCP_PARAMETER_REQUEST_LIST));
  426. // Set interface. It is required for the server to generate server id.
  427. received->setIface("eth0");
  428. if (msg_type == DHCPDISCOVER) {
  429. ASSERT_NO_THROW(rsp = srv->processDiscover(received));
  430. // Should return non-NULL packet.
  431. ASSERT_TRUE(rsp);
  432. EXPECT_EQ(DHCPOFFER, rsp->getType());
  433. } else {
  434. ASSERT_NO_THROW(rsp = srv->processRequest(received));
  435. // Should return non-NULL packet.
  436. ASSERT_TRUE(rsp);
  437. EXPECT_EQ(DHCPACK, rsp->getType());
  438. }
  439. // Check that the requested options are returned.
  440. EXPECT_TRUE(basicOptionsPresent(rsp));
  441. EXPECT_TRUE(requestedOptionsPresent(rsp));
  442. // The following part of the test will test that the NAK is sent when
  443. // there is no address pool configured. In the same time, we expect
  444. // that the requested options are not included in NAK message, but that
  445. // they are only included when yiaddr is set to non-zero value.
  446. ASSERT_NO_THROW(subnet_->delPools(Lease::TYPE_V4));
  447. // There has been a lease allocated for the particular client. So,
  448. // even though we deleted the subnet, the client would get the
  449. // existing lease (not a NAK). Therefore, we have to change the chaddr
  450. // in the packet so as the existing lease is not returned.
  451. req->setHWAddr(1, 6, std::vector<uint8_t>(2, 6));
  452. ASSERT_TRUE(createPacketFromBuffer(req, received));
  453. ASSERT_TRUE(received->getOption(DHO_DHCP_PARAMETER_REQUEST_LIST));
  454. // Set interface. It is required for the server to generate server id.
  455. received->setIface("eth0");
  456. if (msg_type == DHCPDISCOVER) {
  457. ASSERT_NO_THROW(rsp = srv->processDiscover(received));
  458. // Should return NULL packet.
  459. ASSERT_FALSE(rsp);
  460. } else {
  461. ASSERT_NO_THROW(rsp = srv->processRequest(received));
  462. // Should return non-NULL packet.
  463. ASSERT_TRUE(rsp);
  464. // We should get the NAK packet with yiaddr set to 0.
  465. EXPECT_EQ(DHCPNAK, rsp->getType());
  466. ASSERT_EQ("0.0.0.0", rsp->getYiaddr().toText());
  467. // Make sure that none of the requested options is returned in NAK.
  468. // Also options such as Routers or Subnet Mask should not be there,
  469. // because lease hasn't been acquired.
  470. EXPECT_TRUE(noRequestedOptions(rsp));
  471. EXPECT_TRUE(noBasicOptions(rsp));
  472. }
  473. }
  474. void
  475. Dhcpv4SrvTest::configure(const std::string& config, const bool commit) {
  476. configure(config, srv_, commit);
  477. }
  478. void
  479. Dhcpv4SrvTest::configure(const std::string& config, NakedDhcpv4Srv& srv,
  480. const bool commit) {
  481. ElementPtr json = Element::fromJSON(config);
  482. ConstElementPtr status;
  483. // Configure the server and make sure the config is accepted
  484. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  485. ASSERT_TRUE(status);
  486. int rcode;
  487. ConstElementPtr comment = config::parseAnswer(rcode, status);
  488. ASSERT_EQ(0, rcode);
  489. if (commit) {
  490. CfgMgr::instance().commit();
  491. }
  492. }
  493. Dhcpv4Exchange
  494. Dhcpv4SrvTest::createExchange(const Pkt4Ptr& query) {
  495. return (Dhcpv4Exchange(srv_.alloc_engine_, query, srv_.selectSubnet(query)));
  496. }
  497. }; // end of isc::dhcp::test namespace
  498. }; // end of isc::dhcp namespace
  499. }; // end of isc namespace