dhcp4_srv_unittest.cc 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562
  1. // Copyright (C) 2011-2017 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. ConstSrvConfigPtr cfg = CfgMgr::instance().getCurrentCfg();
  631. const Subnet4Collection* subnets = cfg->getCfgSubnets4()->getAll();
  632. ASSERT_EQ(1, subnets->size());
  633. CfgMgr::instance().clear();
  634. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnets->at(0));
  635. CfgMgr::instance().getStagingCfg()->setEchoClientId(false);
  636. CfgMgr::instance().commit();
  637. offer = srv.processDiscover(dis);
  638. // Check if we get response at all
  639. checkResponse(offer, DHCPOFFER, 1234);
  640. checkClientId(offer, clientid);
  641. }
  642. // Check that option 58 and 59 are not included if they are not specified.
  643. TEST_F(Dhcpv4SrvTest, RequestNoTimers) {
  644. IfaceMgrTestConfig test_config(true);
  645. IfaceMgr::instance().openSockets4();
  646. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  647. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  648. Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  649. req->setRemoteAddr(IOAddress("192.0.2.1"));
  650. OptionPtr clientid = generateClientId();
  651. req->addOption(clientid);
  652. req->setIface("eth1");
  653. // Recreate a subnet but set T1 and T2 to "unspecified".
  654. subnet_.reset(new Subnet4(IOAddress("192.0.2.0"), 24,
  655. Triplet<uint32_t>(),
  656. Triplet<uint32_t>(),
  657. 3000));
  658. pool_ = Pool4Ptr(new Pool4(IOAddress("192.0.2.100"),
  659. IOAddress("192.0.2.110")));
  660. subnet_->addPool(pool_);
  661. CfgMgr::instance().clear();
  662. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnet_);
  663. CfgMgr::instance().commit();
  664. // Pass it to the server and get an ACK.
  665. Pkt4Ptr ack = srv->processRequest(req);
  666. // Check if we get response at all
  667. checkResponse(ack, DHCPACK, 1234);
  668. // T1 and T2 timers must not be present.
  669. checkAddressParams(ack, subnet_, false, false);
  670. // Check identifiers
  671. checkServerId(ack, srv->getServerID());
  672. checkClientId(ack, clientid);
  673. }
  674. // Checks whether echoing back client-id is controllable
  675. TEST_F(Dhcpv4SrvTest, requestEchoClientId) {
  676. IfaceMgrTestConfig test_config(true);
  677. IfaceMgr::instance().openSockets4();
  678. NakedDhcpv4Srv srv(0);
  679. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  680. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  681. OptionPtr clientid = generateClientId();
  682. dis->addOption(clientid);
  683. dis->setIface("eth1");
  684. // Pass it to the server and get ACK
  685. Pkt4Ptr ack = srv.processRequest(dis);
  686. // Check if we get response at all
  687. checkResponse(ack, DHCPACK, 1234);
  688. checkClientId(ack, clientid);
  689. ConstSrvConfigPtr cfg = CfgMgr::instance().getCurrentCfg();
  690. const Subnet4Collection* subnets = cfg->getCfgSubnets4()->getAll();
  691. ASSERT_EQ(1, subnets->size());
  692. CfgMgr::instance().clear();
  693. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnets->at(0));
  694. CfgMgr::instance().getStagingCfg()->setEchoClientId(false);
  695. CfgMgr::instance().commit();
  696. ack = srv.processRequest(dis);
  697. // Check if we get response at all
  698. checkResponse(ack, DHCPACK, 1234);
  699. checkClientId(ack, clientid);
  700. }
  701. // This test verifies that incoming (positive) REQUEST/Renewing can be handled properly, that a
  702. // REPLY is generated, that the response has an address and that address
  703. // really belongs to the configured pool and that lease is actually renewed.
  704. //
  705. // expected:
  706. // - returned REPLY message has copy of client-id
  707. // - returned REPLY message has server-id
  708. // - returned REPLY message has IA that includes IAADDR
  709. // - lease is actually renewed in LeaseMgr
  710. TEST_F(Dhcpv4SrvTest, RenewBasic) {
  711. IfaceMgrTestConfig test_config(true);
  712. IfaceMgr::instance().openSockets4();
  713. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  714. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  715. const IOAddress addr("192.0.2.106");
  716. const uint32_t temp_t1 = 50;
  717. const uint32_t temp_t2 = 75;
  718. const uint32_t temp_valid = 100;
  719. const time_t temp_timestamp = time(NULL) - 10;
  720. // Generate client-id also sets client_id_ member
  721. OptionPtr clientid = generateClientId();
  722. // Check that the address we are about to use is indeed in pool
  723. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
  724. // let's create a lease and put it in the LeaseMgr
  725. uint8_t hwaddr2_data[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  726. HWAddrPtr hwaddr2(new HWAddr(hwaddr2_data, sizeof(hwaddr2_data), HTYPE_ETHER));
  727. Lease4Ptr used(new Lease4(IOAddress("192.0.2.106"), hwaddr2,
  728. &client_id_->getDuid()[0], client_id_->getDuid().size(),
  729. temp_valid, temp_t1, temp_t2, temp_timestamp,
  730. subnet_->getID()));
  731. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  732. // Check that the lease is really in the database
  733. Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
  734. ASSERT_TRUE(l);
  735. // Check that T1, T2, preferred, valid and cltt really set.
  736. // Constructed lease looks as if it was assigned 10 seconds ago
  737. // EXPECT_EQ(l->t1_, temp_t1);
  738. // EXPECT_EQ(l->t2_, temp_t2);
  739. EXPECT_EQ(l->valid_lft_, temp_valid);
  740. EXPECT_EQ(l->cltt_, temp_timestamp);
  741. // Let's create a RENEW
  742. Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  743. req->setRemoteAddr(IOAddress(addr));
  744. req->setYiaddr(addr);
  745. req->setCiaddr(addr); // client's address
  746. req->setIface("eth0");
  747. req->setHWAddr(hwaddr2);
  748. req->addOption(clientid);
  749. req->addOption(srv->getServerID());
  750. // Pass it to the server and hope for a REPLY
  751. Pkt4Ptr ack = srv->processRequest(req);
  752. // Check if we get response at all
  753. checkResponse(ack, DHCPACK, 1234);
  754. EXPECT_EQ(addr, ack->getYiaddr());
  755. // Check that address was returned from proper range, that its lease
  756. // lifetime is correct, that T1 and T2 are returned properly
  757. checkAddressParams(ack, subnet_, true, true);
  758. // Check identifiers
  759. checkServerId(ack, srv->getServerID());
  760. checkClientId(ack, clientid);
  761. // Check that the lease is really in the database
  762. l = checkLease(ack, clientid, req->getHWAddr(), addr);
  763. ASSERT_TRUE(l);
  764. // Check that T1, T2, preferred, valid and cltt were really updated
  765. EXPECT_EQ(l->t1_, subnet_->getT1());
  766. EXPECT_EQ(l->t2_, subnet_->getT2());
  767. EXPECT_EQ(l->valid_lft_, subnet_->getValid());
  768. // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
  769. int32_t cltt = static_cast<int32_t>(l->cltt_);
  770. int32_t expected = static_cast<int32_t>(time(NULL));
  771. // Equality or difference by 1 between cltt and expected is ok.
  772. EXPECT_GE(1, abs(cltt - expected));
  773. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  774. }
  775. // This test verifies that the logic which matches server identifier in the
  776. // received message with server identifiers used by a server works correctly:
  777. // - a message with no server identifier is accepted,
  778. // - a message with a server identifier which matches one of the server
  779. // identifiers used by a server is accepted,
  780. // - a message with a server identifier which doesn't match any server
  781. // identifier used by a server, is not accepted.
  782. TEST_F(Dhcpv4SrvTest, acceptServerId) {
  783. IfaceMgrTestConfig test_config(true);
  784. IfaceMgr::instance().openSockets4();
  785. NakedDhcpv4Srv srv(0);
  786. Pkt4Ptr pkt(new Pkt4(DHCPREQUEST, 1234));
  787. // If no server identifier option is present, the message is always
  788. // accepted.
  789. EXPECT_TRUE(srv.acceptServerId(pkt));
  790. // Create definition of the server identifier option.
  791. OptionDefinition def("server-identifier", DHO_DHCP_SERVER_IDENTIFIER,
  792. "ipv4-address", false);
  793. // Add a server identifier option which doesn't match server ids being
  794. // used by the server. The accepted server ids are the IPv4 addresses
  795. // configured on the interfaces. The 10.1.2.3 is not configured on
  796. // any interfaces.
  797. OptionCustomPtr other_serverid(new OptionCustom(def, Option::V6));
  798. other_serverid->writeAddress(IOAddress("10.1.2.3"));
  799. pkt->addOption(other_serverid);
  800. EXPECT_FALSE(srv.acceptServerId(pkt));
  801. // Remove the server identifier.
  802. ASSERT_NO_THROW(pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER));
  803. // Add a server id being an IPv4 address configured on eth0 interface.
  804. // A DHCPv4 message holding this server identifier should be accepted.
  805. OptionCustomPtr eth0_serverid(new OptionCustom(def, Option::V6));
  806. eth0_serverid->writeAddress(IOAddress("192.0.2.3"));
  807. ASSERT_NO_THROW(pkt->addOption(eth0_serverid));
  808. EXPECT_TRUE(srv.acceptServerId(pkt));
  809. // Remove the server identifier.
  810. ASSERT_NO_THROW(pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER));
  811. // Add a server id being an IPv4 address configured on eth1 interface.
  812. // A DHCPv4 message holding this server identifier should be accepted.
  813. OptionCustomPtr eth1_serverid(new OptionCustom(def, Option::V6));
  814. eth1_serverid->writeAddress(IOAddress("10.0.0.1"));
  815. ASSERT_NO_THROW(pkt->addOption(eth1_serverid));
  816. EXPECT_TRUE(srv.acceptServerId(pkt));
  817. }
  818. // @todo: Implement tests for rejecting renewals
  819. // This test verifies if the sanityCheck() really checks options presence.
  820. TEST_F(Dhcpv4SrvTest, sanityCheck) {
  821. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  822. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  823. Pkt4Ptr pkt = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  824. pkt->setHWAddr(generateHWAddr(6));
  825. // Server-id is optional for information-request, so
  826. EXPECT_NO_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::OPTIONAL));
  827. // Empty packet, no server-id
  828. EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY),
  829. RFCViolation);
  830. pkt->addOption(srv->getServerID());
  831. // Server-id is mandatory and present = no exception
  832. EXPECT_NO_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY));
  833. // Server-id is forbidden, but present => exception
  834. EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::FORBIDDEN),
  835. RFCViolation);
  836. // There's no client-id and no HWADDR. Server needs something to
  837. // identify the client
  838. pkt->setHWAddr(generateHWAddr(0));
  839. EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY),
  840. RFCViolation);
  841. }
  842. // Checks if received relay agent info option is echoed back to the client
  843. TEST_F(Dhcpv4SrvTest, relayAgentInfoEcho) {
  844. IfaceMgrTestConfig test_config(true);
  845. IfaceMgr::instance().openSockets4();
  846. NakedDhcpv4Srv srv(0);
  847. // Use of the captured DHCPDISCOVER packet requires that
  848. // subnet 10.254.226.0/24 is in use, because this packet
  849. // contains the giaddr which belongs to this subnet and
  850. // this giaddr is used to select the subnet
  851. configure(CONFIGS[0]);
  852. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  853. // added option 82 (relay agent info) with 3 suboptions. The server
  854. // is supposed to echo it back in its response.
  855. Pkt4Ptr dis;
  856. ASSERT_NO_THROW(dis = PktCaptures::captureRelayedDiscover());
  857. // Simulate that we have received that traffic
  858. srv.fakeReceive(dis);
  859. // Server will now process to run its normal loop, but instead of calling
  860. // IfaceMgr::receive4(), it will read all packets from the list set by
  861. // fakeReceive()
  862. // In particular, it should call registered buffer4_receive callback.
  863. srv.run();
  864. // Check that the server did send a response
  865. ASSERT_EQ(1, srv.fake_sent_.size());
  866. // Make sure that we received a response
  867. Pkt4Ptr offer = srv.fake_sent_.front();
  868. ASSERT_TRUE(offer);
  869. // Get Relay Agent Info from query...
  870. OptionPtr rai_query = dis->getOption(DHO_DHCP_AGENT_OPTIONS);
  871. ASSERT_TRUE(rai_query);
  872. // Get Relay Agent Info from response...
  873. OptionPtr rai_response = offer->getOption(DHO_DHCP_AGENT_OPTIONS);
  874. ASSERT_TRUE(rai_response);
  875. EXPECT_TRUE(rai_response->equals(rai_query));
  876. }
  877. /// @todo move vendor options tests to a separate file.
  878. /// @todo Add more extensive vendor options tests, including multiple
  879. /// vendor options
  880. // Checks if vendor options are parsed correctly and requested vendor options
  881. // are echoed back.
  882. TEST_F(Dhcpv4SrvTest, vendorOptionsDocsis) {
  883. IfaceMgrTestConfig test_config(true);
  884. IfaceMgr::instance().openSockets4();
  885. NakedDhcpv4Srv srv(0);
  886. string config = "{ \"interfaces-config\": {"
  887. " \"interfaces\": [ \"*\" ]"
  888. "},"
  889. "\"rebind-timer\": 2000, "
  890. "\"renew-timer\": 1000, "
  891. " \"option-data\": [ {"
  892. " \"name\": \"tftp-servers\","
  893. " \"space\": \"vendor-4491\","
  894. " \"code\": 2,"
  895. " \"data\": \"10.253.175.16\","
  896. " \"csv-format\": true"
  897. " }],"
  898. "\"subnet4\": [ { "
  899. " \"pools\": [ { \"pool\": \"10.254.226.0/25\" } ],"
  900. " \"subnet\": \"10.254.226.0/24\", "
  901. " \"rebind-timer\": 2000, "
  902. " \"renew-timer\": 1000, "
  903. " \"valid-lifetime\": 4000,"
  904. " \"interface\": \"eth0\" "
  905. " } ],"
  906. "\"valid-lifetime\": 4000 }";
  907. ConstElementPtr json;
  908. ASSERT_NO_THROW(json = parseDHCP4(config));
  909. ConstElementPtr status;
  910. // Configure the server and make sure the config is accepted
  911. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  912. ASSERT_TRUE(status);
  913. comment_ = config::parseAnswer(rcode_, status);
  914. ASSERT_EQ(0, rcode_);
  915. CfgMgr::instance().commit();
  916. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  917. // added option 82 (relay agent info) with 3 suboptions. The server
  918. // is supposed to echo it back in its response.
  919. Pkt4Ptr dis;
  920. ASSERT_NO_THROW(dis = PktCaptures::captureRelayedDiscover());
  921. // Simulate that we have received that traffic
  922. srv.fakeReceive(dis);
  923. // Server will now process to run its normal loop, but instead of calling
  924. // IfaceMgr::receive4(), it will read all packets from the list set by
  925. // fakeReceive()
  926. // In particular, it should call registered buffer4_receive callback.
  927. srv.run();
  928. // Check that the server did send a response
  929. ASSERT_EQ(1, srv.fake_sent_.size());
  930. // Make sure that we received a response
  931. Pkt4Ptr offer = srv.fake_sent_.front();
  932. ASSERT_TRUE(offer);
  933. // Get Relay Agent Info from query...
  934. OptionPtr vendor_opt_response = offer->getOption(DHO_VIVSO_SUBOPTIONS);
  935. ASSERT_TRUE(vendor_opt_response);
  936. // Check if it's of a correct type
  937. boost::shared_ptr<OptionVendor> vendor_opt =
  938. boost::dynamic_pointer_cast<OptionVendor>(vendor_opt_response);
  939. ASSERT_TRUE(vendor_opt);
  940. // Get Relay Agent Info from response...
  941. OptionPtr tftp_servers_generic = vendor_opt->getOption(DOCSIS3_V4_TFTP_SERVERS);
  942. ASSERT_TRUE(tftp_servers_generic);
  943. Option4AddrLstPtr tftp_servers =
  944. boost::dynamic_pointer_cast<Option4AddrLst>(tftp_servers_generic);
  945. ASSERT_TRUE(tftp_servers);
  946. Option4AddrLst::AddressContainer addrs = tftp_servers->getAddresses();
  947. ASSERT_EQ(1, addrs.size());
  948. EXPECT_EQ("10.253.175.16", addrs[0].toText());
  949. }
  950. /// @todo Implement tests for subnetSelect See tests in dhcp6_srv_unittest.cc:
  951. /// selectSubnetAddr, selectSubnetIface, selectSubnetRelayLinkaddr,
  952. /// selectSubnetRelayInterfaceId. Note that the concept of interface-id is not
  953. /// present in the DHCPv4, so not everything is applicable directly.
  954. /// See ticket #3057
  955. // Checks whether the server uses default (0.0.0.0) siaddr value, unless
  956. // explicitly specified
  957. TEST_F(Dhcpv4SrvTest, siaddrDefault) {
  958. IfaceMgrTestConfig test_config(true);
  959. IfaceMgr::instance().openSockets4();
  960. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  961. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  962. IOAddress hint("192.0.2.107");
  963. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  964. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  965. OptionPtr clientid = generateClientId();
  966. dis->addOption(clientid);
  967. dis->setYiaddr(hint);
  968. dis->setIface("eth1");
  969. // Pass it to the server and get an offer
  970. Pkt4Ptr offer = srv->processDiscover(dis);
  971. ASSERT_TRUE(offer);
  972. // Check if we get response at all
  973. checkResponse(offer, DHCPOFFER, 1234);
  974. // Verify that it is 0.0.0.0
  975. EXPECT_EQ("0.0.0.0", offer->getSiaddr().toText());
  976. }
  977. // Checks whether the server uses specified siaddr value
  978. TEST_F(Dhcpv4SrvTest, siaddr) {
  979. IfaceMgrTestConfig test_config(true);
  980. IfaceMgr::instance().openSockets4();
  981. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  982. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  983. subnet_->setSiaddr(IOAddress("192.0.2.123"));
  984. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  985. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  986. dis->setIface("eth1");
  987. OptionPtr clientid = generateClientId();
  988. dis->addOption(clientid);
  989. // Pass it to the server and get an offer
  990. Pkt4Ptr offer = srv->processDiscover(dis);
  991. ASSERT_TRUE(offer);
  992. // Check if we get response at all
  993. checkResponse(offer, DHCPOFFER, 1234);
  994. // Verify that its value is proper
  995. EXPECT_EQ("192.0.2.123", offer->getSiaddr().toText());
  996. }
  997. // Checks if the next-server defined as global value is overridden by subnet
  998. // specific value and returned in server messages. There's also similar test for
  999. // checking parser only configuration, see Dhcp4ParserTest.nextServerOverride in
  1000. // config_parser_unittest.cc.
  1001. TEST_F(Dhcpv4SrvTest, nextServerOverride) {
  1002. IfaceMgrTestConfig test_config(true);
  1003. IfaceMgr::instance().openSockets4();
  1004. NakedDhcpv4Srv srv(0);
  1005. ConstElementPtr status;
  1006. string config = "{ \"interfaces-config\": {"
  1007. " \"interfaces\": [ \"*\" ]"
  1008. "},"
  1009. "\"rebind-timer\": 2000, "
  1010. "\"renew-timer\": 1000, "
  1011. "\"next-server\": \"192.0.0.1\", "
  1012. "\"subnet4\": [ { "
  1013. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1014. " \"next-server\": \"1.2.3.4\", "
  1015. " \"subnet\": \"192.0.2.0/24\" } ],"
  1016. "\"valid-lifetime\": 4000 }";
  1017. ConstElementPtr json;
  1018. ASSERT_NO_THROW(json = parseDHCP4(config, true));
  1019. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1020. CfgMgr::instance().commit();
  1021. // check if returned status is OK
  1022. ASSERT_TRUE(status);
  1023. comment_ = config::parseAnswer(rcode_, status);
  1024. ASSERT_EQ(0, rcode_);
  1025. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1026. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1027. dis->setIface("eth1");
  1028. OptionPtr clientid = generateClientId();
  1029. dis->addOption(clientid);
  1030. // Pass it to the server and get an offer
  1031. Pkt4Ptr offer = srv.processDiscover(dis);
  1032. ASSERT_TRUE(offer);
  1033. EXPECT_EQ(DHCPOFFER, offer->getType());
  1034. EXPECT_EQ("1.2.3.4", offer->getSiaddr().toText());
  1035. }
  1036. // Checks if the next-server defined as global value is used in responses
  1037. // when there is no specific value defined in subnet and returned to the client
  1038. // properly. There's also similar test for checking parser only configuration,
  1039. // see Dhcp4ParserTest.nextServerGlobal in config_parser_unittest.cc.
  1040. TEST_F(Dhcpv4SrvTest, nextServerGlobal) {
  1041. IfaceMgrTestConfig test_config(true);
  1042. IfaceMgr::instance().openSockets4();
  1043. NakedDhcpv4Srv srv(0);
  1044. ConstElementPtr status;
  1045. string config = "{ \"interfaces-config\": {"
  1046. " \"interfaces\": [ \"*\" ]"
  1047. "},"
  1048. "\"rebind-timer\": 2000, "
  1049. "\"renew-timer\": 1000, "
  1050. "\"next-server\": \"192.0.0.1\", "
  1051. "\"subnet4\": [ { "
  1052. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1053. " \"subnet\": \"192.0.2.0/24\" } ],"
  1054. "\"valid-lifetime\": 4000 }";
  1055. ConstElementPtr json;
  1056. ASSERT_NO_THROW(json = parseDHCP4(config, true));
  1057. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1058. CfgMgr::instance().commit();
  1059. // check if returned status is OK
  1060. ASSERT_TRUE(status);
  1061. comment_ = config::parseAnswer(rcode_, status);
  1062. ASSERT_EQ(0, rcode_);
  1063. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1064. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1065. dis->setIface("eth1");
  1066. OptionPtr clientid = generateClientId();
  1067. dis->addOption(clientid);
  1068. // Pass it to the server and get an offer
  1069. Pkt4Ptr offer = srv.processDiscover(dis);
  1070. ASSERT_TRUE(offer);
  1071. EXPECT_EQ(DHCPOFFER, offer->getType());
  1072. EXPECT_EQ("192.0.0.1", offer->getSiaddr().toText());
  1073. }
  1074. // Checks if server is able to handle a relayed traffic from DOCSIS3.0 modems
  1075. TEST_F(Dhcpv4SrvTest, docsisVendorOptionsParse) {
  1076. // Let's get a traffic capture from DOCSIS3.0 modem
  1077. Pkt4Ptr dis = PktCaptures::captureRelayedDiscover();
  1078. ASSERT_NO_THROW(dis->unpack());
  1079. // Check if the packet contain
  1080. OptionPtr opt = dis->getOption(DHO_VIVSO_SUBOPTIONS);
  1081. ASSERT_TRUE(opt);
  1082. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  1083. ASSERT_TRUE(vendor);
  1084. // This particular capture that we have included options 1 and 5
  1085. EXPECT_TRUE(vendor->getOption(1));
  1086. EXPECT_TRUE(vendor->getOption(5));
  1087. // It did not include options any other options
  1088. EXPECT_FALSE(vendor->getOption(2));
  1089. EXPECT_FALSE(vendor->getOption(3));
  1090. EXPECT_FALSE(vendor->getOption(17));
  1091. }
  1092. // Checks if server is able to parse incoming docsis option and extract suboption 1 (docsis ORO)
  1093. TEST_F(Dhcpv4SrvTest, docsisVendorORO) {
  1094. // Let's get a traffic capture from DOCSIS3.0 modem
  1095. Pkt4Ptr dis = PktCaptures::captureRelayedDiscover();
  1096. EXPECT_NO_THROW(dis->unpack());
  1097. // Check if the packet contains vendor specific information option
  1098. OptionPtr opt = dis->getOption(DHO_VIVSO_SUBOPTIONS);
  1099. ASSERT_TRUE(opt);
  1100. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  1101. ASSERT_TRUE(vendor);
  1102. opt = vendor->getOption(DOCSIS3_V4_ORO);
  1103. ASSERT_TRUE(opt);
  1104. OptionUint8ArrayPtr oro = boost::dynamic_pointer_cast<OptionUint8Array>(opt);
  1105. EXPECT_TRUE(oro);
  1106. }
  1107. // This test checks if Option Request Option (ORO) in docsis (vendor-id=4491)
  1108. // vendor options is parsed correctly and the requested options are actually assigned.
  1109. TEST_F(Dhcpv4SrvTest, vendorOptionsORO) {
  1110. IfaceMgrTestConfig test_config(true);
  1111. IfaceMgr::instance().openSockets4();
  1112. NakedDhcpv4Srv srv(0);
  1113. ConstElementPtr x;
  1114. string config = "{ \"interfaces-config\": {"
  1115. " \"interfaces\": [ \"*\" ]"
  1116. "},"
  1117. "\"rebind-timer\": 2000, "
  1118. "\"renew-timer\": 1000, "
  1119. " \"option-data\": [ {"
  1120. " \"name\": \"tftp-servers\","
  1121. " \"space\": \"vendor-4491\","
  1122. " \"code\": 2,"
  1123. " \"data\": \"192.0.2.1, 192.0.2.2\","
  1124. " \"csv-format\": true"
  1125. " }],"
  1126. "\"subnet4\": [ { "
  1127. " \"pools\": [ { \"pool\": \"192.0.2.0/25\" } ],"
  1128. " \"subnet\": \"192.0.2.0/24\", "
  1129. " \"rebind-timer\": 2000, "
  1130. " \"renew-timer\": 1000, "
  1131. " \"valid-lifetime\": 4000,"
  1132. " \"interface\": \"eth0\" "
  1133. " } ],"
  1134. "\"valid-lifetime\": 4000 }";
  1135. ConstElementPtr json;
  1136. ASSERT_NO_THROW(json = parseDHCP4(config));
  1137. EXPECT_NO_THROW(x = configureDhcp4Server(srv, json));
  1138. ASSERT_TRUE(x);
  1139. comment_ = isc::config::parseAnswer(rcode_, x);
  1140. ASSERT_EQ(0, rcode_);
  1141. CfgMgr::instance().commit();
  1142. boost::shared_ptr<Pkt4> dis(new Pkt4(DHCPDISCOVER, 1234));
  1143. // Set the giaddr and hops to non-zero address as if it was relayed.
  1144. dis->setGiaddr(IOAddress("192.0.2.1"));
  1145. dis->setHops(1);
  1146. OptionPtr clientid = generateClientId();
  1147. dis->addOption(clientid);
  1148. // Set interface. It is required by the server to generate server id.
  1149. dis->setIface("eth0");
  1150. // Pass it to the server and get an advertise
  1151. Pkt4Ptr offer = srv.processDiscover(dis);
  1152. // check if we get response at all
  1153. ASSERT_TRUE(offer);
  1154. // We did not include any vendor opts in DISCOVER, so there should be none
  1155. // in OFFER.
  1156. ASSERT_FALSE(offer->getOption(DHO_VIVSO_SUBOPTIONS));
  1157. // Let's add a vendor-option (vendor-id=4491) with a single sub-option.
  1158. // That suboption has code 1 and is a docsis ORO option.
  1159. boost::shared_ptr<OptionUint8Array> vendor_oro(new OptionUint8Array(Option::V4,
  1160. DOCSIS3_V4_ORO));
  1161. vendor_oro->addValue(DOCSIS3_V4_TFTP_SERVERS); // Request option 33
  1162. OptionPtr vendor(new OptionVendor(Option::V4, 4491));
  1163. vendor->addOption(vendor_oro);
  1164. dis->addOption(vendor);
  1165. // Need to process SOLICIT again after requesting new option.
  1166. offer = srv.processDiscover(dis);
  1167. ASSERT_TRUE(offer);
  1168. // Check if there is a vendor option response
  1169. OptionPtr tmp = offer->getOption(DHO_VIVSO_SUBOPTIONS);
  1170. ASSERT_TRUE(tmp);
  1171. // The response should be OptionVendor object
  1172. boost::shared_ptr<OptionVendor> vendor_resp =
  1173. boost::dynamic_pointer_cast<OptionVendor>(tmp);
  1174. ASSERT_TRUE(vendor_resp);
  1175. OptionPtr docsis2 = vendor_resp->getOption(DOCSIS3_V4_TFTP_SERVERS);
  1176. ASSERT_TRUE(docsis2);
  1177. Option4AddrLstPtr tftp_srvs = boost::dynamic_pointer_cast<Option4AddrLst>(docsis2);
  1178. ASSERT_TRUE(tftp_srvs);
  1179. Option4AddrLst::AddressContainer addrs = tftp_srvs->getAddresses();
  1180. ASSERT_EQ(2, addrs.size());
  1181. EXPECT_EQ("192.0.2.1", addrs[0].toText());
  1182. EXPECT_EQ("192.0.2.2", addrs[1].toText());
  1183. }
  1184. // Test checks whether it is possible to use option definitions defined in
  1185. // src/lib/dhcp/docsis3_option_defs.h.
  1186. TEST_F(Dhcpv4SrvTest, vendorOptionsDocsisDefinitions) {
  1187. ConstElementPtr x;
  1188. string config_prefix = "{ \"interfaces-config\": {"
  1189. " \"interfaces\": [ ]"
  1190. "},"
  1191. "\"rebind-timer\": 2000, "
  1192. "\"renew-timer\": 1000, "
  1193. " \"option-data\": [ {"
  1194. " \"name\": \"tftp-servers\","
  1195. " \"space\": \"vendor-4491\","
  1196. " \"code\": ";
  1197. string config_postfix = ","
  1198. " \"data\": \"192.0.2.1\","
  1199. " \"csv-format\": true"
  1200. " }],"
  1201. "\"subnet4\": [ { "
  1202. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.50\" } ],"
  1203. " \"subnet\": \"192.0.2.0/24\", "
  1204. " \"renew-timer\": 1000, "
  1205. " \"rebind-timer\": 1000, "
  1206. " \"valid-lifetime\": 4000,"
  1207. " \"interface\": \"\""
  1208. " } ],"
  1209. "\"valid-lifetime\": 4000 }";
  1210. // There is docsis3 (vendor-id=4491) vendor option 2, which is a
  1211. // tftp-server. Its format is list of IPv4 addresses.
  1212. string config_valid = config_prefix + "2" + config_postfix;
  1213. // There is no option 99 defined in vendor-id=4491. As there is no
  1214. // definition, the config should fail.
  1215. string config_bogus = config_prefix + "99" + config_postfix;
  1216. ConstElementPtr json_bogus;
  1217. ASSERT_NO_THROW(json_bogus = parseDHCP4(config_bogus));
  1218. ConstElementPtr json_valid;
  1219. ASSERT_NO_THROW(json_valid = parseDHCP4(config_valid));
  1220. NakedDhcpv4Srv srv(0);
  1221. // This should fail (missing option definition)
  1222. EXPECT_NO_THROW(x = configureDhcp4Server(srv, json_bogus));
  1223. ASSERT_TRUE(x);
  1224. comment_ = isc::config::parseAnswer(rcode_, x);
  1225. ASSERT_EQ(1, rcode_);
  1226. // This should work (option definition present)
  1227. EXPECT_NO_THROW(x = configureDhcp4Server(srv, json_valid));
  1228. ASSERT_TRUE(x);
  1229. comment_ = isc::config::parseAnswer(rcode_, x);
  1230. ASSERT_EQ(0, rcode_);
  1231. }
  1232. /// Checks if DOCSIS client packets are classified properly
  1233. ///
  1234. /// @todo: With the change in #4626 the vendorClassSpecificProcessing
  1235. /// code was removed and replaced with generic classification. One day
  1236. /// we should rewrite this test to use classes. It would check that the
  1237. /// classification system can be used for docsis packets.
  1238. TEST_F(Dhcpv4SrvTest, DISABLED_docsisClientClassification) {
  1239. NakedDhcpv4Srv srv(0);
  1240. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  1241. // vendor-class set to docsis3.0
  1242. Pkt4Ptr dis1;
  1243. ASSERT_NO_THROW(dis1 = PktCaptures::captureRelayedDiscover());
  1244. ASSERT_NO_THROW(dis1->unpack());
  1245. srv.classifyPacket(dis1);
  1246. EXPECT_TRUE(dis1->inClass(srv.VENDOR_CLASS_PREFIX + "docsis3.0"));
  1247. EXPECT_FALSE(dis1->inClass(srv.VENDOR_CLASS_PREFIX + "eRouter1.0"));
  1248. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  1249. // vendor-class set to eRouter1.0
  1250. Pkt4Ptr dis2;
  1251. ASSERT_NO_THROW(dis2 = PktCaptures::captureRelayedDiscover2());
  1252. ASSERT_NO_THROW(dis2->unpack());
  1253. srv.classifyPacket(dis2);
  1254. EXPECT_TRUE(dis2->inClass(srv.VENDOR_CLASS_PREFIX + "eRouter1.0"));
  1255. EXPECT_FALSE(dis2->inClass(srv.VENDOR_CLASS_PREFIX + "docsis3.0"));
  1256. }
  1257. // Checks if client packets are classified properly using match expressions.
  1258. TEST_F(Dhcpv4SrvTest, matchClassification) {
  1259. IfaceMgrTestConfig test_config(true);
  1260. IfaceMgr::instance().openSockets4();
  1261. NakedDhcpv4Srv srv(0);
  1262. // The router class matches incoming packets with foo in a host-name
  1263. // option (code 12) and sets an ip-forwarding option in the response.
  1264. string config = "{ \"interfaces-config\": {"
  1265. " \"interfaces\": [ \"*\" ] }, "
  1266. "\"rebind-timer\": 2000, "
  1267. "\"renew-timer\": 1000, "
  1268. "\"valid-lifetime\": 4000, "
  1269. "\"subnet4\": [ "
  1270. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1271. " \"subnet\": \"192.0.2.0/24\" } ], "
  1272. "\"client-classes\": [ "
  1273. "{ \"name\": \"router\", "
  1274. " \"option-data\": ["
  1275. " { \"name\": \"ip-forwarding\", "
  1276. " \"data\": \"true\" } ], "
  1277. " \"test\": \"option[12].text == 'foo'\" } ] }";
  1278. ConstElementPtr json;
  1279. ASSERT_NO_THROW(json = parseDHCP4(config));
  1280. ConstElementPtr status;
  1281. // Configure the server and make sure the config is accepted
  1282. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1283. ASSERT_TRUE(status);
  1284. comment_ = config::parseAnswer(rcode_, status);
  1285. ASSERT_EQ(0, rcode_);
  1286. CfgMgr::instance().commit();
  1287. // Create packets with enough to select the subnet
  1288. OptionPtr clientid = generateClientId();
  1289. Pkt4Ptr query1(new Pkt4(DHCPDISCOVER, 1234));
  1290. query1->setRemoteAddr(IOAddress("192.0.2.1"));
  1291. query1->addOption(clientid);
  1292. query1->setIface("eth1");
  1293. Pkt4Ptr query2(new Pkt4(DHCPDISCOVER, 1234));
  1294. query2->setRemoteAddr(IOAddress("192.0.2.1"));
  1295. query2->addOption(clientid);
  1296. query2->setIface("eth1");
  1297. Pkt4Ptr query3(new Pkt4(DHCPDISCOVER, 1234));
  1298. query3->setRemoteAddr(IOAddress("192.0.2.1"));
  1299. query3->addOption(clientid);
  1300. query3->setIface("eth1");
  1301. // Create and add a PRL option to the first 2 queries
  1302. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1303. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1304. ASSERT_TRUE(prl);
  1305. prl->addValue(DHO_IP_FORWARDING);
  1306. query1->addOption(prl);
  1307. query2->addOption(prl);
  1308. // Create and add a host-name option to the first and last queries
  1309. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1310. ASSERT_TRUE(hostname);
  1311. query1->addOption(hostname);
  1312. query3->addOption(hostname);
  1313. // Classify packets
  1314. srv.classifyPacket(query1);
  1315. srv.classifyPacket(query2);
  1316. srv.classifyPacket(query3);
  1317. // Packets with the exception of the second should be in the router class
  1318. EXPECT_TRUE(query1->inClass("router"));
  1319. EXPECT_FALSE(query2->inClass("router"));
  1320. EXPECT_TRUE(query3->inClass("router"));
  1321. // Process queries
  1322. Pkt4Ptr response1 = srv.processDiscover(query1);
  1323. Pkt4Ptr response2 = srv.processDiscover(query2);
  1324. Pkt4Ptr response3 = srv.processDiscover(query3);
  1325. // Classification processing should add an ip-forwarding option
  1326. OptionPtr opt1 = response1->getOption(DHO_IP_FORWARDING);
  1327. EXPECT_TRUE(opt1);
  1328. // But only for the first query: second was not classified
  1329. OptionPtr opt2 = response2->getOption(DHO_IP_FORWARDING);
  1330. EXPECT_FALSE(opt2);
  1331. // But only for the first query: third has no PRL
  1332. OptionPtr opt3 = response3->getOption(DHO_IP_FORWARDING);
  1333. EXPECT_FALSE(opt3);
  1334. }
  1335. // Checks if client packets are classified properly using match expressions
  1336. // using option names
  1337. TEST_F(Dhcpv4SrvTest, matchClassificationOptionName) {
  1338. NakedDhcpv4Srv srv(0);
  1339. // The router class matches incoming packets with foo in a host-name
  1340. string config = "{ \"interfaces-config\": {"
  1341. " \"interfaces\": [ \"*\" ] }, "
  1342. "\"rebind-timer\": 2000, "
  1343. "\"renew-timer\": 1000, "
  1344. "\"valid-lifetime\": 4000, "
  1345. "\"subnet4\": [ "
  1346. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1347. " \"subnet\": \"192.0.2.0/24\" } ], "
  1348. "\"client-classes\": [ "
  1349. "{ \"name\": \"router\", "
  1350. " \"test\": \"option[host-name].text == 'foo'\" } ] }";
  1351. ConstElementPtr json;
  1352. ASSERT_NO_THROW(json = parseDHCP4(config));
  1353. ConstElementPtr status;
  1354. // Configure the server and make sure the config is accepted
  1355. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1356. ASSERT_TRUE(status);
  1357. comment_ = config::parseAnswer(rcode_, status);
  1358. ASSERT_EQ(0, rcode_);
  1359. CfgMgr::instance().commit();
  1360. // Create a packet with enough to select the subnet
  1361. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1362. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1363. // Create and add a host-name option to the query
  1364. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1365. ASSERT_TRUE(hostname);
  1366. query->addOption(hostname);
  1367. // Classify packets
  1368. srv.classifyPacket(query);
  1369. // The queey should be in the router class
  1370. EXPECT_TRUE(query->inClass("router"));
  1371. }
  1372. // Checks if client packets are classified properly using match expressions
  1373. // using option names and definitions
  1374. TEST_F(Dhcpv4SrvTest, matchClassificationOptionDef) {
  1375. NakedDhcpv4Srv srv(0);
  1376. // The router class matches incoming packets with foo in a defined
  1377. // option
  1378. string config = "{ \"interfaces-config\": {"
  1379. " \"interfaces\": [ \"*\" ] }, "
  1380. "\"rebind-timer\": 2000, "
  1381. "\"renew-timer\": 1000, "
  1382. "\"valid-lifetime\": 4000, "
  1383. "\"subnet4\": [ "
  1384. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1385. " \"subnet\": \"192.0.2.0/24\" } ], "
  1386. "\"client-classes\": [ "
  1387. "{ \"name\": \"router\", "
  1388. " \"test\": \"option[my-host-name].text == 'foo'\" } ], "
  1389. "\"option-def\": [ {"
  1390. " \"name\": \"my-host-name\", "
  1391. " \"code\": 250, "
  1392. " \"type\": \"string\" } ] }";
  1393. ConstElementPtr json;
  1394. ASSERT_NO_THROW(json = parseDHCP4(config));
  1395. ConstElementPtr status;
  1396. // Configure the server and make sure the config is accepted
  1397. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1398. ASSERT_TRUE(status);
  1399. comment_ = config::parseAnswer(rcode_, status);
  1400. ASSERT_EQ(0, rcode_);
  1401. CfgMgr::instance().commit();
  1402. // Create a packet with enough to select the subnet
  1403. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1404. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1405. // Create and add a my-host-name option to the query
  1406. OptionStringPtr hostname(new OptionString(Option::V4, 250, "foo"));
  1407. ASSERT_TRUE(hostname);
  1408. query->addOption(hostname);
  1409. // Classify packets
  1410. srv.classifyPacket(query);
  1411. // The queey should be in the router class
  1412. EXPECT_TRUE(query->inClass("router"));
  1413. }
  1414. // Checks subnet options have the priority over class options
  1415. TEST_F(Dhcpv4SrvTest, subnetClassPriority) {
  1416. IfaceMgrTestConfig test_config(true);
  1417. IfaceMgr::instance().openSockets4();
  1418. NakedDhcpv4Srv srv(0);
  1419. // Subnet sets an ip-forwarding option in the response.
  1420. // The router class matches incoming packets with foo in a host-name
  1421. // option (code 12) and sets an ip-forwarding option in the response.
  1422. string config = "{ \"interfaces-config\": {"
  1423. " \"interfaces\": [ \"*\" ] }, "
  1424. "\"rebind-timer\": 2000, "
  1425. "\"renew-timer\": 1000, "
  1426. "\"valid-lifetime\": 4000, "
  1427. "\"subnet4\": [ "
  1428. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1429. " \"subnet\": \"192.0.2.0/24\", "
  1430. " \"option-data\": ["
  1431. " { \"name\": \"ip-forwarding\", "
  1432. " \"data\": \"false\" } ] } ], "
  1433. "\"client-classes\": [ "
  1434. "{ \"name\": \"router\","
  1435. " \"option-data\": ["
  1436. " { \"name\": \"ip-forwarding\", "
  1437. " \"data\": \"true\" } ], "
  1438. " \"test\": \"option[12].text == 'foo'\" } ] }";
  1439. ConstElementPtr json;
  1440. ASSERT_NO_THROW(json = parseDHCP4(config));
  1441. ConstElementPtr status;
  1442. // Configure the server and make sure the config is accepted
  1443. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1444. ASSERT_TRUE(status);
  1445. comment_ = config::parseAnswer(rcode_, status);
  1446. ASSERT_EQ(0, rcode_);
  1447. CfgMgr::instance().commit();
  1448. // Create a packet with enough to select the subnet and go through
  1449. // the DISCOVER processing
  1450. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1451. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1452. OptionPtr clientid = generateClientId();
  1453. query->addOption(clientid);
  1454. query->setIface("eth1");
  1455. // Create and add a PRL option to the query
  1456. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1457. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1458. ASSERT_TRUE(prl);
  1459. prl->addValue(DHO_IP_FORWARDING);
  1460. query->addOption(prl);
  1461. // Create and add a host-name option to the query
  1462. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1463. ASSERT_TRUE(hostname);
  1464. query->addOption(hostname);
  1465. // Classify the packet
  1466. srv.classifyPacket(query);
  1467. // The packet should be in the router class
  1468. EXPECT_TRUE(query->inClass("router"));
  1469. // Process the query
  1470. Pkt4Ptr response = srv.processDiscover(query);
  1471. // Processing should add an ip-forwarding option
  1472. OptionPtr opt = response->getOption(DHO_IP_FORWARDING);
  1473. ASSERT_TRUE(opt);
  1474. ASSERT_GT(opt->len(), opt->getHeaderLen());
  1475. // Classification sets the value to true/1, subnet to false/0
  1476. // Here subnet has the priority
  1477. EXPECT_EQ(0, opt->getUint8());
  1478. }
  1479. // Checks subnet options have the priority over global options
  1480. TEST_F(Dhcpv4SrvTest, subnetGlobalPriority) {
  1481. IfaceMgrTestConfig test_config(true);
  1482. IfaceMgr::instance().openSockets4();
  1483. NakedDhcpv4Srv srv(0);
  1484. // Subnet and global set an ip-forwarding option in the response.
  1485. string config = "{ \"interfaces-config\": {"
  1486. " \"interfaces\": [ \"*\" ] }, "
  1487. "\"rebind-timer\": 2000, "
  1488. "\"renew-timer\": 1000, "
  1489. "\"valid-lifetime\": 4000, "
  1490. "\"subnet4\": [ "
  1491. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1492. " \"subnet\": \"192.0.2.0/24\", "
  1493. " \"option-data\": ["
  1494. " { \"name\": \"ip-forwarding\", "
  1495. " \"data\": \"false\" } ] } ], "
  1496. "\"option-data\": ["
  1497. " { \"name\": \"ip-forwarding\", "
  1498. " \"data\": \"true\" } ] }";
  1499. ConstElementPtr json;
  1500. ASSERT_NO_THROW(json = parseDHCP4(config));
  1501. ConstElementPtr status;
  1502. // Configure the server and make sure the config is accepted
  1503. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1504. ASSERT_TRUE(status);
  1505. comment_ = config::parseAnswer(rcode_, status);
  1506. ASSERT_EQ(0, rcode_);
  1507. CfgMgr::instance().commit();
  1508. // Create a packet with enough to select the subnet and go through
  1509. // the DISCOVER processing
  1510. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1511. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1512. OptionPtr clientid = generateClientId();
  1513. query->addOption(clientid);
  1514. query->setIface("eth1");
  1515. // Create and add a PRL option to the query
  1516. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1517. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1518. ASSERT_TRUE(prl);
  1519. prl->addValue(DHO_IP_FORWARDING);
  1520. query->addOption(prl);
  1521. // Create and add a host-name option to the query
  1522. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1523. ASSERT_TRUE(hostname);
  1524. query->addOption(hostname);
  1525. // Process the query
  1526. Pkt4Ptr response = srv.processDiscover(query);
  1527. // Processing should add an ip-forwarding option
  1528. OptionPtr opt = response->getOption(DHO_IP_FORWARDING);
  1529. ASSERT_TRUE(opt);
  1530. ASSERT_GT(opt->len(), opt->getHeaderLen());
  1531. // Global sets the value to true/1, subnet to false/0
  1532. // Here subnet has the priority
  1533. EXPECT_EQ(0, opt->getUint8());
  1534. }
  1535. // Checks class options have the priority over global options
  1536. TEST_F(Dhcpv4SrvTest, classGlobalPriority) {
  1537. IfaceMgrTestConfig test_config(true);
  1538. IfaceMgr::instance().openSockets4();
  1539. NakedDhcpv4Srv srv(0);
  1540. // A global ip-forwarding option is set in the response.
  1541. // The router class matches incoming packets with foo in a host-name
  1542. // option (code 12) and sets an ip-forwarding option in the response.
  1543. string config = "{ \"interfaces-config\": {"
  1544. " \"interfaces\": [ \"*\" ] }, "
  1545. "\"rebind-timer\": 2000, "
  1546. "\"renew-timer\": 1000, "
  1547. "\"valid-lifetime\": 4000, "
  1548. "\"subnet4\": [ "
  1549. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], "
  1550. " \"subnet\": \"192.0.2.0/24\" } ], "
  1551. "\"option-data\": ["
  1552. " { \"name\": \"ip-forwarding\", "
  1553. " \"data\": \"false\" } ], "
  1554. "\"client-classes\": [ "
  1555. "{ \"name\": \"router\","
  1556. " \"option-data\": ["
  1557. " { \"name\": \"ip-forwarding\", "
  1558. " \"data\": \"true\" } ], "
  1559. " \"test\": \"option[12].text == 'foo'\" } ] }";
  1560. ConstElementPtr json;
  1561. ASSERT_NO_THROW(json = parseDHCP4(config));
  1562. ConstElementPtr status;
  1563. // Configure the server and make sure the config is accepted
  1564. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1565. ASSERT_TRUE(status);
  1566. comment_ = config::parseAnswer(rcode_, status);
  1567. ASSERT_EQ(0, rcode_);
  1568. CfgMgr::instance().commit();
  1569. // Create a packet with enough to select the subnet and go through
  1570. // the DISCOVER processing
  1571. Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 1234));
  1572. query->setRemoteAddr(IOAddress("192.0.2.1"));
  1573. OptionPtr clientid = generateClientId();
  1574. query->addOption(clientid);
  1575. query->setIface("eth1");
  1576. // Create and add a PRL option to the query
  1577. OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
  1578. DHO_DHCP_PARAMETER_REQUEST_LIST));
  1579. ASSERT_TRUE(prl);
  1580. prl->addValue(DHO_IP_FORWARDING);
  1581. query->addOption(prl);
  1582. // Create and add a host-name option to the query
  1583. OptionStringPtr hostname(new OptionString(Option::V4, 12, "foo"));
  1584. ASSERT_TRUE(hostname);
  1585. query->addOption(hostname);
  1586. // Classify the packet
  1587. srv.classifyPacket(query);
  1588. // The packet should be in the router class
  1589. EXPECT_TRUE(query->inClass("router"));
  1590. // Process the query
  1591. Pkt4Ptr response = srv.processDiscover(query);
  1592. // Processing should add an ip-forwarding option
  1593. OptionPtr opt = response->getOption(DHO_IP_FORWARDING);
  1594. ASSERT_TRUE(opt);
  1595. ASSERT_GT(opt->len(), opt->getHeaderLen());
  1596. // Classification sets the value to true/1, global to false/0
  1597. // Here class has the priority
  1598. EXPECT_NE(0, opt->getUint8());
  1599. }
  1600. // Checks if the client-class field is indeed used for subnet selection.
  1601. // Note that packet classification is already checked in Dhcpv4SrvTest
  1602. // .*Classification above.
  1603. TEST_F(Dhcpv4SrvTest, clientClassify) {
  1604. // This test configures 2 subnets. We actually only need the
  1605. // first one, but since there's still this ugly hack that picks
  1606. // the pool if there is only one, we must use more than one
  1607. // subnet. That ugly hack will be removed in #3242, currently
  1608. // under review.
  1609. // The second subnet does not play any role here. The client's
  1610. // IP address belongs to the first subnet, so only that first
  1611. // subnet is being tested.
  1612. string config = "{ \"interfaces-config\": {"
  1613. " \"interfaces\": [ \"*\" ]"
  1614. "},"
  1615. "\"rebind-timer\": 2000, "
  1616. "\"renew-timer\": 1000, "
  1617. "\"subnet4\": [ "
  1618. "{ \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1619. " \"client-class\": \"foo\", "
  1620. " \"subnet\": \"192.0.2.0/24\" }, "
  1621. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  1622. " \"client-class\": \"xyzzy\", "
  1623. " \"subnet\": \"192.0.3.0/24\" } "
  1624. "],"
  1625. "\"valid-lifetime\": 4000 }";
  1626. ASSERT_NO_THROW(configure(config));
  1627. // Create a simple packet that we'll use for classification
  1628. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1629. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1630. dis->setCiaddr(IOAddress("192.0.2.1"));
  1631. dis->setIface("eth0");
  1632. OptionPtr clientid = generateClientId();
  1633. dis->addOption(clientid);
  1634. // This discover does not belong to foo class, so it will not
  1635. // be serviced
  1636. EXPECT_FALSE(srv_.selectSubnet(dis));
  1637. // Let's add the packet to bar class and try again.
  1638. dis->addClass("bar");
  1639. // Still not supported, because it belongs to wrong class.
  1640. EXPECT_FALSE(srv_.selectSubnet(dis));
  1641. // Let's add it to matching class.
  1642. dis->addClass("foo");
  1643. // This time it should work
  1644. EXPECT_TRUE(srv_.selectSubnet(dis));
  1645. }
  1646. // Checks if relay IP address specified in the relay-info structure in
  1647. // subnet4 is being used properly.
  1648. TEST_F(Dhcpv4SrvTest, relayOverride) {
  1649. // We have 2 subnets defined. Note that both have a relay address
  1650. // defined. Both are not belonging to the subnets. That is
  1651. // important, because if the relay belongs to the subnet, there's
  1652. // no need to specify relay override.
  1653. string config = "{ \"interfaces-config\": {"
  1654. " \"interfaces\": [ \"*\" ]"
  1655. "},"
  1656. "\"rebind-timer\": 2000, "
  1657. "\"renew-timer\": 1000, "
  1658. "\"subnet4\": [ "
  1659. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  1660. " \"relay\": { "
  1661. " \"ip-address\": \"192.0.5.1\""
  1662. " },"
  1663. " \"subnet\": \"192.0.2.0/24\" }, "
  1664. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  1665. " \"relay\": { "
  1666. " \"ip-address\": \"192.0.5.2\""
  1667. " },"
  1668. " \"subnet\": \"192.0.3.0/24\" } "
  1669. "],"
  1670. "\"valid-lifetime\": 4000 }";
  1671. // Use this config to set up the server
  1672. ASSERT_NO_THROW(configure(config));
  1673. // Let's get the subnet configuration objects
  1674. const Subnet4Collection* subnets =
  1675. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  1676. ASSERT_EQ(2, subnets->size());
  1677. // Let's get them for easy reference
  1678. Subnet4Ptr subnet1 = (*subnets)[0];
  1679. Subnet4Ptr subnet2 = (*subnets)[1];
  1680. ASSERT_TRUE(subnet1);
  1681. ASSERT_TRUE(subnet2);
  1682. // Let's create a packet.
  1683. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1684. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1685. dis->setIface("eth0");
  1686. dis->setHops(1);
  1687. OptionPtr clientid = generateClientId();
  1688. dis->addOption(clientid);
  1689. // This is just a sanity check, we're using regular method: ciaddr 192.0.2.1
  1690. // belongs to the first subnet, so it is selected
  1691. dis->setGiaddr(IOAddress("192.0.2.1"));
  1692. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  1693. // Relay belongs to the second subnet, so it should be selected.
  1694. dis->setGiaddr(IOAddress("192.0.3.1"));
  1695. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1696. // Now let's check if the relay override for the first subnets works
  1697. dis->setGiaddr(IOAddress("192.0.5.1"));
  1698. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  1699. // The same check for the second subnet...
  1700. dis->setGiaddr(IOAddress("192.0.5.2"));
  1701. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1702. // And finally, let's check if mis-matched relay address will end up
  1703. // in not selecting a subnet at all
  1704. dis->setGiaddr(IOAddress("192.0.5.3"));
  1705. EXPECT_FALSE(srv_.selectSubnet(dis));
  1706. // Finally, check that the relay override works only with relay address
  1707. // (GIADDR) and does not affect client address (CIADDR)
  1708. dis->setGiaddr(IOAddress("0.0.0.0"));
  1709. dis->setHops(0);
  1710. dis->setCiaddr(IOAddress("192.0.5.1"));
  1711. EXPECT_FALSE(srv_.selectSubnet(dis));
  1712. }
  1713. // Checks if relay IP address specified in the relay-info structure can be
  1714. // used together with client-classification.
  1715. TEST_F(Dhcpv4SrvTest, relayOverrideAndClientClass) {
  1716. // This test configures 2 subnets. They both are on the same link, so they
  1717. // have the same relay-ip address. Furthermore, the first subnet is
  1718. // reserved for clients that belong to class "foo".
  1719. string config = "{ \"interfaces-config\": {"
  1720. " \"interfaces\": [ \"*\" ]"
  1721. "},"
  1722. "\"rebind-timer\": 2000, "
  1723. "\"renew-timer\": 1000, "
  1724. "\"subnet4\": [ "
  1725. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  1726. " \"client-class\": \"foo\", "
  1727. " \"relay\": { "
  1728. " \"ip-address\": \"192.0.5.1\""
  1729. " },"
  1730. " \"subnet\": \"192.0.2.0/24\" }, "
  1731. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  1732. " \"relay\": { "
  1733. " \"ip-address\": \"192.0.5.1\""
  1734. " },"
  1735. " \"subnet\": \"192.0.3.0/24\" } "
  1736. "],"
  1737. "\"valid-lifetime\": 4000 }";
  1738. // Use this config to set up the server
  1739. ASSERT_NO_THROW(configure(config));
  1740. const Subnet4Collection* subnets =
  1741. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  1742. ASSERT_EQ(2, subnets->size());
  1743. // Let's get them for easy reference
  1744. Subnet4Ptr subnet1 = (*subnets)[0];
  1745. Subnet4Ptr subnet2 = (*subnets)[1];
  1746. ASSERT_TRUE(subnet1);
  1747. ASSERT_TRUE(subnet2);
  1748. // Let's create a packet.
  1749. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1750. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1751. dis->setIface("eth0");
  1752. dis->setHops(1);
  1753. dis->setGiaddr(IOAddress("192.0.5.1"));
  1754. OptionPtr clientid = generateClientId();
  1755. dis->addOption(clientid);
  1756. // This packet does not belong to class foo, so it should be rejected in
  1757. // subnet[0], even though the relay-ip matches. It should be accepted in
  1758. // subnet[1], because the subnet matches and there are no class
  1759. // requirements.
  1760. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1761. // Now let's add this packet to class foo and recheck. This time it should
  1762. // be accepted in the first subnet, because both class and relay-ip match.
  1763. dis->addClass("foo");
  1764. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  1765. }
  1766. // Checks if a RAI link selection sub-option works as expected
  1767. TEST_F(Dhcpv4SrvTest, relayLinkSelect) {
  1768. // We have 3 subnets defined.
  1769. string config = "{ \"interfaces-config\": {"
  1770. " \"interfaces\": [ \"*\" ]"
  1771. "},"
  1772. "\"rebind-timer\": 2000, "
  1773. "\"renew-timer\": 1000, "
  1774. "\"subnet4\": [ "
  1775. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  1776. " \"relay\": { "
  1777. " \"ip-address\": \"192.0.5.1\""
  1778. " },"
  1779. " \"subnet\": \"192.0.2.0/24\" }, "
  1780. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  1781. " \"subnet\": \"192.0.3.0/24\" }, "
  1782. "{ \"pools\": [ { \"pool\": \"192.0.4.1 - 192.0.4.100\" } ],"
  1783. " \"client-class\": \"foo\", "
  1784. " \"subnet\": \"192.0.4.0/24\" } "
  1785. "],"
  1786. "\"valid-lifetime\": 4000 }";
  1787. // Use this config to set up the server
  1788. ASSERT_NO_THROW(configure(config));
  1789. // Let's get the subnet configuration objects
  1790. const Subnet4Collection* subnets =
  1791. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  1792. ASSERT_EQ(3, subnets->size());
  1793. // Let's get them for easy reference
  1794. Subnet4Ptr subnet1 = (*subnets)[0];
  1795. Subnet4Ptr subnet2 = (*subnets)[1];
  1796. Subnet4Ptr subnet3 = (*subnets)[2];
  1797. ASSERT_TRUE(subnet1);
  1798. ASSERT_TRUE(subnet2);
  1799. ASSERT_TRUE(subnet3);
  1800. // Let's create a packet.
  1801. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1802. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1803. dis->setIface("eth0");
  1804. dis->setHops(1);
  1805. OptionPtr clientid = generateClientId();
  1806. dis->addOption(clientid);
  1807. // Let's create a Relay Agent Information option
  1808. OptionDefinitionPtr rai_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  1809. DHO_DHCP_AGENT_OPTIONS);
  1810. ASSERT_TRUE(rai_def);
  1811. OptionCustomPtr rai(new OptionCustom(*rai_def, Option::V4));
  1812. ASSERT_TRUE(rai);
  1813. IOAddress addr("192.0.3.2");
  1814. OptionPtr ols(new Option(Option::V4,
  1815. RAI_OPTION_LINK_SELECTION,
  1816. addr.toBytes()));
  1817. ASSERT_TRUE(ols);
  1818. rai->addOption(ols);
  1819. // This is just a sanity check, we're using regular method: ciaddr 192.0.3.1
  1820. // belongs to the second subnet, so it is selected
  1821. dis->setGiaddr(IOAddress("192.0.3.1"));
  1822. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1823. // Setup a relay override for the first subnet as it has a high precedence
  1824. dis->setGiaddr(IOAddress("192.0.5.1"));
  1825. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  1826. // Put a RAI to select back the second subnet as it has
  1827. // the highest precedence
  1828. dis->addOption(rai);
  1829. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1830. // Subnet select option has a lower precedence
  1831. OptionDefinitionPtr sbnsel_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  1832. DHO_SUBNET_SELECTION);
  1833. ASSERT_TRUE(sbnsel_def);
  1834. OptionCustomPtr sbnsel(new OptionCustom(*sbnsel_def, Option::V4));
  1835. ASSERT_TRUE(sbnsel);
  1836. sbnsel->writeAddress(IOAddress("192.0.2.3"));
  1837. dis->addOption(sbnsel);
  1838. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1839. dis->delOption(DHO_SUBNET_SELECTION);
  1840. // Check client-classification still applies
  1841. IOAddress addr_foo("192.0.4.2");
  1842. ols.reset(new Option(Option::V4, RAI_OPTION_LINK_SELECTION,
  1843. addr_foo.toBytes()));
  1844. rai->delOption(RAI_OPTION_LINK_SELECTION);
  1845. dis->delOption(DHO_DHCP_AGENT_OPTIONS);
  1846. rai->addOption(ols);
  1847. dis->addOption(rai);
  1848. // Note it shall fail (vs. try the next criterion).
  1849. EXPECT_FALSE(srv_.selectSubnet(dis));
  1850. // Add the packet to the class and check again: now it shall succeed
  1851. dis->addClass("foo");
  1852. EXPECT_TRUE(subnet3 == srv_.selectSubnet(dis));
  1853. // Check it fails with a bad address in the sub-option
  1854. IOAddress addr_bad("10.0.0.1");
  1855. ols.reset(new Option(Option::V4, RAI_OPTION_LINK_SELECTION,
  1856. addr_bad.toBytes()));
  1857. rai->delOption(RAI_OPTION_LINK_SELECTION);
  1858. dis->delOption(DHO_DHCP_AGENT_OPTIONS);
  1859. rai->addOption(ols);
  1860. dis->addOption(rai);
  1861. EXPECT_FALSE(srv_.selectSubnet(dis));
  1862. }
  1863. // Checks if a subnet selection option works as expected
  1864. TEST_F(Dhcpv4SrvTest, subnetSelect) {
  1865. // We have 3 subnets defined.
  1866. string config = "{ \"interfaces-config\": {"
  1867. " \"interfaces\": [ \"*\" ]"
  1868. "},"
  1869. "\"rebind-timer\": 2000, "
  1870. "\"renew-timer\": 1000, "
  1871. "\"subnet4\": [ "
  1872. "{ \"pools\": [ { \"pool\": \"192.0.2.2 - 192.0.2.100\" } ],"
  1873. " \"relay\": { "
  1874. " \"ip-address\": \"192.0.5.1\""
  1875. " },"
  1876. " \"subnet\": \"192.0.2.0/24\" }, "
  1877. "{ \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  1878. " \"subnet\": \"192.0.3.0/24\" }, "
  1879. "{ \"pools\": [ { \"pool\": \"192.0.4.1 - 192.0.4.100\" } ],"
  1880. " \"client-class\": \"foo\", "
  1881. " \"subnet\": \"192.0.4.0/24\" } "
  1882. "],"
  1883. "\"valid-lifetime\": 4000 }";
  1884. // Use this config to set up the server
  1885. ASSERT_NO_THROW(configure(config));
  1886. // Let's get the subnet configuration objects
  1887. const Subnet4Collection* subnets =
  1888. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  1889. ASSERT_EQ(3, subnets->size());
  1890. // Let's get them for easy reference
  1891. Subnet4Ptr subnet1 = (*subnets)[0];
  1892. Subnet4Ptr subnet2 = (*subnets)[1];
  1893. Subnet4Ptr subnet3 = (*subnets)[2];
  1894. ASSERT_TRUE(subnet1);
  1895. ASSERT_TRUE(subnet2);
  1896. ASSERT_TRUE(subnet3);
  1897. // Let's create a packet.
  1898. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1899. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1900. dis->setIface("eth0");
  1901. dis->setHops(1);
  1902. OptionPtr clientid = generateClientId();
  1903. dis->addOption(clientid);
  1904. // Let's create a Subnet Selection option
  1905. OptionDefinitionPtr sbnsel_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE,
  1906. DHO_SUBNET_SELECTION);
  1907. ASSERT_TRUE(sbnsel_def);
  1908. OptionCustomPtr sbnsel(new OptionCustom(*sbnsel_def, Option::V4));
  1909. ASSERT_TRUE(sbnsel);
  1910. sbnsel->writeAddress(IOAddress("192.0.3.2"));
  1911. // This is just a sanity check, we're using regular method: ciaddr 192.0.3.1
  1912. // belongs to the second subnet, so it is selected
  1913. dis->setGiaddr(IOAddress("192.0.3.1"));
  1914. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1915. // Setup a relay override for the first subnet as it has a high precedence
  1916. dis->setGiaddr(IOAddress("192.0.5.1"));
  1917. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  1918. // Put a subnet select option to select back the second subnet as
  1919. // it has the second highest precedence
  1920. dis->addOption(sbnsel);
  1921. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  1922. // Check client-classification still applies
  1923. sbnsel->writeAddress(IOAddress("192.0.4.2"));
  1924. // Note it shall fail (vs. try the next criterion).
  1925. EXPECT_FALSE(srv_.selectSubnet(dis));
  1926. // Add the packet to the class and check again: now it shall succeed
  1927. dis->addClass("foo");
  1928. EXPECT_TRUE(subnet3 == srv_.selectSubnet(dis));
  1929. // Check it fails with a bad address in the sub-option
  1930. sbnsel->writeAddress(IOAddress("10.0.0.1"));
  1931. EXPECT_FALSE(srv_.selectSubnet(dis));
  1932. }
  1933. // This test verifies that the direct message is dropped when it has been
  1934. // received by the server via an interface for which there is no subnet
  1935. // configured. It also checks that the message is not dropped (is processed)
  1936. // when it is relayed or unicast.
  1937. TEST_F(Dhcpv4SrvTest, acceptDirectRequest) {
  1938. IfaceMgrTestConfig test_config(true);
  1939. IfaceMgr::instance().openSockets4();
  1940. NakedDhcpv4Srv srv(0);
  1941. Pkt4Ptr pkt(new Pkt4(DHCPDISCOVER, 1234));
  1942. // Set Giaddr and local server's unicast address, but don't set hops.
  1943. // Hops value should not matter. The server will treat the message
  1944. // with the hops value of 0 and non-zero giaddr as relayed.
  1945. pkt->setGiaddr(IOAddress("192.0.10.1"));
  1946. pkt->setRemoteAddr(IOAddress("0.0.0.0"));
  1947. pkt->setLocalAddr(IOAddress("192.0.2.3"));
  1948. pkt->setIface("eth1");
  1949. EXPECT_TRUE(srv.accept(pkt));
  1950. // Let's set hops and check that the message is still accepted as
  1951. // a relayed message.
  1952. pkt->setHops(1);
  1953. EXPECT_TRUE(srv.accept(pkt));
  1954. // Make it a direct message but keep unicast server's address. The
  1955. // messages sent to unicast address should be accepted as they are
  1956. // most likely to renew existing leases. The server should respond
  1957. // to renews so they have to be accepted and processed.
  1958. pkt->setHops(0);
  1959. pkt->setGiaddr(IOAddress("0.0.0.0"));
  1960. EXPECT_TRUE(srv.accept(pkt));
  1961. // Direct message is now sent to a broadcast address. The server
  1962. // should accept this message because it has been received via
  1963. // eth1 for which there is a subnet configured (see test fixture
  1964. // class constructor).
  1965. pkt->setLocalAddr(IOAddress("255.255.255.255"));
  1966. EXPECT_TRUE(srv.accept(pkt));
  1967. // For eth0, there is no subnet configured. Such message is expected
  1968. // to be silently dropped.
  1969. pkt->setIface("eth0");
  1970. EXPECT_FALSE(srv.accept(pkt));
  1971. // But, if the message is unicast it should be accepted, even though
  1972. // it has been received via eth0.
  1973. pkt->setLocalAddr(IOAddress("10.0.0.1"));
  1974. EXPECT_TRUE(srv.accept(pkt));
  1975. // For the DHCPINFORM the ciaddr should be set or at least the source
  1976. // address.
  1977. pkt->setType(DHCPINFORM);
  1978. pkt->setRemoteAddr(IOAddress("10.0.0.101"));
  1979. EXPECT_TRUE(srv.accept(pkt));
  1980. // When neither ciaddr nor source address is present, the packet should
  1981. // be dropped.
  1982. pkt->setRemoteAddr(IOAddress("0.0.0.0"));
  1983. EXPECT_FALSE(srv.accept(pkt));
  1984. // When ciaddr is set, the packet should be accepted.
  1985. pkt->setCiaddr(IOAddress("10.0.0.1"));
  1986. EXPECT_TRUE(srv.accept(pkt));
  1987. }
  1988. // This test checks that the server rejects a message with invalid type.
  1989. TEST_F(Dhcpv4SrvTest, acceptMessageType) {
  1990. IfaceMgrTestConfig test_config(true);
  1991. IfaceMgr::instance().openSockets4();
  1992. NakedDhcpv4Srv srv(0);
  1993. // Specify messages to be accepted by the server.
  1994. int allowed[] = {
  1995. DHCPDISCOVER,
  1996. DHCPREQUEST,
  1997. DHCPRELEASE,
  1998. DHCPDECLINE,
  1999. DHCPINFORM
  2000. };
  2001. size_t allowed_size = sizeof(allowed) / sizeof(allowed[0]);
  2002. // Check that the server actually accepts these message types.
  2003. for (size_t i = 0; i < allowed_size; ++i) {
  2004. EXPECT_TRUE(srv.acceptMessageType(Pkt4Ptr(new Pkt4(allowed[i], 1234))))
  2005. << "Test failed for message type " << i;
  2006. }
  2007. // Specify messages which server is supposed to drop.
  2008. int not_allowed[] = {
  2009. DHCPOFFER,
  2010. DHCPACK,
  2011. DHCPNAK,
  2012. DHCPLEASEQUERY,
  2013. DHCPLEASEUNASSIGNED,
  2014. DHCPLEASEUNKNOWN,
  2015. DHCPLEASEACTIVE,
  2016. DHCPBULKLEASEQUERY,
  2017. DHCPLEASEQUERYDONE
  2018. };
  2019. size_t not_allowed_size = sizeof(not_allowed) / sizeof(not_allowed[0]);
  2020. // Actually check that the server will drop these messages.
  2021. for (size_t i = 0; i < not_allowed_size; ++i) {
  2022. EXPECT_FALSE(srv.acceptMessageType(Pkt4Ptr(new Pkt4(not_allowed[i],
  2023. 1234))))
  2024. << "Test failed for message type " << i;
  2025. }
  2026. }
  2027. // Test checks whether statistic is bumped up appropriately when Decline
  2028. // message is received.
  2029. TEST_F(Dhcpv4SrvTest, statisticsDecline) {
  2030. NakedDhcpv4Srv srv(0);
  2031. pretendReceivingPkt(srv, CONFIGS[0], DHCPDECLINE, "pkt4-decline-received");
  2032. }
  2033. // Test checks whether statistic is bumped up appropriately when Offer
  2034. // message is received (this should never happen in a sane network).
  2035. TEST_F(Dhcpv4SrvTest, statisticsOfferRcvd) {
  2036. NakedDhcpv4Srv srv(0);
  2037. pretendReceivingPkt(srv, CONFIGS[0], DHCPOFFER, "pkt4-offer-received");
  2038. }
  2039. // Test checks whether statistic is bumped up appropriately when Ack
  2040. // message is received (this should never happen in a sane network).
  2041. TEST_F(Dhcpv4SrvTest, statisticsAckRcvd) {
  2042. NakedDhcpv4Srv srv(0);
  2043. pretendReceivingPkt(srv, CONFIGS[0], DHCPACK, "pkt4-ack-received");
  2044. }
  2045. // Test checks whether statistic is bumped up appropriately when Nak
  2046. // message is received (this should never happen in a sane network).
  2047. TEST_F(Dhcpv4SrvTest, statisticsNakRcvd) {
  2048. NakedDhcpv4Srv srv(0);
  2049. pretendReceivingPkt(srv, CONFIGS[0], DHCPNAK, "pkt4-nak-received");
  2050. }
  2051. // Test checks whether statistic is bumped up appropriately when Release
  2052. // message is received.
  2053. TEST_F(Dhcpv4SrvTest, statisticsReleaseRcvd) {
  2054. NakedDhcpv4Srv srv(0);
  2055. pretendReceivingPkt(srv, CONFIGS[0], DHCPRELEASE, "pkt4-release-received");
  2056. }
  2057. // Test checks whether statistic is bumped up appropriately when unknown
  2058. // message is received.
  2059. TEST_F(Dhcpv4SrvTest, statisticsUnknownRcvd) {
  2060. NakedDhcpv4Srv srv(0);
  2061. pretendReceivingPkt(srv, CONFIGS[0], 200, "pkt4-unknown-received");
  2062. // There should also be pkt4-receive-drop stat bumped up
  2063. using namespace isc::stats;
  2064. StatsMgr& mgr = StatsMgr::instance();
  2065. ObservationPtr drop_stat = mgr.getObservation("pkt4-receive-drop");
  2066. // This statistic must be present and must be set to 1.
  2067. ASSERT_TRUE(drop_stat);
  2068. EXPECT_EQ(1, drop_stat->getInteger().first);
  2069. }
  2070. // This test verifies that the server is able to handle an empty client-id
  2071. // in incoming client message.
  2072. TEST_F(Dhcpv4SrvTest, emptyClientId) {
  2073. IfaceMgrTestConfig test_config(true);
  2074. IfaceMgr::instance().openSockets4();
  2075. Dhcp4Client client;
  2076. EXPECT_NO_THROW(configure(CONFIGS[0], *client.getServer()));
  2077. // Tell the client to not send client-id on its own.
  2078. client.includeClientId("");
  2079. // Instead, tell him to send this extra option, which happens to be
  2080. // an empty client-id.
  2081. OptionPtr empty_client_id(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER));
  2082. client.addExtraOption(empty_client_id);
  2083. // Let's check whether the server is able to process this packet without
  2084. // throwing any exceptions. We don't care whether the server sent any
  2085. // responses or not. The goal is to check that the server didn't throw
  2086. // any exceptions.
  2087. EXPECT_NO_THROW(client.doDORA());
  2088. }
  2089. // This test verifies that the server is able to handle too long client-id
  2090. // in incoming client message.
  2091. TEST_F(Dhcpv4SrvTest, tooLongClientId) {
  2092. IfaceMgrTestConfig test_config(true);
  2093. IfaceMgr::instance().openSockets4();
  2094. Dhcp4Client client;
  2095. EXPECT_NO_THROW(configure(CONFIGS[0], *client.getServer()));
  2096. // Tell the client to not send client-id on its own.
  2097. client.includeClientId("");
  2098. // Instead, tell him to send this extra option, which happens to be
  2099. // an empty client-id.
  2100. std::vector<uint8_t> data(250, 250);
  2101. OptionPtr long_client_id(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER,
  2102. data));
  2103. client.addExtraOption(long_client_id);
  2104. // Let's check whether the server is able to process this packet without
  2105. // throwing any exceptions. We don't care whether the server sent any
  2106. // responses or not. The goal is to check that the server didn't throw
  2107. // any exceptions.
  2108. EXPECT_NO_THROW(client.doDORA());
  2109. }
  2110. /// @todo: Implement proper tests for MySQL lease/host database,
  2111. /// see ticket #4214.
  2112. }; // end of anonymous namespace