dhcp4_srv_unittest.cc 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. // Copyright (C) 2011-2013 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 <sstream>
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/dhcp4.h>
  18. #include <dhcp/option.h>
  19. #include <dhcp/option4_addrlst.h>
  20. #include <dhcp/option_custom.h>
  21. #include <dhcp/option_int_array.h>
  22. #include <dhcp4/dhcp4_srv.h>
  23. #include <dhcp4/dhcp4_log.h>
  24. #include <dhcpsrv/cfgmgr.h>
  25. #include <dhcpsrv/lease_mgr.h>
  26. #include <dhcpsrv/lease_mgr_factory.h>
  27. #include <dhcpsrv/utils.h>
  28. #include <gtest/gtest.h>
  29. #include <fstream>
  30. #include <iostream>
  31. #include <arpa/inet.h>
  32. using namespace std;
  33. using namespace isc;
  34. using namespace isc::dhcp;
  35. using namespace isc::asiolink;
  36. namespace {
  37. class NakedDhcpv4Srv: public Dhcpv4Srv {
  38. // "Naked" DHCPv4 server, exposes internal fields
  39. public:
  40. NakedDhcpv4Srv(uint16_t port = 0):Dhcpv4Srv(port) { }
  41. using Dhcpv4Srv::processDiscover;
  42. using Dhcpv4Srv::processRequest;
  43. using Dhcpv4Srv::processRelease;
  44. using Dhcpv4Srv::processDecline;
  45. using Dhcpv4Srv::processInform;
  46. using Dhcpv4Srv::getServerID;
  47. using Dhcpv4Srv::loadServerID;
  48. using Dhcpv4Srv::generateServerID;
  49. using Dhcpv4Srv::writeServerID;
  50. using Dhcpv4Srv::sanityCheck;
  51. using Dhcpv4Srv::srvidToString;
  52. };
  53. static const char* SRVID_FILE = "server-id-test.txt";
  54. class Dhcpv4SrvTest : public ::testing::Test {
  55. public:
  56. /// @brief Constructor
  57. ///
  58. /// Initializes common objects used in many tests.
  59. /// Also sets up initial configuration in CfgMgr.
  60. Dhcpv4SrvTest() {
  61. subnet_ = Subnet4Ptr(new Subnet4(IOAddress("192.0.2.0"), 24, 1000,
  62. 2000, 3000));
  63. pool_ = Pool4Ptr(new Pool4(IOAddress("192.0.2.100"), IOAddress("192.0.2.110")));
  64. subnet_->addPool(pool_);
  65. CfgMgr::instance().deleteSubnets4();
  66. CfgMgr::instance().addSubnet4(subnet_);
  67. // Add Router option.
  68. Option4AddrLstPtr opt_routers(new Option4AddrLst(DHO_ROUTERS));
  69. opt_routers->setAddress(IOAddress("192.0.2.2"));
  70. subnet_->addOption(opt_routers, false, "dhcp4");
  71. // it's ok if that fails. There should not be such a file anyway
  72. unlink(SRVID_FILE);
  73. }
  74. /// @brief Add 'Parameter Request List' option to the packet.
  75. ///
  76. /// This function PRL option comprising the following option codes:
  77. /// - 5 - Name Server
  78. /// - 15 - Domain Name
  79. /// - 7 - Log Server
  80. /// - 8 - Quotes Server
  81. /// - 9 - LPR Server
  82. ///
  83. /// @param pkt packet to add PRL option to.
  84. void addPrlOption(Pkt4Ptr& pkt) {
  85. OptionUint8ArrayPtr option_prl =
  86. OptionUint8ArrayPtr(new OptionUint8Array(Option::V4,
  87. DHO_DHCP_PARAMETER_REQUEST_LIST));
  88. // Let's request options that have been configured for the subnet.
  89. option_prl->addValue(DHO_DOMAIN_NAME_SERVERS);
  90. option_prl->addValue(DHO_DOMAIN_NAME);
  91. option_prl->addValue(DHO_LOG_SERVERS);
  92. option_prl->addValue(DHO_COOKIE_SERVERS);
  93. // Let's also request the option that hasn't been configured. In such
  94. // case server should ignore request for this particular option.
  95. option_prl->addValue(DHO_LPR_SERVERS);
  96. // And add 'Parameter Request List' option into the DISCOVER packet.
  97. pkt->addOption(option_prl);
  98. }
  99. /// @brief Configures options being requested in the PRL option.
  100. ///
  101. /// The lpr-servers option is NOT configured here altough it is
  102. /// added to the 'Parameter Request List' option in the
  103. /// \ref addPrlOption. When requested option is not configured
  104. /// the server should not return it in its rensponse. The goal
  105. /// of not configuring the requested option is to verify that
  106. /// the server will not return it.
  107. void configureRequestedOptions() {
  108. // dns-servers
  109. Option4AddrLstPtr
  110. option_dns_servers(new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS));
  111. option_dns_servers->addAddress(IOAddress("192.0.2.1"));
  112. option_dns_servers->addAddress(IOAddress("192.0.2.100"));
  113. ASSERT_NO_THROW(subnet_->addOption(option_dns_servers, false, "dhcp4"));
  114. // domain-name
  115. OptionDefinition def("domain-name", DHO_DOMAIN_NAME, OPT_FQDN_TYPE);
  116. boost::shared_ptr<OptionCustom>
  117. option_domain_name(new OptionCustom(def, Option::V4));
  118. option_domain_name->writeFqdn("example.com");
  119. subnet_->addOption(option_domain_name, false, "dhcp4");
  120. // log-servers
  121. Option4AddrLstPtr option_log_servers(new Option4AddrLst(DHO_LOG_SERVERS));
  122. option_log_servers->addAddress(IOAddress("192.0.2.2"));
  123. option_log_servers->addAddress(IOAddress("192.0.2.10"));
  124. ASSERT_NO_THROW(subnet_->addOption(option_log_servers, false, "dhcp4"));
  125. // cookie-servers
  126. Option4AddrLstPtr option_cookie_servers(new Option4AddrLst(DHO_COOKIE_SERVERS));
  127. option_cookie_servers->addAddress(IOAddress("192.0.2.1"));
  128. ASSERT_NO_THROW(subnet_->addOption(option_cookie_servers, false, "dhcp4"));
  129. }
  130. /// @brief checks that the response matches request
  131. /// @param q query (client's message)
  132. /// @param a answer (server's message)
  133. void messageCheck(const boost::shared_ptr<Pkt4>& q,
  134. const boost::shared_ptr<Pkt4>& a) {
  135. ASSERT_TRUE(q);
  136. ASSERT_TRUE(a);
  137. EXPECT_EQ(q->getHops(), a->getHops());
  138. EXPECT_EQ(q->getIface(), a->getIface());
  139. EXPECT_EQ(q->getIndex(), a->getIndex());
  140. EXPECT_EQ(q->getGiaddr(), a->getGiaddr());
  141. // Check that bare minimum of required options are there.
  142. // We don't check options requested by a client. Those
  143. // are checked elsewhere.
  144. EXPECT_TRUE(a->getOption(DHO_SUBNET_MASK));
  145. EXPECT_TRUE(a->getOption(DHO_ROUTERS));
  146. EXPECT_TRUE(a->getOption(DHO_DHCP_SERVER_IDENTIFIER));
  147. EXPECT_TRUE(a->getOption(DHO_DHCP_LEASE_TIME));
  148. EXPECT_TRUE(a->getOption(DHO_SUBNET_MASK));
  149. // Check that something is offered
  150. EXPECT_TRUE(a->getYiaddr().toText() != "0.0.0.0");
  151. }
  152. /// @brief Check that requested options are present.
  153. ///
  154. /// @param pkt packet to be checked.
  155. void optionsCheck(const Pkt4Ptr& pkt) {
  156. // Check that the requested and configured options are returned
  157. // in the ACK message.
  158. EXPECT_TRUE(pkt->getOption(DHO_DOMAIN_NAME))
  159. << "domain-name not present in the response";
  160. EXPECT_TRUE(pkt->getOption(DHO_DOMAIN_NAME_SERVERS))
  161. << "dns-servers not present in the response";
  162. EXPECT_TRUE(pkt->getOption(DHO_LOG_SERVERS))
  163. << "log-servers not present in the response";
  164. EXPECT_TRUE(pkt->getOption(DHO_COOKIE_SERVERS))
  165. << "cookie-servers not present in the response";
  166. // Check that the requested but not configured options are not
  167. // returned in the ACK message.
  168. EXPECT_FALSE(pkt->getOption(DHO_LPR_SERVERS))
  169. << "domain-name present in the response but it is"
  170. << " expected not to be present";
  171. }
  172. /// @brief generates client-id option
  173. ///
  174. /// Generate client-id option of specified length
  175. /// Ids with different lengths are sufficent to generate
  176. /// unique ids. If more fine grained control is required,
  177. /// tests generate client-ids on their own.
  178. /// Sets client_id_ field.
  179. /// @param size size of the client-id to be generated
  180. OptionPtr generateClientId(size_t size = 4) {
  181. OptionBuffer clnt_id(size);
  182. for (int 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. /// @brief generate hardware address
  191. ///
  192. /// @param size size of the generated MAC address
  193. /// @param pointer to Hardware Address object
  194. HWAddrPtr generateHWAddr(size_t size = 6) {
  195. const uint8_t hw_type = 123; // Just a fake number (typically 6=HTYPE_ETHER, see dhcp4.h)
  196. OptionBuffer mac(size);
  197. for (int i = 0; i < size; ++i) {
  198. mac[i] = 50 + i;
  199. }
  200. return (HWAddrPtr(new HWAddr(mac, hw_type)));
  201. }
  202. /// Check that address was returned from proper range, that its lease
  203. /// lifetime is correct, that T1 and T2 are returned properly
  204. /// @param rsp response to be checked
  205. /// @param subnet subnet that should be used to verify assigned address
  206. /// and options
  207. /// @param t1_mandatory is T1 mandatory?
  208. /// @param t2_mandatory is T2 mandatory?
  209. void checkAddressParams(const Pkt4Ptr& rsp, const SubnetPtr subnet,
  210. bool t1_mandatory = false,
  211. bool t2_mandatory = false) {
  212. // Technically inPool implies inRange, but let's be on the safe
  213. // side and check both.
  214. EXPECT_TRUE(subnet->inRange(rsp->getYiaddr()));
  215. EXPECT_TRUE(subnet->inPool(rsp->getYiaddr()));
  216. // Check lease time
  217. OptionPtr opt = rsp->getOption(DHO_DHCP_LEASE_TIME);
  218. if (!opt) {
  219. ADD_FAILURE() << "Lease time option missing in response";
  220. } else {
  221. EXPECT_EQ(opt->getUint32(), subnet->getValid());
  222. }
  223. // Check T1 timer
  224. opt = rsp->getOption(DHO_DHCP_RENEWAL_TIME);
  225. if (opt) {
  226. EXPECT_EQ(opt->getUint32(), subnet->getT1());
  227. } else {
  228. if (t1_mandatory) {
  229. ADD_FAILURE() << "Required T1 option missing";
  230. }
  231. }
  232. // Check T2 timer
  233. opt = rsp->getOption(DHO_DHCP_REBINDING_TIME);
  234. if (opt) {
  235. EXPECT_EQ(opt->getUint32(), subnet->getT2());
  236. } else {
  237. if (t2_mandatory) {
  238. ADD_FAILURE() << "Required T2 option missing";
  239. }
  240. }
  241. }
  242. /// @brief Basic checks for generated response (message type and trans-id).
  243. ///
  244. /// @param rsp response packet to be validated
  245. /// @param expected_message_type expected message type
  246. /// @param expected_transid expected transaction-id
  247. void checkResponse(const Pkt4Ptr& rsp, uint8_t expected_message_type,
  248. uint32_t expected_transid) {
  249. ASSERT_TRUE(rsp);
  250. EXPECT_EQ(expected_message_type, rsp->getType());
  251. EXPECT_EQ(expected_transid, rsp->getTransid());
  252. }
  253. /// @brief Checks if the lease sent to client is present in the database
  254. ///
  255. /// @param rsp response packet to be validated
  256. /// @param client_id expected client-identifier (or NULL)
  257. /// @param HWAddr expected hardware address (not used now)
  258. /// @param expected_addr expected address
  259. Lease4Ptr checkLease(const Pkt4Ptr& rsp, const OptionPtr& client_id,
  260. const HWAddrPtr&, const IOAddress& expected_addr) {
  261. ClientIdPtr id;
  262. if (client_id) {
  263. OptionBuffer data = client_id->getData();
  264. id.reset(new ClientId(data));
  265. }
  266. Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(expected_addr);
  267. if (!lease) {
  268. cout << "Lease for " << expected_addr.toText()
  269. << " not found in the database backend.";
  270. return (Lease4Ptr());
  271. }
  272. EXPECT_EQ(rsp->getYiaddr().toText(), expected_addr.toText());
  273. EXPECT_EQ(expected_addr.toText(), lease->addr_.toText());
  274. if (client_id) {
  275. EXPECT_TRUE(*lease->client_id_ == *id);
  276. }
  277. EXPECT_EQ(subnet_->getID(), lease->subnet_id_);
  278. return (lease);
  279. }
  280. /// @brief Checks if server response (OFFER, ACK, NAK) includes proper server-id
  281. /// @param rsp response packet to be validated
  282. /// @param expected_srvid expected value of server-id
  283. void checkServerId(const Pkt4Ptr& rsp, const OptionPtr& expected_srvid) {
  284. // Check that server included its server-id
  285. OptionPtr opt = rsp->getOption(DHO_DHCP_SERVER_IDENTIFIER);
  286. ASSERT_TRUE(opt);
  287. EXPECT_EQ(opt->getType(), expected_srvid->getType() );
  288. EXPECT_EQ(opt->len(), expected_srvid->len() );
  289. EXPECT_TRUE(opt->getData() == expected_srvid->getData());
  290. }
  291. /// @brief Checks if server response (OFFER, ACK, NAK) includes proper client-id
  292. /// @param rsp response packet to be validated
  293. /// @param expected_clientid expected value of client-id
  294. void checkClientId(const Pkt4Ptr& rsp, const OptionPtr& expected_clientid) {
  295. // check that server included our own client-id
  296. OptionPtr opt = rsp->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
  297. ASSERT_TRUE(opt);
  298. EXPECT_EQ(expected_clientid->getType(), opt->getType());
  299. EXPECT_EQ(expected_clientid->len(), opt->len());
  300. EXPECT_TRUE(expected_clientid->getData() == opt->getData());
  301. }
  302. ~Dhcpv4SrvTest() {
  303. CfgMgr::instance().deleteSubnets4();
  304. // Let's clean up if there is such a file.
  305. unlink(SRVID_FILE);
  306. };
  307. /// @brief A subnet used in most tests
  308. Subnet4Ptr subnet_;
  309. /// @brief A pool used in most tests
  310. Pool4Ptr pool_;
  311. /// @brief A client-id used in most tests
  312. ClientIdPtr client_id_;
  313. };
  314. // Sanity check. Verifies that both Dhcpv4Srv and its derived
  315. // class NakedDhcpv4Srv can be instantiated and destroyed.
  316. TEST_F(Dhcpv4SrvTest, basic) {
  317. // Check that the base class can be instantiated
  318. Dhcpv4Srv* srv = NULL;
  319. ASSERT_NO_THROW({
  320. srv = new Dhcpv4Srv(DHCP4_SERVER_PORT + 10000);
  321. });
  322. delete srv;
  323. // Check that the derived class can be instantiated
  324. NakedDhcpv4Srv* naked_srv = NULL;
  325. ASSERT_NO_THROW({
  326. naked_srv = new NakedDhcpv4Srv(DHCP4_SERVER_PORT + 10000);
  327. });
  328. EXPECT_TRUE(naked_srv->getServerID());
  329. delete naked_srv;
  330. ASSERT_NO_THROW({
  331. naked_srv = new NakedDhcpv4Srv(0);
  332. });
  333. EXPECT_TRUE(naked_srv->getServerID());
  334. delete naked_srv;
  335. }
  336. // Verifies that received DISCOVER can be processed correctly,
  337. // that the OFFER message generated in response is valid and
  338. // contains necessary options.
  339. //
  340. // Note: this test focuses on the packet correctness. There
  341. // are other tests that verify correctness of the allocation
  342. // engine. See DiscoverBasic, DiscoverHint, DiscoverNoClientId
  343. // and DiscoverInvalidHint.
  344. TEST_F(Dhcpv4SrvTest, processDiscover) {
  345. NakedDhcpv4Srv* srv = new NakedDhcpv4Srv(0);
  346. vector<uint8_t> mac(6);
  347. for (int i = 0; i < 6; i++) {
  348. mac[i] = 255 - i;
  349. }
  350. boost::shared_ptr<Pkt4> pkt(new Pkt4(DHCPDISCOVER, 1234));
  351. boost::shared_ptr<Pkt4> offer;
  352. pkt->setIface("eth0");
  353. pkt->setIndex(17);
  354. pkt->setHWAddr(1, 6, mac);
  355. pkt->setRemoteAddr(IOAddress("192.0.2.56"));
  356. pkt->setGiaddr(IOAddress("192.0.2.67"));
  357. // Let's make it a relayed message
  358. pkt->setHops(3);
  359. pkt->setRemotePort(DHCP4_SERVER_PORT);
  360. // We are going to test that certain options are returned
  361. // (or not returned) in the OFFER message when requested
  362. // using 'Parameter Request List' option. Let's configure
  363. // those options that are returned when requested.
  364. configureRequestedOptions();
  365. // Should not throw
  366. EXPECT_NO_THROW(
  367. offer = srv->processDiscover(pkt);
  368. );
  369. // Should return something
  370. ASSERT_TRUE(offer);
  371. EXPECT_EQ(DHCPOFFER, offer->getType());
  372. // This is relayed message. It should be sent back to relay address.
  373. EXPECT_EQ(pkt->getGiaddr(), offer->getRemoteAddr());
  374. messageCheck(pkt, offer);
  375. // There are some options that are always present in the
  376. // message, even if not requested.
  377. EXPECT_TRUE(offer->getOption(DHO_DOMAIN_NAME));
  378. EXPECT_TRUE(offer->getOption(DHO_DOMAIN_NAME_SERVERS));
  379. // We did not request any options so they should not be present
  380. // in the OFFER.
  381. EXPECT_FALSE(offer->getOption(DHO_LOG_SERVERS));
  382. EXPECT_FALSE(offer->getOption(DHO_COOKIE_SERVERS));
  383. EXPECT_FALSE(offer->getOption(DHO_LPR_SERVERS));
  384. // Add 'Parameter Request List' option.
  385. addPrlOption(pkt);
  386. // Now repeat the test but request some options.
  387. EXPECT_NO_THROW(
  388. offer = srv->processDiscover(pkt);
  389. );
  390. // Should return something
  391. ASSERT_TRUE(offer);
  392. EXPECT_EQ(DHCPOFFER, offer->getType());
  393. // This is relayed message. It should be sent back to relay address.
  394. EXPECT_EQ(pkt->getGiaddr(), offer->getRemoteAddr());
  395. messageCheck(pkt, offer);
  396. // Check that the requested options are returned.
  397. optionsCheck(offer);
  398. // Now repeat the test for directly sent message
  399. pkt->setHops(0);
  400. pkt->setGiaddr(IOAddress("0.0.0.0"));
  401. pkt->setRemotePort(DHCP4_CLIENT_PORT);
  402. EXPECT_NO_THROW(
  403. offer = srv->processDiscover(pkt);
  404. );
  405. // Should return something
  406. ASSERT_TRUE(offer);
  407. EXPECT_EQ(DHCPOFFER, offer->getType());
  408. // This is direct message. It should be sent back to origin, not
  409. // to relay.
  410. EXPECT_EQ(pkt->getRemoteAddr(), offer->getRemoteAddr());
  411. messageCheck(pkt, offer);
  412. // Check that the requested options are returned.
  413. optionsCheck(offer);
  414. delete srv;
  415. }
  416. // Verifies that received REQUEST can be processed correctly,
  417. // that the ACK message generated in response is valid and
  418. // contains necessary options.
  419. //
  420. // Note: this test focuses on the packet correctness. There
  421. // are other tests that verify correctness of the allocation
  422. // engine. See RequestBasic.
  423. TEST_F(Dhcpv4SrvTest, processRequest) {
  424. NakedDhcpv4Srv* srv = new NakedDhcpv4Srv(0);
  425. vector<uint8_t> mac(6);
  426. for (int i = 0; i < 6; i++) {
  427. mac[i] = i*10;
  428. }
  429. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPREQUEST, 1234));
  430. boost::shared_ptr<Pkt4> ack;
  431. req->setIface("eth0");
  432. req->setIndex(17);
  433. req->setHWAddr(1, 6, mac);
  434. req->setRemoteAddr(IOAddress("192.0.2.56"));
  435. req->setGiaddr(IOAddress("192.0.2.67"));
  436. // We are going to test that certain options are returned
  437. // in the ACK message when requested using 'Parameter
  438. // Request List' option. Let's configure those options that
  439. // are returned when requested.
  440. configureRequestedOptions();
  441. // Should not throw
  442. ASSERT_NO_THROW(
  443. ack = srv->processRequest(req);
  444. );
  445. // Should return something
  446. ASSERT_TRUE(ack);
  447. EXPECT_EQ(DHCPACK, ack->getType());
  448. // This is relayed message. It should be sent back to relay address.
  449. EXPECT_EQ(req->getGiaddr(), ack->getRemoteAddr());
  450. messageCheck(req, ack);
  451. // There are some options that are always present in the
  452. // message, even if not requested.
  453. EXPECT_TRUE(ack->getOption(DHO_DOMAIN_NAME));
  454. EXPECT_TRUE(ack->getOption(DHO_DOMAIN_NAME_SERVERS));
  455. // We did not request any options so these should not be present
  456. // in the ACK.
  457. EXPECT_FALSE(ack->getOption(DHO_LOG_SERVERS));
  458. EXPECT_FALSE(ack->getOption(DHO_COOKIE_SERVERS));
  459. EXPECT_FALSE(ack->getOption(DHO_LPR_SERVERS));
  460. // Add 'Parameter Request List' option.
  461. addPrlOption(req);
  462. // Repeat the test but request some options.
  463. ASSERT_NO_THROW(
  464. ack = srv->processRequest(req);
  465. );
  466. // Should return something
  467. ASSERT_TRUE(ack);
  468. EXPECT_EQ(DHCPACK, ack->getType());
  469. // This is relayed message. It should be sent back to relay address.
  470. EXPECT_EQ(req->getGiaddr(), ack->getRemoteAddr());
  471. // Check that the requested options are returned.
  472. optionsCheck(ack);
  473. // Now repeat the test for directly sent message
  474. req->setHops(0);
  475. req->setGiaddr(IOAddress("0.0.0.0"));
  476. req->setRemotePort(DHCP4_CLIENT_PORT);
  477. EXPECT_NO_THROW(
  478. ack = srv->processDiscover(req);
  479. );
  480. // Should return something
  481. ASSERT_TRUE(ack);
  482. EXPECT_EQ(DHCPOFFER, ack->getType());
  483. // This is direct message. It should be sent back to origin, not
  484. // to relay.
  485. EXPECT_EQ(ack->getRemoteAddr(), req->getRemoteAddr());
  486. messageCheck(req, ack);
  487. // Check that the requested options are returned.
  488. optionsCheck(ack);
  489. delete srv;
  490. }
  491. TEST_F(Dhcpv4SrvTest, processRelease) {
  492. NakedDhcpv4Srv* srv = new NakedDhcpv4Srv();
  493. boost::shared_ptr<Pkt4> pkt(new Pkt4(DHCPRELEASE, 1234));
  494. // Should not throw
  495. EXPECT_NO_THROW(
  496. srv->processRelease(pkt);
  497. );
  498. delete srv;
  499. }
  500. TEST_F(Dhcpv4SrvTest, processDecline) {
  501. NakedDhcpv4Srv* srv = new NakedDhcpv4Srv();
  502. boost::shared_ptr<Pkt4> pkt(new Pkt4(DHCPDECLINE, 1234));
  503. // Should not throw
  504. EXPECT_NO_THROW(
  505. srv->processDecline(pkt);
  506. );
  507. delete srv;
  508. }
  509. TEST_F(Dhcpv4SrvTest, processInform) {
  510. NakedDhcpv4Srv* srv = new NakedDhcpv4Srv();
  511. boost::shared_ptr<Pkt4> pkt(new Pkt4(DHCPINFORM, 1234));
  512. // Should not throw
  513. EXPECT_NO_THROW(
  514. srv->processInform(pkt);
  515. );
  516. // Should return something
  517. EXPECT_TRUE(srv->processInform(pkt));
  518. // @todo Implement more reasonable tests before starting
  519. // work on processSomething() method.
  520. delete srv;
  521. }
  522. TEST_F(Dhcpv4SrvTest, serverReceivedPacketName) {
  523. // Check all possible packet types
  524. for (int itype = 0; itype < 256; ++itype) {
  525. uint8_t type = itype;
  526. switch (type) {
  527. case DHCPDECLINE:
  528. EXPECT_STREQ("DECLINE", Dhcpv4Srv::serverReceivedPacketName(type));
  529. break;
  530. case DHCPDISCOVER:
  531. EXPECT_STREQ("DISCOVER", Dhcpv4Srv::serverReceivedPacketName(type));
  532. break;
  533. case DHCPINFORM:
  534. EXPECT_STREQ("INFORM", Dhcpv4Srv::serverReceivedPacketName(type));
  535. break;
  536. case DHCPRELEASE:
  537. EXPECT_STREQ("RELEASE", Dhcpv4Srv::serverReceivedPacketName(type));
  538. break;
  539. case DHCPREQUEST:
  540. EXPECT_STREQ("REQUEST", Dhcpv4Srv::serverReceivedPacketName(type));
  541. break;
  542. default:
  543. EXPECT_STREQ("UNKNOWN", Dhcpv4Srv::serverReceivedPacketName(type));
  544. }
  545. }
  546. }
  547. // This test verifies that incoming DISCOVER can be handled properly, that an
  548. // OFFER is generated, that the response has an address and that address
  549. // really belongs to the configured pool.
  550. //
  551. // constructed very simple DISCOVER message with:
  552. // - client-id option
  553. //
  554. // expected returned OFFER message:
  555. // - copy of client-id
  556. // - server-id
  557. // - offered address
  558. TEST_F(Dhcpv4SrvTest, DiscoverBasic) {
  559. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  560. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  561. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  562. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  563. OptionPtr clientid = generateClientId();
  564. dis->addOption(clientid);
  565. // Pass it to the server and get an offer
  566. Pkt4Ptr offer = srv->processDiscover(dis);
  567. // Check if we get response at all
  568. checkResponse(offer, DHCPOFFER, 1234);
  569. // Check that address was returned from proper range, that its lease
  570. // lifetime is correct, that T1 and T2 are returned properly
  571. checkAddressParams(offer, subnet_);
  572. // Check identifiers
  573. checkServerId(offer, srv->getServerID());
  574. checkClientId(offer, clientid);
  575. }
  576. // This test verifies that incoming DISCOVER can be handled properly, that an
  577. // OFFER is generated, that the response has an address and that address
  578. // really belongs to the configured pool.
  579. //
  580. // constructed very simple DISCOVER message with:
  581. // - client-id option
  582. // - address set to specific value as hint
  583. //
  584. // expected returned OFFER message:
  585. // - copy of client-id
  586. // - server-id
  587. // - offered address
  588. TEST_F(Dhcpv4SrvTest, DiscoverHint) {
  589. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  590. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  591. IOAddress hint("192.0.2.107");
  592. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  593. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  594. OptionPtr clientid = generateClientId();
  595. dis->addOption(clientid);
  596. dis->setYiaddr(hint);
  597. // Pass it to the server and get an offer
  598. Pkt4Ptr offer = srv->processDiscover(dis);
  599. // Check if we get response at all
  600. checkResponse(offer, DHCPOFFER, 1234);
  601. // Check that address was returned from proper range, that its lease
  602. // lifetime is correct, that T1 and T2 are returned properly
  603. checkAddressParams(offer, subnet_);
  604. EXPECT_EQ(offer->getYiaddr().toText(), hint.toText());
  605. // Check identifiers
  606. checkServerId(offer, srv->getServerID());
  607. checkClientId(offer, clientid);
  608. }
  609. // This test verifies that incoming DISCOVER can be handled properly, that an
  610. // OFFER is generated, that the response has an address and that address
  611. // really belongs to the configured pool.
  612. //
  613. // constructed very simple DISCOVER message with:
  614. // - address set to specific value as hint
  615. //
  616. // expected returned OFFER message:
  617. // - copy of client-id
  618. // - server-id
  619. // - offered address
  620. TEST_F(Dhcpv4SrvTest, DiscoverNoClientId) {
  621. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  622. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  623. IOAddress hint("192.0.2.107");
  624. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  625. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  626. dis->setYiaddr(hint);
  627. // Pass it to the server and get an offer
  628. Pkt4Ptr offer = srv->processDiscover(dis);
  629. // Check if we get response at all
  630. checkResponse(offer, DHCPOFFER, 1234);
  631. // Check that address was returned from proper range, that its lease
  632. // lifetime is correct, that T1 and T2 are returned properly
  633. checkAddressParams(offer, subnet_);
  634. EXPECT_EQ(offer->getYiaddr().toText(), hint.toText());
  635. // Check identifiers
  636. checkServerId(offer, srv->getServerID());
  637. }
  638. // This test verifies that incoming DISCOVER can be handled properly, that an
  639. // OFFER is generated, that the response has an address and that address
  640. // really belongs to the configured pool.
  641. //
  642. // constructed very simple DISCOVER message with:
  643. // - client-id option
  644. // - address set to specific value as hint, but that hint is invalid
  645. //
  646. // expected returned OFFER message:
  647. // - copy of client-id
  648. // - server-id
  649. // - offered address (!= hint)
  650. TEST_F(Dhcpv4SrvTest, DiscoverInvalidHint) {
  651. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  652. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  653. IOAddress hint("10.1.2.3");
  654. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  655. dis->setRemoteAddr(IOAddress("192.0.2.107"));
  656. OptionPtr clientid = generateClientId();
  657. dis->addOption(clientid);
  658. dis->setYiaddr(hint);
  659. // Pass it to the server and get an offer
  660. Pkt4Ptr offer = srv->processDiscover(dis);
  661. // Check if we get response at all
  662. checkResponse(offer, DHCPOFFER, 1234);
  663. // Check that address was returned from proper range, that its lease
  664. // lifetime is correct, that T1 and T2 are returned properly
  665. checkAddressParams(offer, subnet_);
  666. EXPECT_NE(offer->getYiaddr().toText(), hint.toText());
  667. // Check identifiers
  668. checkServerId(offer, srv->getServerID());
  669. checkClientId(offer, clientid);
  670. }
  671. /// @todo: Add a test that client sends hint that is in pool, but currently
  672. /// being used by a different client.
  673. // This test checks that the server is offering different addresses to different
  674. // clients in OFFERs. Please note that OFFER is not a guarantee that such
  675. // an address will be assigned. Had the pool was very small and contained only
  676. // 2 addresses, the third client would get the same offer as the first one
  677. // and this is a correct behavior. It is REQUEST that will fail for the third
  678. // client. OFFER is basically saying "if you send me a request, you will
  679. // probably get an address like this" (there are no guarantees).
  680. TEST_F(Dhcpv4SrvTest, ManyDiscovers) {
  681. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  682. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  683. Pkt4Ptr dis1 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  684. Pkt4Ptr dis2 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 2345));
  685. Pkt4Ptr dis3 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 3456));
  686. dis1->setRemoteAddr(IOAddress("192.0.2.1"));
  687. dis2->setRemoteAddr(IOAddress("192.0.2.2"));
  688. dis3->setRemoteAddr(IOAddress("192.0.2.3"));
  689. // Different client-id sizes
  690. OptionPtr clientid1 = generateClientId(4); // length 4
  691. OptionPtr clientid2 = generateClientId(5); // length 5
  692. OptionPtr clientid3 = generateClientId(6); // length 6
  693. dis1->addOption(clientid1);
  694. dis2->addOption(clientid2);
  695. dis3->addOption(clientid3);
  696. // Pass it to the server and get an offer
  697. Pkt4Ptr offer1 = srv->processDiscover(dis1);
  698. Pkt4Ptr offer2 = srv->processDiscover(dis2);
  699. Pkt4Ptr offer3 = srv->processDiscover(dis3);
  700. // Check if we get response at all
  701. checkResponse(offer1, DHCPOFFER, 1234);
  702. checkResponse(offer2, DHCPOFFER, 2345);
  703. checkResponse(offer3, DHCPOFFER, 3456);
  704. IOAddress addr1 = offer1->getYiaddr();
  705. IOAddress addr2 = offer2->getYiaddr();
  706. IOAddress addr3 = offer3->getYiaddr();
  707. // Check that the assigned address is indeed from the configured pool
  708. checkAddressParams(offer1, subnet_);
  709. checkAddressParams(offer2, subnet_);
  710. checkAddressParams(offer3, subnet_);
  711. // Check server-ids
  712. checkServerId(offer1, srv->getServerID());
  713. checkServerId(offer2, srv->getServerID());
  714. checkServerId(offer3, srv->getServerID());
  715. checkClientId(offer1, clientid1);
  716. checkClientId(offer2, clientid2);
  717. checkClientId(offer3, clientid3);
  718. // Finally check that the addresses offered are different
  719. EXPECT_NE(addr1.toText(), addr2.toText());
  720. EXPECT_NE(addr2.toText(), addr3.toText());
  721. EXPECT_NE(addr3.toText(), addr1.toText());
  722. cout << "Offered address to client1=" << addr1.toText() << endl;
  723. cout << "Offered address to client2=" << addr2.toText() << endl;
  724. cout << "Offered address to client3=" << addr3.toText() << endl;
  725. }
  726. // This test verifies that incoming REQUEST can be handled properly, that an
  727. // ACK is generated, that the response has an address and that address
  728. // really belongs to the configured pool.
  729. //
  730. // constructed a single REQUEST message with:
  731. // - client-id option
  732. // - hwaddr information
  733. // - requested address (that the client received in DISCOVER/OFFER exchange)
  734. //
  735. // expected returned ACK message:
  736. // - copy of client-id
  737. // - server-id
  738. // - assigned address
  739. //
  740. // Test verifies that the lease is actually in the database.
  741. TEST_F(Dhcpv4SrvTest, RequestBasic) {
  742. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  743. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  744. IOAddress hint("192.0.2.107");
  745. Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  746. req->setRemoteAddr(IOAddress("192.0.2.1"));
  747. OptionPtr clientid = generateClientId();
  748. req->addOption(clientid);
  749. req->setYiaddr(hint);
  750. // Pass it to the server and get an advertise
  751. Pkt4Ptr ack = srv->processRequest(req);
  752. // Check if we get response at all
  753. checkResponse(ack, DHCPACK, 1234);
  754. EXPECT_EQ(hint.toText(), ack->getYiaddr().toText());
  755. // Check that address was returned from proper range, that its lease
  756. // lifetime is correct, that T1 and T2 are returned properly
  757. checkAddressParams(ack, subnet_);
  758. // Check identifiers
  759. checkServerId(ack, srv->getServerID());
  760. checkClientId(ack, clientid);
  761. // Check that the lease is really in the database
  762. Lease4Ptr l = checkLease(ack, clientid, req->getHWAddr(), hint);
  763. ASSERT_TRUE(l);
  764. LeaseMgrFactory::instance().deleteLease(l->addr_);
  765. }
  766. // This test verifies that incoming REQUEST can be handled properly, that an
  767. // ACK is generated, that the response has an address and that address
  768. // really belongs to the configured pool.
  769. //
  770. // constructed 3 REQUEST messages with:
  771. // - client-id option (differs between messages)
  772. // - hwaddr information (differs between messages)
  773. //
  774. // expected returned ACK message:
  775. // - copy of client-id
  776. // - server-id
  777. // - assigned address (different for each client)
  778. TEST_F(Dhcpv4SrvTest, ManyRequests) {
  779. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  780. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  781. const IOAddress req_addr1("192.0.2.105");
  782. const IOAddress req_addr2("192.0.2.101");
  783. const IOAddress req_addr3("192.0.2.109");
  784. const IOAddress relay("192.0.2.1");
  785. Pkt4Ptr req1 = Pkt4Ptr(new Pkt4(DHCPOFFER, 1234));
  786. Pkt4Ptr req2 = Pkt4Ptr(new Pkt4(DHCPOFFER, 2345));
  787. Pkt4Ptr req3 = Pkt4Ptr(new Pkt4(DHCPOFFER, 3456));
  788. req1->setRemoteAddr(relay);
  789. req2->setRemoteAddr(relay);
  790. req3->setRemoteAddr(relay);
  791. req1->setYiaddr(req_addr1);
  792. req2->setYiaddr(req_addr2);
  793. req3->setYiaddr(req_addr3);
  794. req1->setHWAddr(generateHWAddr(6));
  795. req2->setHWAddr(generateHWAddr(7));
  796. req3->setHWAddr(generateHWAddr(8));
  797. // Different client-id sizes
  798. OptionPtr clientid1 = generateClientId(4); // length 4
  799. OptionPtr clientid2 = generateClientId(5); // length 5
  800. OptionPtr clientid3 = generateClientId(6); // length 6
  801. req1->addOption(clientid1);
  802. req2->addOption(clientid2);
  803. req3->addOption(clientid3);
  804. // Pass it to the server and get an advertise
  805. Pkt4Ptr ack1 = srv->processRequest(req1);
  806. Pkt4Ptr ack2 = srv->processRequest(req2);
  807. Pkt4Ptr ack3 = srv->processRequest(req3);
  808. // Check if we get response at all
  809. checkResponse(ack1, DHCPACK, 1234);
  810. checkResponse(ack2, DHCPACK, 2345);
  811. checkResponse(ack3, DHCPACK, 3456);
  812. IOAddress addr1 = ack1->getYiaddr();
  813. IOAddress addr2 = ack2->getYiaddr();
  814. IOAddress addr3 = ack3->getYiaddr();
  815. // Check that every client received the address it requested
  816. EXPECT_EQ(req_addr1.toText(), addr1.toText());
  817. EXPECT_EQ(req_addr2.toText(), addr2.toText());
  818. EXPECT_EQ(req_addr3.toText(), addr3.toText());
  819. // Check that the assigned address is indeed from the configured pool
  820. checkAddressParams(ack1, subnet_);
  821. checkAddressParams(ack2, subnet_);
  822. checkAddressParams(ack3, subnet_);
  823. // Check DUIDs
  824. checkServerId(ack1, srv->getServerID());
  825. checkServerId(ack2, srv->getServerID());
  826. checkServerId(ack3, srv->getServerID());
  827. checkClientId(ack1, clientid1);
  828. checkClientId(ack2, clientid2);
  829. checkClientId(ack3, clientid3);
  830. // Check that leases are in the database
  831. Lease4Ptr l = checkLease(ack1, clientid1, req1->getHWAddr(), addr1);
  832. EXPECT_TRUE(l);
  833. l = checkLease(ack2, clientid2, req2->getHWAddr(), addr2);
  834. l = checkLease(ack3, clientid3, req3->getHWAddr(), addr3);
  835. // Finally check that the addresses offered are different
  836. EXPECT_NE(addr1.toText(), addr2.toText());
  837. EXPECT_NE(addr2.toText(), addr3.toText());
  838. EXPECT_NE(addr3.toText(), addr1.toText());
  839. cout << "Offered address to client1=" << addr1.toText() << endl;
  840. cout << "Offered address to client2=" << addr2.toText() << endl;
  841. cout << "Offered address to client3=" << addr3.toText() << endl;
  842. }
  843. // This test verifies that incoming (positive) REQUEST/Renewing can be handled properly, that a
  844. // REPLY is generated, that the response has an address and that address
  845. // really belongs to the configured pool and that lease is actually renewed.
  846. //
  847. // expected:
  848. // - returned REPLY message has copy of client-id
  849. // - returned REPLY message has server-id
  850. // - returned REPLY message has IA that includes IAADDR
  851. // - lease is actually renewed in LeaseMgr
  852. TEST_F(Dhcpv4SrvTest, RenewBasic) {
  853. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  854. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  855. const IOAddress addr("192.0.2.106");
  856. const uint32_t temp_t1 = 50;
  857. const uint32_t temp_t2 = 75;
  858. const uint32_t temp_valid = 100;
  859. const time_t temp_timestamp = time(NULL) - 10;
  860. // Generate client-id also sets client_id_ member
  861. OptionPtr clientid = generateClientId();
  862. // Check that the address we are about to use is indeed in pool
  863. ASSERT_TRUE(subnet_->inPool(addr));
  864. // let's create a lease and put it in the LeaseMgr
  865. uint8_t hwaddr2[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  866. Lease4Ptr used(new Lease4(IOAddress("192.0.2.106"), hwaddr2, sizeof(hwaddr2),
  867. &client_id_->getDuid()[0], client_id_->getDuid().size(),
  868. temp_valid, temp_t1, temp_t2, temp_timestamp,
  869. subnet_->getID()));
  870. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  871. // Check that the lease is really in the database
  872. Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
  873. ASSERT_TRUE(l);
  874. // Check that T1, T2, preferred, valid and cltt really set.
  875. // Constructed lease looks as if it was assigned 10 seconds ago
  876. // EXPECT_EQ(l->t1_, temp_t1);
  877. // EXPECT_EQ(l->t2_, temp_t2);
  878. EXPECT_EQ(l->valid_lft_, temp_valid);
  879. EXPECT_EQ(l->cltt_, temp_timestamp);
  880. // Let's create a RENEW
  881. Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  882. req->setRemoteAddr(IOAddress(addr));
  883. req->setYiaddr(addr);
  884. req->setCiaddr(addr); // client's address
  885. req->addOption(clientid);
  886. req->addOption(srv->getServerID());
  887. // Pass it to the server and hope for a REPLY
  888. Pkt4Ptr ack = srv->processRequest(req);
  889. // Check if we get response at all
  890. checkResponse(ack, DHCPACK, 1234);
  891. EXPECT_EQ(addr.toText(), ack->getYiaddr().toText());
  892. // Check that address was returned from proper range, that its lease
  893. // lifetime is correct, that T1 and T2 are returned properly
  894. checkAddressParams(ack, subnet_);
  895. // Check identifiers
  896. checkServerId(ack, srv->getServerID());
  897. checkClientId(ack, clientid);
  898. // Check that the lease is really in the database
  899. l = checkLease(ack, clientid, req->getHWAddr(), addr);
  900. ASSERT_TRUE(l);
  901. // Check that T1, T2, preferred, valid and cltt were really updated
  902. EXPECT_EQ(l->t1_, subnet_->getT1());
  903. EXPECT_EQ(l->t2_, subnet_->getT2());
  904. EXPECT_EQ(l->valid_lft_, subnet_->getValid());
  905. // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
  906. int32_t cltt = static_cast<int32_t>(l->cltt_);
  907. int32_t expected = static_cast<int32_t>(time(NULL));
  908. // Equality or difference by 1 between cltt and expected is ok.
  909. EXPECT_GE(1, abs(cltt - expected));
  910. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  911. }
  912. // @todo: Implement tests for rejecting renewals
  913. // This test verifies if the sanityCheck() really checks options presence.
  914. TEST_F(Dhcpv4SrvTest, sanityCheck) {
  915. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  916. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  917. Pkt4Ptr pkt = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  918. // Client-id is optional for information-request, so
  919. EXPECT_NO_THROW(srv->sanityCheck(pkt, Dhcpv4Srv::OPTIONAL));
  920. // Empty packet, no server-id
  921. EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv4Srv::MANDATORY), RFCViolation);
  922. pkt->addOption(srv->getServerID());
  923. // Server-id is mandatory and present = no exception
  924. EXPECT_NO_THROW(srv->sanityCheck(pkt, Dhcpv4Srv::MANDATORY));
  925. // Server-id is forbidden, but present => exception
  926. EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv4Srv::FORBIDDEN),
  927. RFCViolation);
  928. }
  929. // This test verifies that incoming (positive) RELEASE can be handled properly.
  930. // As there is no REPLY in DHCPv4, the only thing to verify here is that
  931. // the lease is indeed removed from the database.
  932. TEST_F(Dhcpv4SrvTest, ReleaseBasic) {
  933. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  934. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  935. const IOAddress addr("192.0.2.106");
  936. const uint32_t temp_t1 = 50;
  937. const uint32_t temp_t2 = 75;
  938. const uint32_t temp_valid = 100;
  939. const time_t temp_timestamp = time(NULL) - 10;
  940. // Generate client-id also duid_
  941. OptionPtr clientid = generateClientId();
  942. // Check that the address we are about to use is indeed in pool
  943. ASSERT_TRUE(subnet_->inPool(addr));
  944. // Let's create a lease and put it in the LeaseMgr
  945. uint8_t mac_addr[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  946. HWAddrPtr hw(new HWAddr(mac_addr, sizeof(mac_addr), HTYPE_ETHER));
  947. Lease4Ptr used(new Lease4(addr, mac_addr, sizeof(mac_addr),
  948. &client_id_->getDuid()[0], client_id_->getDuid().size(),
  949. temp_valid, temp_t1, temp_t2, temp_timestamp,
  950. subnet_->getID()));
  951. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  952. // Check that the lease is really in the database
  953. Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
  954. ASSERT_TRUE(l);
  955. // Let's create a RELEASE
  956. // Generate client-id also duid_
  957. Pkt4Ptr rel = Pkt4Ptr(new Pkt4(DHCPRELEASE, 1234));
  958. rel->setRemoteAddr(addr);
  959. rel->setYiaddr(addr);
  960. rel->addOption(clientid);
  961. rel->addOption(srv->getServerID());
  962. rel->setHWAddr(hw);
  963. // Pass it to the server and hope for a REPLY
  964. // Note: this is no response to RELEASE in DHCPv4
  965. EXPECT_NO_THROW(srv->processRelease(rel));
  966. // The lease should be gone from LeaseMgr
  967. l = LeaseMgrFactory::instance().getLease4(addr);
  968. EXPECT_FALSE(l);
  969. // Try to get the lease by hardware address
  970. // @todo: Uncomment this once trac2592 is implemented
  971. // Lease4Collection leases = LeaseMgrFactory::instance().getLease4(hw->hwaddr_);
  972. // EXPECT_EQ(leases.size(), 0);
  973. // Try to get it by hw/subnet_id combination
  974. l = LeaseMgrFactory::instance().getLease4(hw->hwaddr_, subnet_->getID());
  975. EXPECT_FALSE(l);
  976. // Try by client-id
  977. // @todo: Uncomment this once trac2592 is implemented
  978. //Lease4Collection leases = LeaseMgrFactory::instance().getLease4(*client_id_);
  979. //EXPECT_EQ(leases.size(), 0);
  980. // Try by client-id/subnet-id
  981. l = LeaseMgrFactory::instance().getLease4(*client_id_, subnet_->getID());
  982. EXPECT_FALSE(l);
  983. // Ok, the lease is *really* not there.
  984. }
  985. // This test verifies that incoming (invalid) RELEASE can be handled properly.
  986. //
  987. // This test checks 3 scenarios:
  988. // 1. there is no such lease at all
  989. // 2. there is such a lease, but it is assigned to a different IAID
  990. // 3. there is such a lease, but it belongs to a different client
  991. TEST_F(Dhcpv4SrvTest, ReleaseReject) {
  992. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  993. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  994. const IOAddress addr("192.0.2.106");
  995. const uint32_t t1 = 50;
  996. const uint32_t t2 = 75;
  997. const uint32_t valid = 100;
  998. const time_t timestamp = time(NULL) - 10;
  999. // Let's create a lease and put it in the LeaseMgr
  1000. uint8_t bogus_mac_addr[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  1001. HWAddrPtr bogus_hw(new HWAddr(bogus_mac_addr, sizeof(bogus_mac_addr), HTYPE_ETHER));
  1002. OptionPtr bogus_clientid = generateClientId(7); // different length
  1003. // Generate client-id also duid_
  1004. OptionPtr clientid = generateClientId();
  1005. // Check that the address we are about to use is indeed in pool
  1006. ASSERT_TRUE(subnet_->inPool(addr));
  1007. // Let's create a RELEASE
  1008. // Generate client-id also duid_
  1009. Pkt4Ptr rel = Pkt4Ptr(new Pkt4(DHCPRELEASE, 1234));
  1010. rel->setRemoteAddr(addr);
  1011. rel->setYiaddr(addr);
  1012. rel->addOption(clientid);
  1013. rel->addOption(srv->getServerID());
  1014. rel->setHWAddr(bogus_hw);
  1015. // Case 1: No lease known to server
  1016. SCOPED_TRACE("CASE 1: Lease is not known to the server");
  1017. // There is nothing to check here. The lease is not there and server does
  1018. // not send anything back. This case is enumerated here just for keeping
  1019. // parity with similar test in DHCPv6.
  1020. EXPECT_NO_THROW(srv->processRelease(rel));
  1021. // CASE 2: Lease is known and belongs to this client, but to a different hardware
  1022. SCOPED_TRACE("CASE 2: Lease is known and belongs to this client, but uses different HW addr");
  1023. // Let's create a lease and put it in the LeaseMgr
  1024. uint8_t mac_addr[] = { 0, 0x1, 0x2, 0x3, 0x4, 0x5};
  1025. HWAddrPtr hw(new HWAddr(mac_addr, sizeof(mac_addr), HTYPE_ETHER));
  1026. Lease4Ptr used(new Lease4(addr, mac_addr, sizeof(mac_addr),
  1027. &client_id_->getDuid()[0], client_id_->getDuid().size(),
  1028. valid, t1, t2, timestamp, subnet_->getID()));
  1029. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  1030. // Check that the lease is really in the database
  1031. Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
  1032. ASSERT_TRUE(l);
  1033. rel->setHWAddr(bogus_hw);
  1034. EXPECT_NO_THROW(srv->processRelease(rel));
  1035. // Check that the lease was not removed (due to hardware address mis-match)
  1036. l = LeaseMgrFactory::instance().getLease4(addr);
  1037. ASSERT_TRUE(l);
  1038. // CASE 3: Lease belongs to a client with different client-id
  1039. SCOPED_TRACE("CASE 3: Lease belongs to a client with different client-id");
  1040. rel->setHWAddr(hw); // proper HW address this time
  1041. rel->delOption(DHO_DHCP_CLIENT_IDENTIFIER);
  1042. rel->addOption(bogus_clientid); // but invalid client-id
  1043. OptionPtr x = rel->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
  1044. EXPECT_NO_THROW(srv->processRelease(rel));
  1045. // Check that the lease is still there
  1046. l = LeaseMgrFactory::instance().getLease4(addr);
  1047. ASSERT_TRUE(l);
  1048. // Final sanity check. Verify that with valid hw and client-id release is successful
  1049. rel->delOption(DHO_DHCP_CLIENT_IDENTIFIER);
  1050. rel->addOption(clientid);
  1051. // It should work this time
  1052. EXPECT_NO_THROW(srv->processRelease(rel));
  1053. // Check that the lease is not there
  1054. l = LeaseMgrFactory::instance().getLease4(addr);
  1055. EXPECT_FALSE(l);
  1056. }
  1057. // This test verifies if the server-id disk operations (read, write) are
  1058. // working properly.
  1059. TEST_F(Dhcpv4SrvTest, ServerID) {
  1060. NakedDhcpv4Srv srv(0);
  1061. string srvid_text = "192.0.2.100";
  1062. IOAddress srvid(srvid_text);
  1063. fstream file1(SRVID_FILE, ios::out | ios::trunc);
  1064. file1 << srvid_text;
  1065. file1.close();
  1066. // Test reading from a file
  1067. EXPECT_TRUE(srv.loadServerID(SRVID_FILE));
  1068. ASSERT_TRUE(srv.getServerID());
  1069. EXPECT_EQ(srvid_text, srv.srvidToString(srv.getServerID()));
  1070. // Now test writing to a file
  1071. EXPECT_EQ(0, unlink(SRVID_FILE));
  1072. EXPECT_NO_THROW(srv.writeServerID(SRVID_FILE));
  1073. fstream file2(SRVID_FILE, ios::in);
  1074. ASSERT_TRUE(file2.good());
  1075. string text;
  1076. file2 >> text;
  1077. file2.close();
  1078. EXPECT_EQ(srvid_text, text);
  1079. }
  1080. } // end of anonymous namespace