dhcp4_srv_unittest.cc 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536
  1. // Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <sstream>
  8. #include <asiolink/io_address.h>
  9. #include <cc/command_interpreter.h>
  10. #include <config/command_mgr.h>
  11. #include <dhcp4/tests/dhcp4_test_utils.h>
  12. #include <dhcp4/tests/dhcp4_client.h>
  13. #include <dhcp/tests/pkt_captures.h>
  14. #include <dhcp/dhcp4.h>
  15. #include <dhcp/iface_mgr.h>
  16. #include <dhcp/libdhcp++.h>
  17. #include <dhcp/option.h>
  18. #include <dhcp/option_int.h>
  19. #include <dhcp/option4_addrlst.h>
  20. #include <dhcp/option_custom.h>
  21. #include <dhcp/option_int_array.h>
  22. #include <dhcp/option_vendor.h>
  23. #include <dhcp/pkt_filter.h>
  24. #include <dhcp/pkt_filter_inet.h>
  25. #include <dhcp/docsis3_option_defs.h>
  26. #include <dhcp/tests/iface_mgr_test_config.h>
  27. #include <dhcp4/dhcp4_srv.h>
  28. #include <dhcp4/dhcp4_log.h>
  29. #include <dhcp4/json_config_parser.h>
  30. #include <dhcpsrv/cfgmgr.h>
  31. #include <dhcpsrv/lease_mgr.h>
  32. #include <dhcpsrv/lease_mgr_factory.h>
  33. #include <dhcpsrv/utils.h>
  34. #include <dhcpsrv/host_mgr.h>
  35. #include <gtest/gtest.h>
  36. #include <stats/stats_mgr.h>
  37. #include <boost/scoped_ptr.hpp>
  38. #include <iostream>
  39. #include <arpa/inet.h>
  40. using namespace std;
  41. using namespace isc;
  42. using namespace isc::dhcp;
  43. using namespace isc::data;
  44. using namespace isc::asiolink;
  45. using namespace isc::config;
  46. using namespace isc::dhcp::test;
  47. namespace {
  48. const char* CONFIGS[] = {
  49. // Configuration 0:
  50. // - 1 subnet: 10.254.226.0/25
  51. // - used for recorded traffic (see PktCaptures::captureRelayedDiscover)
  52. "{ \"interfaces-config\": {"
  53. " \"interfaces\": [ \"*\" ]"
  54. "},"
  55. "\"rebind-timer\": 2000, "
  56. "\"renew-timer\": 1000, "
  57. "\"subnet4\": [ { "
  58. " \"pools\": [ { \"pool\": \"10.254.226.0/25\" } ],"
  59. " \"subnet\": \"10.254.226.0/24\", "
  60. " \"rebind-timer\": 2000, "
  61. " \"renew-timer\": 1000, "
  62. " \"valid-lifetime\": 4000,"
  63. " \"interface\": \"eth0\" "
  64. " } ],"
  65. "\"valid-lifetime\": 4000 }",
  66. // Configuration 1:
  67. // - 1 subnet: 192.0.2.0/24
  68. // - MySQL Host Data Source configured
  69. "{ \"interfaces-config\": {"
  70. " \"interfaces\": [ \"*\" ]"
  71. "},"
  72. "\"hosts-database\": {"
  73. " \"type\": \"mysql\","
  74. " \"name\": \"keatest\","
  75. " \"user\": \"keatest\","
  76. " \"password\": \"keatest\""
  77. "},"
  78. "\"rebind-timer\": 2000, "
  79. "\"renew-timer\": 1000, "
  80. "\"subnet4\": [ { "
  81. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  82. " \"subnet\": \"192.0.2.0/24\", "
  83. " \"rebind-timer\": 2000, "
  84. " \"renew-timer\": 1000, "
  85. " \"valid-lifetime\": 4000,"
  86. " \"interface\": \"eth0\" "
  87. " } ],"
  88. "\"valid-lifetime\": 4000 }"
  89. };
  90. // This test verifies that the destination address of the response
  91. // message is set to giaddr, when giaddr is set to non-zero address
  92. // in the received message.
  93. TEST_F(Dhcpv4SrvTest, adjustIfaceDataRelay) {
  94. IfaceMgrTestConfig test_config(true);
  95. IfaceMgr::instance().openSockets4();
  96. // Create the instance of the incoming packet.
  97. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
  98. // Set the giaddr to non-zero address and hops to non-zero value
  99. // as if it was relayed.
  100. req->setGiaddr(IOAddress("192.0.1.1"));
  101. req->setHops(2);
  102. // Set ciaddr to zero. This simulates the client which applies
  103. // for the new lease.
  104. req->setCiaddr(IOAddress("0.0.0.0"));
  105. // Clear broadcast flag.
  106. req->setFlags(0x0000);
  107. // Set local address, port and interface.
  108. req->setLocalAddr(IOAddress("192.0.2.5"));
  109. req->setLocalPort(1001);
  110. req->setIface("eth1");
  111. req->setIndex(1);
  112. // Create the exchange using the req.
  113. Dhcpv4Exchange ex = createExchange(req);
  114. Pkt4Ptr resp = ex.getResponse();
  115. resp->setYiaddr(IOAddress("192.0.1.100"));
  116. // Clear the remote address.
  117. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  118. // Set hops value for the response.
  119. resp->setHops(req->getHops());
  120. // This function never throws.
  121. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(ex));
  122. // Now the destination address should be relay's address.
  123. EXPECT_EQ("192.0.1.1", resp->getRemoteAddr().toText());
  124. // The query has been relayed, so the response must be sent to the port 67.
  125. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getRemotePort());
  126. // Local address should be the address assigned to interface eth1.
  127. EXPECT_EQ("192.0.2.5", resp->getLocalAddr().toText());
  128. // The local port is always DHCPv4 server port 67.
  129. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
  130. // We will send response over the same interface which was used to receive
  131. // query.
  132. EXPECT_EQ("eth1", resp->getIface());
  133. EXPECT_EQ(1, resp->getIndex());
  134. // Let's do another test and set other fields: ciaddr and
  135. // flags. By doing it, we want to make sure that the relay
  136. // address will take precedence.
  137. req->setGiaddr(IOAddress("192.0.1.50"));
  138. req->setCiaddr(IOAddress("192.0.1.11"));
  139. req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
  140. resp->setYiaddr(IOAddress("192.0.1.100"));
  141. // Clear remote address.
  142. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  143. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(ex));
  144. // Response should be sent back to the relay address.
  145. EXPECT_EQ("192.0.1.50", resp->getRemoteAddr().toText());
  146. }
  147. // This test verifies that the destination address of the response message
  148. // is set to ciaddr when giaddr is set to zero and the ciaddr is set to
  149. // non-zero address in the received message. This is the case when the
  150. // client is in Renew or Rebind state.
  151. TEST_F(Dhcpv4SrvTest, adjustIfaceDataRenew) {
  152. IfaceMgrTestConfig test_config(true);
  153. IfaceMgr::instance().openSockets4();
  154. // Create instance of the incoming packet.
  155. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
  156. // Clear giaddr to simulate direct packet.
  157. req->setGiaddr(IOAddress("0.0.0.0"));
  158. // Set ciaddr to non-zero address. The response should be sent to this
  159. // address as the client is in renewing or rebinding state (it is fully
  160. // configured).
  161. req->setCiaddr(IOAddress("192.0.1.15"));
  162. // Let's configure broadcast flag. It should be ignored because
  163. // we are responding directly to the client having an address
  164. // and trying to extend his lease. Broadcast flag is only used
  165. // when new lease is acquired and server must make a decision
  166. // whether to unicast the response to the acquired address or
  167. // broadcast it.
  168. req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
  169. // This is a direct message, so the hops should be cleared.
  170. req->setHops(0);
  171. // Set local unicast address as if we are renewing a lease.
  172. req->setLocalAddr(IOAddress("192.0.2.1"));
  173. // Request is received on the DHCPv4 server port.
  174. req->setLocalPort(DHCP4_SERVER_PORT);
  175. // Set the interface. The response should be sent over the same interface.
  176. req->setIface("eth1");
  177. req->setIndex(1);
  178. // Create the exchange using the req.
  179. Dhcpv4Exchange ex = createExchange(req);
  180. Pkt4Ptr resp = ex.getResponse();
  181. // Let's extend the lease for the client in such a way that
  182. // it will actually get different address. The response
  183. // should not be sent to this address but rather to ciaddr
  184. // as client still have ciaddr configured.
  185. resp->setYiaddr(IOAddress("192.0.1.13"));
  186. // Clear the remote address.
  187. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  188. // Copy hops value from the query.
  189. resp->setHops(req->getHops());
  190. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(ex));
  191. // Check that server responds to ciaddr
  192. EXPECT_EQ("192.0.1.15", resp->getRemoteAddr().toText());
  193. // The query was non-relayed, so the response should be sent to a DHCPv4
  194. // client port 68.
  195. EXPECT_EQ(DHCP4_CLIENT_PORT, resp->getRemotePort());
  196. // The response should be sent from the unicast address on which the
  197. // query has been received.
  198. EXPECT_EQ("192.0.2.1", resp->getLocalAddr().toText());
  199. // The response should be sent from the DHCPv4 server port.
  200. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
  201. // The interface data should match the data in the query.
  202. EXPECT_EQ("eth1", resp->getIface());
  203. EXPECT_EQ(1, resp->getIndex());
  204. }
  205. // This test verifies that the destination address of the response message
  206. // is set correctly when giaddr and ciaddr is zeroed in the received message
  207. // and the new lease is acquired. The lease address is carried in the
  208. // response message in the yiaddr field. In this case destination address
  209. // of the response should be set to yiaddr if server supports direct responses
  210. // to the client which doesn't have an address yet or broadcast if the server
  211. // doesn't support direct responses.
  212. TEST_F(Dhcpv4SrvTest, adjustIfaceDataSelect) {
  213. IfaceMgrTestConfig test_config(true);
  214. IfaceMgr::instance().openSockets4();
  215. // Create instance of the incoming packet.
  216. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
  217. // Clear giaddr to simulate direct packet.
  218. req->setGiaddr(IOAddress("0.0.0.0"));
  219. // Clear client address as it hasn't got any address configured yet.
  220. req->setCiaddr(IOAddress("0.0.0.0"));
  221. // Let's clear the broadcast flag.
  222. req->setFlags(0);
  223. // This is a non-relayed message, so let's clear hops count.
  224. req->setHops(0);
  225. // The query is sent to the broadcast address in the Select state.
  226. req->setLocalAddr(IOAddress("255.255.255.255"));
  227. // The query has been received on the DHCPv4 server port 67.
  228. req->setLocalPort(DHCP4_SERVER_PORT);
  229. // Set the interface. The response should be sent via the same interface.
  230. req->setIface("eth1");
  231. req->setIndex(1);
  232. // Create the exchange using the req.
  233. Dhcpv4Exchange ex = createExchange(req);
  234. Pkt4Ptr resp = ex.getResponse();
  235. // Assign some new address for this client.
  236. resp->setYiaddr(IOAddress("192.0.1.13"));
  237. // Clear the remote address.
  238. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  239. // Copy hops count.
  240. resp->setHops(req->getHops());
  241. // We want to test the case, when the server (packet filter) doesn't support
  242. // ddirect responses to the client which doesn't have an address yet. In
  243. // case, the server should send its response to the broadcast address.
  244. // We can control whether the current packet filter returns that its support
  245. // direct responses or not.
  246. test_config.setDirectResponse(false);
  247. // When running unit tests, the IfaceMgr is using the default Packet
  248. // Filtering class, PktFilterInet. This class does not support direct
  249. // responses to clients without address assigned. When giaddr and ciaddr
  250. // are zero and client has just got new lease, the assigned address is
  251. // carried in yiaddr. In order to send this address to the client,
  252. // server must broadcast its response.
  253. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(ex));
  254. // Check that the response is sent to broadcast address as the
  255. // server doesn't have capability to respond directly.
  256. EXPECT_EQ("255.255.255.255", resp->getRemoteAddr().toText());
  257. // Although the query has been sent to the broadcast address, the
  258. // server should select a unicast address on the particular interface
  259. // as a source address for the response.
  260. EXPECT_EQ("192.0.2.3", resp->getLocalAddr().toText());
  261. // The response should be sent from the DHCPv4 server port.
  262. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
  263. // The response should be sent via the same interface through which
  264. // query has been received.
  265. EXPECT_EQ("eth1", resp->getIface());
  266. EXPECT_EQ(1, resp->getIndex());
  267. // We also want to test the case when the server has capability to
  268. // respond directly to the client which is not configured. Server
  269. // makes decision whether it responds directly or broadcast its
  270. // response based on the capability reported by IfaceMgr. We can
  271. // control whether the current packet filter returns that it supports
  272. // direct responses or not.
  273. test_config.setDirectResponse(true);
  274. // Now we expect that the server will send its response to the
  275. // address assigned for the client.
  276. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(ex));
  277. EXPECT_EQ("192.0.1.13", resp->getRemoteAddr().toText());
  278. }
  279. // This test verifies that the destination address of the response message
  280. // is set to broadcast address when client set broadcast flag in its
  281. // query. Client sets this flag to indicate that it can't receive direct
  282. // responses from the server when it doesn't have its interface configured.
  283. // Server must respect broadcast flag.
  284. TEST_F(Dhcpv4SrvTest, adjustIfaceDataBroadcast) {
  285. IfaceMgrTestConfig test_config(true);
  286. IfaceMgr::instance().openSockets4();
  287. // Create instance of the incoming packet.
  288. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
  289. // Clear giaddr to simulate direct packet.
  290. req->setGiaddr(IOAddress("0.0.0.0"));
  291. // Clear client address as it hasn't got any address configured yet.
  292. req->setCiaddr(IOAddress("0.0.0.0"));
  293. // The query is sent to the broadcast address in the Select state.
  294. req->setLocalAddr(IOAddress("255.255.255.255"));
  295. // The query has been received on the DHCPv4 server port 67.
  296. req->setLocalPort(DHCP4_SERVER_PORT);
  297. // Set the interface. The response should be sent via the same interface.
  298. req->setIface("eth1");
  299. req->setIndex(1);
  300. // Let's set the broadcast flag.
  301. req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
  302. // Create the exchange using the req.
  303. Dhcpv4Exchange ex = createExchange(req);
  304. Pkt4Ptr resp = ex.getResponse();
  305. // Assign some new address for this client.
  306. resp->setYiaddr(IOAddress("192.0.1.13"));
  307. // Clear the remote address.
  308. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  309. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(ex));
  310. // Server must respond to broadcast address when client desired that
  311. // by setting the broadcast flag in its request.
  312. EXPECT_EQ("255.255.255.255", resp->getRemoteAddr().toText());
  313. // Although the query has been sent to the broadcast address, the
  314. // server should select a unicast address on the particular interface
  315. // as a source address for the response.
  316. EXPECT_EQ("192.0.2.3", resp->getLocalAddr().toText());
  317. // The response should be sent from the DHCPv4 server port.
  318. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
  319. // The response should be sent via the same interface through which
  320. // query has been received.
  321. EXPECT_EQ("eth1", resp->getIface());
  322. EXPECT_EQ(1, resp->getIndex());
  323. }
  324. // This test verifies that the mandatory to copy fields and options
  325. // are really copied into the response.
  326. TEST_F(Dhcpv4SrvTest, initResponse) {
  327. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  328. // Set fields which must be copied
  329. query->setIface("foo");
  330. query->setIndex(111);
  331. query->setHops(5);
  332. const HWAddr& hw = HWAddr::fromText("11:22:33:44:55:66:77:88", 10);
  333. HWAddrPtr hw_addr(new HWAddr(hw));
  334. query->setHWAddr(hw_addr);
  335. query->setGiaddr(IOAddress("10.10.10.10"));
  336. const HWAddr& src_hw = HWAddr::fromText("e4:ce:8f:12:34:56");
  337. HWAddrPtr src_hw_addr(new HWAddr(src_hw));
  338. query->setLocalHWAddr(src_hw_addr);
  339. const HWAddr& dst_hw = HWAddr::fromText("e8:ab:cd:78:9a:bc");
  340. HWAddrPtr dst_hw_addr(new HWAddr(dst_hw));
  341. query->setRemoteHWAddr(dst_hw_addr);
  342. query->setFlags(BOOTP_BROADCAST);
  343. // Add options which must be copied
  344. // client-id echo is optional
  345. // rai echo is done in relayAgentInfoEcho
  346. // Do subnet selection option
  347. OptionDefinitionPtr sbnsel_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  348. DHO_SUBNET_SELECTION);
  349. ASSERT_TRUE(sbnsel_def);
  350. OptionCustomPtr sbnsel(new OptionCustom(*sbnsel_def, Option::V4));
  351. ASSERT_TRUE(sbnsel);
  352. sbnsel->writeAddress(IOAddress("192.0.2.3"));
  353. query->addOption(sbnsel);
  354. // Create exchange and get Response
  355. Dhcpv4Exchange ex = createExchange(query);
  356. Pkt4Ptr response = ex.getResponse();
  357. ASSERT_TRUE(response);
  358. // Check fields
  359. EXPECT_EQ("foo", response->getIface());
  360. EXPECT_EQ(111, response->getIndex());
  361. EXPECT_TRUE(response->getSiaddr().isV4Zero());
  362. EXPECT_TRUE(response->getCiaddr().isV4Zero());
  363. EXPECT_EQ(5, response->getHops());
  364. EXPECT_TRUE(hw == *response->getHWAddr());
  365. EXPECT_EQ(IOAddress("10.10.10.10"), response->getGiaddr());
  366. EXPECT_TRUE(src_hw == *response->getLocalHWAddr());
  367. EXPECT_TRUE(dst_hw == *response->getRemoteHWAddr());
  368. EXPECT_TRUE(BOOTP_BROADCAST == response->getFlags());
  369. // Check options (i.e., subnet selection option)
  370. OptionPtr resp_sbnsel = response->getOption(DHO_SUBNET_SELECTION);
  371. ASSERT_TRUE(resp_sbnsel);
  372. OptionCustomPtr resp_custom =
  373. boost::dynamic_pointer_cast<OptionCustom>(resp_sbnsel);
  374. ASSERT_TRUE(resp_custom);
  375. IOAddress subnet_addr("0.0.0.0");
  376. ASSERT_NO_THROW(subnet_addr = resp_custom->readAddress());
  377. EXPECT_EQ(IOAddress("192.0.2.3"), subnet_addr);
  378. }
  379. // This test verifies that the server identifier option is appended to
  380. // a specified DHCPv4 message and the server identifier is correct.
  381. TEST_F(Dhcpv4SrvTest, appendServerID) {
  382. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  383. Dhcpv4Exchange ex = createExchange(query);
  384. Pkt4Ptr response = ex.getResponse();
  385. // Set a local address. It is required by the function under test
  386. // to create the Server Identifier option.
  387. response->setLocalAddr(IOAddress("192.0.3.1"));
  388. // Append the Server Identifier.
  389. ASSERT_NO_THROW(NakedDhcpv4Srv::appendServerID(ex));
  390. // Make sure that the option has been added.
  391. OptionPtr opt = response->getOption(DHO_DHCP_SERVER_IDENTIFIER);
  392. ASSERT_TRUE(opt);
  393. Option4AddrLstPtr opt_server_id =
  394. boost::dynamic_pointer_cast<Option4AddrLst>(opt);
  395. ASSERT_TRUE(opt_server_id);
  396. // The option is represented as a list of IPv4 addresses but with
  397. // only one address added.
  398. Option4AddrLst::AddressContainer addrs = opt_server_id->getAddresses();
  399. ASSERT_EQ(1, addrs.size());
  400. // This address should match the local address of the packet.
  401. EXPECT_EQ("192.0.3.1", addrs[0].toText());
  402. }
  403. // Sanity check. Verifies that both Dhcpv4Srv and its derived
  404. // class NakedDhcpv4Srv can be instantiated and destroyed.
  405. TEST_F(Dhcpv4SrvTest, basic) {
  406. // Check that the base class can be instantiated
  407. boost::scoped_ptr<Dhcpv4Srv> srv;
  408. ASSERT_NO_THROW(srv.reset(new Dhcpv4Srv(DHCP4_SERVER_PORT + 10000, false,
  409. false)));
  410. srv.reset();
  411. // We have to close open sockets because further in this test we will
  412. // call the Dhcpv4Srv constructor again. This constructor will try to
  413. // set the appropriate packet filter class for IfaceMgr. This requires
  414. // that all sockets are closed.
  415. IfaceMgr::instance().closeSockets();
  416. // Check that the derived class can be instantiated
  417. boost::scoped_ptr<NakedDhcpv4Srv> naked_srv;
  418. ASSERT_NO_THROW(
  419. naked_srv.reset(new NakedDhcpv4Srv(DHCP4_SERVER_PORT + 10000)));
  420. // Close sockets again for the next test.
  421. IfaceMgr::instance().closeSockets();
  422. ASSERT_NO_THROW(naked_srv.reset(new NakedDhcpv4Srv(0)));
  423. }
  424. // Verifies that DISCOVER message can be processed correctly,
  425. // that the OFFER message generated in response is valid and
  426. // contains necessary options.
  427. //
  428. // Note: this test focuses on the packet correctness. There
  429. // are other tests that verify correctness of the allocation
  430. // engine. See DiscoverBasic, DiscoverHint, DiscoverNoClientId
  431. // and DiscoverInvalidHint.
  432. TEST_F(Dhcpv4SrvTest, processDiscover) {
  433. testDiscoverRequest(DHCPDISCOVER);
  434. }
  435. // Verifies that REQUEST message can be processed correctly,
  436. // that the OFFER message generated in response is valid and
  437. // contains necessary options.
  438. //
  439. // Note: this test focuses on the packet correctness. There
  440. // are other tests that verify correctness of the allocation
  441. // engine. See DiscoverBasic, DiscoverHint, DiscoverNoClientId
  442. // and DiscoverInvalidHint.
  443. TEST_F(Dhcpv4SrvTest, processRequest) {
  444. testDiscoverRequest(DHCPREQUEST);
  445. }
  446. TEST_F(Dhcpv4SrvTest, processRelease) {
  447. NakedDhcpv4Srv srv;
  448. Pkt4Ptr pkt(new Pkt4(DHCPRELEASE, 1234));
  449. // Should not throw
  450. EXPECT_NO_THROW(srv.processRelease(pkt));
  451. }
  452. // This test verifies that incoming DISCOVER can be handled properly, that an
  453. // OFFER is generated, that the response has an address and that address
  454. // really belongs to the configured pool.
  455. //
  456. // constructed very simple DISCOVER message with:
  457. // - client-id option
  458. //
  459. // expected returned OFFER message:
  460. // - copy of client-id
  461. // - server-id
  462. // - offered address
  463. TEST_F(Dhcpv4SrvTest, DiscoverBasic) {
  464. IfaceMgrTestConfig test_config(true);
  465. IfaceMgr::instance().openSockets4();
  466. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  467. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  468. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  469. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  470. OptionPtr clientid = generateClientId();
  471. dis->addOption(clientid);
  472. dis->setIface("eth1");
  473. // Pass it to the server and get an offer
  474. Pkt4Ptr offer = srv->processDiscover(dis);
  475. // Check if we get response at all
  476. checkResponse(offer, DHCPOFFER, 1234);
  477. // Check that address was returned from proper range, that its lease
  478. // lifetime is correct, that T1 and T2 are returned properly
  479. checkAddressParams(offer, subnet_, true, true);
  480. // Check identifiers
  481. checkServerId(offer, srv->getServerID());
  482. checkClientId(offer, clientid);
  483. }
  484. // Check that option 58 and 59 are not included if they are not specified.
  485. TEST_F(Dhcpv4SrvTest, DiscoverNoTimers) {
  486. IfaceMgrTestConfig test_config(true);
  487. IfaceMgr::instance().openSockets4();
  488. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  489. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  490. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  491. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  492. OptionPtr clientid = generateClientId();
  493. dis->addOption(clientid);
  494. dis->setIface("eth1");
  495. // Recreate a subnet but set T1 and T2 to "unspecified".
  496. subnet_.reset(new Subnet4(IOAddress("192.0.2.0"), 24,
  497. Triplet<uint32_t>(),
  498. Triplet<uint32_t>(),
  499. 3000));
  500. pool_ = Pool4Ptr(new Pool4(IOAddress("192.0.2.100"),
  501. IOAddress("192.0.2.110")));
  502. subnet_->addPool(pool_);
  503. CfgMgr::instance().clear();
  504. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnet_);
  505. CfgMgr::instance().commit();
  506. // Pass it to the server and get an offer
  507. Pkt4Ptr offer = srv->processDiscover(dis);
  508. // Check if we get response at all
  509. checkResponse(offer, DHCPOFFER, 1234);
  510. // T1 and T2 timers must not be present.
  511. checkAddressParams(offer, subnet_, false, false);
  512. // Check identifiers
  513. checkServerId(offer, srv->getServerID());
  514. checkClientId(offer, clientid);
  515. }
  516. // This test verifies that incoming DISCOVER can be handled properly, that an
  517. // OFFER is generated, that the response has an address and that address
  518. // really belongs to the configured pool.
  519. //
  520. // constructed very simple DISCOVER message with:
  521. // - client-id option
  522. // - address set to specific value as hint, but that hint is invalid
  523. //
  524. // expected returned OFFER message:
  525. // - copy of client-id
  526. // - server-id
  527. // - offered address (!= hint)
  528. TEST_F(Dhcpv4SrvTest, DiscoverInvalidHint) {
  529. IfaceMgrTestConfig test_config(true);
  530. IfaceMgr::instance().openSockets4();
  531. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  532. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  533. IOAddress hint("10.1.2.3");
  534. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  535. dis->setRemoteAddr(IOAddress("192.0.2.107"));
  536. OptionPtr clientid = generateClientId();
  537. dis->addOption(clientid);
  538. dis->setYiaddr(hint);
  539. dis->setIface("eth1");
  540. // Pass it to the server and get an offer
  541. Pkt4Ptr offer = srv->processDiscover(dis);
  542. // Check if we get response at all
  543. checkResponse(offer, DHCPOFFER, 1234);
  544. // Check that address was returned from proper range, that its lease
  545. // lifetime is correct, that T1 and T2 are returned properly
  546. checkAddressParams(offer, subnet_, true, true);
  547. EXPECT_NE(offer->getYiaddr(), hint);
  548. // Check identifiers
  549. checkServerId(offer, srv->getServerID());
  550. checkClientId(offer, clientid);
  551. }
  552. /// @todo: Add a test that client sends hint that is in pool, but currently
  553. /// being used by a different client.
  554. // This test checks that the server is offering different addresses to different
  555. // clients in OFFERs. Please note that OFFER is not a guarantee that such
  556. // an address will be assigned. Had the pool was very small and contained only
  557. // 2 addresses, the third client would get the same offer as the first one
  558. // and this is a correct behavior. It is REQUEST that will fail for the third
  559. // client. OFFER is basically saying "if you send me a request, you will
  560. // probably get an address like this" (there are no guarantees).
  561. TEST_F(Dhcpv4SrvTest, ManyDiscovers) {
  562. IfaceMgrTestConfig test_config(true);
  563. IfaceMgr::instance().openSockets4();
  564. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  565. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  566. Pkt4Ptr dis1 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  567. Pkt4Ptr dis2 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 2345));
  568. Pkt4Ptr dis3 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 3456));
  569. dis1->setRemoteAddr(IOAddress("192.0.2.1"));
  570. dis2->setRemoteAddr(IOAddress("192.0.2.2"));
  571. dis3->setRemoteAddr(IOAddress("192.0.2.3"));
  572. // Assign interfaces
  573. dis1->setIface("eth1");
  574. dis2->setIface("eth1");
  575. dis3->setIface("eth1");
  576. // Different client-id sizes
  577. OptionPtr clientid1 = generateClientId(4); // length 4
  578. OptionPtr clientid2 = generateClientId(5); // length 5
  579. OptionPtr clientid3 = generateClientId(6); // length 6
  580. dis1->addOption(clientid1);
  581. dis2->addOption(clientid2);
  582. dis3->addOption(clientid3);
  583. // Pass it to the server and get an offer
  584. Pkt4Ptr offer1 = srv->processDiscover(dis1);
  585. Pkt4Ptr offer2 = srv->processDiscover(dis2);
  586. Pkt4Ptr offer3 = srv->processDiscover(dis3);
  587. // Check if we get response at all
  588. checkResponse(offer1, DHCPOFFER, 1234);
  589. checkResponse(offer2, DHCPOFFER, 2345);
  590. checkResponse(offer3, DHCPOFFER, 3456);
  591. IOAddress addr1 = offer1->getYiaddr();
  592. IOAddress addr2 = offer2->getYiaddr();
  593. IOAddress addr3 = offer3->getYiaddr();
  594. // Check that the assigned address is indeed from the configured pool
  595. checkAddressParams(offer1, subnet_, true, true);
  596. checkAddressParams(offer2, subnet_, true, true);
  597. checkAddressParams(offer3, subnet_, true, true);
  598. // Check server-ids
  599. checkServerId(offer1, srv->getServerID());
  600. checkServerId(offer2, srv->getServerID());
  601. checkServerId(offer3, srv->getServerID());
  602. checkClientId(offer1, clientid1);
  603. checkClientId(offer2, clientid2);
  604. checkClientId(offer3, clientid3);
  605. // Finally check that the addresses offered are different
  606. EXPECT_NE(addr1, addr2);
  607. EXPECT_NE(addr2, addr3);
  608. EXPECT_NE(addr3, addr1);
  609. cout << "Offered address to client1=" << addr1 << endl;
  610. cout << "Offered address to client2=" << addr2 << endl;
  611. cout << "Offered address to client3=" << addr3 << endl;
  612. }
  613. // Checks whether echoing back client-id is controllable, i.e.
  614. // whether the server obeys echo-client-id and sends (or not)
  615. // client-id
  616. TEST_F(Dhcpv4SrvTest, discoverEchoClientId) {
  617. IfaceMgrTestConfig test_config(true);
  618. IfaceMgr::instance().openSockets4();
  619. NakedDhcpv4Srv srv(0);
  620. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  621. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  622. OptionPtr clientid = generateClientId();
  623. dis->addOption(clientid);
  624. dis->setIface("eth1");
  625. // Pass it to the server and get an offer
  626. Pkt4Ptr offer = srv.processDiscover(dis);
  627. // Check if we get response at all
  628. checkResponse(offer, DHCPOFFER, 1234);
  629. checkClientId(offer, clientid);
  630. CfgMgr::instance().echoClientId(false);
  631. offer = srv.processDiscover(dis);
  632. // Check if we get response at all
  633. checkResponse(offer, DHCPOFFER, 1234);
  634. checkClientId(offer, clientid);
  635. }
  636. // Check that option 58 and 59 are not included if they are not specified.
  637. TEST_F(Dhcpv4SrvTest, RequestNoTimers) {
  638. IfaceMgrTestConfig test_config(true);
  639. IfaceMgr::instance().openSockets4();
  640. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  641. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  642. Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  643. req->setRemoteAddr(IOAddress("192.0.2.1"));
  644. OptionPtr clientid = generateClientId();
  645. req->addOption(clientid);
  646. req->setIface("eth1");
  647. // Recreate a subnet but set T1 and T2 to "unspecified".
  648. subnet_.reset(new Subnet4(IOAddress("192.0.2.0"), 24,
  649. Triplet<uint32_t>(),
  650. Triplet<uint32_t>(),
  651. 3000));
  652. pool_ = Pool4Ptr(new Pool4(IOAddress("192.0.2.100"),
  653. IOAddress("192.0.2.110")));
  654. subnet_->addPool(pool_);
  655. CfgMgr::instance().clear();
  656. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnet_);
  657. CfgMgr::instance().commit();
  658. // Pass it to the server and get an ACK.
  659. Pkt4Ptr ack = srv->processRequest(req);
  660. // Check if we get response at all
  661. checkResponse(ack, DHCPACK, 1234);
  662. // T1 and T2 timers must not be present.
  663. checkAddressParams(ack, subnet_, false, false);
  664. // Check identifiers
  665. checkServerId(ack, srv->getServerID());
  666. checkClientId(ack, clientid);
  667. }
  668. // Checks whether echoing back client-id is controllable
  669. TEST_F(Dhcpv4SrvTest, requestEchoClientId) {
  670. IfaceMgrTestConfig test_config(true);
  671. IfaceMgr::instance().openSockets4();
  672. NakedDhcpv4Srv srv(0);
  673. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  674. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  675. OptionPtr clientid = generateClientId();
  676. dis->addOption(clientid);
  677. dis->setIface("eth1");
  678. // Pass it to the server and get ACK
  679. Pkt4Ptr ack = srv.processRequest(dis);
  680. // Check if we get response at all
  681. checkResponse(ack, DHCPACK, 1234);
  682. checkClientId(ack, clientid);
  683. CfgMgr::instance().echoClientId(false);
  684. ack = srv.processRequest(dis);
  685. // Check if we get response at all
  686. checkResponse(ack, DHCPACK, 1234);
  687. checkClientId(ack, clientid);
  688. }
  689. // This test verifies that incoming (positive) REQUEST/Renewing can be handled properly, that a
  690. // REPLY is generated, that the response has an address and that address
  691. // really belongs to the configured pool and that lease is actually renewed.
  692. //
  693. // expected:
  694. // - returned REPLY message has copy of client-id
  695. // - returned REPLY message has server-id
  696. // - returned REPLY message has IA that includes IAADDR
  697. // - lease is actually renewed in LeaseMgr
  698. TEST_F(Dhcpv4SrvTest, RenewBasic) {
  699. IfaceMgrTestConfig test_config(true);
  700. IfaceMgr::instance().openSockets4();
  701. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  702. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  703. const IOAddress addr("192.0.2.106");
  704. const uint32_t temp_t1 = 50;
  705. const uint32_t temp_t2 = 75;
  706. const uint32_t temp_valid = 100;
  707. const time_t temp_timestamp = time(NULL) - 10;
  708. // Generate client-id also sets client_id_ member
  709. OptionPtr clientid = generateClientId();
  710. // Check that the address we are about to use is indeed in pool
  711. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
  712. // let's create a lease and put it in the LeaseMgr
  713. uint8_t hwaddr2_data[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  714. HWAddrPtr hwaddr2(new HWAddr(hwaddr2_data, sizeof(hwaddr2_data), HTYPE_ETHER));
  715. Lease4Ptr used(new Lease4(IOAddress("192.0.2.106"), hwaddr2,
  716. &client_id_->getDuid()[0], client_id_->getDuid().size(),
  717. temp_valid, temp_t1, temp_t2, temp_timestamp,
  718. subnet_->getID()));
  719. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  720. // Check that the lease is really in the database
  721. Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
  722. ASSERT_TRUE(l);
  723. // Check that T1, T2, preferred, valid and cltt really set.
  724. // Constructed lease looks as if it was assigned 10 seconds ago
  725. // EXPECT_EQ(l->t1_, temp_t1);
  726. // EXPECT_EQ(l->t2_, temp_t2);
  727. EXPECT_EQ(l->valid_lft_, temp_valid);
  728. EXPECT_EQ(l->cltt_, temp_timestamp);
  729. // Let's create a RENEW
  730. Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  731. req->setRemoteAddr(IOAddress(addr));
  732. req->setYiaddr(addr);
  733. req->setCiaddr(addr); // client's address
  734. req->setIface("eth0");
  735. req->setHWAddr(hwaddr2);
  736. req->addOption(clientid);
  737. req->addOption(srv->getServerID());
  738. // Pass it to the server and hope for a REPLY
  739. Pkt4Ptr ack = srv->processRequest(req);
  740. // Check if we get response at all
  741. checkResponse(ack, DHCPACK, 1234);
  742. EXPECT_EQ(addr, ack->getYiaddr());
  743. // Check that address was returned from proper range, that its lease
  744. // lifetime is correct, that T1 and T2 are returned properly
  745. checkAddressParams(ack, subnet_, true, true);
  746. // Check identifiers
  747. checkServerId(ack, srv->getServerID());
  748. checkClientId(ack, clientid);
  749. // Check that the lease is really in the database
  750. l = checkLease(ack, clientid, req->getHWAddr(), addr);
  751. ASSERT_TRUE(l);
  752. // Check that T1, T2, preferred, valid and cltt were really updated
  753. EXPECT_EQ(l->t1_, subnet_->getT1());
  754. EXPECT_EQ(l->t2_, subnet_->getT2());
  755. EXPECT_EQ(l->valid_lft_, subnet_->getValid());
  756. // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
  757. int32_t cltt = static_cast<int32_t>(l->cltt_);
  758. int32_t expected = static_cast<int32_t>(time(NULL));
  759. // Equality or difference by 1 between cltt and expected is ok.
  760. EXPECT_GE(1, abs(cltt - expected));
  761. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  762. }
  763. // This test verifies that the logic which matches server identifier in the
  764. // received message with server identifiers used by a server works correctly:
  765. // - a message with no server identifier is accepted,
  766. // - a message with a server identifier which matches one of the server
  767. // identifiers used by a server is accepted,
  768. // - a message with a server identifier which doesn't match any server
  769. // identifier used by a server, is not accepted.
  770. TEST_F(Dhcpv4SrvTest, acceptServerId) {
  771. IfaceMgrTestConfig test_config(true);
  772. IfaceMgr::instance().openSockets4();
  773. NakedDhcpv4Srv srv(0);
  774. Pkt4Ptr pkt(new Pkt4(DHCPREQUEST, 1234));
  775. // If no server identifier option is present, the message is always
  776. // accepted.
  777. EXPECT_TRUE(srv.acceptServerId(pkt));
  778. // Create definition of the server identifier option.
  779. OptionDefinition def("server-identifier", DHO_DHCP_SERVER_IDENTIFIER,
  780. "ipv4-address", false);
  781. // Add a server identifier option which doesn't match server ids being
  782. // used by the server. The accepted server ids are the IPv4 addresses
  783. // configured on the interfaces. The 10.1.2.3 is not configured on
  784. // any interfaces.
  785. OptionCustomPtr other_serverid(new OptionCustom(def, Option::V6));
  786. other_serverid->writeAddress(IOAddress("10.1.2.3"));
  787. pkt->addOption(other_serverid);
  788. EXPECT_FALSE(srv.acceptServerId(pkt));
  789. // Remove the server identifier.
  790. ASSERT_NO_THROW(pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER));
  791. // Add a server id being an IPv4 address configured on eth0 interface.
  792. // A DHCPv4 message holding this server identifier should be accepted.
  793. OptionCustomPtr eth0_serverid(new OptionCustom(def, Option::V6));
  794. eth0_serverid->writeAddress(IOAddress("192.0.2.3"));
  795. ASSERT_NO_THROW(pkt->addOption(eth0_serverid));
  796. EXPECT_TRUE(srv.acceptServerId(pkt));
  797. // Remove the server identifier.
  798. ASSERT_NO_THROW(pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER));
  799. // Add a server id being an IPv4 address configured on eth1 interface.
  800. // A DHCPv4 message holding this server identifier should be accepted.
  801. OptionCustomPtr eth1_serverid(new OptionCustom(def, Option::V6));
  802. eth1_serverid->writeAddress(IOAddress("10.0.0.1"));
  803. ASSERT_NO_THROW(pkt->addOption(eth1_serverid));
  804. EXPECT_TRUE(srv.acceptServerId(pkt));
  805. }
  806. // @todo: Implement tests for rejecting renewals
  807. // This test verifies if the sanityCheck() really checks options presence.
  808. TEST_F(Dhcpv4SrvTest, sanityCheck) {
  809. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  810. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  811. Pkt4Ptr pkt = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  812. pkt->setHWAddr(generateHWAddr(6));
  813. // Server-id is optional for information-request, so
  814. EXPECT_NO_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::OPTIONAL));
  815. // Empty packet, no server-id
  816. EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY),
  817. RFCViolation);
  818. pkt->addOption(srv->getServerID());
  819. // Server-id is mandatory and present = no exception
  820. EXPECT_NO_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY));
  821. // Server-id is forbidden, but present => exception
  822. EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::FORBIDDEN),
  823. RFCViolation);
  824. // There's no client-id and no HWADDR. Server needs something to
  825. // identify the client
  826. pkt->setHWAddr(generateHWAddr(0));
  827. EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY),
  828. RFCViolation);
  829. }
  830. // Checks if received relay agent info option is echoed back to the client
  831. TEST_F(Dhcpv4SrvTest, relayAgentInfoEcho) {
  832. IfaceMgrTestConfig test_config(true);
  833. IfaceMgr::instance().openSockets4();
  834. NakedDhcpv4Srv srv(0);
  835. // Use of the captured DHCPDISCOVER packet requires that
  836. // subnet 10.254.226.0/24 is in use, because this packet
  837. // contains the giaddr which belongs to this subnet and
  838. // this giaddr is used to select the subnet
  839. configure(CONFIGS[0]);
  840. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  841. // added option 82 (relay agent info) with 3 suboptions. The server
  842. // is supposed to echo it back in its response.
  843. Pkt4Ptr dis;
  844. ASSERT_NO_THROW(dis = PktCaptures::captureRelayedDiscover());
  845. // Simulate that we have received that traffic
  846. srv.fakeReceive(dis);
  847. // Server will now process to run its normal loop, but instead of calling
  848. // IfaceMgr::receive4(), it will read all packets from the list set by
  849. // fakeReceive()
  850. // In particular, it should call registered buffer4_receive callback.
  851. srv.run();
  852. // Check that the server did send a response
  853. ASSERT_EQ(1, srv.fake_sent_.size());
  854. // Make sure that we received a response
  855. Pkt4Ptr offer = srv.fake_sent_.front();
  856. ASSERT_TRUE(offer);
  857. // Get Relay Agent Info from query...
  858. OptionPtr rai_query = dis->getOption(DHO_DHCP_AGENT_OPTIONS);
  859. ASSERT_TRUE(rai_query);
  860. // Get Relay Agent Info from response...
  861. OptionPtr rai_response = offer->getOption(DHO_DHCP_AGENT_OPTIONS);
  862. ASSERT_TRUE(rai_response);
  863. EXPECT_TRUE(rai_response->equals(rai_query));
  864. }
  865. /// @todo move vendor options tests to a separate file.
  866. /// @todo Add more extensive vendor options tests, including multiple
  867. /// vendor options
  868. // Checks if vendor options are parsed correctly and requested vendor options
  869. // are echoed back.
  870. TEST_F(Dhcpv4SrvTest, vendorOptionsDocsis) {
  871. IfaceMgrTestConfig test_config(true);
  872. IfaceMgr::instance().openSockets4();
  873. NakedDhcpv4Srv srv(0);
  874. string config = "{ \"interfaces-config\": {"
  875. " \"interfaces\": [ \"*\" ]"
  876. "},"
  877. "\"rebind-timer\": 2000, "
  878. "\"renew-timer\": 1000, "
  879. " \"option-data\": [ {"
  880. " \"name\": \"tftp-servers\","
  881. " \"space\": \"vendor-4491\","
  882. " \"code\": 2,"
  883. " \"data\": \"10.253.175.16\","
  884. " \"csv-format\": True"
  885. " }],"
  886. "\"subnet4\": [ { "
  887. " \"pools\": [ { \"pool\": \"10.254.226.0/25\" } ],"
  888. " \"subnet\": \"10.254.226.0/24\", "
  889. " \"rebind-timer\": 2000, "
  890. " \"renew-timer\": 1000, "
  891. " \"valid-lifetime\": 4000,"
  892. " \"interface\": \"eth0\" "
  893. " } ],"
  894. "\"valid-lifetime\": 4000 }";
  895. ElementPtr json = Element::fromJSON(config);
  896. ConstElementPtr status;
  897. // Configure the server and make sure the config is accepted
  898. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  899. ASSERT_TRUE(status);
  900. comment_ = config::parseAnswer(rcode_, status);
  901. ASSERT_EQ(0, rcode_);
  902. CfgMgr::instance().commit();
  903. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  904. // added option 82 (relay agent info) with 3 suboptions. The server
  905. // is supposed to echo it back in its response.
  906. Pkt4Ptr dis;
  907. ASSERT_NO_THROW(dis = PktCaptures::captureRelayedDiscover());
  908. // Simulate that we have received that traffic
  909. srv.fakeReceive(dis);
  910. // Server will now process to run its normal loop, but instead of calling
  911. // IfaceMgr::receive4(), it will read all packets from the list set by
  912. // fakeReceive()
  913. // In particular, it should call registered buffer4_receive callback.
  914. srv.run();
  915. // Check that the server did send a response
  916. ASSERT_EQ(1, srv.fake_sent_.size());
  917. // Make sure that we received a response
  918. Pkt4Ptr offer = srv.fake_sent_.front();
  919. ASSERT_TRUE(offer);
  920. // Get Relay Agent Info from query...
  921. OptionPtr vendor_opt_response = offer->getOption(DHO_VIVSO_SUBOPTIONS);
  922. ASSERT_TRUE(vendor_opt_response);
  923. // Check if it's of a correct type
  924. boost::shared_ptr<OptionVendor> vendor_opt =
  925. boost::dynamic_pointer_cast<OptionVendor>(vendor_opt_response);
  926. ASSERT_TRUE(vendor_opt);
  927. // Get Relay Agent Info from response...
  928. OptionPtr tftp_servers_generic = vendor_opt->getOption(DOCSIS3_V4_TFTP_SERVERS);
  929. ASSERT_TRUE(tftp_servers_generic);
  930. Option4AddrLstPtr tftp_servers =
  931. boost::dynamic_pointer_cast<Option4AddrLst>(tftp_servers_generic);
  932. ASSERT_TRUE(tftp_servers);
  933. Option4AddrLst::AddressContainer addrs = tftp_servers->getAddresses();
  934. ASSERT_EQ(1, addrs.size());
  935. EXPECT_EQ("10.253.175.16", addrs[0].toText());
  936. }
  937. /// @todo Implement tests for subnetSelect See tests in dhcp6_srv_unittest.cc:
  938. /// selectSubnetAddr, selectSubnetIface, selectSubnetRelayLinkaddr,
  939. /// selectSubnetRelayInterfaceId. Note that the concept of interface-id is not
  940. /// present in the DHCPv4, so not everything is applicable directly.
  941. /// See ticket #3057
  942. // Checks whether the server uses default (0.0.0.0) siaddr value, unless
  943. // explicitly specified
  944. TEST_F(Dhcpv4SrvTest, siaddrDefault) {
  945. IfaceMgrTestConfig test_config(true);
  946. IfaceMgr::instance().openSockets4();
  947. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  948. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  949. IOAddress hint("192.0.2.107");
  950. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  951. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  952. OptionPtr clientid = generateClientId();
  953. dis->addOption(clientid);
  954. dis->setYiaddr(hint);
  955. dis->setIface("eth1");
  956. // Pass it to the server and get an offer
  957. Pkt4Ptr offer = srv->processDiscover(dis);
  958. ASSERT_TRUE(offer);
  959. // Check if we get response at all
  960. checkResponse(offer, DHCPOFFER, 1234);
  961. // Verify that it is 0.0.0.0
  962. EXPECT_EQ("0.0.0.0", offer->getSiaddr().toText());
  963. }
  964. // Checks whether the server uses specified siaddr value
  965. TEST_F(Dhcpv4SrvTest, siaddr) {
  966. IfaceMgrTestConfig test_config(true);
  967. IfaceMgr::instance().openSockets4();
  968. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  969. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  970. subnet_->setSiaddr(IOAddress("192.0.2.123"));
  971. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  972. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  973. dis->setIface("eth1");
  974. OptionPtr clientid = generateClientId();
  975. dis->addOption(clientid);
  976. // Pass it to the server and get an offer
  977. Pkt4Ptr offer = srv->processDiscover(dis);
  978. ASSERT_TRUE(offer);
  979. // Check if we get response at all
  980. checkResponse(offer, DHCPOFFER, 1234);
  981. // Verify that its value is proper
  982. EXPECT_EQ("192.0.2.123", offer->getSiaddr().toText());
  983. }
  984. // Checks if the next-server defined as global value is overridden by subnet
  985. // specific value and returned in server messages. There's also similar test for
  986. // checking parser only configuration, see Dhcp4ParserTest.nextServerOverride in
  987. // config_parser_unittest.cc.
  988. TEST_F(Dhcpv4SrvTest, nextServerOverride) {
  989. IfaceMgrTestConfig test_config(true);
  990. IfaceMgr::instance().openSockets4();
  991. NakedDhcpv4Srv srv(0);
  992. ConstElementPtr status;
  993. string config = "{ \"interfaces-config\": {"
  994. " \"interfaces\": [ \"*\" ]"
  995. "},"
  996. "\"rebind-timer\": 2000, "
  997. "\"renew-timer\": 1000, "
  998. "\"next-server\": \"192.0.0.1\", "
  999. "\"subnet4\": [ { "
  1000. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1001. " \"next-server\": \"1.2.3.4\", "
  1002. " \"subnet\": \"192.0.2.0/24\" } ],"
  1003. "\"valid-lifetime\": 4000 }";
  1004. ElementPtr json = Element::fromJSON(config);
  1005. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1006. CfgMgr::instance().commit();
  1007. // check if returned status is OK
  1008. ASSERT_TRUE(status);
  1009. comment_ = config::parseAnswer(rcode_, status);
  1010. ASSERT_EQ(0, rcode_);
  1011. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1012. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1013. dis->setIface("eth1");
  1014. OptionPtr clientid = generateClientId();
  1015. dis->addOption(clientid);
  1016. // Pass it to the server and get an offer
  1017. Pkt4Ptr offer = srv.processDiscover(dis);
  1018. ASSERT_TRUE(offer);
  1019. EXPECT_EQ(DHCPOFFER, offer->getType());
  1020. EXPECT_EQ("1.2.3.4", offer->getSiaddr().toText());
  1021. }
  1022. // Checks if the next-server defined as global value is used in responses
  1023. // when there is no specific value defined in subnet and returned to the client
  1024. // properly. There's also similar test for checking parser only configuration,
  1025. // see Dhcp4ParserTest.nextServerGlobal in config_parser_unittest.cc.
  1026. TEST_F(Dhcpv4SrvTest, nextServerGlobal) {
  1027. IfaceMgrTestConfig test_config(true);
  1028. IfaceMgr::instance().openSockets4();
  1029. NakedDhcpv4Srv srv(0);
  1030. ConstElementPtr status;
  1031. string config = "{ \"interfaces-config\": {"
  1032. " \"interfaces\": [ \"*\" ]"
  1033. "},"
  1034. "\"rebind-timer\": 2000, "
  1035. "\"renew-timer\": 1000, "
  1036. "\"next-server\": \"192.0.0.1\", "
  1037. "\"subnet4\": [ { "
  1038. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1039. " \"subnet\": \"192.0.2.0/24\" } ],"
  1040. "\"valid-lifetime\": 4000 }";
  1041. ElementPtr json = Element::fromJSON(config);
  1042. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1043. CfgMgr::instance().commit();
  1044. // check if returned status is OK
  1045. ASSERT_TRUE(status);
  1046. comment_ = config::parseAnswer(rcode_, status);
  1047. ASSERT_EQ(0, rcode_);
  1048. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1049. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1050. dis->setIface("eth1");
  1051. OptionPtr clientid = generateClientId();
  1052. dis->addOption(clientid);
  1053. // Pass it to the server and get an offer
  1054. Pkt4Ptr offer = srv.processDiscover(dis);
  1055. ASSERT_TRUE(offer);
  1056. EXPECT_EQ(DHCPOFFER, offer->getType());
  1057. EXPECT_EQ("192.0.0.1", offer->getSiaddr().toText());
  1058. }
  1059. // Checks if server is able to handle a relayed traffic from DOCSIS3.0 modems
  1060. TEST_F(Dhcpv4SrvTest, docsisVendorOptionsParse) {
  1061. // Let's get a traffic capture from DOCSIS3.0 modem
  1062. Pkt4Ptr dis = PktCaptures::captureRelayedDiscover();
  1063. ASSERT_NO_THROW(dis->unpack());
  1064. // Check if the packet contain
  1065. OptionPtr opt = dis->getOption(DHO_VIVSO_SUBOPTIONS);
  1066. ASSERT_TRUE(opt);
  1067. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  1068. ASSERT_TRUE(vendor);
  1069. // This particular capture that we have included options 1 and 5
  1070. EXPECT_TRUE(vendor->getOption(1));
  1071. EXPECT_TRUE(vendor->getOption(5));
  1072. // It did not include options any other options
  1073. EXPECT_FALSE(vendor->getOption(2));
  1074. EXPECT_FALSE(vendor->getOption(3));
  1075. EXPECT_FALSE(vendor->getOption(17));
  1076. }
  1077. // Checks if server is able to parse incoming docsis option and extract suboption 1 (docsis ORO)
  1078. TEST_F(Dhcpv4SrvTest, docsisVendorORO) {
  1079. // Let's get a traffic capture from DOCSIS3.0 modem
  1080. Pkt4Ptr dis = PktCaptures::captureRelayedDiscover();
  1081. EXPECT_NO_THROW(dis->unpack());
  1082. // Check if the packet contains vendor specific information option
  1083. OptionPtr opt = dis->getOption(DHO_VIVSO_SUBOPTIONS);
  1084. ASSERT_TRUE(opt);
  1085. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  1086. ASSERT_TRUE(vendor);
  1087. opt = vendor->getOption(DOCSIS3_V4_ORO);
  1088. ASSERT_TRUE(opt);
  1089. OptionUint8ArrayPtr oro = boost::dynamic_pointer_cast<OptionUint8Array>(opt);
  1090. EXPECT_TRUE(oro);
  1091. }
  1092. // This test checks if Option Request Option (ORO) in docsis (vendor-id=4491)
  1093. // vendor options is parsed correctly and the requested options are actually assigned.
  1094. TEST_F(Dhcpv4SrvTest, vendorOptionsORO) {
  1095. IfaceMgrTestConfig test_config(true);
  1096. IfaceMgr::instance().openSockets4();
  1097. NakedDhcpv4Srv srv(0);
  1098. ConstElementPtr x;
  1099. string config = "{ \"interfaces-config\": {"
  1100. " \"interfaces\": [ \"*\" ]"
  1101. "},"
  1102. "\"rebind-timer\": 2000, "
  1103. "\"renew-timer\": 1000, "
  1104. " \"option-data\": [ {"
  1105. " \"name\": \"tftp-servers\","
  1106. " \"space\": \"vendor-4491\","
  1107. " \"code\": 2,"
  1108. " \"data\": \"192.0.2.1, 192.0.2.2\","
  1109. " \"csv-format\": True"
  1110. " }],"
  1111. "\"subnet4\": [ { "
  1112. " \"pools\": [ { \"pool\": \"192.0.2.0/25\" } ],"
  1113. " \"subnet\": \"192.0.2.0/24\", "
  1114. " \"rebind-timer\": 2000, "
  1115. " \"renew-timer\": 1000, "
  1116. " \"valid-lifetime\": 4000,"
  1117. " \"interface\": \"eth0\" "
  1118. " } ],"
  1119. "\"valid-lifetime\": 4000 }";
  1120. ElementPtr json = Element::fromJSON(config);
  1121. EXPECT_NO_THROW(x = configureDhcp4Server(srv, json));
  1122. ASSERT_TRUE(x);
  1123. comment_ = isc::config::parseAnswer(rcode_, x);
  1124. ASSERT_EQ(0, rcode_);
  1125. CfgMgr::instance().commit();
  1126. boost::shared_ptr<Pkt4> dis(new Pkt4(DHCPDISCOVER, 1234));
  1127. // Set the giaddr and hops to non-zero address as if it was relayed.
  1128. dis->setGiaddr(IOAddress("192.0.2.1"));
  1129. dis->setHops(1);
  1130. OptionPtr clientid = generateClientId();
  1131. dis->addOption(clientid);
  1132. // Set interface. It is required by the server to generate server id.
  1133. dis->setIface("eth0");
  1134. // Pass it to the server and get an advertise
  1135. Pkt4Ptr offer = srv.processDiscover(dis);
  1136. // check if we get response at all
  1137. ASSERT_TRUE(offer);
  1138. // We did not include any vendor opts in DISCOVER, so there should be none
  1139. // in OFFER.
  1140. ASSERT_FALSE(offer->getOption(DHO_VIVSO_SUBOPTIONS));
  1141. // Let's add a vendor-option (vendor-id=4491) with a single sub-option.
  1142. // That suboption has code 1 and is a docsis ORO option.
  1143. boost::shared_ptr<OptionUint8Array> vendor_oro(new OptionUint8Array(Option::V4,
  1144. DOCSIS3_V4_ORO));
  1145. vendor_oro->addValue(DOCSIS3_V4_TFTP_SERVERS); // Request option 33
  1146. OptionPtr vendor(new OptionVendor(Option::V4, 4491));
  1147. vendor->addOption(vendor_oro);
  1148. dis->addOption(vendor);
  1149. // Need to process SOLICIT again after requesting new option.
  1150. offer = srv.processDiscover(dis);
  1151. ASSERT_TRUE(offer);
  1152. // Check if there is a vendor option response
  1153. OptionPtr tmp = offer->getOption(DHO_VIVSO_SUBOPTIONS);
  1154. ASSERT_TRUE(tmp);
  1155. // The response should be OptionVendor object
  1156. boost::shared_ptr<OptionVendor> vendor_resp =
  1157. boost::dynamic_pointer_cast<OptionVendor>(tmp);
  1158. ASSERT_TRUE(vendor_resp);
  1159. OptionPtr docsis2 = vendor_resp->getOption(DOCSIS3_V4_TFTP_SERVERS);
  1160. ASSERT_TRUE(docsis2);
  1161. Option4AddrLstPtr tftp_srvs = boost::dynamic_pointer_cast<Option4AddrLst>(docsis2);
  1162. ASSERT_TRUE(tftp_srvs);
  1163. Option4AddrLst::AddressContainer addrs = tftp_srvs->getAddresses();
  1164. ASSERT_EQ(2, addrs.size());
  1165. EXPECT_EQ("192.0.2.1", addrs[0].toText());
  1166. EXPECT_EQ("192.0.2.2", addrs[1].toText());
  1167. }
  1168. // Test checks whether it is possible to use option definitions defined in
  1169. // src/lib/dhcp/docsis3_option_defs.h.
  1170. TEST_F(Dhcpv4SrvTest, vendorOptionsDocsisDefinitions) {
  1171. ConstElementPtr x;
  1172. string config_prefix = "{ \"interfaces-config\": {"
  1173. " \"interfaces\": [ ]"
  1174. "},"
  1175. "\"rebind-timer\": 2000, "
  1176. "\"renew-timer\": 1000, "
  1177. " \"option-data\": [ {"
  1178. " \"name\": \"tftp-servers\","
  1179. " \"space\": \"vendor-4491\","
  1180. " \"code\": ";
  1181. string config_postfix = ","
  1182. " \"data\": \"192.0.2.1\","
  1183. " \"csv-format\": True"
  1184. " }],"
  1185. "\"subnet4\": [ { "
  1186. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.50\" } ],"
  1187. " \"subnet\": \"192.0.2.0/24\", "
  1188. " \"renew-timer\": 1000, "
  1189. " \"rebind-timer\": 1000, "
  1190. " \"valid-lifetime\": 4000,"
  1191. " \"interface\": \"\""
  1192. " } ],"
  1193. "\"valid-lifetime\": 4000 }";
  1194. // There is docsis3 (vendor-id=4491) vendor option 2, which is a
  1195. // tftp-server. Its format is list of IPv4 addresses.
  1196. string config_valid = config_prefix + "2" + config_postfix;
  1197. // There is no option 99 defined in vendor-id=4491. As there is no
  1198. // definition, the config should fail.
  1199. string config_bogus = config_prefix + "99" + config_postfix;
  1200. ElementPtr json_bogus = Element::fromJSON(config_bogus);
  1201. ElementPtr json_valid = Element::fromJSON(config_valid);
  1202. NakedDhcpv4Srv srv(0);
  1203. // This should fail (missing option definition)
  1204. EXPECT_NO_THROW(x = configureDhcp4Server(srv, json_bogus));
  1205. ASSERT_TRUE(x);
  1206. comment_ = isc::config::parseAnswer(rcode_, x);
  1207. ASSERT_EQ(1, rcode_);
  1208. // This should work (option definition present)
  1209. EXPECT_NO_THROW(x = configureDhcp4Server(srv, json_valid));
  1210. ASSERT_TRUE(x);
  1211. comment_ = isc::config::parseAnswer(rcode_, x);
  1212. ASSERT_EQ(0, rcode_);
  1213. }
  1214. /// Checks if DOCSIS client packets are classified properly
  1215. ///
  1216. /// @todo: With the change in #4626 the vendorClassSpecificProcessing
  1217. /// code was removed and replaced with generic classification. One day
  1218. /// we should rewrite this test to use classes. It would check that the
  1219. /// classification system can be used for docsis packets.
  1220. TEST_F(Dhcpv4SrvTest, DISABLED_docsisClientClassification) {
  1221. NakedDhcpv4Srv srv(0);
  1222. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  1223. // vendor-class set to docsis3.0
  1224. Pkt4Ptr dis1;
  1225. ASSERT_NO_THROW(dis1 = PktCaptures::captureRelayedDiscover());
  1226. ASSERT_NO_THROW(dis1->unpack());
  1227. srv.classifyPacket(dis1);
  1228. EXPECT_TRUE(dis1->inClass(srv.VENDOR_CLASS_PREFIX + "docsis3.0"));
  1229. EXPECT_FALSE(dis1->inClass(srv.VENDOR_CLASS_PREFIX + "eRouter1.0"));
  1230. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  1231. // vendor-class set to eRouter1.0
  1232. Pkt4Ptr dis2;
  1233. ASSERT_NO_THROW(dis2 = PktCaptures::captureRelayedDiscover2());
  1234. ASSERT_NO_THROW(dis2->unpack());
  1235. srv.classifyPacket(dis2);
  1236. EXPECT_TRUE(dis2->inClass(srv.VENDOR_CLASS_PREFIX + "eRouter1.0"));
  1237. EXPECT_FALSE(dis2->inClass(srv.VENDOR_CLASS_PREFIX + "docsis3.0"));
  1238. }
  1239. // Checks if client packets are classified properly using match expressions.
  1240. TEST_F(Dhcpv4SrvTest, matchClassification) {
  1241. IfaceMgrTestConfig test_config(true);
  1242. IfaceMgr::instance().openSockets4();
  1243. NakedDhcpv4Srv srv(0);
  1244. // The router class matches incoming packets with foo in a host-name
  1245. // option (code 12) and sets an ip-forwarding option in the response.
  1246. string config = "{ \"interfaces-config\": {"
  1247. " \"interfaces\": [ \"*\" ] }, "
  1248. "\"rebind-timer\": 2000, "
  1249. "\"renew-timer\": 1000, "
  1250. "\"valid-lifetime\": 4000, "
  1251. "\"subnet4\": [ "
  1252. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1253. " \"subnet\": \"192.0.2.0/24\" } ], "
  1254. "\"client-classes\": [ "
  1255. "{ \"name\": \"router\", "
  1256. " \"option-data\": ["
  1257. " { \"name\": \"ip-forwarding\", "
  1258. " \"data\": \"true\" } ], "
  1259. " \"test\": \"option[12].text == 'foo'\" } ] }";
  1260. ElementPtr json = Element::fromJSON(config);
  1261. ConstElementPtr status;
  1262. // Configure the server and make sure the config is accepted
  1263. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1264. ASSERT_TRUE(status);
  1265. comment_ = config::parseAnswer(rcode_, status);
  1266. ASSERT_EQ(0, rcode_);
  1267. CfgMgr::instance().commit();
  1268. // Create packets with enough to select the subnet
  1269. OptionPtr clientid = generateClientId();
  1270. Pkt4Ptr query1(new Pkt4(DHCPDISCOVER, 1234));
  1271. query1->setRemoteAddr(IOAddress("192.0.2.1"));
  1272. query1->addOption(clientid);
  1273. query1->setIface("eth1");
  1274. Pkt4Ptr query2(new Pkt4(DHCPDISCOVER, 1234));
  1275. query2->setRemoteAddr(IOAddress("192.0.2.1"));
  1276. query2->addOption(clientid);
  1277. query2->setIface("eth1");
  1278. Pkt4Ptr query3(new Pkt4(DHCPDISCOVER, 1234));
  1279. query3->setRemoteAddr(IOAddress("192.0.2.1"));
  1280. query3->addOption(clientid);
  1281. query3->setIface("eth1");
  1282. // Create and add a PRL option to the first 2 queries
  1283. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1284. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1285. ASSERT_TRUE(prl);
  1286. prl->addValue(DHO_IP_FORWARDING);
  1287. query1->addOption(prl);
  1288. query2->addOption(prl);
  1289. // Create and add a host-name option to the first and last queries
  1290. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1291. ASSERT_TRUE(hostname);
  1292. query1->addOption(hostname);
  1293. query3->addOption(hostname);
  1294. // Classify packets
  1295. srv.classifyPacket(query1);
  1296. srv.classifyPacket(query2);
  1297. srv.classifyPacket(query3);
  1298. // Packets with the exception of the second should be in the router class
  1299. EXPECT_TRUE(query1->inClass("router"));
  1300. EXPECT_FALSE(query2->inClass("router"));
  1301. EXPECT_TRUE(query3->inClass("router"));
  1302. // Process queries
  1303. Pkt4Ptr response1 = srv.processDiscover(query1);
  1304. Pkt4Ptr response2 = srv.processDiscover(query2);
  1305. Pkt4Ptr response3 = srv.processDiscover(query3);
  1306. // Classification processing should add an ip-forwarding option
  1307. OptionPtr opt1 = response1->getOption(DHO_IP_FORWARDING);
  1308. EXPECT_TRUE(opt1);
  1309. // But only for the first query: second was not classified
  1310. OptionPtr opt2 = response2->getOption(DHO_IP_FORWARDING);
  1311. EXPECT_FALSE(opt2);
  1312. // But only for the first query: third has no PRL
  1313. OptionPtr opt3 = response3->getOption(DHO_IP_FORWARDING);
  1314. EXPECT_FALSE(opt3);
  1315. }
  1316. // Checks if client packets are classified properly using match expressions
  1317. // using option names
  1318. TEST_F(Dhcpv4SrvTest, matchClassificationOptionName) {
  1319. NakedDhcpv4Srv srv(0);
  1320. // The router class matches incoming packets with foo in a host-name
  1321. string config = "{ \"interfaces-config\": {"
  1322. " \"interfaces\": [ \"*\" ] }, "
  1323. "\"rebind-timer\": 2000, "
  1324. "\"renew-timer\": 1000, "
  1325. "\"valid-lifetime\": 4000, "
  1326. "\"subnet4\": [ "
  1327. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1328. " \"subnet\": \"192.0.2.0/24\" } ], "
  1329. "\"client-classes\": [ "
  1330. "{ \"name\": \"router\", "
  1331. " \"test\": \"option[host-name].text == 'foo'\" } ] }";
  1332. ElementPtr json = Element::fromJSON(config);
  1333. ConstElementPtr status;
  1334. // Configure the server and make sure the config is accepted
  1335. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1336. ASSERT_TRUE(status);
  1337. comment_ = config::parseAnswer(rcode_, status);
  1338. ASSERT_EQ(0, rcode_);
  1339. CfgMgr::instance().commit();
  1340. // Create a packet with enough to select the subnet
  1341. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1342. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1343. // Create and add a host-name option to the query
  1344. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1345. ASSERT_TRUE(hostname);
  1346. query->addOption(hostname);
  1347. // Classify packets
  1348. srv.classifyPacket(query);
  1349. // The queey should be in the router class
  1350. EXPECT_TRUE(query->inClass("router"));
  1351. }
  1352. // Checks if client packets are classified properly using match expressions
  1353. // using option names and definitions
  1354. TEST_F(Dhcpv4SrvTest, matchClassificationOptionDef) {
  1355. NakedDhcpv4Srv srv(0);
  1356. // The router class matches incoming packets with foo in a defined
  1357. // option
  1358. string config = "{ \"interfaces-config\": {"
  1359. " \"interfaces\": [ \"*\" ] }, "
  1360. "\"rebind-timer\": 2000, "
  1361. "\"renew-timer\": 1000, "
  1362. "\"valid-lifetime\": 4000, "
  1363. "\"subnet4\": [ "
  1364. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1365. " \"subnet\": \"192.0.2.0/24\" } ], "
  1366. "\"client-classes\": [ "
  1367. "{ \"name\": \"router\", "
  1368. " \"test\": \"option[my-host-name].text == 'foo'\" } ], "
  1369. "\"option-def\": [ {"
  1370. " \"name\": \"my-host-name\", "
  1371. " \"code\": 250, "
  1372. " \"type\": \"string\" } ] }";
  1373. ElementPtr json = Element::fromJSON(config);
  1374. ConstElementPtr status;
  1375. // Configure the server and make sure the config is accepted
  1376. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1377. ASSERT_TRUE(status);
  1378. comment_ = config::parseAnswer(rcode_, status);
  1379. ASSERT_EQ(0, rcode_);
  1380. CfgMgr::instance().commit();
  1381. // Create a packet with enough to select the subnet
  1382. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1383. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1384. // Create and add a my-host-name option to the query
  1385. OptionStringPtr hostname(new OptionString(Option::V4, 250, "foo"));
  1386. ASSERT_TRUE(hostname);
  1387. query->addOption(hostname);
  1388. // Classify packets
  1389. srv.classifyPacket(query);
  1390. // The queey should be in the router class
  1391. EXPECT_TRUE(query->inClass("router"));
  1392. }
  1393. // Checks subnet options have the priority over class options
  1394. TEST_F(Dhcpv4SrvTest, subnetClassPriority) {
  1395. IfaceMgrTestConfig test_config(true);
  1396. IfaceMgr::instance().openSockets4();
  1397. NakedDhcpv4Srv srv(0);
  1398. // Subnet sets an ip-forwarding option in the response.
  1399. // The router class matches incoming packets with foo in a host-name
  1400. // option (code 12) and sets an ip-forwarding option in the response.
  1401. string config = "{ \"interfaces-config\": {"
  1402. " \"interfaces\": [ \"*\" ] }, "
  1403. "\"rebind-timer\": 2000, "
  1404. "\"renew-timer\": 1000, "
  1405. "\"valid-lifetime\": 4000, "
  1406. "\"subnet4\": [ "
  1407. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1408. " \"subnet\": \"192.0.2.0/24\", "
  1409. " \"option-data\": ["
  1410. " { \"name\": \"ip-forwarding\", "
  1411. " \"data\": \"false\" } ] } ], "
  1412. "\"client-classes\": [ "
  1413. "{ \"name\": \"router\","
  1414. " \"option-data\": ["
  1415. " { \"name\": \"ip-forwarding\", "
  1416. " \"data\": \"true\" } ], "
  1417. " \"test\": \"option[12].text == 'foo'\" } ] }";
  1418. ElementPtr json = Element::fromJSON(config);
  1419. ConstElementPtr status;
  1420. // Configure the server and make sure the config is accepted
  1421. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1422. ASSERT_TRUE(status);
  1423. comment_ = config::parseAnswer(rcode_, status);
  1424. ASSERT_EQ(0, rcode_);
  1425. CfgMgr::instance().commit();
  1426. // Create a packet with enough to select the subnet and go through
  1427. // the DISCOVER processing
  1428. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1429. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1430. OptionPtr clientid = generateClientId();
  1431. query->addOption(clientid);
  1432. query->setIface("eth1");
  1433. // Create and add a PRL option to the query
  1434. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1435. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1436. ASSERT_TRUE(prl);
  1437. prl->addValue(DHO_IP_FORWARDING);
  1438. query->addOption(prl);
  1439. // Create and add a host-name option to the query
  1440. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1441. ASSERT_TRUE(hostname);
  1442. query->addOption(hostname);
  1443. // Classify the packet
  1444. srv.classifyPacket(query);
  1445. // The packet should be in the router class
  1446. EXPECT_TRUE(query->inClass("router"));
  1447. // Process the query
  1448. Pkt4Ptr response = srv.processDiscover(query);
  1449. // Processing should add an ip-forwarding option
  1450. OptionPtr opt = response->getOption(DHO_IP_FORWARDING);
  1451. ASSERT_TRUE(opt);
  1452. ASSERT_GT(opt->len(), opt->getHeaderLen());
  1453. // Classification sets the value to true/1, subnet to false/0
  1454. // Here subnet has the priority
  1455. EXPECT_EQ(0, opt->getUint8());
  1456. }
  1457. // Checks subnet options have the priority over global options
  1458. TEST_F(Dhcpv4SrvTest, subnetGlobalPriority) {
  1459. IfaceMgrTestConfig test_config(true);
  1460. IfaceMgr::instance().openSockets4();
  1461. NakedDhcpv4Srv srv(0);
  1462. // Subnet and global set an ip-forwarding option in the response.
  1463. string config = "{ \"interfaces-config\": {"
  1464. " \"interfaces\": [ \"*\" ] }, "
  1465. "\"rebind-timer\": 2000, "
  1466. "\"renew-timer\": 1000, "
  1467. "\"valid-lifetime\": 4000, "
  1468. "\"subnet4\": [ "
  1469. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1470. " \"subnet\": \"192.0.2.0/24\", "
  1471. " \"option-data\": ["
  1472. " { \"name\": \"ip-forwarding\", "
  1473. " \"data\": \"false\" } ] } ], "
  1474. "\"option-data\": ["
  1475. " { \"name\": \"ip-forwarding\", "
  1476. " \"data\": \"true\" } ] }";
  1477. ElementPtr json = Element::fromJSON(config);
  1478. ConstElementPtr status;
  1479. // Configure the server and make sure the config is accepted
  1480. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1481. ASSERT_TRUE(status);
  1482. comment_ = config::parseAnswer(rcode_, status);
  1483. ASSERT_EQ(0, rcode_);
  1484. CfgMgr::instance().commit();
  1485. // Create a packet with enough to select the subnet and go through
  1486. // the DISCOVER processing
  1487. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1488. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1489. OptionPtr clientid = generateClientId();
  1490. query->addOption(clientid);
  1491. query->setIface("eth1");
  1492. // Create and add a PRL option to the query
  1493. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1494. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1495. ASSERT_TRUE(prl);
  1496. prl->addValue(DHO_IP_FORWARDING);
  1497. query->addOption(prl);
  1498. // Create and add a host-name option to the query
  1499. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1500. ASSERT_TRUE(hostname);
  1501. query->addOption(hostname);
  1502. // Process the query
  1503. Pkt4Ptr response = srv.processDiscover(query);
  1504. // Processing should add an ip-forwarding option
  1505. OptionPtr opt = response->getOption(DHO_IP_FORWARDING);
  1506. ASSERT_TRUE(opt);
  1507. ASSERT_GT(opt->len(), opt->getHeaderLen());
  1508. // Global sets the value to true/1, subnet to false/0
  1509. // Here subnet has the priority
  1510. EXPECT_EQ(0, opt->getUint8());
  1511. }
  1512. // Checks class options have the priority over global options
  1513. TEST_F(Dhcpv4SrvTest, classGlobalPriority) {
  1514. IfaceMgrTestConfig test_config(true);
  1515. IfaceMgr::instance().openSockets4();
  1516. NakedDhcpv4Srv srv(0);
  1517. // A global ip-forwarding option is set in the response.
  1518. // The router class matches incoming packets with foo in a host-name
  1519. // option (code 12) and sets an ip-forwarding option in the response.
  1520. string config = "{ \"interfaces-config\": {"
  1521. " \"interfaces\": [ \"*\" ] }, "
  1522. "\"rebind-timer\": 2000, "
  1523. "\"renew-timer\": 1000, "
  1524. "\"valid-lifetime\": 4000, "
  1525. "\"subnet4\": [ "
  1526. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1527. " \"subnet\": \"192.0.2.0/24\" } ], "
  1528. "\"option-data\": ["
  1529. " { \"name\": \"ip-forwarding\", "
  1530. " \"data\": \"false\" } ], "
  1531. "\"client-classes\": [ "
  1532. "{ \"name\": \"router\","
  1533. " \"option-data\": ["
  1534. " { \"name\": \"ip-forwarding\", "
  1535. " \"data\": \"true\" } ], "
  1536. " \"test\": \"option[12].text == 'foo'\" } ] }";
  1537. ElementPtr json = Element::fromJSON(config);
  1538. ConstElementPtr status;
  1539. // Configure the server and make sure the config is accepted
  1540. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1541. ASSERT_TRUE(status);
  1542. comment_ = config::parseAnswer(rcode_, status);
  1543. ASSERT_EQ(0, rcode_);
  1544. CfgMgr::instance().commit();
  1545. // Create a packet with enough to select the subnet and go through
  1546. // the DISCOVER processing
  1547. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1548. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1549. OptionPtr clientid = generateClientId();
  1550. query->addOption(clientid);
  1551. query->setIface("eth1");
  1552. // Create and add a PRL option to the query
  1553. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1554. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1555. ASSERT_TRUE(prl);
  1556. prl->addValue(DHO_IP_FORWARDING);
  1557. query->addOption(prl);
  1558. // Create and add a host-name option to the query
  1559. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1560. ASSERT_TRUE(hostname);
  1561. query->addOption(hostname);
  1562. // Classify the packet
  1563. srv.classifyPacket(query);
  1564. // The packet should be in the router class
  1565. EXPECT_TRUE(query->inClass("router"));
  1566. // Process the query
  1567. Pkt4Ptr response = srv.processDiscover(query);
  1568. // Processing should add an ip-forwarding option
  1569. OptionPtr opt = response->getOption(DHO_IP_FORWARDING);
  1570. ASSERT_TRUE(opt);
  1571. ASSERT_GT(opt->len(), opt->getHeaderLen());
  1572. // Classification sets the value to true/1, global to false/0
  1573. // Here class has the priority
  1574. EXPECT_NE(0, opt->getUint8());
  1575. }
  1576. // Checks if the client-class field is indeed used for subnet selection.
  1577. // Note that packet classification is already checked in Dhcpv4SrvTest
  1578. // .*Classification above.
  1579. TEST_F(Dhcpv4SrvTest, clientClassify) {
  1580. // This test configures 2 subnets. We actually only need the
  1581. // first one, but since there's still this ugly hack that picks
  1582. // the pool if there is only one, we must use more than one
  1583. // subnet. That ugly hack will be removed in #3242, currently
  1584. // under review.
  1585. // The second subnet does not play any role here. The client's
  1586. // IP address belongs to the first subnet, so only that first
  1587. // subnet is being tested.
  1588. string config = "{ \"interfaces-config\": {"
  1589. " \"interfaces\": [ \"*\" ]"
  1590. "},"
  1591. "\"rebind-timer\": 2000, "
  1592. "\"renew-timer\": 1000, "
  1593. "\"subnet4\": [ "
  1594. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1595. " \"client-class\": \"foo\", "
  1596. " \"subnet\": \"192.0.2.0/24\" }, "
  1597. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  1598. " \"client-class\": \"xyzzy\", "
  1599. " \"subnet\": \"192.0.3.0/24\" } "
  1600. "],"
  1601. "\"valid-lifetime\": 4000 }";
  1602. ASSERT_NO_THROW(configure(config));
  1603. // Create a simple packet that we'll use for classification
  1604. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1605. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1606. dis->setCiaddr(IOAddress("192.0.2.1"));
  1607. dis->setIface("eth0");
  1608. OptionPtr clientid = generateClientId();
  1609. dis->addOption(clientid);
  1610. // This discover does not belong to foo class, so it will not
  1611. // be serviced
  1612. EXPECT_FALSE(srv_.selectSubnet(dis));
  1613. // Let's add the packet to bar class and try again.
  1614. dis->addClass("bar");
  1615. // Still not supported, because it belongs to wrong class.
  1616. EXPECT_FALSE(srv_.selectSubnet(dis));
  1617. // Let's add it to matching class.
  1618. dis->addClass("foo");
  1619. // This time it should work
  1620. EXPECT_TRUE(srv_.selectSubnet(dis));
  1621. }
  1622. // Checks if relay IP address specified in the relay-info structure in
  1623. // subnet4 is being used properly.
  1624. TEST_F(Dhcpv4SrvTest, relayOverride) {
  1625. // We have 2 subnets defined. Note that both have a relay address
  1626. // defined. Both are not belonging to the subnets. That is
  1627. // important, because if the relay belongs to the subnet, there's
  1628. // no need to specify relay override.
  1629. string config = "{ \"interfaces-config\": {"
  1630. " \"interfaces\": [ \"*\" ]"
  1631. "},"
  1632. "\"rebind-timer\": 2000, "
  1633. "\"renew-timer\": 1000, "
  1634. "\"subnet4\": [ "
  1635. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  1636. " \"relay\": { "
  1637. " \"ip-address\": \"192.0.5.1\""
  1638. " },"
  1639. " \"subnet\": \"192.0.2.0/24\" }, "
  1640. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  1641. " \"relay\": { "
  1642. " \"ip-address\": \"192.0.5.2\""
  1643. " },"
  1644. " \"subnet\": \"192.0.3.0/24\" } "
  1645. "],"
  1646. "\"valid-lifetime\": 4000 }";
  1647. // Use this config to set up the server
  1648. ASSERT_NO_THROW(configure(config));
  1649. // Let's get the subnet configuration objects
  1650. const Subnet4Collection* subnets =
  1651. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  1652. ASSERT_EQ(2, subnets->size());
  1653. // Let's get them for easy reference
  1654. Subnet4Ptr subnet1 = (*subnets)[0];
  1655. Subnet4Ptr subnet2 = (*subnets)[1];
  1656. ASSERT_TRUE(subnet1);
  1657. ASSERT_TRUE(subnet2);
  1658. // Let's create a packet.
  1659. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1660. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1661. dis->setIface("eth0");
  1662. dis->setHops(1);
  1663. OptionPtr clientid = generateClientId();
  1664. dis->addOption(clientid);
  1665. // This is just a sanity check, we're using regular method: ciaddr 192.0.2.1
  1666. // belongs to the first subnet, so it is selected
  1667. dis->setGiaddr(IOAddress("192.0.2.1"));
  1668. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  1669. // Relay belongs to the second subnet, so it should be selected.
  1670. dis->setGiaddr(IOAddress("192.0.3.1"));
  1671. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1672. // Now let's check if the relay override for the first subnets works
  1673. dis->setGiaddr(IOAddress("192.0.5.1"));
  1674. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  1675. // The same check for the second subnet...
  1676. dis->setGiaddr(IOAddress("192.0.5.2"));
  1677. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1678. // And finally, let's check if mis-matched relay address will end up
  1679. // in not selecting a subnet at all
  1680. dis->setGiaddr(IOAddress("192.0.5.3"));
  1681. EXPECT_FALSE(srv_.selectSubnet(dis));
  1682. // Finally, check that the relay override works only with relay address
  1683. // (GIADDR) and does not affect client address (CIADDR)
  1684. dis->setGiaddr(IOAddress("0.0.0.0"));
  1685. dis->setHops(0);
  1686. dis->setCiaddr(IOAddress("192.0.5.1"));
  1687. EXPECT_FALSE(srv_.selectSubnet(dis));
  1688. }
  1689. // Checks if relay IP address specified in the relay-info structure can be
  1690. // used together with client-classification.
  1691. TEST_F(Dhcpv4SrvTest, relayOverrideAndClientClass) {
  1692. // This test configures 2 subnets. They both are on the same link, so they
  1693. // have the same relay-ip address. Furthermore, the first subnet is
  1694. // reserved for clients that belong to class "foo".
  1695. string config = "{ \"interfaces-config\": {"
  1696. " \"interfaces\": [ \"*\" ]"
  1697. "},"
  1698. "\"rebind-timer\": 2000, "
  1699. "\"renew-timer\": 1000, "
  1700. "\"subnet4\": [ "
  1701. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  1702. " \"client-class\": \"foo\", "
  1703. " \"relay\": { "
  1704. " \"ip-address\": \"192.0.5.1\""
  1705. " },"
  1706. " \"subnet\": \"192.0.2.0/24\" }, "
  1707. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  1708. " \"relay\": { "
  1709. " \"ip-address\": \"192.0.5.1\""
  1710. " },"
  1711. " \"subnet\": \"192.0.3.0/24\" } "
  1712. "],"
  1713. "\"valid-lifetime\": 4000 }";
  1714. // Use this config to set up the server
  1715. ASSERT_NO_THROW(configure(config));
  1716. const Subnet4Collection* subnets =
  1717. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  1718. ASSERT_EQ(2, subnets->size());
  1719. // Let's get them for easy reference
  1720. Subnet4Ptr subnet1 = (*subnets)[0];
  1721. Subnet4Ptr subnet2 = (*subnets)[1];
  1722. ASSERT_TRUE(subnet1);
  1723. ASSERT_TRUE(subnet2);
  1724. // Let's create a packet.
  1725. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1726. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1727. dis->setIface("eth0");
  1728. dis->setHops(1);
  1729. dis->setGiaddr(IOAddress("192.0.5.1"));
  1730. OptionPtr clientid = generateClientId();
  1731. dis->addOption(clientid);
  1732. // This packet does not belong to class foo, so it should be rejected in
  1733. // subnet[0], even though the relay-ip matches. It should be accepted in
  1734. // subnet[1], because the subnet matches and there are no class
  1735. // requirements.
  1736. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1737. // Now let's add this packet to class foo and recheck. This time it should
  1738. // be accepted in the first subnet, because both class and relay-ip match.
  1739. dis->addClass("foo");
  1740. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  1741. }
  1742. // Checks if a RAI link selection sub-option works as expected
  1743. TEST_F(Dhcpv4SrvTest, relayLinkSelect) {
  1744. // We have 3 subnets defined.
  1745. string config = "{ \"interfaces-config\": {"
  1746. " \"interfaces\": [ \"*\" ]"
  1747. "},"
  1748. "\"rebind-timer\": 2000, "
  1749. "\"renew-timer\": 1000, "
  1750. "\"subnet4\": [ "
  1751. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  1752. " \"relay\": { "
  1753. " \"ip-address\": \"192.0.5.1\""
  1754. " },"
  1755. " \"subnet\": \"192.0.2.0/24\" }, "
  1756. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  1757. " \"subnet\": \"192.0.3.0/24\" }, "
  1758. "{ \"pools\": [ { \"pool\": \"192.0.4.1 - 192.0.4.100\" } ],"
  1759. " \"client-class\": \"foo\", "
  1760. " \"subnet\": \"192.0.4.0/24\" } "
  1761. "],"
  1762. "\"valid-lifetime\": 4000 }";
  1763. // Use this config to set up the server
  1764. ASSERT_NO_THROW(configure(config));
  1765. // Let's get the subnet configuration objects
  1766. const Subnet4Collection* subnets =
  1767. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  1768. ASSERT_EQ(3, subnets->size());
  1769. // Let's get them for easy reference
  1770. Subnet4Ptr subnet1 = (*subnets)[0];
  1771. Subnet4Ptr subnet2 = (*subnets)[1];
  1772. Subnet4Ptr subnet3 = (*subnets)[2];
  1773. ASSERT_TRUE(subnet1);
  1774. ASSERT_TRUE(subnet2);
  1775. ASSERT_TRUE(subnet3);
  1776. // Let's create a packet.
  1777. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1778. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1779. dis->setIface("eth0");
  1780. dis->setHops(1);
  1781. OptionPtr clientid = generateClientId();
  1782. dis->addOption(clientid);
  1783. // Let's create a Relay Agent Information option
  1784. OptionDefinitionPtr rai_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  1785. DHO_DHCP_AGENT_OPTIONS);
  1786. ASSERT_TRUE(rai_def);
  1787. OptionCustomPtr rai(new OptionCustom(*rai_def, Option::V4));
  1788. ASSERT_TRUE(rai);
  1789. IOAddress addr("192.0.3.2");
  1790. OptionPtr ols(new Option(Option::V4,
  1791. RAI_OPTION_LINK_SELECTION,
  1792. addr.toBytes()));
  1793. ASSERT_TRUE(ols);
  1794. rai->addOption(ols);
  1795. // This is just a sanity check, we're using regular method: ciaddr 192.0.3.1
  1796. // belongs to the second subnet, so it is selected
  1797. dis->setGiaddr(IOAddress("192.0.3.1"));
  1798. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1799. // Setup a relay override for the first subnet as it has a high precedence
  1800. dis->setGiaddr(IOAddress("192.0.5.1"));
  1801. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  1802. // Put a RAI to select back the second subnet as it has
  1803. // the highest precedence
  1804. dis->addOption(rai);
  1805. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1806. // Subnet select option has a lower precedence
  1807. OptionDefinitionPtr sbnsel_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  1808. DHO_SUBNET_SELECTION);
  1809. ASSERT_TRUE(sbnsel_def);
  1810. OptionCustomPtr sbnsel(new OptionCustom(*sbnsel_def, Option::V4));
  1811. ASSERT_TRUE(sbnsel);
  1812. sbnsel->writeAddress(IOAddress("192.0.2.3"));
  1813. dis->addOption(sbnsel);
  1814. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1815. dis->delOption(DHO_SUBNET_SELECTION);
  1816. // Check client-classification still applies
  1817. IOAddress addr_foo("192.0.4.2");
  1818. ols.reset(new Option(Option::V4, RAI_OPTION_LINK_SELECTION,
  1819. addr_foo.toBytes()));
  1820. rai->delOption(RAI_OPTION_LINK_SELECTION);
  1821. dis->delOption(DHO_DHCP_AGENT_OPTIONS);
  1822. rai->addOption(ols);
  1823. dis->addOption(rai);
  1824. // Note it shall fail (vs. try the next criterion).
  1825. EXPECT_FALSE(srv_.selectSubnet(dis));
  1826. // Add the packet to the class and check again: now it shall succeed
  1827. dis->addClass("foo");
  1828. EXPECT_TRUE(subnet3 == srv_.selectSubnet(dis));
  1829. // Check it fails with a bad address in the sub-option
  1830. IOAddress addr_bad("10.0.0.1");
  1831. ols.reset(new Option(Option::V4, RAI_OPTION_LINK_SELECTION,
  1832. addr_bad.toBytes()));
  1833. rai->delOption(RAI_OPTION_LINK_SELECTION);
  1834. dis->delOption(DHO_DHCP_AGENT_OPTIONS);
  1835. rai->addOption(ols);
  1836. dis->addOption(rai);
  1837. EXPECT_FALSE(srv_.selectSubnet(dis));
  1838. }
  1839. // Checks if a subnet selection option works as expected
  1840. TEST_F(Dhcpv4SrvTest, subnetSelect) {
  1841. // We have 3 subnets defined.
  1842. string config = "{ \"interfaces-config\": {"
  1843. " \"interfaces\": [ \"*\" ]"
  1844. "},"
  1845. "\"rebind-timer\": 2000, "
  1846. "\"renew-timer\": 1000, "
  1847. "\"subnet4\": [ "
  1848. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  1849. " \"relay\": { "
  1850. " \"ip-address\": \"192.0.5.1\""
  1851. " },"
  1852. " \"subnet\": \"192.0.2.0/24\" }, "
  1853. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  1854. " \"subnet\": \"192.0.3.0/24\" }, "
  1855. "{ \"pools\": [ { \"pool\": \"192.0.4.1 - 192.0.4.100\" } ],"
  1856. " \"client-class\": \"foo\", "
  1857. " \"subnet\": \"192.0.4.0/24\" } "
  1858. "],"
  1859. "\"valid-lifetime\": 4000 }";
  1860. // Use this config to set up the server
  1861. ASSERT_NO_THROW(configure(config));
  1862. // Let's get the subnet configuration objects
  1863. const Subnet4Collection* subnets =
  1864. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  1865. ASSERT_EQ(3, subnets->size());
  1866. // Let's get them for easy reference
  1867. Subnet4Ptr subnet1 = (*subnets)[0];
  1868. Subnet4Ptr subnet2 = (*subnets)[1];
  1869. Subnet4Ptr subnet3 = (*subnets)[2];
  1870. ASSERT_TRUE(subnet1);
  1871. ASSERT_TRUE(subnet2);
  1872. ASSERT_TRUE(subnet3);
  1873. // Let's create a packet.
  1874. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1875. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1876. dis->setIface("eth0");
  1877. dis->setHops(1);
  1878. OptionPtr clientid = generateClientId();
  1879. dis->addOption(clientid);
  1880. // Let's create a Subnet Selection option
  1881. OptionDefinitionPtr sbnsel_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  1882. DHO_SUBNET_SELECTION);
  1883. ASSERT_TRUE(sbnsel_def);
  1884. OptionCustomPtr sbnsel(new OptionCustom(*sbnsel_def, Option::V4));
  1885. ASSERT_TRUE(sbnsel);
  1886. sbnsel->writeAddress(IOAddress("192.0.3.2"));
  1887. // This is just a sanity check, we're using regular method: ciaddr 192.0.3.1
  1888. // belongs to the second subnet, so it is selected
  1889. dis->setGiaddr(IOAddress("192.0.3.1"));
  1890. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1891. // Setup a relay override for the first subnet as it has a high precedence
  1892. dis->setGiaddr(IOAddress("192.0.5.1"));
  1893. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  1894. // Put a subnet select option to select back the second subnet as
  1895. // it has the second highest precedence
  1896. dis->addOption(sbnsel);
  1897. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1898. // Check client-classification still applies
  1899. sbnsel->writeAddress(IOAddress("192.0.4.2"));
  1900. // Note it shall fail (vs. try the next criterion).
  1901. EXPECT_FALSE(srv_.selectSubnet(dis));
  1902. // Add the packet to the class and check again: now it shall succeed
  1903. dis->addClass("foo");
  1904. EXPECT_TRUE(subnet3 == srv_.selectSubnet(dis));
  1905. // Check it fails with a bad address in the sub-option
  1906. sbnsel->writeAddress(IOAddress("10.0.0.1"));
  1907. EXPECT_FALSE(srv_.selectSubnet(dis));
  1908. }
  1909. // This test verifies that the direct message is dropped when it has been
  1910. // received by the server via an interface for which there is no subnet
  1911. // configured. It also checks that the message is not dropped (is processed)
  1912. // when it is relayed or unicast.
  1913. TEST_F(Dhcpv4SrvTest, acceptDirectRequest) {
  1914. IfaceMgrTestConfig test_config(true);
  1915. IfaceMgr::instance().openSockets4();
  1916. NakedDhcpv4Srv srv(0);
  1917. Pkt4Ptr pkt(new Pkt4(DHCPDISCOVER, 1234));
  1918. // Set Giaddr and local server's unicast address, but don't set hops.
  1919. // Hops value should not matter. The server will treat the message
  1920. // with the hops value of 0 and non-zero giaddr as relayed.
  1921. pkt->setGiaddr(IOAddress("192.0.10.1"));
  1922. pkt->setRemoteAddr(IOAddress("0.0.0.0"));
  1923. pkt->setLocalAddr(IOAddress("192.0.2.3"));
  1924. pkt->setIface("eth1");
  1925. EXPECT_TRUE(srv.accept(pkt));
  1926. // Let's set hops and check that the message is still accepted as
  1927. // a relayed message.
  1928. pkt->setHops(1);
  1929. EXPECT_TRUE(srv.accept(pkt));
  1930. // Make it a direct message but keep unicast server's address. The
  1931. // messages sent to unicast address should be accepted as they are
  1932. // most likely to renew existing leases. The server should respond
  1933. // to renews so they have to be accepted and processed.
  1934. pkt->setHops(0);
  1935. pkt->setGiaddr(IOAddress("0.0.0.0"));
  1936. EXPECT_TRUE(srv.accept(pkt));
  1937. // Direct message is now sent to a broadcast address. The server
  1938. // should accept this message because it has been received via
  1939. // eth1 for which there is a subnet configured (see test fixture
  1940. // class constructor).
  1941. pkt->setLocalAddr(IOAddress("255.255.255.255"));
  1942. EXPECT_TRUE(srv.accept(pkt));
  1943. // For eth0, there is no subnet configured. Such message is expected
  1944. // to be silently dropped.
  1945. pkt->setIface("eth0");
  1946. EXPECT_FALSE(srv.accept(pkt));
  1947. // But, if the message is unicast it should be accepted, even though
  1948. // it has been received via eth0.
  1949. pkt->setLocalAddr(IOAddress("10.0.0.1"));
  1950. EXPECT_TRUE(srv.accept(pkt));
  1951. // For the DHCPINFORM the ciaddr should be set or at least the source
  1952. // address.
  1953. pkt->setType(DHCPINFORM);
  1954. pkt->setRemoteAddr(IOAddress("10.0.0.101"));
  1955. EXPECT_TRUE(srv.accept(pkt));
  1956. // When neither ciaddr nor source address is present, the packet should
  1957. // be dropped.
  1958. pkt->setRemoteAddr(IOAddress("0.0.0.0"));
  1959. EXPECT_FALSE(srv.accept(pkt));
  1960. // When ciaddr is set, the packet should be accepted.
  1961. pkt->setCiaddr(IOAddress("10.0.0.1"));
  1962. EXPECT_TRUE(srv.accept(pkt));
  1963. }
  1964. // This test checks that the server rejects a message with invalid type.
  1965. TEST_F(Dhcpv4SrvTest, acceptMessageType) {
  1966. IfaceMgrTestConfig test_config(true);
  1967. IfaceMgr::instance().openSockets4();
  1968. NakedDhcpv4Srv srv(0);
  1969. // Specify messages to be accepted by the server.
  1970. int allowed[] = {
  1971. DHCPDISCOVER,
  1972. DHCPREQUEST,
  1973. DHCPRELEASE,
  1974. DHCPDECLINE,
  1975. DHCPINFORM
  1976. };
  1977. size_t allowed_size = sizeof(allowed) / sizeof(allowed[0]);
  1978. // Check that the server actually accepts these message types.
  1979. for (size_t i = 0; i < allowed_size; ++i) {
  1980. EXPECT_TRUE(srv.acceptMessageType(Pkt4Ptr(new Pkt4(allowed[i], 1234))))
  1981. << "Test failed for message type " << i;
  1982. }
  1983. // Specify messages which server is supposed to drop.
  1984. int not_allowed[] = {
  1985. DHCPOFFER,
  1986. DHCPACK,
  1987. DHCPNAK,
  1988. DHCPLEASEQUERY,
  1989. DHCPLEASEUNASSIGNED,
  1990. DHCPLEASEUNKNOWN,
  1991. DHCPLEASEACTIVE,
  1992. DHCPBULKLEASEQUERY,
  1993. DHCPLEASEQUERYDONE
  1994. };
  1995. size_t not_allowed_size = sizeof(not_allowed) / sizeof(not_allowed[0]);
  1996. // Actually check that the server will drop these messages.
  1997. for (size_t i = 0; i < not_allowed_size; ++i) {
  1998. EXPECT_FALSE(srv.acceptMessageType(Pkt4Ptr(new Pkt4(not_allowed[i],
  1999. 1234))))
  2000. << "Test failed for message type " << i;
  2001. }
  2002. }
  2003. // Test checks whether statistic is bumped up appropriately when Decline
  2004. // message is received.
  2005. TEST_F(Dhcpv4SrvTest, statisticsDecline) {
  2006. NakedDhcpv4Srv srv(0);
  2007. pretendReceivingPkt(srv, CONFIGS[0], DHCPDECLINE, "pkt4-decline-received");
  2008. }
  2009. // Test checks whether statistic is bumped up appropriately when Offer
  2010. // message is received (this should never happen in a sane metwork).
  2011. TEST_F(Dhcpv4SrvTest, statisticsOfferRcvd) {
  2012. NakedDhcpv4Srv srv(0);
  2013. pretendReceivingPkt(srv, CONFIGS[0], DHCPOFFER, "pkt4-offer-received");
  2014. }
  2015. // Test checks whether statistic is bumped up appropriately when Ack
  2016. // message is received (this should never happen in a sane metwork).
  2017. TEST_F(Dhcpv4SrvTest, statisticsAckRcvd) {
  2018. NakedDhcpv4Srv srv(0);
  2019. pretendReceivingPkt(srv, CONFIGS[0], DHCPACK, "pkt4-ack-received");
  2020. }
  2021. // Test checks whether statistic is bumped up appropriately when Nak
  2022. // message is received (this should never happen in a sane metwork).
  2023. TEST_F(Dhcpv4SrvTest, statisticsNakRcvd) {
  2024. NakedDhcpv4Srv srv(0);
  2025. pretendReceivingPkt(srv, CONFIGS[0], DHCPNAK, "pkt4-nak-received");
  2026. }
  2027. // Test checks whether statistic is bumped up appropriately when Release
  2028. // message is received.
  2029. TEST_F(Dhcpv4SrvTest, statisticsReleaseRcvd) {
  2030. NakedDhcpv4Srv srv(0);
  2031. pretendReceivingPkt(srv, CONFIGS[0], DHCPRELEASE, "pkt4-release-received");
  2032. }
  2033. // Test checks whether statistic is bumped up appropriately when unknown
  2034. // message is received.
  2035. TEST_F(Dhcpv4SrvTest, statisticsUnknownRcvd) {
  2036. NakedDhcpv4Srv srv(0);
  2037. pretendReceivingPkt(srv, CONFIGS[0], 200, "pkt4-unknown-received");
  2038. // There should also be pkt4-receive-drop stat bumped up
  2039. using namespace isc::stats;
  2040. StatsMgr& mgr = StatsMgr::instance();
  2041. ObservationPtr drop_stat = mgr.getObservation("pkt4-receive-drop");
  2042. // This statistic must be present and must be set to 1.
  2043. ASSERT_TRUE(drop_stat);
  2044. EXPECT_EQ(1, drop_stat->getInteger().first);
  2045. }
  2046. // This test verifies that the server is able to handle an empty client-id
  2047. // in incoming client message.
  2048. TEST_F(Dhcpv4SrvTest, emptyClientId) {
  2049. IfaceMgrTestConfig test_config(true);
  2050. IfaceMgr::instance().openSockets4();
  2051. Dhcp4Client client;
  2052. EXPECT_NO_THROW(configure(CONFIGS[0], *client.getServer()));
  2053. // Tell the client to not send client-id on its own.
  2054. client.includeClientId("");
  2055. // Instead, tell him to send this extra option, which happens to be
  2056. // an empty client-id.
  2057. OptionPtr empty_client_id(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER));
  2058. client.addExtraOption(empty_client_id);
  2059. // Let's check whether the server is able to process this packet without
  2060. // throwing any exceptions. We don't care whether the server sent any
  2061. // responses or not. The goal is to check that the server didn't throw
  2062. // any exceptions.
  2063. EXPECT_NO_THROW(client.doDORA());
  2064. }
  2065. // This test verifies that the server is able to handle too long client-id
  2066. // in incoming client message.
  2067. TEST_F(Dhcpv4SrvTest, tooLongClientId) {
  2068. IfaceMgrTestConfig test_config(true);
  2069. IfaceMgr::instance().openSockets4();
  2070. Dhcp4Client client;
  2071. EXPECT_NO_THROW(configure(CONFIGS[0], *client.getServer()));
  2072. // Tell the client to not send client-id on its own.
  2073. client.includeClientId("");
  2074. // Instead, tell him to send this extra option, which happens to be
  2075. // an empty client-id.
  2076. std::vector<uint8_t> data(250, 250);
  2077. OptionPtr long_client_id(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER,
  2078. data));
  2079. client.addExtraOption(long_client_id);
  2080. // Let's check whether the server is able to process this packet without
  2081. // throwing any exceptions. We don't care whether the server sent any
  2082. // responses or not. The goal is to check that the server didn't throw
  2083. // any exceptions.
  2084. EXPECT_NO_THROW(client.doDORA());
  2085. }
  2086. /// @todo: Implement proper tests for MySQL lease/host database,
  2087. /// see ticket #4214.
  2088. }; // end of anonymous namespace